blob: 6ff6ada1fde0b867c6fcec5198e877ff58dfe567 [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 Faulet5cb513a2020-05-13 17:56:56 +0200675
676 if (txn->http_reply) {
677 if (http_reply_message(s, txn->http_reply) == -1)
678 goto return_int_err;
679 goto return_prx_cond;
680 }
Christopher Fauletb8a53712019-12-16 11:29:38 +0100681 goto return_prx_err;
682
683 return_int_err:
684 txn->status = 500;
685 if (!(s->flags & SF_ERR_MASK))
686 s->flags |= SF_ERR_INTERNAL;
687 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100688 if (s->flags & SF_BE_ASSIGNED)
689 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100690 if (sess->listener->counters)
691 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
692 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200693
694 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200695 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100696 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200697 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100698 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100699 /* fall through */
700
701 return_prx_err:
702 http_reply_and_close(s, txn->status, http_error_message(s));
703 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200704
705 return_prx_cond:
706 if (!(s->flags & SF_ERR_MASK))
707 s->flags |= SF_ERR_PRXCOND;
708 if (!(s->flags & SF_FINST_MASK))
709 s->flags |= SF_FINST_R;
710
711 req->analysers &= AN_REQ_FLT_END;
712 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100713 DBG_TRACE_DEVEL("leaving on error",
714 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200715 return 0;
716
717 return_prx_yield:
718 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100719 DBG_TRACE_DEVEL("waiting for more data",
720 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200721 return 0;
722}
723
724/* This function performs all the processing enabled for the current request.
725 * It returns 1 if the processing can continue on next analysers, or zero if it
726 * needs more data, encounters an error, or wants to immediately abort the
727 * request. It relies on buffers flags, and updates s->req.analysers.
728 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200729int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200730{
731 struct session *sess = s->sess;
732 struct http_txn *txn = s->txn;
733 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200734 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200735 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
736
737 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
738 /* we need more data */
739 channel_dont_connect(req);
740 return 0;
741 }
742
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100743 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200744
745 /*
746 * Right now, we know that we have processed the entire headers
747 * and that unwanted requests have been filtered out. We can do
748 * whatever we want with the remaining request. Also, now we
749 * may have separate values for ->fe, ->be.
750 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100751 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200752
753 /*
754 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200755 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200756 */
757 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100758 struct htx_sl *sl;
759 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200760
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200761 if (!sockaddr_alloc(&s->target_addr)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200762 if (!(s->flags & SF_ERR_MASK))
763 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100764 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200765 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200766 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100767 uri = htx_sl_req_uri(sl);
768 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200769
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200770 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200771 goto return_bad_req;
772
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200773 s->target = &s->be->obj_type;
774 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
775
Christopher Faulete0768eb2018-10-03 16:38:02 +0200776 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200777 * uri.ptr and path.ptr (excluded). If it was not found, we need
778 * to replace from all the uri by a single "/".
779 *
780 * Instead of rewritting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100781 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200782 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200783 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100784 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200785 }
786
787 /*
788 * 7: Now we can work with the cookies.
789 * Note that doing so might move headers in the request, but
790 * the fields will stay coherent and the URI will not move.
791 * This should only be performed in the backend.
792 */
793 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200794 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200795
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100796 /* 8: Generate unique ID if a "unique-id-format" is defined.
797 *
798 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
799 * fetches only available in the HTTP request processing stage.
800 */
801 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100802 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200803
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100804 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100805 if (!(s->flags & SF_ERR_MASK))
806 s->flags |= SF_ERR_RESOURCE;
807 goto return_int_err;
808 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200809
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100810 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100811 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100812 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100813 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200814 }
815
816 /*
817 * 9: add X-Forwarded-For if either the frontend or the backend
818 * asks for it.
819 */
820 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200821 struct http_hdr_ctx ctx = { .blk = NULL };
822 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
823 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
824
Christopher Faulete0768eb2018-10-03 16:38:02 +0200825 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200826 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200827 /* The header is set to be added only if none is present
828 * and we found it, so don't do anything.
829 */
830 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200831 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200832 /* Add an X-Forwarded-For header unless the source IP is
833 * in the 'except' network range.
834 */
835 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200836 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200837 != sess->fe->except_net.s_addr) &&
838 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200839 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200840 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200841 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200842
843 /* Note: we rely on the backend to get the header name to be used for
844 * x-forwarded-for, because the header is really meant for the backends.
845 * However, if the backend did not specify any option, we have to rely
846 * on the frontend's header name.
847 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200848 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
849 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100850 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200851 }
852 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200853 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200854 /* FIXME: for the sake of completeness, we should also support
855 * 'except' here, although it is mostly useless in this case.
856 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200857 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200858
Christopher Faulete0768eb2018-10-03 16:38:02 +0200859 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200860 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200861 pn, sizeof(pn));
862
863 /* Note: we rely on the backend to get the header name to be used for
864 * x-forwarded-for, because the header is really meant for the backends.
865 * However, if the backend did not specify any option, we have to rely
866 * on the frontend's header name.
867 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200868 chunk_printf(&trash, "%s", pn);
869 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100870 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200871 }
872 }
873
874 /*
875 * 10: add X-Original-To if either the frontend or the backend
876 * asks for it.
877 */
878 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
879
880 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200881 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 +0200882 /* Add an X-Original-To header unless the destination IP is
883 * in the 'except' network range.
884 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200885 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200886 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200887 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200888 != sess->fe->except_to.s_addr) &&
889 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200890 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200891 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200892 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200893 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200894
895 /* Note: we rely on the backend to get the header name to be used for
896 * x-original-to, because the header is really meant for the backends.
897 * However, if the backend did not specify any option, we have to rely
898 * on the frontend's header name.
899 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200900 if (s->be->orgto_hdr_len)
901 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
902 else
903 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200904
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200905 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
906 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100907 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200908 }
909 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200910 }
911
Christopher Faulete0768eb2018-10-03 16:38:02 +0200912 /* If we have no server assigned yet and we're balancing on url_param
913 * with a POST request, we may be interested in checking the body for
914 * that parameter. This will be done in another analyser.
915 */
916 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100917 s->txn->meth == HTTP_METH_POST &&
918 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200919 channel_dont_connect(req);
920 req->analysers |= AN_REQ_HTTP_BODY;
921 }
922
923 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
924 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100925
Christopher Faulete0768eb2018-10-03 16:38:02 +0200926 /* We expect some data from the client. Unless we know for sure
927 * we already have a full request, we have to re-enable quick-ack
928 * in case we previously disabled it, otherwise we might cause
929 * the client to delay further data.
930 */
931 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200932 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100933 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200934
935 /*************************************************************
936 * OK, that's finished for the headers. We have done what we *
937 * could. Let's switch to the DATA state. *
938 ************************************************************/
939 req->analyse_exp = TICK_ETERNITY;
940 req->analysers &= ~an_bit;
941
942 s->logs.tv_request = now;
943 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100944 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200945 return 1;
946
Christopher Fauletb8a53712019-12-16 11:29:38 +0100947 return_int_err:
948 txn->status = 500;
949 if (!(s->flags & SF_ERR_MASK))
950 s->flags |= SF_ERR_INTERNAL;
951 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100952 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100953 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100954 if (sess->listener->counters)
955 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
956 goto return_prx_cond;
957
Christopher Faulete0768eb2018-10-03 16:38:02 +0200958 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200959 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100960 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200961 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100962 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100963 /* fall through */
964
965 return_prx_cond:
966 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200967
968 if (!(s->flags & SF_ERR_MASK))
969 s->flags |= SF_ERR_PRXCOND;
970 if (!(s->flags & SF_FINST_MASK))
971 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100972
973 req->analysers &= AN_REQ_FLT_END;
974 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100975 DBG_TRACE_DEVEL("leaving on error",
976 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200977 return 0;
978}
979
980/* This function is an analyser which processes the HTTP tarpit. It always
981 * returns zero, at the beginning because it prevents any other processing
982 * from occurring, and at the end because it terminates the request.
983 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200984int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200985{
986 struct http_txn *txn = s->txn;
987
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100988 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200989 /* This connection is being tarpitted. The CLIENT side has
990 * already set the connect expiration date to the right
991 * timeout. We just have to check that the client is still
992 * there and that the timeout has not expired.
993 */
994 channel_dont_connect(req);
995 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100996 !tick_is_expired(req->analyse_exp, now_ms)) {
997 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
998 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200999 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001000 }
1001
Christopher Faulete0768eb2018-10-03 16:38:02 +02001002
1003 /* We will set the queue timer to the time spent, just for
1004 * logging purposes. We fake a 500 server error, so that the
1005 * attacker will not suspect his connection has been tarpitted.
1006 * It will not cause trouble to the logs because we can exclude
1007 * the tarpitted connections by filtering on the 'PT' status flags.
1008 */
1009 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1010
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001011 if (req->flags & CF_READ_ERROR) {
1012 http_reply_and_close(s, txn->status, NULL);
1013 goto end;
1014 }
1015
1016 if (txn->http_reply) {
1017 if (!http_reply_message(s, txn->http_reply))
1018 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001019
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001020 txn->status = 500;
1021 if (!(s->flags & SF_ERR_MASK))
1022 s->flags |= SF_ERR_INTERNAL;
1023 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.internal_errors, 1);
1024 if (s->flags & SF_BE_ASSIGNED)
1025 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
1026 if (s->sess->listener->counters)
1027 _HA_ATOMIC_ADD(&s->sess->listener->counters->internal_errors, 1);
1028 }
1029
1030 http_reply_and_close(s, txn->status, http_error_message(s));
1031
1032 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001033 req->analysers &= AN_REQ_FLT_END;
1034 req->analyse_exp = TICK_ETERNITY;
1035
1036 if (!(s->flags & SF_ERR_MASK))
1037 s->flags |= SF_ERR_PRXCOND;
1038 if (!(s->flags & SF_FINST_MASK))
1039 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001040
1041 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001042 return 0;
1043}
1044
1045/* This function is an analyser which waits for the HTTP request body. It waits
1046 * for either the buffer to be full, or the full advertised contents to have
1047 * reached the buffer. It must only be called after the standard HTTP request
1048 * processing has occurred, because it expects the request to be parsed and will
1049 * look for the Expect header. It may send a 100-Continue interim response. It
1050 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
1051 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
1052 * needs to read more data, or 1 once it has completed its analysis.
1053 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001054int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001055{
1056 struct session *sess = s->sess;
1057 struct http_txn *txn = s->txn;
1058 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001059 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001060
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001061 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001062
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001063 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001064
Willy Tarreau4236f032019-03-05 10:43:32 +01001065 if (htx->flags & HTX_FL_PARSING_ERROR)
1066 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001067 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1068 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001069
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001070 if (msg->msg_state < HTTP_MSG_BODY)
1071 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001072
Christopher Faulete0768eb2018-10-03 16:38:02 +02001073 /* We have to parse the HTTP request body to find any required data.
1074 * "balance url_param check_post" should have been the only way to get
1075 * into this. We were brought here after HTTP header analysis, so all
1076 * related structures are ready.
1077 */
1078
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001079 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001080 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001081 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001082 }
1083
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001084 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001085
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001086 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1087 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001088 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001089 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001090 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001091 goto http_end;
1092
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001093 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001094 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1095 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001096 if (!(s->flags & SF_ERR_MASK))
1097 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001098 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1099 if (sess->listener->counters)
1100 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1101 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001102 }
1103
1104 /* we get here if we need to wait for more data */
1105 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1106 /* Not enough data. We'll re-use the http-request
1107 * timeout here. Ideally, we should set the timeout
1108 * relative to the accept() date. We just set the
1109 * request timeout once at the beginning of the
1110 * request.
1111 */
1112 channel_dont_connect(req);
1113 if (!tick_isset(req->analyse_exp))
1114 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001115 DBG_TRACE_DEVEL("waiting for more data",
1116 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001117 return 0;
1118 }
1119
1120 http_end:
1121 /* The situation will not evolve, so let's give up on the analysis. */
1122 s->logs.tv_request = now; /* update the request timer to reflect full request */
1123 req->analysers &= ~an_bit;
1124 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001125 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001126 return 1;
1127
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001128 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001129 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001130 if (!(s->flags & SF_ERR_MASK))
1131 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001132 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001133 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +01001134 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001135 if (sess->listener->counters)
1136 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
1137 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001138
Christopher Faulete0768eb2018-10-03 16:38:02 +02001139 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001140 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001141 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1142 if (sess->listener->counters)
1143 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1144 /* fall through */
1145
1146 return_prx_cond:
1147 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001148
1149 if (!(s->flags & SF_ERR_MASK))
1150 s->flags |= SF_ERR_PRXCOND;
1151 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001152 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001153
Christopher Faulete0768eb2018-10-03 16:38:02 +02001154 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001155 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001156 DBG_TRACE_DEVEL("leaving on error",
1157 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001158 return 0;
1159}
1160
1161/* This function is an analyser which forwards request body (including chunk
1162 * sizes if any). It is called as soon as we must forward, even if we forward
1163 * zero byte. The only situation where it must not be called is when we're in
1164 * tunnel mode and we want to forward till the close. It's used both to forward
1165 * remaining data and to resync after end of body. It expects the msg_state to
1166 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1167 * read more data, or 1 once we can go on with next request or end the stream.
1168 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1169 * bytes of pending data + the headers if not already done.
1170 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001171int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001172{
1173 struct session *sess = s->sess;
1174 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001175 struct http_msg *msg = &txn->req;
1176 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001177 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001178 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001179
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001180 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001181
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001182 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001183
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001184 if (htx->flags & HTX_FL_PARSING_ERROR)
1185 goto return_bad_req;
1186 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1187 goto return_int_err;
1188
Christopher Faulete0768eb2018-10-03 16:38:02 +02001189 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1190 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1191 /* Output closed while we were sending data. We must abort and
1192 * wake the other side up.
1193 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001194
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001195 /* Don't abort yet if we had L7 retries activated and it
1196 * was a write error, we may recover.
1197 */
1198 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001199 (s->si[1].flags & SI_FL_L7_RETRY)) {
1200 DBG_TRACE_DEVEL("leaving on L7 retry",
1201 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001202 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001203 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001204 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001205 http_end_request(s);
1206 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001207 DBG_TRACE_DEVEL("leaving on error",
1208 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001209 return 1;
1210 }
1211
1212 /* Note that we don't have to send 100-continue back because we don't
1213 * need the data to complete our job, and it's up to the server to
1214 * decide whether to return 100, 417 or anything else in return of
1215 * an "Expect: 100-continue" header.
1216 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001217 if (msg->msg_state == HTTP_MSG_BODY)
1218 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001219
Christopher Faulete0768eb2018-10-03 16:38:02 +02001220 /* in most states, we should abort in case of early close */
1221 channel_auto_close(req);
1222
1223 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001224 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001225 if (req->flags & CF_EOI)
1226 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001227 }
1228 else {
1229 /* We can't process the buffer's contents yet */
1230 req->flags |= CF_WAKE_WRITE;
1231 goto missing_data_or_waiting;
1232 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001233 }
1234
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001235 if (msg->msg_state >= HTTP_MSG_ENDING)
1236 goto ending;
1237
1238 if (txn->meth == HTTP_METH_CONNECT) {
1239 msg->msg_state = HTTP_MSG_ENDING;
1240 goto ending;
1241 }
1242
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001243 /* Forward input data. We get it by removing all outgoing data not
1244 * forwarded yet from HTX data size. If there are some data filters, we
1245 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001246 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001247 if (HAS_REQ_DATA_FILTERS(s)) {
1248 ret = flt_http_payload(s, msg, htx->data);
1249 if (ret < 0)
1250 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001251 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001252 }
1253 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001254 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001255 if (msg->flags & HTTP_MSGF_XFER_LEN)
1256 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001257 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001258
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001259 if (htx->data != co_data(req))
1260 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001261
Christopher Faulet9768c262018-10-22 09:34:31 +02001262 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001263 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1264 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001265 */
1266 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1267 goto missing_data_or_waiting;
1268
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001269 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001270
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001271 ending:
1272 /* other states, ENDING...TUNNEL */
1273 if (msg->msg_state >= HTTP_MSG_DONE)
1274 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001275
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001276 if (HAS_REQ_DATA_FILTERS(s)) {
1277 ret = flt_http_end(s, msg);
1278 if (ret <= 0) {
1279 if (!ret)
1280 goto missing_data_or_waiting;
1281 goto return_bad_req;
1282 }
1283 }
1284
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001285 if (txn->meth == HTTP_METH_CONNECT)
1286 msg->msg_state = HTTP_MSG_TUNNEL;
1287 else {
1288 msg->msg_state = HTTP_MSG_DONE;
1289 req->to_forward = 0;
1290 }
1291
1292 done:
1293 /* we don't want to forward closes on DONE except in tunnel mode. */
1294 if (!(txn->flags & TX_CON_WANT_TUN))
1295 channel_dont_close(req);
1296
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001297 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001298 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001299 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001300 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1301 if (req->flags & CF_SHUTW) {
1302 /* request errors are most likely due to the
1303 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001304 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001305 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001306 goto return_bad_req;
1307 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001308 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001309 return 1;
1310 }
1311
1312 /* If "option abortonclose" is set on the backend, we want to monitor
1313 * the client's connection and forward any shutdown notification to the
1314 * server, which will decide whether to close or to go on processing the
1315 * request. We only do that in tunnel mode, and not in other modes since
1316 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001317 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001318 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001319 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001320 s->si[1].flags |= SI_FL_NOLINGER;
1321 channel_auto_close(req);
1322 }
1323 else if (s->txn->meth == HTTP_METH_POST) {
1324 /* POST requests may require to read extra CRLF sent by broken
1325 * browsers and which could cause an RST to be sent upon close
1326 * on some systems (eg: Linux). */
1327 channel_auto_read(req);
1328 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001329 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1330 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001331 return 0;
1332
1333 missing_data_or_waiting:
1334 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001335 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001336 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001337
1338 waiting:
1339 /* waiting for the last bits to leave the buffer */
1340 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001341 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001342
1343 /* When TE: chunked is used, we need to get there again to parse remaining
1344 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1345 * And when content-length is used, we never want to let the possible
1346 * shutdown be forwarded to the other side, as the state machine will
1347 * take care of it once the client responds. It's also important to
1348 * prevent TIME_WAITs from accumulating on the backend side, and for
1349 * HTTP/2 where the last frame comes with a shutdown.
1350 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001351 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001352 channel_dont_close(req);
1353
1354 /* We know that more data are expected, but we couldn't send more that
1355 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1356 * system knows it must not set a PUSH on this first part. Interactive
1357 * modes are already handled by the stream sock layer. We must not do
1358 * this in content-length mode because it could present the MSG_MORE
1359 * flag with the last block of forwarded data, which would cause an
1360 * additional delay to be observed by the receiver.
1361 */
1362 if (msg->flags & HTTP_MSGF_TE_CHNK)
1363 req->flags |= CF_EXPECT_MORE;
1364
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001365 DBG_TRACE_DEVEL("waiting for more data to forward",
1366 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001367 return 0;
1368
Christopher Faulet93e02d82019-03-08 14:18:50 +01001369 return_cli_abort:
1370 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1371 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001372 if (sess->listener->counters)
1373 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001374 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001375 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001376 if (!(s->flags & SF_ERR_MASK))
1377 s->flags |= SF_ERR_CLICL;
1378 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001379 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001380
1381 return_srv_abort:
1382 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1383 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001384 if (sess->listener->counters)
1385 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001386 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001387 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001388 if (!(s->flags & SF_ERR_MASK))
1389 s->flags |= SF_ERR_SRVCL;
1390 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001391 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001392
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001393 return_int_err:
1394 if (!(s->flags & SF_ERR_MASK))
1395 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001396 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001397 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001398 if (sess->listener->counters)
1399 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001400 if (objt_server(s->target))
1401 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001402 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001403 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001404
Christopher Faulet93e02d82019-03-08 14:18:50 +01001405 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001406 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001407 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001408 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001409 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001410 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001411
Christopher Fauletb8a53712019-12-16 11:29:38 +01001412 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001413 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001414 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001415 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001416 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001417 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001418 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001419 }
1420 req->analysers &= AN_REQ_FLT_END;
1421 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 +01001422 if (!(s->flags & SF_ERR_MASK))
1423 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001424 if (!(s->flags & SF_FINST_MASK))
1425 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001426 DBG_TRACE_DEVEL("leaving on error ",
1427 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001428 return 0;
1429}
1430
Olivier Houcharda254a372019-04-05 15:30:12 +02001431/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1432/* Returns 0 if we can attempt to retry, -1 otherwise */
1433static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1434{
1435 struct channel *req, *res;
1436 int co_data;
1437
1438 si->conn_retries--;
1439 if (si->conn_retries < 0)
1440 return -1;
1441
Willy Tarreau223995e2019-05-04 10:38:31 +02001442 if (objt_server(s->target))
1443 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1444 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1445
Olivier Houcharda254a372019-04-05 15:30:12 +02001446 req = &s->req;
1447 res = &s->res;
1448 /* Remove any write error from the request, and read error from the response */
1449 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1450 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1451 res->analysers = 0;
1452 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001453 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001454 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001455 si->exp = TICK_ETERNITY;
1456 res->rex = TICK_ETERNITY;
1457 res->to_forward = 0;
1458 res->analyse_exp = TICK_ETERNITY;
1459 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001460 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001461 si_release_endpoint(&s->si[1]);
1462 b_free(&req->buf);
1463 /* Swap the L7 buffer with the channel buffer */
1464 /* We know we stored the co_data as b_data, so get it there */
1465 co_data = b_data(&si->l7_buffer);
1466 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1467 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1468
1469 co_set_data(req, co_data);
1470 b_reset(&res->buf);
1471 co_set_data(res, 0);
1472 return 0;
1473}
1474
Christopher Faulete0768eb2018-10-03 16:38:02 +02001475/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1476 * processing can continue on next analysers, or zero if it either needs more
1477 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1478 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1479 * when it has nothing left to do, and may remove any analyser when it wants to
1480 * abort.
1481 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001482int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001483{
Christopher Faulet9768c262018-10-22 09:34:31 +02001484 /*
1485 * We will analyze a complete HTTP response to check the its syntax.
1486 *
1487 * Once the start line and all headers are received, we may perform a
1488 * capture of the error (if any), and we will set a few fields. We also
1489 * logging and finally headers capture.
1490 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001491 struct session *sess = s->sess;
1492 struct http_txn *txn = s->txn;
1493 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001494 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001495 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001496 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001497 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001498 int n;
1499
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001500 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001501
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001502 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001503
Willy Tarreau4236f032019-03-05 10:43:32 +01001504 /* Parsing errors are caught here */
1505 if (htx->flags & HTX_FL_PARSING_ERROR)
1506 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001507 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1508 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001509
Christopher Faulete0768eb2018-10-03 16:38:02 +02001510 /*
1511 * Now we quickly check if we have found a full valid response.
1512 * If not so, we check the FD and buffer states before leaving.
1513 * A full response is indicated by the fact that we have seen
1514 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1515 * responses are checked first.
1516 *
1517 * Depending on whether the client is still there or not, we
1518 * may send an error response back or not. Note that normally
1519 * we should only check for HTTP status there, and check I/O
1520 * errors somewhere else.
1521 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001522 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001523 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001524 /* 1: have we encountered a read error ? */
1525 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001526 struct connection *conn = NULL;
1527
Olivier Houchard865d8392019-05-03 22:46:27 +02001528 if (objt_cs(s->si[1].end))
1529 conn = objt_cs(s->si[1].end)->conn;
1530
1531 if (si_b->flags & SI_FL_L7_RETRY &&
1532 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001533 /* If we arrive here, then CF_READ_ERROR was
1534 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001535 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001536 * the SI_FL_L7_RETRY flag, so it's ok not
1537 * to check s->be->retry_type.
1538 */
1539 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1540 return 0;
1541 }
1542
Olivier Houchard6db16992019-05-17 15:40:49 +02001543 if (txn->flags & TX_NOT_FIRST)
1544 goto abort_keep_alive;
1545
Olivier Houcharda798bf52019-03-08 18:52:00 +01001546 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001547 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001548 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001549 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001550 }
1551
Christopher Faulete0768eb2018-10-03 16:38:02 +02001552 rep->analysers &= AN_RES_FLT_END;
1553 txn->status = 502;
1554
1555 /* Check to see if the server refused the early data.
1556 * If so, just send a 425
1557 */
Olivier Houchard865d8392019-05-03 22:46:27 +02001558 if (conn->err_code == CO_ER_SSL_EARLY_FAILED) {
1559 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001560 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001561 do_l7_retry(s, si_b) == 0) {
1562 DBG_TRACE_DEVEL("leaving on L7 retry",
1563 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001564 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001565 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001566 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001567 }
1568
1569 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001570 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001571
1572 if (!(s->flags & SF_ERR_MASK))
1573 s->flags |= SF_ERR_SRVCL;
1574 if (!(s->flags & SF_FINST_MASK))
1575 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001576 DBG_TRACE_DEVEL("leaving on error",
1577 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001578 return 0;
1579 }
1580
Christopher Faulet9768c262018-10-22 09:34:31 +02001581 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001582 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001583 if ((si_b->flags & SI_FL_L7_RETRY) &&
1584 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001585 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1586 DBG_TRACE_DEVEL("leaving on L7 retry",
1587 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001588 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001589 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001590 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001591 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001592 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001593 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001594 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001595 }
1596
Christopher Faulete0768eb2018-10-03 16:38:02 +02001597 rep->analysers &= AN_RES_FLT_END;
1598 txn->status = 504;
1599 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001600 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001601
1602 if (!(s->flags & SF_ERR_MASK))
1603 s->flags |= SF_ERR_SRVTO;
1604 if (!(s->flags & SF_FINST_MASK))
1605 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001606 DBG_TRACE_DEVEL("leaving on error",
1607 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001608 return 0;
1609 }
1610
Christopher Faulet9768c262018-10-22 09:34:31 +02001611 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001612 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001613 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1614 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001615 if (sess->listener->counters)
1616 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001617 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001618 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001619
1620 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001621 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001622 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001623
1624 if (!(s->flags & SF_ERR_MASK))
1625 s->flags |= SF_ERR_CLICL;
1626 if (!(s->flags & SF_FINST_MASK))
1627 s->flags |= SF_FINST_H;
1628
1629 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001630 DBG_TRACE_DEVEL("leaving on error",
1631 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001632 return 0;
1633 }
1634
Christopher Faulet9768c262018-10-22 09:34:31 +02001635 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001636 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001637 if ((si_b->flags & SI_FL_L7_RETRY) &&
1638 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001639 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1640 DBG_TRACE_DEVEL("leaving on L7 retry",
1641 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001642 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001643 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001644 }
1645
Olivier Houchard6db16992019-05-17 15:40:49 +02001646 if (txn->flags & TX_NOT_FIRST)
1647 goto abort_keep_alive;
1648
Olivier Houcharda798bf52019-03-08 18:52:00 +01001649 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001650 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001651 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001652 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001653 }
1654
Christopher Faulete0768eb2018-10-03 16:38:02 +02001655 rep->analysers &= AN_RES_FLT_END;
1656 txn->status = 502;
1657 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001658 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001659
1660 if (!(s->flags & SF_ERR_MASK))
1661 s->flags |= SF_ERR_SRVCL;
1662 if (!(s->flags & SF_FINST_MASK))
1663 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001664 DBG_TRACE_DEVEL("leaving on error",
1665 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001666 return 0;
1667 }
1668
Christopher Faulet9768c262018-10-22 09:34:31 +02001669 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001670 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001671 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001672 goto abort_keep_alive;
1673
Olivier Houcharda798bf52019-03-08 18:52:00 +01001674 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001675 if (objt_server(s->target))
1676 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001677 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001678
1679 if (!(s->flags & SF_ERR_MASK))
1680 s->flags |= SF_ERR_CLICL;
1681 if (!(s->flags & SF_FINST_MASK))
1682 s->flags |= SF_FINST_H;
1683
1684 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001685 DBG_TRACE_DEVEL("leaving on error",
1686 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001687 return 0;
1688 }
1689
1690 channel_dont_close(rep);
1691 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001692 DBG_TRACE_DEVEL("waiting for more data",
1693 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001694 return 0;
1695 }
1696
1697 /* More interesting part now : we know that we have a complete
1698 * response which at least looks like HTTP. We have an indicator
1699 * of each header's length, so we can parse them quickly.
1700 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001701 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001702 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001703 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001704
Christopher Faulet9768c262018-10-22 09:34:31 +02001705 /* 0: we might have to print this header in debug mode */
1706 if (unlikely((global.mode & MODE_DEBUG) &&
1707 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1708 int32_t pos;
1709
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001710 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001711
Christopher Fauleta3f15502019-05-13 15:27:23 +02001712 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001713 struct htx_blk *blk = htx_get_blk(htx, pos);
1714 enum htx_blk_type type = htx_get_blk_type(blk);
1715
1716 if (type == HTX_BLK_EOH)
1717 break;
1718 if (type != HTX_BLK_HDR)
1719 continue;
1720
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001721 http_debug_hdr("srvhdr", s,
1722 htx_get_blk_name(htx, blk),
1723 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001724 }
1725 }
1726
Christopher Faulet03599112018-11-27 11:21:21 +01001727 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001728 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001729 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001730 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001731 if (sl->flags & HTX_SL_F_XFER_LEN) {
1732 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001733 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001734 if (sl->flags & HTX_SL_F_BODYLESS)
1735 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001736 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001737
1738 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001739 if (n < 1 || n > 5)
1740 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001741
Christopher Faulete0768eb2018-10-03 16:38:02 +02001742 /* when the client triggers a 4xx from the server, it's most often due
1743 * to a missing object or permission. These events should be tracked
1744 * because if they happen often, it may indicate a brute force or a
1745 * vulnerability scan.
1746 */
1747 if (n == 4)
1748 stream_inc_http_err_ctr(s);
1749
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001750 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001751 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001752 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1753 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001754
Christopher Faulete0768eb2018-10-03 16:38:02 +02001755 /* Adjust server's health based on status code. Note: status codes 501
1756 * and 505 are triggered on demand by client request, so we must not
1757 * count them as server failures.
1758 */
1759 if (objt_server(s->target)) {
1760 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001761 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001762 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001763 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001764 }
1765
1766 /*
1767 * We may be facing a 100-continue response, or any other informational
1768 * 1xx response which is non-final, in which case this is not the right
1769 * response, and we're waiting for the next one. Let's allow this response
1770 * to go to the client and wait for the next one. There's an exception for
1771 * 101 which is used later in the code to switch protocols.
1772 */
1773 if (txn->status < 200 &&
1774 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001775 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001776 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001777 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001778 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001779 txn->status = 0;
1780 s->logs.t_data = -1; /* was not a response yet */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001781 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001782 }
1783
1784 /*
1785 * 2: check for cacheability.
1786 */
1787
1788 switch (txn->status) {
1789 case 200:
1790 case 203:
1791 case 204:
1792 case 206:
1793 case 300:
1794 case 301:
1795 case 404:
1796 case 405:
1797 case 410:
1798 case 414:
1799 case 501:
1800 break;
1801 default:
1802 /* RFC7231#6.1:
1803 * Responses with status codes that are defined as
1804 * cacheable by default (e.g., 200, 203, 204, 206,
1805 * 300, 301, 404, 405, 410, 414, and 501 in this
1806 * specification) can be reused by a cache with
1807 * heuristic expiration unless otherwise indicated
1808 * by the method definition or explicit cache
1809 * controls [RFC7234]; all other status codes are
1810 * not cacheable by default.
1811 */
1812 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1813 break;
1814 }
1815
1816 /*
1817 * 3: we may need to capture headers
1818 */
1819 s->logs.logwait &= ~LW_RESP;
1820 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001821 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001822
Christopher Faulet9768c262018-10-22 09:34:31 +02001823 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001824 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1825 txn->status == 101)) {
1826 /* Either we've established an explicit tunnel, or we're
1827 * switching the protocol. In both cases, we're very unlikely
1828 * to understand the next protocols. We have to switch to tunnel
1829 * mode, so that we transfer the request and responses then let
1830 * this protocol pass unmodified. When we later implement specific
1831 * parsers for such protocols, we'll want to check the Upgrade
1832 * header which contains information about that protocol for
1833 * responses with status 101 (eg: see RFC2817 about TLS).
1834 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001835 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001836 }
1837
Christopher Faulet61608322018-11-23 16:23:45 +01001838 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1839 * 407 (Proxy-Authenticate) responses and set the connection to private
1840 */
1841 srv_conn = cs_conn(objt_cs(s->si[1].end));
1842 if (srv_conn) {
1843 struct ist hdr;
1844 struct http_hdr_ctx ctx;
1845
1846 if (txn->status == 401)
1847 hdr = ist("WWW-Authenticate");
1848 else if (txn->status == 407)
1849 hdr = ist("Proxy-Authenticate");
1850 else
1851 goto end;
1852
1853 ctx.blk = NULL;
1854 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001855 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1856 * possibly followed by blanks and a base64 string, the connection
1857 * is private. Since it's a mess to deal with, we only check for
1858 * values starting with "NTLM" or "Nego". Note that often multiple
1859 * headers are sent by the server there.
1860 */
1861 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001862 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001863 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet61608322018-11-23 16:23:45 +01001864 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001865 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001866 }
Christopher Faulet61608322018-11-23 16:23:45 +01001867 }
1868 }
1869
1870 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001871 /* we want to have the response time before we start processing it */
1872 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1873
1874 /* end of job, return OK */
1875 rep->analysers &= ~an_bit;
1876 rep->analyse_exp = TICK_ETERNITY;
1877 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001878 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001879 return 1;
1880
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001881 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001882 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001883 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001884 if (sess->listener->counters)
1885 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001886 if (objt_server(s->target))
1887 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001888 txn->status = 500;
1889 if (!(s->flags & SF_ERR_MASK))
1890 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001891 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001892
1893 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001894 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001895 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001896 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001897 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001898 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001899 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001900 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001901 do_l7_retry(s, si_b) == 0) {
1902 DBG_TRACE_DEVEL("leaving on L7 retry",
1903 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001904 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001905 }
Christopher Faulet47365272018-10-31 17:40:50 +01001906 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001907 /* fall through */
1908
Christopher Fauletb8a53712019-12-16 11:29:38 +01001909 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001910 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001911
1912 if (!(s->flags & SF_ERR_MASK))
1913 s->flags |= SF_ERR_PRXCOND;
1914 if (!(s->flags & SF_FINST_MASK))
1915 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001916
1917 s->si[1].flags |= SI_FL_NOLINGER;
1918 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001919 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001920 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001921 DBG_TRACE_DEVEL("leaving on error",
1922 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001923 return 0;
1924
Christopher Faulete0768eb2018-10-03 16:38:02 +02001925 abort_keep_alive:
1926 /* A keep-alive request to the server failed on a network error.
1927 * The client is required to retry. We need to close without returning
1928 * any other information so that the client retries.
1929 */
1930 txn->status = 0;
1931 rep->analysers &= AN_RES_FLT_END;
1932 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001933 s->logs.logwait = 0;
1934 s->logs.level = 0;
1935 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001936 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001937 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1938 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001939 return 0;
1940}
1941
1942/* This function performs all the processing enabled for the current response.
1943 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1944 * and updates s->res.analysers. It might make sense to explode it into several
1945 * other functions. It works like process_request (see indications above).
1946 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001947int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001948{
1949 struct session *sess = s->sess;
1950 struct http_txn *txn = s->txn;
1951 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001952 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001953 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001954 enum rule_result ret = HTTP_RULE_RES_CONT;
1955
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001956 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1957 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001958
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001959 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001960
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001961 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001962
1963 /* The stats applet needs to adjust the Connection header but we don't
1964 * apply any filter there.
1965 */
1966 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1967 rep->analysers &= ~an_bit;
1968 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001969 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001970 }
1971
1972 /*
1973 * We will have to evaluate the filters.
1974 * As opposed to version 1.2, now they will be evaluated in the
1975 * filters order and not in the header order. This means that
1976 * each filter has to be validated among all headers.
1977 *
1978 * Filters are tried with ->be first, then with ->fe if it is
1979 * different from ->be.
1980 *
1981 * Maybe we are in resume condiion. In this case I choose the
1982 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001983 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001984 * the process with the first one.
1985 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001986 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001987 * pointer and the ->fe rule list. If it doesn't match, I initialize
1988 * the loop with the ->be.
1989 */
1990 if (s->current_rule_list == &sess->fe->http_res_rules)
1991 cur_proxy = sess->fe;
1992 else
1993 cur_proxy = s->be;
1994 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001995 /* evaluate http-response rules */
1996 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001997 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001998
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001999 switch (ret) {
2000 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
2001 goto return_prx_yield;
2002
2003 case HTTP_RULE_RES_CONT:
2004 case HTTP_RULE_RES_STOP: /* nothing to do */
2005 break;
2006
2007 case HTTP_RULE_RES_DENY: /* deny or tarpit */
2008 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002009
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002010 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
2011 goto return_prx_cond;
2012
2013 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01002014 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002015
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002016 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2017 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002018
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002019 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
2020 goto return_int_err;
2021 }
2022
2023 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002024
Christopher Faulete0768eb2018-10-03 16:38:02 +02002025 /* check whether we're already working on the frontend */
2026 if (cur_proxy == sess->fe)
2027 break;
2028 cur_proxy = sess->fe;
2029 }
2030
Christopher Faulete0768eb2018-10-03 16:38:02 +02002031 /* OK that's all we can do for 1xx responses */
2032 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002033 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002034
2035 /*
2036 * Now check for a server cookie.
2037 */
2038 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002039 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002040
2041 /*
2042 * Check for cache-control or pragma headers if required.
2043 */
2044 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002045 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002046
2047 /*
2048 * Add server cookie in the response if needed
2049 */
2050 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
2051 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
2052 (!(s->flags & SF_DIRECT) ||
2053 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
2054 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
2055 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
2056 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
2057 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
2058 !(s->flags & SF_IGNORE_PRST)) {
2059 /* the server is known, it's not the one the client requested, or the
2060 * cookie's last seen date needs to be refreshed. We have to
2061 * insert a set-cookie here, except if we want to insert only on POST
2062 * requests and this one isn't. Note that servers which don't have cookies
2063 * (eg: some backup servers) will return a full cookie removal request.
2064 */
2065 if (!objt_server(s->target)->cookie) {
2066 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002067 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02002068 s->be->cookie_name);
2069 }
2070 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002071 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002072
2073 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2074 /* emit last_date, which is mandatory */
2075 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2076 s30tob64((date.tv_sec+3) >> 2,
2077 trash.area + trash.data);
2078 trash.data += 5;
2079
2080 if (s->be->cookie_maxlife) {
2081 /* emit first_date, which is either the original one or
2082 * the current date.
2083 */
2084 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2085 s30tob64(txn->cookie_first_date ?
2086 txn->cookie_first_date >> 2 :
2087 (date.tv_sec+3) >> 2,
2088 trash.area + trash.data);
2089 trash.data += 5;
2090 }
2091 }
2092 chunk_appendf(&trash, "; path=/");
2093 }
2094
2095 if (s->be->cookie_domain)
2096 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2097
2098 if (s->be->ck_opts & PR_CK_HTTPONLY)
2099 chunk_appendf(&trash, "; HttpOnly");
2100
2101 if (s->be->ck_opts & PR_CK_SECURE)
2102 chunk_appendf(&trash, "; Secure");
2103
Christopher Faulet2f533902020-01-21 11:06:48 +01002104 if (s->be->cookie_attrs)
2105 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
2106
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002107 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002108 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002109
2110 txn->flags &= ~TX_SCK_MASK;
2111 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2112 /* the server did not change, only the date was updated */
2113 txn->flags |= TX_SCK_UPDATED;
2114 else
2115 txn->flags |= TX_SCK_INSERTED;
2116
2117 /* Here, we will tell an eventual cache on the client side that we don't
2118 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2119 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2120 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2121 */
2122 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2123
2124 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2125
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002126 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002127 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002128 }
2129 }
2130
2131 /*
2132 * Check if result will be cacheable with a cookie.
2133 * We'll block the response if security checks have caught
2134 * nasty things such as a cacheable cookie.
2135 */
2136 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2137 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2138 (s->be->options & PR_O_CHK_CACHE)) {
2139 /* we're in presence of a cacheable response containing
2140 * a set-cookie header. We'll block it as requested by
2141 * the 'checkcache' option, and send an alert.
2142 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002143 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2144 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2145 send_log(s->be, LOG_ALERT,
2146 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2147 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002148 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002149 }
2150
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002151 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002152 /*
2153 * Evaluate after-response rules before forwarding the response. rules
2154 * from the backend are evaluated first, then one from the frontend if
2155 * it differs.
2156 */
2157 if (!http_eval_after_res_rules(s))
2158 goto return_int_err;
2159
Christopher Faulete0768eb2018-10-03 16:38:02 +02002160 /* Always enter in the body analyzer */
2161 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2162 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2163
2164 /* if the user wants to log as soon as possible, without counting
2165 * bytes from the server, then this is the right moment. We have
2166 * to temporarily assign bytes_out to log what we currently have.
2167 */
2168 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2169 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002170 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002171 s->do_log(s);
2172 s->logs.bytes_out = 0;
2173 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002174
Christopher Fauletb8a53712019-12-16 11:29:38 +01002175 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002176 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002177 rep->analysers &= ~an_bit;
2178 rep->analyse_exp = TICK_ETERNITY;
2179 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002180
Christopher Fauletb8a53712019-12-16 11:29:38 +01002181 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002182 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002183 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002184 if (sess->listener->counters)
2185 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002186 if (objt_server(s->target))
2187 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002188
2189 if (txn->http_reply) {
2190 if (http_reply_message(s, txn->http_reply) == -1)
2191 goto return_int_err;
2192 goto return_prx_cond;
2193 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002194 goto return_prx_err;
2195
2196 return_int_err:
2197 txn->status = 500;
2198 if (!(s->flags & SF_ERR_MASK))
2199 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002200 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002201 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2202 if (objt_server(s->target))
2203 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002204 if (objt_server(s->target))
2205 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002206 goto return_prx_err;
2207
2208 return_bad_res:
2209 txn->status = 502;
Christopher Fauleta20a6532020-02-05 10:16:41 +01002210 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2211 if (objt_server(s->target)) {
2212 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2213 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2214 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002215 /* fall through */
2216
2217 return_prx_err:
2218 http_reply_and_close(s, txn->status, http_error_message(s));
2219 /* fall through */
2220
2221 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002222 s->logs.t_data = -1; /* was not a valid response */
2223 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002224
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002225 if (!(s->flags & SF_ERR_MASK))
2226 s->flags |= SF_ERR_PRXCOND;
2227 if (!(s->flags & SF_FINST_MASK))
2228 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002229
Christopher Faulete58c0002020-03-02 16:21:01 +01002230 rep->analysers &= AN_RES_FLT_END;
2231 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002232 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002233 DBG_TRACE_DEVEL("leaving on error",
2234 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002235 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002236
2237 return_prx_yield:
2238 channel_dont_close(rep);
2239 DBG_TRACE_DEVEL("waiting for more data",
2240 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2241 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002242}
2243
2244/* This function is an analyser which forwards response body (including chunk
2245 * sizes if any). It is called as soon as we must forward, even if we forward
2246 * zero byte. The only situation where it must not be called is when we're in
2247 * tunnel mode and we want to forward till the close. It's used both to forward
2248 * remaining data and to resync after end of body. It expects the msg_state to
2249 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2250 * read more data, or 1 once we can go on with next request or end the stream.
2251 *
2252 * It is capable of compressing response data both in content-length mode and
2253 * in chunked mode. The state machines follows different flows depending on
2254 * whether content-length and chunked modes are used, since there are no
2255 * trailers in content-length :
2256 *
2257 * chk-mode cl-mode
2258 * ,----- BODY -----.
2259 * / \
2260 * V size > 0 V chk-mode
2261 * .--> SIZE -------------> DATA -------------> CRLF
2262 * | | size == 0 | last byte |
2263 * | v final crlf v inspected |
2264 * | TRAILERS -----------> DONE |
2265 * | |
2266 * `----------------------------------------------'
2267 *
2268 * Compression only happens in the DATA state, and must be flushed in final
2269 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2270 * is performed at once on final states for all bytes parsed, or when leaving
2271 * on missing data.
2272 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002273int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002274{
2275 struct session *sess = s->sess;
2276 struct http_txn *txn = s->txn;
2277 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002278 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002279 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002280
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002281 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002282
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002283 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002284
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002285 if (htx->flags & HTX_FL_PARSING_ERROR)
2286 goto return_bad_res;
2287 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2288 goto return_int_err;
2289
Christopher Faulete0768eb2018-10-03 16:38:02 +02002290 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002291 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002292 /* Output closed while we were sending data. We must abort and
2293 * wake the other side up.
2294 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002295 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002296 http_end_response(s);
2297 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002298 DBG_TRACE_DEVEL("leaving on error",
2299 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002300 return 1;
2301 }
2302
Christopher Faulet9768c262018-10-22 09:34:31 +02002303 if (msg->msg_state == HTTP_MSG_BODY)
2304 msg->msg_state = HTTP_MSG_DATA;
2305
Christopher Faulete0768eb2018-10-03 16:38:02 +02002306 /* in most states, we should abort in case of early close */
2307 channel_auto_close(res);
2308
Christopher Faulete0768eb2018-10-03 16:38:02 +02002309 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002310 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002311 if (res->flags & CF_EOI)
2312 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002313 }
2314 else {
2315 /* We can't process the buffer's contents yet */
2316 res->flags |= CF_WAKE_WRITE;
2317 goto missing_data_or_waiting;
2318 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002319 }
2320
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002321 if (msg->msg_state >= HTTP_MSG_ENDING)
2322 goto ending;
2323
2324 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2325 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2326 msg->msg_state = HTTP_MSG_ENDING;
2327 goto ending;
2328 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002329
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002330 /* Forward input data. We get it by removing all outgoing data not
2331 * forwarded yet from HTX data size. If there are some data filters, we
2332 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002333 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002334 if (HAS_RSP_DATA_FILTERS(s)) {
2335 ret = flt_http_payload(s, msg, htx->data);
2336 if (ret < 0)
2337 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002338 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002339 }
2340 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002341 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002342 if (msg->flags & HTTP_MSGF_XFER_LEN)
2343 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002344 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002345
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002346 if (htx->data != co_data(res))
2347 goto missing_data_or_waiting;
2348
2349 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2350 msg->msg_state = HTTP_MSG_ENDING;
2351 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002352 }
2353
Christopher Faulet9768c262018-10-22 09:34:31 +02002354 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002355 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2356 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002357 */
2358 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2359 goto missing_data_or_waiting;
2360
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002361 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002362
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002363 ending:
2364 /* other states, ENDING...TUNNEL */
2365 if (msg->msg_state >= HTTP_MSG_DONE)
2366 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002367
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002368 if (HAS_RSP_DATA_FILTERS(s)) {
2369 ret = flt_http_end(s, msg);
2370 if (ret <= 0) {
2371 if (!ret)
2372 goto missing_data_or_waiting;
2373 goto return_bad_res;
2374 }
2375 }
2376
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002377 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2378 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2379 msg->msg_state = HTTP_MSG_TUNNEL;
2380 goto ending;
2381 }
2382 else {
2383 msg->msg_state = HTTP_MSG_DONE;
2384 res->to_forward = 0;
2385 }
2386
2387 done:
2388
2389 channel_dont_close(res);
2390
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002391 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002392 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002393 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002394 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2395 if (res->flags & CF_SHUTW) {
2396 /* response errors are most likely due to the
2397 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002398 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002399 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002400 goto return_bad_res;
2401 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002402 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002403 return 1;
2404 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002405 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2406 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002407 return 0;
2408
2409 missing_data_or_waiting:
2410 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002411 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002412
2413 /* stop waiting for data if the input is closed before the end. If the
2414 * client side was already closed, it means that the client has aborted,
2415 * so we don't want to count this as a server abort. Otherwise it's a
2416 * server abort.
2417 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002418 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002419 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002420 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002421 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002422 if (htx_is_empty(htx))
2423 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002424 }
2425
Christopher Faulete0768eb2018-10-03 16:38:02 +02002426 /* When TE: chunked is used, we need to get there again to parse
2427 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002428 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2429 * are filters registered on the stream, we don't want to forward a
2430 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002431 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002432 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002433 channel_dont_close(res);
2434
2435 /* We know that more data are expected, but we couldn't send more that
2436 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2437 * system knows it must not set a PUSH on this first part. Interactive
2438 * modes are already handled by the stream sock layer. We must not do
2439 * this in content-length mode because it could present the MSG_MORE
2440 * flag with the last block of forwarded data, which would cause an
2441 * additional delay to be observed by the receiver.
2442 */
2443 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2444 res->flags |= CF_EXPECT_MORE;
2445
2446 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002447 DBG_TRACE_DEVEL("waiting for more data to forward",
2448 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002449 return 0;
2450
Christopher Faulet93e02d82019-03-08 14:18:50 +01002451 return_srv_abort:
2452 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2453 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002454 if (sess->listener->counters)
2455 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002456 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002457 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002458 if (!(s->flags & SF_ERR_MASK))
2459 s->flags |= SF_ERR_SRVCL;
2460 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002461
Christopher Faulet93e02d82019-03-08 14:18:50 +01002462 return_cli_abort:
2463 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2464 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002465 if (sess->listener->counters)
2466 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002467 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002468 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002469 if (!(s->flags & SF_ERR_MASK))
2470 s->flags |= SF_ERR_CLICL;
2471 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002472
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002473 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002474 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002475 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002476 if (sess->listener->counters)
2477 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002478 if (objt_server(s->target))
2479 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002480 if (!(s->flags & SF_ERR_MASK))
2481 s->flags |= SF_ERR_INTERNAL;
2482 goto return_error;
2483
Christopher Faulet93e02d82019-03-08 14:18:50 +01002484 return_bad_res:
2485 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2486 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002487 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002488 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2489 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002490 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002491 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002492 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002493
Christopher Faulet93e02d82019-03-08 14:18:50 +01002494 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002495 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002496 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002497 res->analysers &= AN_RES_FLT_END;
2498 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 +02002499 if (!(s->flags & SF_FINST_MASK))
2500 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002501 DBG_TRACE_DEVEL("leaving on error",
2502 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002503 return 0;
2504}
2505
Christopher Fauletf2824e62018-10-01 12:12:37 +02002506/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002507 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002508 * as too large a request to build a valid response.
2509 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002510int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002511{
Christopher Faulet99daf282018-11-28 22:58:13 +01002512 struct channel *req = &s->req;
2513 struct channel *res = &s->res;
2514 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002515 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002516 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002517 struct ist status, reason, location;
2518 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002519 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002520
2521 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002522 if (!chunk) {
2523 if (!(s->flags & SF_ERR_MASK))
2524 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002525 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002526 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002527
Christopher Faulet99daf282018-11-28 22:58:13 +01002528 /*
2529 * Create the location
2530 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002531 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002532 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002533 case REDIRECT_TYPE_SCHEME: {
2534 struct http_hdr_ctx ctx;
2535 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002536
Christopher Faulet99daf282018-11-28 22:58:13 +01002537 host = ist("");
2538 ctx.blk = NULL;
2539 if (http_find_header(htx, ist("Host"), &ctx, 0))
2540 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002541
Christopher Faulet297fbb42019-05-13 14:41:27 +02002542 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002543 path = http_get_path(htx_sl_req_uri(sl));
2544 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002545 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002546 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2547 int qs = 0;
2548 while (qs < path.len) {
2549 if (*(path.ptr + qs) == '?') {
2550 path.len = qs;
2551 break;
2552 }
2553 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002554 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002555 }
2556 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002557 else
2558 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002559
Christopher Faulet99daf282018-11-28 22:58:13 +01002560 if (rule->rdr_str) { /* this is an old "redirect" rule */
2561 /* add scheme */
2562 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2563 goto fail;
2564 }
2565 else {
2566 /* add scheme with executing log format */
2567 chunk->data += build_logline(s, chunk->area + chunk->data,
2568 chunk->size - chunk->data,
2569 &rule->rdr_fmt);
2570 }
2571 /* add "://" + host + path */
2572 if (!chunk_memcat(chunk, "://", 3) ||
2573 !chunk_memcat(chunk, host.ptr, host.len) ||
2574 !chunk_memcat(chunk, path.ptr, path.len))
2575 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002576
Christopher Faulet99daf282018-11-28 22:58:13 +01002577 /* append a slash at the end of the location if needed and missing */
2578 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2579 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2580 if (chunk->data + 1 >= chunk->size)
2581 goto fail;
2582 chunk->area[chunk->data++] = '/';
2583 }
2584 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002585 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002586
Christopher Faulet99daf282018-11-28 22:58:13 +01002587 case REDIRECT_TYPE_PREFIX: {
2588 struct ist path;
2589
Christopher Faulet297fbb42019-05-13 14:41:27 +02002590 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002591 path = http_get_path(htx_sl_req_uri(sl));
2592 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002593 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002594 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2595 int qs = 0;
2596 while (qs < path.len) {
2597 if (*(path.ptr + qs) == '?') {
2598 path.len = qs;
2599 break;
2600 }
2601 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002602 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002603 }
2604 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002605 else
2606 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002607
Christopher Faulet99daf282018-11-28 22:58:13 +01002608 if (rule->rdr_str) { /* this is an old "redirect" rule */
2609 /* add prefix. Note that if prefix == "/", we don't want to
2610 * add anything, otherwise it makes it hard for the user to
2611 * configure a self-redirection.
2612 */
2613 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2614 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2615 goto fail;
2616 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002617 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002618 else {
2619 /* add prefix with executing log format */
2620 chunk->data += build_logline(s, chunk->area + chunk->data,
2621 chunk->size - chunk->data,
2622 &rule->rdr_fmt);
2623 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002624
Christopher Faulet99daf282018-11-28 22:58:13 +01002625 /* add path */
2626 if (!chunk_memcat(chunk, path.ptr, path.len))
2627 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002628
Christopher Faulet99daf282018-11-28 22:58:13 +01002629 /* append a slash at the end of the location if needed and missing */
2630 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2631 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2632 if (chunk->data + 1 >= chunk->size)
2633 goto fail;
2634 chunk->area[chunk->data++] = '/';
2635 }
2636 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002637 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002638 case REDIRECT_TYPE_LOCATION:
2639 default:
2640 if (rule->rdr_str) { /* this is an old "redirect" rule */
2641 /* add location */
2642 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2643 goto fail;
2644 }
2645 else {
2646 /* add location with executing log format */
2647 chunk->data += build_logline(s, chunk->area + chunk->data,
2648 chunk->size - chunk->data,
2649 &rule->rdr_fmt);
2650 }
2651 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002652 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002653 location = ist2(chunk->area, chunk->data);
2654
2655 /*
2656 * Create the 30x response
2657 */
2658 switch (rule->code) {
2659 case 308:
2660 status = ist("308");
2661 reason = ist("Permanent Redirect");
2662 break;
2663 case 307:
2664 status = ist("307");
2665 reason = ist("Temporary Redirect");
2666 break;
2667 case 303:
2668 status = ist("303");
2669 reason = ist("See Other");
2670 break;
2671 case 301:
2672 status = ist("301");
2673 reason = ist("Moved Permanently");
2674 break;
2675 case 302:
2676 default:
2677 status = ist("302");
2678 reason = ist("Found");
2679 break;
2680 }
2681
Christopher Faulet08e66462019-05-23 16:44:59 +02002682 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2683 close = 1;
2684
Christopher Faulet99daf282018-11-28 22:58:13 +01002685 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002686 /* Trim any possible response */
2687 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002688 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2689 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2690 if (!sl)
2691 goto fail;
2692 sl->info.res.status = rule->code;
2693 s->txn->status = rule->code;
2694
Christopher Faulet08e66462019-05-23 16:44:59 +02002695 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2696 goto fail;
2697
2698 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002699 !htx_add_header(htx, ist("Location"), location))
2700 goto fail;
2701
2702 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2703 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2704 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002705 }
2706
2707 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002708 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2709 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002710 }
2711
Christopher Faulet99daf282018-11-28 22:58:13 +01002712 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2713 goto fail;
2714
Kevin Zhu96b36392020-01-07 09:42:55 +01002715 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002716 if (!http_forward_proxy_resp(s, 1))
2717 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002718
Christopher Faulet60b33a52020-01-28 09:18:10 +01002719 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2720 /* let's log the request time */
2721 s->logs.tv_request = now;
2722 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002723
Christopher Faulet60b33a52020-01-28 09:18:10 +01002724 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2725 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2726 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002727
2728 if (!(s->flags & SF_ERR_MASK))
2729 s->flags |= SF_ERR_LOCAL;
2730 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002731 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002732
Christopher Faulet99daf282018-11-28 22:58:13 +01002733 free_trash_chunk(chunk);
2734 return 1;
2735
2736 fail:
2737 /* If an error occurred, remove the incomplete HTTP response from the
2738 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002739 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002740 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002741 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002742}
2743
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002744/* Replace all headers matching the name <name>. The header value is replaced if
2745 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2746 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2747 * values are evaluated one by one. It returns 0 on success and -1 on error.
2748 */
2749int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2750 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002751{
2752 struct http_hdr_ctx ctx;
2753 struct buffer *output = get_trash_chunk();
2754
Christopher Faulet72333522018-10-24 11:25:02 +02002755 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002756 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002757 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2758 continue;
2759
2760 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2761 if (output->data == -1)
2762 return -1;
2763 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2764 return -1;
2765 }
2766 return 0;
2767}
2768
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002769/* This function executes one of the set-{method,path,query,uri} actions. It
2770 * takes the string from the variable 'replace' with length 'len', then modifies
2771 * the relevant part of the request line accordingly. Then it updates various
2772 * pointers to the next elements which were moved, and the total buffer length.
2773 * It finds the action to be performed in p[2], previously filled by function
2774 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2775 * error, though this can be revisited when this code is finally exploited.
2776 *
2777 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2778 * query string and 3 to replace uri.
2779 *
2780 * In query string case, the mark question '?' must be set at the start of the
2781 * string by the caller, event if the replacement query string is empty.
2782 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002783int http_req_replace_stline(int action, const char *replace, int len,
2784 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002785{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002786 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002787
2788 switch (action) {
2789 case 0: // method
2790 if (!http_replace_req_meth(htx, ist2(replace, len)))
2791 return -1;
2792 break;
2793
2794 case 1: // path
2795 if (!http_replace_req_path(htx, ist2(replace, len)))
2796 return -1;
2797 break;
2798
2799 case 2: // query
2800 if (!http_replace_req_query(htx, ist2(replace, len)))
2801 return -1;
2802 break;
2803
2804 case 3: // uri
2805 if (!http_replace_req_uri(htx, ist2(replace, len)))
2806 return -1;
2807 break;
2808
2809 default:
2810 return -1;
2811 }
2812 return 0;
2813}
2814
2815/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002816 * variable <status> contains the new status code. This function never fails. It
2817 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002818 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002819int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002820{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002821 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002822 char *res;
2823
2824 chunk_reset(&trash);
2825 res = ultoa_o(status, trash.area, trash.size);
2826 trash.data = res - trash.area;
2827
2828 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002829 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002830 const char *str = http_get_reason(status);
2831 reason = ist2(str, strlen(str));
2832 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002833
Christopher Faulete00d06c2019-12-16 17:18:42 +01002834 if (!http_replace_res_status(htx, ist2(trash.area, trash.data)))
2835 return -1;
Christopher Faulet96bff762019-12-17 13:46:18 +01002836 if (!http_replace_res_reason(htx, reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002837 return -1;
2838 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002839}
2840
Christopher Faulet3e964192018-10-24 11:39:23 +02002841/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2842 * transaction <txn>. Returns the verdict of the first rule that prevents
2843 * further processing of the request (auth, deny, ...), and defaults to
2844 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2845 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2846 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2847 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2848 * status.
2849 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002850static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002851 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002852{
2853 struct session *sess = strm_sess(s);
2854 struct http_txn *txn = s->txn;
2855 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002856 struct act_rule *rule;
2857 struct http_hdr_ctx ctx;
2858 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002859 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002860 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002861
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002862 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002863
2864 /* If "the current_rule_list" match the executed rule list, we are in
2865 * resume condition. If a resume is needed it is always in the action
2866 * and never in the ACL or converters. In this case, we initialise the
2867 * current rule, and go to the action execution point.
2868 */
2869 if (s->current_rule) {
2870 rule = s->current_rule;
2871 s->current_rule = NULL;
2872 if (s->current_rule_list == rules)
2873 goto resume_execution;
2874 }
2875 s->current_rule_list = rules;
2876
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002877 /* start the ruleset evaluation in strict mode */
2878 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002879
Christopher Faulet3e964192018-10-24 11:39:23 +02002880 list_for_each_entry(rule, rules, list) {
2881 /* check optional condition */
2882 if (rule->cond) {
2883 int ret;
2884
2885 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2886 ret = acl_pass(ret);
2887
2888 if (rule->cond->pol == ACL_COND_UNLESS)
2889 ret = !ret;
2890
2891 if (!ret) /* condition not matched */
2892 continue;
2893 }
2894
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002895 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002896 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002897 /* Always call the action function if defined */
2898 if (rule->action_ptr) {
2899 if ((s->req.flags & CF_READ_ERROR) ||
2900 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2901 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002902 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002903
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002904 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002905 case ACT_RET_CONT:
2906 break;
2907 case ACT_RET_STOP:
2908 rule_ret = HTTP_RULE_RES_STOP;
2909 goto end;
2910 case ACT_RET_YIELD:
2911 s->current_rule = rule;
2912 rule_ret = HTTP_RULE_RES_YIELD;
2913 goto end;
2914 case ACT_RET_ERR:
2915 rule_ret = HTTP_RULE_RES_ERROR;
2916 goto end;
2917 case ACT_RET_DONE:
2918 rule_ret = HTTP_RULE_RES_DONE;
2919 goto end;
2920 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002921 if (txn->status == -1)
2922 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002923 rule_ret = HTTP_RULE_RES_DENY;
2924 goto end;
2925 case ACT_RET_ABRT:
2926 rule_ret = HTTP_RULE_RES_ABRT;
2927 goto end;
2928 case ACT_RET_INV:
2929 rule_ret = HTTP_RULE_RES_BADREQ;
2930 goto end;
2931 }
2932 continue; /* eval the next rule */
2933 }
2934
2935 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002936 switch (rule->action) {
2937 case ACT_ACTION_ALLOW:
2938 rule_ret = HTTP_RULE_RES_STOP;
2939 goto end;
2940
2941 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002942 txn->status = rule->arg.http_reply->status;
2943 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002944 rule_ret = HTTP_RULE_RES_DENY;
2945 goto end;
2946
2947 case ACT_HTTP_REQ_TARPIT:
2948 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002949 txn->status = rule->arg.http_reply->status;
2950 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002951 rule_ret = HTTP_RULE_RES_DENY;
2952 goto end;
2953
2954 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002955 /* Auth might be performed on regular http-req rules as well as on stats */
Christopher Faulet96bff762019-12-17 13:46:18 +01002956 auth_realm = rule->arg.http.str.ptr;
Christopher Faulet3e964192018-10-24 11:39:23 +02002957 if (!auth_realm) {
2958 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2959 auth_realm = STATS_DEFAULT_REALM;
2960 else
2961 auth_realm = px->id;
2962 }
2963 /* send 401/407 depending on whether we use a proxy or not. We still
2964 * count one error, because normal browsing won't significantly
2965 * increase the counter but brute force attempts will.
2966 */
Christopher Faulet3e964192018-10-24 11:39:23 +02002967 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002968 if (http_reply_40x_unauthorized(s, auth_realm) == -1)
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002969 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet12c51e22018-11-28 15:59:42 +01002970 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02002971 goto end;
2972
2973 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002974 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002975 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002976 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002977 goto end;
2978
2979 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002980 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002981 break;
2982
2983 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002984 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002985 break;
2986
2987 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002988 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002989 break;
2990
2991 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002992 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002993 break;
2994
Christopher Faulet3e964192018-10-24 11:39:23 +02002995 case ACT_HTTP_DEL_HDR:
2996 /* remove all occurrences of the header */
2997 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01002998 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02002999 http_remove_header(htx, &ctx);
3000 break;
3001
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003002 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003003 default:
3004 break;
3005 }
3006 }
3007
3008 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003009 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003010 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003011 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003012
Christopher Faulet3e964192018-10-24 11:39:23 +02003013 /* we reached the end of the rules, nothing to report */
3014 return rule_ret;
3015}
3016
3017/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3018 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3019 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3020 * is returned, the process can continue the evaluation of next rule list. If
3021 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3022 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02003023 * must be returned. If *YIELD is returned, the caller must call again the
3024 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02003025 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003026static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
3027 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02003028{
3029 struct session *sess = strm_sess(s);
3030 struct http_txn *txn = s->txn;
3031 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02003032 struct act_rule *rule;
3033 struct http_hdr_ctx ctx;
3034 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003035 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02003036
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003037 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02003038
3039 /* If "the current_rule_list" match the executed rule list, we are in
3040 * resume condition. If a resume is needed it is always in the action
3041 * and never in the ACL or converters. In this case, we initialise the
3042 * current rule, and go to the action execution point.
3043 */
3044 if (s->current_rule) {
3045 rule = s->current_rule;
3046 s->current_rule = NULL;
3047 if (s->current_rule_list == rules)
3048 goto resume_execution;
3049 }
3050 s->current_rule_list = rules;
3051
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003052 /* start the ruleset evaluation in strict mode */
3053 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003054
Christopher Faulet3e964192018-10-24 11:39:23 +02003055 list_for_each_entry(rule, rules, list) {
3056 /* check optional condition */
3057 if (rule->cond) {
3058 int ret;
3059
3060 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3061 ret = acl_pass(ret);
3062
3063 if (rule->cond->pol == ACL_COND_UNLESS)
3064 ret = !ret;
3065
3066 if (!ret) /* condition not matched */
3067 continue;
3068 }
3069
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003070 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02003071resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003072
3073 /* Always call the action function if defined */
3074 if (rule->action_ptr) {
3075 if ((s->req.flags & CF_READ_ERROR) ||
3076 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3077 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003078 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003079
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003080 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003081 case ACT_RET_CONT:
3082 break;
3083 case ACT_RET_STOP:
3084 rule_ret = HTTP_RULE_RES_STOP;
3085 goto end;
3086 case ACT_RET_YIELD:
3087 s->current_rule = rule;
3088 rule_ret = HTTP_RULE_RES_YIELD;
3089 goto end;
3090 case ACT_RET_ERR:
3091 rule_ret = HTTP_RULE_RES_ERROR;
3092 goto end;
3093 case ACT_RET_DONE:
3094 rule_ret = HTTP_RULE_RES_DONE;
3095 goto end;
3096 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01003097 if (txn->status == -1)
3098 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003099 rule_ret = HTTP_RULE_RES_DENY;
3100 goto end;
3101 case ACT_RET_ABRT:
3102 rule_ret = HTTP_RULE_RES_ABRT;
3103 goto end;
3104 case ACT_RET_INV:
3105 rule_ret = HTTP_RULE_RES_BADREQ;
3106 goto end;
3107 }
3108 continue; /* eval the next rule */
3109 }
3110
3111 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003112 switch (rule->action) {
3113 case ACT_ACTION_ALLOW:
3114 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3115 goto end;
3116
3117 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02003118 txn->status = rule->arg.http_reply->status;
3119 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003120 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003121 goto end;
3122
3123 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003124 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003125 break;
3126
3127 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003128 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003129 break;
3130
3131 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003132 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003133 break;
3134
3135 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003136 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003137 break;
3138
Christopher Faulet3e964192018-10-24 11:39:23 +02003139 case ACT_HTTP_DEL_HDR:
3140 /* remove all occurrences of the header */
3141 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01003142 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02003143 http_remove_header(htx, &ctx);
3144 break;
3145
Christopher Faulet3e964192018-10-24 11:39:23 +02003146 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003147 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003148 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003149 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003150 goto end;
3151
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003152 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003153 default:
3154 break;
3155 }
3156 }
3157
3158 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003159 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003160 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003161 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003162
Christopher Faulet3e964192018-10-24 11:39:23 +02003163 /* we reached the end of the rules, nothing to report */
3164 return rule_ret;
3165}
3166
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003167/* Executes backend and frontend http-after-response rules for the stream <s>,
3168 * in that order. it return 1 on success and 0 on error. It is the caller
3169 * responsibility to catch error or ignore it. If it catches it, this function
3170 * may be called a second time, for the internal error.
3171 */
3172int http_eval_after_res_rules(struct stream *s)
3173{
3174 struct session *sess = s->sess;
3175 enum rule_result ret = HTTP_RULE_RES_CONT;
3176
3177 /* prune the request variables if not already done and swap to the response variables. */
3178 if (s->vars_reqres.scope != SCOPE_RES) {
3179 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3180 vars_prune(&s->vars_reqres, s->sess, s);
3181 vars_init(&s->vars_reqres, SCOPE_RES);
3182 }
3183
3184 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3185 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3186 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3187
3188 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3189 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3190}
3191
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003192/*
3193 * Manage client-side cookie. It can impact performance by about 2% so it is
3194 * desirable to call it only when needed. This code is quite complex because
3195 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3196 * highly recommended not to touch this part without a good reason !
3197 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003198static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003199{
3200 struct session *sess = s->sess;
3201 struct http_txn *txn = s->txn;
3202 struct htx *htx;
3203 struct http_hdr_ctx ctx;
3204 char *hdr_beg, *hdr_end, *del_from;
3205 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3206 int preserve_hdr;
3207
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003208 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003209 ctx.blk = NULL;
3210 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003211 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003212 del_from = NULL; /* nothing to be deleted */
3213 preserve_hdr = 0; /* assume we may kill the whole header */
3214
3215 /* Now look for cookies. Conforming to RFC2109, we have to support
3216 * attributes whose name begin with a '$', and associate them with
3217 * the right cookie, if we want to delete this cookie.
3218 * So there are 3 cases for each cookie read :
3219 * 1) it's a special attribute, beginning with a '$' : ignore it.
3220 * 2) it's a server id cookie that we *MAY* want to delete : save
3221 * some pointers on it (last semi-colon, beginning of cookie...)
3222 * 3) it's an application cookie : we *MAY* have to delete a previous
3223 * "special" cookie.
3224 * At the end of loop, if a "special" cookie remains, we may have to
3225 * remove it. If no application cookie persists in the header, we
3226 * *MUST* delete it.
3227 *
3228 * Note: RFC2965 is unclear about the processing of spaces around
3229 * the equal sign in the ATTR=VALUE form. A careful inspection of
3230 * the RFC explicitly allows spaces before it, and not within the
3231 * tokens (attrs or values). An inspection of RFC2109 allows that
3232 * too but section 10.1.3 lets one think that spaces may be allowed
3233 * after the equal sign too, resulting in some (rare) buggy
3234 * implementations trying to do that. So let's do what servers do.
3235 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3236 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003237 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003238 * causes parsing to become ambiguous. Browsers also allow spaces
3239 * within values even without quotes.
3240 *
3241 * We have to keep multiple pointers in order to support cookie
3242 * removal at the beginning, middle or end of header without
3243 * corrupting the header. All of these headers are valid :
3244 *
3245 * hdr_beg hdr_end
3246 * | |
3247 * v |
3248 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3249 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3250 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3251 * | | | | | | |
3252 * | | | | | | |
3253 * | | | | | | +--> next
3254 * | | | | | +----> val_end
3255 * | | | | +-----------> val_beg
3256 * | | | +--------------> equal
3257 * | | +----------------> att_end
3258 * | +---------------------> att_beg
3259 * +--------------------------> prev
3260 *
3261 */
3262 hdr_beg = ctx.value.ptr;
3263 hdr_end = hdr_beg + ctx.value.len;
3264 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3265 /* Iterate through all cookies on this line */
3266
3267 /* find att_beg */
3268 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003269 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003270 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003271 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003272
3273 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3274 att_beg++;
3275
3276 /* find att_end : this is the first character after the last non
3277 * space before the equal. It may be equal to hdr_end.
3278 */
3279 equal = att_end = att_beg;
3280 while (equal < hdr_end) {
3281 if (*equal == '=' || *equal == ',' || *equal == ';')
3282 break;
3283 if (HTTP_IS_SPHT(*equal++))
3284 continue;
3285 att_end = equal;
3286 }
3287
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003288 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003289 * is between <att_beg> and <equal>, both may be identical.
3290 */
3291 /* look for end of cookie if there is an equal sign */
3292 if (equal < hdr_end && *equal == '=') {
3293 /* look for the beginning of the value */
3294 val_beg = equal + 1;
3295 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3296 val_beg++;
3297
3298 /* find the end of the value, respecting quotes */
3299 next = http_find_cookie_value_end(val_beg, hdr_end);
3300
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003301 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003302 val_end = next;
3303 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3304 val_end--;
3305 }
3306 else
3307 val_beg = val_end = next = equal;
3308
3309 /* We have nothing to do with attributes beginning with
3310 * '$'. However, they will automatically be removed if a
3311 * header before them is removed, since they're supposed
3312 * to be linked together.
3313 */
3314 if (*att_beg == '$')
3315 continue;
3316
3317 /* Ignore cookies with no equal sign */
3318 if (equal == next) {
3319 /* This is not our cookie, so we must preserve it. But if we already
3320 * scheduled another cookie for removal, we cannot remove the
3321 * complete header, but we can remove the previous block itself.
3322 */
3323 preserve_hdr = 1;
3324 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003325 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003326 val_end += delta;
3327 next += delta;
3328 hdr_end += delta;
3329 prev = del_from;
3330 del_from = NULL;
3331 }
3332 continue;
3333 }
3334
3335 /* if there are spaces around the equal sign, we need to
3336 * strip them otherwise we'll get trouble for cookie captures,
3337 * or even for rewrites. Since this happens extremely rarely,
3338 * it does not hurt performance.
3339 */
3340 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3341 int stripped_before = 0;
3342 int stripped_after = 0;
3343
3344 if (att_end != equal) {
3345 memmove(att_end, equal, hdr_end - equal);
3346 stripped_before = (att_end - equal);
3347 equal += stripped_before;
3348 val_beg += stripped_before;
3349 }
3350
3351 if (val_beg > equal + 1) {
3352 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3353 stripped_after = (equal + 1) - val_beg;
3354 val_beg += stripped_after;
3355 stripped_before += stripped_after;
3356 }
3357
3358 val_end += stripped_before;
3359 next += stripped_before;
3360 hdr_end += stripped_before;
3361 }
3362 /* now everything is as on the diagram above */
3363
3364 /* First, let's see if we want to capture this cookie. We check
3365 * that we don't already have a client side cookie, because we
3366 * can only capture one. Also as an optimisation, we ignore
3367 * cookies shorter than the declared name.
3368 */
3369 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3370 (val_end - att_beg >= sess->fe->capture_namelen) &&
3371 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3372 int log_len = val_end - att_beg;
3373
3374 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3375 ha_alert("HTTP logging : out of memory.\n");
3376 } else {
3377 if (log_len > sess->fe->capture_len)
3378 log_len = sess->fe->capture_len;
3379 memcpy(txn->cli_cookie, att_beg, log_len);
3380 txn->cli_cookie[log_len] = 0;
3381 }
3382 }
3383
3384 /* Persistence cookies in passive, rewrite or insert mode have the
3385 * following form :
3386 *
3387 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3388 *
3389 * For cookies in prefix mode, the form is :
3390 *
3391 * Cookie: NAME=SRV~VALUE
3392 */
3393 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3394 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3395 struct server *srv = s->be->srv;
3396 char *delim;
3397
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003398 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003399 * have the server ID between val_beg and delim, and the original cookie between
3400 * delim+1 and val_end. Otherwise, delim==val_end :
3401 *
3402 * hdr_beg
3403 * |
3404 * v
3405 * NAME=SRV; # in all but prefix modes
3406 * NAME=SRV~OPAQUE ; # in prefix mode
3407 * || || | |+-> next
3408 * || || | +--> val_end
3409 * || || +---------> delim
3410 * || |+------------> val_beg
3411 * || +-------------> att_end = equal
3412 * |+-----------------> att_beg
3413 * +------------------> prev
3414 *
3415 */
3416 if (s->be->ck_opts & PR_CK_PFX) {
3417 for (delim = val_beg; delim < val_end; delim++)
3418 if (*delim == COOKIE_DELIM)
3419 break;
3420 }
3421 else {
3422 char *vbar1;
3423 delim = val_end;
3424 /* Now check if the cookie contains a date field, which would
3425 * appear after a vertical bar ('|') just after the server name
3426 * and before the delimiter.
3427 */
3428 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3429 if (vbar1) {
3430 /* OK, so left of the bar is the server's cookie and
3431 * right is the last seen date. It is a base64 encoded
3432 * 30-bit value representing the UNIX date since the
3433 * epoch in 4-second quantities.
3434 */
3435 int val;
3436 delim = vbar1++;
3437 if (val_end - vbar1 >= 5) {
3438 val = b64tos30(vbar1);
3439 if (val > 0)
3440 txn->cookie_last_date = val << 2;
3441 }
3442 /* look for a second vertical bar */
3443 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3444 if (vbar1 && (val_end - vbar1 > 5)) {
3445 val = b64tos30(vbar1 + 1);
3446 if (val > 0)
3447 txn->cookie_first_date = val << 2;
3448 }
3449 }
3450 }
3451
3452 /* if the cookie has an expiration date and the proxy wants to check
3453 * it, then we do that now. We first check if the cookie is too old,
3454 * then only if it has expired. We detect strict overflow because the
3455 * time resolution here is not great (4 seconds). Cookies with dates
3456 * in the future are ignored if their offset is beyond one day. This
3457 * allows an admin to fix timezone issues without expiring everyone
3458 * and at the same time avoids keeping unwanted side effects for too
3459 * long.
3460 */
3461 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3462 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3463 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3464 txn->flags &= ~TX_CK_MASK;
3465 txn->flags |= TX_CK_OLD;
3466 delim = val_beg; // let's pretend we have not found the cookie
3467 txn->cookie_first_date = 0;
3468 txn->cookie_last_date = 0;
3469 }
3470 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3471 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3472 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3473 txn->flags &= ~TX_CK_MASK;
3474 txn->flags |= TX_CK_EXPIRED;
3475 delim = val_beg; // let's pretend we have not found the cookie
3476 txn->cookie_first_date = 0;
3477 txn->cookie_last_date = 0;
3478 }
3479
3480 /* Here, we'll look for the first running server which supports the cookie.
3481 * This allows to share a same cookie between several servers, for example
3482 * to dedicate backup servers to specific servers only.
3483 * However, to prevent clients from sticking to cookie-less backup server
3484 * when they have incidentely learned an empty cookie, we simply ignore
3485 * empty cookies and mark them as invalid.
3486 * The same behaviour is applied when persistence must be ignored.
3487 */
3488 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3489 srv = NULL;
3490
3491 while (srv) {
3492 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3493 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3494 if ((srv->cur_state != SRV_ST_STOPPED) ||
3495 (s->be->options & PR_O_PERSIST) ||
3496 (s->flags & SF_FORCE_PRST)) {
3497 /* we found the server and we can use it */
3498 txn->flags &= ~TX_CK_MASK;
3499 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3500 s->flags |= SF_DIRECT | SF_ASSIGNED;
3501 s->target = &srv->obj_type;
3502 break;
3503 } else {
3504 /* we found a server, but it's down,
3505 * mark it as such and go on in case
3506 * another one is available.
3507 */
3508 txn->flags &= ~TX_CK_MASK;
3509 txn->flags |= TX_CK_DOWN;
3510 }
3511 }
3512 srv = srv->next;
3513 }
3514
3515 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3516 /* no server matched this cookie or we deliberately skipped it */
3517 txn->flags &= ~TX_CK_MASK;
3518 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3519 txn->flags |= TX_CK_UNUSED;
3520 else
3521 txn->flags |= TX_CK_INVALID;
3522 }
3523
3524 /* depending on the cookie mode, we may have to either :
3525 * - delete the complete cookie if we're in insert+indirect mode, so that
3526 * the server never sees it ;
3527 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003528 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003529 * if we're in cookie prefix mode
3530 */
3531 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3532 int delta; /* negative */
3533
3534 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3535 delta = val_beg - (delim + 1);
3536 val_end += delta;
3537 next += delta;
3538 hdr_end += delta;
3539 del_from = NULL;
3540 preserve_hdr = 1; /* we want to keep this cookie */
3541 }
3542 else if (del_from == NULL &&
3543 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3544 del_from = prev;
3545 }
3546 }
3547 else {
3548 /* This is not our cookie, so we must preserve it. But if we already
3549 * scheduled another cookie for removal, we cannot remove the
3550 * complete header, but we can remove the previous block itself.
3551 */
3552 preserve_hdr = 1;
3553
3554 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003555 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003556 if (att_beg >= del_from)
3557 att_beg += delta;
3558 if (att_end >= del_from)
3559 att_end += delta;
3560 val_beg += delta;
3561 val_end += delta;
3562 next += delta;
3563 hdr_end += delta;
3564 prev = del_from;
3565 del_from = NULL;
3566 }
3567 }
3568
3569 /* continue with next cookie on this header line */
3570 att_beg = next;
3571 } /* for each cookie */
3572
3573
3574 /* There are no more cookies on this line.
3575 * We may still have one (or several) marked for deletion at the
3576 * end of the line. We must do this now in two ways :
3577 * - if some cookies must be preserved, we only delete from the
3578 * mark to the end of line ;
3579 * - if nothing needs to be preserved, simply delete the whole header
3580 */
3581 if (del_from) {
3582 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3583 }
3584 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003585 if (hdr_beg != hdr_end)
3586 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003587 else
3588 http_remove_header(htx, &ctx);
3589 }
3590 } /* for each "Cookie header */
3591}
3592
3593/*
3594 * Manage server-side cookies. It can impact performance by about 2% so it is
3595 * desirable to call it only when needed. This function is also used when we
3596 * just need to know if there is a cookie (eg: for check-cache).
3597 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003598static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003599{
3600 struct session *sess = s->sess;
3601 struct http_txn *txn = s->txn;
3602 struct htx *htx;
3603 struct http_hdr_ctx ctx;
3604 struct server *srv;
3605 char *hdr_beg, *hdr_end;
3606 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003607 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003608
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003609 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003610
3611 ctx.blk = NULL;
3612 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003613 int is_first = 1;
3614
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003615 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3616 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3617 break;
3618 is_cookie2 = 1;
3619 }
3620
3621 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3622 * <prev> points to the colon.
3623 */
3624 txn->flags |= TX_SCK_PRESENT;
3625
3626 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3627 * check-cache is enabled) and we are not interested in checking
3628 * them. Warning, the cookie capture is declared in the frontend.
3629 */
3630 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3631 break;
3632
3633 /* OK so now we know we have to process this response cookie.
3634 * The format of the Set-Cookie header is slightly different
3635 * from the format of the Cookie header in that it does not
3636 * support the comma as a cookie delimiter (thus the header
3637 * cannot be folded) because the Expires attribute described in
3638 * the original Netscape's spec may contain an unquoted date
3639 * with a comma inside. We have to live with this because
3640 * many browsers don't support Max-Age and some browsers don't
3641 * support quoted strings. However the Set-Cookie2 header is
3642 * clean.
3643 *
3644 * We have to keep multiple pointers in order to support cookie
3645 * removal at the beginning, middle or end of header without
3646 * corrupting the header (in case of set-cookie2). A special
3647 * pointer, <scav> points to the beginning of the set-cookie-av
3648 * fields after the first semi-colon. The <next> pointer points
3649 * either to the end of line (set-cookie) or next unquoted comma
3650 * (set-cookie2). All of these headers are valid :
3651 *
3652 * hdr_beg hdr_end
3653 * | |
3654 * v |
3655 * NAME1 = VALUE 1 ; Secure; Path="/" |
3656 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3657 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3658 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3659 * | | | | | | | |
3660 * | | | | | | | +-> next
3661 * | | | | | | +------------> scav
3662 * | | | | | +--------------> val_end
3663 * | | | | +--------------------> val_beg
3664 * | | | +----------------------> equal
3665 * | | +------------------------> att_end
3666 * | +----------------------------> att_beg
3667 * +------------------------------> prev
3668 * -------------------------------> hdr_beg
3669 */
3670 hdr_beg = ctx.value.ptr;
3671 hdr_end = hdr_beg + ctx.value.len;
3672 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3673
3674 /* Iterate through all cookies on this line */
3675
3676 /* find att_beg */
3677 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003678 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003679 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003680 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003681
3682 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3683 att_beg++;
3684
3685 /* find att_end : this is the first character after the last non
3686 * space before the equal. It may be equal to hdr_end.
3687 */
3688 equal = att_end = att_beg;
3689
3690 while (equal < hdr_end) {
3691 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3692 break;
3693 if (HTTP_IS_SPHT(*equal++))
3694 continue;
3695 att_end = equal;
3696 }
3697
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003698 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003699 * is between <att_beg> and <equal>, both may be identical.
3700 */
3701
3702 /* look for end of cookie if there is an equal sign */
3703 if (equal < hdr_end && *equal == '=') {
3704 /* look for the beginning of the value */
3705 val_beg = equal + 1;
3706 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3707 val_beg++;
3708
3709 /* find the end of the value, respecting quotes */
3710 next = http_find_cookie_value_end(val_beg, hdr_end);
3711
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003712 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003713 val_end = next;
3714 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3715 val_end--;
3716 }
3717 else {
3718 /* <equal> points to next comma, semi-colon or EOL */
3719 val_beg = val_end = next = equal;
3720 }
3721
3722 if (next < hdr_end) {
3723 /* Set-Cookie2 supports multiple cookies, and <next> points to
3724 * a colon or semi-colon before the end. So skip all attr-value
3725 * pairs and look for the next comma. For Set-Cookie, since
3726 * commas are permitted in values, skip to the end.
3727 */
3728 if (is_cookie2)
3729 next = http_find_hdr_value_end(next, hdr_end);
3730 else
3731 next = hdr_end;
3732 }
3733
3734 /* Now everything is as on the diagram above */
3735
3736 /* Ignore cookies with no equal sign */
3737 if (equal == val_end)
3738 continue;
3739
3740 /* If there are spaces around the equal sign, we need to
3741 * strip them otherwise we'll get trouble for cookie captures,
3742 * or even for rewrites. Since this happens extremely rarely,
3743 * it does not hurt performance.
3744 */
3745 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3746 int stripped_before = 0;
3747 int stripped_after = 0;
3748
3749 if (att_end != equal) {
3750 memmove(att_end, equal, hdr_end - equal);
3751 stripped_before = (att_end - equal);
3752 equal += stripped_before;
3753 val_beg += stripped_before;
3754 }
3755
3756 if (val_beg > equal + 1) {
3757 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3758 stripped_after = (equal + 1) - val_beg;
3759 val_beg += stripped_after;
3760 stripped_before += stripped_after;
3761 }
3762
3763 val_end += stripped_before;
3764 next += stripped_before;
3765 hdr_end += stripped_before;
3766
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003767 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003768 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003769 }
3770
3771 /* First, let's see if we want to capture this cookie. We check
3772 * that we don't already have a server side cookie, because we
3773 * can only capture one. Also as an optimisation, we ignore
3774 * cookies shorter than the declared name.
3775 */
3776 if (sess->fe->capture_name != NULL &&
3777 txn->srv_cookie == NULL &&
3778 (val_end - att_beg >= sess->fe->capture_namelen) &&
3779 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3780 int log_len = val_end - att_beg;
3781 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3782 ha_alert("HTTP logging : out of memory.\n");
3783 }
3784 else {
3785 if (log_len > sess->fe->capture_len)
3786 log_len = sess->fe->capture_len;
3787 memcpy(txn->srv_cookie, att_beg, log_len);
3788 txn->srv_cookie[log_len] = 0;
3789 }
3790 }
3791
3792 srv = objt_server(s->target);
3793 /* now check if we need to process it for persistence */
3794 if (!(s->flags & SF_IGNORE_PRST) &&
3795 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3796 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3797 /* assume passive cookie by default */
3798 txn->flags &= ~TX_SCK_MASK;
3799 txn->flags |= TX_SCK_FOUND;
3800
3801 /* If the cookie is in insert mode on a known server, we'll delete
3802 * this occurrence because we'll insert another one later.
3803 * We'll delete it too if the "indirect" option is set and we're in
3804 * a direct access.
3805 */
3806 if (s->be->ck_opts & PR_CK_PSV) {
3807 /* The "preserve" flag was set, we don't want to touch the
3808 * server's cookie.
3809 */
3810 }
3811 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3812 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3813 /* this cookie must be deleted */
3814 if (prev == hdr_beg && next == hdr_end) {
3815 /* whole header */
3816 http_remove_header(htx, &ctx);
3817 /* note: while both invalid now, <next> and <hdr_end>
3818 * are still equal, so the for() will stop as expected.
3819 */
3820 } else {
3821 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003822 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003823 next = prev;
3824 hdr_end += delta;
3825 }
3826 txn->flags &= ~TX_SCK_MASK;
3827 txn->flags |= TX_SCK_DELETED;
3828 /* and go on with next cookie */
3829 }
3830 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3831 /* replace bytes val_beg->val_end with the cookie name associated
3832 * with this server since we know it.
3833 */
3834 int sliding, delta;
3835
3836 ctx.value = ist2(val_beg, val_end - val_beg);
3837 ctx.lws_before = ctx.lws_after = 0;
3838 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3839 delta = srv->cklen - (val_end - val_beg);
3840 sliding = (ctx.value.ptr - val_beg);
3841 hdr_beg += sliding;
3842 val_beg += sliding;
3843 next += sliding + delta;
3844 hdr_end += sliding + delta;
3845
3846 txn->flags &= ~TX_SCK_MASK;
3847 txn->flags |= TX_SCK_REPLACED;
3848 }
3849 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3850 /* insert the cookie name associated with this server
3851 * before existing cookie, and insert a delimiter between them..
3852 */
3853 int sliding, delta;
3854 ctx.value = ist2(val_beg, 0);
3855 ctx.lws_before = ctx.lws_after = 0;
3856 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3857 delta = srv->cklen + 1;
3858 sliding = (ctx.value.ptr - val_beg);
3859 hdr_beg += sliding;
3860 val_beg += sliding;
3861 next += sliding + delta;
3862 hdr_end += sliding + delta;
3863
3864 val_beg[srv->cklen] = COOKIE_DELIM;
3865 txn->flags &= ~TX_SCK_MASK;
3866 txn->flags |= TX_SCK_REPLACED;
3867 }
3868 }
3869 /* that's done for this cookie, check the next one on the same
3870 * line when next != hdr_end (only if is_cookie2).
3871 */
3872 }
3873 }
3874}
3875
Christopher Faulet25a02f62018-10-24 12:00:25 +02003876/*
3877 * Parses the Cache-Control and Pragma request header fields to determine if
3878 * the request may be served from the cache and/or if it is cacheable. Updates
3879 * s->txn->flags.
3880 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003881void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003882{
3883 struct http_txn *txn = s->txn;
3884 struct htx *htx;
3885 int32_t pos;
3886 int pragma_found, cc_found, i;
3887
3888 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3889 return; /* nothing more to do here */
3890
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003891 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003892 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02003893 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003894 struct htx_blk *blk = htx_get_blk(htx, pos);
3895 enum htx_blk_type type = htx_get_blk_type(blk);
3896 struct ist n, v;
3897
3898 if (type == HTX_BLK_EOH)
3899 break;
3900 if (type != HTX_BLK_HDR)
3901 continue;
3902
3903 n = htx_get_blk_name(htx, blk);
3904 v = htx_get_blk_value(htx, blk);
3905
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003906 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003907 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3908 pragma_found = 1;
3909 continue;
3910 }
3911 }
3912
3913 /* Don't use the cache and don't try to store if we found the
3914 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003915 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003916 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3917 txn->flags |= TX_CACHE_IGNORE;
3918 continue;
3919 }
3920
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003921 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02003922 continue;
3923
3924 /* OK, right now we know we have a cache-control header */
3925 cc_found = 1;
3926 if (!v.len) /* no info */
3927 continue;
3928
3929 i = 0;
3930 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
3931 !isspace((unsigned char)*(v.ptr+i)))
3932 i++;
3933
3934 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
3935 * values after max-age, max-stale nor min-fresh, we simply don't
3936 * use the cache when they're specified.
3937 */
3938 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
3939 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
3940 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
3941 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
3942 txn->flags |= TX_CACHE_IGNORE;
3943 continue;
3944 }
3945
3946 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
3947 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3948 continue;
3949 }
3950 }
3951
3952 /* RFC7234#5.4:
3953 * When the Cache-Control header field is also present and
3954 * understood in a request, Pragma is ignored.
3955 * When the Cache-Control header field is not present in a
3956 * request, caches MUST consider the no-cache request
3957 * pragma-directive as having the same effect as if
3958 * "Cache-Control: no-cache" were present.
3959 */
3960 if (!cc_found && pragma_found)
3961 txn->flags |= TX_CACHE_IGNORE;
3962}
3963
3964/*
3965 * Check if response is cacheable or not. Updates s->txn->flags.
3966 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003967void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003968{
3969 struct http_txn *txn = s->txn;
3970 struct htx *htx;
3971 int32_t pos;
3972 int i;
3973
3974 if (txn->status < 200) {
3975 /* do not try to cache interim responses! */
3976 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3977 return;
3978 }
3979
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003980 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02003981 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003982 struct htx_blk *blk = htx_get_blk(htx, pos);
3983 enum htx_blk_type type = htx_get_blk_type(blk);
3984 struct ist n, v;
3985
3986 if (type == HTX_BLK_EOH)
3987 break;
3988 if (type != HTX_BLK_HDR)
3989 continue;
3990
3991 n = htx_get_blk_name(htx, blk);
3992 v = htx_get_blk_value(htx, blk);
3993
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003994 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003995 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3996 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3997 return;
3998 }
3999 }
4000
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004001 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004002 continue;
4003
4004 /* OK, right now we know we have a cache-control header */
4005 if (!v.len) /* no info */
4006 continue;
4007
4008 i = 0;
4009 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4010 !isspace((unsigned char)*(v.ptr+i)))
4011 i++;
4012
4013 /* we have a complete value between v.ptr and (v.ptr+i) */
4014 if (i < v.len && *(v.ptr + i) == '=') {
4015 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
4016 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
4017 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4018 continue;
4019 }
4020
4021 /* we have something of the form no-cache="set-cookie" */
4022 if ((v.len >= 21) &&
4023 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
4024 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
4025 txn->flags &= ~TX_CACHE_COOK;
4026 continue;
4027 }
4028
4029 /* OK, so we know that either p2 points to the end of string or to a comma */
4030 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
4031 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4032 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
4033 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4034 return;
4035 }
4036
4037 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4038 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4039 continue;
4040 }
4041 }
4042}
4043
Christopher Faulet377c5a52018-10-24 21:21:30 +02004044/*
4045 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4046 * for the current backend.
4047 *
4048 * It is assumed that the request is either a HEAD, GET, or POST and that the
4049 * uri_auth field is valid.
4050 *
4051 * Returns 1 if stats should be provided, otherwise 0.
4052 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004053static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004054{
4055 struct uri_auth *uri_auth = backend->uri_auth;
4056 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004057 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004058 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004059
4060 if (!uri_auth)
4061 return 0;
4062
4063 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4064 return 0;
4065
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004066 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004067 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004068 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01004069 if (*uri_auth->uri_prefix == '/')
4070 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004071
4072 /* check URI size */
4073 if (uri_auth->uri_len > uri.len)
4074 return 0;
4075
4076 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4077 return 0;
4078
4079 return 1;
4080}
4081
4082/* This function prepares an applet to handle the stats. It can deal with the
4083 * "100-continue" expectation, check that admin rules are met for POST requests,
4084 * and program a response message if something was unexpected. It cannot fail
4085 * and always relies on the stats applet to complete the job. It does not touch
4086 * analysers nor counters, which are left to the caller. It does not touch
4087 * s->target which is supposed to already point to the stats applet. The caller
4088 * is expected to have already assigned an appctx to the stream.
4089 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004090static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004091{
4092 struct stats_admin_rule *stats_admin_rule;
4093 struct stream_interface *si = &s->si[1];
4094 struct session *sess = s->sess;
4095 struct http_txn *txn = s->txn;
4096 struct http_msg *msg = &txn->req;
4097 struct uri_auth *uri_auth = s->be->uri_auth;
4098 const char *h, *lookup, *end;
4099 struct appctx *appctx;
4100 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004101 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004102
4103 appctx = si_appctx(si);
4104 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4105 appctx->st1 = appctx->st2 = 0;
4106 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004107 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004108 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4109 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4110 appctx->ctx.stats.flags |= STAT_CHUNKED;
4111
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004112 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004113 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004114 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4115 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004116
4117 for (h = lookup; h <= end - 3; h++) {
4118 if (memcmp(h, ";up", 3) == 0) {
4119 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4120 break;
4121 }
4122 }
4123
4124 if (uri_auth->refresh) {
4125 for (h = lookup; h <= end - 10; h++) {
4126 if (memcmp(h, ";norefresh", 10) == 0) {
4127 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4128 break;
4129 }
4130 }
4131 }
4132
4133 for (h = lookup; h <= end - 4; h++) {
4134 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004135 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004136 break;
4137 }
4138 }
4139
4140 for (h = lookup; h <= end - 6; h++) {
4141 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004142 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004143 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4144 break;
4145 }
4146 }
4147
Christopher Faulet6338a082019-09-09 15:50:54 +02004148 for (h = lookup; h <= end - 5; h++) {
4149 if (memcmp(h, ";json", 5) == 0) {
4150 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4151 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4152 break;
4153 }
4154 }
4155
4156 for (h = lookup; h <= end - 12; h++) {
4157 if (memcmp(h, ";json-schema", 12) == 0) {
4158 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4159 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4160 break;
4161 }
4162 }
4163
Christopher Faulet377c5a52018-10-24 21:21:30 +02004164 for (h = lookup; h <= end - 8; h++) {
4165 if (memcmp(h, ";st=", 4) == 0) {
4166 int i;
4167 h += 4;
4168 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4169 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4170 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4171 appctx->ctx.stats.st_code = i;
4172 break;
4173 }
4174 }
4175 break;
4176 }
4177 }
4178
4179 appctx->ctx.stats.scope_str = 0;
4180 appctx->ctx.stats.scope_len = 0;
4181 for (h = lookup; h <= end - 8; h++) {
4182 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4183 int itx = 0;
4184 const char *h2;
4185 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4186 const char *err;
4187
4188 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4189 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004190 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4191 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004192 if (*h == ';' || *h == '&' || *h == ' ')
4193 break;
4194 itx++;
4195 h++;
4196 }
4197
4198 if (itx > STAT_SCOPE_TXT_MAXLEN)
4199 itx = STAT_SCOPE_TXT_MAXLEN;
4200 appctx->ctx.stats.scope_len = itx;
4201
4202 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4203 memcpy(scope_txt, h2, itx);
4204 scope_txt[itx] = '\0';
4205 err = invalid_char(scope_txt);
4206 if (err) {
4207 /* bad char in search text => clear scope */
4208 appctx->ctx.stats.scope_str = 0;
4209 appctx->ctx.stats.scope_len = 0;
4210 }
4211 break;
4212 }
4213 }
4214
4215 /* now check whether we have some admin rules for this request */
4216 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4217 int ret = 1;
4218
4219 if (stats_admin_rule->cond) {
4220 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4221 ret = acl_pass(ret);
4222 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4223 ret = !ret;
4224 }
4225
4226 if (ret) {
4227 /* no rule, or the rule matches */
4228 appctx->ctx.stats.flags |= STAT_ADMIN;
4229 break;
4230 }
4231 }
4232
Christopher Faulet5d45e382019-02-27 15:15:23 +01004233 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4234 appctx->st0 = STAT_HTTP_HEAD;
4235 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004236 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004237 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004238 if (msg->msg_state < HTTP_MSG_DATA)
4239 req->analysers |= AN_REQ_HTTP_BODY;
4240 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004241 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004242 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004243 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4244 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4245 appctx->st0 = STAT_HTTP_LAST;
4246 }
4247 }
4248 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004249 /* Unsupported method */
4250 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4251 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4252 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004253 }
4254
4255 s->task->nice = -32; /* small boost for HTTP statistics */
4256 return 1;
4257}
4258
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004259void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004260{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004261 struct channel *req = &s->req;
4262 struct channel *res = &s->res;
4263 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004264 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004265 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004266 struct ist path, location;
4267 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004268
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004269 /*
4270 * Create the location
4271 */
4272 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004273
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004274 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004275 /* special prefix "/" means don't change URL */
4276 srv = __objt_server(s->target);
4277 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4278 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4279 return;
4280 }
4281
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004282 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004283 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004284 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004285 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004286 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004287 return;
4288
4289 if (!chunk_memcat(&trash, path.ptr, path.len))
4290 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004291 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004292
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004293 /*
4294 * Create the 302 respone
4295 */
4296 htx = htx_from_buf(&res->buf);
4297 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4298 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4299 ist("HTTP/1.1"), ist("302"), ist("Found"));
4300 if (!sl)
4301 goto fail;
4302 sl->info.res.status = 302;
4303 s->txn->status = 302;
4304
4305 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4306 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4307 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4308 !htx_add_header(htx, ist("Location"), location))
4309 goto fail;
4310
4311 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4312 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004313
Christopher Fauletc20afb82020-01-24 19:16:26 +01004314 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004315 if (!http_forward_proxy_resp(s, 1))
4316 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004317
4318 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004319 si_shutr(si);
4320 si_shutw(si);
4321 si->err_type = SI_ET_NONE;
4322 si->state = SI_ST_CLO;
4323
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004324 if (!(s->flags & SF_ERR_MASK))
4325 s->flags |= SF_ERR_LOCAL;
4326 if (!(s->flags & SF_FINST_MASK))
4327 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004328
4329 /* FIXME: we should increase a counter of redirects per server and per backend. */
4330 srv_inc_sess_ctr(srv);
4331 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004332 return;
4333
4334 fail:
4335 /* If an error occurred, remove the incomplete HTTP response from the
4336 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004337 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004338}
4339
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004340/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004341 * because an error was triggered during the body forwarding.
4342 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004343static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004344{
4345 struct channel *chn = &s->req;
4346 struct http_txn *txn = s->txn;
4347
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004348 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004349
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004350 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4351 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004352 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004353 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004354 goto end;
4355 }
4356
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004357 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4358 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004359 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004360 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004361
4362 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004363 /* No need to read anymore, the request was completely parsed.
4364 * We can shut the read side unless we want to abort_on_close,
4365 * or we have a POST request. The issue with POST requests is
4366 * that some browsers still send a CRLF after the request, and
4367 * this CRLF must be read so that it does not remain in the kernel
4368 * buffers, otherwise a close could cause an RST on some systems
4369 * (eg: Linux).
4370 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004371 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004372 channel_dont_read(chn);
4373
4374 /* if the server closes the connection, we want to immediately react
4375 * and close the socket to save packets and syscalls.
4376 */
4377 s->si[1].flags |= SI_FL_NOHALF;
4378
4379 /* In any case we've finished parsing the request so we must
4380 * disable Nagle when sending data because 1) we're not going
4381 * to shut this side, and 2) the server is waiting for us to
4382 * send pending data.
4383 */
4384 chn->flags |= CF_NEVER_WAIT;
4385
Christopher Fauletd01ce402019-01-02 17:44:13 +01004386 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4387 /* The server has not finished to respond, so we
4388 * don't want to move in order not to upset it.
4389 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004390 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004391 return;
4392 }
4393
Christopher Fauletf2824e62018-10-01 12:12:37 +02004394 /* When we get here, it means that both the request and the
4395 * response have finished receiving. Depending on the connection
4396 * mode, we'll have to wait for the last bytes to leave in either
4397 * direction, and sometimes for a close to be effective.
4398 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004399 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004400 /* Tunnel mode will not have any analyser so it needs to
4401 * poll for reads.
4402 */
4403 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004404 if (b_data(&chn->buf)) {
4405 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004406 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004407 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004408 txn->req.msg_state = HTTP_MSG_TUNNEL;
4409 }
4410 else {
4411 /* we're not expecting any new data to come for this
4412 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004413 *
4414 * However, there is an exception if the response
4415 * length is undefined. In this case, we need to wait
4416 * the close from the server. The response will be
4417 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004418 */
4419 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4420 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004421 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004422
4423 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4424 channel_shutr_now(chn);
4425 channel_shutw_now(chn);
4426 }
4427 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004428 goto check_channel_flags;
4429 }
4430
4431 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4432 http_msg_closing:
4433 /* nothing else to forward, just waiting for the output buffer
4434 * to be empty and for the shutw_now to take effect.
4435 */
4436 if (channel_is_empty(chn)) {
4437 txn->req.msg_state = HTTP_MSG_CLOSED;
4438 goto http_msg_closed;
4439 }
4440 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004441 txn->req.msg_state = HTTP_MSG_ERROR;
4442 goto end;
4443 }
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 return;
4446 }
4447
4448 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4449 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004450 /* if we don't know whether the server will close, we need to hard close */
4451 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4452 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004453 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004454 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004455 channel_dont_read(chn);
4456 goto end;
4457 }
4458
4459 check_channel_flags:
4460 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4461 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4462 /* if we've just closed an output, let's switch */
4463 txn->req.msg_state = HTTP_MSG_CLOSING;
4464 goto http_msg_closing;
4465 }
4466
4467 end:
4468 chn->analysers &= AN_REQ_FLT_END;
4469 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4470 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4471 channel_auto_close(chn);
4472 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004473 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004474}
4475
4476
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004477/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004478 * because an error was triggered during the body forwarding.
4479 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004480static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004481{
4482 struct channel *chn = &s->res;
4483 struct http_txn *txn = s->txn;
4484
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004485 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004486
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004487 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4488 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004489 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004490 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004491 goto end;
4492 }
4493
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004494 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4495 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004496 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004497 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004498
4499 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4500 /* In theory, we don't need to read anymore, but we must
4501 * still monitor the server connection for a possible close
4502 * while the request is being uploaded, so we don't disable
4503 * reading.
4504 */
4505 /* channel_dont_read(chn); */
4506
4507 if (txn->req.msg_state < HTTP_MSG_DONE) {
4508 /* The client seems to still be sending data, probably
4509 * because we got an error response during an upload.
4510 * We have the choice of either breaking the connection
4511 * or letting it pass through. Let's do the later.
4512 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004513 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004514 return;
4515 }
4516
4517 /* When we get here, it means that both the request and the
4518 * response have finished receiving. Depending on the connection
4519 * mode, we'll have to wait for the last bytes to leave in either
4520 * direction, and sometimes for a close to be effective.
4521 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004522 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004523 channel_auto_read(chn);
4524 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004525 if (b_data(&chn->buf)) {
4526 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004527 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004528 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004529 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4530 }
4531 else {
4532 /* we're not expecting any new data to come for this
4533 * transaction, so we can close it.
4534 */
4535 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4536 channel_shutr_now(chn);
4537 channel_shutw_now(chn);
4538 }
4539 }
4540 goto check_channel_flags;
4541 }
4542
4543 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4544 http_msg_closing:
4545 /* nothing else to forward, just waiting for the output buffer
4546 * to be empty and for the shutw_now to take effect.
4547 */
4548 if (channel_is_empty(chn)) {
4549 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4550 goto http_msg_closed;
4551 }
4552 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004553 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004554 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004555 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004556 if (strm_sess(s)->listener->counters)
4557 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004558 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004559 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004560 goto end;
4561 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004562 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004563 return;
4564 }
4565
4566 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4567 http_msg_closed:
4568 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004569 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004570 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004571 goto end;
4572 }
4573
4574 check_channel_flags:
4575 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4576 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4577 /* if we've just closed an output, let's switch */
4578 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4579 goto http_msg_closing;
4580 }
4581
4582 end:
4583 chn->analysers &= AN_RES_FLT_END;
4584 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4585 chn->analysers |= AN_RES_FLT_XFER_DATA;
4586 channel_auto_close(chn);
4587 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004588 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004589}
4590
Christopher Fauletef70e252020-01-28 09:26:19 +01004591/* Forward a response generated by HAProxy (error/redirect/return). This
4592 * function forwards all pending incoming data. If <final> is set to 0, nothing
4593 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet8d945d62020-04-20 14:58:38 +02004594 * transaction is terminated and the request is emptied. if <final> is greater
4595 * than 1, it means after-response ruleset must not be evaluated. On success 1
4596 * is returned. If an error occurred, 0 is returned.
Christopher Fauletef70e252020-01-28 09:26:19 +01004597 */
4598int http_forward_proxy_resp(struct stream *s, int final)
4599{
4600 struct channel *req = &s->req;
4601 struct channel *res = &s->res;
4602 struct htx *htx = htxbuf(&res->buf);
4603 size_t data;
4604
4605 if (final) {
4606 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet8d945d62020-04-20 14:58:38 +02004607 if (final == 1 && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004608 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004609
4610 channel_auto_read(req);
4611 channel_abort(req);
4612 channel_auto_close(req);
4613 channel_htx_erase(req, htxbuf(&req->buf));
4614
4615 res->wex = tick_add_ifset(now_ms, res->wto);
4616 channel_auto_read(res);
4617 channel_auto_close(res);
4618 channel_shutr_now(res);
4619 }
4620
4621 data = htx->data - co_data(res);
4622 c_adv(res, data);
4623 htx->first = -1;
4624 res->total += data;
4625 return 1;
4626}
4627
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004628void http_server_error(struct stream *s, struct stream_interface *si, int err,
4629 int finst, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004630{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004631 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004632 if (!(s->flags & SF_ERR_MASK))
4633 s->flags |= err;
4634 if (!(s->flags & SF_FINST_MASK))
4635 s->flags |= finst;
4636}
4637
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004638void http_reply_and_close(struct stream *s, short status, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004639{
Christopher Faulet8d945d62020-04-20 14:58:38 +02004640 int final = 1;
4641
4642 retry:
Christopher Faulet0f226952018-10-22 09:29:56 +02004643 channel_auto_read(&s->req);
4644 channel_abort(&s->req);
4645 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004646 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
4647 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004648 channel_auto_read(&s->res);
4649 channel_auto_close(&s->res);
4650 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004651
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004652 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet0f226952018-10-22 09:29:56 +02004653 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004654
4655 /* <msg> is an HTX structure. So we copy it in the response's
4656 * channel */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004657 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004658 struct channel *chn = &s->res;
4659 struct htx *htx;
4660
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004661 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004662 htx = htx_from_buf(&chn->buf);
Christopher Faulet637259e2020-01-23 11:57:31 +01004663 if (channel_htx_copy_msg(chn, htx, msg)) {
Christopher Faulet8d945d62020-04-20 14:58:38 +02004664 if (!http_forward_proxy_resp(s, final)) {
4665 /* On error, return a 500 error message, but
4666 * don't rewrite it if it is already an internal
4667 * error.
4668 */
4669 if (s->txn->status == 500)
4670 final++;
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004671 s->txn->status = 500;
Christopher Faulet8d945d62020-04-20 14:58:38 +02004672 msg = http_error_message(s);
4673 goto retry;
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004674 }
Christopher Faulet637259e2020-01-23 11:57:31 +01004675 }
Christopher Faulet0f226952018-10-22 09:29:56 +02004676 }
Christopher Faulet0f226952018-10-22 09:29:56 +02004677}
4678
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004679struct buffer *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004680{
4681 const int msgnum = http_get_status_idx(s->txn->status);
4682
Christopher Faulet53a87e12020-01-21 10:13:03 +01004683 if (s->txn->errmsg)
Christopher Faulet473e8802020-01-14 11:12:37 +01004684 return s->txn->errmsg;
4685 else if (s->be->errmsg[msgnum])
Christopher Faulet58857752020-01-15 15:19:50 +01004686 return s->be->errmsg[msgnum];
4687 else if (strm_fe(s)->errmsg[msgnum])
4688 return strm_fe(s)->errmsg[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004689 else
Christopher Fauletf7346382019-07-17 22:02:08 +02004690 return &http_err_chunks[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004691}
4692
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004693/* Produces a response from an http reply. Depending on the http reply type, a,
4694 * errorfile, an raw file or a log-format string is used. On success, it returns
4695 * 0. If an error occurs -1 is returned.
4696 */
4697int http_reply_message(struct stream *s, struct http_reply *reply)
4698{
4699 struct channel *res = &s->res;
4700 struct buffer *errmsg;
4701 struct htx *htx = htx_from_buf(&res->buf);
4702 struct htx_sl *sl;
4703 struct buffer *body = NULL;
4704 const char *status, *reason, *clen, *ctype;
4705 unsigned int slflags;
4706 int ret = 0;
4707
4708 s->txn->status = reply->status;
4709 channel_htx_truncate(res, htx);
4710
Christopher Faulete29a97e2020-05-14 14:49:25 +02004711 /*
4712 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4713 *
4714 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4715 * as no payload if NULL. the TXN status code is set with the status
4716 * of the original reply.
4717 */
4718
4719 if (reply->type == HTTP_REPLY_INDIRECT) {
4720 if (reply->body.reply)
4721 reply = reply->body.reply;
4722 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004723
4724 if (reply->type == HTTP_REPLY_ERRMSG) {
4725 /* implicit or explicit error message*/
4726 errmsg = reply->body.errmsg;
4727 if (!errmsg) {
4728 /* get default error message */
4729 errmsg = http_error_message(s);
4730 }
Christopher Faulet5cb513a2020-05-13 17:56:56 +02004731 if (!b_is_null(errmsg) && !channel_htx_copy_msg(res, htx, errmsg))
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004732 goto fail;
4733 }
4734 else {
4735 /* no payload, file or log-format string */
4736 if (reply->type == HTTP_REPLY_RAW) {
4737 /* file */
4738 body = &reply->body.obj;
4739 }
4740 else if (reply->type == HTTP_REPLY_LOGFMT) {
4741 /* log-format string */
4742 body = alloc_trash_chunk();
4743 if (!body)
4744 goto fail_alloc;
4745 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4746 }
4747 /* else no payload */
4748
4749 status = ultoa(reply->status);
4750 reason = http_get_reason(reply->status);
4751 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4752 if (!body || !b_data(body))
4753 slflags |= HTX_SL_F_BODYLESS;
4754 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4755 if (!sl)
4756 goto fail;
4757 sl->info.res.status = reply->status;
4758
4759 clen = (body ? ultoa(b_data(body)) : "0");
4760 ctype = reply->ctype;
4761
4762 if (!LIST_ISEMPTY(&reply->hdrs)) {
4763 struct http_reply_hdr *hdr;
4764 struct buffer *value = alloc_trash_chunk();
4765
4766 if (!value)
4767 goto fail;
4768
4769 list_for_each_entry(hdr, &reply->hdrs, list) {
4770 chunk_reset(value);
4771 value->data = build_logline(s, value->area, value->size, &hdr->value);
4772 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4773 free_trash_chunk(value);
4774 goto fail;
4775 }
4776 chunk_reset(value);
4777 }
4778 free_trash_chunk(value);
4779 }
4780
4781 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4782 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4783 !htx_add_endof(htx, HTX_BLK_EOH) ||
4784 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))) ||
4785 !htx_add_endof(htx, HTX_BLK_EOM))
4786 goto fail;
4787 }
4788
4789 htx_to_buf(htx, &s->res.buf);
4790 if (!http_forward_proxy_resp(s, 1))
4791 goto fail;
4792
4793 leave:
4794 if (reply->type == HTTP_REPLY_LOGFMT)
4795 free_trash_chunk(body);
4796 return ret;
4797
4798 fail_alloc:
4799 if (!(s->flags & SF_ERR_MASK))
4800 s->flags |= SF_ERR_RESOURCE;
4801 ret = -1;
4802 goto leave;
4803
4804 fail:
4805 /* If an error occurred, remove the incomplete HTTP response from the
4806 * buffer */
4807 channel_htx_truncate(res, htx);
4808 ret = -1;
4809 if (!(s->flags & SF_ERR_MASK))
4810 s->flags |= SF_ERR_PRXCOND;
4811 goto leave;
4812}
4813
Christopher Faulet304cc402019-07-15 15:46:28 +02004814/* Return the error message corresponding to si->err_type. It is assumed
4815 * that the server side is closed. Note that err_type is actually a
4816 * bitmask, where almost only aborts may be cumulated with other
4817 * values. We consider that aborted operations are more important
4818 * than timeouts or errors due to the fact that nobody else in the
4819 * logs might explain incomplete retries. All others should avoid
4820 * being cumulated. It should normally not be possible to have multiple
4821 * aborts at once, but just in case, the first one in sequence is reported.
4822 * Note that connection errors appearing on the second request of a keep-alive
4823 * connection are not reported since this allows the client to retry.
4824 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004825void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004826{
4827 int err_type = si->err_type;
4828
4829 /* set s->txn->status for http_error_message(s) */
4830 s->txn->status = 503;
4831
4832 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004833 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4834 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004835 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004836 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4837 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4838 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004839 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004840 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4841 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004842 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004843 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4844 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004845 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004846 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4847 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4848 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004849 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004850 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4851 (s->flags & SF_SRV_REUSED) ? NULL :
4852 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004853 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004854 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4855 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4856 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004857 else { /* SI_ET_CONN_OTHER and others */
4858 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004859 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4860 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004861 }
4862}
4863
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004864
Christopher Faulet4a28a532019-03-01 11:19:40 +01004865/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4866 * on success and -1 on error.
4867 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004868static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004869{
4870 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4871 * then we must send an HTTP/1.1 100 Continue intermediate response.
4872 */
4873 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4874 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4875 struct ist hdr = { .ptr = "Expect", .len = 6 };
4876 struct http_hdr_ctx ctx;
4877
4878 ctx.blk = NULL;
4879 /* Expect is allowed in 1.1, look for it */
4880 if (http_find_header(htx, hdr, &ctx, 0) &&
4881 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004882 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004883 return -1;
4884 http_remove_header(htx, &ctx);
4885 }
4886 }
4887 return 0;
4888}
4889
Christopher Faulet23a3c792018-11-28 10:01:23 +01004890/* Send a 100-Continue response to the client. It returns 0 on success and -1
4891 * on error. The response channel is updated accordingly.
4892 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004893static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004894{
4895 struct channel *res = &s->res;
4896 struct htx *htx = htx_from_buf(&res->buf);
4897 struct htx_sl *sl;
4898 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4899 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004900
4901 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4902 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4903 if (!sl)
4904 goto fail;
4905 sl->info.res.status = 100;
4906
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004907 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004908 goto fail;
4909
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004910 if (!http_forward_proxy_resp(s, 0))
4911 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004912 return 0;
4913
4914 fail:
4915 /* If an error occurred, remove the incomplete HTTP response from the
4916 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004917 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004918 return -1;
4919}
4920
Christopher Faulet12c51e22018-11-28 15:59:42 +01004921
4922/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
4923 * ont whether we use a proxy or not. It returns 0 on success and -1 on
4924 * error. The response channel is updated accordingly.
4925 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004926static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
Christopher Faulet12c51e22018-11-28 15:59:42 +01004927{
4928 struct channel *res = &s->res;
4929 struct htx *htx = htx_from_buf(&res->buf);
4930 struct htx_sl *sl;
4931 struct ist code, body;
4932 int status;
4933 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
Christopher Faulet12c51e22018-11-28 15:59:42 +01004934
4935 if (!(s->txn->flags & TX_USE_PX_CONN)) {
4936 status = 401;
4937 code = ist("401");
4938 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
4939 "You need a valid user and password to access this content.\n"
4940 "</body></html>\n");
4941 }
4942 else {
4943 status = 407;
4944 code = ist("407");
4945 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
4946 "You need a valid user and password to access this content.\n"
4947 "</body></html>\n");
4948 }
4949
4950 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4951 ist("HTTP/1.1"), code, ist("Unauthorized"));
4952 if (!sl)
4953 goto fail;
4954 sl->info.res.status = status;
4955 s->txn->status = status;
4956
4957 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
4958 goto fail;
4959
Willy Tarreaub5ba2b02019-06-11 16:08:25 +02004960 if (!htx_add_header(htx, ist("Content-length"), ist("112")) ||
4961 !htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Faulet12c51e22018-11-28 15:59:42 +01004962 !htx_add_header(htx, ist("Connection"), ist("close")) ||
Jérôme Magnin86cef232018-12-28 14:49:08 +01004963 !htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4964 goto fail;
4965 if (status == 401 && !htx_add_header(htx, ist("WWW-Authenticate"), ist2(trash.area, trash.data)))
4966 goto fail;
4967 if (status == 407 && !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
Christopher Faulet12c51e22018-11-28 15:59:42 +01004968 goto fail;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004969 if (!htx_add_endof(htx, HTX_BLK_EOH))
4970 goto fail;
4971
4972 while (body.len) {
4973 size_t sent = htx_add_data(htx, body);
4974 if (!sent)
4975 goto fail;
4976 body.ptr += sent;
4977 body.len -= sent;
4978 }
4979
4980 if (!htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet12c51e22018-11-28 15:59:42 +01004981 goto fail;
4982
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004983 if (!http_forward_proxy_resp(s, 1))
4984 goto fail;
Christopher Faulet12c51e22018-11-28 15:59:42 +01004985 return 0;
4986
4987 fail:
4988 /* If an error occurred, remove the incomplete HTTP response from the
4989 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004990 channel_htx_truncate(res, htx);
Christopher Faulet12c51e22018-11-28 15:59:42 +01004991 return -1;
4992}
4993
Christopher Faulet0f226952018-10-22 09:29:56 +02004994/*
4995 * Capture headers from message <htx> according to header list <cap_hdr>, and
4996 * fill the <cap> pointers appropriately.
4997 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004998static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004999{
5000 struct cap_hdr *h;
5001 int32_t pos;
5002
Christopher Fauleta3f15502019-05-13 15:27:23 +02005003 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02005004 struct htx_blk *blk = htx_get_blk(htx, pos);
5005 enum htx_blk_type type = htx_get_blk_type(blk);
5006 struct ist n, v;
5007
5008 if (type == HTX_BLK_EOH)
5009 break;
5010 if (type != HTX_BLK_HDR)
5011 continue;
5012
5013 n = htx_get_blk_name(htx, blk);
5014
5015 for (h = cap_hdr; h; h = h->next) {
5016 if (h->namelen && (h->namelen == n.len) &&
5017 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
5018 if (cap[h->index] == NULL)
5019 cap[h->index] =
5020 pool_alloc(h->pool);
5021
5022 if (cap[h->index] == NULL) {
5023 ha_alert("HTTP capture : out of memory.\n");
5024 break;
5025 }
5026
5027 v = htx_get_blk_value(htx, blk);
5028 if (v.len > h->len)
5029 v.len = h->len;
5030
5031 memcpy(cap[h->index], v.ptr, v.len);
5032 cap[h->index][v.len]=0;
5033 }
5034 }
5035 }
5036}
5037
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005038/* Delete a value in a header between delimiters <from> and <next>. The header
5039 * itself is delimited by <start> and <end> pointers. The number of characters
5040 * displaced is returned, and the pointer to the first delimiter is updated if
5041 * required. The function tries as much as possible to respect the following
5042 * principles :
5043 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5044 * in which case <next> is simply removed
5045 * - set exactly one space character after the new first delimiter, unless there
5046 * are not enough characters in the block being moved to do so.
5047 * - remove unneeded spaces before the previous delimiter and after the new
5048 * one.
5049 *
5050 * It is the caller's responsibility to ensure that :
5051 * - <from> points to a valid delimiter or <start> ;
5052 * - <next> points to a valid delimiter or <end> ;
5053 * - there are non-space chars before <from>.
5054 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005055static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005056{
5057 char *prev = *from;
5058
5059 if (prev == start) {
5060 /* We're removing the first value. eat the semicolon, if <next>
5061 * is lower than <end> */
5062 if (next < end)
5063 next++;
5064
5065 while (next < end && HTTP_IS_SPHT(*next))
5066 next++;
5067 }
5068 else {
5069 /* Remove useless spaces before the old delimiter. */
5070 while (HTTP_IS_SPHT(*(prev-1)))
5071 prev--;
5072 *from = prev;
5073
5074 /* copy the delimiter and if possible a space if we're
5075 * not at the end of the line.
5076 */
5077 if (next < end) {
5078 *prev++ = *next++;
5079 if (prev + 1 < next)
5080 *prev++ = ' ';
5081 while (next < end && HTTP_IS_SPHT(*next))
5082 next++;
5083 }
5084 }
5085 memmove(prev, next, end - next);
5086 return (prev - next);
5087}
5088
Christopher Faulet0f226952018-10-22 09:29:56 +02005089
5090/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005091 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005092 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005093static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005094{
5095 struct ist dst = ist2(str, 0);
5096
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005097 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005098 goto end;
5099 if (dst.len + 1 > len)
5100 goto end;
5101 dst.ptr[dst.len++] = ' ';
5102
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005103 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005104 goto end;
5105 if (dst.len + 1 > len)
5106 goto end;
5107 dst.ptr[dst.len++] = ' ';
5108
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005109 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005110 end:
5111 return dst.len;
5112}
5113
5114/*
5115 * Print a debug line with a start line.
5116 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005117static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005118{
5119 struct session *sess = strm_sess(s);
5120 int max;
5121
5122 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5123 dir,
5124 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5125 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5126
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005127 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005128 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005129 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005130 trash.area[trash.data++] = ' ';
5131
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005132 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005133 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005134 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005135 trash.area[trash.data++] = ' ';
5136
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005137 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005138 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005139 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005140 trash.area[trash.data++] = '\n';
5141
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005142 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005143}
5144
5145/*
5146 * Print a debug line with a header.
5147 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005148static 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 +02005149{
5150 struct session *sess = strm_sess(s);
5151 int max;
5152
5153 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5154 dir,
5155 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5156 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5157
5158 max = n.len;
5159 UBOUND(max, trash.size - trash.data - 3);
5160 chunk_memcat(&trash, n.ptr, max);
5161 trash.area[trash.data++] = ':';
5162 trash.area[trash.data++] = ' ';
5163
5164 max = v.len;
5165 UBOUND(max, trash.size - trash.data - 1);
5166 chunk_memcat(&trash, v.ptr, max);
5167 trash.area[trash.data++] = '\n';
5168
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005169 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005170}
5171
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005172/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5173 * In case of allocation failure, everything allocated is freed and NULL is
5174 * returned. Otherwise the new transaction is assigned to the stream and
5175 * returned.
5176 */
5177struct http_txn *http_alloc_txn(struct stream *s)
5178{
5179 struct http_txn *txn = s->txn;
5180
5181 if (txn)
5182 return txn;
5183
5184 txn = pool_alloc(pool_head_http_txn);
5185 if (!txn)
5186 return txn;
5187
5188 s->txn = txn;
5189 return txn;
5190}
5191
5192void http_txn_reset_req(struct http_txn *txn)
5193{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005194 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005195 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5196}
5197
5198void http_txn_reset_res(struct http_txn *txn)
5199{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005200 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005201 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5202}
5203
5204/*
5205 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5206 * the required fields are properly allocated and that we only need to (re)init
5207 * them. This should be used before processing any new request.
5208 */
5209void http_init_txn(struct stream *s)
5210{
5211 struct http_txn *txn = s->txn;
5212 struct conn_stream *cs = objt_cs(s->si[0].end);
5213
5214 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5215 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5216 : 0);
5217 txn->status = -1;
Christopher Faulet473e8802020-01-14 11:12:37 +01005218 txn->errmsg = NULL;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005219 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005220 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005221
5222 txn->cookie_first_date = 0;
5223 txn->cookie_last_date = 0;
5224
5225 txn->srv_cookie = NULL;
5226 txn->cli_cookie = NULL;
5227 txn->uri = NULL;
5228
5229 http_txn_reset_req(txn);
5230 http_txn_reset_res(txn);
5231
5232 txn->req.chn = &s->req;
5233 txn->rsp.chn = &s->res;
5234
5235 txn->auth.method = HTTP_AUTH_UNKNOWN;
5236
5237 vars_init(&s->vars_txn, SCOPE_TXN);
5238 vars_init(&s->vars_reqres, SCOPE_REQ);
5239}
5240
5241/* to be used at the end of a transaction */
5242void http_end_txn(struct stream *s)
5243{
5244 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005245
5246 /* these ones will have been dynamically allocated */
5247 pool_free(pool_head_requri, txn->uri);
5248 pool_free(pool_head_capture, txn->cli_cookie);
5249 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005250 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005251
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005252 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005253 txn->uri = NULL;
5254 txn->srv_cookie = NULL;
5255 txn->cli_cookie = NULL;
5256
Christopher Faulet59399252019-11-07 14:27:52 +01005257 if (!LIST_ISEMPTY(&s->vars_txn.head))
5258 vars_prune(&s->vars_txn, s->sess, s);
5259 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5260 vars_prune(&s->vars_reqres, s->sess, s);
5261}
5262
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005263
5264DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005265
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005266__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005267static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005268{
5269}
5270
5271
5272/*
5273 * Local variables:
5274 * c-indent-level: 8
5275 * c-basic-offset: 8
5276 * End:
5277 */