blob: c93edfea759e97fa3211b35209ec361cc71d8deb [file] [log] [blame]
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Christopher Faulete0768eb2018-10-03 16:38:02 +020013#include <common/base64.h>
14#include <common/config.h>
15#include <common/debug.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010016#include <common/htx.h>
Willy Tarreau8b507582020-02-25 09:35:07 +010017#include <common/net_helper.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020018#include <common/uri_auth.h>
19
Christopher Faulet0f226952018-10-22 09:29:56 +020020#include <types/capture.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020021
22#include <proto/acl.h>
Christopher Faulet3e964192018-10-24 11:39:23 +020023#include <proto/action.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020024#include <proto/channel.h>
25#include <proto/checks.h>
26#include <proto/connection.h>
27#include <proto/filters.h>
Christopher Faulet0f226952018-10-22 09:29:56 +020028#include <proto/http_htx.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020029#include <proto/log.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020030#include <proto/http_ana.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020031#include <proto/proxy.h>
Christopher Fauletfefc73d2018-10-24 21:18:04 +020032#include <proto/server.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020033#include <proto/stream.h>
34#include <proto/stream_interface.h>
35#include <proto/stats.h>
Christopher Fauleta8a46e22019-07-16 14:53:09 +020036#include <proto/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020037
Christopher Fauleteea8fc72019-11-05 16:18:10 +010038#define TRACE_SOURCE &trace_strm
39
Christopher Faulet377c5a52018-10-24 21:21:30 +020040extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020041
Christopher Fauleta8a46e22019-07-16 14:53:09 +020042struct pool_head *pool_head_requri = NULL;
43struct pool_head *pool_head_capture = NULL;
44
45
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020046static void http_end_request(struct stream *s);
47static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020048
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020049static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
50static int http_del_hdr_value(char *start, char *end, char **from, char *next);
51static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020052static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
53static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
Christopher Faulet0f226952018-10-22 09:29:56 +020054
Christopher Fauletb58f62b2020-01-13 16:40:13 +010055static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020056static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020057
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020058static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
59static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020060
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020061static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
62static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020063
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020064static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
65static int http_reply_100_continue(struct stream *s);
66static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm);
Christopher Faulet23a3c792018-11-28 10:01:23 +010067
Christopher Faulete0768eb2018-10-03 16:38:02 +020068/* This stream analyser waits for a complete HTTP request. It returns 1 if the
69 * processing can continue on next analysers, or zero if it either needs more
70 * data or wants to immediately abort the request (eg: timeout, error, ...). It
71 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
72 * when it has nothing left to do, and may remove any analyser when it wants to
73 * abort.
74 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020075int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020076{
Christopher Faulet9768c262018-10-22 09:34:31 +020077
Christopher Faulete0768eb2018-10-03 16:38:02 +020078 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020079 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020080 *
Christopher Faulet9768c262018-10-22 09:34:31 +020081 * Once the start line and all headers are received, we may perform a
82 * capture of the error (if any), and we will set a few fields. We also
83 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020084 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020085 struct session *sess = s->sess;
86 struct http_txn *txn = s->txn;
87 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020088 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010089 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020090
Christopher Fauleteea8fc72019-11-05 16:18:10 +010091 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020092
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010093 htx = htxbuf(&req->buf);
Christopher Faulet9768c262018-10-22 09:34:31 +020094
Willy Tarreau4236f032019-03-05 10:43:32 +010095 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +020096 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +010097 stream_inc_http_req_ctr(s);
98 stream_inc_http_err_ctr(s);
99 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200100 if (htx->flags & HTX_FL_PARSING_ERROR)
101 goto return_bad_req;
102 else
103 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100104 }
105
Christopher Faulete0768eb2018-10-03 16:38:02 +0200106 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200107 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200108
109 /* If there is data available for analysis, log the end of the idle time. */
Christopher Faulet870aad92018-11-29 15:23:46 +0100110 if (c_data(req) && s->logs.t_idle == -1) {
111 const struct cs_info *csinfo = si_get_cs_info(objt_cs(s->si[0].end));
112
113 s->logs.t_idle = ((csinfo)
114 ? csinfo->t_idle
115 : tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake);
116 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200117
Christopher Faulete0768eb2018-10-03 16:38:02 +0200118 /*
119 * Now we quickly check if we have found a full valid request.
120 * If not so, we check the FD and buffer states before leaving.
121 * A full request is indicated by the fact that we have seen
122 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
123 * requests are checked first. When waiting for a second request
124 * on a keep-alive stream, if we encounter and error, close, t/o,
125 * we note the error in the stream flags but don't set any state.
126 * Since the error will be noted there, it will not be counted by
127 * process_stream() as a frontend error.
128 * Last, we may increase some tracked counters' http request errors on
129 * the cases that are deliberately the client's fault. For instance,
130 * a timeout or connection reset is not counted as an error. However
131 * a bad request is.
132 */
Christopher Faulet29f17582019-05-23 11:03:26 +0200133 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200134 if (htx->flags & HTX_FL_UPGRADE)
135 goto failed_keep_alive;
136
Christopher Faulet9768c262018-10-22 09:34:31 +0200137 /* 1: have we encountered a read error ? */
138 if (req->flags & CF_READ_ERROR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200139 if (!(s->flags & SF_ERR_MASK))
140 s->flags |= SF_ERR_CLICL;
141
142 if (txn->flags & TX_WAIT_NEXT_RQ)
143 goto failed_keep_alive;
144
145 if (sess->fe->options & PR_O_IGNORE_PRB)
146 goto failed_keep_alive;
147
Christopher Faulet9768c262018-10-22 09:34:31 +0200148 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200149 stream_inc_http_req_ctr(s);
150 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100151 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200152 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100153 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200154
Christopher Faulet9768c262018-10-22 09:34:31 +0200155 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200156 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet9768c262018-10-22 09:34:31 +0200157 req->analysers &= AN_REQ_FLT_END;
158
Christopher Faulete0768eb2018-10-03 16:38:02 +0200159 if (!(s->flags & SF_FINST_MASK))
160 s->flags |= SF_FINST_R;
161 return 0;
162 }
163
Christopher Faulet9768c262018-10-22 09:34:31 +0200164 /* 2: has the read timeout expired ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200165 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
166 if (!(s->flags & SF_ERR_MASK))
167 s->flags |= SF_ERR_CLITO;
168
169 if (txn->flags & TX_WAIT_NEXT_RQ)
170 goto failed_keep_alive;
171
172 if (sess->fe->options & PR_O_IGNORE_PRB)
173 goto failed_keep_alive;
174
Christopher Faulet9768c262018-10-22 09:34:31 +0200175 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200176 stream_inc_http_req_ctr(s);
177 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100178 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200179 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100180 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200181
Christopher Faulet9768c262018-10-22 09:34:31 +0200182 txn->status = 408;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200183 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200184 req->analysers &= AN_REQ_FLT_END;
185
Christopher Faulete0768eb2018-10-03 16:38:02 +0200186 if (!(s->flags & SF_FINST_MASK))
187 s->flags |= SF_FINST_R;
188 return 0;
189 }
190
Christopher Faulet9768c262018-10-22 09:34:31 +0200191 /* 3: have we encountered a close ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200192 else if (req->flags & CF_SHUTR) {
193 if (!(s->flags & SF_ERR_MASK))
194 s->flags |= SF_ERR_CLICL;
195
196 if (txn->flags & TX_WAIT_NEXT_RQ)
197 goto failed_keep_alive;
198
199 if (sess->fe->options & PR_O_IGNORE_PRB)
200 goto failed_keep_alive;
201
Christopher Faulete0768eb2018-10-03 16:38:02 +0200202 stream_inc_http_err_ctr(s);
203 stream_inc_http_req_ctr(s);
204 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100205 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200206 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100207 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200208
Christopher Faulet9768c262018-10-22 09:34:31 +0200209 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200210 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200211 req->analysers &= AN_REQ_FLT_END;
212
Christopher Faulete0768eb2018-10-03 16:38:02 +0200213 if (!(s->flags & SF_FINST_MASK))
214 s->flags |= SF_FINST_R;
215 return 0;
216 }
217
218 channel_dont_connect(req);
219 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
220 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100221
Christopher Faulet9768c262018-10-22 09:34:31 +0200222 if (sess->listener->options & LI_O_NOQUICKACK && htx_is_not_empty(htx) &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200223 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
224 /* We need more data, we have to re-enable quick-ack in case we
225 * previously disabled it, otherwise we might cause the client
226 * to delay next data.
227 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100228 conn_set_quickack(objt_conn(sess->origin), 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200229 }
Willy Tarreau1a18b542018-12-11 16:37:42 +0100230
Christopher Faulet47365272018-10-31 17:40:50 +0100231 if ((req->flags & CF_READ_PARTIAL) && (txn->flags & TX_WAIT_NEXT_RQ)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200232 /* If the client starts to talk, let's fall back to
233 * request timeout processing.
234 */
235 txn->flags &= ~TX_WAIT_NEXT_RQ;
236 req->analyse_exp = TICK_ETERNITY;
237 }
238
239 /* just set the request timeout once at the beginning of the request */
240 if (!tick_isset(req->analyse_exp)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100241 if ((txn->flags & TX_WAIT_NEXT_RQ) && tick_isset(s->be->timeout.httpka))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200242 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
243 else
244 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
245 }
246
247 /* we're not ready yet */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100248 DBG_TRACE_DEVEL("waiting for the request",
249 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200250 return 0;
251
252 failed_keep_alive:
253 /* Here we process low-level errors for keep-alive requests. In
254 * short, if the request is not the first one and it experiences
255 * a timeout, read error or shutdown, we just silently close so
256 * that the client can try again.
257 */
258 txn->status = 0;
259 msg->msg_state = HTTP_MSG_RQBEFORE;
260 req->analysers &= AN_REQ_FLT_END;
261 s->logs.logwait = 0;
262 s->logs.level = 0;
263 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200264 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100265 DBG_TRACE_DEVEL("leaving by closing K/A connection",
266 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200267 return 0;
268 }
269
Christopher Faulet9768c262018-10-22 09:34:31 +0200270 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200271 stream_inc_http_req_ctr(s);
272 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
273
Christopher Faulet9768c262018-10-22 09:34:31 +0200274 /* kill the pending keep-alive timeout */
275 txn->flags &= ~TX_WAIT_NEXT_RQ;
276 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200277
Christopher Faulet29f17582019-05-23 11:03:26 +0200278 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200279 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100280
Christopher Faulet9768c262018-10-22 09:34:31 +0200281 /* 0: we might have to print this header in debug mode */
282 if (unlikely((global.mode & MODE_DEBUG) &&
283 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
284 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200285
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200286 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200287
Christopher Fauleta3f15502019-05-13 15:27:23 +0200288 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200289 struct htx_blk *blk = htx_get_blk(htx, pos);
290 enum htx_blk_type type = htx_get_blk_type(blk);
291
292 if (type == HTX_BLK_EOH)
293 break;
294 if (type != HTX_BLK_HDR)
295 continue;
296
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200297 http_debug_hdr("clihdr", s,
298 htx_get_blk_name(htx, blk),
299 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200300 }
301 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200302
303 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100304 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200305 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100306 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100307 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200308 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100309 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +0100310 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100311 if (sl->flags & HTX_SL_F_BODYLESS)
312 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200313
314 /* we can make use of server redirect on GET and HEAD */
315 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
316 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100317 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200318 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200319 goto return_bad_req;
320 }
321
322 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100323 * 2: check if the URI matches the monitor_uri. We have to do this for
324 * every request which gets in, because the monitor-uri is defined by
325 * the frontend. If the monitor-uri starts with a '/', the matching is
326 * done against the request's path. Otherwise, the request's uri is
327 * used. It is a workaround to let HTTP/2 health-checks work as
328 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200329 */
330 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Faulet6072beb2020-02-18 15:34:58 +0100331 ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)),
332 ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) ||
333 isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200334 /*
335 * We have found the monitor URI
336 */
337 struct acl_cond *cond;
338
339 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100340 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200341
342 /* Check if we want to fail this monitor request or not */
343 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
344 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
345
346 ret = acl_pass(ret);
347 if (cond->pol == ACL_COND_UNLESS)
348 ret = !ret;
349
350 if (ret) {
351 /* we fail this request, let's return 503 service unavail */
352 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200353 if (!(s->flags & SF_ERR_MASK))
354 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
355 goto return_prx_cond;
356 }
357 }
358
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800359 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200360 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200361 if (!(s->flags & SF_ERR_MASK))
362 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
363 goto return_prx_cond;
364 }
365
366 /*
367 * 3: Maybe we have to copy the original REQURI for the logs ?
368 * Note: we cannot log anymore if the request has been
369 * classified as invalid.
370 */
371 if (unlikely(s->logs.logwait & LW_REQ)) {
372 /* we have a complete HTTP request that we must log */
373 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200374 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200375
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200376 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200377 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200378
379 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
380 s->do_log(s);
381 } else {
382 ha_alert("HTTP logging : out of memory.\n");
383 }
384 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200385
Christopher Faulete0768eb2018-10-03 16:38:02 +0200386 /* if the frontend has "option http-use-proxy-header", we'll check if
387 * we have what looks like a proxied connection instead of a connection,
388 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
389 * Note that this is *not* RFC-compliant, however browsers and proxies
390 * happen to do that despite being non-standard :-(
391 * We consider that a request not beginning with either '/' or '*' is
392 * a proxied connection, which covers both "scheme://location" and
393 * CONNECT ip:port.
394 */
395 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100396 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200397 txn->flags |= TX_USE_PX_CONN;
398
Christopher Faulete0768eb2018-10-03 16:38:02 +0200399 /* 5: we may need to capture headers */
400 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200401 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200402
Christopher Faulete0768eb2018-10-03 16:38:02 +0200403 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200404 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200405 req->analysers |= AN_REQ_HTTP_BODY;
406
407 /*
408 * RFC7234#4:
409 * A cache MUST write through requests with methods
410 * that are unsafe (Section 4.2.1 of [RFC7231]) to
411 * the origin server; i.e., a cache is not allowed
412 * to generate a reply to such a request before
413 * having forwarded the request and having received
414 * a corresponding response.
415 *
416 * RFC7231#4.2.1:
417 * Of the request methods defined by this
418 * specification, the GET, HEAD, OPTIONS, and TRACE
419 * methods are defined to be safe.
420 */
421 if (likely(txn->meth == HTTP_METH_GET ||
422 txn->meth == HTTP_METH_HEAD ||
423 txn->meth == HTTP_METH_OPTIONS ||
424 txn->meth == HTTP_METH_TRACE))
425 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
426
427 /* end of job, return OK */
428 req->analysers &= ~an_bit;
429 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200430
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100431 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200432 return 1;
433
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200434 return_int_err:
435 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200436 if (!(s->flags & SF_ERR_MASK))
437 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100438 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200439 if (sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100440 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200441 goto return_prx_cond;
442
Christopher Faulete0768eb2018-10-03 16:38:02 +0200443 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200444 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100445 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200446 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100447 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200448 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200449
450 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200451 http_reply_and_close(s, txn->status, http_error_message(s));
452
Christopher Faulete0768eb2018-10-03 16:38:02 +0200453 if (!(s->flags & SF_ERR_MASK))
454 s->flags |= SF_ERR_PRXCOND;
455 if (!(s->flags & SF_FINST_MASK))
456 s->flags |= SF_FINST_R;
457
458 req->analysers &= AN_REQ_FLT_END;
459 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100460 DBG_TRACE_DEVEL("leaving on error",
461 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200462 return 0;
463}
464
465
466/* This stream analyser runs all HTTP request processing which is common to
467 * frontends and backends, which means blocking ACLs, filters, connection-close,
468 * reqadd, stats and redirects. This is performed for the designated proxy.
469 * It returns 1 if the processing can continue on next analysers, or zero if it
470 * either needs more data or wants to immediately abort the request (eg: deny,
471 * error, ...).
472 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200473int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200474{
475 struct session *sess = s->sess;
476 struct http_txn *txn = s->txn;
477 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200478 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200479 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200480 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200481 struct connection *conn = objt_conn(sess->origin);
482
483 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
484 /* we need more data */
485 goto return_prx_yield;
486 }
487
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100488 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200489
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100490 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200491
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200492 /* just in case we have some per-backend tracking. Only called the first
493 * execution of the analyser. */
494 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
495 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200496
497 /* evaluate http-request rules */
498 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100499 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200500
501 switch (verdict) {
502 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
503 goto return_prx_yield;
504
505 case HTTP_RULE_RES_CONT:
506 case HTTP_RULE_RES_STOP: /* nothing to do */
507 break;
508
509 case HTTP_RULE_RES_DENY: /* deny or tarpit */
510 if (txn->flags & TX_CLTARPIT)
511 goto tarpit;
512 goto deny;
513
514 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
515 goto return_prx_cond;
516
517 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
518 goto done;
519
520 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
521 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100522
523 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
524 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200525 }
526 }
527
528 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100529 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200530 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200531
Christopher Fauletff2759f2018-10-24 11:13:16 +0200532 ctx.blk = NULL;
533 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
534 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100535 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200536 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200537 }
538
539 /* OK at this stage, we know that the request was accepted according to
540 * the http-request rules, we can check for the stats. Note that the
541 * URI is detected *before* the req* rules in order not to be affected
542 * by a possible reqrep, while they are processed *after* so that a
543 * reqdeny can still block them. This clearly needs to change in 1.6!
544 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200545 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200546 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100547 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200548 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200549 if (!(s->flags & SF_ERR_MASK))
550 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100551 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200552 }
553
554 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200555 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100556 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200557 /* not all actions implemented: deny, allow, auth */
558
559 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
560 goto deny;
561
562 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
563 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100564
565 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
566 goto return_bad_req;
567
568 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
569 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200570 }
571
Christopher Faulet2571bc62019-03-01 11:44:26 +0100572 /* Proceed with the applets now. */
573 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200574 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100575 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200576
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200577 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100578 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100579
Christopher Faulete0768eb2018-10-03 16:38:02 +0200580 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
581 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
582 if (!(s->flags & SF_FINST_MASK))
583 s->flags |= SF_FINST_R;
584
585 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
586 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
587 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
588 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100589
590 req->flags |= CF_SEND_DONTWAIT;
591 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200592 goto done;
593 }
594
595 /* check whether we have some ACLs set to redirect this request */
596 list_for_each_entry(rule, &px->redirect_rules, list) {
597 if (rule->cond) {
598 int ret;
599
600 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
601 ret = acl_pass(ret);
602 if (rule->cond->pol == ACL_COND_UNLESS)
603 ret = !ret;
604 if (!ret)
605 continue;
606 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200607 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100608 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200609 goto done;
610 }
611
612 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
613 * If this happens, then the data will not come immediately, so we must
614 * send all what we have without waiting. Note that due to the small gain
615 * in waiting for the body of the request, it's easier to simply put the
616 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
617 * itself once used.
618 */
619 req->flags |= CF_SEND_DONTWAIT;
620
621 done: /* done with this analyser, continue with next ones that the calling
622 * points will have set, if any.
623 */
624 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500625 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200626 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100627 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200628 return 1;
629
630 tarpit:
631 /* Allow cookie logging
632 */
633 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200634 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200635
636 /* When a connection is tarpitted, we use the tarpit timeout,
637 * which may be the same as the connect timeout if unspecified.
638 * If unset, then set it to zero because we really want it to
639 * eventually expire. We build the tarpit as an analyser.
640 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100641 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200642
643 /* wipe the request out so that we can drop the connection early
644 * if the client closes first.
645 */
646 channel_dont_connect(req);
647
Christopher Faulete0768eb2018-10-03 16:38:02 +0200648 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
649 req->analysers |= AN_REQ_HTTP_TARPIT;
650 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
651 if (!req->analyse_exp)
652 req->analyse_exp = tick_add(now_ms, 0);
653 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100654 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100655 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100656 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200657 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100658 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200659 goto done_without_exp;
660
661 deny: /* this request was blocked (denied) */
662
663 /* Allow cookie logging
664 */
665 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200666 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200667
Christopher Faulete0768eb2018-10-03 16:38:02 +0200668 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200669 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100670 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100671 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100672 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200673 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100674 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100675 goto return_prx_err;
676
677 return_int_err:
678 txn->status = 500;
679 if (!(s->flags & SF_ERR_MASK))
680 s->flags |= SF_ERR_INTERNAL;
681 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100682 if (s->flags & SF_BE_ASSIGNED)
683 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100684 if (sess->listener->counters)
685 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
686 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200687
688 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200689 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100690 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200691 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100692 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100693 /* fall through */
694
695 return_prx_err:
696 http_reply_and_close(s, txn->status, http_error_message(s));
697 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200698
699 return_prx_cond:
700 if (!(s->flags & SF_ERR_MASK))
701 s->flags |= SF_ERR_PRXCOND;
702 if (!(s->flags & SF_FINST_MASK))
703 s->flags |= SF_FINST_R;
704
705 req->analysers &= AN_REQ_FLT_END;
706 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100707 DBG_TRACE_DEVEL("leaving on error",
708 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200709 return 0;
710
711 return_prx_yield:
712 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100713 DBG_TRACE_DEVEL("waiting for more data",
714 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200715 return 0;
716}
717
718/* This function performs all the processing enabled for the current request.
719 * It returns 1 if the processing can continue on next analysers, or zero if it
720 * needs more data, encounters an error, or wants to immediately abort the
721 * request. It relies on buffers flags, and updates s->req.analysers.
722 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200723int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200724{
725 struct session *sess = s->sess;
726 struct http_txn *txn = s->txn;
727 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200728 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200729 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
730
731 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
732 /* we need more data */
733 channel_dont_connect(req);
734 return 0;
735 }
736
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100737 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200738
739 /*
740 * Right now, we know that we have processed the entire headers
741 * and that unwanted requests have been filtered out. We can do
742 * whatever we want with the remaining request. Also, now we
743 * may have separate values for ->fe, ->be.
744 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100745 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200746
747 /*
748 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200749 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200750 */
751 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100752 struct htx_sl *sl;
753 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200754
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200755 if (!sockaddr_alloc(&s->target_addr)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200756 if (!(s->flags & SF_ERR_MASK))
757 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100758 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200759 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200760 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100761 uri = htx_sl_req_uri(sl);
762 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200763
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200764 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200765 goto return_bad_req;
766
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200767 s->target = &s->be->obj_type;
768 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
769
Christopher Faulete0768eb2018-10-03 16:38:02 +0200770 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200771 * uri.ptr and path.ptr (excluded). If it was not found, we need
772 * to replace from all the uri by a single "/".
773 *
774 * Instead of rewritting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100775 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200776 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200777 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100778 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200779 }
780
781 /*
782 * 7: Now we can work with the cookies.
783 * Note that doing so might move headers in the request, but
784 * the fields will stay coherent and the URI will not move.
785 * This should only be performed in the backend.
786 */
787 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200788 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200789
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100790 /* 8: Generate unique ID if a "unique-id-format" is defined.
791 *
792 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
793 * fetches only available in the HTTP request processing stage.
794 */
795 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100796 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200797
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100798 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100799 if (!(s->flags & SF_ERR_MASK))
800 s->flags |= SF_ERR_RESOURCE;
801 goto return_int_err;
802 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200803
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100804 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100805 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100806 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100807 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200808 }
809
810 /*
811 * 9: add X-Forwarded-For if either the frontend or the backend
812 * asks for it.
813 */
814 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200815 struct http_hdr_ctx ctx = { .blk = NULL };
816 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
817 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
818
Christopher Faulete0768eb2018-10-03 16:38:02 +0200819 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200820 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200821 /* The header is set to be added only if none is present
822 * and we found it, so don't do anything.
823 */
824 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200825 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200826 /* Add an X-Forwarded-For header unless the source IP is
827 * in the 'except' network range.
828 */
829 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200830 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200831 != sess->fe->except_net.s_addr) &&
832 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200833 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200834 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200835 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200836
837 /* Note: we rely on the backend to get the header name to be used for
838 * x-forwarded-for, because the header is really meant for the backends.
839 * However, if the backend did not specify any option, we have to rely
840 * on the frontend's header name.
841 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200842 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
843 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100844 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200845 }
846 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200847 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200848 /* FIXME: for the sake of completeness, we should also support
849 * 'except' here, although it is mostly useless in this case.
850 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200851 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200852
Christopher Faulete0768eb2018-10-03 16:38:02 +0200853 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200854 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200855 pn, sizeof(pn));
856
857 /* Note: we rely on the backend to get the header name to be used for
858 * x-forwarded-for, because the header is really meant for the backends.
859 * However, if the backend did not specify any option, we have to rely
860 * on the frontend's header name.
861 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200862 chunk_printf(&trash, "%s", pn);
863 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100864 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200865 }
866 }
867
868 /*
869 * 10: add X-Original-To if either the frontend or the backend
870 * asks for it.
871 */
872 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
873
874 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200875 if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET && conn_get_dst(cli_conn)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200876 /* Add an X-Original-To header unless the destination IP is
877 * in the 'except' network range.
878 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200879 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200880 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200881 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200882 != sess->fe->except_to.s_addr) &&
883 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200884 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200885 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200886 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200887 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200888
889 /* Note: we rely on the backend to get the header name to be used for
890 * x-original-to, because the header is really meant for the backends.
891 * However, if the backend did not specify any option, we have to rely
892 * on the frontend's header name.
893 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200894 if (s->be->orgto_hdr_len)
895 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
896 else
897 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200898
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200899 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
900 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100901 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200902 }
903 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200904 }
905
Christopher Faulete0768eb2018-10-03 16:38:02 +0200906 /* If we have no server assigned yet and we're balancing on url_param
907 * with a POST request, we may be interested in checking the body for
908 * that parameter. This will be done in another analyser.
909 */
910 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100911 s->txn->meth == HTTP_METH_POST &&
912 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200913 channel_dont_connect(req);
914 req->analysers |= AN_REQ_HTTP_BODY;
915 }
916
917 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
918 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100919
Christopher Faulete0768eb2018-10-03 16:38:02 +0200920 /* We expect some data from the client. Unless we know for sure
921 * we already have a full request, we have to re-enable quick-ack
922 * in case we previously disabled it, otherwise we might cause
923 * the client to delay further data.
924 */
925 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200926 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100927 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200928
929 /*************************************************************
930 * OK, that's finished for the headers. We have done what we *
931 * could. Let's switch to the DATA state. *
932 ************************************************************/
933 req->analyse_exp = TICK_ETERNITY;
934 req->analysers &= ~an_bit;
935
936 s->logs.tv_request = now;
937 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100938 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200939 return 1;
940
Christopher Fauletb8a53712019-12-16 11:29:38 +0100941 return_int_err:
942 txn->status = 500;
943 if (!(s->flags & SF_ERR_MASK))
944 s->flags |= SF_ERR_INTERNAL;
945 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100946 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100947 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100948 if (sess->listener->counters)
949 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
950 goto return_prx_cond;
951
Christopher Faulete0768eb2018-10-03 16:38:02 +0200952 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200953 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100954 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200955 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100956 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100957 /* fall through */
958
959 return_prx_cond:
960 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200961
962 if (!(s->flags & SF_ERR_MASK))
963 s->flags |= SF_ERR_PRXCOND;
964 if (!(s->flags & SF_FINST_MASK))
965 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100966
967 req->analysers &= AN_REQ_FLT_END;
968 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100969 DBG_TRACE_DEVEL("leaving on error",
970 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200971 return 0;
972}
973
974/* This function is an analyser which processes the HTTP tarpit. It always
975 * returns zero, at the beginning because it prevents any other processing
976 * from occurring, and at the end because it terminates the request.
977 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200978int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200979{
980 struct http_txn *txn = s->txn;
981
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100982 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200983 /* This connection is being tarpitted. The CLIENT side has
984 * already set the connect expiration date to the right
985 * timeout. We just have to check that the client is still
986 * there and that the timeout has not expired.
987 */
988 channel_dont_connect(req);
989 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100990 !tick_is_expired(req->analyse_exp, now_ms)) {
991 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
992 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200993 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100994 }
995
Christopher Faulete0768eb2018-10-03 16:38:02 +0200996
997 /* We will set the queue timer to the time spent, just for
998 * logging purposes. We fake a 500 server error, so that the
999 * attacker will not suspect his connection has been tarpitted.
1000 * It will not cause trouble to the logs because we can exclude
1001 * the tarpitted connections by filtering on the 'PT' status flags.
1002 */
1003 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1004
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02001005 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001006
1007 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001008 req->analysers &= AN_REQ_FLT_END;
1009 req->analyse_exp = TICK_ETERNITY;
1010
1011 if (!(s->flags & SF_ERR_MASK))
1012 s->flags |= SF_ERR_PRXCOND;
1013 if (!(s->flags & SF_FINST_MASK))
1014 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001015
1016 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001017 return 0;
1018}
1019
1020/* This function is an analyser which waits for the HTTP request body. It waits
1021 * for either the buffer to be full, or the full advertised contents to have
1022 * reached the buffer. It must only be called after the standard HTTP request
1023 * processing has occurred, because it expects the request to be parsed and will
1024 * look for the Expect header. It may send a 100-Continue interim response. It
1025 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
1026 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
1027 * needs to read more data, or 1 once it has completed its analysis.
1028 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001029int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001030{
1031 struct session *sess = s->sess;
1032 struct http_txn *txn = s->txn;
1033 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001034 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001035
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001036 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001037
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001038 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001039
Willy Tarreau4236f032019-03-05 10:43:32 +01001040 if (htx->flags & HTX_FL_PARSING_ERROR)
1041 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001042 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1043 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001044
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001045 if (msg->msg_state < HTTP_MSG_BODY)
1046 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001047
Christopher Faulete0768eb2018-10-03 16:38:02 +02001048 /* We have to parse the HTTP request body to find any required data.
1049 * "balance url_param check_post" should have been the only way to get
1050 * into this. We were brought here after HTTP header analysis, so all
1051 * related structures are ready.
1052 */
1053
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001054 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001055 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001056 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001057 }
1058
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001059 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001060
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001061 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1062 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001063 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001064 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001065 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001066 goto http_end;
1067
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001068 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001069 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1070 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001071 if (!(s->flags & SF_ERR_MASK))
1072 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001073 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1074 if (sess->listener->counters)
1075 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1076 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001077 }
1078
1079 /* we get here if we need to wait for more data */
1080 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1081 /* Not enough data. We'll re-use the http-request
1082 * timeout here. Ideally, we should set the timeout
1083 * relative to the accept() date. We just set the
1084 * request timeout once at the beginning of the
1085 * request.
1086 */
1087 channel_dont_connect(req);
1088 if (!tick_isset(req->analyse_exp))
1089 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001090 DBG_TRACE_DEVEL("waiting for more data",
1091 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001092 return 0;
1093 }
1094
1095 http_end:
1096 /* The situation will not evolve, so let's give up on the analysis. */
1097 s->logs.tv_request = now; /* update the request timer to reflect full request */
1098 req->analysers &= ~an_bit;
1099 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001100 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001101 return 1;
1102
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001103 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001104 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001105 if (!(s->flags & SF_ERR_MASK))
1106 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001107 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001108 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +01001109 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001110 if (sess->listener->counters)
1111 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
1112 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001113
Christopher Faulete0768eb2018-10-03 16:38:02 +02001114 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001115 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001116 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1117 if (sess->listener->counters)
1118 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1119 /* fall through */
1120
1121 return_prx_cond:
1122 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001123
1124 if (!(s->flags & SF_ERR_MASK))
1125 s->flags |= SF_ERR_PRXCOND;
1126 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001127 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001128
Christopher Faulete0768eb2018-10-03 16:38:02 +02001129 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001130 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001131 DBG_TRACE_DEVEL("leaving on error",
1132 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001133 return 0;
1134}
1135
1136/* This function is an analyser which forwards request body (including chunk
1137 * sizes if any). It is called as soon as we must forward, even if we forward
1138 * zero byte. The only situation where it must not be called is when we're in
1139 * tunnel mode and we want to forward till the close. It's used both to forward
1140 * remaining data and to resync after end of body. It expects the msg_state to
1141 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1142 * read more data, or 1 once we can go on with next request or end the stream.
1143 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1144 * bytes of pending data + the headers if not already done.
1145 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001146int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001147{
1148 struct session *sess = s->sess;
1149 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001150 struct http_msg *msg = &txn->req;
1151 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001152 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001153 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001154
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001155 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001156
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001157 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001158
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001159 if (htx->flags & HTX_FL_PARSING_ERROR)
1160 goto return_bad_req;
1161 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1162 goto return_int_err;
1163
Christopher Faulete0768eb2018-10-03 16:38:02 +02001164 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1165 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1166 /* Output closed while we were sending data. We must abort and
1167 * wake the other side up.
1168 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001169
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001170 /* Don't abort yet if we had L7 retries activated and it
1171 * was a write error, we may recover.
1172 */
1173 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001174 (s->si[1].flags & SI_FL_L7_RETRY)) {
1175 DBG_TRACE_DEVEL("leaving on L7 retry",
1176 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001177 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001178 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001179 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001180 http_end_request(s);
1181 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001182 DBG_TRACE_DEVEL("leaving on error",
1183 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001184 return 1;
1185 }
1186
1187 /* Note that we don't have to send 100-continue back because we don't
1188 * need the data to complete our job, and it's up to the server to
1189 * decide whether to return 100, 417 or anything else in return of
1190 * an "Expect: 100-continue" header.
1191 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001192 if (msg->msg_state == HTTP_MSG_BODY)
1193 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001194
Christopher Faulete0768eb2018-10-03 16:38:02 +02001195 /* in most states, we should abort in case of early close */
1196 channel_auto_close(req);
1197
1198 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001199 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001200 if (req->flags & CF_EOI)
1201 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001202 }
1203 else {
1204 /* We can't process the buffer's contents yet */
1205 req->flags |= CF_WAKE_WRITE;
1206 goto missing_data_or_waiting;
1207 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001208 }
1209
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001210 if (msg->msg_state >= HTTP_MSG_ENDING)
1211 goto ending;
1212
1213 if (txn->meth == HTTP_METH_CONNECT) {
1214 msg->msg_state = HTTP_MSG_ENDING;
1215 goto ending;
1216 }
1217
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001218 /* Forward input data. We get it by removing all outgoing data not
1219 * forwarded yet from HTX data size. If there are some data filters, we
1220 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001221 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001222 if (HAS_REQ_DATA_FILTERS(s)) {
1223 ret = flt_http_payload(s, msg, htx->data);
1224 if (ret < 0)
1225 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001226 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001227 }
1228 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001229 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001230 if (msg->flags & HTTP_MSGF_XFER_LEN)
1231 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001232 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001233
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001234 if (htx->data != co_data(req))
1235 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001236
Christopher Faulet9768c262018-10-22 09:34:31 +02001237 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001238 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1239 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001240 */
1241 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1242 goto missing_data_or_waiting;
1243
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001244 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001245
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001246 ending:
1247 /* other states, ENDING...TUNNEL */
1248 if (msg->msg_state >= HTTP_MSG_DONE)
1249 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001250
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001251 if (HAS_REQ_DATA_FILTERS(s)) {
1252 ret = flt_http_end(s, msg);
1253 if (ret <= 0) {
1254 if (!ret)
1255 goto missing_data_or_waiting;
1256 goto return_bad_req;
1257 }
1258 }
1259
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001260 if (txn->meth == HTTP_METH_CONNECT)
1261 msg->msg_state = HTTP_MSG_TUNNEL;
1262 else {
1263 msg->msg_state = HTTP_MSG_DONE;
1264 req->to_forward = 0;
1265 }
1266
1267 done:
1268 /* we don't want to forward closes on DONE except in tunnel mode. */
1269 if (!(txn->flags & TX_CON_WANT_TUN))
1270 channel_dont_close(req);
1271
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001272 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001273 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001274 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001275 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1276 if (req->flags & CF_SHUTW) {
1277 /* request errors are most likely due to the
1278 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001279 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001280 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001281 goto return_bad_req;
1282 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001283 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001284 return 1;
1285 }
1286
1287 /* If "option abortonclose" is set on the backend, we want to monitor
1288 * the client's connection and forward any shutdown notification to the
1289 * server, which will decide whether to close or to go on processing the
1290 * request. We only do that in tunnel mode, and not in other modes since
1291 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001292 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001293 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001294 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001295 s->si[1].flags |= SI_FL_NOLINGER;
1296 channel_auto_close(req);
1297 }
1298 else if (s->txn->meth == HTTP_METH_POST) {
1299 /* POST requests may require to read extra CRLF sent by broken
1300 * browsers and which could cause an RST to be sent upon close
1301 * on some systems (eg: Linux). */
1302 channel_auto_read(req);
1303 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001304 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1305 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001306 return 0;
1307
1308 missing_data_or_waiting:
1309 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001310 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001311 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001312
1313 waiting:
1314 /* waiting for the last bits to leave the buffer */
1315 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001316 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001317
1318 /* When TE: chunked is used, we need to get there again to parse remaining
1319 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1320 * And when content-length is used, we never want to let the possible
1321 * shutdown be forwarded to the other side, as the state machine will
1322 * take care of it once the client responds. It's also important to
1323 * prevent TIME_WAITs from accumulating on the backend side, and for
1324 * HTTP/2 where the last frame comes with a shutdown.
1325 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001326 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001327 channel_dont_close(req);
1328
1329 /* We know that more data are expected, but we couldn't send more that
1330 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1331 * system knows it must not set a PUSH on this first part. Interactive
1332 * modes are already handled by the stream sock layer. We must not do
1333 * this in content-length mode because it could present the MSG_MORE
1334 * flag with the last block of forwarded data, which would cause an
1335 * additional delay to be observed by the receiver.
1336 */
1337 if (msg->flags & HTTP_MSGF_TE_CHNK)
1338 req->flags |= CF_EXPECT_MORE;
1339
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001340 DBG_TRACE_DEVEL("waiting for more data to forward",
1341 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001342 return 0;
1343
Christopher Faulet93e02d82019-03-08 14:18:50 +01001344 return_cli_abort:
1345 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1346 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001347 if (sess->listener->counters)
1348 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001349 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001350 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001351 if (!(s->flags & SF_ERR_MASK))
1352 s->flags |= SF_ERR_CLICL;
1353 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001354 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001355
1356 return_srv_abort:
1357 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1358 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001359 if (sess->listener->counters)
1360 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001361 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001362 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001363 if (!(s->flags & SF_ERR_MASK))
1364 s->flags |= SF_ERR_SRVCL;
1365 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001366 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001367
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001368 return_int_err:
1369 if (!(s->flags & SF_ERR_MASK))
1370 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001371 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001372 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001373 if (sess->listener->counters)
1374 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001375 if (objt_server(s->target))
1376 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001377 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001378 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001379
Christopher Faulet93e02d82019-03-08 14:18:50 +01001380 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001381 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001382 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001383 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001384 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001385 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001386
Christopher Fauletb8a53712019-12-16 11:29:38 +01001387 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001388 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001389 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001390 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001391 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001392 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001393 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001394 }
1395 req->analysers &= AN_REQ_FLT_END;
1396 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 +01001397 if (!(s->flags & SF_ERR_MASK))
1398 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001399 if (!(s->flags & SF_FINST_MASK))
1400 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001401 DBG_TRACE_DEVEL("leaving on error ",
1402 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001403 return 0;
1404}
1405
Olivier Houcharda254a372019-04-05 15:30:12 +02001406/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1407/* Returns 0 if we can attempt to retry, -1 otherwise */
1408static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1409{
1410 struct channel *req, *res;
1411 int co_data;
1412
1413 si->conn_retries--;
1414 if (si->conn_retries < 0)
1415 return -1;
1416
Willy Tarreau223995e2019-05-04 10:38:31 +02001417 if (objt_server(s->target))
1418 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1419 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1420
Olivier Houcharda254a372019-04-05 15:30:12 +02001421 req = &s->req;
1422 res = &s->res;
1423 /* Remove any write error from the request, and read error from the response */
1424 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1425 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1426 res->analysers = 0;
1427 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001428 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001429 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001430 si->exp = TICK_ETERNITY;
1431 res->rex = TICK_ETERNITY;
1432 res->to_forward = 0;
1433 res->analyse_exp = TICK_ETERNITY;
1434 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001435 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001436 si_release_endpoint(&s->si[1]);
1437 b_free(&req->buf);
1438 /* Swap the L7 buffer with the channel buffer */
1439 /* We know we stored the co_data as b_data, so get it there */
1440 co_data = b_data(&si->l7_buffer);
1441 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1442 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1443
1444 co_set_data(req, co_data);
1445 b_reset(&res->buf);
1446 co_set_data(res, 0);
1447 return 0;
1448}
1449
Christopher Faulete0768eb2018-10-03 16:38:02 +02001450/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1451 * processing can continue on next analysers, or zero if it either needs more
1452 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1453 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1454 * when it has nothing left to do, and may remove any analyser when it wants to
1455 * abort.
1456 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001457int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001458{
Christopher Faulet9768c262018-10-22 09:34:31 +02001459 /*
1460 * We will analyze a complete HTTP response to check the its syntax.
1461 *
1462 * Once the start line and all headers are received, we may perform a
1463 * capture of the error (if any), and we will set a few fields. We also
1464 * logging and finally headers capture.
1465 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001466 struct session *sess = s->sess;
1467 struct http_txn *txn = s->txn;
1468 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001469 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001470 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001471 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001472 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001473 int n;
1474
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001475 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001476
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001477 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001478
Willy Tarreau4236f032019-03-05 10:43:32 +01001479 /* Parsing errors are caught here */
1480 if (htx->flags & HTX_FL_PARSING_ERROR)
1481 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001482 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1483 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001484
Christopher Faulete0768eb2018-10-03 16:38:02 +02001485 /*
1486 * Now we quickly check if we have found a full valid response.
1487 * If not so, we check the FD and buffer states before leaving.
1488 * A full response is indicated by the fact that we have seen
1489 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1490 * responses are checked first.
1491 *
1492 * Depending on whether the client is still there or not, we
1493 * may send an error response back or not. Note that normally
1494 * we should only check for HTTP status there, and check I/O
1495 * errors somewhere else.
1496 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001497 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001498 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001499 /* 1: have we encountered a read error ? */
1500 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001501 struct connection *conn = NULL;
1502
Olivier Houchard865d8392019-05-03 22:46:27 +02001503 if (objt_cs(s->si[1].end))
1504 conn = objt_cs(s->si[1].end)->conn;
1505
1506 if (si_b->flags & SI_FL_L7_RETRY &&
1507 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001508 /* If we arrive here, then CF_READ_ERROR was
1509 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001510 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001511 * the SI_FL_L7_RETRY flag, so it's ok not
1512 * to check s->be->retry_type.
1513 */
1514 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1515 return 0;
1516 }
1517
Olivier Houchard6db16992019-05-17 15:40:49 +02001518 if (txn->flags & TX_NOT_FIRST)
1519 goto abort_keep_alive;
1520
Olivier Houcharda798bf52019-03-08 18:52:00 +01001521 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001522 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001523 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001524 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001525 }
1526
Christopher Faulete0768eb2018-10-03 16:38:02 +02001527 rep->analysers &= AN_RES_FLT_END;
1528 txn->status = 502;
1529
1530 /* Check to see if the server refused the early data.
1531 * If so, just send a 425
1532 */
Olivier Houchard865d8392019-05-03 22:46:27 +02001533 if (conn->err_code == CO_ER_SSL_EARLY_FAILED) {
1534 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001535 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001536 do_l7_retry(s, si_b) == 0) {
1537 DBG_TRACE_DEVEL("leaving on L7 retry",
1538 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001539 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001540 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001541 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001542 }
1543
1544 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001545 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001546
1547 if (!(s->flags & SF_ERR_MASK))
1548 s->flags |= SF_ERR_SRVCL;
1549 if (!(s->flags & SF_FINST_MASK))
1550 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001551 DBG_TRACE_DEVEL("leaving on error",
1552 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001553 return 0;
1554 }
1555
Christopher Faulet9768c262018-10-22 09:34:31 +02001556 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001557 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001558 if ((si_b->flags & SI_FL_L7_RETRY) &&
1559 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001560 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1561 DBG_TRACE_DEVEL("leaving on L7 retry",
1562 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001563 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001564 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001565 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001566 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001567 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001568 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001569 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001570 }
1571
Christopher Faulete0768eb2018-10-03 16:38:02 +02001572 rep->analysers &= AN_RES_FLT_END;
1573 txn->status = 504;
1574 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001575 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001576
1577 if (!(s->flags & SF_ERR_MASK))
1578 s->flags |= SF_ERR_SRVTO;
1579 if (!(s->flags & SF_FINST_MASK))
1580 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001581 DBG_TRACE_DEVEL("leaving on error",
1582 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001583 return 0;
1584 }
1585
Christopher Faulet9768c262018-10-22 09:34:31 +02001586 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001587 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001588 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1589 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001590 if (sess->listener->counters)
1591 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 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.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001594
1595 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001596 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001597 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001598
1599 if (!(s->flags & SF_ERR_MASK))
1600 s->flags |= SF_ERR_CLICL;
1601 if (!(s->flags & SF_FINST_MASK))
1602 s->flags |= SF_FINST_H;
1603
1604 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001605 DBG_TRACE_DEVEL("leaving on error",
1606 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001607 return 0;
1608 }
1609
Christopher Faulet9768c262018-10-22 09:34:31 +02001610 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001611 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001612 if ((si_b->flags & SI_FL_L7_RETRY) &&
1613 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001614 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1615 DBG_TRACE_DEVEL("leaving on L7 retry",
1616 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001617 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001618 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001619 }
1620
Olivier Houchard6db16992019-05-17 15:40:49 +02001621 if (txn->flags & TX_NOT_FIRST)
1622 goto abort_keep_alive;
1623
Olivier Houcharda798bf52019-03-08 18:52:00 +01001624 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001625 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001626 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001627 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001628 }
1629
Christopher Faulete0768eb2018-10-03 16:38:02 +02001630 rep->analysers &= AN_RES_FLT_END;
1631 txn->status = 502;
1632 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001633 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001634
1635 if (!(s->flags & SF_ERR_MASK))
1636 s->flags |= SF_ERR_SRVCL;
1637 if (!(s->flags & SF_FINST_MASK))
1638 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001639 DBG_TRACE_DEVEL("leaving on error",
1640 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001641 return 0;
1642 }
1643
Christopher Faulet9768c262018-10-22 09:34:31 +02001644 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001645 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001646 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001647 goto abort_keep_alive;
1648
Olivier Houcharda798bf52019-03-08 18:52:00 +01001649 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001650 if (objt_server(s->target))
1651 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001652 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001653
1654 if (!(s->flags & SF_ERR_MASK))
1655 s->flags |= SF_ERR_CLICL;
1656 if (!(s->flags & SF_FINST_MASK))
1657 s->flags |= SF_FINST_H;
1658
1659 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001660 DBG_TRACE_DEVEL("leaving on error",
1661 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001662 return 0;
1663 }
1664
1665 channel_dont_close(rep);
1666 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001667 DBG_TRACE_DEVEL("waiting for more data",
1668 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001669 return 0;
1670 }
1671
1672 /* More interesting part now : we know that we have a complete
1673 * response which at least looks like HTTP. We have an indicator
1674 * of each header's length, so we can parse them quickly.
1675 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001676 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001677 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001678 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001679
Christopher Faulet9768c262018-10-22 09:34:31 +02001680 /* 0: we might have to print this header in debug mode */
1681 if (unlikely((global.mode & MODE_DEBUG) &&
1682 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1683 int32_t pos;
1684
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001685 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001686
Christopher Fauleta3f15502019-05-13 15:27:23 +02001687 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001688 struct htx_blk *blk = htx_get_blk(htx, pos);
1689 enum htx_blk_type type = htx_get_blk_type(blk);
1690
1691 if (type == HTX_BLK_EOH)
1692 break;
1693 if (type != HTX_BLK_HDR)
1694 continue;
1695
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001696 http_debug_hdr("srvhdr", s,
1697 htx_get_blk_name(htx, blk),
1698 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001699 }
1700 }
1701
Christopher Faulet03599112018-11-27 11:21:21 +01001702 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001703 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001704 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001705 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001706 if (sl->flags & HTX_SL_F_XFER_LEN) {
1707 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001708 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001709 if (sl->flags & HTX_SL_F_BODYLESS)
1710 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001711 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001712
1713 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001714 if (n < 1 || n > 5)
1715 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001716
Christopher Faulete0768eb2018-10-03 16:38:02 +02001717 /* when the client triggers a 4xx from the server, it's most often due
1718 * to a missing object or permission. These events should be tracked
1719 * because if they happen often, it may indicate a brute force or a
1720 * vulnerability scan.
1721 */
1722 if (n == 4)
1723 stream_inc_http_err_ctr(s);
1724
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001725 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001726 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001727 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1728 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001729
Christopher Faulete0768eb2018-10-03 16:38:02 +02001730 /* Adjust server's health based on status code. Note: status codes 501
1731 * and 505 are triggered on demand by client request, so we must not
1732 * count them as server failures.
1733 */
1734 if (objt_server(s->target)) {
1735 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001736 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001737 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001738 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001739 }
1740
1741 /*
1742 * We may be facing a 100-continue response, or any other informational
1743 * 1xx response which is non-final, in which case this is not the right
1744 * response, and we're waiting for the next one. Let's allow this response
1745 * to go to the client and wait for the next one. There's an exception for
1746 * 101 which is used later in the code to switch protocols.
1747 */
1748 if (txn->status < 200 &&
1749 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001750 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001751 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001752 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001753 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001754 txn->status = 0;
1755 s->logs.t_data = -1; /* was not a response yet */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001756 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001757 }
1758
1759 /*
1760 * 2: check for cacheability.
1761 */
1762
1763 switch (txn->status) {
1764 case 200:
1765 case 203:
1766 case 204:
1767 case 206:
1768 case 300:
1769 case 301:
1770 case 404:
1771 case 405:
1772 case 410:
1773 case 414:
1774 case 501:
1775 break;
1776 default:
1777 /* RFC7231#6.1:
1778 * Responses with status codes that are defined as
1779 * cacheable by default (e.g., 200, 203, 204, 206,
1780 * 300, 301, 404, 405, 410, 414, and 501 in this
1781 * specification) can be reused by a cache with
1782 * heuristic expiration unless otherwise indicated
1783 * by the method definition or explicit cache
1784 * controls [RFC7234]; all other status codes are
1785 * not cacheable by default.
1786 */
1787 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1788 break;
1789 }
1790
1791 /*
1792 * 3: we may need to capture headers
1793 */
1794 s->logs.logwait &= ~LW_RESP;
1795 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001796 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001797
Christopher Faulet9768c262018-10-22 09:34:31 +02001798 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001799 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1800 txn->status == 101)) {
1801 /* Either we've established an explicit tunnel, or we're
1802 * switching the protocol. In both cases, we're very unlikely
1803 * to understand the next protocols. We have to switch to tunnel
1804 * mode, so that we transfer the request and responses then let
1805 * this protocol pass unmodified. When we later implement specific
1806 * parsers for such protocols, we'll want to check the Upgrade
1807 * header which contains information about that protocol for
1808 * responses with status 101 (eg: see RFC2817 about TLS).
1809 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001810 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001811 }
1812
Christopher Faulet61608322018-11-23 16:23:45 +01001813 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1814 * 407 (Proxy-Authenticate) responses and set the connection to private
1815 */
1816 srv_conn = cs_conn(objt_cs(s->si[1].end));
1817 if (srv_conn) {
1818 struct ist hdr;
1819 struct http_hdr_ctx ctx;
1820
1821 if (txn->status == 401)
1822 hdr = ist("WWW-Authenticate");
1823 else if (txn->status == 407)
1824 hdr = ist("Proxy-Authenticate");
1825 else
1826 goto end;
1827
1828 ctx.blk = NULL;
1829 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001830 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1831 * possibly followed by blanks and a base64 string, the connection
1832 * is private. Since it's a mess to deal with, we only check for
1833 * values starting with "NTLM" or "Nego". Note that often multiple
1834 * headers are sent by the server there.
1835 */
1836 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001837 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001838 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet61608322018-11-23 16:23:45 +01001839 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001840 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001841 }
Christopher Faulet61608322018-11-23 16:23:45 +01001842 }
1843 }
1844
1845 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001846 /* we want to have the response time before we start processing it */
1847 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1848
1849 /* end of job, return OK */
1850 rep->analysers &= ~an_bit;
1851 rep->analyse_exp = TICK_ETERNITY;
1852 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001853 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001854 return 1;
1855
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001856 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001857 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001858 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001859 if (sess->listener->counters)
1860 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001861 if (objt_server(s->target))
1862 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001863 txn->status = 500;
1864 if (!(s->flags & SF_ERR_MASK))
1865 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001866 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001867
1868 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001869 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001870 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001871 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001872 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001873 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001874 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001875 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001876 do_l7_retry(s, si_b) == 0) {
1877 DBG_TRACE_DEVEL("leaving on L7 retry",
1878 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001879 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001880 }
Christopher Faulet47365272018-10-31 17:40:50 +01001881 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001882 /* fall through */
1883
Christopher Fauletb8a53712019-12-16 11:29:38 +01001884 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001885 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001886
1887 if (!(s->flags & SF_ERR_MASK))
1888 s->flags |= SF_ERR_PRXCOND;
1889 if (!(s->flags & SF_FINST_MASK))
1890 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001891
1892 s->si[1].flags |= SI_FL_NOLINGER;
1893 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001894 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001895 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001896 DBG_TRACE_DEVEL("leaving on error",
1897 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001898 return 0;
1899
Christopher Faulete0768eb2018-10-03 16:38:02 +02001900 abort_keep_alive:
1901 /* A keep-alive request to the server failed on a network error.
1902 * The client is required to retry. We need to close without returning
1903 * any other information so that the client retries.
1904 */
1905 txn->status = 0;
1906 rep->analysers &= AN_RES_FLT_END;
1907 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001908 s->logs.logwait = 0;
1909 s->logs.level = 0;
1910 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001911 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001912 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1913 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001914 return 0;
1915}
1916
1917/* This function performs all the processing enabled for the current response.
1918 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1919 * and updates s->res.analysers. It might make sense to explode it into several
1920 * other functions. It works like process_request (see indications above).
1921 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001922int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001923{
1924 struct session *sess = s->sess;
1925 struct http_txn *txn = s->txn;
1926 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001927 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001928 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001929 enum rule_result ret = HTTP_RULE_RES_CONT;
1930
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001931 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1932 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001933
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001934 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001935
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001936 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001937
1938 /* The stats applet needs to adjust the Connection header but we don't
1939 * apply any filter there.
1940 */
1941 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1942 rep->analysers &= ~an_bit;
1943 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001944 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001945 }
1946
1947 /*
1948 * We will have to evaluate the filters.
1949 * As opposed to version 1.2, now they will be evaluated in the
1950 * filters order and not in the header order. This means that
1951 * each filter has to be validated among all headers.
1952 *
1953 * Filters are tried with ->be first, then with ->fe if it is
1954 * different from ->be.
1955 *
1956 * Maybe we are in resume condiion. In this case I choose the
1957 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001958 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001959 * the process with the first one.
1960 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001961 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001962 * pointer and the ->fe rule list. If it doesn't match, I initialize
1963 * the loop with the ->be.
1964 */
1965 if (s->current_rule_list == &sess->fe->http_res_rules)
1966 cur_proxy = sess->fe;
1967 else
1968 cur_proxy = s->be;
1969 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001970 /* evaluate http-response rules */
1971 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001972 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001973
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001974 switch (ret) {
1975 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1976 goto return_prx_yield;
1977
1978 case HTTP_RULE_RES_CONT:
1979 case HTTP_RULE_RES_STOP: /* nothing to do */
1980 break;
1981
1982 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1983 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001984
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001985 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1986 goto return_prx_cond;
1987
1988 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001989 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001990
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001991 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1992 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001993
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001994 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1995 goto return_int_err;
1996 }
1997
1998 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001999
Christopher Faulete0768eb2018-10-03 16:38:02 +02002000 /* check whether we're already working on the frontend */
2001 if (cur_proxy == sess->fe)
2002 break;
2003 cur_proxy = sess->fe;
2004 }
2005
Christopher Faulete0768eb2018-10-03 16:38:02 +02002006 /* OK that's all we can do for 1xx responses */
2007 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002008 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002009
2010 /*
2011 * Now check for a server cookie.
2012 */
2013 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002014 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002015
2016 /*
2017 * Check for cache-control or pragma headers if required.
2018 */
2019 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002020 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002021
2022 /*
2023 * Add server cookie in the response if needed
2024 */
2025 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
2026 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
2027 (!(s->flags & SF_DIRECT) ||
2028 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
2029 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
2030 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
2031 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
2032 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
2033 !(s->flags & SF_IGNORE_PRST)) {
2034 /* the server is known, it's not the one the client requested, or the
2035 * cookie's last seen date needs to be refreshed. We have to
2036 * insert a set-cookie here, except if we want to insert only on POST
2037 * requests and this one isn't. Note that servers which don't have cookies
2038 * (eg: some backup servers) will return a full cookie removal request.
2039 */
2040 if (!objt_server(s->target)->cookie) {
2041 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002042 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02002043 s->be->cookie_name);
2044 }
2045 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002046 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002047
2048 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2049 /* emit last_date, which is mandatory */
2050 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2051 s30tob64((date.tv_sec+3) >> 2,
2052 trash.area + trash.data);
2053 trash.data += 5;
2054
2055 if (s->be->cookie_maxlife) {
2056 /* emit first_date, which is either the original one or
2057 * the current date.
2058 */
2059 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2060 s30tob64(txn->cookie_first_date ?
2061 txn->cookie_first_date >> 2 :
2062 (date.tv_sec+3) >> 2,
2063 trash.area + trash.data);
2064 trash.data += 5;
2065 }
2066 }
2067 chunk_appendf(&trash, "; path=/");
2068 }
2069
2070 if (s->be->cookie_domain)
2071 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2072
2073 if (s->be->ck_opts & PR_CK_HTTPONLY)
2074 chunk_appendf(&trash, "; HttpOnly");
2075
2076 if (s->be->ck_opts & PR_CK_SECURE)
2077 chunk_appendf(&trash, "; Secure");
2078
Christopher Faulet2f533902020-01-21 11:06:48 +01002079 if (s->be->cookie_attrs)
2080 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
2081
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002082 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002083 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002084
2085 txn->flags &= ~TX_SCK_MASK;
2086 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2087 /* the server did not change, only the date was updated */
2088 txn->flags |= TX_SCK_UPDATED;
2089 else
2090 txn->flags |= TX_SCK_INSERTED;
2091
2092 /* Here, we will tell an eventual cache on the client side that we don't
2093 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2094 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2095 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2096 */
2097 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2098
2099 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2100
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002101 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002102 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002103 }
2104 }
2105
2106 /*
2107 * Check if result will be cacheable with a cookie.
2108 * We'll block the response if security checks have caught
2109 * nasty things such as a cacheable cookie.
2110 */
2111 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2112 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2113 (s->be->options & PR_O_CHK_CACHE)) {
2114 /* we're in presence of a cacheable response containing
2115 * a set-cookie header. We'll block it as requested by
2116 * the 'checkcache' option, and send an alert.
2117 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002118 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2119 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2120 send_log(s->be, LOG_ALERT,
2121 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2122 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002123 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002124 }
2125
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002126 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002127 /*
2128 * Evaluate after-response rules before forwarding the response. rules
2129 * from the backend are evaluated first, then one from the frontend if
2130 * it differs.
2131 */
2132 if (!http_eval_after_res_rules(s))
2133 goto return_int_err;
2134
Christopher Faulete0768eb2018-10-03 16:38:02 +02002135 /* Always enter in the body analyzer */
2136 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2137 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2138
2139 /* if the user wants to log as soon as possible, without counting
2140 * bytes from the server, then this is the right moment. We have
2141 * to temporarily assign bytes_out to log what we currently have.
2142 */
2143 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2144 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002145 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002146 s->do_log(s);
2147 s->logs.bytes_out = 0;
2148 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002149
Christopher Fauletb8a53712019-12-16 11:29:38 +01002150 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002151 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002152 rep->analysers &= ~an_bit;
2153 rep->analyse_exp = TICK_ETERNITY;
2154 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002155
Christopher Fauletb8a53712019-12-16 11:29:38 +01002156 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002157 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002158 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002159 if (sess->listener->counters)
2160 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002161 if (objt_server(s->target))
2162 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002163 goto return_prx_err;
2164
2165 return_int_err:
2166 txn->status = 500;
2167 if (!(s->flags & SF_ERR_MASK))
2168 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002169 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002170 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2171 if (objt_server(s->target))
2172 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002173 if (objt_server(s->target))
2174 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002175 goto return_prx_err;
2176
2177 return_bad_res:
2178 txn->status = 502;
Christopher Fauleta20a6532020-02-05 10:16:41 +01002179 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2180 if (objt_server(s->target)) {
2181 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2182 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2183 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002184 /* fall through */
2185
2186 return_prx_err:
2187 http_reply_and_close(s, txn->status, http_error_message(s));
2188 /* fall through */
2189
2190 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002191 s->logs.t_data = -1; /* was not a valid response */
2192 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002193
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002194 if (!(s->flags & SF_ERR_MASK))
2195 s->flags |= SF_ERR_PRXCOND;
2196 if (!(s->flags & SF_FINST_MASK))
2197 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002198
Christopher Faulete58c0002020-03-02 16:21:01 +01002199 rep->analysers &= AN_RES_FLT_END;
2200 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002201 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002202 DBG_TRACE_DEVEL("leaving on error",
2203 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002204 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002205
2206 return_prx_yield:
2207 channel_dont_close(rep);
2208 DBG_TRACE_DEVEL("waiting for more data",
2209 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2210 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002211}
2212
2213/* This function is an analyser which forwards response body (including chunk
2214 * sizes if any). It is called as soon as we must forward, even if we forward
2215 * zero byte. The only situation where it must not be called is when we're in
2216 * tunnel mode and we want to forward till the close. It's used both to forward
2217 * remaining data and to resync after end of body. It expects the msg_state to
2218 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2219 * read more data, or 1 once we can go on with next request or end the stream.
2220 *
2221 * It is capable of compressing response data both in content-length mode and
2222 * in chunked mode. The state machines follows different flows depending on
2223 * whether content-length and chunked modes are used, since there are no
2224 * trailers in content-length :
2225 *
2226 * chk-mode cl-mode
2227 * ,----- BODY -----.
2228 * / \
2229 * V size > 0 V chk-mode
2230 * .--> SIZE -------------> DATA -------------> CRLF
2231 * | | size == 0 | last byte |
2232 * | v final crlf v inspected |
2233 * | TRAILERS -----------> DONE |
2234 * | |
2235 * `----------------------------------------------'
2236 *
2237 * Compression only happens in the DATA state, and must be flushed in final
2238 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2239 * is performed at once on final states for all bytes parsed, or when leaving
2240 * on missing data.
2241 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002242int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002243{
2244 struct session *sess = s->sess;
2245 struct http_txn *txn = s->txn;
2246 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002247 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002248 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002249
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002250 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002251
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002252 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002253
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002254 if (htx->flags & HTX_FL_PARSING_ERROR)
2255 goto return_bad_res;
2256 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2257 goto return_int_err;
2258
Christopher Faulete0768eb2018-10-03 16:38:02 +02002259 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002260 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002261 /* Output closed while we were sending data. We must abort and
2262 * wake the other side up.
2263 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002264 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002265 http_end_response(s);
2266 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002267 DBG_TRACE_DEVEL("leaving on error",
2268 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002269 return 1;
2270 }
2271
Christopher Faulet9768c262018-10-22 09:34:31 +02002272 if (msg->msg_state == HTTP_MSG_BODY)
2273 msg->msg_state = HTTP_MSG_DATA;
2274
Christopher Faulete0768eb2018-10-03 16:38:02 +02002275 /* in most states, we should abort in case of early close */
2276 channel_auto_close(res);
2277
Christopher Faulete0768eb2018-10-03 16:38:02 +02002278 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002279 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002280 if (res->flags & CF_EOI)
2281 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002282 }
2283 else {
2284 /* We can't process the buffer's contents yet */
2285 res->flags |= CF_WAKE_WRITE;
2286 goto missing_data_or_waiting;
2287 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002288 }
2289
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002290 if (msg->msg_state >= HTTP_MSG_ENDING)
2291 goto ending;
2292
2293 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2294 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2295 msg->msg_state = HTTP_MSG_ENDING;
2296 goto ending;
2297 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002298
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002299 /* Forward input data. We get it by removing all outgoing data not
2300 * forwarded yet from HTX data size. If there are some data filters, we
2301 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002302 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002303 if (HAS_RSP_DATA_FILTERS(s)) {
2304 ret = flt_http_payload(s, msg, htx->data);
2305 if (ret < 0)
2306 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002307 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002308 }
2309 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002310 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002311 if (msg->flags & HTTP_MSGF_XFER_LEN)
2312 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002313 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002314
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002315 if (htx->data != co_data(res))
2316 goto missing_data_or_waiting;
2317
2318 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2319 msg->msg_state = HTTP_MSG_ENDING;
2320 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002321 }
2322
Christopher Faulet9768c262018-10-22 09:34:31 +02002323 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002324 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2325 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002326 */
2327 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2328 goto missing_data_or_waiting;
2329
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002330 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002331
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002332 ending:
2333 /* other states, ENDING...TUNNEL */
2334 if (msg->msg_state >= HTTP_MSG_DONE)
2335 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002336
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002337 if (HAS_RSP_DATA_FILTERS(s)) {
2338 ret = flt_http_end(s, msg);
2339 if (ret <= 0) {
2340 if (!ret)
2341 goto missing_data_or_waiting;
2342 goto return_bad_res;
2343 }
2344 }
2345
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002346 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2347 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2348 msg->msg_state = HTTP_MSG_TUNNEL;
2349 goto ending;
2350 }
2351 else {
2352 msg->msg_state = HTTP_MSG_DONE;
2353 res->to_forward = 0;
2354 }
2355
2356 done:
2357
2358 channel_dont_close(res);
2359
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002360 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002361 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002362 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002363 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2364 if (res->flags & CF_SHUTW) {
2365 /* response errors are most likely due to the
2366 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002367 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002368 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002369 goto return_bad_res;
2370 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002371 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002372 return 1;
2373 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002374 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2375 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002376 return 0;
2377
2378 missing_data_or_waiting:
2379 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002380 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002381
2382 /* stop waiting for data if the input is closed before the end. If the
2383 * client side was already closed, it means that the client has aborted,
2384 * so we don't want to count this as a server abort. Otherwise it's a
2385 * server abort.
2386 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002387 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002388 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002389 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002390 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002391 if (htx_is_empty(htx))
2392 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002393 }
2394
Christopher Faulete0768eb2018-10-03 16:38:02 +02002395 /* When TE: chunked is used, we need to get there again to parse
2396 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002397 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2398 * are filters registered on the stream, we don't want to forward a
2399 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002400 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002401 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002402 channel_dont_close(res);
2403
2404 /* We know that more data are expected, but we couldn't send more that
2405 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2406 * system knows it must not set a PUSH on this first part. Interactive
2407 * modes are already handled by the stream sock layer. We must not do
2408 * this in content-length mode because it could present the MSG_MORE
2409 * flag with the last block of forwarded data, which would cause an
2410 * additional delay to be observed by the receiver.
2411 */
2412 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2413 res->flags |= CF_EXPECT_MORE;
2414
2415 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002416 DBG_TRACE_DEVEL("waiting for more data to forward",
2417 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002418 return 0;
2419
Christopher Faulet93e02d82019-03-08 14:18:50 +01002420 return_srv_abort:
2421 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2422 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002423 if (sess->listener->counters)
2424 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002425 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002426 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002427 if (!(s->flags & SF_ERR_MASK))
2428 s->flags |= SF_ERR_SRVCL;
2429 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002430
Christopher Faulet93e02d82019-03-08 14:18:50 +01002431 return_cli_abort:
2432 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2433 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002434 if (sess->listener->counters)
2435 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002436 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002437 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002438 if (!(s->flags & SF_ERR_MASK))
2439 s->flags |= SF_ERR_CLICL;
2440 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002441
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002442 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002443 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002444 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002445 if (sess->listener->counters)
2446 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002447 if (objt_server(s->target))
2448 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002449 if (!(s->flags & SF_ERR_MASK))
2450 s->flags |= SF_ERR_INTERNAL;
2451 goto return_error;
2452
Christopher Faulet93e02d82019-03-08 14:18:50 +01002453 return_bad_res:
2454 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2455 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002456 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002457 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2458 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002459 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002460 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002461 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002462
Christopher Faulet93e02d82019-03-08 14:18:50 +01002463 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002464 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002465 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002466 res->analysers &= AN_RES_FLT_END;
2467 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 +02002468 if (!(s->flags & SF_FINST_MASK))
2469 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002470 DBG_TRACE_DEVEL("leaving on error",
2471 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002472 return 0;
2473}
2474
Christopher Fauletf2824e62018-10-01 12:12:37 +02002475/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002476 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002477 * as too large a request to build a valid response.
2478 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002479int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002480{
Christopher Faulet99daf282018-11-28 22:58:13 +01002481 struct channel *req = &s->req;
2482 struct channel *res = &s->res;
2483 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002484 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002485 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002486 struct ist status, reason, location;
2487 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002488 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002489
2490 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002491 if (!chunk) {
2492 if (!(s->flags & SF_ERR_MASK))
2493 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002494 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002495 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002496
Christopher Faulet99daf282018-11-28 22:58:13 +01002497 /*
2498 * Create the location
2499 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002500 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002501 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002502 case REDIRECT_TYPE_SCHEME: {
2503 struct http_hdr_ctx ctx;
2504 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002505
Christopher Faulet99daf282018-11-28 22:58:13 +01002506 host = ist("");
2507 ctx.blk = NULL;
2508 if (http_find_header(htx, ist("Host"), &ctx, 0))
2509 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002510
Christopher Faulet297fbb42019-05-13 14:41:27 +02002511 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002512 path = http_get_path(htx_sl_req_uri(sl));
2513 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002514 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002515 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2516 int qs = 0;
2517 while (qs < path.len) {
2518 if (*(path.ptr + qs) == '?') {
2519 path.len = qs;
2520 break;
2521 }
2522 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002523 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002524 }
2525 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002526 else
2527 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002528
Christopher Faulet99daf282018-11-28 22:58:13 +01002529 if (rule->rdr_str) { /* this is an old "redirect" rule */
2530 /* add scheme */
2531 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2532 goto fail;
2533 }
2534 else {
2535 /* add scheme with executing log format */
2536 chunk->data += build_logline(s, chunk->area + chunk->data,
2537 chunk->size - chunk->data,
2538 &rule->rdr_fmt);
2539 }
2540 /* add "://" + host + path */
2541 if (!chunk_memcat(chunk, "://", 3) ||
2542 !chunk_memcat(chunk, host.ptr, host.len) ||
2543 !chunk_memcat(chunk, path.ptr, path.len))
2544 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002545
Christopher Faulet99daf282018-11-28 22:58:13 +01002546 /* append a slash at the end of the location if needed and missing */
2547 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2548 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2549 if (chunk->data + 1 >= chunk->size)
2550 goto fail;
2551 chunk->area[chunk->data++] = '/';
2552 }
2553 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002554 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002555
Christopher Faulet99daf282018-11-28 22:58:13 +01002556 case REDIRECT_TYPE_PREFIX: {
2557 struct ist path;
2558
Christopher Faulet297fbb42019-05-13 14:41:27 +02002559 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002560 path = http_get_path(htx_sl_req_uri(sl));
2561 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002562 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002563 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2564 int qs = 0;
2565 while (qs < path.len) {
2566 if (*(path.ptr + qs) == '?') {
2567 path.len = qs;
2568 break;
2569 }
2570 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002571 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002572 }
2573 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002574 else
2575 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002576
Christopher Faulet99daf282018-11-28 22:58:13 +01002577 if (rule->rdr_str) { /* this is an old "redirect" rule */
2578 /* add prefix. Note that if prefix == "/", we don't want to
2579 * add anything, otherwise it makes it hard for the user to
2580 * configure a self-redirection.
2581 */
2582 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2583 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2584 goto fail;
2585 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002586 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002587 else {
2588 /* add prefix with executing log format */
2589 chunk->data += build_logline(s, chunk->area + chunk->data,
2590 chunk->size - chunk->data,
2591 &rule->rdr_fmt);
2592 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002593
Christopher Faulet99daf282018-11-28 22:58:13 +01002594 /* add path */
2595 if (!chunk_memcat(chunk, path.ptr, path.len))
2596 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002597
Christopher Faulet99daf282018-11-28 22:58:13 +01002598 /* append a slash at the end of the location if needed and missing */
2599 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2600 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2601 if (chunk->data + 1 >= chunk->size)
2602 goto fail;
2603 chunk->area[chunk->data++] = '/';
2604 }
2605 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002606 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002607 case REDIRECT_TYPE_LOCATION:
2608 default:
2609 if (rule->rdr_str) { /* this is an old "redirect" rule */
2610 /* add location */
2611 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2612 goto fail;
2613 }
2614 else {
2615 /* add location with executing log format */
2616 chunk->data += build_logline(s, chunk->area + chunk->data,
2617 chunk->size - chunk->data,
2618 &rule->rdr_fmt);
2619 }
2620 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002621 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002622 location = ist2(chunk->area, chunk->data);
2623
2624 /*
2625 * Create the 30x response
2626 */
2627 switch (rule->code) {
2628 case 308:
2629 status = ist("308");
2630 reason = ist("Permanent Redirect");
2631 break;
2632 case 307:
2633 status = ist("307");
2634 reason = ist("Temporary Redirect");
2635 break;
2636 case 303:
2637 status = ist("303");
2638 reason = ist("See Other");
2639 break;
2640 case 301:
2641 status = ist("301");
2642 reason = ist("Moved Permanently");
2643 break;
2644 case 302:
2645 default:
2646 status = ist("302");
2647 reason = ist("Found");
2648 break;
2649 }
2650
Christopher Faulet08e66462019-05-23 16:44:59 +02002651 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2652 close = 1;
2653
Christopher Faulet99daf282018-11-28 22:58:13 +01002654 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002655 /* Trim any possible response */
2656 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002657 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2658 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2659 if (!sl)
2660 goto fail;
2661 sl->info.res.status = rule->code;
2662 s->txn->status = rule->code;
2663
Christopher Faulet08e66462019-05-23 16:44:59 +02002664 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2665 goto fail;
2666
2667 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002668 !htx_add_header(htx, ist("Location"), location))
2669 goto fail;
2670
2671 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2672 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2673 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002674 }
2675
2676 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002677 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2678 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002679 }
2680
Christopher Faulet99daf282018-11-28 22:58:13 +01002681 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2682 goto fail;
2683
Kevin Zhu96b36392020-01-07 09:42:55 +01002684 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002685 if (!http_forward_proxy_resp(s, 1))
2686 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002687
Christopher Faulet60b33a52020-01-28 09:18:10 +01002688 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2689 /* let's log the request time */
2690 s->logs.tv_request = now;
2691 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002692
Christopher Faulet60b33a52020-01-28 09:18:10 +01002693 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2694 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2695 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002696
2697 if (!(s->flags & SF_ERR_MASK))
2698 s->flags |= SF_ERR_LOCAL;
2699 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002700 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002701
Christopher Faulet99daf282018-11-28 22:58:13 +01002702 free_trash_chunk(chunk);
2703 return 1;
2704
2705 fail:
2706 /* If an error occurred, remove the incomplete HTTP response from the
2707 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002708 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002709 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002710 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002711}
2712
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002713/* Replace all headers matching the name <name>. The header value is replaced if
2714 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2715 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2716 * values are evaluated one by one. It returns 0 on success and -1 on error.
2717 */
2718int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2719 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002720{
2721 struct http_hdr_ctx ctx;
2722 struct buffer *output = get_trash_chunk();
2723
Christopher Faulet72333522018-10-24 11:25:02 +02002724 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002725 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002726 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2727 continue;
2728
2729 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2730 if (output->data == -1)
2731 return -1;
2732 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2733 return -1;
2734 }
2735 return 0;
2736}
2737
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002738/* This function executes one of the set-{method,path,query,uri} actions. It
2739 * takes the string from the variable 'replace' with length 'len', then modifies
2740 * the relevant part of the request line accordingly. Then it updates various
2741 * pointers to the next elements which were moved, and the total buffer length.
2742 * It finds the action to be performed in p[2], previously filled by function
2743 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2744 * error, though this can be revisited when this code is finally exploited.
2745 *
2746 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2747 * query string and 3 to replace uri.
2748 *
2749 * In query string case, the mark question '?' must be set at the start of the
2750 * string by the caller, event if the replacement query string is empty.
2751 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002752int http_req_replace_stline(int action, const char *replace, int len,
2753 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002754{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002755 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002756
2757 switch (action) {
2758 case 0: // method
2759 if (!http_replace_req_meth(htx, ist2(replace, len)))
2760 return -1;
2761 break;
2762
2763 case 1: // path
2764 if (!http_replace_req_path(htx, ist2(replace, len)))
2765 return -1;
2766 break;
2767
2768 case 2: // query
2769 if (!http_replace_req_query(htx, ist2(replace, len)))
2770 return -1;
2771 break;
2772
2773 case 3: // uri
2774 if (!http_replace_req_uri(htx, ist2(replace, len)))
2775 return -1;
2776 break;
2777
2778 default:
2779 return -1;
2780 }
2781 return 0;
2782}
2783
2784/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002785 * variable <status> contains the new status code. This function never fails. It
2786 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002787 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002788int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002789{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002790 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002791 char *res;
2792
2793 chunk_reset(&trash);
2794 res = ultoa_o(status, trash.area, trash.size);
2795 trash.data = res - trash.area;
2796
2797 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002798 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002799 const char *str = http_get_reason(status);
2800 reason = ist2(str, strlen(str));
2801 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002802
Christopher Faulete00d06c2019-12-16 17:18:42 +01002803 if (!http_replace_res_status(htx, ist2(trash.area, trash.data)))
2804 return -1;
Christopher Faulet96bff762019-12-17 13:46:18 +01002805 if (!http_replace_res_reason(htx, reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002806 return -1;
2807 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002808}
2809
Christopher Faulet3e964192018-10-24 11:39:23 +02002810/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2811 * transaction <txn>. Returns the verdict of the first rule that prevents
2812 * further processing of the request (auth, deny, ...), and defaults to
2813 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2814 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2815 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2816 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2817 * status.
2818 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002819static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002820 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002821{
2822 struct session *sess = strm_sess(s);
2823 struct http_txn *txn = s->txn;
2824 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002825 struct act_rule *rule;
2826 struct http_hdr_ctx ctx;
2827 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002828 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002829 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002830
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002831 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002832
2833 /* If "the current_rule_list" match the executed rule list, we are in
2834 * resume condition. If a resume is needed it is always in the action
2835 * and never in the ACL or converters. In this case, we initialise the
2836 * current rule, and go to the action execution point.
2837 */
2838 if (s->current_rule) {
2839 rule = s->current_rule;
2840 s->current_rule = NULL;
2841 if (s->current_rule_list == rules)
2842 goto resume_execution;
2843 }
2844 s->current_rule_list = rules;
2845
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002846 /* start the ruleset evaluation in strict mode */
2847 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002848
Christopher Faulet3e964192018-10-24 11:39:23 +02002849 list_for_each_entry(rule, rules, list) {
2850 /* check optional condition */
2851 if (rule->cond) {
2852 int ret;
2853
2854 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2855 ret = acl_pass(ret);
2856
2857 if (rule->cond->pol == ACL_COND_UNLESS)
2858 ret = !ret;
2859
2860 if (!ret) /* condition not matched */
2861 continue;
2862 }
2863
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002864 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002865 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002866 /* Always call the action function if defined */
2867 if (rule->action_ptr) {
2868 if ((s->req.flags & CF_READ_ERROR) ||
2869 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2870 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002871 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002872
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002873 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002874 case ACT_RET_CONT:
2875 break;
2876 case ACT_RET_STOP:
2877 rule_ret = HTTP_RULE_RES_STOP;
2878 goto end;
2879 case ACT_RET_YIELD:
2880 s->current_rule = rule;
2881 rule_ret = HTTP_RULE_RES_YIELD;
2882 goto end;
2883 case ACT_RET_ERR:
2884 rule_ret = HTTP_RULE_RES_ERROR;
2885 goto end;
2886 case ACT_RET_DONE:
2887 rule_ret = HTTP_RULE_RES_DONE;
2888 goto end;
2889 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002890 if (txn->status == -1)
2891 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002892 rule_ret = HTTP_RULE_RES_DENY;
2893 goto end;
2894 case ACT_RET_ABRT:
2895 rule_ret = HTTP_RULE_RES_ABRT;
2896 goto end;
2897 case ACT_RET_INV:
2898 rule_ret = HTTP_RULE_RES_BADREQ;
2899 goto end;
2900 }
2901 continue; /* eval the next rule */
2902 }
2903
2904 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002905 switch (rule->action) {
2906 case ACT_ACTION_ALLOW:
2907 rule_ret = HTTP_RULE_RES_STOP;
2908 goto end;
2909
2910 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002911 txn->status = rule->arg.http_reply->status;
2912 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002913 rule_ret = HTTP_RULE_RES_DENY;
2914 goto end;
2915
2916 case ACT_HTTP_REQ_TARPIT:
2917 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002918 txn->status = rule->arg.http_reply->status;
2919 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002920 rule_ret = HTTP_RULE_RES_DENY;
2921 goto end;
2922
2923 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002924 /* Auth might be performed on regular http-req rules as well as on stats */
Christopher Faulet96bff762019-12-17 13:46:18 +01002925 auth_realm = rule->arg.http.str.ptr;
Christopher Faulet3e964192018-10-24 11:39:23 +02002926 if (!auth_realm) {
2927 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2928 auth_realm = STATS_DEFAULT_REALM;
2929 else
2930 auth_realm = px->id;
2931 }
2932 /* send 401/407 depending on whether we use a proxy or not. We still
2933 * count one error, because normal browsing won't significantly
2934 * increase the counter but brute force attempts will.
2935 */
Christopher Faulet3e964192018-10-24 11:39:23 +02002936 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002937 if (http_reply_40x_unauthorized(s, auth_realm) == -1)
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002938 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet12c51e22018-11-28 15:59:42 +01002939 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02002940 goto end;
2941
2942 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002943 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002944 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002945 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002946 goto end;
2947
2948 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002949 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002950 break;
2951
2952 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002953 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002954 break;
2955
2956 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002957 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002958 break;
2959
2960 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002961 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002962 break;
2963
Christopher Faulet3e964192018-10-24 11:39:23 +02002964 case ACT_HTTP_DEL_HDR:
2965 /* remove all occurrences of the header */
2966 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01002967 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02002968 http_remove_header(htx, &ctx);
2969 break;
2970
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002971 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002972 default:
2973 break;
2974 }
2975 }
2976
2977 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002978 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002979 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002980 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002981
Christopher Faulet3e964192018-10-24 11:39:23 +02002982 /* we reached the end of the rules, nothing to report */
2983 return rule_ret;
2984}
2985
2986/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2987 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2988 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2989 * is returned, the process can continue the evaluation of next rule list. If
2990 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2991 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002992 * must be returned. If *YIELD is returned, the caller must call again the
2993 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002994 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002995static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2996 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002997{
2998 struct session *sess = strm_sess(s);
2999 struct http_txn *txn = s->txn;
3000 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02003001 struct act_rule *rule;
3002 struct http_hdr_ctx ctx;
3003 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003004 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02003005
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003006 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02003007
3008 /* If "the current_rule_list" match the executed rule list, we are in
3009 * resume condition. If a resume is needed it is always in the action
3010 * and never in the ACL or converters. In this case, we initialise the
3011 * current rule, and go to the action execution point.
3012 */
3013 if (s->current_rule) {
3014 rule = s->current_rule;
3015 s->current_rule = NULL;
3016 if (s->current_rule_list == rules)
3017 goto resume_execution;
3018 }
3019 s->current_rule_list = rules;
3020
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003021 /* start the ruleset evaluation in strict mode */
3022 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003023
Christopher Faulet3e964192018-10-24 11:39:23 +02003024 list_for_each_entry(rule, rules, list) {
3025 /* check optional condition */
3026 if (rule->cond) {
3027 int ret;
3028
3029 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3030 ret = acl_pass(ret);
3031
3032 if (rule->cond->pol == ACL_COND_UNLESS)
3033 ret = !ret;
3034
3035 if (!ret) /* condition not matched */
3036 continue;
3037 }
3038
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003039 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02003040resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003041
3042 /* Always call the action function if defined */
3043 if (rule->action_ptr) {
3044 if ((s->req.flags & CF_READ_ERROR) ||
3045 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3046 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003047 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003048
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003049 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003050 case ACT_RET_CONT:
3051 break;
3052 case ACT_RET_STOP:
3053 rule_ret = HTTP_RULE_RES_STOP;
3054 goto end;
3055 case ACT_RET_YIELD:
3056 s->current_rule = rule;
3057 rule_ret = HTTP_RULE_RES_YIELD;
3058 goto end;
3059 case ACT_RET_ERR:
3060 rule_ret = HTTP_RULE_RES_ERROR;
3061 goto end;
3062 case ACT_RET_DONE:
3063 rule_ret = HTTP_RULE_RES_DONE;
3064 goto end;
3065 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01003066 if (txn->status == -1)
3067 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003068 rule_ret = HTTP_RULE_RES_DENY;
3069 goto end;
3070 case ACT_RET_ABRT:
3071 rule_ret = HTTP_RULE_RES_ABRT;
3072 goto end;
3073 case ACT_RET_INV:
3074 rule_ret = HTTP_RULE_RES_BADREQ;
3075 goto end;
3076 }
3077 continue; /* eval the next rule */
3078 }
3079
3080 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003081 switch (rule->action) {
3082 case ACT_ACTION_ALLOW:
3083 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3084 goto end;
3085
3086 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02003087 txn->status = rule->arg.http_reply->status;
3088 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003089 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003090 goto end;
3091
3092 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003093 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003094 break;
3095
3096 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003097 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003098 break;
3099
3100 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003101 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003102 break;
3103
3104 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003105 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003106 break;
3107
Christopher Faulet3e964192018-10-24 11:39:23 +02003108 case ACT_HTTP_DEL_HDR:
3109 /* remove all occurrences of the header */
3110 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01003111 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02003112 http_remove_header(htx, &ctx);
3113 break;
3114
Christopher Faulet3e964192018-10-24 11:39:23 +02003115 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003116 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003117 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003118 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003119 goto end;
3120
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003121 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003122 default:
3123 break;
3124 }
3125 }
3126
3127 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003128 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003129 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003130 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003131
Christopher Faulet3e964192018-10-24 11:39:23 +02003132 /* we reached the end of the rules, nothing to report */
3133 return rule_ret;
3134}
3135
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003136/* Executes backend and frontend http-after-response rules for the stream <s>,
3137 * in that order. it return 1 on success and 0 on error. It is the caller
3138 * responsibility to catch error or ignore it. If it catches it, this function
3139 * may be called a second time, for the internal error.
3140 */
3141int http_eval_after_res_rules(struct stream *s)
3142{
3143 struct session *sess = s->sess;
3144 enum rule_result ret = HTTP_RULE_RES_CONT;
3145
Christopher Faulet507479b2020-05-15 12:29:46 +02003146 /* Eval after-response ruleset only if the reply is not const */
3147 if (s->txn->flags & TX_CONST_REPLY)
3148 goto end;
3149
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003150 /* prune the request variables if not already done and swap to the response variables. */
3151 if (s->vars_reqres.scope != SCOPE_RES) {
3152 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3153 vars_prune(&s->vars_reqres, s->sess, s);
3154 vars_init(&s->vars_reqres, SCOPE_RES);
3155 }
3156
3157 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3158 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3159 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3160
Christopher Faulet507479b2020-05-15 12:29:46 +02003161 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003162 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3163 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3164}
3165
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003166/*
3167 * Manage client-side cookie. It can impact performance by about 2% so it is
3168 * desirable to call it only when needed. This code is quite complex because
3169 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3170 * highly recommended not to touch this part without a good reason !
3171 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003172static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003173{
3174 struct session *sess = s->sess;
3175 struct http_txn *txn = s->txn;
3176 struct htx *htx;
3177 struct http_hdr_ctx ctx;
3178 char *hdr_beg, *hdr_end, *del_from;
3179 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3180 int preserve_hdr;
3181
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003182 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003183 ctx.blk = NULL;
3184 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003185 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003186 del_from = NULL; /* nothing to be deleted */
3187 preserve_hdr = 0; /* assume we may kill the whole header */
3188
3189 /* Now look for cookies. Conforming to RFC2109, we have to support
3190 * attributes whose name begin with a '$', and associate them with
3191 * the right cookie, if we want to delete this cookie.
3192 * So there are 3 cases for each cookie read :
3193 * 1) it's a special attribute, beginning with a '$' : ignore it.
3194 * 2) it's a server id cookie that we *MAY* want to delete : save
3195 * some pointers on it (last semi-colon, beginning of cookie...)
3196 * 3) it's an application cookie : we *MAY* have to delete a previous
3197 * "special" cookie.
3198 * At the end of loop, if a "special" cookie remains, we may have to
3199 * remove it. If no application cookie persists in the header, we
3200 * *MUST* delete it.
3201 *
3202 * Note: RFC2965 is unclear about the processing of spaces around
3203 * the equal sign in the ATTR=VALUE form. A careful inspection of
3204 * the RFC explicitly allows spaces before it, and not within the
3205 * tokens (attrs or values). An inspection of RFC2109 allows that
3206 * too but section 10.1.3 lets one think that spaces may be allowed
3207 * after the equal sign too, resulting in some (rare) buggy
3208 * implementations trying to do that. So let's do what servers do.
3209 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3210 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003211 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003212 * causes parsing to become ambiguous. Browsers also allow spaces
3213 * within values even without quotes.
3214 *
3215 * We have to keep multiple pointers in order to support cookie
3216 * removal at the beginning, middle or end of header without
3217 * corrupting the header. All of these headers are valid :
3218 *
3219 * hdr_beg hdr_end
3220 * | |
3221 * v |
3222 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3223 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3224 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3225 * | | | | | | |
3226 * | | | | | | |
3227 * | | | | | | +--> next
3228 * | | | | | +----> val_end
3229 * | | | | +-----------> val_beg
3230 * | | | +--------------> equal
3231 * | | +----------------> att_end
3232 * | +---------------------> att_beg
3233 * +--------------------------> prev
3234 *
3235 */
3236 hdr_beg = ctx.value.ptr;
3237 hdr_end = hdr_beg + ctx.value.len;
3238 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3239 /* Iterate through all cookies on this line */
3240
3241 /* find att_beg */
3242 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003243 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003244 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003245 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003246
3247 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3248 att_beg++;
3249
3250 /* find att_end : this is the first character after the last non
3251 * space before the equal. It may be equal to hdr_end.
3252 */
3253 equal = att_end = att_beg;
3254 while (equal < hdr_end) {
3255 if (*equal == '=' || *equal == ',' || *equal == ';')
3256 break;
3257 if (HTTP_IS_SPHT(*equal++))
3258 continue;
3259 att_end = equal;
3260 }
3261
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003262 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003263 * is between <att_beg> and <equal>, both may be identical.
3264 */
3265 /* look for end of cookie if there is an equal sign */
3266 if (equal < hdr_end && *equal == '=') {
3267 /* look for the beginning of the value */
3268 val_beg = equal + 1;
3269 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3270 val_beg++;
3271
3272 /* find the end of the value, respecting quotes */
3273 next = http_find_cookie_value_end(val_beg, hdr_end);
3274
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003275 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003276 val_end = next;
3277 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3278 val_end--;
3279 }
3280 else
3281 val_beg = val_end = next = equal;
3282
3283 /* We have nothing to do with attributes beginning with
3284 * '$'. However, they will automatically be removed if a
3285 * header before them is removed, since they're supposed
3286 * to be linked together.
3287 */
3288 if (*att_beg == '$')
3289 continue;
3290
3291 /* Ignore cookies with no equal sign */
3292 if (equal == next) {
3293 /* This is not our cookie, so we must preserve it. But if we already
3294 * scheduled another cookie for removal, we cannot remove the
3295 * complete header, but we can remove the previous block itself.
3296 */
3297 preserve_hdr = 1;
3298 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003299 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003300 val_end += delta;
3301 next += delta;
3302 hdr_end += delta;
3303 prev = del_from;
3304 del_from = NULL;
3305 }
3306 continue;
3307 }
3308
3309 /* if there are spaces around the equal sign, we need to
3310 * strip them otherwise we'll get trouble for cookie captures,
3311 * or even for rewrites. Since this happens extremely rarely,
3312 * it does not hurt performance.
3313 */
3314 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3315 int stripped_before = 0;
3316 int stripped_after = 0;
3317
3318 if (att_end != equal) {
3319 memmove(att_end, equal, hdr_end - equal);
3320 stripped_before = (att_end - equal);
3321 equal += stripped_before;
3322 val_beg += stripped_before;
3323 }
3324
3325 if (val_beg > equal + 1) {
3326 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3327 stripped_after = (equal + 1) - val_beg;
3328 val_beg += stripped_after;
3329 stripped_before += stripped_after;
3330 }
3331
3332 val_end += stripped_before;
3333 next += stripped_before;
3334 hdr_end += stripped_before;
3335 }
3336 /* now everything is as on the diagram above */
3337
3338 /* First, let's see if we want to capture this cookie. We check
3339 * that we don't already have a client side cookie, because we
3340 * can only capture one. Also as an optimisation, we ignore
3341 * cookies shorter than the declared name.
3342 */
3343 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3344 (val_end - att_beg >= sess->fe->capture_namelen) &&
3345 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3346 int log_len = val_end - att_beg;
3347
3348 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3349 ha_alert("HTTP logging : out of memory.\n");
3350 } else {
3351 if (log_len > sess->fe->capture_len)
3352 log_len = sess->fe->capture_len;
3353 memcpy(txn->cli_cookie, att_beg, log_len);
3354 txn->cli_cookie[log_len] = 0;
3355 }
3356 }
3357
3358 /* Persistence cookies in passive, rewrite or insert mode have the
3359 * following form :
3360 *
3361 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3362 *
3363 * For cookies in prefix mode, the form is :
3364 *
3365 * Cookie: NAME=SRV~VALUE
3366 */
3367 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3368 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3369 struct server *srv = s->be->srv;
3370 char *delim;
3371
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003372 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003373 * have the server ID between val_beg and delim, and the original cookie between
3374 * delim+1 and val_end. Otherwise, delim==val_end :
3375 *
3376 * hdr_beg
3377 * |
3378 * v
3379 * NAME=SRV; # in all but prefix modes
3380 * NAME=SRV~OPAQUE ; # in prefix mode
3381 * || || | |+-> next
3382 * || || | +--> val_end
3383 * || || +---------> delim
3384 * || |+------------> val_beg
3385 * || +-------------> att_end = equal
3386 * |+-----------------> att_beg
3387 * +------------------> prev
3388 *
3389 */
3390 if (s->be->ck_opts & PR_CK_PFX) {
3391 for (delim = val_beg; delim < val_end; delim++)
3392 if (*delim == COOKIE_DELIM)
3393 break;
3394 }
3395 else {
3396 char *vbar1;
3397 delim = val_end;
3398 /* Now check if the cookie contains a date field, which would
3399 * appear after a vertical bar ('|') just after the server name
3400 * and before the delimiter.
3401 */
3402 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3403 if (vbar1) {
3404 /* OK, so left of the bar is the server's cookie and
3405 * right is the last seen date. It is a base64 encoded
3406 * 30-bit value representing the UNIX date since the
3407 * epoch in 4-second quantities.
3408 */
3409 int val;
3410 delim = vbar1++;
3411 if (val_end - vbar1 >= 5) {
3412 val = b64tos30(vbar1);
3413 if (val > 0)
3414 txn->cookie_last_date = val << 2;
3415 }
3416 /* look for a second vertical bar */
3417 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3418 if (vbar1 && (val_end - vbar1 > 5)) {
3419 val = b64tos30(vbar1 + 1);
3420 if (val > 0)
3421 txn->cookie_first_date = val << 2;
3422 }
3423 }
3424 }
3425
3426 /* if the cookie has an expiration date and the proxy wants to check
3427 * it, then we do that now. We first check if the cookie is too old,
3428 * then only if it has expired. We detect strict overflow because the
3429 * time resolution here is not great (4 seconds). Cookies with dates
3430 * in the future are ignored if their offset is beyond one day. This
3431 * allows an admin to fix timezone issues without expiring everyone
3432 * and at the same time avoids keeping unwanted side effects for too
3433 * long.
3434 */
3435 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3436 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3437 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3438 txn->flags &= ~TX_CK_MASK;
3439 txn->flags |= TX_CK_OLD;
3440 delim = val_beg; // let's pretend we have not found the cookie
3441 txn->cookie_first_date = 0;
3442 txn->cookie_last_date = 0;
3443 }
3444 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3445 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3446 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3447 txn->flags &= ~TX_CK_MASK;
3448 txn->flags |= TX_CK_EXPIRED;
3449 delim = val_beg; // let's pretend we have not found the cookie
3450 txn->cookie_first_date = 0;
3451 txn->cookie_last_date = 0;
3452 }
3453
3454 /* Here, we'll look for the first running server which supports the cookie.
3455 * This allows to share a same cookie between several servers, for example
3456 * to dedicate backup servers to specific servers only.
3457 * However, to prevent clients from sticking to cookie-less backup server
3458 * when they have incidentely learned an empty cookie, we simply ignore
3459 * empty cookies and mark them as invalid.
3460 * The same behaviour is applied when persistence must be ignored.
3461 */
3462 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3463 srv = NULL;
3464
3465 while (srv) {
3466 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3467 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3468 if ((srv->cur_state != SRV_ST_STOPPED) ||
3469 (s->be->options & PR_O_PERSIST) ||
3470 (s->flags & SF_FORCE_PRST)) {
3471 /* we found the server and we can use it */
3472 txn->flags &= ~TX_CK_MASK;
3473 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3474 s->flags |= SF_DIRECT | SF_ASSIGNED;
3475 s->target = &srv->obj_type;
3476 break;
3477 } else {
3478 /* we found a server, but it's down,
3479 * mark it as such and go on in case
3480 * another one is available.
3481 */
3482 txn->flags &= ~TX_CK_MASK;
3483 txn->flags |= TX_CK_DOWN;
3484 }
3485 }
3486 srv = srv->next;
3487 }
3488
3489 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3490 /* no server matched this cookie or we deliberately skipped it */
3491 txn->flags &= ~TX_CK_MASK;
3492 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3493 txn->flags |= TX_CK_UNUSED;
3494 else
3495 txn->flags |= TX_CK_INVALID;
3496 }
3497
3498 /* depending on the cookie mode, we may have to either :
3499 * - delete the complete cookie if we're in insert+indirect mode, so that
3500 * the server never sees it ;
3501 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003502 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003503 * if we're in cookie prefix mode
3504 */
3505 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3506 int delta; /* negative */
3507
3508 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3509 delta = val_beg - (delim + 1);
3510 val_end += delta;
3511 next += delta;
3512 hdr_end += delta;
3513 del_from = NULL;
3514 preserve_hdr = 1; /* we want to keep this cookie */
3515 }
3516 else if (del_from == NULL &&
3517 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3518 del_from = prev;
3519 }
3520 }
3521 else {
3522 /* This is not our cookie, so we must preserve it. But if we already
3523 * scheduled another cookie for removal, we cannot remove the
3524 * complete header, but we can remove the previous block itself.
3525 */
3526 preserve_hdr = 1;
3527
3528 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003529 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003530 if (att_beg >= del_from)
3531 att_beg += delta;
3532 if (att_end >= del_from)
3533 att_end += delta;
3534 val_beg += delta;
3535 val_end += delta;
3536 next += delta;
3537 hdr_end += delta;
3538 prev = del_from;
3539 del_from = NULL;
3540 }
3541 }
3542
3543 /* continue with next cookie on this header line */
3544 att_beg = next;
3545 } /* for each cookie */
3546
3547
3548 /* There are no more cookies on this line.
3549 * We may still have one (or several) marked for deletion at the
3550 * end of the line. We must do this now in two ways :
3551 * - if some cookies must be preserved, we only delete from the
3552 * mark to the end of line ;
3553 * - if nothing needs to be preserved, simply delete the whole header
3554 */
3555 if (del_from) {
3556 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3557 }
3558 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003559 if (hdr_beg != hdr_end)
3560 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003561 else
3562 http_remove_header(htx, &ctx);
3563 }
3564 } /* for each "Cookie header */
3565}
3566
3567/*
3568 * Manage server-side cookies. It can impact performance by about 2% so it is
3569 * desirable to call it only when needed. This function is also used when we
3570 * just need to know if there is a cookie (eg: for check-cache).
3571 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003572static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003573{
3574 struct session *sess = s->sess;
3575 struct http_txn *txn = s->txn;
3576 struct htx *htx;
3577 struct http_hdr_ctx ctx;
3578 struct server *srv;
3579 char *hdr_beg, *hdr_end;
3580 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003581 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003582
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003583 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003584
3585 ctx.blk = NULL;
3586 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003587 int is_first = 1;
3588
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003589 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3590 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3591 break;
3592 is_cookie2 = 1;
3593 }
3594
3595 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3596 * <prev> points to the colon.
3597 */
3598 txn->flags |= TX_SCK_PRESENT;
3599
3600 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3601 * check-cache is enabled) and we are not interested in checking
3602 * them. Warning, the cookie capture is declared in the frontend.
3603 */
3604 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3605 break;
3606
3607 /* OK so now we know we have to process this response cookie.
3608 * The format of the Set-Cookie header is slightly different
3609 * from the format of the Cookie header in that it does not
3610 * support the comma as a cookie delimiter (thus the header
3611 * cannot be folded) because the Expires attribute described in
3612 * the original Netscape's spec may contain an unquoted date
3613 * with a comma inside. We have to live with this because
3614 * many browsers don't support Max-Age and some browsers don't
3615 * support quoted strings. However the Set-Cookie2 header is
3616 * clean.
3617 *
3618 * We have to keep multiple pointers in order to support cookie
3619 * removal at the beginning, middle or end of header without
3620 * corrupting the header (in case of set-cookie2). A special
3621 * pointer, <scav> points to the beginning of the set-cookie-av
3622 * fields after the first semi-colon. The <next> pointer points
3623 * either to the end of line (set-cookie) or next unquoted comma
3624 * (set-cookie2). All of these headers are valid :
3625 *
3626 * hdr_beg hdr_end
3627 * | |
3628 * v |
3629 * NAME1 = VALUE 1 ; Secure; Path="/" |
3630 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3631 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3632 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3633 * | | | | | | | |
3634 * | | | | | | | +-> next
3635 * | | | | | | +------------> scav
3636 * | | | | | +--------------> val_end
3637 * | | | | +--------------------> val_beg
3638 * | | | +----------------------> equal
3639 * | | +------------------------> att_end
3640 * | +----------------------------> att_beg
3641 * +------------------------------> prev
3642 * -------------------------------> hdr_beg
3643 */
3644 hdr_beg = ctx.value.ptr;
3645 hdr_end = hdr_beg + ctx.value.len;
3646 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3647
3648 /* Iterate through all cookies on this line */
3649
3650 /* find att_beg */
3651 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003652 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003653 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003654 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003655
3656 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3657 att_beg++;
3658
3659 /* find att_end : this is the first character after the last non
3660 * space before the equal. It may be equal to hdr_end.
3661 */
3662 equal = att_end = att_beg;
3663
3664 while (equal < hdr_end) {
3665 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3666 break;
3667 if (HTTP_IS_SPHT(*equal++))
3668 continue;
3669 att_end = equal;
3670 }
3671
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003672 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003673 * is between <att_beg> and <equal>, both may be identical.
3674 */
3675
3676 /* look for end of cookie if there is an equal sign */
3677 if (equal < hdr_end && *equal == '=') {
3678 /* look for the beginning of the value */
3679 val_beg = equal + 1;
3680 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3681 val_beg++;
3682
3683 /* find the end of the value, respecting quotes */
3684 next = http_find_cookie_value_end(val_beg, hdr_end);
3685
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003686 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003687 val_end = next;
3688 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3689 val_end--;
3690 }
3691 else {
3692 /* <equal> points to next comma, semi-colon or EOL */
3693 val_beg = val_end = next = equal;
3694 }
3695
3696 if (next < hdr_end) {
3697 /* Set-Cookie2 supports multiple cookies, and <next> points to
3698 * a colon or semi-colon before the end. So skip all attr-value
3699 * pairs and look for the next comma. For Set-Cookie, since
3700 * commas are permitted in values, skip to the end.
3701 */
3702 if (is_cookie2)
3703 next = http_find_hdr_value_end(next, hdr_end);
3704 else
3705 next = hdr_end;
3706 }
3707
3708 /* Now everything is as on the diagram above */
3709
3710 /* Ignore cookies with no equal sign */
3711 if (equal == val_end)
3712 continue;
3713
3714 /* If there are spaces around the equal sign, we need to
3715 * strip them otherwise we'll get trouble for cookie captures,
3716 * or even for rewrites. Since this happens extremely rarely,
3717 * it does not hurt performance.
3718 */
3719 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3720 int stripped_before = 0;
3721 int stripped_after = 0;
3722
3723 if (att_end != equal) {
3724 memmove(att_end, equal, hdr_end - equal);
3725 stripped_before = (att_end - equal);
3726 equal += stripped_before;
3727 val_beg += stripped_before;
3728 }
3729
3730 if (val_beg > equal + 1) {
3731 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3732 stripped_after = (equal + 1) - val_beg;
3733 val_beg += stripped_after;
3734 stripped_before += stripped_after;
3735 }
3736
3737 val_end += stripped_before;
3738 next += stripped_before;
3739 hdr_end += stripped_before;
3740
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003741 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003742 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003743 }
3744
3745 /* First, let's see if we want to capture this cookie. We check
3746 * that we don't already have a server side cookie, because we
3747 * can only capture one. Also as an optimisation, we ignore
3748 * cookies shorter than the declared name.
3749 */
3750 if (sess->fe->capture_name != NULL &&
3751 txn->srv_cookie == NULL &&
3752 (val_end - att_beg >= sess->fe->capture_namelen) &&
3753 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3754 int log_len = val_end - att_beg;
3755 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3756 ha_alert("HTTP logging : out of memory.\n");
3757 }
3758 else {
3759 if (log_len > sess->fe->capture_len)
3760 log_len = sess->fe->capture_len;
3761 memcpy(txn->srv_cookie, att_beg, log_len);
3762 txn->srv_cookie[log_len] = 0;
3763 }
3764 }
3765
3766 srv = objt_server(s->target);
3767 /* now check if we need to process it for persistence */
3768 if (!(s->flags & SF_IGNORE_PRST) &&
3769 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3770 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3771 /* assume passive cookie by default */
3772 txn->flags &= ~TX_SCK_MASK;
3773 txn->flags |= TX_SCK_FOUND;
3774
3775 /* If the cookie is in insert mode on a known server, we'll delete
3776 * this occurrence because we'll insert another one later.
3777 * We'll delete it too if the "indirect" option is set and we're in
3778 * a direct access.
3779 */
3780 if (s->be->ck_opts & PR_CK_PSV) {
3781 /* The "preserve" flag was set, we don't want to touch the
3782 * server's cookie.
3783 */
3784 }
3785 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3786 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3787 /* this cookie must be deleted */
3788 if (prev == hdr_beg && next == hdr_end) {
3789 /* whole header */
3790 http_remove_header(htx, &ctx);
3791 /* note: while both invalid now, <next> and <hdr_end>
3792 * are still equal, so the for() will stop as expected.
3793 */
3794 } else {
3795 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003796 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003797 next = prev;
3798 hdr_end += delta;
3799 }
3800 txn->flags &= ~TX_SCK_MASK;
3801 txn->flags |= TX_SCK_DELETED;
3802 /* and go on with next cookie */
3803 }
3804 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3805 /* replace bytes val_beg->val_end with the cookie name associated
3806 * with this server since we know it.
3807 */
3808 int sliding, delta;
3809
3810 ctx.value = ist2(val_beg, val_end - val_beg);
3811 ctx.lws_before = ctx.lws_after = 0;
3812 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3813 delta = srv->cklen - (val_end - val_beg);
3814 sliding = (ctx.value.ptr - val_beg);
3815 hdr_beg += sliding;
3816 val_beg += sliding;
3817 next += sliding + delta;
3818 hdr_end += sliding + delta;
3819
3820 txn->flags &= ~TX_SCK_MASK;
3821 txn->flags |= TX_SCK_REPLACED;
3822 }
3823 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3824 /* insert the cookie name associated with this server
3825 * before existing cookie, and insert a delimiter between them..
3826 */
3827 int sliding, delta;
3828 ctx.value = ist2(val_beg, 0);
3829 ctx.lws_before = ctx.lws_after = 0;
3830 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3831 delta = srv->cklen + 1;
3832 sliding = (ctx.value.ptr - val_beg);
3833 hdr_beg += sliding;
3834 val_beg += sliding;
3835 next += sliding + delta;
3836 hdr_end += sliding + delta;
3837
3838 val_beg[srv->cklen] = COOKIE_DELIM;
3839 txn->flags &= ~TX_SCK_MASK;
3840 txn->flags |= TX_SCK_REPLACED;
3841 }
3842 }
3843 /* that's done for this cookie, check the next one on the same
3844 * line when next != hdr_end (only if is_cookie2).
3845 */
3846 }
3847 }
3848}
3849
Christopher Faulet25a02f62018-10-24 12:00:25 +02003850/*
3851 * Parses the Cache-Control and Pragma request header fields to determine if
3852 * the request may be served from the cache and/or if it is cacheable. Updates
3853 * s->txn->flags.
3854 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003855void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003856{
3857 struct http_txn *txn = s->txn;
3858 struct htx *htx;
3859 int32_t pos;
3860 int pragma_found, cc_found, i;
3861
3862 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3863 return; /* nothing more to do here */
3864
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003865 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003866 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02003867 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003868 struct htx_blk *blk = htx_get_blk(htx, pos);
3869 enum htx_blk_type type = htx_get_blk_type(blk);
3870 struct ist n, v;
3871
3872 if (type == HTX_BLK_EOH)
3873 break;
3874 if (type != HTX_BLK_HDR)
3875 continue;
3876
3877 n = htx_get_blk_name(htx, blk);
3878 v = htx_get_blk_value(htx, blk);
3879
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003880 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003881 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3882 pragma_found = 1;
3883 continue;
3884 }
3885 }
3886
3887 /* Don't use the cache and don't try to store if we found the
3888 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003889 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003890 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3891 txn->flags |= TX_CACHE_IGNORE;
3892 continue;
3893 }
3894
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003895 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02003896 continue;
3897
3898 /* OK, right now we know we have a cache-control header */
3899 cc_found = 1;
3900 if (!v.len) /* no info */
3901 continue;
3902
3903 i = 0;
3904 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
3905 !isspace((unsigned char)*(v.ptr+i)))
3906 i++;
3907
3908 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
3909 * values after max-age, max-stale nor min-fresh, we simply don't
3910 * use the cache when they're specified.
3911 */
3912 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
3913 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
3914 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
3915 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
3916 txn->flags |= TX_CACHE_IGNORE;
3917 continue;
3918 }
3919
3920 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
3921 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3922 continue;
3923 }
3924 }
3925
3926 /* RFC7234#5.4:
3927 * When the Cache-Control header field is also present and
3928 * understood in a request, Pragma is ignored.
3929 * When the Cache-Control header field is not present in a
3930 * request, caches MUST consider the no-cache request
3931 * pragma-directive as having the same effect as if
3932 * "Cache-Control: no-cache" were present.
3933 */
3934 if (!cc_found && pragma_found)
3935 txn->flags |= TX_CACHE_IGNORE;
3936}
3937
3938/*
3939 * Check if response is cacheable or not. Updates s->txn->flags.
3940 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003941void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003942{
3943 struct http_txn *txn = s->txn;
3944 struct htx *htx;
3945 int32_t pos;
3946 int i;
3947
3948 if (txn->status < 200) {
3949 /* do not try to cache interim responses! */
3950 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3951 return;
3952 }
3953
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003954 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02003955 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003956 struct htx_blk *blk = htx_get_blk(htx, pos);
3957 enum htx_blk_type type = htx_get_blk_type(blk);
3958 struct ist n, v;
3959
3960 if (type == HTX_BLK_EOH)
3961 break;
3962 if (type != HTX_BLK_HDR)
3963 continue;
3964
3965 n = htx_get_blk_name(htx, blk);
3966 v = htx_get_blk_value(htx, blk);
3967
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003968 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003969 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3970 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3971 return;
3972 }
3973 }
3974
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003975 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02003976 continue;
3977
3978 /* OK, right now we know we have a cache-control header */
3979 if (!v.len) /* no info */
3980 continue;
3981
3982 i = 0;
3983 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
3984 !isspace((unsigned char)*(v.ptr+i)))
3985 i++;
3986
3987 /* we have a complete value between v.ptr and (v.ptr+i) */
3988 if (i < v.len && *(v.ptr + i) == '=') {
3989 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
3990 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
3991 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3992 continue;
3993 }
3994
3995 /* we have something of the form no-cache="set-cookie" */
3996 if ((v.len >= 21) &&
3997 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
3998 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
3999 txn->flags &= ~TX_CACHE_COOK;
4000 continue;
4001 }
4002
4003 /* OK, so we know that either p2 points to the end of string or to a comma */
4004 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
4005 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4006 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
4007 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4008 return;
4009 }
4010
4011 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4012 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4013 continue;
4014 }
4015 }
4016}
4017
Christopher Faulet377c5a52018-10-24 21:21:30 +02004018/*
4019 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4020 * for the current backend.
4021 *
4022 * It is assumed that the request is either a HEAD, GET, or POST and that the
4023 * uri_auth field is valid.
4024 *
4025 * Returns 1 if stats should be provided, otherwise 0.
4026 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004027static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004028{
4029 struct uri_auth *uri_auth = backend->uri_auth;
4030 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004031 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004032 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004033
4034 if (!uri_auth)
4035 return 0;
4036
4037 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4038 return 0;
4039
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004040 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004041 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004042 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01004043 if (*uri_auth->uri_prefix == '/')
4044 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004045
4046 /* check URI size */
4047 if (uri_auth->uri_len > uri.len)
4048 return 0;
4049
4050 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4051 return 0;
4052
4053 return 1;
4054}
4055
4056/* This function prepares an applet to handle the stats. It can deal with the
4057 * "100-continue" expectation, check that admin rules are met for POST requests,
4058 * and program a response message if something was unexpected. It cannot fail
4059 * and always relies on the stats applet to complete the job. It does not touch
4060 * analysers nor counters, which are left to the caller. It does not touch
4061 * s->target which is supposed to already point to the stats applet. The caller
4062 * is expected to have already assigned an appctx to the stream.
4063 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004064static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004065{
4066 struct stats_admin_rule *stats_admin_rule;
4067 struct stream_interface *si = &s->si[1];
4068 struct session *sess = s->sess;
4069 struct http_txn *txn = s->txn;
4070 struct http_msg *msg = &txn->req;
4071 struct uri_auth *uri_auth = s->be->uri_auth;
4072 const char *h, *lookup, *end;
4073 struct appctx *appctx;
4074 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004075 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004076
4077 appctx = si_appctx(si);
4078 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4079 appctx->st1 = appctx->st2 = 0;
4080 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004081 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004082 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4083 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4084 appctx->ctx.stats.flags |= STAT_CHUNKED;
4085
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004086 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004087 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004088 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4089 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004090
4091 for (h = lookup; h <= end - 3; h++) {
4092 if (memcmp(h, ";up", 3) == 0) {
4093 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4094 break;
4095 }
4096 }
4097
4098 if (uri_auth->refresh) {
4099 for (h = lookup; h <= end - 10; h++) {
4100 if (memcmp(h, ";norefresh", 10) == 0) {
4101 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4102 break;
4103 }
4104 }
4105 }
4106
4107 for (h = lookup; h <= end - 4; h++) {
4108 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004109 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004110 break;
4111 }
4112 }
4113
4114 for (h = lookup; h <= end - 6; h++) {
4115 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004116 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004117 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4118 break;
4119 }
4120 }
4121
Christopher Faulet6338a082019-09-09 15:50:54 +02004122 for (h = lookup; h <= end - 5; h++) {
4123 if (memcmp(h, ";json", 5) == 0) {
4124 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4125 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4126 break;
4127 }
4128 }
4129
4130 for (h = lookup; h <= end - 12; h++) {
4131 if (memcmp(h, ";json-schema", 12) == 0) {
4132 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4133 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4134 break;
4135 }
4136 }
4137
Christopher Faulet377c5a52018-10-24 21:21:30 +02004138 for (h = lookup; h <= end - 8; h++) {
4139 if (memcmp(h, ";st=", 4) == 0) {
4140 int i;
4141 h += 4;
4142 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4143 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4144 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4145 appctx->ctx.stats.st_code = i;
4146 break;
4147 }
4148 }
4149 break;
4150 }
4151 }
4152
4153 appctx->ctx.stats.scope_str = 0;
4154 appctx->ctx.stats.scope_len = 0;
4155 for (h = lookup; h <= end - 8; h++) {
4156 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4157 int itx = 0;
4158 const char *h2;
4159 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4160 const char *err;
4161
4162 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4163 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004164 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4165 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004166 if (*h == ';' || *h == '&' || *h == ' ')
4167 break;
4168 itx++;
4169 h++;
4170 }
4171
4172 if (itx > STAT_SCOPE_TXT_MAXLEN)
4173 itx = STAT_SCOPE_TXT_MAXLEN;
4174 appctx->ctx.stats.scope_len = itx;
4175
4176 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4177 memcpy(scope_txt, h2, itx);
4178 scope_txt[itx] = '\0';
4179 err = invalid_char(scope_txt);
4180 if (err) {
4181 /* bad char in search text => clear scope */
4182 appctx->ctx.stats.scope_str = 0;
4183 appctx->ctx.stats.scope_len = 0;
4184 }
4185 break;
4186 }
4187 }
4188
4189 /* now check whether we have some admin rules for this request */
4190 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4191 int ret = 1;
4192
4193 if (stats_admin_rule->cond) {
4194 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4195 ret = acl_pass(ret);
4196 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4197 ret = !ret;
4198 }
4199
4200 if (ret) {
4201 /* no rule, or the rule matches */
4202 appctx->ctx.stats.flags |= STAT_ADMIN;
4203 break;
4204 }
4205 }
4206
Christopher Faulet5d45e382019-02-27 15:15:23 +01004207 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4208 appctx->st0 = STAT_HTTP_HEAD;
4209 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004210 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004211 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004212 if (msg->msg_state < HTTP_MSG_DATA)
4213 req->analysers |= AN_REQ_HTTP_BODY;
4214 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004215 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004216 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004217 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4218 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4219 appctx->st0 = STAT_HTTP_LAST;
4220 }
4221 }
4222 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004223 /* Unsupported method */
4224 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4225 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4226 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004227 }
4228
4229 s->task->nice = -32; /* small boost for HTTP statistics */
4230 return 1;
4231}
4232
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004233void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004234{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004235 struct channel *req = &s->req;
4236 struct channel *res = &s->res;
4237 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004238 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004239 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004240 struct ist path, location;
4241 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004242
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004243 /*
4244 * Create the location
4245 */
4246 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004247
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004248 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004249 /* special prefix "/" means don't change URL */
4250 srv = __objt_server(s->target);
4251 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4252 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4253 return;
4254 }
4255
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004256 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004257 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004258 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004259 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004260 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004261 return;
4262
4263 if (!chunk_memcat(&trash, path.ptr, path.len))
4264 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004265 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004266
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004267 /*
4268 * Create the 302 respone
4269 */
4270 htx = htx_from_buf(&res->buf);
4271 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4272 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4273 ist("HTTP/1.1"), ist("302"), ist("Found"));
4274 if (!sl)
4275 goto fail;
4276 sl->info.res.status = 302;
4277 s->txn->status = 302;
4278
4279 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4280 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4281 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4282 !htx_add_header(htx, ist("Location"), location))
4283 goto fail;
4284
4285 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4286 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004287
Christopher Fauletc20afb82020-01-24 19:16:26 +01004288 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004289 if (!http_forward_proxy_resp(s, 1))
4290 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004291
4292 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004293 si_shutr(si);
4294 si_shutw(si);
4295 si->err_type = SI_ET_NONE;
4296 si->state = SI_ST_CLO;
4297
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004298 if (!(s->flags & SF_ERR_MASK))
4299 s->flags |= SF_ERR_LOCAL;
4300 if (!(s->flags & SF_FINST_MASK))
4301 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004302
4303 /* FIXME: we should increase a counter of redirects per server and per backend. */
4304 srv_inc_sess_ctr(srv);
4305 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004306 return;
4307
4308 fail:
4309 /* If an error occurred, remove the incomplete HTTP response from the
4310 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004311 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004312}
4313
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004314/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004315 * because an error was triggered during the body forwarding.
4316 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004317static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004318{
4319 struct channel *chn = &s->req;
4320 struct http_txn *txn = s->txn;
4321
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004322 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004323
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004324 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4325 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004326 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004327 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004328 goto end;
4329 }
4330
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004331 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4332 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004333 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004334 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004335
4336 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004337 /* No need to read anymore, the request was completely parsed.
4338 * We can shut the read side unless we want to abort_on_close,
4339 * or we have a POST request. The issue with POST requests is
4340 * that some browsers still send a CRLF after the request, and
4341 * this CRLF must be read so that it does not remain in the kernel
4342 * buffers, otherwise a close could cause an RST on some systems
4343 * (eg: Linux).
4344 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004345 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004346 channel_dont_read(chn);
4347
4348 /* if the server closes the connection, we want to immediately react
4349 * and close the socket to save packets and syscalls.
4350 */
4351 s->si[1].flags |= SI_FL_NOHALF;
4352
4353 /* In any case we've finished parsing the request so we must
4354 * disable Nagle when sending data because 1) we're not going
4355 * to shut this side, and 2) the server is waiting for us to
4356 * send pending data.
4357 */
4358 chn->flags |= CF_NEVER_WAIT;
4359
Christopher Fauletd01ce402019-01-02 17:44:13 +01004360 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4361 /* The server has not finished to respond, so we
4362 * don't want to move in order not to upset it.
4363 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004364 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004365 return;
4366 }
4367
Christopher Fauletf2824e62018-10-01 12:12:37 +02004368 /* When we get here, it means that both the request and the
4369 * response have finished receiving. Depending on the connection
4370 * mode, we'll have to wait for the last bytes to leave in either
4371 * direction, and sometimes for a close to be effective.
4372 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004373 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004374 /* Tunnel mode will not have any analyser so it needs to
4375 * poll for reads.
4376 */
4377 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004378 if (b_data(&chn->buf)) {
4379 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004380 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004381 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004382 txn->req.msg_state = HTTP_MSG_TUNNEL;
4383 }
4384 else {
4385 /* we're not expecting any new data to come for this
4386 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004387 *
4388 * However, there is an exception if the response
4389 * length is undefined. In this case, we need to wait
4390 * the close from the server. The response will be
4391 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004392 */
4393 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4394 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004395 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004396
4397 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4398 channel_shutr_now(chn);
4399 channel_shutw_now(chn);
4400 }
4401 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004402 goto check_channel_flags;
4403 }
4404
4405 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4406 http_msg_closing:
4407 /* nothing else to forward, just waiting for the output buffer
4408 * to be empty and for the shutw_now to take effect.
4409 */
4410 if (channel_is_empty(chn)) {
4411 txn->req.msg_state = HTTP_MSG_CLOSED;
4412 goto http_msg_closed;
4413 }
4414 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004415 txn->req.msg_state = HTTP_MSG_ERROR;
4416 goto end;
4417 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004418 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004419 return;
4420 }
4421
4422 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4423 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004424 /* if we don't know whether the server will close, we need to hard close */
4425 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4426 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004427 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004428 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004429 channel_dont_read(chn);
4430 goto end;
4431 }
4432
4433 check_channel_flags:
4434 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4435 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4436 /* if we've just closed an output, let's switch */
4437 txn->req.msg_state = HTTP_MSG_CLOSING;
4438 goto http_msg_closing;
4439 }
4440
4441 end:
4442 chn->analysers &= AN_REQ_FLT_END;
4443 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4444 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4445 channel_auto_close(chn);
4446 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004447 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004448}
4449
4450
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004451/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004452 * because an error was triggered during the body forwarding.
4453 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004454static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004455{
4456 struct channel *chn = &s->res;
4457 struct http_txn *txn = s->txn;
4458
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004459 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004460
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004461 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4462 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004463 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004464 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004465 goto end;
4466 }
4467
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004468 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4469 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004470 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004471 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004472
4473 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4474 /* In theory, we don't need to read anymore, but we must
4475 * still monitor the server connection for a possible close
4476 * while the request is being uploaded, so we don't disable
4477 * reading.
4478 */
4479 /* channel_dont_read(chn); */
4480
4481 if (txn->req.msg_state < HTTP_MSG_DONE) {
4482 /* The client seems to still be sending data, probably
4483 * because we got an error response during an upload.
4484 * We have the choice of either breaking the connection
4485 * or letting it pass through. Let's do the later.
4486 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004487 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004488 return;
4489 }
4490
4491 /* When we get here, it means that both the request and the
4492 * response have finished receiving. Depending on the connection
4493 * mode, we'll have to wait for the last bytes to leave in either
4494 * direction, and sometimes for a close to be effective.
4495 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004496 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004497 channel_auto_read(chn);
4498 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004499 if (b_data(&chn->buf)) {
4500 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004501 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004502 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004503 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4504 }
4505 else {
4506 /* we're not expecting any new data to come for this
4507 * transaction, so we can close it.
4508 */
4509 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4510 channel_shutr_now(chn);
4511 channel_shutw_now(chn);
4512 }
4513 }
4514 goto check_channel_flags;
4515 }
4516
4517 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4518 http_msg_closing:
4519 /* nothing else to forward, just waiting for the output buffer
4520 * to be empty and for the shutw_now to take effect.
4521 */
4522 if (channel_is_empty(chn)) {
4523 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4524 goto http_msg_closed;
4525 }
4526 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004527 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004528 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004529 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004530 if (strm_sess(s)->listener->counters)
4531 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004532 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004533 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004534 goto end;
4535 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004536 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004537 return;
4538 }
4539
4540 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4541 http_msg_closed:
4542 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004543 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004544 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004545 goto end;
4546 }
4547
4548 check_channel_flags:
4549 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4550 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4551 /* if we've just closed an output, let's switch */
4552 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4553 goto http_msg_closing;
4554 }
4555
4556 end:
4557 chn->analysers &= AN_RES_FLT_END;
4558 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4559 chn->analysers |= AN_RES_FLT_XFER_DATA;
4560 channel_auto_close(chn);
4561 channel_auto_read(chn);
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}
4564
Christopher Fauletef70e252020-01-28 09:26:19 +01004565/* Forward a response generated by HAProxy (error/redirect/return). This
4566 * function forwards all pending incoming data. If <final> is set to 0, nothing
4567 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004568 * transaction is terminated and the request is emptied. On success 1 is
4569 * returned. If an error occurred, 0 is returned.
Christopher Fauletef70e252020-01-28 09:26:19 +01004570 */
4571int http_forward_proxy_resp(struct stream *s, int final)
4572{
4573 struct channel *req = &s->req;
4574 struct channel *res = &s->res;
4575 struct htx *htx = htxbuf(&res->buf);
4576 size_t data;
4577
4578 if (final) {
4579 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004580
4581 if (!http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004582 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004583
4584 channel_auto_read(req);
4585 channel_abort(req);
4586 channel_auto_close(req);
4587 channel_htx_erase(req, htxbuf(&req->buf));
4588
4589 res->wex = tick_add_ifset(now_ms, res->wto);
4590 channel_auto_read(res);
4591 channel_auto_close(res);
4592 channel_shutr_now(res);
4593 }
4594
4595 data = htx->data - co_data(res);
4596 c_adv(res, data);
4597 htx->first = -1;
4598 res->total += data;
4599 return 1;
4600}
4601
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004602void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004603 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004604{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004605 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004606 if (!(s->flags & SF_ERR_MASK))
4607 s->flags |= err;
4608 if (!(s->flags & SF_FINST_MASK))
4609 s->flags |= finst;
4610}
4611
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004612void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004613{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004614 if (!msg) {
4615 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4616 goto end;
4617 }
4618
4619 if (http_reply_message(s, msg) == -1) {
4620 /* On error, return a 500 error message, but don't rewrite it if
4621 * it is already an internal error.
4622 */
4623 if (s->txn->status == 500)
4624 s->txn->flags |= TX_CONST_REPLY;
4625 s->txn->status = 500;
4626 s->txn->http_reply = NULL;
4627 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4628 }
4629
4630end:
4631 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
4632 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
4633
Christopher Faulet0f226952018-10-22 09:29:56 +02004634 channel_auto_read(&s->req);
4635 channel_abort(&s->req);
4636 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004637 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004638 channel_auto_read(&s->res);
4639 channel_auto_close(&s->res);
4640 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004641}
4642
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004643struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004644{
4645 const int msgnum = http_get_status_idx(s->txn->status);
4646
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004647 if (s->txn->http_reply)
4648 return s->txn->http_reply;
4649 else if (s->be->replies[msgnum])
4650 return s->be->replies[msgnum];
4651 else if (strm_fe(s)->replies[msgnum])
4652 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004653 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004654 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004655}
4656
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004657/* Produces a response from an http reply. Depending on the http reply type, a,
4658 * errorfile, an raw file or a log-format string is used. On success, it returns
4659 * 0. If an error occurs -1 is returned.
4660 */
4661int http_reply_message(struct stream *s, struct http_reply *reply)
4662{
4663 struct channel *res = &s->res;
4664 struct buffer *errmsg;
4665 struct htx *htx = htx_from_buf(&res->buf);
4666 struct htx_sl *sl;
4667 struct buffer *body = NULL;
4668 const char *status, *reason, *clen, *ctype;
4669 unsigned int slflags;
4670 int ret = 0;
4671
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004672 if (s->txn->status == -1)
4673 s->txn->status = reply->status;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004674 channel_htx_truncate(res, htx);
4675
Christopher Faulete29a97e2020-05-14 14:49:25 +02004676 /*
4677 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4678 *
4679 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4680 * as no payload if NULL. the TXN status code is set with the status
4681 * of the original reply.
4682 */
4683
4684 if (reply->type == HTTP_REPLY_INDIRECT) {
4685 if (reply->body.reply)
4686 reply = reply->body.reply;
4687 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004688 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4689 /* get default error message */
4690 if (reply == s->txn->http_reply)
4691 s->txn->http_reply = NULL;
4692 reply = http_error_message(s);
4693 if (reply->type == HTTP_REPLY_INDIRECT) {
4694 if (reply->body.reply)
4695 reply = reply->body.reply;
4696 }
4697 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004698
4699 if (reply->type == HTTP_REPLY_ERRMSG) {
4700 /* implicit or explicit error message*/
4701 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004702 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004703 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004704 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004705 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004706 }
4707 else {
4708 /* no payload, file or log-format string */
4709 if (reply->type == HTTP_REPLY_RAW) {
4710 /* file */
4711 body = &reply->body.obj;
4712 }
4713 else if (reply->type == HTTP_REPLY_LOGFMT) {
4714 /* log-format string */
4715 body = alloc_trash_chunk();
4716 if (!body)
4717 goto fail_alloc;
4718 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4719 }
4720 /* else no payload */
4721
4722 status = ultoa(reply->status);
4723 reason = http_get_reason(reply->status);
4724 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4725 if (!body || !b_data(body))
4726 slflags |= HTX_SL_F_BODYLESS;
4727 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4728 if (!sl)
4729 goto fail;
4730 sl->info.res.status = reply->status;
4731
4732 clen = (body ? ultoa(b_data(body)) : "0");
4733 ctype = reply->ctype;
4734
4735 if (!LIST_ISEMPTY(&reply->hdrs)) {
4736 struct http_reply_hdr *hdr;
4737 struct buffer *value = alloc_trash_chunk();
4738
4739 if (!value)
4740 goto fail;
4741
4742 list_for_each_entry(hdr, &reply->hdrs, list) {
4743 chunk_reset(value);
4744 value->data = build_logline(s, value->area, value->size, &hdr->value);
4745 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4746 free_trash_chunk(value);
4747 goto fail;
4748 }
4749 chunk_reset(value);
4750 }
4751 free_trash_chunk(value);
4752 }
4753
4754 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4755 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4756 !htx_add_endof(htx, HTX_BLK_EOH) ||
4757 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))) ||
4758 !htx_add_endof(htx, HTX_BLK_EOM))
4759 goto fail;
4760 }
4761
4762 htx_to_buf(htx, &s->res.buf);
4763 if (!http_forward_proxy_resp(s, 1))
4764 goto fail;
4765
4766 leave:
4767 if (reply->type == HTTP_REPLY_LOGFMT)
4768 free_trash_chunk(body);
4769 return ret;
4770
4771 fail_alloc:
4772 if (!(s->flags & SF_ERR_MASK))
4773 s->flags |= SF_ERR_RESOURCE;
4774 ret = -1;
4775 goto leave;
4776
4777 fail:
4778 /* If an error occurred, remove the incomplete HTTP response from the
4779 * buffer */
4780 channel_htx_truncate(res, htx);
4781 ret = -1;
4782 if (!(s->flags & SF_ERR_MASK))
4783 s->flags |= SF_ERR_PRXCOND;
4784 goto leave;
4785}
4786
Christopher Faulet304cc402019-07-15 15:46:28 +02004787/* Return the error message corresponding to si->err_type. It is assumed
4788 * that the server side is closed. Note that err_type is actually a
4789 * bitmask, where almost only aborts may be cumulated with other
4790 * values. We consider that aborted operations are more important
4791 * than timeouts or errors due to the fact that nobody else in the
4792 * logs might explain incomplete retries. All others should avoid
4793 * being cumulated. It should normally not be possible to have multiple
4794 * aborts at once, but just in case, the first one in sequence is reported.
4795 * Note that connection errors appearing on the second request of a keep-alive
4796 * connection are not reported since this allows the client to retry.
4797 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004798void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004799{
4800 int err_type = si->err_type;
4801
4802 /* set s->txn->status for http_error_message(s) */
4803 s->txn->status = 503;
4804
4805 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004806 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4807 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004808 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004809 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4810 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4811 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004812 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004813 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4814 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004815 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004816 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4817 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004818 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004819 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4820 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4821 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004822 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004823 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4824 (s->flags & SF_SRV_REUSED) ? NULL :
4825 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004826 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004827 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4828 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4829 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004830 else { /* SI_ET_CONN_OTHER and others */
4831 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004832 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4833 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004834 }
4835}
4836
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004837
Christopher Faulet4a28a532019-03-01 11:19:40 +01004838/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4839 * on success and -1 on error.
4840 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004841static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004842{
4843 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4844 * then we must send an HTTP/1.1 100 Continue intermediate response.
4845 */
4846 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4847 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4848 struct ist hdr = { .ptr = "Expect", .len = 6 };
4849 struct http_hdr_ctx ctx;
4850
4851 ctx.blk = NULL;
4852 /* Expect is allowed in 1.1, look for it */
4853 if (http_find_header(htx, hdr, &ctx, 0) &&
4854 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004855 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004856 return -1;
4857 http_remove_header(htx, &ctx);
4858 }
4859 }
4860 return 0;
4861}
4862
Christopher Faulet23a3c792018-11-28 10:01:23 +01004863/* Send a 100-Continue response to the client. It returns 0 on success and -1
4864 * on error. The response channel is updated accordingly.
4865 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004866static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004867{
4868 struct channel *res = &s->res;
4869 struct htx *htx = htx_from_buf(&res->buf);
4870 struct htx_sl *sl;
4871 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4872 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004873
4874 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4875 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4876 if (!sl)
4877 goto fail;
4878 sl->info.res.status = 100;
4879
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004880 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004881 goto fail;
4882
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004883 if (!http_forward_proxy_resp(s, 0))
4884 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004885 return 0;
4886
4887 fail:
4888 /* If an error occurred, remove the incomplete HTTP response from the
4889 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004890 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004891 return -1;
4892}
4893
Christopher Faulet12c51e22018-11-28 15:59:42 +01004894
4895/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
4896 * ont whether we use a proxy or not. It returns 0 on success and -1 on
4897 * error. The response channel is updated accordingly.
4898 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004899static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
Christopher Faulet12c51e22018-11-28 15:59:42 +01004900{
4901 struct channel *res = &s->res;
4902 struct htx *htx = htx_from_buf(&res->buf);
4903 struct htx_sl *sl;
4904 struct ist code, body;
4905 int status;
4906 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
Christopher Faulet12c51e22018-11-28 15:59:42 +01004907
4908 if (!(s->txn->flags & TX_USE_PX_CONN)) {
4909 status = 401;
4910 code = ist("401");
4911 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
4912 "You need a valid user and password to access this content.\n"
4913 "</body></html>\n");
4914 }
4915 else {
4916 status = 407;
4917 code = ist("407");
4918 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
4919 "You need a valid user and password to access this content.\n"
4920 "</body></html>\n");
4921 }
4922
4923 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4924 ist("HTTP/1.1"), code, ist("Unauthorized"));
4925 if (!sl)
4926 goto fail;
4927 sl->info.res.status = status;
4928 s->txn->status = status;
4929
4930 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
4931 goto fail;
4932
Willy Tarreaub5ba2b02019-06-11 16:08:25 +02004933 if (!htx_add_header(htx, ist("Content-length"), ist("112")) ||
4934 !htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Faulet12c51e22018-11-28 15:59:42 +01004935 !htx_add_header(htx, ist("Connection"), ist("close")) ||
Jérôme Magnin86cef232018-12-28 14:49:08 +01004936 !htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4937 goto fail;
4938 if (status == 401 && !htx_add_header(htx, ist("WWW-Authenticate"), ist2(trash.area, trash.data)))
4939 goto fail;
4940 if (status == 407 && !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
Christopher Faulet12c51e22018-11-28 15:59:42 +01004941 goto fail;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004942 if (!htx_add_endof(htx, HTX_BLK_EOH))
4943 goto fail;
4944
4945 while (body.len) {
4946 size_t sent = htx_add_data(htx, body);
4947 if (!sent)
4948 goto fail;
4949 body.ptr += sent;
4950 body.len -= sent;
4951 }
4952
4953 if (!htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet12c51e22018-11-28 15:59:42 +01004954 goto fail;
4955
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004956 if (!http_forward_proxy_resp(s, 1))
4957 goto fail;
Christopher Faulet12c51e22018-11-28 15:59:42 +01004958 return 0;
4959
4960 fail:
4961 /* If an error occurred, remove the incomplete HTTP response from the
4962 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004963 channel_htx_truncate(res, htx);
Christopher Faulet12c51e22018-11-28 15:59:42 +01004964 return -1;
4965}
4966
Christopher Faulet0f226952018-10-22 09:29:56 +02004967/*
4968 * Capture headers from message <htx> according to header list <cap_hdr>, and
4969 * fill the <cap> pointers appropriately.
4970 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004971static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004972{
4973 struct cap_hdr *h;
4974 int32_t pos;
4975
Christopher Fauleta3f15502019-05-13 15:27:23 +02004976 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004977 struct htx_blk *blk = htx_get_blk(htx, pos);
4978 enum htx_blk_type type = htx_get_blk_type(blk);
4979 struct ist n, v;
4980
4981 if (type == HTX_BLK_EOH)
4982 break;
4983 if (type != HTX_BLK_HDR)
4984 continue;
4985
4986 n = htx_get_blk_name(htx, blk);
4987
4988 for (h = cap_hdr; h; h = h->next) {
4989 if (h->namelen && (h->namelen == n.len) &&
4990 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4991 if (cap[h->index] == NULL)
4992 cap[h->index] =
4993 pool_alloc(h->pool);
4994
4995 if (cap[h->index] == NULL) {
4996 ha_alert("HTTP capture : out of memory.\n");
4997 break;
4998 }
4999
5000 v = htx_get_blk_value(htx, blk);
5001 if (v.len > h->len)
5002 v.len = h->len;
5003
5004 memcpy(cap[h->index], v.ptr, v.len);
5005 cap[h->index][v.len]=0;
5006 }
5007 }
5008 }
5009}
5010
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005011/* Delete a value in a header between delimiters <from> and <next>. The header
5012 * itself is delimited by <start> and <end> pointers. The number of characters
5013 * displaced is returned, and the pointer to the first delimiter is updated if
5014 * required. The function tries as much as possible to respect the following
5015 * principles :
5016 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5017 * in which case <next> is simply removed
5018 * - set exactly one space character after the new first delimiter, unless there
5019 * are not enough characters in the block being moved to do so.
5020 * - remove unneeded spaces before the previous delimiter and after the new
5021 * one.
5022 *
5023 * It is the caller's responsibility to ensure that :
5024 * - <from> points to a valid delimiter or <start> ;
5025 * - <next> points to a valid delimiter or <end> ;
5026 * - there are non-space chars before <from>.
5027 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005028static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005029{
5030 char *prev = *from;
5031
5032 if (prev == start) {
5033 /* We're removing the first value. eat the semicolon, if <next>
5034 * is lower than <end> */
5035 if (next < end)
5036 next++;
5037
5038 while (next < end && HTTP_IS_SPHT(*next))
5039 next++;
5040 }
5041 else {
5042 /* Remove useless spaces before the old delimiter. */
5043 while (HTTP_IS_SPHT(*(prev-1)))
5044 prev--;
5045 *from = prev;
5046
5047 /* copy the delimiter and if possible a space if we're
5048 * not at the end of the line.
5049 */
5050 if (next < end) {
5051 *prev++ = *next++;
5052 if (prev + 1 < next)
5053 *prev++ = ' ';
5054 while (next < end && HTTP_IS_SPHT(*next))
5055 next++;
5056 }
5057 }
5058 memmove(prev, next, end - next);
5059 return (prev - next);
5060}
5061
Christopher Faulet0f226952018-10-22 09:29:56 +02005062
5063/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005064 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005065 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005066static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005067{
5068 struct ist dst = ist2(str, 0);
5069
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005070 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005071 goto end;
5072 if (dst.len + 1 > len)
5073 goto end;
5074 dst.ptr[dst.len++] = ' ';
5075
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005076 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005077 goto end;
5078 if (dst.len + 1 > len)
5079 goto end;
5080 dst.ptr[dst.len++] = ' ';
5081
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005082 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005083 end:
5084 return dst.len;
5085}
5086
5087/*
5088 * Print a debug line with a start line.
5089 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005090static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005091{
5092 struct session *sess = strm_sess(s);
5093 int max;
5094
5095 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5096 dir,
5097 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5098 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5099
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005100 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005101 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005102 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005103 trash.area[trash.data++] = ' ';
5104
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005105 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005106 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005107 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005108 trash.area[trash.data++] = ' ';
5109
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005110 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005111 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005112 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005113 trash.area[trash.data++] = '\n';
5114
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005115 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005116}
5117
5118/*
5119 * Print a debug line with a header.
5120 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005121static 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 +02005122{
5123 struct session *sess = strm_sess(s);
5124 int max;
5125
5126 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5127 dir,
5128 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5129 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5130
5131 max = n.len;
5132 UBOUND(max, trash.size - trash.data - 3);
5133 chunk_memcat(&trash, n.ptr, max);
5134 trash.area[trash.data++] = ':';
5135 trash.area[trash.data++] = ' ';
5136
5137 max = v.len;
5138 UBOUND(max, trash.size - trash.data - 1);
5139 chunk_memcat(&trash, v.ptr, max);
5140 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
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005145/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5146 * In case of allocation failure, everything allocated is freed and NULL is
5147 * returned. Otherwise the new transaction is assigned to the stream and
5148 * returned.
5149 */
5150struct http_txn *http_alloc_txn(struct stream *s)
5151{
5152 struct http_txn *txn = s->txn;
5153
5154 if (txn)
5155 return txn;
5156
5157 txn = pool_alloc(pool_head_http_txn);
5158 if (!txn)
5159 return txn;
5160
5161 s->txn = txn;
5162 return txn;
5163}
5164
5165void http_txn_reset_req(struct http_txn *txn)
5166{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005167 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005168 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5169}
5170
5171void http_txn_reset_res(struct http_txn *txn)
5172{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005173 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005174 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5175}
5176
5177/*
5178 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5179 * the required fields are properly allocated and that we only need to (re)init
5180 * them. This should be used before processing any new request.
5181 */
5182void http_init_txn(struct stream *s)
5183{
5184 struct http_txn *txn = s->txn;
5185 struct conn_stream *cs = objt_cs(s->si[0].end);
5186
5187 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5188 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5189 : 0);
5190 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005191 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005192 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005193
5194 txn->cookie_first_date = 0;
5195 txn->cookie_last_date = 0;
5196
5197 txn->srv_cookie = NULL;
5198 txn->cli_cookie = NULL;
5199 txn->uri = NULL;
5200
5201 http_txn_reset_req(txn);
5202 http_txn_reset_res(txn);
5203
5204 txn->req.chn = &s->req;
5205 txn->rsp.chn = &s->res;
5206
5207 txn->auth.method = HTTP_AUTH_UNKNOWN;
5208
5209 vars_init(&s->vars_txn, SCOPE_TXN);
5210 vars_init(&s->vars_reqres, SCOPE_REQ);
5211}
5212
5213/* to be used at the end of a transaction */
5214void http_end_txn(struct stream *s)
5215{
5216 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005217
5218 /* these ones will have been dynamically allocated */
5219 pool_free(pool_head_requri, txn->uri);
5220 pool_free(pool_head_capture, txn->cli_cookie);
5221 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005222 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005223
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005224 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005225 txn->uri = NULL;
5226 txn->srv_cookie = NULL;
5227 txn->cli_cookie = NULL;
5228
Christopher Faulet59399252019-11-07 14:27:52 +01005229 if (!LIST_ISEMPTY(&s->vars_txn.head))
5230 vars_prune(&s->vars_txn, s->sess, s);
5231 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5232 vars_prune(&s->vars_reqres, s->sess, s);
5233}
5234
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005235
5236DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005237
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005238__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005239static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005240{
5241}
5242
5243
5244/*
5245 * Local variables:
5246 * c-indent-level: 8
5247 * c-basic-offset: 8
5248 * End:
5249 */