blob: 41f119de05a44d22f7cb7775ed6f711cc71ac965 [file] [log] [blame]
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020013#include <haproxy/api.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020014#include <haproxy/base64.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010015#include <common/htx.h>
Willy Tarreau8b507582020-02-25 09:35:07 +010016#include <common/net_helper.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020017#include <common/uri_auth.h>
18
Christopher Faulet0f226952018-10-22 09:29:56 +020019#include <types/capture.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020020
21#include <proto/acl.h>
Christopher Faulet3e964192018-10-24 11:39:23 +020022#include <proto/action.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020023#include <proto/channel.h>
24#include <proto/checks.h>
25#include <proto/connection.h>
26#include <proto/filters.h>
Christopher Faulet0f226952018-10-22 09:29:56 +020027#include <proto/http_htx.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020028#include <proto/log.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020029#include <proto/http_ana.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020030#include <proto/proxy.h>
Christopher Fauletfefc73d2018-10-24 21:18:04 +020031#include <proto/server.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020032#include <proto/stream.h>
33#include <proto/stream_interface.h>
34#include <proto/stats.h>
Christopher Fauleta8a46e22019-07-16 14:53:09 +020035#include <proto/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020036
Christopher Fauleteea8fc72019-11-05 16:18:10 +010037#define TRACE_SOURCE &trace_strm
38
Christopher Faulet377c5a52018-10-24 21:21:30 +020039extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020040
Christopher Fauleta8a46e22019-07-16 14:53:09 +020041struct pool_head *pool_head_requri = NULL;
42struct pool_head *pool_head_capture = NULL;
43
44
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020045static void http_end_request(struct stream *s);
46static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020047
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020048static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
49static int http_del_hdr_value(char *start, char *end, char **from, char *next);
50static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020051static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
52static 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 +020053
Christopher Fauletb58f62b2020-01-13 16:40:13 +010054static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020055static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020056
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020057static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
58static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020059
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020060static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
61static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020062
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020063static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
64static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010065
Christopher Faulete0768eb2018-10-03 16:38:02 +020066/* This stream analyser waits for a complete HTTP request. It returns 1 if the
67 * processing can continue on next analysers, or zero if it either needs more
68 * data or wants to immediately abort the request (eg: timeout, error, ...). It
69 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
70 * when it has nothing left to do, and may remove any analyser when it wants to
71 * abort.
72 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020073int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020074{
Christopher Faulet9768c262018-10-22 09:34:31 +020075
Christopher Faulete0768eb2018-10-03 16:38:02 +020076 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020077 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020078 *
Christopher Faulet9768c262018-10-22 09:34:31 +020079 * Once the start line and all headers are received, we may perform a
80 * capture of the error (if any), and we will set a few fields. We also
81 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020082 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020083 struct session *sess = s->sess;
84 struct http_txn *txn = s->txn;
85 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020086 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010087 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020088
Christopher Fauleteea8fc72019-11-05 16:18:10 +010089 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020090
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010091 htx = htxbuf(&req->buf);
Christopher Faulet9768c262018-10-22 09:34:31 +020092
Willy Tarreau4236f032019-03-05 10:43:32 +010093 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +020094 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +010095 stream_inc_http_req_ctr(s);
96 stream_inc_http_err_ctr(s);
97 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletb9a92f32019-09-09 10:15:21 +020098 if (htx->flags & HTX_FL_PARSING_ERROR)
99 goto return_bad_req;
100 else
101 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100102 }
103
Christopher Faulete0768eb2018-10-03 16:38:02 +0200104 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200105 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200106
107 /* If there is data available for analysis, log the end of the idle time. */
Christopher Faulet870aad92018-11-29 15:23:46 +0100108 if (c_data(req) && s->logs.t_idle == -1) {
109 const struct cs_info *csinfo = si_get_cs_info(objt_cs(s->si[0].end));
110
111 s->logs.t_idle = ((csinfo)
112 ? csinfo->t_idle
113 : tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake);
114 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200115
Christopher Faulete0768eb2018-10-03 16:38:02 +0200116 /*
117 * Now we quickly check if we have found a full valid request.
118 * If not so, we check the FD and buffer states before leaving.
119 * A full request is indicated by the fact that we have seen
120 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
121 * requests are checked first. When waiting for a second request
122 * on a keep-alive stream, if we encounter and error, close, t/o,
123 * we note the error in the stream flags but don't set any state.
124 * Since the error will be noted there, it will not be counted by
125 * process_stream() as a frontend error.
126 * Last, we may increase some tracked counters' http request errors on
127 * the cases that are deliberately the client's fault. For instance,
128 * a timeout or connection reset is not counted as an error. However
129 * a bad request is.
130 */
Christopher Faulet29f17582019-05-23 11:03:26 +0200131 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200132 if (htx->flags & HTX_FL_UPGRADE)
133 goto failed_keep_alive;
134
Christopher Faulet9768c262018-10-22 09:34:31 +0200135 /* 1: have we encountered a read error ? */
136 if (req->flags & CF_READ_ERROR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200137 if (!(s->flags & SF_ERR_MASK))
138 s->flags |= SF_ERR_CLICL;
139
140 if (txn->flags & TX_WAIT_NEXT_RQ)
141 goto failed_keep_alive;
142
143 if (sess->fe->options & PR_O_IGNORE_PRB)
144 goto failed_keep_alive;
145
Christopher Faulet9768c262018-10-22 09:34:31 +0200146 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200147 stream_inc_http_req_ctr(s);
148 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100149 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200150 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100151 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200152
Christopher Faulet9768c262018-10-22 09:34:31 +0200153 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200154 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet9768c262018-10-22 09:34:31 +0200155 req->analysers &= AN_REQ_FLT_END;
156
Christopher Faulete0768eb2018-10-03 16:38:02 +0200157 if (!(s->flags & SF_FINST_MASK))
158 s->flags |= SF_FINST_R;
159 return 0;
160 }
161
Christopher Faulet9768c262018-10-22 09:34:31 +0200162 /* 2: has the read timeout expired ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200163 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
164 if (!(s->flags & SF_ERR_MASK))
165 s->flags |= SF_ERR_CLITO;
166
167 if (txn->flags & TX_WAIT_NEXT_RQ)
168 goto failed_keep_alive;
169
170 if (sess->fe->options & PR_O_IGNORE_PRB)
171 goto failed_keep_alive;
172
Christopher Faulet9768c262018-10-22 09:34:31 +0200173 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200174 stream_inc_http_req_ctr(s);
175 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100176 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200177 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100178 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200179
Christopher Faulet9768c262018-10-22 09:34:31 +0200180 txn->status = 408;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200181 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200182 req->analysers &= AN_REQ_FLT_END;
183
Christopher Faulete0768eb2018-10-03 16:38:02 +0200184 if (!(s->flags & SF_FINST_MASK))
185 s->flags |= SF_FINST_R;
186 return 0;
187 }
188
Christopher Faulet9768c262018-10-22 09:34:31 +0200189 /* 3: have we encountered a close ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200190 else if (req->flags & CF_SHUTR) {
191 if (!(s->flags & SF_ERR_MASK))
192 s->flags |= SF_ERR_CLICL;
193
194 if (txn->flags & TX_WAIT_NEXT_RQ)
195 goto failed_keep_alive;
196
197 if (sess->fe->options & PR_O_IGNORE_PRB)
198 goto failed_keep_alive;
199
Christopher Faulete0768eb2018-10-03 16:38:02 +0200200 stream_inc_http_err_ctr(s);
201 stream_inc_http_req_ctr(s);
202 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100203 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200204 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100205 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200206
Christopher Faulet9768c262018-10-22 09:34:31 +0200207 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200208 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200209 req->analysers &= AN_REQ_FLT_END;
210
Christopher Faulete0768eb2018-10-03 16:38:02 +0200211 if (!(s->flags & SF_FINST_MASK))
212 s->flags |= SF_FINST_R;
213 return 0;
214 }
215
216 channel_dont_connect(req);
217 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
218 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100219
Christopher Faulet9768c262018-10-22 09:34:31 +0200220 if (sess->listener->options & LI_O_NOQUICKACK && htx_is_not_empty(htx) &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200221 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
222 /* We need more data, we have to re-enable quick-ack in case we
223 * previously disabled it, otherwise we might cause the client
224 * to delay next data.
225 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100226 conn_set_quickack(objt_conn(sess->origin), 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200227 }
Willy Tarreau1a18b542018-12-11 16:37:42 +0100228
Christopher Faulet47365272018-10-31 17:40:50 +0100229 if ((req->flags & CF_READ_PARTIAL) && (txn->flags & TX_WAIT_NEXT_RQ)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200230 /* If the client starts to talk, let's fall back to
231 * request timeout processing.
232 */
233 txn->flags &= ~TX_WAIT_NEXT_RQ;
234 req->analyse_exp = TICK_ETERNITY;
235 }
236
237 /* just set the request timeout once at the beginning of the request */
238 if (!tick_isset(req->analyse_exp)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100239 if ((txn->flags & TX_WAIT_NEXT_RQ) && tick_isset(s->be->timeout.httpka))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200240 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
241 else
242 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
243 }
244
245 /* we're not ready yet */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100246 DBG_TRACE_DEVEL("waiting for the request",
247 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200248 return 0;
249
250 failed_keep_alive:
251 /* Here we process low-level errors for keep-alive requests. In
252 * short, if the request is not the first one and it experiences
253 * a timeout, read error or shutdown, we just silently close so
254 * that the client can try again.
255 */
256 txn->status = 0;
257 msg->msg_state = HTTP_MSG_RQBEFORE;
258 req->analysers &= AN_REQ_FLT_END;
259 s->logs.logwait = 0;
260 s->logs.level = 0;
261 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200262 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100263 DBG_TRACE_DEVEL("leaving by closing K/A connection",
264 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200265 return 0;
266 }
267
Christopher Faulet9768c262018-10-22 09:34:31 +0200268 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200269 stream_inc_http_req_ctr(s);
270 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
271
Christopher Faulet9768c262018-10-22 09:34:31 +0200272 /* kill the pending keep-alive timeout */
273 txn->flags &= ~TX_WAIT_NEXT_RQ;
274 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200275
Christopher Faulet29f17582019-05-23 11:03:26 +0200276 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200277 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100278
Christopher Faulet9768c262018-10-22 09:34:31 +0200279 /* 0: we might have to print this header in debug mode */
280 if (unlikely((global.mode & MODE_DEBUG) &&
281 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
282 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200283
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200284 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200285
Christopher Fauleta3f15502019-05-13 15:27:23 +0200286 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200287 struct htx_blk *blk = htx_get_blk(htx, pos);
288 enum htx_blk_type type = htx_get_blk_type(blk);
289
290 if (type == HTX_BLK_EOH)
291 break;
292 if (type != HTX_BLK_HDR)
293 continue;
294
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200295 http_debug_hdr("clihdr", s,
296 htx_get_blk_name(htx, blk),
297 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200298 }
299 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200300
301 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100302 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200303 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100304 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100305 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200306 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100307 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +0100308 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100309 if (sl->flags & HTX_SL_F_BODYLESS)
310 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200311
312 /* we can make use of server redirect on GET and HEAD */
313 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
314 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100315 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200316 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200317 goto return_bad_req;
318 }
319
320 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100321 * 2: check if the URI matches the monitor_uri. We have to do this for
322 * every request which gets in, because the monitor-uri is defined by
323 * the frontend. If the monitor-uri starts with a '/', the matching is
324 * done against the request's path. Otherwise, the request's uri is
325 * used. It is a workaround to let HTTP/2 health-checks work as
326 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200327 */
328 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Faulet6072beb2020-02-18 15:34:58 +0100329 ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)),
330 ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) ||
331 isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200332 /*
333 * We have found the monitor URI
334 */
335 struct acl_cond *cond;
336
337 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100338 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200339
340 /* Check if we want to fail this monitor request or not */
341 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
342 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
343
344 ret = acl_pass(ret);
345 if (cond->pol == ACL_COND_UNLESS)
346 ret = !ret;
347
348 if (ret) {
349 /* we fail this request, let's return 503 service unavail */
350 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200351 if (!(s->flags & SF_ERR_MASK))
352 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
353 goto return_prx_cond;
354 }
355 }
356
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800357 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200358 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200359 if (!(s->flags & SF_ERR_MASK))
360 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
361 goto return_prx_cond;
362 }
363
364 /*
365 * 3: Maybe we have to copy the original REQURI for the logs ?
366 * Note: we cannot log anymore if the request has been
367 * classified as invalid.
368 */
369 if (unlikely(s->logs.logwait & LW_REQ)) {
370 /* we have a complete HTTP request that we must log */
371 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200372 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200373
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200374 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200375 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200376
377 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
378 s->do_log(s);
379 } else {
380 ha_alert("HTTP logging : out of memory.\n");
381 }
382 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200383
Christopher Faulete0768eb2018-10-03 16:38:02 +0200384 /* if the frontend has "option http-use-proxy-header", we'll check if
385 * we have what looks like a proxied connection instead of a connection,
386 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
387 * Note that this is *not* RFC-compliant, however browsers and proxies
388 * happen to do that despite being non-standard :-(
389 * We consider that a request not beginning with either '/' or '*' is
390 * a proxied connection, which covers both "scheme://location" and
391 * CONNECT ip:port.
392 */
393 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100394 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200395 txn->flags |= TX_USE_PX_CONN;
396
Christopher Faulete0768eb2018-10-03 16:38:02 +0200397 /* 5: we may need to capture headers */
398 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200399 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200400
Christopher Faulete0768eb2018-10-03 16:38:02 +0200401 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200402 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200403 req->analysers |= AN_REQ_HTTP_BODY;
404
405 /*
406 * RFC7234#4:
407 * A cache MUST write through requests with methods
408 * that are unsafe (Section 4.2.1 of [RFC7231]) to
409 * the origin server; i.e., a cache is not allowed
410 * to generate a reply to such a request before
411 * having forwarded the request and having received
412 * a corresponding response.
413 *
414 * RFC7231#4.2.1:
415 * Of the request methods defined by this
416 * specification, the GET, HEAD, OPTIONS, and TRACE
417 * methods are defined to be safe.
418 */
419 if (likely(txn->meth == HTTP_METH_GET ||
420 txn->meth == HTTP_METH_HEAD ||
421 txn->meth == HTTP_METH_OPTIONS ||
422 txn->meth == HTTP_METH_TRACE))
423 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
424
425 /* end of job, return OK */
426 req->analysers &= ~an_bit;
427 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200428
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100429 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200430 return 1;
431
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200432 return_int_err:
433 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200434 if (!(s->flags & SF_ERR_MASK))
435 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100436 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200437 if (sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100438 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200439 goto return_prx_cond;
440
Christopher Faulete0768eb2018-10-03 16:38:02 +0200441 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200442 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100443 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200444 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100445 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200446 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200447
448 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200449 http_reply_and_close(s, txn->status, http_error_message(s));
450
Christopher Faulete0768eb2018-10-03 16:38:02 +0200451 if (!(s->flags & SF_ERR_MASK))
452 s->flags |= SF_ERR_PRXCOND;
453 if (!(s->flags & SF_FINST_MASK))
454 s->flags |= SF_FINST_R;
455
456 req->analysers &= AN_REQ_FLT_END;
457 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100458 DBG_TRACE_DEVEL("leaving on error",
459 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200460 return 0;
461}
462
463
464/* This stream analyser runs all HTTP request processing which is common to
465 * frontends and backends, which means blocking ACLs, filters, connection-close,
466 * reqadd, stats and redirects. This is performed for the designated proxy.
467 * It returns 1 if the processing can continue on next analysers, or zero if it
468 * either needs more data or wants to immediately abort the request (eg: deny,
469 * error, ...).
470 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200471int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200472{
473 struct session *sess = s->sess;
474 struct http_txn *txn = s->txn;
475 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200476 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200477 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200478 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200479 struct connection *conn = objt_conn(sess->origin);
480
481 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
482 /* we need more data */
483 goto return_prx_yield;
484 }
485
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100486 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200487
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100488 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200489
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200490 /* just in case we have some per-backend tracking. Only called the first
491 * execution of the analyser. */
492 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
493 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200494
495 /* evaluate http-request rules */
496 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100497 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200498
499 switch (verdict) {
500 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
501 goto return_prx_yield;
502
503 case HTTP_RULE_RES_CONT:
504 case HTTP_RULE_RES_STOP: /* nothing to do */
505 break;
506
507 case HTTP_RULE_RES_DENY: /* deny or tarpit */
508 if (txn->flags & TX_CLTARPIT)
509 goto tarpit;
510 goto deny;
511
512 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
513 goto return_prx_cond;
514
515 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
516 goto done;
517
518 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
519 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100520
521 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
522 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200523 }
524 }
525
526 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100527 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200528 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200529
Christopher Fauletff2759f2018-10-24 11:13:16 +0200530 ctx.blk = NULL;
531 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
532 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100533 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200534 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200535 }
536
537 /* OK at this stage, we know that the request was accepted according to
538 * the http-request rules, we can check for the stats. Note that the
539 * URI is detected *before* the req* rules in order not to be affected
540 * by a possible reqrep, while they are processed *after* so that a
541 * reqdeny can still block them. This clearly needs to change in 1.6!
542 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200543 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200544 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100545 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200546 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200547 if (!(s->flags & SF_ERR_MASK))
548 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100549 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200550 }
551
552 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200553 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100554 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200555 /* not all actions implemented: deny, allow, auth */
556
557 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
558 goto deny;
559
560 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
561 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100562
563 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
564 goto return_bad_req;
565
566 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
567 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200568 }
569
Christopher Faulet2571bc62019-03-01 11:44:26 +0100570 /* Proceed with the applets now. */
571 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200572 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100573 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200574
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200575 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100576 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100577
Christopher Faulete0768eb2018-10-03 16:38:02 +0200578 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
579 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
580 if (!(s->flags & SF_FINST_MASK))
581 s->flags |= SF_FINST_R;
582
583 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
584 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
585 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
586 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100587
588 req->flags |= CF_SEND_DONTWAIT;
589 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200590 goto done;
591 }
592
593 /* check whether we have some ACLs set to redirect this request */
594 list_for_each_entry(rule, &px->redirect_rules, list) {
595 if (rule->cond) {
596 int ret;
597
598 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
599 ret = acl_pass(ret);
600 if (rule->cond->pol == ACL_COND_UNLESS)
601 ret = !ret;
602 if (!ret)
603 continue;
604 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200605 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100606 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200607 goto done;
608 }
609
610 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
611 * If this happens, then the data will not come immediately, so we must
612 * send all what we have without waiting. Note that due to the small gain
613 * in waiting for the body of the request, it's easier to simply put the
614 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
615 * itself once used.
616 */
617 req->flags |= CF_SEND_DONTWAIT;
618
619 done: /* done with this analyser, continue with next ones that the calling
620 * points will have set, if any.
621 */
622 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500623 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200624 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100625 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200626 return 1;
627
628 tarpit:
629 /* Allow cookie logging
630 */
631 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200632 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200633
634 /* When a connection is tarpitted, we use the tarpit timeout,
635 * which may be the same as the connect timeout if unspecified.
636 * If unset, then set it to zero because we really want it to
637 * eventually expire. We build the tarpit as an analyser.
638 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100639 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200640
641 /* wipe the request out so that we can drop the connection early
642 * if the client closes first.
643 */
644 channel_dont_connect(req);
645
Christopher Faulete0768eb2018-10-03 16:38:02 +0200646 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
647 req->analysers |= AN_REQ_HTTP_TARPIT;
648 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
649 if (!req->analyse_exp)
650 req->analyse_exp = tick_add(now_ms, 0);
651 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100652 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100653 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100654 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200655 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100656 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200657 goto done_without_exp;
658
659 deny: /* this request was blocked (denied) */
660
661 /* Allow cookie logging
662 */
663 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200664 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200665
Christopher Faulete0768eb2018-10-03 16:38:02 +0200666 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200667 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100668 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100669 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100670 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200671 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100672 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100673 goto return_prx_err;
674
675 return_int_err:
676 txn->status = 500;
677 if (!(s->flags & SF_ERR_MASK))
678 s->flags |= SF_ERR_INTERNAL;
679 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100680 if (s->flags & SF_BE_ASSIGNED)
681 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100682 if (sess->listener->counters)
683 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
684 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200685
686 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200687 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100688 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200689 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100690 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100691 /* fall through */
692
693 return_prx_err:
694 http_reply_and_close(s, txn->status, http_error_message(s));
695 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200696
697 return_prx_cond:
698 if (!(s->flags & SF_ERR_MASK))
699 s->flags |= SF_ERR_PRXCOND;
700 if (!(s->flags & SF_FINST_MASK))
701 s->flags |= SF_FINST_R;
702
703 req->analysers &= AN_REQ_FLT_END;
704 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100705 DBG_TRACE_DEVEL("leaving on error",
706 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200707 return 0;
708
709 return_prx_yield:
710 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100711 DBG_TRACE_DEVEL("waiting for more data",
712 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200713 return 0;
714}
715
716/* This function performs all the processing enabled for the current request.
717 * It returns 1 if the processing can continue on next analysers, or zero if it
718 * needs more data, encounters an error, or wants to immediately abort the
719 * request. It relies on buffers flags, and updates s->req.analysers.
720 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200721int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200722{
723 struct session *sess = s->sess;
724 struct http_txn *txn = s->txn;
725 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200726 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200727 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
728
729 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
730 /* we need more data */
731 channel_dont_connect(req);
732 return 0;
733 }
734
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100735 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200736
737 /*
738 * Right now, we know that we have processed the entire headers
739 * and that unwanted requests have been filtered out. We can do
740 * whatever we want with the remaining request. Also, now we
741 * may have separate values for ->fe, ->be.
742 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100743 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200744
745 /*
746 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200747 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200748 */
749 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100750 struct htx_sl *sl;
751 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200752
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200753 if (!sockaddr_alloc(&s->target_addr)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200754 if (!(s->flags & SF_ERR_MASK))
755 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100756 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200757 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200758 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100759 uri = htx_sl_req_uri(sl);
760 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200761
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200762 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200763 goto return_bad_req;
764
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200765 s->target = &s->be->obj_type;
766 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
767
Christopher Faulete0768eb2018-10-03 16:38:02 +0200768 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200769 * uri.ptr and path.ptr (excluded). If it was not found, we need
770 * to replace from all the uri by a single "/".
771 *
772 * Instead of rewritting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100773 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200774 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200775 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100776 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200777 }
778
779 /*
780 * 7: Now we can work with the cookies.
781 * Note that doing so might move headers in the request, but
782 * the fields will stay coherent and the URI will not move.
783 * This should only be performed in the backend.
784 */
785 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200786 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200787
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100788 /* 8: Generate unique ID if a "unique-id-format" is defined.
789 *
790 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
791 * fetches only available in the HTTP request processing stage.
792 */
793 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100794 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200795
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100796 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100797 if (!(s->flags & SF_ERR_MASK))
798 s->flags |= SF_ERR_RESOURCE;
799 goto return_int_err;
800 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200801
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100802 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100803 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100804 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100805 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200806 }
807
808 /*
809 * 9: add X-Forwarded-For if either the frontend or the backend
810 * asks for it.
811 */
812 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200813 struct http_hdr_ctx ctx = { .blk = NULL };
814 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
815 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
816
Christopher Faulete0768eb2018-10-03 16:38:02 +0200817 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200818 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200819 /* The header is set to be added only if none is present
820 * and we found it, so don't do anything.
821 */
822 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200823 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200824 /* Add an X-Forwarded-For header unless the source IP is
825 * in the 'except' network range.
826 */
827 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200828 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200829 != sess->fe->except_net.s_addr) &&
830 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200831 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200832 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200833 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200834
835 /* Note: we rely on the backend to get the header name to be used for
836 * x-forwarded-for, because the header is really meant for the backends.
837 * However, if the backend did not specify any option, we have to rely
838 * on the frontend's header name.
839 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200840 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
841 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100842 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200843 }
844 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200845 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200846 /* FIXME: for the sake of completeness, we should also support
847 * 'except' here, although it is mostly useless in this case.
848 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200849 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200850
Christopher Faulete0768eb2018-10-03 16:38:02 +0200851 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200852 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200853 pn, sizeof(pn));
854
855 /* Note: we rely on the backend to get the header name to be used for
856 * x-forwarded-for, because the header is really meant for the backends.
857 * However, if the backend did not specify any option, we have to rely
858 * on the frontend's header name.
859 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200860 chunk_printf(&trash, "%s", pn);
861 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100862 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200863 }
864 }
865
866 /*
867 * 10: add X-Original-To if either the frontend or the backend
868 * asks for it.
869 */
870 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
871
872 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200873 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 +0200874 /* Add an X-Original-To header unless the destination IP is
875 * in the 'except' network range.
876 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200877 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200878 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200879 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200880 != sess->fe->except_to.s_addr) &&
881 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200882 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200883 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200884 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200885 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200886
887 /* Note: we rely on the backend to get the header name to be used for
888 * x-original-to, because the header is really meant for the backends.
889 * However, if the backend did not specify any option, we have to rely
890 * on the frontend's header name.
891 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200892 if (s->be->orgto_hdr_len)
893 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
894 else
895 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200896
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200897 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
898 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100899 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200900 }
901 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200902 }
903
Christopher Faulete0768eb2018-10-03 16:38:02 +0200904 /* If we have no server assigned yet and we're balancing on url_param
905 * with a POST request, we may be interested in checking the body for
906 * that parameter. This will be done in another analyser.
907 */
908 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100909 s->txn->meth == HTTP_METH_POST &&
910 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200911 channel_dont_connect(req);
912 req->analysers |= AN_REQ_HTTP_BODY;
913 }
914
915 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
916 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100917
Christopher Faulete0768eb2018-10-03 16:38:02 +0200918 /* We expect some data from the client. Unless we know for sure
919 * we already have a full request, we have to re-enable quick-ack
920 * in case we previously disabled it, otherwise we might cause
921 * the client to delay further data.
922 */
923 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200924 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100925 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200926
927 /*************************************************************
928 * OK, that's finished for the headers. We have done what we *
929 * could. Let's switch to the DATA state. *
930 ************************************************************/
931 req->analyse_exp = TICK_ETERNITY;
932 req->analysers &= ~an_bit;
933
934 s->logs.tv_request = now;
935 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100936 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200937 return 1;
938
Christopher Fauletb8a53712019-12-16 11:29:38 +0100939 return_int_err:
940 txn->status = 500;
941 if (!(s->flags & SF_ERR_MASK))
942 s->flags |= SF_ERR_INTERNAL;
943 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100944 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100945 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100946 if (sess->listener->counters)
947 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
948 goto return_prx_cond;
949
Christopher Faulete0768eb2018-10-03 16:38:02 +0200950 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200951 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100952 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200953 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100954 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100955 /* fall through */
956
957 return_prx_cond:
958 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200959
960 if (!(s->flags & SF_ERR_MASK))
961 s->flags |= SF_ERR_PRXCOND;
962 if (!(s->flags & SF_FINST_MASK))
963 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100964
965 req->analysers &= AN_REQ_FLT_END;
966 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100967 DBG_TRACE_DEVEL("leaving on error",
968 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200969 return 0;
970}
971
972/* This function is an analyser which processes the HTTP tarpit. It always
973 * returns zero, at the beginning because it prevents any other processing
974 * from occurring, and at the end because it terminates the request.
975 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200976int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200977{
978 struct http_txn *txn = s->txn;
979
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100980 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200981 /* This connection is being tarpitted. The CLIENT side has
982 * already set the connect expiration date to the right
983 * timeout. We just have to check that the client is still
984 * there and that the timeout has not expired.
985 */
986 channel_dont_connect(req);
987 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100988 !tick_is_expired(req->analyse_exp, now_ms)) {
989 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
990 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200991 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100992 }
993
Christopher Faulete0768eb2018-10-03 16:38:02 +0200994
995 /* We will set the queue timer to the time spent, just for
996 * logging purposes. We fake a 500 server error, so that the
997 * attacker will not suspect his connection has been tarpitted.
998 * It will not cause trouble to the logs because we can exclude
999 * the tarpitted connections by filtering on the 'PT' status flags.
1000 */
1001 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1002
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02001003 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001004
1005 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001006 req->analysers &= AN_REQ_FLT_END;
1007 req->analyse_exp = TICK_ETERNITY;
1008
1009 if (!(s->flags & SF_ERR_MASK))
1010 s->flags |= SF_ERR_PRXCOND;
1011 if (!(s->flags & SF_FINST_MASK))
1012 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001013
1014 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001015 return 0;
1016}
1017
1018/* This function is an analyser which waits for the HTTP request body. It waits
1019 * for either the buffer to be full, or the full advertised contents to have
1020 * reached the buffer. It must only be called after the standard HTTP request
1021 * processing has occurred, because it expects the request to be parsed and will
1022 * look for the Expect header. It may send a 100-Continue interim response. It
1023 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
1024 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
1025 * needs to read more data, or 1 once it has completed its analysis.
1026 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001027int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001028{
1029 struct session *sess = s->sess;
1030 struct http_txn *txn = s->txn;
1031 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001032 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001033
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001034 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001035
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001036 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001037
Willy Tarreau4236f032019-03-05 10:43:32 +01001038 if (htx->flags & HTX_FL_PARSING_ERROR)
1039 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001040 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1041 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001042
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001043 if (msg->msg_state < HTTP_MSG_BODY)
1044 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001045
Christopher Faulete0768eb2018-10-03 16:38:02 +02001046 /* We have to parse the HTTP request body to find any required data.
1047 * "balance url_param check_post" should have been the only way to get
1048 * into this. We were brought here after HTTP header analysis, so all
1049 * related structures are ready.
1050 */
1051
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001052 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001053 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001054 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001055 }
1056
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001057 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001058
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001059 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1060 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001061 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001062 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001063 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001064 goto http_end;
1065
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001066 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001067 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1068 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001069 if (!(s->flags & SF_ERR_MASK))
1070 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001071 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1072 if (sess->listener->counters)
1073 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1074 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001075 }
1076
1077 /* we get here if we need to wait for more data */
1078 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1079 /* Not enough data. We'll re-use the http-request
1080 * timeout here. Ideally, we should set the timeout
1081 * relative to the accept() date. We just set the
1082 * request timeout once at the beginning of the
1083 * request.
1084 */
1085 channel_dont_connect(req);
1086 if (!tick_isset(req->analyse_exp))
1087 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001088 DBG_TRACE_DEVEL("waiting for more data",
1089 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001090 return 0;
1091 }
1092
1093 http_end:
1094 /* The situation will not evolve, so let's give up on the analysis. */
1095 s->logs.tv_request = now; /* update the request timer to reflect full request */
1096 req->analysers &= ~an_bit;
1097 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001098 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001099 return 1;
1100
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001101 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001102 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001103 if (!(s->flags & SF_ERR_MASK))
1104 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001105 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001106 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +01001107 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001108 if (sess->listener->counters)
1109 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
1110 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001111
Christopher Faulete0768eb2018-10-03 16:38:02 +02001112 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001113 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001114 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1115 if (sess->listener->counters)
1116 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1117 /* fall through */
1118
1119 return_prx_cond:
1120 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001121
1122 if (!(s->flags & SF_ERR_MASK))
1123 s->flags |= SF_ERR_PRXCOND;
1124 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001125 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001126
Christopher Faulete0768eb2018-10-03 16:38:02 +02001127 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001128 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001129 DBG_TRACE_DEVEL("leaving on error",
1130 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001131 return 0;
1132}
1133
1134/* This function is an analyser which forwards request body (including chunk
1135 * sizes if any). It is called as soon as we must forward, even if we forward
1136 * zero byte. The only situation where it must not be called is when we're in
1137 * tunnel mode and we want to forward till the close. It's used both to forward
1138 * remaining data and to resync after end of body. It expects the msg_state to
1139 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1140 * read more data, or 1 once we can go on with next request or end the stream.
1141 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1142 * bytes of pending data + the headers if not already done.
1143 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001144int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001145{
1146 struct session *sess = s->sess;
1147 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001148 struct http_msg *msg = &txn->req;
1149 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001150 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001151 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001152
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001153 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001154
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001155 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001156
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001157 if (htx->flags & HTX_FL_PARSING_ERROR)
1158 goto return_bad_req;
1159 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1160 goto return_int_err;
1161
Christopher Faulete0768eb2018-10-03 16:38:02 +02001162 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1163 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1164 /* Output closed while we were sending data. We must abort and
1165 * wake the other side up.
1166 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001167
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001168 /* Don't abort yet if we had L7 retries activated and it
1169 * was a write error, we may recover.
1170 */
1171 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001172 (s->si[1].flags & SI_FL_L7_RETRY)) {
1173 DBG_TRACE_DEVEL("leaving on L7 retry",
1174 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001175 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001176 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001177 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001178 http_end_request(s);
1179 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001180 DBG_TRACE_DEVEL("leaving on error",
1181 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001182 return 1;
1183 }
1184
1185 /* Note that we don't have to send 100-continue back because we don't
1186 * need the data to complete our job, and it's up to the server to
1187 * decide whether to return 100, 417 or anything else in return of
1188 * an "Expect: 100-continue" header.
1189 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001190 if (msg->msg_state == HTTP_MSG_BODY)
1191 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001192
Christopher Faulete0768eb2018-10-03 16:38:02 +02001193 /* in most states, we should abort in case of early close */
1194 channel_auto_close(req);
1195
1196 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001197 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001198 if (req->flags & CF_EOI)
1199 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001200 }
1201 else {
1202 /* We can't process the buffer's contents yet */
1203 req->flags |= CF_WAKE_WRITE;
1204 goto missing_data_or_waiting;
1205 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001206 }
1207
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001208 if (msg->msg_state >= HTTP_MSG_ENDING)
1209 goto ending;
1210
1211 if (txn->meth == HTTP_METH_CONNECT) {
1212 msg->msg_state = HTTP_MSG_ENDING;
1213 goto ending;
1214 }
1215
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001216 /* Forward input data. We get it by removing all outgoing data not
1217 * forwarded yet from HTX data size. If there are some data filters, we
1218 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001219 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001220 if (HAS_REQ_DATA_FILTERS(s)) {
1221 ret = flt_http_payload(s, msg, htx->data);
1222 if (ret < 0)
1223 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001224 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001225 }
1226 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001227 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001228 if (msg->flags & HTTP_MSGF_XFER_LEN)
1229 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001230 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001231
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001232 if (htx->data != co_data(req))
1233 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001234
Christopher Faulet9768c262018-10-22 09:34:31 +02001235 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001236 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1237 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001238 */
1239 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1240 goto missing_data_or_waiting;
1241
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001242 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001243
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001244 ending:
1245 /* other states, ENDING...TUNNEL */
1246 if (msg->msg_state >= HTTP_MSG_DONE)
1247 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001248
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001249 if (HAS_REQ_DATA_FILTERS(s)) {
1250 ret = flt_http_end(s, msg);
1251 if (ret <= 0) {
1252 if (!ret)
1253 goto missing_data_or_waiting;
1254 goto return_bad_req;
1255 }
1256 }
1257
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001258 if (txn->meth == HTTP_METH_CONNECT)
1259 msg->msg_state = HTTP_MSG_TUNNEL;
1260 else {
1261 msg->msg_state = HTTP_MSG_DONE;
1262 req->to_forward = 0;
1263 }
1264
1265 done:
1266 /* we don't want to forward closes on DONE except in tunnel mode. */
1267 if (!(txn->flags & TX_CON_WANT_TUN))
1268 channel_dont_close(req);
1269
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001270 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001271 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001272 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001273 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1274 if (req->flags & CF_SHUTW) {
1275 /* request errors are most likely due to the
1276 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001277 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001278 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001279 goto return_bad_req;
1280 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001281 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001282 return 1;
1283 }
1284
1285 /* If "option abortonclose" is set on the backend, we want to monitor
1286 * the client's connection and forward any shutdown notification to the
1287 * server, which will decide whether to close or to go on processing the
1288 * request. We only do that in tunnel mode, and not in other modes since
1289 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001290 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001291 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001292 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001293 s->si[1].flags |= SI_FL_NOLINGER;
1294 channel_auto_close(req);
1295 }
1296 else if (s->txn->meth == HTTP_METH_POST) {
1297 /* POST requests may require to read extra CRLF sent by broken
1298 * browsers and which could cause an RST to be sent upon close
1299 * on some systems (eg: Linux). */
1300 channel_auto_read(req);
1301 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001302 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1303 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001304 return 0;
1305
1306 missing_data_or_waiting:
1307 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001308 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001309 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001310
1311 waiting:
1312 /* waiting for the last bits to leave the buffer */
1313 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001314 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001315
1316 /* When TE: chunked is used, we need to get there again to parse remaining
1317 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1318 * And when content-length is used, we never want to let the possible
1319 * shutdown be forwarded to the other side, as the state machine will
1320 * take care of it once the client responds. It's also important to
1321 * prevent TIME_WAITs from accumulating on the backend side, and for
1322 * HTTP/2 where the last frame comes with a shutdown.
1323 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001324 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001325 channel_dont_close(req);
1326
1327 /* We know that more data are expected, but we couldn't send more that
1328 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1329 * system knows it must not set a PUSH on this first part. Interactive
1330 * modes are already handled by the stream sock layer. We must not do
1331 * this in content-length mode because it could present the MSG_MORE
1332 * flag with the last block of forwarded data, which would cause an
1333 * additional delay to be observed by the receiver.
1334 */
1335 if (msg->flags & HTTP_MSGF_TE_CHNK)
1336 req->flags |= CF_EXPECT_MORE;
1337
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001338 DBG_TRACE_DEVEL("waiting for more data to forward",
1339 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001340 return 0;
1341
Christopher Faulet93e02d82019-03-08 14:18:50 +01001342 return_cli_abort:
1343 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1344 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001345 if (sess->listener->counters)
1346 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001347 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001348 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001349 if (!(s->flags & SF_ERR_MASK))
1350 s->flags |= SF_ERR_CLICL;
1351 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001352 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001353
1354 return_srv_abort:
1355 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1356 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001357 if (sess->listener->counters)
1358 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001359 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001360 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001361 if (!(s->flags & SF_ERR_MASK))
1362 s->flags |= SF_ERR_SRVCL;
1363 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001364 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001365
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001366 return_int_err:
1367 if (!(s->flags & SF_ERR_MASK))
1368 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001369 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001370 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001371 if (sess->listener->counters)
1372 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001373 if (objt_server(s->target))
1374 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001375 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001376 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001377
Christopher Faulet93e02d82019-03-08 14:18:50 +01001378 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001379 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001380 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001381 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001382 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001383 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001384
Christopher Fauletb8a53712019-12-16 11:29:38 +01001385 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001386 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001387 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001388 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001389 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001390 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001391 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001392 }
1393 req->analysers &= AN_REQ_FLT_END;
1394 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 +01001395 if (!(s->flags & SF_ERR_MASK))
1396 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001397 if (!(s->flags & SF_FINST_MASK))
1398 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001399 DBG_TRACE_DEVEL("leaving on error ",
1400 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001401 return 0;
1402}
1403
Olivier Houcharda254a372019-04-05 15:30:12 +02001404/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1405/* Returns 0 if we can attempt to retry, -1 otherwise */
1406static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1407{
1408 struct channel *req, *res;
1409 int co_data;
1410
1411 si->conn_retries--;
1412 if (si->conn_retries < 0)
1413 return -1;
1414
Willy Tarreau223995e2019-05-04 10:38:31 +02001415 if (objt_server(s->target))
1416 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1417 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1418
Olivier Houcharda254a372019-04-05 15:30:12 +02001419 req = &s->req;
1420 res = &s->res;
1421 /* Remove any write error from the request, and read error from the response */
1422 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1423 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1424 res->analysers = 0;
1425 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001426 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001427 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001428 si->exp = TICK_ETERNITY;
1429 res->rex = TICK_ETERNITY;
1430 res->to_forward = 0;
1431 res->analyse_exp = TICK_ETERNITY;
1432 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001433 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001434 si_release_endpoint(&s->si[1]);
1435 b_free(&req->buf);
1436 /* Swap the L7 buffer with the channel buffer */
1437 /* We know we stored the co_data as b_data, so get it there */
1438 co_data = b_data(&si->l7_buffer);
1439 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1440 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1441
1442 co_set_data(req, co_data);
1443 b_reset(&res->buf);
1444 co_set_data(res, 0);
1445 return 0;
1446}
1447
Christopher Faulete0768eb2018-10-03 16:38:02 +02001448/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1449 * processing can continue on next analysers, or zero if it either needs more
1450 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1451 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1452 * when it has nothing left to do, and may remove any analyser when it wants to
1453 * abort.
1454 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001455int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001456{
Christopher Faulet9768c262018-10-22 09:34:31 +02001457 /*
1458 * We will analyze a complete HTTP response to check the its syntax.
1459 *
1460 * Once the start line and all headers are received, we may perform a
1461 * capture of the error (if any), and we will set a few fields. We also
1462 * logging and finally headers capture.
1463 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001464 struct session *sess = s->sess;
1465 struct http_txn *txn = s->txn;
1466 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001467 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001468 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001469 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001470 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001471 int n;
1472
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001473 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001474
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001475 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001476
Willy Tarreau4236f032019-03-05 10:43:32 +01001477 /* Parsing errors are caught here */
1478 if (htx->flags & HTX_FL_PARSING_ERROR)
1479 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001480 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1481 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001482
Christopher Faulete0768eb2018-10-03 16:38:02 +02001483 /*
1484 * Now we quickly check if we have found a full valid response.
1485 * If not so, we check the FD and buffer states before leaving.
1486 * A full response is indicated by the fact that we have seen
1487 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1488 * responses are checked first.
1489 *
1490 * Depending on whether the client is still there or not, we
1491 * may send an error response back or not. Note that normally
1492 * we should only check for HTTP status there, and check I/O
1493 * errors somewhere else.
1494 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001495 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001496 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001497 /* 1: have we encountered a read error ? */
1498 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001499 struct connection *conn = NULL;
1500
Olivier Houchard865d8392019-05-03 22:46:27 +02001501 if (objt_cs(s->si[1].end))
1502 conn = objt_cs(s->si[1].end)->conn;
1503
1504 if (si_b->flags & SI_FL_L7_RETRY &&
1505 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001506 /* If we arrive here, then CF_READ_ERROR was
1507 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001508 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001509 * the SI_FL_L7_RETRY flag, so it's ok not
1510 * to check s->be->retry_type.
1511 */
1512 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1513 return 0;
1514 }
1515
Olivier Houchard6db16992019-05-17 15:40:49 +02001516 if (txn->flags & TX_NOT_FIRST)
1517 goto abort_keep_alive;
1518
Olivier Houcharda798bf52019-03-08 18:52:00 +01001519 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001520 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001521 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001522 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001523 }
1524
Christopher Faulete0768eb2018-10-03 16:38:02 +02001525 rep->analysers &= AN_RES_FLT_END;
1526 txn->status = 502;
1527
1528 /* Check to see if the server refused the early data.
1529 * If so, just send a 425
1530 */
Olivier Houchard865d8392019-05-03 22:46:27 +02001531 if (conn->err_code == CO_ER_SSL_EARLY_FAILED) {
1532 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001533 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001534 do_l7_retry(s, si_b) == 0) {
1535 DBG_TRACE_DEVEL("leaving on L7 retry",
1536 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001537 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001538 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001539 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001540 }
1541
1542 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001543 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001544
1545 if (!(s->flags & SF_ERR_MASK))
1546 s->flags |= SF_ERR_SRVCL;
1547 if (!(s->flags & SF_FINST_MASK))
1548 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001549 DBG_TRACE_DEVEL("leaving on error",
1550 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001551 return 0;
1552 }
1553
Christopher Faulet9768c262018-10-22 09:34:31 +02001554 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001555 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001556 if ((si_b->flags & SI_FL_L7_RETRY) &&
1557 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001558 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1559 DBG_TRACE_DEVEL("leaving on L7 retry",
1560 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001561 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001562 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001563 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001564 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001565 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001566 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001567 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001568 }
1569
Christopher Faulete0768eb2018-10-03 16:38:02 +02001570 rep->analysers &= AN_RES_FLT_END;
1571 txn->status = 504;
1572 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001573 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001574
1575 if (!(s->flags & SF_ERR_MASK))
1576 s->flags |= SF_ERR_SRVTO;
1577 if (!(s->flags & SF_FINST_MASK))
1578 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001579 DBG_TRACE_DEVEL("leaving on error",
1580 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001581 return 0;
1582 }
1583
Christopher Faulet9768c262018-10-22 09:34:31 +02001584 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001585 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001586 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1587 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001588 if (sess->listener->counters)
1589 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001590 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001591 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001592
1593 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001594 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001595 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001596
1597 if (!(s->flags & SF_ERR_MASK))
1598 s->flags |= SF_ERR_CLICL;
1599 if (!(s->flags & SF_FINST_MASK))
1600 s->flags |= SF_FINST_H;
1601
1602 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001603 DBG_TRACE_DEVEL("leaving on error",
1604 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001605 return 0;
1606 }
1607
Christopher Faulet9768c262018-10-22 09:34:31 +02001608 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001609 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001610 if ((si_b->flags & SI_FL_L7_RETRY) &&
1611 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001612 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1613 DBG_TRACE_DEVEL("leaving on L7 retry",
1614 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001615 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001616 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001617 }
1618
Olivier Houchard6db16992019-05-17 15:40:49 +02001619 if (txn->flags & TX_NOT_FIRST)
1620 goto abort_keep_alive;
1621
Olivier Houcharda798bf52019-03-08 18:52:00 +01001622 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001623 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001624 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001625 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001626 }
1627
Christopher Faulete0768eb2018-10-03 16:38:02 +02001628 rep->analysers &= AN_RES_FLT_END;
1629 txn->status = 502;
1630 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001631 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001632
1633 if (!(s->flags & SF_ERR_MASK))
1634 s->flags |= SF_ERR_SRVCL;
1635 if (!(s->flags & SF_FINST_MASK))
1636 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001637 DBG_TRACE_DEVEL("leaving on error",
1638 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001639 return 0;
1640 }
1641
Christopher Faulet9768c262018-10-22 09:34:31 +02001642 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001643 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001644 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001645 goto abort_keep_alive;
1646
Olivier Houcharda798bf52019-03-08 18:52:00 +01001647 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001648 if (objt_server(s->target))
1649 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001650 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001651
1652 if (!(s->flags & SF_ERR_MASK))
1653 s->flags |= SF_ERR_CLICL;
1654 if (!(s->flags & SF_FINST_MASK))
1655 s->flags |= SF_FINST_H;
1656
1657 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001658 DBG_TRACE_DEVEL("leaving on error",
1659 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001660 return 0;
1661 }
1662
1663 channel_dont_close(rep);
1664 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001665 DBG_TRACE_DEVEL("waiting for more data",
1666 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001667 return 0;
1668 }
1669
1670 /* More interesting part now : we know that we have a complete
1671 * response which at least looks like HTTP. We have an indicator
1672 * of each header's length, so we can parse them quickly.
1673 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001674 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001675 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001676 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001677
Christopher Faulet9768c262018-10-22 09:34:31 +02001678 /* 0: we might have to print this header in debug mode */
1679 if (unlikely((global.mode & MODE_DEBUG) &&
1680 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1681 int32_t pos;
1682
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001683 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001684
Christopher Fauleta3f15502019-05-13 15:27:23 +02001685 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001686 struct htx_blk *blk = htx_get_blk(htx, pos);
1687 enum htx_blk_type type = htx_get_blk_type(blk);
1688
1689 if (type == HTX_BLK_EOH)
1690 break;
1691 if (type != HTX_BLK_HDR)
1692 continue;
1693
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001694 http_debug_hdr("srvhdr", s,
1695 htx_get_blk_name(htx, blk),
1696 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001697 }
1698 }
1699
Christopher Faulet03599112018-11-27 11:21:21 +01001700 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001701 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001702 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001703 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001704 if (sl->flags & HTX_SL_F_XFER_LEN) {
1705 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001706 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001707 if (sl->flags & HTX_SL_F_BODYLESS)
1708 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001709 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001710
1711 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001712 if (n < 1 || n > 5)
1713 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001714
Christopher Faulete0768eb2018-10-03 16:38:02 +02001715 /* when the client triggers a 4xx from the server, it's most often due
1716 * to a missing object or permission. These events should be tracked
1717 * because if they happen often, it may indicate a brute force or a
1718 * vulnerability scan.
1719 */
1720 if (n == 4)
1721 stream_inc_http_err_ctr(s);
1722
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001723 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001724 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001725 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1726 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001727
Christopher Faulete0768eb2018-10-03 16:38:02 +02001728 /* Adjust server's health based on status code. Note: status codes 501
1729 * and 505 are triggered on demand by client request, so we must not
1730 * count them as server failures.
1731 */
1732 if (objt_server(s->target)) {
1733 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001734 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001735 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001736 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001737 }
1738
1739 /*
1740 * We may be facing a 100-continue response, or any other informational
1741 * 1xx response which is non-final, in which case this is not the right
1742 * response, and we're waiting for the next one. Let's allow this response
1743 * to go to the client and wait for the next one. There's an exception for
1744 * 101 which is used later in the code to switch protocols.
1745 */
1746 if (txn->status < 200 &&
1747 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001748 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001749 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001750 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001751 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001752 txn->status = 0;
1753 s->logs.t_data = -1; /* was not a response yet */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001754 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001755 }
1756
1757 /*
1758 * 2: check for cacheability.
1759 */
1760
1761 switch (txn->status) {
1762 case 200:
1763 case 203:
1764 case 204:
1765 case 206:
1766 case 300:
1767 case 301:
1768 case 404:
1769 case 405:
1770 case 410:
1771 case 414:
1772 case 501:
1773 break;
1774 default:
1775 /* RFC7231#6.1:
1776 * Responses with status codes that are defined as
1777 * cacheable by default (e.g., 200, 203, 204, 206,
1778 * 300, 301, 404, 405, 410, 414, and 501 in this
1779 * specification) can be reused by a cache with
1780 * heuristic expiration unless otherwise indicated
1781 * by the method definition or explicit cache
1782 * controls [RFC7234]; all other status codes are
1783 * not cacheable by default.
1784 */
1785 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1786 break;
1787 }
1788
1789 /*
1790 * 3: we may need to capture headers
1791 */
1792 s->logs.logwait &= ~LW_RESP;
1793 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001794 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001795
Christopher Faulet9768c262018-10-22 09:34:31 +02001796 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001797 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1798 txn->status == 101)) {
1799 /* Either we've established an explicit tunnel, or we're
1800 * switching the protocol. In both cases, we're very unlikely
1801 * to understand the next protocols. We have to switch to tunnel
1802 * mode, so that we transfer the request and responses then let
1803 * this protocol pass unmodified. When we later implement specific
1804 * parsers for such protocols, we'll want to check the Upgrade
1805 * header which contains information about that protocol for
1806 * responses with status 101 (eg: see RFC2817 about TLS).
1807 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001808 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001809 }
1810
Christopher Faulet61608322018-11-23 16:23:45 +01001811 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1812 * 407 (Proxy-Authenticate) responses and set the connection to private
1813 */
1814 srv_conn = cs_conn(objt_cs(s->si[1].end));
1815 if (srv_conn) {
1816 struct ist hdr;
1817 struct http_hdr_ctx ctx;
1818
1819 if (txn->status == 401)
1820 hdr = ist("WWW-Authenticate");
1821 else if (txn->status == 407)
1822 hdr = ist("Proxy-Authenticate");
1823 else
1824 goto end;
1825
1826 ctx.blk = NULL;
1827 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001828 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1829 * possibly followed by blanks and a base64 string, the connection
1830 * is private. Since it's a mess to deal with, we only check for
1831 * values starting with "NTLM" or "Nego". Note that often multiple
1832 * headers are sent by the server there.
1833 */
1834 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001835 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001836 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet61608322018-11-23 16:23:45 +01001837 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001838 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001839 }
Christopher Faulet61608322018-11-23 16:23:45 +01001840 }
1841 }
1842
1843 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001844 /* we want to have the response time before we start processing it */
1845 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1846
1847 /* end of job, return OK */
1848 rep->analysers &= ~an_bit;
1849 rep->analyse_exp = TICK_ETERNITY;
1850 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001851 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001852 return 1;
1853
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001854 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001855 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001856 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001857 if (sess->listener->counters)
1858 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001859 if (objt_server(s->target))
1860 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001861 txn->status = 500;
1862 if (!(s->flags & SF_ERR_MASK))
1863 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001864 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001865
1866 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001867 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001868 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001869 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001870 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001871 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001872 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001873 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001874 do_l7_retry(s, si_b) == 0) {
1875 DBG_TRACE_DEVEL("leaving on L7 retry",
1876 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001877 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001878 }
Christopher Faulet47365272018-10-31 17:40:50 +01001879 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001880 /* fall through */
1881
Christopher Fauletb8a53712019-12-16 11:29:38 +01001882 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001883 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001884
1885 if (!(s->flags & SF_ERR_MASK))
1886 s->flags |= SF_ERR_PRXCOND;
1887 if (!(s->flags & SF_FINST_MASK))
1888 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001889
1890 s->si[1].flags |= SI_FL_NOLINGER;
1891 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001892 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001893 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001894 DBG_TRACE_DEVEL("leaving on error",
1895 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001896 return 0;
1897
Christopher Faulete0768eb2018-10-03 16:38:02 +02001898 abort_keep_alive:
1899 /* A keep-alive request to the server failed on a network error.
1900 * The client is required to retry. We need to close without returning
1901 * any other information so that the client retries.
1902 */
1903 txn->status = 0;
1904 rep->analysers &= AN_RES_FLT_END;
1905 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001906 s->logs.logwait = 0;
1907 s->logs.level = 0;
1908 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001909 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001910 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1911 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001912 return 0;
1913}
1914
1915/* This function performs all the processing enabled for the current response.
1916 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1917 * and updates s->res.analysers. It might make sense to explode it into several
1918 * other functions. It works like process_request (see indications above).
1919 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001920int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001921{
1922 struct session *sess = s->sess;
1923 struct http_txn *txn = s->txn;
1924 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001925 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001926 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001927 enum rule_result ret = HTTP_RULE_RES_CONT;
1928
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001929 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1930 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001931
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001932 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001933
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001934 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001935
1936 /* The stats applet needs to adjust the Connection header but we don't
1937 * apply any filter there.
1938 */
1939 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1940 rep->analysers &= ~an_bit;
1941 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001942 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001943 }
1944
1945 /*
1946 * We will have to evaluate the filters.
1947 * As opposed to version 1.2, now they will be evaluated in the
1948 * filters order and not in the header order. This means that
1949 * each filter has to be validated among all headers.
1950 *
1951 * Filters are tried with ->be first, then with ->fe if it is
1952 * different from ->be.
1953 *
1954 * Maybe we are in resume condiion. In this case I choose the
1955 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001956 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001957 * the process with the first one.
1958 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001959 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001960 * pointer and the ->fe rule list. If it doesn't match, I initialize
1961 * the loop with the ->be.
1962 */
1963 if (s->current_rule_list == &sess->fe->http_res_rules)
1964 cur_proxy = sess->fe;
1965 else
1966 cur_proxy = s->be;
1967 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001968 /* evaluate http-response rules */
1969 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001970 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001971
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001972 switch (ret) {
1973 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1974 goto return_prx_yield;
1975
1976 case HTTP_RULE_RES_CONT:
1977 case HTTP_RULE_RES_STOP: /* nothing to do */
1978 break;
1979
1980 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1981 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001982
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001983 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1984 goto return_prx_cond;
1985
1986 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001987 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001988
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001989 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1990 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001991
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001992 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1993 goto return_int_err;
1994 }
1995
1996 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001997
Christopher Faulete0768eb2018-10-03 16:38:02 +02001998 /* check whether we're already working on the frontend */
1999 if (cur_proxy == sess->fe)
2000 break;
2001 cur_proxy = sess->fe;
2002 }
2003
Christopher Faulete0768eb2018-10-03 16:38:02 +02002004 /* OK that's all we can do for 1xx responses */
2005 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002006 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002007
2008 /*
2009 * Now check for a server cookie.
2010 */
2011 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002012 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002013
2014 /*
2015 * Check for cache-control or pragma headers if required.
2016 */
2017 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002018 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002019
2020 /*
2021 * Add server cookie in the response if needed
2022 */
2023 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
2024 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
2025 (!(s->flags & SF_DIRECT) ||
2026 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
2027 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
2028 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
2029 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
2030 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
2031 !(s->flags & SF_IGNORE_PRST)) {
2032 /* the server is known, it's not the one the client requested, or the
2033 * cookie's last seen date needs to be refreshed. We have to
2034 * insert a set-cookie here, except if we want to insert only on POST
2035 * requests and this one isn't. Note that servers which don't have cookies
2036 * (eg: some backup servers) will return a full cookie removal request.
2037 */
2038 if (!objt_server(s->target)->cookie) {
2039 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002040 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02002041 s->be->cookie_name);
2042 }
2043 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002044 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002045
2046 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2047 /* emit last_date, which is mandatory */
2048 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2049 s30tob64((date.tv_sec+3) >> 2,
2050 trash.area + trash.data);
2051 trash.data += 5;
2052
2053 if (s->be->cookie_maxlife) {
2054 /* emit first_date, which is either the original one or
2055 * the current date.
2056 */
2057 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2058 s30tob64(txn->cookie_first_date ?
2059 txn->cookie_first_date >> 2 :
2060 (date.tv_sec+3) >> 2,
2061 trash.area + trash.data);
2062 trash.data += 5;
2063 }
2064 }
2065 chunk_appendf(&trash, "; path=/");
2066 }
2067
2068 if (s->be->cookie_domain)
2069 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2070
2071 if (s->be->ck_opts & PR_CK_HTTPONLY)
2072 chunk_appendf(&trash, "; HttpOnly");
2073
2074 if (s->be->ck_opts & PR_CK_SECURE)
2075 chunk_appendf(&trash, "; Secure");
2076
Christopher Faulet2f533902020-01-21 11:06:48 +01002077 if (s->be->cookie_attrs)
2078 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
2079
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002080 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002081 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002082
2083 txn->flags &= ~TX_SCK_MASK;
2084 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2085 /* the server did not change, only the date was updated */
2086 txn->flags |= TX_SCK_UPDATED;
2087 else
2088 txn->flags |= TX_SCK_INSERTED;
2089
2090 /* Here, we will tell an eventual cache on the client side that we don't
2091 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2092 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2093 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2094 */
2095 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2096
2097 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2098
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002099 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002100 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002101 }
2102 }
2103
2104 /*
2105 * Check if result will be cacheable with a cookie.
2106 * We'll block the response if security checks have caught
2107 * nasty things such as a cacheable cookie.
2108 */
2109 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2110 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2111 (s->be->options & PR_O_CHK_CACHE)) {
2112 /* we're in presence of a cacheable response containing
2113 * a set-cookie header. We'll block it as requested by
2114 * the 'checkcache' option, and send an alert.
2115 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002116 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2117 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2118 send_log(s->be, LOG_ALERT,
2119 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2120 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002121 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002122 }
2123
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002124 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002125 /*
2126 * Evaluate after-response rules before forwarding the response. rules
2127 * from the backend are evaluated first, then one from the frontend if
2128 * it differs.
2129 */
2130 if (!http_eval_after_res_rules(s))
2131 goto return_int_err;
2132
Christopher Faulete0768eb2018-10-03 16:38:02 +02002133 /* Always enter in the body analyzer */
2134 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2135 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2136
2137 /* if the user wants to log as soon as possible, without counting
2138 * bytes from the server, then this is the right moment. We have
2139 * to temporarily assign bytes_out to log what we currently have.
2140 */
2141 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2142 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002143 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002144 s->do_log(s);
2145 s->logs.bytes_out = 0;
2146 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002147
Christopher Fauletb8a53712019-12-16 11:29:38 +01002148 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002149 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002150 rep->analysers &= ~an_bit;
2151 rep->analyse_exp = TICK_ETERNITY;
2152 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002153
Christopher Fauletb8a53712019-12-16 11:29:38 +01002154 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002155 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002156 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002157 if (sess->listener->counters)
2158 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002159 if (objt_server(s->target))
2160 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002161 goto return_prx_err;
2162
2163 return_int_err:
2164 txn->status = 500;
2165 if (!(s->flags & SF_ERR_MASK))
2166 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002167 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002168 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2169 if (objt_server(s->target))
2170 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002171 if (objt_server(s->target))
2172 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002173 goto return_prx_err;
2174
2175 return_bad_res:
2176 txn->status = 502;
Christopher Fauleta20a6532020-02-05 10:16:41 +01002177 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2178 if (objt_server(s->target)) {
2179 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2180 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2181 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002182 /* fall through */
2183
2184 return_prx_err:
2185 http_reply_and_close(s, txn->status, http_error_message(s));
2186 /* fall through */
2187
2188 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002189 s->logs.t_data = -1; /* was not a valid response */
2190 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002191
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002192 if (!(s->flags & SF_ERR_MASK))
2193 s->flags |= SF_ERR_PRXCOND;
2194 if (!(s->flags & SF_FINST_MASK))
2195 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002196
Christopher Faulete58c0002020-03-02 16:21:01 +01002197 rep->analysers &= AN_RES_FLT_END;
2198 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002199 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002200 DBG_TRACE_DEVEL("leaving on error",
2201 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002202 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002203
2204 return_prx_yield:
2205 channel_dont_close(rep);
2206 DBG_TRACE_DEVEL("waiting for more data",
2207 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2208 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002209}
2210
2211/* This function is an analyser which forwards response body (including chunk
2212 * sizes if any). It is called as soon as we must forward, even if we forward
2213 * zero byte. The only situation where it must not be called is when we're in
2214 * tunnel mode and we want to forward till the close. It's used both to forward
2215 * remaining data and to resync after end of body. It expects the msg_state to
2216 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2217 * read more data, or 1 once we can go on with next request or end the stream.
2218 *
2219 * It is capable of compressing response data both in content-length mode and
2220 * in chunked mode. The state machines follows different flows depending on
2221 * whether content-length and chunked modes are used, since there are no
2222 * trailers in content-length :
2223 *
2224 * chk-mode cl-mode
2225 * ,----- BODY -----.
2226 * / \
2227 * V size > 0 V chk-mode
2228 * .--> SIZE -------------> DATA -------------> CRLF
2229 * | | size == 0 | last byte |
2230 * | v final crlf v inspected |
2231 * | TRAILERS -----------> DONE |
2232 * | |
2233 * `----------------------------------------------'
2234 *
2235 * Compression only happens in the DATA state, and must be flushed in final
2236 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2237 * is performed at once on final states for all bytes parsed, or when leaving
2238 * on missing data.
2239 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002240int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002241{
2242 struct session *sess = s->sess;
2243 struct http_txn *txn = s->txn;
2244 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002245 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002246 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002247
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002248 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002249
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002250 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002251
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002252 if (htx->flags & HTX_FL_PARSING_ERROR)
2253 goto return_bad_res;
2254 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2255 goto return_int_err;
2256
Christopher Faulete0768eb2018-10-03 16:38:02 +02002257 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002258 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002259 /* Output closed while we were sending data. We must abort and
2260 * wake the other side up.
2261 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002262 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002263 http_end_response(s);
2264 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002265 DBG_TRACE_DEVEL("leaving on error",
2266 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002267 return 1;
2268 }
2269
Christopher Faulet9768c262018-10-22 09:34:31 +02002270 if (msg->msg_state == HTTP_MSG_BODY)
2271 msg->msg_state = HTTP_MSG_DATA;
2272
Christopher Faulete0768eb2018-10-03 16:38:02 +02002273 /* in most states, we should abort in case of early close */
2274 channel_auto_close(res);
2275
Christopher Faulete0768eb2018-10-03 16:38:02 +02002276 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002277 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002278 if (res->flags & CF_EOI)
2279 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002280 }
2281 else {
2282 /* We can't process the buffer's contents yet */
2283 res->flags |= CF_WAKE_WRITE;
2284 goto missing_data_or_waiting;
2285 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002286 }
2287
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002288 if (msg->msg_state >= HTTP_MSG_ENDING)
2289 goto ending;
2290
2291 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2292 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2293 msg->msg_state = HTTP_MSG_ENDING;
2294 goto ending;
2295 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002296
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002297 /* Forward input data. We get it by removing all outgoing data not
2298 * forwarded yet from HTX data size. If there are some data filters, we
2299 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002300 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002301 if (HAS_RSP_DATA_FILTERS(s)) {
2302 ret = flt_http_payload(s, msg, htx->data);
2303 if (ret < 0)
2304 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002305 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002306 }
2307 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002308 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002309 if (msg->flags & HTTP_MSGF_XFER_LEN)
2310 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002311 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002312
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002313 if (htx->data != co_data(res))
2314 goto missing_data_or_waiting;
2315
2316 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2317 msg->msg_state = HTTP_MSG_ENDING;
2318 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002319 }
2320
Christopher Faulet9768c262018-10-22 09:34:31 +02002321 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002322 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2323 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002324 */
2325 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2326 goto missing_data_or_waiting;
2327
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002328 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002329
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002330 ending:
2331 /* other states, ENDING...TUNNEL */
2332 if (msg->msg_state >= HTTP_MSG_DONE)
2333 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002334
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002335 if (HAS_RSP_DATA_FILTERS(s)) {
2336 ret = flt_http_end(s, msg);
2337 if (ret <= 0) {
2338 if (!ret)
2339 goto missing_data_or_waiting;
2340 goto return_bad_res;
2341 }
2342 }
2343
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002344 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2345 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2346 msg->msg_state = HTTP_MSG_TUNNEL;
2347 goto ending;
2348 }
2349 else {
2350 msg->msg_state = HTTP_MSG_DONE;
2351 res->to_forward = 0;
2352 }
2353
2354 done:
2355
2356 channel_dont_close(res);
2357
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002358 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002359 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002360 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002361 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2362 if (res->flags & CF_SHUTW) {
2363 /* response errors are most likely due to the
2364 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002365 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002366 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002367 goto return_bad_res;
2368 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002369 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002370 return 1;
2371 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002372 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2373 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002374 return 0;
2375
2376 missing_data_or_waiting:
2377 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002378 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002379
2380 /* stop waiting for data if the input is closed before the end. If the
2381 * client side was already closed, it means that the client has aborted,
2382 * so we don't want to count this as a server abort. Otherwise it's a
2383 * server abort.
2384 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002385 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002386 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002387 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002388 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002389 if (htx_is_empty(htx))
2390 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002391 }
2392
Christopher Faulete0768eb2018-10-03 16:38:02 +02002393 /* When TE: chunked is used, we need to get there again to parse
2394 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002395 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2396 * are filters registered on the stream, we don't want to forward a
2397 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002398 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002399 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002400 channel_dont_close(res);
2401
2402 /* We know that more data are expected, but we couldn't send more that
2403 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2404 * system knows it must not set a PUSH on this first part. Interactive
2405 * modes are already handled by the stream sock layer. We must not do
2406 * this in content-length mode because it could present the MSG_MORE
2407 * flag with the last block of forwarded data, which would cause an
2408 * additional delay to be observed by the receiver.
2409 */
2410 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2411 res->flags |= CF_EXPECT_MORE;
2412
2413 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002414 DBG_TRACE_DEVEL("waiting for more data to forward",
2415 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002416 return 0;
2417
Christopher Faulet93e02d82019-03-08 14:18:50 +01002418 return_srv_abort:
2419 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2420 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002421 if (sess->listener->counters)
2422 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002423 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002424 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002425 if (!(s->flags & SF_ERR_MASK))
2426 s->flags |= SF_ERR_SRVCL;
2427 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002428
Christopher Faulet93e02d82019-03-08 14:18:50 +01002429 return_cli_abort:
2430 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2431 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002432 if (sess->listener->counters)
2433 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002434 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002435 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002436 if (!(s->flags & SF_ERR_MASK))
2437 s->flags |= SF_ERR_CLICL;
2438 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002439
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002440 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002441 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002442 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002443 if (sess->listener->counters)
2444 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002445 if (objt_server(s->target))
2446 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002447 if (!(s->flags & SF_ERR_MASK))
2448 s->flags |= SF_ERR_INTERNAL;
2449 goto return_error;
2450
Christopher Faulet93e02d82019-03-08 14:18:50 +01002451 return_bad_res:
2452 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2453 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002454 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002455 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2456 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002457 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002458 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002459 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002460
Christopher Faulet93e02d82019-03-08 14:18:50 +01002461 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002462 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002463 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002464 res->analysers &= AN_RES_FLT_END;
2465 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 +02002466 if (!(s->flags & SF_FINST_MASK))
2467 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002468 DBG_TRACE_DEVEL("leaving on error",
2469 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002470 return 0;
2471}
2472
Christopher Fauletf2824e62018-10-01 12:12:37 +02002473/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002474 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002475 * as too large a request to build a valid response.
2476 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002477int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002478{
Christopher Faulet99daf282018-11-28 22:58:13 +01002479 struct channel *req = &s->req;
2480 struct channel *res = &s->res;
2481 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002482 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002483 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002484 struct ist status, reason, location;
2485 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002486 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002487
2488 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002489 if (!chunk) {
2490 if (!(s->flags & SF_ERR_MASK))
2491 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002492 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002493 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002494
Christopher Faulet99daf282018-11-28 22:58:13 +01002495 /*
2496 * Create the location
2497 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002498 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002499 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002500 case REDIRECT_TYPE_SCHEME: {
2501 struct http_hdr_ctx ctx;
2502 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002503
Christopher Faulet99daf282018-11-28 22:58:13 +01002504 host = ist("");
2505 ctx.blk = NULL;
2506 if (http_find_header(htx, ist("Host"), &ctx, 0))
2507 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002508
Christopher Faulet297fbb42019-05-13 14:41:27 +02002509 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002510 path = http_get_path(htx_sl_req_uri(sl));
2511 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002512 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002513 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2514 int qs = 0;
2515 while (qs < path.len) {
2516 if (*(path.ptr + qs) == '?') {
2517 path.len = qs;
2518 break;
2519 }
2520 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002521 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002522 }
2523 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002524 else
2525 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002526
Christopher Faulet99daf282018-11-28 22:58:13 +01002527 if (rule->rdr_str) { /* this is an old "redirect" rule */
2528 /* add scheme */
2529 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2530 goto fail;
2531 }
2532 else {
2533 /* add scheme with executing log format */
2534 chunk->data += build_logline(s, chunk->area + chunk->data,
2535 chunk->size - chunk->data,
2536 &rule->rdr_fmt);
2537 }
2538 /* add "://" + host + path */
2539 if (!chunk_memcat(chunk, "://", 3) ||
2540 !chunk_memcat(chunk, host.ptr, host.len) ||
2541 !chunk_memcat(chunk, path.ptr, path.len))
2542 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002543
Christopher Faulet99daf282018-11-28 22:58:13 +01002544 /* append a slash at the end of the location if needed and missing */
2545 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2546 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2547 if (chunk->data + 1 >= chunk->size)
2548 goto fail;
2549 chunk->area[chunk->data++] = '/';
2550 }
2551 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002552 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002553
Christopher Faulet99daf282018-11-28 22:58:13 +01002554 case REDIRECT_TYPE_PREFIX: {
2555 struct ist path;
2556
Christopher Faulet297fbb42019-05-13 14:41:27 +02002557 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002558 path = http_get_path(htx_sl_req_uri(sl));
2559 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002560 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002561 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2562 int qs = 0;
2563 while (qs < path.len) {
2564 if (*(path.ptr + qs) == '?') {
2565 path.len = qs;
2566 break;
2567 }
2568 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002569 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002570 }
2571 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002572 else
2573 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002574
Christopher Faulet99daf282018-11-28 22:58:13 +01002575 if (rule->rdr_str) { /* this is an old "redirect" rule */
2576 /* add prefix. Note that if prefix == "/", we don't want to
2577 * add anything, otherwise it makes it hard for the user to
2578 * configure a self-redirection.
2579 */
2580 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2581 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2582 goto fail;
2583 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002584 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002585 else {
2586 /* add prefix with executing log format */
2587 chunk->data += build_logline(s, chunk->area + chunk->data,
2588 chunk->size - chunk->data,
2589 &rule->rdr_fmt);
2590 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002591
Christopher Faulet99daf282018-11-28 22:58:13 +01002592 /* add path */
2593 if (!chunk_memcat(chunk, path.ptr, path.len))
2594 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002595
Christopher Faulet99daf282018-11-28 22:58:13 +01002596 /* append a slash at the end of the location if needed and missing */
2597 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2598 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2599 if (chunk->data + 1 >= chunk->size)
2600 goto fail;
2601 chunk->area[chunk->data++] = '/';
2602 }
2603 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002604 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002605 case REDIRECT_TYPE_LOCATION:
2606 default:
2607 if (rule->rdr_str) { /* this is an old "redirect" rule */
2608 /* add location */
2609 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2610 goto fail;
2611 }
2612 else {
2613 /* add location with executing log format */
2614 chunk->data += build_logline(s, chunk->area + chunk->data,
2615 chunk->size - chunk->data,
2616 &rule->rdr_fmt);
2617 }
2618 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002619 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002620 location = ist2(chunk->area, chunk->data);
2621
2622 /*
2623 * Create the 30x response
2624 */
2625 switch (rule->code) {
2626 case 308:
2627 status = ist("308");
2628 reason = ist("Permanent Redirect");
2629 break;
2630 case 307:
2631 status = ist("307");
2632 reason = ist("Temporary Redirect");
2633 break;
2634 case 303:
2635 status = ist("303");
2636 reason = ist("See Other");
2637 break;
2638 case 301:
2639 status = ist("301");
2640 reason = ist("Moved Permanently");
2641 break;
2642 case 302:
2643 default:
2644 status = ist("302");
2645 reason = ist("Found");
2646 break;
2647 }
2648
Christopher Faulet08e66462019-05-23 16:44:59 +02002649 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2650 close = 1;
2651
Christopher Faulet99daf282018-11-28 22:58:13 +01002652 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002653 /* Trim any possible response */
2654 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002655 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2656 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2657 if (!sl)
2658 goto fail;
2659 sl->info.res.status = rule->code;
2660 s->txn->status = rule->code;
2661
Christopher Faulet08e66462019-05-23 16:44:59 +02002662 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2663 goto fail;
2664
2665 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002666 !htx_add_header(htx, ist("Location"), location))
2667 goto fail;
2668
2669 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2670 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2671 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002672 }
2673
2674 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002675 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2676 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002677 }
2678
Christopher Faulet99daf282018-11-28 22:58:13 +01002679 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2680 goto fail;
2681
Kevin Zhu96b36392020-01-07 09:42:55 +01002682 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002683 if (!http_forward_proxy_resp(s, 1))
2684 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002685
Christopher Faulet60b33a52020-01-28 09:18:10 +01002686 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2687 /* let's log the request time */
2688 s->logs.tv_request = now;
2689 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002690
Christopher Faulet60b33a52020-01-28 09:18:10 +01002691 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2692 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2693 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002694
2695 if (!(s->flags & SF_ERR_MASK))
2696 s->flags |= SF_ERR_LOCAL;
2697 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002698 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002699
Christopher Faulet99daf282018-11-28 22:58:13 +01002700 free_trash_chunk(chunk);
2701 return 1;
2702
2703 fail:
2704 /* If an error occurred, remove the incomplete HTTP response from the
2705 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002706 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002707 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002708 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002709}
2710
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002711/* Replace all headers matching the name <name>. The header value is replaced if
2712 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2713 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2714 * values are evaluated one by one. It returns 0 on success and -1 on error.
2715 */
2716int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2717 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002718{
2719 struct http_hdr_ctx ctx;
2720 struct buffer *output = get_trash_chunk();
2721
Christopher Faulet72333522018-10-24 11:25:02 +02002722 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002723 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002724 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2725 continue;
2726
2727 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2728 if (output->data == -1)
2729 return -1;
2730 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2731 return -1;
2732 }
2733 return 0;
2734}
2735
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002736/* This function executes one of the set-{method,path,query,uri} actions. It
2737 * takes the string from the variable 'replace' with length 'len', then modifies
2738 * the relevant part of the request line accordingly. Then it updates various
2739 * pointers to the next elements which were moved, and the total buffer length.
2740 * It finds the action to be performed in p[2], previously filled by function
2741 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2742 * error, though this can be revisited when this code is finally exploited.
2743 *
2744 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2745 * query string and 3 to replace uri.
2746 *
2747 * In query string case, the mark question '?' must be set at the start of the
2748 * string by the caller, event if the replacement query string is empty.
2749 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002750int http_req_replace_stline(int action, const char *replace, int len,
2751 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002752{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002753 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002754
2755 switch (action) {
2756 case 0: // method
2757 if (!http_replace_req_meth(htx, ist2(replace, len)))
2758 return -1;
2759 break;
2760
2761 case 1: // path
2762 if (!http_replace_req_path(htx, ist2(replace, len)))
2763 return -1;
2764 break;
2765
2766 case 2: // query
2767 if (!http_replace_req_query(htx, ist2(replace, len)))
2768 return -1;
2769 break;
2770
2771 case 3: // uri
2772 if (!http_replace_req_uri(htx, ist2(replace, len)))
2773 return -1;
2774 break;
2775
2776 default:
2777 return -1;
2778 }
2779 return 0;
2780}
2781
2782/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002783 * variable <status> contains the new status code. This function never fails. It
2784 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002785 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002786int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002787{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002788 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002789 char *res;
2790
2791 chunk_reset(&trash);
2792 res = ultoa_o(status, trash.area, trash.size);
2793 trash.data = res - trash.area;
2794
2795 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002796 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002797 const char *str = http_get_reason(status);
2798 reason = ist2(str, strlen(str));
2799 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002800
Christopher Faulete00d06c2019-12-16 17:18:42 +01002801 if (!http_replace_res_status(htx, ist2(trash.area, trash.data)))
2802 return -1;
Christopher Faulet96bff762019-12-17 13:46:18 +01002803 if (!http_replace_res_reason(htx, reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002804 return -1;
2805 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002806}
2807
Christopher Faulet3e964192018-10-24 11:39:23 +02002808/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2809 * transaction <txn>. Returns the verdict of the first rule that prevents
2810 * further processing of the request (auth, deny, ...), and defaults to
2811 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2812 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2813 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2814 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2815 * status.
2816 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002817static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002818 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002819{
2820 struct session *sess = strm_sess(s);
2821 struct http_txn *txn = s->txn;
2822 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002823 struct act_rule *rule;
2824 struct http_hdr_ctx ctx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002825 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002826 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002827
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002828 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002829
2830 /* If "the current_rule_list" match the executed rule list, we are in
2831 * resume condition. If a resume is needed it is always in the action
2832 * and never in the ACL or converters. In this case, we initialise the
2833 * current rule, and go to the action execution point.
2834 */
2835 if (s->current_rule) {
2836 rule = s->current_rule;
2837 s->current_rule = NULL;
2838 if (s->current_rule_list == rules)
2839 goto resume_execution;
2840 }
2841 s->current_rule_list = rules;
2842
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002843 /* start the ruleset evaluation in strict mode */
2844 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002845
Christopher Faulet3e964192018-10-24 11:39:23 +02002846 list_for_each_entry(rule, rules, list) {
2847 /* check optional condition */
2848 if (rule->cond) {
2849 int ret;
2850
2851 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2852 ret = acl_pass(ret);
2853
2854 if (rule->cond->pol == ACL_COND_UNLESS)
2855 ret = !ret;
2856
2857 if (!ret) /* condition not matched */
2858 continue;
2859 }
2860
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002861 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002862 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002863 /* Always call the action function if defined */
2864 if (rule->action_ptr) {
2865 if ((s->req.flags & CF_READ_ERROR) ||
2866 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2867 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002868 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002869
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002870 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002871 case ACT_RET_CONT:
2872 break;
2873 case ACT_RET_STOP:
2874 rule_ret = HTTP_RULE_RES_STOP;
2875 goto end;
2876 case ACT_RET_YIELD:
2877 s->current_rule = rule;
2878 rule_ret = HTTP_RULE_RES_YIELD;
2879 goto end;
2880 case ACT_RET_ERR:
2881 rule_ret = HTTP_RULE_RES_ERROR;
2882 goto end;
2883 case ACT_RET_DONE:
2884 rule_ret = HTTP_RULE_RES_DONE;
2885 goto end;
2886 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002887 if (txn->status == -1)
2888 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002889 rule_ret = HTTP_RULE_RES_DENY;
2890 goto end;
2891 case ACT_RET_ABRT:
2892 rule_ret = HTTP_RULE_RES_ABRT;
2893 goto end;
2894 case ACT_RET_INV:
2895 rule_ret = HTTP_RULE_RES_BADREQ;
2896 goto end;
2897 }
2898 continue; /* eval the next rule */
2899 }
2900
2901 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002902 switch (rule->action) {
2903 case ACT_ACTION_ALLOW:
2904 rule_ret = HTTP_RULE_RES_STOP;
2905 goto end;
2906
2907 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002908 txn->status = rule->arg.http_reply->status;
2909 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002910 rule_ret = HTTP_RULE_RES_DENY;
2911 goto end;
2912
2913 case ACT_HTTP_REQ_TARPIT:
2914 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002915 txn->status = rule->arg.http_reply->status;
2916 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002917 rule_ret = HTTP_RULE_RES_DENY;
2918 goto end;
2919
Christopher Faulet3e964192018-10-24 11:39:23 +02002920 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002921 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002922 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002923 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002924 goto end;
2925
2926 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002927 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002928 break;
2929
2930 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002931 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002932 break;
2933
2934 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002935 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002936 break;
2937
2938 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002939 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002940 break;
2941
Christopher Faulet3e964192018-10-24 11:39:23 +02002942 case ACT_HTTP_DEL_HDR:
2943 /* remove all occurrences of the header */
2944 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01002945 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02002946 http_remove_header(htx, &ctx);
2947 break;
2948
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002949 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002950 default:
2951 break;
2952 }
2953 }
2954
2955 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002956 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002957 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002958 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002959
Christopher Faulet3e964192018-10-24 11:39:23 +02002960 /* we reached the end of the rules, nothing to report */
2961 return rule_ret;
2962}
2963
2964/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2965 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2966 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2967 * is returned, the process can continue the evaluation of next rule list. If
2968 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2969 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002970 * must be returned. If *YIELD is returned, the caller must call again the
2971 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002972 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002973static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2974 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002975{
2976 struct session *sess = strm_sess(s);
2977 struct http_txn *txn = s->txn;
2978 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002979 struct act_rule *rule;
2980 struct http_hdr_ctx ctx;
2981 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002982 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002983
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002984 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002985
2986 /* If "the current_rule_list" match the executed rule list, we are in
2987 * resume condition. If a resume is needed it is always in the action
2988 * and never in the ACL or converters. In this case, we initialise the
2989 * current rule, and go to the action execution point.
2990 */
2991 if (s->current_rule) {
2992 rule = s->current_rule;
2993 s->current_rule = NULL;
2994 if (s->current_rule_list == rules)
2995 goto resume_execution;
2996 }
2997 s->current_rule_list = rules;
2998
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002999 /* start the ruleset evaluation in strict mode */
3000 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003001
Christopher Faulet3e964192018-10-24 11:39:23 +02003002 list_for_each_entry(rule, rules, list) {
3003 /* check optional condition */
3004 if (rule->cond) {
3005 int ret;
3006
3007 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3008 ret = acl_pass(ret);
3009
3010 if (rule->cond->pol == ACL_COND_UNLESS)
3011 ret = !ret;
3012
3013 if (!ret) /* condition not matched */
3014 continue;
3015 }
3016
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003017 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02003018resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003019
3020 /* Always call the action function if defined */
3021 if (rule->action_ptr) {
3022 if ((s->req.flags & CF_READ_ERROR) ||
3023 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3024 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003025 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003026
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003027 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003028 case ACT_RET_CONT:
3029 break;
3030 case ACT_RET_STOP:
3031 rule_ret = HTTP_RULE_RES_STOP;
3032 goto end;
3033 case ACT_RET_YIELD:
3034 s->current_rule = rule;
3035 rule_ret = HTTP_RULE_RES_YIELD;
3036 goto end;
3037 case ACT_RET_ERR:
3038 rule_ret = HTTP_RULE_RES_ERROR;
3039 goto end;
3040 case ACT_RET_DONE:
3041 rule_ret = HTTP_RULE_RES_DONE;
3042 goto end;
3043 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01003044 if (txn->status == -1)
3045 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003046 rule_ret = HTTP_RULE_RES_DENY;
3047 goto end;
3048 case ACT_RET_ABRT:
3049 rule_ret = HTTP_RULE_RES_ABRT;
3050 goto end;
3051 case ACT_RET_INV:
3052 rule_ret = HTTP_RULE_RES_BADREQ;
3053 goto end;
3054 }
3055 continue; /* eval the next rule */
3056 }
3057
3058 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003059 switch (rule->action) {
3060 case ACT_ACTION_ALLOW:
3061 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3062 goto end;
3063
3064 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02003065 txn->status = rule->arg.http_reply->status;
3066 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003067 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003068 goto end;
3069
3070 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003071 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003072 break;
3073
3074 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003075 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003076 break;
3077
3078 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003079 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003080 break;
3081
3082 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003083 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003084 break;
3085
Christopher Faulet3e964192018-10-24 11:39:23 +02003086 case ACT_HTTP_DEL_HDR:
3087 /* remove all occurrences of the header */
3088 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01003089 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02003090 http_remove_header(htx, &ctx);
3091 break;
3092
Christopher Faulet3e964192018-10-24 11:39:23 +02003093 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003094 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003095 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003096 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003097 goto end;
3098
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003099 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003100 default:
3101 break;
3102 }
3103 }
3104
3105 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003106 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003107 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003108 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003109
Christopher Faulet3e964192018-10-24 11:39:23 +02003110 /* we reached the end of the rules, nothing to report */
3111 return rule_ret;
3112}
3113
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003114/* Executes backend and frontend http-after-response rules for the stream <s>,
3115 * in that order. it return 1 on success and 0 on error. It is the caller
3116 * responsibility to catch error or ignore it. If it catches it, this function
3117 * may be called a second time, for the internal error.
3118 */
3119int http_eval_after_res_rules(struct stream *s)
3120{
3121 struct session *sess = s->sess;
3122 enum rule_result ret = HTTP_RULE_RES_CONT;
3123
Christopher Faulet507479b2020-05-15 12:29:46 +02003124 /* Eval after-response ruleset only if the reply is not const */
3125 if (s->txn->flags & TX_CONST_REPLY)
3126 goto end;
3127
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003128 /* prune the request variables if not already done and swap to the response variables. */
3129 if (s->vars_reqres.scope != SCOPE_RES) {
3130 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3131 vars_prune(&s->vars_reqres, s->sess, s);
3132 vars_init(&s->vars_reqres, SCOPE_RES);
3133 }
3134
3135 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3136 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3137 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3138
Christopher Faulet507479b2020-05-15 12:29:46 +02003139 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003140 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3141 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3142}
3143
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003144/*
3145 * Manage client-side cookie. It can impact performance by about 2% so it is
3146 * desirable to call it only when needed. This code is quite complex because
3147 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3148 * highly recommended not to touch this part without a good reason !
3149 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003150static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003151{
3152 struct session *sess = s->sess;
3153 struct http_txn *txn = s->txn;
3154 struct htx *htx;
3155 struct http_hdr_ctx ctx;
3156 char *hdr_beg, *hdr_end, *del_from;
3157 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3158 int preserve_hdr;
3159
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003160 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003161 ctx.blk = NULL;
3162 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003163 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003164 del_from = NULL; /* nothing to be deleted */
3165 preserve_hdr = 0; /* assume we may kill the whole header */
3166
3167 /* Now look for cookies. Conforming to RFC2109, we have to support
3168 * attributes whose name begin with a '$', and associate them with
3169 * the right cookie, if we want to delete this cookie.
3170 * So there are 3 cases for each cookie read :
3171 * 1) it's a special attribute, beginning with a '$' : ignore it.
3172 * 2) it's a server id cookie that we *MAY* want to delete : save
3173 * some pointers on it (last semi-colon, beginning of cookie...)
3174 * 3) it's an application cookie : we *MAY* have to delete a previous
3175 * "special" cookie.
3176 * At the end of loop, if a "special" cookie remains, we may have to
3177 * remove it. If no application cookie persists in the header, we
3178 * *MUST* delete it.
3179 *
3180 * Note: RFC2965 is unclear about the processing of spaces around
3181 * the equal sign in the ATTR=VALUE form. A careful inspection of
3182 * the RFC explicitly allows spaces before it, and not within the
3183 * tokens (attrs or values). An inspection of RFC2109 allows that
3184 * too but section 10.1.3 lets one think that spaces may be allowed
3185 * after the equal sign too, resulting in some (rare) buggy
3186 * implementations trying to do that. So let's do what servers do.
3187 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3188 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003189 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003190 * causes parsing to become ambiguous. Browsers also allow spaces
3191 * within values even without quotes.
3192 *
3193 * We have to keep multiple pointers in order to support cookie
3194 * removal at the beginning, middle or end of header without
3195 * corrupting the header. All of these headers are valid :
3196 *
3197 * hdr_beg hdr_end
3198 * | |
3199 * v |
3200 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3201 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3202 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3203 * | | | | | | |
3204 * | | | | | | |
3205 * | | | | | | +--> next
3206 * | | | | | +----> val_end
3207 * | | | | +-----------> val_beg
3208 * | | | +--------------> equal
3209 * | | +----------------> att_end
3210 * | +---------------------> att_beg
3211 * +--------------------------> prev
3212 *
3213 */
3214 hdr_beg = ctx.value.ptr;
3215 hdr_end = hdr_beg + ctx.value.len;
3216 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3217 /* Iterate through all cookies on this line */
3218
3219 /* find att_beg */
3220 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003221 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003222 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003223 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003224
3225 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3226 att_beg++;
3227
3228 /* find att_end : this is the first character after the last non
3229 * space before the equal. It may be equal to hdr_end.
3230 */
3231 equal = att_end = att_beg;
3232 while (equal < hdr_end) {
3233 if (*equal == '=' || *equal == ',' || *equal == ';')
3234 break;
3235 if (HTTP_IS_SPHT(*equal++))
3236 continue;
3237 att_end = equal;
3238 }
3239
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003240 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003241 * is between <att_beg> and <equal>, both may be identical.
3242 */
3243 /* look for end of cookie if there is an equal sign */
3244 if (equal < hdr_end && *equal == '=') {
3245 /* look for the beginning of the value */
3246 val_beg = equal + 1;
3247 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3248 val_beg++;
3249
3250 /* find the end of the value, respecting quotes */
3251 next = http_find_cookie_value_end(val_beg, hdr_end);
3252
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003253 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003254 val_end = next;
3255 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3256 val_end--;
3257 }
3258 else
3259 val_beg = val_end = next = equal;
3260
3261 /* We have nothing to do with attributes beginning with
3262 * '$'. However, they will automatically be removed if a
3263 * header before them is removed, since they're supposed
3264 * to be linked together.
3265 */
3266 if (*att_beg == '$')
3267 continue;
3268
3269 /* Ignore cookies with no equal sign */
3270 if (equal == next) {
3271 /* This is not our cookie, so we must preserve it. But if we already
3272 * scheduled another cookie for removal, we cannot remove the
3273 * complete header, but we can remove the previous block itself.
3274 */
3275 preserve_hdr = 1;
3276 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003277 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003278 val_end += delta;
3279 next += delta;
3280 hdr_end += delta;
3281 prev = del_from;
3282 del_from = NULL;
3283 }
3284 continue;
3285 }
3286
3287 /* if there are spaces around the equal sign, we need to
3288 * strip them otherwise we'll get trouble for cookie captures,
3289 * or even for rewrites. Since this happens extremely rarely,
3290 * it does not hurt performance.
3291 */
3292 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3293 int stripped_before = 0;
3294 int stripped_after = 0;
3295
3296 if (att_end != equal) {
3297 memmove(att_end, equal, hdr_end - equal);
3298 stripped_before = (att_end - equal);
3299 equal += stripped_before;
3300 val_beg += stripped_before;
3301 }
3302
3303 if (val_beg > equal + 1) {
3304 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3305 stripped_after = (equal + 1) - val_beg;
3306 val_beg += stripped_after;
3307 stripped_before += stripped_after;
3308 }
3309
3310 val_end += stripped_before;
3311 next += stripped_before;
3312 hdr_end += stripped_before;
3313 }
3314 /* now everything is as on the diagram above */
3315
3316 /* First, let's see if we want to capture this cookie. We check
3317 * that we don't already have a client side cookie, because we
3318 * can only capture one. Also as an optimisation, we ignore
3319 * cookies shorter than the declared name.
3320 */
3321 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3322 (val_end - att_beg >= sess->fe->capture_namelen) &&
3323 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3324 int log_len = val_end - att_beg;
3325
3326 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3327 ha_alert("HTTP logging : out of memory.\n");
3328 } else {
3329 if (log_len > sess->fe->capture_len)
3330 log_len = sess->fe->capture_len;
3331 memcpy(txn->cli_cookie, att_beg, log_len);
3332 txn->cli_cookie[log_len] = 0;
3333 }
3334 }
3335
3336 /* Persistence cookies in passive, rewrite or insert mode have the
3337 * following form :
3338 *
3339 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3340 *
3341 * For cookies in prefix mode, the form is :
3342 *
3343 * Cookie: NAME=SRV~VALUE
3344 */
3345 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3346 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3347 struct server *srv = s->be->srv;
3348 char *delim;
3349
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003350 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003351 * have the server ID between val_beg and delim, and the original cookie between
3352 * delim+1 and val_end. Otherwise, delim==val_end :
3353 *
3354 * hdr_beg
3355 * |
3356 * v
3357 * NAME=SRV; # in all but prefix modes
3358 * NAME=SRV~OPAQUE ; # in prefix mode
3359 * || || | |+-> next
3360 * || || | +--> val_end
3361 * || || +---------> delim
3362 * || |+------------> val_beg
3363 * || +-------------> att_end = equal
3364 * |+-----------------> att_beg
3365 * +------------------> prev
3366 *
3367 */
3368 if (s->be->ck_opts & PR_CK_PFX) {
3369 for (delim = val_beg; delim < val_end; delim++)
3370 if (*delim == COOKIE_DELIM)
3371 break;
3372 }
3373 else {
3374 char *vbar1;
3375 delim = val_end;
3376 /* Now check if the cookie contains a date field, which would
3377 * appear after a vertical bar ('|') just after the server name
3378 * and before the delimiter.
3379 */
3380 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3381 if (vbar1) {
3382 /* OK, so left of the bar is the server's cookie and
3383 * right is the last seen date. It is a base64 encoded
3384 * 30-bit value representing the UNIX date since the
3385 * epoch in 4-second quantities.
3386 */
3387 int val;
3388 delim = vbar1++;
3389 if (val_end - vbar1 >= 5) {
3390 val = b64tos30(vbar1);
3391 if (val > 0)
3392 txn->cookie_last_date = val << 2;
3393 }
3394 /* look for a second vertical bar */
3395 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3396 if (vbar1 && (val_end - vbar1 > 5)) {
3397 val = b64tos30(vbar1 + 1);
3398 if (val > 0)
3399 txn->cookie_first_date = val << 2;
3400 }
3401 }
3402 }
3403
3404 /* if the cookie has an expiration date and the proxy wants to check
3405 * it, then we do that now. We first check if the cookie is too old,
3406 * then only if it has expired. We detect strict overflow because the
3407 * time resolution here is not great (4 seconds). Cookies with dates
3408 * in the future are ignored if their offset is beyond one day. This
3409 * allows an admin to fix timezone issues without expiring everyone
3410 * and at the same time avoids keeping unwanted side effects for too
3411 * long.
3412 */
3413 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3414 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3415 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3416 txn->flags &= ~TX_CK_MASK;
3417 txn->flags |= TX_CK_OLD;
3418 delim = val_beg; // let's pretend we have not found the cookie
3419 txn->cookie_first_date = 0;
3420 txn->cookie_last_date = 0;
3421 }
3422 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3423 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3424 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3425 txn->flags &= ~TX_CK_MASK;
3426 txn->flags |= TX_CK_EXPIRED;
3427 delim = val_beg; // let's pretend we have not found the cookie
3428 txn->cookie_first_date = 0;
3429 txn->cookie_last_date = 0;
3430 }
3431
3432 /* Here, we'll look for the first running server which supports the cookie.
3433 * This allows to share a same cookie between several servers, for example
3434 * to dedicate backup servers to specific servers only.
3435 * However, to prevent clients from sticking to cookie-less backup server
3436 * when they have incidentely learned an empty cookie, we simply ignore
3437 * empty cookies and mark them as invalid.
3438 * The same behaviour is applied when persistence must be ignored.
3439 */
3440 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3441 srv = NULL;
3442
3443 while (srv) {
3444 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3445 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3446 if ((srv->cur_state != SRV_ST_STOPPED) ||
3447 (s->be->options & PR_O_PERSIST) ||
3448 (s->flags & SF_FORCE_PRST)) {
3449 /* we found the server and we can use it */
3450 txn->flags &= ~TX_CK_MASK;
3451 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3452 s->flags |= SF_DIRECT | SF_ASSIGNED;
3453 s->target = &srv->obj_type;
3454 break;
3455 } else {
3456 /* we found a server, but it's down,
3457 * mark it as such and go on in case
3458 * another one is available.
3459 */
3460 txn->flags &= ~TX_CK_MASK;
3461 txn->flags |= TX_CK_DOWN;
3462 }
3463 }
3464 srv = srv->next;
3465 }
3466
3467 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3468 /* no server matched this cookie or we deliberately skipped it */
3469 txn->flags &= ~TX_CK_MASK;
3470 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3471 txn->flags |= TX_CK_UNUSED;
3472 else
3473 txn->flags |= TX_CK_INVALID;
3474 }
3475
3476 /* depending on the cookie mode, we may have to either :
3477 * - delete the complete cookie if we're in insert+indirect mode, so that
3478 * the server never sees it ;
3479 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003480 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003481 * if we're in cookie prefix mode
3482 */
3483 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3484 int delta; /* negative */
3485
3486 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3487 delta = val_beg - (delim + 1);
3488 val_end += delta;
3489 next += delta;
3490 hdr_end += delta;
3491 del_from = NULL;
3492 preserve_hdr = 1; /* we want to keep this cookie */
3493 }
3494 else if (del_from == NULL &&
3495 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3496 del_from = prev;
3497 }
3498 }
3499 else {
3500 /* This is not our cookie, so we must preserve it. But if we already
3501 * scheduled another cookie for removal, we cannot remove the
3502 * complete header, but we can remove the previous block itself.
3503 */
3504 preserve_hdr = 1;
3505
3506 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003507 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003508 if (att_beg >= del_from)
3509 att_beg += delta;
3510 if (att_end >= del_from)
3511 att_end += delta;
3512 val_beg += delta;
3513 val_end += delta;
3514 next += delta;
3515 hdr_end += delta;
3516 prev = del_from;
3517 del_from = NULL;
3518 }
3519 }
3520
3521 /* continue with next cookie on this header line */
3522 att_beg = next;
3523 } /* for each cookie */
3524
3525
3526 /* There are no more cookies on this line.
3527 * We may still have one (or several) marked for deletion at the
3528 * end of the line. We must do this now in two ways :
3529 * - if some cookies must be preserved, we only delete from the
3530 * mark to the end of line ;
3531 * - if nothing needs to be preserved, simply delete the whole header
3532 */
3533 if (del_from) {
3534 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3535 }
3536 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003537 if (hdr_beg != hdr_end)
3538 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003539 else
3540 http_remove_header(htx, &ctx);
3541 }
3542 } /* for each "Cookie header */
3543}
3544
3545/*
3546 * Manage server-side cookies. It can impact performance by about 2% so it is
3547 * desirable to call it only when needed. This function is also used when we
3548 * just need to know if there is a cookie (eg: for check-cache).
3549 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003550static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003551{
3552 struct session *sess = s->sess;
3553 struct http_txn *txn = s->txn;
3554 struct htx *htx;
3555 struct http_hdr_ctx ctx;
3556 struct server *srv;
3557 char *hdr_beg, *hdr_end;
3558 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003559 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003560
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003561 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003562
3563 ctx.blk = NULL;
3564 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003565 int is_first = 1;
3566
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003567 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3568 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3569 break;
3570 is_cookie2 = 1;
3571 }
3572
3573 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3574 * <prev> points to the colon.
3575 */
3576 txn->flags |= TX_SCK_PRESENT;
3577
3578 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3579 * check-cache is enabled) and we are not interested in checking
3580 * them. Warning, the cookie capture is declared in the frontend.
3581 */
3582 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3583 break;
3584
3585 /* OK so now we know we have to process this response cookie.
3586 * The format of the Set-Cookie header is slightly different
3587 * from the format of the Cookie header in that it does not
3588 * support the comma as a cookie delimiter (thus the header
3589 * cannot be folded) because the Expires attribute described in
3590 * the original Netscape's spec may contain an unquoted date
3591 * with a comma inside. We have to live with this because
3592 * many browsers don't support Max-Age and some browsers don't
3593 * support quoted strings. However the Set-Cookie2 header is
3594 * clean.
3595 *
3596 * We have to keep multiple pointers in order to support cookie
3597 * removal at the beginning, middle or end of header without
3598 * corrupting the header (in case of set-cookie2). A special
3599 * pointer, <scav> points to the beginning of the set-cookie-av
3600 * fields after the first semi-colon. The <next> pointer points
3601 * either to the end of line (set-cookie) or next unquoted comma
3602 * (set-cookie2). All of these headers are valid :
3603 *
3604 * hdr_beg hdr_end
3605 * | |
3606 * v |
3607 * NAME1 = VALUE 1 ; Secure; Path="/" |
3608 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3609 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3610 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3611 * | | | | | | | |
3612 * | | | | | | | +-> next
3613 * | | | | | | +------------> scav
3614 * | | | | | +--------------> val_end
3615 * | | | | +--------------------> val_beg
3616 * | | | +----------------------> equal
3617 * | | +------------------------> att_end
3618 * | +----------------------------> att_beg
3619 * +------------------------------> prev
3620 * -------------------------------> hdr_beg
3621 */
3622 hdr_beg = ctx.value.ptr;
3623 hdr_end = hdr_beg + ctx.value.len;
3624 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3625
3626 /* Iterate through all cookies on this line */
3627
3628 /* find att_beg */
3629 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003630 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003631 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003632 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003633
3634 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3635 att_beg++;
3636
3637 /* find att_end : this is the first character after the last non
3638 * space before the equal. It may be equal to hdr_end.
3639 */
3640 equal = att_end = att_beg;
3641
3642 while (equal < hdr_end) {
3643 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3644 break;
3645 if (HTTP_IS_SPHT(*equal++))
3646 continue;
3647 att_end = equal;
3648 }
3649
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003650 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003651 * is between <att_beg> and <equal>, both may be identical.
3652 */
3653
3654 /* look for end of cookie if there is an equal sign */
3655 if (equal < hdr_end && *equal == '=') {
3656 /* look for the beginning of the value */
3657 val_beg = equal + 1;
3658 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3659 val_beg++;
3660
3661 /* find the end of the value, respecting quotes */
3662 next = http_find_cookie_value_end(val_beg, hdr_end);
3663
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003664 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003665 val_end = next;
3666 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3667 val_end--;
3668 }
3669 else {
3670 /* <equal> points to next comma, semi-colon or EOL */
3671 val_beg = val_end = next = equal;
3672 }
3673
3674 if (next < hdr_end) {
3675 /* Set-Cookie2 supports multiple cookies, and <next> points to
3676 * a colon or semi-colon before the end. So skip all attr-value
3677 * pairs and look for the next comma. For Set-Cookie, since
3678 * commas are permitted in values, skip to the end.
3679 */
3680 if (is_cookie2)
3681 next = http_find_hdr_value_end(next, hdr_end);
3682 else
3683 next = hdr_end;
3684 }
3685
3686 /* Now everything is as on the diagram above */
3687
3688 /* Ignore cookies with no equal sign */
3689 if (equal == val_end)
3690 continue;
3691
3692 /* If there are spaces around the equal sign, we need to
3693 * strip them otherwise we'll get trouble for cookie captures,
3694 * or even for rewrites. Since this happens extremely rarely,
3695 * it does not hurt performance.
3696 */
3697 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3698 int stripped_before = 0;
3699 int stripped_after = 0;
3700
3701 if (att_end != equal) {
3702 memmove(att_end, equal, hdr_end - equal);
3703 stripped_before = (att_end - equal);
3704 equal += stripped_before;
3705 val_beg += stripped_before;
3706 }
3707
3708 if (val_beg > equal + 1) {
3709 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3710 stripped_after = (equal + 1) - val_beg;
3711 val_beg += stripped_after;
3712 stripped_before += stripped_after;
3713 }
3714
3715 val_end += stripped_before;
3716 next += stripped_before;
3717 hdr_end += stripped_before;
3718
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003719 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003720 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003721 }
3722
3723 /* First, let's see if we want to capture this cookie. We check
3724 * that we don't already have a server side cookie, because we
3725 * can only capture one. Also as an optimisation, we ignore
3726 * cookies shorter than the declared name.
3727 */
3728 if (sess->fe->capture_name != NULL &&
3729 txn->srv_cookie == NULL &&
3730 (val_end - att_beg >= sess->fe->capture_namelen) &&
3731 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3732 int log_len = val_end - att_beg;
3733 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3734 ha_alert("HTTP logging : out of memory.\n");
3735 }
3736 else {
3737 if (log_len > sess->fe->capture_len)
3738 log_len = sess->fe->capture_len;
3739 memcpy(txn->srv_cookie, att_beg, log_len);
3740 txn->srv_cookie[log_len] = 0;
3741 }
3742 }
3743
3744 srv = objt_server(s->target);
3745 /* now check if we need to process it for persistence */
3746 if (!(s->flags & SF_IGNORE_PRST) &&
3747 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3748 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3749 /* assume passive cookie by default */
3750 txn->flags &= ~TX_SCK_MASK;
3751 txn->flags |= TX_SCK_FOUND;
3752
3753 /* If the cookie is in insert mode on a known server, we'll delete
3754 * this occurrence because we'll insert another one later.
3755 * We'll delete it too if the "indirect" option is set and we're in
3756 * a direct access.
3757 */
3758 if (s->be->ck_opts & PR_CK_PSV) {
3759 /* The "preserve" flag was set, we don't want to touch the
3760 * server's cookie.
3761 */
3762 }
3763 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3764 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3765 /* this cookie must be deleted */
3766 if (prev == hdr_beg && next == hdr_end) {
3767 /* whole header */
3768 http_remove_header(htx, &ctx);
3769 /* note: while both invalid now, <next> and <hdr_end>
3770 * are still equal, so the for() will stop as expected.
3771 */
3772 } else {
3773 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003774 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003775 next = prev;
3776 hdr_end += delta;
3777 }
3778 txn->flags &= ~TX_SCK_MASK;
3779 txn->flags |= TX_SCK_DELETED;
3780 /* and go on with next cookie */
3781 }
3782 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3783 /* replace bytes val_beg->val_end with the cookie name associated
3784 * with this server since we know it.
3785 */
3786 int sliding, delta;
3787
3788 ctx.value = ist2(val_beg, val_end - val_beg);
3789 ctx.lws_before = ctx.lws_after = 0;
3790 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3791 delta = srv->cklen - (val_end - val_beg);
3792 sliding = (ctx.value.ptr - val_beg);
3793 hdr_beg += sliding;
3794 val_beg += sliding;
3795 next += sliding + delta;
3796 hdr_end += sliding + delta;
3797
3798 txn->flags &= ~TX_SCK_MASK;
3799 txn->flags |= TX_SCK_REPLACED;
3800 }
3801 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3802 /* insert the cookie name associated with this server
3803 * before existing cookie, and insert a delimiter between them..
3804 */
3805 int sliding, delta;
3806 ctx.value = ist2(val_beg, 0);
3807 ctx.lws_before = ctx.lws_after = 0;
3808 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3809 delta = srv->cklen + 1;
3810 sliding = (ctx.value.ptr - val_beg);
3811 hdr_beg += sliding;
3812 val_beg += sliding;
3813 next += sliding + delta;
3814 hdr_end += sliding + delta;
3815
3816 val_beg[srv->cklen] = COOKIE_DELIM;
3817 txn->flags &= ~TX_SCK_MASK;
3818 txn->flags |= TX_SCK_REPLACED;
3819 }
3820 }
3821 /* that's done for this cookie, check the next one on the same
3822 * line when next != hdr_end (only if is_cookie2).
3823 */
3824 }
3825 }
3826}
3827
Christopher Faulet25a02f62018-10-24 12:00:25 +02003828/*
3829 * Parses the Cache-Control and Pragma request header fields to determine if
3830 * the request may be served from the cache and/or if it is cacheable. Updates
3831 * s->txn->flags.
3832 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003833void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003834{
3835 struct http_txn *txn = s->txn;
3836 struct htx *htx;
3837 int32_t pos;
3838 int pragma_found, cc_found, i;
3839
3840 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3841 return; /* nothing more to do here */
3842
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003843 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003844 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02003845 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003846 struct htx_blk *blk = htx_get_blk(htx, pos);
3847 enum htx_blk_type type = htx_get_blk_type(blk);
3848 struct ist n, v;
3849
3850 if (type == HTX_BLK_EOH)
3851 break;
3852 if (type != HTX_BLK_HDR)
3853 continue;
3854
3855 n = htx_get_blk_name(htx, blk);
3856 v = htx_get_blk_value(htx, blk);
3857
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003858 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003859 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3860 pragma_found = 1;
3861 continue;
3862 }
3863 }
3864
3865 /* Don't use the cache and don't try to store if we found the
3866 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003867 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003868 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3869 txn->flags |= TX_CACHE_IGNORE;
3870 continue;
3871 }
3872
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003873 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02003874 continue;
3875
3876 /* OK, right now we know we have a cache-control header */
3877 cc_found = 1;
3878 if (!v.len) /* no info */
3879 continue;
3880
3881 i = 0;
3882 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
3883 !isspace((unsigned char)*(v.ptr+i)))
3884 i++;
3885
3886 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
3887 * values after max-age, max-stale nor min-fresh, we simply don't
3888 * use the cache when they're specified.
3889 */
3890 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
3891 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
3892 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
3893 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
3894 txn->flags |= TX_CACHE_IGNORE;
3895 continue;
3896 }
3897
3898 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
3899 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3900 continue;
3901 }
3902 }
3903
3904 /* RFC7234#5.4:
3905 * When the Cache-Control header field is also present and
3906 * understood in a request, Pragma is ignored.
3907 * When the Cache-Control header field is not present in a
3908 * request, caches MUST consider the no-cache request
3909 * pragma-directive as having the same effect as if
3910 * "Cache-Control: no-cache" were present.
3911 */
3912 if (!cc_found && pragma_found)
3913 txn->flags |= TX_CACHE_IGNORE;
3914}
3915
3916/*
3917 * Check if response is cacheable or not. Updates s->txn->flags.
3918 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003919void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003920{
3921 struct http_txn *txn = s->txn;
3922 struct htx *htx;
3923 int32_t pos;
3924 int i;
3925
3926 if (txn->status < 200) {
3927 /* do not try to cache interim responses! */
3928 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3929 return;
3930 }
3931
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003932 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02003933 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003934 struct htx_blk *blk = htx_get_blk(htx, pos);
3935 enum htx_blk_type type = htx_get_blk_type(blk);
3936 struct ist n, v;
3937
3938 if (type == HTX_BLK_EOH)
3939 break;
3940 if (type != HTX_BLK_HDR)
3941 continue;
3942
3943 n = htx_get_blk_name(htx, blk);
3944 v = htx_get_blk_value(htx, blk);
3945
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003946 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003947 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3948 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3949 return;
3950 }
3951 }
3952
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003953 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02003954 continue;
3955
3956 /* OK, right now we know we have a cache-control header */
3957 if (!v.len) /* no info */
3958 continue;
3959
3960 i = 0;
3961 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
3962 !isspace((unsigned char)*(v.ptr+i)))
3963 i++;
3964
3965 /* we have a complete value between v.ptr and (v.ptr+i) */
3966 if (i < v.len && *(v.ptr + i) == '=') {
3967 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
3968 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
3969 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3970 continue;
3971 }
3972
3973 /* we have something of the form no-cache="set-cookie" */
3974 if ((v.len >= 21) &&
3975 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
3976 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
3977 txn->flags &= ~TX_CACHE_COOK;
3978 continue;
3979 }
3980
3981 /* OK, so we know that either p2 points to the end of string or to a comma */
3982 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
3983 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
3984 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
3985 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3986 return;
3987 }
3988
3989 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
3990 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
3991 continue;
3992 }
3993 }
3994}
3995
Christopher Faulet377c5a52018-10-24 21:21:30 +02003996/*
3997 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3998 * for the current backend.
3999 *
4000 * It is assumed that the request is either a HEAD, GET, or POST and that the
4001 * uri_auth field is valid.
4002 *
4003 * Returns 1 if stats should be provided, otherwise 0.
4004 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004005static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004006{
4007 struct uri_auth *uri_auth = backend->uri_auth;
4008 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004009 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004010 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004011
4012 if (!uri_auth)
4013 return 0;
4014
4015 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4016 return 0;
4017
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004018 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004019 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004020 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01004021 if (*uri_auth->uri_prefix == '/')
4022 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004023
4024 /* check URI size */
4025 if (uri_auth->uri_len > uri.len)
4026 return 0;
4027
4028 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4029 return 0;
4030
4031 return 1;
4032}
4033
4034/* This function prepares an applet to handle the stats. It can deal with the
4035 * "100-continue" expectation, check that admin rules are met for POST requests,
4036 * and program a response message if something was unexpected. It cannot fail
4037 * and always relies on the stats applet to complete the job. It does not touch
4038 * analysers nor counters, which are left to the caller. It does not touch
4039 * s->target which is supposed to already point to the stats applet. The caller
4040 * is expected to have already assigned an appctx to the stream.
4041 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004042static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004043{
4044 struct stats_admin_rule *stats_admin_rule;
4045 struct stream_interface *si = &s->si[1];
4046 struct session *sess = s->sess;
4047 struct http_txn *txn = s->txn;
4048 struct http_msg *msg = &txn->req;
4049 struct uri_auth *uri_auth = s->be->uri_auth;
4050 const char *h, *lookup, *end;
4051 struct appctx *appctx;
4052 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004053 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004054
4055 appctx = si_appctx(si);
4056 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4057 appctx->st1 = appctx->st2 = 0;
4058 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004059 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004060 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4061 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4062 appctx->ctx.stats.flags |= STAT_CHUNKED;
4063
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004064 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004065 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004066 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4067 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004068
4069 for (h = lookup; h <= end - 3; h++) {
4070 if (memcmp(h, ";up", 3) == 0) {
4071 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4072 break;
4073 }
4074 }
4075
4076 if (uri_auth->refresh) {
4077 for (h = lookup; h <= end - 10; h++) {
4078 if (memcmp(h, ";norefresh", 10) == 0) {
4079 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4080 break;
4081 }
4082 }
4083 }
4084
4085 for (h = lookup; h <= end - 4; h++) {
4086 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004087 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004088 break;
4089 }
4090 }
4091
4092 for (h = lookup; h <= end - 6; h++) {
4093 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004094 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004095 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4096 break;
4097 }
4098 }
4099
Christopher Faulet6338a082019-09-09 15:50:54 +02004100 for (h = lookup; h <= end - 5; h++) {
4101 if (memcmp(h, ";json", 5) == 0) {
4102 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4103 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4104 break;
4105 }
4106 }
4107
4108 for (h = lookup; h <= end - 12; h++) {
4109 if (memcmp(h, ";json-schema", 12) == 0) {
4110 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4111 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4112 break;
4113 }
4114 }
4115
Christopher Faulet377c5a52018-10-24 21:21:30 +02004116 for (h = lookup; h <= end - 8; h++) {
4117 if (memcmp(h, ";st=", 4) == 0) {
4118 int i;
4119 h += 4;
4120 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4121 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4122 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4123 appctx->ctx.stats.st_code = i;
4124 break;
4125 }
4126 }
4127 break;
4128 }
4129 }
4130
4131 appctx->ctx.stats.scope_str = 0;
4132 appctx->ctx.stats.scope_len = 0;
4133 for (h = lookup; h <= end - 8; h++) {
4134 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4135 int itx = 0;
4136 const char *h2;
4137 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4138 const char *err;
4139
4140 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4141 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004142 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4143 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004144 if (*h == ';' || *h == '&' || *h == ' ')
4145 break;
4146 itx++;
4147 h++;
4148 }
4149
4150 if (itx > STAT_SCOPE_TXT_MAXLEN)
4151 itx = STAT_SCOPE_TXT_MAXLEN;
4152 appctx->ctx.stats.scope_len = itx;
4153
4154 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4155 memcpy(scope_txt, h2, itx);
4156 scope_txt[itx] = '\0';
4157 err = invalid_char(scope_txt);
4158 if (err) {
4159 /* bad char in search text => clear scope */
4160 appctx->ctx.stats.scope_str = 0;
4161 appctx->ctx.stats.scope_len = 0;
4162 }
4163 break;
4164 }
4165 }
4166
4167 /* now check whether we have some admin rules for this request */
4168 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4169 int ret = 1;
4170
4171 if (stats_admin_rule->cond) {
4172 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4173 ret = acl_pass(ret);
4174 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4175 ret = !ret;
4176 }
4177
4178 if (ret) {
4179 /* no rule, or the rule matches */
4180 appctx->ctx.stats.flags |= STAT_ADMIN;
4181 break;
4182 }
4183 }
4184
Christopher Faulet5d45e382019-02-27 15:15:23 +01004185 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4186 appctx->st0 = STAT_HTTP_HEAD;
4187 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004188 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004189 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004190 if (msg->msg_state < HTTP_MSG_DATA)
4191 req->analysers |= AN_REQ_HTTP_BODY;
4192 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004193 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004194 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004195 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4196 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4197 appctx->st0 = STAT_HTTP_LAST;
4198 }
4199 }
4200 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004201 /* Unsupported method */
4202 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4203 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4204 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004205 }
4206
4207 s->task->nice = -32; /* small boost for HTTP statistics */
4208 return 1;
4209}
4210
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004211void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004212{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004213 struct channel *req = &s->req;
4214 struct channel *res = &s->res;
4215 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004216 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004217 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004218 struct ist path, location;
4219 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004220
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004221 /*
4222 * Create the location
4223 */
4224 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004225
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004226 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004227 /* special prefix "/" means don't change URL */
4228 srv = __objt_server(s->target);
4229 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4230 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4231 return;
4232 }
4233
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004234 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004235 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004236 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004237 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004238 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004239 return;
4240
4241 if (!chunk_memcat(&trash, path.ptr, path.len))
4242 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004243 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004244
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004245 /*
4246 * Create the 302 respone
4247 */
4248 htx = htx_from_buf(&res->buf);
4249 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4250 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4251 ist("HTTP/1.1"), ist("302"), ist("Found"));
4252 if (!sl)
4253 goto fail;
4254 sl->info.res.status = 302;
4255 s->txn->status = 302;
4256
4257 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4258 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4259 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4260 !htx_add_header(htx, ist("Location"), location))
4261 goto fail;
4262
4263 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4264 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004265
Christopher Fauletc20afb82020-01-24 19:16:26 +01004266 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004267 if (!http_forward_proxy_resp(s, 1))
4268 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004269
4270 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004271 si_shutr(si);
4272 si_shutw(si);
4273 si->err_type = SI_ET_NONE;
4274 si->state = SI_ST_CLO;
4275
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004276 if (!(s->flags & SF_ERR_MASK))
4277 s->flags |= SF_ERR_LOCAL;
4278 if (!(s->flags & SF_FINST_MASK))
4279 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004280
4281 /* FIXME: we should increase a counter of redirects per server and per backend. */
4282 srv_inc_sess_ctr(srv);
4283 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004284 return;
4285
4286 fail:
4287 /* If an error occurred, remove the incomplete HTTP response from the
4288 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004289 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004290}
4291
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004292/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004293 * because an error was triggered during the body forwarding.
4294 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004295static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004296{
4297 struct channel *chn = &s->req;
4298 struct http_txn *txn = s->txn;
4299
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004300 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004301
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004302 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4303 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004304 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004305 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004306 goto end;
4307 }
4308
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004309 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4310 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004311 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004312 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004313
4314 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004315 /* No need to read anymore, the request was completely parsed.
4316 * We can shut the read side unless we want to abort_on_close,
4317 * or we have a POST request. The issue with POST requests is
4318 * that some browsers still send a CRLF after the request, and
4319 * this CRLF must be read so that it does not remain in the kernel
4320 * buffers, otherwise a close could cause an RST on some systems
4321 * (eg: Linux).
4322 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004323 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004324 channel_dont_read(chn);
4325
4326 /* if the server closes the connection, we want to immediately react
4327 * and close the socket to save packets and syscalls.
4328 */
4329 s->si[1].flags |= SI_FL_NOHALF;
4330
4331 /* In any case we've finished parsing the request so we must
4332 * disable Nagle when sending data because 1) we're not going
4333 * to shut this side, and 2) the server is waiting for us to
4334 * send pending data.
4335 */
4336 chn->flags |= CF_NEVER_WAIT;
4337
Christopher Fauletd01ce402019-01-02 17:44:13 +01004338 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4339 /* The server has not finished to respond, so we
4340 * don't want to move in order not to upset it.
4341 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004342 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004343 return;
4344 }
4345
Christopher Fauletf2824e62018-10-01 12:12:37 +02004346 /* When we get here, it means that both the request and the
4347 * response have finished receiving. Depending on the connection
4348 * mode, we'll have to wait for the last bytes to leave in either
4349 * direction, and sometimes for a close to be effective.
4350 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004351 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004352 /* Tunnel mode will not have any analyser so it needs to
4353 * poll for reads.
4354 */
4355 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004356 if (b_data(&chn->buf)) {
4357 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004358 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004359 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004360 txn->req.msg_state = HTTP_MSG_TUNNEL;
4361 }
4362 else {
4363 /* we're not expecting any new data to come for this
4364 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004365 *
4366 * However, there is an exception if the response
4367 * length is undefined. In this case, we need to wait
4368 * the close from the server. The response will be
4369 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004370 */
4371 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4372 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004373 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004374
4375 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4376 channel_shutr_now(chn);
4377 channel_shutw_now(chn);
4378 }
4379 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004380 goto check_channel_flags;
4381 }
4382
4383 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4384 http_msg_closing:
4385 /* nothing else to forward, just waiting for the output buffer
4386 * to be empty and for the shutw_now to take effect.
4387 */
4388 if (channel_is_empty(chn)) {
4389 txn->req.msg_state = HTTP_MSG_CLOSED;
4390 goto http_msg_closed;
4391 }
4392 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004393 txn->req.msg_state = HTTP_MSG_ERROR;
4394 goto end;
4395 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004396 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004397 return;
4398 }
4399
4400 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4401 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004402 /* if we don't know whether the server will close, we need to hard close */
4403 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4404 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004405 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004406 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004407 channel_dont_read(chn);
4408 goto end;
4409 }
4410
4411 check_channel_flags:
4412 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4413 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4414 /* if we've just closed an output, let's switch */
4415 txn->req.msg_state = HTTP_MSG_CLOSING;
4416 goto http_msg_closing;
4417 }
4418
4419 end:
4420 chn->analysers &= AN_REQ_FLT_END;
4421 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4422 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4423 channel_auto_close(chn);
4424 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004425 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004426}
4427
4428
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004429/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004430 * because an error was triggered during the body forwarding.
4431 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004432static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004433{
4434 struct channel *chn = &s->res;
4435 struct http_txn *txn = s->txn;
4436
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004437 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004438
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004439 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4440 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004441 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004442 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004443 goto end;
4444 }
4445
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004446 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4447 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004448 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004449 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004450
4451 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4452 /* In theory, we don't need to read anymore, but we must
4453 * still monitor the server connection for a possible close
4454 * while the request is being uploaded, so we don't disable
4455 * reading.
4456 */
4457 /* channel_dont_read(chn); */
4458
4459 if (txn->req.msg_state < HTTP_MSG_DONE) {
4460 /* The client seems to still be sending data, probably
4461 * because we got an error response during an upload.
4462 * We have the choice of either breaking the connection
4463 * or letting it pass through. Let's do the later.
4464 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004465 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004466 return;
4467 }
4468
4469 /* When we get here, it means that both the request and the
4470 * response have finished receiving. Depending on the connection
4471 * mode, we'll have to wait for the last bytes to leave in either
4472 * direction, and sometimes for a close to be effective.
4473 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004474 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004475 channel_auto_read(chn);
4476 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004477 if (b_data(&chn->buf)) {
4478 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004479 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004480 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004481 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4482 }
4483 else {
4484 /* we're not expecting any new data to come for this
4485 * transaction, so we can close it.
4486 */
4487 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4488 channel_shutr_now(chn);
4489 channel_shutw_now(chn);
4490 }
4491 }
4492 goto check_channel_flags;
4493 }
4494
4495 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4496 http_msg_closing:
4497 /* nothing else to forward, just waiting for the output buffer
4498 * to be empty and for the shutw_now to take effect.
4499 */
4500 if (channel_is_empty(chn)) {
4501 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4502 goto http_msg_closed;
4503 }
4504 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004505 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004506 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004507 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004508 if (strm_sess(s)->listener->counters)
4509 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004510 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004511 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004512 goto end;
4513 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004514 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004515 return;
4516 }
4517
4518 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4519 http_msg_closed:
4520 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004521 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004522 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004523 goto end;
4524 }
4525
4526 check_channel_flags:
4527 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4528 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4529 /* if we've just closed an output, let's switch */
4530 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4531 goto http_msg_closing;
4532 }
4533
4534 end:
4535 chn->analysers &= AN_RES_FLT_END;
4536 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4537 chn->analysers |= AN_RES_FLT_XFER_DATA;
4538 channel_auto_close(chn);
4539 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004540 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004541}
4542
Christopher Fauletef70e252020-01-28 09:26:19 +01004543/* Forward a response generated by HAProxy (error/redirect/return). This
4544 * function forwards all pending incoming data. If <final> is set to 0, nothing
4545 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004546 * transaction is terminated and the request is emptied. On success 1 is
4547 * returned. If an error occurred, 0 is returned.
Christopher Fauletef70e252020-01-28 09:26:19 +01004548 */
4549int http_forward_proxy_resp(struct stream *s, int final)
4550{
4551 struct channel *req = &s->req;
4552 struct channel *res = &s->res;
4553 struct htx *htx = htxbuf(&res->buf);
4554 size_t data;
4555
4556 if (final) {
4557 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004558
4559 if (!http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004560 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004561
4562 channel_auto_read(req);
4563 channel_abort(req);
4564 channel_auto_close(req);
4565 channel_htx_erase(req, htxbuf(&req->buf));
4566
4567 res->wex = tick_add_ifset(now_ms, res->wto);
4568 channel_auto_read(res);
4569 channel_auto_close(res);
4570 channel_shutr_now(res);
4571 }
4572
4573 data = htx->data - co_data(res);
4574 c_adv(res, data);
4575 htx->first = -1;
4576 res->total += data;
4577 return 1;
4578}
4579
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004580void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004581 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004582{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004583 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004584 if (!(s->flags & SF_ERR_MASK))
4585 s->flags |= err;
4586 if (!(s->flags & SF_FINST_MASK))
4587 s->flags |= finst;
4588}
4589
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004590void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004591{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004592 if (!msg) {
4593 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4594 goto end;
4595 }
4596
4597 if (http_reply_message(s, msg) == -1) {
4598 /* On error, return a 500 error message, but don't rewrite it if
4599 * it is already an internal error.
4600 */
4601 if (s->txn->status == 500)
4602 s->txn->flags |= TX_CONST_REPLY;
4603 s->txn->status = 500;
4604 s->txn->http_reply = NULL;
4605 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4606 }
4607
4608end:
4609 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
4610 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
4611
Christopher Faulet0f226952018-10-22 09:29:56 +02004612 channel_auto_read(&s->req);
4613 channel_abort(&s->req);
4614 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004615 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004616 channel_auto_read(&s->res);
4617 channel_auto_close(&s->res);
4618 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004619}
4620
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004621struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004622{
4623 const int msgnum = http_get_status_idx(s->txn->status);
4624
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004625 if (s->txn->http_reply)
4626 return s->txn->http_reply;
4627 else if (s->be->replies[msgnum])
4628 return s->be->replies[msgnum];
4629 else if (strm_fe(s)->replies[msgnum])
4630 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004631 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004632 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004633}
4634
Christopher Faulet97e466c2020-05-15 15:12:47 +02004635/* Produces an HTX message from an http reply. Depending on the http reply type, a,
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004636 * errorfile, an raw file or a log-format string is used. On success, it returns
4637 * 0. If an error occurs -1 is returned.
4638 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004639int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004640{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004641 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004642 struct htx_sl *sl;
4643 struct buffer *body = NULL;
4644 const char *status, *reason, *clen, *ctype;
4645 unsigned int slflags;
4646 int ret = 0;
4647
Christopher Faulete29a97e2020-05-14 14:49:25 +02004648 /*
4649 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4650 *
4651 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4652 * as no payload if NULL. the TXN status code is set with the status
4653 * of the original reply.
4654 */
4655
4656 if (reply->type == HTTP_REPLY_INDIRECT) {
4657 if (reply->body.reply)
4658 reply = reply->body.reply;
4659 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004660 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4661 /* get default error message */
4662 if (reply == s->txn->http_reply)
4663 s->txn->http_reply = NULL;
4664 reply = http_error_message(s);
4665 if (reply->type == HTTP_REPLY_INDIRECT) {
4666 if (reply->body.reply)
4667 reply = reply->body.reply;
4668 }
4669 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004670
4671 if (reply->type == HTTP_REPLY_ERRMSG) {
4672 /* implicit or explicit error message*/
4673 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004674 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004675 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004676 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004677 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004678 }
4679 else {
4680 /* no payload, file or log-format string */
4681 if (reply->type == HTTP_REPLY_RAW) {
4682 /* file */
4683 body = &reply->body.obj;
4684 }
4685 else if (reply->type == HTTP_REPLY_LOGFMT) {
4686 /* log-format string */
4687 body = alloc_trash_chunk();
4688 if (!body)
4689 goto fail_alloc;
4690 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4691 }
4692 /* else no payload */
4693
4694 status = ultoa(reply->status);
4695 reason = http_get_reason(reply->status);
4696 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4697 if (!body || !b_data(body))
4698 slflags |= HTX_SL_F_BODYLESS;
4699 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4700 if (!sl)
4701 goto fail;
4702 sl->info.res.status = reply->status;
4703
4704 clen = (body ? ultoa(b_data(body)) : "0");
4705 ctype = reply->ctype;
4706
4707 if (!LIST_ISEMPTY(&reply->hdrs)) {
4708 struct http_reply_hdr *hdr;
4709 struct buffer *value = alloc_trash_chunk();
4710
4711 if (!value)
4712 goto fail;
4713
4714 list_for_each_entry(hdr, &reply->hdrs, list) {
4715 chunk_reset(value);
4716 value->data = build_logline(s, value->area, value->size, &hdr->value);
4717 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4718 free_trash_chunk(value);
4719 goto fail;
4720 }
4721 chunk_reset(value);
4722 }
4723 free_trash_chunk(value);
4724 }
4725
4726 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4727 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4728 !htx_add_endof(htx, HTX_BLK_EOH) ||
4729 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))) ||
4730 !htx_add_endof(htx, HTX_BLK_EOM))
4731 goto fail;
4732 }
4733
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004734 leave:
4735 if (reply->type == HTTP_REPLY_LOGFMT)
4736 free_trash_chunk(body);
4737 return ret;
4738
4739 fail_alloc:
4740 if (!(s->flags & SF_ERR_MASK))
4741 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004742 /* fall through */
4743 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004744 ret = -1;
4745 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004746}
4747
4748/* Send an http reply to the client. On success, it returns 0. If an error
4749 * occurs -1 is returned.
4750 */
4751int http_reply_message(struct stream *s, struct http_reply *reply)
4752{
4753 struct channel *res = &s->res;
4754 struct htx *htx = htx_from_buf(&res->buf);
4755
4756 if (s->txn->status == -1)
4757 s->txn->status = reply->status;
4758 channel_htx_truncate(res, htx);
4759
4760 if (http_reply_to_htx(s, htx, reply) == -1)
4761 goto fail;
4762
4763 htx_to_buf(htx, &s->res.buf);
4764 if (!http_forward_proxy_resp(s, 1))
4765 goto fail;
4766 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004767
4768 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004769 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004770 if (!(s->flags & SF_ERR_MASK))
4771 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004772 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004773}
4774
Christopher Faulet304cc402019-07-15 15:46:28 +02004775/* Return the error message corresponding to si->err_type. It is assumed
4776 * that the server side is closed. Note that err_type is actually a
4777 * bitmask, where almost only aborts may be cumulated with other
4778 * values. We consider that aborted operations are more important
4779 * than timeouts or errors due to the fact that nobody else in the
4780 * logs might explain incomplete retries. All others should avoid
4781 * being cumulated. It should normally not be possible to have multiple
4782 * aborts at once, but just in case, the first one in sequence is reported.
4783 * Note that connection errors appearing on the second request of a keep-alive
4784 * connection are not reported since this allows the client to retry.
4785 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004786void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004787{
4788 int err_type = si->err_type;
4789
4790 /* set s->txn->status for http_error_message(s) */
4791 s->txn->status = 503;
4792
4793 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004794 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4795 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004796 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004797 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4798 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4799 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004800 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004801 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4802 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004803 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004804 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4805 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004806 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004807 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4808 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4809 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004810 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004811 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4812 (s->flags & SF_SRV_REUSED) ? NULL :
4813 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004814 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004815 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4816 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4817 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004818 else { /* SI_ET_CONN_OTHER and others */
4819 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004820 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4821 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004822 }
4823}
4824
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004825
Christopher Faulet4a28a532019-03-01 11:19:40 +01004826/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4827 * on success and -1 on error.
4828 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004829static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004830{
4831 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4832 * then we must send an HTTP/1.1 100 Continue intermediate response.
4833 */
4834 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4835 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4836 struct ist hdr = { .ptr = "Expect", .len = 6 };
4837 struct http_hdr_ctx ctx;
4838
4839 ctx.blk = NULL;
4840 /* Expect is allowed in 1.1, look for it */
4841 if (http_find_header(htx, hdr, &ctx, 0) &&
4842 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004843 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004844 return -1;
4845 http_remove_header(htx, &ctx);
4846 }
4847 }
4848 return 0;
4849}
4850
Christopher Faulet23a3c792018-11-28 10:01:23 +01004851/* Send a 100-Continue response to the client. It returns 0 on success and -1
4852 * on error. The response channel is updated accordingly.
4853 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004854static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004855{
4856 struct channel *res = &s->res;
4857 struct htx *htx = htx_from_buf(&res->buf);
4858 struct htx_sl *sl;
4859 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4860 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004861
4862 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4863 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4864 if (!sl)
4865 goto fail;
4866 sl->info.res.status = 100;
4867
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004868 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004869 goto fail;
4870
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004871 if (!http_forward_proxy_resp(s, 0))
4872 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004873 return 0;
4874
4875 fail:
4876 /* If an error occurred, remove the incomplete HTTP response from the
4877 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004878 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004879 return -1;
4880}
4881
Christopher Faulet12c51e22018-11-28 15:59:42 +01004882
Christopher Faulet0f226952018-10-22 09:29:56 +02004883/*
4884 * Capture headers from message <htx> according to header list <cap_hdr>, and
4885 * fill the <cap> pointers appropriately.
4886 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004887static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004888{
4889 struct cap_hdr *h;
4890 int32_t pos;
4891
Christopher Fauleta3f15502019-05-13 15:27:23 +02004892 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004893 struct htx_blk *blk = htx_get_blk(htx, pos);
4894 enum htx_blk_type type = htx_get_blk_type(blk);
4895 struct ist n, v;
4896
4897 if (type == HTX_BLK_EOH)
4898 break;
4899 if (type != HTX_BLK_HDR)
4900 continue;
4901
4902 n = htx_get_blk_name(htx, blk);
4903
4904 for (h = cap_hdr; h; h = h->next) {
4905 if (h->namelen && (h->namelen == n.len) &&
4906 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4907 if (cap[h->index] == NULL)
4908 cap[h->index] =
4909 pool_alloc(h->pool);
4910
4911 if (cap[h->index] == NULL) {
4912 ha_alert("HTTP capture : out of memory.\n");
4913 break;
4914 }
4915
4916 v = htx_get_blk_value(htx, blk);
4917 if (v.len > h->len)
4918 v.len = h->len;
4919
4920 memcpy(cap[h->index], v.ptr, v.len);
4921 cap[h->index][v.len]=0;
4922 }
4923 }
4924 }
4925}
4926
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004927/* Delete a value in a header between delimiters <from> and <next>. The header
4928 * itself is delimited by <start> and <end> pointers. The number of characters
4929 * displaced is returned, and the pointer to the first delimiter is updated if
4930 * required. The function tries as much as possible to respect the following
4931 * principles :
4932 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4933 * in which case <next> is simply removed
4934 * - set exactly one space character after the new first delimiter, unless there
4935 * are not enough characters in the block being moved to do so.
4936 * - remove unneeded spaces before the previous delimiter and after the new
4937 * one.
4938 *
4939 * It is the caller's responsibility to ensure that :
4940 * - <from> points to a valid delimiter or <start> ;
4941 * - <next> points to a valid delimiter or <end> ;
4942 * - there are non-space chars before <from>.
4943 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004944static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004945{
4946 char *prev = *from;
4947
4948 if (prev == start) {
4949 /* We're removing the first value. eat the semicolon, if <next>
4950 * is lower than <end> */
4951 if (next < end)
4952 next++;
4953
4954 while (next < end && HTTP_IS_SPHT(*next))
4955 next++;
4956 }
4957 else {
4958 /* Remove useless spaces before the old delimiter. */
4959 while (HTTP_IS_SPHT(*(prev-1)))
4960 prev--;
4961 *from = prev;
4962
4963 /* copy the delimiter and if possible a space if we're
4964 * not at the end of the line.
4965 */
4966 if (next < end) {
4967 *prev++ = *next++;
4968 if (prev + 1 < next)
4969 *prev++ = ' ';
4970 while (next < end && HTTP_IS_SPHT(*next))
4971 next++;
4972 }
4973 }
4974 memmove(prev, next, end - next);
4975 return (prev - next);
4976}
4977
Christopher Faulet0f226952018-10-22 09:29:56 +02004978
4979/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004980 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004981 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004982static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004983{
4984 struct ist dst = ist2(str, 0);
4985
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004986 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004987 goto end;
4988 if (dst.len + 1 > len)
4989 goto end;
4990 dst.ptr[dst.len++] = ' ';
4991
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004992 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004993 goto end;
4994 if (dst.len + 1 > len)
4995 goto end;
4996 dst.ptr[dst.len++] = ' ';
4997
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004998 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004999 end:
5000 return dst.len;
5001}
5002
5003/*
5004 * Print a debug line with a start line.
5005 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005006static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005007{
5008 struct session *sess = strm_sess(s);
5009 int max;
5010
5011 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5012 dir,
5013 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5014 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5015
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005016 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005017 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005018 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005019 trash.area[trash.data++] = ' ';
5020
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005021 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005022 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005023 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005024 trash.area[trash.data++] = ' ';
5025
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005026 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005027 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005028 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005029 trash.area[trash.data++] = '\n';
5030
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005031 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005032}
5033
5034/*
5035 * Print a debug line with a header.
5036 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005037static 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 +02005038{
5039 struct session *sess = strm_sess(s);
5040 int max;
5041
5042 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5043 dir,
5044 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5045 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5046
5047 max = n.len;
5048 UBOUND(max, trash.size - trash.data - 3);
5049 chunk_memcat(&trash, n.ptr, max);
5050 trash.area[trash.data++] = ':';
5051 trash.area[trash.data++] = ' ';
5052
5053 max = v.len;
5054 UBOUND(max, trash.size - trash.data - 1);
5055 chunk_memcat(&trash, v.ptr, max);
5056 trash.area[trash.data++] = '\n';
5057
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005058 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005059}
5060
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005061/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5062 * In case of allocation failure, everything allocated is freed and NULL is
5063 * returned. Otherwise the new transaction is assigned to the stream and
5064 * returned.
5065 */
5066struct http_txn *http_alloc_txn(struct stream *s)
5067{
5068 struct http_txn *txn = s->txn;
5069
5070 if (txn)
5071 return txn;
5072
5073 txn = pool_alloc(pool_head_http_txn);
5074 if (!txn)
5075 return txn;
5076
5077 s->txn = txn;
5078 return txn;
5079}
5080
5081void http_txn_reset_req(struct http_txn *txn)
5082{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005083 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005084 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5085}
5086
5087void http_txn_reset_res(struct http_txn *txn)
5088{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005089 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005090 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5091}
5092
5093/*
5094 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5095 * the required fields are properly allocated and that we only need to (re)init
5096 * them. This should be used before processing any new request.
5097 */
5098void http_init_txn(struct stream *s)
5099{
5100 struct http_txn *txn = s->txn;
5101 struct conn_stream *cs = objt_cs(s->si[0].end);
5102
5103 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5104 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5105 : 0);
5106 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005107 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005108 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005109
5110 txn->cookie_first_date = 0;
5111 txn->cookie_last_date = 0;
5112
5113 txn->srv_cookie = NULL;
5114 txn->cli_cookie = NULL;
5115 txn->uri = NULL;
5116
5117 http_txn_reset_req(txn);
5118 http_txn_reset_res(txn);
5119
5120 txn->req.chn = &s->req;
5121 txn->rsp.chn = &s->res;
5122
5123 txn->auth.method = HTTP_AUTH_UNKNOWN;
5124
5125 vars_init(&s->vars_txn, SCOPE_TXN);
5126 vars_init(&s->vars_reqres, SCOPE_REQ);
5127}
5128
5129/* to be used at the end of a transaction */
5130void http_end_txn(struct stream *s)
5131{
5132 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005133
5134 /* these ones will have been dynamically allocated */
5135 pool_free(pool_head_requri, txn->uri);
5136 pool_free(pool_head_capture, txn->cli_cookie);
5137 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005138 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005139
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005140 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005141 txn->uri = NULL;
5142 txn->srv_cookie = NULL;
5143 txn->cli_cookie = NULL;
5144
Christopher Faulet59399252019-11-07 14:27:52 +01005145 if (!LIST_ISEMPTY(&s->vars_txn.head))
5146 vars_prune(&s->vars_txn, s->sess, s);
5147 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5148 vars_prune(&s->vars_reqres, s->sess, s);
5149}
5150
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005151
5152DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005153
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005154__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005155static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005156{
5157}
5158
5159
5160/*
5161 * Local variables:
5162 * c-indent-level: 8
5163 * c-basic-offset: 8
5164 * End:
5165 */