blob: 3f681c0f59d4e7d211ac891578f3bd24b7d8e538 [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 Faulet377c5a52018-10-24 21:21:30 +020038extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020039
Christopher Fauleta8a46e22019-07-16 14:53:09 +020040struct pool_head *pool_head_requri = NULL;
41struct pool_head *pool_head_capture = NULL;
42
43
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020044static void http_end_request(struct stream *s);
45static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020046
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020047static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
48static int http_del_hdr_value(char *start, char *end, char **from, char *next);
49static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020050static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
51static 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 +020052
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020053static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status);
54static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020055
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020056static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
57static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020058
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020059static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
60static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020061
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020062static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
63static int http_reply_100_continue(struct stream *s);
64static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm);
Christopher Faulet23a3c792018-11-28 10:01:23 +010065
Christopher Faulete0768eb2018-10-03 16:38:02 +020066/* This stream analyser waits for a complete HTTP request. It returns 1 if the
67 * processing can continue on next analysers, or zero if it either needs more
68 * data or wants to immediately abort the request (eg: timeout, error, ...). It
69 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
70 * when it has nothing left to do, and may remove any analyser when it wants to
71 * abort.
72 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020073int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020074{
Christopher Faulet9768c262018-10-22 09:34:31 +020075
Christopher Faulete0768eb2018-10-03 16:38:02 +020076 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020077 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020078 *
Christopher Faulet9768c262018-10-22 09:34:31 +020079 * Once the start line and all headers are received, we may perform a
80 * capture of the error (if any), and we will set a few fields. We also
81 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020082 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020083 struct session *sess = s->sess;
84 struct http_txn *txn = s->txn;
85 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020086 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010087 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020088
89 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
90 now_ms, __FUNCTION__,
91 s,
92 req,
93 req->rex, req->wex,
94 req->flags,
95 ci_data(req),
96 req->analysers);
97
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010098 htx = htxbuf(&req->buf);
Christopher Faulet9768c262018-10-22 09:34:31 +020099
Willy Tarreau4236f032019-03-05 10:43:32 +0100100 /* Parsing errors are caught here */
101 if (htx->flags & HTX_FL_PARSING_ERROR) {
102 stream_inc_http_req_ctr(s);
103 stream_inc_http_err_ctr(s);
104 proxy_inc_fe_req_ctr(sess->fe);
105 goto return_bad_req;
106 }
107
Christopher Faulete0768eb2018-10-03 16:38:02 +0200108 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200109 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200110
111 /* If there is data available for analysis, log the end of the idle time. */
Christopher Faulet870aad92018-11-29 15:23:46 +0100112 if (c_data(req) && s->logs.t_idle == -1) {
113 const struct cs_info *csinfo = si_get_cs_info(objt_cs(s->si[0].end));
114
115 s->logs.t_idle = ((csinfo)
116 ? csinfo->t_idle
117 : tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake);
118 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200119
Christopher Faulete0768eb2018-10-03 16:38:02 +0200120 /*
121 * Now we quickly check if we have found a full valid request.
122 * If not so, we check the FD and buffer states before leaving.
123 * A full request is indicated by the fact that we have seen
124 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
125 * requests are checked first. When waiting for a second request
126 * on a keep-alive stream, if we encounter and error, close, t/o,
127 * we note the error in the stream flags but don't set any state.
128 * Since the error will be noted there, it will not be counted by
129 * process_stream() as a frontend error.
130 * Last, we may increase some tracked counters' http request errors on
131 * the cases that are deliberately the client's fault. For instance,
132 * a timeout or connection reset is not counted as an error. However
133 * a bad request is.
134 */
Christopher Faulet29f17582019-05-23 11:03:26 +0200135 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200136 if (htx->flags & HTX_FL_UPGRADE)
137 goto failed_keep_alive;
138
Christopher Faulet9768c262018-10-22 09:34:31 +0200139 /* 1: have we encountered a read error ? */
140 if (req->flags & CF_READ_ERROR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200141 if (!(s->flags & SF_ERR_MASK))
142 s->flags |= SF_ERR_CLICL;
143
144 if (txn->flags & TX_WAIT_NEXT_RQ)
145 goto failed_keep_alive;
146
147 if (sess->fe->options & PR_O_IGNORE_PRB)
148 goto failed_keep_alive;
149
Christopher Faulet9768c262018-10-22 09:34:31 +0200150 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200151 stream_inc_http_req_ctr(s);
152 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100153 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200154 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100155 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200156
Christopher Faulet9768c262018-10-22 09:34:31 +0200157 txn->status = 400;
158 msg->err_state = msg->msg_state;
159 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200160 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet9768c262018-10-22 09:34:31 +0200161 req->analysers &= AN_REQ_FLT_END;
162
Christopher Faulete0768eb2018-10-03 16:38:02 +0200163 if (!(s->flags & SF_FINST_MASK))
164 s->flags |= SF_FINST_R;
165 return 0;
166 }
167
Christopher Faulet9768c262018-10-22 09:34:31 +0200168 /* 2: has the read timeout expired ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200169 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
170 if (!(s->flags & SF_ERR_MASK))
171 s->flags |= SF_ERR_CLITO;
172
173 if (txn->flags & TX_WAIT_NEXT_RQ)
174 goto failed_keep_alive;
175
176 if (sess->fe->options & PR_O_IGNORE_PRB)
177 goto failed_keep_alive;
178
Christopher Faulet9768c262018-10-22 09:34:31 +0200179 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200180 stream_inc_http_req_ctr(s);
181 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100182 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200183 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100184 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200185
Christopher Faulet9768c262018-10-22 09:34:31 +0200186 txn->status = 408;
187 msg->err_state = msg->msg_state;
188 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200189 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200190 req->analysers &= AN_REQ_FLT_END;
191
Christopher Faulete0768eb2018-10-03 16:38:02 +0200192 if (!(s->flags & SF_FINST_MASK))
193 s->flags |= SF_FINST_R;
194 return 0;
195 }
196
Christopher Faulet9768c262018-10-22 09:34:31 +0200197 /* 3: have we encountered a close ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200198 else if (req->flags & CF_SHUTR) {
199 if (!(s->flags & SF_ERR_MASK))
200 s->flags |= SF_ERR_CLICL;
201
202 if (txn->flags & TX_WAIT_NEXT_RQ)
203 goto failed_keep_alive;
204
205 if (sess->fe->options & PR_O_IGNORE_PRB)
206 goto failed_keep_alive;
207
Christopher Faulete0768eb2018-10-03 16:38:02 +0200208 stream_inc_http_err_ctr(s);
209 stream_inc_http_req_ctr(s);
210 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100211 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200212 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100213 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200214
Christopher Faulet9768c262018-10-22 09:34:31 +0200215 txn->status = 400;
216 msg->err_state = msg->msg_state;
217 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200218 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200219 req->analysers &= AN_REQ_FLT_END;
220
Christopher Faulete0768eb2018-10-03 16:38:02 +0200221 if (!(s->flags & SF_FINST_MASK))
222 s->flags |= SF_FINST_R;
223 return 0;
224 }
225
226 channel_dont_connect(req);
227 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
228 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100229
Christopher Faulet9768c262018-10-22 09:34:31 +0200230 if (sess->listener->options & LI_O_NOQUICKACK && htx_is_not_empty(htx) &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200231 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
232 /* We need more data, we have to re-enable quick-ack in case we
233 * previously disabled it, otherwise we might cause the client
234 * to delay next data.
235 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100236 conn_set_quickack(objt_conn(sess->origin), 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200237 }
Willy Tarreau1a18b542018-12-11 16:37:42 +0100238
Christopher Faulet47365272018-10-31 17:40:50 +0100239 if ((req->flags & CF_READ_PARTIAL) && (txn->flags & TX_WAIT_NEXT_RQ)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200240 /* If the client starts to talk, let's fall back to
241 * request timeout processing.
242 */
243 txn->flags &= ~TX_WAIT_NEXT_RQ;
244 req->analyse_exp = TICK_ETERNITY;
245 }
246
247 /* just set the request timeout once at the beginning of the request */
248 if (!tick_isset(req->analyse_exp)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100249 if ((txn->flags & TX_WAIT_NEXT_RQ) && tick_isset(s->be->timeout.httpka))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200250 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
251 else
252 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
253 }
254
255 /* we're not ready yet */
256 return 0;
257
258 failed_keep_alive:
259 /* Here we process low-level errors for keep-alive requests. In
260 * short, if the request is not the first one and it experiences
261 * a timeout, read error or shutdown, we just silently close so
262 * that the client can try again.
263 */
264 txn->status = 0;
265 msg->msg_state = HTTP_MSG_RQBEFORE;
266 req->analysers &= AN_REQ_FLT_END;
267 s->logs.logwait = 0;
268 s->logs.level = 0;
269 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200270 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200271 return 0;
272 }
273
Christopher Faulet9768c262018-10-22 09:34:31 +0200274 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200275 stream_inc_http_req_ctr(s);
276 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
277
Christopher Faulet9768c262018-10-22 09:34:31 +0200278 /* kill the pending keep-alive timeout */
279 txn->flags &= ~TX_WAIT_NEXT_RQ;
280 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200281
Christopher Faulet29f17582019-05-23 11:03:26 +0200282 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200283 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100284
Christopher Faulet9768c262018-10-22 09:34:31 +0200285 /* 0: we might have to print this header in debug mode */
286 if (unlikely((global.mode & MODE_DEBUG) &&
287 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
288 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200289
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200290 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200291
Christopher Fauleta3f15502019-05-13 15:27:23 +0200292 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200293 struct htx_blk *blk = htx_get_blk(htx, pos);
294 enum htx_blk_type type = htx_get_blk_type(blk);
295
296 if (type == HTX_BLK_EOH)
297 break;
298 if (type != HTX_BLK_HDR)
299 continue;
300
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200301 http_debug_hdr("clihdr", s,
302 htx_get_blk_name(htx, blk),
303 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200304 }
305 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200306
307 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100308 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200309 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100310 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100311 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200312 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100313 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +0100314 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100315 if (sl->flags & HTX_SL_F_BODYLESS)
316 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200317
318 /* we can make use of server redirect on GET and HEAD */
319 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
320 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100321 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200322 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200323 goto return_bad_req;
324 }
325
326 /*
327 * 2: check if the URI matches the monitor_uri.
328 * We have to do this for every request which gets in, because
329 * the monitor-uri is defined by the frontend.
330 */
331 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100332 isteqi(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200333 /*
334 * We have found the monitor URI
335 */
336 struct acl_cond *cond;
337
338 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100339 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200340
341 /* Check if we want to fail this monitor request or not */
342 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
343 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
344
345 ret = acl_pass(ret);
346 if (cond->pol == ACL_COND_UNLESS)
347 ret = !ret;
348
349 if (ret) {
350 /* we fail this request, let's return 503 service unavail */
351 txn->status = 503;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200352 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200353 if (!(s->flags & SF_ERR_MASK))
354 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
355 goto return_prx_cond;
356 }
357 }
358
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800359 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200360 txn->status = 200;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200361 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200362 if (!(s->flags & SF_ERR_MASK))
363 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
364 goto return_prx_cond;
365 }
366
367 /*
368 * 3: Maybe we have to copy the original REQURI for the logs ?
369 * Note: we cannot log anymore if the request has been
370 * classified as invalid.
371 */
372 if (unlikely(s->logs.logwait & LW_REQ)) {
373 /* we have a complete HTTP request that we must log */
374 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200375 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200376
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200377 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200378 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200379
380 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
381 s->do_log(s);
382 } else {
383 ha_alert("HTTP logging : out of memory.\n");
384 }
385 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200386
Christopher Faulete0768eb2018-10-03 16:38:02 +0200387 /* if the frontend has "option http-use-proxy-header", we'll check if
388 * we have what looks like a proxied connection instead of a connection,
389 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
390 * Note that this is *not* RFC-compliant, however browsers and proxies
391 * happen to do that despite being non-standard :-(
392 * We consider that a request not beginning with either '/' or '*' is
393 * a proxied connection, which covers both "scheme://location" and
394 * CONNECT ip:port.
395 */
396 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100397 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200398 txn->flags |= TX_USE_PX_CONN;
399
Christopher Faulete0768eb2018-10-03 16:38:02 +0200400 /* 5: we may need to capture headers */
401 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200402 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200403
Christopher Faulete0768eb2018-10-03 16:38:02 +0200404 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200405 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200406 req->analysers |= AN_REQ_HTTP_BODY;
407
408 /*
409 * RFC7234#4:
410 * A cache MUST write through requests with methods
411 * that are unsafe (Section 4.2.1 of [RFC7231]) to
412 * the origin server; i.e., a cache is not allowed
413 * to generate a reply to such a request before
414 * having forwarded the request and having received
415 * a corresponding response.
416 *
417 * RFC7231#4.2.1:
418 * Of the request methods defined by this
419 * specification, the GET, HEAD, OPTIONS, and TRACE
420 * methods are defined to be safe.
421 */
422 if (likely(txn->meth == HTTP_METH_GET ||
423 txn->meth == HTTP_METH_HEAD ||
424 txn->meth == HTTP_METH_OPTIONS ||
425 txn->meth == HTTP_METH_TRACE))
426 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
427
428 /* end of job, return OK */
429 req->analysers &= ~an_bit;
430 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200431
Christopher Faulete0768eb2018-10-03 16:38:02 +0200432 return 1;
433
434 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200435 txn->status = 400;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200436 txn->req.err_state = txn->req.msg_state;
437 txn->req.msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200438 http_reply_and_close(s, txn->status, http_error_message(s));
Olivier Houcharda798bf52019-03-08 18:52:00 +0100439 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200440 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100441 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200442
443 return_prx_cond:
444 if (!(s->flags & SF_ERR_MASK))
445 s->flags |= SF_ERR_PRXCOND;
446 if (!(s->flags & SF_FINST_MASK))
447 s->flags |= SF_FINST_R;
448
449 req->analysers &= AN_REQ_FLT_END;
450 req->analyse_exp = TICK_ETERNITY;
451 return 0;
452}
453
454
455/* This stream analyser runs all HTTP request processing which is common to
456 * frontends and backends, which means blocking ACLs, filters, connection-close,
457 * reqadd, stats and redirects. This is performed for the designated proxy.
458 * It returns 1 if the processing can continue on next analysers, or zero if it
459 * either needs more data or wants to immediately abort the request (eg: deny,
460 * error, ...).
461 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200462int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200463{
464 struct session *sess = s->sess;
465 struct http_txn *txn = s->txn;
466 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200467 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200468 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200469 enum rule_result verdict;
470 int deny_status = HTTP_ERR_403;
471 struct connection *conn = objt_conn(sess->origin);
472
473 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
474 /* we need more data */
475 goto return_prx_yield;
476 }
477
478 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
479 now_ms, __FUNCTION__,
480 s,
481 req,
482 req->rex, req->wex,
483 req->flags,
484 ci_data(req),
485 req->analysers);
486
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100487 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200488
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200489 /* just in case we have some per-backend tracking. Only called the first
490 * execution of the analyser. */
491 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
492 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200493
494 /* evaluate http-request rules */
495 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200496 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200497
498 switch (verdict) {
499 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
500 goto return_prx_yield;
501
502 case HTTP_RULE_RES_CONT:
503 case HTTP_RULE_RES_STOP: /* nothing to do */
504 break;
505
506 case HTTP_RULE_RES_DENY: /* deny or tarpit */
507 if (txn->flags & TX_CLTARPIT)
508 goto tarpit;
509 goto deny;
510
511 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
512 goto return_prx_cond;
513
514 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
515 goto done;
516
517 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
518 goto return_bad_req;
519 }
520 }
521
522 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
523 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200524 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200525
Christopher Fauletff2759f2018-10-24 11:13:16 +0200526 ctx.blk = NULL;
527 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
528 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200529 goto return_bad_req;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200530 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200531 }
532
533 /* OK at this stage, we know that the request was accepted according to
534 * the http-request rules, we can check for the stats. Note that the
535 * URI is detected *before* the req* rules in order not to be affected
536 * by a possible reqrep, while they are processed *after* so that a
537 * reqdeny can still block them. This clearly needs to change in 1.6!
538 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200539 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200540 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100541 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200542 txn->status = 500;
543 s->logs.tv_request = now;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200544 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200545
546 if (!(s->flags & SF_ERR_MASK))
547 s->flags |= SF_ERR_RESOURCE;
548 goto return_prx_cond;
549 }
550
551 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200552 http_handle_stats(s, req);
553 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200554 /* not all actions implemented: deny, allow, auth */
555
556 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
557 goto deny;
558
559 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
560 goto return_prx_cond;
561 }
562
Christopher Faulet2571bc62019-03-01 11:44:26 +0100563 /* Proceed with the applets now. */
564 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200565 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100566 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200567
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200568 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100569 goto return_bad_req;
570
Christopher Faulete0768eb2018-10-03 16:38:02 +0200571 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
572 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
573 if (!(s->flags & SF_FINST_MASK))
574 s->flags |= SF_FINST_R;
575
576 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
577 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
578 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
579 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100580
581 req->flags |= CF_SEND_DONTWAIT;
582 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200583 goto done;
584 }
585
586 /* check whether we have some ACLs set to redirect this request */
587 list_for_each_entry(rule, &px->redirect_rules, list) {
588 if (rule->cond) {
589 int ret;
590
591 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
592 ret = acl_pass(ret);
593 if (rule->cond->pol == ACL_COND_UNLESS)
594 ret = !ret;
595 if (!ret)
596 continue;
597 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200598 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200599 goto return_bad_req;
600 goto done;
601 }
602
603 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
604 * If this happens, then the data will not come immediately, so we must
605 * send all what we have without waiting. Note that due to the small gain
606 * in waiting for the body of the request, it's easier to simply put the
607 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
608 * itself once used.
609 */
610 req->flags |= CF_SEND_DONTWAIT;
611
612 done: /* done with this analyser, continue with next ones that the calling
613 * points will have set, if any.
614 */
615 req->analyse_exp = TICK_ETERNITY;
616 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
617 req->analysers &= ~an_bit;
618 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->req.err_state = txn->req.msg_state;
674 txn->req.msg_state = HTTP_MSG_ERROR;
675 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200676 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200677
Olivier Houcharda798bf52019-03-08 18:52:00 +0100678 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200679 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100680 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200681
682 return_prx_cond:
683 if (!(s->flags & SF_ERR_MASK))
684 s->flags |= SF_ERR_PRXCOND;
685 if (!(s->flags & SF_FINST_MASK))
686 s->flags |= SF_FINST_R;
687
688 req->analysers &= AN_REQ_FLT_END;
689 req->analyse_exp = TICK_ETERNITY;
690 return 0;
691
692 return_prx_yield:
693 channel_dont_connect(req);
694 return 0;
695}
696
697/* This function performs all the processing enabled for the current request.
698 * It returns 1 if the processing can continue on next analysers, or zero if it
699 * needs more data, encounters an error, or wants to immediately abort the
700 * request. It relies on buffers flags, and updates s->req.analysers.
701 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200702int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200703{
704 struct session *sess = s->sess;
705 struct http_txn *txn = s->txn;
706 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200707 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200708 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
709
710 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
711 /* we need more data */
712 channel_dont_connect(req);
713 return 0;
714 }
715
716 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
717 now_ms, __FUNCTION__,
718 s,
719 req,
720 req->rex, req->wex,
721 req->flags,
722 ci_data(req),
723 req->analysers);
724
725 /*
726 * Right now, we know that we have processed the entire headers
727 * and that unwanted requests have been filtered out. We can do
728 * whatever we want with the remaining request. Also, now we
729 * may have separate values for ->fe, ->be.
730 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100731 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200732
733 /*
734 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200735 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200736 */
737 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100738 struct htx_sl *sl;
739 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200740
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200741 if (!sockaddr_alloc(&s->target_addr)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200742 txn->req.err_state = txn->req.msg_state;
743 txn->req.msg_state = HTTP_MSG_ERROR;
744 txn->status = 500;
745 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200746 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200747
748 if (!(s->flags & SF_ERR_MASK))
749 s->flags |= SF_ERR_RESOURCE;
750 if (!(s->flags & SF_FINST_MASK))
751 s->flags |= SF_FINST_R;
752
753 return 0;
754 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200755 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100756 uri = htx_sl_req_uri(sl);
757 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200758
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200759 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200760 goto return_bad_req;
761
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200762 s->target = &s->be->obj_type;
763 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
764
Christopher Faulete0768eb2018-10-03 16:38:02 +0200765 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200766 * uri.ptr and path.ptr (excluded). If it was not found, we need
767 * to replace from all the uri by a single "/".
768 *
769 * Instead of rewritting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100770 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200771 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200772 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100773 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200774 }
775
776 /*
777 * 7: Now we can work with the cookies.
778 * Note that doing so might move headers in the request, but
779 * the fields will stay coherent and the URI will not move.
780 * This should only be performed in the backend.
781 */
782 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200783 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200784
785 /* add unique-id if "header-unique-id" is specified */
786
787 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
788 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
789 goto return_bad_req;
790 s->unique_id[0] = '\0';
791 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
792 }
793
794 if (sess->fe->header_unique_id && s->unique_id) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200795 struct ist n = ist2(sess->fe->header_unique_id, strlen(sess->fe->header_unique_id));
796 struct ist v = ist2(s->unique_id, strlen(s->unique_id));
797
798 if (unlikely(!http_add_header(htx, n, v)))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200799 goto return_bad_req;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200800 }
801
802 /*
803 * 9: add X-Forwarded-For if either the frontend or the backend
804 * asks for it.
805 */
806 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200807 struct http_hdr_ctx ctx = { .blk = NULL };
808 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
809 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
810
Christopher Faulete0768eb2018-10-03 16:38:02 +0200811 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200812 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200813 /* The header is set to be added only if none is present
814 * and we found it, so don't do anything.
815 */
816 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200817 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200818 /* Add an X-Forwarded-For header unless the source IP is
819 * in the 'except' network range.
820 */
821 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200822 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200823 != sess->fe->except_net.s_addr) &&
824 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200825 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200826 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200827 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200828
829 /* Note: we rely on the backend to get the header name to be used for
830 * x-forwarded-for, because the header is really meant for the backends.
831 * However, if the backend did not specify any option, we have to rely
832 * on the frontend's header name.
833 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200834 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
835 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200836 goto return_bad_req;
837 }
838 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200839 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200840 /* FIXME: for the sake of completeness, we should also support
841 * 'except' here, although it is mostly useless in this case.
842 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200843 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200844
Christopher Faulete0768eb2018-10-03 16:38:02 +0200845 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200846 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200847 pn, sizeof(pn));
848
849 /* Note: we rely on the backend to get the header name to be used for
850 * x-forwarded-for, because the header is really meant for the backends.
851 * However, if the backend did not specify any option, we have to rely
852 * on the frontend's header name.
853 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200854 chunk_printf(&trash, "%s", pn);
855 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200856 goto return_bad_req;
857 }
858 }
859
860 /*
861 * 10: add X-Original-To if either the frontend or the backend
862 * asks for it.
863 */
864 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
865
866 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200867 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 +0200868 /* Add an X-Original-To header unless the destination IP is
869 * in the 'except' network range.
870 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200871 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200872 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200873 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200874 != sess->fe->except_to.s_addr) &&
875 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200876 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200877 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200878 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200879 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200880
881 /* Note: we rely on the backend to get the header name to be used for
882 * x-original-to, because the header is really meant for the backends.
883 * However, if the backend did not specify any option, we have to rely
884 * on the frontend's header name.
885 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200886 if (s->be->orgto_hdr_len)
887 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
888 else
889 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200890
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200891 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
892 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200893 goto return_bad_req;
894 }
895 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200896 }
897
Christopher Faulete0768eb2018-10-03 16:38:02 +0200898 /* If we have no server assigned yet and we're balancing on url_param
899 * with a POST request, we may be interested in checking the body for
900 * that parameter. This will be done in another analyser.
901 */
902 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100903 s->txn->meth == HTTP_METH_POST &&
904 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200905 channel_dont_connect(req);
906 req->analysers |= AN_REQ_HTTP_BODY;
907 }
908
909 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
910 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100911
Christopher Faulete0768eb2018-10-03 16:38:02 +0200912 /* We expect some data from the client. Unless we know for sure
913 * we already have a full request, we have to re-enable quick-ack
914 * in case we previously disabled it, otherwise we might cause
915 * the client to delay further data.
916 */
917 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200918 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100919 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200920
921 /*************************************************************
922 * OK, that's finished for the headers. We have done what we *
923 * could. Let's switch to the DATA state. *
924 ************************************************************/
925 req->analyse_exp = TICK_ETERNITY;
926 req->analysers &= ~an_bit;
927
928 s->logs.tv_request = now;
929 /* OK let's go on with the BODY now */
930 return 1;
931
932 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200933 txn->req.err_state = txn->req.msg_state;
934 txn->req.msg_state = HTTP_MSG_ERROR;
935 txn->status = 400;
936 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200937 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200938
Olivier Houcharda798bf52019-03-08 18:52:00 +0100939 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200940 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100941 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200942
943 if (!(s->flags & SF_ERR_MASK))
944 s->flags |= SF_ERR_PRXCOND;
945 if (!(s->flags & SF_FINST_MASK))
946 s->flags |= SF_FINST_R;
947 return 0;
948}
949
950/* This function is an analyser which processes the HTTP tarpit. It always
951 * returns zero, at the beginning because it prevents any other processing
952 * from occurring, and at the end because it terminates the request.
953 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200954int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200955{
956 struct http_txn *txn = s->txn;
957
958 /* This connection is being tarpitted. The CLIENT side has
959 * already set the connect expiration date to the right
960 * timeout. We just have to check that the client is still
961 * there and that the timeout has not expired.
962 */
963 channel_dont_connect(req);
964 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
965 !tick_is_expired(req->analyse_exp, now_ms))
966 return 0;
967
968 /* We will set the queue timer to the time spent, just for
969 * logging purposes. We fake a 500 server error, so that the
970 * attacker will not suspect his connection has been tarpitted.
971 * It will not cause trouble to the logs because we can exclude
972 * the tarpitted connections by filtering on the 'PT' status flags.
973 */
974 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
975
976 if (!(req->flags & CF_READ_ERROR))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200977 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200978
979 req->analysers &= AN_REQ_FLT_END;
980 req->analyse_exp = TICK_ETERNITY;
981
982 if (!(s->flags & SF_ERR_MASK))
983 s->flags |= SF_ERR_PRXCOND;
984 if (!(s->flags & SF_FINST_MASK))
985 s->flags |= SF_FINST_T;
986 return 0;
987}
988
989/* This function is an analyser which waits for the HTTP request body. It waits
990 * for either the buffer to be full, or the full advertised contents to have
991 * reached the buffer. It must only be called after the standard HTTP request
992 * processing has occurred, because it expects the request to be parsed and will
993 * look for the Expect header. It may send a 100-Continue interim response. It
994 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
995 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
996 * needs to read more data, or 1 once it has completed its analysis.
997 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200998int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200999{
1000 struct session *sess = s->sess;
1001 struct http_txn *txn = s->txn;
1002 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001003 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001004
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001005
1006 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1007 now_ms, __FUNCTION__,
1008 s,
1009 req,
1010 req->rex, req->wex,
1011 req->flags,
1012 ci_data(req),
1013 req->analysers);
1014
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001015 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001016
Willy Tarreau4236f032019-03-05 10:43:32 +01001017 if (htx->flags & HTX_FL_PARSING_ERROR)
1018 goto return_bad_req;
1019
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001020 if (msg->msg_state < HTTP_MSG_BODY)
1021 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001022
Christopher Faulete0768eb2018-10-03 16:38:02 +02001023 /* We have to parse the HTTP request body to find any required data.
1024 * "balance url_param check_post" should have been the only way to get
1025 * into this. We were brought here after HTTP header analysis, so all
1026 * related structures are ready.
1027 */
1028
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001029 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001030 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01001031 goto return_bad_req;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001032 }
1033
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001034 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001035
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001036 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1037 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001038 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001039 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001040 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001041 goto http_end;
1042
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001043 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001044 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1045 txn->status = 408;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001046 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001047
1048 if (!(s->flags & SF_ERR_MASK))
1049 s->flags |= SF_ERR_CLITO;
1050 if (!(s->flags & SF_FINST_MASK))
1051 s->flags |= SF_FINST_D;
1052 goto return_err_msg;
1053 }
1054
1055 /* we get here if we need to wait for more data */
1056 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1057 /* Not enough data. We'll re-use the http-request
1058 * timeout here. Ideally, we should set the timeout
1059 * relative to the accept() date. We just set the
1060 * request timeout once at the beginning of the
1061 * request.
1062 */
1063 channel_dont_connect(req);
1064 if (!tick_isset(req->analyse_exp))
1065 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1066 return 0;
1067 }
1068
1069 http_end:
1070 /* The situation will not evolve, so let's give up on the analysis. */
1071 s->logs.tv_request = now; /* update the request timer to reflect full request */
1072 req->analysers &= ~an_bit;
1073 req->analyse_exp = TICK_ETERNITY;
1074 return 1;
1075
1076 return_bad_req: /* let's centralize all bad requests */
1077 txn->req.err_state = txn->req.msg_state;
1078 txn->req.msg_state = HTTP_MSG_ERROR;
1079 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001080 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001081
1082 if (!(s->flags & SF_ERR_MASK))
1083 s->flags |= SF_ERR_PRXCOND;
1084 if (!(s->flags & SF_FINST_MASK))
1085 s->flags |= SF_FINST_R;
1086
1087 return_err_msg:
1088 req->analysers &= AN_REQ_FLT_END;
Olivier Houcharda798bf52019-03-08 18:52:00 +01001089 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001090 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001091 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001092 return 0;
1093}
1094
1095/* This function is an analyser which forwards request body (including chunk
1096 * sizes if any). It is called as soon as we must forward, even if we forward
1097 * zero byte. The only situation where it must not be called is when we're in
1098 * tunnel mode and we want to forward till the close. It's used both to forward
1099 * remaining data and to resync after end of body. It expects the msg_state to
1100 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1101 * read more data, or 1 once we can go on with next request or end the stream.
1102 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1103 * bytes of pending data + the headers if not already done.
1104 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001105int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001106{
1107 struct session *sess = s->sess;
1108 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001109 struct http_msg *msg = &txn->req;
1110 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001111 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001112 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001113
1114 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1115 now_ms, __FUNCTION__,
1116 s,
1117 req,
1118 req->rex, req->wex,
1119 req->flags,
1120 ci_data(req),
1121 req->analysers);
1122
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001123 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001124
1125 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1126 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1127 /* Output closed while we were sending data. We must abort and
1128 * wake the other side up.
1129 */
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001130 /* Don't abort yet if we had L7 retries activated and it
1131 * was a write error, we may recover.
1132 */
1133 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
1134 (s->si[1].flags & SI_FL_L7_RETRY))
1135 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001136 msg->err_state = msg->msg_state;
1137 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001138 http_end_request(s);
1139 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001140 return 1;
1141 }
1142
1143 /* Note that we don't have to send 100-continue back because we don't
1144 * need the data to complete our job, and it's up to the server to
1145 * decide whether to return 100, 417 or anything else in return of
1146 * an "Expect: 100-continue" header.
1147 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001148 if (msg->msg_state == HTTP_MSG_BODY)
1149 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001150
Christopher Faulete0768eb2018-10-03 16:38:02 +02001151 /* in most states, we should abort in case of early close */
1152 channel_auto_close(req);
1153
1154 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001155 if (req->to_forward == CHN_INFINITE_FORWARD) {
1156 if (req->flags & (CF_SHUTR|CF_EOI)) {
1157 msg->msg_state = HTTP_MSG_DONE;
1158 req->to_forward = 0;
1159 goto done;
1160 }
1161 }
1162 else {
1163 /* We can't process the buffer's contents yet */
1164 req->flags |= CF_WAKE_WRITE;
1165 goto missing_data_or_waiting;
1166 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001167 }
1168
Christopher Faulet9768c262018-10-22 09:34:31 +02001169 if (msg->msg_state >= HTTP_MSG_DONE)
1170 goto done;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001171 /* Forward input data. We get it by removing all outgoing data not
1172 * forwarded yet from HTX data size. If there are some data filters, we
1173 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001174 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001175 if (HAS_REQ_DATA_FILTERS(s)) {
1176 ret = flt_http_payload(s, msg, htx->data);
1177 if (ret < 0)
1178 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001179 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001180 }
1181 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001182 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001183 if (msg->flags & HTTP_MSGF_XFER_LEN)
1184 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001185 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001186
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001187 if (txn->meth == HTTP_METH_CONNECT) {
1188 msg->msg_state = HTTP_MSG_TUNNEL;
1189 goto done;
1190 }
1191
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001192
Christopher Faulet9768c262018-10-22 09:34:31 +02001193 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001194 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1195 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001196 */
1197 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1198 goto missing_data_or_waiting;
1199
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001200 msg->msg_state = HTTP_MSG_ENDING;
1201 if (htx->data != co_data(req))
1202 goto missing_data_or_waiting;
Christopher Faulet9768c262018-10-22 09:34:31 +02001203 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletaed68d42019-03-28 18:12:46 +01001204 req->to_forward = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001205
1206 done:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001207 /* other states, DONE...TUNNEL */
1208 /* we don't want to forward closes on DONE except in tunnel mode. */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001209 if (!(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001210 channel_dont_close(req);
1211
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001212 if (HAS_REQ_DATA_FILTERS(s)) {
1213 ret = flt_http_end(s, msg);
1214 if (ret <= 0) {
1215 if (!ret)
1216 goto missing_data_or_waiting;
1217 goto return_bad_req;
1218 }
1219 }
1220
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001221 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001222 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001223 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001224 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1225 if (req->flags & CF_SHUTW) {
1226 /* request errors are most likely due to the
1227 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001228 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001229 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001230 goto return_bad_req;
1231 }
1232 return 1;
1233 }
1234
1235 /* If "option abortonclose" is set on the backend, we want to monitor
1236 * the client's connection and forward any shutdown notification to the
1237 * server, which will decide whether to close or to go on processing the
1238 * request. We only do that in tunnel mode, and not in other modes since
1239 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001240 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001241 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001242 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001243 s->si[1].flags |= SI_FL_NOLINGER;
1244 channel_auto_close(req);
1245 }
1246 else if (s->txn->meth == HTTP_METH_POST) {
1247 /* POST requests may require to read extra CRLF sent by broken
1248 * browsers and which could cause an RST to be sent upon close
1249 * on some systems (eg: Linux). */
1250 channel_auto_read(req);
1251 }
1252 return 0;
1253
1254 missing_data_or_waiting:
1255 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001256 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001257 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001258
1259 waiting:
1260 /* waiting for the last bits to leave the buffer */
1261 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001262 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001263
Christopher Faulet47365272018-10-31 17:40:50 +01001264 if (htx->flags & HTX_FL_PARSING_ERROR)
1265 goto return_bad_req;
Christopher Faulet9768c262018-10-22 09:34:31 +02001266
Christopher Faulete0768eb2018-10-03 16:38:02 +02001267 /* When TE: chunked is used, we need to get there again to parse remaining
1268 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1269 * And when content-length is used, we never want to let the possible
1270 * shutdown be forwarded to the other side, as the state machine will
1271 * take care of it once the client responds. It's also important to
1272 * prevent TIME_WAITs from accumulating on the backend side, and for
1273 * HTTP/2 where the last frame comes with a shutdown.
1274 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001275 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001276 channel_dont_close(req);
1277
1278 /* We know that more data are expected, but we couldn't send more that
1279 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1280 * system knows it must not set a PUSH on this first part. Interactive
1281 * modes are already handled by the stream sock layer. We must not do
1282 * this in content-length mode because it could present the MSG_MORE
1283 * flag with the last block of forwarded data, which would cause an
1284 * additional delay to be observed by the receiver.
1285 */
1286 if (msg->flags & HTTP_MSGF_TE_CHNK)
1287 req->flags |= CF_EXPECT_MORE;
1288
1289 return 0;
1290
Christopher Faulet93e02d82019-03-08 14:18:50 +01001291 return_cli_abort:
1292 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1293 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
1294 if (objt_server(s->target))
1295 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
1296 if (!(s->flags & SF_ERR_MASK))
1297 s->flags |= SF_ERR_CLICL;
1298 status = 400;
1299 goto return_error;
1300
1301 return_srv_abort:
1302 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1303 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
1304 if (objt_server(s->target))
1305 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
1306 if (!(s->flags & SF_ERR_MASK))
1307 s->flags |= SF_ERR_SRVCL;
1308 status = 502;
1309 goto return_error;
1310
1311 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001312 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001313 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001314 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001315 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01001316 s->flags |= SF_ERR_CLICL;
1317 status = 400;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001318
Christopher Faulet93e02d82019-03-08 14:18:50 +01001319 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001320 txn->req.err_state = txn->req.msg_state;
1321 txn->req.msg_state = HTTP_MSG_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001322 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001323 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001324 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001325 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001326 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001327 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001328 }
1329 req->analysers &= AN_REQ_FLT_END;
1330 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 +01001331 if (!(s->flags & SF_FINST_MASK))
1332 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001333 return 0;
1334}
1335
Olivier Houcharda254a372019-04-05 15:30:12 +02001336/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1337/* Returns 0 if we can attempt to retry, -1 otherwise */
1338static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1339{
1340 struct channel *req, *res;
1341 int co_data;
1342
1343 si->conn_retries--;
1344 if (si->conn_retries < 0)
1345 return -1;
1346
Willy Tarreau223995e2019-05-04 10:38:31 +02001347 if (objt_server(s->target))
1348 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1349 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1350
Olivier Houcharda254a372019-04-05 15:30:12 +02001351 req = &s->req;
1352 res = &s->res;
1353 /* Remove any write error from the request, and read error from the response */
1354 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1355 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1356 res->analysers = 0;
1357 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001358 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001359 si->exp = TICK_ETERNITY;
1360 res->rex = TICK_ETERNITY;
1361 res->to_forward = 0;
1362 res->analyse_exp = TICK_ETERNITY;
1363 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001364 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001365 si_release_endpoint(&s->si[1]);
1366 b_free(&req->buf);
1367 /* Swap the L7 buffer with the channel buffer */
1368 /* We know we stored the co_data as b_data, so get it there */
1369 co_data = b_data(&si->l7_buffer);
1370 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1371 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1372
1373 co_set_data(req, co_data);
1374 b_reset(&res->buf);
1375 co_set_data(res, 0);
1376 return 0;
1377}
1378
Christopher Faulete0768eb2018-10-03 16:38:02 +02001379/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1380 * processing can continue on next analysers, or zero if it either needs more
1381 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1382 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1383 * when it has nothing left to do, and may remove any analyser when it wants to
1384 * abort.
1385 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001386int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001387{
Christopher Faulet9768c262018-10-22 09:34:31 +02001388 /*
1389 * We will analyze a complete HTTP response to check the its syntax.
1390 *
1391 * Once the start line and all headers are received, we may perform a
1392 * capture of the error (if any), and we will set a few fields. We also
1393 * logging and finally headers capture.
1394 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001395 struct session *sess = s->sess;
1396 struct http_txn *txn = s->txn;
1397 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001398 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001399 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001400 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001401 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001402 int n;
1403
1404 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1405 now_ms, __FUNCTION__,
1406 s,
1407 rep,
1408 rep->rex, rep->wex,
1409 rep->flags,
1410 ci_data(rep),
1411 rep->analysers);
1412
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001413 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001414
Willy Tarreau4236f032019-03-05 10:43:32 +01001415 /* Parsing errors are caught here */
1416 if (htx->flags & HTX_FL_PARSING_ERROR)
1417 goto return_bad_res;
1418
Christopher Faulete0768eb2018-10-03 16:38:02 +02001419 /*
1420 * Now we quickly check if we have found a full valid response.
1421 * If not so, we check the FD and buffer states before leaving.
1422 * A full response is indicated by the fact that we have seen
1423 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1424 * responses are checked first.
1425 *
1426 * Depending on whether the client is still there or not, we
1427 * may send an error response back or not. Note that normally
1428 * we should only check for HTTP status there, and check I/O
1429 * errors somewhere else.
1430 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001431 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001432 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001433 /* 1: have we encountered a read error ? */
1434 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001435 struct connection *conn = NULL;
1436
Olivier Houchard865d8392019-05-03 22:46:27 +02001437 if (objt_cs(s->si[1].end))
1438 conn = objt_cs(s->si[1].end)->conn;
1439
1440 if (si_b->flags & SI_FL_L7_RETRY &&
1441 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001442 /* If we arrive here, then CF_READ_ERROR was
1443 * set by si_cs_recv() because we matched a
1444 * status, overwise it would have removed
1445 * the SI_FL_L7_RETRY flag, so it's ok not
1446 * to check s->be->retry_type.
1447 */
1448 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1449 return 0;
1450 }
1451
Olivier Houchard6db16992019-05-17 15:40:49 +02001452 if (txn->flags & TX_NOT_FIRST)
1453 goto abort_keep_alive;
1454
Olivier Houcharda798bf52019-03-08 18:52:00 +01001455 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001456 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001457 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001458 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001459 }
1460
Christopher Faulete0768eb2018-10-03 16:38:02 +02001461 rep->analysers &= AN_RES_FLT_END;
1462 txn->status = 502;
1463
1464 /* Check to see if the server refused the early data.
1465 * If so, just send a 425
1466 */
Olivier Houchard865d8392019-05-03 22:46:27 +02001467 if (conn->err_code == CO_ER_SSL_EARLY_FAILED) {
1468 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001469 (si_b->flags & SI_FL_L7_RETRY) &&
Olivier Houchard865d8392019-05-03 22:46:27 +02001470 do_l7_retry(s, si_b) == 0)
1471 return 0;
1472 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001473 }
1474
1475 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001476 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001477
1478 if (!(s->flags & SF_ERR_MASK))
1479 s->flags |= SF_ERR_SRVCL;
1480 if (!(s->flags & SF_FINST_MASK))
1481 s->flags |= SF_FINST_H;
1482 return 0;
1483 }
1484
Christopher Faulet9768c262018-10-22 09:34:31 +02001485 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001486 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001487 if ((si_b->flags & SI_FL_L7_RETRY) &&
1488 (s->be->retry_type & PR_RE_TIMEOUT)) {
1489 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1490 return 0;
1491 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001492 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001493 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001494 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001495 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001496 }
1497
Christopher Faulete0768eb2018-10-03 16:38:02 +02001498 rep->analysers &= AN_RES_FLT_END;
1499 txn->status = 504;
1500 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001501 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001502
1503 if (!(s->flags & SF_ERR_MASK))
1504 s->flags |= SF_ERR_SRVTO;
1505 if (!(s->flags & SF_FINST_MASK))
1506 s->flags |= SF_FINST_H;
1507 return 0;
1508 }
1509
Christopher Faulet9768c262018-10-22 09:34:31 +02001510 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001511 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001512 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1513 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001514 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001515 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001516
1517 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001518 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001519 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001520
1521 if (!(s->flags & SF_ERR_MASK))
1522 s->flags |= SF_ERR_CLICL;
1523 if (!(s->flags & SF_FINST_MASK))
1524 s->flags |= SF_FINST_H;
1525
1526 /* process_stream() will take care of the error */
1527 return 0;
1528 }
1529
Christopher Faulet9768c262018-10-22 09:34:31 +02001530 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001531 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001532 if ((si_b->flags & SI_FL_L7_RETRY) &&
1533 (s->be->retry_type & PR_RE_DISCONNECTED)) {
1534 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1535 return 0;
1536 }
1537
Olivier Houchard6db16992019-05-17 15:40:49 +02001538 if (txn->flags & TX_NOT_FIRST)
1539 goto abort_keep_alive;
1540
Olivier Houcharda798bf52019-03-08 18:52:00 +01001541 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001542 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001543 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001544 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001545 }
1546
Christopher Faulete0768eb2018-10-03 16:38:02 +02001547 rep->analysers &= AN_RES_FLT_END;
1548 txn->status = 502;
1549 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001550 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001551
1552 if (!(s->flags & SF_ERR_MASK))
1553 s->flags |= SF_ERR_SRVCL;
1554 if (!(s->flags & SF_FINST_MASK))
1555 s->flags |= SF_FINST_H;
1556 return 0;
1557 }
1558
Christopher Faulet9768c262018-10-22 09:34:31 +02001559 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001560 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001561 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001562 goto abort_keep_alive;
1563
Olivier Houcharda798bf52019-03-08 18:52:00 +01001564 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001565 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001566
1567 if (!(s->flags & SF_ERR_MASK))
1568 s->flags |= SF_ERR_CLICL;
1569 if (!(s->flags & SF_FINST_MASK))
1570 s->flags |= SF_FINST_H;
1571
1572 /* process_stream() will take care of the error */
1573 return 0;
1574 }
1575
1576 channel_dont_close(rep);
1577 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
1578 return 0;
1579 }
1580
1581 /* More interesting part now : we know that we have a complete
1582 * response which at least looks like HTTP. We have an indicator
1583 * of each header's length, so we can parse them quickly.
1584 */
1585
Christopher Faulet9768c262018-10-22 09:34:31 +02001586 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001587 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001588 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001589
Christopher Faulet9768c262018-10-22 09:34:31 +02001590 /* 0: we might have to print this header in debug mode */
1591 if (unlikely((global.mode & MODE_DEBUG) &&
1592 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1593 int32_t pos;
1594
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001595 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001596
Christopher Fauleta3f15502019-05-13 15:27:23 +02001597 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001598 struct htx_blk *blk = htx_get_blk(htx, pos);
1599 enum htx_blk_type type = htx_get_blk_type(blk);
1600
1601 if (type == HTX_BLK_EOH)
1602 break;
1603 if (type != HTX_BLK_HDR)
1604 continue;
1605
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001606 http_debug_hdr("srvhdr", s,
1607 htx_get_blk_name(htx, blk),
1608 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001609 }
1610 }
1611
Christopher Faulet03599112018-11-27 11:21:21 +01001612 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001613 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001614 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001615 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001616 if (sl->flags & HTX_SL_F_XFER_LEN) {
1617 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001618 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001619 if (sl->flags & HTX_SL_F_BODYLESS)
1620 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001621 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001622
1623 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001624 if (n < 1 || n > 5)
1625 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001626
Christopher Faulete0768eb2018-10-03 16:38:02 +02001627 /* when the client triggers a 4xx from the server, it's most often due
1628 * to a missing object or permission. These events should be tracked
1629 * because if they happen often, it may indicate a brute force or a
1630 * vulnerability scan.
1631 */
1632 if (n == 4)
1633 stream_inc_http_err_ctr(s);
1634
1635 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001636 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001637
Christopher Faulete0768eb2018-10-03 16:38:02 +02001638 /* Adjust server's health based on status code. Note: status codes 501
1639 * and 505 are triggered on demand by client request, so we must not
1640 * count them as server failures.
1641 */
1642 if (objt_server(s->target)) {
1643 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001644 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001645 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001646 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001647 }
1648
1649 /*
1650 * We may be facing a 100-continue response, or any other informational
1651 * 1xx response which is non-final, in which case this is not the right
1652 * response, and we're waiting for the next one. Let's allow this response
1653 * to go to the client and wait for the next one. There's an exception for
1654 * 101 which is used later in the code to switch protocols.
1655 */
1656 if (txn->status < 200 &&
1657 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001658 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001659 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001660 msg->msg_state = HTTP_MSG_RPBEFORE;
1661 txn->status = 0;
1662 s->logs.t_data = -1; /* was not a response yet */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001663 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001664 }
1665
1666 /*
1667 * 2: check for cacheability.
1668 */
1669
1670 switch (txn->status) {
1671 case 200:
1672 case 203:
1673 case 204:
1674 case 206:
1675 case 300:
1676 case 301:
1677 case 404:
1678 case 405:
1679 case 410:
1680 case 414:
1681 case 501:
1682 break;
1683 default:
1684 /* RFC7231#6.1:
1685 * Responses with status codes that are defined as
1686 * cacheable by default (e.g., 200, 203, 204, 206,
1687 * 300, 301, 404, 405, 410, 414, and 501 in this
1688 * specification) can be reused by a cache with
1689 * heuristic expiration unless otherwise indicated
1690 * by the method definition or explicit cache
1691 * controls [RFC7234]; all other status codes are
1692 * not cacheable by default.
1693 */
1694 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1695 break;
1696 }
1697
1698 /*
1699 * 3: we may need to capture headers
1700 */
1701 s->logs.logwait &= ~LW_RESP;
1702 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001703 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001704
Christopher Faulet9768c262018-10-22 09:34:31 +02001705 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001706 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1707 txn->status == 101)) {
1708 /* Either we've established an explicit tunnel, or we're
1709 * switching the protocol. In both cases, we're very unlikely
1710 * to understand the next protocols. We have to switch to tunnel
1711 * mode, so that we transfer the request and responses then let
1712 * this protocol pass unmodified. When we later implement specific
1713 * parsers for such protocols, we'll want to check the Upgrade
1714 * header which contains information about that protocol for
1715 * responses with status 101 (eg: see RFC2817 about TLS).
1716 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001717 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001718 }
1719
Christopher Faulet61608322018-11-23 16:23:45 +01001720 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1721 * 407 (Proxy-Authenticate) responses and set the connection to private
1722 */
1723 srv_conn = cs_conn(objt_cs(s->si[1].end));
1724 if (srv_conn) {
1725 struct ist hdr;
1726 struct http_hdr_ctx ctx;
1727
1728 if (txn->status == 401)
1729 hdr = ist("WWW-Authenticate");
1730 else if (txn->status == 407)
1731 hdr = ist("Proxy-Authenticate");
1732 else
1733 goto end;
1734
1735 ctx.blk = NULL;
1736 while (http_find_header(htx, hdr, &ctx, 0)) {
1737 if ((ctx.value.len >= 9 && word_match(ctx.value.ptr, ctx.value.len, "Negotiate", 9)) ||
Olivier Houchard250031e2019-05-29 15:01:50 +02001738 (ctx.value.len >= 4 && word_match(ctx.value.ptr, ctx.value.len, "NTLM", 4))) {
1739 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet61608322018-11-23 16:23:45 +01001740 srv_conn->flags |= CO_FL_PRIVATE;
Olivier Houchard250031e2019-05-29 15:01:50 +02001741 }
Christopher Faulet61608322018-11-23 16:23:45 +01001742 }
1743 }
1744
1745 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001746 /* we want to have the response time before we start processing it */
1747 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1748
1749 /* end of job, return OK */
1750 rep->analysers &= ~an_bit;
1751 rep->analyse_exp = TICK_ETERNITY;
1752 channel_auto_close(rep);
1753 return 1;
1754
Christopher Faulet47365272018-10-31 17:40:50 +01001755 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001756 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001757 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001758 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001759 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001760 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001761 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001762 (si_b->flags & SI_FL_L7_RETRY) &&
Olivier Houcharde3249a92019-05-03 23:01:47 +02001763 do_l7_retry(s, si_b) == 0)
1764 return 0;
Christopher Faulet47365272018-10-31 17:40:50 +01001765 txn->status = 502;
1766 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001767 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001768 rep->analysers &= AN_RES_FLT_END;
1769
1770 if (!(s->flags & SF_ERR_MASK))
1771 s->flags |= SF_ERR_PRXCOND;
1772 if (!(s->flags & SF_FINST_MASK))
1773 s->flags |= SF_FINST_H;
1774 return 0;
1775
Christopher Faulete0768eb2018-10-03 16:38:02 +02001776 abort_keep_alive:
1777 /* A keep-alive request to the server failed on a network error.
1778 * The client is required to retry. We need to close without returning
1779 * any other information so that the client retries.
1780 */
1781 txn->status = 0;
1782 rep->analysers &= AN_RES_FLT_END;
1783 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001784 s->logs.logwait = 0;
1785 s->logs.level = 0;
1786 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001787 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001788 return 0;
1789}
1790
1791/* This function performs all the processing enabled for the current response.
1792 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1793 * and updates s->res.analysers. It might make sense to explode it into several
1794 * other functions. It works like process_request (see indications above).
1795 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001796int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001797{
1798 struct session *sess = s->sess;
1799 struct http_txn *txn = s->txn;
1800 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001801 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001802 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001803 enum rule_result ret = HTTP_RULE_RES_CONT;
1804
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001805 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1806 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001807
Christopher Faulete0768eb2018-10-03 16:38:02 +02001808 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1809 now_ms, __FUNCTION__,
1810 s,
1811 rep,
1812 rep->rex, rep->wex,
1813 rep->flags,
1814 ci_data(rep),
1815 rep->analysers);
1816
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001817 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001818
1819 /* The stats applet needs to adjust the Connection header but we don't
1820 * apply any filter there.
1821 */
1822 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1823 rep->analysers &= ~an_bit;
1824 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001825 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001826 }
1827
1828 /*
1829 * We will have to evaluate the filters.
1830 * As opposed to version 1.2, now they will be evaluated in the
1831 * filters order and not in the header order. This means that
1832 * each filter has to be validated among all headers.
1833 *
1834 * Filters are tried with ->be first, then with ->fe if it is
1835 * different from ->be.
1836 *
1837 * Maybe we are in resume condiion. In this case I choose the
1838 * "struct proxy" which contains the rule list matching the resume
1839 * pointer. If none of theses "struct proxy" match, I initialise
1840 * the process with the first one.
1841 *
1842 * In fact, I check only correspondance betwwen the current list
1843 * pointer and the ->fe rule list. If it doesn't match, I initialize
1844 * the loop with the ->be.
1845 */
1846 if (s->current_rule_list == &sess->fe->http_res_rules)
1847 cur_proxy = sess->fe;
1848 else
1849 cur_proxy = s->be;
1850 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001851 /* evaluate http-response rules */
1852 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001853 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001854
1855 if (ret == HTTP_RULE_RES_BADREQ)
1856 goto return_srv_prx_502;
1857
1858 if (ret == HTTP_RULE_RES_DONE) {
1859 rep->analysers &= ~an_bit;
1860 rep->analyse_exp = TICK_ETERNITY;
1861 return 1;
1862 }
1863 }
1864
1865 /* we need to be called again. */
1866 if (ret == HTTP_RULE_RES_YIELD) {
1867 channel_dont_close(rep);
1868 return 0;
1869 }
1870
Christopher Faulete0768eb2018-10-03 16:38:02 +02001871 /* has the response been denied ? */
1872 if (txn->flags & TX_SVDENY) {
1873 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001874 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_secu, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001875
Olivier Houcharda798bf52019-03-08 18:52:00 +01001876 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
1877 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001878 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001879 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001880 goto return_srv_prx_502;
1881 }
1882
Christopher Faulete0768eb2018-10-03 16:38:02 +02001883 /* check whether we're already working on the frontend */
1884 if (cur_proxy == sess->fe)
1885 break;
1886 cur_proxy = sess->fe;
1887 }
1888
1889 /* After this point, this anayzer can't return yield, so we can
1890 * remove the bit corresponding to this analyzer from the list.
1891 *
1892 * Note that the intermediate returns and goto found previously
1893 * reset the analyzers.
1894 */
1895 rep->analysers &= ~an_bit;
1896 rep->analyse_exp = TICK_ETERNITY;
1897
1898 /* OK that's all we can do for 1xx responses */
1899 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001900 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001901
1902 /*
1903 * Now check for a server cookie.
1904 */
1905 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001906 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001907
1908 /*
1909 * Check for cache-control or pragma headers if required.
1910 */
1911 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001912 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001913
1914 /*
1915 * Add server cookie in the response if needed
1916 */
1917 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1918 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1919 (!(s->flags & SF_DIRECT) ||
1920 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1921 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1922 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1923 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1924 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1925 !(s->flags & SF_IGNORE_PRST)) {
1926 /* the server is known, it's not the one the client requested, or the
1927 * cookie's last seen date needs to be refreshed. We have to
1928 * insert a set-cookie here, except if we want to insert only on POST
1929 * requests and this one isn't. Note that servers which don't have cookies
1930 * (eg: some backup servers) will return a full cookie removal request.
1931 */
1932 if (!objt_server(s->target)->cookie) {
1933 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001934 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001935 s->be->cookie_name);
1936 }
1937 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001938 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001939
1940 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1941 /* emit last_date, which is mandatory */
1942 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1943 s30tob64((date.tv_sec+3) >> 2,
1944 trash.area + trash.data);
1945 trash.data += 5;
1946
1947 if (s->be->cookie_maxlife) {
1948 /* emit first_date, which is either the original one or
1949 * the current date.
1950 */
1951 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1952 s30tob64(txn->cookie_first_date ?
1953 txn->cookie_first_date >> 2 :
1954 (date.tv_sec+3) >> 2,
1955 trash.area + trash.data);
1956 trash.data += 5;
1957 }
1958 }
1959 chunk_appendf(&trash, "; path=/");
1960 }
1961
1962 if (s->be->cookie_domain)
1963 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1964
1965 if (s->be->ck_opts & PR_CK_HTTPONLY)
1966 chunk_appendf(&trash, "; HttpOnly");
1967
1968 if (s->be->ck_opts & PR_CK_SECURE)
1969 chunk_appendf(&trash, "; Secure");
1970
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001971 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001972 goto return_bad_resp;
1973
1974 txn->flags &= ~TX_SCK_MASK;
1975 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1976 /* the server did not change, only the date was updated */
1977 txn->flags |= TX_SCK_UPDATED;
1978 else
1979 txn->flags |= TX_SCK_INSERTED;
1980
1981 /* Here, we will tell an eventual cache on the client side that we don't
1982 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1983 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1984 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1985 */
1986 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1987
1988 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1989
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001990 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001991 goto return_bad_resp;
1992 }
1993 }
1994
1995 /*
1996 * Check if result will be cacheable with a cookie.
1997 * We'll block the response if security checks have caught
1998 * nasty things such as a cacheable cookie.
1999 */
2000 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2001 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2002 (s->be->options & PR_O_CHK_CACHE)) {
2003 /* we're in presence of a cacheable response containing
2004 * a set-cookie header. We'll block it as requested by
2005 * the 'checkcache' option, and send an alert.
2006 */
2007 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01002008 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002009
Olivier Houcharda798bf52019-03-08 18:52:00 +01002010 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
2011 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002012 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002013 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002014
2015 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2016 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2017 send_log(s->be, LOG_ALERT,
2018 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2019 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2020 goto return_srv_prx_502;
2021 }
2022
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002023 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002024 /* Always enter in the body analyzer */
2025 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2026 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2027
2028 /* if the user wants to log as soon as possible, without counting
2029 * bytes from the server, then this is the right moment. We have
2030 * to temporarily assign bytes_out to log what we currently have.
2031 */
2032 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2033 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002034 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002035 s->do_log(s);
2036 s->logs.bytes_out = 0;
2037 }
2038 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002039
2040 return_bad_resp:
2041 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01002042 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01002043 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002044 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01002045 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002046
2047 return_srv_prx_502:
2048 rep->analysers &= AN_RES_FLT_END;
2049 txn->status = 502;
2050 s->logs.t_data = -1; /* was not a valid response */
2051 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002052 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002053 if (!(s->flags & SF_ERR_MASK))
2054 s->flags |= SF_ERR_PRXCOND;
2055 if (!(s->flags & SF_FINST_MASK))
2056 s->flags |= SF_FINST_H;
2057 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002058}
2059
2060/* This function is an analyser which forwards response body (including chunk
2061 * sizes if any). It is called as soon as we must forward, even if we forward
2062 * zero byte. The only situation where it must not be called is when we're in
2063 * tunnel mode and we want to forward till the close. It's used both to forward
2064 * remaining data and to resync after end of body. It expects the msg_state to
2065 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2066 * read more data, or 1 once we can go on with next request or end the stream.
2067 *
2068 * It is capable of compressing response data both in content-length mode and
2069 * in chunked mode. The state machines follows different flows depending on
2070 * whether content-length and chunked modes are used, since there are no
2071 * trailers in content-length :
2072 *
2073 * chk-mode cl-mode
2074 * ,----- BODY -----.
2075 * / \
2076 * V size > 0 V chk-mode
2077 * .--> SIZE -------------> DATA -------------> CRLF
2078 * | | size == 0 | last byte |
2079 * | v final crlf v inspected |
2080 * | TRAILERS -----------> DONE |
2081 * | |
2082 * `----------------------------------------------'
2083 *
2084 * Compression only happens in the DATA state, and must be flushed in final
2085 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2086 * is performed at once on final states for all bytes parsed, or when leaving
2087 * on missing data.
2088 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002089int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002090{
2091 struct session *sess = s->sess;
2092 struct http_txn *txn = s->txn;
2093 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002094 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002095 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002096
2097 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
2098 now_ms, __FUNCTION__,
2099 s,
2100 res,
2101 res->rex, res->wex,
2102 res->flags,
2103 ci_data(res),
2104 res->analysers);
2105
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002106 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002107
2108 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002109 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002110 /* Output closed while we were sending data. We must abort and
2111 * wake the other side up.
2112 */
2113 msg->err_state = msg->msg_state;
2114 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002115 http_end_response(s);
2116 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002117 return 1;
2118 }
2119
Christopher Faulet9768c262018-10-22 09:34:31 +02002120 if (msg->msg_state == HTTP_MSG_BODY)
2121 msg->msg_state = HTTP_MSG_DATA;
2122
Christopher Faulete0768eb2018-10-03 16:38:02 +02002123 /* in most states, we should abort in case of early close */
2124 channel_auto_close(res);
2125
Christopher Faulete0768eb2018-10-03 16:38:02 +02002126 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002127 if (res->to_forward == CHN_INFINITE_FORWARD) {
2128 if (res->flags & (CF_SHUTR|CF_EOI)) {
2129 msg->msg_state = HTTP_MSG_DONE;
2130 res->to_forward = 0;
2131 goto done;
2132 }
2133 }
2134 else {
2135 /* We can't process the buffer's contents yet */
2136 res->flags |= CF_WAKE_WRITE;
2137 goto missing_data_or_waiting;
2138 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002139 }
2140
Christopher Faulet9768c262018-10-22 09:34:31 +02002141 if (msg->msg_state >= HTTP_MSG_DONE)
2142 goto done;
2143
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002144 /* Forward input data. We get it by removing all outgoing data not
2145 * forwarded yet from HTX data size. If there are some data filters, we
2146 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002147 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002148 if (HAS_RSP_DATA_FILTERS(s)) {
2149 ret = flt_http_payload(s, msg, htx->data);
2150 if (ret < 0)
2151 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002152 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002153 }
2154 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002155 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002156 if (msg->flags & HTTP_MSGF_XFER_LEN)
2157 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002158 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002159
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002160 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2161 (!(msg->flags & HTTP_MSGF_XFER_LEN) && (res->flags & CF_SHUTR || !HAS_RSP_DATA_FILTERS(s)))) {
2162 msg->msg_state = HTTP_MSG_TUNNEL;
2163 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002164 }
2165
Christopher Faulet9768c262018-10-22 09:34:31 +02002166 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002167 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2168 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002169 */
2170 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2171 goto missing_data_or_waiting;
2172
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002173 msg->msg_state = HTTP_MSG_ENDING;
2174 if (htx->data != co_data(res))
2175 goto missing_data_or_waiting;
Christopher Faulet9768c262018-10-22 09:34:31 +02002176 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletaed68d42019-03-28 18:12:46 +01002177 res->to_forward = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002178
2179 done:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002180 /* other states, DONE...TUNNEL */
Christopher Faulet9768c262018-10-22 09:34:31 +02002181 channel_dont_close(res);
2182
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002183 if (HAS_RSP_DATA_FILTERS(s)) {
2184 ret = flt_http_end(s, msg);
2185 if (ret <= 0) {
2186 if (!ret)
2187 goto missing_data_or_waiting;
2188 goto return_bad_res;
2189 }
2190 }
2191
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002192 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002193 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002194 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002195 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2196 if (res->flags & CF_SHUTW) {
2197 /* response errors are most likely due to the
2198 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002199 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002200 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002201 goto return_bad_res;
2202 }
2203 return 1;
2204 }
2205 return 0;
2206
2207 missing_data_or_waiting:
2208 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002209 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002210
Christopher Faulet47365272018-10-31 17:40:50 +01002211 if (htx->flags & HTX_FL_PARSING_ERROR)
2212 goto return_bad_res;
2213
Christopher Faulete0768eb2018-10-03 16:38:02 +02002214 /* stop waiting for data if the input is closed before the end. If the
2215 * client side was already closed, it means that the client has aborted,
2216 * so we don't want to count this as a server abort. Otherwise it's a
2217 * server abort.
2218 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002219 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002220 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002221 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002222 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002223 if (htx_is_empty(htx))
2224 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002225 }
2226
Christopher Faulete0768eb2018-10-03 16:38:02 +02002227 /* When TE: chunked is used, we need to get there again to parse
2228 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002229 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2230 * are filters registered on the stream, we don't want to forward a
2231 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002232 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002233 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002234 channel_dont_close(res);
2235
2236 /* We know that more data are expected, but we couldn't send more that
2237 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2238 * system knows it must not set a PUSH on this first part. Interactive
2239 * modes are already handled by the stream sock layer. We must not do
2240 * this in content-length mode because it could present the MSG_MORE
2241 * flag with the last block of forwarded data, which would cause an
2242 * additional delay to be observed by the receiver.
2243 */
2244 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2245 res->flags |= CF_EXPECT_MORE;
2246
2247 /* the stream handler will take care of timeouts and errors */
2248 return 0;
2249
Christopher Faulet93e02d82019-03-08 14:18:50 +01002250 return_srv_abort:
2251 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2252 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002253 if (objt_server(s->target))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002254 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
2255 if (!(s->flags & SF_ERR_MASK))
2256 s->flags |= SF_ERR_SRVCL;
2257 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002258
Christopher Faulet93e02d82019-03-08 14:18:50 +01002259 return_cli_abort:
2260 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2261 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002262 if (objt_server(s->target))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002263 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
2264 if (!(s->flags & SF_ERR_MASK))
2265 s->flags |= SF_ERR_CLICL;
2266 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002267
Christopher Faulet93e02d82019-03-08 14:18:50 +01002268 return_bad_res:
2269 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2270 if (objt_server(s->target)) {
2271 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
2272 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2273 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002274 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002275 s->flags |= SF_ERR_SRVCL;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002276
Christopher Faulet93e02d82019-03-08 14:18:50 +01002277 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002278 txn->rsp.err_state = txn->rsp.msg_state;
2279 txn->rsp.msg_state = HTTP_MSG_ERROR;
2280 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002281 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002282 res->analysers &= AN_RES_FLT_END;
2283 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 +02002284 if (!(s->flags & SF_FINST_MASK))
2285 s->flags |= SF_FINST_D;
2286 return 0;
2287}
2288
Christopher Fauletf2824e62018-10-01 12:12:37 +02002289/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002290 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002291 * as too large a request to build a valid response.
2292 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002293int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002294{
Christopher Faulet99daf282018-11-28 22:58:13 +01002295 struct channel *req = &s->req;
2296 struct channel *res = &s->res;
2297 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002298 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002299 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002300 struct ist status, reason, location;
2301 unsigned int flags;
2302 size_t data;
Christopher Faulet08e66462019-05-23 16:44:59 +02002303 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002304
2305 chunk = alloc_trash_chunk();
2306 if (!chunk)
Christopher Faulet99daf282018-11-28 22:58:13 +01002307 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002308
Christopher Faulet99daf282018-11-28 22:58:13 +01002309 /*
2310 * Create the location
2311 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002312 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002313 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002314 case REDIRECT_TYPE_SCHEME: {
2315 struct http_hdr_ctx ctx;
2316 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002317
Christopher Faulet99daf282018-11-28 22:58:13 +01002318 host = ist("");
2319 ctx.blk = NULL;
2320 if (http_find_header(htx, ist("Host"), &ctx, 0))
2321 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002322
Christopher Faulet297fbb42019-05-13 14:41:27 +02002323 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002324 path = http_get_path(htx_sl_req_uri(sl));
2325 /* build message using path */
2326 if (path.ptr) {
2327 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2328 int qs = 0;
2329 while (qs < path.len) {
2330 if (*(path.ptr + qs) == '?') {
2331 path.len = qs;
2332 break;
2333 }
2334 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002335 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002336 }
2337 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002338 else
2339 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002340
Christopher Faulet99daf282018-11-28 22:58:13 +01002341 if (rule->rdr_str) { /* this is an old "redirect" rule */
2342 /* add scheme */
2343 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2344 goto fail;
2345 }
2346 else {
2347 /* add scheme with executing log format */
2348 chunk->data += build_logline(s, chunk->area + chunk->data,
2349 chunk->size - chunk->data,
2350 &rule->rdr_fmt);
2351 }
2352 /* add "://" + host + path */
2353 if (!chunk_memcat(chunk, "://", 3) ||
2354 !chunk_memcat(chunk, host.ptr, host.len) ||
2355 !chunk_memcat(chunk, path.ptr, path.len))
2356 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002357
Christopher Faulet99daf282018-11-28 22:58:13 +01002358 /* append a slash at the end of the location if needed and missing */
2359 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2360 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2361 if (chunk->data + 1 >= chunk->size)
2362 goto fail;
2363 chunk->area[chunk->data++] = '/';
2364 }
2365 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002366 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002367
Christopher Faulet99daf282018-11-28 22:58:13 +01002368 case REDIRECT_TYPE_PREFIX: {
2369 struct ist path;
2370
Christopher Faulet297fbb42019-05-13 14:41:27 +02002371 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002372 path = http_get_path(htx_sl_req_uri(sl));
2373 /* build message using path */
2374 if (path.ptr) {
2375 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2376 int qs = 0;
2377 while (qs < path.len) {
2378 if (*(path.ptr + qs) == '?') {
2379 path.len = qs;
2380 break;
2381 }
2382 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002383 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002384 }
2385 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002386 else
2387 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002388
Christopher Faulet99daf282018-11-28 22:58:13 +01002389 if (rule->rdr_str) { /* this is an old "redirect" rule */
2390 /* add prefix. Note that if prefix == "/", we don't want to
2391 * add anything, otherwise it makes it hard for the user to
2392 * configure a self-redirection.
2393 */
2394 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2395 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2396 goto fail;
2397 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002398 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002399 else {
2400 /* add prefix with executing log format */
2401 chunk->data += build_logline(s, chunk->area + chunk->data,
2402 chunk->size - chunk->data,
2403 &rule->rdr_fmt);
2404 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002405
Christopher Faulet99daf282018-11-28 22:58:13 +01002406 /* add path */
2407 if (!chunk_memcat(chunk, path.ptr, path.len))
2408 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002409
Christopher Faulet99daf282018-11-28 22:58:13 +01002410 /* append a slash at the end of the location if needed and missing */
2411 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2412 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2413 if (chunk->data + 1 >= chunk->size)
2414 goto fail;
2415 chunk->area[chunk->data++] = '/';
2416 }
2417 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002418 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002419 case REDIRECT_TYPE_LOCATION:
2420 default:
2421 if (rule->rdr_str) { /* this is an old "redirect" rule */
2422 /* add location */
2423 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2424 goto fail;
2425 }
2426 else {
2427 /* add location with executing log format */
2428 chunk->data += build_logline(s, chunk->area + chunk->data,
2429 chunk->size - chunk->data,
2430 &rule->rdr_fmt);
2431 }
2432 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002433 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002434 location = ist2(chunk->area, chunk->data);
2435
2436 /*
2437 * Create the 30x response
2438 */
2439 switch (rule->code) {
2440 case 308:
2441 status = ist("308");
2442 reason = ist("Permanent Redirect");
2443 break;
2444 case 307:
2445 status = ist("307");
2446 reason = ist("Temporary Redirect");
2447 break;
2448 case 303:
2449 status = ist("303");
2450 reason = ist("See Other");
2451 break;
2452 case 301:
2453 status = ist("301");
2454 reason = ist("Moved Permanently");
2455 break;
2456 case 302:
2457 default:
2458 status = ist("302");
2459 reason = ist("Found");
2460 break;
2461 }
2462
Christopher Faulet08e66462019-05-23 16:44:59 +02002463 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2464 close = 1;
2465
Christopher Faulet99daf282018-11-28 22:58:13 +01002466 htx = htx_from_buf(&res->buf);
2467 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2468 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2469 if (!sl)
2470 goto fail;
2471 sl->info.res.status = rule->code;
2472 s->txn->status = rule->code;
2473
Christopher Faulet08e66462019-05-23 16:44:59 +02002474 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2475 goto fail;
2476
2477 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002478 !htx_add_header(htx, ist("Location"), location))
2479 goto fail;
2480
2481 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2482 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2483 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002484 }
2485
2486 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002487 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2488 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002489 }
2490
Christopher Faulet99daf282018-11-28 22:58:13 +01002491 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2492 goto fail;
2493
Christopher Fauletf2824e62018-10-01 12:12:37 +02002494 /* let's log the request time */
2495 s->logs.tv_request = now;
2496
Christopher Faulet99daf282018-11-28 22:58:13 +01002497 data = htx->data - co_data(res);
Christopher Faulet99daf282018-11-28 22:58:13 +01002498 c_adv(res, data);
2499 res->total += data;
2500
2501 channel_auto_read(req);
2502 channel_abort(req);
2503 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002504 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet99daf282018-11-28 22:58:13 +01002505
2506 res->wex = tick_add_ifset(now_ms, res->wto);
2507 channel_auto_read(res);
2508 channel_auto_close(res);
2509 channel_shutr_now(res);
2510
2511 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002512
2513 if (!(s->flags & SF_ERR_MASK))
2514 s->flags |= SF_ERR_LOCAL;
2515 if (!(s->flags & SF_FINST_MASK))
2516 s->flags |= SF_FINST_R;
2517
Christopher Faulet99daf282018-11-28 22:58:13 +01002518 free_trash_chunk(chunk);
2519 return 1;
2520
2521 fail:
2522 /* If an error occurred, remove the incomplete HTTP response from the
2523 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002524 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002525 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002526 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002527}
2528
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002529int http_transform_header_str(struct stream* s, struct channel *chn, struct htx *htx,
2530 struct ist name, const char *str, struct my_regex *re, int action)
Christopher Faulet72333522018-10-24 11:25:02 +02002531{
2532 struct http_hdr_ctx ctx;
2533 struct buffer *output = get_trash_chunk();
2534
2535 /* find full header is action is ACT_HTTP_REPLACE_HDR */
2536 ctx.blk = NULL;
2537 while (http_find_header(htx, name, &ctx, (action == ACT_HTTP_REPLACE_HDR))) {
2538 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2539 continue;
2540
2541 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2542 if (output->data == -1)
2543 return -1;
2544 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2545 return -1;
2546 }
2547 return 0;
2548}
2549
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002550static int http_transform_header(struct stream* s, struct channel *chn, struct htx *htx,
2551 const struct ist name, struct list *fmt, struct my_regex *re, int action)
Christopher Faulet72333522018-10-24 11:25:02 +02002552{
2553 struct buffer *replace;
2554 int ret = -1;
2555
2556 replace = alloc_trash_chunk();
2557 if (!replace)
2558 goto leave;
2559
2560 replace->data = build_logline(s, replace->area, replace->size, fmt);
2561 if (replace->data >= replace->size - 1)
2562 goto leave;
2563
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002564 ret = http_transform_header_str(s, chn, htx, name, replace->area, re, action);
Christopher Faulet72333522018-10-24 11:25:02 +02002565
2566 leave:
2567 free_trash_chunk(replace);
2568 return ret;
2569}
2570
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002571
2572/* Terminate a 103-Erly-hints response and send it to the client. It returns 0
2573 * on success and -1 on error. The response channel is updated accordingly.
2574 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002575static int http_reply_103_early_hints(struct channel *res)
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002576{
2577 struct htx *htx = htx_from_buf(&res->buf);
2578 size_t data;
2579
Christopher Faulet1d5ec092019-06-26 14:23:54 +02002580 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002581 /* If an error occurred during an Early-hint rule,
2582 * remove the incomplete HTTP 103 response from the
2583 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002584 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002585 return -1;
2586 }
2587
2588 data = htx->data - co_data(res);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002589 c_adv(res, data);
2590 res->total += data;
2591 return 0;
2592}
2593
Christopher Faulet6eb92892018-11-15 16:39:29 +01002594/*
2595 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
2596 * built according to <fmt> log line format.
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002597 * If <early_hints> is 0, it is starts a new response by adding the start
2598 * line. If an error occurred -1 is returned. On success 0 is returned. The
2599 * channel is not updated here. It must be done calling the function
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002600 * http_reply_103_early_hints().
Christopher Faulet6eb92892018-11-15 16:39:29 +01002601 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002602static 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 +01002603{
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002604 struct channel *res = &s->res;
2605 struct htx *htx = htx_from_buf(&res->buf);
2606 struct buffer *value = alloc_trash_chunk();
2607
Christopher Faulet6eb92892018-11-15 16:39:29 +01002608 if (!early_hints) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002609 struct htx_sl *sl;
2610 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
2611 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2612
2613 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
2614 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
2615 if (!sl)
Christopher Faulet6eb92892018-11-15 16:39:29 +01002616 goto fail;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002617 sl->info.res.status = 103;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002618 }
2619
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002620 value->data = build_logline(s, b_tail(value), b_room(value), fmt);
2621 if (!htx_add_header(htx, name, ist2(b_head(value), b_data(value))))
Christopher Faulet6eb92892018-11-15 16:39:29 +01002622 goto fail;
2623
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002624 free_trash_chunk(value);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002625 return 1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002626
2627 fail:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002628 /* If an error occurred during an Early-hint rule, remove the incomplete
2629 * HTTP 103 response from the buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002630 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002631 free_trash_chunk(value);
2632 return -1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002633}
2634
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002635/* This function executes one of the set-{method,path,query,uri} actions. It
2636 * takes the string from the variable 'replace' with length 'len', then modifies
2637 * the relevant part of the request line accordingly. Then it updates various
2638 * pointers to the next elements which were moved, and the total buffer length.
2639 * It finds the action to be performed in p[2], previously filled by function
2640 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2641 * error, though this can be revisited when this code is finally exploited.
2642 *
2643 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2644 * query string and 3 to replace uri.
2645 *
2646 * In query string case, the mark question '?' must be set at the start of the
2647 * string by the caller, event if the replacement query string is empty.
2648 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002649int http_req_replace_stline(int action, const char *replace, int len,
2650 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002651{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002652 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002653
2654 switch (action) {
2655 case 0: // method
2656 if (!http_replace_req_meth(htx, ist2(replace, len)))
2657 return -1;
2658 break;
2659
2660 case 1: // path
2661 if (!http_replace_req_path(htx, ist2(replace, len)))
2662 return -1;
2663 break;
2664
2665 case 2: // query
2666 if (!http_replace_req_query(htx, ist2(replace, len)))
2667 return -1;
2668 break;
2669
2670 case 3: // uri
2671 if (!http_replace_req_uri(htx, ist2(replace, len)))
2672 return -1;
2673 break;
2674
2675 default:
2676 return -1;
2677 }
2678 return 0;
2679}
2680
2681/* This function replace the HTTP status code and the associated message. The
2682 * variable <status> contains the new status code. This function never fails.
2683 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002684void http_res_set_status(unsigned int status, const char *reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002685{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002686 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002687 char *res;
2688
2689 chunk_reset(&trash);
2690 res = ultoa_o(status, trash.area, trash.size);
2691 trash.data = res - trash.area;
2692
2693 /* Do we have a custom reason format string? */
2694 if (reason == NULL)
2695 reason = http_get_reason(status);
2696
Christopher Faulet87a2c0d2018-12-13 21:58:18 +01002697 if (http_replace_res_status(htx, ist2(trash.area, trash.data)))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002698 http_replace_res_reason(htx, ist2(reason, strlen(reason)));
2699}
2700
Christopher Faulet3e964192018-10-24 11:39:23 +02002701/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2702 * transaction <txn>. Returns the verdict of the first rule that prevents
2703 * further processing of the request (auth, deny, ...), and defaults to
2704 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2705 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2706 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2707 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2708 * status.
2709 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002710static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
2711 struct stream *s, int *deny_status)
Christopher Faulet3e964192018-10-24 11:39:23 +02002712{
2713 struct session *sess = strm_sess(s);
2714 struct http_txn *txn = s->txn;
2715 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002716 struct act_rule *rule;
2717 struct http_hdr_ctx ctx;
2718 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002719 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2720 int act_flags = 0;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002721 int early_hints = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002722
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002723 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002724
2725 /* If "the current_rule_list" match the executed rule list, we are in
2726 * resume condition. If a resume is needed it is always in the action
2727 * and never in the ACL or converters. In this case, we initialise the
2728 * current rule, and go to the action execution point.
2729 */
2730 if (s->current_rule) {
2731 rule = s->current_rule;
2732 s->current_rule = NULL;
2733 if (s->current_rule_list == rules)
2734 goto resume_execution;
2735 }
2736 s->current_rule_list = rules;
2737
2738 list_for_each_entry(rule, rules, list) {
2739 /* check optional condition */
2740 if (rule->cond) {
2741 int ret;
2742
2743 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2744 ret = acl_pass(ret);
2745
2746 if (rule->cond->pol == ACL_COND_UNLESS)
2747 ret = !ret;
2748
2749 if (!ret) /* condition not matched */
2750 continue;
2751 }
2752
2753 act_flags |= ACT_FLAG_FIRST;
2754 resume_execution:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002755 if (early_hints && rule->action != ACT_HTTP_EARLY_HINT) {
2756 early_hints = 0;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002757 if (http_reply_103_early_hints(&s->res) == -1) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002758 rule_ret = HTTP_RULE_RES_BADREQ;
2759 goto end;
2760 }
2761 }
2762
Christopher Faulet3e964192018-10-24 11:39:23 +02002763 switch (rule->action) {
2764 case ACT_ACTION_ALLOW:
2765 rule_ret = HTTP_RULE_RES_STOP;
2766 goto end;
2767
2768 case ACT_ACTION_DENY:
2769 if (deny_status)
2770 *deny_status = rule->deny_status;
2771 rule_ret = HTTP_RULE_RES_DENY;
2772 goto end;
2773
2774 case ACT_HTTP_REQ_TARPIT:
2775 txn->flags |= TX_CLTARPIT;
2776 if (deny_status)
2777 *deny_status = rule->deny_status;
2778 rule_ret = HTTP_RULE_RES_DENY;
2779 goto end;
2780
2781 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002782 /* Auth might be performed on regular http-req rules as well as on stats */
2783 auth_realm = rule->arg.auth.realm;
2784 if (!auth_realm) {
2785 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2786 auth_realm = STATS_DEFAULT_REALM;
2787 else
2788 auth_realm = px->id;
2789 }
2790 /* send 401/407 depending on whether we use a proxy or not. We still
2791 * count one error, because normal browsing won't significantly
2792 * increase the counter but brute force attempts will.
2793 */
Christopher Faulet3e964192018-10-24 11:39:23 +02002794 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002795 if (http_reply_40x_unauthorized(s, auth_realm) == -1)
Christopher Faulet12c51e22018-11-28 15:59:42 +01002796 rule_ret = HTTP_RULE_RES_BADREQ;
2797 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02002798 goto end;
2799
2800 case ACT_HTTP_REDIR:
Christopher Faulet3e964192018-10-24 11:39:23 +02002801 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002802 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3e964192018-10-24 11:39:23 +02002803 rule_ret = HTTP_RULE_RES_BADREQ;
2804 goto end;
2805
2806 case ACT_HTTP_SET_NICE:
2807 s->task->nice = rule->arg.nice;
2808 break;
2809
2810 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01002811 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Christopher Faulet3e964192018-10-24 11:39:23 +02002812 break;
2813
2814 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01002815 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Christopher Faulet3e964192018-10-24 11:39:23 +02002816 break;
2817
2818 case ACT_HTTP_SET_LOGL:
2819 s->logs.level = rule->arg.loglevel;
2820 break;
2821
2822 case ACT_HTTP_REPLACE_HDR:
2823 case ACT_HTTP_REPLACE_VAL:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002824 if (http_transform_header(s, &s->req, htx,
2825 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
2826 &rule->arg.hdr_add.fmt,
2827 rule->arg.hdr_add.re, rule->action)) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002828 rule_ret = HTTP_RULE_RES_BADREQ;
2829 goto end;
2830 }
2831 break;
2832
2833 case ACT_HTTP_DEL_HDR:
2834 /* remove all occurrences of the header */
2835 ctx.blk = NULL;
2836 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
2837 http_remove_header(htx, &ctx);
2838 break;
2839
2840 case ACT_HTTP_SET_HDR:
2841 case ACT_HTTP_ADD_HDR: {
2842 /* The scope of the trash buffer must be limited to this function. The
2843 * build_logline() function can execute a lot of other function which
2844 * can use the trash buffer. So for limiting the scope of this global
2845 * buffer, we build first the header value using build_logline, and
2846 * after we store the header name.
2847 */
2848 struct buffer *replace;
2849 struct ist n, v;
2850
2851 replace = alloc_trash_chunk();
2852 if (!replace) {
2853 rule_ret = HTTP_RULE_RES_BADREQ;
2854 goto end;
2855 }
2856
2857 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
2858 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2859 v = ist2(replace->area, replace->data);
2860
2861 if (rule->action == ACT_HTTP_SET_HDR) {
2862 /* remove all occurrences of the header */
2863 ctx.blk = NULL;
2864 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
2865 http_remove_header(htx, &ctx);
2866 }
2867
2868 if (!http_add_header(htx, n, v)) {
2869 static unsigned char rate_limit = 0;
2870
2871 if ((rate_limit++ & 255) == 0) {
2872 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);
2873 }
2874
Olivier Houcharda798bf52019-03-08 18:52:00 +01002875 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002876 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002877 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002878 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002879 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002880 }
2881 free_trash_chunk(replace);
2882 break;
2883 }
2884
2885 case ACT_HTTP_DEL_ACL:
2886 case ACT_HTTP_DEL_MAP: {
2887 struct pat_ref *ref;
2888 struct buffer *key;
2889
2890 /* collect reference */
2891 ref = pat_ref_lookup(rule->arg.map.ref);
2892 if (!ref)
2893 continue;
2894
2895 /* allocate key */
2896 key = alloc_trash_chunk();
2897 if (!key) {
2898 rule_ret = HTTP_RULE_RES_BADREQ;
2899 goto end;
2900 }
2901
2902 /* collect key */
2903 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2904 key->area[key->data] = '\0';
2905
2906 /* perform update */
2907 /* returned code: 1=ok, 0=ko */
2908 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
2909 pat_ref_delete(ref, key->area);
2910 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
2911
2912 free_trash_chunk(key);
2913 break;
2914 }
2915
2916 case ACT_HTTP_ADD_ACL: {
2917 struct pat_ref *ref;
2918 struct buffer *key;
2919
2920 /* collect reference */
2921 ref = pat_ref_lookup(rule->arg.map.ref);
2922 if (!ref)
2923 continue;
2924
2925 /* allocate key */
2926 key = alloc_trash_chunk();
2927 if (!key) {
2928 rule_ret = HTTP_RULE_RES_BADREQ;
2929 goto end;
2930 }
2931
2932 /* collect key */
2933 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2934 key->area[key->data] = '\0';
2935
2936 /* perform update */
2937 /* add entry only if it does not already exist */
2938 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
2939 if (pat_ref_find_elt(ref, key->area) == NULL)
2940 pat_ref_add(ref, key->area, NULL, NULL);
2941 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
2942
2943 free_trash_chunk(key);
2944 break;
2945 }
2946
2947 case ACT_HTTP_SET_MAP: {
2948 struct pat_ref *ref;
2949 struct buffer *key, *value;
2950
2951 /* collect reference */
2952 ref = pat_ref_lookup(rule->arg.map.ref);
2953 if (!ref)
2954 continue;
2955
2956 /* allocate key */
2957 key = alloc_trash_chunk();
2958 if (!key) {
2959 rule_ret = HTTP_RULE_RES_BADREQ;
2960 goto end;
2961 }
2962
2963 /* allocate value */
2964 value = alloc_trash_chunk();
2965 if (!value) {
2966 free_trash_chunk(key);
2967 rule_ret = HTTP_RULE_RES_BADREQ;
2968 goto end;
2969 }
2970
2971 /* collect key */
2972 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2973 key->area[key->data] = '\0';
2974
2975 /* collect value */
2976 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
2977 value->area[value->data] = '\0';
2978
2979 /* perform update */
Christopher Faulete84289e2019-04-19 14:50:55 +02002980 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02002981 if (pat_ref_find_elt(ref, key->area) != NULL)
2982 /* update entry if it exists */
2983 pat_ref_set(ref, key->area, value->area, NULL);
2984 else
2985 /* insert a new entry */
2986 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02002987 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02002988 free_trash_chunk(key);
2989 free_trash_chunk(value);
2990 break;
2991 }
2992
2993 case ACT_HTTP_EARLY_HINT:
2994 if (!(txn->req.flags & HTTP_MSGF_VER_11))
2995 break;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002996 early_hints = http_add_early_hint_header(s, early_hints,
2997 ist2(rule->arg.early_hint.name, rule->arg.early_hint.name_len),
2998 &rule->arg.early_hint.fmt);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002999 if (early_hints == -1) {
3000 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02003001 goto end;
3002 }
3003 break;
3004
3005 case ACT_CUSTOM:
3006 if ((s->req.flags & CF_READ_ERROR) ||
3007 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Christopher Faulet3e964192018-10-24 11:39:23 +02003008 (px->options & PR_O_ABRT_CLOSE)))
3009 act_flags |= ACT_FLAG_FINAL;
3010
3011 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
3012 case ACT_RET_ERR:
3013 case ACT_RET_CONT:
3014 break;
3015 case ACT_RET_STOP:
Christopher Faulet8f1aa772019-07-04 11:27:15 +02003016 rule_ret = HTTP_RULE_RES_STOP;
3017 goto end;
3018 case ACT_RET_DONE:
Christopher Faulet3e964192018-10-24 11:39:23 +02003019 rule_ret = HTTP_RULE_RES_DONE;
3020 goto end;
3021 case ACT_RET_YIELD:
3022 s->current_rule = rule;
3023 rule_ret = HTTP_RULE_RES_YIELD;
3024 goto end;
3025 }
3026 break;
3027
3028 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3029 /* Note: only the first valid tracking parameter of each
3030 * applies.
3031 */
3032
3033 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3034 struct stktable *t;
3035 struct stksess *ts;
3036 struct stktable_key *key;
3037 void *ptr1, *ptr2;
3038
3039 t = rule->arg.trk_ctr.table.t;
3040 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL,
3041 rule->arg.trk_ctr.expr, NULL);
3042
3043 if (key && (ts = stktable_get_entry(t, key))) {
3044 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3045
3046 /* let's count a new HTTP request as it's the first time we do it */
3047 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3048 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3049 if (ptr1 || ptr2) {
3050 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3051
3052 if (ptr1)
3053 stktable_data_cast(ptr1, http_req_cnt)++;
3054
3055 if (ptr2)
3056 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
3057 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3058
3059 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3060
3061 /* If data was modified, we need to touch to re-schedule sync */
3062 stktable_touch_local(t, ts, 0);
3063 }
3064
3065 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3066 if (sess->fe != s->be)
3067 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3068 }
3069 }
3070 break;
3071
Joseph Herlantc42c0e92018-11-25 10:43:27 -08003072 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003073 default:
3074 break;
3075 }
3076 }
3077
3078 end:
3079 if (early_hints) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003080 if (http_reply_103_early_hints(&s->res) == -1)
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003081 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02003082 }
3083
3084 /* we reached the end of the rules, nothing to report */
3085 return rule_ret;
3086}
3087
3088/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3089 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3090 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3091 * is returned, the process can continue the evaluation of next rule list. If
3092 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3093 * is returned, it means the operation could not be processed and a server error
3094 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3095 * deny rule. If *YIELD is returned, the caller must call again the function
3096 * with the same context.
3097 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003098static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
3099 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02003100{
3101 struct session *sess = strm_sess(s);
3102 struct http_txn *txn = s->txn;
3103 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02003104 struct act_rule *rule;
3105 struct http_hdr_ctx ctx;
3106 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
3107 int act_flags = 0;
3108
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003109 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02003110
3111 /* If "the current_rule_list" match the executed rule list, we are in
3112 * resume condition. If a resume is needed it is always in the action
3113 * and never in the ACL or converters. In this case, we initialise the
3114 * current rule, and go to the action execution point.
3115 */
3116 if (s->current_rule) {
3117 rule = s->current_rule;
3118 s->current_rule = NULL;
3119 if (s->current_rule_list == rules)
3120 goto resume_execution;
3121 }
3122 s->current_rule_list = rules;
3123
3124 list_for_each_entry(rule, rules, list) {
3125 /* check optional condition */
3126 if (rule->cond) {
3127 int ret;
3128
3129 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3130 ret = acl_pass(ret);
3131
3132 if (rule->cond->pol == ACL_COND_UNLESS)
3133 ret = !ret;
3134
3135 if (!ret) /* condition not matched */
3136 continue;
3137 }
3138
3139 act_flags |= ACT_FLAG_FIRST;
3140resume_execution:
3141 switch (rule->action) {
3142 case ACT_ACTION_ALLOW:
3143 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3144 goto end;
3145
3146 case ACT_ACTION_DENY:
3147 txn->flags |= TX_SVDENY;
3148 rule_ret = HTTP_RULE_RES_STOP;
3149 goto end;
3150
3151 case ACT_HTTP_SET_NICE:
3152 s->task->nice = rule->arg.nice;
3153 break;
3154
3155 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003156 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Christopher Faulet3e964192018-10-24 11:39:23 +02003157 break;
3158
3159 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003160 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Christopher Faulet3e964192018-10-24 11:39:23 +02003161 break;
3162
3163 case ACT_HTTP_SET_LOGL:
3164 s->logs.level = rule->arg.loglevel;
3165 break;
3166
3167 case ACT_HTTP_REPLACE_HDR:
3168 case ACT_HTTP_REPLACE_VAL:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003169 if (http_transform_header(s, &s->res, htx,
3170 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
3171 &rule->arg.hdr_add.fmt,
3172 rule->arg.hdr_add.re, rule->action)) {
Christopher Faulet3e964192018-10-24 11:39:23 +02003173 rule_ret = HTTP_RULE_RES_BADREQ;
3174 goto end;
3175 }
3176 break;
3177
3178 case ACT_HTTP_DEL_HDR:
3179 /* remove all occurrences of the header */
3180 ctx.blk = NULL;
3181 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3182 http_remove_header(htx, &ctx);
3183 break;
3184
3185 case ACT_HTTP_SET_HDR:
3186 case ACT_HTTP_ADD_HDR: {
3187 struct buffer *replace;
3188 struct ist n, v;
3189
3190 replace = alloc_trash_chunk();
3191 if (!replace) {
3192 rule_ret = HTTP_RULE_RES_BADREQ;
3193 goto end;
3194 }
3195
3196 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
3197 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3198 v = ist2(replace->area, replace->data);
3199
3200 if (rule->action == ACT_HTTP_SET_HDR) {
3201 /* remove all occurrences of the header */
3202 ctx.blk = NULL;
3203 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3204 http_remove_header(htx, &ctx);
3205 }
3206
3207 if (!http_add_header(htx, n, v)) {
3208 static unsigned char rate_limit = 0;
3209
3210 if ((rate_limit++ & 255) == 0) {
3211 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);
3212 }
3213
Olivier Houcharda798bf52019-03-08 18:52:00 +01003214 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003215 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003216 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003217 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003218 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003219 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01003220 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003221 }
3222 free_trash_chunk(replace);
3223 break;
3224 }
3225
3226 case ACT_HTTP_DEL_ACL:
3227 case ACT_HTTP_DEL_MAP: {
3228 struct pat_ref *ref;
3229 struct buffer *key;
3230
3231 /* collect reference */
3232 ref = pat_ref_lookup(rule->arg.map.ref);
3233 if (!ref)
3234 continue;
3235
3236 /* allocate key */
3237 key = alloc_trash_chunk();
3238 if (!key) {
3239 rule_ret = HTTP_RULE_RES_BADREQ;
3240 goto end;
3241 }
3242
3243 /* collect key */
3244 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3245 key->area[key->data] = '\0';
3246
3247 /* perform update */
3248 /* returned code: 1=ok, 0=ko */
3249 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3250 pat_ref_delete(ref, key->area);
3251 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3252
3253 free_trash_chunk(key);
3254 break;
3255 }
3256
3257 case ACT_HTTP_ADD_ACL: {
3258 struct pat_ref *ref;
3259 struct buffer *key;
3260
3261 /* collect reference */
3262 ref = pat_ref_lookup(rule->arg.map.ref);
3263 if (!ref)
3264 continue;
3265
3266 /* allocate key */
3267 key = alloc_trash_chunk();
3268 if (!key) {
3269 rule_ret = HTTP_RULE_RES_BADREQ;
3270 goto end;
3271 }
3272
3273 /* collect key */
3274 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3275 key->area[key->data] = '\0';
3276
3277 /* perform update */
3278 /* check if the entry already exists */
Christopher Faulete84289e2019-04-19 14:50:55 +02003279 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003280 if (pat_ref_find_elt(ref, key->area) == NULL)
3281 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02003282 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003283 free_trash_chunk(key);
3284 break;
3285 }
3286
3287 case ACT_HTTP_SET_MAP: {
3288 struct pat_ref *ref;
3289 struct buffer *key, *value;
3290
3291 /* collect reference */
3292 ref = pat_ref_lookup(rule->arg.map.ref);
3293 if (!ref)
3294 continue;
3295
3296 /* allocate key */
3297 key = alloc_trash_chunk();
3298 if (!key) {
3299 rule_ret = HTTP_RULE_RES_BADREQ;
3300 goto end;
3301 }
3302
3303 /* allocate value */
3304 value = alloc_trash_chunk();
3305 if (!value) {
3306 free_trash_chunk(key);
3307 rule_ret = HTTP_RULE_RES_BADREQ;
3308 goto end;
3309 }
3310
3311 /* collect key */
3312 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3313 key->area[key->data] = '\0';
3314
3315 /* collect value */
3316 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3317 value->area[value->data] = '\0';
3318
3319 /* perform update */
3320 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3321 if (pat_ref_find_elt(ref, key->area) != NULL)
3322 /* update entry if it exists */
3323 pat_ref_set(ref, key->area, value->area, NULL);
3324 else
3325 /* insert a new entry */
3326 pat_ref_add(ref, key->area, value->area, NULL);
3327 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3328 free_trash_chunk(key);
3329 free_trash_chunk(value);
3330 break;
3331 }
3332
3333 case ACT_HTTP_REDIR:
3334 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003335 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3e964192018-10-24 11:39:23 +02003336 rule_ret = HTTP_RULE_RES_BADREQ;
3337 goto end;
3338
3339 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3340 /* Note: only the first valid tracking parameter of each
3341 * applies.
3342 */
3343 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3344 struct stktable *t;
3345 struct stksess *ts;
3346 struct stktable_key *key;
3347 void *ptr;
3348
3349 t = rule->arg.trk_ctr.table.t;
3350 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL,
3351 rule->arg.trk_ctr.expr, NULL);
3352
3353 if (key && (ts = stktable_get_entry(t, key))) {
3354 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3355
3356 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3357
3358 /* let's count a new HTTP request as it's the first time we do it */
3359 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3360 if (ptr)
3361 stktable_data_cast(ptr, http_req_cnt)++;
3362
3363 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3364 if (ptr)
3365 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3366 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3367
3368 /* When the client triggers a 4xx from the server, it's most often due
3369 * to a missing object or permission. These events should be tracked
3370 * because if they happen often, it may indicate a brute force or a
3371 * vulnerability scan. Normally this is done when receiving the response
3372 * but here we're tracking after this ought to have been done so we have
3373 * to do it on purpose.
3374 */
3375 if ((unsigned)(txn->status - 400) < 100) {
3376 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3377 if (ptr)
3378 stktable_data_cast(ptr, http_err_cnt)++;
3379
3380 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3381 if (ptr)
3382 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3383 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3384 }
3385
3386 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3387
3388 /* If data was modified, we need to touch to re-schedule sync */
3389 stktable_touch_local(t, ts, 0);
3390
3391 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3392 if (sess->fe != s->be)
3393 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3394 }
3395 }
3396 break;
3397
3398 case ACT_CUSTOM:
3399 if ((s->req.flags & CF_READ_ERROR) ||
3400 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Christopher Faulet3e964192018-10-24 11:39:23 +02003401 (px->options & PR_O_ABRT_CLOSE)))
3402 act_flags |= ACT_FLAG_FINAL;
3403
3404 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
3405 case ACT_RET_ERR:
3406 case ACT_RET_CONT:
3407 break;
3408 case ACT_RET_STOP:
3409 rule_ret = HTTP_RULE_RES_STOP;
3410 goto end;
Christopher Faulet8f1aa772019-07-04 11:27:15 +02003411 case ACT_RET_DONE:
3412 rule_ret = HTTP_RULE_RES_DONE;
3413 goto end;
Christopher Faulet3e964192018-10-24 11:39:23 +02003414 case ACT_RET_YIELD:
3415 s->current_rule = rule;
3416 rule_ret = HTTP_RULE_RES_YIELD;
3417 goto end;
3418 }
3419 break;
3420
Joseph Herlantc42c0e92018-11-25 10:43:27 -08003421 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003422 default:
3423 break;
3424 }
3425 }
3426
3427 end:
3428 /* we reached the end of the rules, nothing to report */
3429 return rule_ret;
3430}
3431
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003432/*
3433 * Manage client-side cookie. It can impact performance by about 2% so it is
3434 * desirable to call it only when needed. This code is quite complex because
3435 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3436 * highly recommended not to touch this part without a good reason !
3437 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003438static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003439{
3440 struct session *sess = s->sess;
3441 struct http_txn *txn = s->txn;
3442 struct htx *htx;
3443 struct http_hdr_ctx ctx;
3444 char *hdr_beg, *hdr_end, *del_from;
3445 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3446 int preserve_hdr;
3447
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003448 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003449 ctx.blk = NULL;
3450 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003451 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003452 del_from = NULL; /* nothing to be deleted */
3453 preserve_hdr = 0; /* assume we may kill the whole header */
3454
3455 /* Now look for cookies. Conforming to RFC2109, we have to support
3456 * attributes whose name begin with a '$', and associate them with
3457 * the right cookie, if we want to delete this cookie.
3458 * So there are 3 cases for each cookie read :
3459 * 1) it's a special attribute, beginning with a '$' : ignore it.
3460 * 2) it's a server id cookie that we *MAY* want to delete : save
3461 * some pointers on it (last semi-colon, beginning of cookie...)
3462 * 3) it's an application cookie : we *MAY* have to delete a previous
3463 * "special" cookie.
3464 * At the end of loop, if a "special" cookie remains, we may have to
3465 * remove it. If no application cookie persists in the header, we
3466 * *MUST* delete it.
3467 *
3468 * Note: RFC2965 is unclear about the processing of spaces around
3469 * the equal sign in the ATTR=VALUE form. A careful inspection of
3470 * the RFC explicitly allows spaces before it, and not within the
3471 * tokens (attrs or values). An inspection of RFC2109 allows that
3472 * too but section 10.1.3 lets one think that spaces may be allowed
3473 * after the equal sign too, resulting in some (rare) buggy
3474 * implementations trying to do that. So let's do what servers do.
3475 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3476 * allowed quoted strings in values, with any possible character
3477 * after a backslash, including control chars and delimitors, which
3478 * causes parsing to become ambiguous. Browsers also allow spaces
3479 * within values even without quotes.
3480 *
3481 * We have to keep multiple pointers in order to support cookie
3482 * removal at the beginning, middle or end of header without
3483 * corrupting the header. All of these headers are valid :
3484 *
3485 * hdr_beg hdr_end
3486 * | |
3487 * v |
3488 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3489 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3490 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3491 * | | | | | | |
3492 * | | | | | | |
3493 * | | | | | | +--> next
3494 * | | | | | +----> val_end
3495 * | | | | +-----------> val_beg
3496 * | | | +--------------> equal
3497 * | | +----------------> att_end
3498 * | +---------------------> att_beg
3499 * +--------------------------> prev
3500 *
3501 */
3502 hdr_beg = ctx.value.ptr;
3503 hdr_end = hdr_beg + ctx.value.len;
3504 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3505 /* Iterate through all cookies on this line */
3506
3507 /* find att_beg */
3508 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003509 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003510 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003511 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003512
3513 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3514 att_beg++;
3515
3516 /* find att_end : this is the first character after the last non
3517 * space before the equal. It may be equal to hdr_end.
3518 */
3519 equal = att_end = att_beg;
3520 while (equal < hdr_end) {
3521 if (*equal == '=' || *equal == ',' || *equal == ';')
3522 break;
3523 if (HTTP_IS_SPHT(*equal++))
3524 continue;
3525 att_end = equal;
3526 }
3527
3528 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3529 * is between <att_beg> and <equal>, both may be identical.
3530 */
3531 /* look for end of cookie if there is an equal sign */
3532 if (equal < hdr_end && *equal == '=') {
3533 /* look for the beginning of the value */
3534 val_beg = equal + 1;
3535 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3536 val_beg++;
3537
3538 /* find the end of the value, respecting quotes */
3539 next = http_find_cookie_value_end(val_beg, hdr_end);
3540
3541 /* make val_end point to the first white space or delimitor after the value */
3542 val_end = next;
3543 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3544 val_end--;
3545 }
3546 else
3547 val_beg = val_end = next = equal;
3548
3549 /* We have nothing to do with attributes beginning with
3550 * '$'. However, they will automatically be removed if a
3551 * header before them is removed, since they're supposed
3552 * to be linked together.
3553 */
3554 if (*att_beg == '$')
3555 continue;
3556
3557 /* Ignore cookies with no equal sign */
3558 if (equal == next) {
3559 /* This is not our cookie, so we must preserve it. But if we already
3560 * scheduled another cookie for removal, we cannot remove the
3561 * complete header, but we can remove the previous block itself.
3562 */
3563 preserve_hdr = 1;
3564 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003565 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003566 val_end += delta;
3567 next += delta;
3568 hdr_end += delta;
3569 prev = del_from;
3570 del_from = NULL;
3571 }
3572 continue;
3573 }
3574
3575 /* if there are spaces around the equal sign, we need to
3576 * strip them otherwise we'll get trouble for cookie captures,
3577 * or even for rewrites. Since this happens extremely rarely,
3578 * it does not hurt performance.
3579 */
3580 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3581 int stripped_before = 0;
3582 int stripped_after = 0;
3583
3584 if (att_end != equal) {
3585 memmove(att_end, equal, hdr_end - equal);
3586 stripped_before = (att_end - equal);
3587 equal += stripped_before;
3588 val_beg += stripped_before;
3589 }
3590
3591 if (val_beg > equal + 1) {
3592 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3593 stripped_after = (equal + 1) - val_beg;
3594 val_beg += stripped_after;
3595 stripped_before += stripped_after;
3596 }
3597
3598 val_end += stripped_before;
3599 next += stripped_before;
3600 hdr_end += stripped_before;
3601 }
3602 /* now everything is as on the diagram above */
3603
3604 /* First, let's see if we want to capture this cookie. We check
3605 * that we don't already have a client side cookie, because we
3606 * can only capture one. Also as an optimisation, we ignore
3607 * cookies shorter than the declared name.
3608 */
3609 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3610 (val_end - att_beg >= sess->fe->capture_namelen) &&
3611 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3612 int log_len = val_end - att_beg;
3613
3614 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3615 ha_alert("HTTP logging : out of memory.\n");
3616 } else {
3617 if (log_len > sess->fe->capture_len)
3618 log_len = sess->fe->capture_len;
3619 memcpy(txn->cli_cookie, att_beg, log_len);
3620 txn->cli_cookie[log_len] = 0;
3621 }
3622 }
3623
3624 /* Persistence cookies in passive, rewrite or insert mode have the
3625 * following form :
3626 *
3627 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3628 *
3629 * For cookies in prefix mode, the form is :
3630 *
3631 * Cookie: NAME=SRV~VALUE
3632 */
3633 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3634 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3635 struct server *srv = s->be->srv;
3636 char *delim;
3637
3638 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3639 * have the server ID between val_beg and delim, and the original cookie between
3640 * delim+1 and val_end. Otherwise, delim==val_end :
3641 *
3642 * hdr_beg
3643 * |
3644 * v
3645 * NAME=SRV; # in all but prefix modes
3646 * NAME=SRV~OPAQUE ; # in prefix mode
3647 * || || | |+-> next
3648 * || || | +--> val_end
3649 * || || +---------> delim
3650 * || |+------------> val_beg
3651 * || +-------------> att_end = equal
3652 * |+-----------------> att_beg
3653 * +------------------> prev
3654 *
3655 */
3656 if (s->be->ck_opts & PR_CK_PFX) {
3657 for (delim = val_beg; delim < val_end; delim++)
3658 if (*delim == COOKIE_DELIM)
3659 break;
3660 }
3661 else {
3662 char *vbar1;
3663 delim = val_end;
3664 /* Now check if the cookie contains a date field, which would
3665 * appear after a vertical bar ('|') just after the server name
3666 * and before the delimiter.
3667 */
3668 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3669 if (vbar1) {
3670 /* OK, so left of the bar is the server's cookie and
3671 * right is the last seen date. It is a base64 encoded
3672 * 30-bit value representing the UNIX date since the
3673 * epoch in 4-second quantities.
3674 */
3675 int val;
3676 delim = vbar1++;
3677 if (val_end - vbar1 >= 5) {
3678 val = b64tos30(vbar1);
3679 if (val > 0)
3680 txn->cookie_last_date = val << 2;
3681 }
3682 /* look for a second vertical bar */
3683 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3684 if (vbar1 && (val_end - vbar1 > 5)) {
3685 val = b64tos30(vbar1 + 1);
3686 if (val > 0)
3687 txn->cookie_first_date = val << 2;
3688 }
3689 }
3690 }
3691
3692 /* if the cookie has an expiration date and the proxy wants to check
3693 * it, then we do that now. We first check if the cookie is too old,
3694 * then only if it has expired. We detect strict overflow because the
3695 * time resolution here is not great (4 seconds). Cookies with dates
3696 * in the future are ignored if their offset is beyond one day. This
3697 * allows an admin to fix timezone issues without expiring everyone
3698 * and at the same time avoids keeping unwanted side effects for too
3699 * long.
3700 */
3701 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3702 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3703 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3704 txn->flags &= ~TX_CK_MASK;
3705 txn->flags |= TX_CK_OLD;
3706 delim = val_beg; // let's pretend we have not found the cookie
3707 txn->cookie_first_date = 0;
3708 txn->cookie_last_date = 0;
3709 }
3710 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3711 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3712 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3713 txn->flags &= ~TX_CK_MASK;
3714 txn->flags |= TX_CK_EXPIRED;
3715 delim = val_beg; // let's pretend we have not found the cookie
3716 txn->cookie_first_date = 0;
3717 txn->cookie_last_date = 0;
3718 }
3719
3720 /* Here, we'll look for the first running server which supports the cookie.
3721 * This allows to share a same cookie between several servers, for example
3722 * to dedicate backup servers to specific servers only.
3723 * However, to prevent clients from sticking to cookie-less backup server
3724 * when they have incidentely learned an empty cookie, we simply ignore
3725 * empty cookies and mark them as invalid.
3726 * The same behaviour is applied when persistence must be ignored.
3727 */
3728 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3729 srv = NULL;
3730
3731 while (srv) {
3732 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3733 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3734 if ((srv->cur_state != SRV_ST_STOPPED) ||
3735 (s->be->options & PR_O_PERSIST) ||
3736 (s->flags & SF_FORCE_PRST)) {
3737 /* we found the server and we can use it */
3738 txn->flags &= ~TX_CK_MASK;
3739 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3740 s->flags |= SF_DIRECT | SF_ASSIGNED;
3741 s->target = &srv->obj_type;
3742 break;
3743 } else {
3744 /* we found a server, but it's down,
3745 * mark it as such and go on in case
3746 * another one is available.
3747 */
3748 txn->flags &= ~TX_CK_MASK;
3749 txn->flags |= TX_CK_DOWN;
3750 }
3751 }
3752 srv = srv->next;
3753 }
3754
3755 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3756 /* no server matched this cookie or we deliberately skipped it */
3757 txn->flags &= ~TX_CK_MASK;
3758 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3759 txn->flags |= TX_CK_UNUSED;
3760 else
3761 txn->flags |= TX_CK_INVALID;
3762 }
3763
3764 /* depending on the cookie mode, we may have to either :
3765 * - delete the complete cookie if we're in insert+indirect mode, so that
3766 * the server never sees it ;
3767 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003768 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003769 * if we're in cookie prefix mode
3770 */
3771 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3772 int delta; /* negative */
3773
3774 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3775 delta = val_beg - (delim + 1);
3776 val_end += delta;
3777 next += delta;
3778 hdr_end += delta;
3779 del_from = NULL;
3780 preserve_hdr = 1; /* we want to keep this cookie */
3781 }
3782 else if (del_from == NULL &&
3783 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3784 del_from = prev;
3785 }
3786 }
3787 else {
3788 /* This is not our cookie, so we must preserve it. But if we already
3789 * scheduled another cookie for removal, we cannot remove the
3790 * complete header, but we can remove the previous block itself.
3791 */
3792 preserve_hdr = 1;
3793
3794 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003795 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003796 if (att_beg >= del_from)
3797 att_beg += delta;
3798 if (att_end >= del_from)
3799 att_end += delta;
3800 val_beg += delta;
3801 val_end += delta;
3802 next += delta;
3803 hdr_end += delta;
3804 prev = del_from;
3805 del_from = NULL;
3806 }
3807 }
3808
3809 /* continue with next cookie on this header line */
3810 att_beg = next;
3811 } /* for each cookie */
3812
3813
3814 /* There are no more cookies on this line.
3815 * We may still have one (or several) marked for deletion at the
3816 * end of the line. We must do this now in two ways :
3817 * - if some cookies must be preserved, we only delete from the
3818 * mark to the end of line ;
3819 * - if nothing needs to be preserved, simply delete the whole header
3820 */
3821 if (del_from) {
3822 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3823 }
3824 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003825 if (hdr_beg != hdr_end)
3826 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003827 else
3828 http_remove_header(htx, &ctx);
3829 }
3830 } /* for each "Cookie header */
3831}
3832
3833/*
3834 * Manage server-side cookies. It can impact performance by about 2% so it is
3835 * desirable to call it only when needed. This function is also used when we
3836 * just need to know if there is a cookie (eg: for check-cache).
3837 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003838static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003839{
3840 struct session *sess = s->sess;
3841 struct http_txn *txn = s->txn;
3842 struct htx *htx;
3843 struct http_hdr_ctx ctx;
3844 struct server *srv;
3845 char *hdr_beg, *hdr_end;
3846 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003847 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003848
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003849 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003850
3851 ctx.blk = NULL;
3852 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003853 int is_first = 1;
3854
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003855 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3856 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3857 break;
3858 is_cookie2 = 1;
3859 }
3860
3861 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3862 * <prev> points to the colon.
3863 */
3864 txn->flags |= TX_SCK_PRESENT;
3865
3866 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3867 * check-cache is enabled) and we are not interested in checking
3868 * them. Warning, the cookie capture is declared in the frontend.
3869 */
3870 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3871 break;
3872
3873 /* OK so now we know we have to process this response cookie.
3874 * The format of the Set-Cookie header is slightly different
3875 * from the format of the Cookie header in that it does not
3876 * support the comma as a cookie delimiter (thus the header
3877 * cannot be folded) because the Expires attribute described in
3878 * the original Netscape's spec may contain an unquoted date
3879 * with a comma inside. We have to live with this because
3880 * many browsers don't support Max-Age and some browsers don't
3881 * support quoted strings. However the Set-Cookie2 header is
3882 * clean.
3883 *
3884 * We have to keep multiple pointers in order to support cookie
3885 * removal at the beginning, middle or end of header without
3886 * corrupting the header (in case of set-cookie2). A special
3887 * pointer, <scav> points to the beginning of the set-cookie-av
3888 * fields after the first semi-colon. The <next> pointer points
3889 * either to the end of line (set-cookie) or next unquoted comma
3890 * (set-cookie2). All of these headers are valid :
3891 *
3892 * hdr_beg hdr_end
3893 * | |
3894 * v |
3895 * NAME1 = VALUE 1 ; Secure; Path="/" |
3896 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3897 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3898 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3899 * | | | | | | | |
3900 * | | | | | | | +-> next
3901 * | | | | | | +------------> scav
3902 * | | | | | +--------------> val_end
3903 * | | | | +--------------------> val_beg
3904 * | | | +----------------------> equal
3905 * | | +------------------------> att_end
3906 * | +----------------------------> att_beg
3907 * +------------------------------> prev
3908 * -------------------------------> hdr_beg
3909 */
3910 hdr_beg = ctx.value.ptr;
3911 hdr_end = hdr_beg + ctx.value.len;
3912 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3913
3914 /* Iterate through all cookies on this line */
3915
3916 /* find att_beg */
3917 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003918 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003919 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003920 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003921
3922 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3923 att_beg++;
3924
3925 /* find att_end : this is the first character after the last non
3926 * space before the equal. It may be equal to hdr_end.
3927 */
3928 equal = att_end = att_beg;
3929
3930 while (equal < hdr_end) {
3931 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3932 break;
3933 if (HTTP_IS_SPHT(*equal++))
3934 continue;
3935 att_end = equal;
3936 }
3937
3938 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3939 * is between <att_beg> and <equal>, both may be identical.
3940 */
3941
3942 /* look for end of cookie if there is an equal sign */
3943 if (equal < hdr_end && *equal == '=') {
3944 /* look for the beginning of the value */
3945 val_beg = equal + 1;
3946 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3947 val_beg++;
3948
3949 /* find the end of the value, respecting quotes */
3950 next = http_find_cookie_value_end(val_beg, hdr_end);
3951
3952 /* make val_end point to the first white space or delimitor after the value */
3953 val_end = next;
3954 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3955 val_end--;
3956 }
3957 else {
3958 /* <equal> points to next comma, semi-colon or EOL */
3959 val_beg = val_end = next = equal;
3960 }
3961
3962 if (next < hdr_end) {
3963 /* Set-Cookie2 supports multiple cookies, and <next> points to
3964 * a colon or semi-colon before the end. So skip all attr-value
3965 * pairs and look for the next comma. For Set-Cookie, since
3966 * commas are permitted in values, skip to the end.
3967 */
3968 if (is_cookie2)
3969 next = http_find_hdr_value_end(next, hdr_end);
3970 else
3971 next = hdr_end;
3972 }
3973
3974 /* Now everything is as on the diagram above */
3975
3976 /* Ignore cookies with no equal sign */
3977 if (equal == val_end)
3978 continue;
3979
3980 /* If there are spaces around the equal sign, we need to
3981 * strip them otherwise we'll get trouble for cookie captures,
3982 * or even for rewrites. Since this happens extremely rarely,
3983 * it does not hurt performance.
3984 */
3985 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3986 int stripped_before = 0;
3987 int stripped_after = 0;
3988
3989 if (att_end != equal) {
3990 memmove(att_end, equal, hdr_end - equal);
3991 stripped_before = (att_end - equal);
3992 equal += stripped_before;
3993 val_beg += stripped_before;
3994 }
3995
3996 if (val_beg > equal + 1) {
3997 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3998 stripped_after = (equal + 1) - val_beg;
3999 val_beg += stripped_after;
4000 stripped_before += stripped_after;
4001 }
4002
4003 val_end += stripped_before;
4004 next += stripped_before;
4005 hdr_end += stripped_before;
4006
Christopher Faulet3e2638e2019-06-18 09:49:16 +02004007 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004008 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004009 }
4010
4011 /* First, let's see if we want to capture this cookie. We check
4012 * that we don't already have a server side cookie, because we
4013 * can only capture one. Also as an optimisation, we ignore
4014 * cookies shorter than the declared name.
4015 */
4016 if (sess->fe->capture_name != NULL &&
4017 txn->srv_cookie == NULL &&
4018 (val_end - att_beg >= sess->fe->capture_namelen) &&
4019 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
4020 int log_len = val_end - att_beg;
4021 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
4022 ha_alert("HTTP logging : out of memory.\n");
4023 }
4024 else {
4025 if (log_len > sess->fe->capture_len)
4026 log_len = sess->fe->capture_len;
4027 memcpy(txn->srv_cookie, att_beg, log_len);
4028 txn->srv_cookie[log_len] = 0;
4029 }
4030 }
4031
4032 srv = objt_server(s->target);
4033 /* now check if we need to process it for persistence */
4034 if (!(s->flags & SF_IGNORE_PRST) &&
4035 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
4036 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
4037 /* assume passive cookie by default */
4038 txn->flags &= ~TX_SCK_MASK;
4039 txn->flags |= TX_SCK_FOUND;
4040
4041 /* If the cookie is in insert mode on a known server, we'll delete
4042 * this occurrence because we'll insert another one later.
4043 * We'll delete it too if the "indirect" option is set and we're in
4044 * a direct access.
4045 */
4046 if (s->be->ck_opts & PR_CK_PSV) {
4047 /* The "preserve" flag was set, we don't want to touch the
4048 * server's cookie.
4049 */
4050 }
4051 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
4052 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
4053 /* this cookie must be deleted */
4054 if (prev == hdr_beg && next == hdr_end) {
4055 /* whole header */
4056 http_remove_header(htx, &ctx);
4057 /* note: while both invalid now, <next> and <hdr_end>
4058 * are still equal, so the for() will stop as expected.
4059 */
4060 } else {
4061 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004062 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004063 next = prev;
4064 hdr_end += delta;
4065 }
4066 txn->flags &= ~TX_SCK_MASK;
4067 txn->flags |= TX_SCK_DELETED;
4068 /* and go on with next cookie */
4069 }
4070 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
4071 /* replace bytes val_beg->val_end with the cookie name associated
4072 * with this server since we know it.
4073 */
4074 int sliding, delta;
4075
4076 ctx.value = ist2(val_beg, val_end - val_beg);
4077 ctx.lws_before = ctx.lws_after = 0;
4078 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
4079 delta = srv->cklen - (val_end - val_beg);
4080 sliding = (ctx.value.ptr - val_beg);
4081 hdr_beg += sliding;
4082 val_beg += sliding;
4083 next += sliding + delta;
4084 hdr_end += sliding + delta;
4085
4086 txn->flags &= ~TX_SCK_MASK;
4087 txn->flags |= TX_SCK_REPLACED;
4088 }
4089 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
4090 /* insert the cookie name associated with this server
4091 * before existing cookie, and insert a delimiter between them..
4092 */
4093 int sliding, delta;
4094 ctx.value = ist2(val_beg, 0);
4095 ctx.lws_before = ctx.lws_after = 0;
4096 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
4097 delta = srv->cklen + 1;
4098 sliding = (ctx.value.ptr - val_beg);
4099 hdr_beg += sliding;
4100 val_beg += sliding;
4101 next += sliding + delta;
4102 hdr_end += sliding + delta;
4103
4104 val_beg[srv->cklen] = COOKIE_DELIM;
4105 txn->flags &= ~TX_SCK_MASK;
4106 txn->flags |= TX_SCK_REPLACED;
4107 }
4108 }
4109 /* that's done for this cookie, check the next one on the same
4110 * line when next != hdr_end (only if is_cookie2).
4111 */
4112 }
4113 }
4114}
4115
Christopher Faulet25a02f62018-10-24 12:00:25 +02004116/*
4117 * Parses the Cache-Control and Pragma request header fields to determine if
4118 * the request may be served from the cache and/or if it is cacheable. Updates
4119 * s->txn->flags.
4120 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004121void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004122{
4123 struct http_txn *txn = s->txn;
4124 struct htx *htx;
4125 int32_t pos;
4126 int pragma_found, cc_found, i;
4127
4128 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
4129 return; /* nothing more to do here */
4130
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004131 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02004132 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02004133 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004134 struct htx_blk *blk = htx_get_blk(htx, pos);
4135 enum htx_blk_type type = htx_get_blk_type(blk);
4136 struct ist n, v;
4137
4138 if (type == HTX_BLK_EOH)
4139 break;
4140 if (type != HTX_BLK_HDR)
4141 continue;
4142
4143 n = htx_get_blk_name(htx, blk);
4144 v = htx_get_blk_value(htx, blk);
4145
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004146 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004147 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4148 pragma_found = 1;
4149 continue;
4150 }
4151 }
4152
4153 /* Don't use the cache and don't try to store if we found the
4154 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004155 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004156 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4157 txn->flags |= TX_CACHE_IGNORE;
4158 continue;
4159 }
4160
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004161 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004162 continue;
4163
4164 /* OK, right now we know we have a cache-control header */
4165 cc_found = 1;
4166 if (!v.len) /* no info */
4167 continue;
4168
4169 i = 0;
4170 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4171 !isspace((unsigned char)*(v.ptr+i)))
4172 i++;
4173
4174 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
4175 * values after max-age, max-stale nor min-fresh, we simply don't
4176 * use the cache when they're specified.
4177 */
4178 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
4179 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4180 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
4181 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
4182 txn->flags |= TX_CACHE_IGNORE;
4183 continue;
4184 }
4185
4186 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
4187 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4188 continue;
4189 }
4190 }
4191
4192 /* RFC7234#5.4:
4193 * When the Cache-Control header field is also present and
4194 * understood in a request, Pragma is ignored.
4195 * When the Cache-Control header field is not present in a
4196 * request, caches MUST consider the no-cache request
4197 * pragma-directive as having the same effect as if
4198 * "Cache-Control: no-cache" were present.
4199 */
4200 if (!cc_found && pragma_found)
4201 txn->flags |= TX_CACHE_IGNORE;
4202}
4203
4204/*
4205 * Check if response is cacheable or not. Updates s->txn->flags.
4206 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004207void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004208{
4209 struct http_txn *txn = s->txn;
4210 struct htx *htx;
4211 int32_t pos;
4212 int i;
4213
4214 if (txn->status < 200) {
4215 /* do not try to cache interim responses! */
4216 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4217 return;
4218 }
4219
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004220 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004221 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004222 struct htx_blk *blk = htx_get_blk(htx, pos);
4223 enum htx_blk_type type = htx_get_blk_type(blk);
4224 struct ist n, v;
4225
4226 if (type == HTX_BLK_EOH)
4227 break;
4228 if (type != HTX_BLK_HDR)
4229 continue;
4230
4231 n = htx_get_blk_name(htx, blk);
4232 v = htx_get_blk_value(htx, blk);
4233
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004234 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004235 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4236 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4237 return;
4238 }
4239 }
4240
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004241 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004242 continue;
4243
4244 /* OK, right now we know we have a cache-control header */
4245 if (!v.len) /* no info */
4246 continue;
4247
4248 i = 0;
4249 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4250 !isspace((unsigned char)*(v.ptr+i)))
4251 i++;
4252
4253 /* we have a complete value between v.ptr and (v.ptr+i) */
4254 if (i < v.len && *(v.ptr + i) == '=') {
4255 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
4256 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
4257 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4258 continue;
4259 }
4260
4261 /* we have something of the form no-cache="set-cookie" */
4262 if ((v.len >= 21) &&
4263 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
4264 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
4265 txn->flags &= ~TX_CACHE_COOK;
4266 continue;
4267 }
4268
4269 /* OK, so we know that either p2 points to the end of string or to a comma */
4270 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
4271 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4272 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
4273 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4274 return;
4275 }
4276
4277 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4278 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4279 continue;
4280 }
4281 }
4282}
4283
Christopher Faulet64159df2018-10-24 21:15:35 +02004284/* send a server's name with an outgoing request over an established connection.
4285 * Note: this function is designed to be called once the request has been
4286 * scheduled for being forwarded. This is the reason why the number of forwarded
4287 * bytes have to be adjusted.
4288 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004289int http_send_name_header(struct stream *s, struct proxy *be, const char *srv_name)
Christopher Faulet64159df2018-10-24 21:15:35 +02004290{
4291 struct htx *htx;
4292 struct http_hdr_ctx ctx;
4293 struct ist hdr;
4294 uint32_t data;
4295
4296 hdr = ist2(be->server_id_hdr_name, be->server_id_hdr_len);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004297 htx = htxbuf(&s->req.buf);
Christopher Faulet64159df2018-10-24 21:15:35 +02004298 data = htx->data;
4299
4300 ctx.blk = NULL;
4301 while (http_find_header(htx, hdr, &ctx, 1))
4302 http_remove_header(htx, &ctx);
4303 http_add_header(htx, hdr, ist2(srv_name, strlen(srv_name)));
4304
4305 if (co_data(&s->req)) {
4306 if (data >= htx->data)
4307 c_rew(&s->req, data - htx->data);
4308 else
4309 c_adv(&s->req, htx->data - data);
4310 }
4311 return 0;
4312}
4313
Christopher Faulet377c5a52018-10-24 21:21:30 +02004314/*
4315 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4316 * for the current backend.
4317 *
4318 * It is assumed that the request is either a HEAD, GET, or POST and that the
4319 * uri_auth field is valid.
4320 *
4321 * Returns 1 if stats should be provided, otherwise 0.
4322 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004323static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004324{
4325 struct uri_auth *uri_auth = backend->uri_auth;
4326 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004327 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004328 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004329
4330 if (!uri_auth)
4331 return 0;
4332
4333 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4334 return 0;
4335
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004336 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004337 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004338 uri = htx_sl_req_uri(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004339
4340 /* check URI size */
4341 if (uri_auth->uri_len > uri.len)
4342 return 0;
4343
4344 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4345 return 0;
4346
4347 return 1;
4348}
4349
4350/* This function prepares an applet to handle the stats. It can deal with the
4351 * "100-continue" expectation, check that admin rules are met for POST requests,
4352 * and program a response message if something was unexpected. It cannot fail
4353 * and always relies on the stats applet to complete the job. It does not touch
4354 * analysers nor counters, which are left to the caller. It does not touch
4355 * s->target which is supposed to already point to the stats applet. The caller
4356 * is expected to have already assigned an appctx to the stream.
4357 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004358static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004359{
4360 struct stats_admin_rule *stats_admin_rule;
4361 struct stream_interface *si = &s->si[1];
4362 struct session *sess = s->sess;
4363 struct http_txn *txn = s->txn;
4364 struct http_msg *msg = &txn->req;
4365 struct uri_auth *uri_auth = s->be->uri_auth;
4366 const char *h, *lookup, *end;
4367 struct appctx *appctx;
4368 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004369 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004370
4371 appctx = si_appctx(si);
4372 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4373 appctx->st1 = appctx->st2 = 0;
4374 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
4375 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4376 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4377 appctx->ctx.stats.flags |= STAT_CHUNKED;
4378
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004379 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004380 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004381 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4382 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004383
4384 for (h = lookup; h <= end - 3; h++) {
4385 if (memcmp(h, ";up", 3) == 0) {
4386 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4387 break;
4388 }
4389 }
4390
4391 if (uri_auth->refresh) {
4392 for (h = lookup; h <= end - 10; h++) {
4393 if (memcmp(h, ";norefresh", 10) == 0) {
4394 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4395 break;
4396 }
4397 }
4398 }
4399
4400 for (h = lookup; h <= end - 4; h++) {
4401 if (memcmp(h, ";csv", 4) == 0) {
4402 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
4403 break;
4404 }
4405 }
4406
4407 for (h = lookup; h <= end - 6; h++) {
4408 if (memcmp(h, ";typed", 6) == 0) {
4409 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
4410 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4411 break;
4412 }
4413 }
4414
4415 for (h = lookup; h <= end - 8; h++) {
4416 if (memcmp(h, ";st=", 4) == 0) {
4417 int i;
4418 h += 4;
4419 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4420 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4421 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4422 appctx->ctx.stats.st_code = i;
4423 break;
4424 }
4425 }
4426 break;
4427 }
4428 }
4429
4430 appctx->ctx.stats.scope_str = 0;
4431 appctx->ctx.stats.scope_len = 0;
4432 for (h = lookup; h <= end - 8; h++) {
4433 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4434 int itx = 0;
4435 const char *h2;
4436 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4437 const char *err;
4438
4439 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4440 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004441 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4442 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004443 if (*h == ';' || *h == '&' || *h == ' ')
4444 break;
4445 itx++;
4446 h++;
4447 }
4448
4449 if (itx > STAT_SCOPE_TXT_MAXLEN)
4450 itx = STAT_SCOPE_TXT_MAXLEN;
4451 appctx->ctx.stats.scope_len = itx;
4452
4453 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4454 memcpy(scope_txt, h2, itx);
4455 scope_txt[itx] = '\0';
4456 err = invalid_char(scope_txt);
4457 if (err) {
4458 /* bad char in search text => clear scope */
4459 appctx->ctx.stats.scope_str = 0;
4460 appctx->ctx.stats.scope_len = 0;
4461 }
4462 break;
4463 }
4464 }
4465
4466 /* now check whether we have some admin rules for this request */
4467 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4468 int ret = 1;
4469
4470 if (stats_admin_rule->cond) {
4471 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4472 ret = acl_pass(ret);
4473 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4474 ret = !ret;
4475 }
4476
4477 if (ret) {
4478 /* no rule, or the rule matches */
4479 appctx->ctx.stats.flags |= STAT_ADMIN;
4480 break;
4481 }
4482 }
4483
Christopher Faulet5d45e382019-02-27 15:15:23 +01004484 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4485 appctx->st0 = STAT_HTTP_HEAD;
4486 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbcf242a2019-03-01 11:36:26 +01004487 if (appctx->ctx.stats.flags & STAT_ADMIN)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004488 appctx->st0 = STAT_HTTP_POST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004489 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004490 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004491 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4492 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4493 appctx->st0 = STAT_HTTP_LAST;
4494 }
4495 }
4496 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004497 /* Unsupported method */
4498 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4499 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4500 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004501 }
4502
4503 s->task->nice = -32; /* small boost for HTTP statistics */
4504 return 1;
4505}
4506
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004507void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004508{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004509 struct channel *req = &s->req;
4510 struct channel *res = &s->res;
4511 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004512 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004513 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004514 struct ist path, location;
4515 unsigned int flags;
4516 size_t data;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004517
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004518 /*
4519 * Create the location
4520 */
4521 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004522
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004523 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004524 /* special prefix "/" means don't change URL */
4525 srv = __objt_server(s->target);
4526 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4527 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4528 return;
4529 }
4530
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004531 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004532 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004533 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004534 path = http_get_path(htx_sl_req_uri(sl));
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004535 if (!path.ptr)
4536 return;
4537
4538 if (!chunk_memcat(&trash, path.ptr, path.len))
4539 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004540 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004541
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004542 /*
4543 * Create the 302 respone
4544 */
4545 htx = htx_from_buf(&res->buf);
4546 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4547 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4548 ist("HTTP/1.1"), ist("302"), ist("Found"));
4549 if (!sl)
4550 goto fail;
4551 sl->info.res.status = 302;
4552 s->txn->status = 302;
4553
4554 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4555 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4556 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4557 !htx_add_header(htx, ist("Location"), location))
4558 goto fail;
4559
4560 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4561 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004562
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004563 /*
4564 * Send the message
4565 */
4566 data = htx->data - co_data(res);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004567 c_adv(res, data);
4568 res->total += data;
4569
4570 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004571 si_shutr(si);
4572 si_shutw(si);
4573 si->err_type = SI_ET_NONE;
4574 si->state = SI_ST_CLO;
4575
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004576 channel_auto_read(req);
4577 channel_abort(req);
4578 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004579 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004580 channel_auto_read(res);
4581 channel_auto_close(res);
4582
4583 if (!(s->flags & SF_ERR_MASK))
4584 s->flags |= SF_ERR_LOCAL;
4585 if (!(s->flags & SF_FINST_MASK))
4586 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004587
4588 /* FIXME: we should increase a counter of redirects per server and per backend. */
4589 srv_inc_sess_ctr(srv);
4590 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004591 return;
4592
4593 fail:
4594 /* If an error occurred, remove the incomplete HTTP response from the
4595 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004596 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004597}
4598
Christopher Fauletf2824e62018-10-01 12:12:37 +02004599/* This function terminates the request because it was completly analyzed or
4600 * because an error was triggered during the body forwarding.
4601 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004602static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004603{
4604 struct channel *chn = &s->req;
4605 struct http_txn *txn = s->txn;
4606
4607 DPRINTF(stderr,"[%u] %s: stream=%p states=%s,%s req->analysers=0x%08x res->analysers=0x%08x\n",
4608 now_ms, __FUNCTION__, s,
4609 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
4610 s->req.analysers, s->res.analysers);
4611
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004612 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4613 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004614 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004615 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004616 goto end;
4617 }
4618
4619 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
4620 return;
4621
4622 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004623 /* No need to read anymore, the request was completely parsed.
4624 * We can shut the read side unless we want to abort_on_close,
4625 * or we have a POST request. The issue with POST requests is
4626 * that some browsers still send a CRLF after the request, and
4627 * this CRLF must be read so that it does not remain in the kernel
4628 * buffers, otherwise a close could cause an RST on some systems
4629 * (eg: Linux).
4630 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004631 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004632 channel_dont_read(chn);
4633
4634 /* if the server closes the connection, we want to immediately react
4635 * and close the socket to save packets and syscalls.
4636 */
4637 s->si[1].flags |= SI_FL_NOHALF;
4638
4639 /* In any case we've finished parsing the request so we must
4640 * disable Nagle when sending data because 1) we're not going
4641 * to shut this side, and 2) the server is waiting for us to
4642 * send pending data.
4643 */
4644 chn->flags |= CF_NEVER_WAIT;
4645
Christopher Fauletd01ce402019-01-02 17:44:13 +01004646 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4647 /* The server has not finished to respond, so we
4648 * don't want to move in order not to upset it.
4649 */
4650 return;
4651 }
4652
Christopher Fauletf2824e62018-10-01 12:12:37 +02004653 /* When we get here, it means that both the request and the
4654 * response have finished receiving. Depending on the connection
4655 * mode, we'll have to wait for the last bytes to leave in either
4656 * direction, and sometimes for a close to be effective.
4657 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004658 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004659 /* Tunnel mode will not have any analyser so it needs to
4660 * poll for reads.
4661 */
4662 channel_auto_read(chn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004663 if (b_data(&chn->buf))
4664 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004665 txn->req.msg_state = HTTP_MSG_TUNNEL;
4666 }
4667 else {
4668 /* we're not expecting any new data to come for this
4669 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004670 *
4671 * However, there is an exception if the response
4672 * length is undefined. In this case, we need to wait
4673 * the close from the server. The response will be
4674 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004675 */
4676 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4677 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004678 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004679
4680 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4681 channel_shutr_now(chn);
4682 channel_shutw_now(chn);
4683 }
4684 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004685 goto check_channel_flags;
4686 }
4687
4688 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4689 http_msg_closing:
4690 /* nothing else to forward, just waiting for the output buffer
4691 * to be empty and for the shutw_now to take effect.
4692 */
4693 if (channel_is_empty(chn)) {
4694 txn->req.msg_state = HTTP_MSG_CLOSED;
4695 goto http_msg_closed;
4696 }
4697 else if (chn->flags & CF_SHUTW) {
4698 txn->req.err_state = txn->req.msg_state;
4699 txn->req.msg_state = HTTP_MSG_ERROR;
4700 goto end;
4701 }
4702 return;
4703 }
4704
4705 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4706 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004707 /* if we don't know whether the server will close, we need to hard close */
4708 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4709 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004710 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004711 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004712 channel_dont_read(chn);
4713 goto end;
4714 }
4715
4716 check_channel_flags:
4717 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4718 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4719 /* if we've just closed an output, let's switch */
4720 txn->req.msg_state = HTTP_MSG_CLOSING;
4721 goto http_msg_closing;
4722 }
4723
4724 end:
4725 chn->analysers &= AN_REQ_FLT_END;
4726 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4727 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4728 channel_auto_close(chn);
4729 channel_auto_read(chn);
4730}
4731
4732
4733/* This function terminates the response because it was completly analyzed or
4734 * because an error was triggered during the body forwarding.
4735 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004736static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004737{
4738 struct channel *chn = &s->res;
4739 struct http_txn *txn = s->txn;
4740
4741 DPRINTF(stderr,"[%u] %s: stream=%p states=%s,%s req->analysers=0x%08x res->analysers=0x%08x\n",
4742 now_ms, __FUNCTION__, s,
4743 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
4744 s->req.analysers, s->res.analysers);
4745
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004746 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4747 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004748 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004749 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004750 goto end;
4751 }
4752
4753 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
4754 return;
4755
4756 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4757 /* In theory, we don't need to read anymore, but we must
4758 * still monitor the server connection for a possible close
4759 * while the request is being uploaded, so we don't disable
4760 * reading.
4761 */
4762 /* channel_dont_read(chn); */
4763
4764 if (txn->req.msg_state < HTTP_MSG_DONE) {
4765 /* The client seems to still be sending data, probably
4766 * because we got an error response during an upload.
4767 * We have the choice of either breaking the connection
4768 * or letting it pass through. Let's do the later.
4769 */
4770 return;
4771 }
4772
4773 /* When we get here, it means that both the request and the
4774 * response have finished receiving. Depending on the connection
4775 * mode, we'll have to wait for the last bytes to leave in either
4776 * direction, and sometimes for a close to be effective.
4777 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004778 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004779 channel_auto_read(chn);
4780 chn->flags |= CF_NEVER_WAIT;
Christopher Faulet9768c262018-10-22 09:34:31 +02004781 if (b_data(&chn->buf))
4782 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004783 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4784 }
4785 else {
4786 /* we're not expecting any new data to come for this
4787 * transaction, so we can close it.
4788 */
4789 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4790 channel_shutr_now(chn);
4791 channel_shutw_now(chn);
4792 }
4793 }
4794 goto check_channel_flags;
4795 }
4796
4797 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4798 http_msg_closing:
4799 /* nothing else to forward, just waiting for the output buffer
4800 * to be empty and for the shutw_now to take effect.
4801 */
4802 if (channel_is_empty(chn)) {
4803 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4804 goto http_msg_closed;
4805 }
4806 else if (chn->flags & CF_SHUTW) {
4807 txn->rsp.err_state = txn->rsp.msg_state;
4808 txn->rsp.msg_state = HTTP_MSG_ERROR;
Olivier Houcharda798bf52019-03-08 18:52:00 +01004809 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004810 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004811 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004812 goto end;
4813 }
4814 return;
4815 }
4816
4817 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4818 http_msg_closed:
4819 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004820 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004821 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004822 goto end;
4823 }
4824
4825 check_channel_flags:
4826 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4827 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4828 /* if we've just closed an output, let's switch */
4829 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4830 goto http_msg_closing;
4831 }
4832
4833 end:
4834 chn->analysers &= AN_RES_FLT_END;
4835 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4836 chn->analysers |= AN_RES_FLT_XFER_DATA;
4837 channel_auto_close(chn);
4838 channel_auto_read(chn);
4839}
4840
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004841void http_server_error(struct stream *s, struct stream_interface *si, int err,
4842 int finst, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004843{
4844 channel_auto_read(si_oc(si));
4845 channel_abort(si_oc(si));
4846 channel_auto_close(si_oc(si));
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004847 channel_htx_erase(si_oc(si), htxbuf(&(si_oc(si))->buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02004848 channel_auto_close(si_ic(si));
4849 channel_auto_read(si_ic(si));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004850
4851 /* <msg> is an HTX structure. So we copy it in the response's
4852 * channel */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004853 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004854 struct channel *chn = si_ic(si);
4855 struct htx *htx;
4856
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004857 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004858 chn->buf.data = msg->data;
4859 memcpy(chn->buf.area, msg->area, msg->data);
4860 htx = htx_from_buf(&chn->buf);
Christopher Faulet0f226952018-10-22 09:29:56 +02004861 c_adv(chn, htx->data);
4862 chn->total += htx->data;
4863 }
4864 if (!(s->flags & SF_ERR_MASK))
4865 s->flags |= err;
4866 if (!(s->flags & SF_FINST_MASK))
4867 s->flags |= finst;
4868}
4869
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004870void http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004871{
4872 channel_auto_read(&s->req);
4873 channel_abort(&s->req);
4874 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004875 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
4876 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02004877
4878 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004879
4880 /* <msg> is an HTX structure. So we copy it in the response's
4881 * channel */
4882 /* FIXME: It is a problem for now if there is some outgoing data */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004883 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004884 struct channel *chn = &s->res;
4885 struct htx *htx;
4886
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004887 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004888 chn->buf.data = msg->data;
4889 memcpy(chn->buf.area, msg->area, msg->data);
4890 htx = htx_from_buf(&chn->buf);
Christopher Faulet0f226952018-10-22 09:29:56 +02004891 c_adv(chn, htx->data);
4892 chn->total += htx->data;
4893 }
4894
4895 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
4896 channel_auto_read(&s->res);
4897 channel_auto_close(&s->res);
4898 channel_shutr_now(&s->res);
4899}
4900
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004901struct buffer *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004902{
4903 const int msgnum = http_get_status_idx(s->txn->status);
4904
4905 if (s->be->errmsg[msgnum].area)
4906 return &s->be->errmsg[msgnum];
4907 else if (strm_fe(s)->errmsg[msgnum].area)
4908 return &strm_fe(s)->errmsg[msgnum];
4909 else
Christopher Fauletf7346382019-07-17 22:02:08 +02004910 return &http_err_chunks[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004911}
4912
Christopher Faulet304cc402019-07-15 15:46:28 +02004913/* Return the error message corresponding to si->err_type. It is assumed
4914 * that the server side is closed. Note that err_type is actually a
4915 * bitmask, where almost only aborts may be cumulated with other
4916 * values. We consider that aborted operations are more important
4917 * than timeouts or errors due to the fact that nobody else in the
4918 * logs might explain incomplete retries. All others should avoid
4919 * being cumulated. It should normally not be possible to have multiple
4920 * aborts at once, but just in case, the first one in sequence is reported.
4921 * Note that connection errors appearing on the second request of a keep-alive
4922 * connection are not reported since this allows the client to retry.
4923 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004924void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004925{
4926 int err_type = si->err_type;
4927
4928 /* set s->txn->status for http_error_message(s) */
4929 s->txn->status = 503;
4930
4931 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004932 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4933 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004934 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004935 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4936 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4937 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004938 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004939 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4940 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004941 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004942 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4943 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004944 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004945 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4946 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4947 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004948 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004949 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4950 (s->flags & SF_SRV_REUSED) ? NULL :
4951 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004952 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004953 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4954 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4955 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004956 else { /* SI_ET_CONN_OTHER and others */
4957 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004958 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4959 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004960 }
4961}
4962
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004963
Christopher Faulet4a28a532019-03-01 11:19:40 +01004964/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4965 * on success and -1 on error.
4966 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004967static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004968{
4969 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4970 * then we must send an HTTP/1.1 100 Continue intermediate response.
4971 */
4972 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4973 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4974 struct ist hdr = { .ptr = "Expect", .len = 6 };
4975 struct http_hdr_ctx ctx;
4976
4977 ctx.blk = NULL;
4978 /* Expect is allowed in 1.1, look for it */
4979 if (http_find_header(htx, hdr, &ctx, 0) &&
4980 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004981 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004982 return -1;
4983 http_remove_header(htx, &ctx);
4984 }
4985 }
4986 return 0;
4987}
4988
Christopher Faulet23a3c792018-11-28 10:01:23 +01004989/* Send a 100-Continue response to the client. It returns 0 on success and -1
4990 * on error. The response channel is updated accordingly.
4991 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004992static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004993{
4994 struct channel *res = &s->res;
4995 struct htx *htx = htx_from_buf(&res->buf);
4996 struct htx_sl *sl;
4997 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4998 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4999 size_t data;
5000
5001 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5002 ist("HTTP/1.1"), ist("100"), ist("Continue"));
5003 if (!sl)
5004 goto fail;
5005 sl->info.res.status = 100;
5006
Christopher Faulet1d5ec092019-06-26 14:23:54 +02005007 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01005008 goto fail;
5009
5010 data = htx->data - co_data(res);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005011 c_adv(res, data);
5012 res->total += data;
5013 return 0;
5014
5015 fail:
5016 /* If an error occurred, remove the incomplete HTTP response from the
5017 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005018 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005019 return -1;
5020}
5021
Christopher Faulet12c51e22018-11-28 15:59:42 +01005022
5023/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
5024 * ont whether we use a proxy or not. It returns 0 on success and -1 on
5025 * error. The response channel is updated accordingly.
5026 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005027static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
Christopher Faulet12c51e22018-11-28 15:59:42 +01005028{
5029 struct channel *res = &s->res;
5030 struct htx *htx = htx_from_buf(&res->buf);
5031 struct htx_sl *sl;
5032 struct ist code, body;
5033 int status;
5034 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5035 size_t data;
5036
5037 if (!(s->txn->flags & TX_USE_PX_CONN)) {
5038 status = 401;
5039 code = ist("401");
5040 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
5041 "You need a valid user and password to access this content.\n"
5042 "</body></html>\n");
5043 }
5044 else {
5045 status = 407;
5046 code = ist("407");
5047 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
5048 "You need a valid user and password to access this content.\n"
5049 "</body></html>\n");
5050 }
5051
5052 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5053 ist("HTTP/1.1"), code, ist("Unauthorized"));
5054 if (!sl)
5055 goto fail;
5056 sl->info.res.status = status;
5057 s->txn->status = status;
5058
5059 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
5060 goto fail;
5061
Willy Tarreaub5ba2b02019-06-11 16:08:25 +02005062 if (!htx_add_header(htx, ist("Content-length"), ist("112")) ||
5063 !htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Faulet12c51e22018-11-28 15:59:42 +01005064 !htx_add_header(htx, ist("Connection"), ist("close")) ||
Jérôme Magnin86cef232018-12-28 14:49:08 +01005065 !htx_add_header(htx, ist("Content-Type"), ist("text/html")))
5066 goto fail;
5067 if (status == 401 && !htx_add_header(htx, ist("WWW-Authenticate"), ist2(trash.area, trash.data)))
5068 goto fail;
5069 if (status == 407 && !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005070 goto fail;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02005071 if (!htx_add_endof(htx, HTX_BLK_EOH))
5072 goto fail;
5073
5074 while (body.len) {
5075 size_t sent = htx_add_data(htx, body);
5076 if (!sent)
5077 goto fail;
5078 body.ptr += sent;
5079 body.len -= sent;
5080 }
5081
5082 if (!htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005083 goto fail;
5084
5085 data = htx->data - co_data(res);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005086 c_adv(res, data);
5087 res->total += data;
5088
5089 channel_auto_read(&s->req);
5090 channel_abort(&s->req);
5091 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005092 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet12c51e22018-11-28 15:59:42 +01005093
5094 res->wex = tick_add_ifset(now_ms, res->wto);
5095 channel_auto_read(res);
5096 channel_auto_close(res);
5097 channel_shutr_now(res);
5098 return 0;
5099
5100 fail:
5101 /* If an error occurred, remove the incomplete HTTP response from the
5102 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005103 channel_htx_truncate(res, htx);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005104 return -1;
5105}
5106
Christopher Faulet0f226952018-10-22 09:29:56 +02005107/*
5108 * Capture headers from message <htx> according to header list <cap_hdr>, and
5109 * fill the <cap> pointers appropriately.
5110 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005111static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02005112{
5113 struct cap_hdr *h;
5114 int32_t pos;
5115
Christopher Fauleta3f15502019-05-13 15:27:23 +02005116 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02005117 struct htx_blk *blk = htx_get_blk(htx, pos);
5118 enum htx_blk_type type = htx_get_blk_type(blk);
5119 struct ist n, v;
5120
5121 if (type == HTX_BLK_EOH)
5122 break;
5123 if (type != HTX_BLK_HDR)
5124 continue;
5125
5126 n = htx_get_blk_name(htx, blk);
5127
5128 for (h = cap_hdr; h; h = h->next) {
5129 if (h->namelen && (h->namelen == n.len) &&
5130 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
5131 if (cap[h->index] == NULL)
5132 cap[h->index] =
5133 pool_alloc(h->pool);
5134
5135 if (cap[h->index] == NULL) {
5136 ha_alert("HTTP capture : out of memory.\n");
5137 break;
5138 }
5139
5140 v = htx_get_blk_value(htx, blk);
5141 if (v.len > h->len)
5142 v.len = h->len;
5143
5144 memcpy(cap[h->index], v.ptr, v.len);
5145 cap[h->index][v.len]=0;
5146 }
5147 }
5148 }
5149}
5150
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005151/* Delete a value in a header between delimiters <from> and <next>. The header
5152 * itself is delimited by <start> and <end> pointers. The number of characters
5153 * displaced is returned, and the pointer to the first delimiter is updated if
5154 * required. The function tries as much as possible to respect the following
5155 * principles :
5156 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5157 * in which case <next> is simply removed
5158 * - set exactly one space character after the new first delimiter, unless there
5159 * are not enough characters in the block being moved to do so.
5160 * - remove unneeded spaces before the previous delimiter and after the new
5161 * one.
5162 *
5163 * It is the caller's responsibility to ensure that :
5164 * - <from> points to a valid delimiter or <start> ;
5165 * - <next> points to a valid delimiter or <end> ;
5166 * - there are non-space chars before <from>.
5167 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005168static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005169{
5170 char *prev = *from;
5171
5172 if (prev == start) {
5173 /* We're removing the first value. eat the semicolon, if <next>
5174 * is lower than <end> */
5175 if (next < end)
5176 next++;
5177
5178 while (next < end && HTTP_IS_SPHT(*next))
5179 next++;
5180 }
5181 else {
5182 /* Remove useless spaces before the old delimiter. */
5183 while (HTTP_IS_SPHT(*(prev-1)))
5184 prev--;
5185 *from = prev;
5186
5187 /* copy the delimiter and if possible a space if we're
5188 * not at the end of the line.
5189 */
5190 if (next < end) {
5191 *prev++ = *next++;
5192 if (prev + 1 < next)
5193 *prev++ = ' ';
5194 while (next < end && HTTP_IS_SPHT(*next))
5195 next++;
5196 }
5197 }
5198 memmove(prev, next, end - next);
5199 return (prev - next);
5200}
5201
Christopher Faulet0f226952018-10-22 09:29:56 +02005202
5203/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005204 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005205 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005206static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005207{
5208 struct ist dst = ist2(str, 0);
5209
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005210 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005211 goto end;
5212 if (dst.len + 1 > len)
5213 goto end;
5214 dst.ptr[dst.len++] = ' ';
5215
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005216 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005217 goto end;
5218 if (dst.len + 1 > len)
5219 goto end;
5220 dst.ptr[dst.len++] = ' ';
5221
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005222 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005223 end:
5224 return dst.len;
5225}
5226
5227/*
5228 * Print a debug line with a start line.
5229 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005230static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005231{
5232 struct session *sess = strm_sess(s);
5233 int max;
5234
5235 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5236 dir,
5237 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5238 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5239
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005240 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005241 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005242 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005243 trash.area[trash.data++] = ' ';
5244
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005245 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005246 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005247 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005248 trash.area[trash.data++] = ' ';
5249
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005250 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005251 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005252 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005253 trash.area[trash.data++] = '\n';
5254
5255 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5256}
5257
5258/*
5259 * Print a debug line with a header.
5260 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005261static 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 +02005262{
5263 struct session *sess = strm_sess(s);
5264 int max;
5265
5266 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5267 dir,
5268 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5269 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5270
5271 max = n.len;
5272 UBOUND(max, trash.size - trash.data - 3);
5273 chunk_memcat(&trash, n.ptr, max);
5274 trash.area[trash.data++] = ':';
5275 trash.area[trash.data++] = ' ';
5276
5277 max = v.len;
5278 UBOUND(max, trash.size - trash.data - 1);
5279 chunk_memcat(&trash, v.ptr, max);
5280 trash.area[trash.data++] = '\n';
5281
5282 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5283}
5284
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005285/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5286 * In case of allocation failure, everything allocated is freed and NULL is
5287 * returned. Otherwise the new transaction is assigned to the stream and
5288 * returned.
5289 */
5290struct http_txn *http_alloc_txn(struct stream *s)
5291{
5292 struct http_txn *txn = s->txn;
5293
5294 if (txn)
5295 return txn;
5296
5297 txn = pool_alloc(pool_head_http_txn);
5298 if (!txn)
5299 return txn;
5300
5301 s->txn = txn;
5302 return txn;
5303}
5304
5305void http_txn_reset_req(struct http_txn *txn)
5306{
5307 txn->req.flags = 0;
5308 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5309}
5310
5311void http_txn_reset_res(struct http_txn *txn)
5312{
5313 txn->rsp.flags = 0;
5314 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5315}
5316
5317/*
5318 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5319 * the required fields are properly allocated and that we only need to (re)init
5320 * them. This should be used before processing any new request.
5321 */
5322void http_init_txn(struct stream *s)
5323{
5324 struct http_txn *txn = s->txn;
5325 struct conn_stream *cs = objt_cs(s->si[0].end);
5326
5327 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5328 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5329 : 0);
5330 txn->status = -1;
5331 *(unsigned int *)txn->cache_hash = 0;
5332
5333 txn->cookie_first_date = 0;
5334 txn->cookie_last_date = 0;
5335
5336 txn->srv_cookie = NULL;
5337 txn->cli_cookie = NULL;
5338 txn->uri = NULL;
5339
5340 http_txn_reset_req(txn);
5341 http_txn_reset_res(txn);
5342
5343 txn->req.chn = &s->req;
5344 txn->rsp.chn = &s->res;
5345
5346 txn->auth.method = HTTP_AUTH_UNKNOWN;
5347
5348 vars_init(&s->vars_txn, SCOPE_TXN);
5349 vars_init(&s->vars_reqres, SCOPE_REQ);
5350}
5351
5352/* to be used at the end of a transaction */
5353void http_end_txn(struct stream *s)
5354{
5355 struct http_txn *txn = s->txn;
5356 struct proxy *fe = strm_fe(s);
5357
5358 /* these ones will have been dynamically allocated */
5359 pool_free(pool_head_requri, txn->uri);
5360 pool_free(pool_head_capture, txn->cli_cookie);
5361 pool_free(pool_head_capture, txn->srv_cookie);
5362 pool_free(pool_head_uniqueid, s->unique_id);
5363
5364 s->unique_id = NULL;
5365 txn->uri = NULL;
5366 txn->srv_cookie = NULL;
5367 txn->cli_cookie = NULL;
5368
5369 if (s->req_cap) {
5370 struct cap_hdr *h;
5371 for (h = fe->req_cap; h; h = h->next)
5372 pool_free(h->pool, s->req_cap[h->index]);
5373 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
5374 }
5375
5376 if (s->res_cap) {
5377 struct cap_hdr *h;
5378 for (h = fe->rsp_cap; h; h = h->next)
5379 pool_free(h->pool, s->res_cap[h->index]);
5380 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
5381 }
5382
5383 if (!LIST_ISEMPTY(&s->vars_txn.head))
5384 vars_prune(&s->vars_txn, s->sess, s);
5385 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5386 vars_prune(&s->vars_reqres, s->sess, s);
5387}
5388
5389/* to be used at the end of a transaction to prepare a new one */
5390void http_reset_txn(struct stream *s)
5391{
5392 http_end_txn(s);
5393 http_init_txn(s);
5394
5395 /* reinitialise the current rule list pointer to NULL. We are sure that
5396 * any rulelist match the NULL pointer.
5397 */
5398 s->current_rule_list = NULL;
5399
5400 s->be = strm_fe(s);
5401 s->logs.logwait = strm_fe(s)->to_log;
5402 s->logs.level = 0;
5403 stream_del_srv_conn(s);
5404 s->target = NULL;
5405 /* re-init store persistence */
5406 s->store_count = 0;
5407 s->uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1);
5408
5409 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
5410
5411 /* We must trim any excess data from the response buffer, because we
5412 * may have blocked an invalid response from a server that we don't
5413 * want to accidently forward once we disable the analysers, nor do
5414 * we want those data to come along with next response. A typical
5415 * example of such data would be from a buggy server responding to
5416 * a HEAD with some data, or sending more than the advertised
5417 * content-length.
5418 */
5419 if (unlikely(ci_data(&s->res)))
5420 b_set_data(&s->res.buf, co_data(&s->res));
5421
5422 /* Now we can realign the response buffer */
5423 c_realign_if_empty(&s->res);
5424
5425 s->req.rto = strm_fe(s)->timeout.client;
5426 s->req.wto = TICK_ETERNITY;
5427
5428 s->res.rto = TICK_ETERNITY;
5429 s->res.wto = strm_fe(s)->timeout.client;
5430
5431 s->req.rex = TICK_ETERNITY;
5432 s->req.wex = TICK_ETERNITY;
5433 s->req.analyse_exp = TICK_ETERNITY;
5434 s->res.rex = TICK_ETERNITY;
5435 s->res.wex = TICK_ETERNITY;
5436 s->res.analyse_exp = TICK_ETERNITY;
5437 s->si[1].hcto = TICK_ETERNITY;
5438}
5439
5440
5441DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
5442DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
Christopher Faulet0f226952018-10-22 09:29:56 +02005443
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005444__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005445static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005446{
5447}
5448
5449
5450/*
5451 * Local variables:
5452 * c-indent-level: 8
5453 * c-basic-offset: 8
5454 * End:
5455 */