blob: da28dbc340937772ecd85fa78ba5dd9706625472 [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>
16#include <common/uri_auth.h>
17
18#include <types/cache.h>
Christopher Faulet0f226952018-10-22 09:29:56 +020019#include <types/capture.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020020
21#include <proto/acl.h>
Christopher Faulet3e964192018-10-24 11:39:23 +020022#include <proto/action.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020023#include <proto/channel.h>
24#include <proto/checks.h>
25#include <proto/connection.h>
26#include <proto/filters.h>
27#include <proto/hdr_idx.h>
Christopher Faulet0f226952018-10-22 09:29:56 +020028#include <proto/http_htx.h>
29#include <proto/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 Faulet9768c262018-10-22 09:34:31 +020098 htx = htx_from_buf(&req->buf);
99
Christopher Faulete0768eb2018-10-03 16:38:02 +0200100 /* we're speaking HTTP here, so let's speak HTTP to the client */
101 s->srv_error = http_return_srv_error;
102
103 /* If there is data available for analysis, log the end of the idle time. */
Christopher Faulet870aad92018-11-29 15:23:46 +0100104 if (c_data(req) && s->logs.t_idle == -1) {
105 const struct cs_info *csinfo = si_get_cs_info(objt_cs(s->si[0].end));
106
107 s->logs.t_idle = ((csinfo)
108 ? csinfo->t_idle
109 : tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake);
110 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200111
Christopher Faulete0768eb2018-10-03 16:38:02 +0200112 /*
113 * Now we quickly check if we have found a full valid request.
114 * If not so, we check the FD and buffer states before leaving.
115 * A full request is indicated by the fact that we have seen
116 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
117 * requests are checked first. When waiting for a second request
118 * on a keep-alive stream, if we encounter and error, close, t/o,
119 * we note the error in the stream flags but don't set any state.
120 * Since the error will be noted there, it will not be counted by
121 * process_stream() as a frontend error.
122 * Last, we may increase some tracked counters' http request errors on
123 * the cases that are deliberately the client's fault. For instance,
124 * a timeout or connection reset is not counted as an error. However
125 * a bad request is.
126 */
Christopher Faulet9768c262018-10-22 09:34:31 +0200127 if (unlikely(htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100128 /*
129 * First catch invalid request
130 */
131 if (htx->flags & HTX_FL_PARSING_ERROR) {
132 stream_inc_http_req_ctr(s);
133 stream_inc_http_err_ctr(s);
134 proxy_inc_fe_req_ctr(sess->fe);
135 goto return_bad_req;
136 }
137
Christopher Faulet9768c262018-10-22 09:34:31 +0200138 /* 1: have we encountered a read error ? */
139 if (req->flags & CF_READ_ERROR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200140 if (!(s->flags & SF_ERR_MASK))
141 s->flags |= SF_ERR_CLICL;
142
143 if (txn->flags & TX_WAIT_NEXT_RQ)
144 goto failed_keep_alive;
145
146 if (sess->fe->options & PR_O_IGNORE_PRB)
147 goto failed_keep_alive;
148
Christopher Faulet9768c262018-10-22 09:34:31 +0200149 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200150 stream_inc_http_req_ctr(s);
151 proxy_inc_fe_req_ctr(sess->fe);
152 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
153 if (sess->listener->counters)
154 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
155
Christopher Faulet9768c262018-10-22 09:34:31 +0200156 txn->status = 400;
157 msg->err_state = msg->msg_state;
158 msg->msg_state = HTTP_MSG_ERROR;
159 htx_reply_and_close(s, txn->status, NULL);
160 req->analysers &= AN_REQ_FLT_END;
161
Christopher Faulete0768eb2018-10-03 16:38:02 +0200162 if (!(s->flags & SF_FINST_MASK))
163 s->flags |= SF_FINST_R;
164 return 0;
165 }
166
Christopher Faulet9768c262018-10-22 09:34:31 +0200167 /* 2: has the read timeout expired ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200168 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
169 if (!(s->flags & SF_ERR_MASK))
170 s->flags |= SF_ERR_CLITO;
171
172 if (txn->flags & TX_WAIT_NEXT_RQ)
173 goto failed_keep_alive;
174
175 if (sess->fe->options & PR_O_IGNORE_PRB)
176 goto failed_keep_alive;
177
Christopher Faulet9768c262018-10-22 09:34:31 +0200178 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200179 stream_inc_http_req_ctr(s);
180 proxy_inc_fe_req_ctr(sess->fe);
181 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
182 if (sess->listener->counters)
183 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
184
Christopher Faulet9768c262018-10-22 09:34:31 +0200185 txn->status = 408;
186 msg->err_state = msg->msg_state;
187 msg->msg_state = HTTP_MSG_ERROR;
188 htx_reply_and_close(s, txn->status, http_error_message(s));
189 req->analysers &= AN_REQ_FLT_END;
190
Christopher Faulete0768eb2018-10-03 16:38:02 +0200191 if (!(s->flags & SF_FINST_MASK))
192 s->flags |= SF_FINST_R;
193 return 0;
194 }
195
Christopher Faulet9768c262018-10-22 09:34:31 +0200196 /* 3: have we encountered a close ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200197 else if (req->flags & CF_SHUTR) {
198 if (!(s->flags & SF_ERR_MASK))
199 s->flags |= SF_ERR_CLICL;
200
201 if (txn->flags & TX_WAIT_NEXT_RQ)
202 goto failed_keep_alive;
203
204 if (sess->fe->options & PR_O_IGNORE_PRB)
205 goto failed_keep_alive;
206
Christopher Faulete0768eb2018-10-03 16:38:02 +0200207 stream_inc_http_err_ctr(s);
208 stream_inc_http_req_ctr(s);
209 proxy_inc_fe_req_ctr(sess->fe);
210 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
211 if (sess->listener->counters)
212 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
213
Christopher Faulet9768c262018-10-22 09:34:31 +0200214 txn->status = 400;
215 msg->err_state = msg->msg_state;
216 msg->msg_state = HTTP_MSG_ERROR;
217 htx_reply_and_close(s, txn->status, http_error_message(s));
218 req->analysers &= AN_REQ_FLT_END;
219
Christopher Faulete0768eb2018-10-03 16:38:02 +0200220 if (!(s->flags & SF_FINST_MASK))
221 s->flags |= SF_FINST_R;
222 return 0;
223 }
224
225 channel_dont_connect(req);
226 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
227 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
228#ifdef TCP_QUICKACK
Christopher Faulet9768c262018-10-22 09:34:31 +0200229 if (sess->listener->options & LI_O_NOQUICKACK && htx_is_not_empty(htx) &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200230 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
231 /* We need more data, we have to re-enable quick-ack in case we
232 * previously disabled it, otherwise we might cause the client
233 * to delay next data.
234 */
235 setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
236 }
237#endif
Christopher Faulet47365272018-10-31 17:40:50 +0100238 if ((req->flags & CF_READ_PARTIAL) && (txn->flags & TX_WAIT_NEXT_RQ)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200239 /* If the client starts to talk, let's fall back to
240 * request timeout processing.
241 */
242 txn->flags &= ~TX_WAIT_NEXT_RQ;
243 req->analyse_exp = TICK_ETERNITY;
244 }
245
246 /* just set the request timeout once at the beginning of the request */
247 if (!tick_isset(req->analyse_exp)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100248 if ((txn->flags & TX_WAIT_NEXT_RQ) && tick_isset(s->be->timeout.httpka))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200249 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
250 else
251 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
252 }
253
254 /* we're not ready yet */
255 return 0;
256
257 failed_keep_alive:
258 /* Here we process low-level errors for keep-alive requests. In
259 * short, if the request is not the first one and it experiences
260 * a timeout, read error or shutdown, we just silently close so
261 * that the client can try again.
262 */
263 txn->status = 0;
264 msg->msg_state = HTTP_MSG_RQBEFORE;
265 req->analysers &= AN_REQ_FLT_END;
266 s->logs.logwait = 0;
267 s->logs.level = 0;
268 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Faulet9768c262018-10-22 09:34:31 +0200269 htx_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200270 return 0;
271 }
272
Christopher Faulet9768c262018-10-22 09:34:31 +0200273 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200274 stream_inc_http_req_ctr(s);
275 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
276
Christopher Faulet9768c262018-10-22 09:34:31 +0200277 /* kill the pending keep-alive timeout */
278 txn->flags &= ~TX_WAIT_NEXT_RQ;
279 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200280
Christopher Faulet03599112018-11-27 11:21:21 +0100281 sl = http_find_stline(htx);
282
Christopher Faulet9768c262018-10-22 09:34:31 +0200283 /* 0: we might have to print this header in debug mode */
284 if (unlikely((global.mode & MODE_DEBUG) &&
285 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
286 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200287
Christopher Faulet03599112018-11-27 11:21:21 +0100288 htx_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200289
290 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
291 struct htx_blk *blk = htx_get_blk(htx, pos);
292 enum htx_blk_type type = htx_get_blk_type(blk);
293
294 if (type == HTX_BLK_EOH)
295 break;
296 if (type != HTX_BLK_HDR)
297 continue;
298
299 htx_debug_hdr("clihdr", s,
300 htx_get_blk_name(htx, blk),
301 htx_get_blk_value(htx, blk));
302 }
303 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200304
305 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100306 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200307 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100308 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100309 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200310 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100311 msg->flags |= HTTP_MSGF_XFER_LEN;
312 msg->flags |= ((sl->flags & HTX_SL_F_CHNK) ? HTTP_MSGF_TE_CHNK : HTTP_MSGF_CNT_LEN);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100313 if (sl->flags & HTX_SL_F_BODYLESS)
314 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200315
316 /* we can make use of server redirect on GET and HEAD */
317 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
318 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100319 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200320 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200321 goto return_bad_req;
322 }
323
324 /*
325 * 2: check if the URI matches the monitor_uri.
326 * We have to do this for every request which gets in, because
327 * the monitor-uri is defined by the frontend.
328 */
329 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100330 isteqi(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200331 /*
332 * We have found the monitor URI
333 */
334 struct acl_cond *cond;
335
336 s->flags |= SF_MONITOR;
337 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
338
339 /* Check if we want to fail this monitor request or not */
340 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
341 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
342
343 ret = acl_pass(ret);
344 if (cond->pol == ACL_COND_UNLESS)
345 ret = !ret;
346
347 if (ret) {
348 /* we fail this request, let's return 503 service unavail */
349 txn->status = 503;
Christopher Faulet9768c262018-10-22 09:34:31 +0200350 htx_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200351 if (!(s->flags & SF_ERR_MASK))
352 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
353 goto return_prx_cond;
354 }
355 }
356
357 /* nothing to fail, let's reply normaly */
358 txn->status = 200;
Christopher Faulet9768c262018-10-22 09:34:31 +0200359 htx_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200360 if (!(s->flags & SF_ERR_MASK))
361 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
362 goto return_prx_cond;
363 }
364
365 /*
366 * 3: Maybe we have to copy the original REQURI for the logs ?
367 * Note: we cannot log anymore if the request has been
368 * classified as invalid.
369 */
370 if (unlikely(s->logs.logwait & LW_REQ)) {
371 /* we have a complete HTTP request that we must log */
372 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200373 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200374
Christopher Faulet9768c262018-10-22 09:34:31 +0200375 len = htx_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
376 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200377
378 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
379 s->do_log(s);
380 } else {
381 ha_alert("HTTP logging : out of memory.\n");
382 }
383 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200384
Christopher Faulete0768eb2018-10-03 16:38:02 +0200385 /* if the frontend has "option http-use-proxy-header", we'll check if
386 * we have what looks like a proxied connection instead of a connection,
387 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
388 * Note that this is *not* RFC-compliant, however browsers and proxies
389 * happen to do that despite being non-standard :-(
390 * We consider that a request not beginning with either '/' or '*' is
391 * a proxied connection, which covers both "scheme://location" and
392 * CONNECT ip:port.
393 */
394 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100395 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200396 txn->flags |= TX_USE_PX_CONN;
397
Christopher Faulete0768eb2018-10-03 16:38:02 +0200398 /* 5: we may need to capture headers */
399 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Faulet9768c262018-10-22 09:34:31 +0200400 htx_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200401
402 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
403 * only change if both the request and the config reference something else.
404 * Option httpclose by itself sets tunnel mode where headers are mangled.
405 * However, if another mode is set, it will affect it (eg: server-close/
406 * keep-alive + httpclose = close). Note that we avoid to redo the same work
407 * if FE and BE have the same settings (common). The method consists in
408 * checking if options changed between the two calls (implying that either
409 * one is non-null, or one of them is non-null and we are there for the first
410 * time.
411 */
Christopher Fauletf2824e62018-10-01 12:12:37 +0200412 if ((sess->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))
Christopher Faulet0f226952018-10-22 09:29:56 +0200413 htx_adjust_conn_mode(s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200414
415 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200416 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200417 req->analysers |= AN_REQ_HTTP_BODY;
418
419 /*
420 * RFC7234#4:
421 * A cache MUST write through requests with methods
422 * that are unsafe (Section 4.2.1 of [RFC7231]) to
423 * the origin server; i.e., a cache is not allowed
424 * to generate a reply to such a request before
425 * having forwarded the request and having received
426 * a corresponding response.
427 *
428 * RFC7231#4.2.1:
429 * Of the request methods defined by this
430 * specification, the GET, HEAD, OPTIONS, and TRACE
431 * methods are defined to be safe.
432 */
433 if (likely(txn->meth == HTTP_METH_GET ||
434 txn->meth == HTTP_METH_HEAD ||
435 txn->meth == HTTP_METH_OPTIONS ||
436 txn->meth == HTTP_METH_TRACE))
437 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
438
439 /* end of job, return OK */
440 req->analysers &= ~an_bit;
441 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200442
Christopher Faulete0768eb2018-10-03 16:38:02 +0200443 return 1;
444
445 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200446 txn->status = 400;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200447 txn->req.err_state = txn->req.msg_state;
448 txn->req.msg_state = HTTP_MSG_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +0200449 htx_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200450 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
451 if (sess->listener->counters)
452 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
453
454 return_prx_cond:
455 if (!(s->flags & SF_ERR_MASK))
456 s->flags |= SF_ERR_PRXCOND;
457 if (!(s->flags & SF_FINST_MASK))
458 s->flags |= SF_FINST_R;
459
460 req->analysers &= AN_REQ_FLT_END;
461 req->analyse_exp = TICK_ETERNITY;
462 return 0;
463}
464
465
466/* This stream analyser runs all HTTP request processing which is common to
467 * frontends and backends, which means blocking ACLs, filters, connection-close,
468 * reqadd, stats and redirects. This is performed for the designated proxy.
469 * It returns 1 if the processing can continue on next analysers, or zero if it
470 * either needs more data or wants to immediately abort the request (eg: deny,
471 * error, ...).
472 */
473int htx_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
474{
475 struct session *sess = s->sess;
476 struct http_txn *txn = s->txn;
477 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200478 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200479 struct redirect_rule *rule;
480 struct cond_wordlist *wl;
481 enum rule_result verdict;
482 int deny_status = HTTP_ERR_403;
483 struct connection *conn = objt_conn(sess->origin);
484
485 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
486 /* we need more data */
487 goto return_prx_yield;
488 }
489
490 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
491 now_ms, __FUNCTION__,
492 s,
493 req,
494 req->rex, req->wex,
495 req->flags,
496 ci_data(req),
497 req->analysers);
498
Christopher Fauletff2759f2018-10-24 11:13:16 +0200499 htx = htx_from_buf(&req->buf);
500
Christopher Faulete0768eb2018-10-03 16:38:02 +0200501 /* just in case we have some per-backend tracking */
502 stream_inc_be_http_req_ctr(s);
503
504 /* evaluate http-request rules */
505 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200506 verdict = htx_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200507
508 switch (verdict) {
509 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
510 goto return_prx_yield;
511
512 case HTTP_RULE_RES_CONT:
513 case HTTP_RULE_RES_STOP: /* nothing to do */
514 break;
515
516 case HTTP_RULE_RES_DENY: /* deny or tarpit */
517 if (txn->flags & TX_CLTARPIT)
518 goto tarpit;
519 goto deny;
520
521 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
522 goto return_prx_cond;
523
524 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
525 goto done;
526
527 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
528 goto return_bad_req;
529 }
530 }
531
532 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
533 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200534 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200535
Christopher Fauletff2759f2018-10-24 11:13:16 +0200536 ctx.blk = NULL;
537 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
538 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200539 goto return_bad_req;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200540 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200541 }
542
543 /* OK at this stage, we know that the request was accepted according to
544 * the http-request rules, we can check for the stats. Note that the
545 * URI is detected *before* the req* rules in order not to be affected
546 * by a possible reqrep, while they are processed *after* so that a
547 * reqdeny can still block them. This clearly needs to change in 1.6!
548 */
Christopher Fauletff2759f2018-10-24 11:13:16 +0200549 if (htx_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200550 s->target = &http_stats_applet.obj_type;
551 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) {
552 txn->status = 500;
553 s->logs.tv_request = now;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200554 htx_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200555
556 if (!(s->flags & SF_ERR_MASK))
557 s->flags |= SF_ERR_RESOURCE;
558 goto return_prx_cond;
559 }
560
561 /* parse the whole stats request and extract the relevant information */
Christopher Fauletff2759f2018-10-24 11:13:16 +0200562 htx_handle_stats(s, req);
563 verdict = htx_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200564 /* not all actions implemented: deny, allow, auth */
565
566 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
567 goto deny;
568
569 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
570 goto return_prx_cond;
571 }
572
573 /* evaluate the req* rules except reqadd */
574 if (px->req_exp != NULL) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200575 if (htx_apply_filters_to_request(s, req, px) < 0)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200576 goto return_bad_req;
577
578 if (txn->flags & TX_CLDENY)
579 goto deny;
580
581 if (txn->flags & TX_CLTARPIT) {
582 deny_status = HTTP_ERR_500;
583 goto tarpit;
584 }
585 }
586
587 /* add request headers from the rule sets in the same order */
588 list_for_each_entry(wl, &px->req_add, list) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200589 struct ist n,v;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200590 if (wl->cond) {
591 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
592 ret = acl_pass(ret);
593 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
594 ret = !ret;
595 if (!ret)
596 continue;
597 }
598
Christopher Fauletff2759f2018-10-24 11:13:16 +0200599 http_parse_header(ist2(wl->s, strlen(wl->s)), &n, &v);
600 if (unlikely(!http_add_header(htx, n, v)))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200601 goto return_bad_req;
602 }
603
Christopher Faulete0768eb2018-10-03 16:38:02 +0200604 /* Proceed with the stats now. */
605 if (unlikely(objt_applet(s->target) == &http_stats_applet) ||
606 unlikely(objt_applet(s->target) == &http_cache_applet)) {
Christopher Fauletef779222018-10-31 08:47:01 +0100607
608 if (unlikely(objt_applet(s->target) == &http_cache_applet)) {
609 // TODO: Disabled for now, waiting to be adapted for HTX implementation
610 goto deny;
611 }
Christopher Fauletff2759f2018-10-24 11:13:16 +0200612
Christopher Faulete0768eb2018-10-03 16:38:02 +0200613 /* process the stats request now */
614 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
615 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
616
617 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
618 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
619 if (!(s->flags & SF_FINST_MASK))
620 s->flags |= SF_FINST_R;
621
622 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
623 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
624 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
625 req->analysers |= AN_REQ_HTTP_XFER_BODY;
626 goto done;
627 }
628
629 /* check whether we have some ACLs set to redirect this request */
630 list_for_each_entry(rule, &px->redirect_rules, list) {
631 if (rule->cond) {
632 int ret;
633
634 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
635 ret = acl_pass(ret);
636 if (rule->cond->pol == ACL_COND_UNLESS)
637 ret = !ret;
638 if (!ret)
639 continue;
640 }
Christopher Fauletf2824e62018-10-01 12:12:37 +0200641 if (!htx_apply_redirect_rule(rule, s, txn))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200642 goto return_bad_req;
643 goto done;
644 }
645
646 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
647 * If this happens, then the data will not come immediately, so we must
648 * send all what we have without waiting. Note that due to the small gain
649 * in waiting for the body of the request, it's easier to simply put the
650 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
651 * itself once used.
652 */
653 req->flags |= CF_SEND_DONTWAIT;
654
655 done: /* done with this analyser, continue with next ones that the calling
656 * points will have set, if any.
657 */
658 req->analyse_exp = TICK_ETERNITY;
659 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
660 req->analysers &= ~an_bit;
661 return 1;
662
663 tarpit:
664 /* Allow cookie logging
665 */
666 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletff2759f2018-10-24 11:13:16 +0200667 htx_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200668
669 /* When a connection is tarpitted, we use the tarpit timeout,
670 * which may be the same as the connect timeout if unspecified.
671 * If unset, then set it to zero because we really want it to
672 * eventually expire. We build the tarpit as an analyser.
673 */
674 channel_erase(&s->req);
675
676 /* wipe the request out so that we can drop the connection early
677 * if the client closes first.
678 */
679 channel_dont_connect(req);
680
681 txn->status = http_err_codes[deny_status];
682
683 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
684 req->analysers |= AN_REQ_HTTP_TARPIT;
685 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
686 if (!req->analyse_exp)
687 req->analyse_exp = tick_add(now_ms, 0);
688 stream_inc_http_err_ctr(s);
689 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
690 if (sess->fe != s->be)
691 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
692 if (sess->listener->counters)
693 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
694 goto done_without_exp;
695
696 deny: /* this request was blocked (denied) */
697
698 /* Allow cookie logging
699 */
700 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletff2759f2018-10-24 11:13:16 +0200701 htx_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200702
703 txn->flags |= TX_CLDENY;
704 txn->status = http_err_codes[deny_status];
705 s->logs.tv_request = now;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200706 htx_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200707 stream_inc_http_err_ctr(s);
708 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
709 if (sess->fe != s->be)
710 HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
711 if (sess->listener->counters)
712 HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
713 goto return_prx_cond;
714
715 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200716 txn->req.err_state = txn->req.msg_state;
717 txn->req.msg_state = HTTP_MSG_ERROR;
718 txn->status = 400;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200719 htx_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200720
721 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
722 if (sess->listener->counters)
723 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
724
725 return_prx_cond:
726 if (!(s->flags & SF_ERR_MASK))
727 s->flags |= SF_ERR_PRXCOND;
728 if (!(s->flags & SF_FINST_MASK))
729 s->flags |= SF_FINST_R;
730
731 req->analysers &= AN_REQ_FLT_END;
732 req->analyse_exp = TICK_ETERNITY;
733 return 0;
734
735 return_prx_yield:
736 channel_dont_connect(req);
737 return 0;
738}
739
740/* This function performs all the processing enabled for the current request.
741 * It returns 1 if the processing can continue on next analysers, or zero if it
742 * needs more data, encounters an error, or wants to immediately abort the
743 * request. It relies on buffers flags, and updates s->req.analysers.
744 */
745int htx_process_request(struct stream *s, struct channel *req, int an_bit)
746{
747 struct session *sess = s->sess;
748 struct http_txn *txn = s->txn;
749 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200750 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200751 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
752
753 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
754 /* we need more data */
755 channel_dont_connect(req);
756 return 0;
757 }
758
759 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
760 now_ms, __FUNCTION__,
761 s,
762 req,
763 req->rex, req->wex,
764 req->flags,
765 ci_data(req),
766 req->analysers);
767
768 /*
769 * Right now, we know that we have processed the entire headers
770 * and that unwanted requests have been filtered out. We can do
771 * whatever we want with the remaining request. Also, now we
772 * may have separate values for ->fe, ->be.
773 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200774 htx = htx_from_buf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200775
776 /*
777 * If HTTP PROXY is set we simply get remote server address parsing
778 * incoming request. Note that this requires that a connection is
779 * allocated on the server side.
780 */
781 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
782 struct connection *conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100783 struct htx_sl *sl;
784 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200785
786 /* Note that for now we don't reuse existing proxy connections */
787 if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) {
788 txn->req.err_state = txn->req.msg_state;
789 txn->req.msg_state = HTTP_MSG_ERROR;
790 txn->status = 500;
791 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200792 htx_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200793
794 if (!(s->flags & SF_ERR_MASK))
795 s->flags |= SF_ERR_RESOURCE;
796 if (!(s->flags & SF_FINST_MASK))
797 s->flags |= SF_FINST_R;
798
799 return 0;
800 }
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200801 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100802 uri = htx_sl_req_uri(sl);
803 path = http_get_path(uri);
804 if (url2sa(uri.ptr, uri.len - path.len, &conn->addr.to, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200805 goto return_bad_req;
806
807 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200808 * uri.ptr and path.ptr (excluded). If it was not found, we need
809 * to replace from all the uri by a single "/".
810 *
811 * Instead of rewritting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100812 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200813 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200814 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100815 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200816 }
817
818 /*
819 * 7: Now we can work with the cookies.
820 * Note that doing so might move headers in the request, but
821 * the fields will stay coherent and the URI will not move.
822 * This should only be performed in the backend.
823 */
824 if (s->be->cookie_name || sess->fe->capture_name)
825 manage_client_side_cookies(s, req);
826
827 /* add unique-id if "header-unique-id" is specified */
828
829 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
830 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
831 goto return_bad_req;
832 s->unique_id[0] = '\0';
833 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
834 }
835
836 if (sess->fe->header_unique_id && s->unique_id) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200837 struct ist n = ist2(sess->fe->header_unique_id, strlen(sess->fe->header_unique_id));
838 struct ist v = ist2(s->unique_id, strlen(s->unique_id));
839
840 if (unlikely(!http_add_header(htx, n, v)))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200841 goto return_bad_req;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200842 }
843
844 /*
845 * 9: add X-Forwarded-For if either the frontend or the backend
846 * asks for it.
847 */
848 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200849 struct http_hdr_ctx ctx = { .blk = NULL };
850 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
851 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
852
Christopher Faulete0768eb2018-10-03 16:38:02 +0200853 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200854 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200855 /* The header is set to be added only if none is present
856 * and we found it, so don't do anything.
857 */
858 }
859 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
860 /* Add an X-Forwarded-For header unless the source IP is
861 * in the 'except' network range.
862 */
863 if ((!sess->fe->except_mask.s_addr ||
864 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
865 != sess->fe->except_net.s_addr) &&
866 (!s->be->except_mask.s_addr ||
867 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
868 != s->be->except_net.s_addr)) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200869 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200870
871 /* Note: we rely on the backend to get the header name to be used for
872 * x-forwarded-for, because the header is really meant for the backends.
873 * However, if the backend did not specify any option, we have to rely
874 * on the frontend's header name.
875 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200876 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
877 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200878 goto return_bad_req;
879 }
880 }
881 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
882 /* FIXME: for the sake of completeness, we should also support
883 * 'except' here, although it is mostly useless in this case.
884 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200885 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200886
Christopher Faulete0768eb2018-10-03 16:38:02 +0200887 inet_ntop(AF_INET6,
888 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
889 pn, sizeof(pn));
890
891 /* Note: we rely on the backend to get the header name to be used for
892 * x-forwarded-for, because the header is really meant for the backends.
893 * However, if the backend did not specify any option, we have to rely
894 * on the frontend's header name.
895 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200896 chunk_printf(&trash, "%s", pn);
897 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200898 goto return_bad_req;
899 }
900 }
901
902 /*
903 * 10: add X-Original-To if either the frontend or the backend
904 * asks for it.
905 */
906 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
907
908 /* FIXME: don't know if IPv6 can handle that case too. */
909 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
910 /* Add an X-Original-To header unless the destination IP is
911 * in the 'except' network range.
912 */
913 conn_get_to_addr(cli_conn);
914
915 if (cli_conn->addr.to.ss_family == AF_INET &&
916 ((!sess->fe->except_mask_to.s_addr ||
917 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
918 != sess->fe->except_to.s_addr) &&
919 (!s->be->except_mask_to.s_addr ||
920 (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
921 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200922 struct ist hdr;
923 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200924
925 /* Note: we rely on the backend to get the header name to be used for
926 * x-original-to, because the header is really meant for the backends.
927 * However, if the backend did not specify any option, we have to rely
928 * on the frontend's header name.
929 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200930 if (s->be->orgto_hdr_len)
931 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
932 else
933 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200934
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200935 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
936 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200937 goto return_bad_req;
938 }
939 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200940 }
941
Christopher Faulete0768eb2018-10-03 16:38:02 +0200942 /* If we have no server assigned yet and we're balancing on url_param
943 * with a POST request, we may be interested in checking the body for
944 * that parameter. This will be done in another analyser.
945 */
946 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200947 s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200948 channel_dont_connect(req);
949 req->analysers |= AN_REQ_HTTP_BODY;
950 }
951
952 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
953 req->analysers |= AN_REQ_HTTP_XFER_BODY;
954#ifdef TCP_QUICKACK
955 /* We expect some data from the client. Unless we know for sure
956 * we already have a full request, we have to re-enable quick-ack
957 * in case we previously disabled it, otherwise we might cause
958 * the client to delay further data.
959 */
960 if ((sess->listener->options & LI_O_NOQUICKACK) &&
961 cli_conn && conn_ctrl_ready(cli_conn) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200962 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200963 setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
964#endif
965
966 /*************************************************************
967 * OK, that's finished for the headers. We have done what we *
968 * could. Let's switch to the DATA state. *
969 ************************************************************/
970 req->analyse_exp = TICK_ETERNITY;
971 req->analysers &= ~an_bit;
972
973 s->logs.tv_request = now;
974 /* OK let's go on with the BODY now */
975 return 1;
976
977 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200978 txn->req.err_state = txn->req.msg_state;
979 txn->req.msg_state = HTTP_MSG_ERROR;
980 txn->status = 400;
981 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200982 htx_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200983
984 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
985 if (sess->listener->counters)
986 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
987
988 if (!(s->flags & SF_ERR_MASK))
989 s->flags |= SF_ERR_PRXCOND;
990 if (!(s->flags & SF_FINST_MASK))
991 s->flags |= SF_FINST_R;
992 return 0;
993}
994
995/* This function is an analyser which processes the HTTP tarpit. It always
996 * returns zero, at the beginning because it prevents any other processing
997 * from occurring, and at the end because it terminates the request.
998 */
999int htx_process_tarpit(struct stream *s, struct channel *req, int an_bit)
1000{
1001 struct http_txn *txn = s->txn;
1002
1003 /* This connection is being tarpitted. The CLIENT side has
1004 * already set the connect expiration date to the right
1005 * timeout. We just have to check that the client is still
1006 * there and that the timeout has not expired.
1007 */
1008 channel_dont_connect(req);
1009 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
1010 !tick_is_expired(req->analyse_exp, now_ms))
1011 return 0;
1012
1013 /* We will set the queue timer to the time spent, just for
1014 * logging purposes. We fake a 500 server error, so that the
1015 * attacker will not suspect his connection has been tarpitted.
1016 * It will not cause trouble to the logs because we can exclude
1017 * the tarpitted connections by filtering on the 'PT' status flags.
1018 */
1019 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1020
1021 if (!(req->flags & CF_READ_ERROR))
Christopher Faulet8137c272018-10-24 11:15:09 +02001022 htx_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001023
1024 req->analysers &= AN_REQ_FLT_END;
1025 req->analyse_exp = TICK_ETERNITY;
1026
1027 if (!(s->flags & SF_ERR_MASK))
1028 s->flags |= SF_ERR_PRXCOND;
1029 if (!(s->flags & SF_FINST_MASK))
1030 s->flags |= SF_FINST_T;
1031 return 0;
1032}
1033
1034/* This function is an analyser which waits for the HTTP request body. It waits
1035 * for either the buffer to be full, or the full advertised contents to have
1036 * reached the buffer. It must only be called after the standard HTTP request
1037 * processing has occurred, because it expects the request to be parsed and will
1038 * look for the Expect header. It may send a 100-Continue interim response. It
1039 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
1040 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
1041 * needs to read more data, or 1 once it has completed its analysis.
1042 */
1043int htx_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
1044{
1045 struct session *sess = s->sess;
1046 struct http_txn *txn = s->txn;
1047 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001048 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001049
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001050
1051 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1052 now_ms, __FUNCTION__,
1053 s,
1054 req,
1055 req->rex, req->wex,
1056 req->flags,
1057 ci_data(req),
1058 req->analysers);
1059
1060 htx = htx_from_buf(&req->buf);
1061
1062 if (msg->msg_state < HTTP_MSG_BODY)
1063 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001064
Christopher Faulete0768eb2018-10-03 16:38:02 +02001065 /* We have to parse the HTTP request body to find any required data.
1066 * "balance url_param check_post" should have been the only way to get
1067 * into this. We were brought here after HTTP header analysis, so all
1068 * related structures are ready.
1069 */
1070
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001071 if (msg->msg_state < HTTP_MSG_DATA) {
1072 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
1073 * send an HTTP/1.1 100 Continue intermediate response.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001074 */
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001075 if (msg->flags & HTTP_MSGF_VER_11) {
1076 struct ist hdr = { .ptr = "Expect", .len = 6 };
1077 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001078
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001079 ctx.blk = NULL;
1080 /* Expect is allowed in 1.1, look for it */
1081 if (http_find_header(htx, hdr, &ctx, 0) &&
1082 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Faulet23a3c792018-11-28 10:01:23 +01001083 if (htx_reply_100_continue(s) == -1)
1084 goto return_bad_req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001085 http_remove_header(htx, &ctx);
1086 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001087 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001088 }
1089
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001090 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001091
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001092 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1093 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001094 */
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001095 if (htx_get_tail_type(htx) >= HTX_BLK_EOD ||
1096 htx_used_space(htx) + global.tune.maxrewrite >= htx->size)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001097 goto http_end;
1098
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001099 missing_data:
Christopher Faulet47365272018-10-31 17:40:50 +01001100 if (htx->flags & HTX_FL_PARSING_ERROR)
1101 goto return_bad_req;
1102
Christopher Faulete0768eb2018-10-03 16:38:02 +02001103 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1104 txn->status = 408;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001105 htx_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001106
1107 if (!(s->flags & SF_ERR_MASK))
1108 s->flags |= SF_ERR_CLITO;
1109 if (!(s->flags & SF_FINST_MASK))
1110 s->flags |= SF_FINST_D;
1111 goto return_err_msg;
1112 }
1113
1114 /* we get here if we need to wait for more data */
1115 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1116 /* Not enough data. We'll re-use the http-request
1117 * timeout here. Ideally, we should set the timeout
1118 * relative to the accept() date. We just set the
1119 * request timeout once at the beginning of the
1120 * request.
1121 */
1122 channel_dont_connect(req);
1123 if (!tick_isset(req->analyse_exp))
1124 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
1125 return 0;
1126 }
1127
1128 http_end:
1129 /* The situation will not evolve, so let's give up on the analysis. */
1130 s->logs.tv_request = now; /* update the request timer to reflect full request */
1131 req->analysers &= ~an_bit;
1132 req->analyse_exp = TICK_ETERNITY;
1133 return 1;
1134
1135 return_bad_req: /* let's centralize all bad requests */
1136 txn->req.err_state = txn->req.msg_state;
1137 txn->req.msg_state = HTTP_MSG_ERROR;
1138 txn->status = 400;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001139 htx_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001140
1141 if (!(s->flags & SF_ERR_MASK))
1142 s->flags |= SF_ERR_PRXCOND;
1143 if (!(s->flags & SF_FINST_MASK))
1144 s->flags |= SF_FINST_R;
1145
1146 return_err_msg:
1147 req->analysers &= AN_REQ_FLT_END;
1148 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1149 if (sess->listener->counters)
1150 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1151 return 0;
1152}
1153
1154/* This function is an analyser which forwards request body (including chunk
1155 * sizes if any). It is called as soon as we must forward, even if we forward
1156 * zero byte. The only situation where it must not be called is when we're in
1157 * tunnel mode and we want to forward till the close. It's used both to forward
1158 * remaining data and to resync after end of body. It expects the msg_state to
1159 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1160 * read more data, or 1 once we can go on with next request or end the stream.
1161 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1162 * bytes of pending data + the headers if not already done.
1163 */
1164int htx_request_forward_body(struct stream *s, struct channel *req, int an_bit)
1165{
1166 struct session *sess = s->sess;
1167 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001168 struct http_msg *msg = &txn->req;
1169 struct htx *htx;
1170 //int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001171
1172 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1173 now_ms, __FUNCTION__,
1174 s,
1175 req,
1176 req->rex, req->wex,
1177 req->flags,
1178 ci_data(req),
1179 req->analysers);
1180
Christopher Faulet9768c262018-10-22 09:34:31 +02001181 htx = htx_from_buf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001182
1183 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1184 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1185 /* Output closed while we were sending data. We must abort and
1186 * wake the other side up.
1187 */
1188 msg->err_state = msg->msg_state;
1189 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001190 htx_end_request(s);
1191 htx_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001192 return 1;
1193 }
1194
1195 /* Note that we don't have to send 100-continue back because we don't
1196 * need the data to complete our job, and it's up to the server to
1197 * decide whether to return 100, 417 or anything else in return of
1198 * an "Expect: 100-continue" header.
1199 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001200 if (msg->msg_state == HTTP_MSG_BODY)
1201 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001202
1203 /* Some post-connect processing might want us to refrain from starting to
1204 * forward data. Currently, the only reason for this is "balance url_param"
1205 * whichs need to parse/process the request after we've enabled forwarding.
1206 */
1207 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
1208 if (!(s->res.flags & CF_READ_ATTACHED)) {
1209 channel_auto_connect(req);
1210 req->flags |= CF_WAKE_CONNECT;
1211 channel_dont_close(req); /* don't fail on early shutr */
1212 goto waiting;
1213 }
1214 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
1215 }
1216
1217 /* in most states, we should abort in case of early close */
1218 channel_auto_close(req);
1219
1220 if (req->to_forward) {
1221 /* We can't process the buffer's contents yet */
1222 req->flags |= CF_WAKE_WRITE;
1223 goto missing_data_or_waiting;
1224 }
1225
Christopher Faulet9768c262018-10-22 09:34:31 +02001226 if (msg->msg_state >= HTTP_MSG_DONE)
1227 goto done;
1228
1229 /* Forward all input data. We get it by removing all outgoing data not
1230 * forwarded yet from HTX data size.
1231 */
1232 c_adv(req, htx->data - co_data(req));
1233
1234 /* To let the function channel_forward work as expected we must update
1235 * the channel's buffer to pretend there is no more input data. The
1236 * right length is then restored. We must do that, because when an HTX
1237 * message is stored into a buffer, it appears as full.
1238 */
1239 b_set_data(&req->buf, co_data(req));
Christopher Faulet03599112018-11-27 11:21:21 +01001240 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02001241 htx->extra -= channel_forward(req, htx->extra);
1242 b_set_data(&req->buf, b_size(&req->buf));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001243
Christopher Faulet9768c262018-10-22 09:34:31 +02001244 /* Check if the end-of-message is reached and if so, switch the message
1245 * in HTTP_MSG_DONE state.
1246 */
1247 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1248 goto missing_data_or_waiting;
1249
1250 msg->msg_state = HTTP_MSG_DONE;
1251
1252 done:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001253 /* other states, DONE...TUNNEL */
1254 /* we don't want to forward closes on DONE except in tunnel mode. */
1255 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
1256 channel_dont_close(req);
1257
Christopher Fauletf2824e62018-10-01 12:12:37 +02001258 htx_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001259 if (!(req->analysers & an_bit)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02001260 htx_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001261 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1262 if (req->flags & CF_SHUTW) {
1263 /* request errors are most likely due to the
1264 * server aborting the transfer. */
1265 goto aborted_xfer;
1266 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001267 goto return_bad_req;
1268 }
1269 return 1;
1270 }
1271
1272 /* If "option abortonclose" is set on the backend, we want to monitor
1273 * the client's connection and forward any shutdown notification to the
1274 * server, which will decide whether to close or to go on processing the
1275 * request. We only do that in tunnel mode, and not in other modes since
1276 * it can be abused to exhaust source ports. */
1277 if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) {
1278 channel_auto_read(req);
1279 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
1280 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
1281 s->si[1].flags |= SI_FL_NOLINGER;
1282 channel_auto_close(req);
1283 }
1284 else if (s->txn->meth == HTTP_METH_POST) {
1285 /* POST requests may require to read extra CRLF sent by broken
1286 * browsers and which could cause an RST to be sent upon close
1287 * on some systems (eg: Linux). */
1288 channel_auto_read(req);
1289 }
1290 return 0;
1291
1292 missing_data_or_waiting:
1293 /* stop waiting for data if the input is closed before the end */
Christopher Faulet9768c262018-10-22 09:34:31 +02001294 if (msg->msg_state < HTTP_MSG_DONE && req->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001295 if (!(s->flags & SF_ERR_MASK))
1296 s->flags |= SF_ERR_CLICL;
1297 if (!(s->flags & SF_FINST_MASK)) {
1298 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
1299 s->flags |= SF_FINST_H;
1300 else
1301 s->flags |= SF_FINST_D;
1302 }
1303
1304 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1305 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
1306 if (objt_server(s->target))
1307 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
1308
1309 goto return_bad_req_stats_ok;
1310 }
1311
1312 waiting:
1313 /* waiting for the last bits to leave the buffer */
1314 if (req->flags & CF_SHUTW)
1315 goto aborted_xfer;
1316
Christopher Faulet47365272018-10-31 17:40:50 +01001317 if (htx->flags & HTX_FL_PARSING_ERROR)
1318 goto return_bad_req;
Christopher Faulet9768c262018-10-22 09:34:31 +02001319
Christopher Faulete0768eb2018-10-03 16:38:02 +02001320 /* When TE: chunked is used, we need to get there again to parse remaining
1321 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1322 * And when content-length is used, we never want to let the possible
1323 * shutdown be forwarded to the other side, as the state machine will
1324 * take care of it once the client responds. It's also important to
1325 * prevent TIME_WAITs from accumulating on the backend side, and for
1326 * HTTP/2 where the last frame comes with a shutdown.
1327 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001328 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001329 channel_dont_close(req);
1330
1331 /* We know that more data are expected, but we couldn't send more that
1332 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1333 * system knows it must not set a PUSH on this first part. Interactive
1334 * modes are already handled by the stream sock layer. We must not do
1335 * this in content-length mode because it could present the MSG_MORE
1336 * flag with the last block of forwarded data, which would cause an
1337 * additional delay to be observed by the receiver.
1338 */
1339 if (msg->flags & HTTP_MSGF_TE_CHNK)
1340 req->flags |= CF_EXPECT_MORE;
1341
1342 return 0;
1343
1344 return_bad_req: /* let's centralize all bad requests */
1345 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1346 if (sess->listener->counters)
1347 HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1348
1349 return_bad_req_stats_ok:
1350 txn->req.err_state = txn->req.msg_state;
1351 txn->req.msg_state = HTTP_MSG_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001352 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001353 /* Note: we don't send any error if some data were already sent */
Christopher Faulet9768c262018-10-22 09:34:31 +02001354 htx_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001355 } else {
1356 txn->status = 400;
Christopher Faulet9768c262018-10-22 09:34:31 +02001357 htx_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001358 }
1359 req->analysers &= AN_REQ_FLT_END;
1360 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
1361
1362 if (!(s->flags & SF_ERR_MASK))
1363 s->flags |= SF_ERR_PRXCOND;
1364 if (!(s->flags & SF_FINST_MASK)) {
1365 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
1366 s->flags |= SF_FINST_H;
1367 else
1368 s->flags |= SF_FINST_D;
1369 }
1370 return 0;
1371
1372 aborted_xfer:
1373 txn->req.err_state = txn->req.msg_state;
1374 txn->req.msg_state = HTTP_MSG_ERROR;
Christopher Faulet9768c262018-10-22 09:34:31 +02001375 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001376 /* Note: we don't send any error if some data were already sent */
Christopher Faulet9768c262018-10-22 09:34:31 +02001377 htx_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001378 } else {
1379 txn->status = 502;
Christopher Faulet9768c262018-10-22 09:34:31 +02001380 htx_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001381 }
1382 req->analysers &= AN_REQ_FLT_END;
1383 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
1384
1385 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1386 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
1387 if (objt_server(s->target))
1388 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
1389
1390 if (!(s->flags & SF_ERR_MASK))
1391 s->flags |= SF_ERR_SRVCL;
1392 if (!(s->flags & SF_FINST_MASK)) {
1393 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
1394 s->flags |= SF_FINST_H;
1395 else
1396 s->flags |= SF_FINST_D;
1397 }
1398 return 0;
1399}
1400
1401/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1402 * processing can continue on next analysers, or zero if it either needs more
1403 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1404 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1405 * when it has nothing left to do, and may remove any analyser when it wants to
1406 * abort.
1407 */
1408int htx_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
1409{
Christopher Faulet9768c262018-10-22 09:34:31 +02001410 /*
1411 * We will analyze a complete HTTP response to check the its syntax.
1412 *
1413 * Once the start line and all headers are received, we may perform a
1414 * capture of the error (if any), and we will set a few fields. We also
1415 * logging and finally headers capture.
1416 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001417 struct session *sess = s->sess;
1418 struct http_txn *txn = s->txn;
1419 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001420 struct htx *htx;
Christopher Faulet61608322018-11-23 16:23:45 +01001421 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001422 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001423 int n;
1424
1425 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1426 now_ms, __FUNCTION__,
1427 s,
1428 rep,
1429 rep->rex, rep->wex,
1430 rep->flags,
1431 ci_data(rep),
1432 rep->analysers);
1433
Christopher Faulet9768c262018-10-22 09:34:31 +02001434 htx = htx_from_buf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001435
1436 /*
1437 * Now we quickly check if we have found a full valid response.
1438 * If not so, we check the FD and buffer states before leaving.
1439 * A full response is indicated by the fact that we have seen
1440 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1441 * responses are checked first.
1442 *
1443 * Depending on whether the client is still there or not, we
1444 * may send an error response back or not. Note that normally
1445 * we should only check for HTTP status there, and check I/O
1446 * errors somewhere else.
1447 */
Christopher Faulet72b62732018-11-28 16:44:44 +01001448 if (unlikely(co_data(rep) || htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH)) {
Christopher Faulet47365272018-10-31 17:40:50 +01001449 /*
1450 * First catch invalid response
1451 */
1452 if (htx->flags & HTX_FL_PARSING_ERROR)
1453 goto return_bad_res;
1454
Christopher Faulet9768c262018-10-22 09:34:31 +02001455 /* 1: have we encountered a read error ? */
1456 if (rep->flags & CF_READ_ERROR) {
1457 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001458 goto abort_keep_alive;
1459
1460 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
1461 if (objt_server(s->target)) {
1462 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
1463 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
1464 }
1465
Christopher Faulete0768eb2018-10-03 16:38:02 +02001466 rep->analysers &= AN_RES_FLT_END;
1467 txn->status = 502;
1468
1469 /* Check to see if the server refused the early data.
1470 * If so, just send a 425
1471 */
1472 if (objt_cs(s->si[1].end)) {
1473 struct connection *conn = objt_cs(s->si[1].end)->conn;
1474
1475 if (conn->err_code == CO_ER_SSL_EARLY_FAILED)
1476 txn->status = 425;
1477 }
1478
1479 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Faulet9768c262018-10-22 09:34:31 +02001480 htx_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001481
1482 if (!(s->flags & SF_ERR_MASK))
1483 s->flags |= SF_ERR_SRVCL;
1484 if (!(s->flags & SF_FINST_MASK))
1485 s->flags |= SF_FINST_H;
1486 return 0;
1487 }
1488
Christopher Faulet9768c262018-10-22 09:34:31 +02001489 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001490 else if (rep->flags & CF_READ_TIMEOUT) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001491 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
1492 if (objt_server(s->target)) {
1493 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
1494 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
1495 }
1496
Christopher Faulete0768eb2018-10-03 16:38:02 +02001497 rep->analysers &= AN_RES_FLT_END;
1498 txn->status = 504;
1499 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Faulet9768c262018-10-22 09:34:31 +02001500 htx_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001501
1502 if (!(s->flags & SF_ERR_MASK))
1503 s->flags |= SF_ERR_SRVTO;
1504 if (!(s->flags & SF_FINST_MASK))
1505 s->flags |= SF_FINST_H;
1506 return 0;
1507 }
1508
Christopher Faulet9768c262018-10-22 09:34:31 +02001509 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001510 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
1511 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1512 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
1513 if (objt_server(s->target))
1514 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
1515
1516 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001517 txn->status = 400;
Christopher Faulet9768c262018-10-22 09:34:31 +02001518 htx_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001519
1520 if (!(s->flags & SF_ERR_MASK))
1521 s->flags |= SF_ERR_CLICL;
1522 if (!(s->flags & SF_FINST_MASK))
1523 s->flags |= SF_FINST_H;
1524
1525 /* process_stream() will take care of the error */
1526 return 0;
1527 }
1528
Christopher Faulet9768c262018-10-22 09:34:31 +02001529 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001530 else if (rep->flags & CF_SHUTR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001531 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001532 goto abort_keep_alive;
1533
1534 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
1535 if (objt_server(s->target)) {
1536 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
1537 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
1538 }
1539
Christopher Faulete0768eb2018-10-03 16:38:02 +02001540 rep->analysers &= AN_RES_FLT_END;
1541 txn->status = 502;
1542 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Faulet9768c262018-10-22 09:34:31 +02001543 htx_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001544
1545 if (!(s->flags & SF_ERR_MASK))
1546 s->flags |= SF_ERR_SRVCL;
1547 if (!(s->flags & SF_FINST_MASK))
1548 s->flags |= SF_FINST_H;
1549 return 0;
1550 }
1551
Christopher Faulet9768c262018-10-22 09:34:31 +02001552 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001553 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001554 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001555 goto abort_keep_alive;
1556
1557 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
1558 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001559
1560 if (!(s->flags & SF_ERR_MASK))
1561 s->flags |= SF_ERR_CLICL;
1562 if (!(s->flags & SF_FINST_MASK))
1563 s->flags |= SF_FINST_H;
1564
1565 /* process_stream() will take care of the error */
1566 return 0;
1567 }
1568
1569 channel_dont_close(rep);
1570 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
1571 return 0;
1572 }
1573
1574 /* More interesting part now : we know that we have a complete
1575 * response which at least looks like HTTP. We have an indicator
1576 * of each header's length, so we can parse them quickly.
1577 */
1578
Christopher Faulet9768c262018-10-22 09:34:31 +02001579 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet03599112018-11-27 11:21:21 +01001580 sl = http_find_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001581
Christopher Faulet9768c262018-10-22 09:34:31 +02001582 /* 0: we might have to print this header in debug mode */
1583 if (unlikely((global.mode & MODE_DEBUG) &&
1584 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1585 int32_t pos;
1586
Christopher Faulet03599112018-11-27 11:21:21 +01001587 htx_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001588
1589 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1590 struct htx_blk *blk = htx_get_blk(htx, pos);
1591 enum htx_blk_type type = htx_get_blk_type(blk);
1592
1593 if (type == HTX_BLK_EOH)
1594 break;
1595 if (type != HTX_BLK_HDR)
1596 continue;
1597
1598 htx_debug_hdr("srvhdr", s,
1599 htx_get_blk_name(htx, blk),
1600 htx_get_blk_value(htx, blk));
1601 }
1602 }
1603
Christopher Faulet03599112018-11-27 11:21:21 +01001604 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001605 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001606 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001607 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001608 if (sl->flags & HTX_SL_F_XFER_LEN) {
1609 msg->flags |= HTTP_MSGF_XFER_LEN;
1610 msg->flags |= ((sl->flags & HTX_SL_F_CHNK) ? HTTP_MSGF_TE_CHNK : HTTP_MSGF_CNT_LEN);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001611 if (sl->flags & HTX_SL_F_BODYLESS)
1612 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001613 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001614
1615 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001616 if (n < 1 || n > 5)
1617 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001618
Christopher Faulete0768eb2018-10-03 16:38:02 +02001619 /* when the client triggers a 4xx from the server, it's most often due
1620 * to a missing object or permission. These events should be tracked
1621 * because if they happen often, it may indicate a brute force or a
1622 * vulnerability scan.
1623 */
1624 if (n == 4)
1625 stream_inc_http_err_ctr(s);
1626
1627 if (objt_server(s->target))
1628 HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1);
1629
Christopher Faulete0768eb2018-10-03 16:38:02 +02001630 /* Adjust server's health based on status code. Note: status codes 501
1631 * and 505 are triggered on demand by client request, so we must not
1632 * count them as server failures.
1633 */
1634 if (objt_server(s->target)) {
1635 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
1636 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
1637 else
1638 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
1639 }
1640
1641 /*
1642 * We may be facing a 100-continue response, or any other informational
1643 * 1xx response which is non-final, in which case this is not the right
1644 * response, and we're waiting for the next one. Let's allow this response
1645 * to go to the client and wait for the next one. There's an exception for
1646 * 101 which is used later in the code to switch protocols.
1647 */
1648 if (txn->status < 200 &&
1649 (txn->status == 100 || txn->status >= 102)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001650 //FLT_STRM_CB(s, flt_htx_reset(s, http, htx));
1651 c_adv(rep, htx->data);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001652 msg->msg_state = HTTP_MSG_RPBEFORE;
1653 txn->status = 0;
1654 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet9768c262018-10-22 09:34:31 +02001655 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001656 }
1657
1658 /*
1659 * 2: check for cacheability.
1660 */
1661
1662 switch (txn->status) {
1663 case 200:
1664 case 203:
1665 case 204:
1666 case 206:
1667 case 300:
1668 case 301:
1669 case 404:
1670 case 405:
1671 case 410:
1672 case 414:
1673 case 501:
1674 break;
1675 default:
1676 /* RFC7231#6.1:
1677 * Responses with status codes that are defined as
1678 * cacheable by default (e.g., 200, 203, 204, 206,
1679 * 300, 301, 404, 405, 410, 414, and 501 in this
1680 * specification) can be reused by a cache with
1681 * heuristic expiration unless otherwise indicated
1682 * by the method definition or explicit cache
1683 * controls [RFC7234]; all other status codes are
1684 * not cacheable by default.
1685 */
1686 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1687 break;
1688 }
1689
1690 /*
1691 * 3: we may need to capture headers
1692 */
1693 s->logs.logwait &= ~LW_RESP;
1694 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Faulet9768c262018-10-22 09:34:31 +02001695 htx_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001696
Christopher Faulet9768c262018-10-22 09:34:31 +02001697 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001698 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1699 txn->status == 101)) {
1700 /* Either we've established an explicit tunnel, or we're
1701 * switching the protocol. In both cases, we're very unlikely
1702 * to understand the next protocols. We have to switch to tunnel
1703 * mode, so that we transfer the request and responses then let
1704 * this protocol pass unmodified. When we later implement specific
1705 * parsers for such protocols, we'll want to check the Upgrade
1706 * header which contains information about that protocol for
1707 * responses with status 101 (eg: see RFC2817 about TLS).
1708 */
1709 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001710 }
1711
Christopher Faulet61608322018-11-23 16:23:45 +01001712 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1713 * 407 (Proxy-Authenticate) responses and set the connection to private
1714 */
1715 srv_conn = cs_conn(objt_cs(s->si[1].end));
1716 if (srv_conn) {
1717 struct ist hdr;
1718 struct http_hdr_ctx ctx;
1719
1720 if (txn->status == 401)
1721 hdr = ist("WWW-Authenticate");
1722 else if (txn->status == 407)
1723 hdr = ist("Proxy-Authenticate");
1724 else
1725 goto end;
1726
1727 ctx.blk = NULL;
1728 while (http_find_header(htx, hdr, &ctx, 0)) {
1729 if ((ctx.value.len >= 9 && word_match(ctx.value.ptr, ctx.value.len, "Negotiate", 9)) ||
1730 (ctx.value.len >= 4 && word_match(ctx.value.ptr, ctx.value.len, "NTLM", 4)))
1731 srv_conn->flags |= CO_FL_PRIVATE;
1732 }
1733 }
1734
1735 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001736 /* we want to have the response time before we start processing it */
1737 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1738
1739 /* end of job, return OK */
1740 rep->analysers &= ~an_bit;
1741 rep->analyse_exp = TICK_ETERNITY;
1742 channel_auto_close(rep);
1743 return 1;
1744
Christopher Faulet47365272018-10-31 17:40:50 +01001745 return_bad_res:
1746 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
1747 if (objt_server(s->target)) {
1748 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
1749 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
1750 }
1751 txn->status = 502;
1752 s->si[1].flags |= SI_FL_NOLINGER;
1753 htx_reply_and_close(s, txn->status, http_error_message(s));
1754 rep->analysers &= AN_RES_FLT_END;
1755
1756 if (!(s->flags & SF_ERR_MASK))
1757 s->flags |= SF_ERR_PRXCOND;
1758 if (!(s->flags & SF_FINST_MASK))
1759 s->flags |= SF_FINST_H;
1760 return 0;
1761
Christopher Faulete0768eb2018-10-03 16:38:02 +02001762 abort_keep_alive:
1763 /* A keep-alive request to the server failed on a network error.
1764 * The client is required to retry. We need to close without returning
1765 * any other information so that the client retries.
1766 */
1767 txn->status = 0;
1768 rep->analysers &= AN_RES_FLT_END;
1769 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001770 s->logs.logwait = 0;
1771 s->logs.level = 0;
1772 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Faulet9768c262018-10-22 09:34:31 +02001773 htx_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001774 return 0;
1775}
1776
1777/* This function performs all the processing enabled for the current response.
1778 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1779 * and updates s->res.analysers. It might make sense to explode it into several
1780 * other functions. It works like process_request (see indications above).
1781 */
1782int htx_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
1783{
1784 struct session *sess = s->sess;
1785 struct http_txn *txn = s->txn;
1786 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001787 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001788 struct proxy *cur_proxy;
1789 struct cond_wordlist *wl;
1790 enum rule_result ret = HTTP_RULE_RES_CONT;
1791
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001792 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1793 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001794
Christopher Faulete0768eb2018-10-03 16:38:02 +02001795 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
1796 now_ms, __FUNCTION__,
1797 s,
1798 rep,
1799 rep->rex, rep->wex,
1800 rep->flags,
1801 ci_data(rep),
1802 rep->analysers);
1803
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001804 htx = htx_from_buf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001805
1806 /* The stats applet needs to adjust the Connection header but we don't
1807 * apply any filter there.
1808 */
1809 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1810 rep->analysers &= ~an_bit;
1811 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001812 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001813 }
1814
1815 /*
1816 * We will have to evaluate the filters.
1817 * As opposed to version 1.2, now they will be evaluated in the
1818 * filters order and not in the header order. This means that
1819 * each filter has to be validated among all headers.
1820 *
1821 * Filters are tried with ->be first, then with ->fe if it is
1822 * different from ->be.
1823 *
1824 * Maybe we are in resume condiion. In this case I choose the
1825 * "struct proxy" which contains the rule list matching the resume
1826 * pointer. If none of theses "struct proxy" match, I initialise
1827 * the process with the first one.
1828 *
1829 * In fact, I check only correspondance betwwen the current list
1830 * pointer and the ->fe rule list. If it doesn't match, I initialize
1831 * the loop with the ->be.
1832 */
1833 if (s->current_rule_list == &sess->fe->http_res_rules)
1834 cur_proxy = sess->fe;
1835 else
1836 cur_proxy = s->be;
1837 while (1) {
1838 struct proxy *rule_set = cur_proxy;
1839
1840 /* evaluate http-response rules */
1841 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001842 ret = htx_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001843
1844 if (ret == HTTP_RULE_RES_BADREQ)
1845 goto return_srv_prx_502;
1846
1847 if (ret == HTTP_RULE_RES_DONE) {
1848 rep->analysers &= ~an_bit;
1849 rep->analyse_exp = TICK_ETERNITY;
1850 return 1;
1851 }
1852 }
1853
1854 /* we need to be called again. */
1855 if (ret == HTTP_RULE_RES_YIELD) {
1856 channel_dont_close(rep);
1857 return 0;
1858 }
1859
1860 /* try headers filters */
1861 if (rule_set->rsp_exp != NULL) {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001862 if (htx_apply_filters_to_response(s, rep, rule_set) < 0)
1863 goto return_bad_resp;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001864 }
1865
1866 /* has the response been denied ? */
1867 if (txn->flags & TX_SVDENY) {
1868 if (objt_server(s->target))
1869 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
1870
1871 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
1872 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
1873 if (sess->listener->counters)
1874 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001875 goto return_srv_prx_502;
1876 }
1877
1878 /* add response headers from the rule sets in the same order */
1879 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001880 struct ist n, v;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001881 if (txn->status < 200 && txn->status != 101)
1882 break;
1883 if (wl->cond) {
1884 int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
1885 ret = acl_pass(ret);
1886 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
1887 ret = !ret;
1888 if (!ret)
1889 continue;
1890 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001891
1892 http_parse_header(ist2(wl->s, strlen(wl->s)), &n, &v);
1893 if (unlikely(!http_add_header(htx, n, v)))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001894 goto return_bad_resp;
1895 }
1896
1897 /* check whether we're already working on the frontend */
1898 if (cur_proxy == sess->fe)
1899 break;
1900 cur_proxy = sess->fe;
1901 }
1902
1903 /* After this point, this anayzer can't return yield, so we can
1904 * remove the bit corresponding to this analyzer from the list.
1905 *
1906 * Note that the intermediate returns and goto found previously
1907 * reset the analyzers.
1908 */
1909 rep->analysers &= ~an_bit;
1910 rep->analyse_exp = TICK_ETERNITY;
1911
1912 /* OK that's all we can do for 1xx responses */
1913 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001914 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001915
1916 /*
1917 * Now check for a server cookie.
1918 */
1919 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001920 htx_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001921
1922 /*
1923 * Check for cache-control or pragma headers if required.
1924 */
1925 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
1926 check_response_for_cacheability(s, rep);
1927
1928 /*
1929 * Add server cookie in the response if needed
1930 */
1931 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1932 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1933 (!(s->flags & SF_DIRECT) ||
1934 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1935 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1936 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1937 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1938 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1939 !(s->flags & SF_IGNORE_PRST)) {
1940 /* the server is known, it's not the one the client requested, or the
1941 * cookie's last seen date needs to be refreshed. We have to
1942 * insert a set-cookie here, except if we want to insert only on POST
1943 * requests and this one isn't. Note that servers which don't have cookies
1944 * (eg: some backup servers) will return a full cookie removal request.
1945 */
1946 if (!objt_server(s->target)->cookie) {
1947 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001948 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02001949 s->be->cookie_name);
1950 }
1951 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001952 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001953
1954 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
1955 /* emit last_date, which is mandatory */
1956 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1957 s30tob64((date.tv_sec+3) >> 2,
1958 trash.area + trash.data);
1959 trash.data += 5;
1960
1961 if (s->be->cookie_maxlife) {
1962 /* emit first_date, which is either the original one or
1963 * the current date.
1964 */
1965 trash.area[trash.data++] = COOKIE_DELIM_DATE;
1966 s30tob64(txn->cookie_first_date ?
1967 txn->cookie_first_date >> 2 :
1968 (date.tv_sec+3) >> 2,
1969 trash.area + trash.data);
1970 trash.data += 5;
1971 }
1972 }
1973 chunk_appendf(&trash, "; path=/");
1974 }
1975
1976 if (s->be->cookie_domain)
1977 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
1978
1979 if (s->be->ck_opts & PR_CK_HTTPONLY)
1980 chunk_appendf(&trash, "; HttpOnly");
1981
1982 if (s->be->ck_opts & PR_CK_SECURE)
1983 chunk_appendf(&trash, "; Secure");
1984
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001985 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001986 goto return_bad_resp;
1987
1988 txn->flags &= ~TX_SCK_MASK;
1989 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
1990 /* the server did not change, only the date was updated */
1991 txn->flags |= TX_SCK_UPDATED;
1992 else
1993 txn->flags |= TX_SCK_INSERTED;
1994
1995 /* Here, we will tell an eventual cache on the client side that we don't
1996 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
1997 * Some caches understand the correct form: 'no-cache="set-cookie"', but
1998 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
1999 */
2000 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2001
2002 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2003
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002004 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002005 goto return_bad_resp;
2006 }
2007 }
2008
2009 /*
2010 * Check if result will be cacheable with a cookie.
2011 * We'll block the response if security checks have caught
2012 * nasty things such as a cacheable cookie.
2013 */
2014 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2015 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2016 (s->be->options & PR_O_CHK_CACHE)) {
2017 /* we're in presence of a cacheable response containing
2018 * a set-cookie header. We'll block it as requested by
2019 * the 'checkcache' option, and send an alert.
2020 */
2021 if (objt_server(s->target))
2022 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1);
2023
2024 HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
2025 HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
2026 if (sess->listener->counters)
2027 HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
2028
2029 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2030 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2031 send_log(s->be, LOG_ALERT,
2032 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2033 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2034 goto return_srv_prx_502;
2035 }
2036
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002037 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002038 /* Always enter in the body analyzer */
2039 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2040 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2041
2042 /* if the user wants to log as soon as possible, without counting
2043 * bytes from the server, then this is the right moment. We have
2044 * to temporarily assign bytes_out to log what we currently have.
2045 */
2046 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2047 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002048 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002049 s->do_log(s);
2050 s->logs.bytes_out = 0;
2051 }
2052 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002053
2054 return_bad_resp:
2055 if (objt_server(s->target)) {
2056 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
2057 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
2058 }
2059 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2060
2061 return_srv_prx_502:
2062 rep->analysers &= AN_RES_FLT_END;
2063 txn->status = 502;
2064 s->logs.t_data = -1; /* was not a valid response */
2065 s->si[1].flags |= SI_FL_NOLINGER;
2066 htx_reply_and_close(s, txn->status, http_error_message(s));
2067 if (!(s->flags & SF_ERR_MASK))
2068 s->flags |= SF_ERR_PRXCOND;
2069 if (!(s->flags & SF_FINST_MASK))
2070 s->flags |= SF_FINST_H;
2071 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002072}
2073
2074/* This function is an analyser which forwards response body (including chunk
2075 * sizes if any). It is called as soon as we must forward, even if we forward
2076 * zero byte. The only situation where it must not be called is when we're in
2077 * tunnel mode and we want to forward till the close. It's used both to forward
2078 * remaining data and to resync after end of body. It expects the msg_state to
2079 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2080 * read more data, or 1 once we can go on with next request or end the stream.
2081 *
2082 * It is capable of compressing response data both in content-length mode and
2083 * in chunked mode. The state machines follows different flows depending on
2084 * whether content-length and chunked modes are used, since there are no
2085 * trailers in content-length :
2086 *
2087 * chk-mode cl-mode
2088 * ,----- BODY -----.
2089 * / \
2090 * V size > 0 V chk-mode
2091 * .--> SIZE -------------> DATA -------------> CRLF
2092 * | | size == 0 | last byte |
2093 * | v final crlf v inspected |
2094 * | TRAILERS -----------> DONE |
2095 * | |
2096 * `----------------------------------------------'
2097 *
2098 * Compression only happens in the DATA state, and must be flushed in final
2099 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2100 * is performed at once on final states for all bytes parsed, or when leaving
2101 * on missing data.
2102 */
2103int htx_response_forward_body(struct stream *s, struct channel *res, int an_bit)
2104{
2105 struct session *sess = s->sess;
2106 struct http_txn *txn = s->txn;
2107 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002108 struct htx *htx;
2109 //int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002110
2111 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
2112 now_ms, __FUNCTION__,
2113 s,
2114 res,
2115 res->rex, res->wex,
2116 res->flags,
2117 ci_data(res),
2118 res->analysers);
2119
Christopher Faulet9768c262018-10-22 09:34:31 +02002120 htx = htx_from_buf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002121
2122 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002123 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002124 /* Output closed while we were sending data. We must abort and
2125 * wake the other side up.
2126 */
2127 msg->err_state = msg->msg_state;
2128 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002129 htx_end_response(s);
2130 htx_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002131 return 1;
2132 }
2133
Christopher Faulet9768c262018-10-22 09:34:31 +02002134 if (msg->msg_state == HTTP_MSG_BODY)
2135 msg->msg_state = HTTP_MSG_DATA;
2136
Christopher Faulete0768eb2018-10-03 16:38:02 +02002137 /* in most states, we should abort in case of early close */
2138 channel_auto_close(res);
2139
Christopher Faulete0768eb2018-10-03 16:38:02 +02002140 if (res->to_forward) {
2141 /* We can't process the buffer's contents yet */
2142 res->flags |= CF_WAKE_WRITE;
2143 goto missing_data_or_waiting;
2144 }
2145
Christopher Faulet9768c262018-10-22 09:34:31 +02002146 if (msg->msg_state >= HTTP_MSG_DONE)
2147 goto done;
2148
2149 /* Forward all input data. We get it by removing all outgoing data not
2150 * forwarded yet from HTX data size.
2151 */
2152 c_adv(res, htx->data - co_data(res));
2153
2154 /* To let the function channel_forward work as expected we must update
2155 * the channel's buffer to pretend there is no more input data. The
2156 * right length is then restored. We must do that, because when an HTX
2157 * message is stored into a buffer, it appears as full.
2158 */
2159 b_set_data(&res->buf, co_data(res));
Christopher Faulet03599112018-11-27 11:21:21 +01002160 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulet9768c262018-10-22 09:34:31 +02002161 htx->extra -= channel_forward(res, htx->extra);
2162 b_set_data(&res->buf, b_size(&res->buf));
2163
2164 if (!(msg->flags & HTTP_MSGF_XFER_LEN)) {
2165 /* The server still sending data that should be filtered */
2166 if (res->flags & CF_SHUTR || !HAS_DATA_FILTERS(s, res)) {
2167 msg->msg_state = HTTP_MSG_TUNNEL;
2168 goto done;
2169 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002170 }
2171
Christopher Faulet9768c262018-10-22 09:34:31 +02002172 /* Check if the end-of-message is reached and if so, switch the message
2173 * in HTTP_MSG_DONE state.
2174 */
2175 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2176 goto missing_data_or_waiting;
2177
2178 msg->msg_state = HTTP_MSG_DONE;
2179
2180 done:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002181 /* other states, DONE...TUNNEL */
Christopher Faulet9768c262018-10-22 09:34:31 +02002182 channel_dont_close(res);
2183
Christopher Fauletf2824e62018-10-01 12:12:37 +02002184 htx_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002185 if (!(res->analysers & an_bit)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02002186 htx_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002187 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2188 if (res->flags & CF_SHUTW) {
2189 /* response errors are most likely due to the
2190 * client aborting the transfer. */
2191 goto aborted_xfer;
2192 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002193 goto return_bad_res;
2194 }
2195 return 1;
2196 }
2197 return 0;
2198
2199 missing_data_or_waiting:
2200 if (res->flags & CF_SHUTW)
2201 goto aborted_xfer;
2202
Christopher Faulet47365272018-10-31 17:40:50 +01002203 if (htx->flags & HTX_FL_PARSING_ERROR)
2204 goto return_bad_res;
2205
Christopher Faulete0768eb2018-10-03 16:38:02 +02002206 /* stop waiting for data if the input is closed before the end. If the
2207 * client side was already closed, it means that the client has aborted,
2208 * so we don't want to count this as a server abort. Otherwise it's a
2209 * server abort.
2210 */
Christopher Faulet9768c262018-10-22 09:34:31 +02002211 if (msg->msg_state < HTTP_MSG_DONE && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002212 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
2213 goto aborted_xfer;
2214 /* If we have some pending data, we continue the processing */
Christopher Faulet9768c262018-10-22 09:34:31 +02002215 if (htx_is_empty(htx)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002216 if (!(s->flags & SF_ERR_MASK))
2217 s->flags |= SF_ERR_SRVCL;
2218 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
2219 if (objt_server(s->target))
2220 HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
2221 goto return_bad_res_stats_ok;
2222 }
2223 }
2224
Christopher Faulete0768eb2018-10-03 16:38:02 +02002225 /* When TE: chunked is used, we need to get there again to parse
2226 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002227 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2228 * are filters registered on the stream, we don't want to forward a
2229 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002230 */
Christopher Faulet9768c262018-10-22 09:34:31 +02002231 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_DATA_FILTERS(s, res))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002232 channel_dont_close(res);
2233
2234 /* We know that more data are expected, but we couldn't send more that
2235 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2236 * system knows it must not set a PUSH on this first part. Interactive
2237 * modes are already handled by the stream sock layer. We must not do
2238 * this in content-length mode because it could present the MSG_MORE
2239 * flag with the last block of forwarded data, which would cause an
2240 * additional delay to be observed by the receiver.
2241 */
2242 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2243 res->flags |= CF_EXPECT_MORE;
2244
2245 /* the stream handler will take care of timeouts and errors */
2246 return 0;
2247
2248 return_bad_res: /* let's centralize all bad responses */
2249 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2250 if (objt_server(s->target))
2251 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
2252
2253 return_bad_res_stats_ok:
2254 txn->rsp.err_state = txn->rsp.msg_state;
2255 txn->rsp.msg_state = HTTP_MSG_ERROR;
2256 /* don't send any error message as we're in the body */
Christopher Faulet9768c262018-10-22 09:34:31 +02002257 htx_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002258 res->analysers &= AN_RES_FLT_END;
2259 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
2260 if (objt_server(s->target))
2261 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
2262
2263 if (!(s->flags & SF_ERR_MASK))
2264 s->flags |= SF_ERR_PRXCOND;
2265 if (!(s->flags & SF_FINST_MASK))
2266 s->flags |= SF_FINST_D;
2267 return 0;
2268
2269 aborted_xfer:
2270 txn->rsp.err_state = txn->rsp.msg_state;
2271 txn->rsp.msg_state = HTTP_MSG_ERROR;
2272 /* don't send any error message as we're in the body */
Christopher Faulet9768c262018-10-22 09:34:31 +02002273 htx_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002274 res->analysers &= AN_RES_FLT_END;
2275 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
2276
2277 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2278 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
2279 if (objt_server(s->target))
2280 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
2281
2282 if (!(s->flags & SF_ERR_MASK))
2283 s->flags |= SF_ERR_CLICL;
2284 if (!(s->flags & SF_FINST_MASK))
2285 s->flags |= SF_FINST_D;
2286 return 0;
2287}
2288
Christopher Faulet0f226952018-10-22 09:29:56 +02002289void htx_adjust_conn_mode(struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002290{
2291 struct proxy *fe = strm_fe(s);
2292 int tmp = TX_CON_WANT_CLO;
2293
2294 if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2295 tmp = TX_CON_WANT_TUN;
2296
2297 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
Christopher Faulet0f226952018-10-22 09:29:56 +02002298 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002299}
2300
2301/* Perform an HTTP redirect based on the information in <rule>. The function
2302 * returns non-zero on success, or zero in case of a, irrecoverable error such
2303 * as too large a request to build a valid response.
2304 */
2305int htx_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
2306{
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002307 struct htx *htx = htx_from_buf(&s->req.buf);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002308 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002309 const char *msg_fmt;
2310 struct buffer *chunk;
2311 int ret = 0;
2312
2313 chunk = alloc_trash_chunk();
2314 if (!chunk)
2315 goto leave;
2316
2317 /* build redirect message */
2318 switch(rule->code) {
2319 case 308:
2320 msg_fmt = HTTP_308;
2321 break;
2322 case 307:
2323 msg_fmt = HTTP_307;
2324 break;
2325 case 303:
2326 msg_fmt = HTTP_303;
2327 break;
2328 case 301:
2329 msg_fmt = HTTP_301;
2330 break;
2331 case 302:
2332 default:
2333 msg_fmt = HTTP_302;
2334 break;
2335 }
2336
2337 if (unlikely(!chunk_strcpy(chunk, msg_fmt)))
2338 goto leave;
2339
2340 switch(rule->type) {
2341 case REDIRECT_TYPE_SCHEME: {
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002342 struct http_hdr_ctx ctx;
2343 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002344
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002345 host = ist("");
2346 ctx.blk = NULL;
2347 if (http_find_header(htx, ist("Host"), &ctx, 0))
2348 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002349
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002350 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002351 path = http_get_path(htx_sl_req_uri(sl));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002352 /* build message using path */
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002353 if (path.ptr) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02002354 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2355 int qs = 0;
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002356 while (qs < path.len) {
2357 if (*(path.ptr + qs) == '?') {
2358 path.len = qs;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002359 break;
2360 }
2361 qs++;
2362 }
2363 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002364 }
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002365 else
2366 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002367
2368 if (rule->rdr_str) { /* this is an old "redirect" rule */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002369 /* add scheme */
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002370 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2371 goto leave;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002372 }
2373 else {
2374 /* add scheme with executing log format */
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002375 chunk->data += build_logline(s, chunk->area + chunk->data,
2376 chunk->size - chunk->data,
2377 &rule->rdr_fmt);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002378 }
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002379 /* add "://" + host + path */
2380 if (!chunk_memcat(chunk, "://", 3) ||
2381 !chunk_memcat(chunk, host.ptr, host.len) ||
2382 !chunk_memcat(chunk, path.ptr, path.len))
2383 goto leave;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002384
2385 /* append a slash at the end of the location if needed and missing */
2386 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2387 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002388 if (chunk->data + 1 >= chunk->size)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002389 goto leave;
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002390 chunk->area[chunk->data++] = '/';
Christopher Fauletf2824e62018-10-01 12:12:37 +02002391 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002392 break;
2393 }
2394 case REDIRECT_TYPE_PREFIX: {
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002395 struct ist path;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002396
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002397 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002398 path = http_get_path(htx_sl_req_uri(sl));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002399 /* build message using path */
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002400 if (path.ptr) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02002401 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2402 int qs = 0;
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002403 while (qs < path.len) {
2404 if (*(path.ptr + qs) == '?') {
2405 path.len = qs;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002406 break;
2407 }
2408 qs++;
2409 }
2410 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002411 }
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002412 else
2413 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002414
2415 if (rule->rdr_str) { /* this is an old "redirect" rule */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002416 /* add prefix. Note that if prefix == "/", we don't want to
2417 * add anything, otherwise it makes it hard for the user to
2418 * configure a self-redirection.
2419 */
2420 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002421 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2422 goto leave;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002423 }
2424 }
2425 else {
2426 /* add prefix with executing log format */
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002427 chunk->data += build_logline(s, chunk->area + chunk->data,
2428 chunk->size - chunk->data,
2429 &rule->rdr_fmt);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002430 }
2431
2432 /* add path */
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002433 if (!chunk_memcat(chunk, path.ptr, path.len))
2434 goto leave;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002435
2436 /* append a slash at the end of the location if needed and missing */
2437 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2438 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002439 if (chunk->data + 1 >= chunk->size)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002440 goto leave;
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002441 chunk->area[chunk->data++] = '/';
Christopher Fauletf2824e62018-10-01 12:12:37 +02002442 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002443 break;
2444 }
2445 case REDIRECT_TYPE_LOCATION:
2446 default:
2447 if (rule->rdr_str) { /* this is an old "redirect" rule */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002448 /* add location */
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002449 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2450 goto leave;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002451 }
2452 else {
2453 /* add location with executing log format */
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002454 chunk->data += build_logline(s, chunk->area + chunk->data,
2455 chunk->size - chunk->data,
2456 &rule->rdr_fmt);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002457 }
2458 break;
2459 }
2460
2461 if (rule->cookie_len) {
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002462 if (!chunk_memcat(chunk, "\r\nSet-Cookie: ", 14) ||
2463 !chunk_memcat(chunk, rule->cookie_str, rule->cookie_len))
2464 goto leave;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002465 }
2466
2467 /* add end of headers and the keep-alive/close status. */
2468 txn->status = rule->code;
2469 /* let's log the request time */
2470 s->logs.tv_request = now;
2471
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002472 /* FIXME: close for now, but it could be cool to handle the keep-alive here */
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002473 /* FIXME: check if EOM is here to do keep-alive or not */
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002474 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2475 if (!chunk_memcat(chunk, "\r\nProxy-Connection: close\r\n\r\n", 29))
2476 goto leave;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002477 } else {
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002478 if (!chunk_memcat(chunk, "\r\nConnection: close\r\n\r\n", 23))
2479 goto leave;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002480 }
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002481 htx_reply_and_close(s, txn->status, chunk);
2482 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002483
2484 if (!(s->flags & SF_ERR_MASK))
2485 s->flags |= SF_ERR_LOCAL;
2486 if (!(s->flags & SF_FINST_MASK))
2487 s->flags |= SF_FINST_R;
2488
2489 ret = 1;
Christopher Faulet80f14bf2018-10-24 11:02:25 +02002490 leave:
Christopher Fauletf2824e62018-10-01 12:12:37 +02002491 free_trash_chunk(chunk);
2492 return ret;
2493}
2494
Christopher Faulet72333522018-10-24 11:25:02 +02002495int htx_transform_header_str(struct stream* s, struct channel *chn, struct htx *htx,
2496 struct ist name, const char *str, struct my_regex *re, int action)
2497{
2498 struct http_hdr_ctx ctx;
2499 struct buffer *output = get_trash_chunk();
2500
2501 /* find full header is action is ACT_HTTP_REPLACE_HDR */
2502 ctx.blk = NULL;
2503 while (http_find_header(htx, name, &ctx, (action == ACT_HTTP_REPLACE_HDR))) {
2504 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2505 continue;
2506
2507 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2508 if (output->data == -1)
2509 return -1;
2510 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2511 return -1;
2512 }
2513 return 0;
2514}
2515
2516static int htx_transform_header(struct stream* s, struct channel *chn, struct htx *htx,
2517 const struct ist name, struct list *fmt, struct my_regex *re, int action)
2518{
2519 struct buffer *replace;
2520 int ret = -1;
2521
2522 replace = alloc_trash_chunk();
2523 if (!replace)
2524 goto leave;
2525
2526 replace->data = build_logline(s, replace->area, replace->size, fmt);
2527 if (replace->data >= replace->size - 1)
2528 goto leave;
2529
2530 ret = htx_transform_header_str(s, chn, htx, name, replace->area, re, action);
2531
2532 leave:
2533 free_trash_chunk(replace);
2534 return ret;
2535}
2536
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002537
2538/* Terminate a 103-Erly-hints response and send it to the client. It returns 0
2539 * on success and -1 on error. The response channel is updated accordingly.
2540 */
2541static int htx_reply_103_early_hints(struct channel *res)
2542{
2543 struct htx *htx = htx_from_buf(&res->buf);
2544 size_t data;
2545
2546 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM)) {
2547 /* If an error occurred during an Early-hint rule,
2548 * remove the incomplete HTTP 103 response from the
2549 * buffer */
2550 channel_truncate(res);
2551 return -1;
2552 }
2553
2554 data = htx->data - co_data(res);
2555 b_set_data(&res->buf, b_size(&res->buf));
2556 c_adv(res, data);
2557 res->total += data;
2558 return 0;
2559}
2560
Christopher Faulet6eb92892018-11-15 16:39:29 +01002561/*
2562 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
2563 * built according to <fmt> log line format.
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002564 * If <early_hints> is 0, it is starts a new response by adding the start
2565 * line. If an error occurred -1 is returned. On success 0 is returned. The
2566 * channel is not updated here. It must be done calling the function
2567 * htx_reply_103_early_hints().
Christopher Faulet6eb92892018-11-15 16:39:29 +01002568 */
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002569static 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 +01002570{
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002571 struct channel *res = &s->res;
2572 struct htx *htx = htx_from_buf(&res->buf);
2573 struct buffer *value = alloc_trash_chunk();
2574
Christopher Faulet6eb92892018-11-15 16:39:29 +01002575 if (!early_hints) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002576 struct htx_sl *sl;
2577 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
2578 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2579
2580 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
2581 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
2582 if (!sl)
Christopher Faulet6eb92892018-11-15 16:39:29 +01002583 goto fail;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002584 sl->info.res.status = 103;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002585 }
2586
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002587 value->data = build_logline(s, b_tail(value), b_room(value), fmt);
2588 if (!htx_add_header(htx, name, ist2(b_head(value), b_data(value))))
Christopher Faulet6eb92892018-11-15 16:39:29 +01002589 goto fail;
2590
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002591 free_trash_chunk(value);
2592 b_set_data(&res->buf, b_size(&res->buf));
2593 return 1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002594
2595 fail:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002596 /* If an error occurred during an Early-hint rule, remove the incomplete
2597 * HTTP 103 response from the buffer */
2598 channel_truncate(res);
2599 free_trash_chunk(value);
2600 return -1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002601}
2602
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002603/* This function executes one of the set-{method,path,query,uri} actions. It
2604 * takes the string from the variable 'replace' with length 'len', then modifies
2605 * the relevant part of the request line accordingly. Then it updates various
2606 * pointers to the next elements which were moved, and the total buffer length.
2607 * It finds the action to be performed in p[2], previously filled by function
2608 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2609 * error, though this can be revisited when this code is finally exploited.
2610 *
2611 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2612 * query string and 3 to replace uri.
2613 *
2614 * In query string case, the mark question '?' must be set at the start of the
2615 * string by the caller, event if the replacement query string is empty.
2616 */
2617int htx_req_replace_stline(int action, const char *replace, int len,
2618 struct proxy *px, struct stream *s)
2619{
2620 struct htx *htx = htx_from_buf(&s->req.buf);
2621
2622 switch (action) {
2623 case 0: // method
2624 if (!http_replace_req_meth(htx, ist2(replace, len)))
2625 return -1;
2626 break;
2627
2628 case 1: // path
2629 if (!http_replace_req_path(htx, ist2(replace, len)))
2630 return -1;
2631 break;
2632
2633 case 2: // query
2634 if (!http_replace_req_query(htx, ist2(replace, len)))
2635 return -1;
2636 break;
2637
2638 case 3: // uri
2639 if (!http_replace_req_uri(htx, ist2(replace, len)))
2640 return -1;
2641 break;
2642
2643 default:
2644 return -1;
2645 }
2646 return 0;
2647}
2648
2649/* This function replace the HTTP status code and the associated message. The
2650 * variable <status> contains the new status code. This function never fails.
2651 */
2652void htx_res_set_status(unsigned int status, const char *reason, struct stream *s)
2653{
2654 struct htx *htx = htx_from_buf(&s->res.buf);
2655 char *res;
2656
2657 chunk_reset(&trash);
2658 res = ultoa_o(status, trash.area, trash.size);
2659 trash.data = res - trash.area;
2660
2661 /* Do we have a custom reason format string? */
2662 if (reason == NULL)
2663 reason = http_get_reason(status);
2664
2665 if (!http_replace_res_status(htx, ist2(trash.area, trash.data)))
2666 http_replace_res_reason(htx, ist2(reason, strlen(reason)));
2667}
2668
Christopher Faulet3e964192018-10-24 11:39:23 +02002669/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2670 * transaction <txn>. Returns the verdict of the first rule that prevents
2671 * further processing of the request (auth, deny, ...), and defaults to
2672 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2673 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2674 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2675 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2676 * status.
2677 */
2678static enum rule_result htx_req_get_intercept_rule(struct proxy *px, struct list *rules,
2679 struct stream *s, int *deny_status)
2680{
2681 struct session *sess = strm_sess(s);
2682 struct http_txn *txn = s->txn;
2683 struct htx *htx;
2684 struct connection *cli_conn;
2685 struct act_rule *rule;
2686 struct http_hdr_ctx ctx;
2687 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002688 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2689 int act_flags = 0;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002690 int early_hints = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002691
2692 htx = htx_from_buf(&s->req.buf);
2693
2694 /* If "the current_rule_list" match the executed rule list, we are in
2695 * resume condition. If a resume is needed it is always in the action
2696 * and never in the ACL or converters. In this case, we initialise the
2697 * current rule, and go to the action execution point.
2698 */
2699 if (s->current_rule) {
2700 rule = s->current_rule;
2701 s->current_rule = NULL;
2702 if (s->current_rule_list == rules)
2703 goto resume_execution;
2704 }
2705 s->current_rule_list = rules;
2706
2707 list_for_each_entry(rule, rules, list) {
2708 /* check optional condition */
2709 if (rule->cond) {
2710 int ret;
2711
2712 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2713 ret = acl_pass(ret);
2714
2715 if (rule->cond->pol == ACL_COND_UNLESS)
2716 ret = !ret;
2717
2718 if (!ret) /* condition not matched */
2719 continue;
2720 }
2721
2722 act_flags |= ACT_FLAG_FIRST;
2723 resume_execution:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002724 if (early_hints && rule->action != ACT_HTTP_EARLY_HINT) {
2725 early_hints = 0;
2726 if (htx_reply_103_early_hints(&s->res) == -1) {
2727 rule_ret = HTTP_RULE_RES_BADREQ;
2728 goto end;
2729 }
2730 }
2731
Christopher Faulet3e964192018-10-24 11:39:23 +02002732 switch (rule->action) {
2733 case ACT_ACTION_ALLOW:
2734 rule_ret = HTTP_RULE_RES_STOP;
2735 goto end;
2736
2737 case ACT_ACTION_DENY:
2738 if (deny_status)
2739 *deny_status = rule->deny_status;
2740 rule_ret = HTTP_RULE_RES_DENY;
2741 goto end;
2742
2743 case ACT_HTTP_REQ_TARPIT:
2744 txn->flags |= TX_CLTARPIT;
2745 if (deny_status)
2746 *deny_status = rule->deny_status;
2747 rule_ret = HTTP_RULE_RES_DENY;
2748 goto end;
2749
2750 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002751 /* Auth might be performed on regular http-req rules as well as on stats */
2752 auth_realm = rule->arg.auth.realm;
2753 if (!auth_realm) {
2754 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2755 auth_realm = STATS_DEFAULT_REALM;
2756 else
2757 auth_realm = px->id;
2758 }
2759 /* send 401/407 depending on whether we use a proxy or not. We still
2760 * count one error, because normal browsing won't significantly
2761 * increase the counter but brute force attempts will.
2762 */
Christopher Faulet3e964192018-10-24 11:39:23 +02002763 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Faulet12c51e22018-11-28 15:59:42 +01002764 if (htx_reply_40x_unauthorized(s, auth_realm) == -1)
2765 rule_ret = HTTP_RULE_RES_BADREQ;
2766 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02002767 goto end;
2768
2769 case ACT_HTTP_REDIR:
Christopher Faulet3e964192018-10-24 11:39:23 +02002770 rule_ret = HTTP_RULE_RES_DONE;
2771 if (!htx_apply_redirect_rule(rule->arg.redir, s, txn))
2772 rule_ret = HTTP_RULE_RES_BADREQ;
2773 goto end;
2774
2775 case ACT_HTTP_SET_NICE:
2776 s->task->nice = rule->arg.nice;
2777 break;
2778
2779 case ACT_HTTP_SET_TOS:
2780 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
2781 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
2782 break;
2783
2784 case ACT_HTTP_SET_MARK:
2785#ifdef SO_MARK
2786 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
2787 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
2788#endif
2789 break;
2790
2791 case ACT_HTTP_SET_LOGL:
2792 s->logs.level = rule->arg.loglevel;
2793 break;
2794
2795 case ACT_HTTP_REPLACE_HDR:
2796 case ACT_HTTP_REPLACE_VAL:
2797 if (htx_transform_header(s, &s->req, htx,
2798 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
2799 &rule->arg.hdr_add.fmt,
2800 &rule->arg.hdr_add.re, rule->action)) {
2801 rule_ret = HTTP_RULE_RES_BADREQ;
2802 goto end;
2803 }
2804 break;
2805
2806 case ACT_HTTP_DEL_HDR:
2807 /* remove all occurrences of the header */
2808 ctx.blk = NULL;
2809 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
2810 http_remove_header(htx, &ctx);
2811 break;
2812
2813 case ACT_HTTP_SET_HDR:
2814 case ACT_HTTP_ADD_HDR: {
2815 /* The scope of the trash buffer must be limited to this function. The
2816 * build_logline() function can execute a lot of other function which
2817 * can use the trash buffer. So for limiting the scope of this global
2818 * buffer, we build first the header value using build_logline, and
2819 * after we store the header name.
2820 */
2821 struct buffer *replace;
2822 struct ist n, v;
2823
2824 replace = alloc_trash_chunk();
2825 if (!replace) {
2826 rule_ret = HTTP_RULE_RES_BADREQ;
2827 goto end;
2828 }
2829
2830 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
2831 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2832 v = ist2(replace->area, replace->data);
2833
2834 if (rule->action == ACT_HTTP_SET_HDR) {
2835 /* remove all occurrences of the header */
2836 ctx.blk = NULL;
2837 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
2838 http_remove_header(htx, &ctx);
2839 }
2840
2841 if (!http_add_header(htx, n, v)) {
2842 static unsigned char rate_limit = 0;
2843
2844 if ((rate_limit++ & 255) == 0) {
2845 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);
2846 }
2847
2848 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2849 if (sess->fe != s->be)
2850 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2851 if (sess->listener->counters)
2852 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2853 }
2854 free_trash_chunk(replace);
2855 break;
2856 }
2857
2858 case ACT_HTTP_DEL_ACL:
2859 case ACT_HTTP_DEL_MAP: {
2860 struct pat_ref *ref;
2861 struct buffer *key;
2862
2863 /* collect reference */
2864 ref = pat_ref_lookup(rule->arg.map.ref);
2865 if (!ref)
2866 continue;
2867
2868 /* allocate key */
2869 key = alloc_trash_chunk();
2870 if (!key) {
2871 rule_ret = HTTP_RULE_RES_BADREQ;
2872 goto end;
2873 }
2874
2875 /* collect key */
2876 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2877 key->area[key->data] = '\0';
2878
2879 /* perform update */
2880 /* returned code: 1=ok, 0=ko */
2881 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
2882 pat_ref_delete(ref, key->area);
2883 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
2884
2885 free_trash_chunk(key);
2886 break;
2887 }
2888
2889 case ACT_HTTP_ADD_ACL: {
2890 struct pat_ref *ref;
2891 struct buffer *key;
2892
2893 /* collect reference */
2894 ref = pat_ref_lookup(rule->arg.map.ref);
2895 if (!ref)
2896 continue;
2897
2898 /* allocate key */
2899 key = alloc_trash_chunk();
2900 if (!key) {
2901 rule_ret = HTTP_RULE_RES_BADREQ;
2902 goto end;
2903 }
2904
2905 /* collect key */
2906 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2907 key->area[key->data] = '\0';
2908
2909 /* perform update */
2910 /* add entry only if it does not already exist */
2911 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
2912 if (pat_ref_find_elt(ref, key->area) == NULL)
2913 pat_ref_add(ref, key->area, NULL, NULL);
2914 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
2915
2916 free_trash_chunk(key);
2917 break;
2918 }
2919
2920 case ACT_HTTP_SET_MAP: {
2921 struct pat_ref *ref;
2922 struct buffer *key, *value;
2923
2924 /* collect reference */
2925 ref = pat_ref_lookup(rule->arg.map.ref);
2926 if (!ref)
2927 continue;
2928
2929 /* allocate key */
2930 key = alloc_trash_chunk();
2931 if (!key) {
2932 rule_ret = HTTP_RULE_RES_BADREQ;
2933 goto end;
2934 }
2935
2936 /* allocate value */
2937 value = alloc_trash_chunk();
2938 if (!value) {
2939 free_trash_chunk(key);
2940 rule_ret = HTTP_RULE_RES_BADREQ;
2941 goto end;
2942 }
2943
2944 /* collect key */
2945 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2946 key->area[key->data] = '\0';
2947
2948 /* collect value */
2949 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
2950 value->area[value->data] = '\0';
2951
2952 /* perform update */
2953 if (pat_ref_find_elt(ref, key->area) != NULL)
2954 /* update entry if it exists */
2955 pat_ref_set(ref, key->area, value->area, NULL);
2956 else
2957 /* insert a new entry */
2958 pat_ref_add(ref, key->area, value->area, NULL);
2959
2960 free_trash_chunk(key);
2961 free_trash_chunk(value);
2962 break;
2963 }
2964
2965 case ACT_HTTP_EARLY_HINT:
2966 if (!(txn->req.flags & HTTP_MSGF_VER_11))
2967 break;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002968 early_hints = htx_add_early_hint_header(s, early_hints,
2969 ist2(rule->arg.early_hint.name, rule->arg.early_hint.name_len),
Christopher Faulet3e964192018-10-24 11:39:23 +02002970 &rule->arg.early_hint.fmt);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002971 if (early_hints == -1) {
2972 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02002973 goto end;
2974 }
2975 break;
2976
2977 case ACT_CUSTOM:
2978 if ((s->req.flags & CF_READ_ERROR) ||
2979 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2980 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
2981 (px->options & PR_O_ABRT_CLOSE)))
2982 act_flags |= ACT_FLAG_FINAL;
2983
2984 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
2985 case ACT_RET_ERR:
2986 case ACT_RET_CONT:
2987 break;
2988 case ACT_RET_STOP:
2989 rule_ret = HTTP_RULE_RES_DONE;
2990 goto end;
2991 case ACT_RET_YIELD:
2992 s->current_rule = rule;
2993 rule_ret = HTTP_RULE_RES_YIELD;
2994 goto end;
2995 }
2996 break;
2997
2998 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
2999 /* Note: only the first valid tracking parameter of each
3000 * applies.
3001 */
3002
3003 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3004 struct stktable *t;
3005 struct stksess *ts;
3006 struct stktable_key *key;
3007 void *ptr1, *ptr2;
3008
3009 t = rule->arg.trk_ctr.table.t;
3010 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL,
3011 rule->arg.trk_ctr.expr, NULL);
3012
3013 if (key && (ts = stktable_get_entry(t, key))) {
3014 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3015
3016 /* let's count a new HTTP request as it's the first time we do it */
3017 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3018 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3019 if (ptr1 || ptr2) {
3020 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3021
3022 if (ptr1)
3023 stktable_data_cast(ptr1, http_req_cnt)++;
3024
3025 if (ptr2)
3026 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
3027 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3028
3029 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3030
3031 /* If data was modified, we need to touch to re-schedule sync */
3032 stktable_touch_local(t, ts, 0);
3033 }
3034
3035 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3036 if (sess->fe != s->be)
3037 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3038 }
3039 }
3040 break;
3041
3042 /* other flags exists, but normaly, they never be matched. */
3043 default:
3044 break;
3045 }
3046 }
3047
3048 end:
3049 if (early_hints) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003050 if (htx_reply_103_early_hints(&s->res) == -1)
3051 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02003052 }
3053
3054 /* we reached the end of the rules, nothing to report */
3055 return rule_ret;
3056}
3057
3058/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3059 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3060 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3061 * is returned, the process can continue the evaluation of next rule list. If
3062 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3063 * is returned, it means the operation could not be processed and a server error
3064 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3065 * deny rule. If *YIELD is returned, the caller must call again the function
3066 * with the same context.
3067 */
3068static enum rule_result htx_res_get_intercept_rule(struct proxy *px, struct list *rules,
3069 struct stream *s)
3070{
3071 struct session *sess = strm_sess(s);
3072 struct http_txn *txn = s->txn;
3073 struct htx *htx;
3074 struct connection *cli_conn;
3075 struct act_rule *rule;
3076 struct http_hdr_ctx ctx;
3077 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
3078 int act_flags = 0;
3079
3080 htx = htx_from_buf(&s->res.buf);
3081
3082 /* If "the current_rule_list" match the executed rule list, we are in
3083 * resume condition. If a resume is needed it is always in the action
3084 * and never in the ACL or converters. In this case, we initialise the
3085 * current rule, and go to the action execution point.
3086 */
3087 if (s->current_rule) {
3088 rule = s->current_rule;
3089 s->current_rule = NULL;
3090 if (s->current_rule_list == rules)
3091 goto resume_execution;
3092 }
3093 s->current_rule_list = rules;
3094
3095 list_for_each_entry(rule, rules, list) {
3096 /* check optional condition */
3097 if (rule->cond) {
3098 int ret;
3099
3100 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3101 ret = acl_pass(ret);
3102
3103 if (rule->cond->pol == ACL_COND_UNLESS)
3104 ret = !ret;
3105
3106 if (!ret) /* condition not matched */
3107 continue;
3108 }
3109
3110 act_flags |= ACT_FLAG_FIRST;
3111resume_execution:
3112 switch (rule->action) {
3113 case ACT_ACTION_ALLOW:
3114 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3115 goto end;
3116
3117 case ACT_ACTION_DENY:
3118 txn->flags |= TX_SVDENY;
3119 rule_ret = HTTP_RULE_RES_STOP;
3120 goto end;
3121
3122 case ACT_HTTP_SET_NICE:
3123 s->task->nice = rule->arg.nice;
3124 break;
3125
3126 case ACT_HTTP_SET_TOS:
3127 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
3128 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
3129 break;
3130
3131 case ACT_HTTP_SET_MARK:
3132#ifdef SO_MARK
3133 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
3134 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
3135#endif
3136 break;
3137
3138 case ACT_HTTP_SET_LOGL:
3139 s->logs.level = rule->arg.loglevel;
3140 break;
3141
3142 case ACT_HTTP_REPLACE_HDR:
3143 case ACT_HTTP_REPLACE_VAL:
3144 if (htx_transform_header(s, &s->res, htx,
3145 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
3146 &rule->arg.hdr_add.fmt,
3147 &rule->arg.hdr_add.re, rule->action)) {
3148 rule_ret = HTTP_RULE_RES_BADREQ;
3149 goto end;
3150 }
3151 break;
3152
3153 case ACT_HTTP_DEL_HDR:
3154 /* remove all occurrences of the header */
3155 ctx.blk = NULL;
3156 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3157 http_remove_header(htx, &ctx);
3158 break;
3159
3160 case ACT_HTTP_SET_HDR:
3161 case ACT_HTTP_ADD_HDR: {
3162 struct buffer *replace;
3163 struct ist n, v;
3164
3165 replace = alloc_trash_chunk();
3166 if (!replace) {
3167 rule_ret = HTTP_RULE_RES_BADREQ;
3168 goto end;
3169 }
3170
3171 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
3172 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3173 v = ist2(replace->area, replace->data);
3174
3175 if (rule->action == ACT_HTTP_SET_HDR) {
3176 /* remove all occurrences of the header */
3177 ctx.blk = NULL;
3178 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3179 http_remove_header(htx, &ctx);
3180 }
3181
3182 if (!http_add_header(htx, n, v)) {
3183 static unsigned char rate_limit = 0;
3184
3185 if ((rate_limit++ & 255) == 0) {
3186 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);
3187 }
3188
3189 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
3190 if (sess->fe != s->be)
3191 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
3192 if (sess->listener->counters)
3193 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
3194 if (objt_server(s->target))
3195 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
3196 }
3197 free_trash_chunk(replace);
3198 break;
3199 }
3200
3201 case ACT_HTTP_DEL_ACL:
3202 case ACT_HTTP_DEL_MAP: {
3203 struct pat_ref *ref;
3204 struct buffer *key;
3205
3206 /* collect reference */
3207 ref = pat_ref_lookup(rule->arg.map.ref);
3208 if (!ref)
3209 continue;
3210
3211 /* allocate key */
3212 key = alloc_trash_chunk();
3213 if (!key) {
3214 rule_ret = HTTP_RULE_RES_BADREQ;
3215 goto end;
3216 }
3217
3218 /* collect key */
3219 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3220 key->area[key->data] = '\0';
3221
3222 /* perform update */
3223 /* returned code: 1=ok, 0=ko */
3224 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3225 pat_ref_delete(ref, key->area);
3226 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3227
3228 free_trash_chunk(key);
3229 break;
3230 }
3231
3232 case ACT_HTTP_ADD_ACL: {
3233 struct pat_ref *ref;
3234 struct buffer *key;
3235
3236 /* collect reference */
3237 ref = pat_ref_lookup(rule->arg.map.ref);
3238 if (!ref)
3239 continue;
3240
3241 /* allocate key */
3242 key = alloc_trash_chunk();
3243 if (!key) {
3244 rule_ret = HTTP_RULE_RES_BADREQ;
3245 goto end;
3246 }
3247
3248 /* collect key */
3249 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3250 key->area[key->data] = '\0';
3251
3252 /* perform update */
3253 /* check if the entry already exists */
3254 if (pat_ref_find_elt(ref, key->area) == NULL)
3255 pat_ref_add(ref, key->area, NULL, NULL);
3256
3257 free_trash_chunk(key);
3258 break;
3259 }
3260
3261 case ACT_HTTP_SET_MAP: {
3262 struct pat_ref *ref;
3263 struct buffer *key, *value;
3264
3265 /* collect reference */
3266 ref = pat_ref_lookup(rule->arg.map.ref);
3267 if (!ref)
3268 continue;
3269
3270 /* allocate key */
3271 key = alloc_trash_chunk();
3272 if (!key) {
3273 rule_ret = HTTP_RULE_RES_BADREQ;
3274 goto end;
3275 }
3276
3277 /* allocate value */
3278 value = alloc_trash_chunk();
3279 if (!value) {
3280 free_trash_chunk(key);
3281 rule_ret = HTTP_RULE_RES_BADREQ;
3282 goto end;
3283 }
3284
3285 /* collect key */
3286 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3287 key->area[key->data] = '\0';
3288
3289 /* collect value */
3290 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3291 value->area[value->data] = '\0';
3292
3293 /* perform update */
3294 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3295 if (pat_ref_find_elt(ref, key->area) != NULL)
3296 /* update entry if it exists */
3297 pat_ref_set(ref, key->area, value->area, NULL);
3298 else
3299 /* insert a new entry */
3300 pat_ref_add(ref, key->area, value->area, NULL);
3301 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3302 free_trash_chunk(key);
3303 free_trash_chunk(value);
3304 break;
3305 }
3306
3307 case ACT_HTTP_REDIR:
3308 rule_ret = HTTP_RULE_RES_DONE;
3309 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3310 rule_ret = HTTP_RULE_RES_BADREQ;
3311 goto end;
3312
3313 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3314 /* Note: only the first valid tracking parameter of each
3315 * applies.
3316 */
3317 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3318 struct stktable *t;
3319 struct stksess *ts;
3320 struct stktable_key *key;
3321 void *ptr;
3322
3323 t = rule->arg.trk_ctr.table.t;
3324 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL,
3325 rule->arg.trk_ctr.expr, NULL);
3326
3327 if (key && (ts = stktable_get_entry(t, key))) {
3328 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3329
3330 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3331
3332 /* let's count a new HTTP request as it's the first time we do it */
3333 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3334 if (ptr)
3335 stktable_data_cast(ptr, http_req_cnt)++;
3336
3337 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3338 if (ptr)
3339 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3340 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3341
3342 /* When the client triggers a 4xx from the server, it's most often due
3343 * to a missing object or permission. These events should be tracked
3344 * because if they happen often, it may indicate a brute force or a
3345 * vulnerability scan. Normally this is done when receiving the response
3346 * but here we're tracking after this ought to have been done so we have
3347 * to do it on purpose.
3348 */
3349 if ((unsigned)(txn->status - 400) < 100) {
3350 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3351 if (ptr)
3352 stktable_data_cast(ptr, http_err_cnt)++;
3353
3354 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3355 if (ptr)
3356 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3357 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3358 }
3359
3360 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3361
3362 /* If data was modified, we need to touch to re-schedule sync */
3363 stktable_touch_local(t, ts, 0);
3364
3365 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3366 if (sess->fe != s->be)
3367 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3368 }
3369 }
3370 break;
3371
3372 case ACT_CUSTOM:
3373 if ((s->req.flags & CF_READ_ERROR) ||
3374 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3375 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
3376 (px->options & PR_O_ABRT_CLOSE)))
3377 act_flags |= ACT_FLAG_FINAL;
3378
3379 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
3380 case ACT_RET_ERR:
3381 case ACT_RET_CONT:
3382 break;
3383 case ACT_RET_STOP:
3384 rule_ret = HTTP_RULE_RES_STOP;
3385 goto end;
3386 case ACT_RET_YIELD:
3387 s->current_rule = rule;
3388 rule_ret = HTTP_RULE_RES_YIELD;
3389 goto end;
3390 }
3391 break;
3392
3393 /* other flags exists, but normaly, they never be matched. */
3394 default:
3395 break;
3396 }
3397 }
3398
3399 end:
3400 /* we reached the end of the rules, nothing to report */
3401 return rule_ret;
3402}
3403
Christopher Faulet33640082018-10-24 11:53:01 +02003404/* Iterate the same filter through all request headers.
3405 * Returns 1 if this filter can be stopped upon return, otherwise 0.
3406 * Since it can manage the switch to another backend, it updates the per-proxy
3407 * DENY stats.
3408 */
3409static int htx_apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
3410{
3411 struct http_txn *txn = s->txn;
3412 struct htx *htx;
3413 struct buffer *hdr = get_trash_chunk();
3414 int32_t pos;
3415
3416 htx = htx_from_buf(&req->buf);
3417
3418 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3419 struct htx_blk *blk = htx_get_blk(htx, pos);
3420 enum htx_blk_type type;
3421 struct ist n, v;
3422
3423 next_hdr:
3424 type = htx_get_blk_type(blk);
3425 if (type == HTX_BLK_EOH)
3426 break;
3427 if (type != HTX_BLK_HDR)
3428 continue;
3429
3430 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
3431 return 1;
3432 else if (unlikely(txn->flags & TX_CLALLOW) &&
3433 (exp->action == ACT_ALLOW ||
3434 exp->action == ACT_DENY ||
3435 exp->action == ACT_TARPIT))
3436 return 0;
3437
3438 n = htx_get_blk_name(htx, blk);
3439 v = htx_get_blk_value(htx, blk);
3440
3441 chunk_memcat(hdr, n.ptr, n.len);
3442 hdr->area[hdr->data++] = ':';
3443 hdr->area[hdr->data++] = ' ';
3444 chunk_memcat(hdr, v.ptr, v.len);
3445
3446 /* Now we have one header in <hdr> */
3447
3448 if (regex_exec_match2(exp->preg, hdr->area, hdr->data, MAX_MATCH, pmatch, 0)) {
3449 struct http_hdr_ctx ctx;
3450 int len;
3451
3452 switch (exp->action) {
3453 case ACT_ALLOW:
3454 txn->flags |= TX_CLALLOW;
3455 goto end;
3456
3457 case ACT_DENY:
3458 txn->flags |= TX_CLDENY;
3459 goto end;
3460
3461 case ACT_TARPIT:
3462 txn->flags |= TX_CLTARPIT;
3463 goto end;
3464
3465 case ACT_REPLACE:
3466 len = exp_replace(trash.area, trash.size, hdr->area, exp->replace, pmatch);
3467 if (len < 0)
3468 return -1;
3469
3470 http_parse_header(ist2(trash.area, len), &n, &v);
3471 ctx.blk = blk;
3472 ctx.value = v;
3473 if (!http_replace_header(htx, &ctx, n, v))
3474 return -1;
3475 if (!ctx.blk)
3476 goto end;
3477 pos = htx_get_blk_pos(htx, blk);
3478 break;
3479
3480 case ACT_REMOVE:
3481 ctx.blk = blk;
3482 ctx.value = v;
3483 if (!http_remove_header(htx, &ctx))
3484 return -1;
3485 if (!ctx.blk)
3486 goto end;
3487 pos = htx_get_blk_pos(htx, blk);
3488 goto next_hdr;
3489
3490 }
3491 }
3492 }
3493 end:
3494 return 0;
3495}
3496
3497/* Apply the filter to the request line.
3498 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3499 * or -1 if a replacement resulted in an invalid request line.
3500 * Since it can manage the switch to another backend, it updates the per-proxy
3501 * DENY stats.
3502 */
3503static int htx_apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
3504{
3505 struct http_txn *txn = s->txn;
3506 struct htx *htx;
3507 struct buffer *reqline = get_trash_chunk();
3508 int done;
3509
3510 htx = htx_from_buf(&req->buf);
3511
3512 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
3513 return 1;
3514 else if (unlikely(txn->flags & TX_CLALLOW) &&
3515 (exp->action == ACT_ALLOW ||
3516 exp->action == ACT_DENY ||
3517 exp->action == ACT_TARPIT))
3518 return 0;
3519 else if (exp->action == ACT_REMOVE)
3520 return 0;
3521
3522 done = 0;
3523
3524 reqline->data = htx_fmt_req_line(http_find_stline(htx), reqline->area, reqline->size);
3525
3526 /* Now we have the request line between cur_ptr and cur_end */
3527 if (regex_exec_match2(exp->preg, reqline->area, reqline->data, MAX_MATCH, pmatch, 0)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003528 struct htx_sl *sl = http_find_stline(htx);
3529 struct ist meth, uri, vsn;
Christopher Faulet33640082018-10-24 11:53:01 +02003530 int len;
3531
3532 switch (exp->action) {
3533 case ACT_ALLOW:
3534 txn->flags |= TX_CLALLOW;
3535 done = 1;
3536 break;
3537
3538 case ACT_DENY:
3539 txn->flags |= TX_CLDENY;
3540 done = 1;
3541 break;
3542
3543 case ACT_TARPIT:
3544 txn->flags |= TX_CLTARPIT;
3545 done = 1;
3546 break;
3547
3548 case ACT_REPLACE:
3549 len = exp_replace(trash.area, trash.size, reqline->area, exp->replace, pmatch);
3550 if (len < 0)
3551 return -1;
3552
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003553 http_parse_stline(ist2(trash.area, len), &meth, &uri, &vsn);
3554 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
3555 if (!http_replace_stline(htx, meth, uri, vsn))
Christopher Faulet33640082018-10-24 11:53:01 +02003556 return -1;
3557 done = 1;
3558 break;
3559 }
3560 }
3561 return done;
3562}
3563
3564/*
3565 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
3566 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
3567 * unparsable request. Since it can manage the switch to another backend, it
3568 * updates the per-proxy DENY stats.
3569 */
3570static int htx_apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
3571{
3572 struct session *sess = s->sess;
3573 struct http_txn *txn = s->txn;
3574 struct hdr_exp *exp;
3575
3576 for (exp = px->req_exp; exp; exp = exp->next) {
3577 int ret;
3578
3579 /*
3580 * The interleaving of transformations and verdicts
3581 * makes it difficult to decide to continue or stop
3582 * the evaluation.
3583 */
3584
3585 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
3586 break;
3587
3588 if ((txn->flags & TX_CLALLOW) &&
3589 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3590 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
3591 continue;
3592
3593 /* if this filter had a condition, evaluate it now and skip to
3594 * next filter if the condition does not match.
3595 */
3596 if (exp->cond) {
3597 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3598 ret = acl_pass(ret);
3599 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
3600 ret = !ret;
3601
3602 if (!ret)
3603 continue;
3604 }
3605
3606 /* Apply the filter to the request line. */
3607 ret = htx_apply_filter_to_req_line(s, req, exp);
3608 if (unlikely(ret < 0))
3609 return -1;
3610
3611 if (likely(ret == 0)) {
3612 /* The filter did not match the request, it can be
3613 * iterated through all headers.
3614 */
3615 if (unlikely(htx_apply_filter_to_req_headers(s, req, exp) < 0))
3616 return -1;
3617 }
3618 }
3619 return 0;
3620}
3621
3622/* Iterate the same filter through all response headers contained in <res>.
3623 * Returns 1 if this filter can be stopped upon return, otherwise 0.
3624 */
3625static int htx_apply_filter_to_resp_headers(struct stream *s, struct channel *res, struct hdr_exp *exp)
3626{
3627 struct http_txn *txn = s->txn;
3628 struct htx *htx;
3629 struct buffer *hdr = get_trash_chunk();
3630 int32_t pos;
3631
3632 htx = htx_from_buf(&res->buf);
3633
3634 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3635 struct htx_blk *blk = htx_get_blk(htx, pos);
3636 enum htx_blk_type type;
3637 struct ist n, v;
3638
3639 next_hdr:
3640 type = htx_get_blk_type(blk);
3641 if (type == HTX_BLK_EOH)
3642 break;
3643 if (type != HTX_BLK_HDR)
3644 continue;
3645
3646 if (unlikely(txn->flags & TX_SVDENY))
3647 return 1;
3648 else if (unlikely(txn->flags & TX_SVALLOW) &&
3649 (exp->action == ACT_ALLOW ||
3650 exp->action == ACT_DENY))
3651 return 0;
3652
3653 n = htx_get_blk_name(htx, blk);
3654 v = htx_get_blk_value(htx, blk);
3655
3656 chunk_memcat(hdr, n.ptr, n.len);
3657 hdr->area[hdr->data++] = ':';
3658 hdr->area[hdr->data++] = ' ';
3659 chunk_memcat(hdr, v.ptr, v.len);
3660
3661 /* Now we have one header in <hdr> */
3662
3663 if (regex_exec_match2(exp->preg, hdr->area, hdr->data, MAX_MATCH, pmatch, 0)) {
3664 struct http_hdr_ctx ctx;
3665 int len;
3666
3667 switch (exp->action) {
3668 case ACT_ALLOW:
3669 txn->flags |= TX_SVALLOW;
3670 goto end;
3671 break;
3672
3673 case ACT_DENY:
3674 txn->flags |= TX_SVDENY;
3675 goto end;
3676 break;
3677
3678 case ACT_REPLACE:
3679 len = exp_replace(trash.area, trash.size, hdr->area, exp->replace, pmatch);
3680 if (len < 0)
3681 return -1;
3682
3683 http_parse_header(ist2(trash.area, len), &n, &v);
3684 ctx.blk = blk;
3685 ctx.value = v;
3686 if (!http_replace_header(htx, &ctx, n, v))
3687 return -1;
3688 if (!ctx.blk)
3689 goto end;
3690 pos = htx_get_blk_pos(htx, blk);
3691 break;
3692
3693 case ACT_REMOVE:
3694 ctx.blk = blk;
3695 ctx.value = v;
3696 if (!http_remove_header(htx, &ctx))
3697 return -1;
3698 if (!ctx.blk)
3699 goto end;
3700 pos = htx_get_blk_pos(htx, blk);
3701 goto next_hdr;
3702 }
3703 }
3704
3705 }
3706 end:
3707 return 0;
3708}
3709
3710/* Apply the filter to the status line in the response buffer <res>.
3711 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3712 * or -1 if a replacement resulted in an invalid status line.
3713 */
3714static int htx_apply_filter_to_sts_line(struct stream *s, struct channel *res, struct hdr_exp *exp)
3715{
3716 struct http_txn *txn = s->txn;
3717 struct htx *htx;
3718 struct buffer *resline = get_trash_chunk();
3719 int done;
3720
3721 htx = htx_from_buf(&res->buf);
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 else if (exp->action == ACT_REMOVE)
3730 return 0;
3731
3732 done = 0;
3733 resline->data = htx_fmt_res_line(http_find_stline(htx), resline->area, resline->size);
3734
3735 /* Now we have the status line between cur_ptr and cur_end */
3736 if (regex_exec_match2(exp->preg, resline->area, resline->data, MAX_MATCH, pmatch, 0)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003737 struct htx_sl *sl = http_find_stline(htx);
3738 struct ist vsn, code, reason;
Christopher Faulet33640082018-10-24 11:53:01 +02003739 int len;
3740
3741 switch (exp->action) {
3742 case ACT_ALLOW:
3743 txn->flags |= TX_SVALLOW;
3744 done = 1;
3745 break;
3746
3747 case ACT_DENY:
3748 txn->flags |= TX_SVDENY;
3749 done = 1;
3750 break;
3751
3752 case ACT_REPLACE:
3753 len = exp_replace(trash.area, trash.size, resline->area, exp->replace, pmatch);
3754 if (len < 0)
3755 return -1;
3756
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003757 http_parse_stline(ist2(trash.area, len), &vsn, &code, &reason);
3758 sl->info.res.status = strl2ui(code.ptr, code.len);
3759 if (!http_replace_stline(htx, vsn, code, reason))
Christopher Faulet33640082018-10-24 11:53:01 +02003760 return -1;
3761
3762 done = 1;
3763 return 1;
3764 }
3765 }
3766 return done;
3767}
3768
3769/*
3770 * Apply all the resp filters of proxy <px> to all headers in buffer <res> of stream <s>.
3771 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
3772 * unparsable response.
3773 */
3774static int htx_apply_filters_to_response(struct stream *s, struct channel *res, struct proxy *px)
3775{
3776 struct session *sess = s->sess;
3777 struct http_txn *txn = s->txn;
3778 struct hdr_exp *exp;
3779
3780 for (exp = px->rsp_exp; exp; exp = exp->next) {
3781 int ret;
3782
3783 /*
3784 * The interleaving of transformations and verdicts
3785 * makes it difficult to decide to continue or stop
3786 * the evaluation.
3787 */
3788
3789 if (txn->flags & TX_SVDENY)
3790 break;
3791
3792 if ((txn->flags & TX_SVALLOW) &&
3793 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3794 exp->action == ACT_PASS)) {
3795 exp = exp->next;
3796 continue;
3797 }
3798
3799 /* if this filter had a condition, evaluate it now and skip to
3800 * next filter if the condition does not match.
3801 */
3802 if (exp->cond) {
3803 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3804 ret = acl_pass(ret);
3805 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
3806 ret = !ret;
3807 if (!ret)
3808 continue;
3809 }
3810
3811 /* Apply the filter to the status line. */
3812 ret = htx_apply_filter_to_sts_line(s, res, exp);
3813 if (unlikely(ret < 0))
3814 return -1;
3815
3816 if (likely(ret == 0)) {
3817 /* The filter did not match the response, it can be
3818 * iterated through all headers.
3819 */
3820 if (unlikely(htx_apply_filter_to_resp_headers(s, res, exp) < 0))
3821 return -1;
3822 }
3823 }
3824 return 0;
3825}
3826
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003827/*
3828 * Manage client-side cookie. It can impact performance by about 2% so it is
3829 * desirable to call it only when needed. This code is quite complex because
3830 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3831 * highly recommended not to touch this part without a good reason !
3832 */
3833static void htx_manage_client_side_cookies(struct stream *s, struct channel *req)
3834{
3835 struct session *sess = s->sess;
3836 struct http_txn *txn = s->txn;
3837 struct htx *htx;
3838 struct http_hdr_ctx ctx;
3839 char *hdr_beg, *hdr_end, *del_from;
3840 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3841 int preserve_hdr;
3842
3843 htx = htx_from_buf(&req->buf);
3844 ctx.blk = NULL;
3845 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
3846 del_from = NULL; /* nothing to be deleted */
3847 preserve_hdr = 0; /* assume we may kill the whole header */
3848
3849 /* Now look for cookies. Conforming to RFC2109, we have to support
3850 * attributes whose name begin with a '$', and associate them with
3851 * the right cookie, if we want to delete this cookie.
3852 * So there are 3 cases for each cookie read :
3853 * 1) it's a special attribute, beginning with a '$' : ignore it.
3854 * 2) it's a server id cookie that we *MAY* want to delete : save
3855 * some pointers on it (last semi-colon, beginning of cookie...)
3856 * 3) it's an application cookie : we *MAY* have to delete a previous
3857 * "special" cookie.
3858 * At the end of loop, if a "special" cookie remains, we may have to
3859 * remove it. If no application cookie persists in the header, we
3860 * *MUST* delete it.
3861 *
3862 * Note: RFC2965 is unclear about the processing of spaces around
3863 * the equal sign in the ATTR=VALUE form. A careful inspection of
3864 * the RFC explicitly allows spaces before it, and not within the
3865 * tokens (attrs or values). An inspection of RFC2109 allows that
3866 * too but section 10.1.3 lets one think that spaces may be allowed
3867 * after the equal sign too, resulting in some (rare) buggy
3868 * implementations trying to do that. So let's do what servers do.
3869 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3870 * allowed quoted strings in values, with any possible character
3871 * after a backslash, including control chars and delimitors, which
3872 * causes parsing to become ambiguous. Browsers also allow spaces
3873 * within values even without quotes.
3874 *
3875 * We have to keep multiple pointers in order to support cookie
3876 * removal at the beginning, middle or end of header without
3877 * corrupting the header. All of these headers are valid :
3878 *
3879 * hdr_beg hdr_end
3880 * | |
3881 * v |
3882 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3883 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3884 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3885 * | | | | | | |
3886 * | | | | | | |
3887 * | | | | | | +--> next
3888 * | | | | | +----> val_end
3889 * | | | | +-----------> val_beg
3890 * | | | +--------------> equal
3891 * | | +----------------> att_end
3892 * | +---------------------> att_beg
3893 * +--------------------------> prev
3894 *
3895 */
3896 hdr_beg = ctx.value.ptr;
3897 hdr_end = hdr_beg + ctx.value.len;
3898 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3899 /* Iterate through all cookies on this line */
3900
3901 /* find att_beg */
3902 att_beg = prev;
3903 if (prev > hdr_beg)
3904 att_beg++;
3905
3906 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3907 att_beg++;
3908
3909 /* find att_end : this is the first character after the last non
3910 * space before the equal. It may be equal to hdr_end.
3911 */
3912 equal = att_end = att_beg;
3913 while (equal < hdr_end) {
3914 if (*equal == '=' || *equal == ',' || *equal == ';')
3915 break;
3916 if (HTTP_IS_SPHT(*equal++))
3917 continue;
3918 att_end = equal;
3919 }
3920
3921 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3922 * is between <att_beg> and <equal>, both may be identical.
3923 */
3924 /* look for end of cookie if there is an equal sign */
3925 if (equal < hdr_end && *equal == '=') {
3926 /* look for the beginning of the value */
3927 val_beg = equal + 1;
3928 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3929 val_beg++;
3930
3931 /* find the end of the value, respecting quotes */
3932 next = http_find_cookie_value_end(val_beg, hdr_end);
3933
3934 /* make val_end point to the first white space or delimitor after the value */
3935 val_end = next;
3936 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3937 val_end--;
3938 }
3939 else
3940 val_beg = val_end = next = equal;
3941
3942 /* We have nothing to do with attributes beginning with
3943 * '$'. However, they will automatically be removed if a
3944 * header before them is removed, since they're supposed
3945 * to be linked together.
3946 */
3947 if (*att_beg == '$')
3948 continue;
3949
3950 /* Ignore cookies with no equal sign */
3951 if (equal == next) {
3952 /* This is not our cookie, so we must preserve it. But if we already
3953 * scheduled another cookie for removal, we cannot remove the
3954 * complete header, but we can remove the previous block itself.
3955 */
3956 preserve_hdr = 1;
3957 if (del_from != NULL) {
3958 int delta = htx_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
3959 val_end += delta;
3960 next += delta;
3961 hdr_end += delta;
3962 prev = del_from;
3963 del_from = NULL;
3964 }
3965 continue;
3966 }
3967
3968 /* if there are spaces around the equal sign, we need to
3969 * strip them otherwise we'll get trouble for cookie captures,
3970 * or even for rewrites. Since this happens extremely rarely,
3971 * it does not hurt performance.
3972 */
3973 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3974 int stripped_before = 0;
3975 int stripped_after = 0;
3976
3977 if (att_end != equal) {
3978 memmove(att_end, equal, hdr_end - equal);
3979 stripped_before = (att_end - equal);
3980 equal += stripped_before;
3981 val_beg += stripped_before;
3982 }
3983
3984 if (val_beg > equal + 1) {
3985 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3986 stripped_after = (equal + 1) - val_beg;
3987 val_beg += stripped_after;
3988 stripped_before += stripped_after;
3989 }
3990
3991 val_end += stripped_before;
3992 next += stripped_before;
3993 hdr_end += stripped_before;
3994 }
3995 /* now everything is as on the diagram above */
3996
3997 /* First, let's see if we want to capture this cookie. We check
3998 * that we don't already have a client side cookie, because we
3999 * can only capture one. Also as an optimisation, we ignore
4000 * cookies shorter than the declared name.
4001 */
4002 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
4003 (val_end - att_beg >= sess->fe->capture_namelen) &&
4004 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
4005 int log_len = val_end - att_beg;
4006
4007 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
4008 ha_alert("HTTP logging : out of memory.\n");
4009 } else {
4010 if (log_len > sess->fe->capture_len)
4011 log_len = sess->fe->capture_len;
4012 memcpy(txn->cli_cookie, att_beg, log_len);
4013 txn->cli_cookie[log_len] = 0;
4014 }
4015 }
4016
4017 /* Persistence cookies in passive, rewrite or insert mode have the
4018 * following form :
4019 *
4020 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
4021 *
4022 * For cookies in prefix mode, the form is :
4023 *
4024 * Cookie: NAME=SRV~VALUE
4025 */
4026 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
4027 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
4028 struct server *srv = s->be->srv;
4029 char *delim;
4030
4031 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4032 * have the server ID between val_beg and delim, and the original cookie between
4033 * delim+1 and val_end. Otherwise, delim==val_end :
4034 *
4035 * hdr_beg
4036 * |
4037 * v
4038 * NAME=SRV; # in all but prefix modes
4039 * NAME=SRV~OPAQUE ; # in prefix mode
4040 * || || | |+-> next
4041 * || || | +--> val_end
4042 * || || +---------> delim
4043 * || |+------------> val_beg
4044 * || +-------------> att_end = equal
4045 * |+-----------------> att_beg
4046 * +------------------> prev
4047 *
4048 */
4049 if (s->be->ck_opts & PR_CK_PFX) {
4050 for (delim = val_beg; delim < val_end; delim++)
4051 if (*delim == COOKIE_DELIM)
4052 break;
4053 }
4054 else {
4055 char *vbar1;
4056 delim = val_end;
4057 /* Now check if the cookie contains a date field, which would
4058 * appear after a vertical bar ('|') just after the server name
4059 * and before the delimiter.
4060 */
4061 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
4062 if (vbar1) {
4063 /* OK, so left of the bar is the server's cookie and
4064 * right is the last seen date. It is a base64 encoded
4065 * 30-bit value representing the UNIX date since the
4066 * epoch in 4-second quantities.
4067 */
4068 int val;
4069 delim = vbar1++;
4070 if (val_end - vbar1 >= 5) {
4071 val = b64tos30(vbar1);
4072 if (val > 0)
4073 txn->cookie_last_date = val << 2;
4074 }
4075 /* look for a second vertical bar */
4076 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
4077 if (vbar1 && (val_end - vbar1 > 5)) {
4078 val = b64tos30(vbar1 + 1);
4079 if (val > 0)
4080 txn->cookie_first_date = val << 2;
4081 }
4082 }
4083 }
4084
4085 /* if the cookie has an expiration date and the proxy wants to check
4086 * it, then we do that now. We first check if the cookie is too old,
4087 * then only if it has expired. We detect strict overflow because the
4088 * time resolution here is not great (4 seconds). Cookies with dates
4089 * in the future are ignored if their offset is beyond one day. This
4090 * allows an admin to fix timezone issues without expiring everyone
4091 * and at the same time avoids keeping unwanted side effects for too
4092 * long.
4093 */
4094 if (txn->cookie_first_date && s->be->cookie_maxlife &&
4095 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
4096 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
4097 txn->flags &= ~TX_CK_MASK;
4098 txn->flags |= TX_CK_OLD;
4099 delim = val_beg; // let's pretend we have not found the cookie
4100 txn->cookie_first_date = 0;
4101 txn->cookie_last_date = 0;
4102 }
4103 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
4104 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
4105 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
4106 txn->flags &= ~TX_CK_MASK;
4107 txn->flags |= TX_CK_EXPIRED;
4108 delim = val_beg; // let's pretend we have not found the cookie
4109 txn->cookie_first_date = 0;
4110 txn->cookie_last_date = 0;
4111 }
4112
4113 /* Here, we'll look for the first running server which supports the cookie.
4114 * This allows to share a same cookie between several servers, for example
4115 * to dedicate backup servers to specific servers only.
4116 * However, to prevent clients from sticking to cookie-less backup server
4117 * when they have incidentely learned an empty cookie, we simply ignore
4118 * empty cookies and mark them as invalid.
4119 * The same behaviour is applied when persistence must be ignored.
4120 */
4121 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
4122 srv = NULL;
4123
4124 while (srv) {
4125 if (srv->cookie && (srv->cklen == delim - val_beg) &&
4126 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
4127 if ((srv->cur_state != SRV_ST_STOPPED) ||
4128 (s->be->options & PR_O_PERSIST) ||
4129 (s->flags & SF_FORCE_PRST)) {
4130 /* we found the server and we can use it */
4131 txn->flags &= ~TX_CK_MASK;
4132 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
4133 s->flags |= SF_DIRECT | SF_ASSIGNED;
4134 s->target = &srv->obj_type;
4135 break;
4136 } else {
4137 /* we found a server, but it's down,
4138 * mark it as such and go on in case
4139 * another one is available.
4140 */
4141 txn->flags &= ~TX_CK_MASK;
4142 txn->flags |= TX_CK_DOWN;
4143 }
4144 }
4145 srv = srv->next;
4146 }
4147
4148 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
4149 /* no server matched this cookie or we deliberately skipped it */
4150 txn->flags &= ~TX_CK_MASK;
4151 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
4152 txn->flags |= TX_CK_UNUSED;
4153 else
4154 txn->flags |= TX_CK_INVALID;
4155 }
4156
4157 /* depending on the cookie mode, we may have to either :
4158 * - delete the complete cookie if we're in insert+indirect mode, so that
4159 * the server never sees it ;
4160 * - remove the server id from the cookie value, and tag the cookie as an
4161 * application cookie so that it does not get accidentely removed later,
4162 * if we're in cookie prefix mode
4163 */
4164 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
4165 int delta; /* negative */
4166
4167 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
4168 delta = val_beg - (delim + 1);
4169 val_end += delta;
4170 next += delta;
4171 hdr_end += delta;
4172 del_from = NULL;
4173 preserve_hdr = 1; /* we want to keep this cookie */
4174 }
4175 else if (del_from == NULL &&
4176 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
4177 del_from = prev;
4178 }
4179 }
4180 else {
4181 /* This is not our cookie, so we must preserve it. But if we already
4182 * scheduled another cookie for removal, we cannot remove the
4183 * complete header, but we can remove the previous block itself.
4184 */
4185 preserve_hdr = 1;
4186
4187 if (del_from != NULL) {
4188 int delta = htx_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
4189 if (att_beg >= del_from)
4190 att_beg += delta;
4191 if (att_end >= del_from)
4192 att_end += delta;
4193 val_beg += delta;
4194 val_end += delta;
4195 next += delta;
4196 hdr_end += delta;
4197 prev = del_from;
4198 del_from = NULL;
4199 }
4200 }
4201
4202 /* continue with next cookie on this header line */
4203 att_beg = next;
4204 } /* for each cookie */
4205
4206
4207 /* There are no more cookies on this line.
4208 * We may still have one (or several) marked for deletion at the
4209 * end of the line. We must do this now in two ways :
4210 * - if some cookies must be preserved, we only delete from the
4211 * mark to the end of line ;
4212 * - if nothing needs to be preserved, simply delete the whole header
4213 */
4214 if (del_from) {
4215 hdr_end = (preserve_hdr ? del_from : hdr_beg);
4216 }
4217 if ((hdr_end - hdr_beg) != ctx.value.len) {
4218 if (hdr_beg != hdr_end) {
4219 htx_set_blk_value_len(ctx.blk, hdr_end - hdr_beg);
4220 htx->data -= (hdr_end - ctx.value.ptr);
4221 }
4222 else
4223 http_remove_header(htx, &ctx);
4224 }
4225 } /* for each "Cookie header */
4226}
4227
4228/*
4229 * Manage server-side cookies. It can impact performance by about 2% so it is
4230 * desirable to call it only when needed. This function is also used when we
4231 * just need to know if there is a cookie (eg: for check-cache).
4232 */
4233static void htx_manage_server_side_cookies(struct stream *s, struct channel *res)
4234{
4235 struct session *sess = s->sess;
4236 struct http_txn *txn = s->txn;
4237 struct htx *htx;
4238 struct http_hdr_ctx ctx;
4239 struct server *srv;
4240 char *hdr_beg, *hdr_end;
4241 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
4242 int is_cookie2;
4243
4244 htx = htx_from_buf(&res->buf);
4245
4246 ctx.blk = NULL;
4247 while (1) {
4248 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
4249 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
4250 break;
4251 is_cookie2 = 1;
4252 }
4253
4254 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
4255 * <prev> points to the colon.
4256 */
4257 txn->flags |= TX_SCK_PRESENT;
4258
4259 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
4260 * check-cache is enabled) and we are not interested in checking
4261 * them. Warning, the cookie capture is declared in the frontend.
4262 */
4263 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
4264 break;
4265
4266 /* OK so now we know we have to process this response cookie.
4267 * The format of the Set-Cookie header is slightly different
4268 * from the format of the Cookie header in that it does not
4269 * support the comma as a cookie delimiter (thus the header
4270 * cannot be folded) because the Expires attribute described in
4271 * the original Netscape's spec may contain an unquoted date
4272 * with a comma inside. We have to live with this because
4273 * many browsers don't support Max-Age and some browsers don't
4274 * support quoted strings. However the Set-Cookie2 header is
4275 * clean.
4276 *
4277 * We have to keep multiple pointers in order to support cookie
4278 * removal at the beginning, middle or end of header without
4279 * corrupting the header (in case of set-cookie2). A special
4280 * pointer, <scav> points to the beginning of the set-cookie-av
4281 * fields after the first semi-colon. The <next> pointer points
4282 * either to the end of line (set-cookie) or next unquoted comma
4283 * (set-cookie2). All of these headers are valid :
4284 *
4285 * hdr_beg hdr_end
4286 * | |
4287 * v |
4288 * NAME1 = VALUE 1 ; Secure; Path="/" |
4289 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
4290 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
4291 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
4292 * | | | | | | | |
4293 * | | | | | | | +-> next
4294 * | | | | | | +------------> scav
4295 * | | | | | +--------------> val_end
4296 * | | | | +--------------------> val_beg
4297 * | | | +----------------------> equal
4298 * | | +------------------------> att_end
4299 * | +----------------------------> att_beg
4300 * +------------------------------> prev
4301 * -------------------------------> hdr_beg
4302 */
4303 hdr_beg = ctx.value.ptr;
4304 hdr_end = hdr_beg + ctx.value.len;
4305 for (prev = hdr_beg; prev < hdr_end; prev = next) {
4306
4307 /* Iterate through all cookies on this line */
4308
4309 /* find att_beg */
4310 att_beg = prev;
4311 if (prev > hdr_beg)
4312 att_beg++;
4313
4314 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
4315 att_beg++;
4316
4317 /* find att_end : this is the first character after the last non
4318 * space before the equal. It may be equal to hdr_end.
4319 */
4320 equal = att_end = att_beg;
4321
4322 while (equal < hdr_end) {
4323 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
4324 break;
4325 if (HTTP_IS_SPHT(*equal++))
4326 continue;
4327 att_end = equal;
4328 }
4329
4330 /* here, <equal> points to '=', a delimitor or the end. <att_end>
4331 * is between <att_beg> and <equal>, both may be identical.
4332 */
4333
4334 /* look for end of cookie if there is an equal sign */
4335 if (equal < hdr_end && *equal == '=') {
4336 /* look for the beginning of the value */
4337 val_beg = equal + 1;
4338 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
4339 val_beg++;
4340
4341 /* find the end of the value, respecting quotes */
4342 next = http_find_cookie_value_end(val_beg, hdr_end);
4343
4344 /* make val_end point to the first white space or delimitor after the value */
4345 val_end = next;
4346 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
4347 val_end--;
4348 }
4349 else {
4350 /* <equal> points to next comma, semi-colon or EOL */
4351 val_beg = val_end = next = equal;
4352 }
4353
4354 if (next < hdr_end) {
4355 /* Set-Cookie2 supports multiple cookies, and <next> points to
4356 * a colon or semi-colon before the end. So skip all attr-value
4357 * pairs and look for the next comma. For Set-Cookie, since
4358 * commas are permitted in values, skip to the end.
4359 */
4360 if (is_cookie2)
4361 next = http_find_hdr_value_end(next, hdr_end);
4362 else
4363 next = hdr_end;
4364 }
4365
4366 /* Now everything is as on the diagram above */
4367
4368 /* Ignore cookies with no equal sign */
4369 if (equal == val_end)
4370 continue;
4371
4372 /* If there are spaces around the equal sign, we need to
4373 * strip them otherwise we'll get trouble for cookie captures,
4374 * or even for rewrites. Since this happens extremely rarely,
4375 * it does not hurt performance.
4376 */
4377 if (unlikely(att_end != equal || val_beg > equal + 1)) {
4378 int stripped_before = 0;
4379 int stripped_after = 0;
4380
4381 if (att_end != equal) {
4382 memmove(att_end, equal, hdr_end - equal);
4383 stripped_before = (att_end - equal);
4384 equal += stripped_before;
4385 val_beg += stripped_before;
4386 }
4387
4388 if (val_beg > equal + 1) {
4389 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
4390 stripped_after = (equal + 1) - val_beg;
4391 val_beg += stripped_after;
4392 stripped_before += stripped_after;
4393 }
4394
4395 val_end += stripped_before;
4396 next += stripped_before;
4397 hdr_end += stripped_before;
4398
4399 ctx.value.len = hdr_end - hdr_beg;
4400 htx_set_blk_value_len(ctx.blk, ctx.value.len);
4401 htx->data -= (hdr_end - ctx.value.ptr);
4402 }
4403
4404 /* First, let's see if we want to capture this cookie. We check
4405 * that we don't already have a server side cookie, because we
4406 * can only capture one. Also as an optimisation, we ignore
4407 * cookies shorter than the declared name.
4408 */
4409 if (sess->fe->capture_name != NULL &&
4410 txn->srv_cookie == NULL &&
4411 (val_end - att_beg >= sess->fe->capture_namelen) &&
4412 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
4413 int log_len = val_end - att_beg;
4414 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
4415 ha_alert("HTTP logging : out of memory.\n");
4416 }
4417 else {
4418 if (log_len > sess->fe->capture_len)
4419 log_len = sess->fe->capture_len;
4420 memcpy(txn->srv_cookie, att_beg, log_len);
4421 txn->srv_cookie[log_len] = 0;
4422 }
4423 }
4424
4425 srv = objt_server(s->target);
4426 /* now check if we need to process it for persistence */
4427 if (!(s->flags & SF_IGNORE_PRST) &&
4428 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
4429 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
4430 /* assume passive cookie by default */
4431 txn->flags &= ~TX_SCK_MASK;
4432 txn->flags |= TX_SCK_FOUND;
4433
4434 /* If the cookie is in insert mode on a known server, we'll delete
4435 * this occurrence because we'll insert another one later.
4436 * We'll delete it too if the "indirect" option is set and we're in
4437 * a direct access.
4438 */
4439 if (s->be->ck_opts & PR_CK_PSV) {
4440 /* The "preserve" flag was set, we don't want to touch the
4441 * server's cookie.
4442 */
4443 }
4444 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
4445 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
4446 /* this cookie must be deleted */
4447 if (prev == hdr_beg && next == hdr_end) {
4448 /* whole header */
4449 http_remove_header(htx, &ctx);
4450 /* note: while both invalid now, <next> and <hdr_end>
4451 * are still equal, so the for() will stop as expected.
4452 */
4453 } else {
4454 /* just remove the value */
4455 int delta = htx_del_hdr_value(hdr_beg, hdr_end, &prev, next);
4456 next = prev;
4457 hdr_end += delta;
4458 }
4459 txn->flags &= ~TX_SCK_MASK;
4460 txn->flags |= TX_SCK_DELETED;
4461 /* and go on with next cookie */
4462 }
4463 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
4464 /* replace bytes val_beg->val_end with the cookie name associated
4465 * with this server since we know it.
4466 */
4467 int sliding, delta;
4468
4469 ctx.value = ist2(val_beg, val_end - val_beg);
4470 ctx.lws_before = ctx.lws_after = 0;
4471 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
4472 delta = srv->cklen - (val_end - val_beg);
4473 sliding = (ctx.value.ptr - val_beg);
4474 hdr_beg += sliding;
4475 val_beg += sliding;
4476 next += sliding + delta;
4477 hdr_end += sliding + delta;
4478
4479 txn->flags &= ~TX_SCK_MASK;
4480 txn->flags |= TX_SCK_REPLACED;
4481 }
4482 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
4483 /* insert the cookie name associated with this server
4484 * before existing cookie, and insert a delimiter between them..
4485 */
4486 int sliding, delta;
4487 ctx.value = ist2(val_beg, 0);
4488 ctx.lws_before = ctx.lws_after = 0;
4489 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
4490 delta = srv->cklen + 1;
4491 sliding = (ctx.value.ptr - val_beg);
4492 hdr_beg += sliding;
4493 val_beg += sliding;
4494 next += sliding + delta;
4495 hdr_end += sliding + delta;
4496
4497 val_beg[srv->cklen] = COOKIE_DELIM;
4498 txn->flags &= ~TX_SCK_MASK;
4499 txn->flags |= TX_SCK_REPLACED;
4500 }
4501 }
4502 /* that's done for this cookie, check the next one on the same
4503 * line when next != hdr_end (only if is_cookie2).
4504 */
4505 }
4506 }
4507}
4508
Christopher Faulet25a02f62018-10-24 12:00:25 +02004509/*
4510 * Parses the Cache-Control and Pragma request header fields to determine if
4511 * the request may be served from the cache and/or if it is cacheable. Updates
4512 * s->txn->flags.
4513 */
4514void htx_check_request_for_cacheability(struct stream *s, struct channel *req)
4515{
4516 struct http_txn *txn = s->txn;
4517 struct htx *htx;
4518 int32_t pos;
4519 int pragma_found, cc_found, i;
4520
4521 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
4522 return; /* nothing more to do here */
4523
4524 htx = htx_from_buf(&req->buf);
4525 pragma_found = cc_found = 0;
4526 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4527 struct htx_blk *blk = htx_get_blk(htx, pos);
4528 enum htx_blk_type type = htx_get_blk_type(blk);
4529 struct ist n, v;
4530
4531 if (type == HTX_BLK_EOH)
4532 break;
4533 if (type != HTX_BLK_HDR)
4534 continue;
4535
4536 n = htx_get_blk_name(htx, blk);
4537 v = htx_get_blk_value(htx, blk);
4538
4539 if (isteqi(n, ist("Pragma"))) {
4540 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4541 pragma_found = 1;
4542 continue;
4543 }
4544 }
4545
4546 /* Don't use the cache and don't try to store if we found the
4547 * Authorization header */
4548 if (isteqi(n, ist("Authorization"))) {
4549 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4550 txn->flags |= TX_CACHE_IGNORE;
4551 continue;
4552 }
4553
4554 if (!isteqi(n, ist("Cache-control")))
4555 continue;
4556
4557 /* OK, right now we know we have a cache-control header */
4558 cc_found = 1;
4559 if (!v.len) /* no info */
4560 continue;
4561
4562 i = 0;
4563 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4564 !isspace((unsigned char)*(v.ptr+i)))
4565 i++;
4566
4567 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
4568 * values after max-age, max-stale nor min-fresh, we simply don't
4569 * use the cache when they're specified.
4570 */
4571 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
4572 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4573 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
4574 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
4575 txn->flags |= TX_CACHE_IGNORE;
4576 continue;
4577 }
4578
4579 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
4580 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4581 continue;
4582 }
4583 }
4584
4585 /* RFC7234#5.4:
4586 * When the Cache-Control header field is also present and
4587 * understood in a request, Pragma is ignored.
4588 * When the Cache-Control header field is not present in a
4589 * request, caches MUST consider the no-cache request
4590 * pragma-directive as having the same effect as if
4591 * "Cache-Control: no-cache" were present.
4592 */
4593 if (!cc_found && pragma_found)
4594 txn->flags |= TX_CACHE_IGNORE;
4595}
4596
4597/*
4598 * Check if response is cacheable or not. Updates s->txn->flags.
4599 */
4600void htx_check_response_for_cacheability(struct stream *s, struct channel *res)
4601{
4602 struct http_txn *txn = s->txn;
4603 struct htx *htx;
4604 int32_t pos;
4605 int i;
4606
4607 if (txn->status < 200) {
4608 /* do not try to cache interim responses! */
4609 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4610 return;
4611 }
4612
4613 htx = htx_from_buf(&res->buf);
4614 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4615 struct htx_blk *blk = htx_get_blk(htx, pos);
4616 enum htx_blk_type type = htx_get_blk_type(blk);
4617 struct ist n, v;
4618
4619 if (type == HTX_BLK_EOH)
4620 break;
4621 if (type != HTX_BLK_HDR)
4622 continue;
4623
4624 n = htx_get_blk_name(htx, blk);
4625 v = htx_get_blk_value(htx, blk);
4626
4627 if (isteqi(n, ist("Pragma"))) {
4628 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4629 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4630 return;
4631 }
4632 }
4633
4634 if (!isteqi(n, ist("Cache-control")))
4635 continue;
4636
4637 /* OK, right now we know we have a cache-control header */
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) */
4647 if (i < v.len && *(v.ptr + i) == '=') {
4648 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
4649 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
4650 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4651 continue;
4652 }
4653
4654 /* we have something of the form no-cache="set-cookie" */
4655 if ((v.len >= 21) &&
4656 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
4657 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
4658 txn->flags &= ~TX_CACHE_COOK;
4659 continue;
4660 }
4661
4662 /* OK, so we know that either p2 points to the end of string or to a comma */
4663 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
4664 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4665 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
4666 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4667 return;
4668 }
4669
4670 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4671 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4672 continue;
4673 }
4674 }
4675}
4676
Christopher Faulet64159df2018-10-24 21:15:35 +02004677/* send a server's name with an outgoing request over an established connection.
4678 * Note: this function is designed to be called once the request has been
4679 * scheduled for being forwarded. This is the reason why the number of forwarded
4680 * bytes have to be adjusted.
4681 */
4682int htx_send_name_header(struct stream *s, struct proxy *be, const char *srv_name)
4683{
4684 struct htx *htx;
4685 struct http_hdr_ctx ctx;
4686 struct ist hdr;
4687 uint32_t data;
4688
4689 hdr = ist2(be->server_id_hdr_name, be->server_id_hdr_len);
4690 htx = htx_from_buf(&s->req.buf);
4691 data = htx->data;
4692
4693 ctx.blk = NULL;
4694 while (http_find_header(htx, hdr, &ctx, 1))
4695 http_remove_header(htx, &ctx);
4696 http_add_header(htx, hdr, ist2(srv_name, strlen(srv_name)));
4697
4698 if (co_data(&s->req)) {
4699 if (data >= htx->data)
4700 c_rew(&s->req, data - htx->data);
4701 else
4702 c_adv(&s->req, htx->data - data);
4703 }
4704 return 0;
4705}
4706
Christopher Faulet377c5a52018-10-24 21:21:30 +02004707/*
4708 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4709 * for the current backend.
4710 *
4711 * It is assumed that the request is either a HEAD, GET, or POST and that the
4712 * uri_auth field is valid.
4713 *
4714 * Returns 1 if stats should be provided, otherwise 0.
4715 */
4716static int htx_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
4717{
4718 struct uri_auth *uri_auth = backend->uri_auth;
4719 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004720 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004721 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004722
4723 if (!uri_auth)
4724 return 0;
4725
4726 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4727 return 0;
4728
4729 htx = htx_from_buf(&s->req.buf);
4730 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004731 uri = htx_sl_req_uri(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004732
4733 /* check URI size */
4734 if (uri_auth->uri_len > uri.len)
4735 return 0;
4736
4737 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4738 return 0;
4739
4740 return 1;
4741}
4742
4743/* This function prepares an applet to handle the stats. It can deal with the
4744 * "100-continue" expectation, check that admin rules are met for POST requests,
4745 * and program a response message if something was unexpected. It cannot fail
4746 * and always relies on the stats applet to complete the job. It does not touch
4747 * analysers nor counters, which are left to the caller. It does not touch
4748 * s->target which is supposed to already point to the stats applet. The caller
4749 * is expected to have already assigned an appctx to the stream.
4750 */
4751static int htx_handle_stats(struct stream *s, struct channel *req)
4752{
4753 struct stats_admin_rule *stats_admin_rule;
4754 struct stream_interface *si = &s->si[1];
4755 struct session *sess = s->sess;
4756 struct http_txn *txn = s->txn;
4757 struct http_msg *msg = &txn->req;
4758 struct uri_auth *uri_auth = s->be->uri_auth;
4759 const char *h, *lookup, *end;
4760 struct appctx *appctx;
4761 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004762 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004763
4764 appctx = si_appctx(si);
4765 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4766 appctx->st1 = appctx->st2 = 0;
4767 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
4768 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4769 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4770 appctx->ctx.stats.flags |= STAT_CHUNKED;
4771
4772 htx = htx_from_buf(&req->buf);
4773 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004774 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4775 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004776
4777 for (h = lookup; h <= end - 3; h++) {
4778 if (memcmp(h, ";up", 3) == 0) {
4779 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4780 break;
4781 }
4782 }
4783
4784 if (uri_auth->refresh) {
4785 for (h = lookup; h <= end - 10; h++) {
4786 if (memcmp(h, ";norefresh", 10) == 0) {
4787 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4788 break;
4789 }
4790 }
4791 }
4792
4793 for (h = lookup; h <= end - 4; h++) {
4794 if (memcmp(h, ";csv", 4) == 0) {
4795 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
4796 break;
4797 }
4798 }
4799
4800 for (h = lookup; h <= end - 6; h++) {
4801 if (memcmp(h, ";typed", 6) == 0) {
4802 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
4803 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4804 break;
4805 }
4806 }
4807
4808 for (h = lookup; h <= end - 8; h++) {
4809 if (memcmp(h, ";st=", 4) == 0) {
4810 int i;
4811 h += 4;
4812 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4813 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4814 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4815 appctx->ctx.stats.st_code = i;
4816 break;
4817 }
4818 }
4819 break;
4820 }
4821 }
4822
4823 appctx->ctx.stats.scope_str = 0;
4824 appctx->ctx.stats.scope_len = 0;
4825 for (h = lookup; h <= end - 8; h++) {
4826 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4827 int itx = 0;
4828 const char *h2;
4829 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4830 const char *err;
4831
4832 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4833 h2 = h;
4834 appctx->ctx.stats.scope_str = h2 - s->txn->uri;
4835 while (h <= end) {
4836 if (*h == ';' || *h == '&' || *h == ' ')
4837 break;
4838 itx++;
4839 h++;
4840 }
4841
4842 if (itx > STAT_SCOPE_TXT_MAXLEN)
4843 itx = STAT_SCOPE_TXT_MAXLEN;
4844 appctx->ctx.stats.scope_len = itx;
4845
4846 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4847 memcpy(scope_txt, h2, itx);
4848 scope_txt[itx] = '\0';
4849 err = invalid_char(scope_txt);
4850 if (err) {
4851 /* bad char in search text => clear scope */
4852 appctx->ctx.stats.scope_str = 0;
4853 appctx->ctx.stats.scope_len = 0;
4854 }
4855 break;
4856 }
4857 }
4858
4859 /* now check whether we have some admin rules for this request */
4860 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4861 int ret = 1;
4862
4863 if (stats_admin_rule->cond) {
4864 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4865 ret = acl_pass(ret);
4866 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4867 ret = !ret;
4868 }
4869
4870 if (ret) {
4871 /* no rule, or the rule matches */
4872 appctx->ctx.stats.flags |= STAT_ADMIN;
4873 break;
4874 }
4875 }
4876
4877 /* Was the status page requested with a POST ? */
4878 if (unlikely(txn->meth == HTTP_METH_POST)) {
4879 if (appctx->ctx.stats.flags & STAT_ADMIN) {
4880 /* we'll need the request body, possibly after sending 100-continue */
4881 if (msg->msg_state < HTTP_MSG_DATA)
4882 req->analysers |= AN_REQ_HTTP_BODY;
4883 appctx->st0 = STAT_HTTP_POST;
4884 }
4885 else {
4886 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4887 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4888 appctx->st0 = STAT_HTTP_LAST;
4889 }
4890 }
4891 else {
4892 /* So it was another method (GET/HEAD) */
4893 appctx->st0 = STAT_HTTP_HEAD;
4894 }
4895
4896 s->task->nice = -32; /* small boost for HTTP statistics */
4897 return 1;
4898}
4899
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004900void htx_perform_server_redirect(struct stream *s, struct stream_interface *si)
4901{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004902 struct channel *req = &s->req;
4903 struct channel *res = &s->res;
4904 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004905 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004906 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004907 struct ist path, location;
4908 unsigned int flags;
4909 size_t data;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004910
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004911 /*
4912 * Create the location
4913 */
4914 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004915
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004916 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004917 /* special prefix "/" means don't change URL */
4918 srv = __objt_server(s->target);
4919 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4920 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4921 return;
4922 }
4923
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004924 /* 2: add the request Path */
4925 htx = htx_from_buf(&req->buf);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004926 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004927 path = http_get_path(htx_sl_req_uri(sl));
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004928 if (!path.ptr)
4929 return;
4930
4931 if (!chunk_memcat(&trash, path.ptr, path.len))
4932 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004933 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004934
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004935 /*
4936 * Create the 302 respone
4937 */
4938 htx = htx_from_buf(&res->buf);
4939 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4940 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4941 ist("HTTP/1.1"), ist("302"), ist("Found"));
4942 if (!sl)
4943 goto fail;
4944 sl->info.res.status = 302;
4945 s->txn->status = 302;
4946
4947 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4948 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4949 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4950 !htx_add_header(htx, ist("Location"), location))
4951 goto fail;
4952
4953 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4954 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004955
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004956 /*
4957 * Send the message
4958 */
4959 data = htx->data - co_data(res);
4960 b_set_data(&res->buf, b_size(&res->buf));
4961 c_adv(res, data);
4962 res->total += data;
4963
4964 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004965 si_shutr(si);
4966 si_shutw(si);
4967 si->err_type = SI_ET_NONE;
4968 si->state = SI_ST_CLO;
4969
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004970 channel_auto_read(req);
4971 channel_abort(req);
4972 channel_auto_close(req);
4973 channel_erase(req);
4974 channel_auto_read(res);
4975 channel_auto_close(res);
4976
4977 if (!(s->flags & SF_ERR_MASK))
4978 s->flags |= SF_ERR_LOCAL;
4979 if (!(s->flags & SF_FINST_MASK))
4980 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004981
4982 /* FIXME: we should increase a counter of redirects per server and per backend. */
4983 srv_inc_sess_ctr(srv);
4984 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004985 return;
4986
4987 fail:
4988 /* If an error occurred, remove the incomplete HTTP response from the
4989 * buffer */
4990 channel_truncate(res);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004991}
4992
Christopher Fauletf2824e62018-10-01 12:12:37 +02004993/* This function terminates the request because it was completly analyzed or
4994 * because an error was triggered during the body forwarding.
4995 */
4996static void htx_end_request(struct stream *s)
4997{
4998 struct channel *chn = &s->req;
4999 struct http_txn *txn = s->txn;
5000
5001 DPRINTF(stderr,"[%u] %s: stream=%p states=%s,%s req->analysers=0x%08x res->analysers=0x%08x\n",
5002 now_ms, __FUNCTION__, s,
5003 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
5004 s->req.analysers, s->res.analysers);
5005
Christopher Fauletb42a8b62018-11-19 21:59:00 +01005006 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
5007 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02005008 channel_abort(chn);
5009 channel_truncate(chn);
5010 goto end;
5011 }
5012
5013 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
5014 return;
5015
5016 if (txn->req.msg_state == HTTP_MSG_DONE) {
5017 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5018 /* The server has not finished to respond, so we
5019 * don't want to move in order not to upset it.
5020 */
5021 return;
5022 }
5023
5024 /* No need to read anymore, the request was completely parsed.
5025 * We can shut the read side unless we want to abort_on_close,
5026 * or we have a POST request. The issue with POST requests is
5027 * that some browsers still send a CRLF after the request, and
5028 * this CRLF must be read so that it does not remain in the kernel
5029 * buffers, otherwise a close could cause an RST on some systems
5030 * (eg: Linux).
5031 */
5032 if ((!(s->be->options & PR_O_ABRT_CLOSE) || (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
5033 txn->meth != HTTP_METH_POST)
5034 channel_dont_read(chn);
5035
5036 /* if the server closes the connection, we want to immediately react
5037 * and close the socket to save packets and syscalls.
5038 */
5039 s->si[1].flags |= SI_FL_NOHALF;
5040
5041 /* In any case we've finished parsing the request so we must
5042 * disable Nagle when sending data because 1) we're not going
5043 * to shut this side, and 2) the server is waiting for us to
5044 * send pending data.
5045 */
5046 chn->flags |= CF_NEVER_WAIT;
5047
5048 /* When we get here, it means that both the request and the
5049 * response have finished receiving. Depending on the connection
5050 * mode, we'll have to wait for the last bytes to leave in either
5051 * direction, and sometimes for a close to be effective.
5052 */
5053 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5054 /* Tunnel mode will not have any analyser so it needs to
5055 * poll for reads.
5056 */
5057 channel_auto_read(chn);
Christopher Faulet9768c262018-10-22 09:34:31 +02005058 if (b_data(&chn->buf))
5059 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02005060 txn->req.msg_state = HTTP_MSG_TUNNEL;
5061 }
5062 else {
5063 /* we're not expecting any new data to come for this
5064 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02005065 *
5066 * However, there is an exception if the response
5067 * length is undefined. In this case, we need to wait
5068 * the close from the server. The response will be
5069 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02005070 */
5071 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
5072 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02005073 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02005074
5075 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5076 channel_shutr_now(chn);
5077 channel_shutw_now(chn);
5078 }
5079 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02005080 goto check_channel_flags;
5081 }
5082
5083 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5084 http_msg_closing:
5085 /* nothing else to forward, just waiting for the output buffer
5086 * to be empty and for the shutw_now to take effect.
5087 */
5088 if (channel_is_empty(chn)) {
5089 txn->req.msg_state = HTTP_MSG_CLOSED;
5090 goto http_msg_closed;
5091 }
5092 else if (chn->flags & CF_SHUTW) {
5093 txn->req.err_state = txn->req.msg_state;
5094 txn->req.msg_state = HTTP_MSG_ERROR;
5095 goto end;
5096 }
5097 return;
5098 }
5099
5100 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5101 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02005102 /* if we don't know whether the server will close, we need to hard close */
5103 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
5104 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02005105 /* see above in MSG_DONE why we only do this in these states */
5106 if ((!(s->be->options & PR_O_ABRT_CLOSE) || (s->si[0].flags & SI_FL_CLEAN_ABRT)))
5107 channel_dont_read(chn);
5108 goto end;
5109 }
5110
5111 check_channel_flags:
5112 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
5113 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
5114 /* if we've just closed an output, let's switch */
5115 txn->req.msg_state = HTTP_MSG_CLOSING;
5116 goto http_msg_closing;
5117 }
5118
5119 end:
5120 chn->analysers &= AN_REQ_FLT_END;
5121 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
5122 chn->analysers |= AN_REQ_FLT_XFER_DATA;
5123 channel_auto_close(chn);
5124 channel_auto_read(chn);
5125}
5126
5127
5128/* This function terminates the response because it was completly analyzed or
5129 * because an error was triggered during the body forwarding.
5130 */
5131static void htx_end_response(struct stream *s)
5132{
5133 struct channel *chn = &s->res;
5134 struct http_txn *txn = s->txn;
5135
5136 DPRINTF(stderr,"[%u] %s: stream=%p states=%s,%s req->analysers=0x%08x res->analysers=0x%08x\n",
5137 now_ms, __FUNCTION__, s,
5138 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
5139 s->req.analysers, s->res.analysers);
5140
Christopher Fauletb42a8b62018-11-19 21:59:00 +01005141 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
5142 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02005143 channel_truncate(chn);
Christopher Faulet9768c262018-10-22 09:34:31 +02005144 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005145 goto end;
5146 }
5147
5148 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
5149 return;
5150
5151 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5152 /* In theory, we don't need to read anymore, but we must
5153 * still monitor the server connection for a possible close
5154 * while the request is being uploaded, so we don't disable
5155 * reading.
5156 */
5157 /* channel_dont_read(chn); */
5158
5159 if (txn->req.msg_state < HTTP_MSG_DONE) {
5160 /* The client seems to still be sending data, probably
5161 * because we got an error response during an upload.
5162 * We have the choice of either breaking the connection
5163 * or letting it pass through. Let's do the later.
5164 */
5165 return;
5166 }
5167
5168 /* When we get here, it means that both the request and the
5169 * response have finished receiving. Depending on the connection
5170 * mode, we'll have to wait for the last bytes to leave in either
5171 * direction, and sometimes for a close to be effective.
5172 */
5173 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5174 channel_auto_read(chn);
5175 chn->flags |= CF_NEVER_WAIT;
Christopher Faulet9768c262018-10-22 09:34:31 +02005176 if (b_data(&chn->buf))
5177 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02005178 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
5179 }
5180 else {
5181 /* we're not expecting any new data to come for this
5182 * transaction, so we can close it.
5183 */
5184 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5185 channel_shutr_now(chn);
5186 channel_shutw_now(chn);
5187 }
5188 }
5189 goto check_channel_flags;
5190 }
5191
5192 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5193 http_msg_closing:
5194 /* nothing else to forward, just waiting for the output buffer
5195 * to be empty and for the shutw_now to take effect.
5196 */
5197 if (channel_is_empty(chn)) {
5198 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5199 goto http_msg_closed;
5200 }
5201 else if (chn->flags & CF_SHUTW) {
5202 txn->rsp.err_state = txn->rsp.msg_state;
5203 txn->rsp.msg_state = HTTP_MSG_ERROR;
5204 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
5205 if (objt_server(s->target))
5206 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
5207 goto end;
5208 }
5209 return;
5210 }
5211
5212 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5213 http_msg_closed:
5214 /* drop any pending data */
5215 channel_truncate(chn);
Christopher Faulet9768c262018-10-22 09:34:31 +02005216 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005217 goto end;
5218 }
5219
5220 check_channel_flags:
5221 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
5222 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
5223 /* if we've just closed an output, let's switch */
5224 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5225 goto http_msg_closing;
5226 }
5227
5228 end:
5229 chn->analysers &= AN_RES_FLT_END;
5230 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
5231 chn->analysers |= AN_RES_FLT_XFER_DATA;
5232 channel_auto_close(chn);
5233 channel_auto_read(chn);
5234}
5235
Christopher Faulet0f226952018-10-22 09:29:56 +02005236void htx_server_error(struct stream *s, struct stream_interface *si, int err,
5237 int finst, const struct buffer *msg)
5238{
5239 channel_auto_read(si_oc(si));
5240 channel_abort(si_oc(si));
5241 channel_auto_close(si_oc(si));
5242 channel_erase(si_oc(si));
5243 channel_auto_close(si_ic(si));
5244 channel_auto_read(si_ic(si));
5245 if (msg) {
5246 struct channel *chn = si_ic(si);
5247 struct htx *htx;
5248
5249 htx = htx_from_buf(&chn->buf);
5250 htx_add_oob(htx, ist2(msg->area, msg->data));
5251 //FLT_STRM_CB(s, flt_htx_reply(s, s->txn->status, htx));
5252 b_set_data(&chn->buf, b_size(&chn->buf));
5253 c_adv(chn, htx->data);
5254 chn->total += htx->data;
5255 }
5256 if (!(s->flags & SF_ERR_MASK))
5257 s->flags |= err;
5258 if (!(s->flags & SF_FINST_MASK))
5259 s->flags |= finst;
5260}
5261
5262void htx_reply_and_close(struct stream *s, short status, struct buffer *msg)
5263{
5264 channel_auto_read(&s->req);
5265 channel_abort(&s->req);
5266 channel_auto_close(&s->req);
5267 channel_erase(&s->req);
5268 channel_truncate(&s->res);
5269
5270 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
5271 if (msg) {
5272 struct channel *chn = &s->res;
5273 struct htx *htx;
5274
5275 htx = htx_from_buf(&chn->buf);
5276 htx_add_oob(htx, ist2(msg->area, msg->data));
5277 //FLT_STRM_CB(s, flt_htx_reply(s, s->txn->status, htx));
5278 b_set_data(&chn->buf, b_size(&chn->buf));
5279 c_adv(chn, htx->data);
5280 chn->total += htx->data;
5281 }
5282
5283 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
5284 channel_auto_read(&s->res);
5285 channel_auto_close(&s->res);
5286 channel_shutr_now(&s->res);
5287}
5288
Christopher Faulet23a3c792018-11-28 10:01:23 +01005289/* Send a 100-Continue response to the client. It returns 0 on success and -1
5290 * on error. The response channel is updated accordingly.
5291 */
5292static int htx_reply_100_continue(struct stream *s)
5293{
5294 struct channel *res = &s->res;
5295 struct htx *htx = htx_from_buf(&res->buf);
5296 struct htx_sl *sl;
5297 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
5298 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
5299 size_t data;
5300
5301 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5302 ist("HTTP/1.1"), ist("100"), ist("Continue"));
5303 if (!sl)
5304 goto fail;
5305 sl->info.res.status = 100;
5306
5307 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
5308 goto fail;
5309
5310 data = htx->data - co_data(res);
5311 b_set_data(&res->buf, b_size(&res->buf));
5312 c_adv(res, data);
5313 res->total += data;
5314 return 0;
5315
5316 fail:
5317 /* If an error occurred, remove the incomplete HTTP response from the
5318 * buffer */
5319 channel_truncate(res);
5320 return -1;
5321}
5322
Christopher Faulet12c51e22018-11-28 15:59:42 +01005323
5324/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
5325 * ont whether we use a proxy or not. It returns 0 on success and -1 on
5326 * error. The response channel is updated accordingly.
5327 */
5328static int htx_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
5329{
5330 struct channel *res = &s->res;
5331 struct htx *htx = htx_from_buf(&res->buf);
5332 struct htx_sl *sl;
5333 struct ist code, body;
5334 int status;
5335 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5336 size_t data;
5337
5338 if (!(s->txn->flags & TX_USE_PX_CONN)) {
5339 status = 401;
5340 code = ist("401");
5341 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
5342 "You need a valid user and password to access this content.\n"
5343 "</body></html>\n");
5344 }
5345 else {
5346 status = 407;
5347 code = ist("407");
5348 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
5349 "You need a valid user and password to access this content.\n"
5350 "</body></html>\n");
5351 }
5352
5353 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5354 ist("HTTP/1.1"), code, ist("Unauthorized"));
5355 if (!sl)
5356 goto fail;
5357 sl->info.res.status = status;
5358 s->txn->status = status;
5359
5360 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
5361 goto fail;
5362
5363 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
5364 !htx_add_header(htx, ist("Connection"), ist("close")) ||
5365 !htx_add_header(htx, ist("Content-Type"), ist("text/html")) ||
5366 !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
5367 goto fail;
5368
5369 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_data(htx, body) || !htx_add_endof(htx, HTX_BLK_EOM))
5370 goto fail;
5371
5372 data = htx->data - co_data(res);
5373 b_set_data(&res->buf, b_size(&res->buf));
5374 c_adv(res, data);
5375 res->total += data;
5376
5377 channel_auto_read(&s->req);
5378 channel_abort(&s->req);
5379 channel_auto_close(&s->req);
5380 channel_erase(&s->req);
5381
5382 res->wex = tick_add_ifset(now_ms, res->wto);
5383 channel_auto_read(res);
5384 channel_auto_close(res);
5385 channel_shutr_now(res);
5386 return 0;
5387
5388 fail:
5389 /* If an error occurred, remove the incomplete HTTP response from the
5390 * buffer */
5391 channel_truncate(res);
5392 return -1;
5393}
5394
Christopher Faulet0f226952018-10-22 09:29:56 +02005395/*
5396 * Capture headers from message <htx> according to header list <cap_hdr>, and
5397 * fill the <cap> pointers appropriately.
5398 */
5399static void htx_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
5400{
5401 struct cap_hdr *h;
5402 int32_t pos;
5403
5404 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
5405 struct htx_blk *blk = htx_get_blk(htx, pos);
5406 enum htx_blk_type type = htx_get_blk_type(blk);
5407 struct ist n, v;
5408
5409 if (type == HTX_BLK_EOH)
5410 break;
5411 if (type != HTX_BLK_HDR)
5412 continue;
5413
5414 n = htx_get_blk_name(htx, blk);
5415
5416 for (h = cap_hdr; h; h = h->next) {
5417 if (h->namelen && (h->namelen == n.len) &&
5418 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
5419 if (cap[h->index] == NULL)
5420 cap[h->index] =
5421 pool_alloc(h->pool);
5422
5423 if (cap[h->index] == NULL) {
5424 ha_alert("HTTP capture : out of memory.\n");
5425 break;
5426 }
5427
5428 v = htx_get_blk_value(htx, blk);
5429 if (v.len > h->len)
5430 v.len = h->len;
5431
5432 memcpy(cap[h->index], v.ptr, v.len);
5433 cap[h->index][v.len]=0;
5434 }
5435 }
5436 }
5437}
5438
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005439/* Delete a value in a header between delimiters <from> and <next>. The header
5440 * itself is delimited by <start> and <end> pointers. The number of characters
5441 * displaced is returned, and the pointer to the first delimiter is updated if
5442 * required. The function tries as much as possible to respect the following
5443 * principles :
5444 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5445 * in which case <next> is simply removed
5446 * - set exactly one space character after the new first delimiter, unless there
5447 * are not enough characters in the block being moved to do so.
5448 * - remove unneeded spaces before the previous delimiter and after the new
5449 * one.
5450 *
5451 * It is the caller's responsibility to ensure that :
5452 * - <from> points to a valid delimiter or <start> ;
5453 * - <next> points to a valid delimiter or <end> ;
5454 * - there are non-space chars before <from>.
5455 */
5456static int htx_del_hdr_value(char *start, char *end, char **from, char *next)
5457{
5458 char *prev = *from;
5459
5460 if (prev == start) {
5461 /* We're removing the first value. eat the semicolon, if <next>
5462 * is lower than <end> */
5463 if (next < end)
5464 next++;
5465
5466 while (next < end && HTTP_IS_SPHT(*next))
5467 next++;
5468 }
5469 else {
5470 /* Remove useless spaces before the old delimiter. */
5471 while (HTTP_IS_SPHT(*(prev-1)))
5472 prev--;
5473 *from = prev;
5474
5475 /* copy the delimiter and if possible a space if we're
5476 * not at the end of the line.
5477 */
5478 if (next < end) {
5479 *prev++ = *next++;
5480 if (prev + 1 < next)
5481 *prev++ = ' ';
5482 while (next < end && HTTP_IS_SPHT(*next))
5483 next++;
5484 }
5485 }
5486 memmove(prev, next, end - next);
5487 return (prev - next);
5488}
5489
Christopher Faulet0f226952018-10-22 09:29:56 +02005490
5491/* Formats the start line of the request (without CRLF) and puts it in <str> and
5492 * return the written lenght. The line can be truncated if it exceeds <len>.
5493 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005494static size_t htx_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005495{
5496 struct ist dst = ist2(str, 0);
5497
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005498 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005499 goto end;
5500 if (dst.len + 1 > len)
5501 goto end;
5502 dst.ptr[dst.len++] = ' ';
5503
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005504 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005505 goto end;
5506 if (dst.len + 1 > len)
5507 goto end;
5508 dst.ptr[dst.len++] = ' ';
5509
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005510 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005511 end:
5512 return dst.len;
5513}
5514
Christopher Fauletf0523542018-10-24 11:06:58 +02005515/* Formats the start line of the response (without CRLF) and puts it in <str> and
5516 * return the written lenght. The line can be truncated if it exceeds <len>.
5517 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005518static size_t htx_fmt_res_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Fauletf0523542018-10-24 11:06:58 +02005519{
5520 struct ist dst = ist2(str, 0);
5521
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005522 if (istcat(&dst, htx_sl_res_vsn(sl), len) == -1)
Christopher Fauletf0523542018-10-24 11:06:58 +02005523 goto end;
5524 if (dst.len + 1 > len)
5525 goto end;
5526 dst.ptr[dst.len++] = ' ';
5527
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005528 if (istcat(&dst, htx_sl_res_code(sl), len) == -1)
Christopher Fauletf0523542018-10-24 11:06:58 +02005529 goto end;
5530 if (dst.len + 1 > len)
5531 goto end;
5532 dst.ptr[dst.len++] = ' ';
5533
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005534 istcat(&dst, htx_sl_res_reason(sl), len);
Christopher Fauletf0523542018-10-24 11:06:58 +02005535 end:
5536 return dst.len;
5537}
5538
5539
Christopher Faulet0f226952018-10-22 09:29:56 +02005540/*
5541 * Print a debug line with a start line.
5542 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005543static void htx_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005544{
5545 struct session *sess = strm_sess(s);
5546 int max;
5547
5548 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5549 dir,
5550 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5551 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5552
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005553 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005554 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005555 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005556 trash.area[trash.data++] = ' ';
5557
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005558 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005559 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005560 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005561 trash.area[trash.data++] = ' ';
5562
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005563 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005564 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005565 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005566 trash.area[trash.data++] = '\n';
5567
5568 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5569}
5570
5571/*
5572 * Print a debug line with a header.
5573 */
5574static void htx_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v)
5575{
5576 struct session *sess = strm_sess(s);
5577 int max;
5578
5579 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5580 dir,
5581 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5582 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5583
5584 max = n.len;
5585 UBOUND(max, trash.size - trash.data - 3);
5586 chunk_memcat(&trash, n.ptr, max);
5587 trash.area[trash.data++] = ':';
5588 trash.area[trash.data++] = ' ';
5589
5590 max = v.len;
5591 UBOUND(max, trash.size - trash.data - 1);
5592 chunk_memcat(&trash, v.ptr, max);
5593 trash.area[trash.data++] = '\n';
5594
5595 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5596}
5597
5598
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005599__attribute__((constructor))
5600static void __htx_protocol_init(void)
5601{
5602}
5603
5604
5605/*
5606 * Local variables:
5607 * c-indent-level: 8
5608 * c-basic-offset: 8
5609 * End:
5610 */