blob: e268d4c951bb86a66032cca8b5c2e214ef231cfc [file] [log] [blame]
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Christopher Faulete0768eb2018-10-03 16:38:02 +020013#include <common/base64.h>
14#include <common/config.h>
15#include <common/debug.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010016#include <common/htx.h>
Willy Tarreau8b507582020-02-25 09:35:07 +010017#include <common/net_helper.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020018#include <common/uri_auth.h>
19
Christopher Faulet0f226952018-10-22 09:29:56 +020020#include <types/capture.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020021
22#include <proto/acl.h>
Christopher Faulet3e964192018-10-24 11:39:23 +020023#include <proto/action.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020024#include <proto/channel.h>
25#include <proto/checks.h>
26#include <proto/connection.h>
27#include <proto/filters.h>
Christopher Faulet0f226952018-10-22 09:29:56 +020028#include <proto/http_htx.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020029#include <proto/log.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020030#include <proto/http_ana.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020031#include <proto/proxy.h>
Christopher Fauletfefc73d2018-10-24 21:18:04 +020032#include <proto/server.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020033#include <proto/stream.h>
34#include <proto/stream_interface.h>
35#include <proto/stats.h>
Christopher Fauleta8a46e22019-07-16 14:53:09 +020036#include <proto/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020037
Christopher Fauleteea8fc72019-11-05 16:18:10 +010038#define TRACE_SOURCE &trace_strm
39
Christopher Faulet377c5a52018-10-24 21:21:30 +020040extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020041
Christopher Fauleta8a46e22019-07-16 14:53:09 +020042struct pool_head *pool_head_requri = NULL;
43struct pool_head *pool_head_capture = NULL;
44
45
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020046static void http_end_request(struct stream *s);
47static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020048
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020049static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
50static int http_del_hdr_value(char *start, char *end, char **from, char *next);
51static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020052static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
53static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
Christopher Faulet0f226952018-10-22 09:29:56 +020054
Christopher Fauletb58f62b2020-01-13 16:40:13 +010055static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020056static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020057
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020058static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
59static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020060
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020061static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
62static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020063
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020064static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
65static int http_reply_100_continue(struct stream *s);
66static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm);
Christopher Faulet23a3c792018-11-28 10:01:23 +010067
Christopher Faulete0768eb2018-10-03 16:38:02 +020068/* This stream analyser waits for a complete HTTP request. It returns 1 if the
69 * processing can continue on next analysers, or zero if it either needs more
70 * data or wants to immediately abort the request (eg: timeout, error, ...). It
71 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
72 * when it has nothing left to do, and may remove any analyser when it wants to
73 * abort.
74 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020075int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020076{
Christopher Faulet9768c262018-10-22 09:34:31 +020077
Christopher Faulete0768eb2018-10-03 16:38:02 +020078 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020079 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020080 *
Christopher Faulet9768c262018-10-22 09:34:31 +020081 * Once the start line and all headers are received, we may perform a
82 * capture of the error (if any), and we will set a few fields. We also
83 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020084 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020085 struct session *sess = s->sess;
86 struct http_txn *txn = s->txn;
87 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020088 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010089 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020090
Christopher Fauleteea8fc72019-11-05 16:18:10 +010091 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020092
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010093 htx = htxbuf(&req->buf);
Christopher Faulet9768c262018-10-22 09:34:31 +020094
Willy Tarreau4236f032019-03-05 10:43:32 +010095 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +020096 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +010097 stream_inc_http_req_ctr(s);
98 stream_inc_http_err_ctr(s);
99 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200100 if (htx->flags & HTX_FL_PARSING_ERROR)
101 goto return_bad_req;
102 else
103 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100104 }
105
Christopher Faulete0768eb2018-10-03 16:38:02 +0200106 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200107 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200108
109 /* If there is data available for analysis, log the end of the idle time. */
Christopher Faulet870aad92018-11-29 15:23:46 +0100110 if (c_data(req) && s->logs.t_idle == -1) {
111 const struct cs_info *csinfo = si_get_cs_info(objt_cs(s->si[0].end));
112
113 s->logs.t_idle = ((csinfo)
114 ? csinfo->t_idle
115 : tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake);
116 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200117
Christopher Faulete0768eb2018-10-03 16:38:02 +0200118 /*
119 * Now we quickly check if we have found a full valid request.
120 * If not so, we check the FD and buffer states before leaving.
121 * A full request is indicated by the fact that we have seen
122 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
123 * requests are checked first. When waiting for a second request
124 * on a keep-alive stream, if we encounter and error, close, t/o,
125 * we note the error in the stream flags but don't set any state.
126 * Since the error will be noted there, it will not be counted by
127 * process_stream() as a frontend error.
128 * Last, we may increase some tracked counters' http request errors on
129 * the cases that are deliberately the client's fault. For instance,
130 * a timeout or connection reset is not counted as an error. However
131 * a bad request is.
132 */
Christopher Faulet29f17582019-05-23 11:03:26 +0200133 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200134 if (htx->flags & HTX_FL_UPGRADE)
135 goto failed_keep_alive;
136
Christopher Faulet9768c262018-10-22 09:34:31 +0200137 /* 1: have we encountered a read error ? */
138 if (req->flags & CF_READ_ERROR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200139 if (!(s->flags & SF_ERR_MASK))
140 s->flags |= SF_ERR_CLICL;
141
142 if (txn->flags & TX_WAIT_NEXT_RQ)
143 goto failed_keep_alive;
144
145 if (sess->fe->options & PR_O_IGNORE_PRB)
146 goto failed_keep_alive;
147
Christopher Faulet9768c262018-10-22 09:34:31 +0200148 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200149 stream_inc_http_req_ctr(s);
150 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100151 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200152 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100153 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200154
Christopher Faulet9768c262018-10-22 09:34:31 +0200155 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200156 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet9768c262018-10-22 09:34:31 +0200157 req->analysers &= AN_REQ_FLT_END;
158
Christopher Faulete0768eb2018-10-03 16:38:02 +0200159 if (!(s->flags & SF_FINST_MASK))
160 s->flags |= SF_FINST_R;
161 return 0;
162 }
163
Christopher Faulet9768c262018-10-22 09:34:31 +0200164 /* 2: has the read timeout expired ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200165 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
166 if (!(s->flags & SF_ERR_MASK))
167 s->flags |= SF_ERR_CLITO;
168
169 if (txn->flags & TX_WAIT_NEXT_RQ)
170 goto failed_keep_alive;
171
172 if (sess->fe->options & PR_O_IGNORE_PRB)
173 goto failed_keep_alive;
174
Christopher Faulet9768c262018-10-22 09:34:31 +0200175 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200176 stream_inc_http_req_ctr(s);
177 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100178 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200179 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100180 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200181
Christopher Faulet9768c262018-10-22 09:34:31 +0200182 txn->status = 408;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200183 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200184 req->analysers &= AN_REQ_FLT_END;
185
Christopher Faulete0768eb2018-10-03 16:38:02 +0200186 if (!(s->flags & SF_FINST_MASK))
187 s->flags |= SF_FINST_R;
188 return 0;
189 }
190
Christopher Faulet9768c262018-10-22 09:34:31 +0200191 /* 3: have we encountered a close ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200192 else if (req->flags & CF_SHUTR) {
193 if (!(s->flags & SF_ERR_MASK))
194 s->flags |= SF_ERR_CLICL;
195
196 if (txn->flags & TX_WAIT_NEXT_RQ)
197 goto failed_keep_alive;
198
199 if (sess->fe->options & PR_O_IGNORE_PRB)
200 goto failed_keep_alive;
201
Christopher Faulete0768eb2018-10-03 16:38:02 +0200202 stream_inc_http_err_ctr(s);
203 stream_inc_http_req_ctr(s);
204 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100205 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200206 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100207 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200208
Christopher Faulet9768c262018-10-22 09:34:31 +0200209 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200210 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200211 req->analysers &= AN_REQ_FLT_END;
212
Christopher Faulete0768eb2018-10-03 16:38:02 +0200213 if (!(s->flags & SF_FINST_MASK))
214 s->flags |= SF_FINST_R;
215 return 0;
216 }
217
218 channel_dont_connect(req);
219 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
220 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100221
Christopher Faulet9768c262018-10-22 09:34:31 +0200222 if (sess->listener->options & LI_O_NOQUICKACK && htx_is_not_empty(htx) &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200223 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
224 /* We need more data, we have to re-enable quick-ack in case we
225 * previously disabled it, otherwise we might cause the client
226 * to delay next data.
227 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100228 conn_set_quickack(objt_conn(sess->origin), 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200229 }
Willy Tarreau1a18b542018-12-11 16:37:42 +0100230
Christopher Faulet47365272018-10-31 17:40:50 +0100231 if ((req->flags & CF_READ_PARTIAL) && (txn->flags & TX_WAIT_NEXT_RQ)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200232 /* If the client starts to talk, let's fall back to
233 * request timeout processing.
234 */
235 txn->flags &= ~TX_WAIT_NEXT_RQ;
236 req->analyse_exp = TICK_ETERNITY;
237 }
238
239 /* just set the request timeout once at the beginning of the request */
240 if (!tick_isset(req->analyse_exp)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100241 if ((txn->flags & TX_WAIT_NEXT_RQ) && tick_isset(s->be->timeout.httpka))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200242 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
243 else
244 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
245 }
246
247 /* we're not ready yet */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100248 DBG_TRACE_DEVEL("waiting for the request",
249 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200250 return 0;
251
252 failed_keep_alive:
253 /* Here we process low-level errors for keep-alive requests. In
254 * short, if the request is not the first one and it experiences
255 * a timeout, read error or shutdown, we just silently close so
256 * that the client can try again.
257 */
258 txn->status = 0;
259 msg->msg_state = HTTP_MSG_RQBEFORE;
260 req->analysers &= AN_REQ_FLT_END;
261 s->logs.logwait = 0;
262 s->logs.level = 0;
263 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200264 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100265 DBG_TRACE_DEVEL("leaving by closing K/A connection",
266 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200267 return 0;
268 }
269
Christopher Faulet9768c262018-10-22 09:34:31 +0200270 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200271 stream_inc_http_req_ctr(s);
272 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
273
Christopher Faulet9768c262018-10-22 09:34:31 +0200274 /* kill the pending keep-alive timeout */
275 txn->flags &= ~TX_WAIT_NEXT_RQ;
276 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200277
Christopher Faulet29f17582019-05-23 11:03:26 +0200278 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200279 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100280
Christopher Faulet9768c262018-10-22 09:34:31 +0200281 /* 0: we might have to print this header in debug mode */
282 if (unlikely((global.mode & MODE_DEBUG) &&
283 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
284 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200285
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200286 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200287
Christopher Fauleta3f15502019-05-13 15:27:23 +0200288 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200289 struct htx_blk *blk = htx_get_blk(htx, pos);
290 enum htx_blk_type type = htx_get_blk_type(blk);
291
292 if (type == HTX_BLK_EOH)
293 break;
294 if (type != HTX_BLK_HDR)
295 continue;
296
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200297 http_debug_hdr("clihdr", s,
298 htx_get_blk_name(htx, blk),
299 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200300 }
301 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200302
303 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100304 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200305 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100306 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100307 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200308 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100309 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +0100310 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100311 if (sl->flags & HTX_SL_F_BODYLESS)
312 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200313
314 /* we can make use of server redirect on GET and HEAD */
315 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
316 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100317 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200318 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200319 goto return_bad_req;
320 }
321
322 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100323 * 2: check if the URI matches the monitor_uri. We have to do this for
324 * every request which gets in, because the monitor-uri is defined by
325 * the frontend. If the monitor-uri starts with a '/', the matching is
326 * done against the request's path. Otherwise, the request's uri is
327 * used. It is a workaround to let HTTP/2 health-checks work as
328 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200329 */
330 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Faulet6072beb2020-02-18 15:34:58 +0100331 ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)),
332 ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) ||
333 isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200334 /*
335 * We have found the monitor URI
336 */
337 struct acl_cond *cond;
338
339 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100340 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200341
342 /* Check if we want to fail this monitor request or not */
343 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
344 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
345
346 ret = acl_pass(ret);
347 if (cond->pol == ACL_COND_UNLESS)
348 ret = !ret;
349
350 if (ret) {
351 /* we fail this request, let's return 503 service unavail */
352 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200353 if (!(s->flags & SF_ERR_MASK))
354 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
355 goto return_prx_cond;
356 }
357 }
358
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800359 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200360 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200361 if (!(s->flags & SF_ERR_MASK))
362 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
363 goto return_prx_cond;
364 }
365
366 /*
367 * 3: Maybe we have to copy the original REQURI for the logs ?
368 * Note: we cannot log anymore if the request has been
369 * classified as invalid.
370 */
371 if (unlikely(s->logs.logwait & LW_REQ)) {
372 /* we have a complete HTTP request that we must log */
373 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200374 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200375
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200376 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200377 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200378
379 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
380 s->do_log(s);
381 } else {
382 ha_alert("HTTP logging : out of memory.\n");
383 }
384 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200385
Christopher Faulete0768eb2018-10-03 16:38:02 +0200386 /* if the frontend has "option http-use-proxy-header", we'll check if
387 * we have what looks like a proxied connection instead of a connection,
388 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
389 * Note that this is *not* RFC-compliant, however browsers and proxies
390 * happen to do that despite being non-standard :-(
391 * We consider that a request not beginning with either '/' or '*' is
392 * a proxied connection, which covers both "scheme://location" and
393 * CONNECT ip:port.
394 */
395 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100396 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200397 txn->flags |= TX_USE_PX_CONN;
398
Christopher Faulete0768eb2018-10-03 16:38:02 +0200399 /* 5: we may need to capture headers */
400 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200401 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200402
Christopher Faulete0768eb2018-10-03 16:38:02 +0200403 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200404 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200405 req->analysers |= AN_REQ_HTTP_BODY;
406
407 /*
408 * RFC7234#4:
409 * A cache MUST write through requests with methods
410 * that are unsafe (Section 4.2.1 of [RFC7231]) to
411 * the origin server; i.e., a cache is not allowed
412 * to generate a reply to such a request before
413 * having forwarded the request and having received
414 * a corresponding response.
415 *
416 * RFC7231#4.2.1:
417 * Of the request methods defined by this
418 * specification, the GET, HEAD, OPTIONS, and TRACE
419 * methods are defined to be safe.
420 */
421 if (likely(txn->meth == HTTP_METH_GET ||
422 txn->meth == HTTP_METH_HEAD ||
423 txn->meth == HTTP_METH_OPTIONS ||
424 txn->meth == HTTP_METH_TRACE))
425 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
426
427 /* end of job, return OK */
428 req->analysers &= ~an_bit;
429 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200430
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100431 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200432 return 1;
433
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200434 return_int_err:
435 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200436 if (!(s->flags & SF_ERR_MASK))
437 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100438 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200439 if (sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100440 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200441 goto return_prx_cond;
442
Christopher Faulete0768eb2018-10-03 16:38:02 +0200443 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200444 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100445 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200446 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100447 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200448 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200449
450 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200451 http_reply_and_close(s, txn->status, http_error_message(s));
452
Christopher Faulete0768eb2018-10-03 16:38:02 +0200453 if (!(s->flags & SF_ERR_MASK))
454 s->flags |= SF_ERR_PRXCOND;
455 if (!(s->flags & SF_FINST_MASK))
456 s->flags |= SF_FINST_R;
457
458 req->analysers &= AN_REQ_FLT_END;
459 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100460 DBG_TRACE_DEVEL("leaving on error",
461 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200462 return 0;
463}
464
465
466/* This stream analyser runs all HTTP request processing which is common to
467 * frontends and backends, which means blocking ACLs, filters, connection-close,
468 * reqadd, stats and redirects. This is performed for the designated proxy.
469 * It returns 1 if the processing can continue on next analysers, or zero if it
470 * either needs more data or wants to immediately abort the request (eg: deny,
471 * error, ...).
472 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200473int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200474{
475 struct session *sess = s->sess;
476 struct http_txn *txn = s->txn;
477 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200478 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200479 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200480 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200481 struct connection *conn = objt_conn(sess->origin);
482
483 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
484 /* we need more data */
485 goto return_prx_yield;
486 }
487
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100488 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200489
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100490 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200491
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200492 /* just in case we have some per-backend tracking. Only called the first
493 * execution of the analyser. */
494 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
495 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200496
497 /* evaluate http-request rules */
498 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100499 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200500
501 switch (verdict) {
502 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
503 goto return_prx_yield;
504
505 case HTTP_RULE_RES_CONT:
506 case HTTP_RULE_RES_STOP: /* nothing to do */
507 break;
508
509 case HTTP_RULE_RES_DENY: /* deny or tarpit */
510 if (txn->flags & TX_CLTARPIT)
511 goto tarpit;
512 goto deny;
513
514 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
515 goto return_prx_cond;
516
517 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
518 goto done;
519
520 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
521 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100522
523 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
524 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200525 }
526 }
527
528 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100529 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200530 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200531
Christopher Fauletff2759f2018-10-24 11:13:16 +0200532 ctx.blk = NULL;
533 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
534 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100535 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200536 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200537 }
538
539 /* OK at this stage, we know that the request was accepted according to
540 * the http-request rules, we can check for the stats. Note that the
541 * URI is detected *before* the req* rules in order not to be affected
542 * by a possible reqrep, while they are processed *after* so that a
543 * reqdeny can still block them. This clearly needs to change in 1.6!
544 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200545 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200546 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100547 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200548 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200549 if (!(s->flags & SF_ERR_MASK))
550 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100551 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200552 }
553
554 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200555 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100556 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200557 /* not all actions implemented: deny, allow, auth */
558
559 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
560 goto deny;
561
562 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
563 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100564
565 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
566 goto return_bad_req;
567
568 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
569 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200570 }
571
Christopher Faulet2571bc62019-03-01 11:44:26 +0100572 /* Proceed with the applets now. */
573 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200574 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100575 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200576
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200577 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100578 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100579
Christopher Faulete0768eb2018-10-03 16:38:02 +0200580 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
581 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
582 if (!(s->flags & SF_FINST_MASK))
583 s->flags |= SF_FINST_R;
584
585 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
586 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
587 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
588 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100589
590 req->flags |= CF_SEND_DONTWAIT;
591 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200592 goto done;
593 }
594
595 /* check whether we have some ACLs set to redirect this request */
596 list_for_each_entry(rule, &px->redirect_rules, list) {
597 if (rule->cond) {
598 int ret;
599
600 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
601 ret = acl_pass(ret);
602 if (rule->cond->pol == ACL_COND_UNLESS)
603 ret = !ret;
604 if (!ret)
605 continue;
606 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200607 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100608 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200609 goto done;
610 }
611
612 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
613 * If this happens, then the data will not come immediately, so we must
614 * send all what we have without waiting. Note that due to the small gain
615 * in waiting for the body of the request, it's easier to simply put the
616 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
617 * itself once used.
618 */
619 req->flags |= CF_SEND_DONTWAIT;
620
621 done: /* done with this analyser, continue with next ones that the calling
622 * points will have set, if any.
623 */
624 req->analyse_exp = TICK_ETERNITY;
625 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
626 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100627 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200628 return 1;
629
630 tarpit:
631 /* Allow cookie logging
632 */
633 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200634 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200635
636 /* When a connection is tarpitted, we use the tarpit timeout,
637 * which may be the same as the connect timeout if unspecified.
638 * If unset, then set it to zero because we really want it to
639 * eventually expire. We build the tarpit as an analyser.
640 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100641 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200642
643 /* wipe the request out so that we can drop the connection early
644 * if the client closes first.
645 */
646 channel_dont_connect(req);
647
Christopher Faulete0768eb2018-10-03 16:38:02 +0200648 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
649 req->analysers |= AN_REQ_HTTP_TARPIT;
650 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
651 if (!req->analyse_exp)
652 req->analyse_exp = tick_add(now_ms, 0);
653 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100654 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100655 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100656 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200657 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100658 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200659 goto done_without_exp;
660
661 deny: /* this request was blocked (denied) */
662
663 /* Allow cookie logging
664 */
665 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200666 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200667
Christopher Faulete0768eb2018-10-03 16:38:02 +0200668 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200669 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100670 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100671 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100672 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200673 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100674 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100675 goto return_prx_err;
676
677 return_int_err:
678 txn->status = 500;
679 if (!(s->flags & SF_ERR_MASK))
680 s->flags |= SF_ERR_INTERNAL;
681 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100682 if (s->flags & SF_BE_ASSIGNED)
683 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100684 if (sess->listener->counters)
685 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
686 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200687
688 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200689 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100690 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200691 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100692 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100693 /* fall through */
694
695 return_prx_err:
696 http_reply_and_close(s, txn->status, http_error_message(s));
697 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200698
699 return_prx_cond:
700 if (!(s->flags & SF_ERR_MASK))
701 s->flags |= SF_ERR_PRXCOND;
702 if (!(s->flags & SF_FINST_MASK))
703 s->flags |= SF_FINST_R;
704
705 req->analysers &= AN_REQ_FLT_END;
706 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100707 DBG_TRACE_DEVEL("leaving on error",
708 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200709 return 0;
710
711 return_prx_yield:
712 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100713 DBG_TRACE_DEVEL("waiting for more data",
714 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200715 return 0;
716}
717
718/* This function performs all the processing enabled for the current request.
719 * It returns 1 if the processing can continue on next analysers, or zero if it
720 * needs more data, encounters an error, or wants to immediately abort the
721 * request. It relies on buffers flags, and updates s->req.analysers.
722 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200723int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200724{
725 struct session *sess = s->sess;
726 struct http_txn *txn = s->txn;
727 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200728 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200729 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
730
731 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
732 /* we need more data */
733 channel_dont_connect(req);
734 return 0;
735 }
736
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100737 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200738
739 /*
740 * Right now, we know that we have processed the entire headers
741 * and that unwanted requests have been filtered out. We can do
742 * whatever we want with the remaining request. Also, now we
743 * may have separate values for ->fe, ->be.
744 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100745 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200746
747 /*
748 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200749 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200750 */
751 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100752 struct htx_sl *sl;
753 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200754
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200755 if (!sockaddr_alloc(&s->target_addr)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200756 if (!(s->flags & SF_ERR_MASK))
757 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100758 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200759 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200760 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100761 uri = htx_sl_req_uri(sl);
762 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200763
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200764 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200765 goto return_bad_req;
766
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200767 s->target = &s->be->obj_type;
768 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
769
Christopher Faulete0768eb2018-10-03 16:38:02 +0200770 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200771 * uri.ptr and path.ptr (excluded). If it was not found, we need
772 * to replace from all the uri by a single "/".
773 *
774 * Instead of rewritting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100775 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200776 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200777 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100778 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200779 }
780
781 /*
782 * 7: Now we can work with the cookies.
783 * Note that doing so might move headers in the request, but
784 * the fields will stay coherent and the URI will not move.
785 * This should only be performed in the backend.
786 */
787 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200788 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200789
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100790 /* 8: Generate unique ID if a "unique-id-format" is defined.
791 *
792 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
793 * fetches only available in the HTTP request processing stage.
794 */
795 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
796 int length;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200797
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100798 if ((length = stream_generate_unique_id(s, &sess->fe->format_unique_id)) < 0) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100799 if (!(s->flags & SF_ERR_MASK))
800 s->flags |= SF_ERR_RESOURCE;
801 goto return_int_err;
802 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200803
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100804 /* send unique ID if a "unique-id-header" is defined */
805 if (sess->fe->header_unique_id) {
806 struct ist n, v;
807 n = ist2(sess->fe->header_unique_id, strlen(sess->fe->header_unique_id));
808 v = ist2(s->unique_id, length);
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200809
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100810 if (unlikely(!http_add_header(htx, n, v)))
811 goto return_int_err;
812 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200813 }
814
815 /*
816 * 9: add X-Forwarded-For if either the frontend or the backend
817 * asks for it.
818 */
819 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200820 struct http_hdr_ctx ctx = { .blk = NULL };
821 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
822 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
823
Christopher Faulete0768eb2018-10-03 16:38:02 +0200824 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200825 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200826 /* The header is set to be added only if none is present
827 * and we found it, so don't do anything.
828 */
829 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200830 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200831 /* Add an X-Forwarded-For header unless the source IP is
832 * in the 'except' network range.
833 */
834 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200835 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200836 != sess->fe->except_net.s_addr) &&
837 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200838 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200839 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200840 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200841
842 /* Note: we rely on the backend to get the header name to be used for
843 * x-forwarded-for, because the header is really meant for the backends.
844 * However, if the backend did not specify any option, we have to rely
845 * on the frontend's header name.
846 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200847 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
848 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100849 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200850 }
851 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200852 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200853 /* FIXME: for the sake of completeness, we should also support
854 * 'except' here, although it is mostly useless in this case.
855 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200856 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200857
Christopher Faulete0768eb2018-10-03 16:38:02 +0200858 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200859 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200860 pn, sizeof(pn));
861
862 /* Note: we rely on the backend to get the header name to be used for
863 * x-forwarded-for, because the header is really meant for the backends.
864 * However, if the backend did not specify any option, we have to rely
865 * on the frontend's header name.
866 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200867 chunk_printf(&trash, "%s", pn);
868 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100869 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200870 }
871 }
872
873 /*
874 * 10: add X-Original-To if either the frontend or the backend
875 * asks for it.
876 */
877 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
878
879 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200880 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 +0200881 /* Add an X-Original-To header unless the destination IP is
882 * in the 'except' network range.
883 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200884 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200885 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200886 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200887 != sess->fe->except_to.s_addr) &&
888 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200889 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200890 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200891 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200892 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200893
894 /* Note: we rely on the backend to get the header name to be used for
895 * x-original-to, because the header is really meant for the backends.
896 * However, if the backend did not specify any option, we have to rely
897 * on the frontend's header name.
898 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200899 if (s->be->orgto_hdr_len)
900 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
901 else
902 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200903
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200904 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
905 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100906 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200907 }
908 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200909 }
910
Christopher Faulete0768eb2018-10-03 16:38:02 +0200911 /* If we have no server assigned yet and we're balancing on url_param
912 * with a POST request, we may be interested in checking the body for
913 * that parameter. This will be done in another analyser.
914 */
915 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100916 s->txn->meth == HTTP_METH_POST &&
917 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200918 channel_dont_connect(req);
919 req->analysers |= AN_REQ_HTTP_BODY;
920 }
921
922 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
923 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100924
Christopher Faulete0768eb2018-10-03 16:38:02 +0200925 /* We expect some data from the client. Unless we know for sure
926 * we already have a full request, we have to re-enable quick-ack
927 * in case we previously disabled it, otherwise we might cause
928 * the client to delay further data.
929 */
930 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200931 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100932 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200933
934 /*************************************************************
935 * OK, that's finished for the headers. We have done what we *
936 * could. Let's switch to the DATA state. *
937 ************************************************************/
938 req->analyse_exp = TICK_ETERNITY;
939 req->analysers &= ~an_bit;
940
941 s->logs.tv_request = now;
942 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100943 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200944 return 1;
945
Christopher Fauletb8a53712019-12-16 11:29:38 +0100946 return_int_err:
947 txn->status = 500;
948 if (!(s->flags & SF_ERR_MASK))
949 s->flags |= SF_ERR_INTERNAL;
950 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100951 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100952 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100953 if (sess->listener->counters)
954 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
955 goto return_prx_cond;
956
Christopher Faulete0768eb2018-10-03 16:38:02 +0200957 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200958 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100959 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200960 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100961 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100962 /* fall through */
963
964 return_prx_cond:
965 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200966
967 if (!(s->flags & SF_ERR_MASK))
968 s->flags |= SF_ERR_PRXCOND;
969 if (!(s->flags & SF_FINST_MASK))
970 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100971
972 req->analysers &= AN_REQ_FLT_END;
973 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100974 DBG_TRACE_DEVEL("leaving on error",
975 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200976 return 0;
977}
978
979/* This function is an analyser which processes the HTTP tarpit. It always
980 * returns zero, at the beginning because it prevents any other processing
981 * from occurring, and at the end because it terminates the request.
982 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200983int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200984{
985 struct http_txn *txn = s->txn;
986
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100987 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200988 /* This connection is being tarpitted. The CLIENT side has
989 * already set the connect expiration date to the right
990 * timeout. We just have to check that the client is still
991 * there and that the timeout has not expired.
992 */
993 channel_dont_connect(req);
994 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100995 !tick_is_expired(req->analyse_exp, now_ms)) {
996 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
997 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200998 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100999 }
1000
Christopher Faulete0768eb2018-10-03 16:38:02 +02001001
1002 /* We will set the queue timer to the time spent, just for
1003 * logging purposes. We fake a 500 server error, so that the
1004 * attacker will not suspect his connection has been tarpitted.
1005 * It will not cause trouble to the logs because we can exclude
1006 * the tarpitted connections by filtering on the 'PT' status flags.
1007 */
1008 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1009
Christopher Faulet9d9d6452020-02-21 10:20:46 +01001010 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001011
1012 req->analysers &= AN_REQ_FLT_END;
1013 req->analyse_exp = TICK_ETERNITY;
1014
1015 if (!(s->flags & SF_ERR_MASK))
1016 s->flags |= SF_ERR_PRXCOND;
1017 if (!(s->flags & SF_FINST_MASK))
1018 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001019
1020 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001021 return 0;
1022}
1023
1024/* This function is an analyser which waits for the HTTP request body. It waits
1025 * for either the buffer to be full, or the full advertised contents to have
1026 * reached the buffer. It must only be called after the standard HTTP request
1027 * processing has occurred, because it expects the request to be parsed and will
1028 * look for the Expect header. It may send a 100-Continue interim response. It
1029 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
1030 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
1031 * needs to read more data, or 1 once it has completed its analysis.
1032 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001033int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001034{
1035 struct session *sess = s->sess;
1036 struct http_txn *txn = s->txn;
1037 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001038 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001039
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001040 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001041
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001042 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001043
Willy Tarreau4236f032019-03-05 10:43:32 +01001044 if (htx->flags & HTX_FL_PARSING_ERROR)
1045 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001046 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1047 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001048
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001049 if (msg->msg_state < HTTP_MSG_BODY)
1050 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001051
Christopher Faulete0768eb2018-10-03 16:38:02 +02001052 /* We have to parse the HTTP request body to find any required data.
1053 * "balance url_param check_post" should have been the only way to get
1054 * into this. We were brought here after HTTP header analysis, so all
1055 * related structures are ready.
1056 */
1057
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001058 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001059 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001060 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001061 }
1062
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001063 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001064
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001065 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1066 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001067 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001068 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001069 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001070 goto http_end;
1071
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001072 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001073 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1074 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001075 if (!(s->flags & SF_ERR_MASK))
1076 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001077 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1078 if (sess->listener->counters)
1079 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1080 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001081 }
1082
1083 /* we get here if we need to wait for more data */
1084 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1085 /* Not enough data. We'll re-use the http-request
1086 * timeout here. Ideally, we should set the timeout
1087 * relative to the accept() date. We just set the
1088 * request timeout once at the beginning of the
1089 * request.
1090 */
1091 channel_dont_connect(req);
1092 if (!tick_isset(req->analyse_exp))
1093 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001094 DBG_TRACE_DEVEL("waiting for more data",
1095 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001096 return 0;
1097 }
1098
1099 http_end:
1100 /* The situation will not evolve, so let's give up on the analysis. */
1101 s->logs.tv_request = now; /* update the request timer to reflect full request */
1102 req->analysers &= ~an_bit;
1103 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001104 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001105 return 1;
1106
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001107 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001108 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001109 if (!(s->flags & SF_ERR_MASK))
1110 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001111 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001112 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +01001113 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001114 if (sess->listener->counters)
1115 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
1116 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001117
Christopher Faulete0768eb2018-10-03 16:38:02 +02001118 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001119 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001120 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1121 if (sess->listener->counters)
1122 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1123 /* fall through */
1124
1125 return_prx_cond:
1126 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001127
1128 if (!(s->flags & SF_ERR_MASK))
1129 s->flags |= SF_ERR_PRXCOND;
1130 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001131 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001132
Christopher Faulete0768eb2018-10-03 16:38:02 +02001133 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001134 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001135 DBG_TRACE_DEVEL("leaving on error",
1136 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001137 return 0;
1138}
1139
1140/* This function is an analyser which forwards request body (including chunk
1141 * sizes if any). It is called as soon as we must forward, even if we forward
1142 * zero byte. The only situation where it must not be called is when we're in
1143 * tunnel mode and we want to forward till the close. It's used both to forward
1144 * remaining data and to resync after end of body. It expects the msg_state to
1145 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1146 * read more data, or 1 once we can go on with next request or end the stream.
1147 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1148 * bytes of pending data + the headers if not already done.
1149 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001150int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001151{
1152 struct session *sess = s->sess;
1153 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001154 struct http_msg *msg = &txn->req;
1155 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001156 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001157 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001158
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001159 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001160
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001161 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001162
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001163 if (htx->flags & HTX_FL_PARSING_ERROR)
1164 goto return_bad_req;
1165 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1166 goto return_int_err;
1167
Christopher Faulete0768eb2018-10-03 16:38:02 +02001168 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1169 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1170 /* Output closed while we were sending data. We must abort and
1171 * wake the other side up.
1172 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001173
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001174 /* Don't abort yet if we had L7 retries activated and it
1175 * was a write error, we may recover.
1176 */
1177 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001178 (s->si[1].flags & SI_FL_L7_RETRY)) {
1179 DBG_TRACE_DEVEL("leaving on L7 retry",
1180 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001181 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001182 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001183 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001184 http_end_request(s);
1185 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001186 DBG_TRACE_DEVEL("leaving on error",
1187 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001188 return 1;
1189 }
1190
1191 /* Note that we don't have to send 100-continue back because we don't
1192 * need the data to complete our job, and it's up to the server to
1193 * decide whether to return 100, 417 or anything else in return of
1194 * an "Expect: 100-continue" header.
1195 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001196 if (msg->msg_state == HTTP_MSG_BODY)
1197 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001198
Christopher Faulete0768eb2018-10-03 16:38:02 +02001199 /* in most states, we should abort in case of early close */
1200 channel_auto_close(req);
1201
1202 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001203 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001204 if (req->flags & CF_EOI)
1205 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001206 }
1207 else {
1208 /* We can't process the buffer's contents yet */
1209 req->flags |= CF_WAKE_WRITE;
1210 goto missing_data_or_waiting;
1211 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001212 }
1213
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001214 if (msg->msg_state >= HTTP_MSG_ENDING)
1215 goto ending;
1216
1217 if (txn->meth == HTTP_METH_CONNECT) {
1218 msg->msg_state = HTTP_MSG_ENDING;
1219 goto ending;
1220 }
1221
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001222 /* Forward input data. We get it by removing all outgoing data not
1223 * forwarded yet from HTX data size. If there are some data filters, we
1224 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001225 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001226 if (HAS_REQ_DATA_FILTERS(s)) {
1227 ret = flt_http_payload(s, msg, htx->data);
1228 if (ret < 0)
1229 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001230 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001231 }
1232 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001233 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001234 if (msg->flags & HTTP_MSGF_XFER_LEN)
1235 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001236 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001237
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001238 if (htx->data != co_data(req))
1239 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001240
Christopher Faulet9768c262018-10-22 09:34:31 +02001241 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001242 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1243 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001244 */
1245 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1246 goto missing_data_or_waiting;
1247
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001248 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001249
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001250 ending:
1251 /* other states, ENDING...TUNNEL */
1252 if (msg->msg_state >= HTTP_MSG_DONE)
1253 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001254
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001255 if (HAS_REQ_DATA_FILTERS(s)) {
1256 ret = flt_http_end(s, msg);
1257 if (ret <= 0) {
1258 if (!ret)
1259 goto missing_data_or_waiting;
1260 goto return_bad_req;
1261 }
1262 }
1263
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001264 if (txn->meth == HTTP_METH_CONNECT)
1265 msg->msg_state = HTTP_MSG_TUNNEL;
1266 else {
1267 msg->msg_state = HTTP_MSG_DONE;
1268 req->to_forward = 0;
1269 }
1270
1271 done:
1272 /* we don't want to forward closes on DONE except in tunnel mode. */
1273 if (!(txn->flags & TX_CON_WANT_TUN))
1274 channel_dont_close(req);
1275
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001276 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001277 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001278 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001279 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1280 if (req->flags & CF_SHUTW) {
1281 /* request errors are most likely due to the
1282 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001283 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001284 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001285 goto return_bad_req;
1286 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001287 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001288 return 1;
1289 }
1290
1291 /* If "option abortonclose" is set on the backend, we want to monitor
1292 * the client's connection and forward any shutdown notification to the
1293 * server, which will decide whether to close or to go on processing the
1294 * request. We only do that in tunnel mode, and not in other modes since
1295 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001296 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001297 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001298 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001299 s->si[1].flags |= SI_FL_NOLINGER;
1300 channel_auto_close(req);
1301 }
1302 else if (s->txn->meth == HTTP_METH_POST) {
1303 /* POST requests may require to read extra CRLF sent by broken
1304 * browsers and which could cause an RST to be sent upon close
1305 * on some systems (eg: Linux). */
1306 channel_auto_read(req);
1307 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001308 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1309 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001310 return 0;
1311
1312 missing_data_or_waiting:
1313 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001314 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001315 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001316
1317 waiting:
1318 /* waiting for the last bits to leave the buffer */
1319 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001320 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001321
1322 /* When TE: chunked is used, we need to get there again to parse remaining
1323 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1324 * And when content-length is used, we never want to let the possible
1325 * shutdown be forwarded to the other side, as the state machine will
1326 * take care of it once the client responds. It's also important to
1327 * prevent TIME_WAITs from accumulating on the backend side, and for
1328 * HTTP/2 where the last frame comes with a shutdown.
1329 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001330 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001331 channel_dont_close(req);
1332
1333 /* We know that more data are expected, but we couldn't send more that
1334 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1335 * system knows it must not set a PUSH on this first part. Interactive
1336 * modes are already handled by the stream sock layer. We must not do
1337 * this in content-length mode because it could present the MSG_MORE
1338 * flag with the last block of forwarded data, which would cause an
1339 * additional delay to be observed by the receiver.
1340 */
1341 if (msg->flags & HTTP_MSGF_TE_CHNK)
1342 req->flags |= CF_EXPECT_MORE;
1343
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001344 DBG_TRACE_DEVEL("waiting for more data to forward",
1345 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001346 return 0;
1347
Christopher Faulet93e02d82019-03-08 14:18:50 +01001348 return_cli_abort:
1349 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1350 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001351 if (sess->listener->counters)
1352 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001353 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001354 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001355 if (!(s->flags & SF_ERR_MASK))
1356 s->flags |= SF_ERR_CLICL;
1357 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001358 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001359
1360 return_srv_abort:
1361 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1362 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001363 if (sess->listener->counters)
1364 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001365 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001366 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001367 if (!(s->flags & SF_ERR_MASK))
1368 s->flags |= SF_ERR_SRVCL;
1369 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001370 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001371
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001372 return_int_err:
1373 if (!(s->flags & SF_ERR_MASK))
1374 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001375 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001376 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001377 if (sess->listener->counters)
1378 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001379 if (objt_server(s->target))
1380 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001381 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001382 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001383
Christopher Faulet93e02d82019-03-08 14:18:50 +01001384 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001385 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001386 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001387 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001388 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001389 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001390
Christopher Fauletb8a53712019-12-16 11:29:38 +01001391 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001392 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001393 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001394 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001395 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001396 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001397 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001398 }
1399 req->analysers &= AN_REQ_FLT_END;
1400 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 +01001401 if (!(s->flags & SF_ERR_MASK))
1402 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001403 if (!(s->flags & SF_FINST_MASK))
1404 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001405 DBG_TRACE_DEVEL("leaving on error ",
1406 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001407 return 0;
1408}
1409
Olivier Houcharda254a372019-04-05 15:30:12 +02001410/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1411/* Returns 0 if we can attempt to retry, -1 otherwise */
1412static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1413{
1414 struct channel *req, *res;
1415 int co_data;
1416
1417 si->conn_retries--;
1418 if (si->conn_retries < 0)
1419 return -1;
1420
Willy Tarreau223995e2019-05-04 10:38:31 +02001421 if (objt_server(s->target))
1422 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1423 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1424
Olivier Houcharda254a372019-04-05 15:30:12 +02001425 req = &s->req;
1426 res = &s->res;
1427 /* Remove any write error from the request, and read error from the response */
1428 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1429 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1430 res->analysers = 0;
1431 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001432 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001433 si->exp = TICK_ETERNITY;
1434 res->rex = TICK_ETERNITY;
1435 res->to_forward = 0;
1436 res->analyse_exp = TICK_ETERNITY;
1437 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001438 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001439 si_release_endpoint(&s->si[1]);
1440 b_free(&req->buf);
1441 /* Swap the L7 buffer with the channel buffer */
1442 /* We know we stored the co_data as b_data, so get it there */
1443 co_data = b_data(&si->l7_buffer);
1444 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1445 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1446
1447 co_set_data(req, co_data);
1448 b_reset(&res->buf);
1449 co_set_data(res, 0);
1450 return 0;
1451}
1452
Christopher Faulete0768eb2018-10-03 16:38:02 +02001453/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1454 * processing can continue on next analysers, or zero if it either needs more
1455 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1456 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1457 * when it has nothing left to do, and may remove any analyser when it wants to
1458 * abort.
1459 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001460int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001461{
Christopher Faulet9768c262018-10-22 09:34:31 +02001462 /*
1463 * We will analyze a complete HTTP response to check the its syntax.
1464 *
1465 * Once the start line and all headers are received, we may perform a
1466 * capture of the error (if any), and we will set a few fields. We also
1467 * logging and finally headers capture.
1468 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001469 struct session *sess = s->sess;
1470 struct http_txn *txn = s->txn;
1471 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001472 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001473 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001474 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001475 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001476 int n;
1477
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001478 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001479
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001480 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001481
Willy Tarreau4236f032019-03-05 10:43:32 +01001482 /* Parsing errors are caught here */
1483 if (htx->flags & HTX_FL_PARSING_ERROR)
1484 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001485 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1486 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001487
Christopher Faulete0768eb2018-10-03 16:38:02 +02001488 /*
1489 * Now we quickly check if we have found a full valid response.
1490 * If not so, we check the FD and buffer states before leaving.
1491 * A full response is indicated by the fact that we have seen
1492 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1493 * responses are checked first.
1494 *
1495 * Depending on whether the client is still there or not, we
1496 * may send an error response back or not. Note that normally
1497 * we should only check for HTTP status there, and check I/O
1498 * errors somewhere else.
1499 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001500 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001501 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001502 /* 1: have we encountered a read error ? */
1503 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001504 struct connection *conn = NULL;
1505
Olivier Houchard865d8392019-05-03 22:46:27 +02001506 if (objt_cs(s->si[1].end))
1507 conn = objt_cs(s->si[1].end)->conn;
1508
1509 if (si_b->flags & SI_FL_L7_RETRY &&
1510 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001511 /* If we arrive here, then CF_READ_ERROR was
1512 * set by si_cs_recv() because we matched a
1513 * status, overwise it would have removed
1514 * the SI_FL_L7_RETRY flag, so it's ok not
1515 * to check s->be->retry_type.
1516 */
1517 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1518 return 0;
1519 }
1520
Olivier Houchard6db16992019-05-17 15:40:49 +02001521 if (txn->flags & TX_NOT_FIRST)
1522 goto abort_keep_alive;
1523
Olivier Houcharda798bf52019-03-08 18:52:00 +01001524 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001525 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001526 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001527 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001528 }
1529
Christopher Faulete0768eb2018-10-03 16:38:02 +02001530 rep->analysers &= AN_RES_FLT_END;
1531 txn->status = 502;
1532
1533 /* Check to see if the server refused the early data.
1534 * If so, just send a 425
1535 */
Olivier Houchard865d8392019-05-03 22:46:27 +02001536 if (conn->err_code == CO_ER_SSL_EARLY_FAILED) {
1537 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001538 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001539 do_l7_retry(s, si_b) == 0) {
1540 DBG_TRACE_DEVEL("leaving on L7 retry",
1541 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001542 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001543 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001544 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001545 }
1546
1547 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001548 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001549
1550 if (!(s->flags & SF_ERR_MASK))
1551 s->flags |= SF_ERR_SRVCL;
1552 if (!(s->flags & SF_FINST_MASK))
1553 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001554 DBG_TRACE_DEVEL("leaving on error",
1555 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001556 return 0;
1557 }
1558
Christopher Faulet9768c262018-10-22 09:34:31 +02001559 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001560 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001561 if ((si_b->flags & SI_FL_L7_RETRY) &&
1562 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001563 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1564 DBG_TRACE_DEVEL("leaving on L7 retry",
1565 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001566 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001567 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001568 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001569 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001570 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001571 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001572 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001573 }
1574
Christopher Faulete0768eb2018-10-03 16:38:02 +02001575 rep->analysers &= AN_RES_FLT_END;
1576 txn->status = 504;
1577 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001578 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001579
1580 if (!(s->flags & SF_ERR_MASK))
1581 s->flags |= SF_ERR_SRVTO;
1582 if (!(s->flags & SF_FINST_MASK))
1583 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001584 DBG_TRACE_DEVEL("leaving on error",
1585 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001586 return 0;
1587 }
1588
Christopher Faulet9768c262018-10-22 09:34:31 +02001589 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001590 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001591 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1592 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001593 if (sess->listener->counters)
1594 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001595 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001596 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001597
1598 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001599 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001600 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001601
1602 if (!(s->flags & SF_ERR_MASK))
1603 s->flags |= SF_ERR_CLICL;
1604 if (!(s->flags & SF_FINST_MASK))
1605 s->flags |= SF_FINST_H;
1606
1607 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001608 DBG_TRACE_DEVEL("leaving on error",
1609 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001610 return 0;
1611 }
1612
Christopher Faulet9768c262018-10-22 09:34:31 +02001613 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001614 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001615 if ((si_b->flags & SI_FL_L7_RETRY) &&
1616 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001617 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1618 DBG_TRACE_DEVEL("leaving on L7 retry",
1619 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001620 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001621 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001622 }
1623
Olivier Houchard6db16992019-05-17 15:40:49 +02001624 if (txn->flags & TX_NOT_FIRST)
1625 goto abort_keep_alive;
1626
Olivier Houcharda798bf52019-03-08 18:52:00 +01001627 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001628 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001629 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001630 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001631 }
1632
Christopher Faulete0768eb2018-10-03 16:38:02 +02001633 rep->analysers &= AN_RES_FLT_END;
1634 txn->status = 502;
1635 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001636 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001637
1638 if (!(s->flags & SF_ERR_MASK))
1639 s->flags |= SF_ERR_SRVCL;
1640 if (!(s->flags & SF_FINST_MASK))
1641 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001642 DBG_TRACE_DEVEL("leaving on error",
1643 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001644 return 0;
1645 }
1646
Christopher Faulet9768c262018-10-22 09:34:31 +02001647 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001648 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001649 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001650 goto abort_keep_alive;
1651
Olivier Houcharda798bf52019-03-08 18:52:00 +01001652 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001653 if (objt_server(s->target))
1654 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001655 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001656
1657 if (!(s->flags & SF_ERR_MASK))
1658 s->flags |= SF_ERR_CLICL;
1659 if (!(s->flags & SF_FINST_MASK))
1660 s->flags |= SF_FINST_H;
1661
1662 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001663 DBG_TRACE_DEVEL("leaving on error",
1664 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001665 return 0;
1666 }
1667
1668 channel_dont_close(rep);
1669 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001670 DBG_TRACE_DEVEL("waiting for more data",
1671 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001672 return 0;
1673 }
1674
1675 /* More interesting part now : we know that we have a complete
1676 * response which at least looks like HTTP. We have an indicator
1677 * of each header's length, so we can parse them quickly.
1678 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001679 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001680 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001681 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001682
Christopher Faulet9768c262018-10-22 09:34:31 +02001683 /* 0: we might have to print this header in debug mode */
1684 if (unlikely((global.mode & MODE_DEBUG) &&
1685 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1686 int32_t pos;
1687
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001688 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001689
Christopher Fauleta3f15502019-05-13 15:27:23 +02001690 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001691 struct htx_blk *blk = htx_get_blk(htx, pos);
1692 enum htx_blk_type type = htx_get_blk_type(blk);
1693
1694 if (type == HTX_BLK_EOH)
1695 break;
1696 if (type != HTX_BLK_HDR)
1697 continue;
1698
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001699 http_debug_hdr("srvhdr", s,
1700 htx_get_blk_name(htx, blk),
1701 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001702 }
1703 }
1704
Christopher Faulet03599112018-11-27 11:21:21 +01001705 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001706 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001707 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001708 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001709 if (sl->flags & HTX_SL_F_XFER_LEN) {
1710 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001711 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001712 if (sl->flags & HTX_SL_F_BODYLESS)
1713 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001714 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001715
1716 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001717 if (n < 1 || n > 5)
1718 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001719
Christopher Faulete0768eb2018-10-03 16:38:02 +02001720 /* when the client triggers a 4xx from the server, it's most often due
1721 * to a missing object or permission. These events should be tracked
1722 * because if they happen often, it may indicate a brute force or a
1723 * vulnerability scan.
1724 */
1725 if (n == 4)
1726 stream_inc_http_err_ctr(s);
1727
1728 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001729 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001730
Christopher Faulete0768eb2018-10-03 16:38:02 +02001731 /* Adjust server's health based on status code. Note: status codes 501
1732 * and 505 are triggered on demand by client request, so we must not
1733 * count them as server failures.
1734 */
1735 if (objt_server(s->target)) {
1736 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001737 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001738 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001739 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001740 }
1741
1742 /*
1743 * We may be facing a 100-continue response, or any other informational
1744 * 1xx response which is non-final, in which case this is not the right
1745 * response, and we're waiting for the next one. Let's allow this response
1746 * to go to the client and wait for the next one. There's an exception for
1747 * 101 which is used later in the code to switch protocols.
1748 */
1749 if (txn->status < 200 &&
1750 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001751 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001752 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001753 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001754 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001755 txn->status = 0;
1756 s->logs.t_data = -1; /* was not a response yet */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001757 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001758 }
1759
1760 /*
1761 * 2: check for cacheability.
1762 */
1763
1764 switch (txn->status) {
1765 case 200:
1766 case 203:
1767 case 204:
1768 case 206:
1769 case 300:
1770 case 301:
1771 case 404:
1772 case 405:
1773 case 410:
1774 case 414:
1775 case 501:
1776 break;
1777 default:
1778 /* RFC7231#6.1:
1779 * Responses with status codes that are defined as
1780 * cacheable by default (e.g., 200, 203, 204, 206,
1781 * 300, 301, 404, 405, 410, 414, and 501 in this
1782 * specification) can be reused by a cache with
1783 * heuristic expiration unless otherwise indicated
1784 * by the method definition or explicit cache
1785 * controls [RFC7234]; all other status codes are
1786 * not cacheable by default.
1787 */
1788 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1789 break;
1790 }
1791
1792 /*
1793 * 3: we may need to capture headers
1794 */
1795 s->logs.logwait &= ~LW_RESP;
1796 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001797 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001798
Christopher Faulet9768c262018-10-22 09:34:31 +02001799 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001800 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1801 txn->status == 101)) {
1802 /* Either we've established an explicit tunnel, or we're
1803 * switching the protocol. In both cases, we're very unlikely
1804 * to understand the next protocols. We have to switch to tunnel
1805 * mode, so that we transfer the request and responses then let
1806 * this protocol pass unmodified. When we later implement specific
1807 * parsers for such protocols, we'll want to check the Upgrade
1808 * header which contains information about that protocol for
1809 * responses with status 101 (eg: see RFC2817 about TLS).
1810 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001811 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001812 }
1813
Christopher Faulet61608322018-11-23 16:23:45 +01001814 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1815 * 407 (Proxy-Authenticate) responses and set the connection to private
1816 */
1817 srv_conn = cs_conn(objt_cs(s->si[1].end));
1818 if (srv_conn) {
1819 struct ist hdr;
1820 struct http_hdr_ctx ctx;
1821
1822 if (txn->status == 401)
1823 hdr = ist("WWW-Authenticate");
1824 else if (txn->status == 407)
1825 hdr = ist("Proxy-Authenticate");
1826 else
1827 goto end;
1828
1829 ctx.blk = NULL;
1830 while (http_find_header(htx, hdr, &ctx, 0)) {
1831 if ((ctx.value.len >= 9 && word_match(ctx.value.ptr, ctx.value.len, "Negotiate", 9)) ||
Olivier Houchard250031e2019-05-29 15:01:50 +02001832 (ctx.value.len >= 4 && word_match(ctx.value.ptr, ctx.value.len, "NTLM", 4))) {
1833 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet61608322018-11-23 16:23:45 +01001834 srv_conn->flags |= CO_FL_PRIVATE;
Olivier Houchard250031e2019-05-29 15:01:50 +02001835 }
Christopher Faulet61608322018-11-23 16:23:45 +01001836 }
1837 }
1838
1839 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001840 /* we want to have the response time before we start processing it */
1841 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1842
1843 /* end of job, return OK */
1844 rep->analysers &= ~an_bit;
1845 rep->analyse_exp = TICK_ETERNITY;
1846 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001847 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001848 return 1;
1849
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001850 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001851 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001852 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001853 if (sess->listener->counters)
1854 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001855 if (objt_server(s->target))
1856 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001857 txn->status = 500;
1858 if (!(s->flags & SF_ERR_MASK))
1859 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001860 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001861
1862 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001863 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001864 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001865 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001866 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001867 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001868 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001869 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001870 do_l7_retry(s, si_b) == 0) {
1871 DBG_TRACE_DEVEL("leaving on L7 retry",
1872 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001873 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001874 }
Christopher Faulet47365272018-10-31 17:40:50 +01001875 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001876 /* fall through */
1877
Christopher Fauletb8a53712019-12-16 11:29:38 +01001878 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001879 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001880
1881 if (!(s->flags & SF_ERR_MASK))
1882 s->flags |= SF_ERR_PRXCOND;
1883 if (!(s->flags & SF_FINST_MASK))
1884 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001885
1886 s->si[1].flags |= SI_FL_NOLINGER;
1887 rep->analysers &= AN_RES_FLT_END;
1888 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001889 DBG_TRACE_DEVEL("leaving on error",
1890 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001891 return 0;
1892
Christopher Faulete0768eb2018-10-03 16:38:02 +02001893 abort_keep_alive:
1894 /* A keep-alive request to the server failed on a network error.
1895 * The client is required to retry. We need to close without returning
1896 * any other information so that the client retries.
1897 */
1898 txn->status = 0;
1899 rep->analysers &= AN_RES_FLT_END;
1900 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001901 s->logs.logwait = 0;
1902 s->logs.level = 0;
1903 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001904 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001905 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1906 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001907 return 0;
1908}
1909
1910/* This function performs all the processing enabled for the current response.
1911 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1912 * and updates s->res.analysers. It might make sense to explode it into several
1913 * other functions. It works like process_request (see indications above).
1914 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001915int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001916{
1917 struct session *sess = s->sess;
1918 struct http_txn *txn = s->txn;
1919 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001920 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001921 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001922 enum rule_result ret = HTTP_RULE_RES_CONT;
1923
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001924 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1925 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001926
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001927 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001928
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001929 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001930
1931 /* The stats applet needs to adjust the Connection header but we don't
1932 * apply any filter there.
1933 */
1934 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1935 rep->analysers &= ~an_bit;
1936 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001937 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001938 }
1939
1940 /*
1941 * We will have to evaluate the filters.
1942 * As opposed to version 1.2, now they will be evaluated in the
1943 * filters order and not in the header order. This means that
1944 * each filter has to be validated among all headers.
1945 *
1946 * Filters are tried with ->be first, then with ->fe if it is
1947 * different from ->be.
1948 *
1949 * Maybe we are in resume condiion. In this case I choose the
1950 * "struct proxy" which contains the rule list matching the resume
1951 * pointer. If none of theses "struct proxy" match, I initialise
1952 * the process with the first one.
1953 *
1954 * In fact, I check only correspondance betwwen the current list
1955 * pointer and the ->fe rule list. If it doesn't match, I initialize
1956 * the loop with the ->be.
1957 */
1958 if (s->current_rule_list == &sess->fe->http_res_rules)
1959 cur_proxy = sess->fe;
1960 else
1961 cur_proxy = s->be;
1962 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001963 /* evaluate http-response rules */
1964 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001965 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001966
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001967 switch (ret) {
1968 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1969 goto return_prx_yield;
1970
1971 case HTTP_RULE_RES_CONT:
1972 case HTTP_RULE_RES_STOP: /* nothing to do */
1973 break;
1974
1975 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1976 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001977
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001978 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1979 goto return_prx_cond;
1980
1981 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001982 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001983
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001984 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1985 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001986
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001987 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1988 goto return_int_err;
1989 }
1990
1991 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001992
Christopher Faulete0768eb2018-10-03 16:38:02 +02001993 /* check whether we're already working on the frontend */
1994 if (cur_proxy == sess->fe)
1995 break;
1996 cur_proxy = sess->fe;
1997 }
1998
Christopher Faulete0768eb2018-10-03 16:38:02 +02001999 /* OK that's all we can do for 1xx responses */
2000 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002001 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002002
2003 /*
2004 * Now check for a server cookie.
2005 */
2006 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002007 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002008
2009 /*
2010 * Check for cache-control or pragma headers if required.
2011 */
2012 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002013 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002014
2015 /*
2016 * Add server cookie in the response if needed
2017 */
2018 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
2019 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
2020 (!(s->flags & SF_DIRECT) ||
2021 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
2022 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
2023 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
2024 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
2025 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
2026 !(s->flags & SF_IGNORE_PRST)) {
2027 /* the server is known, it's not the one the client requested, or the
2028 * cookie's last seen date needs to be refreshed. We have to
2029 * insert a set-cookie here, except if we want to insert only on POST
2030 * requests and this one isn't. Note that servers which don't have cookies
2031 * (eg: some backup servers) will return a full cookie removal request.
2032 */
2033 if (!objt_server(s->target)->cookie) {
2034 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002035 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02002036 s->be->cookie_name);
2037 }
2038 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002039 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002040
2041 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2042 /* emit last_date, which is mandatory */
2043 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2044 s30tob64((date.tv_sec+3) >> 2,
2045 trash.area + trash.data);
2046 trash.data += 5;
2047
2048 if (s->be->cookie_maxlife) {
2049 /* emit first_date, which is either the original one or
2050 * the current date.
2051 */
2052 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2053 s30tob64(txn->cookie_first_date ?
2054 txn->cookie_first_date >> 2 :
2055 (date.tv_sec+3) >> 2,
2056 trash.area + trash.data);
2057 trash.data += 5;
2058 }
2059 }
2060 chunk_appendf(&trash, "; path=/");
2061 }
2062
2063 if (s->be->cookie_domain)
2064 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2065
2066 if (s->be->ck_opts & PR_CK_HTTPONLY)
2067 chunk_appendf(&trash, "; HttpOnly");
2068
2069 if (s->be->ck_opts & PR_CK_SECURE)
2070 chunk_appendf(&trash, "; Secure");
2071
Christopher Faulet2f533902020-01-21 11:06:48 +01002072 if (s->be->cookie_attrs)
2073 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
2074
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002075 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002076 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002077
2078 txn->flags &= ~TX_SCK_MASK;
2079 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2080 /* the server did not change, only the date was updated */
2081 txn->flags |= TX_SCK_UPDATED;
2082 else
2083 txn->flags |= TX_SCK_INSERTED;
2084
2085 /* Here, we will tell an eventual cache on the client side that we don't
2086 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2087 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2088 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2089 */
2090 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2091
2092 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2093
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002094 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002095 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002096 }
2097 }
2098
2099 /*
2100 * Check if result will be cacheable with a cookie.
2101 * We'll block the response if security checks have caught
2102 * nasty things such as a cacheable cookie.
2103 */
2104 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2105 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2106 (s->be->options & PR_O_CHK_CACHE)) {
2107 /* we're in presence of a cacheable response containing
2108 * a set-cookie header. We'll block it as requested by
2109 * the 'checkcache' option, and send an alert.
2110 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002111 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2112 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2113 send_log(s->be, LOG_ALERT,
2114 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2115 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002116 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002117 }
2118
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002119 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002120 /*
2121 * Evaluate after-response rules before forwarding the response. rules
2122 * from the backend are evaluated first, then one from the frontend if
2123 * it differs.
2124 */
2125 if (!http_eval_after_res_rules(s))
2126 goto return_int_err;
2127
Christopher Faulete0768eb2018-10-03 16:38:02 +02002128 /* Always enter in the body analyzer */
2129 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2130 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2131
2132 /* if the user wants to log as soon as possible, without counting
2133 * bytes from the server, then this is the right moment. We have
2134 * to temporarily assign bytes_out to log what we currently have.
2135 */
2136 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2137 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002138 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002139 s->do_log(s);
2140 s->logs.bytes_out = 0;
2141 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002142
Christopher Fauletb8a53712019-12-16 11:29:38 +01002143 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002144 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002145 rep->analysers &= ~an_bit;
2146 rep->analyse_exp = TICK_ETERNITY;
2147 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002148
Christopher Fauletb8a53712019-12-16 11:29:38 +01002149 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002150 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002151 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002152 if (sess->listener->counters)
2153 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002154 if (objt_server(s->target))
2155 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002156 goto return_prx_err;
2157
2158 return_int_err:
2159 txn->status = 500;
2160 if (!(s->flags & SF_ERR_MASK))
2161 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002162 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002163 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2164 if (objt_server(s->target))
2165 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002166 if (objt_server(s->target))
2167 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002168 goto return_prx_err;
2169
2170 return_bad_res:
2171 txn->status = 502;
Christopher Fauleta20a6532020-02-05 10:16:41 +01002172 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2173 if (objt_server(s->target)) {
2174 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2175 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2176 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002177 /* fall through */
2178
2179 return_prx_err:
2180 http_reply_and_close(s, txn->status, http_error_message(s));
2181 /* fall through */
2182
2183 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002184 s->logs.t_data = -1; /* was not a valid response */
2185 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002186
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002187 if (!(s->flags & SF_ERR_MASK))
2188 s->flags |= SF_ERR_PRXCOND;
2189 if (!(s->flags & SF_FINST_MASK))
2190 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002191
2192 rep->analysers &= ~an_bit;
2193 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002194 DBG_TRACE_DEVEL("leaving on error",
2195 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002196 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002197
2198 return_prx_yield:
2199 channel_dont_close(rep);
2200 DBG_TRACE_DEVEL("waiting for more data",
2201 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2202 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002203}
2204
2205/* This function is an analyser which forwards response body (including chunk
2206 * sizes if any). It is called as soon as we must forward, even if we forward
2207 * zero byte. The only situation where it must not be called is when we're in
2208 * tunnel mode and we want to forward till the close. It's used both to forward
2209 * remaining data and to resync after end of body. It expects the msg_state to
2210 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2211 * read more data, or 1 once we can go on with next request or end the stream.
2212 *
2213 * It is capable of compressing response data both in content-length mode and
2214 * in chunked mode. The state machines follows different flows depending on
2215 * whether content-length and chunked modes are used, since there are no
2216 * trailers in content-length :
2217 *
2218 * chk-mode cl-mode
2219 * ,----- BODY -----.
2220 * / \
2221 * V size > 0 V chk-mode
2222 * .--> SIZE -------------> DATA -------------> CRLF
2223 * | | size == 0 | last byte |
2224 * | v final crlf v inspected |
2225 * | TRAILERS -----------> DONE |
2226 * | |
2227 * `----------------------------------------------'
2228 *
2229 * Compression only happens in the DATA state, and must be flushed in final
2230 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2231 * is performed at once on final states for all bytes parsed, or when leaving
2232 * on missing data.
2233 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002234int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002235{
2236 struct session *sess = s->sess;
2237 struct http_txn *txn = s->txn;
2238 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002239 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002240 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002241
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002242 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002243
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002244 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002245
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002246 if (htx->flags & HTX_FL_PARSING_ERROR)
2247 goto return_bad_res;
2248 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2249 goto return_int_err;
2250
Christopher Faulete0768eb2018-10-03 16:38:02 +02002251 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002252 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002253 /* Output closed while we were sending data. We must abort and
2254 * wake the other side up.
2255 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002256 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002257 http_end_response(s);
2258 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002259 DBG_TRACE_DEVEL("leaving on error",
2260 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002261 return 1;
2262 }
2263
Christopher Faulet9768c262018-10-22 09:34:31 +02002264 if (msg->msg_state == HTTP_MSG_BODY)
2265 msg->msg_state = HTTP_MSG_DATA;
2266
Christopher Faulete0768eb2018-10-03 16:38:02 +02002267 /* in most states, we should abort in case of early close */
2268 channel_auto_close(res);
2269
Christopher Faulete0768eb2018-10-03 16:38:02 +02002270 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002271 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002272 if (res->flags & CF_EOI)
2273 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002274 }
2275 else {
2276 /* We can't process the buffer's contents yet */
2277 res->flags |= CF_WAKE_WRITE;
2278 goto missing_data_or_waiting;
2279 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002280 }
2281
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002282 if (msg->msg_state >= HTTP_MSG_ENDING)
2283 goto ending;
2284
2285 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2286 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2287 msg->msg_state = HTTP_MSG_ENDING;
2288 goto ending;
2289 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002290
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002291 /* Forward input data. We get it by removing all outgoing data not
2292 * forwarded yet from HTX data size. If there are some data filters, we
2293 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002294 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002295 if (HAS_RSP_DATA_FILTERS(s)) {
2296 ret = flt_http_payload(s, msg, htx->data);
2297 if (ret < 0)
2298 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002299 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002300 }
2301 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002302 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002303 if (msg->flags & HTTP_MSGF_XFER_LEN)
2304 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002305 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002306
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002307 if (htx->data != co_data(res))
2308 goto missing_data_or_waiting;
2309
2310 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2311 msg->msg_state = HTTP_MSG_ENDING;
2312 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002313 }
2314
Christopher Faulet9768c262018-10-22 09:34:31 +02002315 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002316 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2317 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002318 */
2319 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2320 goto missing_data_or_waiting;
2321
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002322 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002323
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002324 ending:
2325 /* other states, ENDING...TUNNEL */
2326 if (msg->msg_state >= HTTP_MSG_DONE)
2327 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002328
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002329 if (HAS_RSP_DATA_FILTERS(s)) {
2330 ret = flt_http_end(s, msg);
2331 if (ret <= 0) {
2332 if (!ret)
2333 goto missing_data_or_waiting;
2334 goto return_bad_res;
2335 }
2336 }
2337
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002338 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2339 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2340 msg->msg_state = HTTP_MSG_TUNNEL;
2341 goto ending;
2342 }
2343 else {
2344 msg->msg_state = HTTP_MSG_DONE;
2345 res->to_forward = 0;
2346 }
2347
2348 done:
2349
2350 channel_dont_close(res);
2351
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002352 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002353 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002354 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002355 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2356 if (res->flags & CF_SHUTW) {
2357 /* response errors are most likely due to the
2358 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002359 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002360 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002361 goto return_bad_res;
2362 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002363 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002364 return 1;
2365 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002366 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2367 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002368 return 0;
2369
2370 missing_data_or_waiting:
2371 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002372 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002373
2374 /* stop waiting for data if the input is closed before the end. If the
2375 * client side was already closed, it means that the client has aborted,
2376 * so we don't want to count this as a server abort. Otherwise it's a
2377 * server abort.
2378 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002379 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002380 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002381 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002382 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002383 if (htx_is_empty(htx))
2384 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002385 }
2386
Christopher Faulete0768eb2018-10-03 16:38:02 +02002387 /* When TE: chunked is used, we need to get there again to parse
2388 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002389 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2390 * are filters registered on the stream, we don't want to forward a
2391 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002392 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002393 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002394 channel_dont_close(res);
2395
2396 /* We know that more data are expected, but we couldn't send more that
2397 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2398 * system knows it must not set a PUSH on this first part. Interactive
2399 * modes are already handled by the stream sock layer. We must not do
2400 * this in content-length mode because it could present the MSG_MORE
2401 * flag with the last block of forwarded data, which would cause an
2402 * additional delay to be observed by the receiver.
2403 */
2404 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2405 res->flags |= CF_EXPECT_MORE;
2406
2407 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002408 DBG_TRACE_DEVEL("waiting for more data to forward",
2409 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002410 return 0;
2411
Christopher Faulet93e02d82019-03-08 14:18:50 +01002412 return_srv_abort:
2413 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2414 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002415 if (sess->listener->counters)
2416 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002417 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002418 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002419 if (!(s->flags & SF_ERR_MASK))
2420 s->flags |= SF_ERR_SRVCL;
2421 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002422
Christopher Faulet93e02d82019-03-08 14:18:50 +01002423 return_cli_abort:
2424 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2425 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002426 if (sess->listener->counters)
2427 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002428 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002429 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002430 if (!(s->flags & SF_ERR_MASK))
2431 s->flags |= SF_ERR_CLICL;
2432 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002433
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002434 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002435 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002436 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002437 if (sess->listener->counters)
2438 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002439 if (objt_server(s->target))
2440 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002441 if (!(s->flags & SF_ERR_MASK))
2442 s->flags |= SF_ERR_INTERNAL;
2443 goto return_error;
2444
Christopher Faulet93e02d82019-03-08 14:18:50 +01002445 return_bad_res:
2446 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2447 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002448 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002449 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2450 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002451 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002452 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002453 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002454
Christopher Faulet93e02d82019-03-08 14:18:50 +01002455 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002456 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002457 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002458 res->analysers &= AN_RES_FLT_END;
2459 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 +02002460 if (!(s->flags & SF_FINST_MASK))
2461 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002462 DBG_TRACE_DEVEL("leaving on error",
2463 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002464 return 0;
2465}
2466
Christopher Fauletf2824e62018-10-01 12:12:37 +02002467/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002468 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002469 * as too large a request to build a valid response.
2470 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002471int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002472{
Christopher Faulet99daf282018-11-28 22:58:13 +01002473 struct channel *req = &s->req;
2474 struct channel *res = &s->res;
2475 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002476 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002477 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002478 struct ist status, reason, location;
2479 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002480 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002481
2482 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002483 if (!chunk) {
2484 if (!(s->flags & SF_ERR_MASK))
2485 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002486 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002487 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002488
Christopher Faulet99daf282018-11-28 22:58:13 +01002489 /*
2490 * Create the location
2491 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002492 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002493 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002494 case REDIRECT_TYPE_SCHEME: {
2495 struct http_hdr_ctx ctx;
2496 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002497
Christopher Faulet99daf282018-11-28 22:58:13 +01002498 host = ist("");
2499 ctx.blk = NULL;
2500 if (http_find_header(htx, ist("Host"), &ctx, 0))
2501 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002502
Christopher Faulet297fbb42019-05-13 14:41:27 +02002503 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002504 path = http_get_path(htx_sl_req_uri(sl));
2505 /* build message using path */
2506 if (path.ptr) {
2507 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2508 int qs = 0;
2509 while (qs < path.len) {
2510 if (*(path.ptr + qs) == '?') {
2511 path.len = qs;
2512 break;
2513 }
2514 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002515 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002516 }
2517 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002518 else
2519 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002520
Christopher Faulet99daf282018-11-28 22:58:13 +01002521 if (rule->rdr_str) { /* this is an old "redirect" rule */
2522 /* add scheme */
2523 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2524 goto fail;
2525 }
2526 else {
2527 /* add scheme with executing log format */
2528 chunk->data += build_logline(s, chunk->area + chunk->data,
2529 chunk->size - chunk->data,
2530 &rule->rdr_fmt);
2531 }
2532 /* add "://" + host + path */
2533 if (!chunk_memcat(chunk, "://", 3) ||
2534 !chunk_memcat(chunk, host.ptr, host.len) ||
2535 !chunk_memcat(chunk, path.ptr, path.len))
2536 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002537
Christopher Faulet99daf282018-11-28 22:58:13 +01002538 /* append a slash at the end of the location if needed and missing */
2539 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2540 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2541 if (chunk->data + 1 >= chunk->size)
2542 goto fail;
2543 chunk->area[chunk->data++] = '/';
2544 }
2545 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002546 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002547
Christopher Faulet99daf282018-11-28 22:58:13 +01002548 case REDIRECT_TYPE_PREFIX: {
2549 struct ist path;
2550
Christopher Faulet297fbb42019-05-13 14:41:27 +02002551 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002552 path = http_get_path(htx_sl_req_uri(sl));
2553 /* build message using path */
2554 if (path.ptr) {
2555 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2556 int qs = 0;
2557 while (qs < path.len) {
2558 if (*(path.ptr + qs) == '?') {
2559 path.len = qs;
2560 break;
2561 }
2562 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002563 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002564 }
2565 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002566 else
2567 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002568
Christopher Faulet99daf282018-11-28 22:58:13 +01002569 if (rule->rdr_str) { /* this is an old "redirect" rule */
2570 /* add prefix. Note that if prefix == "/", we don't want to
2571 * add anything, otherwise it makes it hard for the user to
2572 * configure a self-redirection.
2573 */
2574 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2575 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2576 goto fail;
2577 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002578 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002579 else {
2580 /* add prefix with executing log format */
2581 chunk->data += build_logline(s, chunk->area + chunk->data,
2582 chunk->size - chunk->data,
2583 &rule->rdr_fmt);
2584 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002585
Christopher Faulet99daf282018-11-28 22:58:13 +01002586 /* add path */
2587 if (!chunk_memcat(chunk, path.ptr, path.len))
2588 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002589
Christopher Faulet99daf282018-11-28 22:58:13 +01002590 /* append a slash at the end of the location if needed and missing */
2591 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2592 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2593 if (chunk->data + 1 >= chunk->size)
2594 goto fail;
2595 chunk->area[chunk->data++] = '/';
2596 }
2597 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002598 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002599 case REDIRECT_TYPE_LOCATION:
2600 default:
2601 if (rule->rdr_str) { /* this is an old "redirect" rule */
2602 /* add location */
2603 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2604 goto fail;
2605 }
2606 else {
2607 /* add location with executing log format */
2608 chunk->data += build_logline(s, chunk->area + chunk->data,
2609 chunk->size - chunk->data,
2610 &rule->rdr_fmt);
2611 }
2612 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002613 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002614 location = ist2(chunk->area, chunk->data);
2615
2616 /*
2617 * Create the 30x response
2618 */
2619 switch (rule->code) {
2620 case 308:
2621 status = ist("308");
2622 reason = ist("Permanent Redirect");
2623 break;
2624 case 307:
2625 status = ist("307");
2626 reason = ist("Temporary Redirect");
2627 break;
2628 case 303:
2629 status = ist("303");
2630 reason = ist("See Other");
2631 break;
2632 case 301:
2633 status = ist("301");
2634 reason = ist("Moved Permanently");
2635 break;
2636 case 302:
2637 default:
2638 status = ist("302");
2639 reason = ist("Found");
2640 break;
2641 }
2642
Christopher Faulet08e66462019-05-23 16:44:59 +02002643 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2644 close = 1;
2645
Christopher Faulet99daf282018-11-28 22:58:13 +01002646 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002647 /* Trim any possible response */
2648 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002649 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2650 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2651 if (!sl)
2652 goto fail;
2653 sl->info.res.status = rule->code;
2654 s->txn->status = rule->code;
2655
Christopher Faulet08e66462019-05-23 16:44:59 +02002656 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2657 goto fail;
2658
2659 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002660 !htx_add_header(htx, ist("Location"), location))
2661 goto fail;
2662
2663 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2664 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2665 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002666 }
2667
2668 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002669 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2670 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002671 }
2672
Christopher Faulet99daf282018-11-28 22:58:13 +01002673 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2674 goto fail;
2675
Kevin Zhu96b36392020-01-07 09:42:55 +01002676 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002677 if (!http_forward_proxy_resp(s, 1))
2678 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002679
Christopher Faulet60b33a52020-01-28 09:18:10 +01002680 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2681 /* let's log the request time */
2682 s->logs.tv_request = now;
2683 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002684
Christopher Faulet60b33a52020-01-28 09:18:10 +01002685 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2686 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2687 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002688
2689 if (!(s->flags & SF_ERR_MASK))
2690 s->flags |= SF_ERR_LOCAL;
2691 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002692 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002693
Christopher Faulet99daf282018-11-28 22:58:13 +01002694 free_trash_chunk(chunk);
2695 return 1;
2696
2697 fail:
2698 /* If an error occurred, remove the incomplete HTTP response from the
2699 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002700 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002701 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002702 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002703}
2704
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002705/* Replace all headers matching the name <name>. The header value is replaced if
2706 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2707 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2708 * values are evaluated one by one. It returns 0 on success and -1 on error.
2709 */
2710int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2711 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002712{
2713 struct http_hdr_ctx ctx;
2714 struct buffer *output = get_trash_chunk();
2715
Christopher Faulet72333522018-10-24 11:25:02 +02002716 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002717 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002718 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2719 continue;
2720
2721 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2722 if (output->data == -1)
2723 return -1;
2724 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2725 return -1;
2726 }
2727 return 0;
2728}
2729
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002730/* This function executes one of the set-{method,path,query,uri} actions. It
2731 * takes the string from the variable 'replace' with length 'len', then modifies
2732 * the relevant part of the request line accordingly. Then it updates various
2733 * pointers to the next elements which were moved, and the total buffer length.
2734 * It finds the action to be performed in p[2], previously filled by function
2735 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2736 * error, though this can be revisited when this code is finally exploited.
2737 *
2738 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2739 * query string and 3 to replace uri.
2740 *
2741 * In query string case, the mark question '?' must be set at the start of the
2742 * string by the caller, event if the replacement query string is empty.
2743 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002744int http_req_replace_stline(int action, const char *replace, int len,
2745 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002746{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002747 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002748
2749 switch (action) {
2750 case 0: // method
2751 if (!http_replace_req_meth(htx, ist2(replace, len)))
2752 return -1;
2753 break;
2754
2755 case 1: // path
2756 if (!http_replace_req_path(htx, ist2(replace, len)))
2757 return -1;
2758 break;
2759
2760 case 2: // query
2761 if (!http_replace_req_query(htx, ist2(replace, len)))
2762 return -1;
2763 break;
2764
2765 case 3: // uri
2766 if (!http_replace_req_uri(htx, ist2(replace, len)))
2767 return -1;
2768 break;
2769
2770 default:
2771 return -1;
2772 }
2773 return 0;
2774}
2775
2776/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002777 * variable <status> contains the new status code. This function never fails. It
2778 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002779 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002780int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002781{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002782 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002783 char *res;
2784
2785 chunk_reset(&trash);
2786 res = ultoa_o(status, trash.area, trash.size);
2787 trash.data = res - trash.area;
2788
2789 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002790 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002791 const char *str = http_get_reason(status);
2792 reason = ist2(str, strlen(str));
2793 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002794
Christopher Faulete00d06c2019-12-16 17:18:42 +01002795 if (!http_replace_res_status(htx, ist2(trash.area, trash.data)))
2796 return -1;
Christopher Faulet96bff762019-12-17 13:46:18 +01002797 if (!http_replace_res_reason(htx, reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002798 return -1;
2799 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002800}
2801
Christopher Faulet3e964192018-10-24 11:39:23 +02002802/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2803 * transaction <txn>. Returns the verdict of the first rule that prevents
2804 * further processing of the request (auth, deny, ...), and defaults to
2805 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2806 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2807 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2808 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2809 * status.
2810 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002811static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002812 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002813{
2814 struct session *sess = strm_sess(s);
2815 struct http_txn *txn = s->txn;
2816 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002817 struct act_rule *rule;
2818 struct http_hdr_ctx ctx;
2819 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002820 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002821 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002822
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002823 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002824
2825 /* If "the current_rule_list" match the executed rule list, we are in
2826 * resume condition. If a resume is needed it is always in the action
2827 * and never in the ACL or converters. In this case, we initialise the
2828 * current rule, and go to the action execution point.
2829 */
2830 if (s->current_rule) {
2831 rule = s->current_rule;
2832 s->current_rule = NULL;
2833 if (s->current_rule_list == rules)
2834 goto resume_execution;
2835 }
2836 s->current_rule_list = rules;
2837
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002838 /* start the ruleset evaluation in strict mode */
2839 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002840
Christopher Faulet3e964192018-10-24 11:39:23 +02002841 list_for_each_entry(rule, rules, list) {
2842 /* check optional condition */
2843 if (rule->cond) {
2844 int ret;
2845
2846 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2847 ret = acl_pass(ret);
2848
2849 if (rule->cond->pol == ACL_COND_UNLESS)
2850 ret = !ret;
2851
2852 if (!ret) /* condition not matched */
2853 continue;
2854 }
2855
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002856 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002857 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002858 /* Always call the action function if defined */
2859 if (rule->action_ptr) {
2860 if ((s->req.flags & CF_READ_ERROR) ||
2861 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2862 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002863 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002864
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002865 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002866 case ACT_RET_CONT:
2867 break;
2868 case ACT_RET_STOP:
2869 rule_ret = HTTP_RULE_RES_STOP;
2870 goto end;
2871 case ACT_RET_YIELD:
2872 s->current_rule = rule;
2873 rule_ret = HTTP_RULE_RES_YIELD;
2874 goto end;
2875 case ACT_RET_ERR:
2876 rule_ret = HTTP_RULE_RES_ERROR;
2877 goto end;
2878 case ACT_RET_DONE:
2879 rule_ret = HTTP_RULE_RES_DONE;
2880 goto end;
2881 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002882 txn->flags |= TX_CLDENY;
2883 if (txn->status == -1)
2884 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002885 rule_ret = HTTP_RULE_RES_DENY;
2886 goto end;
2887 case ACT_RET_ABRT:
2888 rule_ret = HTTP_RULE_RES_ABRT;
2889 goto end;
2890 case ACT_RET_INV:
2891 rule_ret = HTTP_RULE_RES_BADREQ;
2892 goto end;
2893 }
2894 continue; /* eval the next rule */
2895 }
2896
2897 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002898 switch (rule->action) {
2899 case ACT_ACTION_ALLOW:
2900 rule_ret = HTTP_RULE_RES_STOP;
2901 goto end;
2902
2903 case ACT_ACTION_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002904 txn->flags |= TX_CLDENY;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01002905 txn->status = rule->arg.http_deny.status;
2906 if (rule->arg.http_deny.errmsg)
2907 txn->errmsg = rule->arg.http_deny.errmsg;
Christopher Faulet3e964192018-10-24 11:39:23 +02002908 rule_ret = HTTP_RULE_RES_DENY;
2909 goto end;
2910
2911 case ACT_HTTP_REQ_TARPIT:
2912 txn->flags |= TX_CLTARPIT;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01002913 txn->status = rule->arg.http_deny.status;
2914 if (rule->arg.http_deny.errmsg)
2915 txn->errmsg = rule->arg.http_deny.errmsg;
Christopher Faulet3e964192018-10-24 11:39:23 +02002916 rule_ret = HTTP_RULE_RES_DENY;
2917 goto end;
2918
2919 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002920 /* Auth might be performed on regular http-req rules as well as on stats */
Christopher Faulet96bff762019-12-17 13:46:18 +01002921 auth_realm = rule->arg.http.str.ptr;
Christopher Faulet3e964192018-10-24 11:39:23 +02002922 if (!auth_realm) {
2923 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2924 auth_realm = STATS_DEFAULT_REALM;
2925 else
2926 auth_realm = px->id;
2927 }
2928 /* send 401/407 depending on whether we use a proxy or not. We still
2929 * count one error, because normal browsing won't significantly
2930 * increase the counter but brute force attempts will.
2931 */
Christopher Faulet3e964192018-10-24 11:39:23 +02002932 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002933 if (http_reply_40x_unauthorized(s, auth_realm) == -1)
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002934 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet12c51e22018-11-28 15:59:42 +01002935 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02002936 goto end;
2937
2938 case ACT_HTTP_REDIR:
Christopher Faulet3e964192018-10-24 11:39:23 +02002939 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002940 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002941 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002942 goto end;
2943
2944 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002945 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002946 break;
2947
2948 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002949 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002950 break;
2951
2952 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002953 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002954 break;
2955
2956 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002957 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002958 break;
2959
Christopher Faulet3e964192018-10-24 11:39:23 +02002960 case ACT_HTTP_DEL_HDR:
2961 /* remove all occurrences of the header */
2962 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01002963 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02002964 http_remove_header(htx, &ctx);
2965 break;
2966
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002967 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002968 default:
2969 break;
2970 }
2971 }
2972
2973 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002974 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002975 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002976 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002977
Christopher Faulet3e964192018-10-24 11:39:23 +02002978 /* we reached the end of the rules, nothing to report */
2979 return rule_ret;
2980}
2981
2982/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2983 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2984 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2985 * is returned, the process can continue the evaluation of next rule list. If
2986 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2987 * is returned, it means the operation could not be processed and a server error
2988 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2989 * deny rule. If *YIELD is returned, the caller must call again the function
2990 * with the same context.
2991 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002992static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2993 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002994{
2995 struct session *sess = strm_sess(s);
2996 struct http_txn *txn = s->txn;
2997 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002998 struct act_rule *rule;
2999 struct http_hdr_ctx ctx;
3000 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003001 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02003002
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003003 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02003004
3005 /* If "the current_rule_list" match the executed rule list, we are in
3006 * resume condition. If a resume is needed it is always in the action
3007 * and never in the ACL or converters. In this case, we initialise the
3008 * current rule, and go to the action execution point.
3009 */
3010 if (s->current_rule) {
3011 rule = s->current_rule;
3012 s->current_rule = NULL;
3013 if (s->current_rule_list == rules)
3014 goto resume_execution;
3015 }
3016 s->current_rule_list = rules;
3017
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003018 /* start the ruleset evaluation in strict mode */
3019 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003020
Christopher Faulet3e964192018-10-24 11:39:23 +02003021 list_for_each_entry(rule, rules, list) {
3022 /* check optional condition */
3023 if (rule->cond) {
3024 int ret;
3025
3026 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3027 ret = acl_pass(ret);
3028
3029 if (rule->cond->pol == ACL_COND_UNLESS)
3030 ret = !ret;
3031
3032 if (!ret) /* condition not matched */
3033 continue;
3034 }
3035
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003036 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02003037resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003038
3039 /* Always call the action function if defined */
3040 if (rule->action_ptr) {
3041 if ((s->req.flags & CF_READ_ERROR) ||
3042 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3043 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003044 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003045
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003046 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003047 case ACT_RET_CONT:
3048 break;
3049 case ACT_RET_STOP:
3050 rule_ret = HTTP_RULE_RES_STOP;
3051 goto end;
3052 case ACT_RET_YIELD:
3053 s->current_rule = rule;
3054 rule_ret = HTTP_RULE_RES_YIELD;
3055 goto end;
3056 case ACT_RET_ERR:
3057 rule_ret = HTTP_RULE_RES_ERROR;
3058 goto end;
3059 case ACT_RET_DONE:
3060 rule_ret = HTTP_RULE_RES_DONE;
3061 goto end;
3062 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01003063 txn->flags |= TX_CLDENY;
3064 if (txn->status == -1)
3065 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003066 rule_ret = HTTP_RULE_RES_DENY;
3067 goto end;
3068 case ACT_RET_ABRT:
3069 rule_ret = HTTP_RULE_RES_ABRT;
3070 goto end;
3071 case ACT_RET_INV:
3072 rule_ret = HTTP_RULE_RES_BADREQ;
3073 goto end;
3074 }
3075 continue; /* eval the next rule */
3076 }
3077
3078 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003079 switch (rule->action) {
3080 case ACT_ACTION_ALLOW:
3081 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3082 goto end;
3083
3084 case ACT_ACTION_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01003085 txn->flags |= TX_CLDENY;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01003086 txn->status = rule->arg.http_deny.status;
3087 if (rule->arg.http_deny.errmsg)
3088 txn->errmsg = rule->arg.http_deny.errmsg;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003089 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003090 goto end;
3091
3092 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003093 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003094 break;
3095
3096 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003097 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003098 break;
3099
3100 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003101 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003102 break;
3103
3104 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003105 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003106 break;
3107
Christopher Faulet3e964192018-10-24 11:39:23 +02003108 case ACT_HTTP_DEL_HDR:
3109 /* remove all occurrences of the header */
3110 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01003111 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02003112 http_remove_header(htx, &ctx);
3113 break;
3114
Christopher Faulet3e964192018-10-24 11:39:23 +02003115 case ACT_HTTP_REDIR:
3116 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003117 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003118 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003119 goto end;
3120
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003121 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003122 default:
3123 break;
3124 }
3125 }
3126
3127 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003128 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003129 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003130 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003131
Christopher Faulet3e964192018-10-24 11:39:23 +02003132 /* we reached the end of the rules, nothing to report */
3133 return rule_ret;
3134}
3135
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003136/* Executes backend and frontend http-after-response rules for the stream <s>,
3137 * in that order. it return 1 on success and 0 on error. It is the caller
3138 * responsibility to catch error or ignore it. If it catches it, this function
3139 * may be called a second time, for the internal error.
3140 */
3141int http_eval_after_res_rules(struct stream *s)
3142{
3143 struct session *sess = s->sess;
3144 enum rule_result ret = HTTP_RULE_RES_CONT;
3145
3146 /* prune the request variables if not already done and swap to the response variables. */
3147 if (s->vars_reqres.scope != SCOPE_RES) {
3148 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3149 vars_prune(&s->vars_reqres, s->sess, s);
3150 vars_init(&s->vars_reqres, SCOPE_RES);
3151 }
3152
3153 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3154 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3155 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3156
3157 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3158 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3159}
3160
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003161/*
3162 * Manage client-side cookie. It can impact performance by about 2% so it is
3163 * desirable to call it only when needed. This code is quite complex because
3164 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3165 * highly recommended not to touch this part without a good reason !
3166 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003167static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003168{
3169 struct session *sess = s->sess;
3170 struct http_txn *txn = s->txn;
3171 struct htx *htx;
3172 struct http_hdr_ctx ctx;
3173 char *hdr_beg, *hdr_end, *del_from;
3174 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3175 int preserve_hdr;
3176
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003177 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003178 ctx.blk = NULL;
3179 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003180 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003181 del_from = NULL; /* nothing to be deleted */
3182 preserve_hdr = 0; /* assume we may kill the whole header */
3183
3184 /* Now look for cookies. Conforming to RFC2109, we have to support
3185 * attributes whose name begin with a '$', and associate them with
3186 * the right cookie, if we want to delete this cookie.
3187 * So there are 3 cases for each cookie read :
3188 * 1) it's a special attribute, beginning with a '$' : ignore it.
3189 * 2) it's a server id cookie that we *MAY* want to delete : save
3190 * some pointers on it (last semi-colon, beginning of cookie...)
3191 * 3) it's an application cookie : we *MAY* have to delete a previous
3192 * "special" cookie.
3193 * At the end of loop, if a "special" cookie remains, we may have to
3194 * remove it. If no application cookie persists in the header, we
3195 * *MUST* delete it.
3196 *
3197 * Note: RFC2965 is unclear about the processing of spaces around
3198 * the equal sign in the ATTR=VALUE form. A careful inspection of
3199 * the RFC explicitly allows spaces before it, and not within the
3200 * tokens (attrs or values). An inspection of RFC2109 allows that
3201 * too but section 10.1.3 lets one think that spaces may be allowed
3202 * after the equal sign too, resulting in some (rare) buggy
3203 * implementations trying to do that. So let's do what servers do.
3204 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3205 * allowed quoted strings in values, with any possible character
3206 * after a backslash, including control chars and delimitors, which
3207 * causes parsing to become ambiguous. Browsers also allow spaces
3208 * within values even without quotes.
3209 *
3210 * We have to keep multiple pointers in order to support cookie
3211 * removal at the beginning, middle or end of header without
3212 * corrupting the header. All of these headers are valid :
3213 *
3214 * hdr_beg hdr_end
3215 * | |
3216 * v |
3217 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3218 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3219 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3220 * | | | | | | |
3221 * | | | | | | |
3222 * | | | | | | +--> next
3223 * | | | | | +----> val_end
3224 * | | | | +-----------> val_beg
3225 * | | | +--------------> equal
3226 * | | +----------------> att_end
3227 * | +---------------------> att_beg
3228 * +--------------------------> prev
3229 *
3230 */
3231 hdr_beg = ctx.value.ptr;
3232 hdr_end = hdr_beg + ctx.value.len;
3233 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3234 /* Iterate through all cookies on this line */
3235
3236 /* find att_beg */
3237 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003238 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003239 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003240 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003241
3242 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3243 att_beg++;
3244
3245 /* find att_end : this is the first character after the last non
3246 * space before the equal. It may be equal to hdr_end.
3247 */
3248 equal = att_end = att_beg;
3249 while (equal < hdr_end) {
3250 if (*equal == '=' || *equal == ',' || *equal == ';')
3251 break;
3252 if (HTTP_IS_SPHT(*equal++))
3253 continue;
3254 att_end = equal;
3255 }
3256
3257 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3258 * is between <att_beg> and <equal>, both may be identical.
3259 */
3260 /* look for end of cookie if there is an equal sign */
3261 if (equal < hdr_end && *equal == '=') {
3262 /* look for the beginning of the value */
3263 val_beg = equal + 1;
3264 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3265 val_beg++;
3266
3267 /* find the end of the value, respecting quotes */
3268 next = http_find_cookie_value_end(val_beg, hdr_end);
3269
3270 /* make val_end point to the first white space or delimitor after the value */
3271 val_end = next;
3272 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3273 val_end--;
3274 }
3275 else
3276 val_beg = val_end = next = equal;
3277
3278 /* We have nothing to do with attributes beginning with
3279 * '$'. However, they will automatically be removed if a
3280 * header before them is removed, since they're supposed
3281 * to be linked together.
3282 */
3283 if (*att_beg == '$')
3284 continue;
3285
3286 /* Ignore cookies with no equal sign */
3287 if (equal == next) {
3288 /* This is not our cookie, so we must preserve it. But if we already
3289 * scheduled another cookie for removal, we cannot remove the
3290 * complete header, but we can remove the previous block itself.
3291 */
3292 preserve_hdr = 1;
3293 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003294 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003295 val_end += delta;
3296 next += delta;
3297 hdr_end += delta;
3298 prev = del_from;
3299 del_from = NULL;
3300 }
3301 continue;
3302 }
3303
3304 /* if there are spaces around the equal sign, we need to
3305 * strip them otherwise we'll get trouble for cookie captures,
3306 * or even for rewrites. Since this happens extremely rarely,
3307 * it does not hurt performance.
3308 */
3309 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3310 int stripped_before = 0;
3311 int stripped_after = 0;
3312
3313 if (att_end != equal) {
3314 memmove(att_end, equal, hdr_end - equal);
3315 stripped_before = (att_end - equal);
3316 equal += stripped_before;
3317 val_beg += stripped_before;
3318 }
3319
3320 if (val_beg > equal + 1) {
3321 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3322 stripped_after = (equal + 1) - val_beg;
3323 val_beg += stripped_after;
3324 stripped_before += stripped_after;
3325 }
3326
3327 val_end += stripped_before;
3328 next += stripped_before;
3329 hdr_end += stripped_before;
3330 }
3331 /* now everything is as on the diagram above */
3332
3333 /* First, let's see if we want to capture this cookie. We check
3334 * that we don't already have a client side cookie, because we
3335 * can only capture one. Also as an optimisation, we ignore
3336 * cookies shorter than the declared name.
3337 */
3338 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3339 (val_end - att_beg >= sess->fe->capture_namelen) &&
3340 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3341 int log_len = val_end - att_beg;
3342
3343 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3344 ha_alert("HTTP logging : out of memory.\n");
3345 } else {
3346 if (log_len > sess->fe->capture_len)
3347 log_len = sess->fe->capture_len;
3348 memcpy(txn->cli_cookie, att_beg, log_len);
3349 txn->cli_cookie[log_len] = 0;
3350 }
3351 }
3352
3353 /* Persistence cookies in passive, rewrite or insert mode have the
3354 * following form :
3355 *
3356 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3357 *
3358 * For cookies in prefix mode, the form is :
3359 *
3360 * Cookie: NAME=SRV~VALUE
3361 */
3362 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3363 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3364 struct server *srv = s->be->srv;
3365 char *delim;
3366
3367 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3368 * have the server ID between val_beg and delim, and the original cookie between
3369 * delim+1 and val_end. Otherwise, delim==val_end :
3370 *
3371 * hdr_beg
3372 * |
3373 * v
3374 * NAME=SRV; # in all but prefix modes
3375 * NAME=SRV~OPAQUE ; # in prefix mode
3376 * || || | |+-> next
3377 * || || | +--> val_end
3378 * || || +---------> delim
3379 * || |+------------> val_beg
3380 * || +-------------> att_end = equal
3381 * |+-----------------> att_beg
3382 * +------------------> prev
3383 *
3384 */
3385 if (s->be->ck_opts & PR_CK_PFX) {
3386 for (delim = val_beg; delim < val_end; delim++)
3387 if (*delim == COOKIE_DELIM)
3388 break;
3389 }
3390 else {
3391 char *vbar1;
3392 delim = val_end;
3393 /* Now check if the cookie contains a date field, which would
3394 * appear after a vertical bar ('|') just after the server name
3395 * and before the delimiter.
3396 */
3397 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3398 if (vbar1) {
3399 /* OK, so left of the bar is the server's cookie and
3400 * right is the last seen date. It is a base64 encoded
3401 * 30-bit value representing the UNIX date since the
3402 * epoch in 4-second quantities.
3403 */
3404 int val;
3405 delim = vbar1++;
3406 if (val_end - vbar1 >= 5) {
3407 val = b64tos30(vbar1);
3408 if (val > 0)
3409 txn->cookie_last_date = val << 2;
3410 }
3411 /* look for a second vertical bar */
3412 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3413 if (vbar1 && (val_end - vbar1 > 5)) {
3414 val = b64tos30(vbar1 + 1);
3415 if (val > 0)
3416 txn->cookie_first_date = val << 2;
3417 }
3418 }
3419 }
3420
3421 /* if the cookie has an expiration date and the proxy wants to check
3422 * it, then we do that now. We first check if the cookie is too old,
3423 * then only if it has expired. We detect strict overflow because the
3424 * time resolution here is not great (4 seconds). Cookies with dates
3425 * in the future are ignored if their offset is beyond one day. This
3426 * allows an admin to fix timezone issues without expiring everyone
3427 * and at the same time avoids keeping unwanted side effects for too
3428 * long.
3429 */
3430 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3431 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3432 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3433 txn->flags &= ~TX_CK_MASK;
3434 txn->flags |= TX_CK_OLD;
3435 delim = val_beg; // let's pretend we have not found the cookie
3436 txn->cookie_first_date = 0;
3437 txn->cookie_last_date = 0;
3438 }
3439 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3440 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3441 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3442 txn->flags &= ~TX_CK_MASK;
3443 txn->flags |= TX_CK_EXPIRED;
3444 delim = val_beg; // let's pretend we have not found the cookie
3445 txn->cookie_first_date = 0;
3446 txn->cookie_last_date = 0;
3447 }
3448
3449 /* Here, we'll look for the first running server which supports the cookie.
3450 * This allows to share a same cookie between several servers, for example
3451 * to dedicate backup servers to specific servers only.
3452 * However, to prevent clients from sticking to cookie-less backup server
3453 * when they have incidentely learned an empty cookie, we simply ignore
3454 * empty cookies and mark them as invalid.
3455 * The same behaviour is applied when persistence must be ignored.
3456 */
3457 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3458 srv = NULL;
3459
3460 while (srv) {
3461 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3462 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3463 if ((srv->cur_state != SRV_ST_STOPPED) ||
3464 (s->be->options & PR_O_PERSIST) ||
3465 (s->flags & SF_FORCE_PRST)) {
3466 /* we found the server and we can use it */
3467 txn->flags &= ~TX_CK_MASK;
3468 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3469 s->flags |= SF_DIRECT | SF_ASSIGNED;
3470 s->target = &srv->obj_type;
3471 break;
3472 } else {
3473 /* we found a server, but it's down,
3474 * mark it as such and go on in case
3475 * another one is available.
3476 */
3477 txn->flags &= ~TX_CK_MASK;
3478 txn->flags |= TX_CK_DOWN;
3479 }
3480 }
3481 srv = srv->next;
3482 }
3483
3484 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3485 /* no server matched this cookie or we deliberately skipped it */
3486 txn->flags &= ~TX_CK_MASK;
3487 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3488 txn->flags |= TX_CK_UNUSED;
3489 else
3490 txn->flags |= TX_CK_INVALID;
3491 }
3492
3493 /* depending on the cookie mode, we may have to either :
3494 * - delete the complete cookie if we're in insert+indirect mode, so that
3495 * the server never sees it ;
3496 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003497 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003498 * if we're in cookie prefix mode
3499 */
3500 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3501 int delta; /* negative */
3502
3503 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3504 delta = val_beg - (delim + 1);
3505 val_end += delta;
3506 next += delta;
3507 hdr_end += delta;
3508 del_from = NULL;
3509 preserve_hdr = 1; /* we want to keep this cookie */
3510 }
3511 else if (del_from == NULL &&
3512 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3513 del_from = prev;
3514 }
3515 }
3516 else {
3517 /* This is not our cookie, so we must preserve it. But if we already
3518 * scheduled another cookie for removal, we cannot remove the
3519 * complete header, but we can remove the previous block itself.
3520 */
3521 preserve_hdr = 1;
3522
3523 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003524 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003525 if (att_beg >= del_from)
3526 att_beg += delta;
3527 if (att_end >= del_from)
3528 att_end += delta;
3529 val_beg += delta;
3530 val_end += delta;
3531 next += delta;
3532 hdr_end += delta;
3533 prev = del_from;
3534 del_from = NULL;
3535 }
3536 }
3537
3538 /* continue with next cookie on this header line */
3539 att_beg = next;
3540 } /* for each cookie */
3541
3542
3543 /* There are no more cookies on this line.
3544 * We may still have one (or several) marked for deletion at the
3545 * end of the line. We must do this now in two ways :
3546 * - if some cookies must be preserved, we only delete from the
3547 * mark to the end of line ;
3548 * - if nothing needs to be preserved, simply delete the whole header
3549 */
3550 if (del_from) {
3551 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3552 }
3553 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003554 if (hdr_beg != hdr_end)
3555 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003556 else
3557 http_remove_header(htx, &ctx);
3558 }
3559 } /* for each "Cookie header */
3560}
3561
3562/*
3563 * Manage server-side cookies. It can impact performance by about 2% so it is
3564 * desirable to call it only when needed. This function is also used when we
3565 * just need to know if there is a cookie (eg: for check-cache).
3566 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003567static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003568{
3569 struct session *sess = s->sess;
3570 struct http_txn *txn = s->txn;
3571 struct htx *htx;
3572 struct http_hdr_ctx ctx;
3573 struct server *srv;
3574 char *hdr_beg, *hdr_end;
3575 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003576 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003577
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003578 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003579
3580 ctx.blk = NULL;
3581 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003582 int is_first = 1;
3583
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003584 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3585 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3586 break;
3587 is_cookie2 = 1;
3588 }
3589
3590 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3591 * <prev> points to the colon.
3592 */
3593 txn->flags |= TX_SCK_PRESENT;
3594
3595 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3596 * check-cache is enabled) and we are not interested in checking
3597 * them. Warning, the cookie capture is declared in the frontend.
3598 */
3599 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3600 break;
3601
3602 /* OK so now we know we have to process this response cookie.
3603 * The format of the Set-Cookie header is slightly different
3604 * from the format of the Cookie header in that it does not
3605 * support the comma as a cookie delimiter (thus the header
3606 * cannot be folded) because the Expires attribute described in
3607 * the original Netscape's spec may contain an unquoted date
3608 * with a comma inside. We have to live with this because
3609 * many browsers don't support Max-Age and some browsers don't
3610 * support quoted strings. However the Set-Cookie2 header is
3611 * clean.
3612 *
3613 * We have to keep multiple pointers in order to support cookie
3614 * removal at the beginning, middle or end of header without
3615 * corrupting the header (in case of set-cookie2). A special
3616 * pointer, <scav> points to the beginning of the set-cookie-av
3617 * fields after the first semi-colon. The <next> pointer points
3618 * either to the end of line (set-cookie) or next unquoted comma
3619 * (set-cookie2). All of these headers are valid :
3620 *
3621 * hdr_beg hdr_end
3622 * | |
3623 * v |
3624 * NAME1 = VALUE 1 ; Secure; Path="/" |
3625 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3626 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3627 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3628 * | | | | | | | |
3629 * | | | | | | | +-> next
3630 * | | | | | | +------------> scav
3631 * | | | | | +--------------> val_end
3632 * | | | | +--------------------> val_beg
3633 * | | | +----------------------> equal
3634 * | | +------------------------> att_end
3635 * | +----------------------------> att_beg
3636 * +------------------------------> prev
3637 * -------------------------------> hdr_beg
3638 */
3639 hdr_beg = ctx.value.ptr;
3640 hdr_end = hdr_beg + ctx.value.len;
3641 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3642
3643 /* Iterate through all cookies on this line */
3644
3645 /* find att_beg */
3646 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003647 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003648 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003649 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003650
3651 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3652 att_beg++;
3653
3654 /* find att_end : this is the first character after the last non
3655 * space before the equal. It may be equal to hdr_end.
3656 */
3657 equal = att_end = att_beg;
3658
3659 while (equal < hdr_end) {
3660 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3661 break;
3662 if (HTTP_IS_SPHT(*equal++))
3663 continue;
3664 att_end = equal;
3665 }
3666
3667 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3668 * is between <att_beg> and <equal>, both may be identical.
3669 */
3670
3671 /* look for end of cookie if there is an equal sign */
3672 if (equal < hdr_end && *equal == '=') {
3673 /* look for the beginning of the value */
3674 val_beg = equal + 1;
3675 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3676 val_beg++;
3677
3678 /* find the end of the value, respecting quotes */
3679 next = http_find_cookie_value_end(val_beg, hdr_end);
3680
3681 /* make val_end point to the first white space or delimitor after the value */
3682 val_end = next;
3683 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3684 val_end--;
3685 }
3686 else {
3687 /* <equal> points to next comma, semi-colon or EOL */
3688 val_beg = val_end = next = equal;
3689 }
3690
3691 if (next < hdr_end) {
3692 /* Set-Cookie2 supports multiple cookies, and <next> points to
3693 * a colon or semi-colon before the end. So skip all attr-value
3694 * pairs and look for the next comma. For Set-Cookie, since
3695 * commas are permitted in values, skip to the end.
3696 */
3697 if (is_cookie2)
3698 next = http_find_hdr_value_end(next, hdr_end);
3699 else
3700 next = hdr_end;
3701 }
3702
3703 /* Now everything is as on the diagram above */
3704
3705 /* Ignore cookies with no equal sign */
3706 if (equal == val_end)
3707 continue;
3708
3709 /* If there are spaces around the equal sign, we need to
3710 * strip them otherwise we'll get trouble for cookie captures,
3711 * or even for rewrites. Since this happens extremely rarely,
3712 * it does not hurt performance.
3713 */
3714 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3715 int stripped_before = 0;
3716 int stripped_after = 0;
3717
3718 if (att_end != equal) {
3719 memmove(att_end, equal, hdr_end - equal);
3720 stripped_before = (att_end - equal);
3721 equal += stripped_before;
3722 val_beg += stripped_before;
3723 }
3724
3725 if (val_beg > equal + 1) {
3726 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3727 stripped_after = (equal + 1) - val_beg;
3728 val_beg += stripped_after;
3729 stripped_before += stripped_after;
3730 }
3731
3732 val_end += stripped_before;
3733 next += stripped_before;
3734 hdr_end += stripped_before;
3735
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003736 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003737 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003738 }
3739
3740 /* First, let's see if we want to capture this cookie. We check
3741 * that we don't already have a server side cookie, because we
3742 * can only capture one. Also as an optimisation, we ignore
3743 * cookies shorter than the declared name.
3744 */
3745 if (sess->fe->capture_name != NULL &&
3746 txn->srv_cookie == NULL &&
3747 (val_end - att_beg >= sess->fe->capture_namelen) &&
3748 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3749 int log_len = val_end - att_beg;
3750 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3751 ha_alert("HTTP logging : out of memory.\n");
3752 }
3753 else {
3754 if (log_len > sess->fe->capture_len)
3755 log_len = sess->fe->capture_len;
3756 memcpy(txn->srv_cookie, att_beg, log_len);
3757 txn->srv_cookie[log_len] = 0;
3758 }
3759 }
3760
3761 srv = objt_server(s->target);
3762 /* now check if we need to process it for persistence */
3763 if (!(s->flags & SF_IGNORE_PRST) &&
3764 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3765 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3766 /* assume passive cookie by default */
3767 txn->flags &= ~TX_SCK_MASK;
3768 txn->flags |= TX_SCK_FOUND;
3769
3770 /* If the cookie is in insert mode on a known server, we'll delete
3771 * this occurrence because we'll insert another one later.
3772 * We'll delete it too if the "indirect" option is set and we're in
3773 * a direct access.
3774 */
3775 if (s->be->ck_opts & PR_CK_PSV) {
3776 /* The "preserve" flag was set, we don't want to touch the
3777 * server's cookie.
3778 */
3779 }
3780 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3781 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3782 /* this cookie must be deleted */
3783 if (prev == hdr_beg && next == hdr_end) {
3784 /* whole header */
3785 http_remove_header(htx, &ctx);
3786 /* note: while both invalid now, <next> and <hdr_end>
3787 * are still equal, so the for() will stop as expected.
3788 */
3789 } else {
3790 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003791 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003792 next = prev;
3793 hdr_end += delta;
3794 }
3795 txn->flags &= ~TX_SCK_MASK;
3796 txn->flags |= TX_SCK_DELETED;
3797 /* and go on with next cookie */
3798 }
3799 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3800 /* replace bytes val_beg->val_end with the cookie name associated
3801 * with this server since we know it.
3802 */
3803 int sliding, delta;
3804
3805 ctx.value = ist2(val_beg, val_end - val_beg);
3806 ctx.lws_before = ctx.lws_after = 0;
3807 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3808 delta = srv->cklen - (val_end - val_beg);
3809 sliding = (ctx.value.ptr - val_beg);
3810 hdr_beg += sliding;
3811 val_beg += sliding;
3812 next += sliding + delta;
3813 hdr_end += sliding + delta;
3814
3815 txn->flags &= ~TX_SCK_MASK;
3816 txn->flags |= TX_SCK_REPLACED;
3817 }
3818 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3819 /* insert the cookie name associated with this server
3820 * before existing cookie, and insert a delimiter between them..
3821 */
3822 int sliding, delta;
3823 ctx.value = ist2(val_beg, 0);
3824 ctx.lws_before = ctx.lws_after = 0;
3825 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3826 delta = srv->cklen + 1;
3827 sliding = (ctx.value.ptr - val_beg);
3828 hdr_beg += sliding;
3829 val_beg += sliding;
3830 next += sliding + delta;
3831 hdr_end += sliding + delta;
3832
3833 val_beg[srv->cklen] = COOKIE_DELIM;
3834 txn->flags &= ~TX_SCK_MASK;
3835 txn->flags |= TX_SCK_REPLACED;
3836 }
3837 }
3838 /* that's done for this cookie, check the next one on the same
3839 * line when next != hdr_end (only if is_cookie2).
3840 */
3841 }
3842 }
3843}
3844
Christopher Faulet25a02f62018-10-24 12:00:25 +02003845/*
3846 * Parses the Cache-Control and Pragma request header fields to determine if
3847 * the request may be served from the cache and/or if it is cacheable. Updates
3848 * s->txn->flags.
3849 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003850void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003851{
3852 struct http_txn *txn = s->txn;
3853 struct htx *htx;
3854 int32_t pos;
3855 int pragma_found, cc_found, i;
3856
3857 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3858 return; /* nothing more to do here */
3859
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003860 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003861 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02003862 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003863 struct htx_blk *blk = htx_get_blk(htx, pos);
3864 enum htx_blk_type type = htx_get_blk_type(blk);
3865 struct ist n, v;
3866
3867 if (type == HTX_BLK_EOH)
3868 break;
3869 if (type != HTX_BLK_HDR)
3870 continue;
3871
3872 n = htx_get_blk_name(htx, blk);
3873 v = htx_get_blk_value(htx, blk);
3874
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003875 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003876 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3877 pragma_found = 1;
3878 continue;
3879 }
3880 }
3881
3882 /* Don't use the cache and don't try to store if we found the
3883 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003884 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003885 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3886 txn->flags |= TX_CACHE_IGNORE;
3887 continue;
3888 }
3889
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003890 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02003891 continue;
3892
3893 /* OK, right now we know we have a cache-control header */
3894 cc_found = 1;
3895 if (!v.len) /* no info */
3896 continue;
3897
3898 i = 0;
3899 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
3900 !isspace((unsigned char)*(v.ptr+i)))
3901 i++;
3902
3903 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
3904 * values after max-age, max-stale nor min-fresh, we simply don't
3905 * use the cache when they're specified.
3906 */
3907 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
3908 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
3909 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
3910 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
3911 txn->flags |= TX_CACHE_IGNORE;
3912 continue;
3913 }
3914
3915 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
3916 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3917 continue;
3918 }
3919 }
3920
3921 /* RFC7234#5.4:
3922 * When the Cache-Control header field is also present and
3923 * understood in a request, Pragma is ignored.
3924 * When the Cache-Control header field is not present in a
3925 * request, caches MUST consider the no-cache request
3926 * pragma-directive as having the same effect as if
3927 * "Cache-Control: no-cache" were present.
3928 */
3929 if (!cc_found && pragma_found)
3930 txn->flags |= TX_CACHE_IGNORE;
3931}
3932
3933/*
3934 * Check if response is cacheable or not. Updates s->txn->flags.
3935 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003936void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003937{
3938 struct http_txn *txn = s->txn;
3939 struct htx *htx;
3940 int32_t pos;
3941 int i;
3942
3943 if (txn->status < 200) {
3944 /* do not try to cache interim responses! */
3945 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3946 return;
3947 }
3948
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003949 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02003950 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003951 struct htx_blk *blk = htx_get_blk(htx, pos);
3952 enum htx_blk_type type = htx_get_blk_type(blk);
3953 struct ist n, v;
3954
3955 if (type == HTX_BLK_EOH)
3956 break;
3957 if (type != HTX_BLK_HDR)
3958 continue;
3959
3960 n = htx_get_blk_name(htx, blk);
3961 v = htx_get_blk_value(htx, blk);
3962
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003963 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003964 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3965 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3966 return;
3967 }
3968 }
3969
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003970 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02003971 continue;
3972
3973 /* OK, right now we know we have a cache-control header */
3974 if (!v.len) /* no info */
3975 continue;
3976
3977 i = 0;
3978 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
3979 !isspace((unsigned char)*(v.ptr+i)))
3980 i++;
3981
3982 /* we have a complete value between v.ptr and (v.ptr+i) */
3983 if (i < v.len && *(v.ptr + i) == '=') {
3984 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
3985 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
3986 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3987 continue;
3988 }
3989
3990 /* we have something of the form no-cache="set-cookie" */
3991 if ((v.len >= 21) &&
3992 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
3993 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
3994 txn->flags &= ~TX_CACHE_COOK;
3995 continue;
3996 }
3997
3998 /* OK, so we know that either p2 points to the end of string or to a comma */
3999 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
4000 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4001 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
4002 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4003 return;
4004 }
4005
4006 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4007 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4008 continue;
4009 }
4010 }
4011}
4012
Christopher Faulet377c5a52018-10-24 21:21:30 +02004013/*
4014 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4015 * for the current backend.
4016 *
4017 * It is assumed that the request is either a HEAD, GET, or POST and that the
4018 * uri_auth field is valid.
4019 *
4020 * Returns 1 if stats should be provided, otherwise 0.
4021 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004022static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004023{
4024 struct uri_auth *uri_auth = backend->uri_auth;
4025 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004026 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004027 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004028
4029 if (!uri_auth)
4030 return 0;
4031
4032 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4033 return 0;
4034
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004035 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004036 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004037 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01004038 if (*uri_auth->uri_prefix == '/')
4039 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004040
4041 /* check URI size */
4042 if (uri_auth->uri_len > uri.len)
4043 return 0;
4044
4045 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4046 return 0;
4047
4048 return 1;
4049}
4050
4051/* This function prepares an applet to handle the stats. It can deal with the
4052 * "100-continue" expectation, check that admin rules are met for POST requests,
4053 * and program a response message if something was unexpected. It cannot fail
4054 * and always relies on the stats applet to complete the job. It does not touch
4055 * analysers nor counters, which are left to the caller. It does not touch
4056 * s->target which is supposed to already point to the stats applet. The caller
4057 * is expected to have already assigned an appctx to the stream.
4058 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004059static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004060{
4061 struct stats_admin_rule *stats_admin_rule;
4062 struct stream_interface *si = &s->si[1];
4063 struct session *sess = s->sess;
4064 struct http_txn *txn = s->txn;
4065 struct http_msg *msg = &txn->req;
4066 struct uri_auth *uri_auth = s->be->uri_auth;
4067 const char *h, *lookup, *end;
4068 struct appctx *appctx;
4069 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004070 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004071
4072 appctx = si_appctx(si);
4073 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4074 appctx->st1 = appctx->st2 = 0;
4075 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004076 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004077 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4078 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4079 appctx->ctx.stats.flags |= STAT_CHUNKED;
4080
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004081 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004082 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004083 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4084 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004085
4086 for (h = lookup; h <= end - 3; h++) {
4087 if (memcmp(h, ";up", 3) == 0) {
4088 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4089 break;
4090 }
4091 }
4092
4093 if (uri_auth->refresh) {
4094 for (h = lookup; h <= end - 10; h++) {
4095 if (memcmp(h, ";norefresh", 10) == 0) {
4096 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4097 break;
4098 }
4099 }
4100 }
4101
4102 for (h = lookup; h <= end - 4; h++) {
4103 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004104 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004105 break;
4106 }
4107 }
4108
4109 for (h = lookup; h <= end - 6; h++) {
4110 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004111 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004112 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4113 break;
4114 }
4115 }
4116
Christopher Faulet6338a082019-09-09 15:50:54 +02004117 for (h = lookup; h <= end - 5; h++) {
4118 if (memcmp(h, ";json", 5) == 0) {
4119 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4120 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4121 break;
4122 }
4123 }
4124
4125 for (h = lookup; h <= end - 12; h++) {
4126 if (memcmp(h, ";json-schema", 12) == 0) {
4127 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4128 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4129 break;
4130 }
4131 }
4132
Christopher Faulet377c5a52018-10-24 21:21:30 +02004133 for (h = lookup; h <= end - 8; h++) {
4134 if (memcmp(h, ";st=", 4) == 0) {
4135 int i;
4136 h += 4;
4137 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4138 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4139 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4140 appctx->ctx.stats.st_code = i;
4141 break;
4142 }
4143 }
4144 break;
4145 }
4146 }
4147
4148 appctx->ctx.stats.scope_str = 0;
4149 appctx->ctx.stats.scope_len = 0;
4150 for (h = lookup; h <= end - 8; h++) {
4151 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4152 int itx = 0;
4153 const char *h2;
4154 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4155 const char *err;
4156
4157 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4158 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004159 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4160 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004161 if (*h == ';' || *h == '&' || *h == ' ')
4162 break;
4163 itx++;
4164 h++;
4165 }
4166
4167 if (itx > STAT_SCOPE_TXT_MAXLEN)
4168 itx = STAT_SCOPE_TXT_MAXLEN;
4169 appctx->ctx.stats.scope_len = itx;
4170
4171 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4172 memcpy(scope_txt, h2, itx);
4173 scope_txt[itx] = '\0';
4174 err = invalid_char(scope_txt);
4175 if (err) {
4176 /* bad char in search text => clear scope */
4177 appctx->ctx.stats.scope_str = 0;
4178 appctx->ctx.stats.scope_len = 0;
4179 }
4180 break;
4181 }
4182 }
4183
4184 /* now check whether we have some admin rules for this request */
4185 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4186 int ret = 1;
4187
4188 if (stats_admin_rule->cond) {
4189 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4190 ret = acl_pass(ret);
4191 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4192 ret = !ret;
4193 }
4194
4195 if (ret) {
4196 /* no rule, or the rule matches */
4197 appctx->ctx.stats.flags |= STAT_ADMIN;
4198 break;
4199 }
4200 }
4201
Christopher Faulet5d45e382019-02-27 15:15:23 +01004202 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4203 appctx->st0 = STAT_HTTP_HEAD;
4204 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004205 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004206 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004207 if (msg->msg_state < HTTP_MSG_DATA)
4208 req->analysers |= AN_REQ_HTTP_BODY;
4209 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004210 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004211 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004212 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4213 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4214 appctx->st0 = STAT_HTTP_LAST;
4215 }
4216 }
4217 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004218 /* Unsupported method */
4219 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4220 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4221 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004222 }
4223
4224 s->task->nice = -32; /* small boost for HTTP statistics */
4225 return 1;
4226}
4227
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004228void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004229{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004230 struct channel *req = &s->req;
4231 struct channel *res = &s->res;
4232 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004233 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004234 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004235 struct ist path, location;
4236 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004237
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004238 /*
4239 * Create the location
4240 */
4241 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004242
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004243 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004244 /* special prefix "/" means don't change URL */
4245 srv = __objt_server(s->target);
4246 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4247 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4248 return;
4249 }
4250
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004251 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004252 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004253 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004254 path = http_get_path(htx_sl_req_uri(sl));
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004255 if (!path.ptr)
4256 return;
4257
4258 if (!chunk_memcat(&trash, path.ptr, path.len))
4259 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004260 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004261
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004262 /*
4263 * Create the 302 respone
4264 */
4265 htx = htx_from_buf(&res->buf);
4266 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4267 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4268 ist("HTTP/1.1"), ist("302"), ist("Found"));
4269 if (!sl)
4270 goto fail;
4271 sl->info.res.status = 302;
4272 s->txn->status = 302;
4273
4274 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4275 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4276 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4277 !htx_add_header(htx, ist("Location"), location))
4278 goto fail;
4279
4280 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4281 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004282
Christopher Fauletc20afb82020-01-24 19:16:26 +01004283 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004284 if (!http_forward_proxy_resp(s, 1))
4285 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004286
4287 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004288 si_shutr(si);
4289 si_shutw(si);
4290 si->err_type = SI_ET_NONE;
4291 si->state = SI_ST_CLO;
4292
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004293 if (!(s->flags & SF_ERR_MASK))
4294 s->flags |= SF_ERR_LOCAL;
4295 if (!(s->flags & SF_FINST_MASK))
4296 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004297
4298 /* FIXME: we should increase a counter of redirects per server and per backend. */
4299 srv_inc_sess_ctr(srv);
4300 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004301 return;
4302
4303 fail:
4304 /* If an error occurred, remove the incomplete HTTP response from the
4305 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004306 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004307}
4308
Christopher Fauletf2824e62018-10-01 12:12:37 +02004309/* This function terminates the request because it was completly analyzed or
4310 * because an error was triggered during the body forwarding.
4311 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004312static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004313{
4314 struct channel *chn = &s->req;
4315 struct http_txn *txn = s->txn;
4316
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004317 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004318
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004319 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4320 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004321 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004322 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004323 goto end;
4324 }
4325
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004326 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4327 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004328 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004329 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004330
4331 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004332 /* No need to read anymore, the request was completely parsed.
4333 * We can shut the read side unless we want to abort_on_close,
4334 * or we have a POST request. The issue with POST requests is
4335 * that some browsers still send a CRLF after the request, and
4336 * this CRLF must be read so that it does not remain in the kernel
4337 * buffers, otherwise a close could cause an RST on some systems
4338 * (eg: Linux).
4339 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004340 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004341 channel_dont_read(chn);
4342
4343 /* if the server closes the connection, we want to immediately react
4344 * and close the socket to save packets and syscalls.
4345 */
4346 s->si[1].flags |= SI_FL_NOHALF;
4347
4348 /* In any case we've finished parsing the request so we must
4349 * disable Nagle when sending data because 1) we're not going
4350 * to shut this side, and 2) the server is waiting for us to
4351 * send pending data.
4352 */
4353 chn->flags |= CF_NEVER_WAIT;
4354
Christopher Fauletd01ce402019-01-02 17:44:13 +01004355 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4356 /* The server has not finished to respond, so we
4357 * don't want to move in order not to upset it.
4358 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004359 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004360 return;
4361 }
4362
Christopher Fauletf2824e62018-10-01 12:12:37 +02004363 /* When we get here, it means that both the request and the
4364 * response have finished receiving. Depending on the connection
4365 * mode, we'll have to wait for the last bytes to leave in either
4366 * direction, and sometimes for a close to be effective.
4367 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004368 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004369 /* Tunnel mode will not have any analyser so it needs to
4370 * poll for reads.
4371 */
4372 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004373 if (b_data(&chn->buf)) {
4374 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004375 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004376 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004377 txn->req.msg_state = HTTP_MSG_TUNNEL;
4378 }
4379 else {
4380 /* we're not expecting any new data to come for this
4381 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004382 *
4383 * However, there is an exception if the response
4384 * length is undefined. In this case, we need to wait
4385 * the close from the server. The response will be
4386 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004387 */
4388 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4389 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004390 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004391
4392 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4393 channel_shutr_now(chn);
4394 channel_shutw_now(chn);
4395 }
4396 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004397 goto check_channel_flags;
4398 }
4399
4400 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4401 http_msg_closing:
4402 /* nothing else to forward, just waiting for the output buffer
4403 * to be empty and for the shutw_now to take effect.
4404 */
4405 if (channel_is_empty(chn)) {
4406 txn->req.msg_state = HTTP_MSG_CLOSED;
4407 goto http_msg_closed;
4408 }
4409 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004410 txn->req.msg_state = HTTP_MSG_ERROR;
4411 goto end;
4412 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004413 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004414 return;
4415 }
4416
4417 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4418 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004419 /* if we don't know whether the server will close, we need to hard close */
4420 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4421 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004422 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004423 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004424 channel_dont_read(chn);
4425 goto end;
4426 }
4427
4428 check_channel_flags:
4429 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4430 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4431 /* if we've just closed an output, let's switch */
4432 txn->req.msg_state = HTTP_MSG_CLOSING;
4433 goto http_msg_closing;
4434 }
4435
4436 end:
4437 chn->analysers &= AN_REQ_FLT_END;
4438 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4439 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4440 channel_auto_close(chn);
4441 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004442 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004443}
4444
4445
4446/* This function terminates the response because it was completly analyzed or
4447 * because an error was triggered during the body forwarding.
4448 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004449static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004450{
4451 struct channel *chn = &s->res;
4452 struct http_txn *txn = s->txn;
4453
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004454 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004455
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004456 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4457 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004458 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004459 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004460 goto end;
4461 }
4462
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004463 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4464 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004465 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004466 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004467
4468 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4469 /* In theory, we don't need to read anymore, but we must
4470 * still monitor the server connection for a possible close
4471 * while the request is being uploaded, so we don't disable
4472 * reading.
4473 */
4474 /* channel_dont_read(chn); */
4475
4476 if (txn->req.msg_state < HTTP_MSG_DONE) {
4477 /* The client seems to still be sending data, probably
4478 * because we got an error response during an upload.
4479 * We have the choice of either breaking the connection
4480 * or letting it pass through. Let's do the later.
4481 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004482 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004483 return;
4484 }
4485
4486 /* When we get here, it means that both the request and the
4487 * response have finished receiving. Depending on the connection
4488 * mode, we'll have to wait for the last bytes to leave in either
4489 * direction, and sometimes for a close to be effective.
4490 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004491 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004492 channel_auto_read(chn);
4493 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004494 if (b_data(&chn->buf)) {
4495 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004496 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004497 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004498 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4499 }
4500 else {
4501 /* we're not expecting any new data to come for this
4502 * transaction, so we can close it.
4503 */
4504 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4505 channel_shutr_now(chn);
4506 channel_shutw_now(chn);
4507 }
4508 }
4509 goto check_channel_flags;
4510 }
4511
4512 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4513 http_msg_closing:
4514 /* nothing else to forward, just waiting for the output buffer
4515 * to be empty and for the shutw_now to take effect.
4516 */
4517 if (channel_is_empty(chn)) {
4518 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4519 goto http_msg_closed;
4520 }
4521 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004522 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004523 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004524 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004525 if (strm_sess(s)->listener->counters)
4526 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004527 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004528 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004529 goto end;
4530 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004531 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004532 return;
4533 }
4534
4535 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4536 http_msg_closed:
4537 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004538 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004539 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004540 goto end;
4541 }
4542
4543 check_channel_flags:
4544 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4545 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4546 /* if we've just closed an output, let's switch */
4547 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4548 goto http_msg_closing;
4549 }
4550
4551 end:
4552 chn->analysers &= AN_RES_FLT_END;
4553 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4554 chn->analysers |= AN_RES_FLT_XFER_DATA;
4555 channel_auto_close(chn);
4556 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004557 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004558}
4559
Christopher Fauletef70e252020-01-28 09:26:19 +01004560/* Forward a response generated by HAProxy (error/redirect/return). This
4561 * function forwards all pending incoming data. If <final> is set to 0, nothing
4562 * more is performed. It is used for 1xx informational messages. Otherwise, the
4563 * transaction is terminated and the request is emptied. On success 1 is
4564 * returned. If an error occurred, 0 is returned.
4565 */
4566int http_forward_proxy_resp(struct stream *s, int final)
4567{
4568 struct channel *req = &s->req;
4569 struct channel *res = &s->res;
4570 struct htx *htx = htxbuf(&res->buf);
4571 size_t data;
4572
4573 if (final) {
4574 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004575 if (!http_eval_after_res_rules(s))
4576 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004577
4578 channel_auto_read(req);
4579 channel_abort(req);
4580 channel_auto_close(req);
4581 channel_htx_erase(req, htxbuf(&req->buf));
4582
4583 res->wex = tick_add_ifset(now_ms, res->wto);
4584 channel_auto_read(res);
4585 channel_auto_close(res);
4586 channel_shutr_now(res);
4587 }
4588
4589 data = htx->data - co_data(res);
4590 c_adv(res, data);
4591 htx->first = -1;
4592 res->total += data;
4593 return 1;
4594}
4595
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004596void http_server_error(struct stream *s, struct stream_interface *si, int err,
4597 int finst, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004598{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004599 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004600 if (!(s->flags & SF_ERR_MASK))
4601 s->flags |= err;
4602 if (!(s->flags & SF_FINST_MASK))
4603 s->flags |= finst;
4604}
4605
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004606void http_reply_and_close(struct stream *s, short status, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004607{
4608 channel_auto_read(&s->req);
4609 channel_abort(&s->req);
4610 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004611 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
4612 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004613 channel_auto_read(&s->res);
4614 channel_auto_close(&s->res);
4615 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004616
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004617 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
Christopher Faulet0f226952018-10-22 09:29:56 +02004618 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004619
4620 /* <msg> is an HTX structure. So we copy it in the response's
4621 * channel */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004622 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004623 struct channel *chn = &s->res;
4624 struct htx *htx;
4625
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004626 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004627 htx = htx_from_buf(&chn->buf);
Christopher Faulet637259e2020-01-23 11:57:31 +01004628 if (channel_htx_copy_msg(chn, htx, msg)) {
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004629 if (!http_forward_proxy_resp(s, 1) && s->txn->status != 500) {
4630 s->txn->status = 500;
4631 http_reply_and_close(s, s->txn->status, http_error_message(s));
4632 }
Christopher Faulet637259e2020-01-23 11:57:31 +01004633 }
Christopher Faulet0f226952018-10-22 09:29:56 +02004634 }
Christopher Faulet0f226952018-10-22 09:29:56 +02004635}
4636
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004637struct buffer *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004638{
4639 const int msgnum = http_get_status_idx(s->txn->status);
4640
Christopher Faulet53a87e12020-01-21 10:13:03 +01004641 if (s->txn->errmsg)
Christopher Faulet473e8802020-01-14 11:12:37 +01004642 return s->txn->errmsg;
4643 else if (s->be->errmsg[msgnum])
Christopher Faulet58857752020-01-15 15:19:50 +01004644 return s->be->errmsg[msgnum];
4645 else if (strm_fe(s)->errmsg[msgnum])
4646 return strm_fe(s)->errmsg[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004647 else
Christopher Fauletf7346382019-07-17 22:02:08 +02004648 return &http_err_chunks[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004649}
4650
Christopher Faulet304cc402019-07-15 15:46:28 +02004651/* Return the error message corresponding to si->err_type. It is assumed
4652 * that the server side is closed. Note that err_type is actually a
4653 * bitmask, where almost only aborts may be cumulated with other
4654 * values. We consider that aborted operations are more important
4655 * than timeouts or errors due to the fact that nobody else in the
4656 * logs might explain incomplete retries. All others should avoid
4657 * being cumulated. It should normally not be possible to have multiple
4658 * aborts at once, but just in case, the first one in sequence is reported.
4659 * Note that connection errors appearing on the second request of a keep-alive
4660 * connection are not reported since this allows the client to retry.
4661 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004662void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004663{
4664 int err_type = si->err_type;
4665
4666 /* set s->txn->status for http_error_message(s) */
4667 s->txn->status = 503;
4668
4669 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004670 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4671 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004672 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004673 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4674 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4675 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004676 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004677 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4678 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004679 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004680 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4681 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004682 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004683 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4684 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4685 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004686 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004687 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4688 (s->flags & SF_SRV_REUSED) ? NULL :
4689 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004690 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004691 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4692 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4693 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004694 else { /* SI_ET_CONN_OTHER and others */
4695 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004696 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4697 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004698 }
4699}
4700
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004701
Christopher Faulet4a28a532019-03-01 11:19:40 +01004702/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4703 * on success and -1 on error.
4704 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004705static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004706{
4707 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4708 * then we must send an HTTP/1.1 100 Continue intermediate response.
4709 */
4710 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4711 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4712 struct ist hdr = { .ptr = "Expect", .len = 6 };
4713 struct http_hdr_ctx ctx;
4714
4715 ctx.blk = NULL;
4716 /* Expect is allowed in 1.1, look for it */
4717 if (http_find_header(htx, hdr, &ctx, 0) &&
4718 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004719 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004720 return -1;
4721 http_remove_header(htx, &ctx);
4722 }
4723 }
4724 return 0;
4725}
4726
Christopher Faulet23a3c792018-11-28 10:01:23 +01004727/* Send a 100-Continue response to the client. It returns 0 on success and -1
4728 * on error. The response channel is updated accordingly.
4729 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004730static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004731{
4732 struct channel *res = &s->res;
4733 struct htx *htx = htx_from_buf(&res->buf);
4734 struct htx_sl *sl;
4735 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4736 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004737
4738 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4739 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4740 if (!sl)
4741 goto fail;
4742 sl->info.res.status = 100;
4743
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004744 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004745 goto fail;
4746
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004747 if (!http_forward_proxy_resp(s, 0))
4748 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004749 return 0;
4750
4751 fail:
4752 /* If an error occurred, remove the incomplete HTTP response from the
4753 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004754 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004755 return -1;
4756}
4757
Christopher Faulet12c51e22018-11-28 15:59:42 +01004758
4759/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
4760 * ont whether we use a proxy or not. It returns 0 on success and -1 on
4761 * error. The response channel is updated accordingly.
4762 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004763static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
Christopher Faulet12c51e22018-11-28 15:59:42 +01004764{
4765 struct channel *res = &s->res;
4766 struct htx *htx = htx_from_buf(&res->buf);
4767 struct htx_sl *sl;
4768 struct ist code, body;
4769 int status;
4770 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
Christopher Faulet12c51e22018-11-28 15:59:42 +01004771
4772 if (!(s->txn->flags & TX_USE_PX_CONN)) {
4773 status = 401;
4774 code = ist("401");
4775 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
4776 "You need a valid user and password to access this content.\n"
4777 "</body></html>\n");
4778 }
4779 else {
4780 status = 407;
4781 code = ist("407");
4782 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
4783 "You need a valid user and password to access this content.\n"
4784 "</body></html>\n");
4785 }
4786
4787 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4788 ist("HTTP/1.1"), code, ist("Unauthorized"));
4789 if (!sl)
4790 goto fail;
4791 sl->info.res.status = status;
4792 s->txn->status = status;
4793
4794 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
4795 goto fail;
4796
Willy Tarreaub5ba2b02019-06-11 16:08:25 +02004797 if (!htx_add_header(htx, ist("Content-length"), ist("112")) ||
4798 !htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Faulet12c51e22018-11-28 15:59:42 +01004799 !htx_add_header(htx, ist("Connection"), ist("close")) ||
Jérôme Magnin86cef232018-12-28 14:49:08 +01004800 !htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4801 goto fail;
4802 if (status == 401 && !htx_add_header(htx, ist("WWW-Authenticate"), ist2(trash.area, trash.data)))
4803 goto fail;
4804 if (status == 407 && !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
Christopher Faulet12c51e22018-11-28 15:59:42 +01004805 goto fail;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004806 if (!htx_add_endof(htx, HTX_BLK_EOH))
4807 goto fail;
4808
4809 while (body.len) {
4810 size_t sent = htx_add_data(htx, body);
4811 if (!sent)
4812 goto fail;
4813 body.ptr += sent;
4814 body.len -= sent;
4815 }
4816
4817 if (!htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet12c51e22018-11-28 15:59:42 +01004818 goto fail;
4819
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004820 if (!http_forward_proxy_resp(s, 1))
4821 goto fail;
Christopher Faulet12c51e22018-11-28 15:59:42 +01004822 return 0;
4823
4824 fail:
4825 /* If an error occurred, remove the incomplete HTTP response from the
4826 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004827 channel_htx_truncate(res, htx);
Christopher Faulet12c51e22018-11-28 15:59:42 +01004828 return -1;
4829}
4830
Christopher Faulet0f226952018-10-22 09:29:56 +02004831/*
4832 * Capture headers from message <htx> according to header list <cap_hdr>, and
4833 * fill the <cap> pointers appropriately.
4834 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004835static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004836{
4837 struct cap_hdr *h;
4838 int32_t pos;
4839
Christopher Fauleta3f15502019-05-13 15:27:23 +02004840 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004841 struct htx_blk *blk = htx_get_blk(htx, pos);
4842 enum htx_blk_type type = htx_get_blk_type(blk);
4843 struct ist n, v;
4844
4845 if (type == HTX_BLK_EOH)
4846 break;
4847 if (type != HTX_BLK_HDR)
4848 continue;
4849
4850 n = htx_get_blk_name(htx, blk);
4851
4852 for (h = cap_hdr; h; h = h->next) {
4853 if (h->namelen && (h->namelen == n.len) &&
4854 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4855 if (cap[h->index] == NULL)
4856 cap[h->index] =
4857 pool_alloc(h->pool);
4858
4859 if (cap[h->index] == NULL) {
4860 ha_alert("HTTP capture : out of memory.\n");
4861 break;
4862 }
4863
4864 v = htx_get_blk_value(htx, blk);
4865 if (v.len > h->len)
4866 v.len = h->len;
4867
4868 memcpy(cap[h->index], v.ptr, v.len);
4869 cap[h->index][v.len]=0;
4870 }
4871 }
4872 }
4873}
4874
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004875/* Delete a value in a header between delimiters <from> and <next>. The header
4876 * itself is delimited by <start> and <end> pointers. The number of characters
4877 * displaced is returned, and the pointer to the first delimiter is updated if
4878 * required. The function tries as much as possible to respect the following
4879 * principles :
4880 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4881 * in which case <next> is simply removed
4882 * - set exactly one space character after the new first delimiter, unless there
4883 * are not enough characters in the block being moved to do so.
4884 * - remove unneeded spaces before the previous delimiter and after the new
4885 * one.
4886 *
4887 * It is the caller's responsibility to ensure that :
4888 * - <from> points to a valid delimiter or <start> ;
4889 * - <next> points to a valid delimiter or <end> ;
4890 * - there are non-space chars before <from>.
4891 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004892static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004893{
4894 char *prev = *from;
4895
4896 if (prev == start) {
4897 /* We're removing the first value. eat the semicolon, if <next>
4898 * is lower than <end> */
4899 if (next < end)
4900 next++;
4901
4902 while (next < end && HTTP_IS_SPHT(*next))
4903 next++;
4904 }
4905 else {
4906 /* Remove useless spaces before the old delimiter. */
4907 while (HTTP_IS_SPHT(*(prev-1)))
4908 prev--;
4909 *from = prev;
4910
4911 /* copy the delimiter and if possible a space if we're
4912 * not at the end of the line.
4913 */
4914 if (next < end) {
4915 *prev++ = *next++;
4916 if (prev + 1 < next)
4917 *prev++ = ' ';
4918 while (next < end && HTTP_IS_SPHT(*next))
4919 next++;
4920 }
4921 }
4922 memmove(prev, next, end - next);
4923 return (prev - next);
4924}
4925
Christopher Faulet0f226952018-10-22 09:29:56 +02004926
4927/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004928 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004929 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004930static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004931{
4932 struct ist dst = ist2(str, 0);
4933
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004934 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004935 goto end;
4936 if (dst.len + 1 > len)
4937 goto end;
4938 dst.ptr[dst.len++] = ' ';
4939
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004940 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004941 goto end;
4942 if (dst.len + 1 > len)
4943 goto end;
4944 dst.ptr[dst.len++] = ' ';
4945
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004946 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004947 end:
4948 return dst.len;
4949}
4950
4951/*
4952 * Print a debug line with a start line.
4953 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004954static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004955{
4956 struct session *sess = strm_sess(s);
4957 int max;
4958
4959 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4960 dir,
4961 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4962 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4963
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004964 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004965 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004966 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004967 trash.area[trash.data++] = ' ';
4968
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004969 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004970 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004971 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004972 trash.area[trash.data++] = ' ';
4973
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004974 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004975 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004976 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004977 trash.area[trash.data++] = '\n';
4978
4979 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
4980}
4981
4982/*
4983 * Print a debug line with a header.
4984 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004985static 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 +02004986{
4987 struct session *sess = strm_sess(s);
4988 int max;
4989
4990 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4991 dir,
4992 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4993 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4994
4995 max = n.len;
4996 UBOUND(max, trash.size - trash.data - 3);
4997 chunk_memcat(&trash, n.ptr, max);
4998 trash.area[trash.data++] = ':';
4999 trash.area[trash.data++] = ' ';
5000
5001 max = v.len;
5002 UBOUND(max, trash.size - trash.data - 1);
5003 chunk_memcat(&trash, v.ptr, max);
5004 trash.area[trash.data++] = '\n';
5005
5006 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5007}
5008
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005009/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5010 * In case of allocation failure, everything allocated is freed and NULL is
5011 * returned. Otherwise the new transaction is assigned to the stream and
5012 * returned.
5013 */
5014struct http_txn *http_alloc_txn(struct stream *s)
5015{
5016 struct http_txn *txn = s->txn;
5017
5018 if (txn)
5019 return txn;
5020
5021 txn = pool_alloc(pool_head_http_txn);
5022 if (!txn)
5023 return txn;
5024
5025 s->txn = txn;
5026 return txn;
5027}
5028
5029void http_txn_reset_req(struct http_txn *txn)
5030{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005031 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005032 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5033}
5034
5035void http_txn_reset_res(struct http_txn *txn)
5036{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005037 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005038 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5039}
5040
5041/*
5042 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5043 * the required fields are properly allocated and that we only need to (re)init
5044 * them. This should be used before processing any new request.
5045 */
5046void http_init_txn(struct stream *s)
5047{
5048 struct http_txn *txn = s->txn;
5049 struct conn_stream *cs = objt_cs(s->si[0].end);
5050
5051 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5052 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5053 : 0);
5054 txn->status = -1;
Christopher Faulet473e8802020-01-14 11:12:37 +01005055 txn->errmsg = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005056 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005057
5058 txn->cookie_first_date = 0;
5059 txn->cookie_last_date = 0;
5060
5061 txn->srv_cookie = NULL;
5062 txn->cli_cookie = NULL;
5063 txn->uri = NULL;
5064
5065 http_txn_reset_req(txn);
5066 http_txn_reset_res(txn);
5067
5068 txn->req.chn = &s->req;
5069 txn->rsp.chn = &s->res;
5070
5071 txn->auth.method = HTTP_AUTH_UNKNOWN;
5072
5073 vars_init(&s->vars_txn, SCOPE_TXN);
5074 vars_init(&s->vars_reqres, SCOPE_REQ);
5075}
5076
5077/* to be used at the end of a transaction */
5078void http_end_txn(struct stream *s)
5079{
5080 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005081
5082 /* these ones will have been dynamically allocated */
5083 pool_free(pool_head_requri, txn->uri);
5084 pool_free(pool_head_capture, txn->cli_cookie);
5085 pool_free(pool_head_capture, txn->srv_cookie);
5086 pool_free(pool_head_uniqueid, s->unique_id);
5087
5088 s->unique_id = NULL;
5089 txn->uri = NULL;
5090 txn->srv_cookie = NULL;
5091 txn->cli_cookie = NULL;
5092
Christopher Faulet59399252019-11-07 14:27:52 +01005093 if (!LIST_ISEMPTY(&s->vars_txn.head))
5094 vars_prune(&s->vars_txn, s->sess, s);
5095 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5096 vars_prune(&s->vars_reqres, s->sess, s);
5097}
5098
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005099
5100DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005101
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005102__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005103static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005104{
5105}
5106
5107
5108/*
5109 * Local variables:
5110 * c-indent-level: 8
5111 * c-basic-offset: 8
5112 * End:
5113 */