blob: ee00d2c76121c4b600af751db6a8d350187b5eb5 [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);
2529 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2530 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2531 if (!sl)
2532 goto fail;
2533 sl->info.res.status = rule->code;
2534 s->txn->status = rule->code;
2535
Christopher Faulet08e66462019-05-23 16:44:59 +02002536 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2537 goto fail;
2538
2539 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002540 !htx_add_header(htx, ist("Location"), location))
2541 goto fail;
2542
2543 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2544 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2545 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002546 }
2547
2548 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002549 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2550 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002551 }
2552
Christopher Faulet99daf282018-11-28 22:58:13 +01002553 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2554 goto fail;
2555
Christopher Fauletf2824e62018-10-01 12:12:37 +02002556 /* let's log the request time */
2557 s->logs.tv_request = now;
2558
Christopher Faulet06511812019-10-16 09:38:27 +02002559 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet99daf282018-11-28 22:58:13 +01002560 data = htx->data - co_data(res);
Christopher Faulet99daf282018-11-28 22:58:13 +01002561 c_adv(res, data);
2562 res->total += data;
2563
2564 channel_auto_read(req);
2565 channel_abort(req);
2566 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002567 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet99daf282018-11-28 22:58:13 +01002568
2569 res->wex = tick_add_ifset(now_ms, res->wto);
2570 channel_auto_read(res);
2571 channel_auto_close(res);
2572 channel_shutr_now(res);
2573
2574 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002575
2576 if (!(s->flags & SF_ERR_MASK))
2577 s->flags |= SF_ERR_LOCAL;
2578 if (!(s->flags & SF_FINST_MASK))
2579 s->flags |= SF_FINST_R;
2580
Christopher Faulet99daf282018-11-28 22:58:13 +01002581 free_trash_chunk(chunk);
2582 return 1;
2583
2584 fail:
2585 /* If an error occurred, remove the incomplete HTTP response from the
2586 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002587 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002588 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002589 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002590}
2591
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002592int http_transform_header_str(struct stream* s, struct channel *chn, struct htx *htx,
2593 struct ist name, const char *str, struct my_regex *re, int action)
Christopher Faulet72333522018-10-24 11:25:02 +02002594{
2595 struct http_hdr_ctx ctx;
2596 struct buffer *output = get_trash_chunk();
2597
2598 /* find full header is action is ACT_HTTP_REPLACE_HDR */
2599 ctx.blk = NULL;
2600 while (http_find_header(htx, name, &ctx, (action == ACT_HTTP_REPLACE_HDR))) {
2601 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2602 continue;
2603
2604 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2605 if (output->data == -1)
2606 return -1;
2607 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2608 return -1;
2609 }
2610 return 0;
2611}
2612
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002613static int http_transform_header(struct stream* s, struct channel *chn, struct htx *htx,
2614 const struct ist name, struct list *fmt, struct my_regex *re, int action)
Christopher Faulet72333522018-10-24 11:25:02 +02002615{
2616 struct buffer *replace;
2617 int ret = -1;
2618
2619 replace = alloc_trash_chunk();
2620 if (!replace)
2621 goto leave;
2622
2623 replace->data = build_logline(s, replace->area, replace->size, fmt);
2624 if (replace->data >= replace->size - 1)
2625 goto leave;
2626
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002627 ret = http_transform_header_str(s, chn, htx, name, replace->area, re, action);
Christopher Faulet72333522018-10-24 11:25:02 +02002628
2629 leave:
2630 free_trash_chunk(replace);
2631 return ret;
2632}
2633
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002634
2635/* Terminate a 103-Erly-hints response and send it to the client. It returns 0
2636 * on success and -1 on error. The response channel is updated accordingly.
2637 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002638static int http_reply_103_early_hints(struct channel *res)
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002639{
2640 struct htx *htx = htx_from_buf(&res->buf);
2641 size_t data;
2642
Christopher Faulet1d5ec092019-06-26 14:23:54 +02002643 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002644 /* If an error occurred during an Early-hint rule,
2645 * remove the incomplete HTTP 103 response from the
2646 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002647 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002648 return -1;
2649 }
2650
2651 data = htx->data - co_data(res);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002652 c_adv(res, data);
2653 res->total += data;
2654 return 0;
2655}
2656
Christopher Faulet6eb92892018-11-15 16:39:29 +01002657/*
2658 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
2659 * built according to <fmt> log line format.
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002660 * If <early_hints> is 0, it is starts a new response by adding the start
2661 * line. If an error occurred -1 is returned. On success 0 is returned. The
2662 * channel is not updated here. It must be done calling the function
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002663 * http_reply_103_early_hints().
Christopher Faulet6eb92892018-11-15 16:39:29 +01002664 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002665static 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 +01002666{
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002667 struct channel *res = &s->res;
2668 struct htx *htx = htx_from_buf(&res->buf);
2669 struct buffer *value = alloc_trash_chunk();
2670
Christopher Faulet6eb92892018-11-15 16:39:29 +01002671 if (!early_hints) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002672 struct htx_sl *sl;
2673 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
2674 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2675
2676 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
2677 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
2678 if (!sl)
Christopher Faulet6eb92892018-11-15 16:39:29 +01002679 goto fail;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002680 sl->info.res.status = 103;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002681 }
2682
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002683 value->data = build_logline(s, b_tail(value), b_room(value), fmt);
2684 if (!htx_add_header(htx, name, ist2(b_head(value), b_data(value))))
Christopher Faulet6eb92892018-11-15 16:39:29 +01002685 goto fail;
2686
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002687 free_trash_chunk(value);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002688 return 1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002689
2690 fail:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002691 /* If an error occurred during an Early-hint rule, remove the incomplete
2692 * HTTP 103 response from the buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002693 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002694 free_trash_chunk(value);
2695 return -1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002696}
2697
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002698/* This function executes one of the set-{method,path,query,uri} actions. It
2699 * takes the string from the variable 'replace' with length 'len', then modifies
2700 * the relevant part of the request line accordingly. Then it updates various
2701 * pointers to the next elements which were moved, and the total buffer length.
2702 * It finds the action to be performed in p[2], previously filled by function
2703 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2704 * error, though this can be revisited when this code is finally exploited.
2705 *
2706 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2707 * query string and 3 to replace uri.
2708 *
2709 * In query string case, the mark question '?' must be set at the start of the
2710 * string by the caller, event if the replacement query string is empty.
2711 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002712int http_req_replace_stline(int action, const char *replace, int len,
2713 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002714{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002715 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002716
2717 switch (action) {
2718 case 0: // method
2719 if (!http_replace_req_meth(htx, ist2(replace, len)))
2720 return -1;
2721 break;
2722
2723 case 1: // path
2724 if (!http_replace_req_path(htx, ist2(replace, len)))
2725 return -1;
2726 break;
2727
2728 case 2: // query
2729 if (!http_replace_req_query(htx, ist2(replace, len)))
2730 return -1;
2731 break;
2732
2733 case 3: // uri
2734 if (!http_replace_req_uri(htx, ist2(replace, len)))
2735 return -1;
2736 break;
2737
2738 default:
2739 return -1;
2740 }
2741 return 0;
2742}
2743
2744/* This function replace the HTTP status code and the associated message. The
2745 * variable <status> contains the new status code. This function never fails.
2746 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002747void http_res_set_status(unsigned int status, const char *reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002748{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002749 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002750 char *res;
2751
2752 chunk_reset(&trash);
2753 res = ultoa_o(status, trash.area, trash.size);
2754 trash.data = res - trash.area;
2755
2756 /* Do we have a custom reason format string? */
2757 if (reason == NULL)
2758 reason = http_get_reason(status);
2759
Christopher Faulet87a2c0d2018-12-13 21:58:18 +01002760 if (http_replace_res_status(htx, ist2(trash.area, trash.data)))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002761 http_replace_res_reason(htx, ist2(reason, strlen(reason)));
2762}
2763
Christopher Faulet3e964192018-10-24 11:39:23 +02002764/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2765 * transaction <txn>. Returns the verdict of the first rule that prevents
2766 * further processing of the request (auth, deny, ...), and defaults to
2767 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2768 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2769 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2770 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2771 * status.
2772 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002773static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
2774 struct stream *s, int *deny_status)
Christopher Faulet3e964192018-10-24 11:39:23 +02002775{
2776 struct session *sess = strm_sess(s);
2777 struct http_txn *txn = s->txn;
2778 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002779 struct act_rule *rule;
2780 struct http_hdr_ctx ctx;
2781 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002782 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2783 int act_flags = 0;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002784 int early_hints = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002785
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002786 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002787
2788 /* If "the current_rule_list" match the executed rule list, we are in
2789 * resume condition. If a resume is needed it is always in the action
2790 * and never in the ACL or converters. In this case, we initialise the
2791 * current rule, and go to the action execution point.
2792 */
2793 if (s->current_rule) {
2794 rule = s->current_rule;
2795 s->current_rule = NULL;
2796 if (s->current_rule_list == rules)
2797 goto resume_execution;
2798 }
2799 s->current_rule_list = rules;
2800
2801 list_for_each_entry(rule, rules, list) {
2802 /* check optional condition */
2803 if (rule->cond) {
2804 int ret;
2805
2806 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2807 ret = acl_pass(ret);
2808
2809 if (rule->cond->pol == ACL_COND_UNLESS)
2810 ret = !ret;
2811
2812 if (!ret) /* condition not matched */
2813 continue;
2814 }
2815
2816 act_flags |= ACT_FLAG_FIRST;
2817 resume_execution:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002818 if (early_hints && rule->action != ACT_HTTP_EARLY_HINT) {
2819 early_hints = 0;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002820 if (http_reply_103_early_hints(&s->res) == -1) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002821 rule_ret = HTTP_RULE_RES_BADREQ;
2822 goto end;
2823 }
2824 }
2825
Christopher Faulet3e964192018-10-24 11:39:23 +02002826 switch (rule->action) {
2827 case ACT_ACTION_ALLOW:
2828 rule_ret = HTTP_RULE_RES_STOP;
2829 goto end;
2830
2831 case ACT_ACTION_DENY:
2832 if (deny_status)
2833 *deny_status = rule->deny_status;
2834 rule_ret = HTTP_RULE_RES_DENY;
2835 goto end;
2836
2837 case ACT_HTTP_REQ_TARPIT:
2838 txn->flags |= TX_CLTARPIT;
2839 if (deny_status)
2840 *deny_status = rule->deny_status;
2841 rule_ret = HTTP_RULE_RES_DENY;
2842 goto end;
2843
2844 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002845 /* Auth might be performed on regular http-req rules as well as on stats */
2846 auth_realm = rule->arg.auth.realm;
2847 if (!auth_realm) {
2848 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2849 auth_realm = STATS_DEFAULT_REALM;
2850 else
2851 auth_realm = px->id;
2852 }
2853 /* send 401/407 depending on whether we use a proxy or not. We still
2854 * count one error, because normal browsing won't significantly
2855 * increase the counter but brute force attempts will.
2856 */
Christopher Faulet3e964192018-10-24 11:39:23 +02002857 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002858 if (http_reply_40x_unauthorized(s, auth_realm) == -1)
Christopher Faulet12c51e22018-11-28 15:59:42 +01002859 rule_ret = HTTP_RULE_RES_BADREQ;
2860 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02002861 goto end;
2862
2863 case ACT_HTTP_REDIR:
Christopher Faulet3e964192018-10-24 11:39:23 +02002864 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002865 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3e964192018-10-24 11:39:23 +02002866 rule_ret = HTTP_RULE_RES_BADREQ;
2867 goto end;
2868
2869 case ACT_HTTP_SET_NICE:
2870 s->task->nice = rule->arg.nice;
2871 break;
2872
2873 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01002874 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Christopher Faulet3e964192018-10-24 11:39:23 +02002875 break;
2876
2877 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01002878 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Christopher Faulet3e964192018-10-24 11:39:23 +02002879 break;
2880
2881 case ACT_HTTP_SET_LOGL:
2882 s->logs.level = rule->arg.loglevel;
2883 break;
2884
2885 case ACT_HTTP_REPLACE_HDR:
2886 case ACT_HTTP_REPLACE_VAL:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002887 if (http_transform_header(s, &s->req, htx,
2888 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
2889 &rule->arg.hdr_add.fmt,
2890 rule->arg.hdr_add.re, rule->action)) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002891 rule_ret = HTTP_RULE_RES_BADREQ;
2892 goto end;
2893 }
2894 break;
2895
2896 case ACT_HTTP_DEL_HDR:
2897 /* remove all occurrences of the header */
2898 ctx.blk = NULL;
2899 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
2900 http_remove_header(htx, &ctx);
2901 break;
2902
2903 case ACT_HTTP_SET_HDR:
2904 case ACT_HTTP_ADD_HDR: {
2905 /* The scope of the trash buffer must be limited to this function. The
2906 * build_logline() function can execute a lot of other function which
2907 * can use the trash buffer. So for limiting the scope of this global
2908 * buffer, we build first the header value using build_logline, and
2909 * after we store the header name.
2910 */
2911 struct buffer *replace;
2912 struct ist n, v;
2913
2914 replace = alloc_trash_chunk();
2915 if (!replace) {
2916 rule_ret = HTTP_RULE_RES_BADREQ;
2917 goto end;
2918 }
2919
2920 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
2921 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2922 v = ist2(replace->area, replace->data);
2923
2924 if (rule->action == ACT_HTTP_SET_HDR) {
2925 /* remove all occurrences of the header */
2926 ctx.blk = NULL;
2927 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
2928 http_remove_header(htx, &ctx);
2929 }
2930
2931 if (!http_add_header(htx, n, v)) {
2932 static unsigned char rate_limit = 0;
2933
2934 if ((rate_limit++ & 255) == 0) {
2935 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);
2936 }
2937
Olivier Houcharda798bf52019-03-08 18:52:00 +01002938 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002939 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002940 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002941 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002942 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002943 }
2944 free_trash_chunk(replace);
2945 break;
2946 }
2947
2948 case ACT_HTTP_DEL_ACL:
2949 case ACT_HTTP_DEL_MAP: {
2950 struct pat_ref *ref;
2951 struct buffer *key;
2952
2953 /* collect reference */
2954 ref = pat_ref_lookup(rule->arg.map.ref);
2955 if (!ref)
2956 continue;
2957
2958 /* allocate key */
2959 key = alloc_trash_chunk();
2960 if (!key) {
2961 rule_ret = HTTP_RULE_RES_BADREQ;
2962 goto end;
2963 }
2964
2965 /* collect key */
2966 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2967 key->area[key->data] = '\0';
2968
2969 /* perform update */
2970 /* returned code: 1=ok, 0=ko */
2971 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
2972 pat_ref_delete(ref, key->area);
2973 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
2974
2975 free_trash_chunk(key);
2976 break;
2977 }
2978
2979 case ACT_HTTP_ADD_ACL: {
2980 struct pat_ref *ref;
2981 struct buffer *key;
2982
2983 /* collect reference */
2984 ref = pat_ref_lookup(rule->arg.map.ref);
2985 if (!ref)
2986 continue;
2987
2988 /* allocate key */
2989 key = alloc_trash_chunk();
2990 if (!key) {
2991 rule_ret = HTTP_RULE_RES_BADREQ;
2992 goto end;
2993 }
2994
2995 /* collect key */
2996 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2997 key->area[key->data] = '\0';
2998
2999 /* perform update */
3000 /* add entry only if it does not already exist */
3001 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3002 if (pat_ref_find_elt(ref, key->area) == NULL)
3003 pat_ref_add(ref, key->area, NULL, NULL);
3004 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3005
3006 free_trash_chunk(key);
3007 break;
3008 }
3009
3010 case ACT_HTTP_SET_MAP: {
3011 struct pat_ref *ref;
3012 struct buffer *key, *value;
3013
3014 /* collect reference */
3015 ref = pat_ref_lookup(rule->arg.map.ref);
3016 if (!ref)
3017 continue;
3018
3019 /* allocate key */
3020 key = alloc_trash_chunk();
3021 if (!key) {
3022 rule_ret = HTTP_RULE_RES_BADREQ;
3023 goto end;
3024 }
3025
3026 /* allocate value */
3027 value = alloc_trash_chunk();
3028 if (!value) {
3029 free_trash_chunk(key);
3030 rule_ret = HTTP_RULE_RES_BADREQ;
3031 goto end;
3032 }
3033
3034 /* collect key */
3035 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3036 key->area[key->data] = '\0';
3037
3038 /* collect value */
3039 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3040 value->area[value->data] = '\0';
3041
3042 /* perform update */
Christopher Faulete84289e2019-04-19 14:50:55 +02003043 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003044 if (pat_ref_find_elt(ref, key->area) != NULL)
3045 /* update entry if it exists */
3046 pat_ref_set(ref, key->area, value->area, NULL);
3047 else
3048 /* insert a new entry */
3049 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02003050 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003051 free_trash_chunk(key);
3052 free_trash_chunk(value);
3053 break;
3054 }
3055
3056 case ACT_HTTP_EARLY_HINT:
3057 if (!(txn->req.flags & HTTP_MSGF_VER_11))
3058 break;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003059 early_hints = http_add_early_hint_header(s, early_hints,
3060 ist2(rule->arg.early_hint.name, rule->arg.early_hint.name_len),
3061 &rule->arg.early_hint.fmt);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003062 if (early_hints == -1) {
3063 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02003064 goto end;
3065 }
3066 break;
3067
3068 case ACT_CUSTOM:
3069 if ((s->req.flags & CF_READ_ERROR) ||
3070 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Christopher Faulet3e964192018-10-24 11:39:23 +02003071 (px->options & PR_O_ABRT_CLOSE)))
3072 act_flags |= ACT_FLAG_FINAL;
3073
3074 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
3075 case ACT_RET_ERR:
3076 case ACT_RET_CONT:
3077 break;
3078 case ACT_RET_STOP:
Christopher Faulet8f1aa772019-07-04 11:27:15 +02003079 rule_ret = HTTP_RULE_RES_STOP;
3080 goto end;
3081 case ACT_RET_DONE:
Christopher Faulet3e964192018-10-24 11:39:23 +02003082 rule_ret = HTTP_RULE_RES_DONE;
3083 goto end;
3084 case ACT_RET_YIELD:
3085 s->current_rule = rule;
3086 rule_ret = HTTP_RULE_RES_YIELD;
3087 goto end;
3088 }
3089 break;
3090
3091 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3092 /* Note: only the first valid tracking parameter of each
3093 * applies.
3094 */
3095
3096 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3097 struct stktable *t;
3098 struct stksess *ts;
3099 struct stktable_key *key;
3100 void *ptr1, *ptr2;
3101
3102 t = rule->arg.trk_ctr.table.t;
3103 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL,
3104 rule->arg.trk_ctr.expr, NULL);
3105
3106 if (key && (ts = stktable_get_entry(t, key))) {
3107 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3108
3109 /* let's count a new HTTP request as it's the first time we do it */
3110 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3111 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3112 if (ptr1 || ptr2) {
3113 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3114
3115 if (ptr1)
3116 stktable_data_cast(ptr1, http_req_cnt)++;
3117
3118 if (ptr2)
3119 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
3120 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3121
3122 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3123
3124 /* If data was modified, we need to touch to re-schedule sync */
3125 stktable_touch_local(t, ts, 0);
3126 }
3127
3128 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3129 if (sess->fe != s->be)
3130 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3131 }
3132 }
3133 break;
3134
Joseph Herlantc42c0e92018-11-25 10:43:27 -08003135 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003136 default:
3137 break;
3138 }
3139 }
3140
3141 end:
3142 if (early_hints) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003143 if (http_reply_103_early_hints(&s->res) == -1)
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003144 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02003145 }
3146
3147 /* we reached the end of the rules, nothing to report */
3148 return rule_ret;
3149}
3150
3151/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3152 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3153 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3154 * is returned, the process can continue the evaluation of next rule list. If
3155 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3156 * is returned, it means the operation could not be processed and a server error
3157 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3158 * deny rule. If *YIELD is returned, the caller must call again the function
3159 * with the same context.
3160 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003161static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
3162 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02003163{
3164 struct session *sess = strm_sess(s);
3165 struct http_txn *txn = s->txn;
3166 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02003167 struct act_rule *rule;
3168 struct http_hdr_ctx ctx;
3169 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
3170 int act_flags = 0;
3171
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003172 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02003173
3174 /* If "the current_rule_list" match the executed rule list, we are in
3175 * resume condition. If a resume is needed it is always in the action
3176 * and never in the ACL or converters. In this case, we initialise the
3177 * current rule, and go to the action execution point.
3178 */
3179 if (s->current_rule) {
3180 rule = s->current_rule;
3181 s->current_rule = NULL;
3182 if (s->current_rule_list == rules)
3183 goto resume_execution;
3184 }
3185 s->current_rule_list = rules;
3186
3187 list_for_each_entry(rule, rules, list) {
3188 /* check optional condition */
3189 if (rule->cond) {
3190 int ret;
3191
3192 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3193 ret = acl_pass(ret);
3194
3195 if (rule->cond->pol == ACL_COND_UNLESS)
3196 ret = !ret;
3197
3198 if (!ret) /* condition not matched */
3199 continue;
3200 }
3201
3202 act_flags |= ACT_FLAG_FIRST;
3203resume_execution:
3204 switch (rule->action) {
3205 case ACT_ACTION_ALLOW:
3206 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3207 goto end;
3208
3209 case ACT_ACTION_DENY:
3210 txn->flags |= TX_SVDENY;
3211 rule_ret = HTTP_RULE_RES_STOP;
3212 goto end;
3213
3214 case ACT_HTTP_SET_NICE:
3215 s->task->nice = rule->arg.nice;
3216 break;
3217
3218 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003219 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Christopher Faulet3e964192018-10-24 11:39:23 +02003220 break;
3221
3222 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003223 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Christopher Faulet3e964192018-10-24 11:39:23 +02003224 break;
3225
3226 case ACT_HTTP_SET_LOGL:
3227 s->logs.level = rule->arg.loglevel;
3228 break;
3229
3230 case ACT_HTTP_REPLACE_HDR:
3231 case ACT_HTTP_REPLACE_VAL:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003232 if (http_transform_header(s, &s->res, htx,
3233 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
3234 &rule->arg.hdr_add.fmt,
3235 rule->arg.hdr_add.re, rule->action)) {
Christopher Faulet3e964192018-10-24 11:39:23 +02003236 rule_ret = HTTP_RULE_RES_BADREQ;
3237 goto end;
3238 }
3239 break;
3240
3241 case ACT_HTTP_DEL_HDR:
3242 /* remove all occurrences of the header */
3243 ctx.blk = NULL;
3244 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3245 http_remove_header(htx, &ctx);
3246 break;
3247
3248 case ACT_HTTP_SET_HDR:
3249 case ACT_HTTP_ADD_HDR: {
3250 struct buffer *replace;
3251 struct ist n, v;
3252
3253 replace = alloc_trash_chunk();
3254 if (!replace) {
3255 rule_ret = HTTP_RULE_RES_BADREQ;
3256 goto end;
3257 }
3258
3259 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
3260 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3261 v = ist2(replace->area, replace->data);
3262
3263 if (rule->action == ACT_HTTP_SET_HDR) {
3264 /* remove all occurrences of the header */
3265 ctx.blk = NULL;
3266 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3267 http_remove_header(htx, &ctx);
3268 }
3269
3270 if (!http_add_header(htx, n, v)) {
3271 static unsigned char rate_limit = 0;
3272
3273 if ((rate_limit++ & 255) == 0) {
3274 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);
3275 }
3276
Olivier Houcharda798bf52019-03-08 18:52:00 +01003277 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003278 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003279 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003280 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003281 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003282 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01003283 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003284 }
3285 free_trash_chunk(replace);
3286 break;
3287 }
3288
3289 case ACT_HTTP_DEL_ACL:
3290 case ACT_HTTP_DEL_MAP: {
3291 struct pat_ref *ref;
3292 struct buffer *key;
3293
3294 /* collect reference */
3295 ref = pat_ref_lookup(rule->arg.map.ref);
3296 if (!ref)
3297 continue;
3298
3299 /* allocate key */
3300 key = alloc_trash_chunk();
3301 if (!key) {
3302 rule_ret = HTTP_RULE_RES_BADREQ;
3303 goto end;
3304 }
3305
3306 /* collect key */
3307 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3308 key->area[key->data] = '\0';
3309
3310 /* perform update */
3311 /* returned code: 1=ok, 0=ko */
3312 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3313 pat_ref_delete(ref, key->area);
3314 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3315
3316 free_trash_chunk(key);
3317 break;
3318 }
3319
3320 case ACT_HTTP_ADD_ACL: {
3321 struct pat_ref *ref;
3322 struct buffer *key;
3323
3324 /* collect reference */
3325 ref = pat_ref_lookup(rule->arg.map.ref);
3326 if (!ref)
3327 continue;
3328
3329 /* allocate key */
3330 key = alloc_trash_chunk();
3331 if (!key) {
3332 rule_ret = HTTP_RULE_RES_BADREQ;
3333 goto end;
3334 }
3335
3336 /* collect key */
3337 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3338 key->area[key->data] = '\0';
3339
3340 /* perform update */
3341 /* check if the entry already exists */
Christopher Faulete84289e2019-04-19 14:50:55 +02003342 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003343 if (pat_ref_find_elt(ref, key->area) == NULL)
3344 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02003345 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003346 free_trash_chunk(key);
3347 break;
3348 }
3349
3350 case ACT_HTTP_SET_MAP: {
3351 struct pat_ref *ref;
3352 struct buffer *key, *value;
3353
3354 /* collect reference */
3355 ref = pat_ref_lookup(rule->arg.map.ref);
3356 if (!ref)
3357 continue;
3358
3359 /* allocate key */
3360 key = alloc_trash_chunk();
3361 if (!key) {
3362 rule_ret = HTTP_RULE_RES_BADREQ;
3363 goto end;
3364 }
3365
3366 /* allocate value */
3367 value = alloc_trash_chunk();
3368 if (!value) {
3369 free_trash_chunk(key);
3370 rule_ret = HTTP_RULE_RES_BADREQ;
3371 goto end;
3372 }
3373
3374 /* collect key */
3375 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3376 key->area[key->data] = '\0';
3377
3378 /* collect value */
3379 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3380 value->area[value->data] = '\0';
3381
3382 /* perform update */
3383 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3384 if (pat_ref_find_elt(ref, key->area) != NULL)
3385 /* update entry if it exists */
3386 pat_ref_set(ref, key->area, value->area, NULL);
3387 else
3388 /* insert a new entry */
3389 pat_ref_add(ref, key->area, value->area, NULL);
3390 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3391 free_trash_chunk(key);
3392 free_trash_chunk(value);
3393 break;
3394 }
3395
3396 case ACT_HTTP_REDIR:
3397 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003398 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3e964192018-10-24 11:39:23 +02003399 rule_ret = HTTP_RULE_RES_BADREQ;
3400 goto end;
3401
3402 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3403 /* Note: only the first valid tracking parameter of each
3404 * applies.
3405 */
3406 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3407 struct stktable *t;
3408 struct stksess *ts;
3409 struct stktable_key *key;
3410 void *ptr;
3411
3412 t = rule->arg.trk_ctr.table.t;
3413 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL,
3414 rule->arg.trk_ctr.expr, NULL);
3415
3416 if (key && (ts = stktable_get_entry(t, key))) {
3417 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3418
3419 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3420
3421 /* let's count a new HTTP request as it's the first time we do it */
3422 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3423 if (ptr)
3424 stktable_data_cast(ptr, http_req_cnt)++;
3425
3426 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3427 if (ptr)
3428 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3429 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3430
3431 /* When the client triggers a 4xx from the server, it's most often due
3432 * to a missing object or permission. These events should be tracked
3433 * because if they happen often, it may indicate a brute force or a
3434 * vulnerability scan. Normally this is done when receiving the response
3435 * but here we're tracking after this ought to have been done so we have
3436 * to do it on purpose.
3437 */
3438 if ((unsigned)(txn->status - 400) < 100) {
3439 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3440 if (ptr)
3441 stktable_data_cast(ptr, http_err_cnt)++;
3442
3443 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3444 if (ptr)
3445 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3446 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3447 }
3448
3449 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3450
3451 /* If data was modified, we need to touch to re-schedule sync */
3452 stktable_touch_local(t, ts, 0);
3453
3454 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3455 if (sess->fe != s->be)
3456 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3457 }
3458 }
3459 break;
3460
3461 case ACT_CUSTOM:
3462 if ((s->req.flags & CF_READ_ERROR) ||
3463 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Christopher Faulet3e964192018-10-24 11:39:23 +02003464 (px->options & PR_O_ABRT_CLOSE)))
3465 act_flags |= ACT_FLAG_FINAL;
3466
3467 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
3468 case ACT_RET_ERR:
3469 case ACT_RET_CONT:
3470 break;
3471 case ACT_RET_STOP:
3472 rule_ret = HTTP_RULE_RES_STOP;
3473 goto end;
Christopher Faulet8f1aa772019-07-04 11:27:15 +02003474 case ACT_RET_DONE:
3475 rule_ret = HTTP_RULE_RES_DONE;
3476 goto end;
Christopher Faulet3e964192018-10-24 11:39:23 +02003477 case ACT_RET_YIELD:
3478 s->current_rule = rule;
3479 rule_ret = HTTP_RULE_RES_YIELD;
3480 goto end;
3481 }
3482 break;
3483
Joseph Herlantc42c0e92018-11-25 10:43:27 -08003484 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003485 default:
3486 break;
3487 }
3488 }
3489
3490 end:
3491 /* we reached the end of the rules, nothing to report */
3492 return rule_ret;
3493}
3494
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003495/*
3496 * Manage client-side cookie. It can impact performance by about 2% so it is
3497 * desirable to call it only when needed. This code is quite complex because
3498 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3499 * highly recommended not to touch this part without a good reason !
3500 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003501static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003502{
3503 struct session *sess = s->sess;
3504 struct http_txn *txn = s->txn;
3505 struct htx *htx;
3506 struct http_hdr_ctx ctx;
3507 char *hdr_beg, *hdr_end, *del_from;
3508 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3509 int preserve_hdr;
3510
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003511 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003512 ctx.blk = NULL;
3513 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003514 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003515 del_from = NULL; /* nothing to be deleted */
3516 preserve_hdr = 0; /* assume we may kill the whole header */
3517
3518 /* Now look for cookies. Conforming to RFC2109, we have to support
3519 * attributes whose name begin with a '$', and associate them with
3520 * the right cookie, if we want to delete this cookie.
3521 * So there are 3 cases for each cookie read :
3522 * 1) it's a special attribute, beginning with a '$' : ignore it.
3523 * 2) it's a server id cookie that we *MAY* want to delete : save
3524 * some pointers on it (last semi-colon, beginning of cookie...)
3525 * 3) it's an application cookie : we *MAY* have to delete a previous
3526 * "special" cookie.
3527 * At the end of loop, if a "special" cookie remains, we may have to
3528 * remove it. If no application cookie persists in the header, we
3529 * *MUST* delete it.
3530 *
3531 * Note: RFC2965 is unclear about the processing of spaces around
3532 * the equal sign in the ATTR=VALUE form. A careful inspection of
3533 * the RFC explicitly allows spaces before it, and not within the
3534 * tokens (attrs or values). An inspection of RFC2109 allows that
3535 * too but section 10.1.3 lets one think that spaces may be allowed
3536 * after the equal sign too, resulting in some (rare) buggy
3537 * implementations trying to do that. So let's do what servers do.
3538 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3539 * allowed quoted strings in values, with any possible character
3540 * after a backslash, including control chars and delimitors, which
3541 * causes parsing to become ambiguous. Browsers also allow spaces
3542 * within values even without quotes.
3543 *
3544 * We have to keep multiple pointers in order to support cookie
3545 * removal at the beginning, middle or end of header without
3546 * corrupting the header. All of these headers are valid :
3547 *
3548 * hdr_beg hdr_end
3549 * | |
3550 * v |
3551 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3552 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3553 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3554 * | | | | | | |
3555 * | | | | | | |
3556 * | | | | | | +--> next
3557 * | | | | | +----> val_end
3558 * | | | | +-----------> val_beg
3559 * | | | +--------------> equal
3560 * | | +----------------> att_end
3561 * | +---------------------> att_beg
3562 * +--------------------------> prev
3563 *
3564 */
3565 hdr_beg = ctx.value.ptr;
3566 hdr_end = hdr_beg + ctx.value.len;
3567 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3568 /* Iterate through all cookies on this line */
3569
3570 /* find att_beg */
3571 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003572 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003573 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003574 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003575
3576 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3577 att_beg++;
3578
3579 /* find att_end : this is the first character after the last non
3580 * space before the equal. It may be equal to hdr_end.
3581 */
3582 equal = att_end = att_beg;
3583 while (equal < hdr_end) {
3584 if (*equal == '=' || *equal == ',' || *equal == ';')
3585 break;
3586 if (HTTP_IS_SPHT(*equal++))
3587 continue;
3588 att_end = equal;
3589 }
3590
3591 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3592 * is between <att_beg> and <equal>, both may be identical.
3593 */
3594 /* look for end of cookie if there is an equal sign */
3595 if (equal < hdr_end && *equal == '=') {
3596 /* look for the beginning of the value */
3597 val_beg = equal + 1;
3598 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3599 val_beg++;
3600
3601 /* find the end of the value, respecting quotes */
3602 next = http_find_cookie_value_end(val_beg, hdr_end);
3603
3604 /* make val_end point to the first white space or delimitor after the value */
3605 val_end = next;
3606 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3607 val_end--;
3608 }
3609 else
3610 val_beg = val_end = next = equal;
3611
3612 /* We have nothing to do with attributes beginning with
3613 * '$'. However, they will automatically be removed if a
3614 * header before them is removed, since they're supposed
3615 * to be linked together.
3616 */
3617 if (*att_beg == '$')
3618 continue;
3619
3620 /* Ignore cookies with no equal sign */
3621 if (equal == next) {
3622 /* This is not our cookie, so we must preserve it. But if we already
3623 * scheduled another cookie for removal, we cannot remove the
3624 * complete header, but we can remove the previous block itself.
3625 */
3626 preserve_hdr = 1;
3627 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003628 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003629 val_end += delta;
3630 next += delta;
3631 hdr_end += delta;
3632 prev = del_from;
3633 del_from = NULL;
3634 }
3635 continue;
3636 }
3637
3638 /* if there are spaces around the equal sign, we need to
3639 * strip them otherwise we'll get trouble for cookie captures,
3640 * or even for rewrites. Since this happens extremely rarely,
3641 * it does not hurt performance.
3642 */
3643 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3644 int stripped_before = 0;
3645 int stripped_after = 0;
3646
3647 if (att_end != equal) {
3648 memmove(att_end, equal, hdr_end - equal);
3649 stripped_before = (att_end - equal);
3650 equal += stripped_before;
3651 val_beg += stripped_before;
3652 }
3653
3654 if (val_beg > equal + 1) {
3655 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3656 stripped_after = (equal + 1) - val_beg;
3657 val_beg += stripped_after;
3658 stripped_before += stripped_after;
3659 }
3660
3661 val_end += stripped_before;
3662 next += stripped_before;
3663 hdr_end += stripped_before;
3664 }
3665 /* now everything is as on the diagram above */
3666
3667 /* First, let's see if we want to capture this cookie. We check
3668 * that we don't already have a client side cookie, because we
3669 * can only capture one. Also as an optimisation, we ignore
3670 * cookies shorter than the declared name.
3671 */
3672 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3673 (val_end - att_beg >= sess->fe->capture_namelen) &&
3674 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3675 int log_len = val_end - att_beg;
3676
3677 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3678 ha_alert("HTTP logging : out of memory.\n");
3679 } else {
3680 if (log_len > sess->fe->capture_len)
3681 log_len = sess->fe->capture_len;
3682 memcpy(txn->cli_cookie, att_beg, log_len);
3683 txn->cli_cookie[log_len] = 0;
3684 }
3685 }
3686
3687 /* Persistence cookies in passive, rewrite or insert mode have the
3688 * following form :
3689 *
3690 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3691 *
3692 * For cookies in prefix mode, the form is :
3693 *
3694 * Cookie: NAME=SRV~VALUE
3695 */
3696 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3697 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3698 struct server *srv = s->be->srv;
3699 char *delim;
3700
3701 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3702 * have the server ID between val_beg and delim, and the original cookie between
3703 * delim+1 and val_end. Otherwise, delim==val_end :
3704 *
3705 * hdr_beg
3706 * |
3707 * v
3708 * NAME=SRV; # in all but prefix modes
3709 * NAME=SRV~OPAQUE ; # in prefix mode
3710 * || || | |+-> next
3711 * || || | +--> val_end
3712 * || || +---------> delim
3713 * || |+------------> val_beg
3714 * || +-------------> att_end = equal
3715 * |+-----------------> att_beg
3716 * +------------------> prev
3717 *
3718 */
3719 if (s->be->ck_opts & PR_CK_PFX) {
3720 for (delim = val_beg; delim < val_end; delim++)
3721 if (*delim == COOKIE_DELIM)
3722 break;
3723 }
3724 else {
3725 char *vbar1;
3726 delim = val_end;
3727 /* Now check if the cookie contains a date field, which would
3728 * appear after a vertical bar ('|') just after the server name
3729 * and before the delimiter.
3730 */
3731 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3732 if (vbar1) {
3733 /* OK, so left of the bar is the server's cookie and
3734 * right is the last seen date. It is a base64 encoded
3735 * 30-bit value representing the UNIX date since the
3736 * epoch in 4-second quantities.
3737 */
3738 int val;
3739 delim = vbar1++;
3740 if (val_end - vbar1 >= 5) {
3741 val = b64tos30(vbar1);
3742 if (val > 0)
3743 txn->cookie_last_date = val << 2;
3744 }
3745 /* look for a second vertical bar */
3746 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3747 if (vbar1 && (val_end - vbar1 > 5)) {
3748 val = b64tos30(vbar1 + 1);
3749 if (val > 0)
3750 txn->cookie_first_date = val << 2;
3751 }
3752 }
3753 }
3754
3755 /* if the cookie has an expiration date and the proxy wants to check
3756 * it, then we do that now. We first check if the cookie is too old,
3757 * then only if it has expired. We detect strict overflow because the
3758 * time resolution here is not great (4 seconds). Cookies with dates
3759 * in the future are ignored if their offset is beyond one day. This
3760 * allows an admin to fix timezone issues without expiring everyone
3761 * and at the same time avoids keeping unwanted side effects for too
3762 * long.
3763 */
3764 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3765 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3766 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3767 txn->flags &= ~TX_CK_MASK;
3768 txn->flags |= TX_CK_OLD;
3769 delim = val_beg; // let's pretend we have not found the cookie
3770 txn->cookie_first_date = 0;
3771 txn->cookie_last_date = 0;
3772 }
3773 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3774 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3775 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3776 txn->flags &= ~TX_CK_MASK;
3777 txn->flags |= TX_CK_EXPIRED;
3778 delim = val_beg; // let's pretend we have not found the cookie
3779 txn->cookie_first_date = 0;
3780 txn->cookie_last_date = 0;
3781 }
3782
3783 /* Here, we'll look for the first running server which supports the cookie.
3784 * This allows to share a same cookie between several servers, for example
3785 * to dedicate backup servers to specific servers only.
3786 * However, to prevent clients from sticking to cookie-less backup server
3787 * when they have incidentely learned an empty cookie, we simply ignore
3788 * empty cookies and mark them as invalid.
3789 * The same behaviour is applied when persistence must be ignored.
3790 */
3791 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3792 srv = NULL;
3793
3794 while (srv) {
3795 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3796 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3797 if ((srv->cur_state != SRV_ST_STOPPED) ||
3798 (s->be->options & PR_O_PERSIST) ||
3799 (s->flags & SF_FORCE_PRST)) {
3800 /* we found the server and we can use it */
3801 txn->flags &= ~TX_CK_MASK;
3802 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3803 s->flags |= SF_DIRECT | SF_ASSIGNED;
3804 s->target = &srv->obj_type;
3805 break;
3806 } else {
3807 /* we found a server, but it's down,
3808 * mark it as such and go on in case
3809 * another one is available.
3810 */
3811 txn->flags &= ~TX_CK_MASK;
3812 txn->flags |= TX_CK_DOWN;
3813 }
3814 }
3815 srv = srv->next;
3816 }
3817
3818 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3819 /* no server matched this cookie or we deliberately skipped it */
3820 txn->flags &= ~TX_CK_MASK;
3821 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3822 txn->flags |= TX_CK_UNUSED;
3823 else
3824 txn->flags |= TX_CK_INVALID;
3825 }
3826
3827 /* depending on the cookie mode, we may have to either :
3828 * - delete the complete cookie if we're in insert+indirect mode, so that
3829 * the server never sees it ;
3830 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003831 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003832 * if we're in cookie prefix mode
3833 */
3834 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3835 int delta; /* negative */
3836
3837 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3838 delta = val_beg - (delim + 1);
3839 val_end += delta;
3840 next += delta;
3841 hdr_end += delta;
3842 del_from = NULL;
3843 preserve_hdr = 1; /* we want to keep this cookie */
3844 }
3845 else if (del_from == NULL &&
3846 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3847 del_from = prev;
3848 }
3849 }
3850 else {
3851 /* This is not our cookie, so we must preserve it. But if we already
3852 * scheduled another cookie for removal, we cannot remove the
3853 * complete header, but we can remove the previous block itself.
3854 */
3855 preserve_hdr = 1;
3856
3857 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003858 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003859 if (att_beg >= del_from)
3860 att_beg += delta;
3861 if (att_end >= del_from)
3862 att_end += delta;
3863 val_beg += delta;
3864 val_end += delta;
3865 next += delta;
3866 hdr_end += delta;
3867 prev = del_from;
3868 del_from = NULL;
3869 }
3870 }
3871
3872 /* continue with next cookie on this header line */
3873 att_beg = next;
3874 } /* for each cookie */
3875
3876
3877 /* There are no more cookies on this line.
3878 * We may still have one (or several) marked for deletion at the
3879 * end of the line. We must do this now in two ways :
3880 * - if some cookies must be preserved, we only delete from the
3881 * mark to the end of line ;
3882 * - if nothing needs to be preserved, simply delete the whole header
3883 */
3884 if (del_from) {
3885 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3886 }
3887 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003888 if (hdr_beg != hdr_end)
3889 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003890 else
3891 http_remove_header(htx, &ctx);
3892 }
3893 } /* for each "Cookie header */
3894}
3895
3896/*
3897 * Manage server-side cookies. It can impact performance by about 2% so it is
3898 * desirable to call it only when needed. This function is also used when we
3899 * just need to know if there is a cookie (eg: for check-cache).
3900 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003901static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003902{
3903 struct session *sess = s->sess;
3904 struct http_txn *txn = s->txn;
3905 struct htx *htx;
3906 struct http_hdr_ctx ctx;
3907 struct server *srv;
3908 char *hdr_beg, *hdr_end;
3909 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003910 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003911
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003912 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003913
3914 ctx.blk = NULL;
3915 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003916 int is_first = 1;
3917
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003918 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3919 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3920 break;
3921 is_cookie2 = 1;
3922 }
3923
3924 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3925 * <prev> points to the colon.
3926 */
3927 txn->flags |= TX_SCK_PRESENT;
3928
3929 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3930 * check-cache is enabled) and we are not interested in checking
3931 * them. Warning, the cookie capture is declared in the frontend.
3932 */
3933 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3934 break;
3935
3936 /* OK so now we know we have to process this response cookie.
3937 * The format of the Set-Cookie header is slightly different
3938 * from the format of the Cookie header in that it does not
3939 * support the comma as a cookie delimiter (thus the header
3940 * cannot be folded) because the Expires attribute described in
3941 * the original Netscape's spec may contain an unquoted date
3942 * with a comma inside. We have to live with this because
3943 * many browsers don't support Max-Age and some browsers don't
3944 * support quoted strings. However the Set-Cookie2 header is
3945 * clean.
3946 *
3947 * We have to keep multiple pointers in order to support cookie
3948 * removal at the beginning, middle or end of header without
3949 * corrupting the header (in case of set-cookie2). A special
3950 * pointer, <scav> points to the beginning of the set-cookie-av
3951 * fields after the first semi-colon. The <next> pointer points
3952 * either to the end of line (set-cookie) or next unquoted comma
3953 * (set-cookie2). All of these headers are valid :
3954 *
3955 * hdr_beg hdr_end
3956 * | |
3957 * v |
3958 * NAME1 = VALUE 1 ; Secure; Path="/" |
3959 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3960 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3961 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3962 * | | | | | | | |
3963 * | | | | | | | +-> next
3964 * | | | | | | +------------> scav
3965 * | | | | | +--------------> val_end
3966 * | | | | +--------------------> val_beg
3967 * | | | +----------------------> equal
3968 * | | +------------------------> att_end
3969 * | +----------------------------> att_beg
3970 * +------------------------------> prev
3971 * -------------------------------> hdr_beg
3972 */
3973 hdr_beg = ctx.value.ptr;
3974 hdr_end = hdr_beg + ctx.value.len;
3975 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3976
3977 /* Iterate through all cookies on this line */
3978
3979 /* find att_beg */
3980 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003981 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003982 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003983 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003984
3985 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3986 att_beg++;
3987
3988 /* find att_end : this is the first character after the last non
3989 * space before the equal. It may be equal to hdr_end.
3990 */
3991 equal = att_end = att_beg;
3992
3993 while (equal < hdr_end) {
3994 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3995 break;
3996 if (HTTP_IS_SPHT(*equal++))
3997 continue;
3998 att_end = equal;
3999 }
4000
4001 /* here, <equal> points to '=', a delimitor or the end. <att_end>
4002 * is between <att_beg> and <equal>, both may be identical.
4003 */
4004
4005 /* look for end of cookie if there is an equal sign */
4006 if (equal < hdr_end && *equal == '=') {
4007 /* look for the beginning of the value */
4008 val_beg = equal + 1;
4009 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
4010 val_beg++;
4011
4012 /* find the end of the value, respecting quotes */
4013 next = http_find_cookie_value_end(val_beg, hdr_end);
4014
4015 /* make val_end point to the first white space or delimitor after the value */
4016 val_end = next;
4017 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
4018 val_end--;
4019 }
4020 else {
4021 /* <equal> points to next comma, semi-colon or EOL */
4022 val_beg = val_end = next = equal;
4023 }
4024
4025 if (next < hdr_end) {
4026 /* Set-Cookie2 supports multiple cookies, and <next> points to
4027 * a colon or semi-colon before the end. So skip all attr-value
4028 * pairs and look for the next comma. For Set-Cookie, since
4029 * commas are permitted in values, skip to the end.
4030 */
4031 if (is_cookie2)
4032 next = http_find_hdr_value_end(next, hdr_end);
4033 else
4034 next = hdr_end;
4035 }
4036
4037 /* Now everything is as on the diagram above */
4038
4039 /* Ignore cookies with no equal sign */
4040 if (equal == val_end)
4041 continue;
4042
4043 /* If there are spaces around the equal sign, we need to
4044 * strip them otherwise we'll get trouble for cookie captures,
4045 * or even for rewrites. Since this happens extremely rarely,
4046 * it does not hurt performance.
4047 */
4048 if (unlikely(att_end != equal || val_beg > equal + 1)) {
4049 int stripped_before = 0;
4050 int stripped_after = 0;
4051
4052 if (att_end != equal) {
4053 memmove(att_end, equal, hdr_end - equal);
4054 stripped_before = (att_end - equal);
4055 equal += stripped_before;
4056 val_beg += stripped_before;
4057 }
4058
4059 if (val_beg > equal + 1) {
4060 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
4061 stripped_after = (equal + 1) - val_beg;
4062 val_beg += stripped_after;
4063 stripped_before += stripped_after;
4064 }
4065
4066 val_end += stripped_before;
4067 next += stripped_before;
4068 hdr_end += stripped_before;
4069
Christopher Faulet3e2638e2019-06-18 09:49:16 +02004070 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004071 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004072 }
4073
4074 /* First, let's see if we want to capture this cookie. We check
4075 * that we don't already have a server side cookie, because we
4076 * can only capture one. Also as an optimisation, we ignore
4077 * cookies shorter than the declared name.
4078 */
4079 if (sess->fe->capture_name != NULL &&
4080 txn->srv_cookie == NULL &&
4081 (val_end - att_beg >= sess->fe->capture_namelen) &&
4082 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
4083 int log_len = val_end - att_beg;
4084 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
4085 ha_alert("HTTP logging : out of memory.\n");
4086 }
4087 else {
4088 if (log_len > sess->fe->capture_len)
4089 log_len = sess->fe->capture_len;
4090 memcpy(txn->srv_cookie, att_beg, log_len);
4091 txn->srv_cookie[log_len] = 0;
4092 }
4093 }
4094
4095 srv = objt_server(s->target);
4096 /* now check if we need to process it for persistence */
4097 if (!(s->flags & SF_IGNORE_PRST) &&
4098 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
4099 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
4100 /* assume passive cookie by default */
4101 txn->flags &= ~TX_SCK_MASK;
4102 txn->flags |= TX_SCK_FOUND;
4103
4104 /* If the cookie is in insert mode on a known server, we'll delete
4105 * this occurrence because we'll insert another one later.
4106 * We'll delete it too if the "indirect" option is set and we're in
4107 * a direct access.
4108 */
4109 if (s->be->ck_opts & PR_CK_PSV) {
4110 /* The "preserve" flag was set, we don't want to touch the
4111 * server's cookie.
4112 */
4113 }
4114 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
4115 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
4116 /* this cookie must be deleted */
4117 if (prev == hdr_beg && next == hdr_end) {
4118 /* whole header */
4119 http_remove_header(htx, &ctx);
4120 /* note: while both invalid now, <next> and <hdr_end>
4121 * are still equal, so the for() will stop as expected.
4122 */
4123 } else {
4124 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004125 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004126 next = prev;
4127 hdr_end += delta;
4128 }
4129 txn->flags &= ~TX_SCK_MASK;
4130 txn->flags |= TX_SCK_DELETED;
4131 /* and go on with next cookie */
4132 }
4133 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
4134 /* replace bytes val_beg->val_end with the cookie name associated
4135 * with this server since we know it.
4136 */
4137 int sliding, delta;
4138
4139 ctx.value = ist2(val_beg, val_end - val_beg);
4140 ctx.lws_before = ctx.lws_after = 0;
4141 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
4142 delta = srv->cklen - (val_end - val_beg);
4143 sliding = (ctx.value.ptr - val_beg);
4144 hdr_beg += sliding;
4145 val_beg += sliding;
4146 next += sliding + delta;
4147 hdr_end += sliding + delta;
4148
4149 txn->flags &= ~TX_SCK_MASK;
4150 txn->flags |= TX_SCK_REPLACED;
4151 }
4152 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
4153 /* insert the cookie name associated with this server
4154 * before existing cookie, and insert a delimiter between them..
4155 */
4156 int sliding, delta;
4157 ctx.value = ist2(val_beg, 0);
4158 ctx.lws_before = ctx.lws_after = 0;
4159 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
4160 delta = srv->cklen + 1;
4161 sliding = (ctx.value.ptr - val_beg);
4162 hdr_beg += sliding;
4163 val_beg += sliding;
4164 next += sliding + delta;
4165 hdr_end += sliding + delta;
4166
4167 val_beg[srv->cklen] = COOKIE_DELIM;
4168 txn->flags &= ~TX_SCK_MASK;
4169 txn->flags |= TX_SCK_REPLACED;
4170 }
4171 }
4172 /* that's done for this cookie, check the next one on the same
4173 * line when next != hdr_end (only if is_cookie2).
4174 */
4175 }
4176 }
4177}
4178
Christopher Faulet25a02f62018-10-24 12:00:25 +02004179/*
4180 * Parses the Cache-Control and Pragma request header fields to determine if
4181 * the request may be served from the cache and/or if it is cacheable. Updates
4182 * s->txn->flags.
4183 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004184void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004185{
4186 struct http_txn *txn = s->txn;
4187 struct htx *htx;
4188 int32_t pos;
4189 int pragma_found, cc_found, i;
4190
4191 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
4192 return; /* nothing more to do here */
4193
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004194 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02004195 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02004196 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004197 struct htx_blk *blk = htx_get_blk(htx, pos);
4198 enum htx_blk_type type = htx_get_blk_type(blk);
4199 struct ist n, v;
4200
4201 if (type == HTX_BLK_EOH)
4202 break;
4203 if (type != HTX_BLK_HDR)
4204 continue;
4205
4206 n = htx_get_blk_name(htx, blk);
4207 v = htx_get_blk_value(htx, blk);
4208
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004209 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004210 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4211 pragma_found = 1;
4212 continue;
4213 }
4214 }
4215
4216 /* Don't use the cache and don't try to store if we found the
4217 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004218 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004219 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4220 txn->flags |= TX_CACHE_IGNORE;
4221 continue;
4222 }
4223
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004224 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004225 continue;
4226
4227 /* OK, right now we know we have a cache-control header */
4228 cc_found = 1;
4229 if (!v.len) /* no info */
4230 continue;
4231
4232 i = 0;
4233 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4234 !isspace((unsigned char)*(v.ptr+i)))
4235 i++;
4236
4237 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
4238 * values after max-age, max-stale nor min-fresh, we simply don't
4239 * use the cache when they're specified.
4240 */
4241 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
4242 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4243 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
4244 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
4245 txn->flags |= TX_CACHE_IGNORE;
4246 continue;
4247 }
4248
4249 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
4250 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4251 continue;
4252 }
4253 }
4254
4255 /* RFC7234#5.4:
4256 * When the Cache-Control header field is also present and
4257 * understood in a request, Pragma is ignored.
4258 * When the Cache-Control header field is not present in a
4259 * request, caches MUST consider the no-cache request
4260 * pragma-directive as having the same effect as if
4261 * "Cache-Control: no-cache" were present.
4262 */
4263 if (!cc_found && pragma_found)
4264 txn->flags |= TX_CACHE_IGNORE;
4265}
4266
4267/*
4268 * Check if response is cacheable or not. Updates s->txn->flags.
4269 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004270void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004271{
4272 struct http_txn *txn = s->txn;
4273 struct htx *htx;
4274 int32_t pos;
4275 int i;
4276
4277 if (txn->status < 200) {
4278 /* do not try to cache interim responses! */
4279 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4280 return;
4281 }
4282
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004283 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004284 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004285 struct htx_blk *blk = htx_get_blk(htx, pos);
4286 enum htx_blk_type type = htx_get_blk_type(blk);
4287 struct ist n, v;
4288
4289 if (type == HTX_BLK_EOH)
4290 break;
4291 if (type != HTX_BLK_HDR)
4292 continue;
4293
4294 n = htx_get_blk_name(htx, blk);
4295 v = htx_get_blk_value(htx, blk);
4296
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004297 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004298 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4299 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4300 return;
4301 }
4302 }
4303
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004304 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004305 continue;
4306
4307 /* OK, right now we know we have a cache-control header */
4308 if (!v.len) /* no info */
4309 continue;
4310
4311 i = 0;
4312 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4313 !isspace((unsigned char)*(v.ptr+i)))
4314 i++;
4315
4316 /* we have a complete value between v.ptr and (v.ptr+i) */
4317 if (i < v.len && *(v.ptr + i) == '=') {
4318 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
4319 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
4320 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4321 continue;
4322 }
4323
4324 /* we have something of the form no-cache="set-cookie" */
4325 if ((v.len >= 21) &&
4326 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
4327 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
4328 txn->flags &= ~TX_CACHE_COOK;
4329 continue;
4330 }
4331
4332 /* OK, so we know that either p2 points to the end of string or to a comma */
4333 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
4334 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4335 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
4336 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4337 return;
4338 }
4339
4340 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4341 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4342 continue;
4343 }
4344 }
4345}
4346
Christopher Faulet377c5a52018-10-24 21:21:30 +02004347/*
4348 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4349 * for the current backend.
4350 *
4351 * It is assumed that the request is either a HEAD, GET, or POST and that the
4352 * uri_auth field is valid.
4353 *
4354 * Returns 1 if stats should be provided, otherwise 0.
4355 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004356static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004357{
4358 struct uri_auth *uri_auth = backend->uri_auth;
4359 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004360 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004361 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004362
4363 if (!uri_auth)
4364 return 0;
4365
4366 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4367 return 0;
4368
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004369 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004370 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004371 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01004372 if (*uri_auth->uri_prefix == '/')
4373 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004374
4375 /* check URI size */
4376 if (uri_auth->uri_len > uri.len)
4377 return 0;
4378
4379 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4380 return 0;
4381
4382 return 1;
4383}
4384
4385/* This function prepares an applet to handle the stats. It can deal with the
4386 * "100-continue" expectation, check that admin rules are met for POST requests,
4387 * and program a response message if something was unexpected. It cannot fail
4388 * and always relies on the stats applet to complete the job. It does not touch
4389 * analysers nor counters, which are left to the caller. It does not touch
4390 * s->target which is supposed to already point to the stats applet. The caller
4391 * is expected to have already assigned an appctx to the stream.
4392 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004393static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004394{
4395 struct stats_admin_rule *stats_admin_rule;
4396 struct stream_interface *si = &s->si[1];
4397 struct session *sess = s->sess;
4398 struct http_txn *txn = s->txn;
4399 struct http_msg *msg = &txn->req;
4400 struct uri_auth *uri_auth = s->be->uri_auth;
4401 const char *h, *lookup, *end;
4402 struct appctx *appctx;
4403 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004404 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004405
4406 appctx = si_appctx(si);
4407 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4408 appctx->st1 = appctx->st2 = 0;
4409 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004410 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004411 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4412 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4413 appctx->ctx.stats.flags |= STAT_CHUNKED;
4414
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004415 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004416 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004417 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4418 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004419
4420 for (h = lookup; h <= end - 3; h++) {
4421 if (memcmp(h, ";up", 3) == 0) {
4422 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4423 break;
4424 }
4425 }
4426
4427 if (uri_auth->refresh) {
4428 for (h = lookup; h <= end - 10; h++) {
4429 if (memcmp(h, ";norefresh", 10) == 0) {
4430 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4431 break;
4432 }
4433 }
4434 }
4435
4436 for (h = lookup; h <= end - 4; h++) {
4437 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004438 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004439 break;
4440 }
4441 }
4442
4443 for (h = lookup; h <= end - 6; h++) {
4444 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004445 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004446 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4447 break;
4448 }
4449 }
4450
Christopher Faulet6338a082019-09-09 15:50:54 +02004451 for (h = lookup; h <= end - 5; h++) {
4452 if (memcmp(h, ";json", 5) == 0) {
4453 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4454 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4455 break;
4456 }
4457 }
4458
4459 for (h = lookup; h <= end - 12; h++) {
4460 if (memcmp(h, ";json-schema", 12) == 0) {
4461 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4462 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4463 break;
4464 }
4465 }
4466
Christopher Faulet377c5a52018-10-24 21:21:30 +02004467 for (h = lookup; h <= end - 8; h++) {
4468 if (memcmp(h, ";st=", 4) == 0) {
4469 int i;
4470 h += 4;
4471 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4472 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4473 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4474 appctx->ctx.stats.st_code = i;
4475 break;
4476 }
4477 }
4478 break;
4479 }
4480 }
4481
4482 appctx->ctx.stats.scope_str = 0;
4483 appctx->ctx.stats.scope_len = 0;
4484 for (h = lookup; h <= end - 8; h++) {
4485 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4486 int itx = 0;
4487 const char *h2;
4488 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4489 const char *err;
4490
4491 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4492 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004493 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4494 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004495 if (*h == ';' || *h == '&' || *h == ' ')
4496 break;
4497 itx++;
4498 h++;
4499 }
4500
4501 if (itx > STAT_SCOPE_TXT_MAXLEN)
4502 itx = STAT_SCOPE_TXT_MAXLEN;
4503 appctx->ctx.stats.scope_len = itx;
4504
4505 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4506 memcpy(scope_txt, h2, itx);
4507 scope_txt[itx] = '\0';
4508 err = invalid_char(scope_txt);
4509 if (err) {
4510 /* bad char in search text => clear scope */
4511 appctx->ctx.stats.scope_str = 0;
4512 appctx->ctx.stats.scope_len = 0;
4513 }
4514 break;
4515 }
4516 }
4517
4518 /* now check whether we have some admin rules for this request */
4519 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4520 int ret = 1;
4521
4522 if (stats_admin_rule->cond) {
4523 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4524 ret = acl_pass(ret);
4525 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4526 ret = !ret;
4527 }
4528
4529 if (ret) {
4530 /* no rule, or the rule matches */
4531 appctx->ctx.stats.flags |= STAT_ADMIN;
4532 break;
4533 }
4534 }
4535
Christopher Faulet5d45e382019-02-27 15:15:23 +01004536 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4537 appctx->st0 = STAT_HTTP_HEAD;
4538 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004539 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004540 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004541 if (msg->msg_state < HTTP_MSG_DATA)
4542 req->analysers |= AN_REQ_HTTP_BODY;
4543 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004544 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004545 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004546 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4547 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4548 appctx->st0 = STAT_HTTP_LAST;
4549 }
4550 }
4551 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004552 /* Unsupported method */
4553 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4554 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4555 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004556 }
4557
4558 s->task->nice = -32; /* small boost for HTTP statistics */
4559 return 1;
4560}
4561
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004562void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004563{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004564 struct channel *req = &s->req;
4565 struct channel *res = &s->res;
4566 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004567 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004568 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004569 struct ist path, location;
4570 unsigned int flags;
4571 size_t data;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004572
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004573 /*
4574 * Create the location
4575 */
4576 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004577
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004578 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004579 /* special prefix "/" means don't change URL */
4580 srv = __objt_server(s->target);
4581 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4582 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4583 return;
4584 }
4585
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004586 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004587 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004588 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004589 path = http_get_path(htx_sl_req_uri(sl));
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004590 if (!path.ptr)
4591 return;
4592
4593 if (!chunk_memcat(&trash, path.ptr, path.len))
4594 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004595 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004596
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004597 /*
4598 * Create the 302 respone
4599 */
4600 htx = htx_from_buf(&res->buf);
4601 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4602 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4603 ist("HTTP/1.1"), ist("302"), ist("Found"));
4604 if (!sl)
4605 goto fail;
4606 sl->info.res.status = 302;
4607 s->txn->status = 302;
4608
4609 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4610 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4611 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4612 !htx_add_header(htx, ist("Location"), location))
4613 goto fail;
4614
4615 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4616 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004617
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004618 /*
4619 * Send the message
4620 */
4621 data = htx->data - co_data(res);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004622 c_adv(res, data);
4623 res->total += data;
4624
4625 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004626 si_shutr(si);
4627 si_shutw(si);
4628 si->err_type = SI_ET_NONE;
4629 si->state = SI_ST_CLO;
4630
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004631 channel_auto_read(req);
4632 channel_abort(req);
4633 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004634 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004635 channel_auto_read(res);
4636 channel_auto_close(res);
4637
4638 if (!(s->flags & SF_ERR_MASK))
4639 s->flags |= SF_ERR_LOCAL;
4640 if (!(s->flags & SF_FINST_MASK))
4641 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004642
4643 /* FIXME: we should increase a counter of redirects per server and per backend. */
4644 srv_inc_sess_ctr(srv);
4645 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004646 return;
4647
4648 fail:
4649 /* If an error occurred, remove the incomplete HTTP response from the
4650 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004651 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004652}
4653
Christopher Fauletf2824e62018-10-01 12:12:37 +02004654/* This function terminates the request because it was completly analyzed or
4655 * because an error was triggered during the body forwarding.
4656 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004657static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004658{
4659 struct channel *chn = &s->req;
4660 struct http_txn *txn = s->txn;
4661
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004662 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004663
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004664 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4665 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004666 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004667 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004668 goto end;
4669 }
4670
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004671 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4672 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004673 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004674 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004675
4676 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004677 /* No need to read anymore, the request was completely parsed.
4678 * We can shut the read side unless we want to abort_on_close,
4679 * or we have a POST request. The issue with POST requests is
4680 * that some browsers still send a CRLF after the request, and
4681 * this CRLF must be read so that it does not remain in the kernel
4682 * buffers, otherwise a close could cause an RST on some systems
4683 * (eg: Linux).
4684 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004685 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004686 channel_dont_read(chn);
4687
4688 /* if the server closes the connection, we want to immediately react
4689 * and close the socket to save packets and syscalls.
4690 */
4691 s->si[1].flags |= SI_FL_NOHALF;
4692
4693 /* In any case we've finished parsing the request so we must
4694 * disable Nagle when sending data because 1) we're not going
4695 * to shut this side, and 2) the server is waiting for us to
4696 * send pending data.
4697 */
4698 chn->flags |= CF_NEVER_WAIT;
4699
Christopher Fauletd01ce402019-01-02 17:44:13 +01004700 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4701 /* The server has not finished to respond, so we
4702 * don't want to move in order not to upset it.
4703 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004704 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004705 return;
4706 }
4707
Christopher Fauletf2824e62018-10-01 12:12:37 +02004708 /* When we get here, it means that both the request and the
4709 * response have finished receiving. Depending on the connection
4710 * mode, we'll have to wait for the last bytes to leave in either
4711 * direction, and sometimes for a close to be effective.
4712 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004713 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004714 /* Tunnel mode will not have any analyser so it needs to
4715 * poll for reads.
4716 */
4717 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004718 if (b_data(&chn->buf)) {
4719 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004720 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004721 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004722 txn->req.msg_state = HTTP_MSG_TUNNEL;
4723 }
4724 else {
4725 /* we're not expecting any new data to come for this
4726 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004727 *
4728 * However, there is an exception if the response
4729 * length is undefined. In this case, we need to wait
4730 * the close from the server. The response will be
4731 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004732 */
4733 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4734 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004735 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004736
4737 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4738 channel_shutr_now(chn);
4739 channel_shutw_now(chn);
4740 }
4741 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004742 goto check_channel_flags;
4743 }
4744
4745 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4746 http_msg_closing:
4747 /* nothing else to forward, just waiting for the output buffer
4748 * to be empty and for the shutw_now to take effect.
4749 */
4750 if (channel_is_empty(chn)) {
4751 txn->req.msg_state = HTTP_MSG_CLOSED;
4752 goto http_msg_closed;
4753 }
4754 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004755 txn->req.msg_state = HTTP_MSG_ERROR;
4756 goto end;
4757 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004758 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004759 return;
4760 }
4761
4762 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4763 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004764 /* if we don't know whether the server will close, we need to hard close */
4765 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4766 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004767 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004768 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004769 channel_dont_read(chn);
4770 goto end;
4771 }
4772
4773 check_channel_flags:
4774 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4775 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4776 /* if we've just closed an output, let's switch */
4777 txn->req.msg_state = HTTP_MSG_CLOSING;
4778 goto http_msg_closing;
4779 }
4780
4781 end:
4782 chn->analysers &= AN_REQ_FLT_END;
4783 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4784 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4785 channel_auto_close(chn);
4786 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004787 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004788}
4789
4790
4791/* This function terminates the response because it was completly analyzed or
4792 * because an error was triggered during the body forwarding.
4793 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004794static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004795{
4796 struct channel *chn = &s->res;
4797 struct http_txn *txn = s->txn;
4798
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004799 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004800
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004801 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4802 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004803 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004804 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004805 goto end;
4806 }
4807
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004808 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4809 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004810 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004811 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004812
4813 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4814 /* In theory, we don't need to read anymore, but we must
4815 * still monitor the server connection for a possible close
4816 * while the request is being uploaded, so we don't disable
4817 * reading.
4818 */
4819 /* channel_dont_read(chn); */
4820
4821 if (txn->req.msg_state < HTTP_MSG_DONE) {
4822 /* The client seems to still be sending data, probably
4823 * because we got an error response during an upload.
4824 * We have the choice of either breaking the connection
4825 * or letting it pass through. Let's do the later.
4826 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004827 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004828 return;
4829 }
4830
4831 /* When we get here, it means that both the request and the
4832 * response have finished receiving. Depending on the connection
4833 * mode, we'll have to wait for the last bytes to leave in either
4834 * direction, and sometimes for a close to be effective.
4835 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004836 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004837 channel_auto_read(chn);
4838 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004839 if (b_data(&chn->buf)) {
4840 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004841 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004842 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004843 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4844 }
4845 else {
4846 /* we're not expecting any new data to come for this
4847 * transaction, so we can close it.
4848 */
4849 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4850 channel_shutr_now(chn);
4851 channel_shutw_now(chn);
4852 }
4853 }
4854 goto check_channel_flags;
4855 }
4856
4857 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4858 http_msg_closing:
4859 /* nothing else to forward, just waiting for the output buffer
4860 * to be empty and for the shutw_now to take effect.
4861 */
4862 if (channel_is_empty(chn)) {
4863 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4864 goto http_msg_closed;
4865 }
4866 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004867 txn->rsp.msg_state = HTTP_MSG_ERROR;
Olivier Houcharda798bf52019-03-08 18:52:00 +01004868 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004869 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004870 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004871 goto end;
4872 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004873 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004874 return;
4875 }
4876
4877 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4878 http_msg_closed:
4879 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004880 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004881 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004882 goto end;
4883 }
4884
4885 check_channel_flags:
4886 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4887 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4888 /* if we've just closed an output, let's switch */
4889 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4890 goto http_msg_closing;
4891 }
4892
4893 end:
4894 chn->analysers &= AN_RES_FLT_END;
4895 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4896 chn->analysers |= AN_RES_FLT_XFER_DATA;
4897 channel_auto_close(chn);
4898 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004899 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004900}
4901
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004902void http_server_error(struct stream *s, struct stream_interface *si, int err,
4903 int finst, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004904{
4905 channel_auto_read(si_oc(si));
4906 channel_abort(si_oc(si));
4907 channel_auto_close(si_oc(si));
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004908 channel_htx_erase(si_oc(si), htxbuf(&(si_oc(si))->buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02004909 channel_auto_close(si_ic(si));
4910 channel_auto_read(si_ic(si));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004911
4912 /* <msg> is an HTX structure. So we copy it in the response's
4913 * channel */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004914 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004915 struct channel *chn = si_ic(si);
4916 struct htx *htx;
4917
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004918 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004919 chn->buf.data = msg->data;
4920 memcpy(chn->buf.area, msg->area, msg->data);
4921 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02004922 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02004923 c_adv(chn, htx->data);
4924 chn->total += htx->data;
4925 }
4926 if (!(s->flags & SF_ERR_MASK))
4927 s->flags |= err;
4928 if (!(s->flags & SF_FINST_MASK))
4929 s->flags |= finst;
4930}
4931
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004932void http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004933{
4934 channel_auto_read(&s->req);
4935 channel_abort(&s->req);
4936 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004937 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
4938 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02004939
4940 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004941
4942 /* <msg> is an HTX structure. So we copy it in the response's
4943 * channel */
4944 /* FIXME: It is a problem for now if there is some outgoing data */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004945 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004946 struct channel *chn = &s->res;
4947 struct htx *htx;
4948
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004949 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004950 chn->buf.data = msg->data;
4951 memcpy(chn->buf.area, msg->area, msg->data);
4952 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02004953 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02004954 c_adv(chn, htx->data);
4955 chn->total += htx->data;
4956 }
4957
4958 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
4959 channel_auto_read(&s->res);
4960 channel_auto_close(&s->res);
4961 channel_shutr_now(&s->res);
4962}
4963
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004964struct buffer *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004965{
4966 const int msgnum = http_get_status_idx(s->txn->status);
4967
4968 if (s->be->errmsg[msgnum].area)
4969 return &s->be->errmsg[msgnum];
4970 else if (strm_fe(s)->errmsg[msgnum].area)
4971 return &strm_fe(s)->errmsg[msgnum];
4972 else
Christopher Fauletf7346382019-07-17 22:02:08 +02004973 return &http_err_chunks[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004974}
4975
Christopher Faulet304cc402019-07-15 15:46:28 +02004976/* Return the error message corresponding to si->err_type. It is assumed
4977 * that the server side is closed. Note that err_type is actually a
4978 * bitmask, where almost only aborts may be cumulated with other
4979 * values. We consider that aborted operations are more important
4980 * than timeouts or errors due to the fact that nobody else in the
4981 * logs might explain incomplete retries. All others should avoid
4982 * being cumulated. It should normally not be possible to have multiple
4983 * aborts at once, but just in case, the first one in sequence is reported.
4984 * Note that connection errors appearing on the second request of a keep-alive
4985 * connection are not reported since this allows the client to retry.
4986 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004987void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004988{
4989 int err_type = si->err_type;
4990
4991 /* set s->txn->status for http_error_message(s) */
4992 s->txn->status = 503;
4993
4994 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004995 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4996 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004997 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004998 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4999 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5000 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005001 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005002 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
5003 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005004 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005005 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
5006 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005007 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005008 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
5009 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5010 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005011 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005012 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
5013 (s->flags & SF_SRV_REUSED) ? NULL :
5014 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005015 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005016 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
5017 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5018 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005019 else { /* SI_ET_CONN_OTHER and others */
5020 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005021 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
5022 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005023 }
5024}
5025
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005026
Christopher Faulet4a28a532019-03-01 11:19:40 +01005027/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
5028 * on success and -1 on error.
5029 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005030static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01005031{
5032 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
5033 * then we must send an HTTP/1.1 100 Continue intermediate response.
5034 */
5035 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
5036 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
5037 struct ist hdr = { .ptr = "Expect", .len = 6 };
5038 struct http_hdr_ctx ctx;
5039
5040 ctx.blk = NULL;
5041 /* Expect is allowed in 1.1, look for it */
5042 if (http_find_header(htx, hdr, &ctx, 0) &&
5043 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005044 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01005045 return -1;
5046 http_remove_header(htx, &ctx);
5047 }
5048 }
5049 return 0;
5050}
5051
Christopher Faulet23a3c792018-11-28 10:01:23 +01005052/* Send a 100-Continue response to the client. It returns 0 on success and -1
5053 * on error. The response channel is updated accordingly.
5054 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005055static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01005056{
5057 struct channel *res = &s->res;
5058 struct htx *htx = htx_from_buf(&res->buf);
5059 struct htx_sl *sl;
5060 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
5061 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
5062 size_t data;
5063
5064 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5065 ist("HTTP/1.1"), ist("100"), ist("Continue"));
5066 if (!sl)
5067 goto fail;
5068 sl->info.res.status = 100;
5069
Christopher Faulet1d5ec092019-06-26 14:23:54 +02005070 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01005071 goto fail;
5072
5073 data = htx->data - co_data(res);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005074 c_adv(res, data);
5075 res->total += data;
5076 return 0;
5077
5078 fail:
5079 /* If an error occurred, remove the incomplete HTTP response from the
5080 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005081 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005082 return -1;
5083}
5084
Christopher Faulet12c51e22018-11-28 15:59:42 +01005085
5086/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
5087 * ont whether we use a proxy or not. It returns 0 on success and -1 on
5088 * error. The response channel is updated accordingly.
5089 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005090static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
Christopher Faulet12c51e22018-11-28 15:59:42 +01005091{
5092 struct channel *res = &s->res;
5093 struct htx *htx = htx_from_buf(&res->buf);
5094 struct htx_sl *sl;
5095 struct ist code, body;
5096 int status;
5097 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5098 size_t data;
5099
5100 if (!(s->txn->flags & TX_USE_PX_CONN)) {
5101 status = 401;
5102 code = ist("401");
5103 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
5104 "You need a valid user and password to access this content.\n"
5105 "</body></html>\n");
5106 }
5107 else {
5108 status = 407;
5109 code = ist("407");
5110 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
5111 "You need a valid user and password to access this content.\n"
5112 "</body></html>\n");
5113 }
5114
5115 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5116 ist("HTTP/1.1"), code, ist("Unauthorized"));
5117 if (!sl)
5118 goto fail;
5119 sl->info.res.status = status;
5120 s->txn->status = status;
5121
5122 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
5123 goto fail;
5124
Willy Tarreaub5ba2b02019-06-11 16:08:25 +02005125 if (!htx_add_header(htx, ist("Content-length"), ist("112")) ||
5126 !htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Faulet12c51e22018-11-28 15:59:42 +01005127 !htx_add_header(htx, ist("Connection"), ist("close")) ||
Jérôme Magnin86cef232018-12-28 14:49:08 +01005128 !htx_add_header(htx, ist("Content-Type"), ist("text/html")))
5129 goto fail;
5130 if (status == 401 && !htx_add_header(htx, ist("WWW-Authenticate"), ist2(trash.area, trash.data)))
5131 goto fail;
5132 if (status == 407 && !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005133 goto fail;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02005134 if (!htx_add_endof(htx, HTX_BLK_EOH))
5135 goto fail;
5136
5137 while (body.len) {
5138 size_t sent = htx_add_data(htx, body);
5139 if (!sent)
5140 goto fail;
5141 body.ptr += sent;
5142 body.len -= sent;
5143 }
5144
5145 if (!htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005146 goto fail;
5147
Christopher Faulet06511812019-10-16 09:38:27 +02005148 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet12c51e22018-11-28 15:59:42 +01005149 data = htx->data - co_data(res);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005150 c_adv(res, data);
5151 res->total += data;
5152
5153 channel_auto_read(&s->req);
5154 channel_abort(&s->req);
5155 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005156 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet12c51e22018-11-28 15:59:42 +01005157
5158 res->wex = tick_add_ifset(now_ms, res->wto);
5159 channel_auto_read(res);
5160 channel_auto_close(res);
5161 channel_shutr_now(res);
5162 return 0;
5163
5164 fail:
5165 /* If an error occurred, remove the incomplete HTTP response from the
5166 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005167 channel_htx_truncate(res, htx);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005168 return -1;
5169}
5170
Christopher Faulet0f226952018-10-22 09:29:56 +02005171/*
5172 * Capture headers from message <htx> according to header list <cap_hdr>, and
5173 * fill the <cap> pointers appropriately.
5174 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005175static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02005176{
5177 struct cap_hdr *h;
5178 int32_t pos;
5179
Christopher Fauleta3f15502019-05-13 15:27:23 +02005180 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02005181 struct htx_blk *blk = htx_get_blk(htx, pos);
5182 enum htx_blk_type type = htx_get_blk_type(blk);
5183 struct ist n, v;
5184
5185 if (type == HTX_BLK_EOH)
5186 break;
5187 if (type != HTX_BLK_HDR)
5188 continue;
5189
5190 n = htx_get_blk_name(htx, blk);
5191
5192 for (h = cap_hdr; h; h = h->next) {
5193 if (h->namelen && (h->namelen == n.len) &&
5194 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
5195 if (cap[h->index] == NULL)
5196 cap[h->index] =
5197 pool_alloc(h->pool);
5198
5199 if (cap[h->index] == NULL) {
5200 ha_alert("HTTP capture : out of memory.\n");
5201 break;
5202 }
5203
5204 v = htx_get_blk_value(htx, blk);
5205 if (v.len > h->len)
5206 v.len = h->len;
5207
5208 memcpy(cap[h->index], v.ptr, v.len);
5209 cap[h->index][v.len]=0;
5210 }
5211 }
5212 }
5213}
5214
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005215/* Delete a value in a header between delimiters <from> and <next>. The header
5216 * itself is delimited by <start> and <end> pointers. The number of characters
5217 * displaced is returned, and the pointer to the first delimiter is updated if
5218 * required. The function tries as much as possible to respect the following
5219 * principles :
5220 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5221 * in which case <next> is simply removed
5222 * - set exactly one space character after the new first delimiter, unless there
5223 * are not enough characters in the block being moved to do so.
5224 * - remove unneeded spaces before the previous delimiter and after the new
5225 * one.
5226 *
5227 * It is the caller's responsibility to ensure that :
5228 * - <from> points to a valid delimiter or <start> ;
5229 * - <next> points to a valid delimiter or <end> ;
5230 * - there are non-space chars before <from>.
5231 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005232static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005233{
5234 char *prev = *from;
5235
5236 if (prev == start) {
5237 /* We're removing the first value. eat the semicolon, if <next>
5238 * is lower than <end> */
5239 if (next < end)
5240 next++;
5241
5242 while (next < end && HTTP_IS_SPHT(*next))
5243 next++;
5244 }
5245 else {
5246 /* Remove useless spaces before the old delimiter. */
5247 while (HTTP_IS_SPHT(*(prev-1)))
5248 prev--;
5249 *from = prev;
5250
5251 /* copy the delimiter and if possible a space if we're
5252 * not at the end of the line.
5253 */
5254 if (next < end) {
5255 *prev++ = *next++;
5256 if (prev + 1 < next)
5257 *prev++ = ' ';
5258 while (next < end && HTTP_IS_SPHT(*next))
5259 next++;
5260 }
5261 }
5262 memmove(prev, next, end - next);
5263 return (prev - next);
5264}
5265
Christopher Faulet0f226952018-10-22 09:29:56 +02005266
5267/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005268 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005269 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005270static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005271{
5272 struct ist dst = ist2(str, 0);
5273
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005274 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005275 goto end;
5276 if (dst.len + 1 > len)
5277 goto end;
5278 dst.ptr[dst.len++] = ' ';
5279
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005280 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005281 goto end;
5282 if (dst.len + 1 > len)
5283 goto end;
5284 dst.ptr[dst.len++] = ' ';
5285
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005286 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005287 end:
5288 return dst.len;
5289}
5290
5291/*
5292 * Print a debug line with a start line.
5293 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005294static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005295{
5296 struct session *sess = strm_sess(s);
5297 int max;
5298
5299 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5300 dir,
5301 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5302 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5303
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005304 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005305 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005306 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005307 trash.area[trash.data++] = ' ';
5308
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005309 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005310 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005311 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005312 trash.area[trash.data++] = ' ';
5313
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005314 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005315 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005316 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005317 trash.area[trash.data++] = '\n';
5318
5319 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5320}
5321
5322/*
5323 * Print a debug line with a header.
5324 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005325static 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 +02005326{
5327 struct session *sess = strm_sess(s);
5328 int max;
5329
5330 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5331 dir,
5332 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5333 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5334
5335 max = n.len;
5336 UBOUND(max, trash.size - trash.data - 3);
5337 chunk_memcat(&trash, n.ptr, max);
5338 trash.area[trash.data++] = ':';
5339 trash.area[trash.data++] = ' ';
5340
5341 max = v.len;
5342 UBOUND(max, trash.size - trash.data - 1);
5343 chunk_memcat(&trash, v.ptr, max);
5344 trash.area[trash.data++] = '\n';
5345
5346 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5347}
5348
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005349/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5350 * In case of allocation failure, everything allocated is freed and NULL is
5351 * returned. Otherwise the new transaction is assigned to the stream and
5352 * returned.
5353 */
5354struct http_txn *http_alloc_txn(struct stream *s)
5355{
5356 struct http_txn *txn = s->txn;
5357
5358 if (txn)
5359 return txn;
5360
5361 txn = pool_alloc(pool_head_http_txn);
5362 if (!txn)
5363 return txn;
5364
5365 s->txn = txn;
5366 return txn;
5367}
5368
5369void http_txn_reset_req(struct http_txn *txn)
5370{
5371 txn->req.flags = 0;
5372 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5373}
5374
5375void http_txn_reset_res(struct http_txn *txn)
5376{
5377 txn->rsp.flags = 0;
5378 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5379}
5380
5381/*
5382 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5383 * the required fields are properly allocated and that we only need to (re)init
5384 * them. This should be used before processing any new request.
5385 */
5386void http_init_txn(struct stream *s)
5387{
5388 struct http_txn *txn = s->txn;
5389 struct conn_stream *cs = objt_cs(s->si[0].end);
5390
5391 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5392 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5393 : 0);
5394 txn->status = -1;
5395 *(unsigned int *)txn->cache_hash = 0;
5396
5397 txn->cookie_first_date = 0;
5398 txn->cookie_last_date = 0;
5399
5400 txn->srv_cookie = NULL;
5401 txn->cli_cookie = NULL;
5402 txn->uri = NULL;
5403
5404 http_txn_reset_req(txn);
5405 http_txn_reset_res(txn);
5406
5407 txn->req.chn = &s->req;
5408 txn->rsp.chn = &s->res;
5409
5410 txn->auth.method = HTTP_AUTH_UNKNOWN;
5411
5412 vars_init(&s->vars_txn, SCOPE_TXN);
5413 vars_init(&s->vars_reqres, SCOPE_REQ);
5414}
5415
5416/* to be used at the end of a transaction */
5417void http_end_txn(struct stream *s)
5418{
5419 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005420
5421 /* these ones will have been dynamically allocated */
5422 pool_free(pool_head_requri, txn->uri);
5423 pool_free(pool_head_capture, txn->cli_cookie);
5424 pool_free(pool_head_capture, txn->srv_cookie);
5425 pool_free(pool_head_uniqueid, s->unique_id);
5426
5427 s->unique_id = NULL;
5428 txn->uri = NULL;
5429 txn->srv_cookie = NULL;
5430 txn->cli_cookie = NULL;
5431
Christopher Faulet59399252019-11-07 14:27:52 +01005432 if (!LIST_ISEMPTY(&s->vars_txn.head))
5433 vars_prune(&s->vars_txn, s->sess, s);
5434 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5435 vars_prune(&s->vars_reqres, s->sess, s);
5436}
5437
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005438
5439DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
5440DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
Christopher Faulet0f226952018-10-22 09:29:56 +02005441
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005442__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005443static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005444{
5445}
5446
5447
5448/*
5449 * Local variables:
5450 * c-indent-level: 8
5451 * c-basic-offset: 8
5452 * End:
5453 */