blob: 047ed813a5b7f9ddae0f9c737db977dd7066c0ae [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 Faulet1a3e0272019-11-15 16:31:46 +01001169 if (req->flags & CF_EOI)
1170 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001171 }
1172 else {
1173 /* We can't process the buffer's contents yet */
1174 req->flags |= CF_WAKE_WRITE;
1175 goto missing_data_or_waiting;
1176 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001177 }
1178
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001179 if (msg->msg_state >= HTTP_MSG_ENDING)
1180 goto ending;
1181
1182 if (txn->meth == HTTP_METH_CONNECT) {
1183 msg->msg_state = HTTP_MSG_ENDING;
1184 goto ending;
1185 }
1186
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001187 /* Forward input data. We get it by removing all outgoing data not
1188 * forwarded yet from HTX data size. If there are some data filters, we
1189 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001190 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001191 if (HAS_REQ_DATA_FILTERS(s)) {
1192 ret = flt_http_payload(s, msg, htx->data);
1193 if (ret < 0)
1194 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001195 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001196 }
1197 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001198 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001199 if (msg->flags & HTTP_MSGF_XFER_LEN)
1200 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001201 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001202
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001203 if (htx->data != co_data(req))
1204 goto missing_data_or_waiting;
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;
Christopher Faulet9768c262018-10-22 09:34:31 +02001214
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001215 ending:
1216 /* other states, ENDING...TUNNEL */
1217 if (msg->msg_state >= HTTP_MSG_DONE)
1218 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001219
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001220 if (HAS_REQ_DATA_FILTERS(s)) {
1221 ret = flt_http_end(s, msg);
1222 if (ret <= 0) {
1223 if (!ret)
1224 goto missing_data_or_waiting;
1225 goto return_bad_req;
1226 }
1227 }
1228
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001229 if (txn->meth == HTTP_METH_CONNECT)
1230 msg->msg_state = HTTP_MSG_TUNNEL;
1231 else {
1232 msg->msg_state = HTTP_MSG_DONE;
1233 req->to_forward = 0;
1234 }
1235
1236 done:
1237 /* we don't want to forward closes on DONE except in tunnel mode. */
1238 if (!(txn->flags & TX_CON_WANT_TUN))
1239 channel_dont_close(req);
1240
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001241 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001242 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001243 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001244 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1245 if (req->flags & CF_SHUTW) {
1246 /* request errors are most likely due to the
1247 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001248 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001249 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001250 goto return_bad_req;
1251 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001252 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001253 return 1;
1254 }
1255
1256 /* If "option abortonclose" is set on the backend, we want to monitor
1257 * the client's connection and forward any shutdown notification to the
1258 * server, which will decide whether to close or to go on processing the
1259 * request. We only do that in tunnel mode, and not in other modes since
1260 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001261 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001262 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001263 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001264 s->si[1].flags |= SI_FL_NOLINGER;
1265 channel_auto_close(req);
1266 }
1267 else if (s->txn->meth == HTTP_METH_POST) {
1268 /* POST requests may require to read extra CRLF sent by broken
1269 * browsers and which could cause an RST to be sent upon close
1270 * on some systems (eg: Linux). */
1271 channel_auto_read(req);
1272 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001273 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1274 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001275 return 0;
1276
1277 missing_data_or_waiting:
1278 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001279 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001280 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001281
1282 waiting:
1283 /* waiting for the last bits to leave the buffer */
1284 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001285 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001286
1287 /* When TE: chunked is used, we need to get there again to parse remaining
1288 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1289 * And when content-length is used, we never want to let the possible
1290 * shutdown be forwarded to the other side, as the state machine will
1291 * take care of it once the client responds. It's also important to
1292 * prevent TIME_WAITs from accumulating on the backend side, and for
1293 * HTTP/2 where the last frame comes with a shutdown.
1294 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001295 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001296 channel_dont_close(req);
1297
1298 /* We know that more data are expected, but we couldn't send more that
1299 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1300 * system knows it must not set a PUSH on this first part. Interactive
1301 * modes are already handled by the stream sock layer. We must not do
1302 * this in content-length mode because it could present the MSG_MORE
1303 * flag with the last block of forwarded data, which would cause an
1304 * additional delay to be observed by the receiver.
1305 */
1306 if (msg->flags & HTTP_MSGF_TE_CHNK)
1307 req->flags |= CF_EXPECT_MORE;
1308
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001309 DBG_TRACE_DEVEL("waiting for more data to forward",
1310 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001311 return 0;
1312
Christopher Faulet93e02d82019-03-08 14:18:50 +01001313 return_cli_abort:
1314 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1315 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
1316 if (objt_server(s->target))
1317 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
1318 if (!(s->flags & SF_ERR_MASK))
1319 s->flags |= SF_ERR_CLICL;
1320 status = 400;
1321 goto return_error;
1322
1323 return_srv_abort:
1324 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1325 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
1326 if (objt_server(s->target))
1327 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
1328 if (!(s->flags & SF_ERR_MASK))
1329 s->flags |= SF_ERR_SRVCL;
1330 status = 502;
1331 goto return_error;
1332
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001333 return_int_err:
1334 if (!(s->flags & SF_ERR_MASK))
1335 s->flags |= SF_ERR_INTERNAL;
1336 status = 500;
1337 goto return_error;
1338
Christopher Faulet93e02d82019-03-08 14:18:50 +01001339 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001340 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001341 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001342 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001343 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01001344 s->flags |= SF_ERR_CLICL;
1345 status = 400;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001346
Christopher Faulet93e02d82019-03-08 14:18:50 +01001347 return_error:
Christopher Faulet9768c262018-10-22 09:34:31 +02001348 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001349 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001350 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001351 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001352 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001353 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001354 }
1355 req->analysers &= AN_REQ_FLT_END;
1356 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 +01001357 if (!(s->flags & SF_FINST_MASK))
1358 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001359 DBG_TRACE_DEVEL("leaving on error ",
1360 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001361 return 0;
1362}
1363
Olivier Houcharda254a372019-04-05 15:30:12 +02001364/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1365/* Returns 0 if we can attempt to retry, -1 otherwise */
1366static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1367{
1368 struct channel *req, *res;
1369 int co_data;
1370
1371 si->conn_retries--;
1372 if (si->conn_retries < 0)
1373 return -1;
1374
Willy Tarreau223995e2019-05-04 10:38:31 +02001375 if (objt_server(s->target))
1376 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1377 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1378
Olivier Houcharda254a372019-04-05 15:30:12 +02001379 req = &s->req;
1380 res = &s->res;
1381 /* Remove any write error from the request, and read error from the response */
1382 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1383 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1384 res->analysers = 0;
1385 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001386 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001387 si->exp = TICK_ETERNITY;
1388 res->rex = TICK_ETERNITY;
1389 res->to_forward = 0;
1390 res->analyse_exp = TICK_ETERNITY;
1391 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001392 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001393 si_release_endpoint(&s->si[1]);
1394 b_free(&req->buf);
1395 /* Swap the L7 buffer with the channel buffer */
1396 /* We know we stored the co_data as b_data, so get it there */
1397 co_data = b_data(&si->l7_buffer);
1398 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1399 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1400
1401 co_set_data(req, co_data);
1402 b_reset(&res->buf);
1403 co_set_data(res, 0);
1404 return 0;
1405}
1406
Christopher Faulete0768eb2018-10-03 16:38:02 +02001407/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1408 * processing can continue on next analysers, or zero if it either needs more
1409 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1410 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1411 * when it has nothing left to do, and may remove any analyser when it wants to
1412 * abort.
1413 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001414int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001415{
Christopher Faulet9768c262018-10-22 09:34:31 +02001416 /*
1417 * We will analyze a complete HTTP response to check the its syntax.
1418 *
1419 * Once the start line and all headers are received, we may perform a
1420 * capture of the error (if any), and we will set a few fields. We also
1421 * logging and finally headers capture.
1422 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001423 struct session *sess = s->sess;
1424 struct http_txn *txn = s->txn;
1425 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001426 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001427 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001428 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001429 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001430 int n;
1431
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001432 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001433
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001434 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001435
Willy Tarreau4236f032019-03-05 10:43:32 +01001436 /* Parsing errors are caught here */
1437 if (htx->flags & HTX_FL_PARSING_ERROR)
1438 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001439 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1440 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001441
Christopher Faulete0768eb2018-10-03 16:38:02 +02001442 /*
1443 * Now we quickly check if we have found a full valid response.
1444 * If not so, we check the FD and buffer states before leaving.
1445 * A full response is indicated by the fact that we have seen
1446 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1447 * responses are checked first.
1448 *
1449 * Depending on whether the client is still there or not, we
1450 * may send an error response back or not. Note that normally
1451 * we should only check for HTTP status there, and check I/O
1452 * errors somewhere else.
1453 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001454 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001455 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001456 /* 1: have we encountered a read error ? */
1457 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001458 struct connection *conn = NULL;
1459
Olivier Houchard865d8392019-05-03 22:46:27 +02001460 if (objt_cs(s->si[1].end))
1461 conn = objt_cs(s->si[1].end)->conn;
1462
1463 if (si_b->flags & SI_FL_L7_RETRY &&
1464 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001465 /* If we arrive here, then CF_READ_ERROR was
1466 * set by si_cs_recv() because we matched a
1467 * status, overwise it would have removed
1468 * the SI_FL_L7_RETRY flag, so it's ok not
1469 * to check s->be->retry_type.
1470 */
1471 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1472 return 0;
1473 }
1474
Olivier Houchard6db16992019-05-17 15:40:49 +02001475 if (txn->flags & TX_NOT_FIRST)
1476 goto abort_keep_alive;
1477
Olivier Houcharda798bf52019-03-08 18:52:00 +01001478 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001479 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001480 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001481 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001482 }
1483
Christopher Faulete0768eb2018-10-03 16:38:02 +02001484 rep->analysers &= AN_RES_FLT_END;
1485 txn->status = 502;
1486
1487 /* Check to see if the server refused the early data.
1488 * If so, just send a 425
1489 */
Olivier Houchard865d8392019-05-03 22:46:27 +02001490 if (conn->err_code == CO_ER_SSL_EARLY_FAILED) {
1491 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001492 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001493 do_l7_retry(s, si_b) == 0) {
1494 DBG_TRACE_DEVEL("leaving on L7 retry",
1495 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001496 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001497 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001498 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001499 }
1500
1501 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001502 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001503
1504 if (!(s->flags & SF_ERR_MASK))
1505 s->flags |= SF_ERR_SRVCL;
1506 if (!(s->flags & SF_FINST_MASK))
1507 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001508 DBG_TRACE_DEVEL("leaving on error",
1509 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001510 return 0;
1511 }
1512
Christopher Faulet9768c262018-10-22 09:34:31 +02001513 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001514 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001515 if ((si_b->flags & SI_FL_L7_RETRY) &&
1516 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001517 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1518 DBG_TRACE_DEVEL("leaving on L7 retry",
1519 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001520 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001521 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001522 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001523 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001524 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001525 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001526 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001527 }
1528
Christopher Faulete0768eb2018-10-03 16:38:02 +02001529 rep->analysers &= AN_RES_FLT_END;
1530 txn->status = 504;
1531 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001532 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001533
1534 if (!(s->flags & SF_ERR_MASK))
1535 s->flags |= SF_ERR_SRVTO;
1536 if (!(s->flags & SF_FINST_MASK))
1537 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001538 DBG_TRACE_DEVEL("leaving on error",
1539 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001540 return 0;
1541 }
1542
Christopher Faulet9768c262018-10-22 09:34:31 +02001543 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001544 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001545 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1546 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001547 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001548 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001549
1550 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001551 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001552 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001553
1554 if (!(s->flags & SF_ERR_MASK))
1555 s->flags |= SF_ERR_CLICL;
1556 if (!(s->flags & SF_FINST_MASK))
1557 s->flags |= SF_FINST_H;
1558
1559 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001560 DBG_TRACE_DEVEL("leaving on error",
1561 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001562 return 0;
1563 }
1564
Christopher Faulet9768c262018-10-22 09:34:31 +02001565 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001566 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001567 if ((si_b->flags & SI_FL_L7_RETRY) &&
1568 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001569 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1570 DBG_TRACE_DEVEL("leaving on L7 retry",
1571 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001572 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001573 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001574 }
1575
Olivier Houchard6db16992019-05-17 15:40:49 +02001576 if (txn->flags & TX_NOT_FIRST)
1577 goto abort_keep_alive;
1578
Olivier Houcharda798bf52019-03-08 18:52:00 +01001579 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001580 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001581 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001582 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001583 }
1584
Christopher Faulete0768eb2018-10-03 16:38:02 +02001585 rep->analysers &= AN_RES_FLT_END;
1586 txn->status = 502;
1587 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001588 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001589
1590 if (!(s->flags & SF_ERR_MASK))
1591 s->flags |= SF_ERR_SRVCL;
1592 if (!(s->flags & SF_FINST_MASK))
1593 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001594 DBG_TRACE_DEVEL("leaving on error",
1595 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001596 return 0;
1597 }
1598
Christopher Faulet9768c262018-10-22 09:34:31 +02001599 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001600 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001601 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001602 goto abort_keep_alive;
1603
Olivier Houcharda798bf52019-03-08 18:52:00 +01001604 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001605 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001606
1607 if (!(s->flags & SF_ERR_MASK))
1608 s->flags |= SF_ERR_CLICL;
1609 if (!(s->flags & SF_FINST_MASK))
1610 s->flags |= SF_FINST_H;
1611
1612 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001613 DBG_TRACE_DEVEL("leaving on error",
1614 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001615 return 0;
1616 }
1617
1618 channel_dont_close(rep);
1619 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001620 DBG_TRACE_DEVEL("waiting for more data",
1621 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001622 return 0;
1623 }
1624
1625 /* More interesting part now : we know that we have a complete
1626 * response which at least looks like HTTP. We have an indicator
1627 * of each header's length, so we can parse them quickly.
1628 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001629 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001630 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001631 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001632
Christopher Faulet9768c262018-10-22 09:34:31 +02001633 /* 0: we might have to print this header in debug mode */
1634 if (unlikely((global.mode & MODE_DEBUG) &&
1635 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1636 int32_t pos;
1637
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001638 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001639
Christopher Fauleta3f15502019-05-13 15:27:23 +02001640 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001641 struct htx_blk *blk = htx_get_blk(htx, pos);
1642 enum htx_blk_type type = htx_get_blk_type(blk);
1643
1644 if (type == HTX_BLK_EOH)
1645 break;
1646 if (type != HTX_BLK_HDR)
1647 continue;
1648
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001649 http_debug_hdr("srvhdr", s,
1650 htx_get_blk_name(htx, blk),
1651 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001652 }
1653 }
1654
Christopher Faulet03599112018-11-27 11:21:21 +01001655 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001656 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001657 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001658 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001659 if (sl->flags & HTX_SL_F_XFER_LEN) {
1660 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001661 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001662 if (sl->flags & HTX_SL_F_BODYLESS)
1663 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001664 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001665
1666 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001667 if (n < 1 || n > 5)
1668 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001669
Christopher Faulete0768eb2018-10-03 16:38:02 +02001670 /* when the client triggers a 4xx from the server, it's most often due
1671 * to a missing object or permission. These events should be tracked
1672 * because if they happen often, it may indicate a brute force or a
1673 * vulnerability scan.
1674 */
1675 if (n == 4)
1676 stream_inc_http_err_ctr(s);
1677
1678 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001679 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001680
Christopher Faulete0768eb2018-10-03 16:38:02 +02001681 /* Adjust server's health based on status code. Note: status codes 501
1682 * and 505 are triggered on demand by client request, so we must not
1683 * count them as server failures.
1684 */
1685 if (objt_server(s->target)) {
1686 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001687 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001688 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001689 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001690 }
1691
1692 /*
1693 * We may be facing a 100-continue response, or any other informational
1694 * 1xx response which is non-final, in which case this is not the right
1695 * response, and we're waiting for the next one. Let's allow this response
1696 * to go to the client and wait for the next one. There's an exception for
1697 * 101 which is used later in the code to switch protocols.
1698 */
1699 if (txn->status < 200 &&
1700 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001701 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001702 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001703 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001704 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001705 txn->status = 0;
1706 s->logs.t_data = -1; /* was not a response yet */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001707 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001708 }
1709
1710 /*
1711 * 2: check for cacheability.
1712 */
1713
1714 switch (txn->status) {
1715 case 200:
1716 case 203:
1717 case 204:
1718 case 206:
1719 case 300:
1720 case 301:
1721 case 404:
1722 case 405:
1723 case 410:
1724 case 414:
1725 case 501:
1726 break;
1727 default:
1728 /* RFC7231#6.1:
1729 * Responses with status codes that are defined as
1730 * cacheable by default (e.g., 200, 203, 204, 206,
1731 * 300, 301, 404, 405, 410, 414, and 501 in this
1732 * specification) can be reused by a cache with
1733 * heuristic expiration unless otherwise indicated
1734 * by the method definition or explicit cache
1735 * controls [RFC7234]; all other status codes are
1736 * not cacheable by default.
1737 */
1738 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1739 break;
1740 }
1741
1742 /*
1743 * 3: we may need to capture headers
1744 */
1745 s->logs.logwait &= ~LW_RESP;
1746 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001747 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001748
Christopher Faulet9768c262018-10-22 09:34:31 +02001749 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001750 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1751 txn->status == 101)) {
1752 /* Either we've established an explicit tunnel, or we're
1753 * switching the protocol. In both cases, we're very unlikely
1754 * to understand the next protocols. We have to switch to tunnel
1755 * mode, so that we transfer the request and responses then let
1756 * this protocol pass unmodified. When we later implement specific
1757 * parsers for such protocols, we'll want to check the Upgrade
1758 * header which contains information about that protocol for
1759 * responses with status 101 (eg: see RFC2817 about TLS).
1760 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001761 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001762 }
1763
Christopher Faulet61608322018-11-23 16:23:45 +01001764 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1765 * 407 (Proxy-Authenticate) responses and set the connection to private
1766 */
1767 srv_conn = cs_conn(objt_cs(s->si[1].end));
1768 if (srv_conn) {
1769 struct ist hdr;
1770 struct http_hdr_ctx ctx;
1771
1772 if (txn->status == 401)
1773 hdr = ist("WWW-Authenticate");
1774 else if (txn->status == 407)
1775 hdr = ist("Proxy-Authenticate");
1776 else
1777 goto end;
1778
1779 ctx.blk = NULL;
1780 while (http_find_header(htx, hdr, &ctx, 0)) {
1781 if ((ctx.value.len >= 9 && word_match(ctx.value.ptr, ctx.value.len, "Negotiate", 9)) ||
Olivier Houchard250031e2019-05-29 15:01:50 +02001782 (ctx.value.len >= 4 && word_match(ctx.value.ptr, ctx.value.len, "NTLM", 4))) {
1783 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet61608322018-11-23 16:23:45 +01001784 srv_conn->flags |= CO_FL_PRIVATE;
Olivier Houchard250031e2019-05-29 15:01:50 +02001785 }
Christopher Faulet61608322018-11-23 16:23:45 +01001786 }
1787 }
1788
1789 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001790 /* we want to have the response time before we start processing it */
1791 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1792
1793 /* end of job, return OK */
1794 rep->analysers &= ~an_bit;
1795 rep->analyse_exp = TICK_ETERNITY;
1796 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001797 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001798 return 1;
1799
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001800 return_int_err:
1801 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
1802 txn->status = 500;
1803 if (!(s->flags & SF_ERR_MASK))
1804 s->flags |= SF_ERR_INTERNAL;
1805 goto return_error;
1806
1807 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001808 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001809 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001810 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001811 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001812 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001813 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001814 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001815 do_l7_retry(s, si_b) == 0) {
1816 DBG_TRACE_DEVEL("leaving on L7 retry",
1817 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001818 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001819 }
Christopher Faulet47365272018-10-31 17:40:50 +01001820 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001821 /* fall through */
1822
1823 return_error:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001824 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001825
1826 if (!(s->flags & SF_ERR_MASK))
1827 s->flags |= SF_ERR_PRXCOND;
1828 if (!(s->flags & SF_FINST_MASK))
1829 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001830
1831 s->si[1].flags |= SI_FL_NOLINGER;
1832 rep->analysers &= AN_RES_FLT_END;
1833 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001834 DBG_TRACE_DEVEL("leaving on error",
1835 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001836 return 0;
1837
Christopher Faulete0768eb2018-10-03 16:38:02 +02001838 abort_keep_alive:
1839 /* A keep-alive request to the server failed on a network error.
1840 * The client is required to retry. We need to close without returning
1841 * any other information so that the client retries.
1842 */
1843 txn->status = 0;
1844 rep->analysers &= AN_RES_FLT_END;
1845 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001846 s->logs.logwait = 0;
1847 s->logs.level = 0;
1848 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001849 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001850 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1851 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001852 return 0;
1853}
1854
1855/* This function performs all the processing enabled for the current response.
1856 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1857 * and updates s->res.analysers. It might make sense to explode it into several
1858 * other functions. It works like process_request (see indications above).
1859 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001860int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001861{
1862 struct session *sess = s->sess;
1863 struct http_txn *txn = s->txn;
1864 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001865 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001866 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001867 enum rule_result ret = HTTP_RULE_RES_CONT;
1868
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001869 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1870 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001871
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001872 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001873
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001874 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001875
1876 /* The stats applet needs to adjust the Connection header but we don't
1877 * apply any filter there.
1878 */
1879 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1880 rep->analysers &= ~an_bit;
1881 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001882 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001883 }
1884
1885 /*
1886 * We will have to evaluate the filters.
1887 * As opposed to version 1.2, now they will be evaluated in the
1888 * filters order and not in the header order. This means that
1889 * each filter has to be validated among all headers.
1890 *
1891 * Filters are tried with ->be first, then with ->fe if it is
1892 * different from ->be.
1893 *
1894 * Maybe we are in resume condiion. In this case I choose the
1895 * "struct proxy" which contains the rule list matching the resume
1896 * pointer. If none of theses "struct proxy" match, I initialise
1897 * the process with the first one.
1898 *
1899 * In fact, I check only correspondance betwwen the current list
1900 * pointer and the ->fe rule list. If it doesn't match, I initialize
1901 * the loop with the ->be.
1902 */
1903 if (s->current_rule_list == &sess->fe->http_res_rules)
1904 cur_proxy = sess->fe;
1905 else
1906 cur_proxy = s->be;
1907 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001908 /* evaluate http-response rules */
1909 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001910 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001911
1912 if (ret == HTTP_RULE_RES_BADREQ)
1913 goto return_srv_prx_502;
1914
1915 if (ret == HTTP_RULE_RES_DONE) {
1916 rep->analysers &= ~an_bit;
1917 rep->analyse_exp = TICK_ETERNITY;
1918 return 1;
1919 }
1920 }
1921
1922 /* we need to be called again. */
1923 if (ret == HTTP_RULE_RES_YIELD) {
1924 channel_dont_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001925 DBG_TRACE_DEVEL("waiting for more data",
1926 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001927 return 0;
1928 }
1929
Christopher Faulete0768eb2018-10-03 16:38:02 +02001930 /* has the response been denied ? */
1931 if (txn->flags & TX_SVDENY) {
1932 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001933 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_secu, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001934
Olivier Houcharda798bf52019-03-08 18:52:00 +01001935 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
1936 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001937 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001938 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001939 goto return_srv_prx_502;
1940 }
1941
Christopher Faulete0768eb2018-10-03 16:38:02 +02001942 /* check whether we're already working on the frontend */
1943 if (cur_proxy == sess->fe)
1944 break;
1945 cur_proxy = sess->fe;
1946 }
1947
1948 /* After this point, this anayzer can't return yield, so we can
1949 * remove the bit corresponding to this analyzer from the list.
1950 *
1951 * Note that the intermediate returns and goto found previously
1952 * reset the analyzers.
1953 */
1954 rep->analysers &= ~an_bit;
1955 rep->analyse_exp = TICK_ETERNITY;
1956
1957 /* OK that's all we can do for 1xx responses */
1958 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001959 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001960
1961 /*
1962 * Now check for a server cookie.
1963 */
1964 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001965 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001966
1967 /*
1968 * Check for cache-control or pragma headers if required.
1969 */
1970 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001971 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001972
1973 /*
1974 * Add server cookie in the response if needed
1975 */
1976 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1977 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1978 (!(s->flags & SF_DIRECT) ||
1979 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1980 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1981 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1982 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1983 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1984 !(s->flags & SF_IGNORE_PRST)) {
1985 /* the server is known, it's not the one the client requested, or the
1986 * cookie's last seen date needs to be refreshed. We have to
1987 * insert a set-cookie here, except if we want to insert only on POST
1988 * requests and this one isn't. Note that servers which don't have cookies
1989 * (eg: some backup servers) will return a full cookie removal request.
1990 */
1991 if (!objt_server(s->target)->cookie) {
1992 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001993 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001994 s->be->cookie_name);
1995 }
1996 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001997 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001998
1999 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2000 /* emit last_date, which is mandatory */
2001 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2002 s30tob64((date.tv_sec+3) >> 2,
2003 trash.area + trash.data);
2004 trash.data += 5;
2005
2006 if (s->be->cookie_maxlife) {
2007 /* emit first_date, which is either the original one or
2008 * the current date.
2009 */
2010 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2011 s30tob64(txn->cookie_first_date ?
2012 txn->cookie_first_date >> 2 :
2013 (date.tv_sec+3) >> 2,
2014 trash.area + trash.data);
2015 trash.data += 5;
2016 }
2017 }
2018 chunk_appendf(&trash, "; path=/");
2019 }
2020
2021 if (s->be->cookie_domain)
2022 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2023
2024 if (s->be->ck_opts & PR_CK_HTTPONLY)
2025 chunk_appendf(&trash, "; HttpOnly");
2026
2027 if (s->be->ck_opts & PR_CK_SECURE)
2028 chunk_appendf(&trash, "; Secure");
2029
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002030 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002031 goto return_bad_resp;
2032
2033 txn->flags &= ~TX_SCK_MASK;
2034 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2035 /* the server did not change, only the date was updated */
2036 txn->flags |= TX_SCK_UPDATED;
2037 else
2038 txn->flags |= TX_SCK_INSERTED;
2039
2040 /* Here, we will tell an eventual cache on the client side that we don't
2041 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2042 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2043 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2044 */
2045 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2046
2047 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2048
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002049 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002050 goto return_bad_resp;
2051 }
2052 }
2053
2054 /*
2055 * Check if result will be cacheable with a cookie.
2056 * We'll block the response if security checks have caught
2057 * nasty things such as a cacheable cookie.
2058 */
2059 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2060 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2061 (s->be->options & PR_O_CHK_CACHE)) {
2062 /* we're in presence of a cacheable response containing
2063 * a set-cookie header. We'll block it as requested by
2064 * the 'checkcache' option, and send an alert.
2065 */
2066 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01002067 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002068
Olivier Houcharda798bf52019-03-08 18:52:00 +01002069 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
2070 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002071 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002072 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002073
2074 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2075 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2076 send_log(s->be, LOG_ALERT,
2077 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2078 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2079 goto return_srv_prx_502;
2080 }
2081
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002082 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002083 /* Always enter in the body analyzer */
2084 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2085 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2086
2087 /* if the user wants to log as soon as possible, without counting
2088 * bytes from the server, then this is the right moment. We have
2089 * to temporarily assign bytes_out to log what we currently have.
2090 */
2091 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2092 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002093 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002094 s->do_log(s);
2095 s->logs.bytes_out = 0;
2096 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002097 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002098 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002099
2100 return_bad_resp:
2101 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01002102 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01002103 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002104 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01002105 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002106
2107 return_srv_prx_502:
2108 rep->analysers &= AN_RES_FLT_END;
2109 txn->status = 502;
2110 s->logs.t_data = -1; /* was not a valid response */
2111 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002112 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002113 if (!(s->flags & SF_ERR_MASK))
2114 s->flags |= SF_ERR_PRXCOND;
2115 if (!(s->flags & SF_FINST_MASK))
2116 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002117 DBG_TRACE_DEVEL("leaving on error",
2118 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002119 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002120}
2121
2122/* This function is an analyser which forwards response body (including chunk
2123 * sizes if any). It is called as soon as we must forward, even if we forward
2124 * zero byte. The only situation where it must not be called is when we're in
2125 * tunnel mode and we want to forward till the close. It's used both to forward
2126 * remaining data and to resync after end of body. It expects the msg_state to
2127 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2128 * read more data, or 1 once we can go on with next request or end the stream.
2129 *
2130 * It is capable of compressing response data both in content-length mode and
2131 * in chunked mode. The state machines follows different flows depending on
2132 * whether content-length and chunked modes are used, since there are no
2133 * trailers in content-length :
2134 *
2135 * chk-mode cl-mode
2136 * ,----- BODY -----.
2137 * / \
2138 * V size > 0 V chk-mode
2139 * .--> SIZE -------------> DATA -------------> CRLF
2140 * | | size == 0 | last byte |
2141 * | v final crlf v inspected |
2142 * | TRAILERS -----------> DONE |
2143 * | |
2144 * `----------------------------------------------'
2145 *
2146 * Compression only happens in the DATA state, and must be flushed in final
2147 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2148 * is performed at once on final states for all bytes parsed, or when leaving
2149 * on missing data.
2150 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002151int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002152{
2153 struct session *sess = s->sess;
2154 struct http_txn *txn = s->txn;
2155 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002156 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002157 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002158
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002159 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002160
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002161 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002162
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002163 if (htx->flags & HTX_FL_PARSING_ERROR)
2164 goto return_bad_res;
2165 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2166 goto return_int_err;
2167
Christopher Faulete0768eb2018-10-03 16:38:02 +02002168 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002169 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002170 /* Output closed while we were sending data. We must abort and
2171 * wake the other side up.
2172 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002173 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002174 http_end_response(s);
2175 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002176 DBG_TRACE_DEVEL("leaving on error",
2177 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002178 return 1;
2179 }
2180
Christopher Faulet9768c262018-10-22 09:34:31 +02002181 if (msg->msg_state == HTTP_MSG_BODY)
2182 msg->msg_state = HTTP_MSG_DATA;
2183
Christopher Faulete0768eb2018-10-03 16:38:02 +02002184 /* in most states, we should abort in case of early close */
2185 channel_auto_close(res);
2186
Christopher Faulete0768eb2018-10-03 16:38:02 +02002187 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002188 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002189 if (res->flags & CF_EOI)
2190 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002191 }
2192 else {
2193 /* We can't process the buffer's contents yet */
2194 res->flags |= CF_WAKE_WRITE;
2195 goto missing_data_or_waiting;
2196 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002197 }
2198
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002199 if (msg->msg_state >= HTTP_MSG_ENDING)
2200 goto ending;
2201
2202 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2203 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2204 msg->msg_state = HTTP_MSG_ENDING;
2205 goto ending;
2206 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002207
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002208 /* Forward input data. We get it by removing all outgoing data not
2209 * forwarded yet from HTX data size. If there are some data filters, we
2210 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002211 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002212 if (HAS_RSP_DATA_FILTERS(s)) {
2213 ret = flt_http_payload(s, msg, htx->data);
2214 if (ret < 0)
2215 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002216 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002217 }
2218 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002219 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002220 if (msg->flags & HTTP_MSGF_XFER_LEN)
2221 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002222 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002223
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002224 if (htx->data != co_data(res))
2225 goto missing_data_or_waiting;
2226
2227 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2228 msg->msg_state = HTTP_MSG_ENDING;
2229 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002230 }
2231
Christopher Faulet9768c262018-10-22 09:34:31 +02002232 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002233 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2234 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002235 */
2236 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2237 goto missing_data_or_waiting;
2238
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002239 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002240
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002241 ending:
2242 /* other states, ENDING...TUNNEL */
2243 if (msg->msg_state >= HTTP_MSG_DONE)
2244 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002245
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002246 if (HAS_RSP_DATA_FILTERS(s)) {
2247 ret = flt_http_end(s, msg);
2248 if (ret <= 0) {
2249 if (!ret)
2250 goto missing_data_or_waiting;
2251 goto return_bad_res;
2252 }
2253 }
2254
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002255 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2256 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2257 msg->msg_state = HTTP_MSG_TUNNEL;
2258 goto ending;
2259 }
2260 else {
2261 msg->msg_state = HTTP_MSG_DONE;
2262 res->to_forward = 0;
2263 }
2264
2265 done:
2266
2267 channel_dont_close(res);
2268
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002269 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002270 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002271 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002272 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2273 if (res->flags & CF_SHUTW) {
2274 /* response errors are most likely due to the
2275 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002276 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002277 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002278 goto return_bad_res;
2279 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002280 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002281 return 1;
2282 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002283 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2284 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002285 return 0;
2286
2287 missing_data_or_waiting:
2288 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002289 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002290
2291 /* stop waiting for data if the input is closed before the end. If the
2292 * client side was already closed, it means that the client has aborted,
2293 * so we don't want to count this as a server abort. Otherwise it's a
2294 * server abort.
2295 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002296 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002297 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002298 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002299 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002300 if (htx_is_empty(htx))
2301 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002302 }
2303
Christopher Faulete0768eb2018-10-03 16:38:02 +02002304 /* When TE: chunked is used, we need to get there again to parse
2305 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002306 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2307 * are filters registered on the stream, we don't want to forward a
2308 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002309 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002310 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002311 channel_dont_close(res);
2312
2313 /* We know that more data are expected, but we couldn't send more that
2314 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2315 * system knows it must not set a PUSH on this first part. Interactive
2316 * modes are already handled by the stream sock layer. We must not do
2317 * this in content-length mode because it could present the MSG_MORE
2318 * flag with the last block of forwarded data, which would cause an
2319 * additional delay to be observed by the receiver.
2320 */
2321 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2322 res->flags |= CF_EXPECT_MORE;
2323
2324 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002325 DBG_TRACE_DEVEL("waiting for more data to forward",
2326 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002327 return 0;
2328
Christopher Faulet93e02d82019-03-08 14:18:50 +01002329 return_srv_abort:
2330 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2331 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002332 if (objt_server(s->target))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002333 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
2334 if (!(s->flags & SF_ERR_MASK))
2335 s->flags |= SF_ERR_SRVCL;
2336 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002337
Christopher Faulet93e02d82019-03-08 14:18:50 +01002338 return_cli_abort:
2339 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2340 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002341 if (objt_server(s->target))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002342 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
2343 if (!(s->flags & SF_ERR_MASK))
2344 s->flags |= SF_ERR_CLICL;
2345 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002346
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002347 return_int_err:
2348 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2349 if (!(s->flags & SF_ERR_MASK))
2350 s->flags |= SF_ERR_INTERNAL;
2351 goto return_error;
2352
Christopher Faulet93e02d82019-03-08 14:18:50 +01002353 return_bad_res:
2354 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2355 if (objt_server(s->target)) {
2356 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
2357 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2358 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002359 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002360 s->flags |= SF_ERR_SRVCL;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002361
Christopher Faulet93e02d82019-03-08 14:18:50 +01002362 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002363 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002364 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002365 res->analysers &= AN_RES_FLT_END;
2366 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 +02002367 if (!(s->flags & SF_FINST_MASK))
2368 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002369 DBG_TRACE_DEVEL("leaving on error",
2370 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002371 return 0;
2372}
2373
Christopher Fauletf2824e62018-10-01 12:12:37 +02002374/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002375 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002376 * as too large a request to build a valid response.
2377 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002378int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002379{
Christopher Faulet99daf282018-11-28 22:58:13 +01002380 struct channel *req = &s->req;
2381 struct channel *res = &s->res;
2382 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002383 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002384 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002385 struct ist status, reason, location;
2386 unsigned int flags;
2387 size_t data;
Christopher Faulet08e66462019-05-23 16:44:59 +02002388 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002389
2390 chunk = alloc_trash_chunk();
2391 if (!chunk)
Christopher Faulet99daf282018-11-28 22:58:13 +01002392 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002393
Christopher Faulet99daf282018-11-28 22:58:13 +01002394 /*
2395 * Create the location
2396 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002397 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002398 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002399 case REDIRECT_TYPE_SCHEME: {
2400 struct http_hdr_ctx ctx;
2401 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002402
Christopher Faulet99daf282018-11-28 22:58:13 +01002403 host = ist("");
2404 ctx.blk = NULL;
2405 if (http_find_header(htx, ist("Host"), &ctx, 0))
2406 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002407
Christopher Faulet297fbb42019-05-13 14:41:27 +02002408 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002409 path = http_get_path(htx_sl_req_uri(sl));
2410 /* build message using path */
2411 if (path.ptr) {
2412 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2413 int qs = 0;
2414 while (qs < path.len) {
2415 if (*(path.ptr + qs) == '?') {
2416 path.len = qs;
2417 break;
2418 }
2419 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002420 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002421 }
2422 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002423 else
2424 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002425
Christopher Faulet99daf282018-11-28 22:58:13 +01002426 if (rule->rdr_str) { /* this is an old "redirect" rule */
2427 /* add scheme */
2428 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2429 goto fail;
2430 }
2431 else {
2432 /* add scheme with executing log format */
2433 chunk->data += build_logline(s, chunk->area + chunk->data,
2434 chunk->size - chunk->data,
2435 &rule->rdr_fmt);
2436 }
2437 /* add "://" + host + path */
2438 if (!chunk_memcat(chunk, "://", 3) ||
2439 !chunk_memcat(chunk, host.ptr, host.len) ||
2440 !chunk_memcat(chunk, path.ptr, path.len))
2441 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002442
Christopher Faulet99daf282018-11-28 22:58:13 +01002443 /* append a slash at the end of the location if needed and missing */
2444 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2445 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2446 if (chunk->data + 1 >= chunk->size)
2447 goto fail;
2448 chunk->area[chunk->data++] = '/';
2449 }
2450 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002451 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002452
Christopher Faulet99daf282018-11-28 22:58:13 +01002453 case REDIRECT_TYPE_PREFIX: {
2454 struct ist path;
2455
Christopher Faulet297fbb42019-05-13 14:41:27 +02002456 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002457 path = http_get_path(htx_sl_req_uri(sl));
2458 /* build message using path */
2459 if (path.ptr) {
2460 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2461 int qs = 0;
2462 while (qs < path.len) {
2463 if (*(path.ptr + qs) == '?') {
2464 path.len = qs;
2465 break;
2466 }
2467 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002468 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002469 }
2470 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002471 else
2472 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002473
Christopher Faulet99daf282018-11-28 22:58:13 +01002474 if (rule->rdr_str) { /* this is an old "redirect" rule */
2475 /* add prefix. Note that if prefix == "/", we don't want to
2476 * add anything, otherwise it makes it hard for the user to
2477 * configure a self-redirection.
2478 */
2479 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2480 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2481 goto fail;
2482 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002483 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002484 else {
2485 /* add prefix with executing log format */
2486 chunk->data += build_logline(s, chunk->area + chunk->data,
2487 chunk->size - chunk->data,
2488 &rule->rdr_fmt);
2489 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002490
Christopher Faulet99daf282018-11-28 22:58:13 +01002491 /* add path */
2492 if (!chunk_memcat(chunk, path.ptr, path.len))
2493 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002494
Christopher Faulet99daf282018-11-28 22:58:13 +01002495 /* append a slash at the end of the location if needed and missing */
2496 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2497 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2498 if (chunk->data + 1 >= chunk->size)
2499 goto fail;
2500 chunk->area[chunk->data++] = '/';
2501 }
2502 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002503 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002504 case REDIRECT_TYPE_LOCATION:
2505 default:
2506 if (rule->rdr_str) { /* this is an old "redirect" rule */
2507 /* add location */
2508 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2509 goto fail;
2510 }
2511 else {
2512 /* add location with executing log format */
2513 chunk->data += build_logline(s, chunk->area + chunk->data,
2514 chunk->size - chunk->data,
2515 &rule->rdr_fmt);
2516 }
2517 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002518 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002519 location = ist2(chunk->area, chunk->data);
2520
2521 /*
2522 * Create the 30x response
2523 */
2524 switch (rule->code) {
2525 case 308:
2526 status = ist("308");
2527 reason = ist("Permanent Redirect");
2528 break;
2529 case 307:
2530 status = ist("307");
2531 reason = ist("Temporary Redirect");
2532 break;
2533 case 303:
2534 status = ist("303");
2535 reason = ist("See Other");
2536 break;
2537 case 301:
2538 status = ist("301");
2539 reason = ist("Moved Permanently");
2540 break;
2541 case 302:
2542 default:
2543 status = ist("302");
2544 reason = ist("Found");
2545 break;
2546 }
2547
Christopher Faulet08e66462019-05-23 16:44:59 +02002548 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2549 close = 1;
2550
Christopher Faulet99daf282018-11-28 22:58:13 +01002551 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002552 /* Trim any possible response */
2553 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002554 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2555 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2556 if (!sl)
2557 goto fail;
2558 sl->info.res.status = rule->code;
2559 s->txn->status = rule->code;
2560
Christopher Faulet08e66462019-05-23 16:44:59 +02002561 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2562 goto fail;
2563
2564 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002565 !htx_add_header(htx, ist("Location"), location))
2566 goto fail;
2567
2568 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2569 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2570 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002571 }
2572
2573 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002574 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2575 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002576 }
2577
Christopher Faulet99daf282018-11-28 22:58:13 +01002578 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2579 goto fail;
2580
Kevin Zhu96b36392020-01-07 09:42:55 +01002581 htx_to_buf(htx, &res->buf);
2582
Christopher Fauletf2824e62018-10-01 12:12:37 +02002583 /* let's log the request time */
2584 s->logs.tv_request = now;
2585
Christopher Faulet06511812019-10-16 09:38:27 +02002586 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet99daf282018-11-28 22:58:13 +01002587 data = htx->data - co_data(res);
Christopher Faulet99daf282018-11-28 22:58:13 +01002588 c_adv(res, data);
2589 res->total += data;
2590
2591 channel_auto_read(req);
2592 channel_abort(req);
2593 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002594 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet99daf282018-11-28 22:58:13 +01002595
2596 res->wex = tick_add_ifset(now_ms, res->wto);
2597 channel_auto_read(res);
2598 channel_auto_close(res);
2599 channel_shutr_now(res);
2600
2601 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002602
2603 if (!(s->flags & SF_ERR_MASK))
2604 s->flags |= SF_ERR_LOCAL;
2605 if (!(s->flags & SF_FINST_MASK))
2606 s->flags |= SF_FINST_R;
2607
Christopher Faulet99daf282018-11-28 22:58:13 +01002608 free_trash_chunk(chunk);
2609 return 1;
2610
2611 fail:
2612 /* If an error occurred, remove the incomplete HTTP response from the
2613 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002614 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002615 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002616 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002617}
2618
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002619int http_transform_header_str(struct stream* s, struct channel *chn, struct htx *htx,
2620 struct ist name, const char *str, struct my_regex *re, int action)
Christopher Faulet72333522018-10-24 11:25:02 +02002621{
2622 struct http_hdr_ctx ctx;
2623 struct buffer *output = get_trash_chunk();
2624
2625 /* find full header is action is ACT_HTTP_REPLACE_HDR */
2626 ctx.blk = NULL;
2627 while (http_find_header(htx, name, &ctx, (action == ACT_HTTP_REPLACE_HDR))) {
2628 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2629 continue;
2630
2631 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2632 if (output->data == -1)
2633 return -1;
2634 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2635 return -1;
2636 }
2637 return 0;
2638}
2639
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002640static int http_transform_header(struct stream* s, struct channel *chn, struct htx *htx,
2641 const struct ist name, struct list *fmt, struct my_regex *re, int action)
Christopher Faulet72333522018-10-24 11:25:02 +02002642{
2643 struct buffer *replace;
2644 int ret = -1;
2645
2646 replace = alloc_trash_chunk();
2647 if (!replace)
2648 goto leave;
2649
2650 replace->data = build_logline(s, replace->area, replace->size, fmt);
2651 if (replace->data >= replace->size - 1)
2652 goto leave;
2653
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002654 ret = http_transform_header_str(s, chn, htx, name, replace->area, re, action);
Christopher Faulet72333522018-10-24 11:25:02 +02002655
2656 leave:
2657 free_trash_chunk(replace);
2658 return ret;
2659}
2660
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002661
2662/* Terminate a 103-Erly-hints response and send it to the client. It returns 0
2663 * on success and -1 on error. The response channel is updated accordingly.
2664 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002665static int http_reply_103_early_hints(struct channel *res)
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002666{
2667 struct htx *htx = htx_from_buf(&res->buf);
2668 size_t data;
2669
Christopher Faulet1d5ec092019-06-26 14:23:54 +02002670 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002671 /* If an error occurred during an Early-hint rule,
2672 * remove the incomplete HTTP 103 response from the
2673 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002674 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002675 return -1;
2676 }
2677
2678 data = htx->data - co_data(res);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002679 c_adv(res, data);
2680 res->total += data;
2681 return 0;
2682}
2683
Christopher Faulet6eb92892018-11-15 16:39:29 +01002684/*
2685 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
2686 * built according to <fmt> log line format.
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002687 * If <early_hints> is 0, it is starts a new response by adding the start
2688 * line. If an error occurred -1 is returned. On success 0 is returned. The
2689 * channel is not updated here. It must be done calling the function
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002690 * http_reply_103_early_hints().
Christopher Faulet6eb92892018-11-15 16:39:29 +01002691 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002692static 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 +01002693{
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002694 struct channel *res = &s->res;
2695 struct htx *htx = htx_from_buf(&res->buf);
2696 struct buffer *value = alloc_trash_chunk();
2697
Christopher Faulet6eb92892018-11-15 16:39:29 +01002698 if (!early_hints) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002699 struct htx_sl *sl;
2700 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
2701 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2702
2703 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
2704 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
2705 if (!sl)
Christopher Faulet6eb92892018-11-15 16:39:29 +01002706 goto fail;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002707 sl->info.res.status = 103;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002708 }
2709
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002710 value->data = build_logline(s, b_tail(value), b_room(value), fmt);
2711 if (!htx_add_header(htx, name, ist2(b_head(value), b_data(value))))
Christopher Faulet6eb92892018-11-15 16:39:29 +01002712 goto fail;
2713
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002714 free_trash_chunk(value);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002715 return 1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002716
2717 fail:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002718 /* If an error occurred during an Early-hint rule, remove the incomplete
2719 * HTTP 103 response from the buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002720 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002721 free_trash_chunk(value);
2722 return -1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002723}
2724
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002725/* This function executes one of the set-{method,path,query,uri} actions. It
2726 * takes the string from the variable 'replace' with length 'len', then modifies
2727 * the relevant part of the request line accordingly. Then it updates various
2728 * pointers to the next elements which were moved, and the total buffer length.
2729 * It finds the action to be performed in p[2], previously filled by function
2730 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2731 * error, though this can be revisited when this code is finally exploited.
2732 *
2733 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2734 * query string and 3 to replace uri.
2735 *
2736 * In query string case, the mark question '?' must be set at the start of the
2737 * string by the caller, event if the replacement query string is empty.
2738 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002739int http_req_replace_stline(int action, const char *replace, int len,
2740 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002741{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002742 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002743
2744 switch (action) {
2745 case 0: // method
2746 if (!http_replace_req_meth(htx, ist2(replace, len)))
2747 return -1;
2748 break;
2749
2750 case 1: // path
2751 if (!http_replace_req_path(htx, ist2(replace, len)))
2752 return -1;
2753 break;
2754
2755 case 2: // query
2756 if (!http_replace_req_query(htx, ist2(replace, len)))
2757 return -1;
2758 break;
2759
2760 case 3: // uri
2761 if (!http_replace_req_uri(htx, ist2(replace, len)))
2762 return -1;
2763 break;
2764
2765 default:
2766 return -1;
2767 }
2768 return 0;
2769}
2770
2771/* This function replace the HTTP status code and the associated message. The
2772 * variable <status> contains the new status code. This function never fails.
2773 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002774void http_res_set_status(unsigned int status, const char *reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002775{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002776 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002777 char *res;
2778
2779 chunk_reset(&trash);
2780 res = ultoa_o(status, trash.area, trash.size);
2781 trash.data = res - trash.area;
2782
2783 /* Do we have a custom reason format string? */
2784 if (reason == NULL)
2785 reason = http_get_reason(status);
2786
Christopher Faulet87a2c0d2018-12-13 21:58:18 +01002787 if (http_replace_res_status(htx, ist2(trash.area, trash.data)))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002788 http_replace_res_reason(htx, ist2(reason, strlen(reason)));
2789}
2790
Christopher Faulet3e964192018-10-24 11:39:23 +02002791/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2792 * transaction <txn>. Returns the verdict of the first rule that prevents
2793 * further processing of the request (auth, deny, ...), and defaults to
2794 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2795 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2796 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2797 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2798 * status.
2799 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002800static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
2801 struct stream *s, int *deny_status)
Christopher Faulet3e964192018-10-24 11:39:23 +02002802{
2803 struct session *sess = strm_sess(s);
2804 struct http_txn *txn = s->txn;
2805 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002806 struct act_rule *rule;
2807 struct http_hdr_ctx ctx;
2808 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002809 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2810 int act_flags = 0;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002811 int early_hints = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002812
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002813 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002814
2815 /* If "the current_rule_list" match the executed rule list, we are in
2816 * resume condition. If a resume is needed it is always in the action
2817 * and never in the ACL or converters. In this case, we initialise the
2818 * current rule, and go to the action execution point.
2819 */
2820 if (s->current_rule) {
2821 rule = s->current_rule;
2822 s->current_rule = NULL;
2823 if (s->current_rule_list == rules)
2824 goto resume_execution;
2825 }
2826 s->current_rule_list = rules;
2827
2828 list_for_each_entry(rule, rules, list) {
2829 /* check optional condition */
2830 if (rule->cond) {
2831 int ret;
2832
2833 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2834 ret = acl_pass(ret);
2835
2836 if (rule->cond->pol == ACL_COND_UNLESS)
2837 ret = !ret;
2838
2839 if (!ret) /* condition not matched */
2840 continue;
2841 }
2842
2843 act_flags |= ACT_FLAG_FIRST;
2844 resume_execution:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002845 if (early_hints && rule->action != ACT_HTTP_EARLY_HINT) {
2846 early_hints = 0;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002847 if (http_reply_103_early_hints(&s->res) == -1) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002848 rule_ret = HTTP_RULE_RES_BADREQ;
2849 goto end;
2850 }
2851 }
2852
Christopher Faulet3e964192018-10-24 11:39:23 +02002853 switch (rule->action) {
2854 case ACT_ACTION_ALLOW:
2855 rule_ret = HTTP_RULE_RES_STOP;
2856 goto end;
2857
2858 case ACT_ACTION_DENY:
2859 if (deny_status)
2860 *deny_status = rule->deny_status;
2861 rule_ret = HTTP_RULE_RES_DENY;
2862 goto end;
2863
2864 case ACT_HTTP_REQ_TARPIT:
2865 txn->flags |= TX_CLTARPIT;
2866 if (deny_status)
2867 *deny_status = rule->deny_status;
2868 rule_ret = HTTP_RULE_RES_DENY;
2869 goto end;
2870
2871 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002872 /* Auth might be performed on regular http-req rules as well as on stats */
2873 auth_realm = rule->arg.auth.realm;
2874 if (!auth_realm) {
2875 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2876 auth_realm = STATS_DEFAULT_REALM;
2877 else
2878 auth_realm = px->id;
2879 }
2880 /* send 401/407 depending on whether we use a proxy or not. We still
2881 * count one error, because normal browsing won't significantly
2882 * increase the counter but brute force attempts will.
2883 */
Christopher Faulet3e964192018-10-24 11:39:23 +02002884 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002885 if (http_reply_40x_unauthorized(s, auth_realm) == -1)
Christopher Faulet12c51e22018-11-28 15:59:42 +01002886 rule_ret = HTTP_RULE_RES_BADREQ;
2887 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02002888 goto end;
2889
2890 case ACT_HTTP_REDIR:
Christopher Faulet3e964192018-10-24 11:39:23 +02002891 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002892 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3e964192018-10-24 11:39:23 +02002893 rule_ret = HTTP_RULE_RES_BADREQ;
2894 goto end;
2895
2896 case ACT_HTTP_SET_NICE:
2897 s->task->nice = rule->arg.nice;
2898 break;
2899
2900 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01002901 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Christopher Faulet3e964192018-10-24 11:39:23 +02002902 break;
2903
2904 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01002905 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Christopher Faulet3e964192018-10-24 11:39:23 +02002906 break;
2907
2908 case ACT_HTTP_SET_LOGL:
2909 s->logs.level = rule->arg.loglevel;
2910 break;
2911
2912 case ACT_HTTP_REPLACE_HDR:
2913 case ACT_HTTP_REPLACE_VAL:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002914 if (http_transform_header(s, &s->req, htx,
2915 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
2916 &rule->arg.hdr_add.fmt,
2917 rule->arg.hdr_add.re, rule->action)) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002918 rule_ret = HTTP_RULE_RES_BADREQ;
2919 goto end;
2920 }
2921 break;
2922
2923 case ACT_HTTP_DEL_HDR:
2924 /* remove all occurrences of the header */
2925 ctx.blk = NULL;
2926 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
2927 http_remove_header(htx, &ctx);
2928 break;
2929
2930 case ACT_HTTP_SET_HDR:
2931 case ACT_HTTP_ADD_HDR: {
2932 /* The scope of the trash buffer must be limited to this function. The
2933 * build_logline() function can execute a lot of other function which
2934 * can use the trash buffer. So for limiting the scope of this global
2935 * buffer, we build first the header value using build_logline, and
2936 * after we store the header name.
2937 */
2938 struct buffer *replace;
2939 struct ist n, v;
2940
2941 replace = alloc_trash_chunk();
2942 if (!replace) {
2943 rule_ret = HTTP_RULE_RES_BADREQ;
2944 goto end;
2945 }
2946
2947 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
2948 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2949 v = ist2(replace->area, replace->data);
2950
2951 if (rule->action == ACT_HTTP_SET_HDR) {
2952 /* remove all occurrences of the header */
2953 ctx.blk = NULL;
2954 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
2955 http_remove_header(htx, &ctx);
2956 }
2957
2958 if (!http_add_header(htx, n, v)) {
2959 static unsigned char rate_limit = 0;
2960
2961 if ((rate_limit++ & 255) == 0) {
2962 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);
2963 }
2964
Olivier Houcharda798bf52019-03-08 18:52:00 +01002965 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002966 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002967 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002968 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002969 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002970 }
2971 free_trash_chunk(replace);
2972 break;
2973 }
2974
2975 case ACT_HTTP_DEL_ACL:
2976 case ACT_HTTP_DEL_MAP: {
2977 struct pat_ref *ref;
2978 struct buffer *key;
2979
2980 /* collect reference */
2981 ref = pat_ref_lookup(rule->arg.map.ref);
2982 if (!ref)
2983 continue;
2984
2985 /* allocate key */
2986 key = alloc_trash_chunk();
2987 if (!key) {
2988 rule_ret = HTTP_RULE_RES_BADREQ;
2989 goto end;
2990 }
2991
2992 /* collect key */
2993 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2994 key->area[key->data] = '\0';
2995
2996 /* perform update */
2997 /* returned code: 1=ok, 0=ko */
2998 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
2999 pat_ref_delete(ref, key->area);
3000 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3001
3002 free_trash_chunk(key);
3003 break;
3004 }
3005
3006 case ACT_HTTP_ADD_ACL: {
3007 struct pat_ref *ref;
3008 struct buffer *key;
3009
3010 /* collect reference */
3011 ref = pat_ref_lookup(rule->arg.map.ref);
3012 if (!ref)
3013 continue;
3014
3015 /* allocate key */
3016 key = alloc_trash_chunk();
3017 if (!key) {
3018 rule_ret = HTTP_RULE_RES_BADREQ;
3019 goto end;
3020 }
3021
3022 /* collect key */
3023 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3024 key->area[key->data] = '\0';
3025
3026 /* perform update */
3027 /* add entry only if it does not already exist */
3028 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3029 if (pat_ref_find_elt(ref, key->area) == NULL)
3030 pat_ref_add(ref, key->area, NULL, NULL);
3031 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3032
3033 free_trash_chunk(key);
3034 break;
3035 }
3036
3037 case ACT_HTTP_SET_MAP: {
3038 struct pat_ref *ref;
3039 struct buffer *key, *value;
3040
3041 /* collect reference */
3042 ref = pat_ref_lookup(rule->arg.map.ref);
3043 if (!ref)
3044 continue;
3045
3046 /* allocate key */
3047 key = alloc_trash_chunk();
3048 if (!key) {
3049 rule_ret = HTTP_RULE_RES_BADREQ;
3050 goto end;
3051 }
3052
3053 /* allocate value */
3054 value = alloc_trash_chunk();
3055 if (!value) {
3056 free_trash_chunk(key);
3057 rule_ret = HTTP_RULE_RES_BADREQ;
3058 goto end;
3059 }
3060
3061 /* collect key */
3062 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3063 key->area[key->data] = '\0';
3064
3065 /* collect value */
3066 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3067 value->area[value->data] = '\0';
3068
3069 /* perform update */
Christopher Faulete84289e2019-04-19 14:50:55 +02003070 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003071 if (pat_ref_find_elt(ref, key->area) != NULL)
3072 /* update entry if it exists */
3073 pat_ref_set(ref, key->area, value->area, NULL);
3074 else
3075 /* insert a new entry */
3076 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02003077 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003078 free_trash_chunk(key);
3079 free_trash_chunk(value);
3080 break;
3081 }
3082
3083 case ACT_HTTP_EARLY_HINT:
3084 if (!(txn->req.flags & HTTP_MSGF_VER_11))
3085 break;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003086 early_hints = http_add_early_hint_header(s, early_hints,
3087 ist2(rule->arg.early_hint.name, rule->arg.early_hint.name_len),
3088 &rule->arg.early_hint.fmt);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003089 if (early_hints == -1) {
3090 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02003091 goto end;
3092 }
3093 break;
3094
3095 case ACT_CUSTOM:
3096 if ((s->req.flags & CF_READ_ERROR) ||
3097 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Christopher Faulet3e964192018-10-24 11:39:23 +02003098 (px->options & PR_O_ABRT_CLOSE)))
3099 act_flags |= ACT_FLAG_FINAL;
3100
3101 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
3102 case ACT_RET_ERR:
3103 case ACT_RET_CONT:
3104 break;
3105 case ACT_RET_STOP:
Christopher Faulet8f1aa772019-07-04 11:27:15 +02003106 rule_ret = HTTP_RULE_RES_STOP;
3107 goto end;
3108 case ACT_RET_DONE:
Christopher Faulet3e964192018-10-24 11:39:23 +02003109 rule_ret = HTTP_RULE_RES_DONE;
3110 goto end;
3111 case ACT_RET_YIELD:
3112 s->current_rule = rule;
3113 rule_ret = HTTP_RULE_RES_YIELD;
3114 goto end;
3115 }
3116 break;
3117
3118 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3119 /* Note: only the first valid tracking parameter of each
3120 * applies.
3121 */
3122
3123 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3124 struct stktable *t;
3125 struct stksess *ts;
3126 struct stktable_key *key;
3127 void *ptr1, *ptr2;
3128
3129 t = rule->arg.trk_ctr.table.t;
3130 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL,
3131 rule->arg.trk_ctr.expr, NULL);
3132
3133 if (key && (ts = stktable_get_entry(t, key))) {
3134 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3135
3136 /* let's count a new HTTP request as it's the first time we do it */
3137 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3138 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3139 if (ptr1 || ptr2) {
3140 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3141
3142 if (ptr1)
3143 stktable_data_cast(ptr1, http_req_cnt)++;
3144
3145 if (ptr2)
3146 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
3147 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3148
3149 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3150
3151 /* If data was modified, we need to touch to re-schedule sync */
3152 stktable_touch_local(t, ts, 0);
3153 }
3154
3155 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3156 if (sess->fe != s->be)
3157 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3158 }
3159 }
3160 break;
3161
Joseph Herlantc42c0e92018-11-25 10:43:27 -08003162 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003163 default:
3164 break;
3165 }
3166 }
3167
3168 end:
3169 if (early_hints) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003170 if (http_reply_103_early_hints(&s->res) == -1)
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003171 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02003172 }
3173
3174 /* we reached the end of the rules, nothing to report */
3175 return rule_ret;
3176}
3177
3178/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3179 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3180 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3181 * is returned, the process can continue the evaluation of next rule list. If
3182 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3183 * is returned, it means the operation could not be processed and a server error
3184 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3185 * deny rule. If *YIELD is returned, the caller must call again the function
3186 * with the same context.
3187 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003188static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
3189 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02003190{
3191 struct session *sess = strm_sess(s);
3192 struct http_txn *txn = s->txn;
3193 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02003194 struct act_rule *rule;
3195 struct http_hdr_ctx ctx;
3196 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
3197 int act_flags = 0;
3198
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003199 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02003200
3201 /* If "the current_rule_list" match the executed rule list, we are in
3202 * resume condition. If a resume is needed it is always in the action
3203 * and never in the ACL or converters. In this case, we initialise the
3204 * current rule, and go to the action execution point.
3205 */
3206 if (s->current_rule) {
3207 rule = s->current_rule;
3208 s->current_rule = NULL;
3209 if (s->current_rule_list == rules)
3210 goto resume_execution;
3211 }
3212 s->current_rule_list = rules;
3213
3214 list_for_each_entry(rule, rules, list) {
3215 /* check optional condition */
3216 if (rule->cond) {
3217 int ret;
3218
3219 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3220 ret = acl_pass(ret);
3221
3222 if (rule->cond->pol == ACL_COND_UNLESS)
3223 ret = !ret;
3224
3225 if (!ret) /* condition not matched */
3226 continue;
3227 }
3228
3229 act_flags |= ACT_FLAG_FIRST;
3230resume_execution:
3231 switch (rule->action) {
3232 case ACT_ACTION_ALLOW:
3233 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3234 goto end;
3235
3236 case ACT_ACTION_DENY:
3237 txn->flags |= TX_SVDENY;
3238 rule_ret = HTTP_RULE_RES_STOP;
3239 goto end;
3240
3241 case ACT_HTTP_SET_NICE:
3242 s->task->nice = rule->arg.nice;
3243 break;
3244
3245 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003246 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Christopher Faulet3e964192018-10-24 11:39:23 +02003247 break;
3248
3249 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003250 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Christopher Faulet3e964192018-10-24 11:39:23 +02003251 break;
3252
3253 case ACT_HTTP_SET_LOGL:
3254 s->logs.level = rule->arg.loglevel;
3255 break;
3256
3257 case ACT_HTTP_REPLACE_HDR:
3258 case ACT_HTTP_REPLACE_VAL:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003259 if (http_transform_header(s, &s->res, htx,
3260 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
3261 &rule->arg.hdr_add.fmt,
3262 rule->arg.hdr_add.re, rule->action)) {
Christopher Faulet3e964192018-10-24 11:39:23 +02003263 rule_ret = HTTP_RULE_RES_BADREQ;
3264 goto end;
3265 }
3266 break;
3267
3268 case ACT_HTTP_DEL_HDR:
3269 /* remove all occurrences of the header */
3270 ctx.blk = NULL;
3271 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3272 http_remove_header(htx, &ctx);
3273 break;
3274
3275 case ACT_HTTP_SET_HDR:
3276 case ACT_HTTP_ADD_HDR: {
3277 struct buffer *replace;
3278 struct ist n, v;
3279
3280 replace = alloc_trash_chunk();
3281 if (!replace) {
3282 rule_ret = HTTP_RULE_RES_BADREQ;
3283 goto end;
3284 }
3285
3286 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
3287 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3288 v = ist2(replace->area, replace->data);
3289
3290 if (rule->action == ACT_HTTP_SET_HDR) {
3291 /* remove all occurrences of the header */
3292 ctx.blk = NULL;
3293 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3294 http_remove_header(htx, &ctx);
3295 }
3296
3297 if (!http_add_header(htx, n, v)) {
3298 static unsigned char rate_limit = 0;
3299
3300 if ((rate_limit++ & 255) == 0) {
3301 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);
3302 }
3303
Olivier Houcharda798bf52019-03-08 18:52:00 +01003304 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003305 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003306 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003307 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003308 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003309 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01003310 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003311 }
3312 free_trash_chunk(replace);
3313 break;
3314 }
3315
3316 case ACT_HTTP_DEL_ACL:
3317 case ACT_HTTP_DEL_MAP: {
3318 struct pat_ref *ref;
3319 struct buffer *key;
3320
3321 /* collect reference */
3322 ref = pat_ref_lookup(rule->arg.map.ref);
3323 if (!ref)
3324 continue;
3325
3326 /* allocate key */
3327 key = alloc_trash_chunk();
3328 if (!key) {
3329 rule_ret = HTTP_RULE_RES_BADREQ;
3330 goto end;
3331 }
3332
3333 /* collect key */
3334 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3335 key->area[key->data] = '\0';
3336
3337 /* perform update */
3338 /* returned code: 1=ok, 0=ko */
3339 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3340 pat_ref_delete(ref, key->area);
3341 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3342
3343 free_trash_chunk(key);
3344 break;
3345 }
3346
3347 case ACT_HTTP_ADD_ACL: {
3348 struct pat_ref *ref;
3349 struct buffer *key;
3350
3351 /* collect reference */
3352 ref = pat_ref_lookup(rule->arg.map.ref);
3353 if (!ref)
3354 continue;
3355
3356 /* allocate key */
3357 key = alloc_trash_chunk();
3358 if (!key) {
3359 rule_ret = HTTP_RULE_RES_BADREQ;
3360 goto end;
3361 }
3362
3363 /* collect key */
3364 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3365 key->area[key->data] = '\0';
3366
3367 /* perform update */
3368 /* check if the entry already exists */
Christopher Faulete84289e2019-04-19 14:50:55 +02003369 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003370 if (pat_ref_find_elt(ref, key->area) == NULL)
3371 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02003372 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003373 free_trash_chunk(key);
3374 break;
3375 }
3376
3377 case ACT_HTTP_SET_MAP: {
3378 struct pat_ref *ref;
3379 struct buffer *key, *value;
3380
3381 /* collect reference */
3382 ref = pat_ref_lookup(rule->arg.map.ref);
3383 if (!ref)
3384 continue;
3385
3386 /* allocate key */
3387 key = alloc_trash_chunk();
3388 if (!key) {
3389 rule_ret = HTTP_RULE_RES_BADREQ;
3390 goto end;
3391 }
3392
3393 /* allocate value */
3394 value = alloc_trash_chunk();
3395 if (!value) {
3396 free_trash_chunk(key);
3397 rule_ret = HTTP_RULE_RES_BADREQ;
3398 goto end;
3399 }
3400
3401 /* collect key */
3402 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3403 key->area[key->data] = '\0';
3404
3405 /* collect value */
3406 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3407 value->area[value->data] = '\0';
3408
3409 /* perform update */
3410 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3411 if (pat_ref_find_elt(ref, key->area) != NULL)
3412 /* update entry if it exists */
3413 pat_ref_set(ref, key->area, value->area, NULL);
3414 else
3415 /* insert a new entry */
3416 pat_ref_add(ref, key->area, value->area, NULL);
3417 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3418 free_trash_chunk(key);
3419 free_trash_chunk(value);
3420 break;
3421 }
3422
3423 case ACT_HTTP_REDIR:
3424 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003425 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3e964192018-10-24 11:39:23 +02003426 rule_ret = HTTP_RULE_RES_BADREQ;
3427 goto end;
3428
3429 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3430 /* Note: only the first valid tracking parameter of each
3431 * applies.
3432 */
3433 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3434 struct stktable *t;
3435 struct stksess *ts;
3436 struct stktable_key *key;
3437 void *ptr;
3438
3439 t = rule->arg.trk_ctr.table.t;
3440 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL,
3441 rule->arg.trk_ctr.expr, NULL);
3442
3443 if (key && (ts = stktable_get_entry(t, key))) {
3444 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3445
3446 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3447
3448 /* let's count a new HTTP request as it's the first time we do it */
3449 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3450 if (ptr)
3451 stktable_data_cast(ptr, http_req_cnt)++;
3452
3453 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3454 if (ptr)
3455 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3456 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3457
3458 /* When the client triggers a 4xx from the server, it's most often due
3459 * to a missing object or permission. These events should be tracked
3460 * because if they happen often, it may indicate a brute force or a
3461 * vulnerability scan. Normally this is done when receiving the response
3462 * but here we're tracking after this ought to have been done so we have
3463 * to do it on purpose.
3464 */
3465 if ((unsigned)(txn->status - 400) < 100) {
3466 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3467 if (ptr)
3468 stktable_data_cast(ptr, http_err_cnt)++;
3469
3470 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3471 if (ptr)
3472 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3473 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3474 }
3475
3476 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3477
3478 /* If data was modified, we need to touch to re-schedule sync */
3479 stktable_touch_local(t, ts, 0);
3480
3481 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3482 if (sess->fe != s->be)
3483 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3484 }
3485 }
3486 break;
3487
3488 case ACT_CUSTOM:
3489 if ((s->req.flags & CF_READ_ERROR) ||
3490 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Christopher Faulet3e964192018-10-24 11:39:23 +02003491 (px->options & PR_O_ABRT_CLOSE)))
3492 act_flags |= ACT_FLAG_FINAL;
3493
3494 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
3495 case ACT_RET_ERR:
3496 case ACT_RET_CONT:
3497 break;
3498 case ACT_RET_STOP:
3499 rule_ret = HTTP_RULE_RES_STOP;
3500 goto end;
Christopher Faulet8f1aa772019-07-04 11:27:15 +02003501 case ACT_RET_DONE:
3502 rule_ret = HTTP_RULE_RES_DONE;
3503 goto end;
Christopher Faulet3e964192018-10-24 11:39:23 +02003504 case ACT_RET_YIELD:
3505 s->current_rule = rule;
3506 rule_ret = HTTP_RULE_RES_YIELD;
3507 goto end;
3508 }
3509 break;
3510
Joseph Herlantc42c0e92018-11-25 10:43:27 -08003511 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003512 default:
3513 break;
3514 }
3515 }
3516
3517 end:
3518 /* we reached the end of the rules, nothing to report */
3519 return rule_ret;
3520}
3521
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003522/*
3523 * Manage client-side cookie. It can impact performance by about 2% so it is
3524 * desirable to call it only when needed. This code is quite complex because
3525 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3526 * highly recommended not to touch this part without a good reason !
3527 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003528static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003529{
3530 struct session *sess = s->sess;
3531 struct http_txn *txn = s->txn;
3532 struct htx *htx;
3533 struct http_hdr_ctx ctx;
3534 char *hdr_beg, *hdr_end, *del_from;
3535 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3536 int preserve_hdr;
3537
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003538 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003539 ctx.blk = NULL;
3540 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003541 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003542 del_from = NULL; /* nothing to be deleted */
3543 preserve_hdr = 0; /* assume we may kill the whole header */
3544
3545 /* Now look for cookies. Conforming to RFC2109, we have to support
3546 * attributes whose name begin with a '$', and associate them with
3547 * the right cookie, if we want to delete this cookie.
3548 * So there are 3 cases for each cookie read :
3549 * 1) it's a special attribute, beginning with a '$' : ignore it.
3550 * 2) it's a server id cookie that we *MAY* want to delete : save
3551 * some pointers on it (last semi-colon, beginning of cookie...)
3552 * 3) it's an application cookie : we *MAY* have to delete a previous
3553 * "special" cookie.
3554 * At the end of loop, if a "special" cookie remains, we may have to
3555 * remove it. If no application cookie persists in the header, we
3556 * *MUST* delete it.
3557 *
3558 * Note: RFC2965 is unclear about the processing of spaces around
3559 * the equal sign in the ATTR=VALUE form. A careful inspection of
3560 * the RFC explicitly allows spaces before it, and not within the
3561 * tokens (attrs or values). An inspection of RFC2109 allows that
3562 * too but section 10.1.3 lets one think that spaces may be allowed
3563 * after the equal sign too, resulting in some (rare) buggy
3564 * implementations trying to do that. So let's do what servers do.
3565 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3566 * allowed quoted strings in values, with any possible character
3567 * after a backslash, including control chars and delimitors, which
3568 * causes parsing to become ambiguous. Browsers also allow spaces
3569 * within values even without quotes.
3570 *
3571 * We have to keep multiple pointers in order to support cookie
3572 * removal at the beginning, middle or end of header without
3573 * corrupting the header. All of these headers are valid :
3574 *
3575 * hdr_beg hdr_end
3576 * | |
3577 * v |
3578 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3579 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3580 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3581 * | | | | | | |
3582 * | | | | | | |
3583 * | | | | | | +--> next
3584 * | | | | | +----> val_end
3585 * | | | | +-----------> val_beg
3586 * | | | +--------------> equal
3587 * | | +----------------> att_end
3588 * | +---------------------> att_beg
3589 * +--------------------------> prev
3590 *
3591 */
3592 hdr_beg = ctx.value.ptr;
3593 hdr_end = hdr_beg + ctx.value.len;
3594 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3595 /* Iterate through all cookies on this line */
3596
3597 /* find att_beg */
3598 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003599 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003600 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003601 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003602
3603 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3604 att_beg++;
3605
3606 /* find att_end : this is the first character after the last non
3607 * space before the equal. It may be equal to hdr_end.
3608 */
3609 equal = att_end = att_beg;
3610 while (equal < hdr_end) {
3611 if (*equal == '=' || *equal == ',' || *equal == ';')
3612 break;
3613 if (HTTP_IS_SPHT(*equal++))
3614 continue;
3615 att_end = equal;
3616 }
3617
3618 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3619 * is between <att_beg> and <equal>, both may be identical.
3620 */
3621 /* look for end of cookie if there is an equal sign */
3622 if (equal < hdr_end && *equal == '=') {
3623 /* look for the beginning of the value */
3624 val_beg = equal + 1;
3625 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3626 val_beg++;
3627
3628 /* find the end of the value, respecting quotes */
3629 next = http_find_cookie_value_end(val_beg, hdr_end);
3630
3631 /* make val_end point to the first white space or delimitor after the value */
3632 val_end = next;
3633 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3634 val_end--;
3635 }
3636 else
3637 val_beg = val_end = next = equal;
3638
3639 /* We have nothing to do with attributes beginning with
3640 * '$'. However, they will automatically be removed if a
3641 * header before them is removed, since they're supposed
3642 * to be linked together.
3643 */
3644 if (*att_beg == '$')
3645 continue;
3646
3647 /* Ignore cookies with no equal sign */
3648 if (equal == next) {
3649 /* This is not our cookie, so we must preserve it. But if we already
3650 * scheduled another cookie for removal, we cannot remove the
3651 * complete header, but we can remove the previous block itself.
3652 */
3653 preserve_hdr = 1;
3654 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003655 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003656 val_end += delta;
3657 next += delta;
3658 hdr_end += delta;
3659 prev = del_from;
3660 del_from = NULL;
3661 }
3662 continue;
3663 }
3664
3665 /* if there are spaces around the equal sign, we need to
3666 * strip them otherwise we'll get trouble for cookie captures,
3667 * or even for rewrites. Since this happens extremely rarely,
3668 * it does not hurt performance.
3669 */
3670 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3671 int stripped_before = 0;
3672 int stripped_after = 0;
3673
3674 if (att_end != equal) {
3675 memmove(att_end, equal, hdr_end - equal);
3676 stripped_before = (att_end - equal);
3677 equal += stripped_before;
3678 val_beg += stripped_before;
3679 }
3680
3681 if (val_beg > equal + 1) {
3682 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3683 stripped_after = (equal + 1) - val_beg;
3684 val_beg += stripped_after;
3685 stripped_before += stripped_after;
3686 }
3687
3688 val_end += stripped_before;
3689 next += stripped_before;
3690 hdr_end += stripped_before;
3691 }
3692 /* now everything is as on the diagram above */
3693
3694 /* First, let's see if we want to capture this cookie. We check
3695 * that we don't already have a client side cookie, because we
3696 * can only capture one. Also as an optimisation, we ignore
3697 * cookies shorter than the declared name.
3698 */
3699 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3700 (val_end - att_beg >= sess->fe->capture_namelen) &&
3701 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3702 int log_len = val_end - att_beg;
3703
3704 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3705 ha_alert("HTTP logging : out of memory.\n");
3706 } else {
3707 if (log_len > sess->fe->capture_len)
3708 log_len = sess->fe->capture_len;
3709 memcpy(txn->cli_cookie, att_beg, log_len);
3710 txn->cli_cookie[log_len] = 0;
3711 }
3712 }
3713
3714 /* Persistence cookies in passive, rewrite or insert mode have the
3715 * following form :
3716 *
3717 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3718 *
3719 * For cookies in prefix mode, the form is :
3720 *
3721 * Cookie: NAME=SRV~VALUE
3722 */
3723 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3724 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3725 struct server *srv = s->be->srv;
3726 char *delim;
3727
3728 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3729 * have the server ID between val_beg and delim, and the original cookie between
3730 * delim+1 and val_end. Otherwise, delim==val_end :
3731 *
3732 * hdr_beg
3733 * |
3734 * v
3735 * NAME=SRV; # in all but prefix modes
3736 * NAME=SRV~OPAQUE ; # in prefix mode
3737 * || || | |+-> next
3738 * || || | +--> val_end
3739 * || || +---------> delim
3740 * || |+------------> val_beg
3741 * || +-------------> att_end = equal
3742 * |+-----------------> att_beg
3743 * +------------------> prev
3744 *
3745 */
3746 if (s->be->ck_opts & PR_CK_PFX) {
3747 for (delim = val_beg; delim < val_end; delim++)
3748 if (*delim == COOKIE_DELIM)
3749 break;
3750 }
3751 else {
3752 char *vbar1;
3753 delim = val_end;
3754 /* Now check if the cookie contains a date field, which would
3755 * appear after a vertical bar ('|') just after the server name
3756 * and before the delimiter.
3757 */
3758 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3759 if (vbar1) {
3760 /* OK, so left of the bar is the server's cookie and
3761 * right is the last seen date. It is a base64 encoded
3762 * 30-bit value representing the UNIX date since the
3763 * epoch in 4-second quantities.
3764 */
3765 int val;
3766 delim = vbar1++;
3767 if (val_end - vbar1 >= 5) {
3768 val = b64tos30(vbar1);
3769 if (val > 0)
3770 txn->cookie_last_date = val << 2;
3771 }
3772 /* look for a second vertical bar */
3773 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3774 if (vbar1 && (val_end - vbar1 > 5)) {
3775 val = b64tos30(vbar1 + 1);
3776 if (val > 0)
3777 txn->cookie_first_date = val << 2;
3778 }
3779 }
3780 }
3781
3782 /* if the cookie has an expiration date and the proxy wants to check
3783 * it, then we do that now. We first check if the cookie is too old,
3784 * then only if it has expired. We detect strict overflow because the
3785 * time resolution here is not great (4 seconds). Cookies with dates
3786 * in the future are ignored if their offset is beyond one day. This
3787 * allows an admin to fix timezone issues without expiring everyone
3788 * and at the same time avoids keeping unwanted side effects for too
3789 * long.
3790 */
3791 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3792 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3793 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3794 txn->flags &= ~TX_CK_MASK;
3795 txn->flags |= TX_CK_OLD;
3796 delim = val_beg; // let's pretend we have not found the cookie
3797 txn->cookie_first_date = 0;
3798 txn->cookie_last_date = 0;
3799 }
3800 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3801 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3802 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3803 txn->flags &= ~TX_CK_MASK;
3804 txn->flags |= TX_CK_EXPIRED;
3805 delim = val_beg; // let's pretend we have not found the cookie
3806 txn->cookie_first_date = 0;
3807 txn->cookie_last_date = 0;
3808 }
3809
3810 /* Here, we'll look for the first running server which supports the cookie.
3811 * This allows to share a same cookie between several servers, for example
3812 * to dedicate backup servers to specific servers only.
3813 * However, to prevent clients from sticking to cookie-less backup server
3814 * when they have incidentely learned an empty cookie, we simply ignore
3815 * empty cookies and mark them as invalid.
3816 * The same behaviour is applied when persistence must be ignored.
3817 */
3818 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3819 srv = NULL;
3820
3821 while (srv) {
3822 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3823 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3824 if ((srv->cur_state != SRV_ST_STOPPED) ||
3825 (s->be->options & PR_O_PERSIST) ||
3826 (s->flags & SF_FORCE_PRST)) {
3827 /* we found the server and we can use it */
3828 txn->flags &= ~TX_CK_MASK;
3829 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3830 s->flags |= SF_DIRECT | SF_ASSIGNED;
3831 s->target = &srv->obj_type;
3832 break;
3833 } else {
3834 /* we found a server, but it's down,
3835 * mark it as such and go on in case
3836 * another one is available.
3837 */
3838 txn->flags &= ~TX_CK_MASK;
3839 txn->flags |= TX_CK_DOWN;
3840 }
3841 }
3842 srv = srv->next;
3843 }
3844
3845 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3846 /* no server matched this cookie or we deliberately skipped it */
3847 txn->flags &= ~TX_CK_MASK;
3848 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3849 txn->flags |= TX_CK_UNUSED;
3850 else
3851 txn->flags |= TX_CK_INVALID;
3852 }
3853
3854 /* depending on the cookie mode, we may have to either :
3855 * - delete the complete cookie if we're in insert+indirect mode, so that
3856 * the server never sees it ;
3857 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003858 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003859 * if we're in cookie prefix mode
3860 */
3861 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3862 int delta; /* negative */
3863
3864 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3865 delta = val_beg - (delim + 1);
3866 val_end += delta;
3867 next += delta;
3868 hdr_end += delta;
3869 del_from = NULL;
3870 preserve_hdr = 1; /* we want to keep this cookie */
3871 }
3872 else if (del_from == NULL &&
3873 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3874 del_from = prev;
3875 }
3876 }
3877 else {
3878 /* This is not our cookie, so we must preserve it. But if we already
3879 * scheduled another cookie for removal, we cannot remove the
3880 * complete header, but we can remove the previous block itself.
3881 */
3882 preserve_hdr = 1;
3883
3884 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003885 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003886 if (att_beg >= del_from)
3887 att_beg += delta;
3888 if (att_end >= del_from)
3889 att_end += delta;
3890 val_beg += delta;
3891 val_end += delta;
3892 next += delta;
3893 hdr_end += delta;
3894 prev = del_from;
3895 del_from = NULL;
3896 }
3897 }
3898
3899 /* continue with next cookie on this header line */
3900 att_beg = next;
3901 } /* for each cookie */
3902
3903
3904 /* There are no more cookies on this line.
3905 * We may still have one (or several) marked for deletion at the
3906 * end of the line. We must do this now in two ways :
3907 * - if some cookies must be preserved, we only delete from the
3908 * mark to the end of line ;
3909 * - if nothing needs to be preserved, simply delete the whole header
3910 */
3911 if (del_from) {
3912 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3913 }
3914 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003915 if (hdr_beg != hdr_end)
3916 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003917 else
3918 http_remove_header(htx, &ctx);
3919 }
3920 } /* for each "Cookie header */
3921}
3922
3923/*
3924 * Manage server-side cookies. It can impact performance by about 2% so it is
3925 * desirable to call it only when needed. This function is also used when we
3926 * just need to know if there is a cookie (eg: for check-cache).
3927 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003928static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003929{
3930 struct session *sess = s->sess;
3931 struct http_txn *txn = s->txn;
3932 struct htx *htx;
3933 struct http_hdr_ctx ctx;
3934 struct server *srv;
3935 char *hdr_beg, *hdr_end;
3936 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003937 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003938
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003939 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003940
3941 ctx.blk = NULL;
3942 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003943 int is_first = 1;
3944
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003945 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3946 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3947 break;
3948 is_cookie2 = 1;
3949 }
3950
3951 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3952 * <prev> points to the colon.
3953 */
3954 txn->flags |= TX_SCK_PRESENT;
3955
3956 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3957 * check-cache is enabled) and we are not interested in checking
3958 * them. Warning, the cookie capture is declared in the frontend.
3959 */
3960 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3961 break;
3962
3963 /* OK so now we know we have to process this response cookie.
3964 * The format of the Set-Cookie header is slightly different
3965 * from the format of the Cookie header in that it does not
3966 * support the comma as a cookie delimiter (thus the header
3967 * cannot be folded) because the Expires attribute described in
3968 * the original Netscape's spec may contain an unquoted date
3969 * with a comma inside. We have to live with this because
3970 * many browsers don't support Max-Age and some browsers don't
3971 * support quoted strings. However the Set-Cookie2 header is
3972 * clean.
3973 *
3974 * We have to keep multiple pointers in order to support cookie
3975 * removal at the beginning, middle or end of header without
3976 * corrupting the header (in case of set-cookie2). A special
3977 * pointer, <scav> points to the beginning of the set-cookie-av
3978 * fields after the first semi-colon. The <next> pointer points
3979 * either to the end of line (set-cookie) or next unquoted comma
3980 * (set-cookie2). All of these headers are valid :
3981 *
3982 * hdr_beg hdr_end
3983 * | |
3984 * v |
3985 * NAME1 = VALUE 1 ; Secure; Path="/" |
3986 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3987 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3988 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3989 * | | | | | | | |
3990 * | | | | | | | +-> next
3991 * | | | | | | +------------> scav
3992 * | | | | | +--------------> val_end
3993 * | | | | +--------------------> val_beg
3994 * | | | +----------------------> equal
3995 * | | +------------------------> att_end
3996 * | +----------------------------> att_beg
3997 * +------------------------------> prev
3998 * -------------------------------> hdr_beg
3999 */
4000 hdr_beg = ctx.value.ptr;
4001 hdr_end = hdr_beg + ctx.value.len;
4002 for (prev = hdr_beg; prev < hdr_end; prev = next) {
4003
4004 /* Iterate through all cookies on this line */
4005
4006 /* find att_beg */
4007 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02004008 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004009 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02004010 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004011
4012 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
4013 att_beg++;
4014
4015 /* find att_end : this is the first character after the last non
4016 * space before the equal. It may be equal to hdr_end.
4017 */
4018 equal = att_end = att_beg;
4019
4020 while (equal < hdr_end) {
4021 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
4022 break;
4023 if (HTTP_IS_SPHT(*equal++))
4024 continue;
4025 att_end = equal;
4026 }
4027
4028 /* here, <equal> points to '=', a delimitor or the end. <att_end>
4029 * is between <att_beg> and <equal>, both may be identical.
4030 */
4031
4032 /* look for end of cookie if there is an equal sign */
4033 if (equal < hdr_end && *equal == '=') {
4034 /* look for the beginning of the value */
4035 val_beg = equal + 1;
4036 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
4037 val_beg++;
4038
4039 /* find the end of the value, respecting quotes */
4040 next = http_find_cookie_value_end(val_beg, hdr_end);
4041
4042 /* make val_end point to the first white space or delimitor after the value */
4043 val_end = next;
4044 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
4045 val_end--;
4046 }
4047 else {
4048 /* <equal> points to next comma, semi-colon or EOL */
4049 val_beg = val_end = next = equal;
4050 }
4051
4052 if (next < hdr_end) {
4053 /* Set-Cookie2 supports multiple cookies, and <next> points to
4054 * a colon or semi-colon before the end. So skip all attr-value
4055 * pairs and look for the next comma. For Set-Cookie, since
4056 * commas are permitted in values, skip to the end.
4057 */
4058 if (is_cookie2)
4059 next = http_find_hdr_value_end(next, hdr_end);
4060 else
4061 next = hdr_end;
4062 }
4063
4064 /* Now everything is as on the diagram above */
4065
4066 /* Ignore cookies with no equal sign */
4067 if (equal == val_end)
4068 continue;
4069
4070 /* If there are spaces around the equal sign, we need to
4071 * strip them otherwise we'll get trouble for cookie captures,
4072 * or even for rewrites. Since this happens extremely rarely,
4073 * it does not hurt performance.
4074 */
4075 if (unlikely(att_end != equal || val_beg > equal + 1)) {
4076 int stripped_before = 0;
4077 int stripped_after = 0;
4078
4079 if (att_end != equal) {
4080 memmove(att_end, equal, hdr_end - equal);
4081 stripped_before = (att_end - equal);
4082 equal += stripped_before;
4083 val_beg += stripped_before;
4084 }
4085
4086 if (val_beg > equal + 1) {
4087 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
4088 stripped_after = (equal + 1) - val_beg;
4089 val_beg += stripped_after;
4090 stripped_before += stripped_after;
4091 }
4092
4093 val_end += stripped_before;
4094 next += stripped_before;
4095 hdr_end += stripped_before;
4096
Christopher Faulet3e2638e2019-06-18 09:49:16 +02004097 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004098 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004099 }
4100
4101 /* First, let's see if we want to capture this cookie. We check
4102 * that we don't already have a server side cookie, because we
4103 * can only capture one. Also as an optimisation, we ignore
4104 * cookies shorter than the declared name.
4105 */
4106 if (sess->fe->capture_name != NULL &&
4107 txn->srv_cookie == NULL &&
4108 (val_end - att_beg >= sess->fe->capture_namelen) &&
4109 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
4110 int log_len = val_end - att_beg;
4111 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
4112 ha_alert("HTTP logging : out of memory.\n");
4113 }
4114 else {
4115 if (log_len > sess->fe->capture_len)
4116 log_len = sess->fe->capture_len;
4117 memcpy(txn->srv_cookie, att_beg, log_len);
4118 txn->srv_cookie[log_len] = 0;
4119 }
4120 }
4121
4122 srv = objt_server(s->target);
4123 /* now check if we need to process it for persistence */
4124 if (!(s->flags & SF_IGNORE_PRST) &&
4125 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
4126 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
4127 /* assume passive cookie by default */
4128 txn->flags &= ~TX_SCK_MASK;
4129 txn->flags |= TX_SCK_FOUND;
4130
4131 /* If the cookie is in insert mode on a known server, we'll delete
4132 * this occurrence because we'll insert another one later.
4133 * We'll delete it too if the "indirect" option is set and we're in
4134 * a direct access.
4135 */
4136 if (s->be->ck_opts & PR_CK_PSV) {
4137 /* The "preserve" flag was set, we don't want to touch the
4138 * server's cookie.
4139 */
4140 }
4141 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
4142 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
4143 /* this cookie must be deleted */
4144 if (prev == hdr_beg && next == hdr_end) {
4145 /* whole header */
4146 http_remove_header(htx, &ctx);
4147 /* note: while both invalid now, <next> and <hdr_end>
4148 * are still equal, so the for() will stop as expected.
4149 */
4150 } else {
4151 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004152 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004153 next = prev;
4154 hdr_end += delta;
4155 }
4156 txn->flags &= ~TX_SCK_MASK;
4157 txn->flags |= TX_SCK_DELETED;
4158 /* and go on with next cookie */
4159 }
4160 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
4161 /* replace bytes val_beg->val_end with the cookie name associated
4162 * with this server since we know it.
4163 */
4164 int sliding, delta;
4165
4166 ctx.value = ist2(val_beg, val_end - val_beg);
4167 ctx.lws_before = ctx.lws_after = 0;
4168 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
4169 delta = srv->cklen - (val_end - val_beg);
4170 sliding = (ctx.value.ptr - val_beg);
4171 hdr_beg += sliding;
4172 val_beg += sliding;
4173 next += sliding + delta;
4174 hdr_end += sliding + delta;
4175
4176 txn->flags &= ~TX_SCK_MASK;
4177 txn->flags |= TX_SCK_REPLACED;
4178 }
4179 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
4180 /* insert the cookie name associated with this server
4181 * before existing cookie, and insert a delimiter between them..
4182 */
4183 int sliding, delta;
4184 ctx.value = ist2(val_beg, 0);
4185 ctx.lws_before = ctx.lws_after = 0;
4186 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
4187 delta = srv->cklen + 1;
4188 sliding = (ctx.value.ptr - val_beg);
4189 hdr_beg += sliding;
4190 val_beg += sliding;
4191 next += sliding + delta;
4192 hdr_end += sliding + delta;
4193
4194 val_beg[srv->cklen] = COOKIE_DELIM;
4195 txn->flags &= ~TX_SCK_MASK;
4196 txn->flags |= TX_SCK_REPLACED;
4197 }
4198 }
4199 /* that's done for this cookie, check the next one on the same
4200 * line when next != hdr_end (only if is_cookie2).
4201 */
4202 }
4203 }
4204}
4205
Christopher Faulet25a02f62018-10-24 12:00:25 +02004206/*
4207 * Parses the Cache-Control and Pragma request header fields to determine if
4208 * the request may be served from the cache and/or if it is cacheable. Updates
4209 * s->txn->flags.
4210 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004211void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004212{
4213 struct http_txn *txn = s->txn;
4214 struct htx *htx;
4215 int32_t pos;
4216 int pragma_found, cc_found, i;
4217
4218 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
4219 return; /* nothing more to do here */
4220
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004221 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02004222 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02004223 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004224 struct htx_blk *blk = htx_get_blk(htx, pos);
4225 enum htx_blk_type type = htx_get_blk_type(blk);
4226 struct ist n, v;
4227
4228 if (type == HTX_BLK_EOH)
4229 break;
4230 if (type != HTX_BLK_HDR)
4231 continue;
4232
4233 n = htx_get_blk_name(htx, blk);
4234 v = htx_get_blk_value(htx, blk);
4235
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004236 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004237 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4238 pragma_found = 1;
4239 continue;
4240 }
4241 }
4242
4243 /* Don't use the cache and don't try to store if we found the
4244 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004245 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004246 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4247 txn->flags |= TX_CACHE_IGNORE;
4248 continue;
4249 }
4250
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004251 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004252 continue;
4253
4254 /* OK, right now we know we have a cache-control header */
4255 cc_found = 1;
4256 if (!v.len) /* no info */
4257 continue;
4258
4259 i = 0;
4260 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4261 !isspace((unsigned char)*(v.ptr+i)))
4262 i++;
4263
4264 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
4265 * values after max-age, max-stale nor min-fresh, we simply don't
4266 * use the cache when they're specified.
4267 */
4268 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
4269 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4270 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
4271 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
4272 txn->flags |= TX_CACHE_IGNORE;
4273 continue;
4274 }
4275
4276 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
4277 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4278 continue;
4279 }
4280 }
4281
4282 /* RFC7234#5.4:
4283 * When the Cache-Control header field is also present and
4284 * understood in a request, Pragma is ignored.
4285 * When the Cache-Control header field is not present in a
4286 * request, caches MUST consider the no-cache request
4287 * pragma-directive as having the same effect as if
4288 * "Cache-Control: no-cache" were present.
4289 */
4290 if (!cc_found && pragma_found)
4291 txn->flags |= TX_CACHE_IGNORE;
4292}
4293
4294/*
4295 * Check if response is cacheable or not. Updates s->txn->flags.
4296 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004297void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004298{
4299 struct http_txn *txn = s->txn;
4300 struct htx *htx;
4301 int32_t pos;
4302 int i;
4303
4304 if (txn->status < 200) {
4305 /* do not try to cache interim responses! */
4306 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4307 return;
4308 }
4309
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004310 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004311 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004312 struct htx_blk *blk = htx_get_blk(htx, pos);
4313 enum htx_blk_type type = htx_get_blk_type(blk);
4314 struct ist n, v;
4315
4316 if (type == HTX_BLK_EOH)
4317 break;
4318 if (type != HTX_BLK_HDR)
4319 continue;
4320
4321 n = htx_get_blk_name(htx, blk);
4322 v = htx_get_blk_value(htx, blk);
4323
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004324 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004325 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4326 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4327 return;
4328 }
4329 }
4330
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004331 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004332 continue;
4333
4334 /* OK, right now we know we have a cache-control header */
4335 if (!v.len) /* no info */
4336 continue;
4337
4338 i = 0;
4339 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4340 !isspace((unsigned char)*(v.ptr+i)))
4341 i++;
4342
4343 /* we have a complete value between v.ptr and (v.ptr+i) */
4344 if (i < v.len && *(v.ptr + i) == '=') {
4345 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
4346 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
4347 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4348 continue;
4349 }
4350
4351 /* we have something of the form no-cache="set-cookie" */
4352 if ((v.len >= 21) &&
4353 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
4354 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
4355 txn->flags &= ~TX_CACHE_COOK;
4356 continue;
4357 }
4358
4359 /* OK, so we know that either p2 points to the end of string or to a comma */
4360 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
4361 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4362 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
4363 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4364 return;
4365 }
4366
4367 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4368 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4369 continue;
4370 }
4371 }
4372}
4373
Christopher Faulet377c5a52018-10-24 21:21:30 +02004374/*
4375 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4376 * for the current backend.
4377 *
4378 * It is assumed that the request is either a HEAD, GET, or POST and that the
4379 * uri_auth field is valid.
4380 *
4381 * Returns 1 if stats should be provided, otherwise 0.
4382 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004383static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004384{
4385 struct uri_auth *uri_auth = backend->uri_auth;
4386 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004387 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004388 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004389
4390 if (!uri_auth)
4391 return 0;
4392
4393 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4394 return 0;
4395
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004396 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004397 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004398 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01004399 if (*uri_auth->uri_prefix == '/')
4400 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004401
4402 /* check URI size */
4403 if (uri_auth->uri_len > uri.len)
4404 return 0;
4405
4406 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4407 return 0;
4408
4409 return 1;
4410}
4411
4412/* This function prepares an applet to handle the stats. It can deal with the
4413 * "100-continue" expectation, check that admin rules are met for POST requests,
4414 * and program a response message if something was unexpected. It cannot fail
4415 * and always relies on the stats applet to complete the job. It does not touch
4416 * analysers nor counters, which are left to the caller. It does not touch
4417 * s->target which is supposed to already point to the stats applet. The caller
4418 * is expected to have already assigned an appctx to the stream.
4419 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004420static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004421{
4422 struct stats_admin_rule *stats_admin_rule;
4423 struct stream_interface *si = &s->si[1];
4424 struct session *sess = s->sess;
4425 struct http_txn *txn = s->txn;
4426 struct http_msg *msg = &txn->req;
4427 struct uri_auth *uri_auth = s->be->uri_auth;
4428 const char *h, *lookup, *end;
4429 struct appctx *appctx;
4430 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004431 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004432
4433 appctx = si_appctx(si);
4434 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4435 appctx->st1 = appctx->st2 = 0;
4436 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004437 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004438 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4439 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4440 appctx->ctx.stats.flags |= STAT_CHUNKED;
4441
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004442 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004443 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004444 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4445 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004446
4447 for (h = lookup; h <= end - 3; h++) {
4448 if (memcmp(h, ";up", 3) == 0) {
4449 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4450 break;
4451 }
4452 }
4453
4454 if (uri_auth->refresh) {
4455 for (h = lookup; h <= end - 10; h++) {
4456 if (memcmp(h, ";norefresh", 10) == 0) {
4457 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4458 break;
4459 }
4460 }
4461 }
4462
4463 for (h = lookup; h <= end - 4; h++) {
4464 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004465 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004466 break;
4467 }
4468 }
4469
4470 for (h = lookup; h <= end - 6; h++) {
4471 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004472 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004473 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4474 break;
4475 }
4476 }
4477
Christopher Faulet6338a082019-09-09 15:50:54 +02004478 for (h = lookup; h <= end - 5; h++) {
4479 if (memcmp(h, ";json", 5) == 0) {
4480 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4481 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4482 break;
4483 }
4484 }
4485
4486 for (h = lookup; h <= end - 12; h++) {
4487 if (memcmp(h, ";json-schema", 12) == 0) {
4488 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4489 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4490 break;
4491 }
4492 }
4493
Christopher Faulet377c5a52018-10-24 21:21:30 +02004494 for (h = lookup; h <= end - 8; h++) {
4495 if (memcmp(h, ";st=", 4) == 0) {
4496 int i;
4497 h += 4;
4498 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4499 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4500 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4501 appctx->ctx.stats.st_code = i;
4502 break;
4503 }
4504 }
4505 break;
4506 }
4507 }
4508
4509 appctx->ctx.stats.scope_str = 0;
4510 appctx->ctx.stats.scope_len = 0;
4511 for (h = lookup; h <= end - 8; h++) {
4512 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4513 int itx = 0;
4514 const char *h2;
4515 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4516 const char *err;
4517
4518 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4519 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004520 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4521 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004522 if (*h == ';' || *h == '&' || *h == ' ')
4523 break;
4524 itx++;
4525 h++;
4526 }
4527
4528 if (itx > STAT_SCOPE_TXT_MAXLEN)
4529 itx = STAT_SCOPE_TXT_MAXLEN;
4530 appctx->ctx.stats.scope_len = itx;
4531
4532 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4533 memcpy(scope_txt, h2, itx);
4534 scope_txt[itx] = '\0';
4535 err = invalid_char(scope_txt);
4536 if (err) {
4537 /* bad char in search text => clear scope */
4538 appctx->ctx.stats.scope_str = 0;
4539 appctx->ctx.stats.scope_len = 0;
4540 }
4541 break;
4542 }
4543 }
4544
4545 /* now check whether we have some admin rules for this request */
4546 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4547 int ret = 1;
4548
4549 if (stats_admin_rule->cond) {
4550 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4551 ret = acl_pass(ret);
4552 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4553 ret = !ret;
4554 }
4555
4556 if (ret) {
4557 /* no rule, or the rule matches */
4558 appctx->ctx.stats.flags |= STAT_ADMIN;
4559 break;
4560 }
4561 }
4562
Christopher Faulet5d45e382019-02-27 15:15:23 +01004563 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4564 appctx->st0 = STAT_HTTP_HEAD;
4565 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004566 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004567 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004568 if (msg->msg_state < HTTP_MSG_DATA)
4569 req->analysers |= AN_REQ_HTTP_BODY;
4570 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004571 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004572 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004573 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4574 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4575 appctx->st0 = STAT_HTTP_LAST;
4576 }
4577 }
4578 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004579 /* Unsupported method */
4580 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4581 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4582 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004583 }
4584
4585 s->task->nice = -32; /* small boost for HTTP statistics */
4586 return 1;
4587}
4588
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004589void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004590{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004591 struct channel *req = &s->req;
4592 struct channel *res = &s->res;
4593 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004594 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004595 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004596 struct ist path, location;
4597 unsigned int flags;
4598 size_t data;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004599
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004600 /*
4601 * Create the location
4602 */
4603 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004604
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004605 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004606 /* special prefix "/" means don't change URL */
4607 srv = __objt_server(s->target);
4608 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4609 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4610 return;
4611 }
4612
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004613 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004614 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004615 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004616 path = http_get_path(htx_sl_req_uri(sl));
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004617 if (!path.ptr)
4618 return;
4619
4620 if (!chunk_memcat(&trash, path.ptr, path.len))
4621 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004622 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004623
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004624 /*
4625 * Create the 302 respone
4626 */
4627 htx = htx_from_buf(&res->buf);
4628 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4629 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4630 ist("HTTP/1.1"), ist("302"), ist("Found"));
4631 if (!sl)
4632 goto fail;
4633 sl->info.res.status = 302;
4634 s->txn->status = 302;
4635
4636 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4637 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4638 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4639 !htx_add_header(htx, ist("Location"), location))
4640 goto fail;
4641
4642 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4643 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004644
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004645 /*
4646 * Send the message
4647 */
4648 data = htx->data - co_data(res);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004649 c_adv(res, data);
4650 res->total += data;
4651
4652 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004653 si_shutr(si);
4654 si_shutw(si);
4655 si->err_type = SI_ET_NONE;
4656 si->state = SI_ST_CLO;
4657
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004658 channel_auto_read(req);
4659 channel_abort(req);
4660 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004661 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004662 channel_auto_read(res);
4663 channel_auto_close(res);
4664
4665 if (!(s->flags & SF_ERR_MASK))
4666 s->flags |= SF_ERR_LOCAL;
4667 if (!(s->flags & SF_FINST_MASK))
4668 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004669
4670 /* FIXME: we should increase a counter of redirects per server and per backend. */
4671 srv_inc_sess_ctr(srv);
4672 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004673 return;
4674
4675 fail:
4676 /* If an error occurred, remove the incomplete HTTP response from the
4677 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004678 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004679}
4680
Christopher Fauletf2824e62018-10-01 12:12:37 +02004681/* This function terminates the request because it was completly analyzed or
4682 * because an error was triggered during the body forwarding.
4683 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004684static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004685{
4686 struct channel *chn = &s->req;
4687 struct http_txn *txn = s->txn;
4688
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004689 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004690
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004691 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4692 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004693 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004694 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004695 goto end;
4696 }
4697
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004698 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4699 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004700 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004701 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004702
4703 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004704 /* No need to read anymore, the request was completely parsed.
4705 * We can shut the read side unless we want to abort_on_close,
4706 * or we have a POST request. The issue with POST requests is
4707 * that some browsers still send a CRLF after the request, and
4708 * this CRLF must be read so that it does not remain in the kernel
4709 * buffers, otherwise a close could cause an RST on some systems
4710 * (eg: Linux).
4711 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004712 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004713 channel_dont_read(chn);
4714
4715 /* if the server closes the connection, we want to immediately react
4716 * and close the socket to save packets and syscalls.
4717 */
4718 s->si[1].flags |= SI_FL_NOHALF;
4719
4720 /* In any case we've finished parsing the request so we must
4721 * disable Nagle when sending data because 1) we're not going
4722 * to shut this side, and 2) the server is waiting for us to
4723 * send pending data.
4724 */
4725 chn->flags |= CF_NEVER_WAIT;
4726
Christopher Fauletd01ce402019-01-02 17:44:13 +01004727 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4728 /* The server has not finished to respond, so we
4729 * don't want to move in order not to upset it.
4730 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004731 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004732 return;
4733 }
4734
Christopher Fauletf2824e62018-10-01 12:12:37 +02004735 /* When we get here, it means that both the request and the
4736 * response have finished receiving. Depending on the connection
4737 * mode, we'll have to wait for the last bytes to leave in either
4738 * direction, and sometimes for a close to be effective.
4739 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004740 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004741 /* Tunnel mode will not have any analyser so it needs to
4742 * poll for reads.
4743 */
4744 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004745 if (b_data(&chn->buf)) {
4746 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004747 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004748 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004749 txn->req.msg_state = HTTP_MSG_TUNNEL;
4750 }
4751 else {
4752 /* we're not expecting any new data to come for this
4753 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004754 *
4755 * However, there is an exception if the response
4756 * length is undefined. In this case, we need to wait
4757 * the close from the server. The response will be
4758 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004759 */
4760 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4761 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004762 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004763
4764 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4765 channel_shutr_now(chn);
4766 channel_shutw_now(chn);
4767 }
4768 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004769 goto check_channel_flags;
4770 }
4771
4772 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4773 http_msg_closing:
4774 /* nothing else to forward, just waiting for the output buffer
4775 * to be empty and for the shutw_now to take effect.
4776 */
4777 if (channel_is_empty(chn)) {
4778 txn->req.msg_state = HTTP_MSG_CLOSED;
4779 goto http_msg_closed;
4780 }
4781 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004782 txn->req.msg_state = HTTP_MSG_ERROR;
4783 goto end;
4784 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004785 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004786 return;
4787 }
4788
4789 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4790 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004791 /* if we don't know whether the server will close, we need to hard close */
4792 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4793 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004794 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004795 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004796 channel_dont_read(chn);
4797 goto end;
4798 }
4799
4800 check_channel_flags:
4801 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4802 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4803 /* if we've just closed an output, let's switch */
4804 txn->req.msg_state = HTTP_MSG_CLOSING;
4805 goto http_msg_closing;
4806 }
4807
4808 end:
4809 chn->analysers &= AN_REQ_FLT_END;
4810 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4811 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4812 channel_auto_close(chn);
4813 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004814 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004815}
4816
4817
4818/* This function terminates the response because it was completly analyzed or
4819 * because an error was triggered during the body forwarding.
4820 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004821static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004822{
4823 struct channel *chn = &s->res;
4824 struct http_txn *txn = s->txn;
4825
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004826 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004827
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004828 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4829 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004830 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004831 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004832 goto end;
4833 }
4834
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004835 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4836 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004837 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004838 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004839
4840 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4841 /* In theory, we don't need to read anymore, but we must
4842 * still monitor the server connection for a possible close
4843 * while the request is being uploaded, so we don't disable
4844 * reading.
4845 */
4846 /* channel_dont_read(chn); */
4847
4848 if (txn->req.msg_state < HTTP_MSG_DONE) {
4849 /* The client seems to still be sending data, probably
4850 * because we got an error response during an upload.
4851 * We have the choice of either breaking the connection
4852 * or letting it pass through. Let's do the later.
4853 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004854 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004855 return;
4856 }
4857
4858 /* When we get here, it means that both the request and the
4859 * response have finished receiving. Depending on the connection
4860 * mode, we'll have to wait for the last bytes to leave in either
4861 * direction, and sometimes for a close to be effective.
4862 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004863 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004864 channel_auto_read(chn);
4865 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004866 if (b_data(&chn->buf)) {
4867 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004868 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004869 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004870 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4871 }
4872 else {
4873 /* we're not expecting any new data to come for this
4874 * transaction, so we can close it.
4875 */
4876 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4877 channel_shutr_now(chn);
4878 channel_shutw_now(chn);
4879 }
4880 }
4881 goto check_channel_flags;
4882 }
4883
4884 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4885 http_msg_closing:
4886 /* nothing else to forward, just waiting for the output buffer
4887 * to be empty and for the shutw_now to take effect.
4888 */
4889 if (channel_is_empty(chn)) {
4890 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4891 goto http_msg_closed;
4892 }
4893 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004894 txn->rsp.msg_state = HTTP_MSG_ERROR;
Olivier Houcharda798bf52019-03-08 18:52:00 +01004895 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004896 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004897 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004898 goto end;
4899 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004900 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004901 return;
4902 }
4903
4904 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4905 http_msg_closed:
4906 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004907 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004908 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004909 goto end;
4910 }
4911
4912 check_channel_flags:
4913 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4914 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4915 /* if we've just closed an output, let's switch */
4916 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4917 goto http_msg_closing;
4918 }
4919
4920 end:
4921 chn->analysers &= AN_RES_FLT_END;
4922 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4923 chn->analysers |= AN_RES_FLT_XFER_DATA;
4924 channel_auto_close(chn);
4925 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004926 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004927}
4928
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004929void http_server_error(struct stream *s, struct stream_interface *si, int err,
4930 int finst, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004931{
4932 channel_auto_read(si_oc(si));
4933 channel_abort(si_oc(si));
4934 channel_auto_close(si_oc(si));
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004935 channel_htx_erase(si_oc(si), htxbuf(&(si_oc(si))->buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02004936 channel_auto_close(si_ic(si));
4937 channel_auto_read(si_ic(si));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004938
4939 /* <msg> is an HTX structure. So we copy it in the response's
4940 * channel */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004941 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004942 struct channel *chn = si_ic(si);
4943 struct htx *htx;
4944
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004945 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004946 chn->buf.data = msg->data;
4947 memcpy(chn->buf.area, msg->area, msg->data);
4948 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02004949 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02004950 c_adv(chn, htx->data);
4951 chn->total += htx->data;
4952 }
4953 if (!(s->flags & SF_ERR_MASK))
4954 s->flags |= err;
4955 if (!(s->flags & SF_FINST_MASK))
4956 s->flags |= finst;
4957}
4958
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004959void http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004960{
4961 channel_auto_read(&s->req);
4962 channel_abort(&s->req);
4963 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004964 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
4965 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02004966
4967 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004968
4969 /* <msg> is an HTX structure. So we copy it in the response's
4970 * channel */
4971 /* FIXME: It is a problem for now if there is some outgoing data */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004972 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004973 struct channel *chn = &s->res;
4974 struct htx *htx;
4975
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004976 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004977 chn->buf.data = msg->data;
4978 memcpy(chn->buf.area, msg->area, msg->data);
4979 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02004980 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02004981 c_adv(chn, htx->data);
4982 chn->total += htx->data;
4983 }
4984
4985 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
4986 channel_auto_read(&s->res);
4987 channel_auto_close(&s->res);
4988 channel_shutr_now(&s->res);
4989}
4990
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004991struct buffer *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004992{
4993 const int msgnum = http_get_status_idx(s->txn->status);
4994
4995 if (s->be->errmsg[msgnum].area)
4996 return &s->be->errmsg[msgnum];
4997 else if (strm_fe(s)->errmsg[msgnum].area)
4998 return &strm_fe(s)->errmsg[msgnum];
4999 else
Christopher Fauletf7346382019-07-17 22:02:08 +02005000 return &http_err_chunks[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005001}
5002
Christopher Faulet304cc402019-07-15 15:46:28 +02005003/* Return the error message corresponding to si->err_type. It is assumed
5004 * that the server side is closed. Note that err_type is actually a
5005 * bitmask, where almost only aborts may be cumulated with other
5006 * values. We consider that aborted operations are more important
5007 * than timeouts or errors due to the fact that nobody else in the
5008 * logs might explain incomplete retries. All others should avoid
5009 * being cumulated. It should normally not be possible to have multiple
5010 * aborts at once, but just in case, the first one in sequence is reported.
5011 * Note that connection errors appearing on the second request of a keep-alive
5012 * connection are not reported since this allows the client to retry.
5013 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005014void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02005015{
5016 int err_type = si->err_type;
5017
5018 /* set s->txn->status for http_error_message(s) */
5019 s->txn->status = 503;
5020
5021 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005022 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
5023 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005024 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005025 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
5026 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5027 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005028 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005029 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
5030 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005031 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005032 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
5033 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005034 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005035 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
5036 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5037 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005038 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005039 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
5040 (s->flags & SF_SRV_REUSED) ? NULL :
5041 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005042 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005043 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
5044 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5045 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005046 else { /* SI_ET_CONN_OTHER and others */
5047 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005048 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
5049 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005050 }
5051}
5052
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005053
Christopher Faulet4a28a532019-03-01 11:19:40 +01005054/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
5055 * on success and -1 on error.
5056 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005057static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01005058{
5059 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
5060 * then we must send an HTTP/1.1 100 Continue intermediate response.
5061 */
5062 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
5063 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
5064 struct ist hdr = { .ptr = "Expect", .len = 6 };
5065 struct http_hdr_ctx ctx;
5066
5067 ctx.blk = NULL;
5068 /* Expect is allowed in 1.1, look for it */
5069 if (http_find_header(htx, hdr, &ctx, 0) &&
5070 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005071 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01005072 return -1;
5073 http_remove_header(htx, &ctx);
5074 }
5075 }
5076 return 0;
5077}
5078
Christopher Faulet23a3c792018-11-28 10:01:23 +01005079/* Send a 100-Continue response to the client. It returns 0 on success and -1
5080 * on error. The response channel is updated accordingly.
5081 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005082static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01005083{
5084 struct channel *res = &s->res;
5085 struct htx *htx = htx_from_buf(&res->buf);
5086 struct htx_sl *sl;
5087 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
5088 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
5089 size_t data;
5090
5091 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5092 ist("HTTP/1.1"), ist("100"), ist("Continue"));
5093 if (!sl)
5094 goto fail;
5095 sl->info.res.status = 100;
5096
Christopher Faulet1d5ec092019-06-26 14:23:54 +02005097 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01005098 goto fail;
5099
5100 data = htx->data - co_data(res);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005101 c_adv(res, data);
5102 res->total += data;
5103 return 0;
5104
5105 fail:
5106 /* If an error occurred, remove the incomplete HTTP response from the
5107 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005108 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005109 return -1;
5110}
5111
Christopher Faulet12c51e22018-11-28 15:59:42 +01005112
5113/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
5114 * ont whether we use a proxy or not. It returns 0 on success and -1 on
5115 * error. The response channel is updated accordingly.
5116 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005117static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
Christopher Faulet12c51e22018-11-28 15:59:42 +01005118{
5119 struct channel *res = &s->res;
5120 struct htx *htx = htx_from_buf(&res->buf);
5121 struct htx_sl *sl;
5122 struct ist code, body;
5123 int status;
5124 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5125 size_t data;
5126
5127 if (!(s->txn->flags & TX_USE_PX_CONN)) {
5128 status = 401;
5129 code = ist("401");
5130 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
5131 "You need a valid user and password to access this content.\n"
5132 "</body></html>\n");
5133 }
5134 else {
5135 status = 407;
5136 code = ist("407");
5137 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
5138 "You need a valid user and password to access this content.\n"
5139 "</body></html>\n");
5140 }
5141
5142 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5143 ist("HTTP/1.1"), code, ist("Unauthorized"));
5144 if (!sl)
5145 goto fail;
5146 sl->info.res.status = status;
5147 s->txn->status = status;
5148
5149 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
5150 goto fail;
5151
Willy Tarreaub5ba2b02019-06-11 16:08:25 +02005152 if (!htx_add_header(htx, ist("Content-length"), ist("112")) ||
5153 !htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Faulet12c51e22018-11-28 15:59:42 +01005154 !htx_add_header(htx, ist("Connection"), ist("close")) ||
Jérôme Magnin86cef232018-12-28 14:49:08 +01005155 !htx_add_header(htx, ist("Content-Type"), ist("text/html")))
5156 goto fail;
5157 if (status == 401 && !htx_add_header(htx, ist("WWW-Authenticate"), ist2(trash.area, trash.data)))
5158 goto fail;
5159 if (status == 407 && !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005160 goto fail;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02005161 if (!htx_add_endof(htx, HTX_BLK_EOH))
5162 goto fail;
5163
5164 while (body.len) {
5165 size_t sent = htx_add_data(htx, body);
5166 if (!sent)
5167 goto fail;
5168 body.ptr += sent;
5169 body.len -= sent;
5170 }
5171
5172 if (!htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005173 goto fail;
5174
Christopher Faulet06511812019-10-16 09:38:27 +02005175 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet12c51e22018-11-28 15:59:42 +01005176 data = htx->data - co_data(res);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005177 c_adv(res, data);
5178 res->total += data;
5179
5180 channel_auto_read(&s->req);
5181 channel_abort(&s->req);
5182 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005183 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet12c51e22018-11-28 15:59:42 +01005184
5185 res->wex = tick_add_ifset(now_ms, res->wto);
5186 channel_auto_read(res);
5187 channel_auto_close(res);
5188 channel_shutr_now(res);
5189 return 0;
5190
5191 fail:
5192 /* If an error occurred, remove the incomplete HTTP response from the
5193 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005194 channel_htx_truncate(res, htx);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005195 return -1;
5196}
5197
Christopher Faulet0f226952018-10-22 09:29:56 +02005198/*
5199 * Capture headers from message <htx> according to header list <cap_hdr>, and
5200 * fill the <cap> pointers appropriately.
5201 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005202static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02005203{
5204 struct cap_hdr *h;
5205 int32_t pos;
5206
Christopher Fauleta3f15502019-05-13 15:27:23 +02005207 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02005208 struct htx_blk *blk = htx_get_blk(htx, pos);
5209 enum htx_blk_type type = htx_get_blk_type(blk);
5210 struct ist n, v;
5211
5212 if (type == HTX_BLK_EOH)
5213 break;
5214 if (type != HTX_BLK_HDR)
5215 continue;
5216
5217 n = htx_get_blk_name(htx, blk);
5218
5219 for (h = cap_hdr; h; h = h->next) {
5220 if (h->namelen && (h->namelen == n.len) &&
5221 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
5222 if (cap[h->index] == NULL)
5223 cap[h->index] =
5224 pool_alloc(h->pool);
5225
5226 if (cap[h->index] == NULL) {
5227 ha_alert("HTTP capture : out of memory.\n");
5228 break;
5229 }
5230
5231 v = htx_get_blk_value(htx, blk);
5232 if (v.len > h->len)
5233 v.len = h->len;
5234
5235 memcpy(cap[h->index], v.ptr, v.len);
5236 cap[h->index][v.len]=0;
5237 }
5238 }
5239 }
5240}
5241
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005242/* Delete a value in a header between delimiters <from> and <next>. The header
5243 * itself is delimited by <start> and <end> pointers. The number of characters
5244 * displaced is returned, and the pointer to the first delimiter is updated if
5245 * required. The function tries as much as possible to respect the following
5246 * principles :
5247 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5248 * in which case <next> is simply removed
5249 * - set exactly one space character after the new first delimiter, unless there
5250 * are not enough characters in the block being moved to do so.
5251 * - remove unneeded spaces before the previous delimiter and after the new
5252 * one.
5253 *
5254 * It is the caller's responsibility to ensure that :
5255 * - <from> points to a valid delimiter or <start> ;
5256 * - <next> points to a valid delimiter or <end> ;
5257 * - there are non-space chars before <from>.
5258 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005259static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005260{
5261 char *prev = *from;
5262
5263 if (prev == start) {
5264 /* We're removing the first value. eat the semicolon, if <next>
5265 * is lower than <end> */
5266 if (next < end)
5267 next++;
5268
5269 while (next < end && HTTP_IS_SPHT(*next))
5270 next++;
5271 }
5272 else {
5273 /* Remove useless spaces before the old delimiter. */
5274 while (HTTP_IS_SPHT(*(prev-1)))
5275 prev--;
5276 *from = prev;
5277
5278 /* copy the delimiter and if possible a space if we're
5279 * not at the end of the line.
5280 */
5281 if (next < end) {
5282 *prev++ = *next++;
5283 if (prev + 1 < next)
5284 *prev++ = ' ';
5285 while (next < end && HTTP_IS_SPHT(*next))
5286 next++;
5287 }
5288 }
5289 memmove(prev, next, end - next);
5290 return (prev - next);
5291}
5292
Christopher Faulet0f226952018-10-22 09:29:56 +02005293
5294/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005295 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005296 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005297static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005298{
5299 struct ist dst = ist2(str, 0);
5300
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005301 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005302 goto end;
5303 if (dst.len + 1 > len)
5304 goto end;
5305 dst.ptr[dst.len++] = ' ';
5306
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005307 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005308 goto end;
5309 if (dst.len + 1 > len)
5310 goto end;
5311 dst.ptr[dst.len++] = ' ';
5312
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005313 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005314 end:
5315 return dst.len;
5316}
5317
5318/*
5319 * Print a debug line with a start line.
5320 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005321static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005322{
5323 struct session *sess = strm_sess(s);
5324 int max;
5325
5326 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5327 dir,
5328 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5329 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5330
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005331 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005332 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005333 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005334 trash.area[trash.data++] = ' ';
5335
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005336 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005337 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005338 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005339 trash.area[trash.data++] = ' ';
5340
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005341 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005342 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005343 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005344 trash.area[trash.data++] = '\n';
5345
5346 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5347}
5348
5349/*
5350 * Print a debug line with a header.
5351 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005352static 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 +02005353{
5354 struct session *sess = strm_sess(s);
5355 int max;
5356
5357 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5358 dir,
5359 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5360 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5361
5362 max = n.len;
5363 UBOUND(max, trash.size - trash.data - 3);
5364 chunk_memcat(&trash, n.ptr, max);
5365 trash.area[trash.data++] = ':';
5366 trash.area[trash.data++] = ' ';
5367
5368 max = v.len;
5369 UBOUND(max, trash.size - trash.data - 1);
5370 chunk_memcat(&trash, v.ptr, max);
5371 trash.area[trash.data++] = '\n';
5372
5373 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5374}
5375
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005376/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5377 * In case of allocation failure, everything allocated is freed and NULL is
5378 * returned. Otherwise the new transaction is assigned to the stream and
5379 * returned.
5380 */
5381struct http_txn *http_alloc_txn(struct stream *s)
5382{
5383 struct http_txn *txn = s->txn;
5384
5385 if (txn)
5386 return txn;
5387
5388 txn = pool_alloc(pool_head_http_txn);
5389 if (!txn)
5390 return txn;
5391
5392 s->txn = txn;
5393 return txn;
5394}
5395
5396void http_txn_reset_req(struct http_txn *txn)
5397{
5398 txn->req.flags = 0;
5399 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5400}
5401
5402void http_txn_reset_res(struct http_txn *txn)
5403{
5404 txn->rsp.flags = 0;
5405 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5406}
5407
5408/*
5409 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5410 * the required fields are properly allocated and that we only need to (re)init
5411 * them. This should be used before processing any new request.
5412 */
5413void http_init_txn(struct stream *s)
5414{
5415 struct http_txn *txn = s->txn;
5416 struct conn_stream *cs = objt_cs(s->si[0].end);
5417
5418 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5419 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5420 : 0);
5421 txn->status = -1;
5422 *(unsigned int *)txn->cache_hash = 0;
5423
5424 txn->cookie_first_date = 0;
5425 txn->cookie_last_date = 0;
5426
5427 txn->srv_cookie = NULL;
5428 txn->cli_cookie = NULL;
5429 txn->uri = NULL;
5430
5431 http_txn_reset_req(txn);
5432 http_txn_reset_res(txn);
5433
5434 txn->req.chn = &s->req;
5435 txn->rsp.chn = &s->res;
5436
5437 txn->auth.method = HTTP_AUTH_UNKNOWN;
5438
5439 vars_init(&s->vars_txn, SCOPE_TXN);
5440 vars_init(&s->vars_reqres, SCOPE_REQ);
5441}
5442
5443/* to be used at the end of a transaction */
5444void http_end_txn(struct stream *s)
5445{
5446 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005447
5448 /* these ones will have been dynamically allocated */
5449 pool_free(pool_head_requri, txn->uri);
5450 pool_free(pool_head_capture, txn->cli_cookie);
5451 pool_free(pool_head_capture, txn->srv_cookie);
5452 pool_free(pool_head_uniqueid, s->unique_id);
5453
5454 s->unique_id = NULL;
5455 txn->uri = NULL;
5456 txn->srv_cookie = NULL;
5457 txn->cli_cookie = NULL;
5458
Christopher Faulet59399252019-11-07 14:27:52 +01005459 if (!LIST_ISEMPTY(&s->vars_txn.head))
5460 vars_prune(&s->vars_txn, s->sess, s);
5461 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5462 vars_prune(&s->vars_reqres, s->sess, s);
5463}
5464
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005465
5466DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
5467DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
Christopher Faulet0f226952018-10-22 09:29:56 +02005468
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005469__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005470static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005471{
5472}
5473
5474
5475/*
5476 * Local variables:
5477 * c-indent-level: 8
5478 * c-basic-offset: 8
5479 * End:
5480 */