blob: a65297dc0818126b8d76ce7a38e279c3300e874e [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
19#include <types/cache.h>
Christopher Faulet0f226952018-10-22 09:29:56 +020020#include <types/capture.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020021
22#include <proto/acl.h>
Christopher Faulet3e964192018-10-24 11:39:23 +020023#include <proto/action.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020024#include <proto/channel.h>
25#include <proto/checks.h>
26#include <proto/connection.h>
27#include <proto/filters.h>
28#include <proto/hdr_idx.h>
Christopher Faulet0f226952018-10-22 09:29:56 +020029#include <proto/http_htx.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020030#include <proto/log.h>
Christopher Faulet3e964192018-10-24 11:39:23 +020031#include <proto/pattern.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020032#include <proto/proto_http.h>
33#include <proto/proxy.h>
Christopher Fauletfefc73d2018-10-24 21:18:04 +020034#include <proto/server.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020035#include <proto/stream.h>
36#include <proto/stream_interface.h>
37#include <proto/stats.h>
38
Christopher Faulet377c5a52018-10-24 21:21:30 +020039extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020040
41static void htx_end_request(struct stream *s);
42static void htx_end_response(struct stream *s);
43
Christopher Faulet0f226952018-10-22 09:29:56 +020044static void htx_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
Christopher Faulet0b6bdc52018-10-24 11:05:36 +020045static int htx_del_hdr_value(char *start, char *end, char **from, char *next);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010046static size_t htx_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
47static size_t htx_fmt_res_line(const struct htx_sl *sl, char *str, size_t len);
48static void htx_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
Christopher Faulet0f226952018-10-22 09:29:56 +020049static void htx_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
50
Christopher Faulet3e964192018-10-24 11:39:23 +020051static enum rule_result htx_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status);
52static enum rule_result htx_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
53
Christopher Faulet33640082018-10-24 11:53:01 +020054static int htx_apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px);
55static int htx_apply_filters_to_response(struct stream *s, struct channel *res, struct proxy *px);
56
Christopher Fauletfcda7c62018-10-24 11:56:22 +020057static void htx_manage_client_side_cookies(struct stream *s, struct channel *req);
58static void htx_manage_server_side_cookies(struct stream *s, struct channel *res);
59
Christopher Faulet377c5a52018-10-24 21:21:30 +020060static int htx_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
61static int htx_handle_stats(struct stream *s, struct channel *req);
62
Christopher Faulet23a3c792018-11-28 10:01:23 +010063static int htx_reply_100_continue(struct stream *s);
Christopher Faulet12c51e22018-11-28 15:59:42 +010064static int htx_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 */
73int htx_wait_for_request(struct stream *s, struct channel *req, int an_bit)
74{
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 */
109 s->srv_error = http_return_srv_error;
110
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 Faulet9768c262018-10-22 09:34:31 +0200135 if (unlikely(htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100136 /*
Willy Tarreau4236f032019-03-05 10:43:32 +0100137 * First catch invalid request because only part of headers have
138 * been transfered. Multiplexers have the responsibility to emit
139 * all headers at once.
Christopher Faulet47365272018-10-31 17:40:50 +0100140 */
Willy Tarreau4236f032019-03-05 10:43:32 +0100141 if (htx_is_not_empty(htx) || (s->si[0].flags & SI_FL_RXBLK_ROOM)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100142 stream_inc_http_req_ctr(s);
143 stream_inc_http_err_ctr(s);
144 proxy_inc_fe_req_ctr(sess->fe);
145 goto return_bad_req;
146 }
147
Christopher Faulet9768c262018-10-22 09:34:31 +0200148 /* 1: have we encountered a read error ? */
149 if (req->flags & CF_READ_ERROR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200150 if (!(s->flags & SF_ERR_MASK))
151 s->flags |= SF_ERR_CLICL;
152
153 if (txn->flags & TX_WAIT_NEXT_RQ)
154 goto failed_keep_alive;
155
156 if (sess->fe->options & PR_O_IGNORE_PRB)
157 goto failed_keep_alive;
158
Christopher Faulet9768c262018-10-22 09:34:31 +0200159 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200160 stream_inc_http_req_ctr(s);
161 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100162 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200163 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100164 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200165
Christopher Faulet9768c262018-10-22 09:34:31 +0200166 txn->status = 400;
167 msg->err_state = msg->msg_state;
168 msg->msg_state = HTTP_MSG_ERROR;
169 htx_reply_and_close(s, txn->status, NULL);
170 req->analysers &= AN_REQ_FLT_END;
171
Christopher Faulete0768eb2018-10-03 16:38:02 +0200172 if (!(s->flags & SF_FINST_MASK))
173 s->flags |= SF_FINST_R;
174 return 0;
175 }
176
Christopher Faulet9768c262018-10-22 09:34:31 +0200177 /* 2: has the read timeout expired ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200178 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
179 if (!(s->flags & SF_ERR_MASK))
180 s->flags |= SF_ERR_CLITO;
181
182 if (txn->flags & TX_WAIT_NEXT_RQ)
183 goto failed_keep_alive;
184
185 if (sess->fe->options & PR_O_IGNORE_PRB)
186 goto failed_keep_alive;
187
Christopher Faulet9768c262018-10-22 09:34:31 +0200188 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200189 stream_inc_http_req_ctr(s);
190 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100191 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200192 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100193 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200194
Christopher Faulet9768c262018-10-22 09:34:31 +0200195 txn->status = 408;
196 msg->err_state = msg->msg_state;
197 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100198 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200199 req->analysers &= AN_REQ_FLT_END;
200
Christopher Faulete0768eb2018-10-03 16:38:02 +0200201 if (!(s->flags & SF_FINST_MASK))
202 s->flags |= SF_FINST_R;
203 return 0;
204 }
205
Christopher Faulet9768c262018-10-22 09:34:31 +0200206 /* 3: have we encountered a close ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200207 else if (req->flags & CF_SHUTR) {
208 if (!(s->flags & SF_ERR_MASK))
209 s->flags |= SF_ERR_CLICL;
210
211 if (txn->flags & TX_WAIT_NEXT_RQ)
212 goto failed_keep_alive;
213
214 if (sess->fe->options & PR_O_IGNORE_PRB)
215 goto failed_keep_alive;
216
Christopher Faulete0768eb2018-10-03 16:38:02 +0200217 stream_inc_http_err_ctr(s);
218 stream_inc_http_req_ctr(s);
219 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100220 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200221 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100222 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200223
Christopher Faulet9768c262018-10-22 09:34:31 +0200224 txn->status = 400;
225 msg->err_state = msg->msg_state;
226 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100227 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200228 req->analysers &= AN_REQ_FLT_END;
229
Christopher Faulete0768eb2018-10-03 16:38:02 +0200230 if (!(s->flags & SF_FINST_MASK))
231 s->flags |= SF_FINST_R;
232 return 0;
233 }
234
235 channel_dont_connect(req);
236 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
237 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100238
Christopher Faulet9768c262018-10-22 09:34:31 +0200239 if (sess->listener->options & LI_O_NOQUICKACK && htx_is_not_empty(htx) &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200240 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
241 /* We need more data, we have to re-enable quick-ack in case we
242 * previously disabled it, otherwise we might cause the client
243 * to delay next data.
244 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100245 conn_set_quickack(objt_conn(sess->origin), 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200246 }
Willy Tarreau1a18b542018-12-11 16:37:42 +0100247
Christopher Faulet47365272018-10-31 17:40:50 +0100248 if ((req->flags & CF_READ_PARTIAL) && (txn->flags & TX_WAIT_NEXT_RQ)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200249 /* If the client starts to talk, let's fall back to
250 * request timeout processing.
251 */
252 txn->flags &= ~TX_WAIT_NEXT_RQ;
253 req->analyse_exp = TICK_ETERNITY;
254 }
255
256 /* just set the request timeout once at the beginning of the request */
257 if (!tick_isset(req->analyse_exp)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100258 if ((txn->flags & TX_WAIT_NEXT_RQ) && tick_isset(s->be->timeout.httpka))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200259 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
260 else
261 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
262 }
263
264 /* we're not ready yet */
265 return 0;
266
267 failed_keep_alive:
268 /* Here we process low-level errors for keep-alive requests. In
269 * short, if the request is not the first one and it experiences
270 * a timeout, read error or shutdown, we just silently close so
271 * that the client can try again.
272 */
273 txn->status = 0;
274 msg->msg_state = HTTP_MSG_RQBEFORE;
275 req->analysers &= AN_REQ_FLT_END;
276 s->logs.logwait = 0;
277 s->logs.level = 0;
278 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Faulet9768c262018-10-22 09:34:31 +0200279 htx_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200280 return 0;
281 }
282
Christopher Faulet9768c262018-10-22 09:34:31 +0200283 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200284 stream_inc_http_req_ctr(s);
285 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
286
Christopher Faulet9768c262018-10-22 09:34:31 +0200287 /* kill the pending keep-alive timeout */
288 txn->flags &= ~TX_WAIT_NEXT_RQ;
289 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200290
Christopher Faulet03599112018-11-27 11:21:21 +0100291 sl = http_find_stline(htx);
292
Christopher Faulet9768c262018-10-22 09:34:31 +0200293 /* 0: we might have to print this header in debug mode */
294 if (unlikely((global.mode & MODE_DEBUG) &&
295 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
296 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200297
Christopher Faulet03599112018-11-27 11:21:21 +0100298 htx_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200299
300 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
301 struct htx_blk *blk = htx_get_blk(htx, pos);
302 enum htx_blk_type type = htx_get_blk_type(blk);
303
304 if (type == HTX_BLK_EOH)
305 break;
306 if (type != HTX_BLK_HDR)
307 continue;
308
309 htx_debug_hdr("clihdr", s,
310 htx_get_blk_name(htx, blk),
311 htx_get_blk_value(htx, blk));
312 }
313 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200314
315 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100316 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200317 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100318 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100319 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200320 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100321 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +0100322 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100323 if (sl->flags & HTX_SL_F_BODYLESS)
324 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200325
326 /* we can make use of server redirect on GET and HEAD */
327 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
328 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100329 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200330 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200331 goto return_bad_req;
332 }
333
334 /*
335 * 2: check if the URI matches the monitor_uri.
336 * We have to do this for every request which gets in, because
337 * the monitor-uri is defined by the frontend.
338 */
339 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100340 isteqi(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200341 /*
342 * We have found the monitor URI
343 */
344 struct acl_cond *cond;
345
346 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100347 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200348
349 /* Check if we want to fail this monitor request or not */
350 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
351 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
352
353 ret = acl_pass(ret);
354 if (cond->pol == ACL_COND_UNLESS)
355 ret = !ret;
356
357 if (ret) {
358 /* we fail this request, let's return 503 service unavail */
359 txn->status = 503;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100360 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200361 if (!(s->flags & SF_ERR_MASK))
362 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
363 goto return_prx_cond;
364 }
365 }
366
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800367 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200368 txn->status = 200;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100369 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200370 if (!(s->flags & SF_ERR_MASK))
371 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
372 goto return_prx_cond;
373 }
374
375 /*
376 * 3: Maybe we have to copy the original REQURI for the logs ?
377 * Note: we cannot log anymore if the request has been
378 * classified as invalid.
379 */
380 if (unlikely(s->logs.logwait & LW_REQ)) {
381 /* we have a complete HTTP request that we must log */
382 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200383 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200384
Christopher Faulet9768c262018-10-22 09:34:31 +0200385 len = htx_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
386 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200387
388 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
389 s->do_log(s);
390 } else {
391 ha_alert("HTTP logging : out of memory.\n");
392 }
393 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200394
Christopher Faulete0768eb2018-10-03 16:38:02 +0200395 /* if the frontend has "option http-use-proxy-header", we'll check if
396 * we have what looks like a proxied connection instead of a connection,
397 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
398 * Note that this is *not* RFC-compliant, however browsers and proxies
399 * happen to do that despite being non-standard :-(
400 * We consider that a request not beginning with either '/' or '*' is
401 * a proxied connection, which covers both "scheme://location" and
402 * CONNECT ip:port.
403 */
404 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100405 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200406 txn->flags |= TX_USE_PX_CONN;
407
Christopher Faulete0768eb2018-10-03 16:38:02 +0200408 /* 5: we may need to capture headers */
409 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet9768c262018-10-22 09:34:31 +0200410 htx_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200411
412 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
413 * only change if both the request and the config reference something else.
414 * Option httpclose by itself sets tunnel mode where headers are mangled.
415 * However, if another mode is set, it will affect it (eg: server-close/
416 * keep-alive + httpclose = close). Note that we avoid to redo the same work
417 * if FE and BE have the same settings (common). The method consists in
418 * checking if options changed between the two calls (implying that either
419 * one is non-null, or one of them is non-null and we are there for the first
420 * time.
421 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200422 if ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))
Christopher Faulet0f226952018-10-22 09:29:56 +0200423 htx_adjust_conn_mode(s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200424
425 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200426 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200427 req->analysers |= AN_REQ_HTTP_BODY;
428
429 /*
430 * RFC7234#4:
431 * A cache MUST write through requests with methods
432 * that are unsafe (Section 4.2.1 of [RFC7231]) to
433 * the origin server; i.e., a cache is not allowed
434 * to generate a reply to such a request before
435 * having forwarded the request and having received
436 * a corresponding response.
437 *
438 * RFC7231#4.2.1:
439 * Of the request methods defined by this
440 * specification, the GET, HEAD, OPTIONS, and TRACE
441 * methods are defined to be safe.
442 */
443 if (likely(txn->meth == HTTP_METH_GET ||
444 txn->meth == HTTP_METH_HEAD ||
445 txn->meth == HTTP_METH_OPTIONS ||
446 txn->meth == HTTP_METH_TRACE))
447 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
448
449 /* end of job, return OK */
450 req->analysers &= ~an_bit;
451 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200452
Christopher Faulete0768eb2018-10-03 16:38:02 +0200453 return 1;
454
455 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200456 txn->status = 400;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200457 txn->req.err_state = txn->req.msg_state;
458 txn->req.msg_state = HTTP_MSG_ERROR;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100459 htx_reply_and_close(s, txn->status, htx_error_message(s));
Olivier Houcharda798bf52019-03-08 18:52:00 +0100460 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200461 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100462 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200463
464 return_prx_cond:
465 if (!(s->flags & SF_ERR_MASK))
466 s->flags |= SF_ERR_PRXCOND;
467 if (!(s->flags & SF_FINST_MASK))
468 s->flags |= SF_FINST_R;
469
470 req->analysers &= AN_REQ_FLT_END;
471 req->analyse_exp = TICK_ETERNITY;
472 return 0;
473}
474
475
476/* This stream analyser runs all HTTP request processing which is common to
477 * frontends and backends, which means blocking ACLs, filters, connection-close,
478 * reqadd, stats and redirects. This is performed for the designated proxy.
479 * It returns 1 if the processing can continue on next analysers, or zero if it
480 * either needs more data or wants to immediately abort the request (eg: deny,
481 * error, ...).
482 */
483int htx_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
484{
485 struct session *sess = s->sess;
486 struct http_txn *txn = s->txn;
487 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200488 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200489 struct redirect_rule *rule;
490 struct cond_wordlist *wl;
491 enum rule_result verdict;
492 int deny_status = HTTP_ERR_403;
493 struct connection *conn = objt_conn(sess->origin);
494
495 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
496 /* we need more data */
497 goto return_prx_yield;
498 }
499
500 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
501 now_ms, __FUNCTION__,
502 s,
503 req,
504 req->rex, req->wex,
505 req->flags,
506 ci_data(req),
507 req->analysers);
508
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100509 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200510
Christopher Faulete0768eb2018-10-03 16:38:02 +0200511 /* just in case we have some per-backend tracking */
512 stream_inc_be_http_req_ctr(s);
513
514 /* evaluate http-request rules */
515 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200516 verdict = htx_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200517
518 switch (verdict) {
519 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
520 goto return_prx_yield;
521
522 case HTTP_RULE_RES_CONT:
523 case HTTP_RULE_RES_STOP: /* nothing to do */
524 break;
525
526 case HTTP_RULE_RES_DENY: /* deny or tarpit */
527 if (txn->flags & TX_CLTARPIT)
528 goto tarpit;
529 goto deny;
530
531 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
532 goto return_prx_cond;
533
534 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
535 goto done;
536
537 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
538 goto return_bad_req;
539 }
540 }
541
542 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
543 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200544 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200545
Christopher Fauletff2759f2018-10-24 11:13:16 +0200546 ctx.blk = NULL;
547 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
548 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200549 goto return_bad_req;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200550 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200551 }
552
553 /* OK at this stage, we know that the request was accepted according to
554 * the http-request rules, we can check for the stats. Note that the
555 * URI is detected *before* the req* rules in order not to be affected
556 * by a possible reqrep, while they are processed *after* so that a
557 * reqdeny can still block them. This clearly needs to change in 1.6!
558 */
Christopher Fauletff2759f2018-10-24 11:13:16 +0200559 if (htx_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200560 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100561 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200562 txn->status = 500;
563 s->logs.tv_request = now;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100564 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200565
566 if (!(s->flags & SF_ERR_MASK))
567 s->flags |= SF_ERR_RESOURCE;
568 goto return_prx_cond;
569 }
570
571 /* parse the whole stats request and extract the relevant information */
Christopher Fauletff2759f2018-10-24 11:13:16 +0200572 htx_handle_stats(s, req);
573 verdict = htx_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200574 /* not all actions implemented: deny, allow, auth */
575
576 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
577 goto deny;
578
579 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
580 goto return_prx_cond;
581 }
582
583 /* evaluate the req* rules except reqadd */
584 if (px->req_exp != NULL) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200585 if (htx_apply_filters_to_request(s, req, px) < 0)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200586 goto return_bad_req;
587
588 if (txn->flags & TX_CLDENY)
589 goto deny;
590
591 if (txn->flags & TX_CLTARPIT) {
592 deny_status = HTTP_ERR_500;
593 goto tarpit;
594 }
595 }
596
597 /* add request headers from the rule sets in the same order */
598 list_for_each_entry(wl, &px->req_add, list) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200599 struct ist n,v;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200600 if (wl->cond) {
601 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
602 ret = acl_pass(ret);
603 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
604 ret = !ret;
605 if (!ret)
606 continue;
607 }
608
Christopher Fauletff2759f2018-10-24 11:13:16 +0200609 http_parse_header(ist2(wl->s, strlen(wl->s)), &n, &v);
610 if (unlikely(!http_add_header(htx, n, v)))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200611 goto return_bad_req;
612 }
613
Christopher Faulete0768eb2018-10-03 16:38:02 +0200614 /* Proceed with the stats now. */
615 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
616 unlikely(objt_applet(s->target) == &http_cache_applet)) {
617 /* process the stats request now */
618 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100619 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200620
621 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
622 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
623 if (!(s->flags & SF_FINST_MASK))
624 s->flags |= SF_FINST_R;
625
626 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
627 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
628 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
629 req->analysers |= AN_REQ_HTTP_XFER_BODY;
630 goto done;
631 }
632
633 /* check whether we have some ACLs set to redirect this request */
634 list_for_each_entry(rule, &px->redirect_rules, list) {
635 if (rule->cond) {
636 int ret;
637
638 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
639 ret = acl_pass(ret);
640 if (rule->cond->pol == ACL_COND_UNLESS)
641 ret = !ret;
642 if (!ret)
643 continue;
644 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200645 if (!htx_apply_redirect_rule(rule, s, txn))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200646 goto return_bad_req;
647 goto done;
648 }
649
650 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
651 * If this happens, then the data will not come immediately, so we must
652 * send all what we have without waiting. Note that due to the small gain
653 * in waiting for the body of the request, it's easier to simply put the
654 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
655 * itself once used.
656 */
657 req->flags |= CF_SEND_DONTWAIT;
658
659 done: /* done with this analyser, continue with next ones that the calling
660 * points will have set, if any.
661 */
662 req->analyse_exp = TICK_ETERNITY;
663 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
664 req->analysers &= ~an_bit;
665 return 1;
666
667 tarpit:
668 /* Allow cookie logging
669 */
670 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletff2759f2018-10-24 11:13:16 +0200671 htx_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200672
673 /* When a connection is tarpitted, we use the tarpit timeout,
674 * which may be the same as the connect timeout if unspecified.
675 * If unset, then set it to zero because we really want it to
676 * eventually expire. We build the tarpit as an analyser.
677 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100678 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200679
680 /* wipe the request out so that we can drop the connection early
681 * if the client closes first.
682 */
683 channel_dont_connect(req);
684
685 txn->status = http_err_codes[deny_status];
686
687 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
688 req->analysers |= AN_REQ_HTTP_TARPIT;
689 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
690 if (!req->analyse_exp)
691 req->analyse_exp = tick_add(now_ms, 0);
692 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100693 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200694 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100695 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200696 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100697 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200698 goto done_without_exp;
699
700 deny: /* this request was blocked (denied) */
701
702 /* Allow cookie logging
703 */
704 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletff2759f2018-10-24 11:13:16 +0200705 htx_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200706
707 txn->flags |= TX_CLDENY;
708 txn->status = http_err_codes[deny_status];
709 s->logs.tv_request = now;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100710 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200711 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100712 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200713 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100714 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200715 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100716 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200717 goto return_prx_cond;
718
719 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200720 txn->req.err_state = txn->req.msg_state;
721 txn->req.msg_state = HTTP_MSG_ERROR;
722 txn->status = 400;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100723 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200724
Olivier Houcharda798bf52019-03-08 18:52:00 +0100725 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200726 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100727 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200728
729 return_prx_cond:
730 if (!(s->flags & SF_ERR_MASK))
731 s->flags |= SF_ERR_PRXCOND;
732 if (!(s->flags & SF_FINST_MASK))
733 s->flags |= SF_FINST_R;
734
735 req->analysers &= AN_REQ_FLT_END;
736 req->analyse_exp = TICK_ETERNITY;
737 return 0;
738
739 return_prx_yield:
740 channel_dont_connect(req);
741 return 0;
742}
743
744/* This function performs all the processing enabled for the current request.
745 * It returns 1 if the processing can continue on next analysers, or zero if it
746 * needs more data, encounters an error, or wants to immediately abort the
747 * request. It relies on buffers flags, and updates s->req.analysers.
748 */
749int htx_process_request(struct stream *s, struct channel *req, int an_bit)
750{
751 struct session *sess = s->sess;
752 struct http_txn *txn = s->txn;
753 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200754 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200755 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
756
757 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
758 /* we need more data */
759 channel_dont_connect(req);
760 return 0;
761 }
762
763 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
764 now_ms, __FUNCTION__,
765 s,
766 req,
767 req->rex, req->wex,
768 req->flags,
769 ci_data(req),
770 req->analysers);
771
772 /*
773 * Right now, we know that we have processed the entire headers
774 * and that unwanted requests have been filtered out. We can do
775 * whatever we want with the remaining request. Also, now we
776 * may have separate values for ->fe, ->be.
777 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100778 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200779
780 /*
781 * If HTTP PROXY is set we simply get remote server address parsing
782 * incoming request. Note that this requires that a connection is
783 * allocated on the server side.
784 */
785 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
786 struct connection *conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100787 struct htx_sl *sl;
788 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200789
790 /* Note that for now we don't reuse existing proxy connections */
791 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
792 txn->req.err_state = txn->req.msg_state;
793 txn->req.msg_state = HTTP_MSG_ERROR;
794 txn->status = 500;
795 req->analysers &= AN_REQ_FLT_END;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100796 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200797
798 if (!(s->flags & SF_ERR_MASK))
799 s->flags |= SF_ERR_RESOURCE;
800 if (!(s->flags & SF_FINST_MASK))
801 s->flags |= SF_FINST_R;
802
803 return 0;
804 }
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200805 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100806 uri = htx_sl_req_uri(sl);
807 path = http_get_path(uri);
808 if (url2sa(uri.ptr, uri.len - path.len, &conn->addr.to, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200809 goto return_bad_req;
810
811 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200812 * uri.ptr and path.ptr (excluded). If it was not found, we need
813 * to replace from all the uri by a single "/".
814 *
815 * Instead of rewritting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100816 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200817 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200818 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100819 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200820 }
821
822 /*
823 * 7: Now we can work with the cookies.
824 * Note that doing so might move headers in the request, but
825 * the fields will stay coherent and the URI will not move.
826 * This should only be performed in the backend.
827 */
828 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletb6aadbd2018-12-18 16:41:31 +0100829 htx_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200830
831 /* add unique-id if "header-unique-id" is specified */
832
833 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
834 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
835 goto return_bad_req;
836 s->unique_id[0] = '\0';
837 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
838 }
839
840 if (sess->fe->header_unique_id && s->unique_id) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200841 struct ist n = ist2(sess->fe->header_unique_id, strlen(sess->fe->header_unique_id));
842 struct ist v = ist2(s->unique_id, strlen(s->unique_id));
843
844 if (unlikely(!http_add_header(htx, n, v)))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200845 goto return_bad_req;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200846 }
847
848 /*
849 * 9: add X-Forwarded-For if either the frontend or the backend
850 * asks for it.
851 */
852 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200853 struct http_hdr_ctx ctx = { .blk = NULL };
854 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
855 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
856
Christopher Faulete0768eb2018-10-03 16:38:02 +0200857 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200858 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200859 /* The header is set to be added only if none is present
860 * and we found it, so don't do anything.
861 */
862 }
863 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
864 /* Add an X-Forwarded-For header unless the source IP is
865 * in the 'except' network range.
866 */
867 if ((!sess->fe->except_mask.s_addr ||
868 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
869 != sess->fe->except_net.s_addr) &&
870 (!s->be->except_mask.s_addr ||
871 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
872 != s->be->except_net.s_addr)) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200873 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200874
875 /* Note: we rely on the backend to get the header name to be used for
876 * x-forwarded-for, because the header is really meant for the backends.
877 * However, if the backend did not specify any option, we have to rely
878 * on the frontend's header name.
879 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200880 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
881 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200882 goto return_bad_req;
883 }
884 }
885 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
886 /* FIXME: for the sake of completeness, we should also support
887 * 'except' here, although it is mostly useless in this case.
888 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200889 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200890
Christopher Faulete0768eb2018-10-03 16:38:02 +0200891 inet_ntop(AF_INET6,
892 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
893 pn, sizeof(pn));
894
895 /* Note: we rely on the backend to get the header name to be used for
896 * x-forwarded-for, because the header is really meant for the backends.
897 * However, if the backend did not specify any option, we have to rely
898 * on the frontend's header name.
899 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200900 chunk_printf(&trash, "%s", pn);
901 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200902 goto return_bad_req;
903 }
904 }
905
906 /*
907 * 10: add X-Original-To if either the frontend or the backend
908 * asks for it.
909 */
910 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
911
912 /* FIXME: don't know if IPv6 can handle that case too. */
913 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
914 /* Add an X-Original-To header unless the destination IP is
915 * in the 'except' network range.
916 */
917 conn_get_to_addr(cli_conn);
918
919 if (cli_conn->addr.to.ss_family == AF_INET &&
920 ((!sess->fe->except_mask_to.s_addr ||
921 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
922 != sess->fe->except_to.s_addr) &&
923 (!s->be->except_mask_to.s_addr ||
924 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
925 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200926 struct ist hdr;
927 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200928
929 /* Note: we rely on the backend to get the header name to be used for
930 * x-original-to, because the header is really meant for the backends.
931 * However, if the backend did not specify any option, we have to rely
932 * on the frontend's header name.
933 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200934 if (s->be->orgto_hdr_len)
935 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
936 else
937 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200938
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200939 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
940 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200941 goto return_bad_req;
942 }
943 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200944 }
945
Christopher Faulete0768eb2018-10-03 16:38:02 +0200946 /* If we have no server assigned yet and we're balancing on url_param
947 * with a POST request, we may be interested in checking the body for
948 * that parameter. This will be done in another analyser.
949 */
950 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100951 s->txn->meth == HTTP_METH_POST &&
952 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200953 channel_dont_connect(req);
954 req->analysers |= AN_REQ_HTTP_BODY;
955 }
956
957 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
958 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100959
Christopher Faulete0768eb2018-10-03 16:38:02 +0200960 /* We expect some data from the client. Unless we know for sure
961 * we already have a full request, we have to re-enable quick-ack
962 * in case we previously disabled it, otherwise we might cause
963 * the client to delay further data.
964 */
965 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200966 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100967 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200968
969 /*************************************************************
970 * OK, that's finished for the headers. We have done what we *
971 * could. Let's switch to the DATA state. *
972 ************************************************************/
973 req->analyse_exp = TICK_ETERNITY;
974 req->analysers &= ~an_bit;
975
976 s->logs.tv_request = now;
977 /* OK let's go on with the BODY now */
978 return 1;
979
980 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200981 txn->req.err_state = txn->req.msg_state;
982 txn->req.msg_state = HTTP_MSG_ERROR;
983 txn->status = 400;
984 req->analysers &= AN_REQ_FLT_END;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100985 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200986
Olivier Houcharda798bf52019-03-08 18:52:00 +0100987 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200988 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100989 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200990
991 if (!(s->flags & SF_ERR_MASK))
992 s->flags |= SF_ERR_PRXCOND;
993 if (!(s->flags & SF_FINST_MASK))
994 s->flags |= SF_FINST_R;
995 return 0;
996}
997
998/* This function is an analyser which processes the HTTP tarpit. It always
999 * returns zero, at the beginning because it prevents any other processing
1000 * from occurring, and at the end because it terminates the request.
1001 */
1002int htx_process_tarpit(struct stream *s, struct channel *req, int an_bit)
1003{
1004 struct http_txn *txn = s->txn;
1005
1006 /* This connection is being tarpitted. The CLIENT side has
1007 * already set the connect expiration date to the right
1008 * timeout. We just have to check that the client is still
1009 * there and that the timeout has not expired.
1010 */
1011 channel_dont_connect(req);
1012 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
1013 !tick_is_expired(req->analyse_exp, now_ms))
1014 return 0;
1015
1016 /* We will set the queue timer to the time spent, just for
1017 * logging purposes. We fake a 500 server error, so that the
1018 * attacker will not suspect his connection has been tarpitted.
1019 * It will not cause trouble to the logs because we can exclude
1020 * the tarpitted connections by filtering on the 'PT' status flags.
1021 */
1022 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1023
1024 if (!(req->flags & CF_READ_ERROR))
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001025 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001026
1027 req->analysers &= AN_REQ_FLT_END;
1028 req->analyse_exp = TICK_ETERNITY;
1029
1030 if (!(s->flags & SF_ERR_MASK))
1031 s->flags |= SF_ERR_PRXCOND;
1032 if (!(s->flags & SF_FINST_MASK))
1033 s->flags |= SF_FINST_T;
1034 return 0;
1035}
1036
1037/* This function is an analyser which waits for the HTTP request body. It waits
1038 * for either the buffer to be full, or the full advertised contents to have
1039 * reached the buffer. It must only be called after the standard HTTP request
1040 * processing has occurred, because it expects the request to be parsed and will
1041 * look for the Expect header. It may send a 100-Continue interim response. It
1042 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
1043 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
1044 * needs to read more data, or 1 once it has completed its analysis.
1045 */
1046int htx_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
1047{
1048 struct session *sess = s->sess;
1049 struct http_txn *txn = s->txn;
1050 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001051 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001052
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001053
1054 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1055 now_ms, __FUNCTION__,
1056 s,
1057 req,
1058 req->rex, req->wex,
1059 req->flags,
1060 ci_data(req),
1061 req->analysers);
1062
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001063 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001064
Willy Tarreau4236f032019-03-05 10:43:32 +01001065 if (htx->flags & HTX_FL_PARSING_ERROR)
1066 goto return_bad_req;
1067
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001068 if (msg->msg_state < HTTP_MSG_BODY)
1069 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001070
Christopher Faulete0768eb2018-10-03 16:38:02 +02001071 /* We have to parse the HTTP request body to find any required data.
1072 * "balance url_param check_post" should have been the only way to get
1073 * into this. We were brought here after HTTP header analysis, so all
1074 * related structures are ready.
1075 */
1076
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001077 if (msg->msg_state < HTTP_MSG_DATA) {
1078 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
1079 * send an HTTP/1.1 100 Continue intermediate response.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001080 */
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001081 if (msg->flags & HTTP_MSGF_VER_11) {
1082 struct ist hdr = { .ptr = "Expect", .len = 6 };
1083 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001084
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001085 ctx.blk = NULL;
1086 /* Expect is allowed in 1.1, look for it */
1087 if (http_find_header(htx, hdr, &ctx, 0) &&
1088 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Faulet23a3c792018-11-28 10:01:23 +01001089 if (htx_reply_100_continue(s) == -1)
1090 goto return_bad_req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001091 http_remove_header(htx, &ctx);
1092 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001093 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001094 }
1095
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001096 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001097
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001098 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1099 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001100 */
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001101 if (htx_get_tail_type(htx) >= HTX_BLK_EOD ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001102 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001103 goto http_end;
1104
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001105 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001106 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1107 txn->status = 408;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001108 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001109
1110 if (!(s->flags & SF_ERR_MASK))
1111 s->flags |= SF_ERR_CLITO;
1112 if (!(s->flags & SF_FINST_MASK))
1113 s->flags |= SF_FINST_D;
1114 goto return_err_msg;
1115 }
1116
1117 /* we get here if we need to wait for more data */
1118 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1119 /* Not enough data. We'll re-use the http-request
1120 * timeout here. Ideally, we should set the timeout
1121 * relative to the accept() date. We just set the
1122 * request timeout once at the beginning of the
1123 * request.
1124 */
1125 channel_dont_connect(req);
1126 if (!tick_isset(req->analyse_exp))
1127 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1128 return 0;
1129 }
1130
1131 http_end:
1132 /* The situation will not evolve, so let's give up on the analysis. */
1133 s->logs.tv_request = now; /* update the request timer to reflect full request */
1134 req->analysers &= ~an_bit;
1135 req->analyse_exp = TICK_ETERNITY;
1136 return 1;
1137
1138 return_bad_req: /* let's centralize all bad requests */
1139 txn->req.err_state = txn->req.msg_state;
1140 txn->req.msg_state = HTTP_MSG_ERROR;
1141 txn->status = 400;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001142 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001143
1144 if (!(s->flags & SF_ERR_MASK))
1145 s->flags |= SF_ERR_PRXCOND;
1146 if (!(s->flags & SF_FINST_MASK))
1147 s->flags |= SF_FINST_R;
1148
1149 return_err_msg:
1150 req->analysers &= AN_REQ_FLT_END;
Olivier Houcharda798bf52019-03-08 18:52:00 +01001151 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001152 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001153 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001154 return 0;
1155}
1156
1157/* This function is an analyser which forwards request body (including chunk
1158 * sizes if any). It is called as soon as we must forward, even if we forward
1159 * zero byte. The only situation where it must not be called is when we're in
1160 * tunnel mode and we want to forward till the close. It's used both to forward
1161 * remaining data and to resync after end of body. It expects the msg_state to
1162 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1163 * read more data, or 1 once we can go on with next request or end the stream.
1164 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1165 * bytes of pending data + the headers if not already done.
1166 */
1167int htx_request_forward_body(struct stream *s, struct channel *req, int an_bit)
1168{
1169 struct session *sess = s->sess;
1170 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001171 struct http_msg *msg = &txn->req;
1172 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001173 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001174
1175 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1176 now_ms, __FUNCTION__,
1177 s,
1178 req,
1179 req->rex, req->wex,
1180 req->flags,
1181 ci_data(req),
1182 req->analysers);
1183
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001184 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001185
1186 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1187 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1188 /* Output closed while we were sending data. We must abort and
1189 * wake the other side up.
1190 */
1191 msg->err_state = msg->msg_state;
1192 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001193 htx_end_request(s);
1194 htx_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001195 return 1;
1196 }
1197
1198 /* Note that we don't have to send 100-continue back because we don't
1199 * need the data to complete our job, and it's up to the server to
1200 * decide whether to return 100, 417 or anything else in return of
1201 * an "Expect: 100-continue" header.
1202 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001203 if (msg->msg_state == HTTP_MSG_BODY)
1204 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001205
1206 /* Some post-connect processing might want us to refrain from starting to
1207 * forward data. Currently, the only reason for this is "balance url_param"
1208 * whichs need to parse/process the request after we've enabled forwarding.
1209 */
1210 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
1211 if (!(s->res.flags & CF_READ_ATTACHED)) {
1212 channel_auto_connect(req);
1213 req->flags |= CF_WAKE_CONNECT;
1214 channel_dont_close(req); /* don't fail on early shutr */
1215 goto waiting;
1216 }
1217 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
1218 }
1219
1220 /* in most states, we should abort in case of early close */
1221 channel_auto_close(req);
1222
1223 if (req->to_forward) {
1224 /* We can't process the buffer's contents yet */
1225 req->flags |= CF_WAKE_WRITE;
1226 goto missing_data_or_waiting;
1227 }
1228
Christopher Faulet9768c262018-10-22 09:34:31 +02001229 if (msg->msg_state >= HTTP_MSG_DONE)
1230 goto done;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001231 /* Forward input data. We get it by removing all outgoing data not
1232 * forwarded yet from HTX data size. If there are some data filters, we
1233 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001234 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001235 if (HAS_REQ_DATA_FILTERS(s)) {
1236 ret = flt_http_payload(s, msg, htx->data);
1237 if (ret < 0)
1238 goto return_bad_req;
1239 c_adv(req, ret);
1240 if (htx->data != co_data(req) || htx->extra)
1241 goto missing_data_or_waiting;
1242 }
1243 else {
1244 c_adv(req, htx->data - co_data(req));
Christopher Faulet9768c262018-10-22 09:34:31 +02001245
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001246 /* To let the function channel_forward work as expected we must update
1247 * the channel's buffer to pretend there is no more input data. The
1248 * right length is then restored. We must do that, because when an HTX
1249 * message is stored into a buffer, it appears as full.
1250 */
Christopher Fauletb2aedea2018-12-05 11:56:15 +01001251 if ((msg->flags & HTTP_MSGF_XFER_LEN) && htx->extra)
1252 htx->extra -= channel_htx_forward(req, htx, htx->extra);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001253 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001254
Christopher Faulet9768c262018-10-22 09:34:31 +02001255 /* Check if the end-of-message is reached and if so, switch the message
1256 * in HTTP_MSG_DONE state.
1257 */
1258 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1259 goto missing_data_or_waiting;
1260
1261 msg->msg_state = HTTP_MSG_DONE;
1262
1263 done:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001264 /* other states, DONE...TUNNEL */
1265 /* we don't want to forward closes on DONE except in tunnel mode. */
1266 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
1267 channel_dont_close(req);
1268
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001269 if (HAS_REQ_DATA_FILTERS(s)) {
1270 ret = flt_http_end(s, msg);
1271 if (ret <= 0) {
1272 if (!ret)
1273 goto missing_data_or_waiting;
1274 goto return_bad_req;
1275 }
1276 }
1277
Christopher Fauletf2824e62018-10-01 12:12:37 +02001278 htx_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001279 if (!(req->analysers & an_bit)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001280 htx_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001281 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1282 if (req->flags & CF_SHUTW) {
1283 /* request errors are most likely due to the
1284 * server aborting the transfer. */
1285 goto aborted_xfer;
1286 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001287 goto return_bad_req;
1288 }
1289 return 1;
1290 }
1291
1292 /* If "option abortonclose" is set on the backend, we want to monitor
1293 * the client's connection and forward any shutdown notification to the
1294 * server, which will decide whether to close or to go on processing the
1295 * request. We only do that in tunnel mode, and not in other modes since
1296 * it can be abused to exhaust source ports. */
1297 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
1298 channel_auto_read(req);
1299 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
1300 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
1301 s->si[1].flags |= SI_FL_NOLINGER;
1302 channel_auto_close(req);
1303 }
1304 else if (s->txn->meth == HTTP_METH_POST) {
1305 /* POST requests may require to read extra CRLF sent by broken
1306 * browsers and which could cause an RST to be sent upon close
1307 * on some systems (eg: Linux). */
1308 channel_auto_read(req);
1309 }
1310 return 0;
1311
1312 missing_data_or_waiting:
1313 /* stop waiting for data if the input is closed before the end */
Christopher Faulet9768c262018-10-22 09:34:31 +02001314 if (msg->msg_state < HTTP_MSG_DONE && req->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001315 if (!(s->flags & SF_ERR_MASK))
1316 s->flags |= SF_ERR_CLICL;
1317 if (!(s->flags & SF_FINST_MASK)) {
1318 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
1319 s->flags |= SF_FINST_H;
1320 else
1321 s->flags |= SF_FINST_D;
1322 }
1323
Olivier Houcharda798bf52019-03-08 18:52:00 +01001324 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1325 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001326 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001327 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001328
1329 goto return_bad_req_stats_ok;
1330 }
1331
1332 waiting:
1333 /* waiting for the last bits to leave the buffer */
1334 if (req->flags & CF_SHUTW)
1335 goto aborted_xfer;
1336
Christopher Faulet47365272018-10-31 17:40:50 +01001337 if (htx->flags & HTX_FL_PARSING_ERROR)
1338 goto return_bad_req;
Christopher Faulet9768c262018-10-22 09:34:31 +02001339
Christopher Faulete0768eb2018-10-03 16:38:02 +02001340 /* When TE: chunked is used, we need to get there again to parse remaining
1341 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1342 * And when content-length is used, we never want to let the possible
1343 * shutdown be forwarded to the other side, as the state machine will
1344 * take care of it once the client responds. It's also important to
1345 * prevent TIME_WAITs from accumulating on the backend side, and for
1346 * HTTP/2 where the last frame comes with a shutdown.
1347 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001348 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001349 channel_dont_close(req);
1350
1351 /* We know that more data are expected, but we couldn't send more that
1352 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1353 * system knows it must not set a PUSH on this first part. Interactive
1354 * modes are already handled by the stream sock layer. We must not do
1355 * this in content-length mode because it could present the MSG_MORE
1356 * flag with the last block of forwarded data, which would cause an
1357 * additional delay to be observed by the receiver.
1358 */
1359 if (msg->flags & HTTP_MSGF_TE_CHNK)
1360 req->flags |= CF_EXPECT_MORE;
1361
1362 return 0;
1363
1364 return_bad_req: /* let's centralize all bad requests */
Olivier Houcharda798bf52019-03-08 18:52:00 +01001365 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001366 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001367 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001368
1369 return_bad_req_stats_ok:
1370 txn->req.err_state = txn->req.msg_state;
1371 txn->req.msg_state = HTTP_MSG_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001372 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001373 /* Note: we don't send any error if some data were already sent */
Christopher Faulet9768c262018-10-22 09:34:31 +02001374 htx_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001375 } else {
1376 txn->status = 400;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001377 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001378 }
1379 req->analysers &= AN_REQ_FLT_END;
1380 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
1381
1382 if (!(s->flags & SF_ERR_MASK))
1383 s->flags |= SF_ERR_PRXCOND;
1384 if (!(s->flags & SF_FINST_MASK)) {
1385 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
1386 s->flags |= SF_FINST_H;
1387 else
1388 s->flags |= SF_FINST_D;
1389 }
1390 return 0;
1391
1392 aborted_xfer:
1393 txn->req.err_state = txn->req.msg_state;
1394 txn->req.msg_state = HTTP_MSG_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001395 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001396 /* Note: we don't send any error if some data were already sent */
Christopher Faulet9768c262018-10-22 09:34:31 +02001397 htx_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001398 } else {
1399 txn->status = 502;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001400 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001401 }
1402 req->analysers &= AN_REQ_FLT_END;
1403 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
1404
Olivier Houcharda798bf52019-03-08 18:52:00 +01001405 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1406 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001407 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001408 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001409
1410 if (!(s->flags & SF_ERR_MASK))
1411 s->flags |= SF_ERR_SRVCL;
1412 if (!(s->flags & SF_FINST_MASK)) {
1413 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
1414 s->flags |= SF_FINST_H;
1415 else
1416 s->flags |= SF_FINST_D;
1417 }
1418 return 0;
1419}
1420
1421/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1422 * processing can continue on next analysers, or zero if it either needs more
1423 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1424 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1425 * when it has nothing left to do, and may remove any analyser when it wants to
1426 * abort.
1427 */
1428int htx_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
1429{
Christopher Faulet9768c262018-10-22 09:34:31 +02001430 /*
1431 * We will analyze a complete HTTP response to check the its syntax.
1432 *
1433 * Once the start line and all headers are received, we may perform a
1434 * capture of the error (if any), and we will set a few fields. We also
1435 * logging and finally headers capture.
1436 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001437 struct session *sess = s->sess;
1438 struct http_txn *txn = s->txn;
1439 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001440 struct htx *htx;
Christopher Faulet61608322018-11-23 16:23:45 +01001441 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001442 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001443 int n;
1444
1445 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1446 now_ms, __FUNCTION__,
1447 s,
1448 rep,
1449 rep->rex, rep->wex,
1450 rep->flags,
1451 ci_data(rep),
1452 rep->analysers);
1453
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001454 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001455
Willy Tarreau4236f032019-03-05 10:43:32 +01001456 /* Parsing errors are caught here */
1457 if (htx->flags & HTX_FL_PARSING_ERROR)
1458 goto return_bad_res;
1459
Christopher Faulete0768eb2018-10-03 16:38:02 +02001460 /*
1461 * Now we quickly check if we have found a full valid response.
1462 * If not so, we check the FD and buffer states before leaving.
1463 * A full response is indicated by the fact that we have seen
1464 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1465 * responses are checked first.
1466 *
1467 * Depending on whether the client is still there or not, we
1468 * may send an error response back or not. Note that normally
1469 * we should only check for HTTP status there, and check I/O
1470 * errors somewhere else.
1471 */
Christopher Faulet72b62732018-11-28 16:44:44 +01001472 if (unlikely(co_data(rep) || htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH)) {
Christopher Faulet47365272018-10-31 17:40:50 +01001473 /*
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001474 * First catch invalid response because of a parsing error or
1475 * because only part of headers have been transfered.
1476 * Multiplexers have the responsibility to emit all headers at
1477 * once. We must be sure to have forwarded all outgoing data
1478 * first.
Christopher Faulet47365272018-10-31 17:40:50 +01001479 */
Willy Tarreau4236f032019-03-05 10:43:32 +01001480 if (!co_data(rep) && (htx_is_not_empty(htx) || (s->si[1].flags & SI_FL_RXBLK_ROOM)))
Christopher Faulet47365272018-10-31 17:40:50 +01001481 goto return_bad_res;
1482
Christopher Faulet9768c262018-10-22 09:34:31 +02001483 /* 1: have we encountered a read error ? */
1484 if (rep->flags & CF_READ_ERROR) {
1485 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001486 goto abort_keep_alive;
1487
Olivier Houcharda798bf52019-03-08 18:52:00 +01001488 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001489 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001490 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001491 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001492 }
1493
Christopher Faulete0768eb2018-10-03 16:38:02 +02001494 rep->analysers &= AN_RES_FLT_END;
1495 txn->status = 502;
1496
1497 /* Check to see if the server refused the early data.
1498 * If so, just send a 425
1499 */
1500 if (objt_cs(s->si[1].end)) {
1501 struct connection *conn = objt_cs(s->si[1].end)->conn;
1502
1503 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
1504 txn->status = 425;
1505 }
1506
1507 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001508 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001509
1510 if (!(s->flags & SF_ERR_MASK))
1511 s->flags |= SF_ERR_SRVCL;
1512 if (!(s->flags & SF_FINST_MASK))
1513 s->flags |= SF_FINST_H;
1514 return 0;
1515 }
1516
Christopher Faulet9768c262018-10-22 09:34:31 +02001517 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001518 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001519 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001520 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001521 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001522 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001523 }
1524
Christopher Faulete0768eb2018-10-03 16:38:02 +02001525 rep->analysers &= AN_RES_FLT_END;
1526 txn->status = 504;
1527 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001528 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001529
1530 if (!(s->flags & SF_ERR_MASK))
1531 s->flags |= SF_ERR_SRVTO;
1532 if (!(s->flags & SF_FINST_MASK))
1533 s->flags |= SF_FINST_H;
1534 return 0;
1535 }
1536
Christopher Faulet9768c262018-10-22 09:34:31 +02001537 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001538 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001539 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1540 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001541 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001542 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001543
1544 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001545 txn->status = 400;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001546 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001547
1548 if (!(s->flags & SF_ERR_MASK))
1549 s->flags |= SF_ERR_CLICL;
1550 if (!(s->flags & SF_FINST_MASK))
1551 s->flags |= SF_FINST_H;
1552
1553 /* process_stream() will take care of the error */
1554 return 0;
1555 }
1556
Christopher Faulet9768c262018-10-22 09:34:31 +02001557 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001558 else if (rep->flags & CF_SHUTR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001559 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001560 goto abort_keep_alive;
1561
Olivier Houcharda798bf52019-03-08 18:52:00 +01001562 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001563 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001564 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001565 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001566 }
1567
Christopher Faulete0768eb2018-10-03 16:38:02 +02001568 rep->analysers &= AN_RES_FLT_END;
1569 txn->status = 502;
1570 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001571 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001572
1573 if (!(s->flags & SF_ERR_MASK))
1574 s->flags |= SF_ERR_SRVCL;
1575 if (!(s->flags & SF_FINST_MASK))
1576 s->flags |= SF_FINST_H;
1577 return 0;
1578 }
1579
Christopher Faulet9768c262018-10-22 09:34:31 +02001580 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001581 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001582 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001583 goto abort_keep_alive;
1584
Olivier Houcharda798bf52019-03-08 18:52:00 +01001585 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001586 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001587
1588 if (!(s->flags & SF_ERR_MASK))
1589 s->flags |= SF_ERR_CLICL;
1590 if (!(s->flags & SF_FINST_MASK))
1591 s->flags |= SF_FINST_H;
1592
1593 /* process_stream() will take care of the error */
1594 return 0;
1595 }
1596
1597 channel_dont_close(rep);
1598 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
1599 return 0;
1600 }
1601
1602 /* More interesting part now : we know that we have a complete
1603 * response which at least looks like HTTP. We have an indicator
1604 * of each header's length, so we can parse them quickly.
1605 */
1606
Christopher Faulet9768c262018-10-22 09:34:31 +02001607 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet03599112018-11-27 11:21:21 +01001608 sl = http_find_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001609
Christopher Faulet9768c262018-10-22 09:34:31 +02001610 /* 0: we might have to print this header in debug mode */
1611 if (unlikely((global.mode & MODE_DEBUG) &&
1612 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1613 int32_t pos;
1614
Christopher Faulet03599112018-11-27 11:21:21 +01001615 htx_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001616
1617 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1618 struct htx_blk *blk = htx_get_blk(htx, pos);
1619 enum htx_blk_type type = htx_get_blk_type(blk);
1620
1621 if (type == HTX_BLK_EOH)
1622 break;
1623 if (type != HTX_BLK_HDR)
1624 continue;
1625
1626 htx_debug_hdr("srvhdr", s,
1627 htx_get_blk_name(htx, blk),
1628 htx_get_blk_value(htx, blk));
1629 }
1630 }
1631
Christopher Faulet03599112018-11-27 11:21:21 +01001632 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001633 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001634 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001635 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001636 if (sl->flags & HTX_SL_F_XFER_LEN) {
1637 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001638 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001639 if (sl->flags & HTX_SL_F_BODYLESS)
1640 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001641 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001642
1643 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001644 if (n < 1 || n > 5)
1645 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001646
Christopher Faulete0768eb2018-10-03 16:38:02 +02001647 /* when the client triggers a 4xx from the server, it's most often due
1648 * to a missing object or permission. These events should be tracked
1649 * because if they happen often, it may indicate a brute force or a
1650 * vulnerability scan.
1651 */
1652 if (n == 4)
1653 stream_inc_http_err_ctr(s);
1654
1655 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001656 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001657
Christopher Faulete0768eb2018-10-03 16:38:02 +02001658 /* Adjust server's health based on status code. Note: status codes 501
1659 * and 505 are triggered on demand by client request, so we must not
1660 * count them as server failures.
1661 */
1662 if (objt_server(s->target)) {
1663 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001664 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001665 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001666 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001667 }
1668
1669 /*
1670 * We may be facing a 100-continue response, or any other informational
1671 * 1xx response which is non-final, in which case this is not the right
1672 * response, and we're waiting for the next one. Let's allow this response
1673 * to go to the client and wait for the next one. There's an exception for
1674 * 101 which is used later in the code to switch protocols.
1675 */
1676 if (txn->status < 200 &&
1677 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001678 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet9768c262018-10-22 09:34:31 +02001679 c_adv(rep, htx->data);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001680 msg->msg_state = HTTP_MSG_RPBEFORE;
1681 txn->status = 0;
1682 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet9768c262018-10-22 09:34:31 +02001683 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001684 }
1685
1686 /*
1687 * 2: check for cacheability.
1688 */
1689
1690 switch (txn->status) {
1691 case 200:
1692 case 203:
1693 case 204:
1694 case 206:
1695 case 300:
1696 case 301:
1697 case 404:
1698 case 405:
1699 case 410:
1700 case 414:
1701 case 501:
1702 break;
1703 default:
1704 /* RFC7231#6.1:
1705 * Responses with status codes that are defined as
1706 * cacheable by default (e.g., 200, 203, 204, 206,
1707 * 300, 301, 404, 405, 410, 414, and 501 in this
1708 * specification) can be reused by a cache with
1709 * heuristic expiration unless otherwise indicated
1710 * by the method definition or explicit cache
1711 * controls [RFC7234]; all other status codes are
1712 * not cacheable by default.
1713 */
1714 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1715 break;
1716 }
1717
1718 /*
1719 * 3: we may need to capture headers
1720 */
1721 s->logs.logwait &= ~LW_RESP;
1722 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet9768c262018-10-22 09:34:31 +02001723 htx_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001724
Christopher Faulet9768c262018-10-22 09:34:31 +02001725 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001726 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1727 txn->status == 101)) {
1728 /* Either we've established an explicit tunnel, or we're
1729 * switching the protocol. In both cases, we're very unlikely
1730 * to understand the next protocols. We have to switch to tunnel
1731 * mode, so that we transfer the request and responses then let
1732 * this protocol pass unmodified. When we later implement specific
1733 * parsers for such protocols, we'll want to check the Upgrade
1734 * header which contains information about that protocol for
1735 * responses with status 101 (eg: see RFC2817 about TLS).
1736 */
1737 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001738 }
1739
Christopher Faulet61608322018-11-23 16:23:45 +01001740 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1741 * 407 (Proxy-Authenticate) responses and set the connection to private
1742 */
1743 srv_conn = cs_conn(objt_cs(s->si[1].end));
1744 if (srv_conn) {
1745 struct ist hdr;
1746 struct http_hdr_ctx ctx;
1747
1748 if (txn->status == 401)
1749 hdr = ist("WWW-Authenticate");
1750 else if (txn->status == 407)
1751 hdr = ist("Proxy-Authenticate");
1752 else
1753 goto end;
1754
1755 ctx.blk = NULL;
1756 while (http_find_header(htx, hdr, &ctx, 0)) {
1757 if ((ctx.value.len >= 9 && word_match(ctx.value.ptr, ctx.value.len, "Negotiate", 9)) ||
1758 (ctx.value.len >= 4 && word_match(ctx.value.ptr, ctx.value.len, "NTLM", 4)))
1759 srv_conn->flags |= CO_FL_PRIVATE;
1760 }
1761 }
1762
1763 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001764 /* we want to have the response time before we start processing it */
1765 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1766
1767 /* end of job, return OK */
1768 rep->analysers &= ~an_bit;
1769 rep->analyse_exp = TICK_ETERNITY;
1770 channel_auto_close(rep);
1771 return 1;
1772
Christopher Faulet47365272018-10-31 17:40:50 +01001773 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001774 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001775 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001776 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001777 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001778 }
1779 txn->status = 502;
1780 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001781 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001782 rep->analysers &= AN_RES_FLT_END;
1783
1784 if (!(s->flags & SF_ERR_MASK))
1785 s->flags |= SF_ERR_PRXCOND;
1786 if (!(s->flags & SF_FINST_MASK))
1787 s->flags |= SF_FINST_H;
1788 return 0;
1789
Christopher Faulete0768eb2018-10-03 16:38:02 +02001790 abort_keep_alive:
1791 /* A keep-alive request to the server failed on a network error.
1792 * The client is required to retry. We need to close without returning
1793 * any other information so that the client retries.
1794 */
1795 txn->status = 0;
1796 rep->analysers &= AN_RES_FLT_END;
1797 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001798 s->logs.logwait = 0;
1799 s->logs.level = 0;
1800 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Faulet9768c262018-10-22 09:34:31 +02001801 htx_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001802 return 0;
1803}
1804
1805/* This function performs all the processing enabled for the current response.
1806 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1807 * and updates s->res.analysers. It might make sense to explode it into several
1808 * other functions. It works like process_request (see indications above).
1809 */
1810int htx_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
1811{
1812 struct session *sess = s->sess;
1813 struct http_txn *txn = s->txn;
1814 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001815 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001816 struct proxy *cur_proxy;
1817 struct cond_wordlist *wl;
1818 enum rule_result ret = HTTP_RULE_RES_CONT;
1819
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001820 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1821 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001822
Christopher Faulete0768eb2018-10-03 16:38:02 +02001823 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1824 now_ms, __FUNCTION__,
1825 s,
1826 rep,
1827 rep->rex, rep->wex,
1828 rep->flags,
1829 ci_data(rep),
1830 rep->analysers);
1831
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001832 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001833
1834 /* The stats applet needs to adjust the Connection header but we don't
1835 * apply any filter there.
1836 */
1837 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1838 rep->analysers &= ~an_bit;
1839 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001840 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001841 }
1842
1843 /*
1844 * We will have to evaluate the filters.
1845 * As opposed to version 1.2, now they will be evaluated in the
1846 * filters order and not in the header order. This means that
1847 * each filter has to be validated among all headers.
1848 *
1849 * Filters are tried with ->be first, then with ->fe if it is
1850 * different from ->be.
1851 *
1852 * Maybe we are in resume condiion. In this case I choose the
1853 * "struct proxy" which contains the rule list matching the resume
1854 * pointer. If none of theses "struct proxy" match, I initialise
1855 * the process with the first one.
1856 *
1857 * In fact, I check only correspondance betwwen the current list
1858 * pointer and the ->fe rule list. If it doesn't match, I initialize
1859 * the loop with the ->be.
1860 */
1861 if (s->current_rule_list == &sess->fe->http_res_rules)
1862 cur_proxy = sess->fe;
1863 else
1864 cur_proxy = s->be;
1865 while (1) {
1866 struct proxy *rule_set = cur_proxy;
1867
1868 /* evaluate http-response rules */
1869 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001870 ret = htx_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001871
1872 if (ret == HTTP_RULE_RES_BADREQ)
1873 goto return_srv_prx_502;
1874
1875 if (ret == HTTP_RULE_RES_DONE) {
1876 rep->analysers &= ~an_bit;
1877 rep->analyse_exp = TICK_ETERNITY;
1878 return 1;
1879 }
1880 }
1881
1882 /* we need to be called again. */
1883 if (ret == HTTP_RULE_RES_YIELD) {
1884 channel_dont_close(rep);
1885 return 0;
1886 }
1887
1888 /* try headers filters */
1889 if (rule_set->rsp_exp != NULL) {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001890 if (htx_apply_filters_to_response(s, rep, rule_set) < 0)
1891 goto return_bad_resp;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001892 }
1893
1894 /* has the response been denied ? */
1895 if (txn->flags & TX_SVDENY) {
1896 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001897 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_secu, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001898
Olivier Houcharda798bf52019-03-08 18:52:00 +01001899 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
1900 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001901 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001902 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001903 goto return_srv_prx_502;
1904 }
1905
1906 /* add response headers from the rule sets in the same order */
1907 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001908 struct ist n, v;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001909 if (txn->status < 200 && txn->status != 101)
1910 break;
1911 if (wl->cond) {
1912 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
1913 ret = acl_pass(ret);
1914 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
1915 ret = !ret;
1916 if (!ret)
1917 continue;
1918 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001919
1920 http_parse_header(ist2(wl->s, strlen(wl->s)), &n, &v);
1921 if (unlikely(!http_add_header(htx, n, v)))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001922 goto return_bad_resp;
1923 }
1924
1925 /* check whether we're already working on the frontend */
1926 if (cur_proxy == sess->fe)
1927 break;
1928 cur_proxy = sess->fe;
1929 }
1930
1931 /* After this point, this anayzer can't return yield, so we can
1932 * remove the bit corresponding to this analyzer from the list.
1933 *
1934 * Note that the intermediate returns and goto found previously
1935 * reset the analyzers.
1936 */
1937 rep->analysers &= ~an_bit;
1938 rep->analyse_exp = TICK_ETERNITY;
1939
1940 /* OK that's all we can do for 1xx responses */
1941 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001942 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001943
1944 /*
1945 * Now check for a server cookie.
1946 */
1947 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001948 htx_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001949
1950 /*
1951 * Check for cache-control or pragma headers if required.
1952 */
1953 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
1954 check_response_for_cacheability(s, rep);
1955
1956 /*
1957 * Add server cookie in the response if needed
1958 */
1959 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1960 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1961 (!(s->flags & SF_DIRECT) ||
1962 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1963 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1964 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1965 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1966 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1967 !(s->flags & SF_IGNORE_PRST)) {
1968 /* the server is known, it's not the one the client requested, or the
1969 * cookie's last seen date needs to be refreshed. We have to
1970 * insert a set-cookie here, except if we want to insert only on POST
1971 * requests and this one isn't. Note that servers which don't have cookies
1972 * (eg: some backup servers) will return a full cookie removal request.
1973 */
1974 if (!objt_server(s->target)->cookie) {
1975 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001976 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001977 s->be->cookie_name);
1978 }
1979 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001980 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001981
1982 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1983 /* emit last_date, which is mandatory */
1984 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1985 s30tob64((date.tv_sec+3) >> 2,
1986 trash.area + trash.data);
1987 trash.data += 5;
1988
1989 if (s->be->cookie_maxlife) {
1990 /* emit first_date, which is either the original one or
1991 * the current date.
1992 */
1993 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1994 s30tob64(txn->cookie_first_date ?
1995 txn->cookie_first_date >> 2 :
1996 (date.tv_sec+3) >> 2,
1997 trash.area + trash.data);
1998 trash.data += 5;
1999 }
2000 }
2001 chunk_appendf(&trash, "; path=/");
2002 }
2003
2004 if (s->be->cookie_domain)
2005 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2006
2007 if (s->be->ck_opts & PR_CK_HTTPONLY)
2008 chunk_appendf(&trash, "; HttpOnly");
2009
2010 if (s->be->ck_opts & PR_CK_SECURE)
2011 chunk_appendf(&trash, "; Secure");
2012
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002013 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002014 goto return_bad_resp;
2015
2016 txn->flags &= ~TX_SCK_MASK;
2017 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2018 /* the server did not change, only the date was updated */
2019 txn->flags |= TX_SCK_UPDATED;
2020 else
2021 txn->flags |= TX_SCK_INSERTED;
2022
2023 /* Here, we will tell an eventual cache on the client side that we don't
2024 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2025 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2026 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2027 */
2028 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2029
2030 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2031
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002032 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002033 goto return_bad_resp;
2034 }
2035 }
2036
2037 /*
2038 * Check if result will be cacheable with a cookie.
2039 * We'll block the response if security checks have caught
2040 * nasty things such as a cacheable cookie.
2041 */
2042 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2043 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2044 (s->be->options & PR_O_CHK_CACHE)) {
2045 /* we're in presence of a cacheable response containing
2046 * a set-cookie header. We'll block it as requested by
2047 * the 'checkcache' option, and send an alert.
2048 */
2049 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01002050 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002051
Olivier Houcharda798bf52019-03-08 18:52:00 +01002052 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
2053 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002054 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002055 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002056
2057 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2058 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2059 send_log(s->be, LOG_ALERT,
2060 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2061 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2062 goto return_srv_prx_502;
2063 }
2064
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002065 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002066 /* Always enter in the body analyzer */
2067 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2068 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2069
2070 /* if the user wants to log as soon as possible, without counting
2071 * bytes from the server, then this is the right moment. We have
2072 * to temporarily assign bytes_out to log what we currently have.
2073 */
2074 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2075 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002076 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002077 s->do_log(s);
2078 s->logs.bytes_out = 0;
2079 }
2080 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002081
2082 return_bad_resp:
2083 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01002084 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01002085 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002086 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01002087 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002088
2089 return_srv_prx_502:
2090 rep->analysers &= AN_RES_FLT_END;
2091 txn->status = 502;
2092 s->logs.t_data = -1; /* was not a valid response */
2093 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01002094 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002095 if (!(s->flags & SF_ERR_MASK))
2096 s->flags |= SF_ERR_PRXCOND;
2097 if (!(s->flags & SF_FINST_MASK))
2098 s->flags |= SF_FINST_H;
2099 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002100}
2101
2102/* This function is an analyser which forwards response body (including chunk
2103 * sizes if any). It is called as soon as we must forward, even if we forward
2104 * zero byte. The only situation where it must not be called is when we're in
2105 * tunnel mode and we want to forward till the close. It's used both to forward
2106 * remaining data and to resync after end of body. It expects the msg_state to
2107 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2108 * read more data, or 1 once we can go on with next request or end the stream.
2109 *
2110 * It is capable of compressing response data both in content-length mode and
2111 * in chunked mode. The state machines follows different flows depending on
2112 * whether content-length and chunked modes are used, since there are no
2113 * trailers in content-length :
2114 *
2115 * chk-mode cl-mode
2116 * ,----- BODY -----.
2117 * / \
2118 * V size > 0 V chk-mode
2119 * .--> SIZE -------------> DATA -------------> CRLF
2120 * | | size == 0 | last byte |
2121 * | v final crlf v inspected |
2122 * | TRAILERS -----------> DONE |
2123 * | |
2124 * `----------------------------------------------'
2125 *
2126 * Compression only happens in the DATA state, and must be flushed in final
2127 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2128 * is performed at once on final states for all bytes parsed, or when leaving
2129 * on missing data.
2130 */
2131int htx_response_forward_body(struct stream *s, struct channel *res, int an_bit)
2132{
2133 struct session *sess = s->sess;
2134 struct http_txn *txn = s->txn;
2135 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002136 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002137 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002138
2139 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
2140 now_ms, __FUNCTION__,
2141 s,
2142 res,
2143 res->rex, res->wex,
2144 res->flags,
2145 ci_data(res),
2146 res->analysers);
2147
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002148 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002149
2150 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002151 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002152 /* Output closed while we were sending data. We must abort and
2153 * wake the other side up.
2154 */
2155 msg->err_state = msg->msg_state;
2156 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002157 htx_end_response(s);
2158 htx_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002159 return 1;
2160 }
2161
Christopher Faulet9768c262018-10-22 09:34:31 +02002162 if (msg->msg_state == HTTP_MSG_BODY)
2163 msg->msg_state = HTTP_MSG_DATA;
2164
Christopher Faulete0768eb2018-10-03 16:38:02 +02002165 /* in most states, we should abort in case of early close */
2166 channel_auto_close(res);
2167
Christopher Faulete0768eb2018-10-03 16:38:02 +02002168 if (res->to_forward) {
2169 /* We can't process the buffer's contents yet */
2170 res->flags |= CF_WAKE_WRITE;
2171 goto missing_data_or_waiting;
2172 }
2173
Christopher Faulet9768c262018-10-22 09:34:31 +02002174 if (msg->msg_state >= HTTP_MSG_DONE)
2175 goto done;
2176
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002177 /* Forward input data. We get it by removing all outgoing data not
2178 * forwarded yet from HTX data size. If there are some data filters, we
2179 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002180 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002181 if (HAS_RSP_DATA_FILTERS(s)) {
2182 ret = flt_http_payload(s, msg, htx->data);
2183 if (ret < 0)
2184 goto return_bad_res;
2185 c_adv(res, ret);
2186 if (htx->data != co_data(res) || htx->extra)
2187 goto missing_data_or_waiting;
2188 }
2189 else {
2190 c_adv(res, htx->data - co_data(res));
Christopher Faulet9768c262018-10-22 09:34:31 +02002191
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002192 /* To let the function channel_forward work as expected we must update
2193 * the channel's buffer to pretend there is no more input data. The
2194 * right length is then restored. We must do that, because when an HTX
2195 * message is stored into a buffer, it appears as full.
2196 */
Christopher Fauletb2aedea2018-12-05 11:56:15 +01002197 if ((msg->flags & HTTP_MSGF_XFER_LEN) && htx->extra)
2198 htx->extra -= channel_htx_forward(res, htx, htx->extra);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002199 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002200
2201 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
2202 /* The server still sending data that should be filtered */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002203 if (res->flags & CF_SHUTR || !HAS_RSP_DATA_FILTERS(s)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02002204 msg->msg_state = HTTP_MSG_TUNNEL;
2205 goto done;
2206 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002207 }
2208
Christopher Faulet9768c262018-10-22 09:34:31 +02002209 /* Check if the end-of-message is reached and if so, switch the message
2210 * in HTTP_MSG_DONE state.
2211 */
2212 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2213 goto missing_data_or_waiting;
2214
2215 msg->msg_state = HTTP_MSG_DONE;
2216
2217 done:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002218 /* other states, DONE...TUNNEL */
Christopher Faulet9768c262018-10-22 09:34:31 +02002219 channel_dont_close(res);
2220
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002221 if (HAS_RSP_DATA_FILTERS(s)) {
2222 ret = flt_http_end(s, msg);
2223 if (ret <= 0) {
2224 if (!ret)
2225 goto missing_data_or_waiting;
2226 goto return_bad_res;
2227 }
2228 }
2229
Christopher Fauletf2824e62018-10-01 12:12:37 +02002230 htx_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002231 if (!(res->analysers & an_bit)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02002232 htx_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002233 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2234 if (res->flags & CF_SHUTW) {
2235 /* response errors are most likely due to the
2236 * client aborting the transfer. */
2237 goto aborted_xfer;
2238 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002239 goto return_bad_res;
2240 }
2241 return 1;
2242 }
2243 return 0;
2244
2245 missing_data_or_waiting:
2246 if (res->flags & CF_SHUTW)
2247 goto aborted_xfer;
2248
Christopher Faulet47365272018-10-31 17:40:50 +01002249 if (htx->flags & HTX_FL_PARSING_ERROR)
2250 goto return_bad_res;
2251
Christopher Faulete0768eb2018-10-03 16:38:02 +02002252 /* stop waiting for data if the input is closed before the end. If the
2253 * client side was already closed, it means that the client has aborted,
2254 * so we don't want to count this as a server abort. Otherwise it's a
2255 * server abort.
2256 */
Christopher Faulet9768c262018-10-22 09:34:31 +02002257 if (msg->msg_state < HTTP_MSG_DONE && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002258 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
2259 goto aborted_xfer;
2260 /* If we have some pending data, we continue the processing */
Christopher Faulet9768c262018-10-22 09:34:31 +02002261 if (htx_is_empty(htx)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002262 if (!(s->flags & SF_ERR_MASK))
2263 s->flags |= SF_ERR_SRVCL;
Olivier Houcharda798bf52019-03-08 18:52:00 +01002264 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002265 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01002266 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002267 goto return_bad_res_stats_ok;
2268 }
2269 }
2270
Christopher Faulete0768eb2018-10-03 16:38:02 +02002271 /* When TE: chunked is used, we need to get there again to parse
2272 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002273 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2274 * are filters registered on the stream, we don't want to forward a
2275 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002276 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002277 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002278 channel_dont_close(res);
2279
2280 /* We know that more data are expected, but we couldn't send more that
2281 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2282 * system knows it must not set a PUSH on this first part. Interactive
2283 * modes are already handled by the stream sock layer. We must not do
2284 * this in content-length mode because it could present the MSG_MORE
2285 * flag with the last block of forwarded data, which would cause an
2286 * additional delay to be observed by the receiver.
2287 */
2288 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2289 res->flags |= CF_EXPECT_MORE;
2290
2291 /* the stream handler will take care of timeouts and errors */
2292 return 0;
2293
2294 return_bad_res: /* let's centralize all bad responses */
Olivier Houcharda798bf52019-03-08 18:52:00 +01002295 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002296 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01002297 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002298
2299 return_bad_res_stats_ok:
2300 txn->rsp.err_state = txn->rsp.msg_state;
2301 txn->rsp.msg_state = HTTP_MSG_ERROR;
2302 /* don't send any error message as we're in the body */
Christopher Faulet9768c262018-10-22 09:34:31 +02002303 htx_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002304 res->analysers &= AN_RES_FLT_END;
2305 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
2306 if (objt_server(s->target))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01002307 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002308
2309 if (!(s->flags & SF_ERR_MASK))
2310 s->flags |= SF_ERR_PRXCOND;
2311 if (!(s->flags & SF_FINST_MASK))
2312 s->flags |= SF_FINST_D;
2313 return 0;
2314
2315 aborted_xfer:
2316 txn->rsp.err_state = txn->rsp.msg_state;
2317 txn->rsp.msg_state = HTTP_MSG_ERROR;
2318 /* don't send any error message as we're in the body */
Christopher Faulet9768c262018-10-22 09:34:31 +02002319 htx_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002320 res->analysers &= AN_RES_FLT_END;
2321 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
2322
Olivier Houcharda798bf52019-03-08 18:52:00 +01002323 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2324 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002325 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01002326 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002327
2328 if (!(s->flags & SF_ERR_MASK))
2329 s->flags |= SF_ERR_CLICL;
2330 if (!(s->flags & SF_FINST_MASK))
2331 s->flags |= SF_FINST_D;
2332 return 0;
2333}
2334
Christopher Faulet0f226952018-10-22 09:29:56 +02002335void htx_adjust_conn_mode(struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002336{
2337 struct proxy *fe = strm_fe(s);
2338 int tmp = TX_CON_WANT_CLO;
2339
2340 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2341 tmp = TX_CON_WANT_TUN;
2342
2343 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
Christopher Faulet0f226952018-10-22 09:29:56 +02002344 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002345}
2346
2347/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002348 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002349 * as too large a request to build a valid response.
2350 */
2351int htx_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
2352{
Christopher Faulet99daf282018-11-28 22:58:13 +01002353 struct channel *req = &s->req;
2354 struct channel *res = &s->res;
2355 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002356 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002357 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002358 struct ist status, reason, location;
2359 unsigned int flags;
2360 size_t data;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002361
2362 chunk = alloc_trash_chunk();
2363 if (!chunk)
Christopher Faulet99daf282018-11-28 22:58:13 +01002364 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002365
Christopher Faulet99daf282018-11-28 22:58:13 +01002366 /*
2367 * Create the location
2368 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002369 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002370 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002371 case REDIRECT_TYPE_SCHEME: {
2372 struct http_hdr_ctx ctx;
2373 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002374
Christopher Faulet99daf282018-11-28 22:58:13 +01002375 host = ist("");
2376 ctx.blk = NULL;
2377 if (http_find_header(htx, ist("Host"), &ctx, 0))
2378 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002379
Christopher Faulet99daf282018-11-28 22:58:13 +01002380 sl = http_find_stline(htx);
2381 path = http_get_path(htx_sl_req_uri(sl));
2382 /* build message using path */
2383 if (path.ptr) {
2384 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2385 int qs = 0;
2386 while (qs < path.len) {
2387 if (*(path.ptr + qs) == '?') {
2388 path.len = qs;
2389 break;
2390 }
2391 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002392 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002393 }
2394 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002395 else
2396 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002397
Christopher Faulet99daf282018-11-28 22:58:13 +01002398 if (rule->rdr_str) { /* this is an old "redirect" rule */
2399 /* add scheme */
2400 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2401 goto fail;
2402 }
2403 else {
2404 /* add scheme with executing log format */
2405 chunk->data += build_logline(s, chunk->area + chunk->data,
2406 chunk->size - chunk->data,
2407 &rule->rdr_fmt);
2408 }
2409 /* add "://" + host + path */
2410 if (!chunk_memcat(chunk, "://", 3) ||
2411 !chunk_memcat(chunk, host.ptr, host.len) ||
2412 !chunk_memcat(chunk, path.ptr, path.len))
2413 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002414
Christopher Faulet99daf282018-11-28 22:58:13 +01002415 /* append a slash at the end of the location if needed and missing */
2416 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2417 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2418 if (chunk->data + 1 >= chunk->size)
2419 goto fail;
2420 chunk->area[chunk->data++] = '/';
2421 }
2422 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002423 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002424
Christopher Faulet99daf282018-11-28 22:58:13 +01002425 case REDIRECT_TYPE_PREFIX: {
2426 struct ist path;
2427
2428 sl = http_find_stline(htx);
2429 path = http_get_path(htx_sl_req_uri(sl));
2430 /* build message using path */
2431 if (path.ptr) {
2432 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2433 int qs = 0;
2434 while (qs < path.len) {
2435 if (*(path.ptr + qs) == '?') {
2436 path.len = qs;
2437 break;
2438 }
2439 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002440 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002441 }
2442 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002443 else
2444 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002445
Christopher Faulet99daf282018-11-28 22:58:13 +01002446 if (rule->rdr_str) { /* this is an old "redirect" rule */
2447 /* add prefix. Note that if prefix == "/", we don't want to
2448 * add anything, otherwise it makes it hard for the user to
2449 * configure a self-redirection.
2450 */
2451 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2452 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2453 goto fail;
2454 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002455 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002456 else {
2457 /* add prefix with executing log format */
2458 chunk->data += build_logline(s, chunk->area + chunk->data,
2459 chunk->size - chunk->data,
2460 &rule->rdr_fmt);
2461 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002462
Christopher Faulet99daf282018-11-28 22:58:13 +01002463 /* add path */
2464 if (!chunk_memcat(chunk, path.ptr, path.len))
2465 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002466
Christopher Faulet99daf282018-11-28 22:58:13 +01002467 /* append a slash at the end of the location if needed and missing */
2468 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2469 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2470 if (chunk->data + 1 >= chunk->size)
2471 goto fail;
2472 chunk->area[chunk->data++] = '/';
2473 }
2474 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002475 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002476 case REDIRECT_TYPE_LOCATION:
2477 default:
2478 if (rule->rdr_str) { /* this is an old "redirect" rule */
2479 /* add location */
2480 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2481 goto fail;
2482 }
2483 else {
2484 /* add location with executing log format */
2485 chunk->data += build_logline(s, chunk->area + chunk->data,
2486 chunk->size - chunk->data,
2487 &rule->rdr_fmt);
2488 }
2489 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002490 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002491 location = ist2(chunk->area, chunk->data);
2492
2493 /*
2494 * Create the 30x response
2495 */
2496 switch (rule->code) {
2497 case 308:
2498 status = ist("308");
2499 reason = ist("Permanent Redirect");
2500 break;
2501 case 307:
2502 status = ist("307");
2503 reason = ist("Temporary Redirect");
2504 break;
2505 case 303:
2506 status = ist("303");
2507 reason = ist("See Other");
2508 break;
2509 case 301:
2510 status = ist("301");
2511 reason = ist("Moved Permanently");
2512 break;
2513 case 302:
2514 default:
2515 status = ist("302");
2516 reason = ist("Found");
2517 break;
2518 }
2519
2520 htx = htx_from_buf(&res->buf);
2521 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2522 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2523 if (!sl)
2524 goto fail;
2525 sl->info.res.status = rule->code;
2526 s->txn->status = rule->code;
2527
2528 if (!htx_add_header(htx, ist("Connection"), ist("close")) ||
2529 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
2530 !htx_add_header(htx, ist("Location"), location))
2531 goto fail;
2532
2533 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2534 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2535 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002536 }
2537
2538 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002539 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2540 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002541 }
2542
Christopher Faulet99daf282018-11-28 22:58:13 +01002543 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2544 goto fail;
2545
Christopher Fauletf2824e62018-10-01 12:12:37 +02002546 /* let's log the request time */
2547 s->logs.tv_request = now;
2548
Christopher Faulet99daf282018-11-28 22:58:13 +01002549 data = htx->data - co_data(res);
Christopher Faulet99daf282018-11-28 22:58:13 +01002550 c_adv(res, data);
2551 res->total += data;
2552
2553 channel_auto_read(req);
2554 channel_abort(req);
2555 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002556 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet99daf282018-11-28 22:58:13 +01002557
2558 res->wex = tick_add_ifset(now_ms, res->wto);
2559 channel_auto_read(res);
2560 channel_auto_close(res);
2561 channel_shutr_now(res);
2562
2563 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002564
2565 if (!(s->flags & SF_ERR_MASK))
2566 s->flags |= SF_ERR_LOCAL;
2567 if (!(s->flags & SF_FINST_MASK))
2568 s->flags |= SF_FINST_R;
2569
Christopher Faulet99daf282018-11-28 22:58:13 +01002570 free_trash_chunk(chunk);
2571 return 1;
2572
2573 fail:
2574 /* If an error occurred, remove the incomplete HTTP response from the
2575 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002576 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002577 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002578 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002579}
2580
Christopher Faulet72333522018-10-24 11:25:02 +02002581int htx_transform_header_str(struct stream* s, struct channel *chn, struct htx *htx,
2582 struct ist name, const char *str, struct my_regex *re, int action)
2583{
2584 struct http_hdr_ctx ctx;
2585 struct buffer *output = get_trash_chunk();
2586
2587 /* find full header is action is ACT_HTTP_REPLACE_HDR */
2588 ctx.blk = NULL;
2589 while (http_find_header(htx, name, &ctx, (action == ACT_HTTP_REPLACE_HDR))) {
2590 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2591 continue;
2592
2593 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2594 if (output->data == -1)
2595 return -1;
2596 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2597 return -1;
2598 }
2599 return 0;
2600}
2601
2602static int htx_transform_header(struct stream* s, struct channel *chn, struct htx *htx,
2603 const struct ist name, struct list *fmt, struct my_regex *re, int action)
2604{
2605 struct buffer *replace;
2606 int ret = -1;
2607
2608 replace = alloc_trash_chunk();
2609 if (!replace)
2610 goto leave;
2611
2612 replace->data = build_logline(s, replace->area, replace->size, fmt);
2613 if (replace->data >= replace->size - 1)
2614 goto leave;
2615
2616 ret = htx_transform_header_str(s, chn, htx, name, replace->area, re, action);
2617
2618 leave:
2619 free_trash_chunk(replace);
2620 return ret;
2621}
2622
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002623
2624/* Terminate a 103-Erly-hints response and send it to the client. It returns 0
2625 * on success and -1 on error. The response channel is updated accordingly.
2626 */
2627static int htx_reply_103_early_hints(struct channel *res)
2628{
2629 struct htx *htx = htx_from_buf(&res->buf);
2630 size_t data;
2631
2632 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM)) {
2633 /* If an error occurred during an Early-hint rule,
2634 * remove the incomplete HTTP 103 response from the
2635 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002636 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002637 return -1;
2638 }
2639
2640 data = htx->data - co_data(res);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002641 c_adv(res, data);
2642 res->total += data;
2643 return 0;
2644}
2645
Christopher Faulet6eb92892018-11-15 16:39:29 +01002646/*
2647 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
2648 * built according to <fmt> log line format.
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002649 * If <early_hints> is 0, it is starts a new response by adding the start
2650 * line. If an error occurred -1 is returned. On success 0 is returned. The
2651 * channel is not updated here. It must be done calling the function
2652 * htx_reply_103_early_hints().
Christopher Faulet6eb92892018-11-15 16:39:29 +01002653 */
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002654static int htx_add_early_hint_header(struct stream *s, int early_hints, const struct ist name, struct list *fmt)
Christopher Faulet6eb92892018-11-15 16:39:29 +01002655{
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002656 struct channel *res = &s->res;
2657 struct htx *htx = htx_from_buf(&res->buf);
2658 struct buffer *value = alloc_trash_chunk();
2659
Christopher Faulet6eb92892018-11-15 16:39:29 +01002660 if (!early_hints) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002661 struct htx_sl *sl;
2662 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
2663 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2664
2665 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
2666 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
2667 if (!sl)
Christopher Faulet6eb92892018-11-15 16:39:29 +01002668 goto fail;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002669 sl->info.res.status = 103;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002670 }
2671
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002672 value->data = build_logline(s, b_tail(value), b_room(value), fmt);
2673 if (!htx_add_header(htx, name, ist2(b_head(value), b_data(value))))
Christopher Faulet6eb92892018-11-15 16:39:29 +01002674 goto fail;
2675
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002676 free_trash_chunk(value);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002677 return 1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002678
2679 fail:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002680 /* If an error occurred during an Early-hint rule, remove the incomplete
2681 * HTTP 103 response from the buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002682 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002683 free_trash_chunk(value);
2684 return -1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002685}
2686
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002687/* This function executes one of the set-{method,path,query,uri} actions. It
2688 * takes the string from the variable 'replace' with length 'len', then modifies
2689 * the relevant part of the request line accordingly. Then it updates various
2690 * pointers to the next elements which were moved, and the total buffer length.
2691 * It finds the action to be performed in p[2], previously filled by function
2692 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2693 * error, though this can be revisited when this code is finally exploited.
2694 *
2695 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2696 * query string and 3 to replace uri.
2697 *
2698 * In query string case, the mark question '?' must be set at the start of the
2699 * string by the caller, event if the replacement query string is empty.
2700 */
2701int htx_req_replace_stline(int action, const char *replace, int len,
2702 struct proxy *px, struct stream *s)
2703{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002704 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002705
2706 switch (action) {
2707 case 0: // method
2708 if (!http_replace_req_meth(htx, ist2(replace, len)))
2709 return -1;
2710 break;
2711
2712 case 1: // path
2713 if (!http_replace_req_path(htx, ist2(replace, len)))
2714 return -1;
2715 break;
2716
2717 case 2: // query
2718 if (!http_replace_req_query(htx, ist2(replace, len)))
2719 return -1;
2720 break;
2721
2722 case 3: // uri
2723 if (!http_replace_req_uri(htx, ist2(replace, len)))
2724 return -1;
2725 break;
2726
2727 default:
2728 return -1;
2729 }
2730 return 0;
2731}
2732
2733/* This function replace the HTTP status code and the associated message. The
2734 * variable <status> contains the new status code. This function never fails.
2735 */
2736void htx_res_set_status(unsigned int status, const char *reason, struct stream *s)
2737{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002738 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002739 char *res;
2740
2741 chunk_reset(&trash);
2742 res = ultoa_o(status, trash.area, trash.size);
2743 trash.data = res - trash.area;
2744
2745 /* Do we have a custom reason format string? */
2746 if (reason == NULL)
2747 reason = http_get_reason(status);
2748
Christopher Faulet87a2c0d2018-12-13 21:58:18 +01002749 if (http_replace_res_status(htx, ist2(trash.area, trash.data)))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002750 http_replace_res_reason(htx, ist2(reason, strlen(reason)));
2751}
2752
Christopher Faulet3e964192018-10-24 11:39:23 +02002753/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2754 * transaction <txn>. Returns the verdict of the first rule that prevents
2755 * further processing of the request (auth, deny, ...), and defaults to
2756 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2757 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2758 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2759 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2760 * status.
2761 */
2762static enum rule_result htx_req_get_intercept_rule(struct proxy *px, struct list *rules,
2763 struct stream *s, int *deny_status)
2764{
2765 struct session *sess = strm_sess(s);
2766 struct http_txn *txn = s->txn;
2767 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002768 struct act_rule *rule;
2769 struct http_hdr_ctx ctx;
2770 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002771 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2772 int act_flags = 0;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002773 int early_hints = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002774
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002775 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002776
2777 /* If "the current_rule_list" match the executed rule list, we are in
2778 * resume condition. If a resume is needed it is always in the action
2779 * and never in the ACL or converters. In this case, we initialise the
2780 * current rule, and go to the action execution point.
2781 */
2782 if (s->current_rule) {
2783 rule = s->current_rule;
2784 s->current_rule = NULL;
2785 if (s->current_rule_list == rules)
2786 goto resume_execution;
2787 }
2788 s->current_rule_list = rules;
2789
2790 list_for_each_entry(rule, rules, list) {
2791 /* check optional condition */
2792 if (rule->cond) {
2793 int ret;
2794
2795 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2796 ret = acl_pass(ret);
2797
2798 if (rule->cond->pol == ACL_COND_UNLESS)
2799 ret = !ret;
2800
2801 if (!ret) /* condition not matched */
2802 continue;
2803 }
2804
2805 act_flags |= ACT_FLAG_FIRST;
2806 resume_execution:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002807 if (early_hints && rule->action != ACT_HTTP_EARLY_HINT) {
2808 early_hints = 0;
2809 if (htx_reply_103_early_hints(&s->res) == -1) {
2810 rule_ret = HTTP_RULE_RES_BADREQ;
2811 goto end;
2812 }
2813 }
2814
Christopher Faulet3e964192018-10-24 11:39:23 +02002815 switch (rule->action) {
2816 case ACT_ACTION_ALLOW:
2817 rule_ret = HTTP_RULE_RES_STOP;
2818 goto end;
2819
2820 case ACT_ACTION_DENY:
2821 if (deny_status)
2822 *deny_status = rule->deny_status;
2823 rule_ret = HTTP_RULE_RES_DENY;
2824 goto end;
2825
2826 case ACT_HTTP_REQ_TARPIT:
2827 txn->flags |= TX_CLTARPIT;
2828 if (deny_status)
2829 *deny_status = rule->deny_status;
2830 rule_ret = HTTP_RULE_RES_DENY;
2831 goto end;
2832
2833 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002834 /* Auth might be performed on regular http-req rules as well as on stats */
2835 auth_realm = rule->arg.auth.realm;
2836 if (!auth_realm) {
2837 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2838 auth_realm = STATS_DEFAULT_REALM;
2839 else
2840 auth_realm = px->id;
2841 }
2842 /* send 401/407 depending on whether we use a proxy or not. We still
2843 * count one error, because normal browsing won't significantly
2844 * increase the counter but brute force attempts will.
2845 */
Christopher Faulet3e964192018-10-24 11:39:23 +02002846 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Faulet12c51e22018-11-28 15:59:42 +01002847 if (htx_reply_40x_unauthorized(s, auth_realm) == -1)
2848 rule_ret = HTTP_RULE_RES_BADREQ;
2849 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02002850 goto end;
2851
2852 case ACT_HTTP_REDIR:
Christopher Faulet3e964192018-10-24 11:39:23 +02002853 rule_ret = HTTP_RULE_RES_DONE;
2854 if (!htx_apply_redirect_rule(rule->arg.redir, s, txn))
2855 rule_ret = HTTP_RULE_RES_BADREQ;
2856 goto end;
2857
2858 case ACT_HTTP_SET_NICE:
2859 s->task->nice = rule->arg.nice;
2860 break;
2861
2862 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01002863 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Christopher Faulet3e964192018-10-24 11:39:23 +02002864 break;
2865
2866 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01002867 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Christopher Faulet3e964192018-10-24 11:39:23 +02002868 break;
2869
2870 case ACT_HTTP_SET_LOGL:
2871 s->logs.level = rule->arg.loglevel;
2872 break;
2873
2874 case ACT_HTTP_REPLACE_HDR:
2875 case ACT_HTTP_REPLACE_VAL:
2876 if (htx_transform_header(s, &s->req, htx,
2877 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
2878 &rule->arg.hdr_add.fmt,
2879 &rule->arg.hdr_add.re, rule->action)) {
2880 rule_ret = HTTP_RULE_RES_BADREQ;
2881 goto end;
2882 }
2883 break;
2884
2885 case ACT_HTTP_DEL_HDR:
2886 /* remove all occurrences of the header */
2887 ctx.blk = NULL;
2888 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
2889 http_remove_header(htx, &ctx);
2890 break;
2891
2892 case ACT_HTTP_SET_HDR:
2893 case ACT_HTTP_ADD_HDR: {
2894 /* The scope of the trash buffer must be limited to this function. The
2895 * build_logline() function can execute a lot of other function which
2896 * can use the trash buffer. So for limiting the scope of this global
2897 * buffer, we build first the header value using build_logline, and
2898 * after we store the header name.
2899 */
2900 struct buffer *replace;
2901 struct ist n, v;
2902
2903 replace = alloc_trash_chunk();
2904 if (!replace) {
2905 rule_ret = HTTP_RULE_RES_BADREQ;
2906 goto end;
2907 }
2908
2909 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
2910 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2911 v = ist2(replace->area, replace->data);
2912
2913 if (rule->action == ACT_HTTP_SET_HDR) {
2914 /* remove all occurrences of the header */
2915 ctx.blk = NULL;
2916 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
2917 http_remove_header(htx, &ctx);
2918 }
2919
2920 if (!http_add_header(htx, n, v)) {
2921 static unsigned char rate_limit = 0;
2922
2923 if ((rate_limit++ & 255) == 0) {
2924 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);
2925 }
2926
Olivier Houcharda798bf52019-03-08 18:52:00 +01002927 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002928 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002929 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002930 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002931 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002932 }
2933 free_trash_chunk(replace);
2934 break;
2935 }
2936
2937 case ACT_HTTP_DEL_ACL:
2938 case ACT_HTTP_DEL_MAP: {
2939 struct pat_ref *ref;
2940 struct buffer *key;
2941
2942 /* collect reference */
2943 ref = pat_ref_lookup(rule->arg.map.ref);
2944 if (!ref)
2945 continue;
2946
2947 /* allocate key */
2948 key = alloc_trash_chunk();
2949 if (!key) {
2950 rule_ret = HTTP_RULE_RES_BADREQ;
2951 goto end;
2952 }
2953
2954 /* collect key */
2955 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2956 key->area[key->data] = '\0';
2957
2958 /* perform update */
2959 /* returned code: 1=ok, 0=ko */
2960 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
2961 pat_ref_delete(ref, key->area);
2962 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
2963
2964 free_trash_chunk(key);
2965 break;
2966 }
2967
2968 case ACT_HTTP_ADD_ACL: {
2969 struct pat_ref *ref;
2970 struct buffer *key;
2971
2972 /* collect reference */
2973 ref = pat_ref_lookup(rule->arg.map.ref);
2974 if (!ref)
2975 continue;
2976
2977 /* allocate key */
2978 key = alloc_trash_chunk();
2979 if (!key) {
2980 rule_ret = HTTP_RULE_RES_BADREQ;
2981 goto end;
2982 }
2983
2984 /* collect key */
2985 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2986 key->area[key->data] = '\0';
2987
2988 /* perform update */
2989 /* add entry only if it does not already exist */
2990 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
2991 if (pat_ref_find_elt(ref, key->area) == NULL)
2992 pat_ref_add(ref, key->area, NULL, NULL);
2993 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
2994
2995 free_trash_chunk(key);
2996 break;
2997 }
2998
2999 case ACT_HTTP_SET_MAP: {
3000 struct pat_ref *ref;
3001 struct buffer *key, *value;
3002
3003 /* collect reference */
3004 ref = pat_ref_lookup(rule->arg.map.ref);
3005 if (!ref)
3006 continue;
3007
3008 /* allocate key */
3009 key = alloc_trash_chunk();
3010 if (!key) {
3011 rule_ret = HTTP_RULE_RES_BADREQ;
3012 goto end;
3013 }
3014
3015 /* allocate value */
3016 value = alloc_trash_chunk();
3017 if (!value) {
3018 free_trash_chunk(key);
3019 rule_ret = HTTP_RULE_RES_BADREQ;
3020 goto end;
3021 }
3022
3023 /* collect key */
3024 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3025 key->area[key->data] = '\0';
3026
3027 /* collect value */
3028 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3029 value->area[value->data] = '\0';
3030
3031 /* perform update */
3032 if (pat_ref_find_elt(ref, key->area) != NULL)
3033 /* update entry if it exists */
3034 pat_ref_set(ref, key->area, value->area, NULL);
3035 else
3036 /* insert a new entry */
3037 pat_ref_add(ref, key->area, value->area, NULL);
3038
3039 free_trash_chunk(key);
3040 free_trash_chunk(value);
3041 break;
3042 }
3043
3044 case ACT_HTTP_EARLY_HINT:
3045 if (!(txn->req.flags & HTTP_MSGF_VER_11))
3046 break;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003047 early_hints = htx_add_early_hint_header(s, early_hints,
3048 ist2(rule->arg.early_hint.name, rule->arg.early_hint.name_len),
Christopher Faulet3e964192018-10-24 11:39:23 +02003049 &rule->arg.early_hint.fmt);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003050 if (early_hints == -1) {
3051 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02003052 goto end;
3053 }
3054 break;
3055
3056 case ACT_CUSTOM:
3057 if ((s->req.flags & CF_READ_ERROR) ||
3058 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3059 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
3060 (px->options & PR_O_ABRT_CLOSE)))
3061 act_flags |= ACT_FLAG_FINAL;
3062
3063 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
3064 case ACT_RET_ERR:
3065 case ACT_RET_CONT:
3066 break;
3067 case ACT_RET_STOP:
3068 rule_ret = HTTP_RULE_RES_DONE;
3069 goto end;
3070 case ACT_RET_YIELD:
3071 s->current_rule = rule;
3072 rule_ret = HTTP_RULE_RES_YIELD;
3073 goto end;
3074 }
3075 break;
3076
3077 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3078 /* Note: only the first valid tracking parameter of each
3079 * applies.
3080 */
3081
3082 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3083 struct stktable *t;
3084 struct stksess *ts;
3085 struct stktable_key *key;
3086 void *ptr1, *ptr2;
3087
3088 t = rule->arg.trk_ctr.table.t;
3089 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL,
3090 rule->arg.trk_ctr.expr, NULL);
3091
3092 if (key && (ts = stktable_get_entry(t, key))) {
3093 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3094
3095 /* let's count a new HTTP request as it's the first time we do it */
3096 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3097 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3098 if (ptr1 || ptr2) {
3099 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3100
3101 if (ptr1)
3102 stktable_data_cast(ptr1, http_req_cnt)++;
3103
3104 if (ptr2)
3105 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
3106 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3107
3108 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3109
3110 /* If data was modified, we need to touch to re-schedule sync */
3111 stktable_touch_local(t, ts, 0);
3112 }
3113
3114 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3115 if (sess->fe != s->be)
3116 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3117 }
3118 }
3119 break;
3120
Joseph Herlantc42c0e92018-11-25 10:43:27 -08003121 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003122 default:
3123 break;
3124 }
3125 }
3126
3127 end:
3128 if (early_hints) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003129 if (htx_reply_103_early_hints(&s->res) == -1)
3130 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02003131 }
3132
3133 /* we reached the end of the rules, nothing to report */
3134 return rule_ret;
3135}
3136
3137/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3138 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3139 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3140 * is returned, the process can continue the evaluation of next rule list. If
3141 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3142 * is returned, it means the operation could not be processed and a server error
3143 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3144 * deny rule. If *YIELD is returned, the caller must call again the function
3145 * with the same context.
3146 */
3147static enum rule_result htx_res_get_intercept_rule(struct proxy *px, struct list *rules,
3148 struct stream *s)
3149{
3150 struct session *sess = strm_sess(s);
3151 struct http_txn *txn = s->txn;
3152 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02003153 struct act_rule *rule;
3154 struct http_hdr_ctx ctx;
3155 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
3156 int act_flags = 0;
3157
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003158 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02003159
3160 /* If "the current_rule_list" match the executed rule list, we are in
3161 * resume condition. If a resume is needed it is always in the action
3162 * and never in the ACL or converters. In this case, we initialise the
3163 * current rule, and go to the action execution point.
3164 */
3165 if (s->current_rule) {
3166 rule = s->current_rule;
3167 s->current_rule = NULL;
3168 if (s->current_rule_list == rules)
3169 goto resume_execution;
3170 }
3171 s->current_rule_list = rules;
3172
3173 list_for_each_entry(rule, rules, list) {
3174 /* check optional condition */
3175 if (rule->cond) {
3176 int ret;
3177
3178 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3179 ret = acl_pass(ret);
3180
3181 if (rule->cond->pol == ACL_COND_UNLESS)
3182 ret = !ret;
3183
3184 if (!ret) /* condition not matched */
3185 continue;
3186 }
3187
3188 act_flags |= ACT_FLAG_FIRST;
3189resume_execution:
3190 switch (rule->action) {
3191 case ACT_ACTION_ALLOW:
3192 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3193 goto end;
3194
3195 case ACT_ACTION_DENY:
3196 txn->flags |= TX_SVDENY;
3197 rule_ret = HTTP_RULE_RES_STOP;
3198 goto end;
3199
3200 case ACT_HTTP_SET_NICE:
3201 s->task->nice = rule->arg.nice;
3202 break;
3203
3204 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003205 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Christopher Faulet3e964192018-10-24 11:39:23 +02003206 break;
3207
3208 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003209 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Christopher Faulet3e964192018-10-24 11:39:23 +02003210 break;
3211
3212 case ACT_HTTP_SET_LOGL:
3213 s->logs.level = rule->arg.loglevel;
3214 break;
3215
3216 case ACT_HTTP_REPLACE_HDR:
3217 case ACT_HTTP_REPLACE_VAL:
3218 if (htx_transform_header(s, &s->res, htx,
3219 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
3220 &rule->arg.hdr_add.fmt,
3221 &rule->arg.hdr_add.re, rule->action)) {
3222 rule_ret = HTTP_RULE_RES_BADREQ;
3223 goto end;
3224 }
3225 break;
3226
3227 case ACT_HTTP_DEL_HDR:
3228 /* remove all occurrences of the header */
3229 ctx.blk = NULL;
3230 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3231 http_remove_header(htx, &ctx);
3232 break;
3233
3234 case ACT_HTTP_SET_HDR:
3235 case ACT_HTTP_ADD_HDR: {
3236 struct buffer *replace;
3237 struct ist n, v;
3238
3239 replace = alloc_trash_chunk();
3240 if (!replace) {
3241 rule_ret = HTTP_RULE_RES_BADREQ;
3242 goto end;
3243 }
3244
3245 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
3246 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3247 v = ist2(replace->area, replace->data);
3248
3249 if (rule->action == ACT_HTTP_SET_HDR) {
3250 /* remove all occurrences of the header */
3251 ctx.blk = NULL;
3252 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3253 http_remove_header(htx, &ctx);
3254 }
3255
3256 if (!http_add_header(htx, n, v)) {
3257 static unsigned char rate_limit = 0;
3258
3259 if ((rate_limit++ & 255) == 0) {
3260 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);
3261 }
3262
Olivier Houcharda798bf52019-03-08 18:52:00 +01003263 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003264 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003265 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003266 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003267 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003268 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01003269 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003270 }
3271 free_trash_chunk(replace);
3272 break;
3273 }
3274
3275 case ACT_HTTP_DEL_ACL:
3276 case ACT_HTTP_DEL_MAP: {
3277 struct pat_ref *ref;
3278 struct buffer *key;
3279
3280 /* collect reference */
3281 ref = pat_ref_lookup(rule->arg.map.ref);
3282 if (!ref)
3283 continue;
3284
3285 /* allocate key */
3286 key = alloc_trash_chunk();
3287 if (!key) {
3288 rule_ret = HTTP_RULE_RES_BADREQ;
3289 goto end;
3290 }
3291
3292 /* collect key */
3293 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3294 key->area[key->data] = '\0';
3295
3296 /* perform update */
3297 /* returned code: 1=ok, 0=ko */
3298 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3299 pat_ref_delete(ref, key->area);
3300 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3301
3302 free_trash_chunk(key);
3303 break;
3304 }
3305
3306 case ACT_HTTP_ADD_ACL: {
3307 struct pat_ref *ref;
3308 struct buffer *key;
3309
3310 /* collect reference */
3311 ref = pat_ref_lookup(rule->arg.map.ref);
3312 if (!ref)
3313 continue;
3314
3315 /* allocate key */
3316 key = alloc_trash_chunk();
3317 if (!key) {
3318 rule_ret = HTTP_RULE_RES_BADREQ;
3319 goto end;
3320 }
3321
3322 /* collect key */
3323 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3324 key->area[key->data] = '\0';
3325
3326 /* perform update */
3327 /* check if the entry already exists */
3328 if (pat_ref_find_elt(ref, key->area) == NULL)
3329 pat_ref_add(ref, key->area, NULL, NULL);
3330
3331 free_trash_chunk(key);
3332 break;
3333 }
3334
3335 case ACT_HTTP_SET_MAP: {
3336 struct pat_ref *ref;
3337 struct buffer *key, *value;
3338
3339 /* collect reference */
3340 ref = pat_ref_lookup(rule->arg.map.ref);
3341 if (!ref)
3342 continue;
3343
3344 /* allocate key */
3345 key = alloc_trash_chunk();
3346 if (!key) {
3347 rule_ret = HTTP_RULE_RES_BADREQ;
3348 goto end;
3349 }
3350
3351 /* allocate value */
3352 value = alloc_trash_chunk();
3353 if (!value) {
3354 free_trash_chunk(key);
3355 rule_ret = HTTP_RULE_RES_BADREQ;
3356 goto end;
3357 }
3358
3359 /* collect key */
3360 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3361 key->area[key->data] = '\0';
3362
3363 /* collect value */
3364 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3365 value->area[value->data] = '\0';
3366
3367 /* perform update */
3368 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3369 if (pat_ref_find_elt(ref, key->area) != NULL)
3370 /* update entry if it exists */
3371 pat_ref_set(ref, key->area, value->area, NULL);
3372 else
3373 /* insert a new entry */
3374 pat_ref_add(ref, key->area, value->area, NULL);
3375 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3376 free_trash_chunk(key);
3377 free_trash_chunk(value);
3378 break;
3379 }
3380
3381 case ACT_HTTP_REDIR:
3382 rule_ret = HTTP_RULE_RES_DONE;
3383 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3384 rule_ret = HTTP_RULE_RES_BADREQ;
3385 goto end;
3386
3387 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3388 /* Note: only the first valid tracking parameter of each
3389 * applies.
3390 */
3391 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3392 struct stktable *t;
3393 struct stksess *ts;
3394 struct stktable_key *key;
3395 void *ptr;
3396
3397 t = rule->arg.trk_ctr.table.t;
3398 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL,
3399 rule->arg.trk_ctr.expr, NULL);
3400
3401 if (key && (ts = stktable_get_entry(t, key))) {
3402 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3403
3404 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3405
3406 /* let's count a new HTTP request as it's the first time we do it */
3407 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3408 if (ptr)
3409 stktable_data_cast(ptr, http_req_cnt)++;
3410
3411 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3412 if (ptr)
3413 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3414 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3415
3416 /* When the client triggers a 4xx from the server, it's most often due
3417 * to a missing object or permission. These events should be tracked
3418 * because if they happen often, it may indicate a brute force or a
3419 * vulnerability scan. Normally this is done when receiving the response
3420 * but here we're tracking after this ought to have been done so we have
3421 * to do it on purpose.
3422 */
3423 if ((unsigned)(txn->status - 400) < 100) {
3424 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3425 if (ptr)
3426 stktable_data_cast(ptr, http_err_cnt)++;
3427
3428 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3429 if (ptr)
3430 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3431 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3432 }
3433
3434 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3435
3436 /* If data was modified, we need to touch to re-schedule sync */
3437 stktable_touch_local(t, ts, 0);
3438
3439 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3440 if (sess->fe != s->be)
3441 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3442 }
3443 }
3444 break;
3445
3446 case ACT_CUSTOM:
3447 if ((s->req.flags & CF_READ_ERROR) ||
3448 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3449 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
3450 (px->options & PR_O_ABRT_CLOSE)))
3451 act_flags |= ACT_FLAG_FINAL;
3452
3453 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
3454 case ACT_RET_ERR:
3455 case ACT_RET_CONT:
3456 break;
3457 case ACT_RET_STOP:
3458 rule_ret = HTTP_RULE_RES_STOP;
3459 goto end;
3460 case ACT_RET_YIELD:
3461 s->current_rule = rule;
3462 rule_ret = HTTP_RULE_RES_YIELD;
3463 goto end;
3464 }
3465 break;
3466
Joseph Herlantc42c0e92018-11-25 10:43:27 -08003467 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003468 default:
3469 break;
3470 }
3471 }
3472
3473 end:
3474 /* we reached the end of the rules, nothing to report */
3475 return rule_ret;
3476}
3477
Christopher Faulet33640082018-10-24 11:53:01 +02003478/* Iterate the same filter through all request headers.
3479 * Returns 1 if this filter can be stopped upon return, otherwise 0.
3480 * Since it can manage the switch to another backend, it updates the per-proxy
3481 * DENY stats.
3482 */
3483static int htx_apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
3484{
3485 struct http_txn *txn = s->txn;
3486 struct htx *htx;
3487 struct buffer *hdr = get_trash_chunk();
3488 int32_t pos;
3489
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003490 htx = htxbuf(&req->buf);
Christopher Faulet33640082018-10-24 11:53:01 +02003491
3492 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3493 struct htx_blk *blk = htx_get_blk(htx, pos);
3494 enum htx_blk_type type;
3495 struct ist n, v;
3496
3497 next_hdr:
3498 type = htx_get_blk_type(blk);
3499 if (type == HTX_BLK_EOH)
3500 break;
3501 if (type != HTX_BLK_HDR)
3502 continue;
3503
3504 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
3505 return 1;
3506 else if (unlikely(txn->flags & TX_CLALLOW) &&
3507 (exp->action == ACT_ALLOW ||
3508 exp->action == ACT_DENY ||
3509 exp->action == ACT_TARPIT))
3510 return 0;
3511
3512 n = htx_get_blk_name(htx, blk);
3513 v = htx_get_blk_value(htx, blk);
3514
Christopher Faulet02e771a2019-02-26 15:36:05 +01003515 chunk_memcpy(hdr, n.ptr, n.len);
Christopher Faulet33640082018-10-24 11:53:01 +02003516 hdr->area[hdr->data++] = ':';
3517 hdr->area[hdr->data++] = ' ';
3518 chunk_memcat(hdr, v.ptr, v.len);
3519
3520 /* Now we have one header in <hdr> */
3521
3522 if (regex_exec_match2(exp->preg, hdr->area, hdr->data, MAX_MATCH, pmatch, 0)) {
3523 struct http_hdr_ctx ctx;
3524 int len;
3525
3526 switch (exp->action) {
3527 case ACT_ALLOW:
3528 txn->flags |= TX_CLALLOW;
3529 goto end;
3530
3531 case ACT_DENY:
3532 txn->flags |= TX_CLDENY;
3533 goto end;
3534
3535 case ACT_TARPIT:
3536 txn->flags |= TX_CLTARPIT;
3537 goto end;
3538
3539 case ACT_REPLACE:
3540 len = exp_replace(trash.area, trash.size, hdr->area, exp->replace, pmatch);
3541 if (len < 0)
3542 return -1;
3543
3544 http_parse_header(ist2(trash.area, len), &n, &v);
3545 ctx.blk = blk;
3546 ctx.value = v;
Christopher Faulet02e771a2019-02-26 15:36:05 +01003547 ctx.lws_before = ctx.lws_after = 0;
Christopher Faulet33640082018-10-24 11:53:01 +02003548 if (!http_replace_header(htx, &ctx, n, v))
3549 return -1;
3550 if (!ctx.blk)
3551 goto end;
3552 pos = htx_get_blk_pos(htx, blk);
3553 break;
3554
3555 case ACT_REMOVE:
3556 ctx.blk = blk;
3557 ctx.value = v;
Christopher Faulet02e771a2019-02-26 15:36:05 +01003558 ctx.lws_before = ctx.lws_after = 0;
Christopher Faulet33640082018-10-24 11:53:01 +02003559 if (!http_remove_header(htx, &ctx))
3560 return -1;
3561 if (!ctx.blk)
3562 goto end;
3563 pos = htx_get_blk_pos(htx, blk);
3564 goto next_hdr;
3565
3566 }
3567 }
3568 }
3569 end:
3570 return 0;
3571}
3572
3573/* Apply the filter to the request line.
3574 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3575 * or -1 if a replacement resulted in an invalid request line.
3576 * Since it can manage the switch to another backend, it updates the per-proxy
3577 * DENY stats.
3578 */
3579static int htx_apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
3580{
3581 struct http_txn *txn = s->txn;
3582 struct htx *htx;
3583 struct buffer *reqline = get_trash_chunk();
3584 int done;
3585
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003586 htx = htxbuf(&req->buf);
Christopher Faulet33640082018-10-24 11:53:01 +02003587
3588 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
3589 return 1;
3590 else if (unlikely(txn->flags & TX_CLALLOW) &&
3591 (exp->action == ACT_ALLOW ||
3592 exp->action == ACT_DENY ||
3593 exp->action == ACT_TARPIT))
3594 return 0;
3595 else if (exp->action == ACT_REMOVE)
3596 return 0;
3597
3598 done = 0;
3599
3600 reqline->data = htx_fmt_req_line(http_find_stline(htx), reqline->area, reqline->size);
3601
3602 /* Now we have the request line between cur_ptr and cur_end */
3603 if (regex_exec_match2(exp->preg, reqline->area, reqline->data, MAX_MATCH, pmatch, 0)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003604 struct htx_sl *sl = http_find_stline(htx);
3605 struct ist meth, uri, vsn;
Christopher Faulet33640082018-10-24 11:53:01 +02003606 int len;
3607
3608 switch (exp->action) {
3609 case ACT_ALLOW:
3610 txn->flags |= TX_CLALLOW;
3611 done = 1;
3612 break;
3613
3614 case ACT_DENY:
3615 txn->flags |= TX_CLDENY;
3616 done = 1;
3617 break;
3618
3619 case ACT_TARPIT:
3620 txn->flags |= TX_CLTARPIT;
3621 done = 1;
3622 break;
3623
3624 case ACT_REPLACE:
3625 len = exp_replace(trash.area, trash.size, reqline->area, exp->replace, pmatch);
3626 if (len < 0)
3627 return -1;
3628
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003629 http_parse_stline(ist2(trash.area, len), &meth, &uri, &vsn);
3630 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
3631 if (!http_replace_stline(htx, meth, uri, vsn))
Christopher Faulet33640082018-10-24 11:53:01 +02003632 return -1;
3633 done = 1;
3634 break;
3635 }
3636 }
3637 return done;
3638}
3639
3640/*
3641 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
3642 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
3643 * unparsable request. Since it can manage the switch to another backend, it
3644 * updates the per-proxy DENY stats.
3645 */
3646static int htx_apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
3647{
3648 struct session *sess = s->sess;
3649 struct http_txn *txn = s->txn;
3650 struct hdr_exp *exp;
3651
3652 for (exp = px->req_exp; exp; exp = exp->next) {
3653 int ret;
3654
3655 /*
3656 * The interleaving of transformations and verdicts
3657 * makes it difficult to decide to continue or stop
3658 * the evaluation.
3659 */
3660
3661 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
3662 break;
3663
3664 if ((txn->flags & TX_CLALLOW) &&
3665 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3666 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
3667 continue;
3668
3669 /* if this filter had a condition, evaluate it now and skip to
3670 * next filter if the condition does not match.
3671 */
3672 if (exp->cond) {
3673 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3674 ret = acl_pass(ret);
3675 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
3676 ret = !ret;
3677
3678 if (!ret)
3679 continue;
3680 }
3681
3682 /* Apply the filter to the request line. */
3683 ret = htx_apply_filter_to_req_line(s, req, exp);
3684 if (unlikely(ret < 0))
3685 return -1;
3686
3687 if (likely(ret == 0)) {
3688 /* The filter did not match the request, it can be
3689 * iterated through all headers.
3690 */
3691 if (unlikely(htx_apply_filter_to_req_headers(s, req, exp) < 0))
3692 return -1;
3693 }
3694 }
3695 return 0;
3696}
3697
3698/* Iterate the same filter through all response headers contained in <res>.
3699 * Returns 1 if this filter can be stopped upon return, otherwise 0.
3700 */
3701static int htx_apply_filter_to_resp_headers(struct stream *s, struct channel *res, struct hdr_exp *exp)
3702{
3703 struct http_txn *txn = s->txn;
3704 struct htx *htx;
3705 struct buffer *hdr = get_trash_chunk();
3706 int32_t pos;
3707
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003708 htx = htxbuf(&res->buf);
Christopher Faulet33640082018-10-24 11:53:01 +02003709
3710 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3711 struct htx_blk *blk = htx_get_blk(htx, pos);
3712 enum htx_blk_type type;
3713 struct ist n, v;
3714
3715 next_hdr:
3716 type = htx_get_blk_type(blk);
3717 if (type == HTX_BLK_EOH)
3718 break;
3719 if (type != HTX_BLK_HDR)
3720 continue;
3721
3722 if (unlikely(txn->flags & TX_SVDENY))
3723 return 1;
3724 else if (unlikely(txn->flags & TX_SVALLOW) &&
3725 (exp->action == ACT_ALLOW ||
3726 exp->action == ACT_DENY))
3727 return 0;
3728
3729 n = htx_get_blk_name(htx, blk);
3730 v = htx_get_blk_value(htx, blk);
3731
Christopher Faulet02e771a2019-02-26 15:36:05 +01003732 chunk_memcpy(hdr, n.ptr, n.len);
Christopher Faulet33640082018-10-24 11:53:01 +02003733 hdr->area[hdr->data++] = ':';
3734 hdr->area[hdr->data++] = ' ';
3735 chunk_memcat(hdr, v.ptr, v.len);
3736
3737 /* Now we have one header in <hdr> */
3738
3739 if (regex_exec_match2(exp->preg, hdr->area, hdr->data, MAX_MATCH, pmatch, 0)) {
3740 struct http_hdr_ctx ctx;
3741 int len;
3742
3743 switch (exp->action) {
3744 case ACT_ALLOW:
3745 txn->flags |= TX_SVALLOW;
3746 goto end;
3747 break;
3748
3749 case ACT_DENY:
3750 txn->flags |= TX_SVDENY;
3751 goto end;
3752 break;
3753
3754 case ACT_REPLACE:
3755 len = exp_replace(trash.area, trash.size, hdr->area, exp->replace, pmatch);
3756 if (len < 0)
3757 return -1;
3758
3759 http_parse_header(ist2(trash.area, len), &n, &v);
3760 ctx.blk = blk;
3761 ctx.value = v;
Christopher Faulet02e771a2019-02-26 15:36:05 +01003762 ctx.lws_before = ctx.lws_after = 0;
Christopher Faulet33640082018-10-24 11:53:01 +02003763 if (!http_replace_header(htx, &ctx, n, v))
3764 return -1;
3765 if (!ctx.blk)
3766 goto end;
3767 pos = htx_get_blk_pos(htx, blk);
3768 break;
3769
3770 case ACT_REMOVE:
3771 ctx.blk = blk;
3772 ctx.value = v;
Christopher Faulet02e771a2019-02-26 15:36:05 +01003773 ctx.lws_before = ctx.lws_after = 0;
Christopher Faulet33640082018-10-24 11:53:01 +02003774 if (!http_remove_header(htx, &ctx))
3775 return -1;
3776 if (!ctx.blk)
3777 goto end;
3778 pos = htx_get_blk_pos(htx, blk);
3779 goto next_hdr;
3780 }
3781 }
3782
3783 }
3784 end:
3785 return 0;
3786}
3787
3788/* Apply the filter to the status line in the response buffer <res>.
3789 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3790 * or -1 if a replacement resulted in an invalid status line.
3791 */
3792static int htx_apply_filter_to_sts_line(struct stream *s, struct channel *res, struct hdr_exp *exp)
3793{
3794 struct http_txn *txn = s->txn;
3795 struct htx *htx;
3796 struct buffer *resline = get_trash_chunk();
3797 int done;
3798
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003799 htx = htxbuf(&res->buf);
Christopher Faulet33640082018-10-24 11:53:01 +02003800
3801 if (unlikely(txn->flags & TX_SVDENY))
3802 return 1;
3803 else if (unlikely(txn->flags & TX_SVALLOW) &&
3804 (exp->action == ACT_ALLOW ||
3805 exp->action == ACT_DENY))
3806 return 0;
3807 else if (exp->action == ACT_REMOVE)
3808 return 0;
3809
3810 done = 0;
3811 resline->data = htx_fmt_res_line(http_find_stline(htx), resline->area, resline->size);
3812
3813 /* Now we have the status line between cur_ptr and cur_end */
3814 if (regex_exec_match2(exp->preg, resline->area, resline->data, MAX_MATCH, pmatch, 0)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003815 struct htx_sl *sl = http_find_stline(htx);
3816 struct ist vsn, code, reason;
Christopher Faulet33640082018-10-24 11:53:01 +02003817 int len;
3818
3819 switch (exp->action) {
3820 case ACT_ALLOW:
3821 txn->flags |= TX_SVALLOW;
3822 done = 1;
3823 break;
3824
3825 case ACT_DENY:
3826 txn->flags |= TX_SVDENY;
3827 done = 1;
3828 break;
3829
3830 case ACT_REPLACE:
3831 len = exp_replace(trash.area, trash.size, resline->area, exp->replace, pmatch);
3832 if (len < 0)
3833 return -1;
3834
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003835 http_parse_stline(ist2(trash.area, len), &vsn, &code, &reason);
3836 sl->info.res.status = strl2ui(code.ptr, code.len);
3837 if (!http_replace_stline(htx, vsn, code, reason))
Christopher Faulet33640082018-10-24 11:53:01 +02003838 return -1;
3839
3840 done = 1;
3841 return 1;
3842 }
3843 }
3844 return done;
3845}
3846
3847/*
3848 * Apply all the resp filters of proxy <px> to all headers in buffer <res> of stream <s>.
3849 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
3850 * unparsable response.
3851 */
3852static int htx_apply_filters_to_response(struct stream *s, struct channel *res, struct proxy *px)
3853{
3854 struct session *sess = s->sess;
3855 struct http_txn *txn = s->txn;
3856 struct hdr_exp *exp;
3857
3858 for (exp = px->rsp_exp; exp; exp = exp->next) {
3859 int ret;
3860
3861 /*
3862 * The interleaving of transformations and verdicts
3863 * makes it difficult to decide to continue or stop
3864 * the evaluation.
3865 */
3866
3867 if (txn->flags & TX_SVDENY)
3868 break;
3869
3870 if ((txn->flags & TX_SVALLOW) &&
3871 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3872 exp->action == ACT_PASS)) {
3873 exp = exp->next;
3874 continue;
3875 }
3876
3877 /* if this filter had a condition, evaluate it now and skip to
3878 * next filter if the condition does not match.
3879 */
3880 if (exp->cond) {
3881 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3882 ret = acl_pass(ret);
3883 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
3884 ret = !ret;
3885 if (!ret)
3886 continue;
3887 }
3888
3889 /* Apply the filter to the status line. */
3890 ret = htx_apply_filter_to_sts_line(s, res, exp);
3891 if (unlikely(ret < 0))
3892 return -1;
3893
3894 if (likely(ret == 0)) {
3895 /* The filter did not match the response, it can be
3896 * iterated through all headers.
3897 */
3898 if (unlikely(htx_apply_filter_to_resp_headers(s, res, exp) < 0))
3899 return -1;
3900 }
3901 }
3902 return 0;
3903}
3904
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003905/*
3906 * Manage client-side cookie. It can impact performance by about 2% so it is
3907 * desirable to call it only when needed. This code is quite complex because
3908 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3909 * highly recommended not to touch this part without a good reason !
3910 */
3911static void htx_manage_client_side_cookies(struct stream *s, struct channel *req)
3912{
3913 struct session *sess = s->sess;
3914 struct http_txn *txn = s->txn;
3915 struct htx *htx;
3916 struct http_hdr_ctx ctx;
3917 char *hdr_beg, *hdr_end, *del_from;
3918 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3919 int preserve_hdr;
3920
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003921 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003922 ctx.blk = NULL;
3923 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
3924 del_from = NULL; /* nothing to be deleted */
3925 preserve_hdr = 0; /* assume we may kill the whole header */
3926
3927 /* Now look for cookies. Conforming to RFC2109, we have to support
3928 * attributes whose name begin with a '$', and associate them with
3929 * the right cookie, if we want to delete this cookie.
3930 * So there are 3 cases for each cookie read :
3931 * 1) it's a special attribute, beginning with a '$' : ignore it.
3932 * 2) it's a server id cookie that we *MAY* want to delete : save
3933 * some pointers on it (last semi-colon, beginning of cookie...)
3934 * 3) it's an application cookie : we *MAY* have to delete a previous
3935 * "special" cookie.
3936 * At the end of loop, if a "special" cookie remains, we may have to
3937 * remove it. If no application cookie persists in the header, we
3938 * *MUST* delete it.
3939 *
3940 * Note: RFC2965 is unclear about the processing of spaces around
3941 * the equal sign in the ATTR=VALUE form. A careful inspection of
3942 * the RFC explicitly allows spaces before it, and not within the
3943 * tokens (attrs or values). An inspection of RFC2109 allows that
3944 * too but section 10.1.3 lets one think that spaces may be allowed
3945 * after the equal sign too, resulting in some (rare) buggy
3946 * implementations trying to do that. So let's do what servers do.
3947 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3948 * allowed quoted strings in values, with any possible character
3949 * after a backslash, including control chars and delimitors, which
3950 * causes parsing to become ambiguous. Browsers also allow spaces
3951 * within values even without quotes.
3952 *
3953 * We have to keep multiple pointers in order to support cookie
3954 * removal at the beginning, middle or end of header without
3955 * corrupting the header. All of these headers are valid :
3956 *
3957 * hdr_beg hdr_end
3958 * | |
3959 * v |
3960 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3961 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3962 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3963 * | | | | | | |
3964 * | | | | | | |
3965 * | | | | | | +--> next
3966 * | | | | | +----> val_end
3967 * | | | | +-----------> val_beg
3968 * | | | +--------------> equal
3969 * | | +----------------> att_end
3970 * | +---------------------> att_beg
3971 * +--------------------------> prev
3972 *
3973 */
3974 hdr_beg = ctx.value.ptr;
3975 hdr_end = hdr_beg + ctx.value.len;
3976 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3977 /* Iterate through all cookies on this line */
3978
3979 /* find att_beg */
3980 att_beg = prev;
3981 if (prev > hdr_beg)
3982 att_beg++;
3983
3984 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3985 att_beg++;
3986
3987 /* find att_end : this is the first character after the last non
3988 * space before the equal. It may be equal to hdr_end.
3989 */
3990 equal = att_end = att_beg;
3991 while (equal < hdr_end) {
3992 if (*equal == '=' || *equal == ',' || *equal == ';')
3993 break;
3994 if (HTTP_IS_SPHT(*equal++))
3995 continue;
3996 att_end = equal;
3997 }
3998
3999 /* here, <equal> points to '=', a delimitor or the end. <att_end>
4000 * is between <att_beg> and <equal>, both may be identical.
4001 */
4002 /* look for end of cookie if there is an equal sign */
4003 if (equal < hdr_end && *equal == '=') {
4004 /* look for the beginning of the value */
4005 val_beg = equal + 1;
4006 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
4007 val_beg++;
4008
4009 /* find the end of the value, respecting quotes */
4010 next = http_find_cookie_value_end(val_beg, hdr_end);
4011
4012 /* make val_end point to the first white space or delimitor after the value */
4013 val_end = next;
4014 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
4015 val_end--;
4016 }
4017 else
4018 val_beg = val_end = next = equal;
4019
4020 /* We have nothing to do with attributes beginning with
4021 * '$'. However, they will automatically be removed if a
4022 * header before them is removed, since they're supposed
4023 * to be linked together.
4024 */
4025 if (*att_beg == '$')
4026 continue;
4027
4028 /* Ignore cookies with no equal sign */
4029 if (equal == next) {
4030 /* This is not our cookie, so we must preserve it. But if we already
4031 * scheduled another cookie for removal, we cannot remove the
4032 * complete header, but we can remove the previous block itself.
4033 */
4034 preserve_hdr = 1;
4035 if (del_from != NULL) {
4036 int delta = htx_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
4037 val_end += delta;
4038 next += delta;
4039 hdr_end += delta;
4040 prev = del_from;
4041 del_from = NULL;
4042 }
4043 continue;
4044 }
4045
4046 /* if there are spaces around the equal sign, we need to
4047 * strip them otherwise we'll get trouble for cookie captures,
4048 * or even for rewrites. Since this happens extremely rarely,
4049 * it does not hurt performance.
4050 */
4051 if (unlikely(att_end != equal || val_beg > equal + 1)) {
4052 int stripped_before = 0;
4053 int stripped_after = 0;
4054
4055 if (att_end != equal) {
4056 memmove(att_end, equal, hdr_end - equal);
4057 stripped_before = (att_end - equal);
4058 equal += stripped_before;
4059 val_beg += stripped_before;
4060 }
4061
4062 if (val_beg > equal + 1) {
4063 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
4064 stripped_after = (equal + 1) - val_beg;
4065 val_beg += stripped_after;
4066 stripped_before += stripped_after;
4067 }
4068
4069 val_end += stripped_before;
4070 next += stripped_before;
4071 hdr_end += stripped_before;
4072 }
4073 /* now everything is as on the diagram above */
4074
4075 /* First, let's see if we want to capture this cookie. We check
4076 * that we don't already have a client side cookie, because we
4077 * can only capture one. Also as an optimisation, we ignore
4078 * cookies shorter than the declared name.
4079 */
4080 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
4081 (val_end - att_beg >= sess->fe->capture_namelen) &&
4082 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
4083 int log_len = val_end - att_beg;
4084
4085 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
4086 ha_alert("HTTP logging : out of memory.\n");
4087 } else {
4088 if (log_len > sess->fe->capture_len)
4089 log_len = sess->fe->capture_len;
4090 memcpy(txn->cli_cookie, att_beg, log_len);
4091 txn->cli_cookie[log_len] = 0;
4092 }
4093 }
4094
4095 /* Persistence cookies in passive, rewrite or insert mode have the
4096 * following form :
4097 *
4098 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
4099 *
4100 * For cookies in prefix mode, the form is :
4101 *
4102 * Cookie: NAME=SRV~VALUE
4103 */
4104 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
4105 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
4106 struct server *srv = s->be->srv;
4107 char *delim;
4108
4109 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4110 * have the server ID between val_beg and delim, and the original cookie between
4111 * delim+1 and val_end. Otherwise, delim==val_end :
4112 *
4113 * hdr_beg
4114 * |
4115 * v
4116 * NAME=SRV; # in all but prefix modes
4117 * NAME=SRV~OPAQUE ; # in prefix mode
4118 * || || | |+-> next
4119 * || || | +--> val_end
4120 * || || +---------> delim
4121 * || |+------------> val_beg
4122 * || +-------------> att_end = equal
4123 * |+-----------------> att_beg
4124 * +------------------> prev
4125 *
4126 */
4127 if (s->be->ck_opts & PR_CK_PFX) {
4128 for (delim = val_beg; delim < val_end; delim++)
4129 if (*delim == COOKIE_DELIM)
4130 break;
4131 }
4132 else {
4133 char *vbar1;
4134 delim = val_end;
4135 /* Now check if the cookie contains a date field, which would
4136 * appear after a vertical bar ('|') just after the server name
4137 * and before the delimiter.
4138 */
4139 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
4140 if (vbar1) {
4141 /* OK, so left of the bar is the server's cookie and
4142 * right is the last seen date. It is a base64 encoded
4143 * 30-bit value representing the UNIX date since the
4144 * epoch in 4-second quantities.
4145 */
4146 int val;
4147 delim = vbar1++;
4148 if (val_end - vbar1 >= 5) {
4149 val = b64tos30(vbar1);
4150 if (val > 0)
4151 txn->cookie_last_date = val << 2;
4152 }
4153 /* look for a second vertical bar */
4154 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
4155 if (vbar1 && (val_end - vbar1 > 5)) {
4156 val = b64tos30(vbar1 + 1);
4157 if (val > 0)
4158 txn->cookie_first_date = val << 2;
4159 }
4160 }
4161 }
4162
4163 /* if the cookie has an expiration date and the proxy wants to check
4164 * it, then we do that now. We first check if the cookie is too old,
4165 * then only if it has expired. We detect strict overflow because the
4166 * time resolution here is not great (4 seconds). Cookies with dates
4167 * in the future are ignored if their offset is beyond one day. This
4168 * allows an admin to fix timezone issues without expiring everyone
4169 * and at the same time avoids keeping unwanted side effects for too
4170 * long.
4171 */
4172 if (txn->cookie_first_date && s->be->cookie_maxlife &&
4173 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
4174 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
4175 txn->flags &= ~TX_CK_MASK;
4176 txn->flags |= TX_CK_OLD;
4177 delim = val_beg; // let's pretend we have not found the cookie
4178 txn->cookie_first_date = 0;
4179 txn->cookie_last_date = 0;
4180 }
4181 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
4182 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
4183 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
4184 txn->flags &= ~TX_CK_MASK;
4185 txn->flags |= TX_CK_EXPIRED;
4186 delim = val_beg; // let's pretend we have not found the cookie
4187 txn->cookie_first_date = 0;
4188 txn->cookie_last_date = 0;
4189 }
4190
4191 /* Here, we'll look for the first running server which supports the cookie.
4192 * This allows to share a same cookie between several servers, for example
4193 * to dedicate backup servers to specific servers only.
4194 * However, to prevent clients from sticking to cookie-less backup server
4195 * when they have incidentely learned an empty cookie, we simply ignore
4196 * empty cookies and mark them as invalid.
4197 * The same behaviour is applied when persistence must be ignored.
4198 */
4199 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
4200 srv = NULL;
4201
4202 while (srv) {
4203 if (srv->cookie && (srv->cklen == delim - val_beg) &&
4204 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
4205 if ((srv->cur_state != SRV_ST_STOPPED) ||
4206 (s->be->options & PR_O_PERSIST) ||
4207 (s->flags & SF_FORCE_PRST)) {
4208 /* we found the server and we can use it */
4209 txn->flags &= ~TX_CK_MASK;
4210 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
4211 s->flags |= SF_DIRECT | SF_ASSIGNED;
4212 s->target = &srv->obj_type;
4213 break;
4214 } else {
4215 /* we found a server, but it's down,
4216 * mark it as such and go on in case
4217 * another one is available.
4218 */
4219 txn->flags &= ~TX_CK_MASK;
4220 txn->flags |= TX_CK_DOWN;
4221 }
4222 }
4223 srv = srv->next;
4224 }
4225
4226 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
4227 /* no server matched this cookie or we deliberately skipped it */
4228 txn->flags &= ~TX_CK_MASK;
4229 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
4230 txn->flags |= TX_CK_UNUSED;
4231 else
4232 txn->flags |= TX_CK_INVALID;
4233 }
4234
4235 /* depending on the cookie mode, we may have to either :
4236 * - delete the complete cookie if we're in insert+indirect mode, so that
4237 * the server never sees it ;
4238 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08004239 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004240 * if we're in cookie prefix mode
4241 */
4242 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
4243 int delta; /* negative */
4244
4245 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
4246 delta = val_beg - (delim + 1);
4247 val_end += delta;
4248 next += delta;
4249 hdr_end += delta;
4250 del_from = NULL;
4251 preserve_hdr = 1; /* we want to keep this cookie */
4252 }
4253 else if (del_from == NULL &&
4254 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
4255 del_from = prev;
4256 }
4257 }
4258 else {
4259 /* This is not our cookie, so we must preserve it. But if we already
4260 * scheduled another cookie for removal, we cannot remove the
4261 * complete header, but we can remove the previous block itself.
4262 */
4263 preserve_hdr = 1;
4264
4265 if (del_from != NULL) {
4266 int delta = htx_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
4267 if (att_beg >= del_from)
4268 att_beg += delta;
4269 if (att_end >= del_from)
4270 att_end += delta;
4271 val_beg += delta;
4272 val_end += delta;
4273 next += delta;
4274 hdr_end += delta;
4275 prev = del_from;
4276 del_from = NULL;
4277 }
4278 }
4279
4280 /* continue with next cookie on this header line */
4281 att_beg = next;
4282 } /* for each cookie */
4283
4284
4285 /* There are no more cookies on this line.
4286 * We may still have one (or several) marked for deletion at the
4287 * end of the line. We must do this now in two ways :
4288 * - if some cookies must be preserved, we only delete from the
4289 * mark to the end of line ;
4290 * - if nothing needs to be preserved, simply delete the whole header
4291 */
4292 if (del_from) {
4293 hdr_end = (preserve_hdr ? del_from : hdr_beg);
4294 }
4295 if ((hdr_end - hdr_beg) != ctx.value.len) {
4296 if (hdr_beg != hdr_end) {
4297 htx_set_blk_value_len(ctx.blk, hdr_end - hdr_beg);
Christopher Faulet6cdaf2a2019-02-12 14:29:57 +01004298 htx->data -= ctx.value.len - (hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004299 }
4300 else
4301 http_remove_header(htx, &ctx);
4302 }
4303 } /* for each "Cookie header */
4304}
4305
4306/*
4307 * Manage server-side cookies. It can impact performance by about 2% so it is
4308 * desirable to call it only when needed. This function is also used when we
4309 * just need to know if there is a cookie (eg: for check-cache).
4310 */
4311static void htx_manage_server_side_cookies(struct stream *s, struct channel *res)
4312{
4313 struct session *sess = s->sess;
4314 struct http_txn *txn = s->txn;
4315 struct htx *htx;
4316 struct http_hdr_ctx ctx;
4317 struct server *srv;
4318 char *hdr_beg, *hdr_end;
4319 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
4320 int is_cookie2;
4321
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004322 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004323
4324 ctx.blk = NULL;
4325 while (1) {
4326 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
4327 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
4328 break;
4329 is_cookie2 = 1;
4330 }
4331
4332 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
4333 * <prev> points to the colon.
4334 */
4335 txn->flags |= TX_SCK_PRESENT;
4336
4337 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
4338 * check-cache is enabled) and we are not interested in checking
4339 * them. Warning, the cookie capture is declared in the frontend.
4340 */
4341 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
4342 break;
4343
4344 /* OK so now we know we have to process this response cookie.
4345 * The format of the Set-Cookie header is slightly different
4346 * from the format of the Cookie header in that it does not
4347 * support the comma as a cookie delimiter (thus the header
4348 * cannot be folded) because the Expires attribute described in
4349 * the original Netscape's spec may contain an unquoted date
4350 * with a comma inside. We have to live with this because
4351 * many browsers don't support Max-Age and some browsers don't
4352 * support quoted strings. However the Set-Cookie2 header is
4353 * clean.
4354 *
4355 * We have to keep multiple pointers in order to support cookie
4356 * removal at the beginning, middle or end of header without
4357 * corrupting the header (in case of set-cookie2). A special
4358 * pointer, <scav> points to the beginning of the set-cookie-av
4359 * fields after the first semi-colon. The <next> pointer points
4360 * either to the end of line (set-cookie) or next unquoted comma
4361 * (set-cookie2). All of these headers are valid :
4362 *
4363 * hdr_beg hdr_end
4364 * | |
4365 * v |
4366 * NAME1 = VALUE 1 ; Secure; Path="/" |
4367 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
4368 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
4369 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
4370 * | | | | | | | |
4371 * | | | | | | | +-> next
4372 * | | | | | | +------------> scav
4373 * | | | | | +--------------> val_end
4374 * | | | | +--------------------> val_beg
4375 * | | | +----------------------> equal
4376 * | | +------------------------> att_end
4377 * | +----------------------------> att_beg
4378 * +------------------------------> prev
4379 * -------------------------------> hdr_beg
4380 */
4381 hdr_beg = ctx.value.ptr;
4382 hdr_end = hdr_beg + ctx.value.len;
4383 for (prev = hdr_beg; prev < hdr_end; prev = next) {
4384
4385 /* Iterate through all cookies on this line */
4386
4387 /* find att_beg */
4388 att_beg = prev;
4389 if (prev > hdr_beg)
4390 att_beg++;
4391
4392 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
4393 att_beg++;
4394
4395 /* find att_end : this is the first character after the last non
4396 * space before the equal. It may be equal to hdr_end.
4397 */
4398 equal = att_end = att_beg;
4399
4400 while (equal < hdr_end) {
4401 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
4402 break;
4403 if (HTTP_IS_SPHT(*equal++))
4404 continue;
4405 att_end = equal;
4406 }
4407
4408 /* here, <equal> points to '=', a delimitor or the end. <att_end>
4409 * is between <att_beg> and <equal>, both may be identical.
4410 */
4411
4412 /* look for end of cookie if there is an equal sign */
4413 if (equal < hdr_end && *equal == '=') {
4414 /* look for the beginning of the value */
4415 val_beg = equal + 1;
4416 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
4417 val_beg++;
4418
4419 /* find the end of the value, respecting quotes */
4420 next = http_find_cookie_value_end(val_beg, hdr_end);
4421
4422 /* make val_end point to the first white space or delimitor after the value */
4423 val_end = next;
4424 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
4425 val_end--;
4426 }
4427 else {
4428 /* <equal> points to next comma, semi-colon or EOL */
4429 val_beg = val_end = next = equal;
4430 }
4431
4432 if (next < hdr_end) {
4433 /* Set-Cookie2 supports multiple cookies, and <next> points to
4434 * a colon or semi-colon before the end. So skip all attr-value
4435 * pairs and look for the next comma. For Set-Cookie, since
4436 * commas are permitted in values, skip to the end.
4437 */
4438 if (is_cookie2)
4439 next = http_find_hdr_value_end(next, hdr_end);
4440 else
4441 next = hdr_end;
4442 }
4443
4444 /* Now everything is as on the diagram above */
4445
4446 /* Ignore cookies with no equal sign */
4447 if (equal == val_end)
4448 continue;
4449
4450 /* If there are spaces around the equal sign, we need to
4451 * strip them otherwise we'll get trouble for cookie captures,
4452 * or even for rewrites. Since this happens extremely rarely,
4453 * it does not hurt performance.
4454 */
4455 if (unlikely(att_end != equal || val_beg > equal + 1)) {
4456 int stripped_before = 0;
4457 int stripped_after = 0;
4458
4459 if (att_end != equal) {
4460 memmove(att_end, equal, hdr_end - equal);
4461 stripped_before = (att_end - equal);
4462 equal += stripped_before;
4463 val_beg += stripped_before;
4464 }
4465
4466 if (val_beg > equal + 1) {
4467 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
4468 stripped_after = (equal + 1) - val_beg;
4469 val_beg += stripped_after;
4470 stripped_before += stripped_after;
4471 }
4472
4473 val_end += stripped_before;
4474 next += stripped_before;
4475 hdr_end += stripped_before;
4476
Christopher Faulet6cdaf2a2019-02-12 14:29:57 +01004477 htx_set_blk_value_len(ctx.blk, hdr_end - hdr_beg);
4478 htx->data -= ctx.value.len - (hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004479 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004480 }
4481
4482 /* First, let's see if we want to capture this cookie. We check
4483 * that we don't already have a server side cookie, because we
4484 * can only capture one. Also as an optimisation, we ignore
4485 * cookies shorter than the declared name.
4486 */
4487 if (sess->fe->capture_name != NULL &&
4488 txn->srv_cookie == NULL &&
4489 (val_end - att_beg >= sess->fe->capture_namelen) &&
4490 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
4491 int log_len = val_end - att_beg;
4492 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
4493 ha_alert("HTTP logging : out of memory.\n");
4494 }
4495 else {
4496 if (log_len > sess->fe->capture_len)
4497 log_len = sess->fe->capture_len;
4498 memcpy(txn->srv_cookie, att_beg, log_len);
4499 txn->srv_cookie[log_len] = 0;
4500 }
4501 }
4502
4503 srv = objt_server(s->target);
4504 /* now check if we need to process it for persistence */
4505 if (!(s->flags & SF_IGNORE_PRST) &&
4506 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
4507 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
4508 /* assume passive cookie by default */
4509 txn->flags &= ~TX_SCK_MASK;
4510 txn->flags |= TX_SCK_FOUND;
4511
4512 /* If the cookie is in insert mode on a known server, we'll delete
4513 * this occurrence because we'll insert another one later.
4514 * We'll delete it too if the "indirect" option is set and we're in
4515 * a direct access.
4516 */
4517 if (s->be->ck_opts & PR_CK_PSV) {
4518 /* The "preserve" flag was set, we don't want to touch the
4519 * server's cookie.
4520 */
4521 }
4522 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
4523 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
4524 /* this cookie must be deleted */
4525 if (prev == hdr_beg && next == hdr_end) {
4526 /* whole header */
4527 http_remove_header(htx, &ctx);
4528 /* note: while both invalid now, <next> and <hdr_end>
4529 * are still equal, so the for() will stop as expected.
4530 */
4531 } else {
4532 /* just remove the value */
4533 int delta = htx_del_hdr_value(hdr_beg, hdr_end, &prev, next);
4534 next = prev;
4535 hdr_end += delta;
4536 }
4537 txn->flags &= ~TX_SCK_MASK;
4538 txn->flags |= TX_SCK_DELETED;
4539 /* and go on with next cookie */
4540 }
4541 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
4542 /* replace bytes val_beg->val_end with the cookie name associated
4543 * with this server since we know it.
4544 */
4545 int sliding, delta;
4546
4547 ctx.value = ist2(val_beg, val_end - val_beg);
4548 ctx.lws_before = ctx.lws_after = 0;
4549 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
4550 delta = srv->cklen - (val_end - val_beg);
4551 sliding = (ctx.value.ptr - val_beg);
4552 hdr_beg += sliding;
4553 val_beg += sliding;
4554 next += sliding + delta;
4555 hdr_end += sliding + delta;
4556
4557 txn->flags &= ~TX_SCK_MASK;
4558 txn->flags |= TX_SCK_REPLACED;
4559 }
4560 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
4561 /* insert the cookie name associated with this server
4562 * before existing cookie, and insert a delimiter between them..
4563 */
4564 int sliding, delta;
4565 ctx.value = ist2(val_beg, 0);
4566 ctx.lws_before = ctx.lws_after = 0;
4567 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
4568 delta = srv->cklen + 1;
4569 sliding = (ctx.value.ptr - val_beg);
4570 hdr_beg += sliding;
4571 val_beg += sliding;
4572 next += sliding + delta;
4573 hdr_end += sliding + delta;
4574
4575 val_beg[srv->cklen] = COOKIE_DELIM;
4576 txn->flags &= ~TX_SCK_MASK;
4577 txn->flags |= TX_SCK_REPLACED;
4578 }
4579 }
4580 /* that's done for this cookie, check the next one on the same
4581 * line when next != hdr_end (only if is_cookie2).
4582 */
4583 }
4584 }
4585}
4586
Christopher Faulet25a02f62018-10-24 12:00:25 +02004587/*
4588 * Parses the Cache-Control and Pragma request header fields to determine if
4589 * the request may be served from the cache and/or if it is cacheable. Updates
4590 * s->txn->flags.
4591 */
4592void htx_check_request_for_cacheability(struct stream *s, struct channel *req)
4593{
4594 struct http_txn *txn = s->txn;
4595 struct htx *htx;
4596 int32_t pos;
4597 int pragma_found, cc_found, i;
4598
4599 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
4600 return; /* nothing more to do here */
4601
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004602 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02004603 pragma_found = cc_found = 0;
4604 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4605 struct htx_blk *blk = htx_get_blk(htx, pos);
4606 enum htx_blk_type type = htx_get_blk_type(blk);
4607 struct ist n, v;
4608
4609 if (type == HTX_BLK_EOH)
4610 break;
4611 if (type != HTX_BLK_HDR)
4612 continue;
4613
4614 n = htx_get_blk_name(htx, blk);
4615 v = htx_get_blk_value(htx, blk);
4616
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004617 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004618 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4619 pragma_found = 1;
4620 continue;
4621 }
4622 }
4623
4624 /* Don't use the cache and don't try to store if we found the
4625 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004626 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004627 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4628 txn->flags |= TX_CACHE_IGNORE;
4629 continue;
4630 }
4631
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004632 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004633 continue;
4634
4635 /* OK, right now we know we have a cache-control header */
4636 cc_found = 1;
4637 if (!v.len) /* no info */
4638 continue;
4639
4640 i = 0;
4641 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4642 !isspace((unsigned char)*(v.ptr+i)))
4643 i++;
4644
4645 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
4646 * values after max-age, max-stale nor min-fresh, we simply don't
4647 * use the cache when they're specified.
4648 */
4649 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
4650 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4651 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
4652 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
4653 txn->flags |= TX_CACHE_IGNORE;
4654 continue;
4655 }
4656
4657 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
4658 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4659 continue;
4660 }
4661 }
4662
4663 /* RFC7234#5.4:
4664 * When the Cache-Control header field is also present and
4665 * understood in a request, Pragma is ignored.
4666 * When the Cache-Control header field is not present in a
4667 * request, caches MUST consider the no-cache request
4668 * pragma-directive as having the same effect as if
4669 * "Cache-Control: no-cache" were present.
4670 */
4671 if (!cc_found && pragma_found)
4672 txn->flags |= TX_CACHE_IGNORE;
4673}
4674
4675/*
4676 * Check if response is cacheable or not. Updates s->txn->flags.
4677 */
4678void htx_check_response_for_cacheability(struct stream *s, struct channel *res)
4679{
4680 struct http_txn *txn = s->txn;
4681 struct htx *htx;
4682 int32_t pos;
4683 int i;
4684
4685 if (txn->status < 200) {
4686 /* do not try to cache interim responses! */
4687 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4688 return;
4689 }
4690
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004691 htx = htxbuf(&res->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02004692 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4693 struct htx_blk *blk = htx_get_blk(htx, pos);
4694 enum htx_blk_type type = htx_get_blk_type(blk);
4695 struct ist n, v;
4696
4697 if (type == HTX_BLK_EOH)
4698 break;
4699 if (type != HTX_BLK_HDR)
4700 continue;
4701
4702 n = htx_get_blk_name(htx, blk);
4703 v = htx_get_blk_value(htx, blk);
4704
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004705 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004706 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4707 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4708 return;
4709 }
4710 }
4711
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004712 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004713 continue;
4714
4715 /* OK, right now we know we have a cache-control header */
4716 if (!v.len) /* no info */
4717 continue;
4718
4719 i = 0;
4720 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4721 !isspace((unsigned char)*(v.ptr+i)))
4722 i++;
4723
4724 /* we have a complete value between v.ptr and (v.ptr+i) */
4725 if (i < v.len && *(v.ptr + i) == '=') {
4726 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
4727 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
4728 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4729 continue;
4730 }
4731
4732 /* we have something of the form no-cache="set-cookie" */
4733 if ((v.len >= 21) &&
4734 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
4735 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
4736 txn->flags &= ~TX_CACHE_COOK;
4737 continue;
4738 }
4739
4740 /* OK, so we know that either p2 points to the end of string or to a comma */
4741 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
4742 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4743 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
4744 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4745 return;
4746 }
4747
4748 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4749 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4750 continue;
4751 }
4752 }
4753}
4754
Christopher Faulet64159df2018-10-24 21:15:35 +02004755/* send a server's name with an outgoing request over an established connection.
4756 * Note: this function is designed to be called once the request has been
4757 * scheduled for being forwarded. This is the reason why the number of forwarded
4758 * bytes have to be adjusted.
4759 */
4760int htx_send_name_header(struct stream *s, struct proxy *be, const char *srv_name)
4761{
4762 struct htx *htx;
4763 struct http_hdr_ctx ctx;
4764 struct ist hdr;
4765 uint32_t data;
4766
4767 hdr = ist2(be->server_id_hdr_name, be->server_id_hdr_len);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004768 htx = htxbuf(&s->req.buf);
Christopher Faulet64159df2018-10-24 21:15:35 +02004769 data = htx->data;
4770
4771 ctx.blk = NULL;
4772 while (http_find_header(htx, hdr, &ctx, 1))
4773 http_remove_header(htx, &ctx);
4774 http_add_header(htx, hdr, ist2(srv_name, strlen(srv_name)));
4775
4776 if (co_data(&s->req)) {
4777 if (data >= htx->data)
4778 c_rew(&s->req, data - htx->data);
4779 else
4780 c_adv(&s->req, htx->data - data);
4781 }
4782 return 0;
4783}
4784
Christopher Faulet377c5a52018-10-24 21:21:30 +02004785/*
4786 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4787 * for the current backend.
4788 *
4789 * It is assumed that the request is either a HEAD, GET, or POST and that the
4790 * uri_auth field is valid.
4791 *
4792 * Returns 1 if stats should be provided, otherwise 0.
4793 */
4794static int htx_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
4795{
4796 struct uri_auth *uri_auth = backend->uri_auth;
4797 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004798 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004799 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004800
4801 if (!uri_auth)
4802 return 0;
4803
4804 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4805 return 0;
4806
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004807 htx = htxbuf(&s->req.buf);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004808 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004809 uri = htx_sl_req_uri(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004810
4811 /* check URI size */
4812 if (uri_auth->uri_len > uri.len)
4813 return 0;
4814
4815 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4816 return 0;
4817
4818 return 1;
4819}
4820
4821/* This function prepares an applet to handle the stats. It can deal with the
4822 * "100-continue" expectation, check that admin rules are met for POST requests,
4823 * and program a response message if something was unexpected. It cannot fail
4824 * and always relies on the stats applet to complete the job. It does not touch
4825 * analysers nor counters, which are left to the caller. It does not touch
4826 * s->target which is supposed to already point to the stats applet. The caller
4827 * is expected to have already assigned an appctx to the stream.
4828 */
4829static int htx_handle_stats(struct stream *s, struct channel *req)
4830{
4831 struct stats_admin_rule *stats_admin_rule;
4832 struct stream_interface *si = &s->si[1];
4833 struct session *sess = s->sess;
4834 struct http_txn *txn = s->txn;
4835 struct http_msg *msg = &txn->req;
4836 struct uri_auth *uri_auth = s->be->uri_auth;
4837 const char *h, *lookup, *end;
4838 struct appctx *appctx;
4839 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004840 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004841
4842 appctx = si_appctx(si);
4843 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4844 appctx->st1 = appctx->st2 = 0;
4845 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
4846 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4847 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4848 appctx->ctx.stats.flags |= STAT_CHUNKED;
4849
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004850 htx = htxbuf(&req->buf);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004851 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004852 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4853 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004854
4855 for (h = lookup; h <= end - 3; h++) {
4856 if (memcmp(h, ";up", 3) == 0) {
4857 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4858 break;
4859 }
4860 }
4861
4862 if (uri_auth->refresh) {
4863 for (h = lookup; h <= end - 10; h++) {
4864 if (memcmp(h, ";norefresh", 10) == 0) {
4865 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4866 break;
4867 }
4868 }
4869 }
4870
4871 for (h = lookup; h <= end - 4; h++) {
4872 if (memcmp(h, ";csv", 4) == 0) {
4873 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
4874 break;
4875 }
4876 }
4877
4878 for (h = lookup; h <= end - 6; h++) {
4879 if (memcmp(h, ";typed", 6) == 0) {
4880 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
4881 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4882 break;
4883 }
4884 }
4885
4886 for (h = lookup; h <= end - 8; h++) {
4887 if (memcmp(h, ";st=", 4) == 0) {
4888 int i;
4889 h += 4;
4890 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4891 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4892 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4893 appctx->ctx.stats.st_code = i;
4894 break;
4895 }
4896 }
4897 break;
4898 }
4899 }
4900
4901 appctx->ctx.stats.scope_str = 0;
4902 appctx->ctx.stats.scope_len = 0;
4903 for (h = lookup; h <= end - 8; h++) {
4904 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4905 int itx = 0;
4906 const char *h2;
4907 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4908 const char *err;
4909
4910 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4911 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004912 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4913 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004914 if (*h == ';' || *h == '&' || *h == ' ')
4915 break;
4916 itx++;
4917 h++;
4918 }
4919
4920 if (itx > STAT_SCOPE_TXT_MAXLEN)
4921 itx = STAT_SCOPE_TXT_MAXLEN;
4922 appctx->ctx.stats.scope_len = itx;
4923
4924 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4925 memcpy(scope_txt, h2, itx);
4926 scope_txt[itx] = '\0';
4927 err = invalid_char(scope_txt);
4928 if (err) {
4929 /* bad char in search text => clear scope */
4930 appctx->ctx.stats.scope_str = 0;
4931 appctx->ctx.stats.scope_len = 0;
4932 }
4933 break;
4934 }
4935 }
4936
4937 /* now check whether we have some admin rules for this request */
4938 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4939 int ret = 1;
4940
4941 if (stats_admin_rule->cond) {
4942 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4943 ret = acl_pass(ret);
4944 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4945 ret = !ret;
4946 }
4947
4948 if (ret) {
4949 /* no rule, or the rule matches */
4950 appctx->ctx.stats.flags |= STAT_ADMIN;
4951 break;
4952 }
4953 }
4954
Christopher Faulet5d45e382019-02-27 15:15:23 +01004955 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4956 appctx->st0 = STAT_HTTP_HEAD;
4957 else if (txn->meth == HTTP_METH_POST) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004958 if (appctx->ctx.stats.flags & STAT_ADMIN) {
4959 /* we'll need the request body, possibly after sending 100-continue */
4960 if (msg->msg_state < HTTP_MSG_DATA)
4961 req->analysers |= AN_REQ_HTTP_BODY;
4962 appctx->st0 = STAT_HTTP_POST;
4963 }
4964 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004965 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004966 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4967 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4968 appctx->st0 = STAT_HTTP_LAST;
4969 }
4970 }
4971 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004972 /* Unsupported method */
4973 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4974 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4975 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004976 }
4977
4978 s->task->nice = -32; /* small boost for HTTP statistics */
4979 return 1;
4980}
4981
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004982void htx_perform_server_redirect(struct stream *s, struct stream_interface *si)
4983{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004984 struct channel *req = &s->req;
4985 struct channel *res = &s->res;
4986 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004987 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004988 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004989 struct ist path, location;
4990 unsigned int flags;
4991 size_t data;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004992
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004993 /*
4994 * Create the location
4995 */
4996 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004997
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004998 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004999 /* special prefix "/" means don't change URL */
5000 srv = __objt_server(s->target);
5001 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
5002 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
5003 return;
5004 }
5005
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01005006 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01005007 htx = htxbuf(&req->buf);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02005008 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005009 path = http_get_path(htx_sl_req_uri(sl));
Christopher Fauletfefc73d2018-10-24 21:18:04 +02005010 if (!path.ptr)
5011 return;
5012
5013 if (!chunk_memcat(&trash, path.ptr, path.len))
5014 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01005015 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02005016
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01005017 /*
5018 * Create the 302 respone
5019 */
5020 htx = htx_from_buf(&res->buf);
5021 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
5022 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5023 ist("HTTP/1.1"), ist("302"), ist("Found"));
5024 if (!sl)
5025 goto fail;
5026 sl->info.res.status = 302;
5027 s->txn->status = 302;
5028
5029 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
5030 !htx_add_header(htx, ist("Connection"), ist("close")) ||
5031 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
5032 !htx_add_header(htx, ist("Location"), location))
5033 goto fail;
5034
5035 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
5036 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02005037
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01005038 /*
5039 * Send the message
5040 */
5041 data = htx->data - co_data(res);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01005042 c_adv(res, data);
5043 res->total += data;
5044
5045 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02005046 si_shutr(si);
5047 si_shutw(si);
5048 si->err_type = SI_ET_NONE;
5049 si->state = SI_ST_CLO;
5050
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01005051 channel_auto_read(req);
5052 channel_abort(req);
5053 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005054 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01005055 channel_auto_read(res);
5056 channel_auto_close(res);
5057
5058 if (!(s->flags & SF_ERR_MASK))
5059 s->flags |= SF_ERR_LOCAL;
5060 if (!(s->flags & SF_FINST_MASK))
5061 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02005062
5063 /* FIXME: we should increase a counter of redirects per server and per backend. */
5064 srv_inc_sess_ctr(srv);
5065 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01005066 return;
5067
5068 fail:
5069 /* If an error occurred, remove the incomplete HTTP response from the
5070 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005071 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02005072}
5073
Christopher Fauletf2824e62018-10-01 12:12:37 +02005074/* This function terminates the request because it was completly analyzed or
5075 * because an error was triggered during the body forwarding.
5076 */
5077static void htx_end_request(struct stream *s)
5078{
5079 struct channel *chn = &s->req;
5080 struct http_txn *txn = s->txn;
5081
5082 DPRINTF(stderr,"[%u] %s: stream=%p states=%s,%s req->analysers=0x%08x res->analysers=0x%08x\n",
5083 now_ms, __FUNCTION__, s,
5084 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
5085 s->req.analysers, s->res.analysers);
5086
Christopher Fauletb42a8b62018-11-19 21:59:00 +01005087 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
5088 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02005089 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005090 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02005091 goto end;
5092 }
5093
5094 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
5095 return;
5096
5097 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02005098 /* No need to read anymore, the request was completely parsed.
5099 * We can shut the read side unless we want to abort_on_close,
5100 * or we have a POST request. The issue with POST requests is
5101 * that some browsers still send a CRLF after the request, and
5102 * this CRLF must be read so that it does not remain in the kernel
5103 * buffers, otherwise a close could cause an RST on some systems
5104 * (eg: Linux).
5105 */
5106 if ((!(s->be->options & PR_O_ABRT_CLOSE) || (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
5107 txn->meth != HTTP_METH_POST)
5108 channel_dont_read(chn);
5109
5110 /* if the server closes the connection, we want to immediately react
5111 * and close the socket to save packets and syscalls.
5112 */
5113 s->si[1].flags |= SI_FL_NOHALF;
5114
5115 /* In any case we've finished parsing the request so we must
5116 * disable Nagle when sending data because 1) we're not going
5117 * to shut this side, and 2) the server is waiting for us to
5118 * send pending data.
5119 */
5120 chn->flags |= CF_NEVER_WAIT;
5121
Christopher Fauletd01ce402019-01-02 17:44:13 +01005122 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5123 /* The server has not finished to respond, so we
5124 * don't want to move in order not to upset it.
5125 */
5126 return;
5127 }
5128
Christopher Fauletf2824e62018-10-01 12:12:37 +02005129 /* When we get here, it means that both the request and the
5130 * response have finished receiving. Depending on the connection
5131 * mode, we'll have to wait for the last bytes to leave in either
5132 * direction, and sometimes for a close to be effective.
5133 */
5134 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5135 /* Tunnel mode will not have any analyser so it needs to
5136 * poll for reads.
5137 */
5138 channel_auto_read(chn);
Christopher Faulet9768c262018-10-22 09:34:31 +02005139 if (b_data(&chn->buf))
5140 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02005141 txn->req.msg_state = HTTP_MSG_TUNNEL;
5142 }
5143 else {
5144 /* we're not expecting any new data to come for this
5145 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02005146 *
5147 * However, there is an exception if the response
5148 * length is undefined. In this case, we need to wait
5149 * the close from the server. The response will be
5150 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02005151 */
5152 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
5153 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02005154 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02005155
5156 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5157 channel_shutr_now(chn);
5158 channel_shutw_now(chn);
5159 }
5160 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02005161 goto check_channel_flags;
5162 }
5163
5164 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5165 http_msg_closing:
5166 /* nothing else to forward, just waiting for the output buffer
5167 * to be empty and for the shutw_now to take effect.
5168 */
5169 if (channel_is_empty(chn)) {
5170 txn->req.msg_state = HTTP_MSG_CLOSED;
5171 goto http_msg_closed;
5172 }
5173 else if (chn->flags & CF_SHUTW) {
5174 txn->req.err_state = txn->req.msg_state;
5175 txn->req.msg_state = HTTP_MSG_ERROR;
5176 goto end;
5177 }
5178 return;
5179 }
5180
5181 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5182 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02005183 /* if we don't know whether the server will close, we need to hard close */
5184 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
5185 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02005186 /* see above in MSG_DONE why we only do this in these states */
5187 if ((!(s->be->options & PR_O_ABRT_CLOSE) || (s->si[0].flags & SI_FL_CLEAN_ABRT)))
5188 channel_dont_read(chn);
5189 goto end;
5190 }
5191
5192 check_channel_flags:
5193 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
5194 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
5195 /* if we've just closed an output, let's switch */
5196 txn->req.msg_state = HTTP_MSG_CLOSING;
5197 goto http_msg_closing;
5198 }
5199
5200 end:
5201 chn->analysers &= AN_REQ_FLT_END;
5202 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
5203 chn->analysers |= AN_REQ_FLT_XFER_DATA;
5204 channel_auto_close(chn);
5205 channel_auto_read(chn);
5206}
5207
5208
5209/* This function terminates the response because it was completly analyzed or
5210 * because an error was triggered during the body forwarding.
5211 */
5212static void htx_end_response(struct stream *s)
5213{
5214 struct channel *chn = &s->res;
5215 struct http_txn *txn = s->txn;
5216
5217 DPRINTF(stderr,"[%u] %s: stream=%p states=%s,%s req->analysers=0x%08x res->analysers=0x%08x\n",
5218 now_ms, __FUNCTION__, s,
5219 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
5220 s->req.analysers, s->res.analysers);
5221
Christopher Fauletb42a8b62018-11-19 21:59:00 +01005222 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
5223 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005224 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02005225 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005226 goto end;
5227 }
5228
5229 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
5230 return;
5231
5232 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5233 /* In theory, we don't need to read anymore, but we must
5234 * still monitor the server connection for a possible close
5235 * while the request is being uploaded, so we don't disable
5236 * reading.
5237 */
5238 /* channel_dont_read(chn); */
5239
5240 if (txn->req.msg_state < HTTP_MSG_DONE) {
5241 /* The client seems to still be sending data, probably
5242 * because we got an error response during an upload.
5243 * We have the choice of either breaking the connection
5244 * or letting it pass through. Let's do the later.
5245 */
5246 return;
5247 }
5248
5249 /* When we get here, it means that both the request and the
5250 * response have finished receiving. Depending on the connection
5251 * mode, we'll have to wait for the last bytes to leave in either
5252 * direction, and sometimes for a close to be effective.
5253 */
5254 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5255 channel_auto_read(chn);
5256 chn->flags |= CF_NEVER_WAIT;
Christopher Faulet9768c262018-10-22 09:34:31 +02005257 if (b_data(&chn->buf))
5258 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02005259 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
5260 }
5261 else {
5262 /* we're not expecting any new data to come for this
5263 * transaction, so we can close it.
5264 */
5265 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5266 channel_shutr_now(chn);
5267 channel_shutw_now(chn);
5268 }
5269 }
5270 goto check_channel_flags;
5271 }
5272
5273 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5274 http_msg_closing:
5275 /* nothing else to forward, just waiting for the output buffer
5276 * to be empty and for the shutw_now to take effect.
5277 */
5278 if (channel_is_empty(chn)) {
5279 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5280 goto http_msg_closed;
5281 }
5282 else if (chn->flags & CF_SHUTW) {
5283 txn->rsp.err_state = txn->rsp.msg_state;
5284 txn->rsp.msg_state = HTTP_MSG_ERROR;
Olivier Houcharda798bf52019-03-08 18:52:00 +01005285 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005286 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01005287 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005288 goto end;
5289 }
5290 return;
5291 }
5292
5293 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5294 http_msg_closed:
5295 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005296 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02005297 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005298 goto end;
5299 }
5300
5301 check_channel_flags:
5302 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
5303 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
5304 /* if we've just closed an output, let's switch */
5305 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5306 goto http_msg_closing;
5307 }
5308
5309 end:
5310 chn->analysers &= AN_RES_FLT_END;
5311 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
5312 chn->analysers |= AN_RES_FLT_XFER_DATA;
5313 channel_auto_close(chn);
5314 channel_auto_read(chn);
5315}
5316
Christopher Faulet0f226952018-10-22 09:29:56 +02005317void htx_server_error(struct stream *s, struct stream_interface *si, int err,
5318 int finst, const struct buffer *msg)
5319{
5320 channel_auto_read(si_oc(si));
5321 channel_abort(si_oc(si));
5322 channel_auto_close(si_oc(si));
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005323 channel_htx_erase(si_oc(si), htxbuf(&(si_oc(si))->buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02005324 channel_auto_close(si_ic(si));
5325 channel_auto_read(si_ic(si));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005326
5327 /* <msg> is an HTX structure. So we copy it in the response's
5328 * channel */
Christopher Faulet0f226952018-10-22 09:29:56 +02005329 if (msg) {
5330 struct channel *chn = si_ic(si);
5331 struct htx *htx;
5332
Christopher Fauletaed82cf2018-11-30 22:22:32 +01005333 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005334 chn->buf.data = msg->data;
5335 memcpy(chn->buf.area, msg->area, msg->data);
5336 htx = htx_from_buf(&chn->buf);
Christopher Faulet0f226952018-10-22 09:29:56 +02005337 c_adv(chn, htx->data);
5338 chn->total += htx->data;
5339 }
5340 if (!(s->flags & SF_ERR_MASK))
5341 s->flags |= err;
5342 if (!(s->flags & SF_FINST_MASK))
5343 s->flags |= finst;
5344}
5345
5346void htx_reply_and_close(struct stream *s, short status, struct buffer *msg)
5347{
5348 channel_auto_read(&s->req);
5349 channel_abort(&s->req);
5350 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005351 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
5352 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02005353
5354 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005355
5356 /* <msg> is an HTX structure. So we copy it in the response's
5357 * channel */
5358 /* FIXME: It is a problem for now if there is some outgoing data */
Christopher Faulet0f226952018-10-22 09:29:56 +02005359 if (msg) {
5360 struct channel *chn = &s->res;
5361 struct htx *htx;
5362
Christopher Fauletaed82cf2018-11-30 22:22:32 +01005363 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005364 chn->buf.data = msg->data;
5365 memcpy(chn->buf.area, msg->area, msg->data);
5366 htx = htx_from_buf(&chn->buf);
Christopher Faulet0f226952018-10-22 09:29:56 +02005367 c_adv(chn, htx->data);
5368 chn->total += htx->data;
5369 }
5370
5371 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
5372 channel_auto_read(&s->res);
5373 channel_auto_close(&s->res);
5374 channel_shutr_now(&s->res);
5375}
5376
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005377struct buffer *htx_error_message(struct stream *s)
5378{
5379 const int msgnum = http_get_status_idx(s->txn->status);
5380
5381 if (s->be->errmsg[msgnum].area)
5382 return &s->be->errmsg[msgnum];
5383 else if (strm_fe(s)->errmsg[msgnum].area)
5384 return &strm_fe(s)->errmsg[msgnum];
5385 else
5386 return &htx_err_chunks[msgnum];
5387}
5388
5389
Christopher Faulet23a3c792018-11-28 10:01:23 +01005390/* Send a 100-Continue response to the client. It returns 0 on success and -1
5391 * on error. The response channel is updated accordingly.
5392 */
5393static int htx_reply_100_continue(struct stream *s)
5394{
5395 struct channel *res = &s->res;
5396 struct htx *htx = htx_from_buf(&res->buf);
5397 struct htx_sl *sl;
5398 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
5399 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
5400 size_t data;
5401
5402 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5403 ist("HTTP/1.1"), ist("100"), ist("Continue"));
5404 if (!sl)
5405 goto fail;
5406 sl->info.res.status = 100;
5407
5408 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
5409 goto fail;
5410
5411 data = htx->data - co_data(res);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005412 c_adv(res, data);
5413 res->total += data;
5414 return 0;
5415
5416 fail:
5417 /* If an error occurred, remove the incomplete HTTP response from the
5418 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005419 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005420 return -1;
5421}
5422
Christopher Faulet12c51e22018-11-28 15:59:42 +01005423
5424/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
5425 * ont whether we use a proxy or not. It returns 0 on success and -1 on
5426 * error. The response channel is updated accordingly.
5427 */
5428static int htx_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
5429{
5430 struct channel *res = &s->res;
5431 struct htx *htx = htx_from_buf(&res->buf);
5432 struct htx_sl *sl;
5433 struct ist code, body;
5434 int status;
5435 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5436 size_t data;
5437
5438 if (!(s->txn->flags & TX_USE_PX_CONN)) {
5439 status = 401;
5440 code = ist("401");
5441 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
5442 "You need a valid user and password to access this content.\n"
5443 "</body></html>\n");
5444 }
5445 else {
5446 status = 407;
5447 code = ist("407");
5448 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
5449 "You need a valid user and password to access this content.\n"
5450 "</body></html>\n");
5451 }
5452
5453 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5454 ist("HTTP/1.1"), code, ist("Unauthorized"));
5455 if (!sl)
5456 goto fail;
5457 sl->info.res.status = status;
5458 s->txn->status = status;
5459
5460 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
5461 goto fail;
5462
5463 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
5464 !htx_add_header(htx, ist("Connection"), ist("close")) ||
Jérôme Magnin86cef232018-12-28 14:49:08 +01005465 !htx_add_header(htx, ist("Content-Type"), ist("text/html")))
5466 goto fail;
5467 if (status == 401 && !htx_add_header(htx, ist("WWW-Authenticate"), ist2(trash.area, trash.data)))
5468 goto fail;
5469 if (status == 407 && !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005470 goto fail;
Christopher Faulet12c51e22018-11-28 15:59:42 +01005471 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_data(htx, body) || !htx_add_endof(htx, HTX_BLK_EOM))
5472 goto fail;
5473
5474 data = htx->data - co_data(res);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005475 c_adv(res, data);
5476 res->total += data;
5477
5478 channel_auto_read(&s->req);
5479 channel_abort(&s->req);
5480 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005481 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet12c51e22018-11-28 15:59:42 +01005482
5483 res->wex = tick_add_ifset(now_ms, res->wto);
5484 channel_auto_read(res);
5485 channel_auto_close(res);
5486 channel_shutr_now(res);
5487 return 0;
5488
5489 fail:
5490 /* If an error occurred, remove the incomplete HTTP response from the
5491 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005492 channel_htx_truncate(res, htx);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005493 return -1;
5494}
5495
Christopher Faulet0f226952018-10-22 09:29:56 +02005496/*
5497 * Capture headers from message <htx> according to header list <cap_hdr>, and
5498 * fill the <cap> pointers appropriately.
5499 */
5500static void htx_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
5501{
5502 struct cap_hdr *h;
5503 int32_t pos;
5504
5505 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
5506 struct htx_blk *blk = htx_get_blk(htx, pos);
5507 enum htx_blk_type type = htx_get_blk_type(blk);
5508 struct ist n, v;
5509
5510 if (type == HTX_BLK_EOH)
5511 break;
5512 if (type != HTX_BLK_HDR)
5513 continue;
5514
5515 n = htx_get_blk_name(htx, blk);
5516
5517 for (h = cap_hdr; h; h = h->next) {
5518 if (h->namelen && (h->namelen == n.len) &&
5519 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
5520 if (cap[h->index] == NULL)
5521 cap[h->index] =
5522 pool_alloc(h->pool);
5523
5524 if (cap[h->index] == NULL) {
5525 ha_alert("HTTP capture : out of memory.\n");
5526 break;
5527 }
5528
5529 v = htx_get_blk_value(htx, blk);
5530 if (v.len > h->len)
5531 v.len = h->len;
5532
5533 memcpy(cap[h->index], v.ptr, v.len);
5534 cap[h->index][v.len]=0;
5535 }
5536 }
5537 }
5538}
5539
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005540/* Delete a value in a header between delimiters <from> and <next>. The header
5541 * itself is delimited by <start> and <end> pointers. The number of characters
5542 * displaced is returned, and the pointer to the first delimiter is updated if
5543 * required. The function tries as much as possible to respect the following
5544 * principles :
5545 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5546 * in which case <next> is simply removed
5547 * - set exactly one space character after the new first delimiter, unless there
5548 * are not enough characters in the block being moved to do so.
5549 * - remove unneeded spaces before the previous delimiter and after the new
5550 * one.
5551 *
5552 * It is the caller's responsibility to ensure that :
5553 * - <from> points to a valid delimiter or <start> ;
5554 * - <next> points to a valid delimiter or <end> ;
5555 * - there are non-space chars before <from>.
5556 */
5557static int htx_del_hdr_value(char *start, char *end, char **from, char *next)
5558{
5559 char *prev = *from;
5560
5561 if (prev == start) {
5562 /* We're removing the first value. eat the semicolon, if <next>
5563 * is lower than <end> */
5564 if (next < end)
5565 next++;
5566
5567 while (next < end && HTTP_IS_SPHT(*next))
5568 next++;
5569 }
5570 else {
5571 /* Remove useless spaces before the old delimiter. */
5572 while (HTTP_IS_SPHT(*(prev-1)))
5573 prev--;
5574 *from = prev;
5575
5576 /* copy the delimiter and if possible a space if we're
5577 * not at the end of the line.
5578 */
5579 if (next < end) {
5580 *prev++ = *next++;
5581 if (prev + 1 < next)
5582 *prev++ = ' ';
5583 while (next < end && HTTP_IS_SPHT(*next))
5584 next++;
5585 }
5586 }
5587 memmove(prev, next, end - next);
5588 return (prev - next);
5589}
5590
Christopher Faulet0f226952018-10-22 09:29:56 +02005591
5592/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005593 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005594 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005595static size_t htx_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005596{
5597 struct ist dst = ist2(str, 0);
5598
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005599 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005600 goto end;
5601 if (dst.len + 1 > len)
5602 goto end;
5603 dst.ptr[dst.len++] = ' ';
5604
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005605 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005606 goto end;
5607 if (dst.len + 1 > len)
5608 goto end;
5609 dst.ptr[dst.len++] = ' ';
5610
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005611 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005612 end:
5613 return dst.len;
5614}
5615
Christopher Fauletf0523542018-10-24 11:06:58 +02005616/* Formats the start line of the response (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005617 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Fauletf0523542018-10-24 11:06:58 +02005618 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005619static size_t htx_fmt_res_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Fauletf0523542018-10-24 11:06:58 +02005620{
5621 struct ist dst = ist2(str, 0);
5622
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005623 if (istcat(&dst, htx_sl_res_vsn(sl), len) == -1)
Christopher Fauletf0523542018-10-24 11:06:58 +02005624 goto end;
5625 if (dst.len + 1 > len)
5626 goto end;
5627 dst.ptr[dst.len++] = ' ';
5628
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005629 if (istcat(&dst, htx_sl_res_code(sl), len) == -1)
Christopher Fauletf0523542018-10-24 11:06:58 +02005630 goto end;
5631 if (dst.len + 1 > len)
5632 goto end;
5633 dst.ptr[dst.len++] = ' ';
5634
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005635 istcat(&dst, htx_sl_res_reason(sl), len);
Christopher Fauletf0523542018-10-24 11:06:58 +02005636 end:
5637 return dst.len;
5638}
5639
5640
Christopher Faulet0f226952018-10-22 09:29:56 +02005641/*
5642 * Print a debug line with a start line.
5643 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005644static void htx_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005645{
5646 struct session *sess = strm_sess(s);
5647 int max;
5648
5649 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5650 dir,
5651 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5652 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5653
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005654 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005655 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005656 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005657 trash.area[trash.data++] = ' ';
5658
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005659 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005660 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005661 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005662 trash.area[trash.data++] = ' ';
5663
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005664 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005665 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005666 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005667 trash.area[trash.data++] = '\n';
5668
5669 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5670}
5671
5672/*
5673 * Print a debug line with a header.
5674 */
5675static void htx_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v)
5676{
5677 struct session *sess = strm_sess(s);
5678 int max;
5679
5680 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5681 dir,
5682 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5683 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5684
5685 max = n.len;
5686 UBOUND(max, trash.size - trash.data - 3);
5687 chunk_memcat(&trash, n.ptr, max);
5688 trash.area[trash.data++] = ':';
5689 trash.area[trash.data++] = ' ';
5690
5691 max = v.len;
5692 UBOUND(max, trash.size - trash.data - 1);
5693 chunk_memcat(&trash, v.ptr, max);
5694 trash.area[trash.data++] = '\n';
5695
5696 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5697}
5698
5699
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005700__attribute__((constructor))
5701static void __htx_protocol_init(void)
5702{
5703}
5704
5705
5706/*
5707 * Local variables:
5708 * c-indent-level: 8
5709 * c-basic-offset: 8
5710 * End:
5711 */