blob: 268796d2e88faa204b6b9cb762d8c9229a37f312 [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>
Christopher Faulete0768eb2018-10-03 16:38:02 +020017#include <common/uri_auth.h>
18
Christopher Faulet0f226952018-10-22 09:29:56 +020019#include <types/capture.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020020
21#include <proto/acl.h>
Christopher Faulet3e964192018-10-24 11:39:23 +020022#include <proto/action.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020023#include <proto/channel.h>
24#include <proto/checks.h>
25#include <proto/connection.h>
26#include <proto/filters.h>
Christopher Faulet0f226952018-10-22 09:29:56 +020027#include <proto/http_htx.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020028#include <proto/log.h>
Christopher Faulet3e964192018-10-24 11:39:23 +020029#include <proto/pattern.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 Fauletfc9cfe42019-07-16 14:54:53 +020055static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status);
56static 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 /*
323 * 2: check if the URI matches the monitor_uri.
324 * We have to do this for every request which gets in, because
325 * the monitor-uri is defined by the frontend.
326 */
327 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100328 isteqi(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200329 /*
330 * We have found the monitor URI
331 */
332 struct acl_cond *cond;
333
334 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100335 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200336
337 /* Check if we want to fail this monitor request or not */
338 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
339 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
340
341 ret = acl_pass(ret);
342 if (cond->pol == ACL_COND_UNLESS)
343 ret = !ret;
344
345 if (ret) {
346 /* we fail this request, let's return 503 service unavail */
347 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200348 if (!(s->flags & SF_ERR_MASK))
349 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
350 goto return_prx_cond;
351 }
352 }
353
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800354 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200355 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200356 if (!(s->flags & SF_ERR_MASK))
357 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
358 goto return_prx_cond;
359 }
360
361 /*
362 * 3: Maybe we have to copy the original REQURI for the logs ?
363 * Note: we cannot log anymore if the request has been
364 * classified as invalid.
365 */
366 if (unlikely(s->logs.logwait & LW_REQ)) {
367 /* we have a complete HTTP request that we must log */
368 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200369 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200370
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200371 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200372 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200373
374 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
375 s->do_log(s);
376 } else {
377 ha_alert("HTTP logging : out of memory.\n");
378 }
379 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200380
Christopher Faulete0768eb2018-10-03 16:38:02 +0200381 /* if the frontend has "option http-use-proxy-header", we'll check if
382 * we have what looks like a proxied connection instead of a connection,
383 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
384 * Note that this is *not* RFC-compliant, however browsers and proxies
385 * happen to do that despite being non-standard :-(
386 * We consider that a request not beginning with either '/' or '*' is
387 * a proxied connection, which covers both "scheme://location" and
388 * CONNECT ip:port.
389 */
390 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100391 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200392 txn->flags |= TX_USE_PX_CONN;
393
Christopher Faulete0768eb2018-10-03 16:38:02 +0200394 /* 5: we may need to capture headers */
395 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200396 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200397
Christopher Faulete0768eb2018-10-03 16:38:02 +0200398 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200399 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200400 req->analysers |= AN_REQ_HTTP_BODY;
401
402 /*
403 * RFC7234#4:
404 * A cache MUST write through requests with methods
405 * that are unsafe (Section 4.2.1 of [RFC7231]) to
406 * the origin server; i.e., a cache is not allowed
407 * to generate a reply to such a request before
408 * having forwarded the request and having received
409 * a corresponding response.
410 *
411 * RFC7231#4.2.1:
412 * Of the request methods defined by this
413 * specification, the GET, HEAD, OPTIONS, and TRACE
414 * methods are defined to be safe.
415 */
416 if (likely(txn->meth == HTTP_METH_GET ||
417 txn->meth == HTTP_METH_HEAD ||
418 txn->meth == HTTP_METH_OPTIONS ||
419 txn->meth == HTTP_METH_TRACE))
420 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
421
422 /* end of job, return OK */
423 req->analysers &= ~an_bit;
424 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200425
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100426 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200427 return 1;
428
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200429 return_int_err:
430 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200431 if (!(s->flags & SF_ERR_MASK))
432 s->flags |= SF_ERR_INTERNAL;
433 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
434 if (sess->listener->counters)
435 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
436 goto return_prx_cond;
437
Christopher Faulete0768eb2018-10-03 16:38:02 +0200438 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200439 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100440 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200441 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100442 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200443 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200444
445 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200446 http_reply_and_close(s, txn->status, http_error_message(s));
447
Christopher Faulete0768eb2018-10-03 16:38:02 +0200448 if (!(s->flags & SF_ERR_MASK))
449 s->flags |= SF_ERR_PRXCOND;
450 if (!(s->flags & SF_FINST_MASK))
451 s->flags |= SF_FINST_R;
452
453 req->analysers &= AN_REQ_FLT_END;
454 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100455 DBG_TRACE_DEVEL("leaving on error",
456 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200457 return 0;
458}
459
460
461/* This stream analyser runs all HTTP request processing which is common to
462 * frontends and backends, which means blocking ACLs, filters, connection-close,
463 * reqadd, stats and redirects. This is performed for the designated proxy.
464 * It returns 1 if the processing can continue on next analysers, or zero if it
465 * either needs more data or wants to immediately abort the request (eg: deny,
466 * error, ...).
467 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200468int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200469{
470 struct session *sess = s->sess;
471 struct http_txn *txn = s->txn;
472 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200473 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200474 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200475 enum rule_result verdict;
476 int deny_status = HTTP_ERR_403;
477 struct connection *conn = objt_conn(sess->origin);
478
479 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
480 /* we need more data */
481 goto return_prx_yield;
482 }
483
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100484 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200485
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100486 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200487
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200488 /* just in case we have some per-backend tracking. Only called the first
489 * execution of the analyser. */
490 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
491 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200492
493 /* evaluate http-request rules */
494 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200495 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200496
497 switch (verdict) {
498 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
499 goto return_prx_yield;
500
501 case HTTP_RULE_RES_CONT:
502 case HTTP_RULE_RES_STOP: /* nothing to do */
503 break;
504
505 case HTTP_RULE_RES_DENY: /* deny or tarpit */
506 if (txn->flags & TX_CLTARPIT)
507 goto tarpit;
508 goto deny;
509
510 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
511 goto return_prx_cond;
512
513 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
514 goto done;
515
516 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
517 goto return_bad_req;
518 }
519 }
520
521 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
522 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200523 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200524
Christopher Fauletff2759f2018-10-24 11:13:16 +0200525 ctx.blk = NULL;
526 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
527 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200528 goto return_bad_req;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200529 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200530 }
531
532 /* OK at this stage, we know that the request was accepted according to
533 * the http-request rules, we can check for the stats. Note that the
534 * URI is detected *before* the req* rules in order not to be affected
535 * by a possible reqrep, while they are processed *after* so that a
536 * reqdeny can still block them. This clearly needs to change in 1.6!
537 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200538 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200539 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100540 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200541 txn->status = 500;
542 s->logs.tv_request = now;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200543 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200544
545 if (!(s->flags & SF_ERR_MASK))
546 s->flags |= SF_ERR_RESOURCE;
547 goto return_prx_cond;
548 }
549
550 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200551 http_handle_stats(s, req);
552 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200553 /* not all actions implemented: deny, allow, auth */
554
555 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
556 goto deny;
557
558 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
559 goto return_prx_cond;
560 }
561
Christopher Faulet2571bc62019-03-01 11:44:26 +0100562 /* Proceed with the applets now. */
563 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200564 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100565 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200566
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200567 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100568 goto return_bad_req;
569
Christopher Faulete0768eb2018-10-03 16:38:02 +0200570 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
571 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
572 if (!(s->flags & SF_FINST_MASK))
573 s->flags |= SF_FINST_R;
574
575 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
576 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
577 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
578 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100579
580 req->flags |= CF_SEND_DONTWAIT;
581 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200582 goto done;
583 }
584
585 /* check whether we have some ACLs set to redirect this request */
586 list_for_each_entry(rule, &px->redirect_rules, list) {
587 if (rule->cond) {
588 int ret;
589
590 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
591 ret = acl_pass(ret);
592 if (rule->cond->pol == ACL_COND_UNLESS)
593 ret = !ret;
594 if (!ret)
595 continue;
596 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200597 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200598 goto return_bad_req;
599 goto done;
600 }
601
602 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
603 * If this happens, then the data will not come immediately, so we must
604 * send all what we have without waiting. Note that due to the small gain
605 * in waiting for the body of the request, it's easier to simply put the
606 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
607 * itself once used.
608 */
609 req->flags |= CF_SEND_DONTWAIT;
610
611 done: /* done with this analyser, continue with next ones that the calling
612 * points will have set, if any.
613 */
614 req->analyse_exp = TICK_ETERNITY;
615 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
616 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100617 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200618 return 1;
619
620 tarpit:
621 /* Allow cookie logging
622 */
623 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200624 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200625
626 /* When a connection is tarpitted, we use the tarpit timeout,
627 * which may be the same as the connect timeout if unspecified.
628 * If unset, then set it to zero because we really want it to
629 * eventually expire. We build the tarpit as an analyser.
630 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100631 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200632
633 /* wipe the request out so that we can drop the connection early
634 * if the client closes first.
635 */
636 channel_dont_connect(req);
637
638 txn->status = http_err_codes[deny_status];
639
640 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
641 req->analysers |= AN_REQ_HTTP_TARPIT;
642 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
643 if (!req->analyse_exp)
644 req->analyse_exp = tick_add(now_ms, 0);
645 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100646 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200647 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100648 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200649 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100650 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200651 goto done_without_exp;
652
653 deny: /* this request was blocked (denied) */
654
655 /* Allow cookie logging
656 */
657 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200658 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200659
660 txn->flags |= TX_CLDENY;
661 txn->status = http_err_codes[deny_status];
662 s->logs.tv_request = now;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200663 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200664 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100665 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200666 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100667 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200668 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100669 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200670 goto return_prx_cond;
671
672 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200673 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200674 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200675
Olivier Houcharda798bf52019-03-08 18:52:00 +0100676 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200677 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100678 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200679
680 return_prx_cond:
681 if (!(s->flags & SF_ERR_MASK))
682 s->flags |= SF_ERR_PRXCOND;
683 if (!(s->flags & SF_FINST_MASK))
684 s->flags |= SF_FINST_R;
685
686 req->analysers &= AN_REQ_FLT_END;
687 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100688 DBG_TRACE_DEVEL("leaving on error",
689 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200690 return 0;
691
692 return_prx_yield:
693 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100694 DBG_TRACE_DEVEL("waiting for more data",
695 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200696 return 0;
697}
698
699/* This function performs all the processing enabled for the current request.
700 * It returns 1 if the processing can continue on next analysers, or zero if it
701 * needs more data, encounters an error, or wants to immediately abort the
702 * request. It relies on buffers flags, and updates s->req.analysers.
703 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200704int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200705{
706 struct session *sess = s->sess;
707 struct http_txn *txn = s->txn;
708 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200709 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200710 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
711
712 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
713 /* we need more data */
714 channel_dont_connect(req);
715 return 0;
716 }
717
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100718 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200719
720 /*
721 * Right now, we know that we have processed the entire headers
722 * and that unwanted requests have been filtered out. We can do
723 * whatever we want with the remaining request. Also, now we
724 * may have separate values for ->fe, ->be.
725 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100726 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200727
728 /*
729 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200730 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200731 */
732 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100733 struct htx_sl *sl;
734 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200735
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200736 if (!sockaddr_alloc(&s->target_addr)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200737 txn->status = 500;
738 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200739 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200740
741 if (!(s->flags & SF_ERR_MASK))
742 s->flags |= SF_ERR_RESOURCE;
743 if (!(s->flags & SF_FINST_MASK))
744 s->flags |= SF_FINST_R;
745
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100746 DBG_TRACE_DEVEL("leaving on error",
747 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200748 return 0;
749 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200750 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100751 uri = htx_sl_req_uri(sl);
752 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200753
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200754 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200755 goto return_bad_req;
756
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200757 s->target = &s->be->obj_type;
758 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
759
Christopher Faulete0768eb2018-10-03 16:38:02 +0200760 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200761 * uri.ptr and path.ptr (excluded). If it was not found, we need
762 * to replace from all the uri by a single "/".
763 *
764 * Instead of rewritting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100765 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200766 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200767 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100768 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200769 }
770
771 /*
772 * 7: Now we can work with the cookies.
773 * Note that doing so might move headers in the request, but
774 * the fields will stay coherent and the URI will not move.
775 * This should only be performed in the backend.
776 */
777 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200778 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200779
780 /* add unique-id if "header-unique-id" is specified */
781
782 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
783 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
784 goto return_bad_req;
785 s->unique_id[0] = '\0';
786 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
787 }
788
789 if (sess->fe->header_unique_id && s->unique_id) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200790 struct ist n = ist2(sess->fe->header_unique_id, strlen(sess->fe->header_unique_id));
791 struct ist v = ist2(s->unique_id, strlen(s->unique_id));
792
793 if (unlikely(!http_add_header(htx, n, v)))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200794 goto return_bad_req;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200795 }
796
797 /*
798 * 9: add X-Forwarded-For if either the frontend or the backend
799 * asks for it.
800 */
801 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200802 struct http_hdr_ctx ctx = { .blk = NULL };
803 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
804 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
805
Christopher Faulete0768eb2018-10-03 16:38:02 +0200806 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200807 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200808 /* The header is set to be added only if none is present
809 * and we found it, so don't do anything.
810 */
811 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200812 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200813 /* Add an X-Forwarded-For header unless the source IP is
814 * in the 'except' network range.
815 */
816 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200817 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200818 != sess->fe->except_net.s_addr) &&
819 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200820 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200821 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200822 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200823
824 /* Note: we rely on the backend to get the header name to be used for
825 * x-forwarded-for, because the header is really meant for the backends.
826 * However, if the backend did not specify any option, we have to rely
827 * on the frontend's header name.
828 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200829 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
830 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200831 goto return_bad_req;
832 }
833 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200834 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200835 /* FIXME: for the sake of completeness, we should also support
836 * 'except' here, although it is mostly useless in this case.
837 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200838 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200839
Christopher Faulete0768eb2018-10-03 16:38:02 +0200840 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200841 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200842 pn, sizeof(pn));
843
844 /* Note: we rely on the backend to get the header name to be used for
845 * x-forwarded-for, because the header is really meant for the backends.
846 * However, if the backend did not specify any option, we have to rely
847 * on the frontend's header name.
848 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200849 chunk_printf(&trash, "%s", pn);
850 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200851 goto return_bad_req;
852 }
853 }
854
855 /*
856 * 10: add X-Original-To if either the frontend or the backend
857 * asks for it.
858 */
859 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
860
861 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200862 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 +0200863 /* Add an X-Original-To header unless the destination IP is
864 * in the 'except' network range.
865 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200866 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200867 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200868 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200869 != sess->fe->except_to.s_addr) &&
870 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200871 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200872 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200873 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200874 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200875
876 /* Note: we rely on the backend to get the header name to be used for
877 * x-original-to, because the header is really meant for the backends.
878 * However, if the backend did not specify any option, we have to rely
879 * on the frontend's header name.
880 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200881 if (s->be->orgto_hdr_len)
882 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
883 else
884 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200885
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200886 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
887 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200888 goto return_bad_req;
889 }
890 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200891 }
892
Christopher Faulete0768eb2018-10-03 16:38:02 +0200893 /* If we have no server assigned yet and we're balancing on url_param
894 * with a POST request, we may be interested in checking the body for
895 * that parameter. This will be done in another analyser.
896 */
897 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100898 s->txn->meth == HTTP_METH_POST &&
899 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200900 channel_dont_connect(req);
901 req->analysers |= AN_REQ_HTTP_BODY;
902 }
903
904 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
905 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100906
Christopher Faulete0768eb2018-10-03 16:38:02 +0200907 /* We expect some data from the client. Unless we know for sure
908 * we already have a full request, we have to re-enable quick-ack
909 * in case we previously disabled it, otherwise we might cause
910 * the client to delay further data.
911 */
912 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200913 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100914 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200915
916 /*************************************************************
917 * OK, that's finished for the headers. We have done what we *
918 * could. Let's switch to the DATA state. *
919 ************************************************************/
920 req->analyse_exp = TICK_ETERNITY;
921 req->analysers &= ~an_bit;
922
923 s->logs.tv_request = now;
924 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100925 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200926 return 1;
927
928 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200929 txn->status = 400;
930 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200931 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200932
Olivier Houcharda798bf52019-03-08 18:52:00 +0100933 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200934 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100935 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200936
937 if (!(s->flags & SF_ERR_MASK))
938 s->flags |= SF_ERR_PRXCOND;
939 if (!(s->flags & SF_FINST_MASK))
940 s->flags |= SF_FINST_R;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100941 DBG_TRACE_DEVEL("leaving on error",
942 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200943 return 0;
944}
945
946/* This function is an analyser which processes the HTTP tarpit. It always
947 * returns zero, at the beginning because it prevents any other processing
948 * from occurring, and at the end because it terminates the request.
949 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200950int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200951{
952 struct http_txn *txn = s->txn;
953
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100954 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200955 /* This connection is being tarpitted. The CLIENT side has
956 * already set the connect expiration date to the right
957 * timeout. We just have to check that the client is still
958 * there and that the timeout has not expired.
959 */
960 channel_dont_connect(req);
961 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100962 !tick_is_expired(req->analyse_exp, now_ms)) {
963 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
964 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200965 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100966 }
967
Christopher Faulete0768eb2018-10-03 16:38:02 +0200968
969 /* We will set the queue timer to the time spent, just for
970 * logging purposes. We fake a 500 server error, so that the
971 * attacker will not suspect his connection has been tarpitted.
972 * It will not cause trouble to the logs because we can exclude
973 * the tarpitted connections by filtering on the 'PT' status flags.
974 */
975 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
976
977 if (!(req->flags & CF_READ_ERROR))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200978 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200979
980 req->analysers &= AN_REQ_FLT_END;
981 req->analyse_exp = TICK_ETERNITY;
982
983 if (!(s->flags & SF_ERR_MASK))
984 s->flags |= SF_ERR_PRXCOND;
985 if (!(s->flags & SF_FINST_MASK))
986 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100987
988 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200989 return 0;
990}
991
992/* This function is an analyser which waits for the HTTP request body. It waits
993 * for either the buffer to be full, or the full advertised contents to have
994 * reached the buffer. It must only be called after the standard HTTP request
995 * processing has occurred, because it expects the request to be parsed and will
996 * look for the Expect header. It may send a 100-Continue interim response. It
997 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
998 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
999 * needs to read more data, or 1 once it has completed its analysis.
1000 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001001int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001002{
1003 struct session *sess = s->sess;
1004 struct http_txn *txn = s->txn;
1005 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001006 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001007
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001008 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001009
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001010 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001011
Willy Tarreau4236f032019-03-05 10:43:32 +01001012 if (htx->flags & HTX_FL_PARSING_ERROR)
1013 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001014 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1015 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001016
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001017 if (msg->msg_state < HTTP_MSG_BODY)
1018 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001019
Christopher Faulete0768eb2018-10-03 16:38:02 +02001020 /* We have to parse the HTTP request body to find any required data.
1021 * "balance url_param check_post" should have been the only way to get
1022 * into this. We were brought here after HTTP header analysis, so all
1023 * related structures are ready.
1024 */
1025
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001026 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001027 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01001028 goto return_bad_req;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001029 }
1030
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001031 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001032
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001033 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1034 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001035 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001036 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001037 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001038 goto http_end;
1039
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001040 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001041 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1042 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001043
1044 if (!(s->flags & SF_ERR_MASK))
1045 s->flags |= SF_ERR_CLITO;
1046 if (!(s->flags & SF_FINST_MASK))
1047 s->flags |= SF_FINST_D;
1048 goto return_err_msg;
1049 }
1050
1051 /* we get here if we need to wait for more data */
1052 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1053 /* Not enough data. We'll re-use the http-request
1054 * timeout here. Ideally, we should set the timeout
1055 * relative to the accept() date. We just set the
1056 * request timeout once at the beginning of the
1057 * request.
1058 */
1059 channel_dont_connect(req);
1060 if (!tick_isset(req->analyse_exp))
1061 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001062 DBG_TRACE_DEVEL("waiting for more data",
1063 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001064 return 0;
1065 }
1066
1067 http_end:
1068 /* The situation will not evolve, so let's give up on the analysis. */
1069 s->logs.tv_request = now; /* update the request timer to reflect full request */
1070 req->analysers &= ~an_bit;
1071 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001072 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001073 return 1;
1074
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001075 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001076 txn->status = 500;
1077
1078 if (!(s->flags & SF_ERR_MASK))
1079 s->flags |= SF_ERR_INTERNAL;
1080 if (!(s->flags & SF_FINST_MASK))
1081 s->flags |= SF_FINST_R;
1082 goto return_err_msg;
1083
Christopher Faulete0768eb2018-10-03 16:38:02 +02001084 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001085 txn->status = 400;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001086
1087 if (!(s->flags & SF_ERR_MASK))
1088 s->flags |= SF_ERR_PRXCOND;
1089 if (!(s->flags & SF_FINST_MASK))
1090 s->flags |= SF_FINST_R;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001091 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001092
1093 return_err_msg:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001094 http_reply_and_close(s, txn->status, http_error_message(s));
1095
Christopher Faulete0768eb2018-10-03 16:38:02 +02001096 req->analysers &= AN_REQ_FLT_END;
Olivier Houcharda798bf52019-03-08 18:52:00 +01001097 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001098 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001099 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001100 DBG_TRACE_DEVEL("leaving on error",
1101 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001102 return 0;
1103}
1104
1105/* This function is an analyser which forwards request body (including chunk
1106 * sizes if any). It is called as soon as we must forward, even if we forward
1107 * zero byte. The only situation where it must not be called is when we're in
1108 * tunnel mode and we want to forward till the close. It's used both to forward
1109 * remaining data and to resync after end of body. It expects the msg_state to
1110 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1111 * read more data, or 1 once we can go on with next request or end the stream.
1112 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1113 * bytes of pending data + the headers if not already done.
1114 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001115int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001116{
1117 struct session *sess = s->sess;
1118 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001119 struct http_msg *msg = &txn->req;
1120 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001121 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001122 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001123
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001124 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001125
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001126 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001127
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001128 if (htx->flags & HTX_FL_PARSING_ERROR)
1129 goto return_bad_req;
1130 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1131 goto return_int_err;
1132
Christopher Faulete0768eb2018-10-03 16:38:02 +02001133 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1134 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1135 /* Output closed while we were sending data. We must abort and
1136 * wake the other side up.
1137 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001138
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001139 /* Don't abort yet if we had L7 retries activated and it
1140 * was a write error, we may recover.
1141 */
1142 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001143 (s->si[1].flags & SI_FL_L7_RETRY)) {
1144 DBG_TRACE_DEVEL("leaving on L7 retry",
1145 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001146 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001147 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001148 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001149 http_end_request(s);
1150 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001151 DBG_TRACE_DEVEL("leaving on error",
1152 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001153 return 1;
1154 }
1155
1156 /* Note that we don't have to send 100-continue back because we don't
1157 * need the data to complete our job, and it's up to the server to
1158 * decide whether to return 100, 417 or anything else in return of
1159 * an "Expect: 100-continue" header.
1160 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001161 if (msg->msg_state == HTTP_MSG_BODY)
1162 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001163
Christopher Faulete0768eb2018-10-03 16:38:02 +02001164 /* in most states, we should abort in case of early close */
1165 channel_auto_close(req);
1166
1167 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001168 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet145719a2019-11-15 11:29:40 +01001169 if (req->flags & CF_EOI) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001170 msg->msg_state = HTTP_MSG_DONE;
1171 req->to_forward = 0;
1172 goto done;
1173 }
1174 }
1175 else {
1176 /* We can't process the buffer's contents yet */
1177 req->flags |= CF_WAKE_WRITE;
1178 goto missing_data_or_waiting;
1179 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001180 }
1181
Christopher Faulet9768c262018-10-22 09:34:31 +02001182 if (msg->msg_state >= HTTP_MSG_DONE)
1183 goto done;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001184 /* Forward input data. We get it by removing all outgoing data not
1185 * forwarded yet from HTX data size. If there are some data filters, we
1186 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001187 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001188 if (HAS_REQ_DATA_FILTERS(s)) {
1189 ret = flt_http_payload(s, msg, htx->data);
1190 if (ret < 0)
1191 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001192 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001193 }
1194 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001195 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001196 if (msg->flags & HTTP_MSGF_XFER_LEN)
1197 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001198 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001199
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001200 if (txn->meth == HTTP_METH_CONNECT) {
1201 msg->msg_state = HTTP_MSG_TUNNEL;
1202 goto done;
1203 }
1204
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001205
Christopher Faulet9768c262018-10-22 09:34:31 +02001206 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001207 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1208 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001209 */
1210 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1211 goto missing_data_or_waiting;
1212
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001213 msg->msg_state = HTTP_MSG_ENDING;
1214 if (htx->data != co_data(req))
1215 goto missing_data_or_waiting;
Christopher Faulet9768c262018-10-22 09:34:31 +02001216 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletaed68d42019-03-28 18:12:46 +01001217 req->to_forward = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001218
1219 done:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001220 /* other states, DONE...TUNNEL */
1221 /* we don't want to forward closes on DONE except in tunnel mode. */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001222 if (!(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001223 channel_dont_close(req);
1224
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001225 if (HAS_REQ_DATA_FILTERS(s)) {
1226 ret = flt_http_end(s, msg);
1227 if (ret <= 0) {
1228 if (!ret)
1229 goto missing_data_or_waiting;
1230 goto return_bad_req;
1231 }
1232 }
1233
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001234 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001235 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001236 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001237 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1238 if (req->flags & CF_SHUTW) {
1239 /* request errors are most likely due to the
1240 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001241 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001242 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001243 goto return_bad_req;
1244 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001245 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001246 return 1;
1247 }
1248
1249 /* If "option abortonclose" is set on the backend, we want to monitor
1250 * the client's connection and forward any shutdown notification to the
1251 * server, which will decide whether to close or to go on processing the
1252 * request. We only do that in tunnel mode, and not in other modes since
1253 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001254 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001255 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001256 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001257 s->si[1].flags |= SI_FL_NOLINGER;
1258 channel_auto_close(req);
1259 }
1260 else if (s->txn->meth == HTTP_METH_POST) {
1261 /* POST requests may require to read extra CRLF sent by broken
1262 * browsers and which could cause an RST to be sent upon close
1263 * on some systems (eg: Linux). */
1264 channel_auto_read(req);
1265 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001266 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1267 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001268 return 0;
1269
1270 missing_data_or_waiting:
1271 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001272 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001273 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001274
1275 waiting:
1276 /* waiting for the last bits to leave the buffer */
1277 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001278 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001279
1280 /* When TE: chunked is used, we need to get there again to parse remaining
1281 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1282 * And when content-length is used, we never want to let the possible
1283 * shutdown be forwarded to the other side, as the state machine will
1284 * take care of it once the client responds. It's also important to
1285 * prevent TIME_WAITs from accumulating on the backend side, and for
1286 * HTTP/2 where the last frame comes with a shutdown.
1287 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001288 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001289 channel_dont_close(req);
1290
1291 /* We know that more data are expected, but we couldn't send more that
1292 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1293 * system knows it must not set a PUSH on this first part. Interactive
1294 * modes are already handled by the stream sock layer. We must not do
1295 * this in content-length mode because it could present the MSG_MORE
1296 * flag with the last block of forwarded data, which would cause an
1297 * additional delay to be observed by the receiver.
1298 */
1299 if (msg->flags & HTTP_MSGF_TE_CHNK)
1300 req->flags |= CF_EXPECT_MORE;
1301
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001302 DBG_TRACE_DEVEL("waiting for more data to forward",
1303 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001304 return 0;
1305
Christopher Faulet93e02d82019-03-08 14:18:50 +01001306 return_cli_abort:
1307 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1308 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
1309 if (objt_server(s->target))
1310 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
1311 if (!(s->flags & SF_ERR_MASK))
1312 s->flags |= SF_ERR_CLICL;
1313 status = 400;
1314 goto return_error;
1315
1316 return_srv_abort:
1317 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1318 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
1319 if (objt_server(s->target))
1320 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
1321 if (!(s->flags & SF_ERR_MASK))
1322 s->flags |= SF_ERR_SRVCL;
1323 status = 502;
1324 goto return_error;
1325
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001326 return_int_err:
1327 if (!(s->flags & SF_ERR_MASK))
1328 s->flags |= SF_ERR_INTERNAL;
1329 status = 500;
1330 goto return_error;
1331
Christopher Faulet93e02d82019-03-08 14:18:50 +01001332 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001333 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001334 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001335 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001336 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01001337 s->flags |= SF_ERR_CLICL;
1338 status = 400;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001339
Christopher Faulet93e02d82019-03-08 14:18:50 +01001340 return_error:
Christopher Faulet9768c262018-10-22 09:34:31 +02001341 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001342 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001343 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001344 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001345 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001346 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001347 }
1348 req->analysers &= AN_REQ_FLT_END;
1349 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001350 if (!(s->flags & SF_FINST_MASK))
1351 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001352 DBG_TRACE_DEVEL("leaving on error ",
1353 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001354 return 0;
1355}
1356
Olivier Houcharda254a372019-04-05 15:30:12 +02001357/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1358/* Returns 0 if we can attempt to retry, -1 otherwise */
1359static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1360{
1361 struct channel *req, *res;
1362 int co_data;
1363
1364 si->conn_retries--;
1365 if (si->conn_retries < 0)
1366 return -1;
1367
Willy Tarreau223995e2019-05-04 10:38:31 +02001368 if (objt_server(s->target))
1369 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1370 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1371
Olivier Houcharda254a372019-04-05 15:30:12 +02001372 req = &s->req;
1373 res = &s->res;
1374 /* Remove any write error from the request, and read error from the response */
1375 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1376 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1377 res->analysers = 0;
1378 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001379 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001380 si->exp = TICK_ETERNITY;
1381 res->rex = TICK_ETERNITY;
1382 res->to_forward = 0;
1383 res->analyse_exp = TICK_ETERNITY;
1384 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001385 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001386 si_release_endpoint(&s->si[1]);
1387 b_free(&req->buf);
1388 /* Swap the L7 buffer with the channel buffer */
1389 /* We know we stored the co_data as b_data, so get it there */
1390 co_data = b_data(&si->l7_buffer);
1391 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1392 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1393
1394 co_set_data(req, co_data);
1395 b_reset(&res->buf);
1396 co_set_data(res, 0);
1397 return 0;
1398}
1399
Christopher Faulete0768eb2018-10-03 16:38:02 +02001400/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1401 * processing can continue on next analysers, or zero if it either needs more
1402 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1403 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1404 * when it has nothing left to do, and may remove any analyser when it wants to
1405 * abort.
1406 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001407int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001408{
Christopher Faulet9768c262018-10-22 09:34:31 +02001409 /*
1410 * We will analyze a complete HTTP response to check the its syntax.
1411 *
1412 * Once the start line and all headers are received, we may perform a
1413 * capture of the error (if any), and we will set a few fields. We also
1414 * logging and finally headers capture.
1415 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001416 struct session *sess = s->sess;
1417 struct http_txn *txn = s->txn;
1418 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001419 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001420 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001421 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001422 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001423 int n;
1424
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001425 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001426
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001427 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001428
Willy Tarreau4236f032019-03-05 10:43:32 +01001429 /* Parsing errors are caught here */
1430 if (htx->flags & HTX_FL_PARSING_ERROR)
1431 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001432 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1433 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001434
Christopher Faulete0768eb2018-10-03 16:38:02 +02001435 /*
1436 * Now we quickly check if we have found a full valid response.
1437 * If not so, we check the FD and buffer states before leaving.
1438 * A full response is indicated by the fact that we have seen
1439 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1440 * responses are checked first.
1441 *
1442 * Depending on whether the client is still there or not, we
1443 * may send an error response back or not. Note that normally
1444 * we should only check for HTTP status there, and check I/O
1445 * errors somewhere else.
1446 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001447 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001448 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001449 /* 1: have we encountered a read error ? */
1450 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001451 struct connection *conn = NULL;
1452
Olivier Houchard865d8392019-05-03 22:46:27 +02001453 if (objt_cs(s->si[1].end))
1454 conn = objt_cs(s->si[1].end)->conn;
1455
1456 if (si_b->flags & SI_FL_L7_RETRY &&
1457 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001458 /* If we arrive here, then CF_READ_ERROR was
1459 * set by si_cs_recv() because we matched a
1460 * status, overwise it would have removed
1461 * the SI_FL_L7_RETRY flag, so it's ok not
1462 * to check s->be->retry_type.
1463 */
1464 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1465 return 0;
1466 }
1467
Olivier Houchard6db16992019-05-17 15:40:49 +02001468 if (txn->flags & TX_NOT_FIRST)
1469 goto abort_keep_alive;
1470
Olivier Houcharda798bf52019-03-08 18:52:00 +01001471 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001472 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001473 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001474 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001475 }
1476
Christopher Faulete0768eb2018-10-03 16:38:02 +02001477 rep->analysers &= AN_RES_FLT_END;
1478 txn->status = 502;
1479
1480 /* Check to see if the server refused the early data.
1481 * If so, just send a 425
1482 */
Olivier Houchard865d8392019-05-03 22:46:27 +02001483 if (conn->err_code == CO_ER_SSL_EARLY_FAILED) {
1484 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001485 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001486 do_l7_retry(s, si_b) == 0) {
1487 DBG_TRACE_DEVEL("leaving on L7 retry",
1488 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001489 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001490 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001491 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001492 }
1493
1494 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001495 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001496
1497 if (!(s->flags & SF_ERR_MASK))
1498 s->flags |= SF_ERR_SRVCL;
1499 if (!(s->flags & SF_FINST_MASK))
1500 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001501 DBG_TRACE_DEVEL("leaving on error",
1502 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001503 return 0;
1504 }
1505
Christopher Faulet9768c262018-10-22 09:34:31 +02001506 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001507 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001508 if ((si_b->flags & SI_FL_L7_RETRY) &&
1509 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001510 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1511 DBG_TRACE_DEVEL("leaving on L7 retry",
1512 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001513 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001514 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001515 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001516 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001517 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001518 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001519 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001520 }
1521
Christopher Faulete0768eb2018-10-03 16:38:02 +02001522 rep->analysers &= AN_RES_FLT_END;
1523 txn->status = 504;
1524 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001525 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001526
1527 if (!(s->flags & SF_ERR_MASK))
1528 s->flags |= SF_ERR_SRVTO;
1529 if (!(s->flags & SF_FINST_MASK))
1530 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001531 DBG_TRACE_DEVEL("leaving on error",
1532 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001533 return 0;
1534 }
1535
Christopher Faulet9768c262018-10-22 09:34:31 +02001536 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001537 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001538 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1539 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001540 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001541 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001542
1543 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001544 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001545 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001546
1547 if (!(s->flags & SF_ERR_MASK))
1548 s->flags |= SF_ERR_CLICL;
1549 if (!(s->flags & SF_FINST_MASK))
1550 s->flags |= SF_FINST_H;
1551
1552 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001553 DBG_TRACE_DEVEL("leaving on error",
1554 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001555 return 0;
1556 }
1557
Christopher Faulet9768c262018-10-22 09:34:31 +02001558 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001559 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001560 if ((si_b->flags & SI_FL_L7_RETRY) &&
1561 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001562 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1563 DBG_TRACE_DEVEL("leaving on L7 retry",
1564 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001565 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001566 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001567 }
1568
Olivier Houchard6db16992019-05-17 15:40:49 +02001569 if (txn->flags & TX_NOT_FIRST)
1570 goto abort_keep_alive;
1571
Olivier Houcharda798bf52019-03-08 18:52:00 +01001572 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001573 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001574 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001575 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001576 }
1577
Christopher Faulete0768eb2018-10-03 16:38:02 +02001578 rep->analysers &= AN_RES_FLT_END;
1579 txn->status = 502;
1580 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001581 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001582
1583 if (!(s->flags & SF_ERR_MASK))
1584 s->flags |= SF_ERR_SRVCL;
1585 if (!(s->flags & SF_FINST_MASK))
1586 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001587 DBG_TRACE_DEVEL("leaving on error",
1588 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001589 return 0;
1590 }
1591
Christopher Faulet9768c262018-10-22 09:34:31 +02001592 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001593 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001594 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001595 goto abort_keep_alive;
1596
Olivier Houcharda798bf52019-03-08 18:52:00 +01001597 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001598 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001599
1600 if (!(s->flags & SF_ERR_MASK))
1601 s->flags |= SF_ERR_CLICL;
1602 if (!(s->flags & SF_FINST_MASK))
1603 s->flags |= SF_FINST_H;
1604
1605 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001606 DBG_TRACE_DEVEL("leaving on error",
1607 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001608 return 0;
1609 }
1610
1611 channel_dont_close(rep);
1612 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001613 DBG_TRACE_DEVEL("waiting for more data",
1614 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001615 return 0;
1616 }
1617
1618 /* More interesting part now : we know that we have a complete
1619 * response which at least looks like HTTP. We have an indicator
1620 * of each header's length, so we can parse them quickly.
1621 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001622 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001623 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001624 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001625
Christopher Faulet9768c262018-10-22 09:34:31 +02001626 /* 0: we might have to print this header in debug mode */
1627 if (unlikely((global.mode & MODE_DEBUG) &&
1628 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1629 int32_t pos;
1630
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001631 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001632
Christopher Fauleta3f15502019-05-13 15:27:23 +02001633 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001634 struct htx_blk *blk = htx_get_blk(htx, pos);
1635 enum htx_blk_type type = htx_get_blk_type(blk);
1636
1637 if (type == HTX_BLK_EOH)
1638 break;
1639 if (type != HTX_BLK_HDR)
1640 continue;
1641
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001642 http_debug_hdr("srvhdr", s,
1643 htx_get_blk_name(htx, blk),
1644 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001645 }
1646 }
1647
Christopher Faulet03599112018-11-27 11:21:21 +01001648 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001649 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001650 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001651 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001652 if (sl->flags & HTX_SL_F_XFER_LEN) {
1653 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001654 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001655 if (sl->flags & HTX_SL_F_BODYLESS)
1656 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001657 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001658
1659 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001660 if (n < 1 || n > 5)
1661 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001662
Christopher Faulete0768eb2018-10-03 16:38:02 +02001663 /* when the client triggers a 4xx from the server, it's most often due
1664 * to a missing object or permission. These events should be tracked
1665 * because if they happen often, it may indicate a brute force or a
1666 * vulnerability scan.
1667 */
1668 if (n == 4)
1669 stream_inc_http_err_ctr(s);
1670
1671 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001672 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001673
Christopher Faulete0768eb2018-10-03 16:38:02 +02001674 /* Adjust server's health based on status code. Note: status codes 501
1675 * and 505 are triggered on demand by client request, so we must not
1676 * count them as server failures.
1677 */
1678 if (objt_server(s->target)) {
1679 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001680 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001681 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001682 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001683 }
1684
1685 /*
1686 * We may be facing a 100-continue response, or any other informational
1687 * 1xx response which is non-final, in which case this is not the right
1688 * response, and we're waiting for the next one. Let's allow this response
1689 * to go to the client and wait for the next one. There's an exception for
1690 * 101 which is used later in the code to switch protocols.
1691 */
1692 if (txn->status < 200 &&
1693 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001694 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001695 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001696 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001697 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001698 txn->status = 0;
1699 s->logs.t_data = -1; /* was not a response yet */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001700 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001701 }
1702
1703 /*
1704 * 2: check for cacheability.
1705 */
1706
1707 switch (txn->status) {
1708 case 200:
1709 case 203:
1710 case 204:
1711 case 206:
1712 case 300:
1713 case 301:
1714 case 404:
1715 case 405:
1716 case 410:
1717 case 414:
1718 case 501:
1719 break;
1720 default:
1721 /* RFC7231#6.1:
1722 * Responses with status codes that are defined as
1723 * cacheable by default (e.g., 200, 203, 204, 206,
1724 * 300, 301, 404, 405, 410, 414, and 501 in this
1725 * specification) can be reused by a cache with
1726 * heuristic expiration unless otherwise indicated
1727 * by the method definition or explicit cache
1728 * controls [RFC7234]; all other status codes are
1729 * not cacheable by default.
1730 */
1731 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1732 break;
1733 }
1734
1735 /*
1736 * 3: we may need to capture headers
1737 */
1738 s->logs.logwait &= ~LW_RESP;
1739 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001740 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001741
Christopher Faulet9768c262018-10-22 09:34:31 +02001742 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001743 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1744 txn->status == 101)) {
1745 /* Either we've established an explicit tunnel, or we're
1746 * switching the protocol. In both cases, we're very unlikely
1747 * to understand the next protocols. We have to switch to tunnel
1748 * mode, so that we transfer the request and responses then let
1749 * this protocol pass unmodified. When we later implement specific
1750 * parsers for such protocols, we'll want to check the Upgrade
1751 * header which contains information about that protocol for
1752 * responses with status 101 (eg: see RFC2817 about TLS).
1753 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001754 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001755 }
1756
Christopher Faulet61608322018-11-23 16:23:45 +01001757 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1758 * 407 (Proxy-Authenticate) responses and set the connection to private
1759 */
1760 srv_conn = cs_conn(objt_cs(s->si[1].end));
1761 if (srv_conn) {
1762 struct ist hdr;
1763 struct http_hdr_ctx ctx;
1764
1765 if (txn->status == 401)
1766 hdr = ist("WWW-Authenticate");
1767 else if (txn->status == 407)
1768 hdr = ist("Proxy-Authenticate");
1769 else
1770 goto end;
1771
1772 ctx.blk = NULL;
1773 while (http_find_header(htx, hdr, &ctx, 0)) {
1774 if ((ctx.value.len >= 9 && word_match(ctx.value.ptr, ctx.value.len, "Negotiate", 9)) ||
Olivier Houchard250031e2019-05-29 15:01:50 +02001775 (ctx.value.len >= 4 && word_match(ctx.value.ptr, ctx.value.len, "NTLM", 4))) {
1776 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet61608322018-11-23 16:23:45 +01001777 srv_conn->flags |= CO_FL_PRIVATE;
Olivier Houchard250031e2019-05-29 15:01:50 +02001778 }
Christopher Faulet61608322018-11-23 16:23:45 +01001779 }
1780 }
1781
1782 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001783 /* we want to have the response time before we start processing it */
1784 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1785
1786 /* end of job, return OK */
1787 rep->analysers &= ~an_bit;
1788 rep->analyse_exp = TICK_ETERNITY;
1789 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001790 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001791 return 1;
1792
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001793 return_int_err:
1794 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
1795 txn->status = 500;
1796 if (!(s->flags & SF_ERR_MASK))
1797 s->flags |= SF_ERR_INTERNAL;
1798 goto return_error;
1799
1800 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001801 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001802 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001803 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001804 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001805 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001806 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001807 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001808 do_l7_retry(s, si_b) == 0) {
1809 DBG_TRACE_DEVEL("leaving on L7 retry",
1810 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001811 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001812 }
Christopher Faulet47365272018-10-31 17:40:50 +01001813 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001814 /* fall through */
1815
1816 return_error:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001817 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001818
1819 if (!(s->flags & SF_ERR_MASK))
1820 s->flags |= SF_ERR_PRXCOND;
1821 if (!(s->flags & SF_FINST_MASK))
1822 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001823
1824 s->si[1].flags |= SI_FL_NOLINGER;
1825 rep->analysers &= AN_RES_FLT_END;
1826 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001827 DBG_TRACE_DEVEL("leaving on error",
1828 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001829 return 0;
1830
Christopher Faulete0768eb2018-10-03 16:38:02 +02001831 abort_keep_alive:
1832 /* A keep-alive request to the server failed on a network error.
1833 * The client is required to retry. We need to close without returning
1834 * any other information so that the client retries.
1835 */
1836 txn->status = 0;
1837 rep->analysers &= AN_RES_FLT_END;
1838 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001839 s->logs.logwait = 0;
1840 s->logs.level = 0;
1841 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001842 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001843 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1844 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001845 return 0;
1846}
1847
1848/* This function performs all the processing enabled for the current response.
1849 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1850 * and updates s->res.analysers. It might make sense to explode it into several
1851 * other functions. It works like process_request (see indications above).
1852 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001853int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001854{
1855 struct session *sess = s->sess;
1856 struct http_txn *txn = s->txn;
1857 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001858 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001859 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001860 enum rule_result ret = HTTP_RULE_RES_CONT;
1861
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001862 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1863 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001864
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001865 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001866
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001867 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001868
1869 /* The stats applet needs to adjust the Connection header but we don't
1870 * apply any filter there.
1871 */
1872 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1873 rep->analysers &= ~an_bit;
1874 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001875 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001876 }
1877
1878 /*
1879 * We will have to evaluate the filters.
1880 * As opposed to version 1.2, now they will be evaluated in the
1881 * filters order and not in the header order. This means that
1882 * each filter has to be validated among all headers.
1883 *
1884 * Filters are tried with ->be first, then with ->fe if it is
1885 * different from ->be.
1886 *
1887 * Maybe we are in resume condiion. In this case I choose the
1888 * "struct proxy" which contains the rule list matching the resume
1889 * pointer. If none of theses "struct proxy" match, I initialise
1890 * the process with the first one.
1891 *
1892 * In fact, I check only correspondance betwwen the current list
1893 * pointer and the ->fe rule list. If it doesn't match, I initialize
1894 * the loop with the ->be.
1895 */
1896 if (s->current_rule_list == &sess->fe->http_res_rules)
1897 cur_proxy = sess->fe;
1898 else
1899 cur_proxy = s->be;
1900 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001901 /* evaluate http-response rules */
1902 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001903 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001904
1905 if (ret == HTTP_RULE_RES_BADREQ)
1906 goto return_srv_prx_502;
1907
1908 if (ret == HTTP_RULE_RES_DONE) {
1909 rep->analysers &= ~an_bit;
1910 rep->analyse_exp = TICK_ETERNITY;
1911 return 1;
1912 }
1913 }
1914
1915 /* we need to be called again. */
1916 if (ret == HTTP_RULE_RES_YIELD) {
1917 channel_dont_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001918 DBG_TRACE_DEVEL("waiting for more data",
1919 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001920 return 0;
1921 }
1922
Christopher Faulete0768eb2018-10-03 16:38:02 +02001923 /* has the response been denied ? */
1924 if (txn->flags & TX_SVDENY) {
1925 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001926 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_secu, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001927
Olivier Houcharda798bf52019-03-08 18:52:00 +01001928 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
1929 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001930 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001931 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001932 goto return_srv_prx_502;
1933 }
1934
Christopher Faulete0768eb2018-10-03 16:38:02 +02001935 /* check whether we're already working on the frontend */
1936 if (cur_proxy == sess->fe)
1937 break;
1938 cur_proxy = sess->fe;
1939 }
1940
1941 /* After this point, this anayzer can't return yield, so we can
1942 * remove the bit corresponding to this analyzer from the list.
1943 *
1944 * Note that the intermediate returns and goto found previously
1945 * reset the analyzers.
1946 */
1947 rep->analysers &= ~an_bit;
1948 rep->analyse_exp = TICK_ETERNITY;
1949
1950 /* OK that's all we can do for 1xx responses */
1951 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001952 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001953
1954 /*
1955 * Now check for a server cookie.
1956 */
1957 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001958 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001959
1960 /*
1961 * Check for cache-control or pragma headers if required.
1962 */
1963 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001964 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001965
1966 /*
1967 * Add server cookie in the response if needed
1968 */
1969 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1970 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1971 (!(s->flags & SF_DIRECT) ||
1972 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1973 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1974 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1975 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1976 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1977 !(s->flags & SF_IGNORE_PRST)) {
1978 /* the server is known, it's not the one the client requested, or the
1979 * cookie's last seen date needs to be refreshed. We have to
1980 * insert a set-cookie here, except if we want to insert only on POST
1981 * requests and this one isn't. Note that servers which don't have cookies
1982 * (eg: some backup servers) will return a full cookie removal request.
1983 */
1984 if (!objt_server(s->target)->cookie) {
1985 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001986 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001987 s->be->cookie_name);
1988 }
1989 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001990 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001991
1992 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1993 /* emit last_date, which is mandatory */
1994 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1995 s30tob64((date.tv_sec+3) >> 2,
1996 trash.area + trash.data);
1997 trash.data += 5;
1998
1999 if (s->be->cookie_maxlife) {
2000 /* emit first_date, which is either the original one or
2001 * the current date.
2002 */
2003 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2004 s30tob64(txn->cookie_first_date ?
2005 txn->cookie_first_date >> 2 :
2006 (date.tv_sec+3) >> 2,
2007 trash.area + trash.data);
2008 trash.data += 5;
2009 }
2010 }
2011 chunk_appendf(&trash, "; path=/");
2012 }
2013
2014 if (s->be->cookie_domain)
2015 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2016
2017 if (s->be->ck_opts & PR_CK_HTTPONLY)
2018 chunk_appendf(&trash, "; HttpOnly");
2019
2020 if (s->be->ck_opts & PR_CK_SECURE)
2021 chunk_appendf(&trash, "; Secure");
2022
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002023 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002024 goto return_bad_resp;
2025
2026 txn->flags &= ~TX_SCK_MASK;
2027 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2028 /* the server did not change, only the date was updated */
2029 txn->flags |= TX_SCK_UPDATED;
2030 else
2031 txn->flags |= TX_SCK_INSERTED;
2032
2033 /* Here, we will tell an eventual cache on the client side that we don't
2034 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2035 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2036 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2037 */
2038 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2039
2040 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2041
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002042 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002043 goto return_bad_resp;
2044 }
2045 }
2046
2047 /*
2048 * Check if result will be cacheable with a cookie.
2049 * We'll block the response if security checks have caught
2050 * nasty things such as a cacheable cookie.
2051 */
2052 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2053 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2054 (s->be->options & PR_O_CHK_CACHE)) {
2055 /* we're in presence of a cacheable response containing
2056 * a set-cookie header. We'll block it as requested by
2057 * the 'checkcache' option, and send an alert.
2058 */
2059 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01002060 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002061
Olivier Houcharda798bf52019-03-08 18:52:00 +01002062 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
2063 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002064 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002065 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002066
2067 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2068 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2069 send_log(s->be, LOG_ALERT,
2070 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2071 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2072 goto return_srv_prx_502;
2073 }
2074
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002075 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002076 /* Always enter in the body analyzer */
2077 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2078 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2079
2080 /* if the user wants to log as soon as possible, without counting
2081 * bytes from the server, then this is the right moment. We have
2082 * to temporarily assign bytes_out to log what we currently have.
2083 */
2084 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2085 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002086 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002087 s->do_log(s);
2088 s->logs.bytes_out = 0;
2089 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002090 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002091 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002092
2093 return_bad_resp:
2094 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01002095 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01002096 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002097 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01002098 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002099
2100 return_srv_prx_502:
2101 rep->analysers &= AN_RES_FLT_END;
2102 txn->status = 502;
2103 s->logs.t_data = -1; /* was not a valid response */
2104 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002105 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002106 if (!(s->flags & SF_ERR_MASK))
2107 s->flags |= SF_ERR_PRXCOND;
2108 if (!(s->flags & SF_FINST_MASK))
2109 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002110 DBG_TRACE_DEVEL("leaving on error",
2111 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002112 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002113}
2114
2115/* This function is an analyser which forwards response body (including chunk
2116 * sizes if any). It is called as soon as we must forward, even if we forward
2117 * zero byte. The only situation where it must not be called is when we're in
2118 * tunnel mode and we want to forward till the close. It's used both to forward
2119 * remaining data and to resync after end of body. It expects the msg_state to
2120 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2121 * read more data, or 1 once we can go on with next request or end the stream.
2122 *
2123 * It is capable of compressing response data both in content-length mode and
2124 * in chunked mode. The state machines follows different flows depending on
2125 * whether content-length and chunked modes are used, since there are no
2126 * trailers in content-length :
2127 *
2128 * chk-mode cl-mode
2129 * ,----- BODY -----.
2130 * / \
2131 * V size > 0 V chk-mode
2132 * .--> SIZE -------------> DATA -------------> CRLF
2133 * | | size == 0 | last byte |
2134 * | v final crlf v inspected |
2135 * | TRAILERS -----------> DONE |
2136 * | |
2137 * `----------------------------------------------'
2138 *
2139 * Compression only happens in the DATA state, and must be flushed in final
2140 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2141 * is performed at once on final states for all bytes parsed, or when leaving
2142 * on missing data.
2143 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002144int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002145{
2146 struct session *sess = s->sess;
2147 struct http_txn *txn = s->txn;
2148 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002149 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002150 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002151
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002152 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002153
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002154 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002155
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002156 if (htx->flags & HTX_FL_PARSING_ERROR)
2157 goto return_bad_res;
2158 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2159 goto return_int_err;
2160
Christopher Faulete0768eb2018-10-03 16:38:02 +02002161 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002162 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002163 /* Output closed while we were sending data. We must abort and
2164 * wake the other side up.
2165 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002166 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002167 http_end_response(s);
2168 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002169 DBG_TRACE_DEVEL("leaving on error",
2170 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002171 return 1;
2172 }
2173
Christopher Faulet9768c262018-10-22 09:34:31 +02002174 if (msg->msg_state == HTTP_MSG_BODY)
2175 msg->msg_state = HTTP_MSG_DATA;
2176
Christopher Faulete0768eb2018-10-03 16:38:02 +02002177 /* in most states, we should abort in case of early close */
2178 channel_auto_close(res);
2179
Christopher Faulete0768eb2018-10-03 16:38:02 +02002180 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002181 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet145719a2019-11-15 11:29:40 +01002182 if (res->flags & CF_EOI) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002183 msg->msg_state = HTTP_MSG_DONE;
2184 res->to_forward = 0;
2185 goto done;
2186 }
2187 }
2188 else {
2189 /* We can't process the buffer's contents yet */
2190 res->flags |= CF_WAKE_WRITE;
2191 goto missing_data_or_waiting;
2192 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002193 }
2194
Christopher Faulet9768c262018-10-22 09:34:31 +02002195 if (msg->msg_state >= HTTP_MSG_DONE)
2196 goto done;
2197
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002198 /* Forward input data. We get it by removing all outgoing data not
2199 * forwarded yet from HTX data size. If there are some data filters, we
2200 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002201 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002202 if (HAS_RSP_DATA_FILTERS(s)) {
2203 ret = flt_http_payload(s, msg, htx->data);
2204 if (ret < 0)
2205 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002206 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002207 }
2208 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002209 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002210 if (msg->flags & HTTP_MSGF_XFER_LEN)
2211 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002212 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002213
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002214 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2215 (!(msg->flags & HTTP_MSGF_XFER_LEN) && (res->flags & CF_SHUTR || !HAS_RSP_DATA_FILTERS(s)))) {
2216 msg->msg_state = HTTP_MSG_TUNNEL;
2217 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002218 }
2219
Christopher Faulet9768c262018-10-22 09:34:31 +02002220 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002221 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2222 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002223 */
2224 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2225 goto missing_data_or_waiting;
2226
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002227 msg->msg_state = HTTP_MSG_ENDING;
2228 if (htx->data != co_data(res))
2229 goto missing_data_or_waiting;
Christopher Faulet9768c262018-10-22 09:34:31 +02002230 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletaed68d42019-03-28 18:12:46 +01002231 res->to_forward = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002232
2233 done:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002234 /* other states, DONE...TUNNEL */
Christopher Faulet9768c262018-10-22 09:34:31 +02002235 channel_dont_close(res);
2236
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002237 if (HAS_RSP_DATA_FILTERS(s)) {
2238 ret = flt_http_end(s, msg);
2239 if (ret <= 0) {
2240 if (!ret)
2241 goto missing_data_or_waiting;
2242 goto return_bad_res;
2243 }
2244 }
2245
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002246 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002247 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002248 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002249 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2250 if (res->flags & CF_SHUTW) {
2251 /* response errors are most likely due to the
2252 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002253 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002254 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002255 goto return_bad_res;
2256 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002257 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002258 return 1;
2259 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002260 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2261 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002262 return 0;
2263
2264 missing_data_or_waiting:
2265 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002266 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002267
2268 /* stop waiting for data if the input is closed before the end. If the
2269 * client side was already closed, it means that the client has aborted,
2270 * so we don't want to count this as a server abort. Otherwise it's a
2271 * server abort.
2272 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002273 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002274 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002275 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002276 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002277 if (htx_is_empty(htx))
2278 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002279 }
2280
Christopher Faulete0768eb2018-10-03 16:38:02 +02002281 /* When TE: chunked is used, we need to get there again to parse
2282 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002283 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2284 * are filters registered on the stream, we don't want to forward a
2285 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002286 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002287 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002288 channel_dont_close(res);
2289
2290 /* We know that more data are expected, but we couldn't send more that
2291 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2292 * system knows it must not set a PUSH on this first part. Interactive
2293 * modes are already handled by the stream sock layer. We must not do
2294 * this in content-length mode because it could present the MSG_MORE
2295 * flag with the last block of forwarded data, which would cause an
2296 * additional delay to be observed by the receiver.
2297 */
2298 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2299 res->flags |= CF_EXPECT_MORE;
2300
2301 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002302 DBG_TRACE_DEVEL("waiting for more data to forward",
2303 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002304 return 0;
2305
Christopher Faulet93e02d82019-03-08 14:18:50 +01002306 return_srv_abort:
2307 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2308 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002309 if (objt_server(s->target))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002310 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
2311 if (!(s->flags & SF_ERR_MASK))
2312 s->flags |= SF_ERR_SRVCL;
2313 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002314
Christopher Faulet93e02d82019-03-08 14:18:50 +01002315 return_cli_abort:
2316 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2317 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002318 if (objt_server(s->target))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002319 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
2320 if (!(s->flags & SF_ERR_MASK))
2321 s->flags |= SF_ERR_CLICL;
2322 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002323
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002324 return_int_err:
2325 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2326 if (!(s->flags & SF_ERR_MASK))
2327 s->flags |= SF_ERR_INTERNAL;
2328 goto return_error;
2329
Christopher Faulet93e02d82019-03-08 14:18:50 +01002330 return_bad_res:
2331 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2332 if (objt_server(s->target)) {
2333 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
2334 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2335 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002336 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002337 s->flags |= SF_ERR_SRVCL;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002338
Christopher Faulet93e02d82019-03-08 14:18:50 +01002339 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002340 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002341 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002342 res->analysers &= AN_RES_FLT_END;
2343 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 +02002344 if (!(s->flags & SF_FINST_MASK))
2345 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002346 DBG_TRACE_DEVEL("leaving on error",
2347 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002348 return 0;
2349}
2350
Christopher Fauletf2824e62018-10-01 12:12:37 +02002351/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002352 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002353 * as too large a request to build a valid response.
2354 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002355int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002356{
Christopher Faulet99daf282018-11-28 22:58:13 +01002357 struct channel *req = &s->req;
2358 struct channel *res = &s->res;
2359 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002360 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002361 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002362 struct ist status, reason, location;
2363 unsigned int flags;
2364 size_t data;
Christopher Faulet08e66462019-05-23 16:44:59 +02002365 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002366
2367 chunk = alloc_trash_chunk();
2368 if (!chunk)
Christopher Faulet99daf282018-11-28 22:58:13 +01002369 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002370
Christopher Faulet99daf282018-11-28 22:58:13 +01002371 /*
2372 * Create the location
2373 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002374 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002375 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002376 case REDIRECT_TYPE_SCHEME: {
2377 struct http_hdr_ctx ctx;
2378 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002379
Christopher Faulet99daf282018-11-28 22:58:13 +01002380 host = ist("");
2381 ctx.blk = NULL;
2382 if (http_find_header(htx, ist("Host"), &ctx, 0))
2383 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002384
Christopher Faulet297fbb42019-05-13 14:41:27 +02002385 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002386 path = http_get_path(htx_sl_req_uri(sl));
2387 /* build message using path */
2388 if (path.ptr) {
2389 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2390 int qs = 0;
2391 while (qs < path.len) {
2392 if (*(path.ptr + qs) == '?') {
2393 path.len = qs;
2394 break;
2395 }
2396 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002397 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002398 }
2399 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002400 else
2401 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002402
Christopher Faulet99daf282018-11-28 22:58:13 +01002403 if (rule->rdr_str) { /* this is an old "redirect" rule */
2404 /* add scheme */
2405 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2406 goto fail;
2407 }
2408 else {
2409 /* add scheme with executing log format */
2410 chunk->data += build_logline(s, chunk->area + chunk->data,
2411 chunk->size - chunk->data,
2412 &rule->rdr_fmt);
2413 }
2414 /* add "://" + host + path */
2415 if (!chunk_memcat(chunk, "://", 3) ||
2416 !chunk_memcat(chunk, host.ptr, host.len) ||
2417 !chunk_memcat(chunk, path.ptr, path.len))
2418 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002419
Christopher Faulet99daf282018-11-28 22:58:13 +01002420 /* append a slash at the end of the location if needed and missing */
2421 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2422 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2423 if (chunk->data + 1 >= chunk->size)
2424 goto fail;
2425 chunk->area[chunk->data++] = '/';
2426 }
2427 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002428 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002429
Christopher Faulet99daf282018-11-28 22:58:13 +01002430 case REDIRECT_TYPE_PREFIX: {
2431 struct ist path;
2432
Christopher Faulet297fbb42019-05-13 14:41:27 +02002433 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002434 path = http_get_path(htx_sl_req_uri(sl));
2435 /* build message using path */
2436 if (path.ptr) {
2437 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2438 int qs = 0;
2439 while (qs < path.len) {
2440 if (*(path.ptr + qs) == '?') {
2441 path.len = qs;
2442 break;
2443 }
2444 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002445 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002446 }
2447 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002448 else
2449 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002450
Christopher Faulet99daf282018-11-28 22:58:13 +01002451 if (rule->rdr_str) { /* this is an old "redirect" rule */
2452 /* add prefix. Note that if prefix == "/", we don't want to
2453 * add anything, otherwise it makes it hard for the user to
2454 * configure a self-redirection.
2455 */
2456 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2457 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2458 goto fail;
2459 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002460 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002461 else {
2462 /* add prefix with executing log format */
2463 chunk->data += build_logline(s, chunk->area + chunk->data,
2464 chunk->size - chunk->data,
2465 &rule->rdr_fmt);
2466 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002467
Christopher Faulet99daf282018-11-28 22:58:13 +01002468 /* add path */
2469 if (!chunk_memcat(chunk, path.ptr, path.len))
2470 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002471
Christopher Faulet99daf282018-11-28 22:58:13 +01002472 /* append a slash at the end of the location if needed and missing */
2473 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2474 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2475 if (chunk->data + 1 >= chunk->size)
2476 goto fail;
2477 chunk->area[chunk->data++] = '/';
2478 }
2479 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002480 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002481 case REDIRECT_TYPE_LOCATION:
2482 default:
2483 if (rule->rdr_str) { /* this is an old "redirect" rule */
2484 /* add location */
2485 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2486 goto fail;
2487 }
2488 else {
2489 /* add location with executing log format */
2490 chunk->data += build_logline(s, chunk->area + chunk->data,
2491 chunk->size - chunk->data,
2492 &rule->rdr_fmt);
2493 }
2494 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002495 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002496 location = ist2(chunk->area, chunk->data);
2497
2498 /*
2499 * Create the 30x response
2500 */
2501 switch (rule->code) {
2502 case 308:
2503 status = ist("308");
2504 reason = ist("Permanent Redirect");
2505 break;
2506 case 307:
2507 status = ist("307");
2508 reason = ist("Temporary Redirect");
2509 break;
2510 case 303:
2511 status = ist("303");
2512 reason = ist("See Other");
2513 break;
2514 case 301:
2515 status = ist("301");
2516 reason = ist("Moved Permanently");
2517 break;
2518 case 302:
2519 default:
2520 status = ist("302");
2521 reason = ist("Found");
2522 break;
2523 }
2524
Christopher Faulet08e66462019-05-23 16:44:59 +02002525 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2526 close = 1;
2527
Christopher Faulet99daf282018-11-28 22:58:13 +01002528 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002529 /* Trim any possible response */
2530 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002531 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2532 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2533 if (!sl)
2534 goto fail;
2535 sl->info.res.status = rule->code;
2536 s->txn->status = rule->code;
2537
Christopher Faulet08e66462019-05-23 16:44:59 +02002538 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2539 goto fail;
2540
2541 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002542 !htx_add_header(htx, ist("Location"), location))
2543 goto fail;
2544
2545 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2546 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2547 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002548 }
2549
2550 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002551 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2552 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002553 }
2554
Christopher Faulet99daf282018-11-28 22:58:13 +01002555 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2556 goto fail;
2557
Kevin Zhu96b36392020-01-07 09:42:55 +01002558 htx_to_buf(htx, &res->buf);
2559
Christopher Fauletf2824e62018-10-01 12:12:37 +02002560 /* let's log the request time */
2561 s->logs.tv_request = now;
2562
Christopher Faulet06511812019-10-16 09:38:27 +02002563 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet99daf282018-11-28 22:58:13 +01002564 data = htx->data - co_data(res);
Christopher Faulet99daf282018-11-28 22:58:13 +01002565 c_adv(res, data);
2566 res->total += data;
2567
2568 channel_auto_read(req);
2569 channel_abort(req);
2570 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002571 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet99daf282018-11-28 22:58:13 +01002572
2573 res->wex = tick_add_ifset(now_ms, res->wto);
2574 channel_auto_read(res);
2575 channel_auto_close(res);
2576 channel_shutr_now(res);
2577
2578 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002579
2580 if (!(s->flags & SF_ERR_MASK))
2581 s->flags |= SF_ERR_LOCAL;
2582 if (!(s->flags & SF_FINST_MASK))
2583 s->flags |= SF_FINST_R;
2584
Christopher Faulet99daf282018-11-28 22:58:13 +01002585 free_trash_chunk(chunk);
2586 return 1;
2587
2588 fail:
2589 /* If an error occurred, remove the incomplete HTTP response from the
2590 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002591 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002592 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002593 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002594}
2595
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002596int http_transform_header_str(struct stream* s, struct channel *chn, struct htx *htx,
2597 struct ist name, const char *str, struct my_regex *re, int action)
Christopher Faulet72333522018-10-24 11:25:02 +02002598{
2599 struct http_hdr_ctx ctx;
2600 struct buffer *output = get_trash_chunk();
2601
2602 /* find full header is action is ACT_HTTP_REPLACE_HDR */
2603 ctx.blk = NULL;
2604 while (http_find_header(htx, name, &ctx, (action == ACT_HTTP_REPLACE_HDR))) {
2605 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2606 continue;
2607
2608 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2609 if (output->data == -1)
2610 return -1;
2611 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2612 return -1;
2613 }
2614 return 0;
2615}
2616
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002617static int http_transform_header(struct stream* s, struct channel *chn, struct htx *htx,
2618 const struct ist name, struct list *fmt, struct my_regex *re, int action)
Christopher Faulet72333522018-10-24 11:25:02 +02002619{
2620 struct buffer *replace;
2621 int ret = -1;
2622
2623 replace = alloc_trash_chunk();
2624 if (!replace)
2625 goto leave;
2626
2627 replace->data = build_logline(s, replace->area, replace->size, fmt);
2628 if (replace->data >= replace->size - 1)
2629 goto leave;
2630
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002631 ret = http_transform_header_str(s, chn, htx, name, replace->area, re, action);
Christopher Faulet72333522018-10-24 11:25:02 +02002632
2633 leave:
2634 free_trash_chunk(replace);
2635 return ret;
2636}
2637
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002638
2639/* Terminate a 103-Erly-hints response and send it to the client. It returns 0
2640 * on success and -1 on error. The response channel is updated accordingly.
2641 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002642static int http_reply_103_early_hints(struct channel *res)
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002643{
2644 struct htx *htx = htx_from_buf(&res->buf);
2645 size_t data;
2646
Christopher Faulet1d5ec092019-06-26 14:23:54 +02002647 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002648 /* If an error occurred during an Early-hint rule,
2649 * remove the incomplete HTTP 103 response from the
2650 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002651 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002652 return -1;
2653 }
2654
2655 data = htx->data - co_data(res);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002656 c_adv(res, data);
2657 res->total += data;
2658 return 0;
2659}
2660
Christopher Faulet6eb92892018-11-15 16:39:29 +01002661/*
2662 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
2663 * built according to <fmt> log line format.
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002664 * If <early_hints> is 0, it is starts a new response by adding the start
2665 * line. If an error occurred -1 is returned. On success 0 is returned. The
2666 * channel is not updated here. It must be done calling the function
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002667 * http_reply_103_early_hints().
Christopher Faulet6eb92892018-11-15 16:39:29 +01002668 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002669static int http_add_early_hint_header(struct stream *s, int early_hints, const struct ist name, struct list *fmt)
Christopher Faulet6eb92892018-11-15 16:39:29 +01002670{
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002671 struct channel *res = &s->res;
2672 struct htx *htx = htx_from_buf(&res->buf);
2673 struct buffer *value = alloc_trash_chunk();
2674
Christopher Faulet6eb92892018-11-15 16:39:29 +01002675 if (!early_hints) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002676 struct htx_sl *sl;
2677 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
2678 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2679
2680 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
2681 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
2682 if (!sl)
Christopher Faulet6eb92892018-11-15 16:39:29 +01002683 goto fail;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002684 sl->info.res.status = 103;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002685 }
2686
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002687 value->data = build_logline(s, b_tail(value), b_room(value), fmt);
2688 if (!htx_add_header(htx, name, ist2(b_head(value), b_data(value))))
Christopher Faulet6eb92892018-11-15 16:39:29 +01002689 goto fail;
2690
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002691 free_trash_chunk(value);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002692 return 1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002693
2694 fail:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002695 /* If an error occurred during an Early-hint rule, remove the incomplete
2696 * HTTP 103 response from the buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002697 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002698 free_trash_chunk(value);
2699 return -1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002700}
2701
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002702/* This function executes one of the set-{method,path,query,uri} actions. It
2703 * takes the string from the variable 'replace' with length 'len', then modifies
2704 * the relevant part of the request line accordingly. Then it updates various
2705 * pointers to the next elements which were moved, and the total buffer length.
2706 * It finds the action to be performed in p[2], previously filled by function
2707 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2708 * error, though this can be revisited when this code is finally exploited.
2709 *
2710 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2711 * query string and 3 to replace uri.
2712 *
2713 * In query string case, the mark question '?' must be set at the start of the
2714 * string by the caller, event if the replacement query string is empty.
2715 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002716int http_req_replace_stline(int action, const char *replace, int len,
2717 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002718{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002719 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002720
2721 switch (action) {
2722 case 0: // method
2723 if (!http_replace_req_meth(htx, ist2(replace, len)))
2724 return -1;
2725 break;
2726
2727 case 1: // path
2728 if (!http_replace_req_path(htx, ist2(replace, len)))
2729 return -1;
2730 break;
2731
2732 case 2: // query
2733 if (!http_replace_req_query(htx, ist2(replace, len)))
2734 return -1;
2735 break;
2736
2737 case 3: // uri
2738 if (!http_replace_req_uri(htx, ist2(replace, len)))
2739 return -1;
2740 break;
2741
2742 default:
2743 return -1;
2744 }
2745 return 0;
2746}
2747
2748/* This function replace the HTTP status code and the associated message. The
2749 * variable <status> contains the new status code. This function never fails.
2750 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002751void http_res_set_status(unsigned int status, const char *reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002752{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002753 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002754 char *res;
2755
2756 chunk_reset(&trash);
2757 res = ultoa_o(status, trash.area, trash.size);
2758 trash.data = res - trash.area;
2759
2760 /* Do we have a custom reason format string? */
2761 if (reason == NULL)
2762 reason = http_get_reason(status);
2763
Christopher Faulet87a2c0d2018-12-13 21:58:18 +01002764 if (http_replace_res_status(htx, ist2(trash.area, trash.data)))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002765 http_replace_res_reason(htx, ist2(reason, strlen(reason)));
2766}
2767
Christopher Faulet3e964192018-10-24 11:39:23 +02002768/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2769 * transaction <txn>. Returns the verdict of the first rule that prevents
2770 * further processing of the request (auth, deny, ...), and defaults to
2771 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2772 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2773 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2774 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2775 * status.
2776 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002777static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
2778 struct stream *s, int *deny_status)
Christopher Faulet3e964192018-10-24 11:39:23 +02002779{
2780 struct session *sess = strm_sess(s);
2781 struct http_txn *txn = s->txn;
2782 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002783 struct act_rule *rule;
2784 struct http_hdr_ctx ctx;
2785 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002786 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2787 int act_flags = 0;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002788 int early_hints = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002789
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002790 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002791
2792 /* If "the current_rule_list" match the executed rule list, we are in
2793 * resume condition. If a resume is needed it is always in the action
2794 * and never in the ACL or converters. In this case, we initialise the
2795 * current rule, and go to the action execution point.
2796 */
2797 if (s->current_rule) {
2798 rule = s->current_rule;
2799 s->current_rule = NULL;
2800 if (s->current_rule_list == rules)
2801 goto resume_execution;
2802 }
2803 s->current_rule_list = rules;
2804
2805 list_for_each_entry(rule, rules, list) {
2806 /* check optional condition */
2807 if (rule->cond) {
2808 int ret;
2809
2810 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2811 ret = acl_pass(ret);
2812
2813 if (rule->cond->pol == ACL_COND_UNLESS)
2814 ret = !ret;
2815
2816 if (!ret) /* condition not matched */
2817 continue;
2818 }
2819
2820 act_flags |= ACT_FLAG_FIRST;
2821 resume_execution:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002822 if (early_hints && rule->action != ACT_HTTP_EARLY_HINT) {
2823 early_hints = 0;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002824 if (http_reply_103_early_hints(&s->res) == -1) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002825 rule_ret = HTTP_RULE_RES_BADREQ;
2826 goto end;
2827 }
2828 }
2829
Christopher Faulet3e964192018-10-24 11:39:23 +02002830 switch (rule->action) {
2831 case ACT_ACTION_ALLOW:
2832 rule_ret = HTTP_RULE_RES_STOP;
2833 goto end;
2834
2835 case ACT_ACTION_DENY:
2836 if (deny_status)
2837 *deny_status = rule->deny_status;
2838 rule_ret = HTTP_RULE_RES_DENY;
2839 goto end;
2840
2841 case ACT_HTTP_REQ_TARPIT:
2842 txn->flags |= TX_CLTARPIT;
2843 if (deny_status)
2844 *deny_status = rule->deny_status;
2845 rule_ret = HTTP_RULE_RES_DENY;
2846 goto end;
2847
2848 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002849 /* Auth might be performed on regular http-req rules as well as on stats */
2850 auth_realm = rule->arg.auth.realm;
2851 if (!auth_realm) {
2852 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2853 auth_realm = STATS_DEFAULT_REALM;
2854 else
2855 auth_realm = px->id;
2856 }
2857 /* send 401/407 depending on whether we use a proxy or not. We still
2858 * count one error, because normal browsing won't significantly
2859 * increase the counter but brute force attempts will.
2860 */
Christopher Faulet3e964192018-10-24 11:39:23 +02002861 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002862 if (http_reply_40x_unauthorized(s, auth_realm) == -1)
Christopher Faulet12c51e22018-11-28 15:59:42 +01002863 rule_ret = HTTP_RULE_RES_BADREQ;
2864 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02002865 goto end;
2866
2867 case ACT_HTTP_REDIR:
Christopher Faulet3e964192018-10-24 11:39:23 +02002868 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002869 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3e964192018-10-24 11:39:23 +02002870 rule_ret = HTTP_RULE_RES_BADREQ;
2871 goto end;
2872
2873 case ACT_HTTP_SET_NICE:
2874 s->task->nice = rule->arg.nice;
2875 break;
2876
2877 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01002878 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Christopher Faulet3e964192018-10-24 11:39:23 +02002879 break;
2880
2881 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01002882 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Christopher Faulet3e964192018-10-24 11:39:23 +02002883 break;
2884
2885 case ACT_HTTP_SET_LOGL:
2886 s->logs.level = rule->arg.loglevel;
2887 break;
2888
2889 case ACT_HTTP_REPLACE_HDR:
2890 case ACT_HTTP_REPLACE_VAL:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002891 if (http_transform_header(s, &s->req, htx,
2892 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
2893 &rule->arg.hdr_add.fmt,
2894 rule->arg.hdr_add.re, rule->action)) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002895 rule_ret = HTTP_RULE_RES_BADREQ;
2896 goto end;
2897 }
2898 break;
2899
2900 case ACT_HTTP_DEL_HDR:
2901 /* remove all occurrences of the header */
2902 ctx.blk = NULL;
2903 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
2904 http_remove_header(htx, &ctx);
2905 break;
2906
2907 case ACT_HTTP_SET_HDR:
2908 case ACT_HTTP_ADD_HDR: {
2909 /* The scope of the trash buffer must be limited to this function. The
2910 * build_logline() function can execute a lot of other function which
2911 * can use the trash buffer. So for limiting the scope of this global
2912 * buffer, we build first the header value using build_logline, and
2913 * after we store the header name.
2914 */
2915 struct buffer *replace;
2916 struct ist n, v;
2917
2918 replace = alloc_trash_chunk();
2919 if (!replace) {
2920 rule_ret = HTTP_RULE_RES_BADREQ;
2921 goto end;
2922 }
2923
2924 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
2925 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2926 v = ist2(replace->area, replace->data);
2927
2928 if (rule->action == ACT_HTTP_SET_HDR) {
2929 /* remove all occurrences of the header */
2930 ctx.blk = NULL;
2931 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
2932 http_remove_header(htx, &ctx);
2933 }
2934
2935 if (!http_add_header(htx, n, v)) {
2936 static unsigned char rate_limit = 0;
2937
2938 if ((rate_limit++ & 255) == 0) {
2939 send_log(px, LOG_WARNING, "Proxy %s failed to add or set the request header '%.*s' for request #%u. You might need to increase tune.maxrewrite.", px->id, (int)n.len, n.ptr, s->uniq_id);
2940 }
2941
Olivier Houcharda798bf52019-03-08 18:52:00 +01002942 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002943 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002944 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002945 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002946 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002947 }
2948 free_trash_chunk(replace);
2949 break;
2950 }
2951
2952 case ACT_HTTP_DEL_ACL:
2953 case ACT_HTTP_DEL_MAP: {
2954 struct pat_ref *ref;
2955 struct buffer *key;
2956
2957 /* collect reference */
2958 ref = pat_ref_lookup(rule->arg.map.ref);
2959 if (!ref)
2960 continue;
2961
2962 /* allocate key */
2963 key = alloc_trash_chunk();
2964 if (!key) {
2965 rule_ret = HTTP_RULE_RES_BADREQ;
2966 goto end;
2967 }
2968
2969 /* collect key */
2970 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2971 key->area[key->data] = '\0';
2972
2973 /* perform update */
2974 /* returned code: 1=ok, 0=ko */
2975 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
2976 pat_ref_delete(ref, key->area);
2977 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
2978
2979 free_trash_chunk(key);
2980 break;
2981 }
2982
2983 case ACT_HTTP_ADD_ACL: {
2984 struct pat_ref *ref;
2985 struct buffer *key;
2986
2987 /* collect reference */
2988 ref = pat_ref_lookup(rule->arg.map.ref);
2989 if (!ref)
2990 continue;
2991
2992 /* allocate key */
2993 key = alloc_trash_chunk();
2994 if (!key) {
2995 rule_ret = HTTP_RULE_RES_BADREQ;
2996 goto end;
2997 }
2998
2999 /* collect key */
3000 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3001 key->area[key->data] = '\0';
3002
3003 /* perform update */
3004 /* add entry only if it does not already exist */
3005 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3006 if (pat_ref_find_elt(ref, key->area) == NULL)
3007 pat_ref_add(ref, key->area, NULL, NULL);
3008 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3009
3010 free_trash_chunk(key);
3011 break;
3012 }
3013
3014 case ACT_HTTP_SET_MAP: {
3015 struct pat_ref *ref;
3016 struct buffer *key, *value;
3017
3018 /* collect reference */
3019 ref = pat_ref_lookup(rule->arg.map.ref);
3020 if (!ref)
3021 continue;
3022
3023 /* allocate key */
3024 key = alloc_trash_chunk();
3025 if (!key) {
3026 rule_ret = HTTP_RULE_RES_BADREQ;
3027 goto end;
3028 }
3029
3030 /* allocate value */
3031 value = alloc_trash_chunk();
3032 if (!value) {
3033 free_trash_chunk(key);
3034 rule_ret = HTTP_RULE_RES_BADREQ;
3035 goto end;
3036 }
3037
3038 /* collect key */
3039 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3040 key->area[key->data] = '\0';
3041
3042 /* collect value */
3043 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3044 value->area[value->data] = '\0';
3045
3046 /* perform update */
Christopher Faulete84289e2019-04-19 14:50:55 +02003047 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003048 if (pat_ref_find_elt(ref, key->area) != NULL)
3049 /* update entry if it exists */
3050 pat_ref_set(ref, key->area, value->area, NULL);
3051 else
3052 /* insert a new entry */
3053 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02003054 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003055 free_trash_chunk(key);
3056 free_trash_chunk(value);
3057 break;
3058 }
3059
3060 case ACT_HTTP_EARLY_HINT:
3061 if (!(txn->req.flags & HTTP_MSGF_VER_11))
3062 break;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003063 early_hints = http_add_early_hint_header(s, early_hints,
3064 ist2(rule->arg.early_hint.name, rule->arg.early_hint.name_len),
3065 &rule->arg.early_hint.fmt);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003066 if (early_hints == -1) {
3067 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02003068 goto end;
3069 }
3070 break;
3071
3072 case ACT_CUSTOM:
3073 if ((s->req.flags & CF_READ_ERROR) ||
3074 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Christopher Faulet3e964192018-10-24 11:39:23 +02003075 (px->options & PR_O_ABRT_CLOSE)))
3076 act_flags |= ACT_FLAG_FINAL;
3077
3078 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
3079 case ACT_RET_ERR:
3080 case ACT_RET_CONT:
3081 break;
3082 case ACT_RET_STOP:
Christopher Faulet8f1aa772019-07-04 11:27:15 +02003083 rule_ret = HTTP_RULE_RES_STOP;
3084 goto end;
3085 case ACT_RET_DONE:
Christopher Faulet3e964192018-10-24 11:39:23 +02003086 rule_ret = HTTP_RULE_RES_DONE;
3087 goto end;
3088 case ACT_RET_YIELD:
3089 s->current_rule = rule;
3090 rule_ret = HTTP_RULE_RES_YIELD;
3091 goto end;
3092 }
3093 break;
3094
3095 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3096 /* Note: only the first valid tracking parameter of each
3097 * applies.
3098 */
3099
3100 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3101 struct stktable *t;
3102 struct stksess *ts;
3103 struct stktable_key *key;
3104 void *ptr1, *ptr2;
3105
3106 t = rule->arg.trk_ctr.table.t;
3107 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL,
3108 rule->arg.trk_ctr.expr, NULL);
3109
3110 if (key && (ts = stktable_get_entry(t, key))) {
3111 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3112
3113 /* let's count a new HTTP request as it's the first time we do it */
3114 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3115 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3116 if (ptr1 || ptr2) {
3117 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3118
3119 if (ptr1)
3120 stktable_data_cast(ptr1, http_req_cnt)++;
3121
3122 if (ptr2)
3123 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
3124 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3125
3126 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3127
3128 /* If data was modified, we need to touch to re-schedule sync */
3129 stktable_touch_local(t, ts, 0);
3130 }
3131
3132 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3133 if (sess->fe != s->be)
3134 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3135 }
3136 }
3137 break;
3138
Joseph Herlantc42c0e92018-11-25 10:43:27 -08003139 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003140 default:
3141 break;
3142 }
3143 }
3144
3145 end:
3146 if (early_hints) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003147 if (http_reply_103_early_hints(&s->res) == -1)
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003148 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02003149 }
3150
3151 /* we reached the end of the rules, nothing to report */
3152 return rule_ret;
3153}
3154
3155/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3156 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3157 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3158 * is returned, the process can continue the evaluation of next rule list. If
3159 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3160 * is returned, it means the operation could not be processed and a server error
3161 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3162 * deny rule. If *YIELD is returned, the caller must call again the function
3163 * with the same context.
3164 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003165static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
3166 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02003167{
3168 struct session *sess = strm_sess(s);
3169 struct http_txn *txn = s->txn;
3170 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02003171 struct act_rule *rule;
3172 struct http_hdr_ctx ctx;
3173 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
3174 int act_flags = 0;
3175
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003176 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02003177
3178 /* If "the current_rule_list" match the executed rule list, we are in
3179 * resume condition. If a resume is needed it is always in the action
3180 * and never in the ACL or converters. In this case, we initialise the
3181 * current rule, and go to the action execution point.
3182 */
3183 if (s->current_rule) {
3184 rule = s->current_rule;
3185 s->current_rule = NULL;
3186 if (s->current_rule_list == rules)
3187 goto resume_execution;
3188 }
3189 s->current_rule_list = rules;
3190
3191 list_for_each_entry(rule, rules, list) {
3192 /* check optional condition */
3193 if (rule->cond) {
3194 int ret;
3195
3196 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3197 ret = acl_pass(ret);
3198
3199 if (rule->cond->pol == ACL_COND_UNLESS)
3200 ret = !ret;
3201
3202 if (!ret) /* condition not matched */
3203 continue;
3204 }
3205
3206 act_flags |= ACT_FLAG_FIRST;
3207resume_execution:
3208 switch (rule->action) {
3209 case ACT_ACTION_ALLOW:
3210 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3211 goto end;
3212
3213 case ACT_ACTION_DENY:
3214 txn->flags |= TX_SVDENY;
3215 rule_ret = HTTP_RULE_RES_STOP;
3216 goto end;
3217
3218 case ACT_HTTP_SET_NICE:
3219 s->task->nice = rule->arg.nice;
3220 break;
3221
3222 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003223 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Christopher Faulet3e964192018-10-24 11:39:23 +02003224 break;
3225
3226 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003227 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Christopher Faulet3e964192018-10-24 11:39:23 +02003228 break;
3229
3230 case ACT_HTTP_SET_LOGL:
3231 s->logs.level = rule->arg.loglevel;
3232 break;
3233
3234 case ACT_HTTP_REPLACE_HDR:
3235 case ACT_HTTP_REPLACE_VAL:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003236 if (http_transform_header(s, &s->res, htx,
3237 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
3238 &rule->arg.hdr_add.fmt,
3239 rule->arg.hdr_add.re, rule->action)) {
Christopher Faulet3e964192018-10-24 11:39:23 +02003240 rule_ret = HTTP_RULE_RES_BADREQ;
3241 goto end;
3242 }
3243 break;
3244
3245 case ACT_HTTP_DEL_HDR:
3246 /* remove all occurrences of the header */
3247 ctx.blk = NULL;
3248 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3249 http_remove_header(htx, &ctx);
3250 break;
3251
3252 case ACT_HTTP_SET_HDR:
3253 case ACT_HTTP_ADD_HDR: {
3254 struct buffer *replace;
3255 struct ist n, v;
3256
3257 replace = alloc_trash_chunk();
3258 if (!replace) {
3259 rule_ret = HTTP_RULE_RES_BADREQ;
3260 goto end;
3261 }
3262
3263 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
3264 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3265 v = ist2(replace->area, replace->data);
3266
3267 if (rule->action == ACT_HTTP_SET_HDR) {
3268 /* remove all occurrences of the header */
3269 ctx.blk = NULL;
3270 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3271 http_remove_header(htx, &ctx);
3272 }
3273
3274 if (!http_add_header(htx, n, v)) {
3275 static unsigned char rate_limit = 0;
3276
3277 if ((rate_limit++ & 255) == 0) {
3278 send_log(px, LOG_WARNING, "Proxy %s failed to add or set the response header '%.*s' for request #%u. You might need to increase tune.maxrewrite.", px->id, (int)n.len, n.ptr, s->uniq_id);
3279 }
3280
Olivier Houcharda798bf52019-03-08 18:52:00 +01003281 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003282 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003283 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003284 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003285 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003286 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01003287 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003288 }
3289 free_trash_chunk(replace);
3290 break;
3291 }
3292
3293 case ACT_HTTP_DEL_ACL:
3294 case ACT_HTTP_DEL_MAP: {
3295 struct pat_ref *ref;
3296 struct buffer *key;
3297
3298 /* collect reference */
3299 ref = pat_ref_lookup(rule->arg.map.ref);
3300 if (!ref)
3301 continue;
3302
3303 /* allocate key */
3304 key = alloc_trash_chunk();
3305 if (!key) {
3306 rule_ret = HTTP_RULE_RES_BADREQ;
3307 goto end;
3308 }
3309
3310 /* collect key */
3311 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3312 key->area[key->data] = '\0';
3313
3314 /* perform update */
3315 /* returned code: 1=ok, 0=ko */
3316 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3317 pat_ref_delete(ref, key->area);
3318 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3319
3320 free_trash_chunk(key);
3321 break;
3322 }
3323
3324 case ACT_HTTP_ADD_ACL: {
3325 struct pat_ref *ref;
3326 struct buffer *key;
3327
3328 /* collect reference */
3329 ref = pat_ref_lookup(rule->arg.map.ref);
3330 if (!ref)
3331 continue;
3332
3333 /* allocate key */
3334 key = alloc_trash_chunk();
3335 if (!key) {
3336 rule_ret = HTTP_RULE_RES_BADREQ;
3337 goto end;
3338 }
3339
3340 /* collect key */
3341 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3342 key->area[key->data] = '\0';
3343
3344 /* perform update */
3345 /* check if the entry already exists */
Christopher Faulete84289e2019-04-19 14:50:55 +02003346 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003347 if (pat_ref_find_elt(ref, key->area) == NULL)
3348 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02003349 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003350 free_trash_chunk(key);
3351 break;
3352 }
3353
3354 case ACT_HTTP_SET_MAP: {
3355 struct pat_ref *ref;
3356 struct buffer *key, *value;
3357
3358 /* collect reference */
3359 ref = pat_ref_lookup(rule->arg.map.ref);
3360 if (!ref)
3361 continue;
3362
3363 /* allocate key */
3364 key = alloc_trash_chunk();
3365 if (!key) {
3366 rule_ret = HTTP_RULE_RES_BADREQ;
3367 goto end;
3368 }
3369
3370 /* allocate value */
3371 value = alloc_trash_chunk();
3372 if (!value) {
3373 free_trash_chunk(key);
3374 rule_ret = HTTP_RULE_RES_BADREQ;
3375 goto end;
3376 }
3377
3378 /* collect key */
3379 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3380 key->area[key->data] = '\0';
3381
3382 /* collect value */
3383 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3384 value->area[value->data] = '\0';
3385
3386 /* perform update */
3387 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3388 if (pat_ref_find_elt(ref, key->area) != NULL)
3389 /* update entry if it exists */
3390 pat_ref_set(ref, key->area, value->area, NULL);
3391 else
3392 /* insert a new entry */
3393 pat_ref_add(ref, key->area, value->area, NULL);
3394 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3395 free_trash_chunk(key);
3396 free_trash_chunk(value);
3397 break;
3398 }
3399
3400 case ACT_HTTP_REDIR:
3401 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003402 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3e964192018-10-24 11:39:23 +02003403 rule_ret = HTTP_RULE_RES_BADREQ;
3404 goto end;
3405
3406 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3407 /* Note: only the first valid tracking parameter of each
3408 * applies.
3409 */
3410 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3411 struct stktable *t;
3412 struct stksess *ts;
3413 struct stktable_key *key;
3414 void *ptr;
3415
3416 t = rule->arg.trk_ctr.table.t;
3417 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL,
3418 rule->arg.trk_ctr.expr, NULL);
3419
3420 if (key && (ts = stktable_get_entry(t, key))) {
3421 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3422
3423 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3424
3425 /* let's count a new HTTP request as it's the first time we do it */
3426 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3427 if (ptr)
3428 stktable_data_cast(ptr, http_req_cnt)++;
3429
3430 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3431 if (ptr)
3432 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3433 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3434
3435 /* When the client triggers a 4xx from the server, it's most often due
3436 * to a missing object or permission. These events should be tracked
3437 * because if they happen often, it may indicate a brute force or a
3438 * vulnerability scan. Normally this is done when receiving the response
3439 * but here we're tracking after this ought to have been done so we have
3440 * to do it on purpose.
3441 */
3442 if ((unsigned)(txn->status - 400) < 100) {
3443 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3444 if (ptr)
3445 stktable_data_cast(ptr, http_err_cnt)++;
3446
3447 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3448 if (ptr)
3449 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3450 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3451 }
3452
3453 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3454
3455 /* If data was modified, we need to touch to re-schedule sync */
3456 stktable_touch_local(t, ts, 0);
3457
3458 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3459 if (sess->fe != s->be)
3460 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3461 }
3462 }
3463 break;
3464
3465 case ACT_CUSTOM:
3466 if ((s->req.flags & CF_READ_ERROR) ||
3467 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Christopher Faulet3e964192018-10-24 11:39:23 +02003468 (px->options & PR_O_ABRT_CLOSE)))
3469 act_flags |= ACT_FLAG_FINAL;
3470
3471 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
3472 case ACT_RET_ERR:
3473 case ACT_RET_CONT:
3474 break;
3475 case ACT_RET_STOP:
3476 rule_ret = HTTP_RULE_RES_STOP;
3477 goto end;
Christopher Faulet8f1aa772019-07-04 11:27:15 +02003478 case ACT_RET_DONE:
3479 rule_ret = HTTP_RULE_RES_DONE;
3480 goto end;
Christopher Faulet3e964192018-10-24 11:39:23 +02003481 case ACT_RET_YIELD:
3482 s->current_rule = rule;
3483 rule_ret = HTTP_RULE_RES_YIELD;
3484 goto end;
3485 }
3486 break;
3487
Joseph Herlantc42c0e92018-11-25 10:43:27 -08003488 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003489 default:
3490 break;
3491 }
3492 }
3493
3494 end:
3495 /* we reached the end of the rules, nothing to report */
3496 return rule_ret;
3497}
3498
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003499/*
3500 * Manage client-side cookie. It can impact performance by about 2% so it is
3501 * desirable to call it only when needed. This code is quite complex because
3502 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3503 * highly recommended not to touch this part without a good reason !
3504 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003505static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003506{
3507 struct session *sess = s->sess;
3508 struct http_txn *txn = s->txn;
3509 struct htx *htx;
3510 struct http_hdr_ctx ctx;
3511 char *hdr_beg, *hdr_end, *del_from;
3512 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3513 int preserve_hdr;
3514
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003515 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003516 ctx.blk = NULL;
3517 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003518 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003519 del_from = NULL; /* nothing to be deleted */
3520 preserve_hdr = 0; /* assume we may kill the whole header */
3521
3522 /* Now look for cookies. Conforming to RFC2109, we have to support
3523 * attributes whose name begin with a '$', and associate them with
3524 * the right cookie, if we want to delete this cookie.
3525 * So there are 3 cases for each cookie read :
3526 * 1) it's a special attribute, beginning with a '$' : ignore it.
3527 * 2) it's a server id cookie that we *MAY* want to delete : save
3528 * some pointers on it (last semi-colon, beginning of cookie...)
3529 * 3) it's an application cookie : we *MAY* have to delete a previous
3530 * "special" cookie.
3531 * At the end of loop, if a "special" cookie remains, we may have to
3532 * remove it. If no application cookie persists in the header, we
3533 * *MUST* delete it.
3534 *
3535 * Note: RFC2965 is unclear about the processing of spaces around
3536 * the equal sign in the ATTR=VALUE form. A careful inspection of
3537 * the RFC explicitly allows spaces before it, and not within the
3538 * tokens (attrs or values). An inspection of RFC2109 allows that
3539 * too but section 10.1.3 lets one think that spaces may be allowed
3540 * after the equal sign too, resulting in some (rare) buggy
3541 * implementations trying to do that. So let's do what servers do.
3542 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3543 * allowed quoted strings in values, with any possible character
3544 * after a backslash, including control chars and delimitors, which
3545 * causes parsing to become ambiguous. Browsers also allow spaces
3546 * within values even without quotes.
3547 *
3548 * We have to keep multiple pointers in order to support cookie
3549 * removal at the beginning, middle or end of header without
3550 * corrupting the header. All of these headers are valid :
3551 *
3552 * hdr_beg hdr_end
3553 * | |
3554 * v |
3555 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3556 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3557 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3558 * | | | | | | |
3559 * | | | | | | |
3560 * | | | | | | +--> next
3561 * | | | | | +----> val_end
3562 * | | | | +-----------> val_beg
3563 * | | | +--------------> equal
3564 * | | +----------------> att_end
3565 * | +---------------------> att_beg
3566 * +--------------------------> prev
3567 *
3568 */
3569 hdr_beg = ctx.value.ptr;
3570 hdr_end = hdr_beg + ctx.value.len;
3571 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3572 /* Iterate through all cookies on this line */
3573
3574 /* find att_beg */
3575 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003576 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003577 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003578 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003579
3580 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3581 att_beg++;
3582
3583 /* find att_end : this is the first character after the last non
3584 * space before the equal. It may be equal to hdr_end.
3585 */
3586 equal = att_end = att_beg;
3587 while (equal < hdr_end) {
3588 if (*equal == '=' || *equal == ',' || *equal == ';')
3589 break;
3590 if (HTTP_IS_SPHT(*equal++))
3591 continue;
3592 att_end = equal;
3593 }
3594
3595 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3596 * is between <att_beg> and <equal>, both may be identical.
3597 */
3598 /* look for end of cookie if there is an equal sign */
3599 if (equal < hdr_end && *equal == '=') {
3600 /* look for the beginning of the value */
3601 val_beg = equal + 1;
3602 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3603 val_beg++;
3604
3605 /* find the end of the value, respecting quotes */
3606 next = http_find_cookie_value_end(val_beg, hdr_end);
3607
3608 /* make val_end point to the first white space or delimitor after the value */
3609 val_end = next;
3610 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3611 val_end--;
3612 }
3613 else
3614 val_beg = val_end = next = equal;
3615
3616 /* We have nothing to do with attributes beginning with
3617 * '$'. However, they will automatically be removed if a
3618 * header before them is removed, since they're supposed
3619 * to be linked together.
3620 */
3621 if (*att_beg == '$')
3622 continue;
3623
3624 /* Ignore cookies with no equal sign */
3625 if (equal == next) {
3626 /* This is not our cookie, so we must preserve it. But if we already
3627 * scheduled another cookie for removal, we cannot remove the
3628 * complete header, but we can remove the previous block itself.
3629 */
3630 preserve_hdr = 1;
3631 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003632 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003633 val_end += delta;
3634 next += delta;
3635 hdr_end += delta;
3636 prev = del_from;
3637 del_from = NULL;
3638 }
3639 continue;
3640 }
3641
3642 /* if there are spaces around the equal sign, we need to
3643 * strip them otherwise we'll get trouble for cookie captures,
3644 * or even for rewrites. Since this happens extremely rarely,
3645 * it does not hurt performance.
3646 */
3647 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3648 int stripped_before = 0;
3649 int stripped_after = 0;
3650
3651 if (att_end != equal) {
3652 memmove(att_end, equal, hdr_end - equal);
3653 stripped_before = (att_end - equal);
3654 equal += stripped_before;
3655 val_beg += stripped_before;
3656 }
3657
3658 if (val_beg > equal + 1) {
3659 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3660 stripped_after = (equal + 1) - val_beg;
3661 val_beg += stripped_after;
3662 stripped_before += stripped_after;
3663 }
3664
3665 val_end += stripped_before;
3666 next += stripped_before;
3667 hdr_end += stripped_before;
3668 }
3669 /* now everything is as on the diagram above */
3670
3671 /* First, let's see if we want to capture this cookie. We check
3672 * that we don't already have a client side cookie, because we
3673 * can only capture one. Also as an optimisation, we ignore
3674 * cookies shorter than the declared name.
3675 */
3676 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3677 (val_end - att_beg >= sess->fe->capture_namelen) &&
3678 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3679 int log_len = val_end - att_beg;
3680
3681 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3682 ha_alert("HTTP logging : out of memory.\n");
3683 } else {
3684 if (log_len > sess->fe->capture_len)
3685 log_len = sess->fe->capture_len;
3686 memcpy(txn->cli_cookie, att_beg, log_len);
3687 txn->cli_cookie[log_len] = 0;
3688 }
3689 }
3690
3691 /* Persistence cookies in passive, rewrite or insert mode have the
3692 * following form :
3693 *
3694 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3695 *
3696 * For cookies in prefix mode, the form is :
3697 *
3698 * Cookie: NAME=SRV~VALUE
3699 */
3700 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3701 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3702 struct server *srv = s->be->srv;
3703 char *delim;
3704
3705 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3706 * have the server ID between val_beg and delim, and the original cookie between
3707 * delim+1 and val_end. Otherwise, delim==val_end :
3708 *
3709 * hdr_beg
3710 * |
3711 * v
3712 * NAME=SRV; # in all but prefix modes
3713 * NAME=SRV~OPAQUE ; # in prefix mode
3714 * || || | |+-> next
3715 * || || | +--> val_end
3716 * || || +---------> delim
3717 * || |+------------> val_beg
3718 * || +-------------> att_end = equal
3719 * |+-----------------> att_beg
3720 * +------------------> prev
3721 *
3722 */
3723 if (s->be->ck_opts & PR_CK_PFX) {
3724 for (delim = val_beg; delim < val_end; delim++)
3725 if (*delim == COOKIE_DELIM)
3726 break;
3727 }
3728 else {
3729 char *vbar1;
3730 delim = val_end;
3731 /* Now check if the cookie contains a date field, which would
3732 * appear after a vertical bar ('|') just after the server name
3733 * and before the delimiter.
3734 */
3735 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3736 if (vbar1) {
3737 /* OK, so left of the bar is the server's cookie and
3738 * right is the last seen date. It is a base64 encoded
3739 * 30-bit value representing the UNIX date since the
3740 * epoch in 4-second quantities.
3741 */
3742 int val;
3743 delim = vbar1++;
3744 if (val_end - vbar1 >= 5) {
3745 val = b64tos30(vbar1);
3746 if (val > 0)
3747 txn->cookie_last_date = val << 2;
3748 }
3749 /* look for a second vertical bar */
3750 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3751 if (vbar1 && (val_end - vbar1 > 5)) {
3752 val = b64tos30(vbar1 + 1);
3753 if (val > 0)
3754 txn->cookie_first_date = val << 2;
3755 }
3756 }
3757 }
3758
3759 /* if the cookie has an expiration date and the proxy wants to check
3760 * it, then we do that now. We first check if the cookie is too old,
3761 * then only if it has expired. We detect strict overflow because the
3762 * time resolution here is not great (4 seconds). Cookies with dates
3763 * in the future are ignored if their offset is beyond one day. This
3764 * allows an admin to fix timezone issues without expiring everyone
3765 * and at the same time avoids keeping unwanted side effects for too
3766 * long.
3767 */
3768 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3769 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3770 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3771 txn->flags &= ~TX_CK_MASK;
3772 txn->flags |= TX_CK_OLD;
3773 delim = val_beg; // let's pretend we have not found the cookie
3774 txn->cookie_first_date = 0;
3775 txn->cookie_last_date = 0;
3776 }
3777 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3778 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3779 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3780 txn->flags &= ~TX_CK_MASK;
3781 txn->flags |= TX_CK_EXPIRED;
3782 delim = val_beg; // let's pretend we have not found the cookie
3783 txn->cookie_first_date = 0;
3784 txn->cookie_last_date = 0;
3785 }
3786
3787 /* Here, we'll look for the first running server which supports the cookie.
3788 * This allows to share a same cookie between several servers, for example
3789 * to dedicate backup servers to specific servers only.
3790 * However, to prevent clients from sticking to cookie-less backup server
3791 * when they have incidentely learned an empty cookie, we simply ignore
3792 * empty cookies and mark them as invalid.
3793 * The same behaviour is applied when persistence must be ignored.
3794 */
3795 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3796 srv = NULL;
3797
3798 while (srv) {
3799 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3800 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3801 if ((srv->cur_state != SRV_ST_STOPPED) ||
3802 (s->be->options & PR_O_PERSIST) ||
3803 (s->flags & SF_FORCE_PRST)) {
3804 /* we found the server and we can use it */
3805 txn->flags &= ~TX_CK_MASK;
3806 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3807 s->flags |= SF_DIRECT | SF_ASSIGNED;
3808 s->target = &srv->obj_type;
3809 break;
3810 } else {
3811 /* we found a server, but it's down,
3812 * mark it as such and go on in case
3813 * another one is available.
3814 */
3815 txn->flags &= ~TX_CK_MASK;
3816 txn->flags |= TX_CK_DOWN;
3817 }
3818 }
3819 srv = srv->next;
3820 }
3821
3822 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3823 /* no server matched this cookie or we deliberately skipped it */
3824 txn->flags &= ~TX_CK_MASK;
3825 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3826 txn->flags |= TX_CK_UNUSED;
3827 else
3828 txn->flags |= TX_CK_INVALID;
3829 }
3830
3831 /* depending on the cookie mode, we may have to either :
3832 * - delete the complete cookie if we're in insert+indirect mode, so that
3833 * the server never sees it ;
3834 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003835 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003836 * if we're in cookie prefix mode
3837 */
3838 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3839 int delta; /* negative */
3840
3841 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3842 delta = val_beg - (delim + 1);
3843 val_end += delta;
3844 next += delta;
3845 hdr_end += delta;
3846 del_from = NULL;
3847 preserve_hdr = 1; /* we want to keep this cookie */
3848 }
3849 else if (del_from == NULL &&
3850 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3851 del_from = prev;
3852 }
3853 }
3854 else {
3855 /* This is not our cookie, so we must preserve it. But if we already
3856 * scheduled another cookie for removal, we cannot remove the
3857 * complete header, but we can remove the previous block itself.
3858 */
3859 preserve_hdr = 1;
3860
3861 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003862 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003863 if (att_beg >= del_from)
3864 att_beg += delta;
3865 if (att_end >= del_from)
3866 att_end += delta;
3867 val_beg += delta;
3868 val_end += delta;
3869 next += delta;
3870 hdr_end += delta;
3871 prev = del_from;
3872 del_from = NULL;
3873 }
3874 }
3875
3876 /* continue with next cookie on this header line */
3877 att_beg = next;
3878 } /* for each cookie */
3879
3880
3881 /* There are no more cookies on this line.
3882 * We may still have one (or several) marked for deletion at the
3883 * end of the line. We must do this now in two ways :
3884 * - if some cookies must be preserved, we only delete from the
3885 * mark to the end of line ;
3886 * - if nothing needs to be preserved, simply delete the whole header
3887 */
3888 if (del_from) {
3889 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3890 }
3891 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003892 if (hdr_beg != hdr_end)
3893 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003894 else
3895 http_remove_header(htx, &ctx);
3896 }
3897 } /* for each "Cookie header */
3898}
3899
3900/*
3901 * Manage server-side cookies. It can impact performance by about 2% so it is
3902 * desirable to call it only when needed. This function is also used when we
3903 * just need to know if there is a cookie (eg: for check-cache).
3904 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003905static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003906{
3907 struct session *sess = s->sess;
3908 struct http_txn *txn = s->txn;
3909 struct htx *htx;
3910 struct http_hdr_ctx ctx;
3911 struct server *srv;
3912 char *hdr_beg, *hdr_end;
3913 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003914 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003915
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003916 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003917
3918 ctx.blk = NULL;
3919 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003920 int is_first = 1;
3921
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003922 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3923 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3924 break;
3925 is_cookie2 = 1;
3926 }
3927
3928 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3929 * <prev> points to the colon.
3930 */
3931 txn->flags |= TX_SCK_PRESENT;
3932
3933 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3934 * check-cache is enabled) and we are not interested in checking
3935 * them. Warning, the cookie capture is declared in the frontend.
3936 */
3937 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3938 break;
3939
3940 /* OK so now we know we have to process this response cookie.
3941 * The format of the Set-Cookie header is slightly different
3942 * from the format of the Cookie header in that it does not
3943 * support the comma as a cookie delimiter (thus the header
3944 * cannot be folded) because the Expires attribute described in
3945 * the original Netscape's spec may contain an unquoted date
3946 * with a comma inside. We have to live with this because
3947 * many browsers don't support Max-Age and some browsers don't
3948 * support quoted strings. However the Set-Cookie2 header is
3949 * clean.
3950 *
3951 * We have to keep multiple pointers in order to support cookie
3952 * removal at the beginning, middle or end of header without
3953 * corrupting the header (in case of set-cookie2). A special
3954 * pointer, <scav> points to the beginning of the set-cookie-av
3955 * fields after the first semi-colon. The <next> pointer points
3956 * either to the end of line (set-cookie) or next unquoted comma
3957 * (set-cookie2). All of these headers are valid :
3958 *
3959 * hdr_beg hdr_end
3960 * | |
3961 * v |
3962 * NAME1 = VALUE 1 ; Secure; Path="/" |
3963 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3964 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3965 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3966 * | | | | | | | |
3967 * | | | | | | | +-> next
3968 * | | | | | | +------------> scav
3969 * | | | | | +--------------> val_end
3970 * | | | | +--------------------> val_beg
3971 * | | | +----------------------> equal
3972 * | | +------------------------> att_end
3973 * | +----------------------------> att_beg
3974 * +------------------------------> prev
3975 * -------------------------------> hdr_beg
3976 */
3977 hdr_beg = ctx.value.ptr;
3978 hdr_end = hdr_beg + ctx.value.len;
3979 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3980
3981 /* Iterate through all cookies on this line */
3982
3983 /* find att_beg */
3984 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003985 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003986 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003987 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003988
3989 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3990 att_beg++;
3991
3992 /* find att_end : this is the first character after the last non
3993 * space before the equal. It may be equal to hdr_end.
3994 */
3995 equal = att_end = att_beg;
3996
3997 while (equal < hdr_end) {
3998 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3999 break;
4000 if (HTTP_IS_SPHT(*equal++))
4001 continue;
4002 att_end = equal;
4003 }
4004
4005 /* here, <equal> points to '=', a delimitor or the end. <att_end>
4006 * is between <att_beg> and <equal>, both may be identical.
4007 */
4008
4009 /* look for end of cookie if there is an equal sign */
4010 if (equal < hdr_end && *equal == '=') {
4011 /* look for the beginning of the value */
4012 val_beg = equal + 1;
4013 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
4014 val_beg++;
4015
4016 /* find the end of the value, respecting quotes */
4017 next = http_find_cookie_value_end(val_beg, hdr_end);
4018
4019 /* make val_end point to the first white space or delimitor after the value */
4020 val_end = next;
4021 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
4022 val_end--;
4023 }
4024 else {
4025 /* <equal> points to next comma, semi-colon or EOL */
4026 val_beg = val_end = next = equal;
4027 }
4028
4029 if (next < hdr_end) {
4030 /* Set-Cookie2 supports multiple cookies, and <next> points to
4031 * a colon or semi-colon before the end. So skip all attr-value
4032 * pairs and look for the next comma. For Set-Cookie, since
4033 * commas are permitted in values, skip to the end.
4034 */
4035 if (is_cookie2)
4036 next = http_find_hdr_value_end(next, hdr_end);
4037 else
4038 next = hdr_end;
4039 }
4040
4041 /* Now everything is as on the diagram above */
4042
4043 /* Ignore cookies with no equal sign */
4044 if (equal == val_end)
4045 continue;
4046
4047 /* If there are spaces around the equal sign, we need to
4048 * strip them otherwise we'll get trouble for cookie captures,
4049 * or even for rewrites. Since this happens extremely rarely,
4050 * it does not hurt performance.
4051 */
4052 if (unlikely(att_end != equal || val_beg > equal + 1)) {
4053 int stripped_before = 0;
4054 int stripped_after = 0;
4055
4056 if (att_end != equal) {
4057 memmove(att_end, equal, hdr_end - equal);
4058 stripped_before = (att_end - equal);
4059 equal += stripped_before;
4060 val_beg += stripped_before;
4061 }
4062
4063 if (val_beg > equal + 1) {
4064 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
4065 stripped_after = (equal + 1) - val_beg;
4066 val_beg += stripped_after;
4067 stripped_before += stripped_after;
4068 }
4069
4070 val_end += stripped_before;
4071 next += stripped_before;
4072 hdr_end += stripped_before;
4073
Christopher Faulet3e2638e2019-06-18 09:49:16 +02004074 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004075 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004076 }
4077
4078 /* First, let's see if we want to capture this cookie. We check
4079 * that we don't already have a server side cookie, because we
4080 * can only capture one. Also as an optimisation, we ignore
4081 * cookies shorter than the declared name.
4082 */
4083 if (sess->fe->capture_name != NULL &&
4084 txn->srv_cookie == NULL &&
4085 (val_end - att_beg >= sess->fe->capture_namelen) &&
4086 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
4087 int log_len = val_end - att_beg;
4088 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
4089 ha_alert("HTTP logging : out of memory.\n");
4090 }
4091 else {
4092 if (log_len > sess->fe->capture_len)
4093 log_len = sess->fe->capture_len;
4094 memcpy(txn->srv_cookie, att_beg, log_len);
4095 txn->srv_cookie[log_len] = 0;
4096 }
4097 }
4098
4099 srv = objt_server(s->target);
4100 /* now check if we need to process it for persistence */
4101 if (!(s->flags & SF_IGNORE_PRST) &&
4102 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
4103 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
4104 /* assume passive cookie by default */
4105 txn->flags &= ~TX_SCK_MASK;
4106 txn->flags |= TX_SCK_FOUND;
4107
4108 /* If the cookie is in insert mode on a known server, we'll delete
4109 * this occurrence because we'll insert another one later.
4110 * We'll delete it too if the "indirect" option is set and we're in
4111 * a direct access.
4112 */
4113 if (s->be->ck_opts & PR_CK_PSV) {
4114 /* The "preserve" flag was set, we don't want to touch the
4115 * server's cookie.
4116 */
4117 }
4118 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
4119 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
4120 /* this cookie must be deleted */
4121 if (prev == hdr_beg && next == hdr_end) {
4122 /* whole header */
4123 http_remove_header(htx, &ctx);
4124 /* note: while both invalid now, <next> and <hdr_end>
4125 * are still equal, so the for() will stop as expected.
4126 */
4127 } else {
4128 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004129 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004130 next = prev;
4131 hdr_end += delta;
4132 }
4133 txn->flags &= ~TX_SCK_MASK;
4134 txn->flags |= TX_SCK_DELETED;
4135 /* and go on with next cookie */
4136 }
4137 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
4138 /* replace bytes val_beg->val_end with the cookie name associated
4139 * with this server since we know it.
4140 */
4141 int sliding, delta;
4142
4143 ctx.value = ist2(val_beg, val_end - val_beg);
4144 ctx.lws_before = ctx.lws_after = 0;
4145 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
4146 delta = srv->cklen - (val_end - val_beg);
4147 sliding = (ctx.value.ptr - val_beg);
4148 hdr_beg += sliding;
4149 val_beg += sliding;
4150 next += sliding + delta;
4151 hdr_end += sliding + delta;
4152
4153 txn->flags &= ~TX_SCK_MASK;
4154 txn->flags |= TX_SCK_REPLACED;
4155 }
4156 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
4157 /* insert the cookie name associated with this server
4158 * before existing cookie, and insert a delimiter between them..
4159 */
4160 int sliding, delta;
4161 ctx.value = ist2(val_beg, 0);
4162 ctx.lws_before = ctx.lws_after = 0;
4163 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
4164 delta = srv->cklen + 1;
4165 sliding = (ctx.value.ptr - val_beg);
4166 hdr_beg += sliding;
4167 val_beg += sliding;
4168 next += sliding + delta;
4169 hdr_end += sliding + delta;
4170
4171 val_beg[srv->cklen] = COOKIE_DELIM;
4172 txn->flags &= ~TX_SCK_MASK;
4173 txn->flags |= TX_SCK_REPLACED;
4174 }
4175 }
4176 /* that's done for this cookie, check the next one on the same
4177 * line when next != hdr_end (only if is_cookie2).
4178 */
4179 }
4180 }
4181}
4182
Christopher Faulet25a02f62018-10-24 12:00:25 +02004183/*
4184 * Parses the Cache-Control and Pragma request header fields to determine if
4185 * the request may be served from the cache and/or if it is cacheable. Updates
4186 * s->txn->flags.
4187 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004188void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004189{
4190 struct http_txn *txn = s->txn;
4191 struct htx *htx;
4192 int32_t pos;
4193 int pragma_found, cc_found, i;
4194
4195 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
4196 return; /* nothing more to do here */
4197
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004198 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02004199 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02004200 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004201 struct htx_blk *blk = htx_get_blk(htx, pos);
4202 enum htx_blk_type type = htx_get_blk_type(blk);
4203 struct ist n, v;
4204
4205 if (type == HTX_BLK_EOH)
4206 break;
4207 if (type != HTX_BLK_HDR)
4208 continue;
4209
4210 n = htx_get_blk_name(htx, blk);
4211 v = htx_get_blk_value(htx, blk);
4212
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004213 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004214 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4215 pragma_found = 1;
4216 continue;
4217 }
4218 }
4219
4220 /* Don't use the cache and don't try to store if we found the
4221 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004222 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004223 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4224 txn->flags |= TX_CACHE_IGNORE;
4225 continue;
4226 }
4227
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004228 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004229 continue;
4230
4231 /* OK, right now we know we have a cache-control header */
4232 cc_found = 1;
4233 if (!v.len) /* no info */
4234 continue;
4235
4236 i = 0;
4237 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4238 !isspace((unsigned char)*(v.ptr+i)))
4239 i++;
4240
4241 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
4242 * values after max-age, max-stale nor min-fresh, we simply don't
4243 * use the cache when they're specified.
4244 */
4245 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
4246 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4247 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
4248 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
4249 txn->flags |= TX_CACHE_IGNORE;
4250 continue;
4251 }
4252
4253 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
4254 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4255 continue;
4256 }
4257 }
4258
4259 /* RFC7234#5.4:
4260 * When the Cache-Control header field is also present and
4261 * understood in a request, Pragma is ignored.
4262 * When the Cache-Control header field is not present in a
4263 * request, caches MUST consider the no-cache request
4264 * pragma-directive as having the same effect as if
4265 * "Cache-Control: no-cache" were present.
4266 */
4267 if (!cc_found && pragma_found)
4268 txn->flags |= TX_CACHE_IGNORE;
4269}
4270
4271/*
4272 * Check if response is cacheable or not. Updates s->txn->flags.
4273 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004274void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004275{
4276 struct http_txn *txn = s->txn;
4277 struct htx *htx;
4278 int32_t pos;
4279 int i;
4280
4281 if (txn->status < 200) {
4282 /* do not try to cache interim responses! */
4283 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4284 return;
4285 }
4286
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004287 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004288 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004289 struct htx_blk *blk = htx_get_blk(htx, pos);
4290 enum htx_blk_type type = htx_get_blk_type(blk);
4291 struct ist n, v;
4292
4293 if (type == HTX_BLK_EOH)
4294 break;
4295 if (type != HTX_BLK_HDR)
4296 continue;
4297
4298 n = htx_get_blk_name(htx, blk);
4299 v = htx_get_blk_value(htx, blk);
4300
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004301 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004302 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4303 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4304 return;
4305 }
4306 }
4307
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004308 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004309 continue;
4310
4311 /* OK, right now we know we have a cache-control header */
4312 if (!v.len) /* no info */
4313 continue;
4314
4315 i = 0;
4316 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4317 !isspace((unsigned char)*(v.ptr+i)))
4318 i++;
4319
4320 /* we have a complete value between v.ptr and (v.ptr+i) */
4321 if (i < v.len && *(v.ptr + i) == '=') {
4322 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
4323 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
4324 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4325 continue;
4326 }
4327
4328 /* we have something of the form no-cache="set-cookie" */
4329 if ((v.len >= 21) &&
4330 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
4331 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
4332 txn->flags &= ~TX_CACHE_COOK;
4333 continue;
4334 }
4335
4336 /* OK, so we know that either p2 points to the end of string or to a comma */
4337 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
4338 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4339 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
4340 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4341 return;
4342 }
4343
4344 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4345 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4346 continue;
4347 }
4348 }
4349}
4350
Christopher Faulet377c5a52018-10-24 21:21:30 +02004351/*
4352 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4353 * for the current backend.
4354 *
4355 * It is assumed that the request is either a HEAD, GET, or POST and that the
4356 * uri_auth field is valid.
4357 *
4358 * Returns 1 if stats should be provided, otherwise 0.
4359 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004360static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004361{
4362 struct uri_auth *uri_auth = backend->uri_auth;
4363 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004364 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004365 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004366
4367 if (!uri_auth)
4368 return 0;
4369
4370 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4371 return 0;
4372
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004373 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004374 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004375 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01004376 if (*uri_auth->uri_prefix == '/')
4377 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004378
4379 /* check URI size */
4380 if (uri_auth->uri_len > uri.len)
4381 return 0;
4382
4383 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4384 return 0;
4385
4386 return 1;
4387}
4388
4389/* This function prepares an applet to handle the stats. It can deal with the
4390 * "100-continue" expectation, check that admin rules are met for POST requests,
4391 * and program a response message if something was unexpected. It cannot fail
4392 * and always relies on the stats applet to complete the job. It does not touch
4393 * analysers nor counters, which are left to the caller. It does not touch
4394 * s->target which is supposed to already point to the stats applet. The caller
4395 * is expected to have already assigned an appctx to the stream.
4396 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004397static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004398{
4399 struct stats_admin_rule *stats_admin_rule;
4400 struct stream_interface *si = &s->si[1];
4401 struct session *sess = s->sess;
4402 struct http_txn *txn = s->txn;
4403 struct http_msg *msg = &txn->req;
4404 struct uri_auth *uri_auth = s->be->uri_auth;
4405 const char *h, *lookup, *end;
4406 struct appctx *appctx;
4407 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004408 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004409
4410 appctx = si_appctx(si);
4411 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4412 appctx->st1 = appctx->st2 = 0;
4413 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004414 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004415 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4416 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4417 appctx->ctx.stats.flags |= STAT_CHUNKED;
4418
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004419 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004420 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004421 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4422 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004423
4424 for (h = lookup; h <= end - 3; h++) {
4425 if (memcmp(h, ";up", 3) == 0) {
4426 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4427 break;
4428 }
4429 }
4430
4431 if (uri_auth->refresh) {
4432 for (h = lookup; h <= end - 10; h++) {
4433 if (memcmp(h, ";norefresh", 10) == 0) {
4434 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4435 break;
4436 }
4437 }
4438 }
4439
4440 for (h = lookup; h <= end - 4; h++) {
4441 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004442 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004443 break;
4444 }
4445 }
4446
4447 for (h = lookup; h <= end - 6; h++) {
4448 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004449 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004450 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4451 break;
4452 }
4453 }
4454
Christopher Faulet6338a082019-09-09 15:50:54 +02004455 for (h = lookup; h <= end - 5; h++) {
4456 if (memcmp(h, ";json", 5) == 0) {
4457 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4458 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4459 break;
4460 }
4461 }
4462
4463 for (h = lookup; h <= end - 12; h++) {
4464 if (memcmp(h, ";json-schema", 12) == 0) {
4465 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4466 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4467 break;
4468 }
4469 }
4470
Christopher Faulet377c5a52018-10-24 21:21:30 +02004471 for (h = lookup; h <= end - 8; h++) {
4472 if (memcmp(h, ";st=", 4) == 0) {
4473 int i;
4474 h += 4;
4475 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4476 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4477 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4478 appctx->ctx.stats.st_code = i;
4479 break;
4480 }
4481 }
4482 break;
4483 }
4484 }
4485
4486 appctx->ctx.stats.scope_str = 0;
4487 appctx->ctx.stats.scope_len = 0;
4488 for (h = lookup; h <= end - 8; h++) {
4489 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4490 int itx = 0;
4491 const char *h2;
4492 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4493 const char *err;
4494
4495 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4496 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004497 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4498 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004499 if (*h == ';' || *h == '&' || *h == ' ')
4500 break;
4501 itx++;
4502 h++;
4503 }
4504
4505 if (itx > STAT_SCOPE_TXT_MAXLEN)
4506 itx = STAT_SCOPE_TXT_MAXLEN;
4507 appctx->ctx.stats.scope_len = itx;
4508
4509 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4510 memcpy(scope_txt, h2, itx);
4511 scope_txt[itx] = '\0';
4512 err = invalid_char(scope_txt);
4513 if (err) {
4514 /* bad char in search text => clear scope */
4515 appctx->ctx.stats.scope_str = 0;
4516 appctx->ctx.stats.scope_len = 0;
4517 }
4518 break;
4519 }
4520 }
4521
4522 /* now check whether we have some admin rules for this request */
4523 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4524 int ret = 1;
4525
4526 if (stats_admin_rule->cond) {
4527 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4528 ret = acl_pass(ret);
4529 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4530 ret = !ret;
4531 }
4532
4533 if (ret) {
4534 /* no rule, or the rule matches */
4535 appctx->ctx.stats.flags |= STAT_ADMIN;
4536 break;
4537 }
4538 }
4539
Christopher Faulet5d45e382019-02-27 15:15:23 +01004540 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4541 appctx->st0 = STAT_HTTP_HEAD;
4542 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004543 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004544 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004545 if (msg->msg_state < HTTP_MSG_DATA)
4546 req->analysers |= AN_REQ_HTTP_BODY;
4547 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004548 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004549 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004550 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4551 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4552 appctx->st0 = STAT_HTTP_LAST;
4553 }
4554 }
4555 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004556 /* Unsupported method */
4557 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4558 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4559 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004560 }
4561
4562 s->task->nice = -32; /* small boost for HTTP statistics */
4563 return 1;
4564}
4565
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004566void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004567{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004568 struct channel *req = &s->req;
4569 struct channel *res = &s->res;
4570 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004571 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004572 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004573 struct ist path, location;
4574 unsigned int flags;
4575 size_t data;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004576
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004577 /*
4578 * Create the location
4579 */
4580 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004581
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004582 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004583 /* special prefix "/" means don't change URL */
4584 srv = __objt_server(s->target);
4585 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4586 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4587 return;
4588 }
4589
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004590 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004591 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004592 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004593 path = http_get_path(htx_sl_req_uri(sl));
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004594 if (!path.ptr)
4595 return;
4596
4597 if (!chunk_memcat(&trash, path.ptr, path.len))
4598 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004599 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004600
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004601 /*
4602 * Create the 302 respone
4603 */
4604 htx = htx_from_buf(&res->buf);
4605 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4606 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4607 ist("HTTP/1.1"), ist("302"), ist("Found"));
4608 if (!sl)
4609 goto fail;
4610 sl->info.res.status = 302;
4611 s->txn->status = 302;
4612
4613 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4614 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4615 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4616 !htx_add_header(htx, ist("Location"), location))
4617 goto fail;
4618
4619 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4620 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004621
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004622 /*
4623 * Send the message
4624 */
4625 data = htx->data - co_data(res);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004626 c_adv(res, data);
4627 res->total += data;
4628
4629 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004630 si_shutr(si);
4631 si_shutw(si);
4632 si->err_type = SI_ET_NONE;
4633 si->state = SI_ST_CLO;
4634
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004635 channel_auto_read(req);
4636 channel_abort(req);
4637 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004638 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004639 channel_auto_read(res);
4640 channel_auto_close(res);
4641
4642 if (!(s->flags & SF_ERR_MASK))
4643 s->flags |= SF_ERR_LOCAL;
4644 if (!(s->flags & SF_FINST_MASK))
4645 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004646
4647 /* FIXME: we should increase a counter of redirects per server and per backend. */
4648 srv_inc_sess_ctr(srv);
4649 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004650 return;
4651
4652 fail:
4653 /* If an error occurred, remove the incomplete HTTP response from the
4654 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004655 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004656}
4657
Christopher Fauletf2824e62018-10-01 12:12:37 +02004658/* This function terminates the request because it was completly analyzed or
4659 * because an error was triggered during the body forwarding.
4660 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004661static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004662{
4663 struct channel *chn = &s->req;
4664 struct http_txn *txn = s->txn;
4665
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004666 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004667
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004668 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4669 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004670 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004671 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004672 goto end;
4673 }
4674
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004675 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4676 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004677 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004678 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004679
4680 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004681 /* No need to read anymore, the request was completely parsed.
4682 * We can shut the read side unless we want to abort_on_close,
4683 * or we have a POST request. The issue with POST requests is
4684 * that some browsers still send a CRLF after the request, and
4685 * this CRLF must be read so that it does not remain in the kernel
4686 * buffers, otherwise a close could cause an RST on some systems
4687 * (eg: Linux).
4688 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004689 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004690 channel_dont_read(chn);
4691
4692 /* if the server closes the connection, we want to immediately react
4693 * and close the socket to save packets and syscalls.
4694 */
4695 s->si[1].flags |= SI_FL_NOHALF;
4696
4697 /* In any case we've finished parsing the request so we must
4698 * disable Nagle when sending data because 1) we're not going
4699 * to shut this side, and 2) the server is waiting for us to
4700 * send pending data.
4701 */
4702 chn->flags |= CF_NEVER_WAIT;
4703
Christopher Fauletd01ce402019-01-02 17:44:13 +01004704 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4705 /* The server has not finished to respond, so we
4706 * don't want to move in order not to upset it.
4707 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004708 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004709 return;
4710 }
4711
Christopher Fauletf2824e62018-10-01 12:12:37 +02004712 /* When we get here, it means that both the request and the
4713 * response have finished receiving. Depending on the connection
4714 * mode, we'll have to wait for the last bytes to leave in either
4715 * direction, and sometimes for a close to be effective.
4716 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004717 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004718 /* Tunnel mode will not have any analyser so it needs to
4719 * poll for reads.
4720 */
4721 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004722 if (b_data(&chn->buf)) {
4723 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004724 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004725 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004726 txn->req.msg_state = HTTP_MSG_TUNNEL;
4727 }
4728 else {
4729 /* we're not expecting any new data to come for this
4730 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004731 *
4732 * However, there is an exception if the response
4733 * length is undefined. In this case, we need to wait
4734 * the close from the server. The response will be
4735 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004736 */
4737 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4738 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004739 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004740
4741 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4742 channel_shutr_now(chn);
4743 channel_shutw_now(chn);
4744 }
4745 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004746 goto check_channel_flags;
4747 }
4748
4749 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4750 http_msg_closing:
4751 /* nothing else to forward, just waiting for the output buffer
4752 * to be empty and for the shutw_now to take effect.
4753 */
4754 if (channel_is_empty(chn)) {
4755 txn->req.msg_state = HTTP_MSG_CLOSED;
4756 goto http_msg_closed;
4757 }
4758 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004759 txn->req.msg_state = HTTP_MSG_ERROR;
4760 goto end;
4761 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004762 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004763 return;
4764 }
4765
4766 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4767 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004768 /* if we don't know whether the server will close, we need to hard close */
4769 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4770 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004771 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004772 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004773 channel_dont_read(chn);
4774 goto end;
4775 }
4776
4777 check_channel_flags:
4778 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4779 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4780 /* if we've just closed an output, let's switch */
4781 txn->req.msg_state = HTTP_MSG_CLOSING;
4782 goto http_msg_closing;
4783 }
4784
4785 end:
4786 chn->analysers &= AN_REQ_FLT_END;
4787 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4788 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4789 channel_auto_close(chn);
4790 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004791 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004792}
4793
4794
4795/* This function terminates the response because it was completly analyzed or
4796 * because an error was triggered during the body forwarding.
4797 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004798static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004799{
4800 struct channel *chn = &s->res;
4801 struct http_txn *txn = s->txn;
4802
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004803 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004804
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004805 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4806 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004807 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004808 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004809 goto end;
4810 }
4811
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004812 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4813 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004814 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004815 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004816
4817 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4818 /* In theory, we don't need to read anymore, but we must
4819 * still monitor the server connection for a possible close
4820 * while the request is being uploaded, so we don't disable
4821 * reading.
4822 */
4823 /* channel_dont_read(chn); */
4824
4825 if (txn->req.msg_state < HTTP_MSG_DONE) {
4826 /* The client seems to still be sending data, probably
4827 * because we got an error response during an upload.
4828 * We have the choice of either breaking the connection
4829 * or letting it pass through. Let's do the later.
4830 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004831 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004832 return;
4833 }
4834
4835 /* When we get here, it means that both the request and the
4836 * response have finished receiving. Depending on the connection
4837 * mode, we'll have to wait for the last bytes to leave in either
4838 * direction, and sometimes for a close to be effective.
4839 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004840 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004841 channel_auto_read(chn);
4842 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004843 if (b_data(&chn->buf)) {
4844 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004845 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004846 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004847 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4848 }
4849 else {
4850 /* we're not expecting any new data to come for this
4851 * transaction, so we can close it.
4852 */
4853 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4854 channel_shutr_now(chn);
4855 channel_shutw_now(chn);
4856 }
4857 }
4858 goto check_channel_flags;
4859 }
4860
4861 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4862 http_msg_closing:
4863 /* nothing else to forward, just waiting for the output buffer
4864 * to be empty and for the shutw_now to take effect.
4865 */
4866 if (channel_is_empty(chn)) {
4867 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4868 goto http_msg_closed;
4869 }
4870 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004871 txn->rsp.msg_state = HTTP_MSG_ERROR;
Olivier Houcharda798bf52019-03-08 18:52:00 +01004872 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004873 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004874 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004875 goto end;
4876 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004877 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004878 return;
4879 }
4880
4881 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4882 http_msg_closed:
4883 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004884 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004885 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004886 goto end;
4887 }
4888
4889 check_channel_flags:
4890 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4891 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4892 /* if we've just closed an output, let's switch */
4893 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4894 goto http_msg_closing;
4895 }
4896
4897 end:
4898 chn->analysers &= AN_RES_FLT_END;
4899 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4900 chn->analysers |= AN_RES_FLT_XFER_DATA;
4901 channel_auto_close(chn);
4902 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004903 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004904}
4905
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004906void http_server_error(struct stream *s, struct stream_interface *si, int err,
4907 int finst, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004908{
4909 channel_auto_read(si_oc(si));
4910 channel_abort(si_oc(si));
4911 channel_auto_close(si_oc(si));
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004912 channel_htx_erase(si_oc(si), htxbuf(&(si_oc(si))->buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02004913 channel_auto_close(si_ic(si));
4914 channel_auto_read(si_ic(si));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004915
4916 /* <msg> is an HTX structure. So we copy it in the response's
4917 * channel */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004918 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004919 struct channel *chn = si_ic(si);
4920 struct htx *htx;
4921
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004922 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004923 chn->buf.data = msg->data;
4924 memcpy(chn->buf.area, msg->area, msg->data);
4925 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02004926 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02004927 c_adv(chn, htx->data);
4928 chn->total += htx->data;
4929 }
4930 if (!(s->flags & SF_ERR_MASK))
4931 s->flags |= err;
4932 if (!(s->flags & SF_FINST_MASK))
4933 s->flags |= finst;
4934}
4935
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004936void http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004937{
4938 channel_auto_read(&s->req);
4939 channel_abort(&s->req);
4940 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004941 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
4942 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02004943
4944 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004945
4946 /* <msg> is an HTX structure. So we copy it in the response's
4947 * channel */
4948 /* FIXME: It is a problem for now if there is some outgoing data */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004949 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004950 struct channel *chn = &s->res;
4951 struct htx *htx;
4952
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004953 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004954 chn->buf.data = msg->data;
4955 memcpy(chn->buf.area, msg->area, msg->data);
4956 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02004957 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02004958 c_adv(chn, htx->data);
4959 chn->total += htx->data;
4960 }
4961
4962 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
4963 channel_auto_read(&s->res);
4964 channel_auto_close(&s->res);
4965 channel_shutr_now(&s->res);
4966}
4967
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004968struct buffer *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004969{
4970 const int msgnum = http_get_status_idx(s->txn->status);
4971
4972 if (s->be->errmsg[msgnum].area)
4973 return &s->be->errmsg[msgnum];
4974 else if (strm_fe(s)->errmsg[msgnum].area)
4975 return &strm_fe(s)->errmsg[msgnum];
4976 else
Christopher Fauletf7346382019-07-17 22:02:08 +02004977 return &http_err_chunks[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004978}
4979
Christopher Faulet304cc402019-07-15 15:46:28 +02004980/* Return the error message corresponding to si->err_type. It is assumed
4981 * that the server side is closed. Note that err_type is actually a
4982 * bitmask, where almost only aborts may be cumulated with other
4983 * values. We consider that aborted operations are more important
4984 * than timeouts or errors due to the fact that nobody else in the
4985 * logs might explain incomplete retries. All others should avoid
4986 * being cumulated. It should normally not be possible to have multiple
4987 * aborts at once, but just in case, the first one in sequence is reported.
4988 * Note that connection errors appearing on the second request of a keep-alive
4989 * connection are not reported since this allows the client to retry.
4990 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004991void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004992{
4993 int err_type = si->err_type;
4994
4995 /* set s->txn->status for http_error_message(s) */
4996 s->txn->status = 503;
4997
4998 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004999 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
5000 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005001 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005002 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
5003 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5004 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005005 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005006 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
5007 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005008 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005009 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
5010 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005011 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005012 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
5013 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5014 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005015 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005016 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
5017 (s->flags & SF_SRV_REUSED) ? NULL :
5018 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005019 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005020 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
5021 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5022 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005023 else { /* SI_ET_CONN_OTHER and others */
5024 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005025 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
5026 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005027 }
5028}
5029
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005030
Christopher Faulet4a28a532019-03-01 11:19:40 +01005031/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
5032 * on success and -1 on error.
5033 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005034static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01005035{
5036 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
5037 * then we must send an HTTP/1.1 100 Continue intermediate response.
5038 */
5039 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
5040 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
5041 struct ist hdr = { .ptr = "Expect", .len = 6 };
5042 struct http_hdr_ctx ctx;
5043
5044 ctx.blk = NULL;
5045 /* Expect is allowed in 1.1, look for it */
5046 if (http_find_header(htx, hdr, &ctx, 0) &&
5047 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005048 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01005049 return -1;
5050 http_remove_header(htx, &ctx);
5051 }
5052 }
5053 return 0;
5054}
5055
Christopher Faulet23a3c792018-11-28 10:01:23 +01005056/* Send a 100-Continue response to the client. It returns 0 on success and -1
5057 * on error. The response channel is updated accordingly.
5058 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005059static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01005060{
5061 struct channel *res = &s->res;
5062 struct htx *htx = htx_from_buf(&res->buf);
5063 struct htx_sl *sl;
5064 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
5065 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
5066 size_t data;
5067
5068 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5069 ist("HTTP/1.1"), ist("100"), ist("Continue"));
5070 if (!sl)
5071 goto fail;
5072 sl->info.res.status = 100;
5073
Christopher Faulet1d5ec092019-06-26 14:23:54 +02005074 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01005075 goto fail;
5076
5077 data = htx->data - co_data(res);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005078 c_adv(res, data);
5079 res->total += data;
5080 return 0;
5081
5082 fail:
5083 /* If an error occurred, remove the incomplete HTTP response from the
5084 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005085 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005086 return -1;
5087}
5088
Christopher Faulet12c51e22018-11-28 15:59:42 +01005089
5090/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
5091 * ont whether we use a proxy or not. It returns 0 on success and -1 on
5092 * error. The response channel is updated accordingly.
5093 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005094static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
Christopher Faulet12c51e22018-11-28 15:59:42 +01005095{
5096 struct channel *res = &s->res;
5097 struct htx *htx = htx_from_buf(&res->buf);
5098 struct htx_sl *sl;
5099 struct ist code, body;
5100 int status;
5101 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5102 size_t data;
5103
5104 if (!(s->txn->flags & TX_USE_PX_CONN)) {
5105 status = 401;
5106 code = ist("401");
5107 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
5108 "You need a valid user and password to access this content.\n"
5109 "</body></html>\n");
5110 }
5111 else {
5112 status = 407;
5113 code = ist("407");
5114 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
5115 "You need a valid user and password to access this content.\n"
5116 "</body></html>\n");
5117 }
5118
5119 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5120 ist("HTTP/1.1"), code, ist("Unauthorized"));
5121 if (!sl)
5122 goto fail;
5123 sl->info.res.status = status;
5124 s->txn->status = status;
5125
5126 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
5127 goto fail;
5128
Willy Tarreaub5ba2b02019-06-11 16:08:25 +02005129 if (!htx_add_header(htx, ist("Content-length"), ist("112")) ||
5130 !htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Faulet12c51e22018-11-28 15:59:42 +01005131 !htx_add_header(htx, ist("Connection"), ist("close")) ||
Jérôme Magnin86cef232018-12-28 14:49:08 +01005132 !htx_add_header(htx, ist("Content-Type"), ist("text/html")))
5133 goto fail;
5134 if (status == 401 && !htx_add_header(htx, ist("WWW-Authenticate"), ist2(trash.area, trash.data)))
5135 goto fail;
5136 if (status == 407 && !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005137 goto fail;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02005138 if (!htx_add_endof(htx, HTX_BLK_EOH))
5139 goto fail;
5140
5141 while (body.len) {
5142 size_t sent = htx_add_data(htx, body);
5143 if (!sent)
5144 goto fail;
5145 body.ptr += sent;
5146 body.len -= sent;
5147 }
5148
5149 if (!htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005150 goto fail;
5151
Christopher Faulet06511812019-10-16 09:38:27 +02005152 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet12c51e22018-11-28 15:59:42 +01005153 data = htx->data - co_data(res);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005154 c_adv(res, data);
5155 res->total += data;
5156
5157 channel_auto_read(&s->req);
5158 channel_abort(&s->req);
5159 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005160 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet12c51e22018-11-28 15:59:42 +01005161
5162 res->wex = tick_add_ifset(now_ms, res->wto);
5163 channel_auto_read(res);
5164 channel_auto_close(res);
5165 channel_shutr_now(res);
5166 return 0;
5167
5168 fail:
5169 /* If an error occurred, remove the incomplete HTTP response from the
5170 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005171 channel_htx_truncate(res, htx);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005172 return -1;
5173}
5174
Christopher Faulet0f226952018-10-22 09:29:56 +02005175/*
5176 * Capture headers from message <htx> according to header list <cap_hdr>, and
5177 * fill the <cap> pointers appropriately.
5178 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005179static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02005180{
5181 struct cap_hdr *h;
5182 int32_t pos;
5183
Christopher Fauleta3f15502019-05-13 15:27:23 +02005184 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02005185 struct htx_blk *blk = htx_get_blk(htx, pos);
5186 enum htx_blk_type type = htx_get_blk_type(blk);
5187 struct ist n, v;
5188
5189 if (type == HTX_BLK_EOH)
5190 break;
5191 if (type != HTX_BLK_HDR)
5192 continue;
5193
5194 n = htx_get_blk_name(htx, blk);
5195
5196 for (h = cap_hdr; h; h = h->next) {
5197 if (h->namelen && (h->namelen == n.len) &&
5198 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
5199 if (cap[h->index] == NULL)
5200 cap[h->index] =
5201 pool_alloc(h->pool);
5202
5203 if (cap[h->index] == NULL) {
5204 ha_alert("HTTP capture : out of memory.\n");
5205 break;
5206 }
5207
5208 v = htx_get_blk_value(htx, blk);
5209 if (v.len > h->len)
5210 v.len = h->len;
5211
5212 memcpy(cap[h->index], v.ptr, v.len);
5213 cap[h->index][v.len]=0;
5214 }
5215 }
5216 }
5217}
5218
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005219/* Delete a value in a header between delimiters <from> and <next>. The header
5220 * itself is delimited by <start> and <end> pointers. The number of characters
5221 * displaced is returned, and the pointer to the first delimiter is updated if
5222 * required. The function tries as much as possible to respect the following
5223 * principles :
5224 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5225 * in which case <next> is simply removed
5226 * - set exactly one space character after the new first delimiter, unless there
5227 * are not enough characters in the block being moved to do so.
5228 * - remove unneeded spaces before the previous delimiter and after the new
5229 * one.
5230 *
5231 * It is the caller's responsibility to ensure that :
5232 * - <from> points to a valid delimiter or <start> ;
5233 * - <next> points to a valid delimiter or <end> ;
5234 * - there are non-space chars before <from>.
5235 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005236static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005237{
5238 char *prev = *from;
5239
5240 if (prev == start) {
5241 /* We're removing the first value. eat the semicolon, if <next>
5242 * is lower than <end> */
5243 if (next < end)
5244 next++;
5245
5246 while (next < end && HTTP_IS_SPHT(*next))
5247 next++;
5248 }
5249 else {
5250 /* Remove useless spaces before the old delimiter. */
5251 while (HTTP_IS_SPHT(*(prev-1)))
5252 prev--;
5253 *from = prev;
5254
5255 /* copy the delimiter and if possible a space if we're
5256 * not at the end of the line.
5257 */
5258 if (next < end) {
5259 *prev++ = *next++;
5260 if (prev + 1 < next)
5261 *prev++ = ' ';
5262 while (next < end && HTTP_IS_SPHT(*next))
5263 next++;
5264 }
5265 }
5266 memmove(prev, next, end - next);
5267 return (prev - next);
5268}
5269
Christopher Faulet0f226952018-10-22 09:29:56 +02005270
5271/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005272 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005273 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005274static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005275{
5276 struct ist dst = ist2(str, 0);
5277
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005278 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005279 goto end;
5280 if (dst.len + 1 > len)
5281 goto end;
5282 dst.ptr[dst.len++] = ' ';
5283
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005284 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005285 goto end;
5286 if (dst.len + 1 > len)
5287 goto end;
5288 dst.ptr[dst.len++] = ' ';
5289
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005290 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005291 end:
5292 return dst.len;
5293}
5294
5295/*
5296 * Print a debug line with a start line.
5297 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005298static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005299{
5300 struct session *sess = strm_sess(s);
5301 int max;
5302
5303 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5304 dir,
5305 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5306 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5307
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005308 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005309 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005310 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005311 trash.area[trash.data++] = ' ';
5312
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005313 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005314 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005315 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005316 trash.area[trash.data++] = ' ';
5317
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005318 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005319 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005320 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005321 trash.area[trash.data++] = '\n';
5322
5323 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5324}
5325
5326/*
5327 * Print a debug line with a header.
5328 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005329static 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 +02005330{
5331 struct session *sess = strm_sess(s);
5332 int max;
5333
5334 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5335 dir,
5336 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5337 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5338
5339 max = n.len;
5340 UBOUND(max, trash.size - trash.data - 3);
5341 chunk_memcat(&trash, n.ptr, max);
5342 trash.area[trash.data++] = ':';
5343 trash.area[trash.data++] = ' ';
5344
5345 max = v.len;
5346 UBOUND(max, trash.size - trash.data - 1);
5347 chunk_memcat(&trash, v.ptr, max);
5348 trash.area[trash.data++] = '\n';
5349
5350 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5351}
5352
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005353/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5354 * In case of allocation failure, everything allocated is freed and NULL is
5355 * returned. Otherwise the new transaction is assigned to the stream and
5356 * returned.
5357 */
5358struct http_txn *http_alloc_txn(struct stream *s)
5359{
5360 struct http_txn *txn = s->txn;
5361
5362 if (txn)
5363 return txn;
5364
5365 txn = pool_alloc(pool_head_http_txn);
5366 if (!txn)
5367 return txn;
5368
5369 s->txn = txn;
5370 return txn;
5371}
5372
5373void http_txn_reset_req(struct http_txn *txn)
5374{
5375 txn->req.flags = 0;
5376 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5377}
5378
5379void http_txn_reset_res(struct http_txn *txn)
5380{
5381 txn->rsp.flags = 0;
5382 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5383}
5384
5385/*
5386 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5387 * the required fields are properly allocated and that we only need to (re)init
5388 * them. This should be used before processing any new request.
5389 */
5390void http_init_txn(struct stream *s)
5391{
5392 struct http_txn *txn = s->txn;
5393 struct conn_stream *cs = objt_cs(s->si[0].end);
5394
5395 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5396 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5397 : 0);
5398 txn->status = -1;
5399 *(unsigned int *)txn->cache_hash = 0;
5400
5401 txn->cookie_first_date = 0;
5402 txn->cookie_last_date = 0;
5403
5404 txn->srv_cookie = NULL;
5405 txn->cli_cookie = NULL;
5406 txn->uri = NULL;
5407
5408 http_txn_reset_req(txn);
5409 http_txn_reset_res(txn);
5410
5411 txn->req.chn = &s->req;
5412 txn->rsp.chn = &s->res;
5413
5414 txn->auth.method = HTTP_AUTH_UNKNOWN;
5415
5416 vars_init(&s->vars_txn, SCOPE_TXN);
5417 vars_init(&s->vars_reqres, SCOPE_REQ);
5418}
5419
5420/* to be used at the end of a transaction */
5421void http_end_txn(struct stream *s)
5422{
5423 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005424
5425 /* these ones will have been dynamically allocated */
5426 pool_free(pool_head_requri, txn->uri);
5427 pool_free(pool_head_capture, txn->cli_cookie);
5428 pool_free(pool_head_capture, txn->srv_cookie);
5429 pool_free(pool_head_uniqueid, s->unique_id);
5430
5431 s->unique_id = NULL;
5432 txn->uri = NULL;
5433 txn->srv_cookie = NULL;
5434 txn->cli_cookie = NULL;
5435
Christopher Faulet59399252019-11-07 14:27:52 +01005436 if (!LIST_ISEMPTY(&s->vars_txn.head))
5437 vars_prune(&s->vars_txn, s->sess, s);
5438 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5439 vars_prune(&s->vars_reqres, s->sess, s);
5440}
5441
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005442
5443DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
5444DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
Christopher Faulet0f226952018-10-22 09:29:56 +02005445
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005446__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005447static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005448{
5449}
5450
5451
5452/*
5453 * Local variables:
5454 * c-indent-level: 8
5455 * c-basic-offset: 8
5456 * End:
5457 */