blob: c5c928d1676e26d0875caaf77c8598ae1b430591 [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
735 * incoming request. Note that this requires that a connection is
736 * allocated on the server side.
737 */
738 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
739 struct connection *conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100740 struct htx_sl *sl;
741 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200742
743 /* Note that for now we don't reuse existing proxy connections */
744 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
745 txn->req.err_state = txn->req.msg_state;
746 txn->req.msg_state = HTTP_MSG_ERROR;
747 txn->status = 500;
748 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200749 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200750
751 if (!(s->flags & SF_ERR_MASK))
752 s->flags |= SF_ERR_RESOURCE;
753 if (!(s->flags & SF_FINST_MASK))
754 s->flags |= SF_FINST_R;
755
756 return 0;
757 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200758 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100759 uri = htx_sl_req_uri(sl);
760 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200761
762 /* FIXME WTA: below we'll need to dynamically allocate the dst address */
763 if (url2sa(uri.ptr, uri.len - path.len, conn->dst, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200764 goto return_bad_req;
765
766 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200767 * uri.ptr and path.ptr (excluded). If it was not found, we need
768 * to replace from all the uri by a single "/".
769 *
770 * Instead of rewritting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100771 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200772 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200773 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100774 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Willy Tarreau69564b12019-07-18 16:17:15 +0200775 conn->target = &s->be->obj_type;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200776 }
777
778 /*
779 * 7: Now we can work with the cookies.
780 * Note that doing so might move headers in the request, but
781 * the fields will stay coherent and the URI will not move.
782 * This should only be performed in the backend.
783 */
784 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200785 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200786
787 /* add unique-id if "header-unique-id" is specified */
788
789 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
790 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
791 goto return_bad_req;
792 s->unique_id[0] = '\0';
793 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
794 }
795
796 if (sess->fe->header_unique_id && s->unique_id) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200797 struct ist n = ist2(sess->fe->header_unique_id, strlen(sess->fe->header_unique_id));
798 struct ist v = ist2(s->unique_id, strlen(s->unique_id));
799
800 if (unlikely(!http_add_header(htx, n, v)))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200801 goto return_bad_req;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200802 }
803
804 /*
805 * 9: add X-Forwarded-For if either the frontend or the backend
806 * asks for it.
807 */
808 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200809 struct http_hdr_ctx ctx = { .blk = NULL };
810 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
811 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
812
Christopher Faulete0768eb2018-10-03 16:38:02 +0200813 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200814 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200815 /* The header is set to be added only if none is present
816 * and we found it, so don't do anything.
817 */
818 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200819 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200820 /* Add an X-Forwarded-For header unless the source IP is
821 * in the 'except' network range.
822 */
823 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200824 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200825 != sess->fe->except_net.s_addr) &&
826 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200827 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200828 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200829 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200830
831 /* Note: we rely on the backend to get the header name to be used for
832 * x-forwarded-for, because the header is really meant for the backends.
833 * However, if the backend did not specify any option, we have to rely
834 * on the frontend's header name.
835 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200836 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
837 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200838 goto return_bad_req;
839 }
840 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200841 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200842 /* FIXME: for the sake of completeness, we should also support
843 * 'except' here, although it is mostly useless in this case.
844 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200845 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200846
Christopher Faulete0768eb2018-10-03 16:38:02 +0200847 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200848 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200849 pn, sizeof(pn));
850
851 /* Note: we rely on the backend to get the header name to be used for
852 * x-forwarded-for, because the header is really meant for the backends.
853 * However, if the backend did not specify any option, we have to rely
854 * on the frontend's header name.
855 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200856 chunk_printf(&trash, "%s", pn);
857 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200858 goto return_bad_req;
859 }
860 }
861
862 /*
863 * 10: add X-Original-To if either the frontend or the backend
864 * asks for it.
865 */
866 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
867
868 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200869 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 +0200870 /* Add an X-Original-To header unless the destination IP is
871 * in the 'except' network range.
872 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200873 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200874 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200875 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200876 != sess->fe->except_to.s_addr) &&
877 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200878 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200879 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200880 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200881 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200882
883 /* Note: we rely on the backend to get the header name to be used for
884 * x-original-to, because the header is really meant for the backends.
885 * However, if the backend did not specify any option, we have to rely
886 * on the frontend's header name.
887 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200888 if (s->be->orgto_hdr_len)
889 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
890 else
891 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200892
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200893 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
894 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200895 goto return_bad_req;
896 }
897 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200898 }
899
Christopher Faulete0768eb2018-10-03 16:38:02 +0200900 /* If we have no server assigned yet and we're balancing on url_param
901 * with a POST request, we may be interested in checking the body for
902 * that parameter. This will be done in another analyser.
903 */
904 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100905 s->txn->meth == HTTP_METH_POST &&
906 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200907 channel_dont_connect(req);
908 req->analysers |= AN_REQ_HTTP_BODY;
909 }
910
911 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
912 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100913
Christopher Faulete0768eb2018-10-03 16:38:02 +0200914 /* We expect some data from the client. Unless we know for sure
915 * we already have a full request, we have to re-enable quick-ack
916 * in case we previously disabled it, otherwise we might cause
917 * the client to delay further data.
918 */
919 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200920 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100921 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200922
923 /*************************************************************
924 * OK, that's finished for the headers. We have done what we *
925 * could. Let's switch to the DATA state. *
926 ************************************************************/
927 req->analyse_exp = TICK_ETERNITY;
928 req->analysers &= ~an_bit;
929
930 s->logs.tv_request = now;
931 /* OK let's go on with the BODY now */
932 return 1;
933
934 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200935 txn->req.err_state = txn->req.msg_state;
936 txn->req.msg_state = HTTP_MSG_ERROR;
937 txn->status = 400;
938 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200939 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200940
Olivier Houcharda798bf52019-03-08 18:52:00 +0100941 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200942 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100943 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200944
945 if (!(s->flags & SF_ERR_MASK))
946 s->flags |= SF_ERR_PRXCOND;
947 if (!(s->flags & SF_FINST_MASK))
948 s->flags |= SF_FINST_R;
949 return 0;
950}
951
952/* This function is an analyser which processes the HTTP tarpit. It always
953 * returns zero, at the beginning because it prevents any other processing
954 * from occurring, and at the end because it terminates the request.
955 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200956int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200957{
958 struct http_txn *txn = s->txn;
959
960 /* This connection is being tarpitted. The CLIENT side has
961 * already set the connect expiration date to the right
962 * timeout. We just have to check that the client is still
963 * there and that the timeout has not expired.
964 */
965 channel_dont_connect(req);
966 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
967 !tick_is_expired(req->analyse_exp, now_ms))
968 return 0;
969
970 /* We will set the queue timer to the time spent, just for
971 * logging purposes. We fake a 500 server error, so that the
972 * attacker will not suspect his connection has been tarpitted.
973 * It will not cause trouble to the logs because we can exclude
974 * the tarpitted connections by filtering on the 'PT' status flags.
975 */
976 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
977
978 if (!(req->flags & CF_READ_ERROR))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200979 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200980
981 req->analysers &= AN_REQ_FLT_END;
982 req->analyse_exp = TICK_ETERNITY;
983
984 if (!(s->flags & SF_ERR_MASK))
985 s->flags |= SF_ERR_PRXCOND;
986 if (!(s->flags & SF_FINST_MASK))
987 s->flags |= SF_FINST_T;
988 return 0;
989}
990
991/* This function is an analyser which waits for the HTTP request body. It waits
992 * for either the buffer to be full, or the full advertised contents to have
993 * reached the buffer. It must only be called after the standard HTTP request
994 * processing has occurred, because it expects the request to be parsed and will
995 * look for the Expect header. It may send a 100-Continue interim response. It
996 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
997 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
998 * needs to read more data, or 1 once it has completed its analysis.
999 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001000int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001001{
1002 struct session *sess = s->sess;
1003 struct http_txn *txn = s->txn;
1004 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001005 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001006
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001007
1008 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1009 now_ms, __FUNCTION__,
1010 s,
1011 req,
1012 req->rex, req->wex,
1013 req->flags,
1014 ci_data(req),
1015 req->analysers);
1016
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001017 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001018
Willy Tarreau4236f032019-03-05 10:43:32 +01001019 if (htx->flags & HTX_FL_PARSING_ERROR)
1020 goto return_bad_req;
1021
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001022 if (msg->msg_state < HTTP_MSG_BODY)
1023 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001024
Christopher Faulete0768eb2018-10-03 16:38:02 +02001025 /* We have to parse the HTTP request body to find any required data.
1026 * "balance url_param check_post" should have been the only way to get
1027 * into this. We were brought here after HTTP header analysis, so all
1028 * related structures are ready.
1029 */
1030
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001031 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001032 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01001033 goto return_bad_req;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001034 }
1035
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001036 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001037
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001038 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1039 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001040 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001041 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001042 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001043 goto http_end;
1044
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001045 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001046 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1047 txn->status = 408;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001048 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001049
1050 if (!(s->flags & SF_ERR_MASK))
1051 s->flags |= SF_ERR_CLITO;
1052 if (!(s->flags & SF_FINST_MASK))
1053 s->flags |= SF_FINST_D;
1054 goto return_err_msg;
1055 }
1056
1057 /* we get here if we need to wait for more data */
1058 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1059 /* Not enough data. We'll re-use the http-request
1060 * timeout here. Ideally, we should set the timeout
1061 * relative to the accept() date. We just set the
1062 * request timeout once at the beginning of the
1063 * request.
1064 */
1065 channel_dont_connect(req);
1066 if (!tick_isset(req->analyse_exp))
1067 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1068 return 0;
1069 }
1070
1071 http_end:
1072 /* The situation will not evolve, so let's give up on the analysis. */
1073 s->logs.tv_request = now; /* update the request timer to reflect full request */
1074 req->analysers &= ~an_bit;
1075 req->analyse_exp = TICK_ETERNITY;
1076 return 1;
1077
1078 return_bad_req: /* let's centralize all bad requests */
1079 txn->req.err_state = txn->req.msg_state;
1080 txn->req.msg_state = HTTP_MSG_ERROR;
1081 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001082 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001083
1084 if (!(s->flags & SF_ERR_MASK))
1085 s->flags |= SF_ERR_PRXCOND;
1086 if (!(s->flags & SF_FINST_MASK))
1087 s->flags |= SF_FINST_R;
1088
1089 return_err_msg:
1090 req->analysers &= AN_REQ_FLT_END;
Olivier Houcharda798bf52019-03-08 18:52:00 +01001091 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001092 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001093 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001094 return 0;
1095}
1096
1097/* This function is an analyser which forwards request body (including chunk
1098 * sizes if any). It is called as soon as we must forward, even if we forward
1099 * zero byte. The only situation where it must not be called is when we're in
1100 * tunnel mode and we want to forward till the close. It's used both to forward
1101 * remaining data and to resync after end of body. It expects the msg_state to
1102 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1103 * read more data, or 1 once we can go on with next request or end the stream.
1104 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1105 * bytes of pending data + the headers if not already done.
1106 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001107int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001108{
1109 struct session *sess = s->sess;
1110 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001111 struct http_msg *msg = &txn->req;
1112 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001113 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001114 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001115
1116 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1117 now_ms, __FUNCTION__,
1118 s,
1119 req,
1120 req->rex, req->wex,
1121 req->flags,
1122 ci_data(req),
1123 req->analysers);
1124
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001125 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001126
1127 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1128 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1129 /* Output closed while we were sending data. We must abort and
1130 * wake the other side up.
1131 */
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001132 /* Don't abort yet if we had L7 retries activated and it
1133 * was a write error, we may recover.
1134 */
1135 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
1136 (s->si[1].flags & SI_FL_L7_RETRY))
1137 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001138 msg->err_state = msg->msg_state;
1139 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001140 http_end_request(s);
1141 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001142 return 1;
1143 }
1144
1145 /* Note that we don't have to send 100-continue back because we don't
1146 * need the data to complete our job, and it's up to the server to
1147 * decide whether to return 100, 417 or anything else in return of
1148 * an "Expect: 100-continue" header.
1149 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001150 if (msg->msg_state == HTTP_MSG_BODY)
1151 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001152
Christopher Faulete0768eb2018-10-03 16:38:02 +02001153 /* in most states, we should abort in case of early close */
1154 channel_auto_close(req);
1155
1156 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001157 if (req->to_forward == CHN_INFINITE_FORWARD) {
1158 if (req->flags & (CF_SHUTR|CF_EOI)) {
1159 msg->msg_state = HTTP_MSG_DONE;
1160 req->to_forward = 0;
1161 goto done;
1162 }
1163 }
1164 else {
1165 /* We can't process the buffer's contents yet */
1166 req->flags |= CF_WAKE_WRITE;
1167 goto missing_data_or_waiting;
1168 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001169 }
1170
Christopher Faulet9768c262018-10-22 09:34:31 +02001171 if (msg->msg_state >= HTTP_MSG_DONE)
1172 goto done;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001173 /* Forward input data. We get it by removing all outgoing data not
1174 * forwarded yet from HTX data size. If there are some data filters, we
1175 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001176 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001177 if (HAS_REQ_DATA_FILTERS(s)) {
1178 ret = flt_http_payload(s, msg, htx->data);
1179 if (ret < 0)
1180 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001181 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001182 }
1183 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001184 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001185 if (msg->flags & HTTP_MSGF_XFER_LEN)
1186 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001187 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001188
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001189 if (txn->meth == HTTP_METH_CONNECT) {
1190 msg->msg_state = HTTP_MSG_TUNNEL;
1191 goto done;
1192 }
1193
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001194
Christopher Faulet9768c262018-10-22 09:34:31 +02001195 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001196 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1197 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001198 */
1199 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1200 goto missing_data_or_waiting;
1201
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001202 msg->msg_state = HTTP_MSG_ENDING;
1203 if (htx->data != co_data(req))
1204 goto missing_data_or_waiting;
Christopher Faulet9768c262018-10-22 09:34:31 +02001205 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletaed68d42019-03-28 18:12:46 +01001206 req->to_forward = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001207
1208 done:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001209 /* other states, DONE...TUNNEL */
1210 /* we don't want to forward closes on DONE except in tunnel mode. */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001211 if (!(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001212 channel_dont_close(req);
1213
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001214 if (HAS_REQ_DATA_FILTERS(s)) {
1215 ret = flt_http_end(s, msg);
1216 if (ret <= 0) {
1217 if (!ret)
1218 goto missing_data_or_waiting;
1219 goto return_bad_req;
1220 }
1221 }
1222
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001223 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001224 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001225 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001226 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1227 if (req->flags & CF_SHUTW) {
1228 /* request errors are most likely due to the
1229 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001230 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001231 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001232 goto return_bad_req;
1233 }
1234 return 1;
1235 }
1236
1237 /* If "option abortonclose" is set on the backend, we want to monitor
1238 * the client's connection and forward any shutdown notification to the
1239 * server, which will decide whether to close or to go on processing the
1240 * request. We only do that in tunnel mode, and not in other modes since
1241 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001242 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001243 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001244 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001245 s->si[1].flags |= SI_FL_NOLINGER;
1246 channel_auto_close(req);
1247 }
1248 else if (s->txn->meth == HTTP_METH_POST) {
1249 /* POST requests may require to read extra CRLF sent by broken
1250 * browsers and which could cause an RST to be sent upon close
1251 * on some systems (eg: Linux). */
1252 channel_auto_read(req);
1253 }
1254 return 0;
1255
1256 missing_data_or_waiting:
1257 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001258 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001259 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001260
1261 waiting:
1262 /* waiting for the last bits to leave the buffer */
1263 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001264 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001265
Christopher Faulet47365272018-10-31 17:40:50 +01001266 if (htx->flags & HTX_FL_PARSING_ERROR)
1267 goto return_bad_req;
Christopher Faulet9768c262018-10-22 09:34:31 +02001268
Christopher Faulete0768eb2018-10-03 16:38:02 +02001269 /* When TE: chunked is used, we need to get there again to parse remaining
1270 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1271 * And when content-length is used, we never want to let the possible
1272 * shutdown be forwarded to the other side, as the state machine will
1273 * take care of it once the client responds. It's also important to
1274 * prevent TIME_WAITs from accumulating on the backend side, and for
1275 * HTTP/2 where the last frame comes with a shutdown.
1276 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001277 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001278 channel_dont_close(req);
1279
1280 /* We know that more data are expected, but we couldn't send more that
1281 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1282 * system knows it must not set a PUSH on this first part. Interactive
1283 * modes are already handled by the stream sock layer. We must not do
1284 * this in content-length mode because it could present the MSG_MORE
1285 * flag with the last block of forwarded data, which would cause an
1286 * additional delay to be observed by the receiver.
1287 */
1288 if (msg->flags & HTTP_MSGF_TE_CHNK)
1289 req->flags |= CF_EXPECT_MORE;
1290
1291 return 0;
1292
Christopher Faulet93e02d82019-03-08 14:18:50 +01001293 return_cli_abort:
1294 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1295 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
1296 if (objt_server(s->target))
1297 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
1298 if (!(s->flags & SF_ERR_MASK))
1299 s->flags |= SF_ERR_CLICL;
1300 status = 400;
1301 goto return_error;
1302
1303 return_srv_abort:
1304 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1305 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
1306 if (objt_server(s->target))
1307 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
1308 if (!(s->flags & SF_ERR_MASK))
1309 s->flags |= SF_ERR_SRVCL;
1310 status = 502;
1311 goto return_error;
1312
1313 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001314 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001315 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001316 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001317 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01001318 s->flags |= SF_ERR_CLICL;
1319 status = 400;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001320
Christopher Faulet93e02d82019-03-08 14:18:50 +01001321 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001322 txn->req.err_state = txn->req.msg_state;
1323 txn->req.msg_state = HTTP_MSG_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001324 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001325 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001326 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001327 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001328 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001329 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001330 }
1331 req->analysers &= AN_REQ_FLT_END;
1332 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 +01001333 if (!(s->flags & SF_FINST_MASK))
1334 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001335 return 0;
1336}
1337
Olivier Houcharda254a372019-04-05 15:30:12 +02001338/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1339/* Returns 0 if we can attempt to retry, -1 otherwise */
1340static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1341{
1342 struct channel *req, *res;
1343 int co_data;
1344
1345 si->conn_retries--;
1346 if (si->conn_retries < 0)
1347 return -1;
1348
Willy Tarreau223995e2019-05-04 10:38:31 +02001349 if (objt_server(s->target))
1350 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1351 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1352
Olivier Houcharda254a372019-04-05 15:30:12 +02001353 req = &s->req;
1354 res = &s->res;
1355 /* Remove any write error from the request, and read error from the response */
1356 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1357 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1358 res->analysers = 0;
1359 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001360 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001361 si->exp = TICK_ETERNITY;
1362 res->rex = TICK_ETERNITY;
1363 res->to_forward = 0;
1364 res->analyse_exp = TICK_ETERNITY;
1365 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001366 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001367 si_release_endpoint(&s->si[1]);
1368 b_free(&req->buf);
1369 /* Swap the L7 buffer with the channel buffer */
1370 /* We know we stored the co_data as b_data, so get it there */
1371 co_data = b_data(&si->l7_buffer);
1372 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1373 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1374
1375 co_set_data(req, co_data);
1376 b_reset(&res->buf);
1377 co_set_data(res, 0);
1378 return 0;
1379}
1380
Christopher Faulete0768eb2018-10-03 16:38:02 +02001381/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1382 * processing can continue on next analysers, or zero if it either needs more
1383 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1384 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1385 * when it has nothing left to do, and may remove any analyser when it wants to
1386 * abort.
1387 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001388int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001389{
Christopher Faulet9768c262018-10-22 09:34:31 +02001390 /*
1391 * We will analyze a complete HTTP response to check the its syntax.
1392 *
1393 * Once the start line and all headers are received, we may perform a
1394 * capture of the error (if any), and we will set a few fields. We also
1395 * logging and finally headers capture.
1396 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001397 struct session *sess = s->sess;
1398 struct http_txn *txn = s->txn;
1399 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001400 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001401 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001402 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001403 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001404 int n;
1405
1406 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1407 now_ms, __FUNCTION__,
1408 s,
1409 rep,
1410 rep->rex, rep->wex,
1411 rep->flags,
1412 ci_data(rep),
1413 rep->analysers);
1414
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001415 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001416
Willy Tarreau4236f032019-03-05 10:43:32 +01001417 /* Parsing errors are caught here */
1418 if (htx->flags & HTX_FL_PARSING_ERROR)
1419 goto return_bad_res;
1420
Christopher Faulete0768eb2018-10-03 16:38:02 +02001421 /*
1422 * Now we quickly check if we have found a full valid response.
1423 * If not so, we check the FD and buffer states before leaving.
1424 * A full response is indicated by the fact that we have seen
1425 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1426 * responses are checked first.
1427 *
1428 * Depending on whether the client is still there or not, we
1429 * may send an error response back or not. Note that normally
1430 * we should only check for HTTP status there, and check I/O
1431 * errors somewhere else.
1432 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001433 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001434 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001435 /* 1: have we encountered a read error ? */
1436 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001437 struct connection *conn = NULL;
1438
Olivier Houchard865d8392019-05-03 22:46:27 +02001439 if (objt_cs(s->si[1].end))
1440 conn = objt_cs(s->si[1].end)->conn;
1441
1442 if (si_b->flags & SI_FL_L7_RETRY &&
1443 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001444 /* If we arrive here, then CF_READ_ERROR was
1445 * set by si_cs_recv() because we matched a
1446 * status, overwise it would have removed
1447 * the SI_FL_L7_RETRY flag, so it's ok not
1448 * to check s->be->retry_type.
1449 */
1450 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1451 return 0;
1452 }
1453
Olivier Houchard6db16992019-05-17 15:40:49 +02001454 if (txn->flags & TX_NOT_FIRST)
1455 goto abort_keep_alive;
1456
Olivier Houcharda798bf52019-03-08 18:52:00 +01001457 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001458 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001459 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001460 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001461 }
1462
Christopher Faulete0768eb2018-10-03 16:38:02 +02001463 rep->analysers &= AN_RES_FLT_END;
1464 txn->status = 502;
1465
1466 /* Check to see if the server refused the early data.
1467 * If so, just send a 425
1468 */
Olivier Houchard865d8392019-05-03 22:46:27 +02001469 if (conn->err_code == CO_ER_SSL_EARLY_FAILED) {
1470 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001471 (si_b->flags & SI_FL_L7_RETRY) &&
Olivier Houchard865d8392019-05-03 22:46:27 +02001472 do_l7_retry(s, si_b) == 0)
1473 return 0;
1474 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001475 }
1476
1477 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001478 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001479
1480 if (!(s->flags & SF_ERR_MASK))
1481 s->flags |= SF_ERR_SRVCL;
1482 if (!(s->flags & SF_FINST_MASK))
1483 s->flags |= SF_FINST_H;
1484 return 0;
1485 }
1486
Christopher Faulet9768c262018-10-22 09:34:31 +02001487 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001488 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001489 if ((si_b->flags & SI_FL_L7_RETRY) &&
1490 (s->be->retry_type & PR_RE_TIMEOUT)) {
1491 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1492 return 0;
1493 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001494 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001495 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001496 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001497 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001498 }
1499
Christopher Faulete0768eb2018-10-03 16:38:02 +02001500 rep->analysers &= AN_RES_FLT_END;
1501 txn->status = 504;
1502 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001503 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001504
1505 if (!(s->flags & SF_ERR_MASK))
1506 s->flags |= SF_ERR_SRVTO;
1507 if (!(s->flags & SF_FINST_MASK))
1508 s->flags |= SF_FINST_H;
1509 return 0;
1510 }
1511
Christopher Faulet9768c262018-10-22 09:34:31 +02001512 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001513 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001514 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1515 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001516 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001517 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001518
1519 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001520 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001521 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001522
1523 if (!(s->flags & SF_ERR_MASK))
1524 s->flags |= SF_ERR_CLICL;
1525 if (!(s->flags & SF_FINST_MASK))
1526 s->flags |= SF_FINST_H;
1527
1528 /* process_stream() will take care of the error */
1529 return 0;
1530 }
1531
Christopher Faulet9768c262018-10-22 09:34:31 +02001532 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001533 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001534 if ((si_b->flags & SI_FL_L7_RETRY) &&
1535 (s->be->retry_type & PR_RE_DISCONNECTED)) {
1536 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1537 return 0;
1538 }
1539
Olivier Houchard6db16992019-05-17 15:40:49 +02001540 if (txn->flags & TX_NOT_FIRST)
1541 goto abort_keep_alive;
1542
Olivier Houcharda798bf52019-03-08 18:52:00 +01001543 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001544 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001545 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001546 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001547 }
1548
Christopher Faulete0768eb2018-10-03 16:38:02 +02001549 rep->analysers &= AN_RES_FLT_END;
1550 txn->status = 502;
1551 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001552 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001553
1554 if (!(s->flags & SF_ERR_MASK))
1555 s->flags |= SF_ERR_SRVCL;
1556 if (!(s->flags & SF_FINST_MASK))
1557 s->flags |= SF_FINST_H;
1558 return 0;
1559 }
1560
Christopher Faulet9768c262018-10-22 09:34:31 +02001561 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001562 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001563 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001564 goto abort_keep_alive;
1565
Olivier Houcharda798bf52019-03-08 18:52:00 +01001566 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001567 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001568
1569 if (!(s->flags & SF_ERR_MASK))
1570 s->flags |= SF_ERR_CLICL;
1571 if (!(s->flags & SF_FINST_MASK))
1572 s->flags |= SF_FINST_H;
1573
1574 /* process_stream() will take care of the error */
1575 return 0;
1576 }
1577
1578 channel_dont_close(rep);
1579 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
1580 return 0;
1581 }
1582
1583 /* More interesting part now : we know that we have a complete
1584 * response which at least looks like HTTP. We have an indicator
1585 * of each header's length, so we can parse them quickly.
1586 */
1587
Christopher Faulet9768c262018-10-22 09:34:31 +02001588 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001589 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001590 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001591
Christopher Faulet9768c262018-10-22 09:34:31 +02001592 /* 0: we might have to print this header in debug mode */
1593 if (unlikely((global.mode & MODE_DEBUG) &&
1594 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1595 int32_t pos;
1596
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001597 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001598
Christopher Fauleta3f15502019-05-13 15:27:23 +02001599 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001600 struct htx_blk *blk = htx_get_blk(htx, pos);
1601 enum htx_blk_type type = htx_get_blk_type(blk);
1602
1603 if (type == HTX_BLK_EOH)
1604 break;
1605 if (type != HTX_BLK_HDR)
1606 continue;
1607
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001608 http_debug_hdr("srvhdr", s,
1609 htx_get_blk_name(htx, blk),
1610 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001611 }
1612 }
1613
Christopher Faulet03599112018-11-27 11:21:21 +01001614 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001615 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001616 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001617 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001618 if (sl->flags & HTX_SL_F_XFER_LEN) {
1619 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001620 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001621 if (sl->flags & HTX_SL_F_BODYLESS)
1622 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001623 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001624
1625 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001626 if (n < 1 || n > 5)
1627 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001628
Christopher Faulete0768eb2018-10-03 16:38:02 +02001629 /* when the client triggers a 4xx from the server, it's most often due
1630 * to a missing object or permission. These events should be tracked
1631 * because if they happen often, it may indicate a brute force or a
1632 * vulnerability scan.
1633 */
1634 if (n == 4)
1635 stream_inc_http_err_ctr(s);
1636
1637 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001638 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001639
Christopher Faulete0768eb2018-10-03 16:38:02 +02001640 /* Adjust server's health based on status code. Note: status codes 501
1641 * and 505 are triggered on demand by client request, so we must not
1642 * count them as server failures.
1643 */
1644 if (objt_server(s->target)) {
1645 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001646 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001647 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001648 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001649 }
1650
1651 /*
1652 * We may be facing a 100-continue response, or any other informational
1653 * 1xx response which is non-final, in which case this is not the right
1654 * response, and we're waiting for the next one. Let's allow this response
1655 * to go to the client and wait for the next one. There's an exception for
1656 * 101 which is used later in the code to switch protocols.
1657 */
1658 if (txn->status < 200 &&
1659 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001660 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001661 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001662 msg->msg_state = HTTP_MSG_RPBEFORE;
1663 txn->status = 0;
1664 s->logs.t_data = -1; /* was not a response yet */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001665 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001666 }
1667
1668 /*
1669 * 2: check for cacheability.
1670 */
1671
1672 switch (txn->status) {
1673 case 200:
1674 case 203:
1675 case 204:
1676 case 206:
1677 case 300:
1678 case 301:
1679 case 404:
1680 case 405:
1681 case 410:
1682 case 414:
1683 case 501:
1684 break;
1685 default:
1686 /* RFC7231#6.1:
1687 * Responses with status codes that are defined as
1688 * cacheable by default (e.g., 200, 203, 204, 206,
1689 * 300, 301, 404, 405, 410, 414, and 501 in this
1690 * specification) can be reused by a cache with
1691 * heuristic expiration unless otherwise indicated
1692 * by the method definition or explicit cache
1693 * controls [RFC7234]; all other status codes are
1694 * not cacheable by default.
1695 */
1696 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1697 break;
1698 }
1699
1700 /*
1701 * 3: we may need to capture headers
1702 */
1703 s->logs.logwait &= ~LW_RESP;
1704 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001705 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001706
Christopher Faulet9768c262018-10-22 09:34:31 +02001707 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001708 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1709 txn->status == 101)) {
1710 /* Either we've established an explicit tunnel, or we're
1711 * switching the protocol. In both cases, we're very unlikely
1712 * to understand the next protocols. We have to switch to tunnel
1713 * mode, so that we transfer the request and responses then let
1714 * this protocol pass unmodified. When we later implement specific
1715 * parsers for such protocols, we'll want to check the Upgrade
1716 * header which contains information about that protocol for
1717 * responses with status 101 (eg: see RFC2817 about TLS).
1718 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001719 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001720 }
1721
Christopher Faulet61608322018-11-23 16:23:45 +01001722 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1723 * 407 (Proxy-Authenticate) responses and set the connection to private
1724 */
1725 srv_conn = cs_conn(objt_cs(s->si[1].end));
1726 if (srv_conn) {
1727 struct ist hdr;
1728 struct http_hdr_ctx ctx;
1729
1730 if (txn->status == 401)
1731 hdr = ist("WWW-Authenticate");
1732 else if (txn->status == 407)
1733 hdr = ist("Proxy-Authenticate");
1734 else
1735 goto end;
1736
1737 ctx.blk = NULL;
1738 while (http_find_header(htx, hdr, &ctx, 0)) {
1739 if ((ctx.value.len >= 9 && word_match(ctx.value.ptr, ctx.value.len, "Negotiate", 9)) ||
Olivier Houchard250031e2019-05-29 15:01:50 +02001740 (ctx.value.len >= 4 && word_match(ctx.value.ptr, ctx.value.len, "NTLM", 4))) {
1741 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet61608322018-11-23 16:23:45 +01001742 srv_conn->flags |= CO_FL_PRIVATE;
Olivier Houchard250031e2019-05-29 15:01:50 +02001743 }
Christopher Faulet61608322018-11-23 16:23:45 +01001744 }
1745 }
1746
1747 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001748 /* we want to have the response time before we start processing it */
1749 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1750
1751 /* end of job, return OK */
1752 rep->analysers &= ~an_bit;
1753 rep->analyse_exp = TICK_ETERNITY;
1754 channel_auto_close(rep);
1755 return 1;
1756
Christopher Faulet47365272018-10-31 17:40:50 +01001757 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001758 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001759 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001760 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001761 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001762 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001763 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001764 (si_b->flags & SI_FL_L7_RETRY) &&
Olivier Houcharde3249a92019-05-03 23:01:47 +02001765 do_l7_retry(s, si_b) == 0)
1766 return 0;
Christopher Faulet47365272018-10-31 17:40:50 +01001767 txn->status = 502;
1768 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001769 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001770 rep->analysers &= AN_RES_FLT_END;
1771
1772 if (!(s->flags & SF_ERR_MASK))
1773 s->flags |= SF_ERR_PRXCOND;
1774 if (!(s->flags & SF_FINST_MASK))
1775 s->flags |= SF_FINST_H;
1776 return 0;
1777
Christopher Faulete0768eb2018-10-03 16:38:02 +02001778 abort_keep_alive:
1779 /* A keep-alive request to the server failed on a network error.
1780 * The client is required to retry. We need to close without returning
1781 * any other information so that the client retries.
1782 */
1783 txn->status = 0;
1784 rep->analysers &= AN_RES_FLT_END;
1785 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001786 s->logs.logwait = 0;
1787 s->logs.level = 0;
1788 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001789 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001790 return 0;
1791}
1792
1793/* This function performs all the processing enabled for the current response.
1794 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1795 * and updates s->res.analysers. It might make sense to explode it into several
1796 * other functions. It works like process_request (see indications above).
1797 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001798int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001799{
1800 struct session *sess = s->sess;
1801 struct http_txn *txn = s->txn;
1802 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001803 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001804 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001805 enum rule_result ret = HTTP_RULE_RES_CONT;
1806
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001807 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1808 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001809
Christopher Faulete0768eb2018-10-03 16:38:02 +02001810 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1811 now_ms, __FUNCTION__,
1812 s,
1813 rep,
1814 rep->rex, rep->wex,
1815 rep->flags,
1816 ci_data(rep),
1817 rep->analysers);
1818
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001819 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001820
1821 /* The stats applet needs to adjust the Connection header but we don't
1822 * apply any filter there.
1823 */
1824 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1825 rep->analysers &= ~an_bit;
1826 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001827 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001828 }
1829
1830 /*
1831 * We will have to evaluate the filters.
1832 * As opposed to version 1.2, now they will be evaluated in the
1833 * filters order and not in the header order. This means that
1834 * each filter has to be validated among all headers.
1835 *
1836 * Filters are tried with ->be first, then with ->fe if it is
1837 * different from ->be.
1838 *
1839 * Maybe we are in resume condiion. In this case I choose the
1840 * "struct proxy" which contains the rule list matching the resume
1841 * pointer. If none of theses "struct proxy" match, I initialise
1842 * the process with the first one.
1843 *
1844 * In fact, I check only correspondance betwwen the current list
1845 * pointer and the ->fe rule list. If it doesn't match, I initialize
1846 * the loop with the ->be.
1847 */
1848 if (s->current_rule_list == &sess->fe->http_res_rules)
1849 cur_proxy = sess->fe;
1850 else
1851 cur_proxy = s->be;
1852 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001853 /* evaluate http-response rules */
1854 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001855 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001856
1857 if (ret == HTTP_RULE_RES_BADREQ)
1858 goto return_srv_prx_502;
1859
1860 if (ret == HTTP_RULE_RES_DONE) {
1861 rep->analysers &= ~an_bit;
1862 rep->analyse_exp = TICK_ETERNITY;
1863 return 1;
1864 }
1865 }
1866
1867 /* we need to be called again. */
1868 if (ret == HTTP_RULE_RES_YIELD) {
1869 channel_dont_close(rep);
1870 return 0;
1871 }
1872
Christopher Faulete0768eb2018-10-03 16:38:02 +02001873 /* has the response been denied ? */
1874 if (txn->flags & TX_SVDENY) {
1875 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001876 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_secu, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001877
Olivier Houcharda798bf52019-03-08 18:52:00 +01001878 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
1879 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001880 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001881 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001882 goto return_srv_prx_502;
1883 }
1884
Christopher Faulete0768eb2018-10-03 16:38:02 +02001885 /* check whether we're already working on the frontend */
1886 if (cur_proxy == sess->fe)
1887 break;
1888 cur_proxy = sess->fe;
1889 }
1890
1891 /* After this point, this anayzer can't return yield, so we can
1892 * remove the bit corresponding to this analyzer from the list.
1893 *
1894 * Note that the intermediate returns and goto found previously
1895 * reset the analyzers.
1896 */
1897 rep->analysers &= ~an_bit;
1898 rep->analyse_exp = TICK_ETERNITY;
1899
1900 /* OK that's all we can do for 1xx responses */
1901 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001902 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001903
1904 /*
1905 * Now check for a server cookie.
1906 */
1907 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001908 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001909
1910 /*
1911 * Check for cache-control or pragma headers if required.
1912 */
1913 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001914 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001915
1916 /*
1917 * Add server cookie in the response if needed
1918 */
1919 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1920 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1921 (!(s->flags & SF_DIRECT) ||
1922 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1923 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1924 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1925 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1926 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1927 !(s->flags & SF_IGNORE_PRST)) {
1928 /* the server is known, it's not the one the client requested, or the
1929 * cookie's last seen date needs to be refreshed. We have to
1930 * insert a set-cookie here, except if we want to insert only on POST
1931 * requests and this one isn't. Note that servers which don't have cookies
1932 * (eg: some backup servers) will return a full cookie removal request.
1933 */
1934 if (!objt_server(s->target)->cookie) {
1935 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001936 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001937 s->be->cookie_name);
1938 }
1939 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001940 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001941
1942 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1943 /* emit last_date, which is mandatory */
1944 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1945 s30tob64((date.tv_sec+3) >> 2,
1946 trash.area + trash.data);
1947 trash.data += 5;
1948
1949 if (s->be->cookie_maxlife) {
1950 /* emit first_date, which is either the original one or
1951 * the current date.
1952 */
1953 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1954 s30tob64(txn->cookie_first_date ?
1955 txn->cookie_first_date >> 2 :
1956 (date.tv_sec+3) >> 2,
1957 trash.area + trash.data);
1958 trash.data += 5;
1959 }
1960 }
1961 chunk_appendf(&trash, "; path=/");
1962 }
1963
1964 if (s->be->cookie_domain)
1965 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1966
1967 if (s->be->ck_opts & PR_CK_HTTPONLY)
1968 chunk_appendf(&trash, "; HttpOnly");
1969
1970 if (s->be->ck_opts & PR_CK_SECURE)
1971 chunk_appendf(&trash, "; Secure");
1972
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001973 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001974 goto return_bad_resp;
1975
1976 txn->flags &= ~TX_SCK_MASK;
1977 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1978 /* the server did not change, only the date was updated */
1979 txn->flags |= TX_SCK_UPDATED;
1980 else
1981 txn->flags |= TX_SCK_INSERTED;
1982
1983 /* Here, we will tell an eventual cache on the client side that we don't
1984 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1985 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1986 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1987 */
1988 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1989
1990 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1991
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001992 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001993 goto return_bad_resp;
1994 }
1995 }
1996
1997 /*
1998 * Check if result will be cacheable with a cookie.
1999 * We'll block the response if security checks have caught
2000 * nasty things such as a cacheable cookie.
2001 */
2002 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2003 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2004 (s->be->options & PR_O_CHK_CACHE)) {
2005 /* we're in presence of a cacheable response containing
2006 * a set-cookie header. We'll block it as requested by
2007 * the 'checkcache' option, and send an alert.
2008 */
2009 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01002010 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002011
Olivier Houcharda798bf52019-03-08 18:52:00 +01002012 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
2013 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002014 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002015 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002016
2017 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2018 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2019 send_log(s->be, LOG_ALERT,
2020 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2021 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2022 goto return_srv_prx_502;
2023 }
2024
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002025 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002026 /* Always enter in the body analyzer */
2027 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2028 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2029
2030 /* if the user wants to log as soon as possible, without counting
2031 * bytes from the server, then this is the right moment. We have
2032 * to temporarily assign bytes_out to log what we currently have.
2033 */
2034 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2035 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002036 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002037 s->do_log(s);
2038 s->logs.bytes_out = 0;
2039 }
2040 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002041
2042 return_bad_resp:
2043 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01002044 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01002045 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002046 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01002047 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002048
2049 return_srv_prx_502:
2050 rep->analysers &= AN_RES_FLT_END;
2051 txn->status = 502;
2052 s->logs.t_data = -1; /* was not a valid response */
2053 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002054 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002055 if (!(s->flags & SF_ERR_MASK))
2056 s->flags |= SF_ERR_PRXCOND;
2057 if (!(s->flags & SF_FINST_MASK))
2058 s->flags |= SF_FINST_H;
2059 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002060}
2061
2062/* This function is an analyser which forwards response body (including chunk
2063 * sizes if any). It is called as soon as we must forward, even if we forward
2064 * zero byte. The only situation where it must not be called is when we're in
2065 * tunnel mode and we want to forward till the close. It's used both to forward
2066 * remaining data and to resync after end of body. It expects the msg_state to
2067 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2068 * read more data, or 1 once we can go on with next request or end the stream.
2069 *
2070 * It is capable of compressing response data both in content-length mode and
2071 * in chunked mode. The state machines follows different flows depending on
2072 * whether content-length and chunked modes are used, since there are no
2073 * trailers in content-length :
2074 *
2075 * chk-mode cl-mode
2076 * ,----- BODY -----.
2077 * / \
2078 * V size > 0 V chk-mode
2079 * .--> SIZE -------------> DATA -------------> CRLF
2080 * | | size == 0 | last byte |
2081 * | v final crlf v inspected |
2082 * | TRAILERS -----------> DONE |
2083 * | |
2084 * `----------------------------------------------'
2085 *
2086 * Compression only happens in the DATA state, and must be flushed in final
2087 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2088 * is performed at once on final states for all bytes parsed, or when leaving
2089 * on missing data.
2090 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002091int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002092{
2093 struct session *sess = s->sess;
2094 struct http_txn *txn = s->txn;
2095 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002096 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002097 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002098
2099 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
2100 now_ms, __FUNCTION__,
2101 s,
2102 res,
2103 res->rex, res->wex,
2104 res->flags,
2105 ci_data(res),
2106 res->analysers);
2107
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002108 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002109
2110 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002111 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002112 /* Output closed while we were sending data. We must abort and
2113 * wake the other side up.
2114 */
2115 msg->err_state = msg->msg_state;
2116 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002117 http_end_response(s);
2118 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002119 return 1;
2120 }
2121
Christopher Faulet9768c262018-10-22 09:34:31 +02002122 if (msg->msg_state == HTTP_MSG_BODY)
2123 msg->msg_state = HTTP_MSG_DATA;
2124
Christopher Faulete0768eb2018-10-03 16:38:02 +02002125 /* in most states, we should abort in case of early close */
2126 channel_auto_close(res);
2127
Christopher Faulete0768eb2018-10-03 16:38:02 +02002128 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002129 if (res->to_forward == CHN_INFINITE_FORWARD) {
2130 if (res->flags & (CF_SHUTR|CF_EOI)) {
2131 msg->msg_state = HTTP_MSG_DONE;
2132 res->to_forward = 0;
2133 goto done;
2134 }
2135 }
2136 else {
2137 /* We can't process the buffer's contents yet */
2138 res->flags |= CF_WAKE_WRITE;
2139 goto missing_data_or_waiting;
2140 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002141 }
2142
Christopher Faulet9768c262018-10-22 09:34:31 +02002143 if (msg->msg_state >= HTTP_MSG_DONE)
2144 goto done;
2145
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002146 /* Forward input data. We get it by removing all outgoing data not
2147 * forwarded yet from HTX data size. If there are some data filters, we
2148 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002149 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002150 if (HAS_RSP_DATA_FILTERS(s)) {
2151 ret = flt_http_payload(s, msg, htx->data);
2152 if (ret < 0)
2153 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002154 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002155 }
2156 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002157 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002158 if (msg->flags & HTTP_MSGF_XFER_LEN)
2159 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002160 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002161
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002162 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2163 (!(msg->flags & HTTP_MSGF_XFER_LEN) && (res->flags & CF_SHUTR || !HAS_RSP_DATA_FILTERS(s)))) {
2164 msg->msg_state = HTTP_MSG_TUNNEL;
2165 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002166 }
2167
Christopher Faulet9768c262018-10-22 09:34:31 +02002168 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002169 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2170 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002171 */
2172 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2173 goto missing_data_or_waiting;
2174
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002175 msg->msg_state = HTTP_MSG_ENDING;
2176 if (htx->data != co_data(res))
2177 goto missing_data_or_waiting;
Christopher Faulet9768c262018-10-22 09:34:31 +02002178 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletaed68d42019-03-28 18:12:46 +01002179 res->to_forward = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002180
2181 done:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002182 /* other states, DONE...TUNNEL */
Christopher Faulet9768c262018-10-22 09:34:31 +02002183 channel_dont_close(res);
2184
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002185 if (HAS_RSP_DATA_FILTERS(s)) {
2186 ret = flt_http_end(s, msg);
2187 if (ret <= 0) {
2188 if (!ret)
2189 goto missing_data_or_waiting;
2190 goto return_bad_res;
2191 }
2192 }
2193
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002194 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002195 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002196 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002197 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2198 if (res->flags & CF_SHUTW) {
2199 /* response errors are most likely due to the
2200 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002201 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002202 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002203 goto return_bad_res;
2204 }
2205 return 1;
2206 }
2207 return 0;
2208
2209 missing_data_or_waiting:
2210 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002211 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002212
Christopher Faulet47365272018-10-31 17:40:50 +01002213 if (htx->flags & HTX_FL_PARSING_ERROR)
2214 goto return_bad_res;
2215
Christopher Faulete0768eb2018-10-03 16:38:02 +02002216 /* stop waiting for data if the input is closed before the end. If the
2217 * client side was already closed, it means that the client has aborted,
2218 * so we don't want to count this as a server abort. Otherwise it's a
2219 * server abort.
2220 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002221 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002222 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002223 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002224 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002225 if (htx_is_empty(htx))
2226 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002227 }
2228
Christopher Faulete0768eb2018-10-03 16:38:02 +02002229 /* When TE: chunked is used, we need to get there again to parse
2230 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002231 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2232 * are filters registered on the stream, we don't want to forward a
2233 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002234 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002235 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002236 channel_dont_close(res);
2237
2238 /* We know that more data are expected, but we couldn't send more that
2239 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2240 * system knows it must not set a PUSH on this first part. Interactive
2241 * modes are already handled by the stream sock layer. We must not do
2242 * this in content-length mode because it could present the MSG_MORE
2243 * flag with the last block of forwarded data, which would cause an
2244 * additional delay to be observed by the receiver.
2245 */
2246 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2247 res->flags |= CF_EXPECT_MORE;
2248
2249 /* the stream handler will take care of timeouts and errors */
2250 return 0;
2251
Christopher Faulet93e02d82019-03-08 14:18:50 +01002252 return_srv_abort:
2253 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2254 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002255 if (objt_server(s->target))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002256 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
2257 if (!(s->flags & SF_ERR_MASK))
2258 s->flags |= SF_ERR_SRVCL;
2259 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002260
Christopher Faulet93e02d82019-03-08 14:18:50 +01002261 return_cli_abort:
2262 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2263 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002264 if (objt_server(s->target))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002265 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
2266 if (!(s->flags & SF_ERR_MASK))
2267 s->flags |= SF_ERR_CLICL;
2268 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002269
Christopher Faulet93e02d82019-03-08 14:18:50 +01002270 return_bad_res:
2271 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2272 if (objt_server(s->target)) {
2273 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
2274 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2275 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002276 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002277 s->flags |= SF_ERR_SRVCL;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002278
Christopher Faulet93e02d82019-03-08 14:18:50 +01002279 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002280 txn->rsp.err_state = txn->rsp.msg_state;
2281 txn->rsp.msg_state = HTTP_MSG_ERROR;
2282 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002283 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002284 res->analysers &= AN_RES_FLT_END;
2285 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 +02002286 if (!(s->flags & SF_FINST_MASK))
2287 s->flags |= SF_FINST_D;
2288 return 0;
2289}
2290
Christopher Fauletf2824e62018-10-01 12:12:37 +02002291/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002292 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002293 * as too large a request to build a valid response.
2294 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002295int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002296{
Christopher Faulet99daf282018-11-28 22:58:13 +01002297 struct channel *req = &s->req;
2298 struct channel *res = &s->res;
2299 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002300 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002301 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002302 struct ist status, reason, location;
2303 unsigned int flags;
2304 size_t data;
Christopher Faulet08e66462019-05-23 16:44:59 +02002305 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002306
2307 chunk = alloc_trash_chunk();
2308 if (!chunk)
Christopher Faulet99daf282018-11-28 22:58:13 +01002309 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002310
Christopher Faulet99daf282018-11-28 22:58:13 +01002311 /*
2312 * Create the location
2313 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002314 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002315 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002316 case REDIRECT_TYPE_SCHEME: {
2317 struct http_hdr_ctx ctx;
2318 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002319
Christopher Faulet99daf282018-11-28 22:58:13 +01002320 host = ist("");
2321 ctx.blk = NULL;
2322 if (http_find_header(htx, ist("Host"), &ctx, 0))
2323 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002324
Christopher Faulet297fbb42019-05-13 14:41:27 +02002325 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002326 path = http_get_path(htx_sl_req_uri(sl));
2327 /* build message using path */
2328 if (path.ptr) {
2329 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2330 int qs = 0;
2331 while (qs < path.len) {
2332 if (*(path.ptr + qs) == '?') {
2333 path.len = qs;
2334 break;
2335 }
2336 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002337 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002338 }
2339 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002340 else
2341 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002342
Christopher Faulet99daf282018-11-28 22:58:13 +01002343 if (rule->rdr_str) { /* this is an old "redirect" rule */
2344 /* add scheme */
2345 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2346 goto fail;
2347 }
2348 else {
2349 /* add scheme with executing log format */
2350 chunk->data += build_logline(s, chunk->area + chunk->data,
2351 chunk->size - chunk->data,
2352 &rule->rdr_fmt);
2353 }
2354 /* add "://" + host + path */
2355 if (!chunk_memcat(chunk, "://", 3) ||
2356 !chunk_memcat(chunk, host.ptr, host.len) ||
2357 !chunk_memcat(chunk, path.ptr, path.len))
2358 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002359
Christopher Faulet99daf282018-11-28 22:58:13 +01002360 /* append a slash at the end of the location if needed and missing */
2361 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2362 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2363 if (chunk->data + 1 >= chunk->size)
2364 goto fail;
2365 chunk->area[chunk->data++] = '/';
2366 }
2367 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002368 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002369
Christopher Faulet99daf282018-11-28 22:58:13 +01002370 case REDIRECT_TYPE_PREFIX: {
2371 struct ist path;
2372
Christopher Faulet297fbb42019-05-13 14:41:27 +02002373 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002374 path = http_get_path(htx_sl_req_uri(sl));
2375 /* build message using path */
2376 if (path.ptr) {
2377 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2378 int qs = 0;
2379 while (qs < path.len) {
2380 if (*(path.ptr + qs) == '?') {
2381 path.len = qs;
2382 break;
2383 }
2384 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002385 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002386 }
2387 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002388 else
2389 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002390
Christopher Faulet99daf282018-11-28 22:58:13 +01002391 if (rule->rdr_str) { /* this is an old "redirect" rule */
2392 /* add prefix. Note that if prefix == "/", we don't want to
2393 * add anything, otherwise it makes it hard for the user to
2394 * configure a self-redirection.
2395 */
2396 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2397 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2398 goto fail;
2399 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002400 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002401 else {
2402 /* add prefix with executing log format */
2403 chunk->data += build_logline(s, chunk->area + chunk->data,
2404 chunk->size - chunk->data,
2405 &rule->rdr_fmt);
2406 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002407
Christopher Faulet99daf282018-11-28 22:58:13 +01002408 /* add path */
2409 if (!chunk_memcat(chunk, path.ptr, path.len))
2410 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002411
Christopher Faulet99daf282018-11-28 22:58:13 +01002412 /* append a slash at the end of the location if needed and missing */
2413 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2414 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2415 if (chunk->data + 1 >= chunk->size)
2416 goto fail;
2417 chunk->area[chunk->data++] = '/';
2418 }
2419 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002420 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002421 case REDIRECT_TYPE_LOCATION:
2422 default:
2423 if (rule->rdr_str) { /* this is an old "redirect" rule */
2424 /* add location */
2425 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2426 goto fail;
2427 }
2428 else {
2429 /* add location with executing log format */
2430 chunk->data += build_logline(s, chunk->area + chunk->data,
2431 chunk->size - chunk->data,
2432 &rule->rdr_fmt);
2433 }
2434 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002435 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002436 location = ist2(chunk->area, chunk->data);
2437
2438 /*
2439 * Create the 30x response
2440 */
2441 switch (rule->code) {
2442 case 308:
2443 status = ist("308");
2444 reason = ist("Permanent Redirect");
2445 break;
2446 case 307:
2447 status = ist("307");
2448 reason = ist("Temporary Redirect");
2449 break;
2450 case 303:
2451 status = ist("303");
2452 reason = ist("See Other");
2453 break;
2454 case 301:
2455 status = ist("301");
2456 reason = ist("Moved Permanently");
2457 break;
2458 case 302:
2459 default:
2460 status = ist("302");
2461 reason = ist("Found");
2462 break;
2463 }
2464
Christopher Faulet08e66462019-05-23 16:44:59 +02002465 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2466 close = 1;
2467
Christopher Faulet99daf282018-11-28 22:58:13 +01002468 htx = htx_from_buf(&res->buf);
2469 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2470 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2471 if (!sl)
2472 goto fail;
2473 sl->info.res.status = rule->code;
2474 s->txn->status = rule->code;
2475
Christopher Faulet08e66462019-05-23 16:44:59 +02002476 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2477 goto fail;
2478
2479 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002480 !htx_add_header(htx, ist("Location"), location))
2481 goto fail;
2482
2483 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2484 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2485 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002486 }
2487
2488 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002489 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2490 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002491 }
2492
Christopher Faulet99daf282018-11-28 22:58:13 +01002493 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2494 goto fail;
2495
Christopher Fauletf2824e62018-10-01 12:12:37 +02002496 /* let's log the request time */
2497 s->logs.tv_request = now;
2498
Christopher Faulet99daf282018-11-28 22:58:13 +01002499 data = htx->data - co_data(res);
Christopher Faulet99daf282018-11-28 22:58:13 +01002500 c_adv(res, data);
2501 res->total += data;
2502
2503 channel_auto_read(req);
2504 channel_abort(req);
2505 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002506 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet99daf282018-11-28 22:58:13 +01002507
2508 res->wex = tick_add_ifset(now_ms, res->wto);
2509 channel_auto_read(res);
2510 channel_auto_close(res);
2511 channel_shutr_now(res);
2512
2513 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002514
2515 if (!(s->flags & SF_ERR_MASK))
2516 s->flags |= SF_ERR_LOCAL;
2517 if (!(s->flags & SF_FINST_MASK))
2518 s->flags |= SF_FINST_R;
2519
Christopher Faulet99daf282018-11-28 22:58:13 +01002520 free_trash_chunk(chunk);
2521 return 1;
2522
2523 fail:
2524 /* If an error occurred, remove the incomplete HTTP response from the
2525 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002526 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002527 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002528 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002529}
2530
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002531int http_transform_header_str(struct stream* s, struct channel *chn, struct htx *htx,
2532 struct ist name, const char *str, struct my_regex *re, int action)
Christopher Faulet72333522018-10-24 11:25:02 +02002533{
2534 struct http_hdr_ctx ctx;
2535 struct buffer *output = get_trash_chunk();
2536
2537 /* find full header is action is ACT_HTTP_REPLACE_HDR */
2538 ctx.blk = NULL;
2539 while (http_find_header(htx, name, &ctx, (action == ACT_HTTP_REPLACE_HDR))) {
2540 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2541 continue;
2542
2543 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2544 if (output->data == -1)
2545 return -1;
2546 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2547 return -1;
2548 }
2549 return 0;
2550}
2551
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002552static int http_transform_header(struct stream* s, struct channel *chn, struct htx *htx,
2553 const struct ist name, struct list *fmt, struct my_regex *re, int action)
Christopher Faulet72333522018-10-24 11:25:02 +02002554{
2555 struct buffer *replace;
2556 int ret = -1;
2557
2558 replace = alloc_trash_chunk();
2559 if (!replace)
2560 goto leave;
2561
2562 replace->data = build_logline(s, replace->area, replace->size, fmt);
2563 if (replace->data >= replace->size - 1)
2564 goto leave;
2565
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002566 ret = http_transform_header_str(s, chn, htx, name, replace->area, re, action);
Christopher Faulet72333522018-10-24 11:25:02 +02002567
2568 leave:
2569 free_trash_chunk(replace);
2570 return ret;
2571}
2572
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002573
2574/* Terminate a 103-Erly-hints response and send it to the client. It returns 0
2575 * on success and -1 on error. The response channel is updated accordingly.
2576 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002577static int http_reply_103_early_hints(struct channel *res)
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002578{
2579 struct htx *htx = htx_from_buf(&res->buf);
2580 size_t data;
2581
Christopher Faulet1d5ec092019-06-26 14:23:54 +02002582 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002583 /* If an error occurred during an Early-hint rule,
2584 * remove the incomplete HTTP 103 response from the
2585 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002586 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002587 return -1;
2588 }
2589
2590 data = htx->data - co_data(res);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002591 c_adv(res, data);
2592 res->total += data;
2593 return 0;
2594}
2595
Christopher Faulet6eb92892018-11-15 16:39:29 +01002596/*
2597 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
2598 * built according to <fmt> log line format.
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002599 * If <early_hints> is 0, it is starts a new response by adding the start
2600 * line. If an error occurred -1 is returned. On success 0 is returned. The
2601 * channel is not updated here. It must be done calling the function
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002602 * http_reply_103_early_hints().
Christopher Faulet6eb92892018-11-15 16:39:29 +01002603 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002604static 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 +01002605{
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002606 struct channel *res = &s->res;
2607 struct htx *htx = htx_from_buf(&res->buf);
2608 struct buffer *value = alloc_trash_chunk();
2609
Christopher Faulet6eb92892018-11-15 16:39:29 +01002610 if (!early_hints) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002611 struct htx_sl *sl;
2612 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
2613 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2614
2615 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
2616 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
2617 if (!sl)
Christopher Faulet6eb92892018-11-15 16:39:29 +01002618 goto fail;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002619 sl->info.res.status = 103;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002620 }
2621
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002622 value->data = build_logline(s, b_tail(value), b_room(value), fmt);
2623 if (!htx_add_header(htx, name, ist2(b_head(value), b_data(value))))
Christopher Faulet6eb92892018-11-15 16:39:29 +01002624 goto fail;
2625
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002626 free_trash_chunk(value);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002627 return 1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002628
2629 fail:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002630 /* If an error occurred during an Early-hint rule, remove the incomplete
2631 * HTTP 103 response from the buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002632 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002633 free_trash_chunk(value);
2634 return -1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002635}
2636
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002637/* This function executes one of the set-{method,path,query,uri} actions. It
2638 * takes the string from the variable 'replace' with length 'len', then modifies
2639 * the relevant part of the request line accordingly. Then it updates various
2640 * pointers to the next elements which were moved, and the total buffer length.
2641 * It finds the action to be performed in p[2], previously filled by function
2642 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2643 * error, though this can be revisited when this code is finally exploited.
2644 *
2645 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2646 * query string and 3 to replace uri.
2647 *
2648 * In query string case, the mark question '?' must be set at the start of the
2649 * string by the caller, event if the replacement query string is empty.
2650 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002651int http_req_replace_stline(int action, const char *replace, int len,
2652 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002653{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002654 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002655
2656 switch (action) {
2657 case 0: // method
2658 if (!http_replace_req_meth(htx, ist2(replace, len)))
2659 return -1;
2660 break;
2661
2662 case 1: // path
2663 if (!http_replace_req_path(htx, ist2(replace, len)))
2664 return -1;
2665 break;
2666
2667 case 2: // query
2668 if (!http_replace_req_query(htx, ist2(replace, len)))
2669 return -1;
2670 break;
2671
2672 case 3: // uri
2673 if (!http_replace_req_uri(htx, ist2(replace, len)))
2674 return -1;
2675 break;
2676
2677 default:
2678 return -1;
2679 }
2680 return 0;
2681}
2682
2683/* This function replace the HTTP status code and the associated message. The
2684 * variable <status> contains the new status code. This function never fails.
2685 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002686void http_res_set_status(unsigned int status, const char *reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002687{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002688 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002689 char *res;
2690
2691 chunk_reset(&trash);
2692 res = ultoa_o(status, trash.area, trash.size);
2693 trash.data = res - trash.area;
2694
2695 /* Do we have a custom reason format string? */
2696 if (reason == NULL)
2697 reason = http_get_reason(status);
2698
Christopher Faulet87a2c0d2018-12-13 21:58:18 +01002699 if (http_replace_res_status(htx, ist2(trash.area, trash.data)))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002700 http_replace_res_reason(htx, ist2(reason, strlen(reason)));
2701}
2702
Christopher Faulet3e964192018-10-24 11:39:23 +02002703/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2704 * transaction <txn>. Returns the verdict of the first rule that prevents
2705 * further processing of the request (auth, deny, ...), and defaults to
2706 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2707 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2708 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2709 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2710 * status.
2711 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002712static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
2713 struct stream *s, int *deny_status)
Christopher Faulet3e964192018-10-24 11:39:23 +02002714{
2715 struct session *sess = strm_sess(s);
2716 struct http_txn *txn = s->txn;
2717 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002718 struct act_rule *rule;
2719 struct http_hdr_ctx ctx;
2720 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002721 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2722 int act_flags = 0;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002723 int early_hints = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002724
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002725 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002726
2727 /* If "the current_rule_list" match the executed rule list, we are in
2728 * resume condition. If a resume is needed it is always in the action
2729 * and never in the ACL or converters. In this case, we initialise the
2730 * current rule, and go to the action execution point.
2731 */
2732 if (s->current_rule) {
2733 rule = s->current_rule;
2734 s->current_rule = NULL;
2735 if (s->current_rule_list == rules)
2736 goto resume_execution;
2737 }
2738 s->current_rule_list = rules;
2739
2740 list_for_each_entry(rule, rules, list) {
2741 /* check optional condition */
2742 if (rule->cond) {
2743 int ret;
2744
2745 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2746 ret = acl_pass(ret);
2747
2748 if (rule->cond->pol == ACL_COND_UNLESS)
2749 ret = !ret;
2750
2751 if (!ret) /* condition not matched */
2752 continue;
2753 }
2754
2755 act_flags |= ACT_FLAG_FIRST;
2756 resume_execution:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002757 if (early_hints && rule->action != ACT_HTTP_EARLY_HINT) {
2758 early_hints = 0;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002759 if (http_reply_103_early_hints(&s->res) == -1) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002760 rule_ret = HTTP_RULE_RES_BADREQ;
2761 goto end;
2762 }
2763 }
2764
Christopher Faulet3e964192018-10-24 11:39:23 +02002765 switch (rule->action) {
2766 case ACT_ACTION_ALLOW:
2767 rule_ret = HTTP_RULE_RES_STOP;
2768 goto end;
2769
2770 case ACT_ACTION_DENY:
2771 if (deny_status)
2772 *deny_status = rule->deny_status;
2773 rule_ret = HTTP_RULE_RES_DENY;
2774 goto end;
2775
2776 case ACT_HTTP_REQ_TARPIT:
2777 txn->flags |= TX_CLTARPIT;
2778 if (deny_status)
2779 *deny_status = rule->deny_status;
2780 rule_ret = HTTP_RULE_RES_DENY;
2781 goto end;
2782
2783 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002784 /* Auth might be performed on regular http-req rules as well as on stats */
2785 auth_realm = rule->arg.auth.realm;
2786 if (!auth_realm) {
2787 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2788 auth_realm = STATS_DEFAULT_REALM;
2789 else
2790 auth_realm = px->id;
2791 }
2792 /* send 401/407 depending on whether we use a proxy or not. We still
2793 * count one error, because normal browsing won't significantly
2794 * increase the counter but brute force attempts will.
2795 */
Christopher Faulet3e964192018-10-24 11:39:23 +02002796 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002797 if (http_reply_40x_unauthorized(s, auth_realm) == -1)
Christopher Faulet12c51e22018-11-28 15:59:42 +01002798 rule_ret = HTTP_RULE_RES_BADREQ;
2799 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02002800 goto end;
2801
2802 case ACT_HTTP_REDIR:
Christopher Faulet3e964192018-10-24 11:39:23 +02002803 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002804 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3e964192018-10-24 11:39:23 +02002805 rule_ret = HTTP_RULE_RES_BADREQ;
2806 goto end;
2807
2808 case ACT_HTTP_SET_NICE:
2809 s->task->nice = rule->arg.nice;
2810 break;
2811
2812 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01002813 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Christopher Faulet3e964192018-10-24 11:39:23 +02002814 break;
2815
2816 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01002817 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Christopher Faulet3e964192018-10-24 11:39:23 +02002818 break;
2819
2820 case ACT_HTTP_SET_LOGL:
2821 s->logs.level = rule->arg.loglevel;
2822 break;
2823
2824 case ACT_HTTP_REPLACE_HDR:
2825 case ACT_HTTP_REPLACE_VAL:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002826 if (http_transform_header(s, &s->req, htx,
2827 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
2828 &rule->arg.hdr_add.fmt,
2829 rule->arg.hdr_add.re, rule->action)) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002830 rule_ret = HTTP_RULE_RES_BADREQ;
2831 goto end;
2832 }
2833 break;
2834
2835 case ACT_HTTP_DEL_HDR:
2836 /* remove all occurrences of the header */
2837 ctx.blk = NULL;
2838 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
2839 http_remove_header(htx, &ctx);
2840 break;
2841
2842 case ACT_HTTP_SET_HDR:
2843 case ACT_HTTP_ADD_HDR: {
2844 /* The scope of the trash buffer must be limited to this function. The
2845 * build_logline() function can execute a lot of other function which
2846 * can use the trash buffer. So for limiting the scope of this global
2847 * buffer, we build first the header value using build_logline, and
2848 * after we store the header name.
2849 */
2850 struct buffer *replace;
2851 struct ist n, v;
2852
2853 replace = alloc_trash_chunk();
2854 if (!replace) {
2855 rule_ret = HTTP_RULE_RES_BADREQ;
2856 goto end;
2857 }
2858
2859 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
2860 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2861 v = ist2(replace->area, replace->data);
2862
2863 if (rule->action == ACT_HTTP_SET_HDR) {
2864 /* remove all occurrences of the header */
2865 ctx.blk = NULL;
2866 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
2867 http_remove_header(htx, &ctx);
2868 }
2869
2870 if (!http_add_header(htx, n, v)) {
2871 static unsigned char rate_limit = 0;
2872
2873 if ((rate_limit++ & 255) == 0) {
2874 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);
2875 }
2876
Olivier Houcharda798bf52019-03-08 18:52:00 +01002877 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002878 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002879 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002880 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002881 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002882 }
2883 free_trash_chunk(replace);
2884 break;
2885 }
2886
2887 case ACT_HTTP_DEL_ACL:
2888 case ACT_HTTP_DEL_MAP: {
2889 struct pat_ref *ref;
2890 struct buffer *key;
2891
2892 /* collect reference */
2893 ref = pat_ref_lookup(rule->arg.map.ref);
2894 if (!ref)
2895 continue;
2896
2897 /* allocate key */
2898 key = alloc_trash_chunk();
2899 if (!key) {
2900 rule_ret = HTTP_RULE_RES_BADREQ;
2901 goto end;
2902 }
2903
2904 /* collect key */
2905 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2906 key->area[key->data] = '\0';
2907
2908 /* perform update */
2909 /* returned code: 1=ok, 0=ko */
2910 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
2911 pat_ref_delete(ref, key->area);
2912 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
2913
2914 free_trash_chunk(key);
2915 break;
2916 }
2917
2918 case ACT_HTTP_ADD_ACL: {
2919 struct pat_ref *ref;
2920 struct buffer *key;
2921
2922 /* collect reference */
2923 ref = pat_ref_lookup(rule->arg.map.ref);
2924 if (!ref)
2925 continue;
2926
2927 /* allocate key */
2928 key = alloc_trash_chunk();
2929 if (!key) {
2930 rule_ret = HTTP_RULE_RES_BADREQ;
2931 goto end;
2932 }
2933
2934 /* collect key */
2935 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2936 key->area[key->data] = '\0';
2937
2938 /* perform update */
2939 /* add entry only if it does not already exist */
2940 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
2941 if (pat_ref_find_elt(ref, key->area) == NULL)
2942 pat_ref_add(ref, key->area, NULL, NULL);
2943 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
2944
2945 free_trash_chunk(key);
2946 break;
2947 }
2948
2949 case ACT_HTTP_SET_MAP: {
2950 struct pat_ref *ref;
2951 struct buffer *key, *value;
2952
2953 /* collect reference */
2954 ref = pat_ref_lookup(rule->arg.map.ref);
2955 if (!ref)
2956 continue;
2957
2958 /* allocate key */
2959 key = alloc_trash_chunk();
2960 if (!key) {
2961 rule_ret = HTTP_RULE_RES_BADREQ;
2962 goto end;
2963 }
2964
2965 /* allocate value */
2966 value = alloc_trash_chunk();
2967 if (!value) {
2968 free_trash_chunk(key);
2969 rule_ret = HTTP_RULE_RES_BADREQ;
2970 goto end;
2971 }
2972
2973 /* collect key */
2974 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2975 key->area[key->data] = '\0';
2976
2977 /* collect value */
2978 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
2979 value->area[value->data] = '\0';
2980
2981 /* perform update */
Christopher Faulete84289e2019-04-19 14:50:55 +02002982 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02002983 if (pat_ref_find_elt(ref, key->area) != NULL)
2984 /* update entry if it exists */
2985 pat_ref_set(ref, key->area, value->area, NULL);
2986 else
2987 /* insert a new entry */
2988 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02002989 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02002990 free_trash_chunk(key);
2991 free_trash_chunk(value);
2992 break;
2993 }
2994
2995 case ACT_HTTP_EARLY_HINT:
2996 if (!(txn->req.flags & HTTP_MSGF_VER_11))
2997 break;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002998 early_hints = http_add_early_hint_header(s, early_hints,
2999 ist2(rule->arg.early_hint.name, rule->arg.early_hint.name_len),
3000 &rule->arg.early_hint.fmt);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003001 if (early_hints == -1) {
3002 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02003003 goto end;
3004 }
3005 break;
3006
3007 case ACT_CUSTOM:
3008 if ((s->req.flags & CF_READ_ERROR) ||
3009 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Christopher Faulet3e964192018-10-24 11:39:23 +02003010 (px->options & PR_O_ABRT_CLOSE)))
3011 act_flags |= ACT_FLAG_FINAL;
3012
3013 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
3014 case ACT_RET_ERR:
3015 case ACT_RET_CONT:
3016 break;
3017 case ACT_RET_STOP:
Christopher Faulet8f1aa772019-07-04 11:27:15 +02003018 rule_ret = HTTP_RULE_RES_STOP;
3019 goto end;
3020 case ACT_RET_DONE:
Christopher Faulet3e964192018-10-24 11:39:23 +02003021 rule_ret = HTTP_RULE_RES_DONE;
3022 goto end;
3023 case ACT_RET_YIELD:
3024 s->current_rule = rule;
3025 rule_ret = HTTP_RULE_RES_YIELD;
3026 goto end;
3027 }
3028 break;
3029
3030 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3031 /* Note: only the first valid tracking parameter of each
3032 * applies.
3033 */
3034
3035 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3036 struct stktable *t;
3037 struct stksess *ts;
3038 struct stktable_key *key;
3039 void *ptr1, *ptr2;
3040
3041 t = rule->arg.trk_ctr.table.t;
3042 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL,
3043 rule->arg.trk_ctr.expr, NULL);
3044
3045 if (key && (ts = stktable_get_entry(t, key))) {
3046 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3047
3048 /* let's count a new HTTP request as it's the first time we do it */
3049 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3050 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3051 if (ptr1 || ptr2) {
3052 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3053
3054 if (ptr1)
3055 stktable_data_cast(ptr1, http_req_cnt)++;
3056
3057 if (ptr2)
3058 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
3059 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3060
3061 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3062
3063 /* If data was modified, we need to touch to re-schedule sync */
3064 stktable_touch_local(t, ts, 0);
3065 }
3066
3067 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3068 if (sess->fe != s->be)
3069 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3070 }
3071 }
3072 break;
3073
Joseph Herlantc42c0e92018-11-25 10:43:27 -08003074 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003075 default:
3076 break;
3077 }
3078 }
3079
3080 end:
3081 if (early_hints) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003082 if (http_reply_103_early_hints(&s->res) == -1)
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003083 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02003084 }
3085
3086 /* we reached the end of the rules, nothing to report */
3087 return rule_ret;
3088}
3089
3090/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3091 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3092 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3093 * is returned, the process can continue the evaluation of next rule list. If
3094 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3095 * is returned, it means the operation could not be processed and a server error
3096 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3097 * deny rule. If *YIELD is returned, the caller must call again the function
3098 * with the same context.
3099 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003100static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
3101 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02003102{
3103 struct session *sess = strm_sess(s);
3104 struct http_txn *txn = s->txn;
3105 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02003106 struct act_rule *rule;
3107 struct http_hdr_ctx ctx;
3108 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
3109 int act_flags = 0;
3110
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003111 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02003112
3113 /* If "the current_rule_list" match the executed rule list, we are in
3114 * resume condition. If a resume is needed it is always in the action
3115 * and never in the ACL or converters. In this case, we initialise the
3116 * current rule, and go to the action execution point.
3117 */
3118 if (s->current_rule) {
3119 rule = s->current_rule;
3120 s->current_rule = NULL;
3121 if (s->current_rule_list == rules)
3122 goto resume_execution;
3123 }
3124 s->current_rule_list = rules;
3125
3126 list_for_each_entry(rule, rules, list) {
3127 /* check optional condition */
3128 if (rule->cond) {
3129 int ret;
3130
3131 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3132 ret = acl_pass(ret);
3133
3134 if (rule->cond->pol == ACL_COND_UNLESS)
3135 ret = !ret;
3136
3137 if (!ret) /* condition not matched */
3138 continue;
3139 }
3140
3141 act_flags |= ACT_FLAG_FIRST;
3142resume_execution:
3143 switch (rule->action) {
3144 case ACT_ACTION_ALLOW:
3145 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3146 goto end;
3147
3148 case ACT_ACTION_DENY:
3149 txn->flags |= TX_SVDENY;
3150 rule_ret = HTTP_RULE_RES_STOP;
3151 goto end;
3152
3153 case ACT_HTTP_SET_NICE:
3154 s->task->nice = rule->arg.nice;
3155 break;
3156
3157 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003158 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Christopher Faulet3e964192018-10-24 11:39:23 +02003159 break;
3160
3161 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003162 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Christopher Faulet3e964192018-10-24 11:39:23 +02003163 break;
3164
3165 case ACT_HTTP_SET_LOGL:
3166 s->logs.level = rule->arg.loglevel;
3167 break;
3168
3169 case ACT_HTTP_REPLACE_HDR:
3170 case ACT_HTTP_REPLACE_VAL:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003171 if (http_transform_header(s, &s->res, htx,
3172 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
3173 &rule->arg.hdr_add.fmt,
3174 rule->arg.hdr_add.re, rule->action)) {
Christopher Faulet3e964192018-10-24 11:39:23 +02003175 rule_ret = HTTP_RULE_RES_BADREQ;
3176 goto end;
3177 }
3178 break;
3179
3180 case ACT_HTTP_DEL_HDR:
3181 /* remove all occurrences of the header */
3182 ctx.blk = NULL;
3183 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3184 http_remove_header(htx, &ctx);
3185 break;
3186
3187 case ACT_HTTP_SET_HDR:
3188 case ACT_HTTP_ADD_HDR: {
3189 struct buffer *replace;
3190 struct ist n, v;
3191
3192 replace = alloc_trash_chunk();
3193 if (!replace) {
3194 rule_ret = HTTP_RULE_RES_BADREQ;
3195 goto end;
3196 }
3197
3198 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
3199 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3200 v = ist2(replace->area, replace->data);
3201
3202 if (rule->action == ACT_HTTP_SET_HDR) {
3203 /* remove all occurrences of the header */
3204 ctx.blk = NULL;
3205 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3206 http_remove_header(htx, &ctx);
3207 }
3208
3209 if (!http_add_header(htx, n, v)) {
3210 static unsigned char rate_limit = 0;
3211
3212 if ((rate_limit++ & 255) == 0) {
3213 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);
3214 }
3215
Olivier Houcharda798bf52019-03-08 18:52:00 +01003216 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003217 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003218 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003219 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003220 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003221 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01003222 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003223 }
3224 free_trash_chunk(replace);
3225 break;
3226 }
3227
3228 case ACT_HTTP_DEL_ACL:
3229 case ACT_HTTP_DEL_MAP: {
3230 struct pat_ref *ref;
3231 struct buffer *key;
3232
3233 /* collect reference */
3234 ref = pat_ref_lookup(rule->arg.map.ref);
3235 if (!ref)
3236 continue;
3237
3238 /* allocate key */
3239 key = alloc_trash_chunk();
3240 if (!key) {
3241 rule_ret = HTTP_RULE_RES_BADREQ;
3242 goto end;
3243 }
3244
3245 /* collect key */
3246 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3247 key->area[key->data] = '\0';
3248
3249 /* perform update */
3250 /* returned code: 1=ok, 0=ko */
3251 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3252 pat_ref_delete(ref, key->area);
3253 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3254
3255 free_trash_chunk(key);
3256 break;
3257 }
3258
3259 case ACT_HTTP_ADD_ACL: {
3260 struct pat_ref *ref;
3261 struct buffer *key;
3262
3263 /* collect reference */
3264 ref = pat_ref_lookup(rule->arg.map.ref);
3265 if (!ref)
3266 continue;
3267
3268 /* allocate key */
3269 key = alloc_trash_chunk();
3270 if (!key) {
3271 rule_ret = HTTP_RULE_RES_BADREQ;
3272 goto end;
3273 }
3274
3275 /* collect key */
3276 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3277 key->area[key->data] = '\0';
3278
3279 /* perform update */
3280 /* check if the entry already exists */
Christopher Faulete84289e2019-04-19 14:50:55 +02003281 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003282 if (pat_ref_find_elt(ref, key->area) == NULL)
3283 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02003284 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003285 free_trash_chunk(key);
3286 break;
3287 }
3288
3289 case ACT_HTTP_SET_MAP: {
3290 struct pat_ref *ref;
3291 struct buffer *key, *value;
3292
3293 /* collect reference */
3294 ref = pat_ref_lookup(rule->arg.map.ref);
3295 if (!ref)
3296 continue;
3297
3298 /* allocate key */
3299 key = alloc_trash_chunk();
3300 if (!key) {
3301 rule_ret = HTTP_RULE_RES_BADREQ;
3302 goto end;
3303 }
3304
3305 /* allocate value */
3306 value = alloc_trash_chunk();
3307 if (!value) {
3308 free_trash_chunk(key);
3309 rule_ret = HTTP_RULE_RES_BADREQ;
3310 goto end;
3311 }
3312
3313 /* collect key */
3314 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3315 key->area[key->data] = '\0';
3316
3317 /* collect value */
3318 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3319 value->area[value->data] = '\0';
3320
3321 /* perform update */
3322 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3323 if (pat_ref_find_elt(ref, key->area) != NULL)
3324 /* update entry if it exists */
3325 pat_ref_set(ref, key->area, value->area, NULL);
3326 else
3327 /* insert a new entry */
3328 pat_ref_add(ref, key->area, value->area, NULL);
3329 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3330 free_trash_chunk(key);
3331 free_trash_chunk(value);
3332 break;
3333 }
3334
3335 case ACT_HTTP_REDIR:
3336 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003337 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3e964192018-10-24 11:39:23 +02003338 rule_ret = HTTP_RULE_RES_BADREQ;
3339 goto end;
3340
3341 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3342 /* Note: only the first valid tracking parameter of each
3343 * applies.
3344 */
3345 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3346 struct stktable *t;
3347 struct stksess *ts;
3348 struct stktable_key *key;
3349 void *ptr;
3350
3351 t = rule->arg.trk_ctr.table.t;
3352 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL,
3353 rule->arg.trk_ctr.expr, NULL);
3354
3355 if (key && (ts = stktable_get_entry(t, key))) {
3356 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3357
3358 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3359
3360 /* let's count a new HTTP request as it's the first time we do it */
3361 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3362 if (ptr)
3363 stktable_data_cast(ptr, http_req_cnt)++;
3364
3365 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3366 if (ptr)
3367 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3368 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3369
3370 /* When the client triggers a 4xx from the server, it's most often due
3371 * to a missing object or permission. These events should be tracked
3372 * because if they happen often, it may indicate a brute force or a
3373 * vulnerability scan. Normally this is done when receiving the response
3374 * but here we're tracking after this ought to have been done so we have
3375 * to do it on purpose.
3376 */
3377 if ((unsigned)(txn->status - 400) < 100) {
3378 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3379 if (ptr)
3380 stktable_data_cast(ptr, http_err_cnt)++;
3381
3382 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3383 if (ptr)
3384 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3385 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3386 }
3387
3388 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3389
3390 /* If data was modified, we need to touch to re-schedule sync */
3391 stktable_touch_local(t, ts, 0);
3392
3393 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3394 if (sess->fe != s->be)
3395 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3396 }
3397 }
3398 break;
3399
3400 case ACT_CUSTOM:
3401 if ((s->req.flags & CF_READ_ERROR) ||
3402 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Christopher Faulet3e964192018-10-24 11:39:23 +02003403 (px->options & PR_O_ABRT_CLOSE)))
3404 act_flags |= ACT_FLAG_FINAL;
3405
3406 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
3407 case ACT_RET_ERR:
3408 case ACT_RET_CONT:
3409 break;
3410 case ACT_RET_STOP:
3411 rule_ret = HTTP_RULE_RES_STOP;
3412 goto end;
Christopher Faulet8f1aa772019-07-04 11:27:15 +02003413 case ACT_RET_DONE:
3414 rule_ret = HTTP_RULE_RES_DONE;
3415 goto end;
Christopher Faulet3e964192018-10-24 11:39:23 +02003416 case ACT_RET_YIELD:
3417 s->current_rule = rule;
3418 rule_ret = HTTP_RULE_RES_YIELD;
3419 goto end;
3420 }
3421 break;
3422
Joseph Herlantc42c0e92018-11-25 10:43:27 -08003423 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003424 default:
3425 break;
3426 }
3427 }
3428
3429 end:
3430 /* we reached the end of the rules, nothing to report */
3431 return rule_ret;
3432}
3433
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003434/*
3435 * Manage client-side cookie. It can impact performance by about 2% so it is
3436 * desirable to call it only when needed. This code is quite complex because
3437 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3438 * highly recommended not to touch this part without a good reason !
3439 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003440static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003441{
3442 struct session *sess = s->sess;
3443 struct http_txn *txn = s->txn;
3444 struct htx *htx;
3445 struct http_hdr_ctx ctx;
3446 char *hdr_beg, *hdr_end, *del_from;
3447 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3448 int preserve_hdr;
3449
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003450 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003451 ctx.blk = NULL;
3452 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
3453 del_from = NULL; /* nothing to be deleted */
3454 preserve_hdr = 0; /* assume we may kill the whole header */
3455
3456 /* Now look for cookies. Conforming to RFC2109, we have to support
3457 * attributes whose name begin with a '$', and associate them with
3458 * the right cookie, if we want to delete this cookie.
3459 * So there are 3 cases for each cookie read :
3460 * 1) it's a special attribute, beginning with a '$' : ignore it.
3461 * 2) it's a server id cookie that we *MAY* want to delete : save
3462 * some pointers on it (last semi-colon, beginning of cookie...)
3463 * 3) it's an application cookie : we *MAY* have to delete a previous
3464 * "special" cookie.
3465 * At the end of loop, if a "special" cookie remains, we may have to
3466 * remove it. If no application cookie persists in the header, we
3467 * *MUST* delete it.
3468 *
3469 * Note: RFC2965 is unclear about the processing of spaces around
3470 * the equal sign in the ATTR=VALUE form. A careful inspection of
3471 * the RFC explicitly allows spaces before it, and not within the
3472 * tokens (attrs or values). An inspection of RFC2109 allows that
3473 * too but section 10.1.3 lets one think that spaces may be allowed
3474 * after the equal sign too, resulting in some (rare) buggy
3475 * implementations trying to do that. So let's do what servers do.
3476 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3477 * allowed quoted strings in values, with any possible character
3478 * after a backslash, including control chars and delimitors, which
3479 * causes parsing to become ambiguous. Browsers also allow spaces
3480 * within values even without quotes.
3481 *
3482 * We have to keep multiple pointers in order to support cookie
3483 * removal at the beginning, middle or end of header without
3484 * corrupting the header. All of these headers are valid :
3485 *
3486 * hdr_beg hdr_end
3487 * | |
3488 * v |
3489 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3490 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3491 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3492 * | | | | | | |
3493 * | | | | | | |
3494 * | | | | | | +--> next
3495 * | | | | | +----> val_end
3496 * | | | | +-----------> val_beg
3497 * | | | +--------------> equal
3498 * | | +----------------> att_end
3499 * | +---------------------> att_beg
3500 * +--------------------------> prev
3501 *
3502 */
3503 hdr_beg = ctx.value.ptr;
3504 hdr_end = hdr_beg + ctx.value.len;
3505 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3506 /* Iterate through all cookies on this line */
3507
3508 /* find att_beg */
3509 att_beg = prev;
3510 if (prev > hdr_beg)
3511 att_beg++;
3512
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) {
3853 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3854 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3855 break;
3856 is_cookie2 = 1;
3857 }
3858
3859 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3860 * <prev> points to the colon.
3861 */
3862 txn->flags |= TX_SCK_PRESENT;
3863
3864 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3865 * check-cache is enabled) and we are not interested in checking
3866 * them. Warning, the cookie capture is declared in the frontend.
3867 */
3868 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3869 break;
3870
3871 /* OK so now we know we have to process this response cookie.
3872 * The format of the Set-Cookie header is slightly different
3873 * from the format of the Cookie header in that it does not
3874 * support the comma as a cookie delimiter (thus the header
3875 * cannot be folded) because the Expires attribute described in
3876 * the original Netscape's spec may contain an unquoted date
3877 * with a comma inside. We have to live with this because
3878 * many browsers don't support Max-Age and some browsers don't
3879 * support quoted strings. However the Set-Cookie2 header is
3880 * clean.
3881 *
3882 * We have to keep multiple pointers in order to support cookie
3883 * removal at the beginning, middle or end of header without
3884 * corrupting the header (in case of set-cookie2). A special
3885 * pointer, <scav> points to the beginning of the set-cookie-av
3886 * fields after the first semi-colon. The <next> pointer points
3887 * either to the end of line (set-cookie) or next unquoted comma
3888 * (set-cookie2). All of these headers are valid :
3889 *
3890 * hdr_beg hdr_end
3891 * | |
3892 * v |
3893 * NAME1 = VALUE 1 ; Secure; Path="/" |
3894 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3895 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3896 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3897 * | | | | | | | |
3898 * | | | | | | | +-> next
3899 * | | | | | | +------------> scav
3900 * | | | | | +--------------> val_end
3901 * | | | | +--------------------> val_beg
3902 * | | | +----------------------> equal
3903 * | | +------------------------> att_end
3904 * | +----------------------------> att_beg
3905 * +------------------------------> prev
3906 * -------------------------------> hdr_beg
3907 */
3908 hdr_beg = ctx.value.ptr;
3909 hdr_end = hdr_beg + ctx.value.len;
3910 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3911
3912 /* Iterate through all cookies on this line */
3913
3914 /* find att_beg */
3915 att_beg = prev;
3916 if (prev > hdr_beg)
3917 att_beg++;
3918
3919 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3920 att_beg++;
3921
3922 /* find att_end : this is the first character after the last non
3923 * space before the equal. It may be equal to hdr_end.
3924 */
3925 equal = att_end = att_beg;
3926
3927 while (equal < hdr_end) {
3928 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3929 break;
3930 if (HTTP_IS_SPHT(*equal++))
3931 continue;
3932 att_end = equal;
3933 }
3934
3935 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3936 * is between <att_beg> and <equal>, both may be identical.
3937 */
3938
3939 /* look for end of cookie if there is an equal sign */
3940 if (equal < hdr_end && *equal == '=') {
3941 /* look for the beginning of the value */
3942 val_beg = equal + 1;
3943 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3944 val_beg++;
3945
3946 /* find the end of the value, respecting quotes */
3947 next = http_find_cookie_value_end(val_beg, hdr_end);
3948
3949 /* make val_end point to the first white space or delimitor after the value */
3950 val_end = next;
3951 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3952 val_end--;
3953 }
3954 else {
3955 /* <equal> points to next comma, semi-colon or EOL */
3956 val_beg = val_end = next = equal;
3957 }
3958
3959 if (next < hdr_end) {
3960 /* Set-Cookie2 supports multiple cookies, and <next> points to
3961 * a colon or semi-colon before the end. So skip all attr-value
3962 * pairs and look for the next comma. For Set-Cookie, since
3963 * commas are permitted in values, skip to the end.
3964 */
3965 if (is_cookie2)
3966 next = http_find_hdr_value_end(next, hdr_end);
3967 else
3968 next = hdr_end;
3969 }
3970
3971 /* Now everything is as on the diagram above */
3972
3973 /* Ignore cookies with no equal sign */
3974 if (equal == val_end)
3975 continue;
3976
3977 /* If there are spaces around the equal sign, we need to
3978 * strip them otherwise we'll get trouble for cookie captures,
3979 * or even for rewrites. Since this happens extremely rarely,
3980 * it does not hurt performance.
3981 */
3982 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3983 int stripped_before = 0;
3984 int stripped_after = 0;
3985
3986 if (att_end != equal) {
3987 memmove(att_end, equal, hdr_end - equal);
3988 stripped_before = (att_end - equal);
3989 equal += stripped_before;
3990 val_beg += stripped_before;
3991 }
3992
3993 if (val_beg > equal + 1) {
3994 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3995 stripped_after = (equal + 1) - val_beg;
3996 val_beg += stripped_after;
3997 stripped_before += stripped_after;
3998 }
3999
4000 val_end += stripped_before;
4001 next += stripped_before;
4002 hdr_end += stripped_before;
4003
Christopher Faulet3e2638e2019-06-18 09:49:16 +02004004 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004005 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004006 }
4007
4008 /* First, let's see if we want to capture this cookie. We check
4009 * that we don't already have a server side cookie, because we
4010 * can only capture one. Also as an optimisation, we ignore
4011 * cookies shorter than the declared name.
4012 */
4013 if (sess->fe->capture_name != NULL &&
4014 txn->srv_cookie == NULL &&
4015 (val_end - att_beg >= sess->fe->capture_namelen) &&
4016 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
4017 int log_len = val_end - att_beg;
4018 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
4019 ha_alert("HTTP logging : out of memory.\n");
4020 }
4021 else {
4022 if (log_len > sess->fe->capture_len)
4023 log_len = sess->fe->capture_len;
4024 memcpy(txn->srv_cookie, att_beg, log_len);
4025 txn->srv_cookie[log_len] = 0;
4026 }
4027 }
4028
4029 srv = objt_server(s->target);
4030 /* now check if we need to process it for persistence */
4031 if (!(s->flags & SF_IGNORE_PRST) &&
4032 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
4033 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
4034 /* assume passive cookie by default */
4035 txn->flags &= ~TX_SCK_MASK;
4036 txn->flags |= TX_SCK_FOUND;
4037
4038 /* If the cookie is in insert mode on a known server, we'll delete
4039 * this occurrence because we'll insert another one later.
4040 * We'll delete it too if the "indirect" option is set and we're in
4041 * a direct access.
4042 */
4043 if (s->be->ck_opts & PR_CK_PSV) {
4044 /* The "preserve" flag was set, we don't want to touch the
4045 * server's cookie.
4046 */
4047 }
4048 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
4049 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
4050 /* this cookie must be deleted */
4051 if (prev == hdr_beg && next == hdr_end) {
4052 /* whole header */
4053 http_remove_header(htx, &ctx);
4054 /* note: while both invalid now, <next> and <hdr_end>
4055 * are still equal, so the for() will stop as expected.
4056 */
4057 } else {
4058 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004059 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004060 next = prev;
4061 hdr_end += delta;
4062 }
4063 txn->flags &= ~TX_SCK_MASK;
4064 txn->flags |= TX_SCK_DELETED;
4065 /* and go on with next cookie */
4066 }
4067 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
4068 /* replace bytes val_beg->val_end with the cookie name associated
4069 * with this server since we know it.
4070 */
4071 int sliding, delta;
4072
4073 ctx.value = ist2(val_beg, val_end - val_beg);
4074 ctx.lws_before = ctx.lws_after = 0;
4075 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
4076 delta = srv->cklen - (val_end - val_beg);
4077 sliding = (ctx.value.ptr - val_beg);
4078 hdr_beg += sliding;
4079 val_beg += sliding;
4080 next += sliding + delta;
4081 hdr_end += sliding + delta;
4082
4083 txn->flags &= ~TX_SCK_MASK;
4084 txn->flags |= TX_SCK_REPLACED;
4085 }
4086 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
4087 /* insert the cookie name associated with this server
4088 * before existing cookie, and insert a delimiter between them..
4089 */
4090 int sliding, delta;
4091 ctx.value = ist2(val_beg, 0);
4092 ctx.lws_before = ctx.lws_after = 0;
4093 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
4094 delta = srv->cklen + 1;
4095 sliding = (ctx.value.ptr - val_beg);
4096 hdr_beg += sliding;
4097 val_beg += sliding;
4098 next += sliding + delta;
4099 hdr_end += sliding + delta;
4100
4101 val_beg[srv->cklen] = COOKIE_DELIM;
4102 txn->flags &= ~TX_SCK_MASK;
4103 txn->flags |= TX_SCK_REPLACED;
4104 }
4105 }
4106 /* that's done for this cookie, check the next one on the same
4107 * line when next != hdr_end (only if is_cookie2).
4108 */
4109 }
4110 }
4111}
4112
Christopher Faulet25a02f62018-10-24 12:00:25 +02004113/*
4114 * Parses the Cache-Control and Pragma request header fields to determine if
4115 * the request may be served from the cache and/or if it is cacheable. Updates
4116 * s->txn->flags.
4117 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004118void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004119{
4120 struct http_txn *txn = s->txn;
4121 struct htx *htx;
4122 int32_t pos;
4123 int pragma_found, cc_found, i;
4124
4125 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
4126 return; /* nothing more to do here */
4127
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004128 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02004129 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02004130 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004131 struct htx_blk *blk = htx_get_blk(htx, pos);
4132 enum htx_blk_type type = htx_get_blk_type(blk);
4133 struct ist n, v;
4134
4135 if (type == HTX_BLK_EOH)
4136 break;
4137 if (type != HTX_BLK_HDR)
4138 continue;
4139
4140 n = htx_get_blk_name(htx, blk);
4141 v = htx_get_blk_value(htx, blk);
4142
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004143 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004144 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4145 pragma_found = 1;
4146 continue;
4147 }
4148 }
4149
4150 /* Don't use the cache and don't try to store if we found the
4151 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004152 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004153 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4154 txn->flags |= TX_CACHE_IGNORE;
4155 continue;
4156 }
4157
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004158 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004159 continue;
4160
4161 /* OK, right now we know we have a cache-control header */
4162 cc_found = 1;
4163 if (!v.len) /* no info */
4164 continue;
4165
4166 i = 0;
4167 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4168 !isspace((unsigned char)*(v.ptr+i)))
4169 i++;
4170
4171 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
4172 * values after max-age, max-stale nor min-fresh, we simply don't
4173 * use the cache when they're specified.
4174 */
4175 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
4176 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4177 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
4178 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
4179 txn->flags |= TX_CACHE_IGNORE;
4180 continue;
4181 }
4182
4183 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
4184 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4185 continue;
4186 }
4187 }
4188
4189 /* RFC7234#5.4:
4190 * When the Cache-Control header field is also present and
4191 * understood in a request, Pragma is ignored.
4192 * When the Cache-Control header field is not present in a
4193 * request, caches MUST consider the no-cache request
4194 * pragma-directive as having the same effect as if
4195 * "Cache-Control: no-cache" were present.
4196 */
4197 if (!cc_found && pragma_found)
4198 txn->flags |= TX_CACHE_IGNORE;
4199}
4200
4201/*
4202 * Check if response is cacheable or not. Updates s->txn->flags.
4203 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004204void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004205{
4206 struct http_txn *txn = s->txn;
4207 struct htx *htx;
4208 int32_t pos;
4209 int i;
4210
4211 if (txn->status < 200) {
4212 /* do not try to cache interim responses! */
4213 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4214 return;
4215 }
4216
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004217 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004218 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004219 struct htx_blk *blk = htx_get_blk(htx, pos);
4220 enum htx_blk_type type = htx_get_blk_type(blk);
4221 struct ist n, v;
4222
4223 if (type == HTX_BLK_EOH)
4224 break;
4225 if (type != HTX_BLK_HDR)
4226 continue;
4227
4228 n = htx_get_blk_name(htx, blk);
4229 v = htx_get_blk_value(htx, blk);
4230
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004231 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004232 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4233 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4234 return;
4235 }
4236 }
4237
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004238 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004239 continue;
4240
4241 /* OK, right now we know we have a cache-control header */
4242 if (!v.len) /* no info */
4243 continue;
4244
4245 i = 0;
4246 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4247 !isspace((unsigned char)*(v.ptr+i)))
4248 i++;
4249
4250 /* we have a complete value between v.ptr and (v.ptr+i) */
4251 if (i < v.len && *(v.ptr + i) == '=') {
4252 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
4253 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
4254 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4255 continue;
4256 }
4257
4258 /* we have something of the form no-cache="set-cookie" */
4259 if ((v.len >= 21) &&
4260 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
4261 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
4262 txn->flags &= ~TX_CACHE_COOK;
4263 continue;
4264 }
4265
4266 /* OK, so we know that either p2 points to the end of string or to a comma */
4267 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
4268 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4269 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
4270 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4271 return;
4272 }
4273
4274 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4275 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4276 continue;
4277 }
4278 }
4279}
4280
Christopher Faulet64159df2018-10-24 21:15:35 +02004281/* send a server's name with an outgoing request over an established connection.
4282 * Note: this function is designed to be called once the request has been
4283 * scheduled for being forwarded. This is the reason why the number of forwarded
4284 * bytes have to be adjusted.
4285 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004286int http_send_name_header(struct stream *s, struct proxy *be, const char *srv_name)
Christopher Faulet64159df2018-10-24 21:15:35 +02004287{
4288 struct htx *htx;
4289 struct http_hdr_ctx ctx;
4290 struct ist hdr;
4291 uint32_t data;
4292
4293 hdr = ist2(be->server_id_hdr_name, be->server_id_hdr_len);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004294 htx = htxbuf(&s->req.buf);
Christopher Faulet64159df2018-10-24 21:15:35 +02004295 data = htx->data;
4296
4297 ctx.blk = NULL;
4298 while (http_find_header(htx, hdr, &ctx, 1))
4299 http_remove_header(htx, &ctx);
4300 http_add_header(htx, hdr, ist2(srv_name, strlen(srv_name)));
4301
4302 if (co_data(&s->req)) {
4303 if (data >= htx->data)
4304 c_rew(&s->req, data - htx->data);
4305 else
4306 c_adv(&s->req, htx->data - data);
4307 }
4308 return 0;
4309}
4310
Christopher Faulet377c5a52018-10-24 21:21:30 +02004311/*
4312 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4313 * for the current backend.
4314 *
4315 * It is assumed that the request is either a HEAD, GET, or POST and that the
4316 * uri_auth field is valid.
4317 *
4318 * Returns 1 if stats should be provided, otherwise 0.
4319 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004320static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004321{
4322 struct uri_auth *uri_auth = backend->uri_auth;
4323 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004324 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004325 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004326
4327 if (!uri_auth)
4328 return 0;
4329
4330 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4331 return 0;
4332
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004333 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004334 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004335 uri = htx_sl_req_uri(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004336
4337 /* check URI size */
4338 if (uri_auth->uri_len > uri.len)
4339 return 0;
4340
4341 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4342 return 0;
4343
4344 return 1;
4345}
4346
4347/* This function prepares an applet to handle the stats. It can deal with the
4348 * "100-continue" expectation, check that admin rules are met for POST requests,
4349 * and program a response message if something was unexpected. It cannot fail
4350 * and always relies on the stats applet to complete the job. It does not touch
4351 * analysers nor counters, which are left to the caller. It does not touch
4352 * s->target which is supposed to already point to the stats applet. The caller
4353 * is expected to have already assigned an appctx to the stream.
4354 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004355static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004356{
4357 struct stats_admin_rule *stats_admin_rule;
4358 struct stream_interface *si = &s->si[1];
4359 struct session *sess = s->sess;
4360 struct http_txn *txn = s->txn;
4361 struct http_msg *msg = &txn->req;
4362 struct uri_auth *uri_auth = s->be->uri_auth;
4363 const char *h, *lookup, *end;
4364 struct appctx *appctx;
4365 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004366 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004367
4368 appctx = si_appctx(si);
4369 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4370 appctx->st1 = appctx->st2 = 0;
4371 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
4372 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4373 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4374 appctx->ctx.stats.flags |= STAT_CHUNKED;
4375
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004376 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004377 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004378 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4379 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004380
4381 for (h = lookup; h <= end - 3; h++) {
4382 if (memcmp(h, ";up", 3) == 0) {
4383 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4384 break;
4385 }
4386 }
4387
4388 if (uri_auth->refresh) {
4389 for (h = lookup; h <= end - 10; h++) {
4390 if (memcmp(h, ";norefresh", 10) == 0) {
4391 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4392 break;
4393 }
4394 }
4395 }
4396
4397 for (h = lookup; h <= end - 4; h++) {
4398 if (memcmp(h, ";csv", 4) == 0) {
4399 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
4400 break;
4401 }
4402 }
4403
4404 for (h = lookup; h <= end - 6; h++) {
4405 if (memcmp(h, ";typed", 6) == 0) {
4406 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
4407 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4408 break;
4409 }
4410 }
4411
4412 for (h = lookup; h <= end - 8; h++) {
4413 if (memcmp(h, ";st=", 4) == 0) {
4414 int i;
4415 h += 4;
4416 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4417 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4418 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4419 appctx->ctx.stats.st_code = i;
4420 break;
4421 }
4422 }
4423 break;
4424 }
4425 }
4426
4427 appctx->ctx.stats.scope_str = 0;
4428 appctx->ctx.stats.scope_len = 0;
4429 for (h = lookup; h <= end - 8; h++) {
4430 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4431 int itx = 0;
4432 const char *h2;
4433 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4434 const char *err;
4435
4436 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4437 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004438 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4439 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004440 if (*h == ';' || *h == '&' || *h == ' ')
4441 break;
4442 itx++;
4443 h++;
4444 }
4445
4446 if (itx > STAT_SCOPE_TXT_MAXLEN)
4447 itx = STAT_SCOPE_TXT_MAXLEN;
4448 appctx->ctx.stats.scope_len = itx;
4449
4450 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4451 memcpy(scope_txt, h2, itx);
4452 scope_txt[itx] = '\0';
4453 err = invalid_char(scope_txt);
4454 if (err) {
4455 /* bad char in search text => clear scope */
4456 appctx->ctx.stats.scope_str = 0;
4457 appctx->ctx.stats.scope_len = 0;
4458 }
4459 break;
4460 }
4461 }
4462
4463 /* now check whether we have some admin rules for this request */
4464 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4465 int ret = 1;
4466
4467 if (stats_admin_rule->cond) {
4468 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4469 ret = acl_pass(ret);
4470 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4471 ret = !ret;
4472 }
4473
4474 if (ret) {
4475 /* no rule, or the rule matches */
4476 appctx->ctx.stats.flags |= STAT_ADMIN;
4477 break;
4478 }
4479 }
4480
Christopher Faulet5d45e382019-02-27 15:15:23 +01004481 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4482 appctx->st0 = STAT_HTTP_HEAD;
4483 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbcf242a2019-03-01 11:36:26 +01004484 if (appctx->ctx.stats.flags & STAT_ADMIN)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004485 appctx->st0 = STAT_HTTP_POST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004486 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004487 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004488 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4489 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4490 appctx->st0 = STAT_HTTP_LAST;
4491 }
4492 }
4493 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004494 /* Unsupported method */
4495 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4496 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4497 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004498 }
4499
4500 s->task->nice = -32; /* small boost for HTTP statistics */
4501 return 1;
4502}
4503
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004504void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004505{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004506 struct channel *req = &s->req;
4507 struct channel *res = &s->res;
4508 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004509 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004510 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004511 struct ist path, location;
4512 unsigned int flags;
4513 size_t data;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004514
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004515 /*
4516 * Create the location
4517 */
4518 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004519
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004520 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004521 /* special prefix "/" means don't change URL */
4522 srv = __objt_server(s->target);
4523 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4524 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4525 return;
4526 }
4527
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004528 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004529 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004530 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004531 path = http_get_path(htx_sl_req_uri(sl));
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004532 if (!path.ptr)
4533 return;
4534
4535 if (!chunk_memcat(&trash, path.ptr, path.len))
4536 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004537 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004538
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004539 /*
4540 * Create the 302 respone
4541 */
4542 htx = htx_from_buf(&res->buf);
4543 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4544 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4545 ist("HTTP/1.1"), ist("302"), ist("Found"));
4546 if (!sl)
4547 goto fail;
4548 sl->info.res.status = 302;
4549 s->txn->status = 302;
4550
4551 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4552 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4553 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4554 !htx_add_header(htx, ist("Location"), location))
4555 goto fail;
4556
4557 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4558 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004559
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004560 /*
4561 * Send the message
4562 */
4563 data = htx->data - co_data(res);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004564 c_adv(res, data);
4565 res->total += data;
4566
4567 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004568 si_shutr(si);
4569 si_shutw(si);
4570 si->err_type = SI_ET_NONE;
4571 si->state = SI_ST_CLO;
4572
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004573 channel_auto_read(req);
4574 channel_abort(req);
4575 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004576 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004577 channel_auto_read(res);
4578 channel_auto_close(res);
4579
4580 if (!(s->flags & SF_ERR_MASK))
4581 s->flags |= SF_ERR_LOCAL;
4582 if (!(s->flags & SF_FINST_MASK))
4583 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004584
4585 /* FIXME: we should increase a counter of redirects per server and per backend. */
4586 srv_inc_sess_ctr(srv);
4587 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004588 return;
4589
4590 fail:
4591 /* If an error occurred, remove the incomplete HTTP response from the
4592 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004593 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004594}
4595
Christopher Fauletf2824e62018-10-01 12:12:37 +02004596/* This function terminates the request because it was completly analyzed or
4597 * because an error was triggered during the body forwarding.
4598 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004599static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004600{
4601 struct channel *chn = &s->req;
4602 struct http_txn *txn = s->txn;
4603
4604 DPRINTF(stderr,"[%u] %s: stream=%p states=%s,%s req->analysers=0x%08x res->analysers=0x%08x\n",
4605 now_ms, __FUNCTION__, s,
4606 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
4607 s->req.analysers, s->res.analysers);
4608
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004609 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4610 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004611 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004612 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004613 goto end;
4614 }
4615
4616 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
4617 return;
4618
4619 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004620 /* No need to read anymore, the request was completely parsed.
4621 * We can shut the read side unless we want to abort_on_close,
4622 * or we have a POST request. The issue with POST requests is
4623 * that some browsers still send a CRLF after the request, and
4624 * this CRLF must be read so that it does not remain in the kernel
4625 * buffers, otherwise a close could cause an RST on some systems
4626 * (eg: Linux).
4627 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004628 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004629 channel_dont_read(chn);
4630
4631 /* if the server closes the connection, we want to immediately react
4632 * and close the socket to save packets and syscalls.
4633 */
4634 s->si[1].flags |= SI_FL_NOHALF;
4635
4636 /* In any case we've finished parsing the request so we must
4637 * disable Nagle when sending data because 1) we're not going
4638 * to shut this side, and 2) the server is waiting for us to
4639 * send pending data.
4640 */
4641 chn->flags |= CF_NEVER_WAIT;
4642
Christopher Fauletd01ce402019-01-02 17:44:13 +01004643 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4644 /* The server has not finished to respond, so we
4645 * don't want to move in order not to upset it.
4646 */
4647 return;
4648 }
4649
Christopher Fauletf2824e62018-10-01 12:12:37 +02004650 /* When we get here, it means that both the request and the
4651 * response have finished receiving. Depending on the connection
4652 * mode, we'll have to wait for the last bytes to leave in either
4653 * direction, and sometimes for a close to be effective.
4654 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004655 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004656 /* Tunnel mode will not have any analyser so it needs to
4657 * poll for reads.
4658 */
4659 channel_auto_read(chn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004660 if (b_data(&chn->buf))
4661 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004662 txn->req.msg_state = HTTP_MSG_TUNNEL;
4663 }
4664 else {
4665 /* we're not expecting any new data to come for this
4666 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004667 *
4668 * However, there is an exception if the response
4669 * length is undefined. In this case, we need to wait
4670 * the close from the server. The response will be
4671 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004672 */
4673 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4674 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004675 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004676
4677 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4678 channel_shutr_now(chn);
4679 channel_shutw_now(chn);
4680 }
4681 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004682 goto check_channel_flags;
4683 }
4684
4685 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4686 http_msg_closing:
4687 /* nothing else to forward, just waiting for the output buffer
4688 * to be empty and for the shutw_now to take effect.
4689 */
4690 if (channel_is_empty(chn)) {
4691 txn->req.msg_state = HTTP_MSG_CLOSED;
4692 goto http_msg_closed;
4693 }
4694 else if (chn->flags & CF_SHUTW) {
4695 txn->req.err_state = txn->req.msg_state;
4696 txn->req.msg_state = HTTP_MSG_ERROR;
4697 goto end;
4698 }
4699 return;
4700 }
4701
4702 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4703 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004704 /* if we don't know whether the server will close, we need to hard close */
4705 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4706 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004707 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004708 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004709 channel_dont_read(chn);
4710 goto end;
4711 }
4712
4713 check_channel_flags:
4714 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4715 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4716 /* if we've just closed an output, let's switch */
4717 txn->req.msg_state = HTTP_MSG_CLOSING;
4718 goto http_msg_closing;
4719 }
4720
4721 end:
4722 chn->analysers &= AN_REQ_FLT_END;
4723 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4724 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4725 channel_auto_close(chn);
4726 channel_auto_read(chn);
4727}
4728
4729
4730/* This function terminates the response because it was completly analyzed or
4731 * because an error was triggered during the body forwarding.
4732 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004733static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004734{
4735 struct channel *chn = &s->res;
4736 struct http_txn *txn = s->txn;
4737
4738 DPRINTF(stderr,"[%u] %s: stream=%p states=%s,%s req->analysers=0x%08x res->analysers=0x%08x\n",
4739 now_ms, __FUNCTION__, s,
4740 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
4741 s->req.analysers, s->res.analysers);
4742
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004743 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4744 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004745 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004746 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004747 goto end;
4748 }
4749
4750 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
4751 return;
4752
4753 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4754 /* In theory, we don't need to read anymore, but we must
4755 * still monitor the server connection for a possible close
4756 * while the request is being uploaded, so we don't disable
4757 * reading.
4758 */
4759 /* channel_dont_read(chn); */
4760
4761 if (txn->req.msg_state < HTTP_MSG_DONE) {
4762 /* The client seems to still be sending data, probably
4763 * because we got an error response during an upload.
4764 * We have the choice of either breaking the connection
4765 * or letting it pass through. Let's do the later.
4766 */
4767 return;
4768 }
4769
4770 /* When we get here, it means that both the request and the
4771 * response have finished receiving. Depending on the connection
4772 * mode, we'll have to wait for the last bytes to leave in either
4773 * direction, and sometimes for a close to be effective.
4774 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004775 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004776 channel_auto_read(chn);
4777 chn->flags |= CF_NEVER_WAIT;
Christopher Faulet9768c262018-10-22 09:34:31 +02004778 if (b_data(&chn->buf))
4779 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004780 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4781 }
4782 else {
4783 /* we're not expecting any new data to come for this
4784 * transaction, so we can close it.
4785 */
4786 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4787 channel_shutr_now(chn);
4788 channel_shutw_now(chn);
4789 }
4790 }
4791 goto check_channel_flags;
4792 }
4793
4794 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4795 http_msg_closing:
4796 /* nothing else to forward, just waiting for the output buffer
4797 * to be empty and for the shutw_now to take effect.
4798 */
4799 if (channel_is_empty(chn)) {
4800 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4801 goto http_msg_closed;
4802 }
4803 else if (chn->flags & CF_SHUTW) {
4804 txn->rsp.err_state = txn->rsp.msg_state;
4805 txn->rsp.msg_state = HTTP_MSG_ERROR;
Olivier Houcharda798bf52019-03-08 18:52:00 +01004806 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004807 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004808 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004809 goto end;
4810 }
4811 return;
4812 }
4813
4814 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4815 http_msg_closed:
4816 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004817 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004818 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004819 goto end;
4820 }
4821
4822 check_channel_flags:
4823 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4824 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4825 /* if we've just closed an output, let's switch */
4826 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4827 goto http_msg_closing;
4828 }
4829
4830 end:
4831 chn->analysers &= AN_RES_FLT_END;
4832 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4833 chn->analysers |= AN_RES_FLT_XFER_DATA;
4834 channel_auto_close(chn);
4835 channel_auto_read(chn);
4836}
4837
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004838void http_server_error(struct stream *s, struct stream_interface *si, int err,
4839 int finst, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004840{
4841 channel_auto_read(si_oc(si));
4842 channel_abort(si_oc(si));
4843 channel_auto_close(si_oc(si));
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004844 channel_htx_erase(si_oc(si), htxbuf(&(si_oc(si))->buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02004845 channel_auto_close(si_ic(si));
4846 channel_auto_read(si_ic(si));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004847
4848 /* <msg> is an HTX structure. So we copy it in the response's
4849 * channel */
Christopher Faulet0f226952018-10-22 09:29:56 +02004850 if (msg) {
4851 struct channel *chn = si_ic(si);
4852 struct htx *htx;
4853
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004854 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004855 chn->buf.data = msg->data;
4856 memcpy(chn->buf.area, msg->area, msg->data);
4857 htx = htx_from_buf(&chn->buf);
Christopher Faulet0f226952018-10-22 09:29:56 +02004858 c_adv(chn, htx->data);
4859 chn->total += htx->data;
4860 }
4861 if (!(s->flags & SF_ERR_MASK))
4862 s->flags |= err;
4863 if (!(s->flags & SF_FINST_MASK))
4864 s->flags |= finst;
4865}
4866
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004867void http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004868{
4869 channel_auto_read(&s->req);
4870 channel_abort(&s->req);
4871 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004872 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
4873 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02004874
4875 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004876
4877 /* <msg> is an HTX structure. So we copy it in the response's
4878 * channel */
4879 /* FIXME: It is a problem for now if there is some outgoing data */
Christopher Faulet0f226952018-10-22 09:29:56 +02004880 if (msg) {
4881 struct channel *chn = &s->res;
4882 struct htx *htx;
4883
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004884 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004885 chn->buf.data = msg->data;
4886 memcpy(chn->buf.area, msg->area, msg->data);
4887 htx = htx_from_buf(&chn->buf);
Christopher Faulet0f226952018-10-22 09:29:56 +02004888 c_adv(chn, htx->data);
4889 chn->total += htx->data;
4890 }
4891
4892 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
4893 channel_auto_read(&s->res);
4894 channel_auto_close(&s->res);
4895 channel_shutr_now(&s->res);
4896}
4897
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004898struct buffer *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004899{
4900 const int msgnum = http_get_status_idx(s->txn->status);
4901
4902 if (s->be->errmsg[msgnum].area)
4903 return &s->be->errmsg[msgnum];
4904 else if (strm_fe(s)->errmsg[msgnum].area)
4905 return &strm_fe(s)->errmsg[msgnum];
4906 else
Christopher Fauletf7346382019-07-17 22:02:08 +02004907 return &http_err_chunks[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004908}
4909
Christopher Faulet304cc402019-07-15 15:46:28 +02004910/* Return the error message corresponding to si->err_type. It is assumed
4911 * that the server side is closed. Note that err_type is actually a
4912 * bitmask, where almost only aborts may be cumulated with other
4913 * values. We consider that aborted operations are more important
4914 * than timeouts or errors due to the fact that nobody else in the
4915 * logs might explain incomplete retries. All others should avoid
4916 * being cumulated. It should normally not be possible to have multiple
4917 * aborts at once, but just in case, the first one in sequence is reported.
4918 * Note that connection errors appearing on the second request of a keep-alive
4919 * connection are not reported since this allows the client to retry.
4920 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004921void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004922{
4923 int err_type = si->err_type;
4924
4925 /* set s->txn->status for http_error_message(s) */
4926 s->txn->status = 503;
4927
4928 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004929 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4930 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004931 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004932 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4933 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4934 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004935 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004936 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4937 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004938 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004939 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4940 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004941 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004942 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4943 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4944 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004945 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004946 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4947 (s->flags & SF_SRV_REUSED) ? NULL :
4948 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004949 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004950 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4951 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4952 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004953 else { /* SI_ET_CONN_OTHER and others */
4954 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004955 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4956 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004957 }
4958}
4959
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004960
Christopher Faulet4a28a532019-03-01 11:19:40 +01004961/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4962 * on success and -1 on error.
4963 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004964static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004965{
4966 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4967 * then we must send an HTTP/1.1 100 Continue intermediate response.
4968 */
4969 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4970 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4971 struct ist hdr = { .ptr = "Expect", .len = 6 };
4972 struct http_hdr_ctx ctx;
4973
4974 ctx.blk = NULL;
4975 /* Expect is allowed in 1.1, look for it */
4976 if (http_find_header(htx, hdr, &ctx, 0) &&
4977 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004978 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004979 return -1;
4980 http_remove_header(htx, &ctx);
4981 }
4982 }
4983 return 0;
4984}
4985
Christopher Faulet23a3c792018-11-28 10:01:23 +01004986/* Send a 100-Continue response to the client. It returns 0 on success and -1
4987 * on error. The response channel is updated accordingly.
4988 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004989static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004990{
4991 struct channel *res = &s->res;
4992 struct htx *htx = htx_from_buf(&res->buf);
4993 struct htx_sl *sl;
4994 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4995 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4996 size_t data;
4997
4998 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4999 ist("HTTP/1.1"), ist("100"), ist("Continue"));
5000 if (!sl)
5001 goto fail;
5002 sl->info.res.status = 100;
5003
Christopher Faulet1d5ec092019-06-26 14:23:54 +02005004 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01005005 goto fail;
5006
5007 data = htx->data - co_data(res);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005008 c_adv(res, data);
5009 res->total += data;
5010 return 0;
5011
5012 fail:
5013 /* If an error occurred, remove the incomplete HTTP response from the
5014 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005015 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005016 return -1;
5017}
5018
Christopher Faulet12c51e22018-11-28 15:59:42 +01005019
5020/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
5021 * ont whether we use a proxy or not. It returns 0 on success and -1 on
5022 * error. The response channel is updated accordingly.
5023 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005024static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
Christopher Faulet12c51e22018-11-28 15:59:42 +01005025{
5026 struct channel *res = &s->res;
5027 struct htx *htx = htx_from_buf(&res->buf);
5028 struct htx_sl *sl;
5029 struct ist code, body;
5030 int status;
5031 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5032 size_t data;
5033
5034 if (!(s->txn->flags & TX_USE_PX_CONN)) {
5035 status = 401;
5036 code = ist("401");
5037 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
5038 "You need a valid user and password to access this content.\n"
5039 "</body></html>\n");
5040 }
5041 else {
5042 status = 407;
5043 code = ist("407");
5044 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
5045 "You need a valid user and password to access this content.\n"
5046 "</body></html>\n");
5047 }
5048
5049 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5050 ist("HTTP/1.1"), code, ist("Unauthorized"));
5051 if (!sl)
5052 goto fail;
5053 sl->info.res.status = status;
5054 s->txn->status = status;
5055
5056 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
5057 goto fail;
5058
Willy Tarreaub5ba2b02019-06-11 16:08:25 +02005059 if (!htx_add_header(htx, ist("Content-length"), ist("112")) ||
5060 !htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Faulet12c51e22018-11-28 15:59:42 +01005061 !htx_add_header(htx, ist("Connection"), ist("close")) ||
Jérôme Magnin86cef232018-12-28 14:49:08 +01005062 !htx_add_header(htx, ist("Content-Type"), ist("text/html")))
5063 goto fail;
5064 if (status == 401 && !htx_add_header(htx, ist("WWW-Authenticate"), ist2(trash.area, trash.data)))
5065 goto fail;
5066 if (status == 407 && !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005067 goto fail;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02005068 if (!htx_add_endof(htx, HTX_BLK_EOH))
5069 goto fail;
5070
5071 while (body.len) {
5072 size_t sent = htx_add_data(htx, body);
5073 if (!sent)
5074 goto fail;
5075 body.ptr += sent;
5076 body.len -= sent;
5077 }
5078
5079 if (!htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005080 goto fail;
5081
5082 data = htx->data - co_data(res);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005083 c_adv(res, data);
5084 res->total += data;
5085
5086 channel_auto_read(&s->req);
5087 channel_abort(&s->req);
5088 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005089 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet12c51e22018-11-28 15:59:42 +01005090
5091 res->wex = tick_add_ifset(now_ms, res->wto);
5092 channel_auto_read(res);
5093 channel_auto_close(res);
5094 channel_shutr_now(res);
5095 return 0;
5096
5097 fail:
5098 /* If an error occurred, remove the incomplete HTTP response from the
5099 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005100 channel_htx_truncate(res, htx);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005101 return -1;
5102}
5103
Christopher Faulet0f226952018-10-22 09:29:56 +02005104/*
5105 * Capture headers from message <htx> according to header list <cap_hdr>, and
5106 * fill the <cap> pointers appropriately.
5107 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005108static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02005109{
5110 struct cap_hdr *h;
5111 int32_t pos;
5112
Christopher Fauleta3f15502019-05-13 15:27:23 +02005113 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02005114 struct htx_blk *blk = htx_get_blk(htx, pos);
5115 enum htx_blk_type type = htx_get_blk_type(blk);
5116 struct ist n, v;
5117
5118 if (type == HTX_BLK_EOH)
5119 break;
5120 if (type != HTX_BLK_HDR)
5121 continue;
5122
5123 n = htx_get_blk_name(htx, blk);
5124
5125 for (h = cap_hdr; h; h = h->next) {
5126 if (h->namelen && (h->namelen == n.len) &&
5127 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
5128 if (cap[h->index] == NULL)
5129 cap[h->index] =
5130 pool_alloc(h->pool);
5131
5132 if (cap[h->index] == NULL) {
5133 ha_alert("HTTP capture : out of memory.\n");
5134 break;
5135 }
5136
5137 v = htx_get_blk_value(htx, blk);
5138 if (v.len > h->len)
5139 v.len = h->len;
5140
5141 memcpy(cap[h->index], v.ptr, v.len);
5142 cap[h->index][v.len]=0;
5143 }
5144 }
5145 }
5146}
5147
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005148/* Delete a value in a header between delimiters <from> and <next>. The header
5149 * itself is delimited by <start> and <end> pointers. The number of characters
5150 * displaced is returned, and the pointer to the first delimiter is updated if
5151 * required. The function tries as much as possible to respect the following
5152 * principles :
5153 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5154 * in which case <next> is simply removed
5155 * - set exactly one space character after the new first delimiter, unless there
5156 * are not enough characters in the block being moved to do so.
5157 * - remove unneeded spaces before the previous delimiter and after the new
5158 * one.
5159 *
5160 * It is the caller's responsibility to ensure that :
5161 * - <from> points to a valid delimiter or <start> ;
5162 * - <next> points to a valid delimiter or <end> ;
5163 * - there are non-space chars before <from>.
5164 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005165static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005166{
5167 char *prev = *from;
5168
5169 if (prev == start) {
5170 /* We're removing the first value. eat the semicolon, if <next>
5171 * is lower than <end> */
5172 if (next < end)
5173 next++;
5174
5175 while (next < end && HTTP_IS_SPHT(*next))
5176 next++;
5177 }
5178 else {
5179 /* Remove useless spaces before the old delimiter. */
5180 while (HTTP_IS_SPHT(*(prev-1)))
5181 prev--;
5182 *from = prev;
5183
5184 /* copy the delimiter and if possible a space if we're
5185 * not at the end of the line.
5186 */
5187 if (next < end) {
5188 *prev++ = *next++;
5189 if (prev + 1 < next)
5190 *prev++ = ' ';
5191 while (next < end && HTTP_IS_SPHT(*next))
5192 next++;
5193 }
5194 }
5195 memmove(prev, next, end - next);
5196 return (prev - next);
5197}
5198
Christopher Faulet0f226952018-10-22 09:29:56 +02005199
5200/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005201 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005202 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005203static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005204{
5205 struct ist dst = ist2(str, 0);
5206
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005207 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005208 goto end;
5209 if (dst.len + 1 > len)
5210 goto end;
5211 dst.ptr[dst.len++] = ' ';
5212
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005213 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005214 goto end;
5215 if (dst.len + 1 > len)
5216 goto end;
5217 dst.ptr[dst.len++] = ' ';
5218
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005219 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005220 end:
5221 return dst.len;
5222}
5223
5224/*
5225 * Print a debug line with a start line.
5226 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005227static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005228{
5229 struct session *sess = strm_sess(s);
5230 int max;
5231
5232 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5233 dir,
5234 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5235 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5236
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005237 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005238 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005239 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005240 trash.area[trash.data++] = ' ';
5241
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005242 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005243 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005244 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005245 trash.area[trash.data++] = ' ';
5246
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005247 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005248 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005249 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005250 trash.area[trash.data++] = '\n';
5251
5252 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5253}
5254
5255/*
5256 * Print a debug line with a header.
5257 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005258static 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 +02005259{
5260 struct session *sess = strm_sess(s);
5261 int max;
5262
5263 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5264 dir,
5265 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5266 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5267
5268 max = n.len;
5269 UBOUND(max, trash.size - trash.data - 3);
5270 chunk_memcat(&trash, n.ptr, max);
5271 trash.area[trash.data++] = ':';
5272 trash.area[trash.data++] = ' ';
5273
5274 max = v.len;
5275 UBOUND(max, trash.size - trash.data - 1);
5276 chunk_memcat(&trash, v.ptr, max);
5277 trash.area[trash.data++] = '\n';
5278
5279 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5280}
5281
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005282/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5283 * In case of allocation failure, everything allocated is freed and NULL is
5284 * returned. Otherwise the new transaction is assigned to the stream and
5285 * returned.
5286 */
5287struct http_txn *http_alloc_txn(struct stream *s)
5288{
5289 struct http_txn *txn = s->txn;
5290
5291 if (txn)
5292 return txn;
5293
5294 txn = pool_alloc(pool_head_http_txn);
5295 if (!txn)
5296 return txn;
5297
5298 s->txn = txn;
5299 return txn;
5300}
5301
5302void http_txn_reset_req(struct http_txn *txn)
5303{
5304 txn->req.flags = 0;
5305 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5306}
5307
5308void http_txn_reset_res(struct http_txn *txn)
5309{
5310 txn->rsp.flags = 0;
5311 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5312}
5313
5314/*
5315 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5316 * the required fields are properly allocated and that we only need to (re)init
5317 * them. This should be used before processing any new request.
5318 */
5319void http_init_txn(struct stream *s)
5320{
5321 struct http_txn *txn = s->txn;
5322 struct conn_stream *cs = objt_cs(s->si[0].end);
5323
5324 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5325 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5326 : 0);
5327 txn->status = -1;
5328 *(unsigned int *)txn->cache_hash = 0;
5329
5330 txn->cookie_first_date = 0;
5331 txn->cookie_last_date = 0;
5332
5333 txn->srv_cookie = NULL;
5334 txn->cli_cookie = NULL;
5335 txn->uri = NULL;
5336
5337 http_txn_reset_req(txn);
5338 http_txn_reset_res(txn);
5339
5340 txn->req.chn = &s->req;
5341 txn->rsp.chn = &s->res;
5342
5343 txn->auth.method = HTTP_AUTH_UNKNOWN;
5344
5345 vars_init(&s->vars_txn, SCOPE_TXN);
5346 vars_init(&s->vars_reqres, SCOPE_REQ);
5347}
5348
5349/* to be used at the end of a transaction */
5350void http_end_txn(struct stream *s)
5351{
5352 struct http_txn *txn = s->txn;
5353 struct proxy *fe = strm_fe(s);
5354
5355 /* these ones will have been dynamically allocated */
5356 pool_free(pool_head_requri, txn->uri);
5357 pool_free(pool_head_capture, txn->cli_cookie);
5358 pool_free(pool_head_capture, txn->srv_cookie);
5359 pool_free(pool_head_uniqueid, s->unique_id);
5360
5361 s->unique_id = NULL;
5362 txn->uri = NULL;
5363 txn->srv_cookie = NULL;
5364 txn->cli_cookie = NULL;
5365
5366 if (s->req_cap) {
5367 struct cap_hdr *h;
5368 for (h = fe->req_cap; h; h = h->next)
5369 pool_free(h->pool, s->req_cap[h->index]);
5370 memset(s->req_cap, 0, fe->nb_req_cap * sizeof(void *));
5371 }
5372
5373 if (s->res_cap) {
5374 struct cap_hdr *h;
5375 for (h = fe->rsp_cap; h; h = h->next)
5376 pool_free(h->pool, s->res_cap[h->index]);
5377 memset(s->res_cap, 0, fe->nb_rsp_cap * sizeof(void *));
5378 }
5379
5380 if (!LIST_ISEMPTY(&s->vars_txn.head))
5381 vars_prune(&s->vars_txn, s->sess, s);
5382 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5383 vars_prune(&s->vars_reqres, s->sess, s);
5384}
5385
5386/* to be used at the end of a transaction to prepare a new one */
5387void http_reset_txn(struct stream *s)
5388{
5389 http_end_txn(s);
5390 http_init_txn(s);
5391
5392 /* reinitialise the current rule list pointer to NULL. We are sure that
5393 * any rulelist match the NULL pointer.
5394 */
5395 s->current_rule_list = NULL;
5396
5397 s->be = strm_fe(s);
5398 s->logs.logwait = strm_fe(s)->to_log;
5399 s->logs.level = 0;
5400 stream_del_srv_conn(s);
5401 s->target = NULL;
5402 /* re-init store persistence */
5403 s->store_count = 0;
5404 s->uniq_id = _HA_ATOMIC_XADD(&global.req_count, 1);
5405
5406 s->req.flags |= CF_READ_DONTWAIT; /* one read is usually enough */
5407
5408 /* We must trim any excess data from the response buffer, because we
5409 * may have blocked an invalid response from a server that we don't
5410 * want to accidently forward once we disable the analysers, nor do
5411 * we want those data to come along with next response. A typical
5412 * example of such data would be from a buggy server responding to
5413 * a HEAD with some data, or sending more than the advertised
5414 * content-length.
5415 */
5416 if (unlikely(ci_data(&s->res)))
5417 b_set_data(&s->res.buf, co_data(&s->res));
5418
5419 /* Now we can realign the response buffer */
5420 c_realign_if_empty(&s->res);
5421
5422 s->req.rto = strm_fe(s)->timeout.client;
5423 s->req.wto = TICK_ETERNITY;
5424
5425 s->res.rto = TICK_ETERNITY;
5426 s->res.wto = strm_fe(s)->timeout.client;
5427
5428 s->req.rex = TICK_ETERNITY;
5429 s->req.wex = TICK_ETERNITY;
5430 s->req.analyse_exp = TICK_ETERNITY;
5431 s->res.rex = TICK_ETERNITY;
5432 s->res.wex = TICK_ETERNITY;
5433 s->res.analyse_exp = TICK_ETERNITY;
5434 s->si[1].hcto = TICK_ETERNITY;
5435}
5436
5437
5438DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
5439DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
Christopher Faulet0f226952018-10-22 09:29:56 +02005440
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005441__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005442static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005443{
5444}
5445
5446
5447/*
5448 * Local variables:
5449 * c-indent-level: 8
5450 * c-basic-offset: 8
5451 * End:
5452 */