blob: 6e961f96e37f43b9d97abfebc434c1c08e8438f8 [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
Christopher Faulet99daf282018-11-28 22:58:13 +01002302 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002303 * 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 Faulet99daf282018-11-28 22:58:13 +01002307 struct channel *req = &s->req;
2308 struct channel *res = &s->res;
2309 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002310 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002311 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002312 struct ist status, reason, location;
2313 unsigned int flags;
2314 size_t data;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002315
2316 chunk = alloc_trash_chunk();
2317 if (!chunk)
Christopher Faulet99daf282018-11-28 22:58:13 +01002318 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002319
Christopher Faulet99daf282018-11-28 22:58:13 +01002320 /*
2321 * Create the location
2322 */
2323 htx = htx_from_buf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002324 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002325 case REDIRECT_TYPE_SCHEME: {
2326 struct http_hdr_ctx ctx;
2327 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002328
Christopher Faulet99daf282018-11-28 22:58:13 +01002329 host = ist("");
2330 ctx.blk = NULL;
2331 if (http_find_header(htx, ist("Host"), &ctx, 0))
2332 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002333
Christopher Faulet99daf282018-11-28 22:58:13 +01002334 sl = http_find_stline(htx);
2335 path = http_get_path(htx_sl_req_uri(sl));
2336 /* build message using path */
2337 if (path.ptr) {
2338 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2339 int qs = 0;
2340 while (qs < path.len) {
2341 if (*(path.ptr + qs) == '?') {
2342 path.len = qs;
2343 break;
2344 }
2345 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002346 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002347 }
2348 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002349 else
2350 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002351
Christopher Faulet99daf282018-11-28 22:58:13 +01002352 if (rule->rdr_str) { /* this is an old "redirect" rule */
2353 /* add scheme */
2354 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2355 goto fail;
2356 }
2357 else {
2358 /* add scheme with executing log format */
2359 chunk->data += build_logline(s, chunk->area + chunk->data,
2360 chunk->size - chunk->data,
2361 &rule->rdr_fmt);
2362 }
2363 /* add "://" + host + path */
2364 if (!chunk_memcat(chunk, "://", 3) ||
2365 !chunk_memcat(chunk, host.ptr, host.len) ||
2366 !chunk_memcat(chunk, path.ptr, path.len))
2367 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002368
Christopher Faulet99daf282018-11-28 22:58:13 +01002369 /* append a slash at the end of the location if needed and missing */
2370 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2371 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2372 if (chunk->data + 1 >= chunk->size)
2373 goto fail;
2374 chunk->area[chunk->data++] = '/';
2375 }
2376 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002377 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002378
Christopher Faulet99daf282018-11-28 22:58:13 +01002379 case REDIRECT_TYPE_PREFIX: {
2380 struct ist path;
2381
2382 sl = http_find_stline(htx);
2383 path = http_get_path(htx_sl_req_uri(sl));
2384 /* build message using path */
2385 if (path.ptr) {
2386 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2387 int qs = 0;
2388 while (qs < path.len) {
2389 if (*(path.ptr + qs) == '?') {
2390 path.len = qs;
2391 break;
2392 }
2393 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002394 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002395 }
2396 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002397 else
2398 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002399
Christopher Faulet99daf282018-11-28 22:58:13 +01002400 if (rule->rdr_str) { /* this is an old "redirect" rule */
2401 /* add prefix. Note that if prefix == "/", we don't want to
2402 * add anything, otherwise it makes it hard for the user to
2403 * configure a self-redirection.
2404 */
2405 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2406 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2407 goto fail;
2408 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002409 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002410 else {
2411 /* add prefix with executing log format */
2412 chunk->data += build_logline(s, chunk->area + chunk->data,
2413 chunk->size - chunk->data,
2414 &rule->rdr_fmt);
2415 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002416
Christopher Faulet99daf282018-11-28 22:58:13 +01002417 /* add path */
2418 if (!chunk_memcat(chunk, path.ptr, path.len))
2419 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002420
Christopher Faulet99daf282018-11-28 22:58:13 +01002421 /* append a slash at the end of the location if needed and missing */
2422 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2423 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2424 if (chunk->data + 1 >= chunk->size)
2425 goto fail;
2426 chunk->area[chunk->data++] = '/';
2427 }
2428 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002429 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002430 case REDIRECT_TYPE_LOCATION:
2431 default:
2432 if (rule->rdr_str) { /* this is an old "redirect" rule */
2433 /* add location */
2434 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2435 goto fail;
2436 }
2437 else {
2438 /* add location with executing log format */
2439 chunk->data += build_logline(s, chunk->area + chunk->data,
2440 chunk->size - chunk->data,
2441 &rule->rdr_fmt);
2442 }
2443 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002444 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002445 location = ist2(chunk->area, chunk->data);
2446
2447 /*
2448 * Create the 30x response
2449 */
2450 switch (rule->code) {
2451 case 308:
2452 status = ist("308");
2453 reason = ist("Permanent Redirect");
2454 break;
2455 case 307:
2456 status = ist("307");
2457 reason = ist("Temporary Redirect");
2458 break;
2459 case 303:
2460 status = ist("303");
2461 reason = ist("See Other");
2462 break;
2463 case 301:
2464 status = ist("301");
2465 reason = ist("Moved Permanently");
2466 break;
2467 case 302:
2468 default:
2469 status = ist("302");
2470 reason = ist("Found");
2471 break;
2472 }
2473
2474 htx = htx_from_buf(&res->buf);
2475 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2476 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2477 if (!sl)
2478 goto fail;
2479 sl->info.res.status = rule->code;
2480 s->txn->status = rule->code;
2481
2482 if (!htx_add_header(htx, ist("Connection"), ist("close")) ||
2483 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
2484 !htx_add_header(htx, ist("Location"), location))
2485 goto fail;
2486
2487 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2488 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2489 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002490 }
2491
2492 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002493 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2494 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002495 }
2496
Christopher Faulet99daf282018-11-28 22:58:13 +01002497 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2498 goto fail;
2499
Christopher Fauletf2824e62018-10-01 12:12:37 +02002500 /* let's log the request time */
2501 s->logs.tv_request = now;
2502
Christopher Faulet99daf282018-11-28 22:58:13 +01002503 data = htx->data - co_data(res);
2504 b_set_data(&res->buf, b_size(&res->buf));
2505 c_adv(res, data);
2506 res->total += data;
2507
2508 channel_auto_read(req);
2509 channel_abort(req);
2510 channel_auto_close(req);
2511 channel_erase(req);
2512
2513 res->wex = tick_add_ifset(now_ms, res->wto);
2514 channel_auto_read(res);
2515 channel_auto_close(res);
2516 channel_shutr_now(res);
2517
2518 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002519
2520 if (!(s->flags & SF_ERR_MASK))
2521 s->flags |= SF_ERR_LOCAL;
2522 if (!(s->flags & SF_FINST_MASK))
2523 s->flags |= SF_FINST_R;
2524
Christopher Faulet99daf282018-11-28 22:58:13 +01002525 free_trash_chunk(chunk);
2526 return 1;
2527
2528 fail:
2529 /* If an error occurred, remove the incomplete HTTP response from the
2530 * buffer */
2531 channel_truncate(res);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002532 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002533 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002534}
2535
Christopher Faulet72333522018-10-24 11:25:02 +02002536int htx_transform_header_str(struct stream* s, struct channel *chn, struct htx *htx,
2537 struct ist name, const char *str, struct my_regex *re, int action)
2538{
2539 struct http_hdr_ctx ctx;
2540 struct buffer *output = get_trash_chunk();
2541
2542 /* find full header is action is ACT_HTTP_REPLACE_HDR */
2543 ctx.blk = NULL;
2544 while (http_find_header(htx, name, &ctx, (action == ACT_HTTP_REPLACE_HDR))) {
2545 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2546 continue;
2547
2548 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2549 if (output->data == -1)
2550 return -1;
2551 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2552 return -1;
2553 }
2554 return 0;
2555}
2556
2557static int htx_transform_header(struct stream* s, struct channel *chn, struct htx *htx,
2558 const struct ist name, struct list *fmt, struct my_regex *re, int action)
2559{
2560 struct buffer *replace;
2561 int ret = -1;
2562
2563 replace = alloc_trash_chunk();
2564 if (!replace)
2565 goto leave;
2566
2567 replace->data = build_logline(s, replace->area, replace->size, fmt);
2568 if (replace->data >= replace->size - 1)
2569 goto leave;
2570
2571 ret = htx_transform_header_str(s, chn, htx, name, replace->area, re, action);
2572
2573 leave:
2574 free_trash_chunk(replace);
2575 return ret;
2576}
2577
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002578
2579/* Terminate a 103-Erly-hints response and send it to the client. It returns 0
2580 * on success and -1 on error. The response channel is updated accordingly.
2581 */
2582static int htx_reply_103_early_hints(struct channel *res)
2583{
2584 struct htx *htx = htx_from_buf(&res->buf);
2585 size_t data;
2586
2587 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM)) {
2588 /* If an error occurred during an Early-hint rule,
2589 * remove the incomplete HTTP 103 response from the
2590 * buffer */
2591 channel_truncate(res);
2592 return -1;
2593 }
2594
2595 data = htx->data - co_data(res);
2596 b_set_data(&res->buf, b_size(&res->buf));
2597 c_adv(res, data);
2598 res->total += data;
2599 return 0;
2600}
2601
Christopher Faulet6eb92892018-11-15 16:39:29 +01002602/*
2603 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
2604 * built according to <fmt> log line format.
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002605 * If <early_hints> is 0, it is starts a new response by adding the start
2606 * line. If an error occurred -1 is returned. On success 0 is returned. The
2607 * channel is not updated here. It must be done calling the function
2608 * htx_reply_103_early_hints().
Christopher Faulet6eb92892018-11-15 16:39:29 +01002609 */
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002610static 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 +01002611{
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002612 struct channel *res = &s->res;
2613 struct htx *htx = htx_from_buf(&res->buf);
2614 struct buffer *value = alloc_trash_chunk();
2615
Christopher Faulet6eb92892018-11-15 16:39:29 +01002616 if (!early_hints) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002617 struct htx_sl *sl;
2618 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
2619 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2620
2621 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
2622 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
2623 if (!sl)
Christopher Faulet6eb92892018-11-15 16:39:29 +01002624 goto fail;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002625 sl->info.res.status = 103;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002626 }
2627
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002628 value->data = build_logline(s, b_tail(value), b_room(value), fmt);
2629 if (!htx_add_header(htx, name, ist2(b_head(value), b_data(value))))
Christopher Faulet6eb92892018-11-15 16:39:29 +01002630 goto fail;
2631
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002632 free_trash_chunk(value);
2633 b_set_data(&res->buf, b_size(&res->buf));
2634 return 1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002635
2636 fail:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002637 /* If an error occurred during an Early-hint rule, remove the incomplete
2638 * HTTP 103 response from the buffer */
2639 channel_truncate(res);
2640 free_trash_chunk(value);
2641 return -1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002642}
2643
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002644/* This function executes one of the set-{method,path,query,uri} actions. It
2645 * takes the string from the variable 'replace' with length 'len', then modifies
2646 * the relevant part of the request line accordingly. Then it updates various
2647 * pointers to the next elements which were moved, and the total buffer length.
2648 * It finds the action to be performed in p[2], previously filled by function
2649 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2650 * error, though this can be revisited when this code is finally exploited.
2651 *
2652 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2653 * query string and 3 to replace uri.
2654 *
2655 * In query string case, the mark question '?' must be set at the start of the
2656 * string by the caller, event if the replacement query string is empty.
2657 */
2658int htx_req_replace_stline(int action, const char *replace, int len,
2659 struct proxy *px, struct stream *s)
2660{
2661 struct htx *htx = htx_from_buf(&s->req.buf);
2662
2663 switch (action) {
2664 case 0: // method
2665 if (!http_replace_req_meth(htx, ist2(replace, len)))
2666 return -1;
2667 break;
2668
2669 case 1: // path
2670 if (!http_replace_req_path(htx, ist2(replace, len)))
2671 return -1;
2672 break;
2673
2674 case 2: // query
2675 if (!http_replace_req_query(htx, ist2(replace, len)))
2676 return -1;
2677 break;
2678
2679 case 3: // uri
2680 if (!http_replace_req_uri(htx, ist2(replace, len)))
2681 return -1;
2682 break;
2683
2684 default:
2685 return -1;
2686 }
2687 return 0;
2688}
2689
2690/* This function replace the HTTP status code and the associated message. The
2691 * variable <status> contains the new status code. This function never fails.
2692 */
2693void htx_res_set_status(unsigned int status, const char *reason, struct stream *s)
2694{
2695 struct htx *htx = htx_from_buf(&s->res.buf);
2696 char *res;
2697
2698 chunk_reset(&trash);
2699 res = ultoa_o(status, trash.area, trash.size);
2700 trash.data = res - trash.area;
2701
2702 /* Do we have a custom reason format string? */
2703 if (reason == NULL)
2704 reason = http_get_reason(status);
2705
2706 if (!http_replace_res_status(htx, ist2(trash.area, trash.data)))
2707 http_replace_res_reason(htx, ist2(reason, strlen(reason)));
2708}
2709
Christopher Faulet3e964192018-10-24 11:39:23 +02002710/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2711 * transaction <txn>. Returns the verdict of the first rule that prevents
2712 * further processing of the request (auth, deny, ...), and defaults to
2713 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2714 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2715 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2716 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2717 * status.
2718 */
2719static enum rule_result htx_req_get_intercept_rule(struct proxy *px, struct list *rules,
2720 struct stream *s, int *deny_status)
2721{
2722 struct session *sess = strm_sess(s);
2723 struct http_txn *txn = s->txn;
2724 struct htx *htx;
2725 struct connection *cli_conn;
2726 struct act_rule *rule;
2727 struct http_hdr_ctx ctx;
2728 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002729 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2730 int act_flags = 0;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002731 int early_hints = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002732
2733 htx = htx_from_buf(&s->req.buf);
2734
2735 /* If "the current_rule_list" match the executed rule list, we are in
2736 * resume condition. If a resume is needed it is always in the action
2737 * and never in the ACL or converters. In this case, we initialise the
2738 * current rule, and go to the action execution point.
2739 */
2740 if (s->current_rule) {
2741 rule = s->current_rule;
2742 s->current_rule = NULL;
2743 if (s->current_rule_list == rules)
2744 goto resume_execution;
2745 }
2746 s->current_rule_list = rules;
2747
2748 list_for_each_entry(rule, rules, list) {
2749 /* check optional condition */
2750 if (rule->cond) {
2751 int ret;
2752
2753 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2754 ret = acl_pass(ret);
2755
2756 if (rule->cond->pol == ACL_COND_UNLESS)
2757 ret = !ret;
2758
2759 if (!ret) /* condition not matched */
2760 continue;
2761 }
2762
2763 act_flags |= ACT_FLAG_FIRST;
2764 resume_execution:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002765 if (early_hints && rule->action != ACT_HTTP_EARLY_HINT) {
2766 early_hints = 0;
2767 if (htx_reply_103_early_hints(&s->res) == -1) {
2768 rule_ret = HTTP_RULE_RES_BADREQ;
2769 goto end;
2770 }
2771 }
2772
Christopher Faulet3e964192018-10-24 11:39:23 +02002773 switch (rule->action) {
2774 case ACT_ACTION_ALLOW:
2775 rule_ret = HTTP_RULE_RES_STOP;
2776 goto end;
2777
2778 case ACT_ACTION_DENY:
2779 if (deny_status)
2780 *deny_status = rule->deny_status;
2781 rule_ret = HTTP_RULE_RES_DENY;
2782 goto end;
2783
2784 case ACT_HTTP_REQ_TARPIT:
2785 txn->flags |= TX_CLTARPIT;
2786 if (deny_status)
2787 *deny_status = rule->deny_status;
2788 rule_ret = HTTP_RULE_RES_DENY;
2789 goto end;
2790
2791 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002792 /* Auth might be performed on regular http-req rules as well as on stats */
2793 auth_realm = rule->arg.auth.realm;
2794 if (!auth_realm) {
2795 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2796 auth_realm = STATS_DEFAULT_REALM;
2797 else
2798 auth_realm = px->id;
2799 }
2800 /* send 401/407 depending on whether we use a proxy or not. We still
2801 * count one error, because normal browsing won't significantly
2802 * increase the counter but brute force attempts will.
2803 */
Christopher Faulet3e964192018-10-24 11:39:23 +02002804 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Faulet12c51e22018-11-28 15:59:42 +01002805 if (htx_reply_40x_unauthorized(s, auth_realm) == -1)
2806 rule_ret = HTTP_RULE_RES_BADREQ;
2807 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02002808 goto end;
2809
2810 case ACT_HTTP_REDIR:
Christopher Faulet3e964192018-10-24 11:39:23 +02002811 rule_ret = HTTP_RULE_RES_DONE;
2812 if (!htx_apply_redirect_rule(rule->arg.redir, s, txn))
2813 rule_ret = HTTP_RULE_RES_BADREQ;
2814 goto end;
2815
2816 case ACT_HTTP_SET_NICE:
2817 s->task->nice = rule->arg.nice;
2818 break;
2819
2820 case ACT_HTTP_SET_TOS:
2821 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
2822 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
2823 break;
2824
2825 case ACT_HTTP_SET_MARK:
2826#ifdef SO_MARK
2827 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
2828 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
2829#endif
2830 break;
2831
2832 case ACT_HTTP_SET_LOGL:
2833 s->logs.level = rule->arg.loglevel;
2834 break;
2835
2836 case ACT_HTTP_REPLACE_HDR:
2837 case ACT_HTTP_REPLACE_VAL:
2838 if (htx_transform_header(s, &s->req, htx,
2839 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
2840 &rule->arg.hdr_add.fmt,
2841 &rule->arg.hdr_add.re, rule->action)) {
2842 rule_ret = HTTP_RULE_RES_BADREQ;
2843 goto end;
2844 }
2845 break;
2846
2847 case ACT_HTTP_DEL_HDR:
2848 /* remove all occurrences of the header */
2849 ctx.blk = NULL;
2850 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
2851 http_remove_header(htx, &ctx);
2852 break;
2853
2854 case ACT_HTTP_SET_HDR:
2855 case ACT_HTTP_ADD_HDR: {
2856 /* The scope of the trash buffer must be limited to this function. The
2857 * build_logline() function can execute a lot of other function which
2858 * can use the trash buffer. So for limiting the scope of this global
2859 * buffer, we build first the header value using build_logline, and
2860 * after we store the header name.
2861 */
2862 struct buffer *replace;
2863 struct ist n, v;
2864
2865 replace = alloc_trash_chunk();
2866 if (!replace) {
2867 rule_ret = HTTP_RULE_RES_BADREQ;
2868 goto end;
2869 }
2870
2871 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
2872 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2873 v = ist2(replace->area, replace->data);
2874
2875 if (rule->action == ACT_HTTP_SET_HDR) {
2876 /* remove all occurrences of the header */
2877 ctx.blk = NULL;
2878 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
2879 http_remove_header(htx, &ctx);
2880 }
2881
2882 if (!http_add_header(htx, n, v)) {
2883 static unsigned char rate_limit = 0;
2884
2885 if ((rate_limit++ & 255) == 0) {
2886 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);
2887 }
2888
2889 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
2890 if (sess->fe != s->be)
2891 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2892 if (sess->listener->counters)
2893 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
2894 }
2895 free_trash_chunk(replace);
2896 break;
2897 }
2898
2899 case ACT_HTTP_DEL_ACL:
2900 case ACT_HTTP_DEL_MAP: {
2901 struct pat_ref *ref;
2902 struct buffer *key;
2903
2904 /* collect reference */
2905 ref = pat_ref_lookup(rule->arg.map.ref);
2906 if (!ref)
2907 continue;
2908
2909 /* allocate key */
2910 key = alloc_trash_chunk();
2911 if (!key) {
2912 rule_ret = HTTP_RULE_RES_BADREQ;
2913 goto end;
2914 }
2915
2916 /* collect key */
2917 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2918 key->area[key->data] = '\0';
2919
2920 /* perform update */
2921 /* returned code: 1=ok, 0=ko */
2922 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
2923 pat_ref_delete(ref, key->area);
2924 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
2925
2926 free_trash_chunk(key);
2927 break;
2928 }
2929
2930 case ACT_HTTP_ADD_ACL: {
2931 struct pat_ref *ref;
2932 struct buffer *key;
2933
2934 /* collect reference */
2935 ref = pat_ref_lookup(rule->arg.map.ref);
2936 if (!ref)
2937 continue;
2938
2939 /* allocate key */
2940 key = alloc_trash_chunk();
2941 if (!key) {
2942 rule_ret = HTTP_RULE_RES_BADREQ;
2943 goto end;
2944 }
2945
2946 /* collect key */
2947 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2948 key->area[key->data] = '\0';
2949
2950 /* perform update */
2951 /* add entry only if it does not already exist */
2952 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
2953 if (pat_ref_find_elt(ref, key->area) == NULL)
2954 pat_ref_add(ref, key->area, NULL, NULL);
2955 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
2956
2957 free_trash_chunk(key);
2958 break;
2959 }
2960
2961 case ACT_HTTP_SET_MAP: {
2962 struct pat_ref *ref;
2963 struct buffer *key, *value;
2964
2965 /* collect reference */
2966 ref = pat_ref_lookup(rule->arg.map.ref);
2967 if (!ref)
2968 continue;
2969
2970 /* allocate key */
2971 key = alloc_trash_chunk();
2972 if (!key) {
2973 rule_ret = HTTP_RULE_RES_BADREQ;
2974 goto end;
2975 }
2976
2977 /* allocate value */
2978 value = alloc_trash_chunk();
2979 if (!value) {
2980 free_trash_chunk(key);
2981 rule_ret = HTTP_RULE_RES_BADREQ;
2982 goto end;
2983 }
2984
2985 /* collect key */
2986 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
2987 key->area[key->data] = '\0';
2988
2989 /* collect value */
2990 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
2991 value->area[value->data] = '\0';
2992
2993 /* perform update */
2994 if (pat_ref_find_elt(ref, key->area) != NULL)
2995 /* update entry if it exists */
2996 pat_ref_set(ref, key->area, value->area, NULL);
2997 else
2998 /* insert a new entry */
2999 pat_ref_add(ref, key->area, value->area, NULL);
3000
3001 free_trash_chunk(key);
3002 free_trash_chunk(value);
3003 break;
3004 }
3005
3006 case ACT_HTTP_EARLY_HINT:
3007 if (!(txn->req.flags & HTTP_MSGF_VER_11))
3008 break;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003009 early_hints = htx_add_early_hint_header(s, early_hints,
3010 ist2(rule->arg.early_hint.name, rule->arg.early_hint.name_len),
Christopher Faulet3e964192018-10-24 11:39:23 +02003011 &rule->arg.early_hint.fmt);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003012 if (early_hints == -1) {
3013 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02003014 goto end;
3015 }
3016 break;
3017
3018 case ACT_CUSTOM:
3019 if ((s->req.flags & CF_READ_ERROR) ||
3020 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3021 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
3022 (px->options & PR_O_ABRT_CLOSE)))
3023 act_flags |= ACT_FLAG_FINAL;
3024
3025 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
3026 case ACT_RET_ERR:
3027 case ACT_RET_CONT:
3028 break;
3029 case ACT_RET_STOP:
3030 rule_ret = HTTP_RULE_RES_DONE;
3031 goto end;
3032 case ACT_RET_YIELD:
3033 s->current_rule = rule;
3034 rule_ret = HTTP_RULE_RES_YIELD;
3035 goto end;
3036 }
3037 break;
3038
3039 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3040 /* Note: only the first valid tracking parameter of each
3041 * applies.
3042 */
3043
3044 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3045 struct stktable *t;
3046 struct stksess *ts;
3047 struct stktable_key *key;
3048 void *ptr1, *ptr2;
3049
3050 t = rule->arg.trk_ctr.table.t;
3051 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL,
3052 rule->arg.trk_ctr.expr, NULL);
3053
3054 if (key && (ts = stktable_get_entry(t, key))) {
3055 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3056
3057 /* let's count a new HTTP request as it's the first time we do it */
3058 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3059 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3060 if (ptr1 || ptr2) {
3061 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3062
3063 if (ptr1)
3064 stktable_data_cast(ptr1, http_req_cnt)++;
3065
3066 if (ptr2)
3067 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
3068 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3069
3070 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3071
3072 /* If data was modified, we need to touch to re-schedule sync */
3073 stktable_touch_local(t, ts, 0);
3074 }
3075
3076 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3077 if (sess->fe != s->be)
3078 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3079 }
3080 }
3081 break;
3082
3083 /* other flags exists, but normaly, they never be matched. */
3084 default:
3085 break;
3086 }
3087 }
3088
3089 end:
3090 if (early_hints) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003091 if (htx_reply_103_early_hints(&s->res) == -1)
3092 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02003093 }
3094
3095 /* we reached the end of the rules, nothing to report */
3096 return rule_ret;
3097}
3098
3099/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3100 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3101 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3102 * is returned, the process can continue the evaluation of next rule list. If
3103 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3104 * is returned, it means the operation could not be processed and a server error
3105 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3106 * deny rule. If *YIELD is returned, the caller must call again the function
3107 * with the same context.
3108 */
3109static enum rule_result htx_res_get_intercept_rule(struct proxy *px, struct list *rules,
3110 struct stream *s)
3111{
3112 struct session *sess = strm_sess(s);
3113 struct http_txn *txn = s->txn;
3114 struct htx *htx;
3115 struct connection *cli_conn;
3116 struct act_rule *rule;
3117 struct http_hdr_ctx ctx;
3118 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
3119 int act_flags = 0;
3120
3121 htx = htx_from_buf(&s->res.buf);
3122
3123 /* If "the current_rule_list" match the executed rule list, we are in
3124 * resume condition. If a resume is needed it is always in the action
3125 * and never in the ACL or converters. In this case, we initialise the
3126 * current rule, and go to the action execution point.
3127 */
3128 if (s->current_rule) {
3129 rule = s->current_rule;
3130 s->current_rule = NULL;
3131 if (s->current_rule_list == rules)
3132 goto resume_execution;
3133 }
3134 s->current_rule_list = rules;
3135
3136 list_for_each_entry(rule, rules, list) {
3137 /* check optional condition */
3138 if (rule->cond) {
3139 int ret;
3140
3141 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3142 ret = acl_pass(ret);
3143
3144 if (rule->cond->pol == ACL_COND_UNLESS)
3145 ret = !ret;
3146
3147 if (!ret) /* condition not matched */
3148 continue;
3149 }
3150
3151 act_flags |= ACT_FLAG_FIRST;
3152resume_execution:
3153 switch (rule->action) {
3154 case ACT_ACTION_ALLOW:
3155 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3156 goto end;
3157
3158 case ACT_ACTION_DENY:
3159 txn->flags |= TX_SVDENY;
3160 rule_ret = HTTP_RULE_RES_STOP;
3161 goto end;
3162
3163 case ACT_HTTP_SET_NICE:
3164 s->task->nice = rule->arg.nice;
3165 break;
3166
3167 case ACT_HTTP_SET_TOS:
3168 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
3169 inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, rule->arg.tos);
3170 break;
3171
3172 case ACT_HTTP_SET_MARK:
3173#ifdef SO_MARK
3174 if ((cli_conn = objt_conn(sess->origin)) && conn_ctrl_ready(cli_conn))
3175 setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
3176#endif
3177 break;
3178
3179 case ACT_HTTP_SET_LOGL:
3180 s->logs.level = rule->arg.loglevel;
3181 break;
3182
3183 case ACT_HTTP_REPLACE_HDR:
3184 case ACT_HTTP_REPLACE_VAL:
3185 if (htx_transform_header(s, &s->res, htx,
3186 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
3187 &rule->arg.hdr_add.fmt,
3188 &rule->arg.hdr_add.re, rule->action)) {
3189 rule_ret = HTTP_RULE_RES_BADREQ;
3190 goto end;
3191 }
3192 break;
3193
3194 case ACT_HTTP_DEL_HDR:
3195 /* remove all occurrences of the header */
3196 ctx.blk = NULL;
3197 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3198 http_remove_header(htx, &ctx);
3199 break;
3200
3201 case ACT_HTTP_SET_HDR:
3202 case ACT_HTTP_ADD_HDR: {
3203 struct buffer *replace;
3204 struct ist n, v;
3205
3206 replace = alloc_trash_chunk();
3207 if (!replace) {
3208 rule_ret = HTTP_RULE_RES_BADREQ;
3209 goto end;
3210 }
3211
3212 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
3213 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3214 v = ist2(replace->area, replace->data);
3215
3216 if (rule->action == ACT_HTTP_SET_HDR) {
3217 /* remove all occurrences of the header */
3218 ctx.blk = NULL;
3219 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3220 http_remove_header(htx, &ctx);
3221 }
3222
3223 if (!http_add_header(htx, n, v)) {
3224 static unsigned char rate_limit = 0;
3225
3226 if ((rate_limit++ & 255) == 0) {
3227 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);
3228 }
3229
3230 HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
3231 if (sess->fe != s->be)
3232 HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
3233 if (sess->listener->counters)
3234 HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
3235 if (objt_server(s->target))
3236 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
3237 }
3238 free_trash_chunk(replace);
3239 break;
3240 }
3241
3242 case ACT_HTTP_DEL_ACL:
3243 case ACT_HTTP_DEL_MAP: {
3244 struct pat_ref *ref;
3245 struct buffer *key;
3246
3247 /* collect reference */
3248 ref = pat_ref_lookup(rule->arg.map.ref);
3249 if (!ref)
3250 continue;
3251
3252 /* allocate key */
3253 key = alloc_trash_chunk();
3254 if (!key) {
3255 rule_ret = HTTP_RULE_RES_BADREQ;
3256 goto end;
3257 }
3258
3259 /* collect key */
3260 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3261 key->area[key->data] = '\0';
3262
3263 /* perform update */
3264 /* returned code: 1=ok, 0=ko */
3265 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3266 pat_ref_delete(ref, key->area);
3267 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3268
3269 free_trash_chunk(key);
3270 break;
3271 }
3272
3273 case ACT_HTTP_ADD_ACL: {
3274 struct pat_ref *ref;
3275 struct buffer *key;
3276
3277 /* collect reference */
3278 ref = pat_ref_lookup(rule->arg.map.ref);
3279 if (!ref)
3280 continue;
3281
3282 /* allocate key */
3283 key = alloc_trash_chunk();
3284 if (!key) {
3285 rule_ret = HTTP_RULE_RES_BADREQ;
3286 goto end;
3287 }
3288
3289 /* collect key */
3290 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3291 key->area[key->data] = '\0';
3292
3293 /* perform update */
3294 /* check if the entry already exists */
3295 if (pat_ref_find_elt(ref, key->area) == NULL)
3296 pat_ref_add(ref, key->area, NULL, NULL);
3297
3298 free_trash_chunk(key);
3299 break;
3300 }
3301
3302 case ACT_HTTP_SET_MAP: {
3303 struct pat_ref *ref;
3304 struct buffer *key, *value;
3305
3306 /* collect reference */
3307 ref = pat_ref_lookup(rule->arg.map.ref);
3308 if (!ref)
3309 continue;
3310
3311 /* allocate key */
3312 key = alloc_trash_chunk();
3313 if (!key) {
3314 rule_ret = HTTP_RULE_RES_BADREQ;
3315 goto end;
3316 }
3317
3318 /* allocate value */
3319 value = alloc_trash_chunk();
3320 if (!value) {
3321 free_trash_chunk(key);
3322 rule_ret = HTTP_RULE_RES_BADREQ;
3323 goto end;
3324 }
3325
3326 /* collect key */
3327 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3328 key->area[key->data] = '\0';
3329
3330 /* collect value */
3331 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3332 value->area[value->data] = '\0';
3333
3334 /* perform update */
3335 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3336 if (pat_ref_find_elt(ref, key->area) != NULL)
3337 /* update entry if it exists */
3338 pat_ref_set(ref, key->area, value->area, NULL);
3339 else
3340 /* insert a new entry */
3341 pat_ref_add(ref, key->area, value->area, NULL);
3342 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3343 free_trash_chunk(key);
3344 free_trash_chunk(value);
3345 break;
3346 }
3347
3348 case ACT_HTTP_REDIR:
3349 rule_ret = HTTP_RULE_RES_DONE;
3350 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3351 rule_ret = HTTP_RULE_RES_BADREQ;
3352 goto end;
3353
3354 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3355 /* Note: only the first valid tracking parameter of each
3356 * applies.
3357 */
3358 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3359 struct stktable *t;
3360 struct stksess *ts;
3361 struct stktable_key *key;
3362 void *ptr;
3363
3364 t = rule->arg.trk_ctr.table.t;
3365 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL,
3366 rule->arg.trk_ctr.expr, NULL);
3367
3368 if (key && (ts = stktable_get_entry(t, key))) {
3369 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3370
3371 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3372
3373 /* let's count a new HTTP request as it's the first time we do it */
3374 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3375 if (ptr)
3376 stktable_data_cast(ptr, http_req_cnt)++;
3377
3378 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3379 if (ptr)
3380 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3381 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3382
3383 /* When the client triggers a 4xx from the server, it's most often due
3384 * to a missing object or permission. These events should be tracked
3385 * because if they happen often, it may indicate a brute force or a
3386 * vulnerability scan. Normally this is done when receiving the response
3387 * but here we're tracking after this ought to have been done so we have
3388 * to do it on purpose.
3389 */
3390 if ((unsigned)(txn->status - 400) < 100) {
3391 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3392 if (ptr)
3393 stktable_data_cast(ptr, http_err_cnt)++;
3394
3395 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3396 if (ptr)
3397 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3398 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3399 }
3400
3401 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3402
3403 /* If data was modified, we need to touch to re-schedule sync */
3404 stktable_touch_local(t, ts, 0);
3405
3406 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3407 if (sess->fe != s->be)
3408 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3409 }
3410 }
3411 break;
3412
3413 case ACT_CUSTOM:
3414 if ((s->req.flags & CF_READ_ERROR) ||
3415 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3416 !(s->si[0].flags & SI_FL_CLEAN_ABRT) &&
3417 (px->options & PR_O_ABRT_CLOSE)))
3418 act_flags |= ACT_FLAG_FINAL;
3419
3420 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
3421 case ACT_RET_ERR:
3422 case ACT_RET_CONT:
3423 break;
3424 case ACT_RET_STOP:
3425 rule_ret = HTTP_RULE_RES_STOP;
3426 goto end;
3427 case ACT_RET_YIELD:
3428 s->current_rule = rule;
3429 rule_ret = HTTP_RULE_RES_YIELD;
3430 goto end;
3431 }
3432 break;
3433
3434 /* other flags exists, but normaly, they never be matched. */
3435 default:
3436 break;
3437 }
3438 }
3439
3440 end:
3441 /* we reached the end of the rules, nothing to report */
3442 return rule_ret;
3443}
3444
Christopher Faulet33640082018-10-24 11:53:01 +02003445/* Iterate the same filter through all request headers.
3446 * Returns 1 if this filter can be stopped upon return, otherwise 0.
3447 * Since it can manage the switch to another backend, it updates the per-proxy
3448 * DENY stats.
3449 */
3450static int htx_apply_filter_to_req_headers(struct stream *s, struct channel *req, struct hdr_exp *exp)
3451{
3452 struct http_txn *txn = s->txn;
3453 struct htx *htx;
3454 struct buffer *hdr = get_trash_chunk();
3455 int32_t pos;
3456
3457 htx = htx_from_buf(&req->buf);
3458
3459 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3460 struct htx_blk *blk = htx_get_blk(htx, pos);
3461 enum htx_blk_type type;
3462 struct ist n, v;
3463
3464 next_hdr:
3465 type = htx_get_blk_type(blk);
3466 if (type == HTX_BLK_EOH)
3467 break;
3468 if (type != HTX_BLK_HDR)
3469 continue;
3470
3471 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
3472 return 1;
3473 else if (unlikely(txn->flags & TX_CLALLOW) &&
3474 (exp->action == ACT_ALLOW ||
3475 exp->action == ACT_DENY ||
3476 exp->action == ACT_TARPIT))
3477 return 0;
3478
3479 n = htx_get_blk_name(htx, blk);
3480 v = htx_get_blk_value(htx, blk);
3481
3482 chunk_memcat(hdr, n.ptr, n.len);
3483 hdr->area[hdr->data++] = ':';
3484 hdr->area[hdr->data++] = ' ';
3485 chunk_memcat(hdr, v.ptr, v.len);
3486
3487 /* Now we have one header in <hdr> */
3488
3489 if (regex_exec_match2(exp->preg, hdr->area, hdr->data, MAX_MATCH, pmatch, 0)) {
3490 struct http_hdr_ctx ctx;
3491 int len;
3492
3493 switch (exp->action) {
3494 case ACT_ALLOW:
3495 txn->flags |= TX_CLALLOW;
3496 goto end;
3497
3498 case ACT_DENY:
3499 txn->flags |= TX_CLDENY;
3500 goto end;
3501
3502 case ACT_TARPIT:
3503 txn->flags |= TX_CLTARPIT;
3504 goto end;
3505
3506 case ACT_REPLACE:
3507 len = exp_replace(trash.area, trash.size, hdr->area, exp->replace, pmatch);
3508 if (len < 0)
3509 return -1;
3510
3511 http_parse_header(ist2(trash.area, len), &n, &v);
3512 ctx.blk = blk;
3513 ctx.value = v;
3514 if (!http_replace_header(htx, &ctx, n, v))
3515 return -1;
3516 if (!ctx.blk)
3517 goto end;
3518 pos = htx_get_blk_pos(htx, blk);
3519 break;
3520
3521 case ACT_REMOVE:
3522 ctx.blk = blk;
3523 ctx.value = v;
3524 if (!http_remove_header(htx, &ctx))
3525 return -1;
3526 if (!ctx.blk)
3527 goto end;
3528 pos = htx_get_blk_pos(htx, blk);
3529 goto next_hdr;
3530
3531 }
3532 }
3533 }
3534 end:
3535 return 0;
3536}
3537
3538/* Apply the filter to the request line.
3539 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3540 * or -1 if a replacement resulted in an invalid request line.
3541 * Since it can manage the switch to another backend, it updates the per-proxy
3542 * DENY stats.
3543 */
3544static int htx_apply_filter_to_req_line(struct stream *s, struct channel *req, struct hdr_exp *exp)
3545{
3546 struct http_txn *txn = s->txn;
3547 struct htx *htx;
3548 struct buffer *reqline = get_trash_chunk();
3549 int done;
3550
3551 htx = htx_from_buf(&req->buf);
3552
3553 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
3554 return 1;
3555 else if (unlikely(txn->flags & TX_CLALLOW) &&
3556 (exp->action == ACT_ALLOW ||
3557 exp->action == ACT_DENY ||
3558 exp->action == ACT_TARPIT))
3559 return 0;
3560 else if (exp->action == ACT_REMOVE)
3561 return 0;
3562
3563 done = 0;
3564
3565 reqline->data = htx_fmt_req_line(http_find_stline(htx), reqline->area, reqline->size);
3566
3567 /* Now we have the request line between cur_ptr and cur_end */
3568 if (regex_exec_match2(exp->preg, reqline->area, reqline->data, MAX_MATCH, pmatch, 0)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003569 struct htx_sl *sl = http_find_stline(htx);
3570 struct ist meth, uri, vsn;
Christopher Faulet33640082018-10-24 11:53:01 +02003571 int len;
3572
3573 switch (exp->action) {
3574 case ACT_ALLOW:
3575 txn->flags |= TX_CLALLOW;
3576 done = 1;
3577 break;
3578
3579 case ACT_DENY:
3580 txn->flags |= TX_CLDENY;
3581 done = 1;
3582 break;
3583
3584 case ACT_TARPIT:
3585 txn->flags |= TX_CLTARPIT;
3586 done = 1;
3587 break;
3588
3589 case ACT_REPLACE:
3590 len = exp_replace(trash.area, trash.size, reqline->area, exp->replace, pmatch);
3591 if (len < 0)
3592 return -1;
3593
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003594 http_parse_stline(ist2(trash.area, len), &meth, &uri, &vsn);
3595 sl->info.req.meth = find_http_meth(meth.ptr, meth.len);
3596 if (!http_replace_stline(htx, meth, uri, vsn))
Christopher Faulet33640082018-10-24 11:53:01 +02003597 return -1;
3598 done = 1;
3599 break;
3600 }
3601 }
3602 return done;
3603}
3604
3605/*
3606 * Apply all the req filters of proxy <px> to all headers in buffer <req> of stream <s>.
3607 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
3608 * unparsable request. Since it can manage the switch to another backend, it
3609 * updates the per-proxy DENY stats.
3610 */
3611static int htx_apply_filters_to_request(struct stream *s, struct channel *req, struct proxy *px)
3612{
3613 struct session *sess = s->sess;
3614 struct http_txn *txn = s->txn;
3615 struct hdr_exp *exp;
3616
3617 for (exp = px->req_exp; exp; exp = exp->next) {
3618 int ret;
3619
3620 /*
3621 * The interleaving of transformations and verdicts
3622 * makes it difficult to decide to continue or stop
3623 * the evaluation.
3624 */
3625
3626 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
3627 break;
3628
3629 if ((txn->flags & TX_CLALLOW) &&
3630 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3631 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
3632 continue;
3633
3634 /* if this filter had a condition, evaluate it now and skip to
3635 * next filter if the condition does not match.
3636 */
3637 if (exp->cond) {
3638 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3639 ret = acl_pass(ret);
3640 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
3641 ret = !ret;
3642
3643 if (!ret)
3644 continue;
3645 }
3646
3647 /* Apply the filter to the request line. */
3648 ret = htx_apply_filter_to_req_line(s, req, exp);
3649 if (unlikely(ret < 0))
3650 return -1;
3651
3652 if (likely(ret == 0)) {
3653 /* The filter did not match the request, it can be
3654 * iterated through all headers.
3655 */
3656 if (unlikely(htx_apply_filter_to_req_headers(s, req, exp) < 0))
3657 return -1;
3658 }
3659 }
3660 return 0;
3661}
3662
3663/* Iterate the same filter through all response headers contained in <res>.
3664 * Returns 1 if this filter can be stopped upon return, otherwise 0.
3665 */
3666static int htx_apply_filter_to_resp_headers(struct stream *s, struct channel *res, struct hdr_exp *exp)
3667{
3668 struct http_txn *txn = s->txn;
3669 struct htx *htx;
3670 struct buffer *hdr = get_trash_chunk();
3671 int32_t pos;
3672
3673 htx = htx_from_buf(&res->buf);
3674
3675 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
3676 struct htx_blk *blk = htx_get_blk(htx, pos);
3677 enum htx_blk_type type;
3678 struct ist n, v;
3679
3680 next_hdr:
3681 type = htx_get_blk_type(blk);
3682 if (type == HTX_BLK_EOH)
3683 break;
3684 if (type != HTX_BLK_HDR)
3685 continue;
3686
3687 if (unlikely(txn->flags & TX_SVDENY))
3688 return 1;
3689 else if (unlikely(txn->flags & TX_SVALLOW) &&
3690 (exp->action == ACT_ALLOW ||
3691 exp->action == ACT_DENY))
3692 return 0;
3693
3694 n = htx_get_blk_name(htx, blk);
3695 v = htx_get_blk_value(htx, blk);
3696
3697 chunk_memcat(hdr, n.ptr, n.len);
3698 hdr->area[hdr->data++] = ':';
3699 hdr->area[hdr->data++] = ' ';
3700 chunk_memcat(hdr, v.ptr, v.len);
3701
3702 /* Now we have one header in <hdr> */
3703
3704 if (regex_exec_match2(exp->preg, hdr->area, hdr->data, MAX_MATCH, pmatch, 0)) {
3705 struct http_hdr_ctx ctx;
3706 int len;
3707
3708 switch (exp->action) {
3709 case ACT_ALLOW:
3710 txn->flags |= TX_SVALLOW;
3711 goto end;
3712 break;
3713
3714 case ACT_DENY:
3715 txn->flags |= TX_SVDENY;
3716 goto end;
3717 break;
3718
3719 case ACT_REPLACE:
3720 len = exp_replace(trash.area, trash.size, hdr->area, exp->replace, pmatch);
3721 if (len < 0)
3722 return -1;
3723
3724 http_parse_header(ist2(trash.area, len), &n, &v);
3725 ctx.blk = blk;
3726 ctx.value = v;
3727 if (!http_replace_header(htx, &ctx, n, v))
3728 return -1;
3729 if (!ctx.blk)
3730 goto end;
3731 pos = htx_get_blk_pos(htx, blk);
3732 break;
3733
3734 case ACT_REMOVE:
3735 ctx.blk = blk;
3736 ctx.value = v;
3737 if (!http_remove_header(htx, &ctx))
3738 return -1;
3739 if (!ctx.blk)
3740 goto end;
3741 pos = htx_get_blk_pos(htx, blk);
3742 goto next_hdr;
3743 }
3744 }
3745
3746 }
3747 end:
3748 return 0;
3749}
3750
3751/* Apply the filter to the status line in the response buffer <res>.
3752 * Returns 0 if nothing has been done, 1 if the filter has been applied,
3753 * or -1 if a replacement resulted in an invalid status line.
3754 */
3755static int htx_apply_filter_to_sts_line(struct stream *s, struct channel *res, struct hdr_exp *exp)
3756{
3757 struct http_txn *txn = s->txn;
3758 struct htx *htx;
3759 struct buffer *resline = get_trash_chunk();
3760 int done;
3761
3762 htx = htx_from_buf(&res->buf);
3763
3764 if (unlikely(txn->flags & TX_SVDENY))
3765 return 1;
3766 else if (unlikely(txn->flags & TX_SVALLOW) &&
3767 (exp->action == ACT_ALLOW ||
3768 exp->action == ACT_DENY))
3769 return 0;
3770 else if (exp->action == ACT_REMOVE)
3771 return 0;
3772
3773 done = 0;
3774 resline->data = htx_fmt_res_line(http_find_stline(htx), resline->area, resline->size);
3775
3776 /* Now we have the status line between cur_ptr and cur_end */
3777 if (regex_exec_match2(exp->preg, resline->area, resline->data, MAX_MATCH, pmatch, 0)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003778 struct htx_sl *sl = http_find_stline(htx);
3779 struct ist vsn, code, reason;
Christopher Faulet33640082018-10-24 11:53:01 +02003780 int len;
3781
3782 switch (exp->action) {
3783 case ACT_ALLOW:
3784 txn->flags |= TX_SVALLOW;
3785 done = 1;
3786 break;
3787
3788 case ACT_DENY:
3789 txn->flags |= TX_SVDENY;
3790 done = 1;
3791 break;
3792
3793 case ACT_REPLACE:
3794 len = exp_replace(trash.area, trash.size, resline->area, exp->replace, pmatch);
3795 if (len < 0)
3796 return -1;
3797
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003798 http_parse_stline(ist2(trash.area, len), &vsn, &code, &reason);
3799 sl->info.res.status = strl2ui(code.ptr, code.len);
3800 if (!http_replace_stline(htx, vsn, code, reason))
Christopher Faulet33640082018-10-24 11:53:01 +02003801 return -1;
3802
3803 done = 1;
3804 return 1;
3805 }
3806 }
3807 return done;
3808}
3809
3810/*
3811 * Apply all the resp filters of proxy <px> to all headers in buffer <res> of stream <s>.
3812 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
3813 * unparsable response.
3814 */
3815static int htx_apply_filters_to_response(struct stream *s, struct channel *res, struct proxy *px)
3816{
3817 struct session *sess = s->sess;
3818 struct http_txn *txn = s->txn;
3819 struct hdr_exp *exp;
3820
3821 for (exp = px->rsp_exp; exp; exp = exp->next) {
3822 int ret;
3823
3824 /*
3825 * The interleaving of transformations and verdicts
3826 * makes it difficult to decide to continue or stop
3827 * the evaluation.
3828 */
3829
3830 if (txn->flags & TX_SVDENY)
3831 break;
3832
3833 if ((txn->flags & TX_SVALLOW) &&
3834 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
3835 exp->action == ACT_PASS)) {
3836 exp = exp->next;
3837 continue;
3838 }
3839
3840 /* if this filter had a condition, evaluate it now and skip to
3841 * next filter if the condition does not match.
3842 */
3843 if (exp->cond) {
3844 ret = acl_exec_cond(exp->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3845 ret = acl_pass(ret);
3846 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
3847 ret = !ret;
3848 if (!ret)
3849 continue;
3850 }
3851
3852 /* Apply the filter to the status line. */
3853 ret = htx_apply_filter_to_sts_line(s, res, exp);
3854 if (unlikely(ret < 0))
3855 return -1;
3856
3857 if (likely(ret == 0)) {
3858 /* The filter did not match the response, it can be
3859 * iterated through all headers.
3860 */
3861 if (unlikely(htx_apply_filter_to_resp_headers(s, res, exp) < 0))
3862 return -1;
3863 }
3864 }
3865 return 0;
3866}
3867
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003868/*
3869 * Manage client-side cookie. It can impact performance by about 2% so it is
3870 * desirable to call it only when needed. This code is quite complex because
3871 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3872 * highly recommended not to touch this part without a good reason !
3873 */
3874static void htx_manage_client_side_cookies(struct stream *s, struct channel *req)
3875{
3876 struct session *sess = s->sess;
3877 struct http_txn *txn = s->txn;
3878 struct htx *htx;
3879 struct http_hdr_ctx ctx;
3880 char *hdr_beg, *hdr_end, *del_from;
3881 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3882 int preserve_hdr;
3883
3884 htx = htx_from_buf(&req->buf);
3885 ctx.blk = NULL;
3886 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
3887 del_from = NULL; /* nothing to be deleted */
3888 preserve_hdr = 0; /* assume we may kill the whole header */
3889
3890 /* Now look for cookies. Conforming to RFC2109, we have to support
3891 * attributes whose name begin with a '$', and associate them with
3892 * the right cookie, if we want to delete this cookie.
3893 * So there are 3 cases for each cookie read :
3894 * 1) it's a special attribute, beginning with a '$' : ignore it.
3895 * 2) it's a server id cookie that we *MAY* want to delete : save
3896 * some pointers on it (last semi-colon, beginning of cookie...)
3897 * 3) it's an application cookie : we *MAY* have to delete a previous
3898 * "special" cookie.
3899 * At the end of loop, if a "special" cookie remains, we may have to
3900 * remove it. If no application cookie persists in the header, we
3901 * *MUST* delete it.
3902 *
3903 * Note: RFC2965 is unclear about the processing of spaces around
3904 * the equal sign in the ATTR=VALUE form. A careful inspection of
3905 * the RFC explicitly allows spaces before it, and not within the
3906 * tokens (attrs or values). An inspection of RFC2109 allows that
3907 * too but section 10.1.3 lets one think that spaces may be allowed
3908 * after the equal sign too, resulting in some (rare) buggy
3909 * implementations trying to do that. So let's do what servers do.
3910 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3911 * allowed quoted strings in values, with any possible character
3912 * after a backslash, including control chars and delimitors, which
3913 * causes parsing to become ambiguous. Browsers also allow spaces
3914 * within values even without quotes.
3915 *
3916 * We have to keep multiple pointers in order to support cookie
3917 * removal at the beginning, middle or end of header without
3918 * corrupting the header. All of these headers are valid :
3919 *
3920 * hdr_beg hdr_end
3921 * | |
3922 * v |
3923 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3924 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3925 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3926 * | | | | | | |
3927 * | | | | | | |
3928 * | | | | | | +--> next
3929 * | | | | | +----> val_end
3930 * | | | | +-----------> val_beg
3931 * | | | +--------------> equal
3932 * | | +----------------> att_end
3933 * | +---------------------> att_beg
3934 * +--------------------------> prev
3935 *
3936 */
3937 hdr_beg = ctx.value.ptr;
3938 hdr_end = hdr_beg + ctx.value.len;
3939 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3940 /* Iterate through all cookies on this line */
3941
3942 /* find att_beg */
3943 att_beg = prev;
3944 if (prev > hdr_beg)
3945 att_beg++;
3946
3947 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3948 att_beg++;
3949
3950 /* find att_end : this is the first character after the last non
3951 * space before the equal. It may be equal to hdr_end.
3952 */
3953 equal = att_end = att_beg;
3954 while (equal < hdr_end) {
3955 if (*equal == '=' || *equal == ',' || *equal == ';')
3956 break;
3957 if (HTTP_IS_SPHT(*equal++))
3958 continue;
3959 att_end = equal;
3960 }
3961
3962 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3963 * is between <att_beg> and <equal>, both may be identical.
3964 */
3965 /* look for end of cookie if there is an equal sign */
3966 if (equal < hdr_end && *equal == '=') {
3967 /* look for the beginning of the value */
3968 val_beg = equal + 1;
3969 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3970 val_beg++;
3971
3972 /* find the end of the value, respecting quotes */
3973 next = http_find_cookie_value_end(val_beg, hdr_end);
3974
3975 /* make val_end point to the first white space or delimitor after the value */
3976 val_end = next;
3977 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3978 val_end--;
3979 }
3980 else
3981 val_beg = val_end = next = equal;
3982
3983 /* We have nothing to do with attributes beginning with
3984 * '$'. However, they will automatically be removed if a
3985 * header before them is removed, since they're supposed
3986 * to be linked together.
3987 */
3988 if (*att_beg == '$')
3989 continue;
3990
3991 /* Ignore cookies with no equal sign */
3992 if (equal == next) {
3993 /* This is not our cookie, so we must preserve it. But if we already
3994 * scheduled another cookie for removal, we cannot remove the
3995 * complete header, but we can remove the previous block itself.
3996 */
3997 preserve_hdr = 1;
3998 if (del_from != NULL) {
3999 int delta = htx_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
4000 val_end += delta;
4001 next += delta;
4002 hdr_end += delta;
4003 prev = del_from;
4004 del_from = NULL;
4005 }
4006 continue;
4007 }
4008
4009 /* if there are spaces around the equal sign, we need to
4010 * strip them otherwise we'll get trouble for cookie captures,
4011 * or even for rewrites. Since this happens extremely rarely,
4012 * it does not hurt performance.
4013 */
4014 if (unlikely(att_end != equal || val_beg > equal + 1)) {
4015 int stripped_before = 0;
4016 int stripped_after = 0;
4017
4018 if (att_end != equal) {
4019 memmove(att_end, equal, hdr_end - equal);
4020 stripped_before = (att_end - equal);
4021 equal += stripped_before;
4022 val_beg += stripped_before;
4023 }
4024
4025 if (val_beg > equal + 1) {
4026 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
4027 stripped_after = (equal + 1) - val_beg;
4028 val_beg += stripped_after;
4029 stripped_before += stripped_after;
4030 }
4031
4032 val_end += stripped_before;
4033 next += stripped_before;
4034 hdr_end += stripped_before;
4035 }
4036 /* now everything is as on the diagram above */
4037
4038 /* First, let's see if we want to capture this cookie. We check
4039 * that we don't already have a client side cookie, because we
4040 * can only capture one. Also as an optimisation, we ignore
4041 * cookies shorter than the declared name.
4042 */
4043 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
4044 (val_end - att_beg >= sess->fe->capture_namelen) &&
4045 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
4046 int log_len = val_end - att_beg;
4047
4048 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
4049 ha_alert("HTTP logging : out of memory.\n");
4050 } else {
4051 if (log_len > sess->fe->capture_len)
4052 log_len = sess->fe->capture_len;
4053 memcpy(txn->cli_cookie, att_beg, log_len);
4054 txn->cli_cookie[log_len] = 0;
4055 }
4056 }
4057
4058 /* Persistence cookies in passive, rewrite or insert mode have the
4059 * following form :
4060 *
4061 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
4062 *
4063 * For cookies in prefix mode, the form is :
4064 *
4065 * Cookie: NAME=SRV~VALUE
4066 */
4067 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
4068 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
4069 struct server *srv = s->be->srv;
4070 char *delim;
4071
4072 /* if we're in cookie prefix mode, we'll search the delimitor so that we
4073 * have the server ID between val_beg and delim, and the original cookie between
4074 * delim+1 and val_end. Otherwise, delim==val_end :
4075 *
4076 * hdr_beg
4077 * |
4078 * v
4079 * NAME=SRV; # in all but prefix modes
4080 * NAME=SRV~OPAQUE ; # in prefix mode
4081 * || || | |+-> next
4082 * || || | +--> val_end
4083 * || || +---------> delim
4084 * || |+------------> val_beg
4085 * || +-------------> att_end = equal
4086 * |+-----------------> att_beg
4087 * +------------------> prev
4088 *
4089 */
4090 if (s->be->ck_opts & PR_CK_PFX) {
4091 for (delim = val_beg; delim < val_end; delim++)
4092 if (*delim == COOKIE_DELIM)
4093 break;
4094 }
4095 else {
4096 char *vbar1;
4097 delim = val_end;
4098 /* Now check if the cookie contains a date field, which would
4099 * appear after a vertical bar ('|') just after the server name
4100 * and before the delimiter.
4101 */
4102 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
4103 if (vbar1) {
4104 /* OK, so left of the bar is the server's cookie and
4105 * right is the last seen date. It is a base64 encoded
4106 * 30-bit value representing the UNIX date since the
4107 * epoch in 4-second quantities.
4108 */
4109 int val;
4110 delim = vbar1++;
4111 if (val_end - vbar1 >= 5) {
4112 val = b64tos30(vbar1);
4113 if (val > 0)
4114 txn->cookie_last_date = val << 2;
4115 }
4116 /* look for a second vertical bar */
4117 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
4118 if (vbar1 && (val_end - vbar1 > 5)) {
4119 val = b64tos30(vbar1 + 1);
4120 if (val > 0)
4121 txn->cookie_first_date = val << 2;
4122 }
4123 }
4124 }
4125
4126 /* if the cookie has an expiration date and the proxy wants to check
4127 * it, then we do that now. We first check if the cookie is too old,
4128 * then only if it has expired. We detect strict overflow because the
4129 * time resolution here is not great (4 seconds). Cookies with dates
4130 * in the future are ignored if their offset is beyond one day. This
4131 * allows an admin to fix timezone issues without expiring everyone
4132 * and at the same time avoids keeping unwanted side effects for too
4133 * long.
4134 */
4135 if (txn->cookie_first_date && s->be->cookie_maxlife &&
4136 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
4137 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
4138 txn->flags &= ~TX_CK_MASK;
4139 txn->flags |= TX_CK_OLD;
4140 delim = val_beg; // let's pretend we have not found the cookie
4141 txn->cookie_first_date = 0;
4142 txn->cookie_last_date = 0;
4143 }
4144 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
4145 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
4146 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
4147 txn->flags &= ~TX_CK_MASK;
4148 txn->flags |= TX_CK_EXPIRED;
4149 delim = val_beg; // let's pretend we have not found the cookie
4150 txn->cookie_first_date = 0;
4151 txn->cookie_last_date = 0;
4152 }
4153
4154 /* Here, we'll look for the first running server which supports the cookie.
4155 * This allows to share a same cookie between several servers, for example
4156 * to dedicate backup servers to specific servers only.
4157 * However, to prevent clients from sticking to cookie-less backup server
4158 * when they have incidentely learned an empty cookie, we simply ignore
4159 * empty cookies and mark them as invalid.
4160 * The same behaviour is applied when persistence must be ignored.
4161 */
4162 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
4163 srv = NULL;
4164
4165 while (srv) {
4166 if (srv->cookie && (srv->cklen == delim - val_beg) &&
4167 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
4168 if ((srv->cur_state != SRV_ST_STOPPED) ||
4169 (s->be->options & PR_O_PERSIST) ||
4170 (s->flags & SF_FORCE_PRST)) {
4171 /* we found the server and we can use it */
4172 txn->flags &= ~TX_CK_MASK;
4173 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
4174 s->flags |= SF_DIRECT | SF_ASSIGNED;
4175 s->target = &srv->obj_type;
4176 break;
4177 } else {
4178 /* we found a server, but it's down,
4179 * mark it as such and go on in case
4180 * another one is available.
4181 */
4182 txn->flags &= ~TX_CK_MASK;
4183 txn->flags |= TX_CK_DOWN;
4184 }
4185 }
4186 srv = srv->next;
4187 }
4188
4189 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
4190 /* no server matched this cookie or we deliberately skipped it */
4191 txn->flags &= ~TX_CK_MASK;
4192 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
4193 txn->flags |= TX_CK_UNUSED;
4194 else
4195 txn->flags |= TX_CK_INVALID;
4196 }
4197
4198 /* depending on the cookie mode, we may have to either :
4199 * - delete the complete cookie if we're in insert+indirect mode, so that
4200 * the server never sees it ;
4201 * - remove the server id from the cookie value, and tag the cookie as an
4202 * application cookie so that it does not get accidentely removed later,
4203 * if we're in cookie prefix mode
4204 */
4205 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
4206 int delta; /* negative */
4207
4208 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
4209 delta = val_beg - (delim + 1);
4210 val_end += delta;
4211 next += delta;
4212 hdr_end += delta;
4213 del_from = NULL;
4214 preserve_hdr = 1; /* we want to keep this cookie */
4215 }
4216 else if (del_from == NULL &&
4217 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
4218 del_from = prev;
4219 }
4220 }
4221 else {
4222 /* This is not our cookie, so we must preserve it. But if we already
4223 * scheduled another cookie for removal, we cannot remove the
4224 * complete header, but we can remove the previous block itself.
4225 */
4226 preserve_hdr = 1;
4227
4228 if (del_from != NULL) {
4229 int delta = htx_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
4230 if (att_beg >= del_from)
4231 att_beg += delta;
4232 if (att_end >= del_from)
4233 att_end += delta;
4234 val_beg += delta;
4235 val_end += delta;
4236 next += delta;
4237 hdr_end += delta;
4238 prev = del_from;
4239 del_from = NULL;
4240 }
4241 }
4242
4243 /* continue with next cookie on this header line */
4244 att_beg = next;
4245 } /* for each cookie */
4246
4247
4248 /* There are no more cookies on this line.
4249 * We may still have one (or several) marked for deletion at the
4250 * end of the line. We must do this now in two ways :
4251 * - if some cookies must be preserved, we only delete from the
4252 * mark to the end of line ;
4253 * - if nothing needs to be preserved, simply delete the whole header
4254 */
4255 if (del_from) {
4256 hdr_end = (preserve_hdr ? del_from : hdr_beg);
4257 }
4258 if ((hdr_end - hdr_beg) != ctx.value.len) {
4259 if (hdr_beg != hdr_end) {
4260 htx_set_blk_value_len(ctx.blk, hdr_end - hdr_beg);
4261 htx->data -= (hdr_end - ctx.value.ptr);
4262 }
4263 else
4264 http_remove_header(htx, &ctx);
4265 }
4266 } /* for each "Cookie header */
4267}
4268
4269/*
4270 * Manage server-side cookies. It can impact performance by about 2% so it is
4271 * desirable to call it only when needed. This function is also used when we
4272 * just need to know if there is a cookie (eg: for check-cache).
4273 */
4274static void htx_manage_server_side_cookies(struct stream *s, struct channel *res)
4275{
4276 struct session *sess = s->sess;
4277 struct http_txn *txn = s->txn;
4278 struct htx *htx;
4279 struct http_hdr_ctx ctx;
4280 struct server *srv;
4281 char *hdr_beg, *hdr_end;
4282 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
4283 int is_cookie2;
4284
4285 htx = htx_from_buf(&res->buf);
4286
4287 ctx.blk = NULL;
4288 while (1) {
4289 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
4290 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
4291 break;
4292 is_cookie2 = 1;
4293 }
4294
4295 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
4296 * <prev> points to the colon.
4297 */
4298 txn->flags |= TX_SCK_PRESENT;
4299
4300 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
4301 * check-cache is enabled) and we are not interested in checking
4302 * them. Warning, the cookie capture is declared in the frontend.
4303 */
4304 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
4305 break;
4306
4307 /* OK so now we know we have to process this response cookie.
4308 * The format of the Set-Cookie header is slightly different
4309 * from the format of the Cookie header in that it does not
4310 * support the comma as a cookie delimiter (thus the header
4311 * cannot be folded) because the Expires attribute described in
4312 * the original Netscape's spec may contain an unquoted date
4313 * with a comma inside. We have to live with this because
4314 * many browsers don't support Max-Age and some browsers don't
4315 * support quoted strings. However the Set-Cookie2 header is
4316 * clean.
4317 *
4318 * We have to keep multiple pointers in order to support cookie
4319 * removal at the beginning, middle or end of header without
4320 * corrupting the header (in case of set-cookie2). A special
4321 * pointer, <scav> points to the beginning of the set-cookie-av
4322 * fields after the first semi-colon. The <next> pointer points
4323 * either to the end of line (set-cookie) or next unquoted comma
4324 * (set-cookie2). All of these headers are valid :
4325 *
4326 * hdr_beg hdr_end
4327 * | |
4328 * v |
4329 * NAME1 = VALUE 1 ; Secure; Path="/" |
4330 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
4331 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
4332 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
4333 * | | | | | | | |
4334 * | | | | | | | +-> next
4335 * | | | | | | +------------> scav
4336 * | | | | | +--------------> val_end
4337 * | | | | +--------------------> val_beg
4338 * | | | +----------------------> equal
4339 * | | +------------------------> att_end
4340 * | +----------------------------> att_beg
4341 * +------------------------------> prev
4342 * -------------------------------> hdr_beg
4343 */
4344 hdr_beg = ctx.value.ptr;
4345 hdr_end = hdr_beg + ctx.value.len;
4346 for (prev = hdr_beg; prev < hdr_end; prev = next) {
4347
4348 /* Iterate through all cookies on this line */
4349
4350 /* find att_beg */
4351 att_beg = prev;
4352 if (prev > hdr_beg)
4353 att_beg++;
4354
4355 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
4356 att_beg++;
4357
4358 /* find att_end : this is the first character after the last non
4359 * space before the equal. It may be equal to hdr_end.
4360 */
4361 equal = att_end = att_beg;
4362
4363 while (equal < hdr_end) {
4364 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
4365 break;
4366 if (HTTP_IS_SPHT(*equal++))
4367 continue;
4368 att_end = equal;
4369 }
4370
4371 /* here, <equal> points to '=', a delimitor or the end. <att_end>
4372 * is between <att_beg> and <equal>, both may be identical.
4373 */
4374
4375 /* look for end of cookie if there is an equal sign */
4376 if (equal < hdr_end && *equal == '=') {
4377 /* look for the beginning of the value */
4378 val_beg = equal + 1;
4379 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
4380 val_beg++;
4381
4382 /* find the end of the value, respecting quotes */
4383 next = http_find_cookie_value_end(val_beg, hdr_end);
4384
4385 /* make val_end point to the first white space or delimitor after the value */
4386 val_end = next;
4387 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
4388 val_end--;
4389 }
4390 else {
4391 /* <equal> points to next comma, semi-colon or EOL */
4392 val_beg = val_end = next = equal;
4393 }
4394
4395 if (next < hdr_end) {
4396 /* Set-Cookie2 supports multiple cookies, and <next> points to
4397 * a colon or semi-colon before the end. So skip all attr-value
4398 * pairs and look for the next comma. For Set-Cookie, since
4399 * commas are permitted in values, skip to the end.
4400 */
4401 if (is_cookie2)
4402 next = http_find_hdr_value_end(next, hdr_end);
4403 else
4404 next = hdr_end;
4405 }
4406
4407 /* Now everything is as on the diagram above */
4408
4409 /* Ignore cookies with no equal sign */
4410 if (equal == val_end)
4411 continue;
4412
4413 /* If there are spaces around the equal sign, we need to
4414 * strip them otherwise we'll get trouble for cookie captures,
4415 * or even for rewrites. Since this happens extremely rarely,
4416 * it does not hurt performance.
4417 */
4418 if (unlikely(att_end != equal || val_beg > equal + 1)) {
4419 int stripped_before = 0;
4420 int stripped_after = 0;
4421
4422 if (att_end != equal) {
4423 memmove(att_end, equal, hdr_end - equal);
4424 stripped_before = (att_end - equal);
4425 equal += stripped_before;
4426 val_beg += stripped_before;
4427 }
4428
4429 if (val_beg > equal + 1) {
4430 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
4431 stripped_after = (equal + 1) - val_beg;
4432 val_beg += stripped_after;
4433 stripped_before += stripped_after;
4434 }
4435
4436 val_end += stripped_before;
4437 next += stripped_before;
4438 hdr_end += stripped_before;
4439
4440 ctx.value.len = hdr_end - hdr_beg;
4441 htx_set_blk_value_len(ctx.blk, ctx.value.len);
4442 htx->data -= (hdr_end - ctx.value.ptr);
4443 }
4444
4445 /* First, let's see if we want to capture this cookie. We check
4446 * that we don't already have a server side cookie, because we
4447 * can only capture one. Also as an optimisation, we ignore
4448 * cookies shorter than the declared name.
4449 */
4450 if (sess->fe->capture_name != NULL &&
4451 txn->srv_cookie == NULL &&
4452 (val_end - att_beg >= sess->fe->capture_namelen) &&
4453 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
4454 int log_len = val_end - att_beg;
4455 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
4456 ha_alert("HTTP logging : out of memory.\n");
4457 }
4458 else {
4459 if (log_len > sess->fe->capture_len)
4460 log_len = sess->fe->capture_len;
4461 memcpy(txn->srv_cookie, att_beg, log_len);
4462 txn->srv_cookie[log_len] = 0;
4463 }
4464 }
4465
4466 srv = objt_server(s->target);
4467 /* now check if we need to process it for persistence */
4468 if (!(s->flags & SF_IGNORE_PRST) &&
4469 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
4470 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
4471 /* assume passive cookie by default */
4472 txn->flags &= ~TX_SCK_MASK;
4473 txn->flags |= TX_SCK_FOUND;
4474
4475 /* If the cookie is in insert mode on a known server, we'll delete
4476 * this occurrence because we'll insert another one later.
4477 * We'll delete it too if the "indirect" option is set and we're in
4478 * a direct access.
4479 */
4480 if (s->be->ck_opts & PR_CK_PSV) {
4481 /* The "preserve" flag was set, we don't want to touch the
4482 * server's cookie.
4483 */
4484 }
4485 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
4486 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
4487 /* this cookie must be deleted */
4488 if (prev == hdr_beg && next == hdr_end) {
4489 /* whole header */
4490 http_remove_header(htx, &ctx);
4491 /* note: while both invalid now, <next> and <hdr_end>
4492 * are still equal, so the for() will stop as expected.
4493 */
4494 } else {
4495 /* just remove the value */
4496 int delta = htx_del_hdr_value(hdr_beg, hdr_end, &prev, next);
4497 next = prev;
4498 hdr_end += delta;
4499 }
4500 txn->flags &= ~TX_SCK_MASK;
4501 txn->flags |= TX_SCK_DELETED;
4502 /* and go on with next cookie */
4503 }
4504 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
4505 /* replace bytes val_beg->val_end with the cookie name associated
4506 * with this server since we know it.
4507 */
4508 int sliding, delta;
4509
4510 ctx.value = ist2(val_beg, val_end - val_beg);
4511 ctx.lws_before = ctx.lws_after = 0;
4512 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
4513 delta = srv->cklen - (val_end - val_beg);
4514 sliding = (ctx.value.ptr - val_beg);
4515 hdr_beg += sliding;
4516 val_beg += sliding;
4517 next += sliding + delta;
4518 hdr_end += sliding + delta;
4519
4520 txn->flags &= ~TX_SCK_MASK;
4521 txn->flags |= TX_SCK_REPLACED;
4522 }
4523 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
4524 /* insert the cookie name associated with this server
4525 * before existing cookie, and insert a delimiter between them..
4526 */
4527 int sliding, delta;
4528 ctx.value = ist2(val_beg, 0);
4529 ctx.lws_before = ctx.lws_after = 0;
4530 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
4531 delta = srv->cklen + 1;
4532 sliding = (ctx.value.ptr - val_beg);
4533 hdr_beg += sliding;
4534 val_beg += sliding;
4535 next += sliding + delta;
4536 hdr_end += sliding + delta;
4537
4538 val_beg[srv->cklen] = COOKIE_DELIM;
4539 txn->flags &= ~TX_SCK_MASK;
4540 txn->flags |= TX_SCK_REPLACED;
4541 }
4542 }
4543 /* that's done for this cookie, check the next one on the same
4544 * line when next != hdr_end (only if is_cookie2).
4545 */
4546 }
4547 }
4548}
4549
Christopher Faulet25a02f62018-10-24 12:00:25 +02004550/*
4551 * Parses the Cache-Control and Pragma request header fields to determine if
4552 * the request may be served from the cache and/or if it is cacheable. Updates
4553 * s->txn->flags.
4554 */
4555void htx_check_request_for_cacheability(struct stream *s, struct channel *req)
4556{
4557 struct http_txn *txn = s->txn;
4558 struct htx *htx;
4559 int32_t pos;
4560 int pragma_found, cc_found, i;
4561
4562 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
4563 return; /* nothing more to do here */
4564
4565 htx = htx_from_buf(&req->buf);
4566 pragma_found = cc_found = 0;
4567 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4568 struct htx_blk *blk = htx_get_blk(htx, pos);
4569 enum htx_blk_type type = htx_get_blk_type(blk);
4570 struct ist n, v;
4571
4572 if (type == HTX_BLK_EOH)
4573 break;
4574 if (type != HTX_BLK_HDR)
4575 continue;
4576
4577 n = htx_get_blk_name(htx, blk);
4578 v = htx_get_blk_value(htx, blk);
4579
4580 if (isteqi(n, ist("Pragma"))) {
4581 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4582 pragma_found = 1;
4583 continue;
4584 }
4585 }
4586
4587 /* Don't use the cache and don't try to store if we found the
4588 * Authorization header */
4589 if (isteqi(n, ist("Authorization"))) {
4590 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4591 txn->flags |= TX_CACHE_IGNORE;
4592 continue;
4593 }
4594
4595 if (!isteqi(n, ist("Cache-control")))
4596 continue;
4597
4598 /* OK, right now we know we have a cache-control header */
4599 cc_found = 1;
4600 if (!v.len) /* no info */
4601 continue;
4602
4603 i = 0;
4604 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4605 !isspace((unsigned char)*(v.ptr+i)))
4606 i++;
4607
4608 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
4609 * values after max-age, max-stale nor min-fresh, we simply don't
4610 * use the cache when they're specified.
4611 */
4612 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
4613 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4614 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
4615 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
4616 txn->flags |= TX_CACHE_IGNORE;
4617 continue;
4618 }
4619
4620 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
4621 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4622 continue;
4623 }
4624 }
4625
4626 /* RFC7234#5.4:
4627 * When the Cache-Control header field is also present and
4628 * understood in a request, Pragma is ignored.
4629 * When the Cache-Control header field is not present in a
4630 * request, caches MUST consider the no-cache request
4631 * pragma-directive as having the same effect as if
4632 * "Cache-Control: no-cache" were present.
4633 */
4634 if (!cc_found && pragma_found)
4635 txn->flags |= TX_CACHE_IGNORE;
4636}
4637
4638/*
4639 * Check if response is cacheable or not. Updates s->txn->flags.
4640 */
4641void htx_check_response_for_cacheability(struct stream *s, struct channel *res)
4642{
4643 struct http_txn *txn = s->txn;
4644 struct htx *htx;
4645 int32_t pos;
4646 int i;
4647
4648 if (txn->status < 200) {
4649 /* do not try to cache interim responses! */
4650 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4651 return;
4652 }
4653
4654 htx = htx_from_buf(&res->buf);
4655 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4656 struct htx_blk *blk = htx_get_blk(htx, pos);
4657 enum htx_blk_type type = htx_get_blk_type(blk);
4658 struct ist n, v;
4659
4660 if (type == HTX_BLK_EOH)
4661 break;
4662 if (type != HTX_BLK_HDR)
4663 continue;
4664
4665 n = htx_get_blk_name(htx, blk);
4666 v = htx_get_blk_value(htx, blk);
4667
4668 if (isteqi(n, ist("Pragma"))) {
4669 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4670 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4671 return;
4672 }
4673 }
4674
4675 if (!isteqi(n, ist("Cache-control")))
4676 continue;
4677
4678 /* OK, right now we know we have a cache-control header */
4679 if (!v.len) /* no info */
4680 continue;
4681
4682 i = 0;
4683 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4684 !isspace((unsigned char)*(v.ptr+i)))
4685 i++;
4686
4687 /* we have a complete value between v.ptr and (v.ptr+i) */
4688 if (i < v.len && *(v.ptr + i) == '=') {
4689 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
4690 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
4691 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4692 continue;
4693 }
4694
4695 /* we have something of the form no-cache="set-cookie" */
4696 if ((v.len >= 21) &&
4697 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
4698 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
4699 txn->flags &= ~TX_CACHE_COOK;
4700 continue;
4701 }
4702
4703 /* OK, so we know that either p2 points to the end of string or to a comma */
4704 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
4705 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4706 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
4707 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4708 return;
4709 }
4710
4711 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4712 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4713 continue;
4714 }
4715 }
4716}
4717
Christopher Faulet64159df2018-10-24 21:15:35 +02004718/* send a server's name with an outgoing request over an established connection.
4719 * Note: this function is designed to be called once the request has been
4720 * scheduled for being forwarded. This is the reason why the number of forwarded
4721 * bytes have to be adjusted.
4722 */
4723int htx_send_name_header(struct stream *s, struct proxy *be, const char *srv_name)
4724{
4725 struct htx *htx;
4726 struct http_hdr_ctx ctx;
4727 struct ist hdr;
4728 uint32_t data;
4729
4730 hdr = ist2(be->server_id_hdr_name, be->server_id_hdr_len);
4731 htx = htx_from_buf(&s->req.buf);
4732 data = htx->data;
4733
4734 ctx.blk = NULL;
4735 while (http_find_header(htx, hdr, &ctx, 1))
4736 http_remove_header(htx, &ctx);
4737 http_add_header(htx, hdr, ist2(srv_name, strlen(srv_name)));
4738
4739 if (co_data(&s->req)) {
4740 if (data >= htx->data)
4741 c_rew(&s->req, data - htx->data);
4742 else
4743 c_adv(&s->req, htx->data - data);
4744 }
4745 return 0;
4746}
4747
Christopher Faulet377c5a52018-10-24 21:21:30 +02004748/*
4749 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4750 * for the current backend.
4751 *
4752 * It is assumed that the request is either a HEAD, GET, or POST and that the
4753 * uri_auth field is valid.
4754 *
4755 * Returns 1 if stats should be provided, otherwise 0.
4756 */
4757static int htx_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
4758{
4759 struct uri_auth *uri_auth = backend->uri_auth;
4760 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004761 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004762 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004763
4764 if (!uri_auth)
4765 return 0;
4766
4767 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4768 return 0;
4769
4770 htx = htx_from_buf(&s->req.buf);
4771 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004772 uri = htx_sl_req_uri(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004773
4774 /* check URI size */
4775 if (uri_auth->uri_len > uri.len)
4776 return 0;
4777
4778 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4779 return 0;
4780
4781 return 1;
4782}
4783
4784/* This function prepares an applet to handle the stats. It can deal with the
4785 * "100-continue" expectation, check that admin rules are met for POST requests,
4786 * and program a response message if something was unexpected. It cannot fail
4787 * and always relies on the stats applet to complete the job. It does not touch
4788 * analysers nor counters, which are left to the caller. It does not touch
4789 * s->target which is supposed to already point to the stats applet. The caller
4790 * is expected to have already assigned an appctx to the stream.
4791 */
4792static int htx_handle_stats(struct stream *s, struct channel *req)
4793{
4794 struct stats_admin_rule *stats_admin_rule;
4795 struct stream_interface *si = &s->si[1];
4796 struct session *sess = s->sess;
4797 struct http_txn *txn = s->txn;
4798 struct http_msg *msg = &txn->req;
4799 struct uri_auth *uri_auth = s->be->uri_auth;
4800 const char *h, *lookup, *end;
4801 struct appctx *appctx;
4802 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004803 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004804
4805 appctx = si_appctx(si);
4806 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4807 appctx->st1 = appctx->st2 = 0;
4808 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
4809 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4810 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4811 appctx->ctx.stats.flags |= STAT_CHUNKED;
4812
4813 htx = htx_from_buf(&req->buf);
4814 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004815 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4816 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004817
4818 for (h = lookup; h <= end - 3; h++) {
4819 if (memcmp(h, ";up", 3) == 0) {
4820 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4821 break;
4822 }
4823 }
4824
4825 if (uri_auth->refresh) {
4826 for (h = lookup; h <= end - 10; h++) {
4827 if (memcmp(h, ";norefresh", 10) == 0) {
4828 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4829 break;
4830 }
4831 }
4832 }
4833
4834 for (h = lookup; h <= end - 4; h++) {
4835 if (memcmp(h, ";csv", 4) == 0) {
4836 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
4837 break;
4838 }
4839 }
4840
4841 for (h = lookup; h <= end - 6; h++) {
4842 if (memcmp(h, ";typed", 6) == 0) {
4843 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
4844 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4845 break;
4846 }
4847 }
4848
4849 for (h = lookup; h <= end - 8; h++) {
4850 if (memcmp(h, ";st=", 4) == 0) {
4851 int i;
4852 h += 4;
4853 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4854 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4855 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4856 appctx->ctx.stats.st_code = i;
4857 break;
4858 }
4859 }
4860 break;
4861 }
4862 }
4863
4864 appctx->ctx.stats.scope_str = 0;
4865 appctx->ctx.stats.scope_len = 0;
4866 for (h = lookup; h <= end - 8; h++) {
4867 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4868 int itx = 0;
4869 const char *h2;
4870 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4871 const char *err;
4872
4873 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4874 h2 = h;
4875 appctx->ctx.stats.scope_str = h2 - s->txn->uri;
4876 while (h <= end) {
4877 if (*h == ';' || *h == '&' || *h == ' ')
4878 break;
4879 itx++;
4880 h++;
4881 }
4882
4883 if (itx > STAT_SCOPE_TXT_MAXLEN)
4884 itx = STAT_SCOPE_TXT_MAXLEN;
4885 appctx->ctx.stats.scope_len = itx;
4886
4887 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4888 memcpy(scope_txt, h2, itx);
4889 scope_txt[itx] = '\0';
4890 err = invalid_char(scope_txt);
4891 if (err) {
4892 /* bad char in search text => clear scope */
4893 appctx->ctx.stats.scope_str = 0;
4894 appctx->ctx.stats.scope_len = 0;
4895 }
4896 break;
4897 }
4898 }
4899
4900 /* now check whether we have some admin rules for this request */
4901 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4902 int ret = 1;
4903
4904 if (stats_admin_rule->cond) {
4905 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4906 ret = acl_pass(ret);
4907 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4908 ret = !ret;
4909 }
4910
4911 if (ret) {
4912 /* no rule, or the rule matches */
4913 appctx->ctx.stats.flags |= STAT_ADMIN;
4914 break;
4915 }
4916 }
4917
4918 /* Was the status page requested with a POST ? */
4919 if (unlikely(txn->meth == HTTP_METH_POST)) {
4920 if (appctx->ctx.stats.flags & STAT_ADMIN) {
4921 /* we'll need the request body, possibly after sending 100-continue */
4922 if (msg->msg_state < HTTP_MSG_DATA)
4923 req->analysers |= AN_REQ_HTTP_BODY;
4924 appctx->st0 = STAT_HTTP_POST;
4925 }
4926 else {
4927 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4928 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4929 appctx->st0 = STAT_HTTP_LAST;
4930 }
4931 }
4932 else {
4933 /* So it was another method (GET/HEAD) */
4934 appctx->st0 = STAT_HTTP_HEAD;
4935 }
4936
4937 s->task->nice = -32; /* small boost for HTTP statistics */
4938 return 1;
4939}
4940
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004941void htx_perform_server_redirect(struct stream *s, struct stream_interface *si)
4942{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004943 struct channel *req = &s->req;
4944 struct channel *res = &s->res;
4945 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004946 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004947 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004948 struct ist path, location;
4949 unsigned int flags;
4950 size_t data;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004951
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004952 /*
4953 * Create the location
4954 */
4955 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004956
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004957 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004958 /* special prefix "/" means don't change URL */
4959 srv = __objt_server(s->target);
4960 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4961 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4962 return;
4963 }
4964
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004965 /* 2: add the request Path */
4966 htx = htx_from_buf(&req->buf);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004967 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004968 path = http_get_path(htx_sl_req_uri(sl));
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004969 if (!path.ptr)
4970 return;
4971
4972 if (!chunk_memcat(&trash, path.ptr, path.len))
4973 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004974 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004975
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004976 /*
4977 * Create the 302 respone
4978 */
4979 htx = htx_from_buf(&res->buf);
4980 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4981 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4982 ist("HTTP/1.1"), ist("302"), ist("Found"));
4983 if (!sl)
4984 goto fail;
4985 sl->info.res.status = 302;
4986 s->txn->status = 302;
4987
4988 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4989 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4990 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4991 !htx_add_header(htx, ist("Location"), location))
4992 goto fail;
4993
4994 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4995 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004996
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004997 /*
4998 * Send the message
4999 */
5000 data = htx->data - co_data(res);
5001 b_set_data(&res->buf, b_size(&res->buf));
5002 c_adv(res, data);
5003 res->total += data;
5004
5005 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02005006 si_shutr(si);
5007 si_shutw(si);
5008 si->err_type = SI_ET_NONE;
5009 si->state = SI_ST_CLO;
5010
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01005011 channel_auto_read(req);
5012 channel_abort(req);
5013 channel_auto_close(req);
5014 channel_erase(req);
5015 channel_auto_read(res);
5016 channel_auto_close(res);
5017
5018 if (!(s->flags & SF_ERR_MASK))
5019 s->flags |= SF_ERR_LOCAL;
5020 if (!(s->flags & SF_FINST_MASK))
5021 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02005022
5023 /* FIXME: we should increase a counter of redirects per server and per backend. */
5024 srv_inc_sess_ctr(srv);
5025 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01005026 return;
5027
5028 fail:
5029 /* If an error occurred, remove the incomplete HTTP response from the
5030 * buffer */
5031 channel_truncate(res);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02005032}
5033
Christopher Fauletf2824e62018-10-01 12:12:37 +02005034/* This function terminates the request because it was completly analyzed or
5035 * because an error was triggered during the body forwarding.
5036 */
5037static void htx_end_request(struct stream *s)
5038{
5039 struct channel *chn = &s->req;
5040 struct http_txn *txn = s->txn;
5041
5042 DPRINTF(stderr,"[%u] %s: stream=%p states=%s,%s req->analysers=0x%08x res->analysers=0x%08x\n",
5043 now_ms, __FUNCTION__, s,
5044 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
5045 s->req.analysers, s->res.analysers);
5046
Christopher Fauletb42a8b62018-11-19 21:59:00 +01005047 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
5048 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02005049 channel_abort(chn);
5050 channel_truncate(chn);
5051 goto end;
5052 }
5053
5054 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE))
5055 return;
5056
5057 if (txn->req.msg_state == HTTP_MSG_DONE) {
5058 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5059 /* The server has not finished to respond, so we
5060 * don't want to move in order not to upset it.
5061 */
5062 return;
5063 }
5064
5065 /* No need to read anymore, the request was completely parsed.
5066 * We can shut the read side unless we want to abort_on_close,
5067 * or we have a POST request. The issue with POST requests is
5068 * that some browsers still send a CRLF after the request, and
5069 * this CRLF must be read so that it does not remain in the kernel
5070 * buffers, otherwise a close could cause an RST on some systems
5071 * (eg: Linux).
5072 */
5073 if ((!(s->be->options & PR_O_ABRT_CLOSE) || (s->si[0].flags & SI_FL_CLEAN_ABRT)) &&
5074 txn->meth != HTTP_METH_POST)
5075 channel_dont_read(chn);
5076
5077 /* if the server closes the connection, we want to immediately react
5078 * and close the socket to save packets and syscalls.
5079 */
5080 s->si[1].flags |= SI_FL_NOHALF;
5081
5082 /* In any case we've finished parsing the request so we must
5083 * disable Nagle when sending data because 1) we're not going
5084 * to shut this side, and 2) the server is waiting for us to
5085 * send pending data.
5086 */
5087 chn->flags |= CF_NEVER_WAIT;
5088
5089 /* When we get here, it means that both the request and the
5090 * response have finished receiving. Depending on the connection
5091 * mode, we'll have to wait for the last bytes to leave in either
5092 * direction, and sometimes for a close to be effective.
5093 */
5094 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5095 /* Tunnel mode will not have any analyser so it needs to
5096 * poll for reads.
5097 */
5098 channel_auto_read(chn);
Christopher Faulet9768c262018-10-22 09:34:31 +02005099 if (b_data(&chn->buf))
5100 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02005101 txn->req.msg_state = HTTP_MSG_TUNNEL;
5102 }
5103 else {
5104 /* we're not expecting any new data to come for this
5105 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02005106 *
5107 * However, there is an exception if the response
5108 * length is undefined. In this case, we need to wait
5109 * the close from the server. The response will be
5110 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02005111 */
5112 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
5113 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02005114 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02005115
5116 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5117 channel_shutr_now(chn);
5118 channel_shutw_now(chn);
5119 }
5120 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02005121 goto check_channel_flags;
5122 }
5123
5124 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5125 http_msg_closing:
5126 /* nothing else to forward, just waiting for the output buffer
5127 * to be empty and for the shutw_now to take effect.
5128 */
5129 if (channel_is_empty(chn)) {
5130 txn->req.msg_state = HTTP_MSG_CLOSED;
5131 goto http_msg_closed;
5132 }
5133 else if (chn->flags & CF_SHUTW) {
5134 txn->req.err_state = txn->req.msg_state;
5135 txn->req.msg_state = HTTP_MSG_ERROR;
5136 goto end;
5137 }
5138 return;
5139 }
5140
5141 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5142 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02005143 /* if we don't know whether the server will close, we need to hard close */
5144 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
5145 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02005146 /* see above in MSG_DONE why we only do this in these states */
5147 if ((!(s->be->options & PR_O_ABRT_CLOSE) || (s->si[0].flags & SI_FL_CLEAN_ABRT)))
5148 channel_dont_read(chn);
5149 goto end;
5150 }
5151
5152 check_channel_flags:
5153 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
5154 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
5155 /* if we've just closed an output, let's switch */
5156 txn->req.msg_state = HTTP_MSG_CLOSING;
5157 goto http_msg_closing;
5158 }
5159
5160 end:
5161 chn->analysers &= AN_REQ_FLT_END;
5162 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
5163 chn->analysers |= AN_REQ_FLT_XFER_DATA;
5164 channel_auto_close(chn);
5165 channel_auto_read(chn);
5166}
5167
5168
5169/* This function terminates the response because it was completly analyzed or
5170 * because an error was triggered during the body forwarding.
5171 */
5172static void htx_end_response(struct stream *s)
5173{
5174 struct channel *chn = &s->res;
5175 struct http_txn *txn = s->txn;
5176
5177 DPRINTF(stderr,"[%u] %s: stream=%p states=%s,%s req->analysers=0x%08x res->analysers=0x%08x\n",
5178 now_ms, __FUNCTION__, s,
5179 h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state),
5180 s->req.analysers, s->res.analysers);
5181
Christopher Fauletb42a8b62018-11-19 21:59:00 +01005182 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
5183 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02005184 channel_truncate(chn);
Christopher Faulet9768c262018-10-22 09:34:31 +02005185 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005186 goto end;
5187 }
5188
5189 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE))
5190 return;
5191
5192 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5193 /* In theory, we don't need to read anymore, but we must
5194 * still monitor the server connection for a possible close
5195 * while the request is being uploaded, so we don't disable
5196 * reading.
5197 */
5198 /* channel_dont_read(chn); */
5199
5200 if (txn->req.msg_state < HTTP_MSG_DONE) {
5201 /* The client seems to still be sending data, probably
5202 * because we got an error response during an upload.
5203 * We have the choice of either breaking the connection
5204 * or letting it pass through. Let's do the later.
5205 */
5206 return;
5207 }
5208
5209 /* When we get here, it means that both the request and the
5210 * response have finished receiving. Depending on the connection
5211 * mode, we'll have to wait for the last bytes to leave in either
5212 * direction, and sometimes for a close to be effective.
5213 */
5214 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5215 channel_auto_read(chn);
5216 chn->flags |= CF_NEVER_WAIT;
Christopher Faulet9768c262018-10-22 09:34:31 +02005217 if (b_data(&chn->buf))
5218 return;
Christopher Fauletf2824e62018-10-01 12:12:37 +02005219 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
5220 }
5221 else {
5222 /* we're not expecting any new data to come for this
5223 * transaction, so we can close it.
5224 */
5225 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5226 channel_shutr_now(chn);
5227 channel_shutw_now(chn);
5228 }
5229 }
5230 goto check_channel_flags;
5231 }
5232
5233 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5234 http_msg_closing:
5235 /* nothing else to forward, just waiting for the output buffer
5236 * to be empty and for the shutw_now to take effect.
5237 */
5238 if (channel_is_empty(chn)) {
5239 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5240 goto http_msg_closed;
5241 }
5242 else if (chn->flags & CF_SHUTW) {
5243 txn->rsp.err_state = txn->rsp.msg_state;
5244 txn->rsp.msg_state = HTTP_MSG_ERROR;
5245 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
5246 if (objt_server(s->target))
5247 HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
5248 goto end;
5249 }
5250 return;
5251 }
5252
5253 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5254 http_msg_closed:
5255 /* drop any pending data */
5256 channel_truncate(chn);
Christopher Faulet9768c262018-10-22 09:34:31 +02005257 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005258 goto end;
5259 }
5260
5261 check_channel_flags:
5262 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
5263 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
5264 /* if we've just closed an output, let's switch */
5265 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5266 goto http_msg_closing;
5267 }
5268
5269 end:
5270 chn->analysers &= AN_RES_FLT_END;
5271 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
5272 chn->analysers |= AN_RES_FLT_XFER_DATA;
5273 channel_auto_close(chn);
5274 channel_auto_read(chn);
5275}
5276
Christopher Faulet0f226952018-10-22 09:29:56 +02005277void htx_server_error(struct stream *s, struct stream_interface *si, int err,
5278 int finst, const struct buffer *msg)
5279{
5280 channel_auto_read(si_oc(si));
5281 channel_abort(si_oc(si));
5282 channel_auto_close(si_oc(si));
5283 channel_erase(si_oc(si));
5284 channel_auto_close(si_ic(si));
5285 channel_auto_read(si_ic(si));
5286 if (msg) {
5287 struct channel *chn = si_ic(si);
5288 struct htx *htx;
5289
5290 htx = htx_from_buf(&chn->buf);
5291 htx_add_oob(htx, ist2(msg->area, msg->data));
5292 //FLT_STRM_CB(s, flt_htx_reply(s, s->txn->status, htx));
5293 b_set_data(&chn->buf, b_size(&chn->buf));
5294 c_adv(chn, htx->data);
5295 chn->total += htx->data;
5296 }
5297 if (!(s->flags & SF_ERR_MASK))
5298 s->flags |= err;
5299 if (!(s->flags & SF_FINST_MASK))
5300 s->flags |= finst;
5301}
5302
5303void htx_reply_and_close(struct stream *s, short status, struct buffer *msg)
5304{
5305 channel_auto_read(&s->req);
5306 channel_abort(&s->req);
5307 channel_auto_close(&s->req);
5308 channel_erase(&s->req);
5309 channel_truncate(&s->res);
5310
5311 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
5312 if (msg) {
5313 struct channel *chn = &s->res;
5314 struct htx *htx;
5315
5316 htx = htx_from_buf(&chn->buf);
5317 htx_add_oob(htx, ist2(msg->area, msg->data));
5318 //FLT_STRM_CB(s, flt_htx_reply(s, s->txn->status, htx));
5319 b_set_data(&chn->buf, b_size(&chn->buf));
5320 c_adv(chn, htx->data);
5321 chn->total += htx->data;
5322 }
5323
5324 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
5325 channel_auto_read(&s->res);
5326 channel_auto_close(&s->res);
5327 channel_shutr_now(&s->res);
5328}
5329
Christopher Faulet23a3c792018-11-28 10:01:23 +01005330/* Send a 100-Continue response to the client. It returns 0 on success and -1
5331 * on error. The response channel is updated accordingly.
5332 */
5333static int htx_reply_100_continue(struct stream *s)
5334{
5335 struct channel *res = &s->res;
5336 struct htx *htx = htx_from_buf(&res->buf);
5337 struct htx_sl *sl;
5338 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
5339 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
5340 size_t data;
5341
5342 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5343 ist("HTTP/1.1"), ist("100"), ist("Continue"));
5344 if (!sl)
5345 goto fail;
5346 sl->info.res.status = 100;
5347
5348 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
5349 goto fail;
5350
5351 data = htx->data - co_data(res);
5352 b_set_data(&res->buf, b_size(&res->buf));
5353 c_adv(res, data);
5354 res->total += data;
5355 return 0;
5356
5357 fail:
5358 /* If an error occurred, remove the incomplete HTTP response from the
5359 * buffer */
5360 channel_truncate(res);
5361 return -1;
5362}
5363
Christopher Faulet12c51e22018-11-28 15:59:42 +01005364
5365/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
5366 * ont whether we use a proxy or not. It returns 0 on success and -1 on
5367 * error. The response channel is updated accordingly.
5368 */
5369static int htx_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
5370{
5371 struct channel *res = &s->res;
5372 struct htx *htx = htx_from_buf(&res->buf);
5373 struct htx_sl *sl;
5374 struct ist code, body;
5375 int status;
5376 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5377 size_t data;
5378
5379 if (!(s->txn->flags & TX_USE_PX_CONN)) {
5380 status = 401;
5381 code = ist("401");
5382 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
5383 "You need a valid user and password to access this content.\n"
5384 "</body></html>\n");
5385 }
5386 else {
5387 status = 407;
5388 code = ist("407");
5389 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
5390 "You need a valid user and password to access this content.\n"
5391 "</body></html>\n");
5392 }
5393
5394 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5395 ist("HTTP/1.1"), code, ist("Unauthorized"));
5396 if (!sl)
5397 goto fail;
5398 sl->info.res.status = status;
5399 s->txn->status = status;
5400
5401 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
5402 goto fail;
5403
5404 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
5405 !htx_add_header(htx, ist("Connection"), ist("close")) ||
5406 !htx_add_header(htx, ist("Content-Type"), ist("text/html")) ||
5407 !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
5408 goto fail;
5409
5410 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_data(htx, body) || !htx_add_endof(htx, HTX_BLK_EOM))
5411 goto fail;
5412
5413 data = htx->data - co_data(res);
5414 b_set_data(&res->buf, b_size(&res->buf));
5415 c_adv(res, data);
5416 res->total += data;
5417
5418 channel_auto_read(&s->req);
5419 channel_abort(&s->req);
5420 channel_auto_close(&s->req);
5421 channel_erase(&s->req);
5422
5423 res->wex = tick_add_ifset(now_ms, res->wto);
5424 channel_auto_read(res);
5425 channel_auto_close(res);
5426 channel_shutr_now(res);
5427 return 0;
5428
5429 fail:
5430 /* If an error occurred, remove the incomplete HTTP response from the
5431 * buffer */
5432 channel_truncate(res);
5433 return -1;
5434}
5435
Christopher Faulet0f226952018-10-22 09:29:56 +02005436/*
5437 * Capture headers from message <htx> according to header list <cap_hdr>, and
5438 * fill the <cap> pointers appropriately.
5439 */
5440static void htx_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
5441{
5442 struct cap_hdr *h;
5443 int32_t pos;
5444
5445 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
5446 struct htx_blk *blk = htx_get_blk(htx, pos);
5447 enum htx_blk_type type = htx_get_blk_type(blk);
5448 struct ist n, v;
5449
5450 if (type == HTX_BLK_EOH)
5451 break;
5452 if (type != HTX_BLK_HDR)
5453 continue;
5454
5455 n = htx_get_blk_name(htx, blk);
5456
5457 for (h = cap_hdr; h; h = h->next) {
5458 if (h->namelen && (h->namelen == n.len) &&
5459 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
5460 if (cap[h->index] == NULL)
5461 cap[h->index] =
5462 pool_alloc(h->pool);
5463
5464 if (cap[h->index] == NULL) {
5465 ha_alert("HTTP capture : out of memory.\n");
5466 break;
5467 }
5468
5469 v = htx_get_blk_value(htx, blk);
5470 if (v.len > h->len)
5471 v.len = h->len;
5472
5473 memcpy(cap[h->index], v.ptr, v.len);
5474 cap[h->index][v.len]=0;
5475 }
5476 }
5477 }
5478}
5479
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005480/* Delete a value in a header between delimiters <from> and <next>. The header
5481 * itself is delimited by <start> and <end> pointers. The number of characters
5482 * displaced is returned, and the pointer to the first delimiter is updated if
5483 * required. The function tries as much as possible to respect the following
5484 * principles :
5485 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5486 * in which case <next> is simply removed
5487 * - set exactly one space character after the new first delimiter, unless there
5488 * are not enough characters in the block being moved to do so.
5489 * - remove unneeded spaces before the previous delimiter and after the new
5490 * one.
5491 *
5492 * It is the caller's responsibility to ensure that :
5493 * - <from> points to a valid delimiter or <start> ;
5494 * - <next> points to a valid delimiter or <end> ;
5495 * - there are non-space chars before <from>.
5496 */
5497static int htx_del_hdr_value(char *start, char *end, char **from, char *next)
5498{
5499 char *prev = *from;
5500
5501 if (prev == start) {
5502 /* We're removing the first value. eat the semicolon, if <next>
5503 * is lower than <end> */
5504 if (next < end)
5505 next++;
5506
5507 while (next < end && HTTP_IS_SPHT(*next))
5508 next++;
5509 }
5510 else {
5511 /* Remove useless spaces before the old delimiter. */
5512 while (HTTP_IS_SPHT(*(prev-1)))
5513 prev--;
5514 *from = prev;
5515
5516 /* copy the delimiter and if possible a space if we're
5517 * not at the end of the line.
5518 */
5519 if (next < end) {
5520 *prev++ = *next++;
5521 if (prev + 1 < next)
5522 *prev++ = ' ';
5523 while (next < end && HTTP_IS_SPHT(*next))
5524 next++;
5525 }
5526 }
5527 memmove(prev, next, end - next);
5528 return (prev - next);
5529}
5530
Christopher Faulet0f226952018-10-22 09:29:56 +02005531
5532/* Formats the start line of the request (without CRLF) and puts it in <str> and
5533 * return the written lenght. The line can be truncated if it exceeds <len>.
5534 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005535static size_t htx_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005536{
5537 struct ist dst = ist2(str, 0);
5538
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005539 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005540 goto end;
5541 if (dst.len + 1 > len)
5542 goto end;
5543 dst.ptr[dst.len++] = ' ';
5544
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005545 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005546 goto end;
5547 if (dst.len + 1 > len)
5548 goto end;
5549 dst.ptr[dst.len++] = ' ';
5550
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005551 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005552 end:
5553 return dst.len;
5554}
5555
Christopher Fauletf0523542018-10-24 11:06:58 +02005556/* Formats the start line of the response (without CRLF) and puts it in <str> and
5557 * return the written lenght. The line can be truncated if it exceeds <len>.
5558 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005559static size_t htx_fmt_res_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Fauletf0523542018-10-24 11:06:58 +02005560{
5561 struct ist dst = ist2(str, 0);
5562
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005563 if (istcat(&dst, htx_sl_res_vsn(sl), len) == -1)
Christopher Fauletf0523542018-10-24 11:06:58 +02005564 goto end;
5565 if (dst.len + 1 > len)
5566 goto end;
5567 dst.ptr[dst.len++] = ' ';
5568
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005569 if (istcat(&dst, htx_sl_res_code(sl), len) == -1)
Christopher Fauletf0523542018-10-24 11:06:58 +02005570 goto end;
5571 if (dst.len + 1 > len)
5572 goto end;
5573 dst.ptr[dst.len++] = ' ';
5574
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005575 istcat(&dst, htx_sl_res_reason(sl), len);
Christopher Fauletf0523542018-10-24 11:06:58 +02005576 end:
5577 return dst.len;
5578}
5579
5580
Christopher Faulet0f226952018-10-22 09:29:56 +02005581/*
5582 * Print a debug line with a start line.
5583 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005584static void htx_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005585{
5586 struct session *sess = strm_sess(s);
5587 int max;
5588
5589 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5590 dir,
5591 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5592 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5593
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005594 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005595 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005596 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005597 trash.area[trash.data++] = ' ';
5598
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005599 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005600 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005601 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005602 trash.area[trash.data++] = ' ';
5603
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005604 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005605 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005606 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005607 trash.area[trash.data++] = '\n';
5608
5609 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5610}
5611
5612/*
5613 * Print a debug line with a header.
5614 */
5615static void htx_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v)
5616{
5617 struct session *sess = strm_sess(s);
5618 int max;
5619
5620 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5621 dir,
5622 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5623 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5624
5625 max = n.len;
5626 UBOUND(max, trash.size - trash.data - 3);
5627 chunk_memcat(&trash, n.ptr, max);
5628 trash.area[trash.data++] = ':';
5629 trash.area[trash.data++] = ' ';
5630
5631 max = v.len;
5632 UBOUND(max, trash.size - trash.data - 1);
5633 chunk_memcat(&trash, v.ptr, max);
5634 trash.area[trash.data++] = '\n';
5635
5636 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5637}
5638
5639
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005640__attribute__((constructor))
5641static void __htx_protocol_init(void)
5642{
5643}
5644
5645
5646/*
5647 * Local variables:
5648 * c-indent-level: 8
5649 * c-basic-offset: 8
5650 * End:
5651 */