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