blob: ee9a2716f208389160ed8b2dd9ad96596588016e [file] [log] [blame]
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Christopher Faulete0768eb2018-10-03 16:38:02 +020013#include <common/base64.h>
14#include <common/config.h>
15#include <common/debug.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010016#include <common/htx.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020017#include <common/uri_auth.h>
18
Christopher Faulet0f226952018-10-22 09:29:56 +020019#include <types/capture.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020020
21#include <proto/acl.h>
Christopher Faulet3e964192018-10-24 11:39:23 +020022#include <proto/action.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020023#include <proto/channel.h>
24#include <proto/checks.h>
25#include <proto/connection.h>
26#include <proto/filters.h>
27#include <proto/hdr_idx.h>
Christopher Faulet0f226952018-10-22 09:29:56 +020028#include <proto/http_htx.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020029#include <proto/log.h>
Christopher Faulet3e964192018-10-24 11:39:23 +020030#include <proto/pattern.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020031#include <proto/proto_http.h>
32#include <proto/proxy.h>
Christopher Fauletfefc73d2018-10-24 21:18:04 +020033#include <proto/server.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020034#include <proto/stream.h>
35#include <proto/stream_interface.h>
36#include <proto/stats.h>
37
Christopher Faulet377c5a52018-10-24 21:21:30 +020038extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020039
40static void htx_end_request(struct stream *s);
41static void htx_end_response(struct stream *s);
42
Christopher Faulet0f226952018-10-22 09:29:56 +020043static void htx_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
Christopher Faulet0b6bdc52018-10-24 11:05:36 +020044static int htx_del_hdr_value(char *start, char *end, char **from, char *next);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010045static size_t htx_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
46static size_t htx_fmt_res_line(const struct htx_sl *sl, char *str, size_t len);
47static void htx_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
Christopher Faulet0f226952018-10-22 09:29:56 +020048static void htx_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
49
Christopher Faulet3e964192018-10-24 11:39:23 +020050static enum rule_result htx_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status);
51static enum rule_result htx_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
52
Christopher Faulet33640082018-10-24 11:53:01 +020053static int htx_apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px);
54static int htx_apply_filters_to_response(struct stream *s, struct channel *res, struct proxy *px);
55
Christopher Fauletfcda7c62018-10-24 11:56:22 +020056static void htx_manage_client_side_cookies(struct stream *s, struct channel *req);
57static void htx_manage_server_side_cookies(struct stream *s, struct channel *res);
58
Christopher Faulet377c5a52018-10-24 21:21:30 +020059static int htx_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
60static int htx_handle_stats(struct stream *s, struct channel *req);
61
Christopher Faulet4a28a532019-03-01 11:19:40 +010062static int htx_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
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 Faulet0ef372a2019-04-08 10:57:20 +0200148 if (htx->flags & HTX_FL_UPGRADE)
149 goto failed_keep_alive;
150
Christopher Faulet9768c262018-10-22 09:34:31 +0200151 /* 1: have we encountered a read error ? */
152 if (req->flags & CF_READ_ERROR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200153 if (!(s->flags & SF_ERR_MASK))
154 s->flags |= SF_ERR_CLICL;
155
156 if (txn->flags & TX_WAIT_NEXT_RQ)
157 goto failed_keep_alive;
158
159 if (sess->fe->options & PR_O_IGNORE_PRB)
160 goto failed_keep_alive;
161
Christopher Faulet9768c262018-10-22 09:34:31 +0200162 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200163 stream_inc_http_req_ctr(s);
164 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100165 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200166 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100167 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200168
Christopher Faulet9768c262018-10-22 09:34:31 +0200169 txn->status = 400;
170 msg->err_state = msg->msg_state;
171 msg->msg_state = HTTP_MSG_ERROR;
172 htx_reply_and_close(s, txn->status, NULL);
173 req->analysers &= AN_REQ_FLT_END;
174
Christopher Faulete0768eb2018-10-03 16:38:02 +0200175 if (!(s->flags & SF_FINST_MASK))
176 s->flags |= SF_FINST_R;
177 return 0;
178 }
179
Christopher Faulet9768c262018-10-22 09:34:31 +0200180 /* 2: has the read timeout expired ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200181 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
182 if (!(s->flags & SF_ERR_MASK))
183 s->flags |= SF_ERR_CLITO;
184
185 if (txn->flags & TX_WAIT_NEXT_RQ)
186 goto failed_keep_alive;
187
188 if (sess->fe->options & PR_O_IGNORE_PRB)
189 goto failed_keep_alive;
190
Christopher Faulet9768c262018-10-22 09:34:31 +0200191 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200192 stream_inc_http_req_ctr(s);
193 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100194 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200195 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100196 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200197
Christopher Faulet9768c262018-10-22 09:34:31 +0200198 txn->status = 408;
199 msg->err_state = msg->msg_state;
200 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100201 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200202 req->analysers &= AN_REQ_FLT_END;
203
Christopher Faulete0768eb2018-10-03 16:38:02 +0200204 if (!(s->flags & SF_FINST_MASK))
205 s->flags |= SF_FINST_R;
206 return 0;
207 }
208
Christopher Faulet9768c262018-10-22 09:34:31 +0200209 /* 3: have we encountered a close ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200210 else if (req->flags & CF_SHUTR) {
211 if (!(s->flags & SF_ERR_MASK))
212 s->flags |= SF_ERR_CLICL;
213
214 if (txn->flags & TX_WAIT_NEXT_RQ)
215 goto failed_keep_alive;
216
217 if (sess->fe->options & PR_O_IGNORE_PRB)
218 goto failed_keep_alive;
219
Christopher Faulete0768eb2018-10-03 16:38:02 +0200220 stream_inc_http_err_ctr(s);
221 stream_inc_http_req_ctr(s);
222 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100223 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200224 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100225 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200226
Christopher Faulet9768c262018-10-22 09:34:31 +0200227 txn->status = 400;
228 msg->err_state = msg->msg_state;
229 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100230 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200231 req->analysers &= AN_REQ_FLT_END;
232
Christopher Faulete0768eb2018-10-03 16:38:02 +0200233 if (!(s->flags & SF_FINST_MASK))
234 s->flags |= SF_FINST_R;
235 return 0;
236 }
237
238 channel_dont_connect(req);
239 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
240 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100241
Christopher Faulet9768c262018-10-22 09:34:31 +0200242 if (sess->listener->options & LI_O_NOQUICKACK && htx_is_not_empty(htx) &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200243 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
244 /* We need more data, we have to re-enable quick-ack in case we
245 * previously disabled it, otherwise we might cause the client
246 * to delay next data.
247 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100248 conn_set_quickack(objt_conn(sess->origin), 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200249 }
Willy Tarreau1a18b542018-12-11 16:37:42 +0100250
Christopher Faulet47365272018-10-31 17:40:50 +0100251 if ((req->flags & CF_READ_PARTIAL) && (txn->flags & TX_WAIT_NEXT_RQ)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200252 /* If the client starts to talk, let's fall back to
253 * request timeout processing.
254 */
255 txn->flags &= ~TX_WAIT_NEXT_RQ;
256 req->analyse_exp = TICK_ETERNITY;
257 }
258
259 /* just set the request timeout once at the beginning of the request */
260 if (!tick_isset(req->analyse_exp)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100261 if ((txn->flags & TX_WAIT_NEXT_RQ) && tick_isset(s->be->timeout.httpka))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200262 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
263 else
264 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
265 }
266
267 /* we're not ready yet */
268 return 0;
269
270 failed_keep_alive:
271 /* Here we process low-level errors for keep-alive requests. In
272 * short, if the request is not the first one and it experiences
273 * a timeout, read error or shutdown, we just silently close so
274 * that the client can try again.
275 */
276 txn->status = 0;
277 msg->msg_state = HTTP_MSG_RQBEFORE;
278 req->analysers &= AN_REQ_FLT_END;
279 s->logs.logwait = 0;
280 s->logs.level = 0;
281 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Faulet9768c262018-10-22 09:34:31 +0200282 htx_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200283 return 0;
284 }
285
Christopher Faulet9768c262018-10-22 09:34:31 +0200286 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200287 stream_inc_http_req_ctr(s);
288 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
289
Christopher Faulet9768c262018-10-22 09:34:31 +0200290 /* kill the pending keep-alive timeout */
291 txn->flags &= ~TX_WAIT_NEXT_RQ;
292 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200293
Christopher Faulet03599112018-11-27 11:21:21 +0100294 sl = http_find_stline(htx);
295
Christopher Faulet9768c262018-10-22 09:34:31 +0200296 /* 0: we might have to print this header in debug mode */
297 if (unlikely((global.mode & MODE_DEBUG) &&
298 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
299 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200300
Christopher Faulet03599112018-11-27 11:21:21 +0100301 htx_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200302
303 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
304 struct htx_blk *blk = htx_get_blk(htx, pos);
305 enum htx_blk_type type = htx_get_blk_type(blk);
306
307 if (type == HTX_BLK_EOH)
308 break;
309 if (type != HTX_BLK_HDR)
310 continue;
311
312 htx_debug_hdr("clihdr", s,
313 htx_get_blk_name(htx, blk),
314 htx_get_blk_value(htx, blk));
315 }
316 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200317
318 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100319 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200320 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100321 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100322 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200323 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100324 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +0100325 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100326 if (sl->flags & HTX_SL_F_BODYLESS)
327 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200328
329 /* we can make use of server redirect on GET and HEAD */
330 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
331 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100332 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200333 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200334 goto return_bad_req;
335 }
336
337 /*
338 * 2: check if the URI matches the monitor_uri.
339 * We have to do this for every request which gets in, because
340 * the monitor-uri is defined by the frontend.
341 */
342 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100343 isteqi(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200344 /*
345 * We have found the monitor URI
346 */
347 struct acl_cond *cond;
348
349 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100350 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200351
352 /* Check if we want to fail this monitor request or not */
353 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
354 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
355
356 ret = acl_pass(ret);
357 if (cond->pol == ACL_COND_UNLESS)
358 ret = !ret;
359
360 if (ret) {
361 /* we fail this request, let's return 503 service unavail */
362 txn->status = 503;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100363 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200364 if (!(s->flags & SF_ERR_MASK))
365 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
366 goto return_prx_cond;
367 }
368 }
369
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800370 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200371 txn->status = 200;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100372 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200373 if (!(s->flags & SF_ERR_MASK))
374 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
375 goto return_prx_cond;
376 }
377
378 /*
379 * 3: Maybe we have to copy the original REQURI for the logs ?
380 * Note: we cannot log anymore if the request has been
381 * classified as invalid.
382 */
383 if (unlikely(s->logs.logwait & LW_REQ)) {
384 /* we have a complete HTTP request that we must log */
385 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200386 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200387
Christopher Faulet9768c262018-10-22 09:34:31 +0200388 len = htx_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
389 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200390
391 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
392 s->do_log(s);
393 } else {
394 ha_alert("HTTP logging : out of memory.\n");
395 }
396 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200397
Christopher Faulete0768eb2018-10-03 16:38:02 +0200398 /* if the frontend has "option http-use-proxy-header", we'll check if
399 * we have what looks like a proxied connection instead of a connection,
400 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
401 * Note that this is *not* RFC-compliant, however browsers and proxies
402 * happen to do that despite being non-standard :-(
403 * We consider that a request not beginning with either '/' or '*' is
404 * a proxied connection, which covers both "scheme://location" and
405 * CONNECT ip:port.
406 */
407 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100408 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200409 txn->flags |= TX_USE_PX_CONN;
410
Christopher Faulete0768eb2018-10-03 16:38:02 +0200411 /* 5: we may need to capture headers */
412 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet9768c262018-10-22 09:34:31 +0200413 htx_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200414
Christopher Faulet03b9d8b2019-03-26 22:02:00 +0100415 /* by default, close the stream at the end of the transaction. */
416 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200417
418 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200419 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200420 req->analysers |= AN_REQ_HTTP_BODY;
421
422 /*
423 * RFC7234#4:
424 * A cache MUST write through requests with methods
425 * that are unsafe (Section 4.2.1 of [RFC7231]) to
426 * the origin server; i.e., a cache is not allowed
427 * to generate a reply to such a request before
428 * having forwarded the request and having received
429 * a corresponding response.
430 *
431 * RFC7231#4.2.1:
432 * Of the request methods defined by this
433 * specification, the GET, HEAD, OPTIONS, and TRACE
434 * methods are defined to be safe.
435 */
436 if (likely(txn->meth == HTTP_METH_GET ||
437 txn->meth == HTTP_METH_HEAD ||
438 txn->meth == HTTP_METH_OPTIONS ||
439 txn->meth == HTTP_METH_TRACE))
440 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
441
442 /* end of job, return OK */
443 req->analysers &= ~an_bit;
444 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200445
Christopher Faulete0768eb2018-10-03 16:38:02 +0200446 return 1;
447
448 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200449 txn->status = 400;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200450 txn->req.err_state = txn->req.msg_state;
451 txn->req.msg_state = HTTP_MSG_ERROR;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100452 htx_reply_and_close(s, txn->status, htx_error_message(s));
Olivier Houcharda798bf52019-03-08 18:52:00 +0100453 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200454 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100455 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200456
457 return_prx_cond:
458 if (!(s->flags & SF_ERR_MASK))
459 s->flags |= SF_ERR_PRXCOND;
460 if (!(s->flags & SF_FINST_MASK))
461 s->flags |= SF_FINST_R;
462
463 req->analysers &= AN_REQ_FLT_END;
464 req->analyse_exp = TICK_ETERNITY;
465 return 0;
466}
467
468
469/* This stream analyser runs all HTTP request processing which is common to
470 * frontends and backends, which means blocking ACLs, filters, connection-close,
471 * reqadd, stats and redirects. This is performed for the designated proxy.
472 * It returns 1 if the processing can continue on next analysers, or zero if it
473 * either needs more data or wants to immediately abort the request (eg: deny,
474 * error, ...).
475 */
476int htx_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
477{
478 struct session *sess = s->sess;
479 struct http_txn *txn = s->txn;
480 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200481 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200482 struct redirect_rule *rule;
483 struct cond_wordlist *wl;
484 enum rule_result verdict;
485 int deny_status = HTTP_ERR_403;
486 struct connection *conn = objt_conn(sess->origin);
487
488 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
489 /* we need more data */
490 goto return_prx_yield;
491 }
492
493 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
494 now_ms, __FUNCTION__,
495 s,
496 req,
497 req->rex, req->wex,
498 req->flags,
499 ci_data(req),
500 req->analysers);
501
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100502 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200503
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200504 /* just in case we have some per-backend tracking. Only called the first
505 * execution of the analyser. */
506 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
507 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200508
509 /* evaluate http-request rules */
510 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200511 verdict = htx_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200512
513 switch (verdict) {
514 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
515 goto return_prx_yield;
516
517 case HTTP_RULE_RES_CONT:
518 case HTTP_RULE_RES_STOP: /* nothing to do */
519 break;
520
521 case HTTP_RULE_RES_DENY: /* deny or tarpit */
522 if (txn->flags & TX_CLTARPIT)
523 goto tarpit;
524 goto deny;
525
526 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
527 goto return_prx_cond;
528
529 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
530 goto done;
531
532 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
533 goto return_bad_req;
534 }
535 }
536
537 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
538 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200539 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200540
Christopher Fauletff2759f2018-10-24 11:13:16 +0200541 ctx.blk = NULL;
542 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
543 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200544 goto return_bad_req;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200545 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200546 }
547
548 /* OK at this stage, we know that the request was accepted according to
549 * the http-request rules, we can check for the stats. Note that the
550 * URI is detected *before* the req* rules in order not to be affected
551 * by a possible reqrep, while they are processed *after* so that a
552 * reqdeny can still block them. This clearly needs to change in 1.6!
553 */
Christopher Fauletff2759f2018-10-24 11:13:16 +0200554 if (htx_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200555 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100556 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200557 txn->status = 500;
558 s->logs.tv_request = now;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100559 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200560
561 if (!(s->flags & SF_ERR_MASK))
562 s->flags |= SF_ERR_RESOURCE;
563 goto return_prx_cond;
564 }
565
566 /* parse the whole stats request and extract the relevant information */
Christopher Fauletff2759f2018-10-24 11:13:16 +0200567 htx_handle_stats(s, req);
568 verdict = htx_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200569 /* not all actions implemented: deny, allow, auth */
570
571 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
572 goto deny;
573
574 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
575 goto return_prx_cond;
576 }
577
578 /* evaluate the req* rules except reqadd */
579 if (px->req_exp != NULL) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200580 if (htx_apply_filters_to_request(s, req, px) < 0)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200581 goto return_bad_req;
582
583 if (txn->flags & TX_CLDENY)
584 goto deny;
585
586 if (txn->flags & TX_CLTARPIT) {
587 deny_status = HTTP_ERR_500;
588 goto tarpit;
589 }
590 }
591
592 /* add request headers from the rule sets in the same order */
593 list_for_each_entry(wl, &px->req_add, list) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200594 struct ist n,v;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200595 if (wl->cond) {
596 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
597 ret = acl_pass(ret);
598 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
599 ret = !ret;
600 if (!ret)
601 continue;
602 }
603
Christopher Fauletff2759f2018-10-24 11:13:16 +0200604 http_parse_header(ist2(wl->s, strlen(wl->s)), &n, &v);
605 if (unlikely(!http_add_header(htx, n, v)))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200606 goto return_bad_req;
607 }
608
Christopher Faulet2571bc62019-03-01 11:44:26 +0100609 /* Proceed with the applets now. */
610 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200611 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100612 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200613
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100614 if (htx_handle_expect_hdr(s, htx, msg) == -1)
615 goto return_bad_req;
616
Christopher Faulete0768eb2018-10-03 16:38:02 +0200617 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
618 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
619 if (!(s->flags & SF_FINST_MASK))
620 s->flags |= SF_FINST_R;
621
622 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
623 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
624 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
625 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100626
627 req->flags |= CF_SEND_DONTWAIT;
628 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200629 goto done;
630 }
631
632 /* check whether we have some ACLs set to redirect this request */
633 list_for_each_entry(rule, &px->redirect_rules, list) {
634 if (rule->cond) {
635 int ret;
636
637 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
638 ret = acl_pass(ret);
639 if (rule->cond->pol == ACL_COND_UNLESS)
640 ret = !ret;
641 if (!ret)
642 continue;
643 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200644 if (!htx_apply_redirect_rule(rule, s, txn))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200645 goto return_bad_req;
646 goto done;
647 }
648
649 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
650 * If this happens, then the data will not come immediately, so we must
651 * send all what we have without waiting. Note that due to the small gain
652 * in waiting for the body of the request, it's easier to simply put the
653 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
654 * itself once used.
655 */
656 req->flags |= CF_SEND_DONTWAIT;
657
658 done: /* done with this analyser, continue with next ones that the calling
659 * points will have set, if any.
660 */
661 req->analyse_exp = TICK_ETERNITY;
662 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
663 req->analysers &= ~an_bit;
664 return 1;
665
666 tarpit:
667 /* Allow cookie logging
668 */
669 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletff2759f2018-10-24 11:13:16 +0200670 htx_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200671
672 /* When a connection is tarpitted, we use the tarpit timeout,
673 * which may be the same as the connect timeout if unspecified.
674 * If unset, then set it to zero because we really want it to
675 * eventually expire. We build the tarpit as an analyser.
676 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100677 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200678
679 /* wipe the request out so that we can drop the connection early
680 * if the client closes first.
681 */
682 channel_dont_connect(req);
683
684 txn->status = http_err_codes[deny_status];
685
686 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
687 req->analysers |= AN_REQ_HTTP_TARPIT;
688 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
689 if (!req->analyse_exp)
690 req->analyse_exp = tick_add(now_ms, 0);
691 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100692 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200693 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100694 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200695 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100696 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200697 goto done_without_exp;
698
699 deny: /* this request was blocked (denied) */
700
701 /* Allow cookie logging
702 */
703 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletff2759f2018-10-24 11:13:16 +0200704 htx_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200705
706 txn->flags |= TX_CLDENY;
707 txn->status = http_err_codes[deny_status];
708 s->logs.tv_request = now;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100709 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200710 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100711 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200712 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100713 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200714 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100715 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200716 goto return_prx_cond;
717
718 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200719 txn->req.err_state = txn->req.msg_state;
720 txn->req.msg_state = HTTP_MSG_ERROR;
721 txn->status = 400;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100722 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200723
Olivier Houcharda798bf52019-03-08 18:52:00 +0100724 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200725 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100726 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200727
728 return_prx_cond:
729 if (!(s->flags & SF_ERR_MASK))
730 s->flags |= SF_ERR_PRXCOND;
731 if (!(s->flags & SF_FINST_MASK))
732 s->flags |= SF_FINST_R;
733
734 req->analysers &= AN_REQ_FLT_END;
735 req->analyse_exp = TICK_ETERNITY;
736 return 0;
737
738 return_prx_yield:
739 channel_dont_connect(req);
740 return 0;
741}
742
743/* This function performs all the processing enabled for the current request.
744 * It returns 1 if the processing can continue on next analysers, or zero if it
745 * needs more data, encounters an error, or wants to immediately abort the
746 * request. It relies on buffers flags, and updates s->req.analysers.
747 */
748int htx_process_request(struct stream *s, struct channel *req, int an_bit)
749{
750 struct session *sess = s->sess;
751 struct http_txn *txn = s->txn;
752 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200753 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200754 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
755
756 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
757 /* we need more data */
758 channel_dont_connect(req);
759 return 0;
760 }
761
762 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
763 now_ms, __FUNCTION__,
764 s,
765 req,
766 req->rex, req->wex,
767 req->flags,
768 ci_data(req),
769 req->analysers);
770
771 /*
772 * Right now, we know that we have processed the entire headers
773 * and that unwanted requests have been filtered out. We can do
774 * whatever we want with the remaining request. Also, now we
775 * may have separate values for ->fe, ->be.
776 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100777 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200778
779 /*
780 * If HTTP PROXY is set we simply get remote server address parsing
781 * incoming request. Note that this requires that a connection is
782 * allocated on the server side.
783 */
784 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
785 struct connection *conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100786 struct htx_sl *sl;
787 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200788
789 /* Note that for now we don't reuse existing proxy connections */
790 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
791 txn->req.err_state = txn->req.msg_state;
792 txn->req.msg_state = HTTP_MSG_ERROR;
793 txn->status = 500;
794 req->analysers &= AN_REQ_FLT_END;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100795 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200796
797 if (!(s->flags & SF_ERR_MASK))
798 s->flags |= SF_ERR_RESOURCE;
799 if (!(s->flags & SF_FINST_MASK))
800 s->flags |= SF_FINST_R;
801
802 return 0;
803 }
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200804 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100805 uri = htx_sl_req_uri(sl);
806 path = http_get_path(uri);
807 if (url2sa(uri.ptr, uri.len - path.len, &conn->addr.to, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200808 goto return_bad_req;
809
810 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200811 * uri.ptr and path.ptr (excluded). If it was not found, we need
812 * to replace from all the uri by a single "/".
813 *
814 * Instead of rewritting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100815 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200816 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200817 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100818 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200819 }
820
821 /*
822 * 7: Now we can work with the cookies.
823 * Note that doing so might move headers in the request, but
824 * the fields will stay coherent and the URI will not move.
825 * This should only be performed in the backend.
826 */
827 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletb6aadbd2018-12-18 16:41:31 +0100828 htx_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200829
830 /* add unique-id if "header-unique-id" is specified */
831
832 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
833 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
834 goto return_bad_req;
835 s->unique_id[0] = '\0';
836 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
837 }
838
839 if (sess->fe->header_unique_id && s->unique_id) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200840 struct ist n = ist2(sess->fe->header_unique_id, strlen(sess->fe->header_unique_id));
841 struct ist v = ist2(s->unique_id, strlen(s->unique_id));
842
843 if (unlikely(!http_add_header(htx, n, v)))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200844 goto return_bad_req;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200845 }
846
847 /*
848 * 9: add X-Forwarded-For if either the frontend or the backend
849 * asks for it.
850 */
851 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200852 struct http_hdr_ctx ctx = { .blk = NULL };
853 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
854 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
855
Christopher Faulete0768eb2018-10-03 16:38:02 +0200856 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200857 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200858 /* The header is set to be added only if none is present
859 * and we found it, so don't do anything.
860 */
861 }
862 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
863 /* Add an X-Forwarded-For header unless the source IP is
864 * in the 'except' network range.
865 */
866 if ((!sess->fe->except_mask.s_addr ||
867 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
868 != sess->fe->except_net.s_addr) &&
869 (!s->be->except_mask.s_addr ||
870 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
871 != s->be->except_net.s_addr)) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200872 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200873
874 /* Note: we rely on the backend to get the header name to be used for
875 * x-forwarded-for, because the header is really meant for the backends.
876 * However, if the backend did not specify any option, we have to rely
877 * on the frontend's header name.
878 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200879 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
880 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200881 goto return_bad_req;
882 }
883 }
884 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
885 /* FIXME: for the sake of completeness, we should also support
886 * 'except' here, although it is mostly useless in this case.
887 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200888 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200889
Christopher Faulete0768eb2018-10-03 16:38:02 +0200890 inet_ntop(AF_INET6,
891 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
892 pn, sizeof(pn));
893
894 /* Note: we rely on the backend to get the header name to be used for
895 * x-forwarded-for, because the header is really meant for the backends.
896 * However, if the backend did not specify any option, we have to rely
897 * on the frontend's header name.
898 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200899 chunk_printf(&trash, "%s", pn);
900 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200901 goto return_bad_req;
902 }
903 }
904
905 /*
906 * 10: add X-Original-To if either the frontend or the backend
907 * asks for it.
908 */
909 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
910
911 /* FIXME: don't know if IPv6 can handle that case too. */
912 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
913 /* Add an X-Original-To header unless the destination IP is
914 * in the 'except' network range.
915 */
916 conn_get_to_addr(cli_conn);
917
918 if (cli_conn->addr.to.ss_family == AF_INET &&
919 ((!sess->fe->except_mask_to.s_addr ||
920 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
921 != sess->fe->except_to.s_addr) &&
922 (!s->be->except_mask_to.s_addr ||
923 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
924 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200925 struct ist hdr;
926 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200927
928 /* Note: we rely on the backend to get the header name to be used for
929 * x-original-to, because the header is really meant for the backends.
930 * However, if the backend did not specify any option, we have to rely
931 * on the frontend's header name.
932 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200933 if (s->be->orgto_hdr_len)
934 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
935 else
936 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200937
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200938 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
939 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200940 goto return_bad_req;
941 }
942 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200943 }
944
Christopher Faulete0768eb2018-10-03 16:38:02 +0200945 /* If we have no server assigned yet and we're balancing on url_param
946 * with a POST request, we may be interested in checking the body for
947 * that parameter. This will be done in another analyser.
948 */
949 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100950 s->txn->meth == HTTP_METH_POST &&
951 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200952 channel_dont_connect(req);
953 req->analysers |= AN_REQ_HTTP_BODY;
954 }
955
956 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
957 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100958
Christopher Faulete0768eb2018-10-03 16:38:02 +0200959 /* We expect some data from the client. Unless we know for sure
960 * we already have a full request, we have to re-enable quick-ack
961 * in case we previously disabled it, otherwise we might cause
962 * the client to delay further data.
963 */
964 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200965 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100966 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200967
968 /*************************************************************
969 * OK, that's finished for the headers. We have done what we *
970 * could. Let's switch to the DATA state. *
971 ************************************************************/
972 req->analyse_exp = TICK_ETERNITY;
973 req->analysers &= ~an_bit;
974
975 s->logs.tv_request = now;
976 /* OK let's go on with the BODY now */
977 return 1;
978
979 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200980 txn->req.err_state = txn->req.msg_state;
981 txn->req.msg_state = HTTP_MSG_ERROR;
982 txn->status = 400;
983 req->analysers &= AN_REQ_FLT_END;
Christopher Fauleta7b677c2018-11-29 16:48:49 +0100984 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200985
Olivier Houcharda798bf52019-03-08 18:52:00 +0100986 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200987 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100988 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200989
990 if (!(s->flags & SF_ERR_MASK))
991 s->flags |= SF_ERR_PRXCOND;
992 if (!(s->flags & SF_FINST_MASK))
993 s->flags |= SF_FINST_R;
994 return 0;
995}
996
997/* This function is an analyser which processes the HTTP tarpit. It always
998 * returns zero, at the beginning because it prevents any other processing
999 * from occurring, and at the end because it terminates the request.
1000 */
1001int htx_process_tarpit(struct stream *s, struct channel *req, int an_bit)
1002{
1003 struct http_txn *txn = s->txn;
1004
1005 /* This connection is being tarpitted. The CLIENT side has
1006 * already set the connect expiration date to the right
1007 * timeout. We just have to check that the client is still
1008 * there and that the timeout has not expired.
1009 */
1010 channel_dont_connect(req);
1011 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
1012 !tick_is_expired(req->analyse_exp, now_ms))
1013 return 0;
1014
1015 /* We will set the queue timer to the time spent, just for
1016 * logging purposes. We fake a 500 server error, so that the
1017 * attacker will not suspect his connection has been tarpitted.
1018 * It will not cause trouble to the logs because we can exclude
1019 * the tarpitted connections by filtering on the 'PT' status flags.
1020 */
1021 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1022
1023 if (!(req->flags & CF_READ_ERROR))
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001024 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001025
1026 req->analysers &= AN_REQ_FLT_END;
1027 req->analyse_exp = TICK_ETERNITY;
1028
1029 if (!(s->flags & SF_ERR_MASK))
1030 s->flags |= SF_ERR_PRXCOND;
1031 if (!(s->flags & SF_FINST_MASK))
1032 s->flags |= SF_FINST_T;
1033 return 0;
1034}
1035
1036/* This function is an analyser which waits for the HTTP request body. It waits
1037 * for either the buffer to be full, or the full advertised contents to have
1038 * reached the buffer. It must only be called after the standard HTTP request
1039 * processing has occurred, because it expects the request to be parsed and will
1040 * look for the Expect header. It may send a 100-Continue interim response. It
1041 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
1042 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
1043 * needs to read more data, or 1 once it has completed its analysis.
1044 */
1045int htx_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
1046{
1047 struct session *sess = s->sess;
1048 struct http_txn *txn = s->txn;
1049 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001050 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001051
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001052
1053 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1054 now_ms, __FUNCTION__,
1055 s,
1056 req,
1057 req->rex, req->wex,
1058 req->flags,
1059 ci_data(req),
1060 req->analysers);
1061
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001062 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001063
Willy Tarreau4236f032019-03-05 10:43:32 +01001064 if (htx->flags & HTX_FL_PARSING_ERROR)
1065 goto return_bad_req;
1066
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001067 if (msg->msg_state < HTTP_MSG_BODY)
1068 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001069
Christopher Faulete0768eb2018-10-03 16:38:02 +02001070 /* We have to parse the HTTP request body to find any required data.
1071 * "balance url_param check_post" should have been the only way to get
1072 * into this. We were brought here after HTTP header analysis, so all
1073 * related structures are ready.
1074 */
1075
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001076 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Faulet4a28a532019-03-01 11:19:40 +01001077 if (htx_handle_expect_hdr(s, htx, msg) == -1)
1078 goto return_bad_req;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001079 }
1080
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001081 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001082
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001083 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1084 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001085 */
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001086 if (htx_get_tail_type(htx) >= HTX_BLK_EOD ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001087 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001088 goto http_end;
1089
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001090 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001091 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1092 txn->status = 408;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001093 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001094
1095 if (!(s->flags & SF_ERR_MASK))
1096 s->flags |= SF_ERR_CLITO;
1097 if (!(s->flags & SF_FINST_MASK))
1098 s->flags |= SF_FINST_D;
1099 goto return_err_msg;
1100 }
1101
1102 /* we get here if we need to wait for more data */
1103 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1104 /* Not enough data. We'll re-use the http-request
1105 * timeout here. Ideally, we should set the timeout
1106 * relative to the accept() date. We just set the
1107 * request timeout once at the beginning of the
1108 * request.
1109 */
1110 channel_dont_connect(req);
1111 if (!tick_isset(req->analyse_exp))
1112 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1113 return 0;
1114 }
1115
1116 http_end:
1117 /* The situation will not evolve, so let's give up on the analysis. */
1118 s->logs.tv_request = now; /* update the request timer to reflect full request */
1119 req->analysers &= ~an_bit;
1120 req->analyse_exp = TICK_ETERNITY;
1121 return 1;
1122
1123 return_bad_req: /* let's centralize all bad requests */
1124 txn->req.err_state = txn->req.msg_state;
1125 txn->req.msg_state = HTTP_MSG_ERROR;
1126 txn->status = 400;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001127 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001128
1129 if (!(s->flags & SF_ERR_MASK))
1130 s->flags |= SF_ERR_PRXCOND;
1131 if (!(s->flags & SF_FINST_MASK))
1132 s->flags |= SF_FINST_R;
1133
1134 return_err_msg:
1135 req->analysers &= AN_REQ_FLT_END;
Olivier Houcharda798bf52019-03-08 18:52:00 +01001136 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001137 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001138 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001139 return 0;
1140}
1141
1142/* This function is an analyser which forwards request body (including chunk
1143 * sizes if any). It is called as soon as we must forward, even if we forward
1144 * zero byte. The only situation where it must not be called is when we're in
1145 * tunnel mode and we want to forward till the close. It's used both to forward
1146 * remaining data and to resync after end of body. It expects the msg_state to
1147 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1148 * read more data, or 1 once we can go on with next request or end the stream.
1149 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1150 * bytes of pending data + the headers if not already done.
1151 */
1152int htx_request_forward_body(struct stream *s, struct channel *req, int an_bit)
1153{
1154 struct session *sess = s->sess;
1155 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001156 struct http_msg *msg = &txn->req;
1157 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001158 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001159 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001160
1161 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1162 now_ms, __FUNCTION__,
1163 s,
1164 req,
1165 req->rex, req->wex,
1166 req->flags,
1167 ci_data(req),
1168 req->analysers);
1169
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001170 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001171
1172 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1173 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1174 /* Output closed while we were sending data. We must abort and
1175 * wake the other side up.
1176 */
1177 msg->err_state = msg->msg_state;
1178 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001179 htx_end_request(s);
1180 htx_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001181 return 1;
1182 }
1183
1184 /* Note that we don't have to send 100-continue back because we don't
1185 * need the data to complete our job, and it's up to the server to
1186 * decide whether to return 100, 417 or anything else in return of
1187 * an "Expect: 100-continue" header.
1188 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001189 if (msg->msg_state == HTTP_MSG_BODY)
1190 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001191
1192 /* Some post-connect processing might want us to refrain from starting to
1193 * forward data. Currently, the only reason for this is "balance url_param"
1194 * whichs need to parse/process the request after we've enabled forwarding.
1195 */
1196 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
1197 if (!(s->res.flags & CF_READ_ATTACHED)) {
1198 channel_auto_connect(req);
1199 req->flags |= CF_WAKE_CONNECT;
1200 channel_dont_close(req); /* don't fail on early shutr */
1201 goto waiting;
1202 }
1203 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
1204 }
1205
1206 /* in most states, we should abort in case of early close */
1207 channel_auto_close(req);
1208
1209 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001210 if (req->to_forward == CHN_INFINITE_FORWARD) {
1211 if (req->flags & (CF_SHUTR|CF_EOI)) {
1212 msg->msg_state = HTTP_MSG_DONE;
1213 req->to_forward = 0;
1214 goto done;
1215 }
1216 }
1217 else {
1218 /* We can't process the buffer's contents yet */
1219 req->flags |= CF_WAKE_WRITE;
1220 goto missing_data_or_waiting;
1221 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001222 }
1223
Christopher Faulet9768c262018-10-22 09:34:31 +02001224 if (msg->msg_state >= HTTP_MSG_DONE)
1225 goto done;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001226 /* Forward input data. We get it by removing all outgoing data not
1227 * forwarded yet from HTX data size. If there are some data filters, we
1228 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001229 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001230 if (HAS_REQ_DATA_FILTERS(s)) {
1231 ret = flt_http_payload(s, msg, htx->data);
1232 if (ret < 0)
1233 goto return_bad_req;
1234 c_adv(req, ret);
1235 if (htx->data != co_data(req) || htx->extra)
1236 goto missing_data_or_waiting;
1237 }
1238 else {
1239 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001240 if (msg->flags & HTTP_MSGF_XFER_LEN)
1241 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001242 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001243
Christopher Fauletc62c2b92019-03-28 11:41:39 +01001244 if (txn->meth == HTTP_METH_CONNECT) {
1245 msg->msg_state = HTTP_MSG_TUNNEL;
1246 goto done;
1247 }
1248
Christopher Faulet9768c262018-10-22 09:34:31 +02001249 /* Check if the end-of-message is reached and if so, switch the message
1250 * in HTTP_MSG_DONE state.
1251 */
1252 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1253 goto missing_data_or_waiting;
1254
1255 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletaed68d42019-03-28 18:12:46 +01001256 req->to_forward = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001257
1258 done:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001259 /* other states, DONE...TUNNEL */
1260 /* we don't want to forward closes on DONE except in tunnel mode. */
1261 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
1262 channel_dont_close(req);
1263
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001264 if (HAS_REQ_DATA_FILTERS(s)) {
1265 ret = flt_http_end(s, msg);
1266 if (ret <= 0) {
1267 if (!ret)
1268 goto missing_data_or_waiting;
1269 goto return_bad_req;
1270 }
1271 }
1272
Christopher Fauletf2824e62018-10-01 12:12:37 +02001273 htx_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001274 if (!(req->analysers & an_bit)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001275 htx_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001276 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1277 if (req->flags & CF_SHUTW) {
1278 /* request errors are most likely due to the
1279 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001280 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001281 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001282 goto return_bad_req;
1283 }
1284 return 1;
1285 }
1286
1287 /* If "option abortonclose" is set on the backend, we want to monitor
1288 * the client's connection and forward any shutdown notification to the
1289 * server, which will decide whether to close or to go on processing the
1290 * request. We only do that in tunnel mode, and not in other modes since
1291 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001292 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001293 channel_auto_read(req);
1294 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
1295 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
1296 s->si[1].flags |= SI_FL_NOLINGER;
1297 channel_auto_close(req);
1298 }
1299 else if (s->txn->meth == HTTP_METH_POST) {
1300 /* POST requests may require to read extra CRLF sent by broken
1301 * browsers and which could cause an RST to be sent upon close
1302 * on some systems (eg: Linux). */
1303 channel_auto_read(req);
1304 }
1305 return 0;
1306
1307 missing_data_or_waiting:
1308 /* stop waiting for data if the input is closed before the end */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001309 if (msg->msg_state < HTTP_MSG_DONE && req->flags & CF_SHUTR)
1310 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001311
1312 waiting:
1313 /* waiting for the last bits to leave the buffer */
1314 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001315 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001316
Christopher Faulet47365272018-10-31 17:40:50 +01001317 if (htx->flags & HTX_FL_PARSING_ERROR)
1318 goto return_bad_req;
Christopher Faulet9768c262018-10-22 09:34:31 +02001319
Christopher Faulete0768eb2018-10-03 16:38:02 +02001320 /* When TE: chunked is used, we need to get there again to parse remaining
1321 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1322 * And when content-length is used, we never want to let the possible
1323 * shutdown be forwarded to the other side, as the state machine will
1324 * take care of it once the client responds. It's also important to
1325 * prevent TIME_WAITs from accumulating on the backend side, and for
1326 * HTTP/2 where the last frame comes with a shutdown.
1327 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001328 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001329 channel_dont_close(req);
1330
1331 /* We know that more data are expected, but we couldn't send more that
1332 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1333 * system knows it must not set a PUSH on this first part. Interactive
1334 * modes are already handled by the stream sock layer. We must not do
1335 * this in content-length mode because it could present the MSG_MORE
1336 * flag with the last block of forwarded data, which would cause an
1337 * additional delay to be observed by the receiver.
1338 */
1339 if (msg->flags & HTTP_MSGF_TE_CHNK)
1340 req->flags |= CF_EXPECT_MORE;
1341
1342 return 0;
1343
Christopher Faulet93e02d82019-03-08 14:18:50 +01001344 return_cli_abort:
1345 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1346 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
1347 if (objt_server(s->target))
1348 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
1349 if (!(s->flags & SF_ERR_MASK))
1350 s->flags |= SF_ERR_CLICL;
1351 status = 400;
1352 goto return_error;
1353
1354 return_srv_abort:
1355 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1356 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
1357 if (objt_server(s->target))
1358 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
1359 if (!(s->flags & SF_ERR_MASK))
1360 s->flags |= SF_ERR_SRVCL;
1361 status = 502;
1362 goto return_error;
1363
1364 return_bad_req:
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 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01001369 s->flags |= SF_ERR_CLICL;
1370 status = 400;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001371
Christopher Faulet93e02d82019-03-08 14:18:50 +01001372 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001373 txn->req.err_state = txn->req.msg_state;
1374 txn->req.msg_state = HTTP_MSG_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001375 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001376 /* Note: we don't send any error if some data were already sent */
Christopher Faulet9768c262018-10-22 09:34:31 +02001377 htx_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001378 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001379 txn->status = status;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001380 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001381 }
1382 req->analysers &= AN_REQ_FLT_END;
1383 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001384 if (!(s->flags & SF_FINST_MASK))
1385 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001386 return 0;
1387}
1388
1389/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1390 * processing can continue on next analysers, or zero if it either needs more
1391 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1392 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1393 * when it has nothing left to do, and may remove any analyser when it wants to
1394 * abort.
1395 */
1396int htx_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
1397{
Christopher Faulet9768c262018-10-22 09:34:31 +02001398 /*
1399 * We will analyze a complete HTTP response to check the its syntax.
1400 *
1401 * Once the start line and all headers are received, we may perform a
1402 * capture of the error (if any), and we will set a few fields. We also
1403 * logging and finally headers capture.
1404 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001405 struct session *sess = s->sess;
1406 struct http_txn *txn = s->txn;
1407 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001408 struct htx *htx;
Christopher Faulet61608322018-11-23 16:23:45 +01001409 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001410 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001411 int n;
1412
1413 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1414 now_ms, __FUNCTION__,
1415 s,
1416 rep,
1417 rep->rex, rep->wex,
1418 rep->flags,
1419 ci_data(rep),
1420 rep->analysers);
1421
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001422 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001423
Willy Tarreau4236f032019-03-05 10:43:32 +01001424 /* Parsing errors are caught here */
1425 if (htx->flags & HTX_FL_PARSING_ERROR)
1426 goto return_bad_res;
1427
Christopher Faulete0768eb2018-10-03 16:38:02 +02001428 /*
1429 * Now we quickly check if we have found a full valid response.
1430 * If not so, we check the FD and buffer states before leaving.
1431 * A full response is indicated by the fact that we have seen
1432 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1433 * responses are checked first.
1434 *
1435 * Depending on whether the client is still there or not, we
1436 * may send an error response back or not. Note that normally
1437 * we should only check for HTTP status there, and check I/O
1438 * errors somewhere else.
1439 */
Christopher Faulet72b62732018-11-28 16:44:44 +01001440 if (unlikely(co_data(rep) || htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH)) {
Christopher Faulet47365272018-10-31 17:40:50 +01001441 /*
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001442 * First catch invalid response because of a parsing error or
1443 * because only part of headers have been transfered.
1444 * Multiplexers have the responsibility to emit all headers at
1445 * once. We must be sure to have forwarded all outgoing data
1446 * first.
Christopher Faulet47365272018-10-31 17:40:50 +01001447 */
Willy Tarreau4236f032019-03-05 10:43:32 +01001448 if (!co_data(rep) && (htx_is_not_empty(htx) || (s->si[1].flags & SI_FL_RXBLK_ROOM)))
Christopher Faulet47365272018-10-31 17:40:50 +01001449 goto return_bad_res;
1450
Christopher Faulet9768c262018-10-22 09:34:31 +02001451 /* 1: have we encountered a read error ? */
1452 if (rep->flags & CF_READ_ERROR) {
1453 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001454 goto abort_keep_alive;
1455
Olivier Houcharda798bf52019-03-08 18:52:00 +01001456 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001457 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001458 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001459 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001460 }
1461
Christopher Faulete0768eb2018-10-03 16:38:02 +02001462 rep->analysers &= AN_RES_FLT_END;
1463 txn->status = 502;
1464
1465 /* Check to see if the server refused the early data.
1466 * If so, just send a 425
1467 */
1468 if (objt_cs(s->si[1].end)) {
1469 struct connection *conn = objt_cs(s->si[1].end)->conn;
1470
1471 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
1472 txn->status = 425;
1473 }
1474
1475 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001476 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001477
1478 if (!(s->flags & SF_ERR_MASK))
1479 s->flags |= SF_ERR_SRVCL;
1480 if (!(s->flags & SF_FINST_MASK))
1481 s->flags |= SF_FINST_H;
1482 return 0;
1483 }
1484
Christopher Faulet9768c262018-10-22 09:34:31 +02001485 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001486 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001487 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001488 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001489 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001490 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001491 }
1492
Christopher Faulete0768eb2018-10-03 16:38:02 +02001493 rep->analysers &= AN_RES_FLT_END;
1494 txn->status = 504;
1495 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001496 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001497
1498 if (!(s->flags & SF_ERR_MASK))
1499 s->flags |= SF_ERR_SRVTO;
1500 if (!(s->flags & SF_FINST_MASK))
1501 s->flags |= SF_FINST_H;
1502 return 0;
1503 }
1504
Christopher Faulet9768c262018-10-22 09:34:31 +02001505 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001506 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001507 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1508 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001509 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001510 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001511
1512 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001513 txn->status = 400;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001514 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001515
1516 if (!(s->flags & SF_ERR_MASK))
1517 s->flags |= SF_ERR_CLICL;
1518 if (!(s->flags & SF_FINST_MASK))
1519 s->flags |= SF_FINST_H;
1520
1521 /* process_stream() will take care of the error */
1522 return 0;
1523 }
1524
Christopher Faulet9768c262018-10-22 09:34:31 +02001525 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001526 else if (rep->flags & CF_SHUTR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001527 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001528 goto abort_keep_alive;
1529
Olivier Houcharda798bf52019-03-08 18:52:00 +01001530 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001531 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001532 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001533 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001534 }
1535
Christopher Faulete0768eb2018-10-03 16:38:02 +02001536 rep->analysers &= AN_RES_FLT_END;
1537 txn->status = 502;
1538 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001539 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001540
1541 if (!(s->flags & SF_ERR_MASK))
1542 s->flags |= SF_ERR_SRVCL;
1543 if (!(s->flags & SF_FINST_MASK))
1544 s->flags |= SF_FINST_H;
1545 return 0;
1546 }
1547
Christopher Faulet9768c262018-10-22 09:34:31 +02001548 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001549 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001550 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001551 goto abort_keep_alive;
1552
Olivier Houcharda798bf52019-03-08 18:52:00 +01001553 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001554 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001555
1556 if (!(s->flags & SF_ERR_MASK))
1557 s->flags |= SF_ERR_CLICL;
1558 if (!(s->flags & SF_FINST_MASK))
1559 s->flags |= SF_FINST_H;
1560
1561 /* process_stream() will take care of the error */
1562 return 0;
1563 }
1564
1565 channel_dont_close(rep);
1566 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
1567 return 0;
1568 }
1569
1570 /* More interesting part now : we know that we have a complete
1571 * response which at least looks like HTTP. We have an indicator
1572 * of each header's length, so we can parse them quickly.
1573 */
1574
Christopher Faulet9768c262018-10-22 09:34:31 +02001575 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet03599112018-11-27 11:21:21 +01001576 sl = http_find_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001577
Christopher Faulet9768c262018-10-22 09:34:31 +02001578 /* 0: we might have to print this header in debug mode */
1579 if (unlikely((global.mode & MODE_DEBUG) &&
1580 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1581 int32_t pos;
1582
Christopher Faulet03599112018-11-27 11:21:21 +01001583 htx_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001584
1585 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1586 struct htx_blk *blk = htx_get_blk(htx, pos);
1587 enum htx_blk_type type = htx_get_blk_type(blk);
1588
1589 if (type == HTX_BLK_EOH)
1590 break;
1591 if (type != HTX_BLK_HDR)
1592 continue;
1593
1594 htx_debug_hdr("srvhdr", s,
1595 htx_get_blk_name(htx, blk),
1596 htx_get_blk_value(htx, blk));
1597 }
1598 }
1599
Christopher Faulet03599112018-11-27 11:21:21 +01001600 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001601 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001602 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001603 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001604 if (sl->flags & HTX_SL_F_XFER_LEN) {
1605 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001606 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001607 if (sl->flags & HTX_SL_F_BODYLESS)
1608 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001609 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001610
1611 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001612 if (n < 1 || n > 5)
1613 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001614
Christopher Faulete0768eb2018-10-03 16:38:02 +02001615 /* when the client triggers a 4xx from the server, it's most often due
1616 * to a missing object or permission. These events should be tracked
1617 * because if they happen often, it may indicate a brute force or a
1618 * vulnerability scan.
1619 */
1620 if (n == 4)
1621 stream_inc_http_err_ctr(s);
1622
1623 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001624 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001625
Christopher Faulete0768eb2018-10-03 16:38:02 +02001626 /* Adjust server's health based on status code. Note: status codes 501
1627 * and 505 are triggered on demand by client request, so we must not
1628 * count them as server failures.
1629 */
1630 if (objt_server(s->target)) {
1631 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001632 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001633 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001634 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001635 }
1636
1637 /*
1638 * We may be facing a 100-continue response, or any other informational
1639 * 1xx response which is non-final, in which case this is not the right
1640 * response, and we're waiting for the next one. Let's allow this response
1641 * to go to the client and wait for the next one. There's an exception for
1642 * 101 which is used later in the code to switch protocols.
1643 */
1644 if (txn->status < 200 &&
1645 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001646 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet9768c262018-10-22 09:34:31 +02001647 c_adv(rep, htx->data);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001648 msg->msg_state = HTTP_MSG_RPBEFORE;
1649 txn->status = 0;
1650 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet9768c262018-10-22 09:34:31 +02001651 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001652 }
1653
1654 /*
1655 * 2: check for cacheability.
1656 */
1657
1658 switch (txn->status) {
1659 case 200:
1660 case 203:
1661 case 204:
1662 case 206:
1663 case 300:
1664 case 301:
1665 case 404:
1666 case 405:
1667 case 410:
1668 case 414:
1669 case 501:
1670 break;
1671 default:
1672 /* RFC7231#6.1:
1673 * Responses with status codes that are defined as
1674 * cacheable by default (e.g., 200, 203, 204, 206,
1675 * 300, 301, 404, 405, 410, 414, and 501 in this
1676 * specification) can be reused by a cache with
1677 * heuristic expiration unless otherwise indicated
1678 * by the method definition or explicit cache
1679 * controls [RFC7234]; all other status codes are
1680 * not cacheable by default.
1681 */
1682 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1683 break;
1684 }
1685
1686 /*
1687 * 3: we may need to capture headers
1688 */
1689 s->logs.logwait &= ~LW_RESP;
1690 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet9768c262018-10-22 09:34:31 +02001691 htx_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001692
Christopher Faulet9768c262018-10-22 09:34:31 +02001693 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001694 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1695 txn->status == 101)) {
1696 /* Either we've established an explicit tunnel, or we're
1697 * switching the protocol. In both cases, we're very unlikely
1698 * to understand the next protocols. We have to switch to tunnel
1699 * mode, so that we transfer the request and responses then let
1700 * this protocol pass unmodified. When we later implement specific
1701 * parsers for such protocols, we'll want to check the Upgrade
1702 * header which contains information about that protocol for
1703 * responses with status 101 (eg: see RFC2817 about TLS).
1704 */
1705 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001706 }
1707
Christopher Faulet61608322018-11-23 16:23:45 +01001708 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1709 * 407 (Proxy-Authenticate) responses and set the connection to private
1710 */
1711 srv_conn = cs_conn(objt_cs(s->si[1].end));
1712 if (srv_conn) {
1713 struct ist hdr;
1714 struct http_hdr_ctx ctx;
1715
1716 if (txn->status == 401)
1717 hdr = ist("WWW-Authenticate");
1718 else if (txn->status == 407)
1719 hdr = ist("Proxy-Authenticate");
1720 else
1721 goto end;
1722
1723 ctx.blk = NULL;
1724 while (http_find_header(htx, hdr, &ctx, 0)) {
1725 if ((ctx.value.len >= 9 && word_match(ctx.value.ptr, ctx.value.len, "Negotiate", 9)) ||
1726 (ctx.value.len >= 4 && word_match(ctx.value.ptr, ctx.value.len, "NTLM", 4)))
1727 srv_conn->flags |= CO_FL_PRIVATE;
1728 }
1729 }
1730
1731 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001732 /* we want to have the response time before we start processing it */
1733 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1734
1735 /* end of job, return OK */
1736 rep->analysers &= ~an_bit;
1737 rep->analyse_exp = TICK_ETERNITY;
1738 channel_auto_close(rep);
1739 return 1;
1740
Christopher Faulet47365272018-10-31 17:40:50 +01001741 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001742 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001743 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001744 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001745 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001746 }
1747 txn->status = 502;
1748 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01001749 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001750 rep->analysers &= AN_RES_FLT_END;
1751
1752 if (!(s->flags & SF_ERR_MASK))
1753 s->flags |= SF_ERR_PRXCOND;
1754 if (!(s->flags & SF_FINST_MASK))
1755 s->flags |= SF_FINST_H;
1756 return 0;
1757
Christopher Faulete0768eb2018-10-03 16:38:02 +02001758 abort_keep_alive:
1759 /* A keep-alive request to the server failed on a network error.
1760 * The client is required to retry. We need to close without returning
1761 * any other information so that the client retries.
1762 */
1763 txn->status = 0;
1764 rep->analysers &= AN_RES_FLT_END;
1765 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001766 s->logs.logwait = 0;
1767 s->logs.level = 0;
1768 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Faulet9768c262018-10-22 09:34:31 +02001769 htx_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001770 return 0;
1771}
1772
1773/* This function performs all the processing enabled for the current response.
1774 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1775 * and updates s->res.analysers. It might make sense to explode it into several
1776 * other functions. It works like process_request (see indications above).
1777 */
1778int htx_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
1779{
1780 struct session *sess = s->sess;
1781 struct http_txn *txn = s->txn;
1782 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001783 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001784 struct proxy *cur_proxy;
1785 struct cond_wordlist *wl;
1786 enum rule_result ret = HTTP_RULE_RES_CONT;
1787
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001788 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1789 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001790
Christopher Faulete0768eb2018-10-03 16:38:02 +02001791 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1792 now_ms, __FUNCTION__,
1793 s,
1794 rep,
1795 rep->rex, rep->wex,
1796 rep->flags,
1797 ci_data(rep),
1798 rep->analysers);
1799
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001800 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001801
1802 /* The stats applet needs to adjust the Connection header but we don't
1803 * apply any filter there.
1804 */
1805 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1806 rep->analysers &= ~an_bit;
1807 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001808 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001809 }
1810
1811 /*
1812 * We will have to evaluate the filters.
1813 * As opposed to version 1.2, now they will be evaluated in the
1814 * filters order and not in the header order. This means that
1815 * each filter has to be validated among all headers.
1816 *
1817 * Filters are tried with ->be first, then with ->fe if it is
1818 * different from ->be.
1819 *
1820 * Maybe we are in resume condiion. In this case I choose the
1821 * "struct proxy" which contains the rule list matching the resume
1822 * pointer. If none of theses "struct proxy" match, I initialise
1823 * the process with the first one.
1824 *
1825 * In fact, I check only correspondance betwwen the current list
1826 * pointer and the ->fe rule list. If it doesn't match, I initialize
1827 * the loop with the ->be.
1828 */
1829 if (s->current_rule_list == &sess->fe->http_res_rules)
1830 cur_proxy = sess->fe;
1831 else
1832 cur_proxy = s->be;
1833 while (1) {
1834 struct proxy *rule_set = cur_proxy;
1835
1836 /* evaluate http-response rules */
1837 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001838 ret = htx_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001839
1840 if (ret == HTTP_RULE_RES_BADREQ)
1841 goto return_srv_prx_502;
1842
1843 if (ret == HTTP_RULE_RES_DONE) {
1844 rep->analysers &= ~an_bit;
1845 rep->analyse_exp = TICK_ETERNITY;
1846 return 1;
1847 }
1848 }
1849
1850 /* we need to be called again. */
1851 if (ret == HTTP_RULE_RES_YIELD) {
1852 channel_dont_close(rep);
1853 return 0;
1854 }
1855
1856 /* try headers filters */
1857 if (rule_set->rsp_exp != NULL) {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001858 if (htx_apply_filters_to_response(s, rep, rule_set) < 0)
1859 goto return_bad_resp;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001860 }
1861
1862 /* has the response been denied ? */
1863 if (txn->flags & TX_SVDENY) {
1864 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001865 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_secu, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001866
Olivier Houcharda798bf52019-03-08 18:52:00 +01001867 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
1868 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001869 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001870 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001871 goto return_srv_prx_502;
1872 }
1873
1874 /* add response headers from the rule sets in the same order */
1875 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001876 struct ist n, v;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001877 if (txn->status < 200 && txn->status != 101)
1878 break;
1879 if (wl->cond) {
1880 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
1881 ret = acl_pass(ret);
1882 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
1883 ret = !ret;
1884 if (!ret)
1885 continue;
1886 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001887
1888 http_parse_header(ist2(wl->s, strlen(wl->s)), &n, &v);
1889 if (unlikely(!http_add_header(htx, n, v)))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001890 goto return_bad_resp;
1891 }
1892
1893 /* check whether we're already working on the frontend */
1894 if (cur_proxy == sess->fe)
1895 break;
1896 cur_proxy = sess->fe;
1897 }
1898
1899 /* After this point, this anayzer can't return yield, so we can
1900 * remove the bit corresponding to this analyzer from the list.
1901 *
1902 * Note that the intermediate returns and goto found previously
1903 * reset the analyzers.
1904 */
1905 rep->analysers &= ~an_bit;
1906 rep->analyse_exp = TICK_ETERNITY;
1907
1908 /* OK that's all we can do for 1xx responses */
1909 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001910 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001911
1912 /*
1913 * Now check for a server cookie.
1914 */
1915 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001916 htx_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001917
1918 /*
1919 * Check for cache-control or pragma headers if required.
1920 */
1921 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
1922 check_response_for_cacheability(s, rep);
1923
1924 /*
1925 * Add server cookie in the response if needed
1926 */
1927 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1928 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1929 (!(s->flags & SF_DIRECT) ||
1930 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1931 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1932 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1933 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1934 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1935 !(s->flags & SF_IGNORE_PRST)) {
1936 /* the server is known, it's not the one the client requested, or the
1937 * cookie's last seen date needs to be refreshed. We have to
1938 * insert a set-cookie here, except if we want to insert only on POST
1939 * requests and this one isn't. Note that servers which don't have cookies
1940 * (eg: some backup servers) will return a full cookie removal request.
1941 */
1942 if (!objt_server(s->target)->cookie) {
1943 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001944 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001945 s->be->cookie_name);
1946 }
1947 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001948 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001949
1950 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1951 /* emit last_date, which is mandatory */
1952 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1953 s30tob64((date.tv_sec+3) >> 2,
1954 trash.area + trash.data);
1955 trash.data += 5;
1956
1957 if (s->be->cookie_maxlife) {
1958 /* emit first_date, which is either the original one or
1959 * the current date.
1960 */
1961 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1962 s30tob64(txn->cookie_first_date ?
1963 txn->cookie_first_date >> 2 :
1964 (date.tv_sec+3) >> 2,
1965 trash.area + trash.data);
1966 trash.data += 5;
1967 }
1968 }
1969 chunk_appendf(&trash, "; path=/");
1970 }
1971
1972 if (s->be->cookie_domain)
1973 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1974
1975 if (s->be->ck_opts & PR_CK_HTTPONLY)
1976 chunk_appendf(&trash, "; HttpOnly");
1977
1978 if (s->be->ck_opts & PR_CK_SECURE)
1979 chunk_appendf(&trash, "; Secure");
1980
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001981 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001982 goto return_bad_resp;
1983
1984 txn->flags &= ~TX_SCK_MASK;
1985 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1986 /* the server did not change, only the date was updated */
1987 txn->flags |= TX_SCK_UPDATED;
1988 else
1989 txn->flags |= TX_SCK_INSERTED;
1990
1991 /* Here, we will tell an eventual cache on the client side that we don't
1992 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1993 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1994 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1995 */
1996 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
1997
1998 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
1999
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002000 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002001 goto return_bad_resp;
2002 }
2003 }
2004
2005 /*
2006 * Check if result will be cacheable with a cookie.
2007 * We'll block the response if security checks have caught
2008 * nasty things such as a cacheable cookie.
2009 */
2010 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2011 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2012 (s->be->options & PR_O_CHK_CACHE)) {
2013 /* we're in presence of a cacheable response containing
2014 * a set-cookie header. We'll block it as requested by
2015 * the 'checkcache' option, and send an alert.
2016 */
2017 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01002018 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002019
Olivier Houcharda798bf52019-03-08 18:52:00 +01002020 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
2021 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002022 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002023 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002024
2025 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2026 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2027 send_log(s->be, LOG_ALERT,
2028 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2029 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2030 goto return_srv_prx_502;
2031 }
2032
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002033 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002034 /* Always enter in the body analyzer */
2035 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2036 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2037
2038 /* if the user wants to log as soon as possible, without counting
2039 * bytes from the server, then this is the right moment. We have
2040 * to temporarily assign bytes_out to log what we currently have.
2041 */
2042 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2043 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002044 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002045 s->do_log(s);
2046 s->logs.bytes_out = 0;
2047 }
2048 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002049
2050 return_bad_resp:
2051 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01002052 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01002053 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002054 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01002055 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002056
2057 return_srv_prx_502:
2058 rep->analysers &= AN_RES_FLT_END;
2059 txn->status = 502;
2060 s->logs.t_data = -1; /* was not a valid response */
2061 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01002062 htx_reply_and_close(s, txn->status, htx_error_message(s));
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002063 if (!(s->flags & SF_ERR_MASK))
2064 s->flags |= SF_ERR_PRXCOND;
2065 if (!(s->flags & SF_FINST_MASK))
2066 s->flags |= SF_FINST_H;
2067 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002068}
2069
2070/* This function is an analyser which forwards response body (including chunk
2071 * sizes if any). It is called as soon as we must forward, even if we forward
2072 * zero byte. The only situation where it must not be called is when we're in
2073 * tunnel mode and we want to forward till the close. It's used both to forward
2074 * remaining data and to resync after end of body. It expects the msg_state to
2075 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2076 * read more data, or 1 once we can go on with next request or end the stream.
2077 *
2078 * It is capable of compressing response data both in content-length mode and
2079 * in chunked mode. The state machines follows different flows depending on
2080 * whether content-length and chunked modes are used, since there are no
2081 * trailers in content-length :
2082 *
2083 * chk-mode cl-mode
2084 * ,----- BODY -----.
2085 * / \
2086 * V size > 0 V chk-mode
2087 * .--> SIZE -------------> DATA -------------> CRLF
2088 * | | size == 0 | last byte |
2089 * | v final crlf v inspected |
2090 * | TRAILERS -----------> DONE |
2091 * | |
2092 * `----------------------------------------------'
2093 *
2094 * Compression only happens in the DATA state, and must be flushed in final
2095 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2096 * is performed at once on final states for all bytes parsed, or when leaving
2097 * on missing data.
2098 */
2099int htx_response_forward_body(struct stream *s, struct channel *res, int an_bit)
2100{
2101 struct session *sess = s->sess;
2102 struct http_txn *txn = s->txn;
2103 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002104 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002105 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002106
2107 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
2108 now_ms, __FUNCTION__,
2109 s,
2110 res,
2111 res->rex, res->wex,
2112 res->flags,
2113 ci_data(res),
2114 res->analysers);
2115
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002116 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002117
2118 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002119 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002120 /* Output closed while we were sending data. We must abort and
2121 * wake the other side up.
2122 */
2123 msg->err_state = msg->msg_state;
2124 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002125 htx_end_response(s);
2126 htx_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002127 return 1;
2128 }
2129
Christopher Faulet9768c262018-10-22 09:34:31 +02002130 if (msg->msg_state == HTTP_MSG_BODY)
2131 msg->msg_state = HTTP_MSG_DATA;
2132
Christopher Faulete0768eb2018-10-03 16:38:02 +02002133 /* in most states, we should abort in case of early close */
2134 channel_auto_close(res);
2135
Christopher Faulete0768eb2018-10-03 16:38:02 +02002136 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002137 if (res->to_forward == CHN_INFINITE_FORWARD) {
2138 if (res->flags & (CF_SHUTR|CF_EOI)) {
2139 msg->msg_state = HTTP_MSG_DONE;
2140 res->to_forward = 0;
2141 goto done;
2142 }
2143 }
2144 else {
2145 /* We can't process the buffer's contents yet */
2146 res->flags |= CF_WAKE_WRITE;
2147 goto missing_data_or_waiting;
2148 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002149 }
2150
Christopher Faulet9768c262018-10-22 09:34:31 +02002151 if (msg->msg_state >= HTTP_MSG_DONE)
2152 goto done;
2153
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002154 /* Forward input data. We get it by removing all outgoing data not
2155 * forwarded yet from HTX data size. If there are some data filters, we
2156 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002157 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002158 if (HAS_RSP_DATA_FILTERS(s)) {
2159 ret = flt_http_payload(s, msg, htx->data);
2160 if (ret < 0)
2161 goto return_bad_res;
2162 c_adv(res, ret);
2163 if (htx->data != co_data(res) || htx->extra)
2164 goto missing_data_or_waiting;
2165 }
2166 else {
2167 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002168 if (msg->flags & HTTP_MSGF_XFER_LEN)
2169 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002170 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002171
Christopher Fauletc62c2b92019-03-28 11:41:39 +01002172 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2173 (!(msg->flags & HTTP_MSGF_XFER_LEN) && (res->flags & CF_SHUTR || !HAS_RSP_DATA_FILTERS(s)))) {
2174 msg->msg_state = HTTP_MSG_TUNNEL;
2175 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002176 }
2177
Christopher Faulet9768c262018-10-22 09:34:31 +02002178 /* Check if the end-of-message is reached and if so, switch the message
2179 * in HTTP_MSG_DONE state.
2180 */
2181 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2182 goto missing_data_or_waiting;
2183
2184 msg->msg_state = HTTP_MSG_DONE;
Christopher Fauletaed68d42019-03-28 18:12:46 +01002185 res->to_forward = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02002186
2187 done:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002188 /* other states, DONE...TUNNEL */
Christopher Faulet9768c262018-10-22 09:34:31 +02002189 channel_dont_close(res);
2190
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002191 if (HAS_RSP_DATA_FILTERS(s)) {
2192 ret = flt_http_end(s, msg);
2193 if (ret <= 0) {
2194 if (!ret)
2195 goto missing_data_or_waiting;
2196 goto return_bad_res;
2197 }
2198 }
2199
Christopher Fauletf2824e62018-10-01 12:12:37 +02002200 htx_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002201 if (!(res->analysers & an_bit)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02002202 htx_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002203 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2204 if (res->flags & CF_SHUTW) {
2205 /* response errors are most likely due to the
2206 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002207 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002208 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002209 goto return_bad_res;
2210 }
2211 return 1;
2212 }
2213 return 0;
2214
2215 missing_data_or_waiting:
2216 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002217 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002218
Christopher Faulet47365272018-10-31 17:40:50 +01002219 if (htx->flags & HTX_FL_PARSING_ERROR)
2220 goto return_bad_res;
2221
Christopher Faulete0768eb2018-10-03 16:38:02 +02002222 /* stop waiting for data if the input is closed before the end. If the
2223 * client side was already closed, it means that the client has aborted,
2224 * so we don't want to count this as a server abort. Otherwise it's a
2225 * server abort.
2226 */
Christopher Faulet9768c262018-10-22 09:34:31 +02002227 if (msg->msg_state < HTTP_MSG_DONE && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002228 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002229 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002230 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002231 if (htx_is_empty(htx))
2232 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002233 }
2234
Christopher Faulete0768eb2018-10-03 16:38:02 +02002235 /* When TE: chunked is used, we need to get there again to parse
2236 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002237 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2238 * are filters registered on the stream, we don't want to forward a
2239 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002240 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002241 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002242 channel_dont_close(res);
2243
2244 /* We know that more data are expected, but we couldn't send more that
2245 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2246 * system knows it must not set a PUSH on this first part. Interactive
2247 * modes are already handled by the stream sock layer. We must not do
2248 * this in content-length mode because it could present the MSG_MORE
2249 * flag with the last block of forwarded data, which would cause an
2250 * additional delay to be observed by the receiver.
2251 */
2252 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2253 res->flags |= CF_EXPECT_MORE;
2254
2255 /* the stream handler will take care of timeouts and errors */
2256 return 0;
2257
Christopher Faulet93e02d82019-03-08 14:18:50 +01002258 return_srv_abort:
2259 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2260 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002261 if (objt_server(s->target))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002262 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
2263 if (!(s->flags & SF_ERR_MASK))
2264 s->flags |= SF_ERR_SRVCL;
2265 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002266
Christopher Faulet93e02d82019-03-08 14:18:50 +01002267 return_cli_abort:
2268 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2269 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002270 if (objt_server(s->target))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002271 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
2272 if (!(s->flags & SF_ERR_MASK))
2273 s->flags |= SF_ERR_CLICL;
2274 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002275
Christopher Faulet93e02d82019-03-08 14:18:50 +01002276 return_bad_res:
2277 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2278 if (objt_server(s->target)) {
2279 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
2280 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2281 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002282 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002283 s->flags |= SF_ERR_SRVCL;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002284
Christopher Faulet93e02d82019-03-08 14:18:50 +01002285 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002286 txn->rsp.err_state = txn->rsp.msg_state;
2287 txn->rsp.msg_state = HTTP_MSG_ERROR;
2288 /* don't send any error message as we're in the body */
Christopher Faulet9768c262018-10-22 09:34:31 +02002289 htx_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002290 res->analysers &= AN_RES_FLT_END;
2291 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002292 if (!(s->flags & SF_FINST_MASK))
2293 s->flags |= SF_FINST_D;
2294 return 0;
2295}
2296
Christopher Fauletf2824e62018-10-01 12:12:37 +02002297/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002298 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002299 * as too large a request to build a valid response.
2300 */
2301int htx_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
2302{
Christopher Faulet99daf282018-11-28 22:58:13 +01002303 struct channel *req = &s->req;
2304 struct channel *res = &s->res;
2305 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002306 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002307 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002308 struct ist status, reason, location;
2309 unsigned int flags;
2310 size_t data;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002311
2312 chunk = alloc_trash_chunk();
2313 if (!chunk)
Christopher Faulet99daf282018-11-28 22:58:13 +01002314 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002315
Christopher Faulet99daf282018-11-28 22:58:13 +01002316 /*
2317 * Create the location
2318 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002319 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002320 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002321 case REDIRECT_TYPE_SCHEME: {
2322 struct http_hdr_ctx ctx;
2323 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002324
Christopher Faulet99daf282018-11-28 22:58:13 +01002325 host = ist("");
2326 ctx.blk = NULL;
2327 if (http_find_header(htx, ist("Host"), &ctx, 0))
2328 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002329
Christopher Faulet99daf282018-11-28 22:58:13 +01002330 sl = http_find_stline(htx);
2331 path = http_get_path(htx_sl_req_uri(sl));
2332 /* build message using path */
2333 if (path.ptr) {
2334 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2335 int qs = 0;
2336 while (qs < path.len) {
2337 if (*(path.ptr + qs) == '?') {
2338 path.len = qs;
2339 break;
2340 }
2341 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002342 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002343 }
2344 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002345 else
2346 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002347
Christopher Faulet99daf282018-11-28 22:58:13 +01002348 if (rule->rdr_str) { /* this is an old "redirect" rule */
2349 /* add scheme */
2350 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2351 goto fail;
2352 }
2353 else {
2354 /* add scheme with executing log format */
2355 chunk->data += build_logline(s, chunk->area + chunk->data,
2356 chunk->size - chunk->data,
2357 &rule->rdr_fmt);
2358 }
2359 /* add "://" + host + path */
2360 if (!chunk_memcat(chunk, "://", 3) ||
2361 !chunk_memcat(chunk, host.ptr, host.len) ||
2362 !chunk_memcat(chunk, path.ptr, path.len))
2363 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002364
Christopher Faulet99daf282018-11-28 22:58:13 +01002365 /* append a slash at the end of the location if needed and missing */
2366 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2367 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2368 if (chunk->data + 1 >= chunk->size)
2369 goto fail;
2370 chunk->area[chunk->data++] = '/';
2371 }
2372 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002373 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002374
Christopher Faulet99daf282018-11-28 22:58:13 +01002375 case REDIRECT_TYPE_PREFIX: {
2376 struct ist path;
2377
2378 sl = http_find_stline(htx);
2379 path = http_get_path(htx_sl_req_uri(sl));
2380 /* build message using path */
2381 if (path.ptr) {
2382 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2383 int qs = 0;
2384 while (qs < path.len) {
2385 if (*(path.ptr + qs) == '?') {
2386 path.len = qs;
2387 break;
2388 }
2389 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002390 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002391 }
2392 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002393 else
2394 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002395
Christopher Faulet99daf282018-11-28 22:58:13 +01002396 if (rule->rdr_str) { /* this is an old "redirect" rule */
2397 /* add prefix. Note that if prefix == "/", we don't want to
2398 * add anything, otherwise it makes it hard for the user to
2399 * configure a self-redirection.
2400 */
2401 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2402 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2403 goto fail;
2404 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002405 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002406 else {
2407 /* add prefix with executing log format */
2408 chunk->data += build_logline(s, chunk->area + chunk->data,
2409 chunk->size - chunk->data,
2410 &rule->rdr_fmt);
2411 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002412
Christopher Faulet99daf282018-11-28 22:58:13 +01002413 /* add path */
2414 if (!chunk_memcat(chunk, path.ptr, path.len))
2415 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002416
Christopher Faulet99daf282018-11-28 22:58:13 +01002417 /* append a slash at the end of the location if needed and missing */
2418 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2419 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2420 if (chunk->data + 1 >= chunk->size)
2421 goto fail;
2422 chunk->area[chunk->data++] = '/';
2423 }
2424 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002425 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002426 case REDIRECT_TYPE_LOCATION:
2427 default:
2428 if (rule->rdr_str) { /* this is an old "redirect" rule */
2429 /* add location */
2430 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2431 goto fail;
2432 }
2433 else {
2434 /* add location with executing log format */
2435 chunk->data += build_logline(s, chunk->area + chunk->data,
2436 chunk->size - chunk->data,
2437 &rule->rdr_fmt);
2438 }
2439 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002440 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002441 location = ist2(chunk->area, chunk->data);
2442
2443 /*
2444 * Create the 30x response
2445 */
2446 switch (rule->code) {
2447 case 308:
2448 status = ist("308");
2449 reason = ist("Permanent Redirect");
2450 break;
2451 case 307:
2452 status = ist("307");
2453 reason = ist("Temporary Redirect");
2454 break;
2455 case 303:
2456 status = ist("303");
2457 reason = ist("See Other");
2458 break;
2459 case 301:
2460 status = ist("301");
2461 reason = ist("Moved Permanently");
2462 break;
2463 case 302:
2464 default:
2465 status = ist("302");
2466 reason = ist("Found");
2467 break;
2468 }
2469
2470 htx = htx_from_buf(&res->buf);
2471 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2472 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2473 if (!sl)
2474 goto fail;
2475 sl->info.res.status = rule->code;
2476 s->txn->status = rule->code;
2477
2478 if (!htx_add_header(htx, ist("Connection"), ist("close")) ||
2479 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
2480 !htx_add_header(htx, ist("Location"), location))
2481 goto fail;
2482
2483 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2484 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2485 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002486 }
2487
2488 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002489 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2490 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002491 }
2492
Christopher Faulet99daf282018-11-28 22:58:13 +01002493 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2494 goto fail;
2495
Christopher Fauletf2824e62018-10-01 12:12:37 +02002496 /* let's log the request time */
2497 s->logs.tv_request = now;
2498
Christopher Faulet99daf282018-11-28 22:58:13 +01002499 data = htx->data - co_data(res);
Christopher Faulet99daf282018-11-28 22:58:13 +01002500 c_adv(res, data);
2501 res->total += data;
2502
2503 channel_auto_read(req);
2504 channel_abort(req);
2505 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002506 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet99daf282018-11-28 22:58:13 +01002507
2508 res->wex = tick_add_ifset(now_ms, res->wto);
2509 channel_auto_read(res);
2510 channel_auto_close(res);
2511 channel_shutr_now(res);
2512
2513 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002514
2515 if (!(s->flags & SF_ERR_MASK))
2516 s->flags |= SF_ERR_LOCAL;
2517 if (!(s->flags & SF_FINST_MASK))
2518 s->flags |= SF_FINST_R;
2519
Christopher Faulet99daf282018-11-28 22:58:13 +01002520 free_trash_chunk(chunk);
2521 return 1;
2522
2523 fail:
2524 /* If an error occurred, remove the incomplete HTTP response from the
2525 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002526 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002527 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002528 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002529}
2530
Christopher Faulet72333522018-10-24 11:25:02 +02002531int htx_transform_header_str(struct stream* s, struct channel *chn, struct htx *htx,
2532 struct ist name, const char *str, struct my_regex *re, int action)
2533{
2534 struct http_hdr_ctx ctx;
2535 struct buffer *output = get_trash_chunk();
2536
2537 /* find full header is action is ACT_HTTP_REPLACE_HDR */
2538 ctx.blk = NULL;
2539 while (http_find_header(htx, name, &ctx, (action == ACT_HTTP_REPLACE_HDR))) {
2540 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2541 continue;
2542
2543 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2544 if (output->data == -1)
2545 return -1;
2546 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2547 return -1;
2548 }
2549 return 0;
2550}
2551
2552static int htx_transform_header(struct stream* s, struct channel *chn, struct htx *htx,
2553 const struct ist name, struct list *fmt, struct my_regex *re, int action)
2554{
2555 struct buffer *replace;
2556 int ret = -1;
2557
2558 replace = alloc_trash_chunk();
2559 if (!replace)
2560 goto leave;
2561
2562 replace->data = build_logline(s, replace->area, replace->size, fmt);
2563 if (replace->data >= replace->size - 1)
2564 goto leave;
2565
2566 ret = htx_transform_header_str(s, chn, htx, name, replace->area, re, action);
2567
2568 leave:
2569 free_trash_chunk(replace);
2570 return ret;
2571}
2572
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002573
2574/* Terminate a 103-Erly-hints response and send it to the client. It returns 0
2575 * on success and -1 on error. The response channel is updated accordingly.
2576 */
2577static int htx_reply_103_early_hints(struct channel *res)
2578{
2579 struct htx *htx = htx_from_buf(&res->buf);
2580 size_t data;
2581
2582 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM)) {
2583 /* If an error occurred during an Early-hint rule,
2584 * remove the incomplete HTTP 103 response from the
2585 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002586 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002587 return -1;
2588 }
2589
2590 data = htx->data - co_data(res);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002591 c_adv(res, data);
2592 res->total += data;
2593 return 0;
2594}
2595
Christopher Faulet6eb92892018-11-15 16:39:29 +01002596/*
2597 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
2598 * built according to <fmt> log line format.
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002599 * If <early_hints> is 0, it is starts a new response by adding the start
2600 * line. If an error occurred -1 is returned. On success 0 is returned. The
2601 * channel is not updated here. It must be done calling the function
2602 * htx_reply_103_early_hints().
Christopher Faulet6eb92892018-11-15 16:39:29 +01002603 */
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002604static 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 +01002605{
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002606 struct channel *res = &s->res;
2607 struct htx *htx = htx_from_buf(&res->buf);
2608 struct buffer *value = alloc_trash_chunk();
2609
Christopher Faulet6eb92892018-11-15 16:39:29 +01002610 if (!early_hints) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002611 struct htx_sl *sl;
2612 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
2613 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2614
2615 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
2616 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
2617 if (!sl)
Christopher Faulet6eb92892018-11-15 16:39:29 +01002618 goto fail;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002619 sl->info.res.status = 103;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002620 }
2621
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002622 value->data = build_logline(s, b_tail(value), b_room(value), fmt);
2623 if (!htx_add_header(htx, name, ist2(b_head(value), b_data(value))))
Christopher Faulet6eb92892018-11-15 16:39:29 +01002624 goto fail;
2625
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002626 free_trash_chunk(value);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002627 return 1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002628
2629 fail:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002630 /* If an error occurred during an Early-hint rule, remove the incomplete
2631 * HTTP 103 response from the buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002632 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002633 free_trash_chunk(value);
2634 return -1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002635}
2636
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002637/* This function executes one of the set-{method,path,query,uri} actions. It
2638 * takes the string from the variable 'replace' with length 'len', then modifies
2639 * the relevant part of the request line accordingly. Then it updates various
2640 * pointers to the next elements which were moved, and the total buffer length.
2641 * It finds the action to be performed in p[2], previously filled by function
2642 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2643 * error, though this can be revisited when this code is finally exploited.
2644 *
2645 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2646 * query string and 3 to replace uri.
2647 *
2648 * In query string case, the mark question '?' must be set at the start of the
2649 * string by the caller, event if the replacement query string is empty.
2650 */
2651int htx_req_replace_stline(int action, const char *replace, int len,
2652 struct proxy *px, struct stream *s)
2653{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002654 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002655
2656 switch (action) {
2657 case 0: // method
2658 if (!http_replace_req_meth(htx, ist2(replace, len)))
2659 return -1;
2660 break;
2661
2662 case 1: // path
2663 if (!http_replace_req_path(htx, ist2(replace, len)))
2664 return -1;
2665 break;
2666
2667 case 2: // query
2668 if (!http_replace_req_query(htx, ist2(replace, len)))
2669 return -1;
2670 break;
2671
2672 case 3: // uri
2673 if (!http_replace_req_uri(htx, ist2(replace, len)))
2674 return -1;
2675 break;
2676
2677 default:
2678 return -1;
2679 }
2680 return 0;
2681}
2682
2683/* This function replace the HTTP status code and the associated message. The
2684 * variable <status> contains the new status code. This function never fails.
2685 */
2686void htx_res_set_status(unsigned int status, const char *reason, struct stream *s)
2687{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002688 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002689 char *res;
2690
2691 chunk_reset(&trash);
2692 res = ultoa_o(status, trash.area, trash.size);
2693 trash.data = res - trash.area;
2694
2695 /* Do we have a custom reason format string? */
2696 if (reason == NULL)
2697 reason = http_get_reason(status);
2698
Christopher Faulet87a2c0d2018-12-13 21:58:18 +01002699 if (http_replace_res_status(htx, ist2(trash.area, trash.data)))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002700 http_replace_res_reason(htx, ist2(reason, strlen(reason)));
2701}
2702
Christopher Faulet3e964192018-10-24 11:39:23 +02002703/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2704 * transaction <txn>. Returns the verdict of the first rule that prevents
2705 * further processing of the request (auth, deny, ...), and defaults to
2706 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2707 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2708 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2709 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2710 * status.
2711 */
2712static enum rule_result htx_req_get_intercept_rule(struct proxy *px, struct list *rules,
2713 struct stream *s, int *deny_status)
2714{
2715 struct session *sess = strm_sess(s);
2716 struct http_txn *txn = s->txn;
2717 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002718 struct act_rule *rule;
2719 struct http_hdr_ctx ctx;
2720 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002721 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2722 int act_flags = 0;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002723 int early_hints = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002724
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002725 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002726
2727 /* If "the current_rule_list" match the executed rule list, we are in
2728 * resume condition. If a resume is needed it is always in the action
2729 * and never in the ACL or converters. In this case, we initialise the
2730 * current rule, and go to the action execution point.
2731 */
2732 if (s->current_rule) {
2733 rule = s->current_rule;
2734 s->current_rule = NULL;
2735 if (s->current_rule_list == rules)
2736 goto resume_execution;
2737 }
2738 s->current_rule_list = rules;
2739
2740 list_for_each_entry(rule, rules, list) {
2741 /* check optional condition */
2742 if (rule->cond) {
2743 int ret;
2744
2745 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2746 ret = acl_pass(ret);
2747
2748 if (rule->cond->pol == ACL_COND_UNLESS)
2749 ret = !ret;
2750
2751 if (!ret) /* condition not matched */
2752 continue;
2753 }
2754
2755 act_flags |= ACT_FLAG_FIRST;
2756 resume_execution:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002757 if (early_hints && rule->action != ACT_HTTP_EARLY_HINT) {
2758 early_hints = 0;
2759 if (htx_reply_103_early_hints(&s->res) == -1) {
2760 rule_ret = HTTP_RULE_RES_BADREQ;
2761 goto end;
2762 }
2763 }
2764
Christopher Faulet3e964192018-10-24 11:39:23 +02002765 switch (rule->action) {
2766 case ACT_ACTION_ALLOW:
2767 rule_ret = HTTP_RULE_RES_STOP;
2768 goto end;
2769
2770 case ACT_ACTION_DENY:
2771 if (deny_status)
2772 *deny_status = rule->deny_status;
2773 rule_ret = HTTP_RULE_RES_DENY;
2774 goto end;
2775
2776 case ACT_HTTP_REQ_TARPIT:
2777 txn->flags |= TX_CLTARPIT;
2778 if (deny_status)
2779 *deny_status = rule->deny_status;
2780 rule_ret = HTTP_RULE_RES_DENY;
2781 goto end;
2782
2783 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002784 /* Auth might be performed on regular http-req rules as well as on stats */
2785 auth_realm = rule->arg.auth.realm;
2786 if (!auth_realm) {
2787 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2788 auth_realm = STATS_DEFAULT_REALM;
2789 else
2790 auth_realm = px->id;
2791 }
2792 /* send 401/407 depending on whether we use a proxy or not. We still
2793 * count one error, because normal browsing won't significantly
2794 * increase the counter but brute force attempts will.
2795 */
Christopher Faulet3e964192018-10-24 11:39:23 +02002796 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Faulet12c51e22018-11-28 15:59:42 +01002797 if (htx_reply_40x_unauthorized(s, auth_realm) == -1)
2798 rule_ret = HTTP_RULE_RES_BADREQ;
2799 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02002800 goto end;
2801
2802 case ACT_HTTP_REDIR:
Christopher Faulet3e964192018-10-24 11:39:23 +02002803 rule_ret = HTTP_RULE_RES_DONE;
2804 if (!htx_apply_redirect_rule(rule->arg.redir, s, txn))
2805 rule_ret = HTTP_RULE_RES_BADREQ;
2806 goto end;
2807
2808 case ACT_HTTP_SET_NICE:
2809 s->task->nice = rule->arg.nice;
2810 break;
2811
2812 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01002813 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Christopher Faulet3e964192018-10-24 11:39:23 +02002814 break;
2815
2816 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01002817 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Christopher Faulet3e964192018-10-24 11:39:23 +02002818 break;
2819
2820 case ACT_HTTP_SET_LOGL:
2821 s->logs.level = rule->arg.loglevel;
2822 break;
2823
2824 case ACT_HTTP_REPLACE_HDR:
2825 case ACT_HTTP_REPLACE_VAL:
2826 if (htx_transform_header(s, &s->req, htx,
2827 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
2828 &rule->arg.hdr_add.fmt,
2829 &rule->arg.hdr_add.re, rule->action)) {
2830 rule_ret = HTTP_RULE_RES_BADREQ;
2831 goto end;
2832 }
2833 break;
2834
2835 case ACT_HTTP_DEL_HDR:
2836 /* remove all occurrences of the header */
2837 ctx.blk = NULL;
2838 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
2839 http_remove_header(htx, &ctx);
2840 break;
2841
2842 case ACT_HTTP_SET_HDR:
2843 case ACT_HTTP_ADD_HDR: {
2844 /* The scope of the trash buffer must be limited to this function. The
2845 * build_logline() function can execute a lot of other function which
2846 * can use the trash buffer. So for limiting the scope of this global
2847 * buffer, we build first the header value using build_logline, and
2848 * after we store the header name.
2849 */
2850 struct buffer *replace;
2851 struct ist n, v;
2852
2853 replace = alloc_trash_chunk();
2854 if (!replace) {
2855 rule_ret = HTTP_RULE_RES_BADREQ;
2856 goto end;
2857 }
2858
2859 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
2860 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2861 v = ist2(replace->area, replace->data);
2862
2863 if (rule->action == ACT_HTTP_SET_HDR) {
2864 /* remove all occurrences of the header */
2865 ctx.blk = NULL;
2866 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
2867 http_remove_header(htx, &ctx);
2868 }
2869
2870 if (!http_add_header(htx, n, v)) {
2871 static unsigned char rate_limit = 0;
2872
2873 if ((rate_limit++ & 255) == 0) {
2874 send_log(px, LOG_WARNING, "Proxy %s failed to add or set the request header '%.*s' for request #%u. You might need to increase tune.maxrewrite.", px->id, (int)n.len, n.ptr, s->uniq_id);
2875 }
2876
Olivier Houcharda798bf52019-03-08 18:52:00 +01002877 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002878 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002879 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002880 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01002881 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02002882 }
2883 free_trash_chunk(replace);
2884 break;
2885 }
2886
2887 case ACT_HTTP_DEL_ACL:
2888 case ACT_HTTP_DEL_MAP: {
2889 struct pat_ref *ref;
2890 struct buffer *key;
2891
2892 /* collect reference */
2893 ref = pat_ref_lookup(rule->arg.map.ref);
2894 if (!ref)
2895 continue;
2896
2897 /* allocate key */
2898 key = alloc_trash_chunk();
2899 if (!key) {
2900 rule_ret = HTTP_RULE_RES_BADREQ;
2901 goto end;
2902 }
2903
2904 /* collect key */
2905 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2906 key->area[key->data] = '\0';
2907
2908 /* perform update */
2909 /* returned code: 1=ok, 0=ko */
2910 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
2911 pat_ref_delete(ref, key->area);
2912 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
2913
2914 free_trash_chunk(key);
2915 break;
2916 }
2917
2918 case ACT_HTTP_ADD_ACL: {
2919 struct pat_ref *ref;
2920 struct buffer *key;
2921
2922 /* collect reference */
2923 ref = pat_ref_lookup(rule->arg.map.ref);
2924 if (!ref)
2925 continue;
2926
2927 /* allocate key */
2928 key = alloc_trash_chunk();
2929 if (!key) {
2930 rule_ret = HTTP_RULE_RES_BADREQ;
2931 goto end;
2932 }
2933
2934 /* collect key */
2935 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2936 key->area[key->data] = '\0';
2937
2938 /* perform update */
2939 /* add entry only if it does not already exist */
2940 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
2941 if (pat_ref_find_elt(ref, key->area) == NULL)
2942 pat_ref_add(ref, key->area, NULL, NULL);
2943 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
2944
2945 free_trash_chunk(key);
2946 break;
2947 }
2948
2949 case ACT_HTTP_SET_MAP: {
2950 struct pat_ref *ref;
2951 struct buffer *key, *value;
2952
2953 /* collect reference */
2954 ref = pat_ref_lookup(rule->arg.map.ref);
2955 if (!ref)
2956 continue;
2957
2958 /* allocate key */
2959 key = alloc_trash_chunk();
2960 if (!key) {
2961 rule_ret = HTTP_RULE_RES_BADREQ;
2962 goto end;
2963 }
2964
2965 /* allocate value */
2966 value = alloc_trash_chunk();
2967 if (!value) {
2968 free_trash_chunk(key);
2969 rule_ret = HTTP_RULE_RES_BADREQ;
2970 goto end;
2971 }
2972
2973 /* collect key */
2974 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2975 key->area[key->data] = '\0';
2976
2977 /* collect value */
2978 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
2979 value->area[value->data] = '\0';
2980
2981 /* perform update */
Christopher Faulete84289e2019-04-19 14:50:55 +02002982 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02002983 if (pat_ref_find_elt(ref, key->area) != NULL)
2984 /* update entry if it exists */
2985 pat_ref_set(ref, key->area, value->area, NULL);
2986 else
2987 /* insert a new entry */
2988 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02002989 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02002990 free_trash_chunk(key);
2991 free_trash_chunk(value);
2992 break;
2993 }
2994
2995 case ACT_HTTP_EARLY_HINT:
2996 if (!(txn->req.flags & HTTP_MSGF_VER_11))
2997 break;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002998 early_hints = htx_add_early_hint_header(s, early_hints,
2999 ist2(rule->arg.early_hint.name, rule->arg.early_hint.name_len),
Christopher Faulet3e964192018-10-24 11:39:23 +02003000 &rule->arg.early_hint.fmt);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003001 if (early_hints == -1) {
3002 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02003003 goto end;
3004 }
3005 break;
3006
3007 case ACT_CUSTOM:
3008 if ((s->req.flags & CF_READ_ERROR) ||
3009 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Christopher Faulet3e964192018-10-24 11:39:23 +02003010 (px->options & PR_O_ABRT_CLOSE)))
3011 act_flags |= ACT_FLAG_FINAL;
3012
3013 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
3014 case ACT_RET_ERR:
3015 case ACT_RET_CONT:
3016 break;
3017 case ACT_RET_STOP:
3018 rule_ret = HTTP_RULE_RES_DONE;
3019 goto end;
3020 case ACT_RET_YIELD:
3021 s->current_rule = rule;
3022 rule_ret = HTTP_RULE_RES_YIELD;
3023 goto end;
3024 }
3025 break;
3026
3027 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3028 /* Note: only the first valid tracking parameter of each
3029 * applies.
3030 */
3031
3032 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3033 struct stktable *t;
3034 struct stksess *ts;
3035 struct stktable_key *key;
3036 void *ptr1, *ptr2;
3037
3038 t = rule->arg.trk_ctr.table.t;
3039 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL,
3040 rule->arg.trk_ctr.expr, NULL);
3041
3042 if (key && (ts = stktable_get_entry(t, key))) {
3043 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3044
3045 /* let's count a new HTTP request as it's the first time we do it */
3046 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3047 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3048 if (ptr1 || ptr2) {
3049 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3050
3051 if (ptr1)
3052 stktable_data_cast(ptr1, http_req_cnt)++;
3053
3054 if (ptr2)
3055 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
3056 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3057
3058 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3059
3060 /* If data was modified, we need to touch to re-schedule sync */
3061 stktable_touch_local(t, ts, 0);
3062 }
3063
3064 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3065 if (sess->fe != s->be)
3066 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3067 }
3068 }
3069 break;
3070
Joseph Herlantc42c0e92018-11-25 10:43:27 -08003071 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003072 default:
3073 break;
3074 }
3075 }
3076
3077 end:
3078 if (early_hints) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003079 if (htx_reply_103_early_hints(&s->res) == -1)
3080 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02003081 }
3082
3083 /* we reached the end of the rules, nothing to report */
3084 return rule_ret;
3085}
3086
3087/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3088 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3089 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3090 * is returned, the process can continue the evaluation of next rule list. If
3091 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3092 * is returned, it means the operation could not be processed and a server error
3093 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3094 * deny rule. If *YIELD is returned, the caller must call again the function
3095 * with the same context.
3096 */
3097static enum rule_result htx_res_get_intercept_rule(struct proxy *px, struct list *rules,
3098 struct stream *s)
3099{
3100 struct session *sess = strm_sess(s);
3101 struct http_txn *txn = s->txn;
3102 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02003103 struct act_rule *rule;
3104 struct http_hdr_ctx ctx;
3105 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
3106 int act_flags = 0;
3107
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003108 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02003109
3110 /* If "the current_rule_list" match the executed rule list, we are in
3111 * resume condition. If a resume is needed it is always in the action
3112 * and never in the ACL or converters. In this case, we initialise the
3113 * current rule, and go to the action execution point.
3114 */
3115 if (s->current_rule) {
3116 rule = s->current_rule;
3117 s->current_rule = NULL;
3118 if (s->current_rule_list == rules)
3119 goto resume_execution;
3120 }
3121 s->current_rule_list = rules;
3122
3123 list_for_each_entry(rule, rules, list) {
3124 /* check optional condition */
3125 if (rule->cond) {
3126 int ret;
3127
3128 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3129 ret = acl_pass(ret);
3130
3131 if (rule->cond->pol == ACL_COND_UNLESS)
3132 ret = !ret;
3133
3134 if (!ret) /* condition not matched */
3135 continue;
3136 }
3137
3138 act_flags |= ACT_FLAG_FIRST;
3139resume_execution:
3140 switch (rule->action) {
3141 case ACT_ACTION_ALLOW:
3142 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3143 goto end;
3144
3145 case ACT_ACTION_DENY:
3146 txn->flags |= TX_SVDENY;
3147 rule_ret = HTTP_RULE_RES_STOP;
3148 goto end;
3149
3150 case ACT_HTTP_SET_NICE:
3151 s->task->nice = rule->arg.nice;
3152 break;
3153
3154 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003155 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Christopher Faulet3e964192018-10-24 11:39:23 +02003156 break;
3157
3158 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003159 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Christopher Faulet3e964192018-10-24 11:39:23 +02003160 break;
3161
3162 case ACT_HTTP_SET_LOGL:
3163 s->logs.level = rule->arg.loglevel;
3164 break;
3165
3166 case ACT_HTTP_REPLACE_HDR:
3167 case ACT_HTTP_REPLACE_VAL:
3168 if (htx_transform_header(s, &s->res, htx,
3169 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
3170 &rule->arg.hdr_add.fmt,
3171 &rule->arg.hdr_add.re, rule->action)) {
3172 rule_ret = HTTP_RULE_RES_BADREQ;
3173 goto end;
3174 }
3175 break;
3176
3177 case ACT_HTTP_DEL_HDR:
3178 /* remove all occurrences of the header */
3179 ctx.blk = NULL;
3180 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3181 http_remove_header(htx, &ctx);
3182 break;
3183
3184 case ACT_HTTP_SET_HDR:
3185 case ACT_HTTP_ADD_HDR: {
3186 struct buffer *replace;
3187 struct ist n, v;
3188
3189 replace = alloc_trash_chunk();
3190 if (!replace) {
3191 rule_ret = HTTP_RULE_RES_BADREQ;
3192 goto end;
3193 }
3194
3195 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
3196 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3197 v = ist2(replace->area, replace->data);
3198
3199 if (rule->action == ACT_HTTP_SET_HDR) {
3200 /* remove all occurrences of the header */
3201 ctx.blk = NULL;
3202 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3203 http_remove_header(htx, &ctx);
3204 }
3205
3206 if (!http_add_header(htx, n, v)) {
3207 static unsigned char rate_limit = 0;
3208
3209 if ((rate_limit++ & 255) == 0) {
3210 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);
3211 }
3212
Olivier Houcharda798bf52019-03-08 18:52:00 +01003213 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003214 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003215 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003216 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003217 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003218 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01003219 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003220 }
3221 free_trash_chunk(replace);
3222 break;
3223 }
3224
3225 case ACT_HTTP_DEL_ACL:
3226 case ACT_HTTP_DEL_MAP: {
3227 struct pat_ref *ref;
3228 struct buffer *key;
3229
3230 /* collect reference */
3231 ref = pat_ref_lookup(rule->arg.map.ref);
3232 if (!ref)
3233 continue;
3234
3235 /* allocate key */
3236 key = alloc_trash_chunk();
3237 if (!key) {
3238 rule_ret = HTTP_RULE_RES_BADREQ;
3239 goto end;
3240 }
3241
3242 /* collect key */
3243 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3244 key->area[key->data] = '\0';
3245
3246 /* perform update */
3247 /* returned code: 1=ok, 0=ko */
3248 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3249 pat_ref_delete(ref, key->area);
3250 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3251
3252 free_trash_chunk(key);
3253 break;
3254 }
3255
3256 case ACT_HTTP_ADD_ACL: {
3257 struct pat_ref *ref;
3258 struct buffer *key;
3259
3260 /* collect reference */
3261 ref = pat_ref_lookup(rule->arg.map.ref);
3262 if (!ref)
3263 continue;
3264
3265 /* allocate key */
3266 key = alloc_trash_chunk();
3267 if (!key) {
3268 rule_ret = HTTP_RULE_RES_BADREQ;
3269 goto end;
3270 }
3271
3272 /* collect key */
3273 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3274 key->area[key->data] = '\0';
3275
3276 /* perform update */
3277 /* check if the entry already exists */
Christopher Faulete84289e2019-04-19 14:50:55 +02003278 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003279 if (pat_ref_find_elt(ref, key->area) == NULL)
3280 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02003281 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003282 free_trash_chunk(key);
3283 break;
3284 }
3285
3286 case ACT_HTTP_SET_MAP: {
3287 struct pat_ref *ref;
3288 struct buffer *key, *value;
3289
3290 /* collect reference */
3291 ref = pat_ref_lookup(rule->arg.map.ref);
3292 if (!ref)
3293 continue;
3294
3295 /* allocate key */
3296 key = alloc_trash_chunk();
3297 if (!key) {
3298 rule_ret = HTTP_RULE_RES_BADREQ;
3299 goto end;
3300 }
3301
3302 /* allocate value */
3303 value = alloc_trash_chunk();
3304 if (!value) {
3305 free_trash_chunk(key);
3306 rule_ret = HTTP_RULE_RES_BADREQ;
3307 goto end;
3308 }
3309
3310 /* collect key */
3311 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3312 key->area[key->data] = '\0';
3313
3314 /* collect value */
3315 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3316 value->area[value->data] = '\0';
3317
3318 /* perform update */
3319 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3320 if (pat_ref_find_elt(ref, key->area) != NULL)
3321 /* update entry if it exists */
3322 pat_ref_set(ref, key->area, value->area, NULL);
3323 else
3324 /* insert a new entry */
3325 pat_ref_add(ref, key->area, value->area, NULL);
3326 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3327 free_trash_chunk(key);
3328 free_trash_chunk(value);
3329 break;
3330 }
3331
3332 case ACT_HTTP_REDIR:
3333 rule_ret = HTTP_RULE_RES_DONE;
3334 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3335 rule_ret = HTTP_RULE_RES_BADREQ;
3336 goto end;
3337
3338 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3339 /* Note: only the first valid tracking parameter of each
3340 * applies.
3341 */
3342 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3343 struct stktable *t;
3344 struct stksess *ts;
3345 struct stktable_key *key;
3346 void *ptr;
3347
3348 t = rule->arg.trk_ctr.table.t;
3349 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL,
3350 rule->arg.trk_ctr.expr, NULL);
3351
3352 if (key && (ts = stktable_get_entry(t, key))) {
3353 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3354
3355 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3356
3357 /* let's count a new HTTP request as it's the first time we do it */
3358 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3359 if (ptr)
3360 stktable_data_cast(ptr, http_req_cnt)++;
3361
3362 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3363 if (ptr)
3364 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3365 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3366
3367 /* When the client triggers a 4xx from the server, it's most often due
3368 * to a missing object or permission. These events should be tracked
3369 * because if they happen often, it may indicate a brute force or a
3370 * vulnerability scan. Normally this is done when receiving the response
3371 * but here we're tracking after this ought to have been done so we have
3372 * to do it on purpose.
3373 */
3374 if ((unsigned)(txn->status - 400) < 100) {
3375 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3376 if (ptr)
3377 stktable_data_cast(ptr, http_err_cnt)++;
3378
3379 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3380 if (ptr)
3381 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3382 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3383 }
3384
3385 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3386
3387 /* If data was modified, we need to touch to re-schedule sync */
3388 stktable_touch_local(t, ts, 0);
3389
3390 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3391 if (sess->fe != s->be)
3392 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3393 }
3394 }
3395 break;
3396
3397 case ACT_CUSTOM:
3398 if ((s->req.flags & CF_READ_ERROR) ||
3399 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Christopher Faulet3e964192018-10-24 11:39:23 +02003400 (px->options & PR_O_ABRT_CLOSE)))
3401 act_flags |= ACT_FLAG_FINAL;
3402
3403 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
3404 case ACT_RET_ERR:
3405 case ACT_RET_CONT:
3406 break;
3407 case ACT_RET_STOP:
3408 rule_ret = HTTP_RULE_RES_STOP;
3409 goto end;
3410 case ACT_RET_YIELD:
3411 s->current_rule = rule;
3412 rule_ret = HTTP_RULE_RES_YIELD;
3413 goto end;
3414 }
3415 break;
3416
Joseph Herlantc42c0e92018-11-25 10:43:27 -08003417 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003418 default:
3419 break;
3420 }
3421 }
3422
3423 end:
3424 /* we reached the end of the rules, nothing to report */
3425 return rule_ret;
3426}
3427
Christopher Faulet33640082018-10-24 11:53:01 +02003428/* Iterate the same filter through all request headers.
3429 * Returns 1 if this filter can be stopped upon return, otherwise 0.
3430 * Since it can manage the switch to another backend, it updates the per-proxy
3431 * DENY stats.
3432 */
3433static int htx_apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
3434{
3435 struct http_txn *txn = s->txn;
3436 struct htx *htx;
3437 struct buffer *hdr = get_trash_chunk();
3438 int32_t pos;
3439
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003440 htx = htxbuf(&req->buf);
Christopher Faulet33640082018-10-24 11:53:01 +02003441
3442 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3443 struct htx_blk *blk = htx_get_blk(htx, pos);
3444 enum htx_blk_type type;
3445 struct ist n, v;
3446
3447 next_hdr:
3448 type = htx_get_blk_type(blk);
3449 if (type == HTX_BLK_EOH)
3450 break;
3451 if (type != HTX_BLK_HDR)
3452 continue;
3453
3454 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
3455 return 1;
3456 else if (unlikely(txn->flags & TX_CLALLOW) &&
3457 (exp->action == ACT_ALLOW ||
3458 exp->action == ACT_DENY ||
3459 exp->action == ACT_TARPIT))
3460 return 0;
3461
3462 n = htx_get_blk_name(htx, blk);
3463 v = htx_get_blk_value(htx, blk);
3464
Christopher Faulet02e771a2019-02-26 15:36:05 +01003465 chunk_memcpy(hdr, n.ptr, n.len);
Christopher Faulet33640082018-10-24 11:53:01 +02003466 hdr->area[hdr->data++] = ':';
3467 hdr->area[hdr->data++] = ' ';
3468 chunk_memcat(hdr, v.ptr, v.len);
3469
3470 /* Now we have one header in <hdr> */
3471
3472 if (regex_exec_match2(exp->preg, hdr->area, hdr->data, MAX_MATCH, pmatch, 0)) {
3473 struct http_hdr_ctx ctx;
3474 int len;
3475
3476 switch (exp->action) {
3477 case ACT_ALLOW:
3478 txn->flags |= TX_CLALLOW;
3479 goto end;
3480
3481 case ACT_DENY:
3482 txn->flags |= TX_CLDENY;
3483 goto end;
3484
3485 case ACT_TARPIT:
3486 txn->flags |= TX_CLTARPIT;
3487 goto end;
3488
3489 case ACT_REPLACE:
3490 len = exp_replace(trash.area, trash.size, hdr->area, exp->replace, pmatch);
3491 if (len < 0)
3492 return -1;
3493
3494 http_parse_header(ist2(trash.area, len), &n, &v);
3495 ctx.blk = blk;
3496 ctx.value = v;
Christopher Faulet02e771a2019-02-26 15:36:05 +01003497 ctx.lws_before = ctx.lws_after = 0;
Christopher Faulet33640082018-10-24 11:53:01 +02003498 if (!http_replace_header(htx, &ctx, n, v))
3499 return -1;
3500 if (!ctx.blk)
3501 goto end;
3502 pos = htx_get_blk_pos(htx, blk);
3503 break;
3504
3505 case ACT_REMOVE:
3506 ctx.blk = blk;
3507 ctx.value = v;
Christopher Faulet02e771a2019-02-26 15:36:05 +01003508 ctx.lws_before = ctx.lws_after = 0;
Christopher Faulet33640082018-10-24 11:53:01 +02003509 if (!http_remove_header(htx, &ctx))
3510 return -1;
3511 if (!ctx.blk)
3512 goto end;
3513 pos = htx_get_blk_pos(htx, blk);
3514 goto next_hdr;
3515
3516 }
3517 }
3518 }
3519 end:
3520 return 0;
3521}
3522
3523/* Apply the filter to the request line.
3524 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3525 * or -1 if a replacement resulted in an invalid request line.
3526 * Since it can manage the switch to another backend, it updates the per-proxy
3527 * DENY stats.
3528 */
3529static int htx_apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
3530{
3531 struct http_txn *txn = s->txn;
3532 struct htx *htx;
3533 struct buffer *reqline = get_trash_chunk();
3534 int done;
3535
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003536 htx = htxbuf(&req->buf);
Christopher Faulet33640082018-10-24 11:53:01 +02003537
3538 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
3539 return 1;
3540 else if (unlikely(txn->flags & TX_CLALLOW) &&
3541 (exp->action == ACT_ALLOW ||
3542 exp->action == ACT_DENY ||
3543 exp->action == ACT_TARPIT))
3544 return 0;
3545 else if (exp->action == ACT_REMOVE)
3546 return 0;
3547
3548 done = 0;
3549
3550 reqline->data = htx_fmt_req_line(http_find_stline(htx), reqline->area, reqline->size);
3551
3552 /* Now we have the request line between cur_ptr and cur_end */
3553 if (regex_exec_match2(exp->preg, reqline->area, reqline->data, MAX_MATCH, pmatch, 0)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003554 struct htx_sl *sl = http_find_stline(htx);
3555 struct ist meth, uri, vsn;
Christopher Faulet33640082018-10-24 11:53:01 +02003556 int len;
3557
3558 switch (exp->action) {
3559 case ACT_ALLOW:
3560 txn->flags |= TX_CLALLOW;
3561 done = 1;
3562 break;
3563
3564 case ACT_DENY:
3565 txn->flags |= TX_CLDENY;
3566 done = 1;
3567 break;
3568
3569 case ACT_TARPIT:
3570 txn->flags |= TX_CLTARPIT;
3571 done = 1;
3572 break;
3573
3574 case ACT_REPLACE:
3575 len = exp_replace(trash.area, trash.size, reqline->area, exp->replace, pmatch);
3576 if (len < 0)
3577 return -1;
3578
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003579 http_parse_stline(ist2(trash.area, len), &meth, &uri, &vsn);
3580 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
3581 if (!http_replace_stline(htx, meth, uri, vsn))
Christopher Faulet33640082018-10-24 11:53:01 +02003582 return -1;
3583 done = 1;
3584 break;
3585 }
3586 }
3587 return done;
3588}
3589
3590/*
3591 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
3592 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
3593 * unparsable request. Since it can manage the switch to another backend, it
3594 * updates the per-proxy DENY stats.
3595 */
3596static int htx_apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
3597{
3598 struct session *sess = s->sess;
3599 struct http_txn *txn = s->txn;
3600 struct hdr_exp *exp;
3601
3602 for (exp = px->req_exp; exp; exp = exp->next) {
3603 int ret;
3604
3605 /*
3606 * The interleaving of transformations and verdicts
3607 * makes it difficult to decide to continue or stop
3608 * the evaluation.
3609 */
3610
3611 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
3612 break;
3613
3614 if ((txn->flags & TX_CLALLOW) &&
3615 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3616 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
3617 continue;
3618
3619 /* if this filter had a condition, evaluate it now and skip to
3620 * next filter if the condition does not match.
3621 */
3622 if (exp->cond) {
3623 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3624 ret = acl_pass(ret);
3625 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
3626 ret = !ret;
3627
3628 if (!ret)
3629 continue;
3630 }
3631
3632 /* Apply the filter to the request line. */
3633 ret = htx_apply_filter_to_req_line(s, req, exp);
3634 if (unlikely(ret < 0))
3635 return -1;
3636
3637 if (likely(ret == 0)) {
3638 /* The filter did not match the request, it can be
3639 * iterated through all headers.
3640 */
3641 if (unlikely(htx_apply_filter_to_req_headers(s, req, exp) < 0))
3642 return -1;
3643 }
3644 }
3645 return 0;
3646}
3647
3648/* Iterate the same filter through all response headers contained in <res>.
3649 * Returns 1 if this filter can be stopped upon return, otherwise 0.
3650 */
3651static int htx_apply_filter_to_resp_headers(struct stream *s, struct channel *res, struct hdr_exp *exp)
3652{
3653 struct http_txn *txn = s->txn;
3654 struct htx *htx;
3655 struct buffer *hdr = get_trash_chunk();
3656 int32_t pos;
3657
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003658 htx = htxbuf(&res->buf);
Christopher Faulet33640082018-10-24 11:53:01 +02003659
3660 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3661 struct htx_blk *blk = htx_get_blk(htx, pos);
3662 enum htx_blk_type type;
3663 struct ist n, v;
3664
3665 next_hdr:
3666 type = htx_get_blk_type(blk);
3667 if (type == HTX_BLK_EOH)
3668 break;
3669 if (type != HTX_BLK_HDR)
3670 continue;
3671
3672 if (unlikely(txn->flags & TX_SVDENY))
3673 return 1;
3674 else if (unlikely(txn->flags & TX_SVALLOW) &&
3675 (exp->action == ACT_ALLOW ||
3676 exp->action == ACT_DENY))
3677 return 0;
3678
3679 n = htx_get_blk_name(htx, blk);
3680 v = htx_get_blk_value(htx, blk);
3681
Christopher Faulet02e771a2019-02-26 15:36:05 +01003682 chunk_memcpy(hdr, n.ptr, n.len);
Christopher Faulet33640082018-10-24 11:53:01 +02003683 hdr->area[hdr->data++] = ':';
3684 hdr->area[hdr->data++] = ' ';
3685 chunk_memcat(hdr, v.ptr, v.len);
3686
3687 /* Now we have one header in <hdr> */
3688
3689 if (regex_exec_match2(exp->preg, hdr->area, hdr->data, MAX_MATCH, pmatch, 0)) {
3690 struct http_hdr_ctx ctx;
3691 int len;
3692
3693 switch (exp->action) {
3694 case ACT_ALLOW:
3695 txn->flags |= TX_SVALLOW;
3696 goto end;
3697 break;
3698
3699 case ACT_DENY:
3700 txn->flags |= TX_SVDENY;
3701 goto end;
3702 break;
3703
3704 case ACT_REPLACE:
3705 len = exp_replace(trash.area, trash.size, hdr->area, exp->replace, pmatch);
3706 if (len < 0)
3707 return -1;
3708
3709 http_parse_header(ist2(trash.area, len), &n, &v);
3710 ctx.blk = blk;
3711 ctx.value = v;
Christopher Faulet02e771a2019-02-26 15:36:05 +01003712 ctx.lws_before = ctx.lws_after = 0;
Christopher Faulet33640082018-10-24 11:53:01 +02003713 if (!http_replace_header(htx, &ctx, n, v))
3714 return -1;
3715 if (!ctx.blk)
3716 goto end;
3717 pos = htx_get_blk_pos(htx, blk);
3718 break;
3719
3720 case ACT_REMOVE:
3721 ctx.blk = blk;
3722 ctx.value = v;
Christopher Faulet02e771a2019-02-26 15:36:05 +01003723 ctx.lws_before = ctx.lws_after = 0;
Christopher Faulet33640082018-10-24 11:53:01 +02003724 if (!http_remove_header(htx, &ctx))
3725 return -1;
3726 if (!ctx.blk)
3727 goto end;
3728 pos = htx_get_blk_pos(htx, blk);
3729 goto next_hdr;
3730 }
3731 }
3732
3733 }
3734 end:
3735 return 0;
3736}
3737
3738/* Apply the filter to the status line in the response buffer <res>.
3739 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3740 * or -1 if a replacement resulted in an invalid status line.
3741 */
3742static int htx_apply_filter_to_sts_line(struct stream *s, struct channel *res, struct hdr_exp *exp)
3743{
3744 struct http_txn *txn = s->txn;
3745 struct htx *htx;
3746 struct buffer *resline = get_trash_chunk();
3747 int done;
3748
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003749 htx = htxbuf(&res->buf);
Christopher Faulet33640082018-10-24 11:53:01 +02003750
3751 if (unlikely(txn->flags & TX_SVDENY))
3752 return 1;
3753 else if (unlikely(txn->flags & TX_SVALLOW) &&
3754 (exp->action == ACT_ALLOW ||
3755 exp->action == ACT_DENY))
3756 return 0;
3757 else if (exp->action == ACT_REMOVE)
3758 return 0;
3759
3760 done = 0;
3761 resline->data = htx_fmt_res_line(http_find_stline(htx), resline->area, resline->size);
3762
3763 /* Now we have the status line between cur_ptr and cur_end */
3764 if (regex_exec_match2(exp->preg, resline->area, resline->data, MAX_MATCH, pmatch, 0)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003765 struct htx_sl *sl = http_find_stline(htx);
3766 struct ist vsn, code, reason;
Christopher Faulet33640082018-10-24 11:53:01 +02003767 int len;
3768
3769 switch (exp->action) {
3770 case ACT_ALLOW:
3771 txn->flags |= TX_SVALLOW;
3772 done = 1;
3773 break;
3774
3775 case ACT_DENY:
3776 txn->flags |= TX_SVDENY;
3777 done = 1;
3778 break;
3779
3780 case ACT_REPLACE:
3781 len = exp_replace(trash.area, trash.size, resline->area, exp->replace, pmatch);
3782 if (len < 0)
3783 return -1;
3784
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003785 http_parse_stline(ist2(trash.area, len), &vsn, &code, &reason);
3786 sl->info.res.status = strl2ui(code.ptr, code.len);
3787 if (!http_replace_stline(htx, vsn, code, reason))
Christopher Faulet33640082018-10-24 11:53:01 +02003788 return -1;
3789
3790 done = 1;
3791 return 1;
3792 }
3793 }
3794 return done;
3795}
3796
3797/*
3798 * Apply all the resp filters of proxy <px> to all headers in buffer <res> of stream <s>.
3799 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
3800 * unparsable response.
3801 */
3802static int htx_apply_filters_to_response(struct stream *s, struct channel *res, struct proxy *px)
3803{
3804 struct session *sess = s->sess;
3805 struct http_txn *txn = s->txn;
3806 struct hdr_exp *exp;
3807
3808 for (exp = px->rsp_exp; exp; exp = exp->next) {
3809 int ret;
3810
3811 /*
3812 * The interleaving of transformations and verdicts
3813 * makes it difficult to decide to continue or stop
3814 * the evaluation.
3815 */
3816
3817 if (txn->flags & TX_SVDENY)
3818 break;
3819
3820 if ((txn->flags & TX_SVALLOW) &&
3821 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3822 exp->action == ACT_PASS)) {
3823 exp = exp->next;
3824 continue;
3825 }
3826
3827 /* if this filter had a condition, evaluate it now and skip to
3828 * next filter if the condition does not match.
3829 */
3830 if (exp->cond) {
3831 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3832 ret = acl_pass(ret);
3833 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
3834 ret = !ret;
3835 if (!ret)
3836 continue;
3837 }
3838
3839 /* Apply the filter to the status line. */
3840 ret = htx_apply_filter_to_sts_line(s, res, exp);
3841 if (unlikely(ret < 0))
3842 return -1;
3843
3844 if (likely(ret == 0)) {
3845 /* The filter did not match the response, it can be
3846 * iterated through all headers.
3847 */
3848 if (unlikely(htx_apply_filter_to_resp_headers(s, res, exp) < 0))
3849 return -1;
3850 }
3851 }
3852 return 0;
3853}
3854
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003855/*
3856 * Manage client-side cookie. It can impact performance by about 2% so it is
3857 * desirable to call it only when needed. This code is quite complex because
3858 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3859 * highly recommended not to touch this part without a good reason !
3860 */
3861static void htx_manage_client_side_cookies(struct stream *s, struct channel *req)
3862{
3863 struct session *sess = s->sess;
3864 struct http_txn *txn = s->txn;
3865 struct htx *htx;
3866 struct http_hdr_ctx ctx;
3867 char *hdr_beg, *hdr_end, *del_from;
3868 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3869 int preserve_hdr;
3870
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003871 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003872 ctx.blk = NULL;
3873 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
3874 del_from = NULL; /* nothing to be deleted */
3875 preserve_hdr = 0; /* assume we may kill the whole header */
3876
3877 /* Now look for cookies. Conforming to RFC2109, we have to support
3878 * attributes whose name begin with a '$', and associate them with
3879 * the right cookie, if we want to delete this cookie.
3880 * So there are 3 cases for each cookie read :
3881 * 1) it's a special attribute, beginning with a '$' : ignore it.
3882 * 2) it's a server id cookie that we *MAY* want to delete : save
3883 * some pointers on it (last semi-colon, beginning of cookie...)
3884 * 3) it's an application cookie : we *MAY* have to delete a previous
3885 * "special" cookie.
3886 * At the end of loop, if a "special" cookie remains, we may have to
3887 * remove it. If no application cookie persists in the header, we
3888 * *MUST* delete it.
3889 *
3890 * Note: RFC2965 is unclear about the processing of spaces around
3891 * the equal sign in the ATTR=VALUE form. A careful inspection of
3892 * the RFC explicitly allows spaces before it, and not within the
3893 * tokens (attrs or values). An inspection of RFC2109 allows that
3894 * too but section 10.1.3 lets one think that spaces may be allowed
3895 * after the equal sign too, resulting in some (rare) buggy
3896 * implementations trying to do that. So let's do what servers do.
3897 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3898 * allowed quoted strings in values, with any possible character
3899 * after a backslash, including control chars and delimitors, which
3900 * causes parsing to become ambiguous. Browsers also allow spaces
3901 * within values even without quotes.
3902 *
3903 * We have to keep multiple pointers in order to support cookie
3904 * removal at the beginning, middle or end of header without
3905 * corrupting the header. All of these headers are valid :
3906 *
3907 * hdr_beg hdr_end
3908 * | |
3909 * v |
3910 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3911 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3912 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3913 * | | | | | | |
3914 * | | | | | | |
3915 * | | | | | | +--> next
3916 * | | | | | +----> val_end
3917 * | | | | +-----------> val_beg
3918 * | | | +--------------> equal
3919 * | | +----------------> att_end
3920 * | +---------------------> att_beg
3921 * +--------------------------> prev
3922 *
3923 */
3924 hdr_beg = ctx.value.ptr;
3925 hdr_end = hdr_beg + ctx.value.len;
3926 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3927 /* Iterate through all cookies on this line */
3928
3929 /* find att_beg */
3930 att_beg = prev;
3931 if (prev > hdr_beg)
3932 att_beg++;
3933
3934 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3935 att_beg++;
3936
3937 /* find att_end : this is the first character after the last non
3938 * space before the equal. It may be equal to hdr_end.
3939 */
3940 equal = att_end = att_beg;
3941 while (equal < hdr_end) {
3942 if (*equal == '=' || *equal == ',' || *equal == ';')
3943 break;
3944 if (HTTP_IS_SPHT(*equal++))
3945 continue;
3946 att_end = equal;
3947 }
3948
3949 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3950 * is between <att_beg> and <equal>, both may be identical.
3951 */
3952 /* look for end of cookie if there is an equal sign */
3953 if (equal < hdr_end && *equal == '=') {
3954 /* look for the beginning of the value */
3955 val_beg = equal + 1;
3956 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3957 val_beg++;
3958
3959 /* find the end of the value, respecting quotes */
3960 next = http_find_cookie_value_end(val_beg, hdr_end);
3961
3962 /* make val_end point to the first white space or delimitor after the value */
3963 val_end = next;
3964 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3965 val_end--;
3966 }
3967 else
3968 val_beg = val_end = next = equal;
3969
3970 /* We have nothing to do with attributes beginning with
3971 * '$'. However, they will automatically be removed if a
3972 * header before them is removed, since they're supposed
3973 * to be linked together.
3974 */
3975 if (*att_beg == '$')
3976 continue;
3977
3978 /* Ignore cookies with no equal sign */
3979 if (equal == next) {
3980 /* This is not our cookie, so we must preserve it. But if we already
3981 * scheduled another cookie for removal, we cannot remove the
3982 * complete header, but we can remove the previous block itself.
3983 */
3984 preserve_hdr = 1;
3985 if (del_from != NULL) {
3986 int delta = htx_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
3987 val_end += delta;
3988 next += delta;
3989 hdr_end += delta;
3990 prev = del_from;
3991 del_from = NULL;
3992 }
3993 continue;
3994 }
3995
3996 /* if there are spaces around the equal sign, we need to
3997 * strip them otherwise we'll get trouble for cookie captures,
3998 * or even for rewrites. Since this happens extremely rarely,
3999 * it does not hurt performance.
4000 */
4001 if (unlikely(att_end != equal || val_beg > equal + 1)) {
4002 int stripped_before = 0;
4003 int stripped_after = 0;
4004
4005 if (att_end != equal) {
4006 memmove(att_end, equal, hdr_end - equal);
4007 stripped_before = (att_end - equal);
4008 equal += stripped_before;
4009 val_beg += stripped_before;
4010 }
4011
4012 if (val_beg > equal + 1) {
4013 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
4014 stripped_after = (equal + 1) - val_beg;
4015 val_beg += stripped_after;
4016 stripped_before += stripped_after;
4017 }
4018
4019 val_end += stripped_before;
4020 next += stripped_before;
4021 hdr_end += stripped_before;
4022 }
4023 /* now everything is as on the diagram above */
4024
4025 /* First, let's see if we want to capture this cookie. We check
4026 * that we don't already have a client side cookie, because we
4027 * can only capture one. Also as an optimisation, we ignore
4028 * cookies shorter than the declared name.
4029 */
4030 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
4031 (val_end - att_beg >= sess->fe->capture_namelen) &&
4032 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
4033 int log_len = val_end - att_beg;
4034
4035 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
4036 ha_alert("HTTP logging : out of memory.\n");
4037 } else {
4038 if (log_len > sess->fe->capture_len)
4039 log_len = sess->fe->capture_len;
4040 memcpy(txn->cli_cookie, att_beg, log_len);
4041 txn->cli_cookie[log_len] = 0;
4042 }
4043 }
4044
4045 /* Persistence cookies in passive, rewrite or insert mode have the
4046 * following form :
4047 *
4048 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
4049 *
4050 * For cookies in prefix mode, the form is :
4051 *
4052 * Cookie: NAME=SRV~VALUE
4053 */
4054 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
4055 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
4056 struct server *srv = s->be->srv;
4057 char *delim;
4058
4059 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4060 * have the server ID between val_beg and delim, and the original cookie between
4061 * delim+1 and val_end. Otherwise, delim==val_end :
4062 *
4063 * hdr_beg
4064 * |
4065 * v
4066 * NAME=SRV; # in all but prefix modes
4067 * NAME=SRV~OPAQUE ; # in prefix mode
4068 * || || | |+-> next
4069 * || || | +--> val_end
4070 * || || +---------> delim
4071 * || |+------------> val_beg
4072 * || +-------------> att_end = equal
4073 * |+-----------------> att_beg
4074 * +------------------> prev
4075 *
4076 */
4077 if (s->be->ck_opts & PR_CK_PFX) {
4078 for (delim = val_beg; delim < val_end; delim++)
4079 if (*delim == COOKIE_DELIM)
4080 break;
4081 }
4082 else {
4083 char *vbar1;
4084 delim = val_end;
4085 /* Now check if the cookie contains a date field, which would
4086 * appear after a vertical bar ('|') just after the server name
4087 * and before the delimiter.
4088 */
4089 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
4090 if (vbar1) {
4091 /* OK, so left of the bar is the server's cookie and
4092 * right is the last seen date. It is a base64 encoded
4093 * 30-bit value representing the UNIX date since the
4094 * epoch in 4-second quantities.
4095 */
4096 int val;
4097 delim = vbar1++;
4098 if (val_end - vbar1 >= 5) {
4099 val = b64tos30(vbar1);
4100 if (val > 0)
4101 txn->cookie_last_date = val << 2;
4102 }
4103 /* look for a second vertical bar */
4104 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
4105 if (vbar1 && (val_end - vbar1 > 5)) {
4106 val = b64tos30(vbar1 + 1);
4107 if (val > 0)
4108 txn->cookie_first_date = val << 2;
4109 }
4110 }
4111 }
4112
4113 /* if the cookie has an expiration date and the proxy wants to check
4114 * it, then we do that now. We first check if the cookie is too old,
4115 * then only if it has expired. We detect strict overflow because the
4116 * time resolution here is not great (4 seconds). Cookies with dates
4117 * in the future are ignored if their offset is beyond one day. This
4118 * allows an admin to fix timezone issues without expiring everyone
4119 * and at the same time avoids keeping unwanted side effects for too
4120 * long.
4121 */
4122 if (txn->cookie_first_date && s->be->cookie_maxlife &&
4123 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
4124 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
4125 txn->flags &= ~TX_CK_MASK;
4126 txn->flags |= TX_CK_OLD;
4127 delim = val_beg; // let's pretend we have not found the cookie
4128 txn->cookie_first_date = 0;
4129 txn->cookie_last_date = 0;
4130 }
4131 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
4132 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
4133 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
4134 txn->flags &= ~TX_CK_MASK;
4135 txn->flags |= TX_CK_EXPIRED;
4136 delim = val_beg; // let's pretend we have not found the cookie
4137 txn->cookie_first_date = 0;
4138 txn->cookie_last_date = 0;
4139 }
4140
4141 /* Here, we'll look for the first running server which supports the cookie.
4142 * This allows to share a same cookie between several servers, for example
4143 * to dedicate backup servers to specific servers only.
4144 * However, to prevent clients from sticking to cookie-less backup server
4145 * when they have incidentely learned an empty cookie, we simply ignore
4146 * empty cookies and mark them as invalid.
4147 * The same behaviour is applied when persistence must be ignored.
4148 */
4149 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
4150 srv = NULL;
4151
4152 while (srv) {
4153 if (srv->cookie && (srv->cklen == delim - val_beg) &&
4154 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
4155 if ((srv->cur_state != SRV_ST_STOPPED) ||
4156 (s->be->options & PR_O_PERSIST) ||
4157 (s->flags & SF_FORCE_PRST)) {
4158 /* we found the server and we can use it */
4159 txn->flags &= ~TX_CK_MASK;
4160 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
4161 s->flags |= SF_DIRECT | SF_ASSIGNED;
4162 s->target = &srv->obj_type;
4163 break;
4164 } else {
4165 /* we found a server, but it's down,
4166 * mark it as such and go on in case
4167 * another one is available.
4168 */
4169 txn->flags &= ~TX_CK_MASK;
4170 txn->flags |= TX_CK_DOWN;
4171 }
4172 }
4173 srv = srv->next;
4174 }
4175
4176 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
4177 /* no server matched this cookie or we deliberately skipped it */
4178 txn->flags &= ~TX_CK_MASK;
4179 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
4180 txn->flags |= TX_CK_UNUSED;
4181 else
4182 txn->flags |= TX_CK_INVALID;
4183 }
4184
4185 /* depending on the cookie mode, we may have to either :
4186 * - delete the complete cookie if we're in insert+indirect mode, so that
4187 * the server never sees it ;
4188 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08004189 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004190 * if we're in cookie prefix mode
4191 */
4192 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
4193 int delta; /* negative */
4194
4195 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
4196 delta = val_beg - (delim + 1);
4197 val_end += delta;
4198 next += delta;
4199 hdr_end += delta;
4200 del_from = NULL;
4201 preserve_hdr = 1; /* we want to keep this cookie */
4202 }
4203 else if (del_from == NULL &&
4204 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
4205 del_from = prev;
4206 }
4207 }
4208 else {
4209 /* This is not our cookie, so we must preserve it. But if we already
4210 * scheduled another cookie for removal, we cannot remove the
4211 * complete header, but we can remove the previous block itself.
4212 */
4213 preserve_hdr = 1;
4214
4215 if (del_from != NULL) {
4216 int delta = htx_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
4217 if (att_beg >= del_from)
4218 att_beg += delta;
4219 if (att_end >= del_from)
4220 att_end += delta;
4221 val_beg += delta;
4222 val_end += delta;
4223 next += delta;
4224 hdr_end += delta;
4225 prev = del_from;
4226 del_from = NULL;
4227 }
4228 }
4229
4230 /* continue with next cookie on this header line */
4231 att_beg = next;
4232 } /* for each cookie */
4233
4234
4235 /* There are no more cookies on this line.
4236 * We may still have one (or several) marked for deletion at the
4237 * end of the line. We must do this now in two ways :
4238 * - if some cookies must be preserved, we only delete from the
4239 * mark to the end of line ;
4240 * - if nothing needs to be preserved, simply delete the whole header
4241 */
4242 if (del_from) {
4243 hdr_end = (preserve_hdr ? del_from : hdr_beg);
4244 }
4245 if ((hdr_end - hdr_beg) != ctx.value.len) {
4246 if (hdr_beg != hdr_end) {
4247 htx_set_blk_value_len(ctx.blk, hdr_end - hdr_beg);
Christopher Faulet6cdaf2a2019-02-12 14:29:57 +01004248 htx->data -= ctx.value.len - (hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004249 }
4250 else
4251 http_remove_header(htx, &ctx);
4252 }
4253 } /* for each "Cookie header */
4254}
4255
4256/*
4257 * Manage server-side cookies. It can impact performance by about 2% so it is
4258 * desirable to call it only when needed. This function is also used when we
4259 * just need to know if there is a cookie (eg: for check-cache).
4260 */
4261static void htx_manage_server_side_cookies(struct stream *s, struct channel *res)
4262{
4263 struct session *sess = s->sess;
4264 struct http_txn *txn = s->txn;
4265 struct htx *htx;
4266 struct http_hdr_ctx ctx;
4267 struct server *srv;
4268 char *hdr_beg, *hdr_end;
4269 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02004270 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004271
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004272 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004273
4274 ctx.blk = NULL;
4275 while (1) {
4276 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
4277 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
4278 break;
4279 is_cookie2 = 1;
4280 }
4281
4282 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
4283 * <prev> points to the colon.
4284 */
4285 txn->flags |= TX_SCK_PRESENT;
4286
4287 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
4288 * check-cache is enabled) and we are not interested in checking
4289 * them. Warning, the cookie capture is declared in the frontend.
4290 */
4291 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
4292 break;
4293
4294 /* OK so now we know we have to process this response cookie.
4295 * The format of the Set-Cookie header is slightly different
4296 * from the format of the Cookie header in that it does not
4297 * support the comma as a cookie delimiter (thus the header
4298 * cannot be folded) because the Expires attribute described in
4299 * the original Netscape's spec may contain an unquoted date
4300 * with a comma inside. We have to live with this because
4301 * many browsers don't support Max-Age and some browsers don't
4302 * support quoted strings. However the Set-Cookie2 header is
4303 * clean.
4304 *
4305 * We have to keep multiple pointers in order to support cookie
4306 * removal at the beginning, middle or end of header without
4307 * corrupting the header (in case of set-cookie2). A special
4308 * pointer, <scav> points to the beginning of the set-cookie-av
4309 * fields after the first semi-colon. The <next> pointer points
4310 * either to the end of line (set-cookie) or next unquoted comma
4311 * (set-cookie2). All of these headers are valid :
4312 *
4313 * hdr_beg hdr_end
4314 * | |
4315 * v |
4316 * NAME1 = VALUE 1 ; Secure; Path="/" |
4317 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
4318 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
4319 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
4320 * | | | | | | | |
4321 * | | | | | | | +-> next
4322 * | | | | | | +------------> scav
4323 * | | | | | +--------------> val_end
4324 * | | | | +--------------------> val_beg
4325 * | | | +----------------------> equal
4326 * | | +------------------------> att_end
4327 * | +----------------------------> att_beg
4328 * +------------------------------> prev
4329 * -------------------------------> hdr_beg
4330 */
4331 hdr_beg = ctx.value.ptr;
4332 hdr_end = hdr_beg + ctx.value.len;
4333 for (prev = hdr_beg; prev < hdr_end; prev = next) {
4334
4335 /* Iterate through all cookies on this line */
4336
4337 /* find att_beg */
4338 att_beg = prev;
4339 if (prev > hdr_beg)
4340 att_beg++;
4341
4342 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
4343 att_beg++;
4344
4345 /* find att_end : this is the first character after the last non
4346 * space before the equal. It may be equal to hdr_end.
4347 */
4348 equal = att_end = att_beg;
4349
4350 while (equal < hdr_end) {
4351 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
4352 break;
4353 if (HTTP_IS_SPHT(*equal++))
4354 continue;
4355 att_end = equal;
4356 }
4357
4358 /* here, <equal> points to '=', a delimitor or the end. <att_end>
4359 * is between <att_beg> and <equal>, both may be identical.
4360 */
4361
4362 /* look for end of cookie if there is an equal sign */
4363 if (equal < hdr_end && *equal == '=') {
4364 /* look for the beginning of the value */
4365 val_beg = equal + 1;
4366 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
4367 val_beg++;
4368
4369 /* find the end of the value, respecting quotes */
4370 next = http_find_cookie_value_end(val_beg, hdr_end);
4371
4372 /* make val_end point to the first white space or delimitor after the value */
4373 val_end = next;
4374 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
4375 val_end--;
4376 }
4377 else {
4378 /* <equal> points to next comma, semi-colon or EOL */
4379 val_beg = val_end = next = equal;
4380 }
4381
4382 if (next < hdr_end) {
4383 /* Set-Cookie2 supports multiple cookies, and <next> points to
4384 * a colon or semi-colon before the end. So skip all attr-value
4385 * pairs and look for the next comma. For Set-Cookie, since
4386 * commas are permitted in values, skip to the end.
4387 */
4388 if (is_cookie2)
4389 next = http_find_hdr_value_end(next, hdr_end);
4390 else
4391 next = hdr_end;
4392 }
4393
4394 /* Now everything is as on the diagram above */
4395
4396 /* Ignore cookies with no equal sign */
4397 if (equal == val_end)
4398 continue;
4399
4400 /* If there are spaces around the equal sign, we need to
4401 * strip them otherwise we'll get trouble for cookie captures,
4402 * or even for rewrites. Since this happens extremely rarely,
4403 * it does not hurt performance.
4404 */
4405 if (unlikely(att_end != equal || val_beg > equal + 1)) {
4406 int stripped_before = 0;
4407 int stripped_after = 0;
4408
4409 if (att_end != equal) {
4410 memmove(att_end, equal, hdr_end - equal);
4411 stripped_before = (att_end - equal);
4412 equal += stripped_before;
4413 val_beg += stripped_before;
4414 }
4415
4416 if (val_beg > equal + 1) {
4417 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
4418 stripped_after = (equal + 1) - val_beg;
4419 val_beg += stripped_after;
4420 stripped_before += stripped_after;
4421 }
4422
4423 val_end += stripped_before;
4424 next += stripped_before;
4425 hdr_end += stripped_before;
4426
Christopher Faulet6cdaf2a2019-02-12 14:29:57 +01004427 htx_set_blk_value_len(ctx.blk, hdr_end - hdr_beg);
4428 htx->data -= ctx.value.len - (hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004429 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004430 }
4431
4432 /* First, let's see if we want to capture this cookie. We check
4433 * that we don't already have a server side cookie, because we
4434 * can only capture one. Also as an optimisation, we ignore
4435 * cookies shorter than the declared name.
4436 */
4437 if (sess->fe->capture_name != NULL &&
4438 txn->srv_cookie == NULL &&
4439 (val_end - att_beg >= sess->fe->capture_namelen) &&
4440 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
4441 int log_len = val_end - att_beg;
4442 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
4443 ha_alert("HTTP logging : out of memory.\n");
4444 }
4445 else {
4446 if (log_len > sess->fe->capture_len)
4447 log_len = sess->fe->capture_len;
4448 memcpy(txn->srv_cookie, att_beg, log_len);
4449 txn->srv_cookie[log_len] = 0;
4450 }
4451 }
4452
4453 srv = objt_server(s->target);
4454 /* now check if we need to process it for persistence */
4455 if (!(s->flags & SF_IGNORE_PRST) &&
4456 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
4457 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
4458 /* assume passive cookie by default */
4459 txn->flags &= ~TX_SCK_MASK;
4460 txn->flags |= TX_SCK_FOUND;
4461
4462 /* If the cookie is in insert mode on a known server, we'll delete
4463 * this occurrence because we'll insert another one later.
4464 * We'll delete it too if the "indirect" option is set and we're in
4465 * a direct access.
4466 */
4467 if (s->be->ck_opts & PR_CK_PSV) {
4468 /* The "preserve" flag was set, we don't want to touch the
4469 * server's cookie.
4470 */
4471 }
4472 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
4473 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
4474 /* this cookie must be deleted */
4475 if (prev == hdr_beg && next == hdr_end) {
4476 /* whole header */
4477 http_remove_header(htx, &ctx);
4478 /* note: while both invalid now, <next> and <hdr_end>
4479 * are still equal, so the for() will stop as expected.
4480 */
4481 } else {
4482 /* just remove the value */
4483 int delta = htx_del_hdr_value(hdr_beg, hdr_end, &prev, next);
4484 next = prev;
4485 hdr_end += delta;
4486 }
4487 txn->flags &= ~TX_SCK_MASK;
4488 txn->flags |= TX_SCK_DELETED;
4489 /* and go on with next cookie */
4490 }
4491 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
4492 /* replace bytes val_beg->val_end with the cookie name associated
4493 * with this server since we know it.
4494 */
4495 int sliding, delta;
4496
4497 ctx.value = ist2(val_beg, val_end - val_beg);
4498 ctx.lws_before = ctx.lws_after = 0;
4499 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
4500 delta = srv->cklen - (val_end - val_beg);
4501 sliding = (ctx.value.ptr - val_beg);
4502 hdr_beg += sliding;
4503 val_beg += sliding;
4504 next += sliding + delta;
4505 hdr_end += sliding + delta;
4506
4507 txn->flags &= ~TX_SCK_MASK;
4508 txn->flags |= TX_SCK_REPLACED;
4509 }
4510 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
4511 /* insert the cookie name associated with this server
4512 * before existing cookie, and insert a delimiter between them..
4513 */
4514 int sliding, delta;
4515 ctx.value = ist2(val_beg, 0);
4516 ctx.lws_before = ctx.lws_after = 0;
4517 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
4518 delta = srv->cklen + 1;
4519 sliding = (ctx.value.ptr - val_beg);
4520 hdr_beg += sliding;
4521 val_beg += sliding;
4522 next += sliding + delta;
4523 hdr_end += sliding + delta;
4524
4525 val_beg[srv->cklen] = COOKIE_DELIM;
4526 txn->flags &= ~TX_SCK_MASK;
4527 txn->flags |= TX_SCK_REPLACED;
4528 }
4529 }
4530 /* that's done for this cookie, check the next one on the same
4531 * line when next != hdr_end (only if is_cookie2).
4532 */
4533 }
4534 }
4535}
4536
Christopher Faulet25a02f62018-10-24 12:00:25 +02004537/*
4538 * Parses the Cache-Control and Pragma request header fields to determine if
4539 * the request may be served from the cache and/or if it is cacheable. Updates
4540 * s->txn->flags.
4541 */
4542void htx_check_request_for_cacheability(struct stream *s, struct channel *req)
4543{
4544 struct http_txn *txn = s->txn;
4545 struct htx *htx;
4546 int32_t pos;
4547 int pragma_found, cc_found, i;
4548
4549 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
4550 return; /* nothing more to do here */
4551
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004552 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02004553 pragma_found = cc_found = 0;
4554 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4555 struct htx_blk *blk = htx_get_blk(htx, pos);
4556 enum htx_blk_type type = htx_get_blk_type(blk);
4557 struct ist n, v;
4558
4559 if (type == HTX_BLK_EOH)
4560 break;
4561 if (type != HTX_BLK_HDR)
4562 continue;
4563
4564 n = htx_get_blk_name(htx, blk);
4565 v = htx_get_blk_value(htx, blk);
4566
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004567 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004568 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4569 pragma_found = 1;
4570 continue;
4571 }
4572 }
4573
4574 /* Don't use the cache and don't try to store if we found the
4575 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004576 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004577 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4578 txn->flags |= TX_CACHE_IGNORE;
4579 continue;
4580 }
4581
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004582 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004583 continue;
4584
4585 /* OK, right now we know we have a cache-control header */
4586 cc_found = 1;
4587 if (!v.len) /* no info */
4588 continue;
4589
4590 i = 0;
4591 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4592 !isspace((unsigned char)*(v.ptr+i)))
4593 i++;
4594
4595 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
4596 * values after max-age, max-stale nor min-fresh, we simply don't
4597 * use the cache when they're specified.
4598 */
4599 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
4600 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4601 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
4602 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
4603 txn->flags |= TX_CACHE_IGNORE;
4604 continue;
4605 }
4606
4607 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
4608 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4609 continue;
4610 }
4611 }
4612
4613 /* RFC7234#5.4:
4614 * When the Cache-Control header field is also present and
4615 * understood in a request, Pragma is ignored.
4616 * When the Cache-Control header field is not present in a
4617 * request, caches MUST consider the no-cache request
4618 * pragma-directive as having the same effect as if
4619 * "Cache-Control: no-cache" were present.
4620 */
4621 if (!cc_found && pragma_found)
4622 txn->flags |= TX_CACHE_IGNORE;
4623}
4624
4625/*
4626 * Check if response is cacheable or not. Updates s->txn->flags.
4627 */
4628void htx_check_response_for_cacheability(struct stream *s, struct channel *res)
4629{
4630 struct http_txn *txn = s->txn;
4631 struct htx *htx;
4632 int32_t pos;
4633 int i;
4634
4635 if (txn->status < 200) {
4636 /* do not try to cache interim responses! */
4637 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4638 return;
4639 }
4640
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004641 htx = htxbuf(&res->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02004642 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4643 struct htx_blk *blk = htx_get_blk(htx, pos);
4644 enum htx_blk_type type = htx_get_blk_type(blk);
4645 struct ist n, v;
4646
4647 if (type == HTX_BLK_EOH)
4648 break;
4649 if (type != HTX_BLK_HDR)
4650 continue;
4651
4652 n = htx_get_blk_name(htx, blk);
4653 v = htx_get_blk_value(htx, blk);
4654
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004655 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004656 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4657 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4658 return;
4659 }
4660 }
4661
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004662 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004663 continue;
4664
4665 /* OK, right now we know we have a cache-control header */
4666 if (!v.len) /* no info */
4667 continue;
4668
4669 i = 0;
4670 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4671 !isspace((unsigned char)*(v.ptr+i)))
4672 i++;
4673
4674 /* we have a complete value between v.ptr and (v.ptr+i) */
4675 if (i < v.len && *(v.ptr + i) == '=') {
4676 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
4677 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
4678 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4679 continue;
4680 }
4681
4682 /* we have something of the form no-cache="set-cookie" */
4683 if ((v.len >= 21) &&
4684 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
4685 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
4686 txn->flags &= ~TX_CACHE_COOK;
4687 continue;
4688 }
4689
4690 /* OK, so we know that either p2 points to the end of string or to a comma */
4691 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
4692 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4693 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
4694 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4695 return;
4696 }
4697
4698 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4699 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4700 continue;
4701 }
4702 }
4703}
4704
Christopher Faulet64159df2018-10-24 21:15:35 +02004705/* send a server's name with an outgoing request over an established connection.
4706 * Note: this function is designed to be called once the request has been
4707 * scheduled for being forwarded. This is the reason why the number of forwarded
4708 * bytes have to be adjusted.
4709 */
4710int htx_send_name_header(struct stream *s, struct proxy *be, const char *srv_name)
4711{
4712 struct htx *htx;
4713 struct http_hdr_ctx ctx;
4714 struct ist hdr;
4715 uint32_t data;
4716
4717 hdr = ist2(be->server_id_hdr_name, be->server_id_hdr_len);
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004718 htx = htxbuf(&s->req.buf);
Christopher Faulet64159df2018-10-24 21:15:35 +02004719 data = htx->data;
4720
4721 ctx.blk = NULL;
4722 while (http_find_header(htx, hdr, &ctx, 1))
4723 http_remove_header(htx, &ctx);
4724 http_add_header(htx, hdr, ist2(srv_name, strlen(srv_name)));
4725
4726 if (co_data(&s->req)) {
4727 if (data >= htx->data)
4728 c_rew(&s->req, data - htx->data);
4729 else
4730 c_adv(&s->req, htx->data - data);
4731 }
4732 return 0;
4733}
4734
Christopher Faulet377c5a52018-10-24 21:21:30 +02004735/*
4736 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4737 * for the current backend.
4738 *
4739 * It is assumed that the request is either a HEAD, GET, or POST and that the
4740 * uri_auth field is valid.
4741 *
4742 * Returns 1 if stats should be provided, otherwise 0.
4743 */
4744static int htx_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
4745{
4746 struct uri_auth *uri_auth = backend->uri_auth;
4747 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004748 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004749 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004750
4751 if (!uri_auth)
4752 return 0;
4753
4754 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4755 return 0;
4756
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004757 htx = htxbuf(&s->req.buf);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004758 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004759 uri = htx_sl_req_uri(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004760
4761 /* check URI size */
4762 if (uri_auth->uri_len > uri.len)
4763 return 0;
4764
4765 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4766 return 0;
4767
4768 return 1;
4769}
4770
4771/* This function prepares an applet to handle the stats. It can deal with the
4772 * "100-continue" expectation, check that admin rules are met for POST requests,
4773 * and program a response message if something was unexpected. It cannot fail
4774 * and always relies on the stats applet to complete the job. It does not touch
4775 * analysers nor counters, which are left to the caller. It does not touch
4776 * s->target which is supposed to already point to the stats applet. The caller
4777 * is expected to have already assigned an appctx to the stream.
4778 */
4779static int htx_handle_stats(struct stream *s, struct channel *req)
4780{
4781 struct stats_admin_rule *stats_admin_rule;
4782 struct stream_interface *si = &s->si[1];
4783 struct session *sess = s->sess;
4784 struct http_txn *txn = s->txn;
4785 struct http_msg *msg = &txn->req;
4786 struct uri_auth *uri_auth = s->be->uri_auth;
4787 const char *h, *lookup, *end;
4788 struct appctx *appctx;
4789 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004790 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004791
4792 appctx = si_appctx(si);
4793 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4794 appctx->st1 = appctx->st2 = 0;
4795 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
4796 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4797 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4798 appctx->ctx.stats.flags |= STAT_CHUNKED;
4799
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004800 htx = htxbuf(&req->buf);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004801 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004802 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4803 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004804
4805 for (h = lookup; h <= end - 3; h++) {
4806 if (memcmp(h, ";up", 3) == 0) {
4807 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4808 break;
4809 }
4810 }
4811
4812 if (uri_auth->refresh) {
4813 for (h = lookup; h <= end - 10; h++) {
4814 if (memcmp(h, ";norefresh", 10) == 0) {
4815 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4816 break;
4817 }
4818 }
4819 }
4820
4821 for (h = lookup; h <= end - 4; h++) {
4822 if (memcmp(h, ";csv", 4) == 0) {
4823 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
4824 break;
4825 }
4826 }
4827
4828 for (h = lookup; h <= end - 6; h++) {
4829 if (memcmp(h, ";typed", 6) == 0) {
4830 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
4831 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4832 break;
4833 }
4834 }
4835
4836 for (h = lookup; h <= end - 8; h++) {
4837 if (memcmp(h, ";st=", 4) == 0) {
4838 int i;
4839 h += 4;
4840 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4841 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4842 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4843 appctx->ctx.stats.st_code = i;
4844 break;
4845 }
4846 }
4847 break;
4848 }
4849 }
4850
4851 appctx->ctx.stats.scope_str = 0;
4852 appctx->ctx.stats.scope_len = 0;
4853 for (h = lookup; h <= end - 8; h++) {
4854 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4855 int itx = 0;
4856 const char *h2;
4857 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4858 const char *err;
4859
4860 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4861 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004862 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4863 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004864 if (*h == ';' || *h == '&' || *h == ' ')
4865 break;
4866 itx++;
4867 h++;
4868 }
4869
4870 if (itx > STAT_SCOPE_TXT_MAXLEN)
4871 itx = STAT_SCOPE_TXT_MAXLEN;
4872 appctx->ctx.stats.scope_len = itx;
4873
4874 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4875 memcpy(scope_txt, h2, itx);
4876 scope_txt[itx] = '\0';
4877 err = invalid_char(scope_txt);
4878 if (err) {
4879 /* bad char in search text => clear scope */
4880 appctx->ctx.stats.scope_str = 0;
4881 appctx->ctx.stats.scope_len = 0;
4882 }
4883 break;
4884 }
4885 }
4886
4887 /* now check whether we have some admin rules for this request */
4888 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4889 int ret = 1;
4890
4891 if (stats_admin_rule->cond) {
4892 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4893 ret = acl_pass(ret);
4894 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4895 ret = !ret;
4896 }
4897
4898 if (ret) {
4899 /* no rule, or the rule matches */
4900 appctx->ctx.stats.flags |= STAT_ADMIN;
4901 break;
4902 }
4903 }
4904
Christopher Faulet5d45e382019-02-27 15:15:23 +01004905 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4906 appctx->st0 = STAT_HTTP_HEAD;
4907 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbcf242a2019-03-01 11:36:26 +01004908 if (appctx->ctx.stats.flags & STAT_ADMIN)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004909 appctx->st0 = STAT_HTTP_POST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004910 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004911 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004912 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4913 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4914 appctx->st0 = STAT_HTTP_LAST;
4915 }
4916 }
4917 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004918 /* Unsupported method */
4919 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4920 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4921 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004922 }
4923
4924 s->task->nice = -32; /* small boost for HTTP statistics */
4925 return 1;
4926}
4927
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004928void htx_perform_server_redirect(struct stream *s, struct stream_interface *si)
4929{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004930 struct channel *req = &s->req;
4931 struct channel *res = &s->res;
4932 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004933 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004934 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004935 struct ist path, location;
4936 unsigned int flags;
4937 size_t data;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004938
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004939 /*
4940 * Create the location
4941 */
4942 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004943
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004944 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004945 /* special prefix "/" means don't change URL */
4946 srv = __objt_server(s->target);
4947 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4948 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4949 return;
4950 }
4951
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004952 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004953 htx = htxbuf(&req->buf);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004954 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004955 path = http_get_path(htx_sl_req_uri(sl));
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004956 if (!path.ptr)
4957 return;
4958
4959 if (!chunk_memcat(&trash, path.ptr, path.len))
4960 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004961 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004962
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004963 /*
4964 * Create the 302 respone
4965 */
4966 htx = htx_from_buf(&res->buf);
4967 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4968 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4969 ist("HTTP/1.1"), ist("302"), ist("Found"));
4970 if (!sl)
4971 goto fail;
4972 sl->info.res.status = 302;
4973 s->txn->status = 302;
4974
4975 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4976 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4977 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4978 !htx_add_header(htx, ist("Location"), location))
4979 goto fail;
4980
4981 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4982 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004983
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004984 /*
4985 * Send the message
4986 */
4987 data = htx->data - co_data(res);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004988 c_adv(res, data);
4989 res->total += data;
4990
4991 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004992 si_shutr(si);
4993 si_shutw(si);
4994 si->err_type = SI_ET_NONE;
4995 si->state = SI_ST_CLO;
4996
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004997 channel_auto_read(req);
4998 channel_abort(req);
4999 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005000 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01005001 channel_auto_read(res);
5002 channel_auto_close(res);
5003
5004 if (!(s->flags & SF_ERR_MASK))
5005 s->flags |= SF_ERR_LOCAL;
5006 if (!(s->flags & SF_FINST_MASK))
5007 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02005008
5009 /* FIXME: we should increase a counter of redirects per server and per backend. */
5010 srv_inc_sess_ctr(srv);
5011 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01005012 return;
5013
5014 fail:
5015 /* If an error occurred, remove the incomplete HTTP response from the
5016 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005017 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02005018}
5019
Christopher Fauletf2824e62018-10-01 12:12:37 +02005020/* This function terminates the request because it was completly analyzed or
5021 * because an error was triggered during the body forwarding.
5022 */
5023static void htx_end_request(struct stream *s)
5024{
5025 struct channel *chn = &s->req;
5026 struct http_txn *txn = s->txn;
5027
5028 DPRINTF(stderr,"[%u] %s: stream=%p states=%s,%s req->analysers=0x%08x res->analysers=0x%08x\n",
5029 now_ms, __FUNCTION__, s,
5030 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
5031 s->req.analysers, s->res.analysers);
5032
Christopher Fauletb42a8b62018-11-19 21:59:00 +01005033 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
5034 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02005035 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005036 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02005037 goto end;
5038 }
5039
5040 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
5041 return;
5042
5043 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02005044 /* No need to read anymore, the request was completely parsed.
5045 * We can shut the read side unless we want to abort_on_close,
5046 * or we have a POST request. The issue with POST requests is
5047 * that some browsers still send a CRLF after the request, and
5048 * this CRLF must be read so that it does not remain in the kernel
5049 * buffers, otherwise a close could cause an RST on some systems
5050 * (eg: Linux).
5051 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01005052 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02005053 channel_dont_read(chn);
5054
5055 /* if the server closes the connection, we want to immediately react
5056 * and close the socket to save packets and syscalls.
5057 */
5058 s->si[1].flags |= SI_FL_NOHALF;
5059
5060 /* In any case we've finished parsing the request so we must
5061 * disable Nagle when sending data because 1) we're not going
5062 * to shut this side, and 2) the server is waiting for us to
5063 * send pending data.
5064 */
5065 chn->flags |= CF_NEVER_WAIT;
5066
Christopher Fauletd01ce402019-01-02 17:44:13 +01005067 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5068 /* The server has not finished to respond, so we
5069 * don't want to move in order not to upset it.
5070 */
5071 return;
5072 }
5073
Christopher Fauletf2824e62018-10-01 12:12:37 +02005074 /* When we get here, it means that both the request and the
5075 * response have finished receiving. Depending on the connection
5076 * mode, we'll have to wait for the last bytes to leave in either
5077 * direction, and sometimes for a close to be effective.
5078 */
5079 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5080 /* Tunnel mode will not have any analyser so it needs to
5081 * poll for reads.
5082 */
5083 channel_auto_read(chn);
Christopher Faulet9768c262018-10-22 09:34:31 +02005084 if (b_data(&chn->buf))
5085 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02005086 txn->req.msg_state = HTTP_MSG_TUNNEL;
5087 }
5088 else {
5089 /* we're not expecting any new data to come for this
5090 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02005091 *
5092 * However, there is an exception if the response
5093 * length is undefined. In this case, we need to wait
5094 * the close from the server. The response will be
5095 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02005096 */
5097 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
5098 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02005099 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02005100
5101 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5102 channel_shutr_now(chn);
5103 channel_shutw_now(chn);
5104 }
5105 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02005106 goto check_channel_flags;
5107 }
5108
5109 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5110 http_msg_closing:
5111 /* nothing else to forward, just waiting for the output buffer
5112 * to be empty and for the shutw_now to take effect.
5113 */
5114 if (channel_is_empty(chn)) {
5115 txn->req.msg_state = HTTP_MSG_CLOSED;
5116 goto http_msg_closed;
5117 }
5118 else if (chn->flags & CF_SHUTW) {
5119 txn->req.err_state = txn->req.msg_state;
5120 txn->req.msg_state = HTTP_MSG_ERROR;
5121 goto end;
5122 }
5123 return;
5124 }
5125
5126 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5127 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02005128 /* if we don't know whether the server will close, we need to hard close */
5129 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
5130 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02005131 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01005132 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02005133 channel_dont_read(chn);
5134 goto end;
5135 }
5136
5137 check_channel_flags:
5138 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
5139 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
5140 /* if we've just closed an output, let's switch */
5141 txn->req.msg_state = HTTP_MSG_CLOSING;
5142 goto http_msg_closing;
5143 }
5144
5145 end:
5146 chn->analysers &= AN_REQ_FLT_END;
5147 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
5148 chn->analysers |= AN_REQ_FLT_XFER_DATA;
5149 channel_auto_close(chn);
5150 channel_auto_read(chn);
5151}
5152
5153
5154/* This function terminates the response because it was completly analyzed or
5155 * because an error was triggered during the body forwarding.
5156 */
5157static void htx_end_response(struct stream *s)
5158{
5159 struct channel *chn = &s->res;
5160 struct http_txn *txn = s->txn;
5161
5162 DPRINTF(stderr,"[%u] %s: stream=%p states=%s,%s req->analysers=0x%08x res->analysers=0x%08x\n",
5163 now_ms, __FUNCTION__, s,
5164 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
5165 s->req.analysers, s->res.analysers);
5166
Christopher Fauletb42a8b62018-11-19 21:59:00 +01005167 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
5168 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005169 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02005170 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005171 goto end;
5172 }
5173
5174 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
5175 return;
5176
5177 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5178 /* In theory, we don't need to read anymore, but we must
5179 * still monitor the server connection for a possible close
5180 * while the request is being uploaded, so we don't disable
5181 * reading.
5182 */
5183 /* channel_dont_read(chn); */
5184
5185 if (txn->req.msg_state < HTTP_MSG_DONE) {
5186 /* The client seems to still be sending data, probably
5187 * because we got an error response during an upload.
5188 * We have the choice of either breaking the connection
5189 * or letting it pass through. Let's do the later.
5190 */
5191 return;
5192 }
5193
5194 /* When we get here, it means that both the request and the
5195 * response have finished receiving. Depending on the connection
5196 * mode, we'll have to wait for the last bytes to leave in either
5197 * direction, and sometimes for a close to be effective.
5198 */
5199 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5200 channel_auto_read(chn);
5201 chn->flags |= CF_NEVER_WAIT;
Christopher Faulet9768c262018-10-22 09:34:31 +02005202 if (b_data(&chn->buf))
5203 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02005204 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
5205 }
5206 else {
5207 /* we're not expecting any new data to come for this
5208 * transaction, so we can close it.
5209 */
5210 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5211 channel_shutr_now(chn);
5212 channel_shutw_now(chn);
5213 }
5214 }
5215 goto check_channel_flags;
5216 }
5217
5218 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5219 http_msg_closing:
5220 /* nothing else to forward, just waiting for the output buffer
5221 * to be empty and for the shutw_now to take effect.
5222 */
5223 if (channel_is_empty(chn)) {
5224 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5225 goto http_msg_closed;
5226 }
5227 else if (chn->flags & CF_SHUTW) {
5228 txn->rsp.err_state = txn->rsp.msg_state;
5229 txn->rsp.msg_state = HTTP_MSG_ERROR;
Olivier Houcharda798bf52019-03-08 18:52:00 +01005230 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005231 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01005232 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005233 goto end;
5234 }
5235 return;
5236 }
5237
5238 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5239 http_msg_closed:
5240 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005241 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02005242 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005243 goto end;
5244 }
5245
5246 check_channel_flags:
5247 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
5248 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
5249 /* if we've just closed an output, let's switch */
5250 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5251 goto http_msg_closing;
5252 }
5253
5254 end:
5255 chn->analysers &= AN_RES_FLT_END;
5256 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
5257 chn->analysers |= AN_RES_FLT_XFER_DATA;
5258 channel_auto_close(chn);
5259 channel_auto_read(chn);
5260}
5261
Christopher Faulet0f226952018-10-22 09:29:56 +02005262void htx_server_error(struct stream *s, struct stream_interface *si, int err,
5263 int finst, const struct buffer *msg)
5264{
5265 channel_auto_read(si_oc(si));
5266 channel_abort(si_oc(si));
5267 channel_auto_close(si_oc(si));
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005268 channel_htx_erase(si_oc(si), htxbuf(&(si_oc(si))->buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02005269 channel_auto_close(si_ic(si));
5270 channel_auto_read(si_ic(si));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005271
5272 /* <msg> is an HTX structure. So we copy it in the response's
5273 * channel */
Christopher Faulet0f226952018-10-22 09:29:56 +02005274 if (msg) {
5275 struct channel *chn = si_ic(si);
5276 struct htx *htx;
5277
Christopher Fauletaed82cf2018-11-30 22:22:32 +01005278 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005279 chn->buf.data = msg->data;
5280 memcpy(chn->buf.area, msg->area, msg->data);
5281 htx = htx_from_buf(&chn->buf);
Christopher Faulet0f226952018-10-22 09:29:56 +02005282 c_adv(chn, htx->data);
5283 chn->total += htx->data;
5284 }
5285 if (!(s->flags & SF_ERR_MASK))
5286 s->flags |= err;
5287 if (!(s->flags & SF_FINST_MASK))
5288 s->flags |= finst;
5289}
5290
5291void htx_reply_and_close(struct stream *s, short status, struct buffer *msg)
5292{
5293 channel_auto_read(&s->req);
5294 channel_abort(&s->req);
5295 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005296 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
5297 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02005298
5299 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005300
5301 /* <msg> is an HTX structure. So we copy it in the response's
5302 * channel */
5303 /* FIXME: It is a problem for now if there is some outgoing data */
Christopher Faulet0f226952018-10-22 09:29:56 +02005304 if (msg) {
5305 struct channel *chn = &s->res;
5306 struct htx *htx;
5307
Christopher Fauletaed82cf2018-11-30 22:22:32 +01005308 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005309 chn->buf.data = msg->data;
5310 memcpy(chn->buf.area, msg->area, msg->data);
5311 htx = htx_from_buf(&chn->buf);
Christopher Faulet0f226952018-10-22 09:29:56 +02005312 c_adv(chn, htx->data);
5313 chn->total += htx->data;
5314 }
5315
5316 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
5317 channel_auto_read(&s->res);
5318 channel_auto_close(&s->res);
5319 channel_shutr_now(&s->res);
5320}
5321
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005322struct buffer *htx_error_message(struct stream *s)
5323{
5324 const int msgnum = http_get_status_idx(s->txn->status);
5325
5326 if (s->be->errmsg[msgnum].area)
5327 return &s->be->errmsg[msgnum];
5328 else if (strm_fe(s)->errmsg[msgnum].area)
5329 return &strm_fe(s)->errmsg[msgnum];
5330 else
5331 return &htx_err_chunks[msgnum];
5332}
5333
5334
Christopher Faulet4a28a532019-03-01 11:19:40 +01005335/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
5336 * on success and -1 on error.
5337 */
5338static int htx_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
5339{
5340 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
5341 * then we must send an HTTP/1.1 100 Continue intermediate response.
5342 */
5343 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
5344 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
5345 struct ist hdr = { .ptr = "Expect", .len = 6 };
5346 struct http_hdr_ctx ctx;
5347
5348 ctx.blk = NULL;
5349 /* Expect is allowed in 1.1, look for it */
5350 if (http_find_header(htx, hdr, &ctx, 0) &&
5351 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
5352 if (htx_reply_100_continue(s) == -1)
5353 return -1;
5354 http_remove_header(htx, &ctx);
5355 }
5356 }
5357 return 0;
5358}
5359
Christopher Faulet23a3c792018-11-28 10:01:23 +01005360/* Send a 100-Continue response to the client. It returns 0 on success and -1
5361 * on error. The response channel is updated accordingly.
5362 */
5363static int htx_reply_100_continue(struct stream *s)
5364{
5365 struct channel *res = &s->res;
5366 struct htx *htx = htx_from_buf(&res->buf);
5367 struct htx_sl *sl;
5368 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
5369 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
5370 size_t data;
5371
5372 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5373 ist("HTTP/1.1"), ist("100"), ist("Continue"));
5374 if (!sl)
5375 goto fail;
5376 sl->info.res.status = 100;
5377
5378 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
5379 goto fail;
5380
5381 data = htx->data - co_data(res);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005382 c_adv(res, data);
5383 res->total += data;
5384 return 0;
5385
5386 fail:
5387 /* If an error occurred, remove the incomplete HTTP response from the
5388 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005389 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005390 return -1;
5391}
5392
Christopher Faulet12c51e22018-11-28 15:59:42 +01005393
5394/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
5395 * ont whether we use a proxy or not. It returns 0 on success and -1 on
5396 * error. The response channel is updated accordingly.
5397 */
5398static int htx_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
5399{
5400 struct channel *res = &s->res;
5401 struct htx *htx = htx_from_buf(&res->buf);
5402 struct htx_sl *sl;
5403 struct ist code, body;
5404 int status;
5405 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5406 size_t data;
5407
5408 if (!(s->txn->flags & TX_USE_PX_CONN)) {
5409 status = 401;
5410 code = ist("401");
5411 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
5412 "You need a valid user and password to access this content.\n"
5413 "</body></html>\n");
5414 }
5415 else {
5416 status = 407;
5417 code = ist("407");
5418 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
5419 "You need a valid user and password to access this content.\n"
5420 "</body></html>\n");
5421 }
5422
5423 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5424 ist("HTTP/1.1"), code, ist("Unauthorized"));
5425 if (!sl)
5426 goto fail;
5427 sl->info.res.status = status;
5428 s->txn->status = status;
5429
5430 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
5431 goto fail;
5432
5433 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
5434 !htx_add_header(htx, ist("Connection"), ist("close")) ||
Jérôme Magnin86cef232018-12-28 14:49:08 +01005435 !htx_add_header(htx, ist("Content-Type"), ist("text/html")))
5436 goto fail;
5437 if (status == 401 && !htx_add_header(htx, ist("WWW-Authenticate"), ist2(trash.area, trash.data)))
5438 goto fail;
5439 if (status == 407 && !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005440 goto fail;
Christopher Faulet12c51e22018-11-28 15:59:42 +01005441 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_data(htx, body) || !htx_add_endof(htx, HTX_BLK_EOM))
5442 goto fail;
5443
5444 data = htx->data - co_data(res);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005445 c_adv(res, data);
5446 res->total += data;
5447
5448 channel_auto_read(&s->req);
5449 channel_abort(&s->req);
5450 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005451 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet12c51e22018-11-28 15:59:42 +01005452
5453 res->wex = tick_add_ifset(now_ms, res->wto);
5454 channel_auto_read(res);
5455 channel_auto_close(res);
5456 channel_shutr_now(res);
5457 return 0;
5458
5459 fail:
5460 /* If an error occurred, remove the incomplete HTTP response from the
5461 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005462 channel_htx_truncate(res, htx);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005463 return -1;
5464}
5465
Christopher Faulet0f226952018-10-22 09:29:56 +02005466/*
5467 * Capture headers from message <htx> according to header list <cap_hdr>, and
5468 * fill the <cap> pointers appropriately.
5469 */
5470static void htx_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
5471{
5472 struct cap_hdr *h;
5473 int32_t pos;
5474
5475 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
5476 struct htx_blk *blk = htx_get_blk(htx, pos);
5477 enum htx_blk_type type = htx_get_blk_type(blk);
5478 struct ist n, v;
5479
5480 if (type == HTX_BLK_EOH)
5481 break;
5482 if (type != HTX_BLK_HDR)
5483 continue;
5484
5485 n = htx_get_blk_name(htx, blk);
5486
5487 for (h = cap_hdr; h; h = h->next) {
5488 if (h->namelen && (h->namelen == n.len) &&
5489 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
5490 if (cap[h->index] == NULL)
5491 cap[h->index] =
5492 pool_alloc(h->pool);
5493
5494 if (cap[h->index] == NULL) {
5495 ha_alert("HTTP capture : out of memory.\n");
5496 break;
5497 }
5498
5499 v = htx_get_blk_value(htx, blk);
5500 if (v.len > h->len)
5501 v.len = h->len;
5502
5503 memcpy(cap[h->index], v.ptr, v.len);
5504 cap[h->index][v.len]=0;
5505 }
5506 }
5507 }
5508}
5509
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005510/* Delete a value in a header between delimiters <from> and <next>. The header
5511 * itself is delimited by <start> and <end> pointers. The number of characters
5512 * displaced is returned, and the pointer to the first delimiter is updated if
5513 * required. The function tries as much as possible to respect the following
5514 * principles :
5515 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5516 * in which case <next> is simply removed
5517 * - set exactly one space character after the new first delimiter, unless there
5518 * are not enough characters in the block being moved to do so.
5519 * - remove unneeded spaces before the previous delimiter and after the new
5520 * one.
5521 *
5522 * It is the caller's responsibility to ensure that :
5523 * - <from> points to a valid delimiter or <start> ;
5524 * - <next> points to a valid delimiter or <end> ;
5525 * - there are non-space chars before <from>.
5526 */
5527static int htx_del_hdr_value(char *start, char *end, char **from, char *next)
5528{
5529 char *prev = *from;
5530
5531 if (prev == start) {
5532 /* We're removing the first value. eat the semicolon, if <next>
5533 * is lower than <end> */
5534 if (next < end)
5535 next++;
5536
5537 while (next < end && HTTP_IS_SPHT(*next))
5538 next++;
5539 }
5540 else {
5541 /* Remove useless spaces before the old delimiter. */
5542 while (HTTP_IS_SPHT(*(prev-1)))
5543 prev--;
5544 *from = prev;
5545
5546 /* copy the delimiter and if possible a space if we're
5547 * not at the end of the line.
5548 */
5549 if (next < end) {
5550 *prev++ = *next++;
5551 if (prev + 1 < next)
5552 *prev++ = ' ';
5553 while (next < end && HTTP_IS_SPHT(*next))
5554 next++;
5555 }
5556 }
5557 memmove(prev, next, end - next);
5558 return (prev - next);
5559}
5560
Christopher Faulet0f226952018-10-22 09:29:56 +02005561
5562/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005563 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005564 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005565static size_t htx_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005566{
5567 struct ist dst = ist2(str, 0);
5568
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005569 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005570 goto end;
5571 if (dst.len + 1 > len)
5572 goto end;
5573 dst.ptr[dst.len++] = ' ';
5574
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005575 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005576 goto end;
5577 if (dst.len + 1 > len)
5578 goto end;
5579 dst.ptr[dst.len++] = ' ';
5580
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005581 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005582 end:
5583 return dst.len;
5584}
5585
Christopher Fauletf0523542018-10-24 11:06:58 +02005586/* Formats the start line of the response (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005587 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Fauletf0523542018-10-24 11:06:58 +02005588 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005589static size_t htx_fmt_res_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Fauletf0523542018-10-24 11:06:58 +02005590{
5591 struct ist dst = ist2(str, 0);
5592
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005593 if (istcat(&dst, htx_sl_res_vsn(sl), len) == -1)
Christopher Fauletf0523542018-10-24 11:06:58 +02005594 goto end;
5595 if (dst.len + 1 > len)
5596 goto end;
5597 dst.ptr[dst.len++] = ' ';
5598
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005599 if (istcat(&dst, htx_sl_res_code(sl), len) == -1)
Christopher Fauletf0523542018-10-24 11:06:58 +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 istcat(&dst, htx_sl_res_reason(sl), len);
Christopher Fauletf0523542018-10-24 11:06:58 +02005606 end:
5607 return dst.len;
5608}
5609
5610
Christopher Faulet0f226952018-10-22 09:29:56 +02005611/*
5612 * Print a debug line with a start line.
5613 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005614static void htx_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005615{
5616 struct session *sess = strm_sess(s);
5617 int max;
5618
5619 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5620 dir,
5621 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5622 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5623
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005624 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005625 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005626 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005627 trash.area[trash.data++] = ' ';
5628
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005629 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005630 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005631 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005632 trash.area[trash.data++] = ' ';
5633
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005634 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005635 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005636 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005637 trash.area[trash.data++] = '\n';
5638
5639 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5640}
5641
5642/*
5643 * Print a debug line with a header.
5644 */
5645static void htx_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v)
5646{
5647 struct session *sess = strm_sess(s);
5648 int max;
5649
5650 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5651 dir,
5652 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5653 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5654
5655 max = n.len;
5656 UBOUND(max, trash.size - trash.data - 3);
5657 chunk_memcat(&trash, n.ptr, max);
5658 trash.area[trash.data++] = ':';
5659 trash.area[trash.data++] = ' ';
5660
5661 max = v.len;
5662 UBOUND(max, trash.size - trash.data - 1);
5663 chunk_memcat(&trash, v.ptr, max);
5664 trash.area[trash.data++] = '\n';
5665
5666 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5667}
5668
5669
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005670__attribute__((constructor))
5671static void __htx_protocol_init(void)
5672{
5673}
5674
5675
5676/*
5677 * Local variables:
5678 * c-indent-level: 8
5679 * c-basic-offset: 8
5680 * End:
5681 */