blob: dc9f06e69c74af52d63abd434b7fc1ffecb0ccd7 [file] [log] [blame]
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Christopher Faulete0768eb2018-10-03 16:38:02 +020013#include <common/base64.h>
14#include <common/config.h>
15#include <common/debug.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010016#include <common/htx.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020017#include <common/uri_auth.h>
18
Christopher Faulet0f226952018-10-22 09:29:56 +020019#include <types/capture.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020020
21#include <proto/acl.h>
Christopher Faulet3e964192018-10-24 11:39:23 +020022#include <proto/action.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020023#include <proto/channel.h>
24#include <proto/checks.h>
25#include <proto/connection.h>
26#include <proto/filters.h>
Christopher Faulet0f226952018-10-22 09:29:56 +020027#include <proto/http_htx.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020028#include <proto/log.h>
Christopher Faulet3e964192018-10-24 11:39:23 +020029#include <proto/pattern.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020030#include <proto/http_ana.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020031#include <proto/proxy.h>
Christopher Fauletfefc73d2018-10-24 21:18:04 +020032#include <proto/server.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020033#include <proto/stream.h>
34#include <proto/stream_interface.h>
35#include <proto/stats.h>
Christopher Fauleta8a46e22019-07-16 14:53:09 +020036#include <proto/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020037
Christopher Fauleteea8fc72019-11-05 16:18:10 +010038#define TRACE_SOURCE &trace_strm
39
Christopher Faulet377c5a52018-10-24 21:21:30 +020040extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020041
Christopher Fauleta8a46e22019-07-16 14:53:09 +020042struct pool_head *pool_head_requri = NULL;
43struct pool_head *pool_head_capture = NULL;
44
45
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020046static void http_end_request(struct stream *s);
47static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020048
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020049static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
50static int http_del_hdr_value(char *start, char *end, char **from, char *next);
51static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020052static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
53static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
Christopher Faulet0f226952018-10-22 09:29:56 +020054
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020055static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status);
56static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020057
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020058static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
59static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020060
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020061static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
62static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020063
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020064static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
65static int http_reply_100_continue(struct stream *s);
66static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm);
Christopher Faulet23a3c792018-11-28 10:01:23 +010067
Christopher Faulete0768eb2018-10-03 16:38:02 +020068/* This stream analyser waits for a complete HTTP request. It returns 1 if the
69 * processing can continue on next analysers, or zero if it either needs more
70 * data or wants to immediately abort the request (eg: timeout, error, ...). It
71 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
72 * when it has nothing left to do, and may remove any analyser when it wants to
73 * abort.
74 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020075int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020076{
Christopher Faulet9768c262018-10-22 09:34:31 +020077
Christopher Faulete0768eb2018-10-03 16:38:02 +020078 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020079 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020080 *
Christopher Faulet9768c262018-10-22 09:34:31 +020081 * Once the start line and all headers are received, we may perform a
82 * capture of the error (if any), and we will set a few fields. We also
83 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020084 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020085 struct session *sess = s->sess;
86 struct http_txn *txn = s->txn;
87 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020088 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010089 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020090
Christopher Fauleteea8fc72019-11-05 16:18:10 +010091 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020092
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010093 htx = htxbuf(&req->buf);
Christopher Faulet9768c262018-10-22 09:34:31 +020094
Willy Tarreau4236f032019-03-05 10:43:32 +010095 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +020096 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +010097 stream_inc_http_req_ctr(s);
98 stream_inc_http_err_ctr(s);
99 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200100 if (htx->flags & HTX_FL_PARSING_ERROR)
101 goto return_bad_req;
102 else
103 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100104 }
105
Christopher Faulete0768eb2018-10-03 16:38:02 +0200106 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200107 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200108
109 /* If there is data available for analysis, log the end of the idle time. */
Christopher Faulet870aad92018-11-29 15:23:46 +0100110 if (c_data(req) && s->logs.t_idle == -1) {
111 const struct cs_info *csinfo = si_get_cs_info(objt_cs(s->si[0].end));
112
113 s->logs.t_idle = ((csinfo)
114 ? csinfo->t_idle
115 : tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake);
116 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200117
Christopher Faulete0768eb2018-10-03 16:38:02 +0200118 /*
119 * Now we quickly check if we have found a full valid request.
120 * If not so, we check the FD and buffer states before leaving.
121 * A full request is indicated by the fact that we have seen
122 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
123 * requests are checked first. When waiting for a second request
124 * on a keep-alive stream, if we encounter and error, close, t/o,
125 * we note the error in the stream flags but don't set any state.
126 * Since the error will be noted there, it will not be counted by
127 * process_stream() as a frontend error.
128 * Last, we may increase some tracked counters' http request errors on
129 * the cases that are deliberately the client's fault. For instance,
130 * a timeout or connection reset is not counted as an error. However
131 * a bad request is.
132 */
Christopher Faulet29f17582019-05-23 11:03:26 +0200133 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200134 if (htx->flags & HTX_FL_UPGRADE)
135 goto failed_keep_alive;
136
Christopher Faulet9768c262018-10-22 09:34:31 +0200137 /* 1: have we encountered a read error ? */
138 if (req->flags & CF_READ_ERROR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200139 if (!(s->flags & SF_ERR_MASK))
140 s->flags |= SF_ERR_CLICL;
141
142 if (txn->flags & TX_WAIT_NEXT_RQ)
143 goto failed_keep_alive;
144
145 if (sess->fe->options & PR_O_IGNORE_PRB)
146 goto failed_keep_alive;
147
Christopher Faulet9768c262018-10-22 09:34:31 +0200148 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200149 stream_inc_http_req_ctr(s);
150 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100151 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200152 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100153 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200154
Christopher Faulet9768c262018-10-22 09:34:31 +0200155 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200156 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet9768c262018-10-22 09:34:31 +0200157 req->analysers &= AN_REQ_FLT_END;
158
Christopher Faulete0768eb2018-10-03 16:38:02 +0200159 if (!(s->flags & SF_FINST_MASK))
160 s->flags |= SF_FINST_R;
161 return 0;
162 }
163
Christopher Faulet9768c262018-10-22 09:34:31 +0200164 /* 2: has the read timeout expired ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200165 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
166 if (!(s->flags & SF_ERR_MASK))
167 s->flags |= SF_ERR_CLITO;
168
169 if (txn->flags & TX_WAIT_NEXT_RQ)
170 goto failed_keep_alive;
171
172 if (sess->fe->options & PR_O_IGNORE_PRB)
173 goto failed_keep_alive;
174
Christopher Faulet9768c262018-10-22 09:34:31 +0200175 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200176 stream_inc_http_req_ctr(s);
177 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100178 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200179 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100180 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200181
Christopher Faulet9768c262018-10-22 09:34:31 +0200182 txn->status = 408;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200183 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200184 req->analysers &= AN_REQ_FLT_END;
185
Christopher Faulete0768eb2018-10-03 16:38:02 +0200186 if (!(s->flags & SF_FINST_MASK))
187 s->flags |= SF_FINST_R;
188 return 0;
189 }
190
Christopher Faulet9768c262018-10-22 09:34:31 +0200191 /* 3: have we encountered a close ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200192 else if (req->flags & CF_SHUTR) {
193 if (!(s->flags & SF_ERR_MASK))
194 s->flags |= SF_ERR_CLICL;
195
196 if (txn->flags & TX_WAIT_NEXT_RQ)
197 goto failed_keep_alive;
198
199 if (sess->fe->options & PR_O_IGNORE_PRB)
200 goto failed_keep_alive;
201
Christopher Faulete0768eb2018-10-03 16:38:02 +0200202 stream_inc_http_err_ctr(s);
203 stream_inc_http_req_ctr(s);
204 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100205 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200206 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100207 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200208
Christopher Faulet9768c262018-10-22 09:34:31 +0200209 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200210 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200211 req->analysers &= AN_REQ_FLT_END;
212
Christopher Faulete0768eb2018-10-03 16:38:02 +0200213 if (!(s->flags & SF_FINST_MASK))
214 s->flags |= SF_FINST_R;
215 return 0;
216 }
217
218 channel_dont_connect(req);
219 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
220 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100221
Christopher Faulet9768c262018-10-22 09:34:31 +0200222 if (sess->listener->options & LI_O_NOQUICKACK && htx_is_not_empty(htx) &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200223 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
224 /* We need more data, we have to re-enable quick-ack in case we
225 * previously disabled it, otherwise we might cause the client
226 * to delay next data.
227 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100228 conn_set_quickack(objt_conn(sess->origin), 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200229 }
Willy Tarreau1a18b542018-12-11 16:37:42 +0100230
Christopher Faulet47365272018-10-31 17:40:50 +0100231 if ((req->flags & CF_READ_PARTIAL) && (txn->flags & TX_WAIT_NEXT_RQ)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200232 /* If the client starts to talk, let's fall back to
233 * request timeout processing.
234 */
235 txn->flags &= ~TX_WAIT_NEXT_RQ;
236 req->analyse_exp = TICK_ETERNITY;
237 }
238
239 /* just set the request timeout once at the beginning of the request */
240 if (!tick_isset(req->analyse_exp)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100241 if ((txn->flags & TX_WAIT_NEXT_RQ) && tick_isset(s->be->timeout.httpka))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200242 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
243 else
244 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
245 }
246
247 /* we're not ready yet */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100248 DBG_TRACE_DEVEL("waiting for the request",
249 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200250 return 0;
251
252 failed_keep_alive:
253 /* Here we process low-level errors for keep-alive requests. In
254 * short, if the request is not the first one and it experiences
255 * a timeout, read error or shutdown, we just silently close so
256 * that the client can try again.
257 */
258 txn->status = 0;
259 msg->msg_state = HTTP_MSG_RQBEFORE;
260 req->analysers &= AN_REQ_FLT_END;
261 s->logs.logwait = 0;
262 s->logs.level = 0;
263 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200264 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100265 DBG_TRACE_DEVEL("leaving by closing K/A connection",
266 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200267 return 0;
268 }
269
Christopher Faulet9768c262018-10-22 09:34:31 +0200270 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200271 stream_inc_http_req_ctr(s);
272 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
273
Christopher Faulet9768c262018-10-22 09:34:31 +0200274 /* kill the pending keep-alive timeout */
275 txn->flags &= ~TX_WAIT_NEXT_RQ;
276 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200277
Christopher Faulet29f17582019-05-23 11:03:26 +0200278 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200279 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100280
Christopher Faulet9768c262018-10-22 09:34:31 +0200281 /* 0: we might have to print this header in debug mode */
282 if (unlikely((global.mode & MODE_DEBUG) &&
283 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
284 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200285
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200286 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200287
Christopher Fauleta3f15502019-05-13 15:27:23 +0200288 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200289 struct htx_blk *blk = htx_get_blk(htx, pos);
290 enum htx_blk_type type = htx_get_blk_type(blk);
291
292 if (type == HTX_BLK_EOH)
293 break;
294 if (type != HTX_BLK_HDR)
295 continue;
296
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200297 http_debug_hdr("clihdr", s,
298 htx_get_blk_name(htx, blk),
299 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200300 }
301 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200302
303 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100304 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200305 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100306 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100307 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200308 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100309 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +0100310 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100311 if (sl->flags & HTX_SL_F_BODYLESS)
312 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200313
314 /* we can make use of server redirect on GET and HEAD */
315 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
316 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100317 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200318 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200319 goto return_bad_req;
320 }
321
322 /*
323 * 2: check if the URI matches the monitor_uri.
324 * We have to do this for every request which gets in, because
325 * the monitor-uri is defined by the frontend.
326 */
327 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100328 isteqi(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200329 /*
330 * We have found the monitor URI
331 */
332 struct acl_cond *cond;
333
334 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100335 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200336
337 /* Check if we want to fail this monitor request or not */
338 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
339 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
340
341 ret = acl_pass(ret);
342 if (cond->pol == ACL_COND_UNLESS)
343 ret = !ret;
344
345 if (ret) {
346 /* we fail this request, let's return 503 service unavail */
347 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200348 if (!(s->flags & SF_ERR_MASK))
349 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
350 goto return_prx_cond;
351 }
352 }
353
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800354 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200355 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200356 if (!(s->flags & SF_ERR_MASK))
357 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
358 goto return_prx_cond;
359 }
360
361 /*
362 * 3: Maybe we have to copy the original REQURI for the logs ?
363 * Note: we cannot log anymore if the request has been
364 * classified as invalid.
365 */
366 if (unlikely(s->logs.logwait & LW_REQ)) {
367 /* we have a complete HTTP request that we must log */
368 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200369 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200370
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200371 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200372 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200373
374 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
375 s->do_log(s);
376 } else {
377 ha_alert("HTTP logging : out of memory.\n");
378 }
379 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200380
Christopher Faulete0768eb2018-10-03 16:38:02 +0200381 /* if the frontend has "option http-use-proxy-header", we'll check if
382 * we have what looks like a proxied connection instead of a connection,
383 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
384 * Note that this is *not* RFC-compliant, however browsers and proxies
385 * happen to do that despite being non-standard :-(
386 * We consider that a request not beginning with either '/' or '*' is
387 * a proxied connection, which covers both "scheme://location" and
388 * CONNECT ip:port.
389 */
390 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100391 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200392 txn->flags |= TX_USE_PX_CONN;
393
Christopher Faulete0768eb2018-10-03 16:38:02 +0200394 /* 5: we may need to capture headers */
395 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200396 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200397
Christopher Faulete0768eb2018-10-03 16:38:02 +0200398 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200399 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200400 req->analysers |= AN_REQ_HTTP_BODY;
401
402 /*
403 * RFC7234#4:
404 * A cache MUST write through requests with methods
405 * that are unsafe (Section 4.2.1 of [RFC7231]) to
406 * the origin server; i.e., a cache is not allowed
407 * to generate a reply to such a request before
408 * having forwarded the request and having received
409 * a corresponding response.
410 *
411 * RFC7231#4.2.1:
412 * Of the request methods defined by this
413 * specification, the GET, HEAD, OPTIONS, and TRACE
414 * methods are defined to be safe.
415 */
416 if (likely(txn->meth == HTTP_METH_GET ||
417 txn->meth == HTTP_METH_HEAD ||
418 txn->meth == HTTP_METH_OPTIONS ||
419 txn->meth == HTTP_METH_TRACE))
420 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
421
422 /* end of job, return OK */
423 req->analysers &= ~an_bit;
424 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200425
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100426 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200427 return 1;
428
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200429 return_int_err:
430 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200431 if (!(s->flags & SF_ERR_MASK))
432 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100433 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200434 if (sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100435 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200436 goto return_prx_cond;
437
Christopher Faulete0768eb2018-10-03 16:38:02 +0200438 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200439 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100440 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200441 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100442 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200443 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200444
445 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200446 http_reply_and_close(s, txn->status, http_error_message(s));
447
Christopher Faulete0768eb2018-10-03 16:38:02 +0200448 if (!(s->flags & SF_ERR_MASK))
449 s->flags |= SF_ERR_PRXCOND;
450 if (!(s->flags & SF_FINST_MASK))
451 s->flags |= SF_FINST_R;
452
453 req->analysers &= AN_REQ_FLT_END;
454 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100455 DBG_TRACE_DEVEL("leaving on error",
456 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200457 return 0;
458}
459
460
461/* This stream analyser runs all HTTP request processing which is common to
462 * frontends and backends, which means blocking ACLs, filters, connection-close,
463 * reqadd, stats and redirects. This is performed for the designated proxy.
464 * It returns 1 if the processing can continue on next analysers, or zero if it
465 * either needs more data or wants to immediately abort the request (eg: deny,
466 * error, ...).
467 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200468int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200469{
470 struct session *sess = s->sess;
471 struct http_txn *txn = s->txn;
472 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200473 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200474 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200475 enum rule_result verdict;
476 int deny_status = HTTP_ERR_403;
477 struct connection *conn = objt_conn(sess->origin);
478
479 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
480 /* we need more data */
481 goto return_prx_yield;
482 }
483
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100484 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200485
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100486 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200487
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200488 /* just in case we have some per-backend tracking. Only called the first
489 * execution of the analyser. */
490 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
491 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200492
493 /* evaluate http-request rules */
494 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200495 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200496
497 switch (verdict) {
498 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
499 goto return_prx_yield;
500
501 case HTTP_RULE_RES_CONT:
502 case HTTP_RULE_RES_STOP: /* nothing to do */
503 break;
504
505 case HTTP_RULE_RES_DENY: /* deny or tarpit */
506 if (txn->flags & TX_CLTARPIT)
507 goto tarpit;
508 goto deny;
509
510 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
511 goto return_prx_cond;
512
513 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
514 goto done;
515
516 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
517 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100518
519 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
520 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200521 }
522 }
523
524 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
525 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200526 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200527
Christopher Fauletff2759f2018-10-24 11:13:16 +0200528 ctx.blk = NULL;
529 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
530 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100531 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200532 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200533 }
534
535 /* OK at this stage, we know that the request was accepted according to
536 * the http-request rules, we can check for the stats. Note that the
537 * URI is detected *before* the req* rules in order not to be affected
538 * by a possible reqrep, while they are processed *after* so that a
539 * reqdeny can still block them. This clearly needs to change in 1.6!
540 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200541 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200542 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100543 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200544 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200545 if (!(s->flags & SF_ERR_MASK))
546 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100547 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200548 }
549
550 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200551 http_handle_stats(s, req);
552 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200553 /* not all actions implemented: deny, allow, auth */
554
555 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
556 goto deny;
557
558 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
559 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100560
561 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
562 goto return_bad_req;
563
564 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
565 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200566 }
567
Christopher Faulet2571bc62019-03-01 11:44:26 +0100568 /* Proceed with the applets now. */
569 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200570 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100571 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200572
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200573 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100574 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100575
Christopher Faulete0768eb2018-10-03 16:38:02 +0200576 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
577 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
578 if (!(s->flags & SF_FINST_MASK))
579 s->flags |= SF_FINST_R;
580
581 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
582 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
583 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
584 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100585
586 req->flags |= CF_SEND_DONTWAIT;
587 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200588 goto done;
589 }
590
591 /* check whether we have some ACLs set to redirect this request */
592 list_for_each_entry(rule, &px->redirect_rules, list) {
593 if (rule->cond) {
594 int ret;
595
596 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
597 ret = acl_pass(ret);
598 if (rule->cond->pol == ACL_COND_UNLESS)
599 ret = !ret;
600 if (!ret)
601 continue;
602 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200603 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100604 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200605 goto done;
606 }
607
608 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
609 * If this happens, then the data will not come immediately, so we must
610 * send all what we have without waiting. Note that due to the small gain
611 * in waiting for the body of the request, it's easier to simply put the
612 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
613 * itself once used.
614 */
615 req->flags |= CF_SEND_DONTWAIT;
616
617 done: /* done with this analyser, continue with next ones that the calling
618 * points will have set, if any.
619 */
620 req->analyse_exp = TICK_ETERNITY;
621 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
622 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100623 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200624 return 1;
625
626 tarpit:
627 /* Allow cookie logging
628 */
629 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200630 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200631
632 /* When a connection is tarpitted, we use the tarpit timeout,
633 * which may be the same as the connect timeout if unspecified.
634 * If unset, then set it to zero because we really want it to
635 * eventually expire. We build the tarpit as an analyser.
636 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100637 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200638
639 /* wipe the request out so that we can drop the connection early
640 * if the client closes first.
641 */
642 channel_dont_connect(req);
643
644 txn->status = http_err_codes[deny_status];
645
646 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
647 req->analysers |= AN_REQ_HTTP_TARPIT;
648 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
649 if (!req->analyse_exp)
650 req->analyse_exp = tick_add(now_ms, 0);
651 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100652 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100653 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100654 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200655 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100656 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200657 goto done_without_exp;
658
659 deny: /* this request was blocked (denied) */
660
661 /* Allow cookie logging
662 */
663 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200664 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200665
666 txn->flags |= TX_CLDENY;
667 txn->status = http_err_codes[deny_status];
668 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200669 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100670 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100671 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100672 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200673 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100674 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100675 goto return_prx_err;
676
677 return_int_err:
678 txn->status = 500;
679 if (!(s->flags & SF_ERR_MASK))
680 s->flags |= SF_ERR_INTERNAL;
681 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100682 if (s->flags & SF_BE_ASSIGNED)
683 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100684 if (sess->listener->counters)
685 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
686 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200687
688 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200689 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100690 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200691 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100692 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100693 /* fall through */
694
695 return_prx_err:
696 http_reply_and_close(s, txn->status, http_error_message(s));
697 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200698
699 return_prx_cond:
700 if (!(s->flags & SF_ERR_MASK))
701 s->flags |= SF_ERR_PRXCOND;
702 if (!(s->flags & SF_FINST_MASK))
703 s->flags |= SF_FINST_R;
704
705 req->analysers &= AN_REQ_FLT_END;
706 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100707 DBG_TRACE_DEVEL("leaving on error",
708 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200709 return 0;
710
711 return_prx_yield:
712 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100713 DBG_TRACE_DEVEL("waiting for more data",
714 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200715 return 0;
716}
717
718/* This function performs all the processing enabled for the current request.
719 * It returns 1 if the processing can continue on next analysers, or zero if it
720 * needs more data, encounters an error, or wants to immediately abort the
721 * request. It relies on buffers flags, and updates s->req.analysers.
722 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200723int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200724{
725 struct session *sess = s->sess;
726 struct http_txn *txn = s->txn;
727 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200728 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200729 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
730
731 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
732 /* we need more data */
733 channel_dont_connect(req);
734 return 0;
735 }
736
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100737 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200738
739 /*
740 * Right now, we know that we have processed the entire headers
741 * and that unwanted requests have been filtered out. We can do
742 * whatever we want with the remaining request. Also, now we
743 * may have separate values for ->fe, ->be.
744 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100745 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200746
747 /*
748 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200749 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200750 */
751 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100752 struct htx_sl *sl;
753 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200754
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200755 if (!sockaddr_alloc(&s->target_addr)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200756 if (!(s->flags & SF_ERR_MASK))
757 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100758 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200759 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200760 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100761 uri = htx_sl_req_uri(sl);
762 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200763
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200764 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200765 goto return_bad_req;
766
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200767 s->target = &s->be->obj_type;
768 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
769
Christopher Faulete0768eb2018-10-03 16:38:02 +0200770 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200771 * uri.ptr and path.ptr (excluded). If it was not found, we need
772 * to replace from all the uri by a single "/".
773 *
774 * Instead of rewritting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100775 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200776 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200777 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100778 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200779 }
780
781 /*
782 * 7: Now we can work with the cookies.
783 * Note that doing so might move headers in the request, but
784 * the fields will stay coherent and the URI will not move.
785 * This should only be performed in the backend.
786 */
787 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200788 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200789
790 /* add unique-id if "header-unique-id" is specified */
791
792 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100793 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) {
794 if (!(s->flags & SF_ERR_MASK))
795 s->flags |= SF_ERR_RESOURCE;
796 goto return_int_err;
797 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200798 s->unique_id[0] = '\0';
799 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
800 }
801
802 if (sess->fe->header_unique_id && s->unique_id) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200803 struct ist n = ist2(sess->fe->header_unique_id, strlen(sess->fe->header_unique_id));
804 struct ist v = ist2(s->unique_id, strlen(s->unique_id));
805
806 if (unlikely(!http_add_header(htx, n, v)))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100807 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200808 }
809
810 /*
811 * 9: add X-Forwarded-For if either the frontend or the backend
812 * asks for it.
813 */
814 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200815 struct http_hdr_ctx ctx = { .blk = NULL };
816 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
817 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
818
Christopher Faulete0768eb2018-10-03 16:38:02 +0200819 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200820 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200821 /* The header is set to be added only if none is present
822 * and we found it, so don't do anything.
823 */
824 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200825 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200826 /* Add an X-Forwarded-For header unless the source IP is
827 * in the 'except' network range.
828 */
829 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200830 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200831 != sess->fe->except_net.s_addr) &&
832 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200833 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200834 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200835 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200836
837 /* Note: we rely on the backend to get the header name to be used for
838 * x-forwarded-for, because the header is really meant for the backends.
839 * However, if the backend did not specify any option, we have to rely
840 * on the frontend's header name.
841 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200842 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
843 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100844 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200845 }
846 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200847 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200848 /* FIXME: for the sake of completeness, we should also support
849 * 'except' here, although it is mostly useless in this case.
850 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200851 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200852
Christopher Faulete0768eb2018-10-03 16:38:02 +0200853 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200854 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200855 pn, sizeof(pn));
856
857 /* Note: we rely on the backend to get the header name to be used for
858 * x-forwarded-for, because the header is really meant for the backends.
859 * However, if the backend did not specify any option, we have to rely
860 * on the frontend's header name.
861 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200862 chunk_printf(&trash, "%s", pn);
863 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100864 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200865 }
866 }
867
868 /*
869 * 10: add X-Original-To if either the frontend or the backend
870 * asks for it.
871 */
872 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
873
874 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200875 if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET && conn_get_dst(cli_conn)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200876 /* Add an X-Original-To header unless the destination IP is
877 * in the 'except' network range.
878 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200879 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200880 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200881 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200882 != sess->fe->except_to.s_addr) &&
883 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200884 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200885 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200886 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200887 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200888
889 /* Note: we rely on the backend to get the header name to be used for
890 * x-original-to, because the header is really meant for the backends.
891 * However, if the backend did not specify any option, we have to rely
892 * on the frontend's header name.
893 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200894 if (s->be->orgto_hdr_len)
895 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
896 else
897 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200898
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200899 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
900 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100901 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200902 }
903 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200904 }
905
Christopher Faulete0768eb2018-10-03 16:38:02 +0200906 /* If we have no server assigned yet and we're balancing on url_param
907 * with a POST request, we may be interested in checking the body for
908 * that parameter. This will be done in another analyser.
909 */
910 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100911 s->txn->meth == HTTP_METH_POST &&
912 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200913 channel_dont_connect(req);
914 req->analysers |= AN_REQ_HTTP_BODY;
915 }
916
917 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
918 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100919
Christopher Faulete0768eb2018-10-03 16:38:02 +0200920 /* We expect some data from the client. Unless we know for sure
921 * we already have a full request, we have to re-enable quick-ack
922 * in case we previously disabled it, otherwise we might cause
923 * the client to delay further data.
924 */
925 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200926 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100927 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200928
929 /*************************************************************
930 * OK, that's finished for the headers. We have done what we *
931 * could. Let's switch to the DATA state. *
932 ************************************************************/
933 req->analyse_exp = TICK_ETERNITY;
934 req->analysers &= ~an_bit;
935
936 s->logs.tv_request = now;
937 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100938 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200939 return 1;
940
Christopher Fauletb8a53712019-12-16 11:29:38 +0100941 return_int_err:
942 txn->status = 500;
943 if (!(s->flags & SF_ERR_MASK))
944 s->flags |= SF_ERR_INTERNAL;
945 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100946 if (s->flags & SF_BE_ASSIGNED)
947 _HA_ATOMIC_ADD(&sess->fe->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100948 if (sess->listener->counters)
949 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
950 goto return_prx_cond;
951
Christopher Faulete0768eb2018-10-03 16:38:02 +0200952 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200953 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100954 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200955 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100956 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100957 /* fall through */
958
959 return_prx_cond:
960 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200961
962 if (!(s->flags & SF_ERR_MASK))
963 s->flags |= SF_ERR_PRXCOND;
964 if (!(s->flags & SF_FINST_MASK))
965 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100966
967 req->analysers &= AN_REQ_FLT_END;
968 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100969 DBG_TRACE_DEVEL("leaving on error",
970 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200971 return 0;
972}
973
974/* This function is an analyser which processes the HTTP tarpit. It always
975 * returns zero, at the beginning because it prevents any other processing
976 * from occurring, and at the end because it terminates the request.
977 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200978int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200979{
980 struct http_txn *txn = s->txn;
981
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100982 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200983 /* This connection is being tarpitted. The CLIENT side has
984 * already set the connect expiration date to the right
985 * timeout. We just have to check that the client is still
986 * there and that the timeout has not expired.
987 */
988 channel_dont_connect(req);
989 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100990 !tick_is_expired(req->analyse_exp, now_ms)) {
991 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
992 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200993 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100994 }
995
Christopher Faulete0768eb2018-10-03 16:38:02 +0200996
997 /* We will set the queue timer to the time spent, just for
998 * logging purposes. We fake a 500 server error, so that the
999 * attacker will not suspect his connection has been tarpitted.
1000 * It will not cause trouble to the logs because we can exclude
1001 * the tarpitted connections by filtering on the 'PT' status flags.
1002 */
1003 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1004
1005 if (!(req->flags & CF_READ_ERROR))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001006 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001007
1008 req->analysers &= AN_REQ_FLT_END;
1009 req->analyse_exp = TICK_ETERNITY;
1010
1011 if (!(s->flags & SF_ERR_MASK))
1012 s->flags |= SF_ERR_PRXCOND;
1013 if (!(s->flags & SF_FINST_MASK))
1014 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001015
1016 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001017 return 0;
1018}
1019
1020/* This function is an analyser which waits for the HTTP request body. It waits
1021 * for either the buffer to be full, or the full advertised contents to have
1022 * reached the buffer. It must only be called after the standard HTTP request
1023 * processing has occurred, because it expects the request to be parsed and will
1024 * look for the Expect header. It may send a 100-Continue interim response. It
1025 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
1026 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
1027 * needs to read more data, or 1 once it has completed its analysis.
1028 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001029int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001030{
1031 struct session *sess = s->sess;
1032 struct http_txn *txn = s->txn;
1033 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001034 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001035
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001036 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001037
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001038 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001039
Willy Tarreau4236f032019-03-05 10:43:32 +01001040 if (htx->flags & HTX_FL_PARSING_ERROR)
1041 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001042 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1043 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001044
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001045 if (msg->msg_state < HTTP_MSG_BODY)
1046 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001047
Christopher Faulete0768eb2018-10-03 16:38:02 +02001048 /* We have to parse the HTTP request body to find any required data.
1049 * "balance url_param check_post" should have been the only way to get
1050 * into this. We were brought here after HTTP header analysis, so all
1051 * related structures are ready.
1052 */
1053
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001054 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001055 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001056 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001057 }
1058
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001059 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001060
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001061 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1062 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001063 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001064 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001065 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001066 goto http_end;
1067
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001068 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001069 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1070 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001071 if (!(s->flags & SF_ERR_MASK))
1072 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001073 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1074 if (sess->listener->counters)
1075 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1076 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001077 }
1078
1079 /* we get here if we need to wait for more data */
1080 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1081 /* Not enough data. We'll re-use the http-request
1082 * timeout here. Ideally, we should set the timeout
1083 * relative to the accept() date. We just set the
1084 * request timeout once at the beginning of the
1085 * request.
1086 */
1087 channel_dont_connect(req);
1088 if (!tick_isset(req->analyse_exp))
1089 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001090 DBG_TRACE_DEVEL("waiting for more data",
1091 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001092 return 0;
1093 }
1094
1095 http_end:
1096 /* The situation will not evolve, so let's give up on the analysis. */
1097 s->logs.tv_request = now; /* update the request timer to reflect full request */
1098 req->analysers &= ~an_bit;
1099 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001100 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001101 return 1;
1102
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001103 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001104 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001105 if (!(s->flags & SF_ERR_MASK))
1106 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001107 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001108 if (s->flags & SF_BE_ASSIGNED)
1109 _HA_ATOMIC_ADD(&sess->fe->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001110 if (sess->listener->counters)
1111 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
1112 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001113
Christopher Faulete0768eb2018-10-03 16:38:02 +02001114 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001115 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001116 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1117 if (sess->listener->counters)
1118 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1119 /* fall through */
1120
1121 return_prx_cond:
1122 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001123
1124 if (!(s->flags & SF_ERR_MASK))
1125 s->flags |= SF_ERR_PRXCOND;
1126 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001127 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001128
Christopher Faulete0768eb2018-10-03 16:38:02 +02001129 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001130 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001131 DBG_TRACE_DEVEL("leaving on error",
1132 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001133 return 0;
1134}
1135
1136/* This function is an analyser which forwards request body (including chunk
1137 * sizes if any). It is called as soon as we must forward, even if we forward
1138 * zero byte. The only situation where it must not be called is when we're in
1139 * tunnel mode and we want to forward till the close. It's used both to forward
1140 * remaining data and to resync after end of body. It expects the msg_state to
1141 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1142 * read more data, or 1 once we can go on with next request or end the stream.
1143 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1144 * bytes of pending data + the headers if not already done.
1145 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001146int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001147{
1148 struct session *sess = s->sess;
1149 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001150 struct http_msg *msg = &txn->req;
1151 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001152 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001153 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001154
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001155 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001156
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001157 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001158
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001159 if (htx->flags & HTX_FL_PARSING_ERROR)
1160 goto return_bad_req;
1161 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1162 goto return_int_err;
1163
Christopher Faulete0768eb2018-10-03 16:38:02 +02001164 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1165 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1166 /* Output closed while we were sending data. We must abort and
1167 * wake the other side up.
1168 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001169
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001170 /* Don't abort yet if we had L7 retries activated and it
1171 * was a write error, we may recover.
1172 */
1173 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001174 (s->si[1].flags & SI_FL_L7_RETRY)) {
1175 DBG_TRACE_DEVEL("leaving on L7 retry",
1176 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001177 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001178 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001179 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001180 http_end_request(s);
1181 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001182 DBG_TRACE_DEVEL("leaving on error",
1183 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001184 return 1;
1185 }
1186
1187 /* Note that we don't have to send 100-continue back because we don't
1188 * need the data to complete our job, and it's up to the server to
1189 * decide whether to return 100, 417 or anything else in return of
1190 * an "Expect: 100-continue" header.
1191 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001192 if (msg->msg_state == HTTP_MSG_BODY)
1193 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001194
Christopher Faulete0768eb2018-10-03 16:38:02 +02001195 /* in most states, we should abort in case of early close */
1196 channel_auto_close(req);
1197
1198 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001199 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001200 if (req->flags & CF_EOI)
1201 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001202 }
1203 else {
1204 /* We can't process the buffer's contents yet */
1205 req->flags |= CF_WAKE_WRITE;
1206 goto missing_data_or_waiting;
1207 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001208 }
1209
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001210 if (msg->msg_state >= HTTP_MSG_ENDING)
1211 goto ending;
1212
1213 if (txn->meth == HTTP_METH_CONNECT) {
1214 msg->msg_state = HTTP_MSG_ENDING;
1215 goto ending;
1216 }
1217
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001218 /* Forward input data. We get it by removing all outgoing data not
1219 * forwarded yet from HTX data size. If there are some data filters, we
1220 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001221 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001222 if (HAS_REQ_DATA_FILTERS(s)) {
1223 ret = flt_http_payload(s, msg, htx->data);
1224 if (ret < 0)
1225 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001226 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001227 }
1228 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001229 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001230 if (msg->flags & HTTP_MSGF_XFER_LEN)
1231 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001232 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001233
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001234 if (htx->data != co_data(req))
1235 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001236
Christopher Faulet9768c262018-10-22 09:34:31 +02001237 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001238 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1239 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001240 */
1241 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1242 goto missing_data_or_waiting;
1243
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001244 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001245
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001246 ending:
1247 /* other states, ENDING...TUNNEL */
1248 if (msg->msg_state >= HTTP_MSG_DONE)
1249 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001250
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001251 if (HAS_REQ_DATA_FILTERS(s)) {
1252 ret = flt_http_end(s, msg);
1253 if (ret <= 0) {
1254 if (!ret)
1255 goto missing_data_or_waiting;
1256 goto return_bad_req;
1257 }
1258 }
1259
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001260 if (txn->meth == HTTP_METH_CONNECT)
1261 msg->msg_state = HTTP_MSG_TUNNEL;
1262 else {
1263 msg->msg_state = HTTP_MSG_DONE;
1264 req->to_forward = 0;
1265 }
1266
1267 done:
1268 /* we don't want to forward closes on DONE except in tunnel mode. */
1269 if (!(txn->flags & TX_CON_WANT_TUN))
1270 channel_dont_close(req);
1271
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001272 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001273 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001274 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001275 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1276 if (req->flags & CF_SHUTW) {
1277 /* request errors are most likely due to the
1278 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001279 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001280 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001281 goto return_bad_req;
1282 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001283 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001284 return 1;
1285 }
1286
1287 /* If "option abortonclose" is set on the backend, we want to monitor
1288 * the client's connection and forward any shutdown notification to the
1289 * server, which will decide whether to close or to go on processing the
1290 * request. We only do that in tunnel mode, and not in other modes since
1291 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001292 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001293 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001294 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001295 s->si[1].flags |= SI_FL_NOLINGER;
1296 channel_auto_close(req);
1297 }
1298 else if (s->txn->meth == HTTP_METH_POST) {
1299 /* POST requests may require to read extra CRLF sent by broken
1300 * browsers and which could cause an RST to be sent upon close
1301 * on some systems (eg: Linux). */
1302 channel_auto_read(req);
1303 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001304 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1305 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001306 return 0;
1307
1308 missing_data_or_waiting:
1309 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001310 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001311 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001312
1313 waiting:
1314 /* waiting for the last bits to leave the buffer */
1315 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001316 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001317
1318 /* When TE: chunked is used, we need to get there again to parse remaining
1319 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1320 * And when content-length is used, we never want to let the possible
1321 * shutdown be forwarded to the other side, as the state machine will
1322 * take care of it once the client responds. It's also important to
1323 * prevent TIME_WAITs from accumulating on the backend side, and for
1324 * HTTP/2 where the last frame comes with a shutdown.
1325 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001326 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001327 channel_dont_close(req);
1328
1329 /* We know that more data are expected, but we couldn't send more that
1330 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1331 * system knows it must not set a PUSH on this first part. Interactive
1332 * modes are already handled by the stream sock layer. We must not do
1333 * this in content-length mode because it could present the MSG_MORE
1334 * flag with the last block of forwarded data, which would cause an
1335 * additional delay to be observed by the receiver.
1336 */
1337 if (msg->flags & HTTP_MSGF_TE_CHNK)
1338 req->flags |= CF_EXPECT_MORE;
1339
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001340 DBG_TRACE_DEVEL("waiting for more data to forward",
1341 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001342 return 0;
1343
Christopher Faulet93e02d82019-03-08 14:18:50 +01001344 return_cli_abort:
1345 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1346 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001347 if (sess->listener->counters)
1348 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001349 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001350 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001351 if (!(s->flags & SF_ERR_MASK))
1352 s->flags |= SF_ERR_CLICL;
1353 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001354 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001355
1356 return_srv_abort:
1357 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1358 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001359 if (sess->listener->counters)
1360 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001361 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001362 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001363 if (!(s->flags & SF_ERR_MASK))
1364 s->flags |= SF_ERR_SRVCL;
1365 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001366 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001367
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001368 return_int_err:
1369 if (!(s->flags & SF_ERR_MASK))
1370 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001371 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001372 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001373 if (sess->listener->counters)
1374 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001375 if (objt_server(s->target))
1376 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001377 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001378 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001379
Christopher Faulet93e02d82019-03-08 14:18:50 +01001380 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001381 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001382 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001383 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001384 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001385 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001386
Christopher Fauletb8a53712019-12-16 11:29:38 +01001387 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001388 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001389 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001390 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001391 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001392 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001393 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001394 }
1395 req->analysers &= AN_REQ_FLT_END;
1396 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001397 if (!(s->flags & SF_ERR_MASK))
1398 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001399 if (!(s->flags & SF_FINST_MASK))
1400 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001401 DBG_TRACE_DEVEL("leaving on error ",
1402 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001403 return 0;
1404}
1405
Olivier Houcharda254a372019-04-05 15:30:12 +02001406/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1407/* Returns 0 if we can attempt to retry, -1 otherwise */
1408static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1409{
1410 struct channel *req, *res;
1411 int co_data;
1412
1413 si->conn_retries--;
1414 if (si->conn_retries < 0)
1415 return -1;
1416
Willy Tarreau223995e2019-05-04 10:38:31 +02001417 if (objt_server(s->target))
1418 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1419 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1420
Olivier Houcharda254a372019-04-05 15:30:12 +02001421 req = &s->req;
1422 res = &s->res;
1423 /* Remove any write error from the request, and read error from the response */
1424 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1425 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1426 res->analysers = 0;
1427 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001428 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001429 si->exp = TICK_ETERNITY;
1430 res->rex = TICK_ETERNITY;
1431 res->to_forward = 0;
1432 res->analyse_exp = TICK_ETERNITY;
1433 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001434 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001435 si_release_endpoint(&s->si[1]);
1436 b_free(&req->buf);
1437 /* Swap the L7 buffer with the channel buffer */
1438 /* We know we stored the co_data as b_data, so get it there */
1439 co_data = b_data(&si->l7_buffer);
1440 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1441 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1442
1443 co_set_data(req, co_data);
1444 b_reset(&res->buf);
1445 co_set_data(res, 0);
1446 return 0;
1447}
1448
Christopher Faulete0768eb2018-10-03 16:38:02 +02001449/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1450 * processing can continue on next analysers, or zero if it either needs more
1451 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1452 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1453 * when it has nothing left to do, and may remove any analyser when it wants to
1454 * abort.
1455 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001456int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001457{
Christopher Faulet9768c262018-10-22 09:34:31 +02001458 /*
1459 * We will analyze a complete HTTP response to check the its syntax.
1460 *
1461 * Once the start line and all headers are received, we may perform a
1462 * capture of the error (if any), and we will set a few fields. We also
1463 * logging and finally headers capture.
1464 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001465 struct session *sess = s->sess;
1466 struct http_txn *txn = s->txn;
1467 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001468 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001469 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001470 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001471 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001472 int n;
1473
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001474 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001475
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001476 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001477
Willy Tarreau4236f032019-03-05 10:43:32 +01001478 /* Parsing errors are caught here */
1479 if (htx->flags & HTX_FL_PARSING_ERROR)
1480 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001481 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1482 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001483
Christopher Faulete0768eb2018-10-03 16:38:02 +02001484 /*
1485 * Now we quickly check if we have found a full valid response.
1486 * If not so, we check the FD and buffer states before leaving.
1487 * A full response is indicated by the fact that we have seen
1488 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1489 * responses are checked first.
1490 *
1491 * Depending on whether the client is still there or not, we
1492 * may send an error response back or not. Note that normally
1493 * we should only check for HTTP status there, and check I/O
1494 * errors somewhere else.
1495 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001496 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001497 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001498 /* 1: have we encountered a read error ? */
1499 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001500 struct connection *conn = NULL;
1501
Olivier Houchard865d8392019-05-03 22:46:27 +02001502 if (objt_cs(s->si[1].end))
1503 conn = objt_cs(s->si[1].end)->conn;
1504
1505 if (si_b->flags & SI_FL_L7_RETRY &&
1506 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001507 /* If we arrive here, then CF_READ_ERROR was
1508 * set by si_cs_recv() because we matched a
1509 * status, overwise it would have removed
1510 * the SI_FL_L7_RETRY flag, so it's ok not
1511 * to check s->be->retry_type.
1512 */
1513 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1514 return 0;
1515 }
1516
Olivier Houchard6db16992019-05-17 15:40:49 +02001517 if (txn->flags & TX_NOT_FIRST)
1518 goto abort_keep_alive;
1519
Olivier Houcharda798bf52019-03-08 18:52:00 +01001520 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001521 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001522 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001523 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001524 }
1525
Christopher Faulete0768eb2018-10-03 16:38:02 +02001526 rep->analysers &= AN_RES_FLT_END;
1527 txn->status = 502;
1528
1529 /* Check to see if the server refused the early data.
1530 * If so, just send a 425
1531 */
Olivier Houchard865d8392019-05-03 22:46:27 +02001532 if (conn->err_code == CO_ER_SSL_EARLY_FAILED) {
1533 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001534 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001535 do_l7_retry(s, si_b) == 0) {
1536 DBG_TRACE_DEVEL("leaving on L7 retry",
1537 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001538 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001539 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001540 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001541 }
1542
1543 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001544 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001545
1546 if (!(s->flags & SF_ERR_MASK))
1547 s->flags |= SF_ERR_SRVCL;
1548 if (!(s->flags & SF_FINST_MASK))
1549 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001550 DBG_TRACE_DEVEL("leaving on error",
1551 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001552 return 0;
1553 }
1554
Christopher Faulet9768c262018-10-22 09:34:31 +02001555 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001556 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001557 if ((si_b->flags & SI_FL_L7_RETRY) &&
1558 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001559 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1560 DBG_TRACE_DEVEL("leaving on L7 retry",
1561 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001562 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001563 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001564 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001565 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001566 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001567 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001568 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001569 }
1570
Christopher Faulete0768eb2018-10-03 16:38:02 +02001571 rep->analysers &= AN_RES_FLT_END;
1572 txn->status = 504;
1573 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001574 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001575
1576 if (!(s->flags & SF_ERR_MASK))
1577 s->flags |= SF_ERR_SRVTO;
1578 if (!(s->flags & SF_FINST_MASK))
1579 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001580 DBG_TRACE_DEVEL("leaving on error",
1581 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001582 return 0;
1583 }
1584
Christopher Faulet9768c262018-10-22 09:34:31 +02001585 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001586 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001587 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1588 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001589 if (sess->listener->counters)
1590 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001591 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001592 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001593
1594 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001595 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001596 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001597
1598 if (!(s->flags & SF_ERR_MASK))
1599 s->flags |= SF_ERR_CLICL;
1600 if (!(s->flags & SF_FINST_MASK))
1601 s->flags |= SF_FINST_H;
1602
1603 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001604 DBG_TRACE_DEVEL("leaving on error",
1605 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001606 return 0;
1607 }
1608
Christopher Faulet9768c262018-10-22 09:34:31 +02001609 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001610 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001611 if ((si_b->flags & SI_FL_L7_RETRY) &&
1612 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001613 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1614 DBG_TRACE_DEVEL("leaving on L7 retry",
1615 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001616 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001617 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001618 }
1619
Olivier Houchard6db16992019-05-17 15:40:49 +02001620 if (txn->flags & TX_NOT_FIRST)
1621 goto abort_keep_alive;
1622
Olivier Houcharda798bf52019-03-08 18:52:00 +01001623 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001624 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001625 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001626 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001627 }
1628
Christopher Faulete0768eb2018-10-03 16:38:02 +02001629 rep->analysers &= AN_RES_FLT_END;
1630 txn->status = 502;
1631 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001632 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001633
1634 if (!(s->flags & SF_ERR_MASK))
1635 s->flags |= SF_ERR_SRVCL;
1636 if (!(s->flags & SF_FINST_MASK))
1637 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001638 DBG_TRACE_DEVEL("leaving on error",
1639 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001640 return 0;
1641 }
1642
Christopher Faulet9768c262018-10-22 09:34:31 +02001643 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001644 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001645 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001646 goto abort_keep_alive;
1647
Olivier Houcharda798bf52019-03-08 18:52:00 +01001648 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001649 if (objt_server(s->target))
1650 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001651 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001652
1653 if (!(s->flags & SF_ERR_MASK))
1654 s->flags |= SF_ERR_CLICL;
1655 if (!(s->flags & SF_FINST_MASK))
1656 s->flags |= SF_FINST_H;
1657
1658 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001659 DBG_TRACE_DEVEL("leaving on error",
1660 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001661 return 0;
1662 }
1663
1664 channel_dont_close(rep);
1665 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001666 DBG_TRACE_DEVEL("waiting for more data",
1667 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001668 return 0;
1669 }
1670
1671 /* More interesting part now : we know that we have a complete
1672 * response which at least looks like HTTP. We have an indicator
1673 * of each header's length, so we can parse them quickly.
1674 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001675 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001676 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001677 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001678
Christopher Faulet9768c262018-10-22 09:34:31 +02001679 /* 0: we might have to print this header in debug mode */
1680 if (unlikely((global.mode & MODE_DEBUG) &&
1681 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1682 int32_t pos;
1683
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001684 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001685
Christopher Fauleta3f15502019-05-13 15:27:23 +02001686 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001687 struct htx_blk *blk = htx_get_blk(htx, pos);
1688 enum htx_blk_type type = htx_get_blk_type(blk);
1689
1690 if (type == HTX_BLK_EOH)
1691 break;
1692 if (type != HTX_BLK_HDR)
1693 continue;
1694
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001695 http_debug_hdr("srvhdr", s,
1696 htx_get_blk_name(htx, blk),
1697 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001698 }
1699 }
1700
Christopher Faulet03599112018-11-27 11:21:21 +01001701 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001702 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001703 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001704 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001705 if (sl->flags & HTX_SL_F_XFER_LEN) {
1706 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001707 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001708 if (sl->flags & HTX_SL_F_BODYLESS)
1709 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001710 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001711
1712 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001713 if (n < 1 || n > 5)
1714 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001715
Christopher Faulete0768eb2018-10-03 16:38:02 +02001716 /* when the client triggers a 4xx from the server, it's most often due
1717 * to a missing object or permission. These events should be tracked
1718 * because if they happen often, it may indicate a brute force or a
1719 * vulnerability scan.
1720 */
1721 if (n == 4)
1722 stream_inc_http_err_ctr(s);
1723
1724 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001725 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001726
Christopher Faulete0768eb2018-10-03 16:38:02 +02001727 /* Adjust server's health based on status code. Note: status codes 501
1728 * and 505 are triggered on demand by client request, so we must not
1729 * count them as server failures.
1730 */
1731 if (objt_server(s->target)) {
1732 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001733 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001734 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001735 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001736 }
1737
1738 /*
1739 * We may be facing a 100-continue response, or any other informational
1740 * 1xx response which is non-final, in which case this is not the right
1741 * response, and we're waiting for the next one. Let's allow this response
1742 * to go to the client and wait for the next one. There's an exception for
1743 * 101 which is used later in the code to switch protocols.
1744 */
1745 if (txn->status < 200 &&
1746 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001747 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001748 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001749 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001750 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001751 txn->status = 0;
1752 s->logs.t_data = -1; /* was not a response yet */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001753 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001754 }
1755
1756 /*
1757 * 2: check for cacheability.
1758 */
1759
1760 switch (txn->status) {
1761 case 200:
1762 case 203:
1763 case 204:
1764 case 206:
1765 case 300:
1766 case 301:
1767 case 404:
1768 case 405:
1769 case 410:
1770 case 414:
1771 case 501:
1772 break;
1773 default:
1774 /* RFC7231#6.1:
1775 * Responses with status codes that are defined as
1776 * cacheable by default (e.g., 200, 203, 204, 206,
1777 * 300, 301, 404, 405, 410, 414, and 501 in this
1778 * specification) can be reused by a cache with
1779 * heuristic expiration unless otherwise indicated
1780 * by the method definition or explicit cache
1781 * controls [RFC7234]; all other status codes are
1782 * not cacheable by default.
1783 */
1784 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1785 break;
1786 }
1787
1788 /*
1789 * 3: we may need to capture headers
1790 */
1791 s->logs.logwait &= ~LW_RESP;
1792 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001793 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001794
Christopher Faulet9768c262018-10-22 09:34:31 +02001795 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001796 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1797 txn->status == 101)) {
1798 /* Either we've established an explicit tunnel, or we're
1799 * switching the protocol. In both cases, we're very unlikely
1800 * to understand the next protocols. We have to switch to tunnel
1801 * mode, so that we transfer the request and responses then let
1802 * this protocol pass unmodified. When we later implement specific
1803 * parsers for such protocols, we'll want to check the Upgrade
1804 * header which contains information about that protocol for
1805 * responses with status 101 (eg: see RFC2817 about TLS).
1806 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001807 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001808 }
1809
Christopher Faulet61608322018-11-23 16:23:45 +01001810 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1811 * 407 (Proxy-Authenticate) responses and set the connection to private
1812 */
1813 srv_conn = cs_conn(objt_cs(s->si[1].end));
1814 if (srv_conn) {
1815 struct ist hdr;
1816 struct http_hdr_ctx ctx;
1817
1818 if (txn->status == 401)
1819 hdr = ist("WWW-Authenticate");
1820 else if (txn->status == 407)
1821 hdr = ist("Proxy-Authenticate");
1822 else
1823 goto end;
1824
1825 ctx.blk = NULL;
1826 while (http_find_header(htx, hdr, &ctx, 0)) {
1827 if ((ctx.value.len >= 9 && word_match(ctx.value.ptr, ctx.value.len, "Negotiate", 9)) ||
Olivier Houchard250031e2019-05-29 15:01:50 +02001828 (ctx.value.len >= 4 && word_match(ctx.value.ptr, ctx.value.len, "NTLM", 4))) {
1829 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet61608322018-11-23 16:23:45 +01001830 srv_conn->flags |= CO_FL_PRIVATE;
Olivier Houchard250031e2019-05-29 15:01:50 +02001831 }
Christopher Faulet61608322018-11-23 16:23:45 +01001832 }
1833 }
1834
1835 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001836 /* we want to have the response time before we start processing it */
1837 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1838
1839 /* end of job, return OK */
1840 rep->analysers &= ~an_bit;
1841 rep->analyse_exp = TICK_ETERNITY;
1842 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001843 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001844 return 1;
1845
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001846 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001847 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001848 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001849 if (sess->listener->counters)
1850 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001851 if (objt_server(s->target))
1852 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001853 txn->status = 500;
1854 if (!(s->flags & SF_ERR_MASK))
1855 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001856 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001857
1858 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001859 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001860 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001861 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001862 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001863 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001864 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001865 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001866 do_l7_retry(s, si_b) == 0) {
1867 DBG_TRACE_DEVEL("leaving on L7 retry",
1868 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001869 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001870 }
Christopher Faulet47365272018-10-31 17:40:50 +01001871 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001872 /* fall through */
1873
Christopher Fauletb8a53712019-12-16 11:29:38 +01001874 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001875 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001876
1877 if (!(s->flags & SF_ERR_MASK))
1878 s->flags |= SF_ERR_PRXCOND;
1879 if (!(s->flags & SF_FINST_MASK))
1880 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001881
1882 s->si[1].flags |= SI_FL_NOLINGER;
1883 rep->analysers &= AN_RES_FLT_END;
1884 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001885 DBG_TRACE_DEVEL("leaving on error",
1886 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001887 return 0;
1888
Christopher Faulete0768eb2018-10-03 16:38:02 +02001889 abort_keep_alive:
1890 /* A keep-alive request to the server failed on a network error.
1891 * The client is required to retry. We need to close without returning
1892 * any other information so that the client retries.
1893 */
1894 txn->status = 0;
1895 rep->analysers &= AN_RES_FLT_END;
1896 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001897 s->logs.logwait = 0;
1898 s->logs.level = 0;
1899 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001900 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001901 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1902 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001903 return 0;
1904}
1905
1906/* This function performs all the processing enabled for the current response.
1907 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1908 * and updates s->res.analysers. It might make sense to explode it into several
1909 * other functions. It works like process_request (see indications above).
1910 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001911int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001912{
1913 struct session *sess = s->sess;
1914 struct http_txn *txn = s->txn;
1915 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001916 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001917 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001918 enum rule_result ret = HTTP_RULE_RES_CONT;
1919
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001920 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1921 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001922
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001923 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001924
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001925 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001926
1927 /* The stats applet needs to adjust the Connection header but we don't
1928 * apply any filter there.
1929 */
1930 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1931 rep->analysers &= ~an_bit;
1932 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001933 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001934 }
1935
1936 /*
1937 * We will have to evaluate the filters.
1938 * As opposed to version 1.2, now they will be evaluated in the
1939 * filters order and not in the header order. This means that
1940 * each filter has to be validated among all headers.
1941 *
1942 * Filters are tried with ->be first, then with ->fe if it is
1943 * different from ->be.
1944 *
1945 * Maybe we are in resume condiion. In this case I choose the
1946 * "struct proxy" which contains the rule list matching the resume
1947 * pointer. If none of theses "struct proxy" match, I initialise
1948 * the process with the first one.
1949 *
1950 * In fact, I check only correspondance betwwen the current list
1951 * pointer and the ->fe rule list. If it doesn't match, I initialize
1952 * the loop with the ->be.
1953 */
1954 if (s->current_rule_list == &sess->fe->http_res_rules)
1955 cur_proxy = sess->fe;
1956 else
1957 cur_proxy = s->be;
1958 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001959 /* evaluate http-response rules */
1960 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001961 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001962
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001963 switch (ret) {
1964 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1965 goto return_prx_yield;
1966
1967 case HTTP_RULE_RES_CONT:
1968 case HTTP_RULE_RES_STOP: /* nothing to do */
1969 break;
1970
1971 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1972 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001973
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001974 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1975 goto return_prx_cond;
1976
1977 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001978 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001979
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001980 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1981 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001982
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001983 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1984 goto return_int_err;
1985 }
1986
1987 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001988
Christopher Faulete0768eb2018-10-03 16:38:02 +02001989 /* check whether we're already working on the frontend */
1990 if (cur_proxy == sess->fe)
1991 break;
1992 cur_proxy = sess->fe;
1993 }
1994
1995 /* After this point, this anayzer can't return yield, so we can
1996 * remove the bit corresponding to this analyzer from the list.
1997 *
1998 * Note that the intermediate returns and goto found previously
1999 * reset the analyzers.
2000 */
2001 rep->analysers &= ~an_bit;
2002 rep->analyse_exp = TICK_ETERNITY;
2003
2004 /* OK that's all we can do for 1xx responses */
2005 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002006 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002007
2008 /*
2009 * Now check for a server cookie.
2010 */
2011 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002012 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002013
2014 /*
2015 * Check for cache-control or pragma headers if required.
2016 */
2017 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002018 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002019
2020 /*
2021 * Add server cookie in the response if needed
2022 */
2023 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
2024 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
2025 (!(s->flags & SF_DIRECT) ||
2026 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
2027 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
2028 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
2029 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
2030 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
2031 !(s->flags & SF_IGNORE_PRST)) {
2032 /* the server is known, it's not the one the client requested, or the
2033 * cookie's last seen date needs to be refreshed. We have to
2034 * insert a set-cookie here, except if we want to insert only on POST
2035 * requests and this one isn't. Note that servers which don't have cookies
2036 * (eg: some backup servers) will return a full cookie removal request.
2037 */
2038 if (!objt_server(s->target)->cookie) {
2039 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002040 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02002041 s->be->cookie_name);
2042 }
2043 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002044 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002045
2046 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2047 /* emit last_date, which is mandatory */
2048 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2049 s30tob64((date.tv_sec+3) >> 2,
2050 trash.area + trash.data);
2051 trash.data += 5;
2052
2053 if (s->be->cookie_maxlife) {
2054 /* emit first_date, which is either the original one or
2055 * the current date.
2056 */
2057 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2058 s30tob64(txn->cookie_first_date ?
2059 txn->cookie_first_date >> 2 :
2060 (date.tv_sec+3) >> 2,
2061 trash.area + trash.data);
2062 trash.data += 5;
2063 }
2064 }
2065 chunk_appendf(&trash, "; path=/");
2066 }
2067
2068 if (s->be->cookie_domain)
2069 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2070
2071 if (s->be->ck_opts & PR_CK_HTTPONLY)
2072 chunk_appendf(&trash, "; HttpOnly");
2073
2074 if (s->be->ck_opts & PR_CK_SECURE)
2075 chunk_appendf(&trash, "; Secure");
2076
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002077 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002078 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002079
2080 txn->flags &= ~TX_SCK_MASK;
2081 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2082 /* the server did not change, only the date was updated */
2083 txn->flags |= TX_SCK_UPDATED;
2084 else
2085 txn->flags |= TX_SCK_INSERTED;
2086
2087 /* Here, we will tell an eventual cache on the client side that we don't
2088 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2089 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2090 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2091 */
2092 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2093
2094 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2095
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002096 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002097 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002098 }
2099 }
2100
2101 /*
2102 * Check if result will be cacheable with a cookie.
2103 * We'll block the response if security checks have caught
2104 * nasty things such as a cacheable cookie.
2105 */
2106 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2107 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2108 (s->be->options & PR_O_CHK_CACHE)) {
2109 /* we're in presence of a cacheable response containing
2110 * a set-cookie header. We'll block it as requested by
2111 * the 'checkcache' option, and send an alert.
2112 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002113 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2114 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2115 send_log(s->be, LOG_ALERT,
2116 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2117 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002118 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002119 }
2120
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002121 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002122 /* Always enter in the body analyzer */
2123 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2124 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2125
2126 /* if the user wants to log as soon as possible, without counting
2127 * bytes from the server, then this is the right moment. We have
2128 * to temporarily assign bytes_out to log what we currently have.
2129 */
2130 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2131 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002132 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002133 s->do_log(s);
2134 s->logs.bytes_out = 0;
2135 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002136 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002137 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002138
Christopher Fauletb8a53712019-12-16 11:29:38 +01002139 done:
2140 rep->analysers &= ~an_bit;
2141 rep->analyse_exp = TICK_ETERNITY;
2142 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002143
Christopher Fauletb8a53712019-12-16 11:29:38 +01002144 deny:
2145 txn->flags |= TX_CLDENY;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002146 txn->status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002147 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002148 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002149 if (sess->listener->counters)
2150 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002151 if (objt_server(s->target))
2152 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002153 goto return_prx_err;
2154
2155 return_int_err:
2156 txn->status = 500;
2157 if (!(s->flags & SF_ERR_MASK))
2158 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002159 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002160 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2161 if (objt_server(s->target))
2162 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002163 if (objt_server(s->target))
2164 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002165 goto return_prx_err;
2166
2167 return_bad_res:
2168 txn->status = 502;
2169 /* fall through */
2170
2171 return_prx_err:
2172 http_reply_and_close(s, txn->status, http_error_message(s));
2173 /* fall through */
2174
2175 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002176 s->logs.t_data = -1; /* was not a valid response */
2177 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002178
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002179 if (!(s->flags & SF_ERR_MASK))
2180 s->flags |= SF_ERR_PRXCOND;
2181 if (!(s->flags & SF_FINST_MASK))
2182 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002183
2184 rep->analysers &= ~an_bit;
2185 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002186 DBG_TRACE_DEVEL("leaving on error",
2187 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002188 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002189
2190 return_prx_yield:
2191 channel_dont_close(rep);
2192 DBG_TRACE_DEVEL("waiting for more data",
2193 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2194 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002195}
2196
2197/* This function is an analyser which forwards response body (including chunk
2198 * sizes if any). It is called as soon as we must forward, even if we forward
2199 * zero byte. The only situation where it must not be called is when we're in
2200 * tunnel mode and we want to forward till the close. It's used both to forward
2201 * remaining data and to resync after end of body. It expects the msg_state to
2202 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2203 * read more data, or 1 once we can go on with next request or end the stream.
2204 *
2205 * It is capable of compressing response data both in content-length mode and
2206 * in chunked mode. The state machines follows different flows depending on
2207 * whether content-length and chunked modes are used, since there are no
2208 * trailers in content-length :
2209 *
2210 * chk-mode cl-mode
2211 * ,----- BODY -----.
2212 * / \
2213 * V size > 0 V chk-mode
2214 * .--> SIZE -------------> DATA -------------> CRLF
2215 * | | size == 0 | last byte |
2216 * | v final crlf v inspected |
2217 * | TRAILERS -----------> DONE |
2218 * | |
2219 * `----------------------------------------------'
2220 *
2221 * Compression only happens in the DATA state, and must be flushed in final
2222 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2223 * is performed at once on final states for all bytes parsed, or when leaving
2224 * on missing data.
2225 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002226int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002227{
2228 struct session *sess = s->sess;
2229 struct http_txn *txn = s->txn;
2230 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002231 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002232 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002233
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002234 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002235
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002236 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002237
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002238 if (htx->flags & HTX_FL_PARSING_ERROR)
2239 goto return_bad_res;
2240 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2241 goto return_int_err;
2242
Christopher Faulete0768eb2018-10-03 16:38:02 +02002243 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002244 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002245 /* Output closed while we were sending data. We must abort and
2246 * wake the other side up.
2247 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002248 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002249 http_end_response(s);
2250 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002251 DBG_TRACE_DEVEL("leaving on error",
2252 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002253 return 1;
2254 }
2255
Christopher Faulet9768c262018-10-22 09:34:31 +02002256 if (msg->msg_state == HTTP_MSG_BODY)
2257 msg->msg_state = HTTP_MSG_DATA;
2258
Christopher Faulete0768eb2018-10-03 16:38:02 +02002259 /* in most states, we should abort in case of early close */
2260 channel_auto_close(res);
2261
Christopher Faulete0768eb2018-10-03 16:38:02 +02002262 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002263 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002264 if (res->flags & CF_EOI)
2265 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002266 }
2267 else {
2268 /* We can't process the buffer's contents yet */
2269 res->flags |= CF_WAKE_WRITE;
2270 goto missing_data_or_waiting;
2271 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002272 }
2273
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002274 if (msg->msg_state >= HTTP_MSG_ENDING)
2275 goto ending;
2276
2277 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2278 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2279 msg->msg_state = HTTP_MSG_ENDING;
2280 goto ending;
2281 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002282
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002283 /* Forward input data. We get it by removing all outgoing data not
2284 * forwarded yet from HTX data size. If there are some data filters, we
2285 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002286 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002287 if (HAS_RSP_DATA_FILTERS(s)) {
2288 ret = flt_http_payload(s, msg, htx->data);
2289 if (ret < 0)
2290 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002291 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002292 }
2293 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002294 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002295 if (msg->flags & HTTP_MSGF_XFER_LEN)
2296 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002297 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002298
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002299 if (htx->data != co_data(res))
2300 goto missing_data_or_waiting;
2301
2302 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2303 msg->msg_state = HTTP_MSG_ENDING;
2304 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002305 }
2306
Christopher Faulet9768c262018-10-22 09:34:31 +02002307 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002308 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2309 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002310 */
2311 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2312 goto missing_data_or_waiting;
2313
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002314 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002315
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002316 ending:
2317 /* other states, ENDING...TUNNEL */
2318 if (msg->msg_state >= HTTP_MSG_DONE)
2319 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002320
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002321 if (HAS_RSP_DATA_FILTERS(s)) {
2322 ret = flt_http_end(s, msg);
2323 if (ret <= 0) {
2324 if (!ret)
2325 goto missing_data_or_waiting;
2326 goto return_bad_res;
2327 }
2328 }
2329
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002330 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2331 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2332 msg->msg_state = HTTP_MSG_TUNNEL;
2333 goto ending;
2334 }
2335 else {
2336 msg->msg_state = HTTP_MSG_DONE;
2337 res->to_forward = 0;
2338 }
2339
2340 done:
2341
2342 channel_dont_close(res);
2343
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002344 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002345 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002346 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002347 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2348 if (res->flags & CF_SHUTW) {
2349 /* response errors are most likely due to the
2350 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002351 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002352 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002353 goto return_bad_res;
2354 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002355 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002356 return 1;
2357 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002358 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2359 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002360 return 0;
2361
2362 missing_data_or_waiting:
2363 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002364 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002365
2366 /* stop waiting for data if the input is closed before the end. If the
2367 * client side was already closed, it means that the client has aborted,
2368 * so we don't want to count this as a server abort. Otherwise it's a
2369 * server abort.
2370 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002371 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002372 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002373 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002374 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002375 if (htx_is_empty(htx))
2376 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002377 }
2378
Christopher Faulete0768eb2018-10-03 16:38:02 +02002379 /* When TE: chunked is used, we need to get there again to parse
2380 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002381 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2382 * are filters registered on the stream, we don't want to forward a
2383 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002384 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002385 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002386 channel_dont_close(res);
2387
2388 /* We know that more data are expected, but we couldn't send more that
2389 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2390 * system knows it must not set a PUSH on this first part. Interactive
2391 * modes are already handled by the stream sock layer. We must not do
2392 * this in content-length mode because it could present the MSG_MORE
2393 * flag with the last block of forwarded data, which would cause an
2394 * additional delay to be observed by the receiver.
2395 */
2396 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2397 res->flags |= CF_EXPECT_MORE;
2398
2399 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002400 DBG_TRACE_DEVEL("waiting for more data to forward",
2401 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002402 return 0;
2403
Christopher Faulet93e02d82019-03-08 14:18:50 +01002404 return_srv_abort:
2405 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2406 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002407 if (sess->listener->counters)
2408 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002409 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002410 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002411 if (!(s->flags & SF_ERR_MASK))
2412 s->flags |= SF_ERR_SRVCL;
2413 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002414
Christopher Faulet93e02d82019-03-08 14:18:50 +01002415 return_cli_abort:
2416 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2417 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002418 if (sess->listener->counters)
2419 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002420 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002421 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002422 if (!(s->flags & SF_ERR_MASK))
2423 s->flags |= SF_ERR_CLICL;
2424 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002425
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002426 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002427 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002428 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002429 if (sess->listener->counters)
2430 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002431 if (objt_server(s->target))
2432 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002433 if (!(s->flags & SF_ERR_MASK))
2434 s->flags |= SF_ERR_INTERNAL;
2435 goto return_error;
2436
Christopher Faulet93e02d82019-03-08 14:18:50 +01002437 return_bad_res:
2438 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2439 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002440 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002441 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2442 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002443 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002444 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002445 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002446
Christopher Faulet93e02d82019-03-08 14:18:50 +01002447 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002448 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002449 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002450 res->analysers &= AN_RES_FLT_END;
2451 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002452 if (!(s->flags & SF_FINST_MASK))
2453 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002454 DBG_TRACE_DEVEL("leaving on error",
2455 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002456 return 0;
2457}
2458
Christopher Fauletf2824e62018-10-01 12:12:37 +02002459/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002460 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002461 * as too large a request to build a valid response.
2462 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002463int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002464{
Christopher Faulet99daf282018-11-28 22:58:13 +01002465 struct channel *req = &s->req;
2466 struct channel *res = &s->res;
2467 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002468 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002469 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002470 struct ist status, reason, location;
2471 unsigned int flags;
2472 size_t data;
Christopher Faulet08e66462019-05-23 16:44:59 +02002473 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002474
2475 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002476 if (!chunk) {
2477 if (!(s->flags & SF_ERR_MASK))
2478 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002479 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002480 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002481
Christopher Faulet99daf282018-11-28 22:58:13 +01002482 /*
2483 * Create the location
2484 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002485 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002486 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002487 case REDIRECT_TYPE_SCHEME: {
2488 struct http_hdr_ctx ctx;
2489 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002490
Christopher Faulet99daf282018-11-28 22:58:13 +01002491 host = ist("");
2492 ctx.blk = NULL;
2493 if (http_find_header(htx, ist("Host"), &ctx, 0))
2494 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002495
Christopher Faulet297fbb42019-05-13 14:41:27 +02002496 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002497 path = http_get_path(htx_sl_req_uri(sl));
2498 /* build message using path */
2499 if (path.ptr) {
2500 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2501 int qs = 0;
2502 while (qs < path.len) {
2503 if (*(path.ptr + qs) == '?') {
2504 path.len = qs;
2505 break;
2506 }
2507 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002508 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002509 }
2510 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002511 else
2512 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002513
Christopher Faulet99daf282018-11-28 22:58:13 +01002514 if (rule->rdr_str) { /* this is an old "redirect" rule */
2515 /* add scheme */
2516 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2517 goto fail;
2518 }
2519 else {
2520 /* add scheme with executing log format */
2521 chunk->data += build_logline(s, chunk->area + chunk->data,
2522 chunk->size - chunk->data,
2523 &rule->rdr_fmt);
2524 }
2525 /* add "://" + host + path */
2526 if (!chunk_memcat(chunk, "://", 3) ||
2527 !chunk_memcat(chunk, host.ptr, host.len) ||
2528 !chunk_memcat(chunk, path.ptr, path.len))
2529 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002530
Christopher Faulet99daf282018-11-28 22:58:13 +01002531 /* append a slash at the end of the location if needed and missing */
2532 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2533 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2534 if (chunk->data + 1 >= chunk->size)
2535 goto fail;
2536 chunk->area[chunk->data++] = '/';
2537 }
2538 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002539 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002540
Christopher Faulet99daf282018-11-28 22:58:13 +01002541 case REDIRECT_TYPE_PREFIX: {
2542 struct ist path;
2543
Christopher Faulet297fbb42019-05-13 14:41:27 +02002544 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002545 path = http_get_path(htx_sl_req_uri(sl));
2546 /* build message using path */
2547 if (path.ptr) {
2548 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2549 int qs = 0;
2550 while (qs < path.len) {
2551 if (*(path.ptr + qs) == '?') {
2552 path.len = qs;
2553 break;
2554 }
2555 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002556 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002557 }
2558 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002559 else
2560 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002561
Christopher Faulet99daf282018-11-28 22:58:13 +01002562 if (rule->rdr_str) { /* this is an old "redirect" rule */
2563 /* add prefix. Note that if prefix == "/", we don't want to
2564 * add anything, otherwise it makes it hard for the user to
2565 * configure a self-redirection.
2566 */
2567 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2568 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2569 goto fail;
2570 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002571 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002572 else {
2573 /* add prefix with executing log format */
2574 chunk->data += build_logline(s, chunk->area + chunk->data,
2575 chunk->size - chunk->data,
2576 &rule->rdr_fmt);
2577 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002578
Christopher Faulet99daf282018-11-28 22:58:13 +01002579 /* add path */
2580 if (!chunk_memcat(chunk, path.ptr, path.len))
2581 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002582
Christopher Faulet99daf282018-11-28 22:58:13 +01002583 /* append a slash at the end of the location if needed and missing */
2584 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2585 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2586 if (chunk->data + 1 >= chunk->size)
2587 goto fail;
2588 chunk->area[chunk->data++] = '/';
2589 }
2590 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002591 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002592 case REDIRECT_TYPE_LOCATION:
2593 default:
2594 if (rule->rdr_str) { /* this is an old "redirect" rule */
2595 /* add location */
2596 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2597 goto fail;
2598 }
2599 else {
2600 /* add location with executing log format */
2601 chunk->data += build_logline(s, chunk->area + chunk->data,
2602 chunk->size - chunk->data,
2603 &rule->rdr_fmt);
2604 }
2605 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002606 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002607 location = ist2(chunk->area, chunk->data);
2608
2609 /*
2610 * Create the 30x response
2611 */
2612 switch (rule->code) {
2613 case 308:
2614 status = ist("308");
2615 reason = ist("Permanent Redirect");
2616 break;
2617 case 307:
2618 status = ist("307");
2619 reason = ist("Temporary Redirect");
2620 break;
2621 case 303:
2622 status = ist("303");
2623 reason = ist("See Other");
2624 break;
2625 case 301:
2626 status = ist("301");
2627 reason = ist("Moved Permanently");
2628 break;
2629 case 302:
2630 default:
2631 status = ist("302");
2632 reason = ist("Found");
2633 break;
2634 }
2635
Christopher Faulet08e66462019-05-23 16:44:59 +02002636 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2637 close = 1;
2638
Christopher Faulet99daf282018-11-28 22:58:13 +01002639 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002640 /* Trim any possible response */
2641 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002642 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2643 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2644 if (!sl)
2645 goto fail;
2646 sl->info.res.status = rule->code;
2647 s->txn->status = rule->code;
2648
Christopher Faulet08e66462019-05-23 16:44:59 +02002649 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2650 goto fail;
2651
2652 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002653 !htx_add_header(htx, ist("Location"), location))
2654 goto fail;
2655
2656 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2657 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2658 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002659 }
2660
2661 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002662 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2663 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002664 }
2665
Christopher Faulet99daf282018-11-28 22:58:13 +01002666 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2667 goto fail;
2668
Kevin Zhu96b36392020-01-07 09:42:55 +01002669 htx_to_buf(htx, &res->buf);
2670
Christopher Fauletf2824e62018-10-01 12:12:37 +02002671 /* let's log the request time */
2672 s->logs.tv_request = now;
2673
Christopher Faulet06511812019-10-16 09:38:27 +02002674 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet99daf282018-11-28 22:58:13 +01002675 data = htx->data - co_data(res);
Christopher Faulet99daf282018-11-28 22:58:13 +01002676 c_adv(res, data);
2677 res->total += data;
2678
2679 channel_auto_read(req);
2680 channel_abort(req);
2681 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002682 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet99daf282018-11-28 22:58:13 +01002683
2684 res->wex = tick_add_ifset(now_ms, res->wto);
2685 channel_auto_read(res);
2686 channel_auto_close(res);
2687 channel_shutr_now(res);
2688
2689 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002690
2691 if (!(s->flags & SF_ERR_MASK))
2692 s->flags |= SF_ERR_LOCAL;
2693 if (!(s->flags & SF_FINST_MASK))
2694 s->flags |= SF_FINST_R;
2695
Christopher Faulet99daf282018-11-28 22:58:13 +01002696 free_trash_chunk(chunk);
2697 return 1;
2698
2699 fail:
2700 /* If an error occurred, remove the incomplete HTTP response from the
2701 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002702 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002703 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002704 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002705}
2706
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002707/* Replace all headers matching the name <name>. The header value is replaced if
2708 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2709 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2710 * values are evaluated one by one. It returns 0 on success and -1 on error.
2711 */
2712int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2713 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002714{
2715 struct http_hdr_ctx ctx;
2716 struct buffer *output = get_trash_chunk();
2717
Christopher Faulet72333522018-10-24 11:25:02 +02002718 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002719 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002720 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2721 continue;
2722
2723 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2724 if (output->data == -1)
2725 return -1;
2726 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2727 return -1;
2728 }
2729 return 0;
2730}
2731
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002732/* Terminate a 103-Erly-hints response and send it to the client. It returns 0
2733 * on success and -1 on error. The response channel is updated accordingly.
2734 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002735static int http_reply_103_early_hints(struct channel *res)
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002736{
2737 struct htx *htx = htx_from_buf(&res->buf);
2738 size_t data;
2739
Christopher Faulet1d5ec092019-06-26 14:23:54 +02002740 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002741 /* If an error occurred during an Early-hint rule,
2742 * remove the incomplete HTTP 103 response from the
2743 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002744 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002745 return -1;
2746 }
2747
2748 data = htx->data - co_data(res);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002749 c_adv(res, data);
2750 res->total += data;
2751 return 0;
2752}
2753
Christopher Faulet6eb92892018-11-15 16:39:29 +01002754/*
2755 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
2756 * built according to <fmt> log line format.
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002757 * If <early_hints> is 0, it is starts a new response by adding the start
2758 * line. If an error occurred -1 is returned. On success 0 is returned. The
2759 * channel is not updated here. It must be done calling the function
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002760 * http_reply_103_early_hints().
Christopher Faulet6eb92892018-11-15 16:39:29 +01002761 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002762static int http_add_early_hint_header(struct stream *s, int early_hints, const struct ist name, struct list *fmt)
Christopher Faulet6eb92892018-11-15 16:39:29 +01002763{
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002764 struct channel *res = &s->res;
2765 struct htx *htx = htx_from_buf(&res->buf);
2766 struct buffer *value = alloc_trash_chunk();
2767
Christopher Fauletb8a53712019-12-16 11:29:38 +01002768 if (!value) {
2769 if (!(s->flags & SF_ERR_MASK))
2770 s->flags |= SF_ERR_RESOURCE;
2771 goto fail;
2772 }
2773
Christopher Faulet6eb92892018-11-15 16:39:29 +01002774 if (!early_hints) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002775 struct htx_sl *sl;
2776 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
2777 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2778
2779 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
2780 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
2781 if (!sl)
Christopher Faulet6eb92892018-11-15 16:39:29 +01002782 goto fail;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002783 sl->info.res.status = 103;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002784 }
2785
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002786 value->data = build_logline(s, b_tail(value), b_room(value), fmt);
2787 if (!htx_add_header(htx, name, ist2(b_head(value), b_data(value))))
Christopher Faulet6eb92892018-11-15 16:39:29 +01002788 goto fail;
2789
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002790 free_trash_chunk(value);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002791 return 1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002792
2793 fail:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002794 /* If an error occurred during an Early-hint rule, remove the incomplete
2795 * HTTP 103 response from the buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002796 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002797 free_trash_chunk(value);
2798 return -1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002799}
2800
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002801/* This function executes one of the set-{method,path,query,uri} actions. It
2802 * takes the string from the variable 'replace' with length 'len', then modifies
2803 * the relevant part of the request line accordingly. Then it updates various
2804 * pointers to the next elements which were moved, and the total buffer length.
2805 * It finds the action to be performed in p[2], previously filled by function
2806 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2807 * error, though this can be revisited when this code is finally exploited.
2808 *
2809 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2810 * query string and 3 to replace uri.
2811 *
2812 * In query string case, the mark question '?' must be set at the start of the
2813 * string by the caller, event if the replacement query string is empty.
2814 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002815int http_req_replace_stline(int action, const char *replace, int len,
2816 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002817{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002818 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002819
2820 switch (action) {
2821 case 0: // method
2822 if (!http_replace_req_meth(htx, ist2(replace, len)))
2823 return -1;
2824 break;
2825
2826 case 1: // path
2827 if (!http_replace_req_path(htx, ist2(replace, len)))
2828 return -1;
2829 break;
2830
2831 case 2: // query
2832 if (!http_replace_req_query(htx, ist2(replace, len)))
2833 return -1;
2834 break;
2835
2836 case 3: // uri
2837 if (!http_replace_req_uri(htx, ist2(replace, len)))
2838 return -1;
2839 break;
2840
2841 default:
2842 return -1;
2843 }
2844 return 0;
2845}
2846
2847/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002848 * variable <status> contains the new status code. This function never fails. It
2849 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002850 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002851int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002852{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002853 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002854 char *res;
2855
2856 chunk_reset(&trash);
2857 res = ultoa_o(status, trash.area, trash.size);
2858 trash.data = res - trash.area;
2859
2860 /* Do we have a custom reason format string? */
Christopher Faulet96bff762019-12-17 13:46:18 +01002861 if (reason.ptr == NULL) {
2862 const char *str = http_get_reason(status);
2863 reason = ist2(str, strlen(str));
2864 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002865
Christopher Faulete00d06c2019-12-16 17:18:42 +01002866 if (!http_replace_res_status(htx, ist2(trash.area, trash.data)))
2867 return -1;
Christopher Faulet96bff762019-12-17 13:46:18 +01002868 if (!http_replace_res_reason(htx, reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002869 return -1;
2870 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002871}
2872
Christopher Faulet3e964192018-10-24 11:39:23 +02002873/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2874 * transaction <txn>. Returns the verdict of the first rule that prevents
2875 * further processing of the request (auth, deny, ...), and defaults to
2876 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2877 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2878 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2879 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2880 * status.
2881 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002882static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
2883 struct stream *s, int *deny_status)
Christopher Faulet3e964192018-10-24 11:39:23 +02002884{
2885 struct session *sess = strm_sess(s);
2886 struct http_txn *txn = s->txn;
2887 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002888 struct act_rule *rule;
2889 struct http_hdr_ctx ctx;
2890 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002891 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002892 int act_opts = 0;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002893 int early_hints = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002894
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002895 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002896
2897 /* If "the current_rule_list" match the executed rule list, we are in
2898 * resume condition. If a resume is needed it is always in the action
2899 * and never in the ACL or converters. In this case, we initialise the
2900 * current rule, and go to the action execution point.
2901 */
2902 if (s->current_rule) {
2903 rule = s->current_rule;
2904 s->current_rule = NULL;
2905 if (s->current_rule_list == rules)
2906 goto resume_execution;
2907 }
2908 s->current_rule_list = rules;
2909
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002910 /* start the ruleset evaluation in strict mode */
2911 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002912
Christopher Faulet3e964192018-10-24 11:39:23 +02002913 list_for_each_entry(rule, rules, list) {
2914 /* check optional condition */
2915 if (rule->cond) {
2916 int ret;
2917
2918 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2919 ret = acl_pass(ret);
2920
2921 if (rule->cond->pol == ACL_COND_UNLESS)
2922 ret = !ret;
2923
2924 if (!ret) /* condition not matched */
2925 continue;
2926 }
2927
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002928 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002929 resume_execution:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002930 if (early_hints && rule->action != ACT_HTTP_EARLY_HINT) {
2931 early_hints = 0;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002932 if (http_reply_103_early_hints(&s->res) == -1) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002933 rule_ret = HTTP_RULE_RES_BADREQ;
2934 goto end;
2935 }
2936 }
2937
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002938 /* Always call the action function if defined */
2939 if (rule->action_ptr) {
2940 if ((s->req.flags & CF_READ_ERROR) ||
2941 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2942 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002943 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002944
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002945 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002946 case ACT_RET_CONT:
2947 break;
2948 case ACT_RET_STOP:
2949 rule_ret = HTTP_RULE_RES_STOP;
2950 goto end;
2951 case ACT_RET_YIELD:
2952 s->current_rule = rule;
2953 rule_ret = HTTP_RULE_RES_YIELD;
2954 goto end;
2955 case ACT_RET_ERR:
2956 rule_ret = HTTP_RULE_RES_ERROR;
2957 goto end;
2958 case ACT_RET_DONE:
2959 rule_ret = HTTP_RULE_RES_DONE;
2960 goto end;
2961 case ACT_RET_DENY:
2962 rule_ret = HTTP_RULE_RES_DENY;
2963 goto end;
2964 case ACT_RET_ABRT:
2965 rule_ret = HTTP_RULE_RES_ABRT;
2966 goto end;
2967 case ACT_RET_INV:
2968 rule_ret = HTTP_RULE_RES_BADREQ;
2969 goto end;
2970 }
2971 continue; /* eval the next rule */
2972 }
2973
2974 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002975 switch (rule->action) {
2976 case ACT_ACTION_ALLOW:
2977 rule_ret = HTTP_RULE_RES_STOP;
2978 goto end;
2979
2980 case ACT_ACTION_DENY:
2981 if (deny_status)
Christopher Faulet96bff762019-12-17 13:46:18 +01002982 *deny_status = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002983 rule_ret = HTTP_RULE_RES_DENY;
2984 goto end;
2985
2986 case ACT_HTTP_REQ_TARPIT:
2987 txn->flags |= TX_CLTARPIT;
2988 if (deny_status)
Christopher Faulet96bff762019-12-17 13:46:18 +01002989 *deny_status = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002990 rule_ret = HTTP_RULE_RES_DENY;
2991 goto end;
2992
2993 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002994 /* Auth might be performed on regular http-req rules as well as on stats */
Christopher Faulet96bff762019-12-17 13:46:18 +01002995 auth_realm = rule->arg.http.str.ptr;
Christopher Faulet3e964192018-10-24 11:39:23 +02002996 if (!auth_realm) {
2997 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2998 auth_realm = STATS_DEFAULT_REALM;
2999 else
3000 auth_realm = px->id;
3001 }
3002 /* send 401/407 depending on whether we use a proxy or not. We still
3003 * count one error, because normal browsing won't significantly
3004 * increase the counter but brute force attempts will.
3005 */
Christopher Faulet3e964192018-10-24 11:39:23 +02003006 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003007 if (http_reply_40x_unauthorized(s, auth_realm) == -1)
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003008 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet12c51e22018-11-28 15:59:42 +01003009 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02003010 goto end;
3011
3012 case ACT_HTTP_REDIR:
Christopher Faulet3e964192018-10-24 11:39:23 +02003013 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003014 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003015 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003016 goto end;
3017
3018 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003019 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003020 break;
3021
3022 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003023 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003024 break;
3025
3026 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003027 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003028 break;
3029
3030 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003031 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003032 break;
3033
Christopher Faulet3e964192018-10-24 11:39:23 +02003034 case ACT_HTTP_DEL_HDR:
3035 /* remove all occurrences of the header */
3036 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01003037 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02003038 http_remove_header(htx, &ctx);
3039 break;
3040
Christopher Faulet3e964192018-10-24 11:39:23 +02003041 case ACT_HTTP_DEL_ACL:
3042 case ACT_HTTP_DEL_MAP: {
3043 struct pat_ref *ref;
3044 struct buffer *key;
3045
3046 /* collect reference */
3047 ref = pat_ref_lookup(rule->arg.map.ref);
3048 if (!ref)
3049 continue;
3050
3051 /* allocate key */
3052 key = alloc_trash_chunk();
3053 if (!key) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003054 if (!(s->flags & SF_ERR_MASK))
3055 s->flags |= SF_ERR_RESOURCE;
3056 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003057 goto end;
3058 }
3059
3060 /* collect key */
3061 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3062 key->area[key->data] = '\0';
3063
3064 /* perform update */
3065 /* returned code: 1=ok, 0=ko */
3066 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3067 pat_ref_delete(ref, key->area);
3068 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3069
3070 free_trash_chunk(key);
3071 break;
3072 }
3073
3074 case ACT_HTTP_ADD_ACL: {
3075 struct pat_ref *ref;
3076 struct buffer *key;
3077
3078 /* collect reference */
3079 ref = pat_ref_lookup(rule->arg.map.ref);
3080 if (!ref)
3081 continue;
3082
3083 /* allocate key */
3084 key = alloc_trash_chunk();
3085 if (!key) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003086 if (!(s->flags & SF_ERR_MASK))
3087 s->flags |= SF_ERR_RESOURCE;
3088 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003089 goto end;
3090 }
3091
3092 /* collect key */
3093 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3094 key->area[key->data] = '\0';
3095
3096 /* perform update */
3097 /* add entry only if it does not already exist */
3098 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3099 if (pat_ref_find_elt(ref, key->area) == NULL)
3100 pat_ref_add(ref, key->area, NULL, NULL);
3101 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3102
3103 free_trash_chunk(key);
3104 break;
3105 }
3106
3107 case ACT_HTTP_SET_MAP: {
3108 struct pat_ref *ref;
3109 struct buffer *key, *value;
3110
3111 /* collect reference */
3112 ref = pat_ref_lookup(rule->arg.map.ref);
3113 if (!ref)
3114 continue;
3115
3116 /* allocate key */
3117 key = alloc_trash_chunk();
3118 if (!key) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003119 if (!(s->flags & SF_ERR_MASK))
3120 s->flags |= SF_ERR_RESOURCE;
3121 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003122 goto end;
3123 }
3124
3125 /* allocate value */
3126 value = alloc_trash_chunk();
3127 if (!value) {
3128 free_trash_chunk(key);
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003129 if (!(s->flags & SF_ERR_MASK))
3130 s->flags |= SF_ERR_RESOURCE;
3131 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003132 goto end;
3133 }
3134
3135 /* collect key */
3136 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3137 key->area[key->data] = '\0';
3138
3139 /* collect value */
3140 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3141 value->area[value->data] = '\0';
3142
3143 /* perform update */
Christopher Faulete84289e2019-04-19 14:50:55 +02003144 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003145 if (pat_ref_find_elt(ref, key->area) != NULL)
3146 /* update entry if it exists */
3147 pat_ref_set(ref, key->area, value->area, NULL);
3148 else
3149 /* insert a new entry */
3150 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02003151 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003152 free_trash_chunk(key);
3153 free_trash_chunk(value);
3154 break;
3155 }
3156
3157 case ACT_HTTP_EARLY_HINT:
3158 if (!(txn->req.flags & HTTP_MSGF_VER_11))
3159 break;
Christopher Faulet96bff762019-12-17 13:46:18 +01003160 early_hints = http_add_early_hint_header(s, early_hints, rule->arg.http.str, &rule->arg.http.fmt);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003161 if (early_hints == -1) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003162 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003163 goto end;
3164 }
3165 break;
3166
Christopher Faulet3e964192018-10-24 11:39:23 +02003167 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3168 /* Note: only the first valid tracking parameter of each
3169 * applies.
3170 */
3171
3172 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3173 struct stktable *t;
3174 struct stksess *ts;
3175 struct stktable_key *key;
3176 void *ptr1, *ptr2;
3177
3178 t = rule->arg.trk_ctr.table.t;
3179 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL,
3180 rule->arg.trk_ctr.expr, NULL);
3181
3182 if (key && (ts = stktable_get_entry(t, key))) {
3183 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3184
3185 /* let's count a new HTTP request as it's the first time we do it */
3186 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3187 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3188 if (ptr1 || ptr2) {
3189 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3190
3191 if (ptr1)
3192 stktable_data_cast(ptr1, http_req_cnt)++;
3193
3194 if (ptr2)
3195 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
3196 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3197
3198 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3199
3200 /* If data was modified, we need to touch to re-schedule sync */
3201 stktable_touch_local(t, ts, 0);
3202 }
3203
3204 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3205 if (sess->fe != s->be)
3206 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3207 }
3208 }
3209 break;
3210
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003211 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003212 default:
3213 break;
3214 }
3215 }
3216
3217 end:
3218 if (early_hints) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003219 if (http_reply_103_early_hints(&s->res) == -1)
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003220 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003221 }
3222
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003223 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003224 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003225 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003226
Christopher Faulet3e964192018-10-24 11:39:23 +02003227 /* we reached the end of the rules, nothing to report */
3228 return rule_ret;
3229}
3230
3231/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3232 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3233 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3234 * is returned, the process can continue the evaluation of next rule list. If
3235 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3236 * is returned, it means the operation could not be processed and a server error
3237 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3238 * deny rule. If *YIELD is returned, the caller must call again the function
3239 * with the same context.
3240 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003241static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
3242 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02003243{
3244 struct session *sess = strm_sess(s);
3245 struct http_txn *txn = s->txn;
3246 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02003247 struct act_rule *rule;
3248 struct http_hdr_ctx ctx;
3249 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003250 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02003251
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003252 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02003253
3254 /* If "the current_rule_list" match the executed rule list, we are in
3255 * resume condition. If a resume is needed it is always in the action
3256 * and never in the ACL or converters. In this case, we initialise the
3257 * current rule, and go to the action execution point.
3258 */
3259 if (s->current_rule) {
3260 rule = s->current_rule;
3261 s->current_rule = NULL;
3262 if (s->current_rule_list == rules)
3263 goto resume_execution;
3264 }
3265 s->current_rule_list = rules;
3266
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003267 /* start the ruleset evaluation in strict mode */
3268 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003269
Christopher Faulet3e964192018-10-24 11:39:23 +02003270 list_for_each_entry(rule, rules, list) {
3271 /* check optional condition */
3272 if (rule->cond) {
3273 int ret;
3274
3275 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3276 ret = acl_pass(ret);
3277
3278 if (rule->cond->pol == ACL_COND_UNLESS)
3279 ret = !ret;
3280
3281 if (!ret) /* condition not matched */
3282 continue;
3283 }
3284
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003285 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02003286resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003287
3288 /* Always call the action function if defined */
3289 if (rule->action_ptr) {
3290 if ((s->req.flags & CF_READ_ERROR) ||
3291 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3292 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003293 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003294
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003295 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003296 case ACT_RET_CONT:
3297 break;
3298 case ACT_RET_STOP:
3299 rule_ret = HTTP_RULE_RES_STOP;
3300 goto end;
3301 case ACT_RET_YIELD:
3302 s->current_rule = rule;
3303 rule_ret = HTTP_RULE_RES_YIELD;
3304 goto end;
3305 case ACT_RET_ERR:
3306 rule_ret = HTTP_RULE_RES_ERROR;
3307 goto end;
3308 case ACT_RET_DONE:
3309 rule_ret = HTTP_RULE_RES_DONE;
3310 goto end;
3311 case ACT_RET_DENY:
3312 rule_ret = HTTP_RULE_RES_DENY;
3313 goto end;
3314 case ACT_RET_ABRT:
3315 rule_ret = HTTP_RULE_RES_ABRT;
3316 goto end;
3317 case ACT_RET_INV:
3318 rule_ret = HTTP_RULE_RES_BADREQ;
3319 goto end;
3320 }
3321 continue; /* eval the next rule */
3322 }
3323
3324 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003325 switch (rule->action) {
3326 case ACT_ACTION_ALLOW:
3327 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3328 goto end;
3329
3330 case ACT_ACTION_DENY:
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003331 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003332 goto end;
3333
3334 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003335 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003336 break;
3337
3338 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003339 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003340 break;
3341
3342 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003343 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003344 break;
3345
3346 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003347 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003348 break;
3349
Christopher Faulet3e964192018-10-24 11:39:23 +02003350 case ACT_HTTP_DEL_HDR:
3351 /* remove all occurrences of the header */
3352 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01003353 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02003354 http_remove_header(htx, &ctx);
3355 break;
3356
Christopher Faulet3e964192018-10-24 11:39:23 +02003357 case ACT_HTTP_DEL_ACL:
3358 case ACT_HTTP_DEL_MAP: {
3359 struct pat_ref *ref;
3360 struct buffer *key;
3361
3362 /* collect reference */
3363 ref = pat_ref_lookup(rule->arg.map.ref);
3364 if (!ref)
3365 continue;
3366
3367 /* allocate key */
3368 key = alloc_trash_chunk();
3369 if (!key) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003370 if (!(s->flags & SF_ERR_MASK))
3371 s->flags |= SF_ERR_RESOURCE;
3372 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003373 goto end;
3374 }
3375
3376 /* collect key */
3377 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3378 key->area[key->data] = '\0';
3379
3380 /* perform update */
3381 /* returned code: 1=ok, 0=ko */
3382 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3383 pat_ref_delete(ref, key->area);
3384 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3385
3386 free_trash_chunk(key);
3387 break;
3388 }
3389
3390 case ACT_HTTP_ADD_ACL: {
3391 struct pat_ref *ref;
3392 struct buffer *key;
3393
3394 /* collect reference */
3395 ref = pat_ref_lookup(rule->arg.map.ref);
3396 if (!ref)
3397 continue;
3398
3399 /* allocate key */
3400 key = alloc_trash_chunk();
3401 if (!key) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003402 if (!(s->flags & SF_ERR_MASK))
3403 s->flags |= SF_ERR_RESOURCE;
3404 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003405 goto end;
3406 }
3407
3408 /* collect key */
3409 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3410 key->area[key->data] = '\0';
3411
3412 /* perform update */
3413 /* check if the entry already exists */
Christopher Faulete84289e2019-04-19 14:50:55 +02003414 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003415 if (pat_ref_find_elt(ref, key->area) == NULL)
3416 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02003417 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003418 free_trash_chunk(key);
3419 break;
3420 }
3421
3422 case ACT_HTTP_SET_MAP: {
3423 struct pat_ref *ref;
3424 struct buffer *key, *value;
3425
3426 /* collect reference */
3427 ref = pat_ref_lookup(rule->arg.map.ref);
3428 if (!ref)
3429 continue;
3430
3431 /* allocate key */
3432 key = alloc_trash_chunk();
3433 if (!key) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003434 if (!(s->flags & SF_ERR_MASK))
3435 s->flags |= SF_ERR_RESOURCE;
3436 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003437 goto end;
3438 }
3439
3440 /* allocate value */
3441 value = alloc_trash_chunk();
3442 if (!value) {
3443 free_trash_chunk(key);
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003444 if (!(s->flags & SF_ERR_MASK))
3445 s->flags |= SF_ERR_RESOURCE;
3446 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003447 goto end;
3448 }
3449
3450 /* collect key */
3451 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3452 key->area[key->data] = '\0';
3453
3454 /* collect value */
3455 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3456 value->area[value->data] = '\0';
3457
3458 /* perform update */
3459 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3460 if (pat_ref_find_elt(ref, key->area) != NULL)
3461 /* update entry if it exists */
3462 pat_ref_set(ref, key->area, value->area, NULL);
3463 else
3464 /* insert a new entry */
3465 pat_ref_add(ref, key->area, value->area, NULL);
3466 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3467 free_trash_chunk(key);
3468 free_trash_chunk(value);
3469 break;
3470 }
3471
3472 case ACT_HTTP_REDIR:
3473 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003474 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003475 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003476 goto end;
3477
3478 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3479 /* Note: only the first valid tracking parameter of each
3480 * applies.
3481 */
3482 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3483 struct stktable *t;
3484 struct stksess *ts;
3485 struct stktable_key *key;
3486 void *ptr;
3487
3488 t = rule->arg.trk_ctr.table.t;
3489 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL,
3490 rule->arg.trk_ctr.expr, NULL);
3491
3492 if (key && (ts = stktable_get_entry(t, key))) {
3493 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3494
3495 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3496
3497 /* let's count a new HTTP request as it's the first time we do it */
3498 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3499 if (ptr)
3500 stktable_data_cast(ptr, http_req_cnt)++;
3501
3502 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3503 if (ptr)
3504 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3505 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3506
3507 /* When the client triggers a 4xx from the server, it's most often due
3508 * to a missing object or permission. These events should be tracked
3509 * because if they happen often, it may indicate a brute force or a
3510 * vulnerability scan. Normally this is done when receiving the response
3511 * but here we're tracking after this ought to have been done so we have
3512 * to do it on purpose.
3513 */
3514 if ((unsigned)(txn->status - 400) < 100) {
3515 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3516 if (ptr)
3517 stktable_data_cast(ptr, http_err_cnt)++;
3518
3519 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3520 if (ptr)
3521 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3522 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3523 }
3524
3525 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3526
3527 /* If data was modified, we need to touch to re-schedule sync */
3528 stktable_touch_local(t, ts, 0);
3529
3530 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3531 if (sess->fe != s->be)
3532 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3533 }
3534 }
3535 break;
3536
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003537 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003538 default:
3539 break;
3540 }
3541 }
3542
3543 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003544 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003545 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003546 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003547
Christopher Faulet3e964192018-10-24 11:39:23 +02003548 /* we reached the end of the rules, nothing to report */
3549 return rule_ret;
3550}
3551
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003552/*
3553 * Manage client-side cookie. It can impact performance by about 2% so it is
3554 * desirable to call it only when needed. This code is quite complex because
3555 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3556 * highly recommended not to touch this part without a good reason !
3557 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003558static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003559{
3560 struct session *sess = s->sess;
3561 struct http_txn *txn = s->txn;
3562 struct htx *htx;
3563 struct http_hdr_ctx ctx;
3564 char *hdr_beg, *hdr_end, *del_from;
3565 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3566 int preserve_hdr;
3567
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003568 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003569 ctx.blk = NULL;
3570 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003571 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003572 del_from = NULL; /* nothing to be deleted */
3573 preserve_hdr = 0; /* assume we may kill the whole header */
3574
3575 /* Now look for cookies. Conforming to RFC2109, we have to support
3576 * attributes whose name begin with a '$', and associate them with
3577 * the right cookie, if we want to delete this cookie.
3578 * So there are 3 cases for each cookie read :
3579 * 1) it's a special attribute, beginning with a '$' : ignore it.
3580 * 2) it's a server id cookie that we *MAY* want to delete : save
3581 * some pointers on it (last semi-colon, beginning of cookie...)
3582 * 3) it's an application cookie : we *MAY* have to delete a previous
3583 * "special" cookie.
3584 * At the end of loop, if a "special" cookie remains, we may have to
3585 * remove it. If no application cookie persists in the header, we
3586 * *MUST* delete it.
3587 *
3588 * Note: RFC2965 is unclear about the processing of spaces around
3589 * the equal sign in the ATTR=VALUE form. A careful inspection of
3590 * the RFC explicitly allows spaces before it, and not within the
3591 * tokens (attrs or values). An inspection of RFC2109 allows that
3592 * too but section 10.1.3 lets one think that spaces may be allowed
3593 * after the equal sign too, resulting in some (rare) buggy
3594 * implementations trying to do that. So let's do what servers do.
3595 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3596 * allowed quoted strings in values, with any possible character
3597 * after a backslash, including control chars and delimitors, which
3598 * causes parsing to become ambiguous. Browsers also allow spaces
3599 * within values even without quotes.
3600 *
3601 * We have to keep multiple pointers in order to support cookie
3602 * removal at the beginning, middle or end of header without
3603 * corrupting the header. All of these headers are valid :
3604 *
3605 * hdr_beg hdr_end
3606 * | |
3607 * v |
3608 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3609 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3610 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3611 * | | | | | | |
3612 * | | | | | | |
3613 * | | | | | | +--> next
3614 * | | | | | +----> val_end
3615 * | | | | +-----------> val_beg
3616 * | | | +--------------> equal
3617 * | | +----------------> att_end
3618 * | +---------------------> att_beg
3619 * +--------------------------> prev
3620 *
3621 */
3622 hdr_beg = ctx.value.ptr;
3623 hdr_end = hdr_beg + ctx.value.len;
3624 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3625 /* Iterate through all cookies on this line */
3626
3627 /* find att_beg */
3628 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003629 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003630 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003631 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003632
3633 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3634 att_beg++;
3635
3636 /* find att_end : this is the first character after the last non
3637 * space before the equal. It may be equal to hdr_end.
3638 */
3639 equal = att_end = att_beg;
3640 while (equal < hdr_end) {
3641 if (*equal == '=' || *equal == ',' || *equal == ';')
3642 break;
3643 if (HTTP_IS_SPHT(*equal++))
3644 continue;
3645 att_end = equal;
3646 }
3647
3648 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3649 * is between <att_beg> and <equal>, both may be identical.
3650 */
3651 /* look for end of cookie if there is an equal sign */
3652 if (equal < hdr_end && *equal == '=') {
3653 /* look for the beginning of the value */
3654 val_beg = equal + 1;
3655 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3656 val_beg++;
3657
3658 /* find the end of the value, respecting quotes */
3659 next = http_find_cookie_value_end(val_beg, hdr_end);
3660
3661 /* make val_end point to the first white space or delimitor after the value */
3662 val_end = next;
3663 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3664 val_end--;
3665 }
3666 else
3667 val_beg = val_end = next = equal;
3668
3669 /* We have nothing to do with attributes beginning with
3670 * '$'. However, they will automatically be removed if a
3671 * header before them is removed, since they're supposed
3672 * to be linked together.
3673 */
3674 if (*att_beg == '$')
3675 continue;
3676
3677 /* Ignore cookies with no equal sign */
3678 if (equal == next) {
3679 /* This is not our cookie, so we must preserve it. But if we already
3680 * scheduled another cookie for removal, we cannot remove the
3681 * complete header, but we can remove the previous block itself.
3682 */
3683 preserve_hdr = 1;
3684 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003685 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003686 val_end += delta;
3687 next += delta;
3688 hdr_end += delta;
3689 prev = del_from;
3690 del_from = NULL;
3691 }
3692 continue;
3693 }
3694
3695 /* if there are spaces around the equal sign, we need to
3696 * strip them otherwise we'll get trouble for cookie captures,
3697 * or even for rewrites. Since this happens extremely rarely,
3698 * it does not hurt performance.
3699 */
3700 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3701 int stripped_before = 0;
3702 int stripped_after = 0;
3703
3704 if (att_end != equal) {
3705 memmove(att_end, equal, hdr_end - equal);
3706 stripped_before = (att_end - equal);
3707 equal += stripped_before;
3708 val_beg += stripped_before;
3709 }
3710
3711 if (val_beg > equal + 1) {
3712 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3713 stripped_after = (equal + 1) - val_beg;
3714 val_beg += stripped_after;
3715 stripped_before += stripped_after;
3716 }
3717
3718 val_end += stripped_before;
3719 next += stripped_before;
3720 hdr_end += stripped_before;
3721 }
3722 /* now everything is as on the diagram above */
3723
3724 /* First, let's see if we want to capture this cookie. We check
3725 * that we don't already have a client side cookie, because we
3726 * can only capture one. Also as an optimisation, we ignore
3727 * cookies shorter than the declared name.
3728 */
3729 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3730 (val_end - att_beg >= sess->fe->capture_namelen) &&
3731 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3732 int log_len = val_end - att_beg;
3733
3734 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3735 ha_alert("HTTP logging : out of memory.\n");
3736 } else {
3737 if (log_len > sess->fe->capture_len)
3738 log_len = sess->fe->capture_len;
3739 memcpy(txn->cli_cookie, att_beg, log_len);
3740 txn->cli_cookie[log_len] = 0;
3741 }
3742 }
3743
3744 /* Persistence cookies in passive, rewrite or insert mode have the
3745 * following form :
3746 *
3747 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3748 *
3749 * For cookies in prefix mode, the form is :
3750 *
3751 * Cookie: NAME=SRV~VALUE
3752 */
3753 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3754 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3755 struct server *srv = s->be->srv;
3756 char *delim;
3757
3758 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3759 * have the server ID between val_beg and delim, and the original cookie between
3760 * delim+1 and val_end. Otherwise, delim==val_end :
3761 *
3762 * hdr_beg
3763 * |
3764 * v
3765 * NAME=SRV; # in all but prefix modes
3766 * NAME=SRV~OPAQUE ; # in prefix mode
3767 * || || | |+-> next
3768 * || || | +--> val_end
3769 * || || +---------> delim
3770 * || |+------------> val_beg
3771 * || +-------------> att_end = equal
3772 * |+-----------------> att_beg
3773 * +------------------> prev
3774 *
3775 */
3776 if (s->be->ck_opts & PR_CK_PFX) {
3777 for (delim = val_beg; delim < val_end; delim++)
3778 if (*delim == COOKIE_DELIM)
3779 break;
3780 }
3781 else {
3782 char *vbar1;
3783 delim = val_end;
3784 /* Now check if the cookie contains a date field, which would
3785 * appear after a vertical bar ('|') just after the server name
3786 * and before the delimiter.
3787 */
3788 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3789 if (vbar1) {
3790 /* OK, so left of the bar is the server's cookie and
3791 * right is the last seen date. It is a base64 encoded
3792 * 30-bit value representing the UNIX date since the
3793 * epoch in 4-second quantities.
3794 */
3795 int val;
3796 delim = vbar1++;
3797 if (val_end - vbar1 >= 5) {
3798 val = b64tos30(vbar1);
3799 if (val > 0)
3800 txn->cookie_last_date = val << 2;
3801 }
3802 /* look for a second vertical bar */
3803 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3804 if (vbar1 && (val_end - vbar1 > 5)) {
3805 val = b64tos30(vbar1 + 1);
3806 if (val > 0)
3807 txn->cookie_first_date = val << 2;
3808 }
3809 }
3810 }
3811
3812 /* if the cookie has an expiration date and the proxy wants to check
3813 * it, then we do that now. We first check if the cookie is too old,
3814 * then only if it has expired. We detect strict overflow because the
3815 * time resolution here is not great (4 seconds). Cookies with dates
3816 * in the future are ignored if their offset is beyond one day. This
3817 * allows an admin to fix timezone issues without expiring everyone
3818 * and at the same time avoids keeping unwanted side effects for too
3819 * long.
3820 */
3821 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3822 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3823 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3824 txn->flags &= ~TX_CK_MASK;
3825 txn->flags |= TX_CK_OLD;
3826 delim = val_beg; // let's pretend we have not found the cookie
3827 txn->cookie_first_date = 0;
3828 txn->cookie_last_date = 0;
3829 }
3830 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3831 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3832 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3833 txn->flags &= ~TX_CK_MASK;
3834 txn->flags |= TX_CK_EXPIRED;
3835 delim = val_beg; // let's pretend we have not found the cookie
3836 txn->cookie_first_date = 0;
3837 txn->cookie_last_date = 0;
3838 }
3839
3840 /* Here, we'll look for the first running server which supports the cookie.
3841 * This allows to share a same cookie between several servers, for example
3842 * to dedicate backup servers to specific servers only.
3843 * However, to prevent clients from sticking to cookie-less backup server
3844 * when they have incidentely learned an empty cookie, we simply ignore
3845 * empty cookies and mark them as invalid.
3846 * The same behaviour is applied when persistence must be ignored.
3847 */
3848 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3849 srv = NULL;
3850
3851 while (srv) {
3852 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3853 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3854 if ((srv->cur_state != SRV_ST_STOPPED) ||
3855 (s->be->options & PR_O_PERSIST) ||
3856 (s->flags & SF_FORCE_PRST)) {
3857 /* we found the server and we can use it */
3858 txn->flags &= ~TX_CK_MASK;
3859 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3860 s->flags |= SF_DIRECT | SF_ASSIGNED;
3861 s->target = &srv->obj_type;
3862 break;
3863 } else {
3864 /* we found a server, but it's down,
3865 * mark it as such and go on in case
3866 * another one is available.
3867 */
3868 txn->flags &= ~TX_CK_MASK;
3869 txn->flags |= TX_CK_DOWN;
3870 }
3871 }
3872 srv = srv->next;
3873 }
3874
3875 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3876 /* no server matched this cookie or we deliberately skipped it */
3877 txn->flags &= ~TX_CK_MASK;
3878 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3879 txn->flags |= TX_CK_UNUSED;
3880 else
3881 txn->flags |= TX_CK_INVALID;
3882 }
3883
3884 /* depending on the cookie mode, we may have to either :
3885 * - delete the complete cookie if we're in insert+indirect mode, so that
3886 * the server never sees it ;
3887 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003888 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003889 * if we're in cookie prefix mode
3890 */
3891 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3892 int delta; /* negative */
3893
3894 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3895 delta = val_beg - (delim + 1);
3896 val_end += delta;
3897 next += delta;
3898 hdr_end += delta;
3899 del_from = NULL;
3900 preserve_hdr = 1; /* we want to keep this cookie */
3901 }
3902 else if (del_from == NULL &&
3903 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3904 del_from = prev;
3905 }
3906 }
3907 else {
3908 /* This is not our cookie, so we must preserve it. But if we already
3909 * scheduled another cookie for removal, we cannot remove the
3910 * complete header, but we can remove the previous block itself.
3911 */
3912 preserve_hdr = 1;
3913
3914 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003915 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003916 if (att_beg >= del_from)
3917 att_beg += delta;
3918 if (att_end >= del_from)
3919 att_end += delta;
3920 val_beg += delta;
3921 val_end += delta;
3922 next += delta;
3923 hdr_end += delta;
3924 prev = del_from;
3925 del_from = NULL;
3926 }
3927 }
3928
3929 /* continue with next cookie on this header line */
3930 att_beg = next;
3931 } /* for each cookie */
3932
3933
3934 /* There are no more cookies on this line.
3935 * We may still have one (or several) marked for deletion at the
3936 * end of the line. We must do this now in two ways :
3937 * - if some cookies must be preserved, we only delete from the
3938 * mark to the end of line ;
3939 * - if nothing needs to be preserved, simply delete the whole header
3940 */
3941 if (del_from) {
3942 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3943 }
3944 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003945 if (hdr_beg != hdr_end)
3946 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003947 else
3948 http_remove_header(htx, &ctx);
3949 }
3950 } /* for each "Cookie header */
3951}
3952
3953/*
3954 * Manage server-side cookies. It can impact performance by about 2% so it is
3955 * desirable to call it only when needed. This function is also used when we
3956 * just need to know if there is a cookie (eg: for check-cache).
3957 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003958static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003959{
3960 struct session *sess = s->sess;
3961 struct http_txn *txn = s->txn;
3962 struct htx *htx;
3963 struct http_hdr_ctx ctx;
3964 struct server *srv;
3965 char *hdr_beg, *hdr_end;
3966 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003967 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003968
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003969 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003970
3971 ctx.blk = NULL;
3972 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003973 int is_first = 1;
3974
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003975 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3976 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3977 break;
3978 is_cookie2 = 1;
3979 }
3980
3981 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3982 * <prev> points to the colon.
3983 */
3984 txn->flags |= TX_SCK_PRESENT;
3985
3986 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3987 * check-cache is enabled) and we are not interested in checking
3988 * them. Warning, the cookie capture is declared in the frontend.
3989 */
3990 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3991 break;
3992
3993 /* OK so now we know we have to process this response cookie.
3994 * The format of the Set-Cookie header is slightly different
3995 * from the format of the Cookie header in that it does not
3996 * support the comma as a cookie delimiter (thus the header
3997 * cannot be folded) because the Expires attribute described in
3998 * the original Netscape's spec may contain an unquoted date
3999 * with a comma inside. We have to live with this because
4000 * many browsers don't support Max-Age and some browsers don't
4001 * support quoted strings. However the Set-Cookie2 header is
4002 * clean.
4003 *
4004 * We have to keep multiple pointers in order to support cookie
4005 * removal at the beginning, middle or end of header without
4006 * corrupting the header (in case of set-cookie2). A special
4007 * pointer, <scav> points to the beginning of the set-cookie-av
4008 * fields after the first semi-colon. The <next> pointer points
4009 * either to the end of line (set-cookie) or next unquoted comma
4010 * (set-cookie2). All of these headers are valid :
4011 *
4012 * hdr_beg hdr_end
4013 * | |
4014 * v |
4015 * NAME1 = VALUE 1 ; Secure; Path="/" |
4016 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
4017 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
4018 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
4019 * | | | | | | | |
4020 * | | | | | | | +-> next
4021 * | | | | | | +------------> scav
4022 * | | | | | +--------------> val_end
4023 * | | | | +--------------------> val_beg
4024 * | | | +----------------------> equal
4025 * | | +------------------------> att_end
4026 * | +----------------------------> att_beg
4027 * +------------------------------> prev
4028 * -------------------------------> hdr_beg
4029 */
4030 hdr_beg = ctx.value.ptr;
4031 hdr_end = hdr_beg + ctx.value.len;
4032 for (prev = hdr_beg; prev < hdr_end; prev = next) {
4033
4034 /* Iterate through all cookies on this line */
4035
4036 /* find att_beg */
4037 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02004038 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004039 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02004040 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004041
4042 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
4043 att_beg++;
4044
4045 /* find att_end : this is the first character after the last non
4046 * space before the equal. It may be equal to hdr_end.
4047 */
4048 equal = att_end = att_beg;
4049
4050 while (equal < hdr_end) {
4051 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
4052 break;
4053 if (HTTP_IS_SPHT(*equal++))
4054 continue;
4055 att_end = equal;
4056 }
4057
4058 /* here, <equal> points to '=', a delimitor or the end. <att_end>
4059 * is between <att_beg> and <equal>, both may be identical.
4060 */
4061
4062 /* look for end of cookie if there is an equal sign */
4063 if (equal < hdr_end && *equal == '=') {
4064 /* look for the beginning of the value */
4065 val_beg = equal + 1;
4066 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
4067 val_beg++;
4068
4069 /* find the end of the value, respecting quotes */
4070 next = http_find_cookie_value_end(val_beg, hdr_end);
4071
4072 /* make val_end point to the first white space or delimitor after the value */
4073 val_end = next;
4074 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
4075 val_end--;
4076 }
4077 else {
4078 /* <equal> points to next comma, semi-colon or EOL */
4079 val_beg = val_end = next = equal;
4080 }
4081
4082 if (next < hdr_end) {
4083 /* Set-Cookie2 supports multiple cookies, and <next> points to
4084 * a colon or semi-colon before the end. So skip all attr-value
4085 * pairs and look for the next comma. For Set-Cookie, since
4086 * commas are permitted in values, skip to the end.
4087 */
4088 if (is_cookie2)
4089 next = http_find_hdr_value_end(next, hdr_end);
4090 else
4091 next = hdr_end;
4092 }
4093
4094 /* Now everything is as on the diagram above */
4095
4096 /* Ignore cookies with no equal sign */
4097 if (equal == val_end)
4098 continue;
4099
4100 /* If there are spaces around the equal sign, we need to
4101 * strip them otherwise we'll get trouble for cookie captures,
4102 * or even for rewrites. Since this happens extremely rarely,
4103 * it does not hurt performance.
4104 */
4105 if (unlikely(att_end != equal || val_beg > equal + 1)) {
4106 int stripped_before = 0;
4107 int stripped_after = 0;
4108
4109 if (att_end != equal) {
4110 memmove(att_end, equal, hdr_end - equal);
4111 stripped_before = (att_end - equal);
4112 equal += stripped_before;
4113 val_beg += stripped_before;
4114 }
4115
4116 if (val_beg > equal + 1) {
4117 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
4118 stripped_after = (equal + 1) - val_beg;
4119 val_beg += stripped_after;
4120 stripped_before += stripped_after;
4121 }
4122
4123 val_end += stripped_before;
4124 next += stripped_before;
4125 hdr_end += stripped_before;
4126
Christopher Faulet3e2638e2019-06-18 09:49:16 +02004127 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004128 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004129 }
4130
4131 /* First, let's see if we want to capture this cookie. We check
4132 * that we don't already have a server side cookie, because we
4133 * can only capture one. Also as an optimisation, we ignore
4134 * cookies shorter than the declared name.
4135 */
4136 if (sess->fe->capture_name != NULL &&
4137 txn->srv_cookie == NULL &&
4138 (val_end - att_beg >= sess->fe->capture_namelen) &&
4139 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
4140 int log_len = val_end - att_beg;
4141 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
4142 ha_alert("HTTP logging : out of memory.\n");
4143 }
4144 else {
4145 if (log_len > sess->fe->capture_len)
4146 log_len = sess->fe->capture_len;
4147 memcpy(txn->srv_cookie, att_beg, log_len);
4148 txn->srv_cookie[log_len] = 0;
4149 }
4150 }
4151
4152 srv = objt_server(s->target);
4153 /* now check if we need to process it for persistence */
4154 if (!(s->flags & SF_IGNORE_PRST) &&
4155 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
4156 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
4157 /* assume passive cookie by default */
4158 txn->flags &= ~TX_SCK_MASK;
4159 txn->flags |= TX_SCK_FOUND;
4160
4161 /* If the cookie is in insert mode on a known server, we'll delete
4162 * this occurrence because we'll insert another one later.
4163 * We'll delete it too if the "indirect" option is set and we're in
4164 * a direct access.
4165 */
4166 if (s->be->ck_opts & PR_CK_PSV) {
4167 /* The "preserve" flag was set, we don't want to touch the
4168 * server's cookie.
4169 */
4170 }
4171 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
4172 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
4173 /* this cookie must be deleted */
4174 if (prev == hdr_beg && next == hdr_end) {
4175 /* whole header */
4176 http_remove_header(htx, &ctx);
4177 /* note: while both invalid now, <next> and <hdr_end>
4178 * are still equal, so the for() will stop as expected.
4179 */
4180 } else {
4181 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004182 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004183 next = prev;
4184 hdr_end += delta;
4185 }
4186 txn->flags &= ~TX_SCK_MASK;
4187 txn->flags |= TX_SCK_DELETED;
4188 /* and go on with next cookie */
4189 }
4190 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
4191 /* replace bytes val_beg->val_end with the cookie name associated
4192 * with this server since we know it.
4193 */
4194 int sliding, delta;
4195
4196 ctx.value = ist2(val_beg, val_end - val_beg);
4197 ctx.lws_before = ctx.lws_after = 0;
4198 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
4199 delta = srv->cklen - (val_end - val_beg);
4200 sliding = (ctx.value.ptr - val_beg);
4201 hdr_beg += sliding;
4202 val_beg += sliding;
4203 next += sliding + delta;
4204 hdr_end += sliding + delta;
4205
4206 txn->flags &= ~TX_SCK_MASK;
4207 txn->flags |= TX_SCK_REPLACED;
4208 }
4209 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
4210 /* insert the cookie name associated with this server
4211 * before existing cookie, and insert a delimiter between them..
4212 */
4213 int sliding, delta;
4214 ctx.value = ist2(val_beg, 0);
4215 ctx.lws_before = ctx.lws_after = 0;
4216 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
4217 delta = srv->cklen + 1;
4218 sliding = (ctx.value.ptr - val_beg);
4219 hdr_beg += sliding;
4220 val_beg += sliding;
4221 next += sliding + delta;
4222 hdr_end += sliding + delta;
4223
4224 val_beg[srv->cklen] = COOKIE_DELIM;
4225 txn->flags &= ~TX_SCK_MASK;
4226 txn->flags |= TX_SCK_REPLACED;
4227 }
4228 }
4229 /* that's done for this cookie, check the next one on the same
4230 * line when next != hdr_end (only if is_cookie2).
4231 */
4232 }
4233 }
4234}
4235
Christopher Faulet25a02f62018-10-24 12:00:25 +02004236/*
4237 * Parses the Cache-Control and Pragma request header fields to determine if
4238 * the request may be served from the cache and/or if it is cacheable. Updates
4239 * s->txn->flags.
4240 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004241void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004242{
4243 struct http_txn *txn = s->txn;
4244 struct htx *htx;
4245 int32_t pos;
4246 int pragma_found, cc_found, i;
4247
4248 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
4249 return; /* nothing more to do here */
4250
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004251 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02004252 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02004253 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004254 struct htx_blk *blk = htx_get_blk(htx, pos);
4255 enum htx_blk_type type = htx_get_blk_type(blk);
4256 struct ist n, v;
4257
4258 if (type == HTX_BLK_EOH)
4259 break;
4260 if (type != HTX_BLK_HDR)
4261 continue;
4262
4263 n = htx_get_blk_name(htx, blk);
4264 v = htx_get_blk_value(htx, blk);
4265
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004266 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004267 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4268 pragma_found = 1;
4269 continue;
4270 }
4271 }
4272
4273 /* Don't use the cache and don't try to store if we found the
4274 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004275 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004276 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4277 txn->flags |= TX_CACHE_IGNORE;
4278 continue;
4279 }
4280
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004281 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004282 continue;
4283
4284 /* OK, right now we know we have a cache-control header */
4285 cc_found = 1;
4286 if (!v.len) /* no info */
4287 continue;
4288
4289 i = 0;
4290 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4291 !isspace((unsigned char)*(v.ptr+i)))
4292 i++;
4293
4294 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
4295 * values after max-age, max-stale nor min-fresh, we simply don't
4296 * use the cache when they're specified.
4297 */
4298 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
4299 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4300 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
4301 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
4302 txn->flags |= TX_CACHE_IGNORE;
4303 continue;
4304 }
4305
4306 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
4307 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4308 continue;
4309 }
4310 }
4311
4312 /* RFC7234#5.4:
4313 * When the Cache-Control header field is also present and
4314 * understood in a request, Pragma is ignored.
4315 * When the Cache-Control header field is not present in a
4316 * request, caches MUST consider the no-cache request
4317 * pragma-directive as having the same effect as if
4318 * "Cache-Control: no-cache" were present.
4319 */
4320 if (!cc_found && pragma_found)
4321 txn->flags |= TX_CACHE_IGNORE;
4322}
4323
4324/*
4325 * Check if response is cacheable or not. Updates s->txn->flags.
4326 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004327void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004328{
4329 struct http_txn *txn = s->txn;
4330 struct htx *htx;
4331 int32_t pos;
4332 int i;
4333
4334 if (txn->status < 200) {
4335 /* do not try to cache interim responses! */
4336 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4337 return;
4338 }
4339
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004340 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004341 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004342 struct htx_blk *blk = htx_get_blk(htx, pos);
4343 enum htx_blk_type type = htx_get_blk_type(blk);
4344 struct ist n, v;
4345
4346 if (type == HTX_BLK_EOH)
4347 break;
4348 if (type != HTX_BLK_HDR)
4349 continue;
4350
4351 n = htx_get_blk_name(htx, blk);
4352 v = htx_get_blk_value(htx, blk);
4353
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004354 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004355 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4356 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4357 return;
4358 }
4359 }
4360
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004361 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004362 continue;
4363
4364 /* OK, right now we know we have a cache-control header */
4365 if (!v.len) /* no info */
4366 continue;
4367
4368 i = 0;
4369 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4370 !isspace((unsigned char)*(v.ptr+i)))
4371 i++;
4372
4373 /* we have a complete value between v.ptr and (v.ptr+i) */
4374 if (i < v.len && *(v.ptr + i) == '=') {
4375 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
4376 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
4377 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4378 continue;
4379 }
4380
4381 /* we have something of the form no-cache="set-cookie" */
4382 if ((v.len >= 21) &&
4383 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
4384 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
4385 txn->flags &= ~TX_CACHE_COOK;
4386 continue;
4387 }
4388
4389 /* OK, so we know that either p2 points to the end of string or to a comma */
4390 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
4391 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4392 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
4393 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4394 return;
4395 }
4396
4397 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4398 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4399 continue;
4400 }
4401 }
4402}
4403
Christopher Faulet377c5a52018-10-24 21:21:30 +02004404/*
4405 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4406 * for the current backend.
4407 *
4408 * It is assumed that the request is either a HEAD, GET, or POST and that the
4409 * uri_auth field is valid.
4410 *
4411 * Returns 1 if stats should be provided, otherwise 0.
4412 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004413static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004414{
4415 struct uri_auth *uri_auth = backend->uri_auth;
4416 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004417 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004418 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004419
4420 if (!uri_auth)
4421 return 0;
4422
4423 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4424 return 0;
4425
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004426 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004427 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004428 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01004429 if (*uri_auth->uri_prefix == '/')
4430 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004431
4432 /* check URI size */
4433 if (uri_auth->uri_len > uri.len)
4434 return 0;
4435
4436 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4437 return 0;
4438
4439 return 1;
4440}
4441
4442/* This function prepares an applet to handle the stats. It can deal with the
4443 * "100-continue" expectation, check that admin rules are met for POST requests,
4444 * and program a response message if something was unexpected. It cannot fail
4445 * and always relies on the stats applet to complete the job. It does not touch
4446 * analysers nor counters, which are left to the caller. It does not touch
4447 * s->target which is supposed to already point to the stats applet. The caller
4448 * is expected to have already assigned an appctx to the stream.
4449 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004450static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004451{
4452 struct stats_admin_rule *stats_admin_rule;
4453 struct stream_interface *si = &s->si[1];
4454 struct session *sess = s->sess;
4455 struct http_txn *txn = s->txn;
4456 struct http_msg *msg = &txn->req;
4457 struct uri_auth *uri_auth = s->be->uri_auth;
4458 const char *h, *lookup, *end;
4459 struct appctx *appctx;
4460 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004461 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004462
4463 appctx = si_appctx(si);
4464 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4465 appctx->st1 = appctx->st2 = 0;
4466 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004467 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004468 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4469 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4470 appctx->ctx.stats.flags |= STAT_CHUNKED;
4471
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004472 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004473 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004474 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4475 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004476
4477 for (h = lookup; h <= end - 3; h++) {
4478 if (memcmp(h, ";up", 3) == 0) {
4479 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4480 break;
4481 }
4482 }
4483
4484 if (uri_auth->refresh) {
4485 for (h = lookup; h <= end - 10; h++) {
4486 if (memcmp(h, ";norefresh", 10) == 0) {
4487 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4488 break;
4489 }
4490 }
4491 }
4492
4493 for (h = lookup; h <= end - 4; h++) {
4494 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004495 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004496 break;
4497 }
4498 }
4499
4500 for (h = lookup; h <= end - 6; h++) {
4501 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004502 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004503 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4504 break;
4505 }
4506 }
4507
Christopher Faulet6338a082019-09-09 15:50:54 +02004508 for (h = lookup; h <= end - 5; h++) {
4509 if (memcmp(h, ";json", 5) == 0) {
4510 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4511 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4512 break;
4513 }
4514 }
4515
4516 for (h = lookup; h <= end - 12; h++) {
4517 if (memcmp(h, ";json-schema", 12) == 0) {
4518 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4519 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4520 break;
4521 }
4522 }
4523
Christopher Faulet377c5a52018-10-24 21:21:30 +02004524 for (h = lookup; h <= end - 8; h++) {
4525 if (memcmp(h, ";st=", 4) == 0) {
4526 int i;
4527 h += 4;
4528 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4529 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4530 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4531 appctx->ctx.stats.st_code = i;
4532 break;
4533 }
4534 }
4535 break;
4536 }
4537 }
4538
4539 appctx->ctx.stats.scope_str = 0;
4540 appctx->ctx.stats.scope_len = 0;
4541 for (h = lookup; h <= end - 8; h++) {
4542 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4543 int itx = 0;
4544 const char *h2;
4545 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4546 const char *err;
4547
4548 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4549 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004550 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4551 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004552 if (*h == ';' || *h == '&' || *h == ' ')
4553 break;
4554 itx++;
4555 h++;
4556 }
4557
4558 if (itx > STAT_SCOPE_TXT_MAXLEN)
4559 itx = STAT_SCOPE_TXT_MAXLEN;
4560 appctx->ctx.stats.scope_len = itx;
4561
4562 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4563 memcpy(scope_txt, h2, itx);
4564 scope_txt[itx] = '\0';
4565 err = invalid_char(scope_txt);
4566 if (err) {
4567 /* bad char in search text => clear scope */
4568 appctx->ctx.stats.scope_str = 0;
4569 appctx->ctx.stats.scope_len = 0;
4570 }
4571 break;
4572 }
4573 }
4574
4575 /* now check whether we have some admin rules for this request */
4576 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4577 int ret = 1;
4578
4579 if (stats_admin_rule->cond) {
4580 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4581 ret = acl_pass(ret);
4582 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4583 ret = !ret;
4584 }
4585
4586 if (ret) {
4587 /* no rule, or the rule matches */
4588 appctx->ctx.stats.flags |= STAT_ADMIN;
4589 break;
4590 }
4591 }
4592
Christopher Faulet5d45e382019-02-27 15:15:23 +01004593 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4594 appctx->st0 = STAT_HTTP_HEAD;
4595 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004596 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004597 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004598 if (msg->msg_state < HTTP_MSG_DATA)
4599 req->analysers |= AN_REQ_HTTP_BODY;
4600 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004601 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004602 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004603 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4604 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4605 appctx->st0 = STAT_HTTP_LAST;
4606 }
4607 }
4608 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004609 /* Unsupported method */
4610 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4611 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4612 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004613 }
4614
4615 s->task->nice = -32; /* small boost for HTTP statistics */
4616 return 1;
4617}
4618
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004619void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004620{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004621 struct channel *req = &s->req;
4622 struct channel *res = &s->res;
4623 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004624 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004625 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004626 struct ist path, location;
4627 unsigned int flags;
4628 size_t data;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004629
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004630 /*
4631 * Create the location
4632 */
4633 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004634
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004635 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004636 /* special prefix "/" means don't change URL */
4637 srv = __objt_server(s->target);
4638 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4639 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4640 return;
4641 }
4642
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004643 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004644 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004645 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004646 path = http_get_path(htx_sl_req_uri(sl));
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004647 if (!path.ptr)
4648 return;
4649
4650 if (!chunk_memcat(&trash, path.ptr, path.len))
4651 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004652 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004653
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004654 /*
4655 * Create the 302 respone
4656 */
4657 htx = htx_from_buf(&res->buf);
4658 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4659 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4660 ist("HTTP/1.1"), ist("302"), ist("Found"));
4661 if (!sl)
4662 goto fail;
4663 sl->info.res.status = 302;
4664 s->txn->status = 302;
4665
4666 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4667 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4668 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4669 !htx_add_header(htx, ist("Location"), location))
4670 goto fail;
4671
4672 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4673 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004674
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004675 /*
4676 * Send the message
4677 */
4678 data = htx->data - co_data(res);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004679 c_adv(res, data);
4680 res->total += data;
4681
4682 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004683 si_shutr(si);
4684 si_shutw(si);
4685 si->err_type = SI_ET_NONE;
4686 si->state = SI_ST_CLO;
4687
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004688 channel_auto_read(req);
4689 channel_abort(req);
4690 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004691 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004692 channel_auto_read(res);
4693 channel_auto_close(res);
4694
4695 if (!(s->flags & SF_ERR_MASK))
4696 s->flags |= SF_ERR_LOCAL;
4697 if (!(s->flags & SF_FINST_MASK))
4698 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004699
4700 /* FIXME: we should increase a counter of redirects per server and per backend. */
4701 srv_inc_sess_ctr(srv);
4702 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004703 return;
4704
4705 fail:
4706 /* If an error occurred, remove the incomplete HTTP response from the
4707 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004708 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004709}
4710
Christopher Fauletf2824e62018-10-01 12:12:37 +02004711/* This function terminates the request because it was completly analyzed or
4712 * because an error was triggered during the body forwarding.
4713 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004714static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004715{
4716 struct channel *chn = &s->req;
4717 struct http_txn *txn = s->txn;
4718
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004719 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004720
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004721 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4722 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004723 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004724 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004725 goto end;
4726 }
4727
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004728 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4729 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004730 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004731 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004732
4733 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004734 /* No need to read anymore, the request was completely parsed.
4735 * We can shut the read side unless we want to abort_on_close,
4736 * or we have a POST request. The issue with POST requests is
4737 * that some browsers still send a CRLF after the request, and
4738 * this CRLF must be read so that it does not remain in the kernel
4739 * buffers, otherwise a close could cause an RST on some systems
4740 * (eg: Linux).
4741 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004742 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004743 channel_dont_read(chn);
4744
4745 /* if the server closes the connection, we want to immediately react
4746 * and close the socket to save packets and syscalls.
4747 */
4748 s->si[1].flags |= SI_FL_NOHALF;
4749
4750 /* In any case we've finished parsing the request so we must
4751 * disable Nagle when sending data because 1) we're not going
4752 * to shut this side, and 2) the server is waiting for us to
4753 * send pending data.
4754 */
4755 chn->flags |= CF_NEVER_WAIT;
4756
Christopher Fauletd01ce402019-01-02 17:44:13 +01004757 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4758 /* The server has not finished to respond, so we
4759 * don't want to move in order not to upset it.
4760 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004761 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004762 return;
4763 }
4764
Christopher Fauletf2824e62018-10-01 12:12:37 +02004765 /* When we get here, it means that both the request and the
4766 * response have finished receiving. Depending on the connection
4767 * mode, we'll have to wait for the last bytes to leave in either
4768 * direction, and sometimes for a close to be effective.
4769 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004770 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004771 /* Tunnel mode will not have any analyser so it needs to
4772 * poll for reads.
4773 */
4774 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004775 if (b_data(&chn->buf)) {
4776 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004777 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004778 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004779 txn->req.msg_state = HTTP_MSG_TUNNEL;
4780 }
4781 else {
4782 /* we're not expecting any new data to come for this
4783 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004784 *
4785 * However, there is an exception if the response
4786 * length is undefined. In this case, we need to wait
4787 * the close from the server. The response will be
4788 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004789 */
4790 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4791 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004792 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004793
4794 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4795 channel_shutr_now(chn);
4796 channel_shutw_now(chn);
4797 }
4798 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004799 goto check_channel_flags;
4800 }
4801
4802 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4803 http_msg_closing:
4804 /* nothing else to forward, just waiting for the output buffer
4805 * to be empty and for the shutw_now to take effect.
4806 */
4807 if (channel_is_empty(chn)) {
4808 txn->req.msg_state = HTTP_MSG_CLOSED;
4809 goto http_msg_closed;
4810 }
4811 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004812 txn->req.msg_state = HTTP_MSG_ERROR;
4813 goto end;
4814 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004815 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004816 return;
4817 }
4818
4819 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4820 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004821 /* if we don't know whether the server will close, we need to hard close */
4822 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4823 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004824 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004825 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004826 channel_dont_read(chn);
4827 goto end;
4828 }
4829
4830 check_channel_flags:
4831 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4832 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4833 /* if we've just closed an output, let's switch */
4834 txn->req.msg_state = HTTP_MSG_CLOSING;
4835 goto http_msg_closing;
4836 }
4837
4838 end:
4839 chn->analysers &= AN_REQ_FLT_END;
4840 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4841 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4842 channel_auto_close(chn);
4843 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004844 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004845}
4846
4847
4848/* This function terminates the response because it was completly analyzed or
4849 * because an error was triggered during the body forwarding.
4850 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004851static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004852{
4853 struct channel *chn = &s->res;
4854 struct http_txn *txn = s->txn;
4855
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004856 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004857
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004858 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4859 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004860 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004861 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004862 goto end;
4863 }
4864
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004865 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4866 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004867 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004868 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004869
4870 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4871 /* In theory, we don't need to read anymore, but we must
4872 * still monitor the server connection for a possible close
4873 * while the request is being uploaded, so we don't disable
4874 * reading.
4875 */
4876 /* channel_dont_read(chn); */
4877
4878 if (txn->req.msg_state < HTTP_MSG_DONE) {
4879 /* The client seems to still be sending data, probably
4880 * because we got an error response during an upload.
4881 * We have the choice of either breaking the connection
4882 * or letting it pass through. Let's do the later.
4883 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004884 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004885 return;
4886 }
4887
4888 /* When we get here, it means that both the request and the
4889 * response have finished receiving. Depending on the connection
4890 * mode, we'll have to wait for the last bytes to leave in either
4891 * direction, and sometimes for a close to be effective.
4892 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004893 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004894 channel_auto_read(chn);
4895 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004896 if (b_data(&chn->buf)) {
4897 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004898 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004899 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004900 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4901 }
4902 else {
4903 /* we're not expecting any new data to come for this
4904 * transaction, so we can close it.
4905 */
4906 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4907 channel_shutr_now(chn);
4908 channel_shutw_now(chn);
4909 }
4910 }
4911 goto check_channel_flags;
4912 }
4913
4914 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4915 http_msg_closing:
4916 /* nothing else to forward, just waiting for the output buffer
4917 * to be empty and for the shutw_now to take effect.
4918 */
4919 if (channel_is_empty(chn)) {
4920 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4921 goto http_msg_closed;
4922 }
4923 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004924 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004925 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004926 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004927 if (strm_sess(s)->listener->counters)
4928 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004929 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004930 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004931 goto end;
4932 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004933 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004934 return;
4935 }
4936
4937 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4938 http_msg_closed:
4939 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004940 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004941 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004942 goto end;
4943 }
4944
4945 check_channel_flags:
4946 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4947 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4948 /* if we've just closed an output, let's switch */
4949 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4950 goto http_msg_closing;
4951 }
4952
4953 end:
4954 chn->analysers &= AN_RES_FLT_END;
4955 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4956 chn->analysers |= AN_RES_FLT_XFER_DATA;
4957 channel_auto_close(chn);
4958 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004959 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004960}
4961
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004962void http_server_error(struct stream *s, struct stream_interface *si, int err,
4963 int finst, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004964{
4965 channel_auto_read(si_oc(si));
4966 channel_abort(si_oc(si));
4967 channel_auto_close(si_oc(si));
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004968 channel_htx_erase(si_oc(si), htxbuf(&(si_oc(si))->buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02004969 channel_auto_close(si_ic(si));
4970 channel_auto_read(si_ic(si));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004971
4972 /* <msg> is an HTX structure. So we copy it in the response's
4973 * channel */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004974 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004975 struct channel *chn = si_ic(si);
4976 struct htx *htx;
4977
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004978 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004979 chn->buf.data = msg->data;
4980 memcpy(chn->buf.area, msg->area, msg->data);
4981 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02004982 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02004983 c_adv(chn, htx->data);
4984 chn->total += htx->data;
4985 }
4986 if (!(s->flags & SF_ERR_MASK))
4987 s->flags |= err;
4988 if (!(s->flags & SF_FINST_MASK))
4989 s->flags |= finst;
4990}
4991
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004992void http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004993{
4994 channel_auto_read(&s->req);
4995 channel_abort(&s->req);
4996 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004997 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
4998 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02004999
5000 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005001
5002 /* <msg> is an HTX structure. So we copy it in the response's
5003 * channel */
5004 /* FIXME: It is a problem for now if there is some outgoing data */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02005005 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02005006 struct channel *chn = &s->res;
5007 struct htx *htx;
5008
Christopher Fauletaed82cf2018-11-30 22:22:32 +01005009 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005010 chn->buf.data = msg->data;
5011 memcpy(chn->buf.area, msg->area, msg->data);
5012 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02005013 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02005014 c_adv(chn, htx->data);
5015 chn->total += htx->data;
5016 }
5017
5018 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
5019 channel_auto_read(&s->res);
5020 channel_auto_close(&s->res);
5021 channel_shutr_now(&s->res);
5022}
5023
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005024struct buffer *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005025{
5026 const int msgnum = http_get_status_idx(s->txn->status);
5027
5028 if (s->be->errmsg[msgnum].area)
5029 return &s->be->errmsg[msgnum];
5030 else if (strm_fe(s)->errmsg[msgnum].area)
5031 return &strm_fe(s)->errmsg[msgnum];
5032 else
Christopher Fauletf7346382019-07-17 22:02:08 +02005033 return &http_err_chunks[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005034}
5035
Christopher Faulet304cc402019-07-15 15:46:28 +02005036/* Return the error message corresponding to si->err_type. It is assumed
5037 * that the server side is closed. Note that err_type is actually a
5038 * bitmask, where almost only aborts may be cumulated with other
5039 * values. We consider that aborted operations are more important
5040 * than timeouts or errors due to the fact that nobody else in the
5041 * logs might explain incomplete retries. All others should avoid
5042 * being cumulated. It should normally not be possible to have multiple
5043 * aborts at once, but just in case, the first one in sequence is reported.
5044 * Note that connection errors appearing on the second request of a keep-alive
5045 * connection are not reported since this allows the client to retry.
5046 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005047void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02005048{
5049 int err_type = si->err_type;
5050
5051 /* set s->txn->status for http_error_message(s) */
5052 s->txn->status = 503;
5053
5054 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005055 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
5056 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005057 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005058 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
5059 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5060 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005061 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005062 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
5063 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005064 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005065 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
5066 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005067 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005068 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
5069 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5070 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005071 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005072 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
5073 (s->flags & SF_SRV_REUSED) ? NULL :
5074 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005075 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005076 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
5077 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5078 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005079 else { /* SI_ET_CONN_OTHER and others */
5080 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005081 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
5082 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005083 }
5084}
5085
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005086
Christopher Faulet4a28a532019-03-01 11:19:40 +01005087/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
5088 * on success and -1 on error.
5089 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005090static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01005091{
5092 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
5093 * then we must send an HTTP/1.1 100 Continue intermediate response.
5094 */
5095 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
5096 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
5097 struct ist hdr = { .ptr = "Expect", .len = 6 };
5098 struct http_hdr_ctx ctx;
5099
5100 ctx.blk = NULL;
5101 /* Expect is allowed in 1.1, look for it */
5102 if (http_find_header(htx, hdr, &ctx, 0) &&
5103 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005104 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01005105 return -1;
5106 http_remove_header(htx, &ctx);
5107 }
5108 }
5109 return 0;
5110}
5111
Christopher Faulet23a3c792018-11-28 10:01:23 +01005112/* Send a 100-Continue response to the client. It returns 0 on success and -1
5113 * on error. The response channel is updated accordingly.
5114 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005115static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01005116{
5117 struct channel *res = &s->res;
5118 struct htx *htx = htx_from_buf(&res->buf);
5119 struct htx_sl *sl;
5120 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
5121 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
5122 size_t data;
5123
5124 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5125 ist("HTTP/1.1"), ist("100"), ist("Continue"));
5126 if (!sl)
5127 goto fail;
5128 sl->info.res.status = 100;
5129
Christopher Faulet1d5ec092019-06-26 14:23:54 +02005130 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01005131 goto fail;
5132
5133 data = htx->data - co_data(res);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005134 c_adv(res, data);
5135 res->total += data;
5136 return 0;
5137
5138 fail:
5139 /* If an error occurred, remove the incomplete HTTP response from the
5140 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005141 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005142 return -1;
5143}
5144
Christopher Faulet12c51e22018-11-28 15:59:42 +01005145
5146/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
5147 * ont whether we use a proxy or not. It returns 0 on success and -1 on
5148 * error. The response channel is updated accordingly.
5149 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005150static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
Christopher Faulet12c51e22018-11-28 15:59:42 +01005151{
5152 struct channel *res = &s->res;
5153 struct htx *htx = htx_from_buf(&res->buf);
5154 struct htx_sl *sl;
5155 struct ist code, body;
5156 int status;
5157 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5158 size_t data;
5159
5160 if (!(s->txn->flags & TX_USE_PX_CONN)) {
5161 status = 401;
5162 code = ist("401");
5163 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
5164 "You need a valid user and password to access this content.\n"
5165 "</body></html>\n");
5166 }
5167 else {
5168 status = 407;
5169 code = ist("407");
5170 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
5171 "You need a valid user and password to access this content.\n"
5172 "</body></html>\n");
5173 }
5174
5175 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5176 ist("HTTP/1.1"), code, ist("Unauthorized"));
5177 if (!sl)
5178 goto fail;
5179 sl->info.res.status = status;
5180 s->txn->status = status;
5181
5182 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
5183 goto fail;
5184
Willy Tarreaub5ba2b02019-06-11 16:08:25 +02005185 if (!htx_add_header(htx, ist("Content-length"), ist("112")) ||
5186 !htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Faulet12c51e22018-11-28 15:59:42 +01005187 !htx_add_header(htx, ist("Connection"), ist("close")) ||
Jérôme Magnin86cef232018-12-28 14:49:08 +01005188 !htx_add_header(htx, ist("Content-Type"), ist("text/html")))
5189 goto fail;
5190 if (status == 401 && !htx_add_header(htx, ist("WWW-Authenticate"), ist2(trash.area, trash.data)))
5191 goto fail;
5192 if (status == 407 && !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005193 goto fail;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02005194 if (!htx_add_endof(htx, HTX_BLK_EOH))
5195 goto fail;
5196
5197 while (body.len) {
5198 size_t sent = htx_add_data(htx, body);
5199 if (!sent)
5200 goto fail;
5201 body.ptr += sent;
5202 body.len -= sent;
5203 }
5204
5205 if (!htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005206 goto fail;
5207
Christopher Faulet06511812019-10-16 09:38:27 +02005208 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet12c51e22018-11-28 15:59:42 +01005209 data = htx->data - co_data(res);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005210 c_adv(res, data);
5211 res->total += data;
5212
5213 channel_auto_read(&s->req);
5214 channel_abort(&s->req);
5215 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005216 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet12c51e22018-11-28 15:59:42 +01005217
5218 res->wex = tick_add_ifset(now_ms, res->wto);
5219 channel_auto_read(res);
5220 channel_auto_close(res);
5221 channel_shutr_now(res);
5222 return 0;
5223
5224 fail:
5225 /* If an error occurred, remove the incomplete HTTP response from the
5226 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005227 channel_htx_truncate(res, htx);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005228 return -1;
5229}
5230
Christopher Faulet0f226952018-10-22 09:29:56 +02005231/*
5232 * Capture headers from message <htx> according to header list <cap_hdr>, and
5233 * fill the <cap> pointers appropriately.
5234 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005235static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02005236{
5237 struct cap_hdr *h;
5238 int32_t pos;
5239
Christopher Fauleta3f15502019-05-13 15:27:23 +02005240 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02005241 struct htx_blk *blk = htx_get_blk(htx, pos);
5242 enum htx_blk_type type = htx_get_blk_type(blk);
5243 struct ist n, v;
5244
5245 if (type == HTX_BLK_EOH)
5246 break;
5247 if (type != HTX_BLK_HDR)
5248 continue;
5249
5250 n = htx_get_blk_name(htx, blk);
5251
5252 for (h = cap_hdr; h; h = h->next) {
5253 if (h->namelen && (h->namelen == n.len) &&
5254 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
5255 if (cap[h->index] == NULL)
5256 cap[h->index] =
5257 pool_alloc(h->pool);
5258
5259 if (cap[h->index] == NULL) {
5260 ha_alert("HTTP capture : out of memory.\n");
5261 break;
5262 }
5263
5264 v = htx_get_blk_value(htx, blk);
5265 if (v.len > h->len)
5266 v.len = h->len;
5267
5268 memcpy(cap[h->index], v.ptr, v.len);
5269 cap[h->index][v.len]=0;
5270 }
5271 }
5272 }
5273}
5274
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005275/* Delete a value in a header between delimiters <from> and <next>. The header
5276 * itself is delimited by <start> and <end> pointers. The number of characters
5277 * displaced is returned, and the pointer to the first delimiter is updated if
5278 * required. The function tries as much as possible to respect the following
5279 * principles :
5280 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5281 * in which case <next> is simply removed
5282 * - set exactly one space character after the new first delimiter, unless there
5283 * are not enough characters in the block being moved to do so.
5284 * - remove unneeded spaces before the previous delimiter and after the new
5285 * one.
5286 *
5287 * It is the caller's responsibility to ensure that :
5288 * - <from> points to a valid delimiter or <start> ;
5289 * - <next> points to a valid delimiter or <end> ;
5290 * - there are non-space chars before <from>.
5291 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005292static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005293{
5294 char *prev = *from;
5295
5296 if (prev == start) {
5297 /* We're removing the first value. eat the semicolon, if <next>
5298 * is lower than <end> */
5299 if (next < end)
5300 next++;
5301
5302 while (next < end && HTTP_IS_SPHT(*next))
5303 next++;
5304 }
5305 else {
5306 /* Remove useless spaces before the old delimiter. */
5307 while (HTTP_IS_SPHT(*(prev-1)))
5308 prev--;
5309 *from = prev;
5310
5311 /* copy the delimiter and if possible a space if we're
5312 * not at the end of the line.
5313 */
5314 if (next < end) {
5315 *prev++ = *next++;
5316 if (prev + 1 < next)
5317 *prev++ = ' ';
5318 while (next < end && HTTP_IS_SPHT(*next))
5319 next++;
5320 }
5321 }
5322 memmove(prev, next, end - next);
5323 return (prev - next);
5324}
5325
Christopher Faulet0f226952018-10-22 09:29:56 +02005326
5327/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005328 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005329 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005330static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005331{
5332 struct ist dst = ist2(str, 0);
5333
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005334 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005335 goto end;
5336 if (dst.len + 1 > len)
5337 goto end;
5338 dst.ptr[dst.len++] = ' ';
5339
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005340 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005341 goto end;
5342 if (dst.len + 1 > len)
5343 goto end;
5344 dst.ptr[dst.len++] = ' ';
5345
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005346 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005347 end:
5348 return dst.len;
5349}
5350
5351/*
5352 * Print a debug line with a start line.
5353 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005354static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005355{
5356 struct session *sess = strm_sess(s);
5357 int max;
5358
5359 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5360 dir,
5361 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5362 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5363
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005364 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005365 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005366 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005367 trash.area[trash.data++] = ' ';
5368
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005369 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005370 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005371 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005372 trash.area[trash.data++] = ' ';
5373
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005374 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005375 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005376 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005377 trash.area[trash.data++] = '\n';
5378
5379 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5380}
5381
5382/*
5383 * Print a debug line with a header.
5384 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005385static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v)
Christopher Faulet0f226952018-10-22 09:29:56 +02005386{
5387 struct session *sess = strm_sess(s);
5388 int max;
5389
5390 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5391 dir,
5392 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5393 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5394
5395 max = n.len;
5396 UBOUND(max, trash.size - trash.data - 3);
5397 chunk_memcat(&trash, n.ptr, max);
5398 trash.area[trash.data++] = ':';
5399 trash.area[trash.data++] = ' ';
5400
5401 max = v.len;
5402 UBOUND(max, trash.size - trash.data - 1);
5403 chunk_memcat(&trash, v.ptr, max);
5404 trash.area[trash.data++] = '\n';
5405
5406 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5407}
5408
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005409/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5410 * In case of allocation failure, everything allocated is freed and NULL is
5411 * returned. Otherwise the new transaction is assigned to the stream and
5412 * returned.
5413 */
5414struct http_txn *http_alloc_txn(struct stream *s)
5415{
5416 struct http_txn *txn = s->txn;
5417
5418 if (txn)
5419 return txn;
5420
5421 txn = pool_alloc(pool_head_http_txn);
5422 if (!txn)
5423 return txn;
5424
5425 s->txn = txn;
5426 return txn;
5427}
5428
5429void http_txn_reset_req(struct http_txn *txn)
5430{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005431 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005432 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5433}
5434
5435void http_txn_reset_res(struct http_txn *txn)
5436{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005437 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005438 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5439}
5440
5441/*
5442 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5443 * the required fields are properly allocated and that we only need to (re)init
5444 * them. This should be used before processing any new request.
5445 */
5446void http_init_txn(struct stream *s)
5447{
5448 struct http_txn *txn = s->txn;
5449 struct conn_stream *cs = objt_cs(s->si[0].end);
5450
5451 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5452 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5453 : 0);
5454 txn->status = -1;
5455 *(unsigned int *)txn->cache_hash = 0;
5456
5457 txn->cookie_first_date = 0;
5458 txn->cookie_last_date = 0;
5459
5460 txn->srv_cookie = NULL;
5461 txn->cli_cookie = NULL;
5462 txn->uri = NULL;
5463
5464 http_txn_reset_req(txn);
5465 http_txn_reset_res(txn);
5466
5467 txn->req.chn = &s->req;
5468 txn->rsp.chn = &s->res;
5469
5470 txn->auth.method = HTTP_AUTH_UNKNOWN;
5471
5472 vars_init(&s->vars_txn, SCOPE_TXN);
5473 vars_init(&s->vars_reqres, SCOPE_REQ);
5474}
5475
5476/* to be used at the end of a transaction */
5477void http_end_txn(struct stream *s)
5478{
5479 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005480
5481 /* these ones will have been dynamically allocated */
5482 pool_free(pool_head_requri, txn->uri);
5483 pool_free(pool_head_capture, txn->cli_cookie);
5484 pool_free(pool_head_capture, txn->srv_cookie);
5485 pool_free(pool_head_uniqueid, s->unique_id);
5486
5487 s->unique_id = NULL;
5488 txn->uri = NULL;
5489 txn->srv_cookie = NULL;
5490 txn->cli_cookie = NULL;
5491
Christopher Faulet59399252019-11-07 14:27:52 +01005492 if (!LIST_ISEMPTY(&s->vars_txn.head))
5493 vars_prune(&s->vars_txn, s->sess, s);
5494 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5495 vars_prune(&s->vars_reqres, s->sess, s);
5496}
5497
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005498
5499DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
5500DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
Christopher Faulet0f226952018-10-22 09:29:56 +02005501
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005502__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005503static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005504{
5505}
5506
5507
5508/*
5509 * Local variables:
5510 * c-indent-level: 8
5511 * c-basic-offset: 8
5512 * End:
5513 */