blob: e63323b29a3b9d21a04056f5df029518b4a0d337 [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;
518 }
519 }
520
521 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
522 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200523 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200524
Christopher Fauletff2759f2018-10-24 11:13:16 +0200525 ctx.blk = NULL;
526 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
527 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100528 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200529 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200530 }
531
532 /* OK at this stage, we know that the request was accepted according to
533 * the http-request rules, we can check for the stats. Note that the
534 * URI is detected *before* the req* rules in order not to be affected
535 * by a possible reqrep, while they are processed *after* so that a
536 * reqdeny can still block them. This clearly needs to change in 1.6!
537 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200538 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200539 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100540 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200541 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200542 if (!(s->flags & SF_ERR_MASK))
543 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100544 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200545 }
546
547 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200548 http_handle_stats(s, req);
549 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200550 /* not all actions implemented: deny, allow, auth */
551
552 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
553 goto deny;
554
555 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
556 goto return_prx_cond;
557 }
558
Christopher Faulet2571bc62019-03-01 11:44:26 +0100559 /* Proceed with the applets now. */
560 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200561 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100562 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200563
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200564 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100565 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100566
Christopher Faulete0768eb2018-10-03 16:38:02 +0200567 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
568 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
569 if (!(s->flags & SF_FINST_MASK))
570 s->flags |= SF_FINST_R;
571
572 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
573 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
574 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
575 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100576
577 req->flags |= CF_SEND_DONTWAIT;
578 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200579 goto done;
580 }
581
582 /* check whether we have some ACLs set to redirect this request */
583 list_for_each_entry(rule, &px->redirect_rules, list) {
584 if (rule->cond) {
585 int ret;
586
587 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
588 ret = acl_pass(ret);
589 if (rule->cond->pol == ACL_COND_UNLESS)
590 ret = !ret;
591 if (!ret)
592 continue;
593 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200594 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100595 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200596 goto done;
597 }
598
599 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
600 * If this happens, then the data will not come immediately, so we must
601 * send all what we have without waiting. Note that due to the small gain
602 * in waiting for the body of the request, it's easier to simply put the
603 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
604 * itself once used.
605 */
606 req->flags |= CF_SEND_DONTWAIT;
607
608 done: /* done with this analyser, continue with next ones that the calling
609 * points will have set, if any.
610 */
611 req->analyse_exp = TICK_ETERNITY;
612 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
613 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100614 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200615 return 1;
616
617 tarpit:
618 /* Allow cookie logging
619 */
620 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200621 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200622
623 /* When a connection is tarpitted, we use the tarpit timeout,
624 * which may be the same as the connect timeout if unspecified.
625 * If unset, then set it to zero because we really want it to
626 * eventually expire. We build the tarpit as an analyser.
627 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100628 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200629
630 /* wipe the request out so that we can drop the connection early
631 * if the client closes first.
632 */
633 channel_dont_connect(req);
634
635 txn->status = http_err_codes[deny_status];
636
637 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
638 req->analysers |= AN_REQ_HTTP_TARPIT;
639 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
640 if (!req->analyse_exp)
641 req->analyse_exp = tick_add(now_ms, 0);
642 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100643 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200644 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100645 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200646 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100647 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200648 goto done_without_exp;
649
650 deny: /* this request was blocked (denied) */
651
652 /* Allow cookie logging
653 */
654 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200655 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200656
657 txn->flags |= TX_CLDENY;
658 txn->status = http_err_codes[deny_status];
659 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200660 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100661 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200662 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100663 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200664 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100665 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100666 goto return_prx_err;
667
668 return_int_err:
669 txn->status = 500;
670 if (!(s->flags & SF_ERR_MASK))
671 s->flags |= SF_ERR_INTERNAL;
672 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
673 if (sess->listener->counters)
674 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
675 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200676
677 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200678 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100679 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200680 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100681 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100682 /* fall through */
683
684 return_prx_err:
685 http_reply_and_close(s, txn->status, http_error_message(s));
686 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200687
688 return_prx_cond:
689 if (!(s->flags & SF_ERR_MASK))
690 s->flags |= SF_ERR_PRXCOND;
691 if (!(s->flags & SF_FINST_MASK))
692 s->flags |= SF_FINST_R;
693
694 req->analysers &= AN_REQ_FLT_END;
695 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100696 DBG_TRACE_DEVEL("leaving on error",
697 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200698 return 0;
699
700 return_prx_yield:
701 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100702 DBG_TRACE_DEVEL("waiting for more data",
703 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200704 return 0;
705}
706
707/* This function performs all the processing enabled for the current request.
708 * It returns 1 if the processing can continue on next analysers, or zero if it
709 * needs more data, encounters an error, or wants to immediately abort the
710 * request. It relies on buffers flags, and updates s->req.analysers.
711 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200712int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200713{
714 struct session *sess = s->sess;
715 struct http_txn *txn = s->txn;
716 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200717 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200718 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
719
720 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
721 /* we need more data */
722 channel_dont_connect(req);
723 return 0;
724 }
725
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100726 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200727
728 /*
729 * Right now, we know that we have processed the entire headers
730 * and that unwanted requests have been filtered out. We can do
731 * whatever we want with the remaining request. Also, now we
732 * may have separate values for ->fe, ->be.
733 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100734 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200735
736 /*
737 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200738 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200739 */
740 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100741 struct htx_sl *sl;
742 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200743
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200744 if (!sockaddr_alloc(&s->target_addr)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200745 if (!(s->flags & SF_ERR_MASK))
746 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100747 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200748 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200749 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100750 uri = htx_sl_req_uri(sl);
751 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200752
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200753 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200754 goto return_bad_req;
755
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200756 s->target = &s->be->obj_type;
757 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
758
Christopher Faulete0768eb2018-10-03 16:38:02 +0200759 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200760 * uri.ptr and path.ptr (excluded). If it was not found, we need
761 * to replace from all the uri by a single "/".
762 *
763 * Instead of rewritting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100764 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200765 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200766 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100767 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200768 }
769
770 /*
771 * 7: Now we can work with the cookies.
772 * Note that doing so might move headers in the request, but
773 * the fields will stay coherent and the URI will not move.
774 * This should only be performed in the backend.
775 */
776 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200777 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200778
779 /* add unique-id if "header-unique-id" is specified */
780
781 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100782 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) {
783 if (!(s->flags & SF_ERR_MASK))
784 s->flags |= SF_ERR_RESOURCE;
785 goto return_int_err;
786 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200787 s->unique_id[0] = '\0';
788 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
789 }
790
791 if (sess->fe->header_unique_id && s->unique_id) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200792 struct ist n = ist2(sess->fe->header_unique_id, strlen(sess->fe->header_unique_id));
793 struct ist v = ist2(s->unique_id, strlen(s->unique_id));
794
795 if (unlikely(!http_add_header(htx, n, v)))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100796 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200797 }
798
799 /*
800 * 9: add X-Forwarded-For if either the frontend or the backend
801 * asks for it.
802 */
803 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200804 struct http_hdr_ctx ctx = { .blk = NULL };
805 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
806 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
807
Christopher Faulete0768eb2018-10-03 16:38:02 +0200808 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200809 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200810 /* The header is set to be added only if none is present
811 * and we found it, so don't do anything.
812 */
813 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200814 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200815 /* Add an X-Forwarded-For header unless the source IP is
816 * in the 'except' network range.
817 */
818 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200819 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200820 != sess->fe->except_net.s_addr) &&
821 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200822 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200823 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200824 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200825
826 /* Note: we rely on the backend to get the header name to be used for
827 * x-forwarded-for, because the header is really meant for the backends.
828 * However, if the backend did not specify any option, we have to rely
829 * on the frontend's header name.
830 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200831 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
832 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100833 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200834 }
835 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200836 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200837 /* FIXME: for the sake of completeness, we should also support
838 * 'except' here, although it is mostly useless in this case.
839 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200840 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200841
Christopher Faulete0768eb2018-10-03 16:38:02 +0200842 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200843 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200844 pn, sizeof(pn));
845
846 /* Note: we rely on the backend to get the header name to be used for
847 * x-forwarded-for, because the header is really meant for the backends.
848 * However, if the backend did not specify any option, we have to rely
849 * on the frontend's header name.
850 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200851 chunk_printf(&trash, "%s", pn);
852 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100853 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200854 }
855 }
856
857 /*
858 * 10: add X-Original-To if either the frontend or the backend
859 * asks for it.
860 */
861 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
862
863 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200864 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 +0200865 /* Add an X-Original-To header unless the destination IP is
866 * in the 'except' network range.
867 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200868 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200869 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200870 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200871 != sess->fe->except_to.s_addr) &&
872 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200873 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200874 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200875 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200876 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200877
878 /* Note: we rely on the backend to get the header name to be used for
879 * x-original-to, because the header is really meant for the backends.
880 * However, if the backend did not specify any option, we have to rely
881 * on the frontend's header name.
882 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200883 if (s->be->orgto_hdr_len)
884 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
885 else
886 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200887
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200888 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
889 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100890 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200891 }
892 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200893 }
894
Christopher Faulete0768eb2018-10-03 16:38:02 +0200895 /* If we have no server assigned yet and we're balancing on url_param
896 * with a POST request, we may be interested in checking the body for
897 * that parameter. This will be done in another analyser.
898 */
899 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100900 s->txn->meth == HTTP_METH_POST &&
901 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200902 channel_dont_connect(req);
903 req->analysers |= AN_REQ_HTTP_BODY;
904 }
905
906 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
907 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100908
Christopher Faulete0768eb2018-10-03 16:38:02 +0200909 /* We expect some data from the client. Unless we know for sure
910 * we already have a full request, we have to re-enable quick-ack
911 * in case we previously disabled it, otherwise we might cause
912 * the client to delay further data.
913 */
914 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200915 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100916 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200917
918 /*************************************************************
919 * OK, that's finished for the headers. We have done what we *
920 * could. Let's switch to the DATA state. *
921 ************************************************************/
922 req->analyse_exp = TICK_ETERNITY;
923 req->analysers &= ~an_bit;
924
925 s->logs.tv_request = now;
926 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100927 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200928 return 1;
929
Christopher Fauletb8a53712019-12-16 11:29:38 +0100930 return_int_err:
931 txn->status = 500;
932 if (!(s->flags & SF_ERR_MASK))
933 s->flags |= SF_ERR_INTERNAL;
934 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
935 if (sess->listener->counters)
936 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
937 goto return_prx_cond;
938
Christopher Faulete0768eb2018-10-03 16:38:02 +0200939 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200940 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100941 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200942 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100943 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100944 /* fall through */
945
946 return_prx_cond:
947 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200948
949 if (!(s->flags & SF_ERR_MASK))
950 s->flags |= SF_ERR_PRXCOND;
951 if (!(s->flags & SF_FINST_MASK))
952 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100953
954 req->analysers &= AN_REQ_FLT_END;
955 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100956 DBG_TRACE_DEVEL("leaving on error",
957 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200958 return 0;
959}
960
961/* This function is an analyser which processes the HTTP tarpit. It always
962 * returns zero, at the beginning because it prevents any other processing
963 * from occurring, and at the end because it terminates the request.
964 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200965int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200966{
967 struct http_txn *txn = s->txn;
968
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100969 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200970 /* This connection is being tarpitted. The CLIENT side has
971 * already set the connect expiration date to the right
972 * timeout. We just have to check that the client is still
973 * there and that the timeout has not expired.
974 */
975 channel_dont_connect(req);
976 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100977 !tick_is_expired(req->analyse_exp, now_ms)) {
978 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
979 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200980 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100981 }
982
Christopher Faulete0768eb2018-10-03 16:38:02 +0200983
984 /* We will set the queue timer to the time spent, just for
985 * logging purposes. We fake a 500 server error, so that the
986 * attacker will not suspect his connection has been tarpitted.
987 * It will not cause trouble to the logs because we can exclude
988 * the tarpitted connections by filtering on the 'PT' status flags.
989 */
990 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
991
992 if (!(req->flags & CF_READ_ERROR))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200993 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200994
995 req->analysers &= AN_REQ_FLT_END;
996 req->analyse_exp = TICK_ETERNITY;
997
998 if (!(s->flags & SF_ERR_MASK))
999 s->flags |= SF_ERR_PRXCOND;
1000 if (!(s->flags & SF_FINST_MASK))
1001 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001002
1003 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001004 return 0;
1005}
1006
1007/* This function is an analyser which waits for the HTTP request body. It waits
1008 * for either the buffer to be full, or the full advertised contents to have
1009 * reached the buffer. It must only be called after the standard HTTP request
1010 * processing has occurred, because it expects the request to be parsed and will
1011 * look for the Expect header. It may send a 100-Continue interim response. It
1012 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
1013 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
1014 * needs to read more data, or 1 once it has completed its analysis.
1015 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001016int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001017{
1018 struct session *sess = s->sess;
1019 struct http_txn *txn = s->txn;
1020 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001021 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001022
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001023 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001024
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001025 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001026
Willy Tarreau4236f032019-03-05 10:43:32 +01001027 if (htx->flags & HTX_FL_PARSING_ERROR)
1028 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001029 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1030 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001031
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001032 if (msg->msg_state < HTTP_MSG_BODY)
1033 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001034
Christopher Faulete0768eb2018-10-03 16:38:02 +02001035 /* We have to parse the HTTP request body to find any required data.
1036 * "balance url_param check_post" should have been the only way to get
1037 * into this. We were brought here after HTTP header analysis, so all
1038 * related structures are ready.
1039 */
1040
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001041 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001042 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001043 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001044 }
1045
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001046 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001047
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001048 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1049 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001050 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001051 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001052 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001053 goto http_end;
1054
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001055 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001056 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1057 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001058 if (!(s->flags & SF_ERR_MASK))
1059 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001060 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1061 if (sess->listener->counters)
1062 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1063 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001064 }
1065
1066 /* we get here if we need to wait for more data */
1067 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1068 /* Not enough data. We'll re-use the http-request
1069 * timeout here. Ideally, we should set the timeout
1070 * relative to the accept() date. We just set the
1071 * request timeout once at the beginning of the
1072 * request.
1073 */
1074 channel_dont_connect(req);
1075 if (!tick_isset(req->analyse_exp))
1076 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001077 DBG_TRACE_DEVEL("waiting for more data",
1078 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001079 return 0;
1080 }
1081
1082 http_end:
1083 /* The situation will not evolve, so let's give up on the analysis. */
1084 s->logs.tv_request = now; /* update the request timer to reflect full request */
1085 req->analysers &= ~an_bit;
1086 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001087 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001088 return 1;
1089
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001090 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001091 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001092 if (!(s->flags & SF_ERR_MASK))
1093 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001094 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
1095 if (sess->listener->counters)
1096 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
1097 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001098
Christopher Faulete0768eb2018-10-03 16:38:02 +02001099 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001100 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001101 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1102 if (sess->listener->counters)
1103 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1104 /* fall through */
1105
1106 return_prx_cond:
1107 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001108
1109 if (!(s->flags & SF_ERR_MASK))
1110 s->flags |= SF_ERR_PRXCOND;
1111 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001112 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001113
Christopher Faulete0768eb2018-10-03 16:38:02 +02001114 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001115 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001116 DBG_TRACE_DEVEL("leaving on error",
1117 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001118 return 0;
1119}
1120
1121/* This function is an analyser which forwards request body (including chunk
1122 * sizes if any). It is called as soon as we must forward, even if we forward
1123 * zero byte. The only situation where it must not be called is when we're in
1124 * tunnel mode and we want to forward till the close. It's used both to forward
1125 * remaining data and to resync after end of body. It expects the msg_state to
1126 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1127 * read more data, or 1 once we can go on with next request or end the stream.
1128 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1129 * bytes of pending data + the headers if not already done.
1130 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001131int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001132{
1133 struct session *sess = s->sess;
1134 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001135 struct http_msg *msg = &txn->req;
1136 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001137 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001138 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001139
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001140 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001141
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001142 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001143
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001144 if (htx->flags & HTX_FL_PARSING_ERROR)
1145 goto return_bad_req;
1146 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1147 goto return_int_err;
1148
Christopher Faulete0768eb2018-10-03 16:38:02 +02001149 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1150 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1151 /* Output closed while we were sending data. We must abort and
1152 * wake the other side up.
1153 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001154
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001155 /* Don't abort yet if we had L7 retries activated and it
1156 * was a write error, we may recover.
1157 */
1158 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001159 (s->si[1].flags & SI_FL_L7_RETRY)) {
1160 DBG_TRACE_DEVEL("leaving on L7 retry",
1161 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001162 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001163 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001164 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001165 http_end_request(s);
1166 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001167 DBG_TRACE_DEVEL("leaving on error",
1168 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001169 return 1;
1170 }
1171
1172 /* Note that we don't have to send 100-continue back because we don't
1173 * need the data to complete our job, and it's up to the server to
1174 * decide whether to return 100, 417 or anything else in return of
1175 * an "Expect: 100-continue" header.
1176 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001177 if (msg->msg_state == HTTP_MSG_BODY)
1178 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001179
Christopher Faulete0768eb2018-10-03 16:38:02 +02001180 /* in most states, we should abort in case of early close */
1181 channel_auto_close(req);
1182
1183 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001184 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001185 if (req->flags & CF_EOI)
1186 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001187 }
1188 else {
1189 /* We can't process the buffer's contents yet */
1190 req->flags |= CF_WAKE_WRITE;
1191 goto missing_data_or_waiting;
1192 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001193 }
1194
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001195 if (msg->msg_state >= HTTP_MSG_ENDING)
1196 goto ending;
1197
1198 if (txn->meth == HTTP_METH_CONNECT) {
1199 msg->msg_state = HTTP_MSG_ENDING;
1200 goto ending;
1201 }
1202
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001203 /* Forward input data. We get it by removing all outgoing data not
1204 * forwarded yet from HTX data size. If there are some data filters, we
1205 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001206 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001207 if (HAS_REQ_DATA_FILTERS(s)) {
1208 ret = flt_http_payload(s, msg, htx->data);
1209 if (ret < 0)
1210 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001211 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001212 }
1213 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001214 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001215 if (msg->flags & HTTP_MSGF_XFER_LEN)
1216 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001217 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001218
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001219 if (htx->data != co_data(req))
1220 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001221
Christopher Faulet9768c262018-10-22 09:34:31 +02001222 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001223 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1224 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001225 */
1226 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1227 goto missing_data_or_waiting;
1228
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001229 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001230
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001231 ending:
1232 /* other states, ENDING...TUNNEL */
1233 if (msg->msg_state >= HTTP_MSG_DONE)
1234 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001235
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001236 if (HAS_REQ_DATA_FILTERS(s)) {
1237 ret = flt_http_end(s, msg);
1238 if (ret <= 0) {
1239 if (!ret)
1240 goto missing_data_or_waiting;
1241 goto return_bad_req;
1242 }
1243 }
1244
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001245 if (txn->meth == HTTP_METH_CONNECT)
1246 msg->msg_state = HTTP_MSG_TUNNEL;
1247 else {
1248 msg->msg_state = HTTP_MSG_DONE;
1249 req->to_forward = 0;
1250 }
1251
1252 done:
1253 /* we don't want to forward closes on DONE except in tunnel mode. */
1254 if (!(txn->flags & TX_CON_WANT_TUN))
1255 channel_dont_close(req);
1256
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001257 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001258 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001259 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001260 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1261 if (req->flags & CF_SHUTW) {
1262 /* request errors are most likely due to the
1263 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001264 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001265 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001266 goto return_bad_req;
1267 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001268 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001269 return 1;
1270 }
1271
1272 /* If "option abortonclose" is set on the backend, we want to monitor
1273 * the client's connection and forward any shutdown notification to the
1274 * server, which will decide whether to close or to go on processing the
1275 * request. We only do that in tunnel mode, and not in other modes since
1276 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001277 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001278 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001279 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001280 s->si[1].flags |= SI_FL_NOLINGER;
1281 channel_auto_close(req);
1282 }
1283 else if (s->txn->meth == HTTP_METH_POST) {
1284 /* POST requests may require to read extra CRLF sent by broken
1285 * browsers and which could cause an RST to be sent upon close
1286 * on some systems (eg: Linux). */
1287 channel_auto_read(req);
1288 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001289 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1290 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001291 return 0;
1292
1293 missing_data_or_waiting:
1294 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001295 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001296 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001297
1298 waiting:
1299 /* waiting for the last bits to leave the buffer */
1300 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001301 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001302
1303 /* When TE: chunked is used, we need to get there again to parse remaining
1304 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1305 * And when content-length is used, we never want to let the possible
1306 * shutdown be forwarded to the other side, as the state machine will
1307 * take care of it once the client responds. It's also important to
1308 * prevent TIME_WAITs from accumulating on the backend side, and for
1309 * HTTP/2 where the last frame comes with a shutdown.
1310 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001311 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001312 channel_dont_close(req);
1313
1314 /* We know that more data are expected, but we couldn't send more that
1315 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1316 * system knows it must not set a PUSH on this first part. Interactive
1317 * modes are already handled by the stream sock layer. We must not do
1318 * this in content-length mode because it could present the MSG_MORE
1319 * flag with the last block of forwarded data, which would cause an
1320 * additional delay to be observed by the receiver.
1321 */
1322 if (msg->flags & HTTP_MSGF_TE_CHNK)
1323 req->flags |= CF_EXPECT_MORE;
1324
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001325 DBG_TRACE_DEVEL("waiting for more data to forward",
1326 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001327 return 0;
1328
Christopher Faulet93e02d82019-03-08 14:18:50 +01001329 return_cli_abort:
1330 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1331 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
1332 if (objt_server(s->target))
1333 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
1334 if (!(s->flags & SF_ERR_MASK))
1335 s->flags |= SF_ERR_CLICL;
1336 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001337 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001338
1339 return_srv_abort:
1340 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1341 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
1342 if (objt_server(s->target))
1343 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
1344 if (!(s->flags & SF_ERR_MASK))
1345 s->flags |= SF_ERR_SRVCL;
1346 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001347 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001348
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001349 return_int_err:
1350 if (!(s->flags & SF_ERR_MASK))
1351 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001352 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
1353 if (sess->listener->counters)
1354 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001355 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001356 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001357
Christopher Faulet93e02d82019-03-08 14:18:50 +01001358 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001359 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001360 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001361 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001362 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001363 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001364
Christopher Fauletb8a53712019-12-16 11:29:38 +01001365 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001366 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001367 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001368 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001369 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001370 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001371 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001372 }
1373 req->analysers &= AN_REQ_FLT_END;
1374 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 +01001375 if (!(s->flags & SF_ERR_MASK))
1376 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001377 if (!(s->flags & SF_FINST_MASK))
1378 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001379 DBG_TRACE_DEVEL("leaving on error ",
1380 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001381 return 0;
1382}
1383
Olivier Houcharda254a372019-04-05 15:30:12 +02001384/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1385/* Returns 0 if we can attempt to retry, -1 otherwise */
1386static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1387{
1388 struct channel *req, *res;
1389 int co_data;
1390
1391 si->conn_retries--;
1392 if (si->conn_retries < 0)
1393 return -1;
1394
Willy Tarreau223995e2019-05-04 10:38:31 +02001395 if (objt_server(s->target))
1396 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1397 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1398
Olivier Houcharda254a372019-04-05 15:30:12 +02001399 req = &s->req;
1400 res = &s->res;
1401 /* Remove any write error from the request, and read error from the response */
1402 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1403 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1404 res->analysers = 0;
1405 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001406 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001407 si->exp = TICK_ETERNITY;
1408 res->rex = TICK_ETERNITY;
1409 res->to_forward = 0;
1410 res->analyse_exp = TICK_ETERNITY;
1411 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001412 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001413 si_release_endpoint(&s->si[1]);
1414 b_free(&req->buf);
1415 /* Swap the L7 buffer with the channel buffer */
1416 /* We know we stored the co_data as b_data, so get it there */
1417 co_data = b_data(&si->l7_buffer);
1418 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1419 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1420
1421 co_set_data(req, co_data);
1422 b_reset(&res->buf);
1423 co_set_data(res, 0);
1424 return 0;
1425}
1426
Christopher Faulete0768eb2018-10-03 16:38:02 +02001427/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1428 * processing can continue on next analysers, or zero if it either needs more
1429 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1430 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1431 * when it has nothing left to do, and may remove any analyser when it wants to
1432 * abort.
1433 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001434int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001435{
Christopher Faulet9768c262018-10-22 09:34:31 +02001436 /*
1437 * We will analyze a complete HTTP response to check the its syntax.
1438 *
1439 * Once the start line and all headers are received, we may perform a
1440 * capture of the error (if any), and we will set a few fields. We also
1441 * logging and finally headers capture.
1442 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001443 struct session *sess = s->sess;
1444 struct http_txn *txn = s->txn;
1445 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001446 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001447 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001448 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001449 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001450 int n;
1451
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001452 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001453
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001454 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001455
Willy Tarreau4236f032019-03-05 10:43:32 +01001456 /* Parsing errors are caught here */
1457 if (htx->flags & HTX_FL_PARSING_ERROR)
1458 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001459 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1460 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001461
Christopher Faulete0768eb2018-10-03 16:38:02 +02001462 /*
1463 * Now we quickly check if we have found a full valid response.
1464 * If not so, we check the FD and buffer states before leaving.
1465 * A full response is indicated by the fact that we have seen
1466 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1467 * responses are checked first.
1468 *
1469 * Depending on whether the client is still there or not, we
1470 * may send an error response back or not. Note that normally
1471 * we should only check for HTTP status there, and check I/O
1472 * errors somewhere else.
1473 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001474 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001475 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001476 /* 1: have we encountered a read error ? */
1477 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001478 struct connection *conn = NULL;
1479
Olivier Houchard865d8392019-05-03 22:46:27 +02001480 if (objt_cs(s->si[1].end))
1481 conn = objt_cs(s->si[1].end)->conn;
1482
1483 if (si_b->flags & SI_FL_L7_RETRY &&
1484 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001485 /* If we arrive here, then CF_READ_ERROR was
1486 * set by si_cs_recv() because we matched a
1487 * status, overwise it would have removed
1488 * the SI_FL_L7_RETRY flag, so it's ok not
1489 * to check s->be->retry_type.
1490 */
1491 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1492 return 0;
1493 }
1494
Olivier Houchard6db16992019-05-17 15:40:49 +02001495 if (txn->flags & TX_NOT_FIRST)
1496 goto abort_keep_alive;
1497
Olivier Houcharda798bf52019-03-08 18:52:00 +01001498 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001499 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001500 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001501 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001502 }
1503
Christopher Faulete0768eb2018-10-03 16:38:02 +02001504 rep->analysers &= AN_RES_FLT_END;
1505 txn->status = 502;
1506
1507 /* Check to see if the server refused the early data.
1508 * If so, just send a 425
1509 */
Olivier Houchard865d8392019-05-03 22:46:27 +02001510 if (conn->err_code == CO_ER_SSL_EARLY_FAILED) {
1511 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001512 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001513 do_l7_retry(s, si_b) == 0) {
1514 DBG_TRACE_DEVEL("leaving on L7 retry",
1515 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001516 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001517 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001518 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001519 }
1520
1521 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001522 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001523
1524 if (!(s->flags & SF_ERR_MASK))
1525 s->flags |= SF_ERR_SRVCL;
1526 if (!(s->flags & SF_FINST_MASK))
1527 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001528 DBG_TRACE_DEVEL("leaving on error",
1529 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001530 return 0;
1531 }
1532
Christopher Faulet9768c262018-10-22 09:34:31 +02001533 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001534 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001535 if ((si_b->flags & SI_FL_L7_RETRY) &&
1536 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001537 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1538 DBG_TRACE_DEVEL("leaving on L7 retry",
1539 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001540 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001541 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001542 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001543 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001544 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001545 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001546 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001547 }
1548
Christopher Faulete0768eb2018-10-03 16:38:02 +02001549 rep->analysers &= AN_RES_FLT_END;
1550 txn->status = 504;
1551 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001552 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001553
1554 if (!(s->flags & SF_ERR_MASK))
1555 s->flags |= SF_ERR_SRVTO;
1556 if (!(s->flags & SF_FINST_MASK))
1557 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001558 DBG_TRACE_DEVEL("leaving on error",
1559 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001560 return 0;
1561 }
1562
Christopher Faulet9768c262018-10-22 09:34:31 +02001563 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001564 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001565 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1566 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001567 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001568 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001569
1570 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001571 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001572 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001573
1574 if (!(s->flags & SF_ERR_MASK))
1575 s->flags |= SF_ERR_CLICL;
1576 if (!(s->flags & SF_FINST_MASK))
1577 s->flags |= SF_FINST_H;
1578
1579 /* process_stream() will take care of the error */
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 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001586 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001587 if ((si_b->flags & SI_FL_L7_RETRY) &&
1588 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001589 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1590 DBG_TRACE_DEVEL("leaving on L7 retry",
1591 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001592 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001593 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001594 }
1595
Olivier Houchard6db16992019-05-17 15:40:49 +02001596 if (txn->flags & TX_NOT_FIRST)
1597 goto abort_keep_alive;
1598
Olivier Houcharda798bf52019-03-08 18:52:00 +01001599 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001600 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001601 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001602 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001603 }
1604
Christopher Faulete0768eb2018-10-03 16:38:02 +02001605 rep->analysers &= AN_RES_FLT_END;
1606 txn->status = 502;
1607 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001608 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001609
1610 if (!(s->flags & SF_ERR_MASK))
1611 s->flags |= SF_ERR_SRVCL;
1612 if (!(s->flags & SF_FINST_MASK))
1613 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001614 DBG_TRACE_DEVEL("leaving on error",
1615 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001616 return 0;
1617 }
1618
Christopher Faulet9768c262018-10-22 09:34:31 +02001619 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001620 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001621 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001622 goto abort_keep_alive;
1623
Olivier Houcharda798bf52019-03-08 18:52:00 +01001624 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001625 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001626
1627 if (!(s->flags & SF_ERR_MASK))
1628 s->flags |= SF_ERR_CLICL;
1629 if (!(s->flags & SF_FINST_MASK))
1630 s->flags |= SF_FINST_H;
1631
1632 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001633 DBG_TRACE_DEVEL("leaving on error",
1634 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001635 return 0;
1636 }
1637
1638 channel_dont_close(rep);
1639 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001640 DBG_TRACE_DEVEL("waiting for more data",
1641 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001642 return 0;
1643 }
1644
1645 /* More interesting part now : we know that we have a complete
1646 * response which at least looks like HTTP. We have an indicator
1647 * of each header's length, so we can parse them quickly.
1648 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001649 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001650 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001651 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001652
Christopher Faulet9768c262018-10-22 09:34:31 +02001653 /* 0: we might have to print this header in debug mode */
1654 if (unlikely((global.mode & MODE_DEBUG) &&
1655 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1656 int32_t pos;
1657
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001658 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001659
Christopher Fauleta3f15502019-05-13 15:27:23 +02001660 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001661 struct htx_blk *blk = htx_get_blk(htx, pos);
1662 enum htx_blk_type type = htx_get_blk_type(blk);
1663
1664 if (type == HTX_BLK_EOH)
1665 break;
1666 if (type != HTX_BLK_HDR)
1667 continue;
1668
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001669 http_debug_hdr("srvhdr", s,
1670 htx_get_blk_name(htx, blk),
1671 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001672 }
1673 }
1674
Christopher Faulet03599112018-11-27 11:21:21 +01001675 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001676 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001677 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001678 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001679 if (sl->flags & HTX_SL_F_XFER_LEN) {
1680 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001681 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001682 if (sl->flags & HTX_SL_F_BODYLESS)
1683 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001684 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001685
1686 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001687 if (n < 1 || n > 5)
1688 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001689
Christopher Faulete0768eb2018-10-03 16:38:02 +02001690 /* when the client triggers a 4xx from the server, it's most often due
1691 * to a missing object or permission. These events should be tracked
1692 * because if they happen often, it may indicate a brute force or a
1693 * vulnerability scan.
1694 */
1695 if (n == 4)
1696 stream_inc_http_err_ctr(s);
1697
1698 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001699 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001700
Christopher Faulete0768eb2018-10-03 16:38:02 +02001701 /* Adjust server's health based on status code. Note: status codes 501
1702 * and 505 are triggered on demand by client request, so we must not
1703 * count them as server failures.
1704 */
1705 if (objt_server(s->target)) {
1706 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001707 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001708 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001709 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001710 }
1711
1712 /*
1713 * We may be facing a 100-continue response, or any other informational
1714 * 1xx response which is non-final, in which case this is not the right
1715 * response, and we're waiting for the next one. Let's allow this response
1716 * to go to the client and wait for the next one. There's an exception for
1717 * 101 which is used later in the code to switch protocols.
1718 */
1719 if (txn->status < 200 &&
1720 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001721 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001722 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001723 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001724 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001725 txn->status = 0;
1726 s->logs.t_data = -1; /* was not a response yet */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001727 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001728 }
1729
1730 /*
1731 * 2: check for cacheability.
1732 */
1733
1734 switch (txn->status) {
1735 case 200:
1736 case 203:
1737 case 204:
1738 case 206:
1739 case 300:
1740 case 301:
1741 case 404:
1742 case 405:
1743 case 410:
1744 case 414:
1745 case 501:
1746 break;
1747 default:
1748 /* RFC7231#6.1:
1749 * Responses with status codes that are defined as
1750 * cacheable by default (e.g., 200, 203, 204, 206,
1751 * 300, 301, 404, 405, 410, 414, and 501 in this
1752 * specification) can be reused by a cache with
1753 * heuristic expiration unless otherwise indicated
1754 * by the method definition or explicit cache
1755 * controls [RFC7234]; all other status codes are
1756 * not cacheable by default.
1757 */
1758 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1759 break;
1760 }
1761
1762 /*
1763 * 3: we may need to capture headers
1764 */
1765 s->logs.logwait &= ~LW_RESP;
1766 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001767 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001768
Christopher Faulet9768c262018-10-22 09:34:31 +02001769 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001770 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1771 txn->status == 101)) {
1772 /* Either we've established an explicit tunnel, or we're
1773 * switching the protocol. In both cases, we're very unlikely
1774 * to understand the next protocols. We have to switch to tunnel
1775 * mode, so that we transfer the request and responses then let
1776 * this protocol pass unmodified. When we later implement specific
1777 * parsers for such protocols, we'll want to check the Upgrade
1778 * header which contains information about that protocol for
1779 * responses with status 101 (eg: see RFC2817 about TLS).
1780 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001781 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001782 }
1783
Christopher Faulet61608322018-11-23 16:23:45 +01001784 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1785 * 407 (Proxy-Authenticate) responses and set the connection to private
1786 */
1787 srv_conn = cs_conn(objt_cs(s->si[1].end));
1788 if (srv_conn) {
1789 struct ist hdr;
1790 struct http_hdr_ctx ctx;
1791
1792 if (txn->status == 401)
1793 hdr = ist("WWW-Authenticate");
1794 else if (txn->status == 407)
1795 hdr = ist("Proxy-Authenticate");
1796 else
1797 goto end;
1798
1799 ctx.blk = NULL;
1800 while (http_find_header(htx, hdr, &ctx, 0)) {
1801 if ((ctx.value.len >= 9 && word_match(ctx.value.ptr, ctx.value.len, "Negotiate", 9)) ||
Olivier Houchard250031e2019-05-29 15:01:50 +02001802 (ctx.value.len >= 4 && word_match(ctx.value.ptr, ctx.value.len, "NTLM", 4))) {
1803 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet61608322018-11-23 16:23:45 +01001804 srv_conn->flags |= CO_FL_PRIVATE;
Olivier Houchard250031e2019-05-29 15:01:50 +02001805 }
Christopher Faulet61608322018-11-23 16:23:45 +01001806 }
1807 }
1808
1809 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001810 /* we want to have the response time before we start processing it */
1811 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1812
1813 /* end of job, return OK */
1814 rep->analysers &= ~an_bit;
1815 rep->analyse_exp = TICK_ETERNITY;
1816 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001817 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001818 return 1;
1819
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001820 return_int_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +01001821 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
1822 if (objt_server(s->target))
1823 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001824 txn->status = 500;
1825 if (!(s->flags & SF_ERR_MASK))
1826 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001827 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001828
1829 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001830 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001831 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001832 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001833 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001834 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001835 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001836 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001837 do_l7_retry(s, si_b) == 0) {
1838 DBG_TRACE_DEVEL("leaving on L7 retry",
1839 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001840 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001841 }
Christopher Faulet47365272018-10-31 17:40:50 +01001842 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001843 /* fall through */
1844
Christopher Fauletb8a53712019-12-16 11:29:38 +01001845 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001846 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001847
1848 if (!(s->flags & SF_ERR_MASK))
1849 s->flags |= SF_ERR_PRXCOND;
1850 if (!(s->flags & SF_FINST_MASK))
1851 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001852
1853 s->si[1].flags |= SI_FL_NOLINGER;
1854 rep->analysers &= AN_RES_FLT_END;
1855 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001856 DBG_TRACE_DEVEL("leaving on error",
1857 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001858 return 0;
1859
Christopher Faulete0768eb2018-10-03 16:38:02 +02001860 abort_keep_alive:
1861 /* A keep-alive request to the server failed on a network error.
1862 * The client is required to retry. We need to close without returning
1863 * any other information so that the client retries.
1864 */
1865 txn->status = 0;
1866 rep->analysers &= AN_RES_FLT_END;
1867 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001868 s->logs.logwait = 0;
1869 s->logs.level = 0;
1870 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001871 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001872 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1873 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001874 return 0;
1875}
1876
1877/* This function performs all the processing enabled for the current response.
1878 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1879 * and updates s->res.analysers. It might make sense to explode it into several
1880 * other functions. It works like process_request (see indications above).
1881 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001882int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001883{
1884 struct session *sess = s->sess;
1885 struct http_txn *txn = s->txn;
1886 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001887 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001888 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001889 enum rule_result ret = HTTP_RULE_RES_CONT;
1890
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001891 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1892 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001893
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001894 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001895
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001896 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001897
1898 /* The stats applet needs to adjust the Connection header but we don't
1899 * apply any filter there.
1900 */
1901 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1902 rep->analysers &= ~an_bit;
1903 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001904 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001905 }
1906
1907 /*
1908 * We will have to evaluate the filters.
1909 * As opposed to version 1.2, now they will be evaluated in the
1910 * filters order and not in the header order. This means that
1911 * each filter has to be validated among all headers.
1912 *
1913 * Filters are tried with ->be first, then with ->fe if it is
1914 * different from ->be.
1915 *
1916 * Maybe we are in resume condiion. In this case I choose the
1917 * "struct proxy" which contains the rule list matching the resume
1918 * pointer. If none of theses "struct proxy" match, I initialise
1919 * the process with the first one.
1920 *
1921 * In fact, I check only correspondance betwwen the current list
1922 * pointer and the ->fe rule list. If it doesn't match, I initialize
1923 * the loop with the ->be.
1924 */
1925 if (s->current_rule_list == &sess->fe->http_res_rules)
1926 cur_proxy = sess->fe;
1927 else
1928 cur_proxy = s->be;
1929 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001930 /* evaluate http-response rules */
1931 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001932 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001933
Christopher Fauletb8a53712019-12-16 11:29:38 +01001934 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001935
Christopher Fauletb8a53712019-12-16 11:29:38 +01001936 if (ret == HTTP_RULE_RES_DONE)
1937 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001938 }
1939
1940 /* we need to be called again. */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001941 if (ret == HTTP_RULE_RES_YIELD)
1942 goto return_prx_yield;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001943
Christopher Faulete0768eb2018-10-03 16:38:02 +02001944 /* has the response been denied ? */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001945 if (txn->flags & TX_SVDENY)
1946 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001947
Christopher Faulete0768eb2018-10-03 16:38:02 +02001948 /* check whether we're already working on the frontend */
1949 if (cur_proxy == sess->fe)
1950 break;
1951 cur_proxy = sess->fe;
1952 }
1953
1954 /* After this point, this anayzer can't return yield, so we can
1955 * remove the bit corresponding to this analyzer from the list.
1956 *
1957 * Note that the intermediate returns and goto found previously
1958 * reset the analyzers.
1959 */
1960 rep->analysers &= ~an_bit;
1961 rep->analyse_exp = TICK_ETERNITY;
1962
1963 /* OK that's all we can do for 1xx responses */
1964 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02001965 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001966
1967 /*
1968 * Now check for a server cookie.
1969 */
1970 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001971 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001972
1973 /*
1974 * Check for cache-control or pragma headers if required.
1975 */
1976 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001977 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001978
1979 /*
1980 * Add server cookie in the response if needed
1981 */
1982 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
1983 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
1984 (!(s->flags & SF_DIRECT) ||
1985 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
1986 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
1987 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
1988 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
1989 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
1990 !(s->flags & SF_IGNORE_PRST)) {
1991 /* the server is known, it's not the one the client requested, or the
1992 * cookie's last seen date needs to be refreshed. We have to
1993 * insert a set-cookie here, except if we want to insert only on POST
1994 * requests and this one isn't. Note that servers which don't have cookies
1995 * (eg: some backup servers) will return a full cookie removal request.
1996 */
1997 if (!objt_server(s->target)->cookie) {
1998 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001999 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02002000 s->be->cookie_name);
2001 }
2002 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002003 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002004
2005 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2006 /* emit last_date, which is mandatory */
2007 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2008 s30tob64((date.tv_sec+3) >> 2,
2009 trash.area + trash.data);
2010 trash.data += 5;
2011
2012 if (s->be->cookie_maxlife) {
2013 /* emit first_date, which is either the original one or
2014 * the current date.
2015 */
2016 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2017 s30tob64(txn->cookie_first_date ?
2018 txn->cookie_first_date >> 2 :
2019 (date.tv_sec+3) >> 2,
2020 trash.area + trash.data);
2021 trash.data += 5;
2022 }
2023 }
2024 chunk_appendf(&trash, "; path=/");
2025 }
2026
2027 if (s->be->cookie_domain)
2028 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2029
2030 if (s->be->ck_opts & PR_CK_HTTPONLY)
2031 chunk_appendf(&trash, "; HttpOnly");
2032
2033 if (s->be->ck_opts & PR_CK_SECURE)
2034 chunk_appendf(&trash, "; Secure");
2035
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002036 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002037 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002038
2039 txn->flags &= ~TX_SCK_MASK;
2040 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2041 /* the server did not change, only the date was updated */
2042 txn->flags |= TX_SCK_UPDATED;
2043 else
2044 txn->flags |= TX_SCK_INSERTED;
2045
2046 /* Here, we will tell an eventual cache on the client side that we don't
2047 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2048 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2049 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2050 */
2051 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2052
2053 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2054
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002055 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002056 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002057 }
2058 }
2059
2060 /*
2061 * Check if result will be cacheable with a cookie.
2062 * We'll block the response if security checks have caught
2063 * nasty things such as a cacheable cookie.
2064 */
2065 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2066 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2067 (s->be->options & PR_O_CHK_CACHE)) {
2068 /* we're in presence of a cacheable response containing
2069 * a set-cookie header. We'll block it as requested by
2070 * the 'checkcache' option, and send an alert.
2071 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002072 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2073 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2074 send_log(s->be, LOG_ALERT,
2075 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2076 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002077 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002078 }
2079
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002080 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002081 /* Always enter in the body analyzer */
2082 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2083 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2084
2085 /* if the user wants to log as soon as possible, without counting
2086 * bytes from the server, then this is the right moment. We have
2087 * to temporarily assign bytes_out to log what we currently have.
2088 */
2089 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2090 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002091 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002092 s->do_log(s);
2093 s->logs.bytes_out = 0;
2094 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002095 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002096 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002097
Christopher Fauletb8a53712019-12-16 11:29:38 +01002098 done:
2099 rep->analysers &= ~an_bit;
2100 rep->analyse_exp = TICK_ETERNITY;
2101 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002102
Christopher Fauletb8a53712019-12-16 11:29:38 +01002103 deny:
2104 txn->flags |= TX_CLDENY;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002105 txn->status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002106 if (objt_server(s->target))
2107 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_secu, 1);
2108 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
2109 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
2110 if (sess->listener->counters)
2111 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
2112 goto return_prx_err;
2113
2114 return_int_err:
2115 txn->status = 500;
2116 if (!(s->flags & SF_ERR_MASK))
2117 s->flags |= SF_ERR_INTERNAL;
2118 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2119 if (objt_server(s->target))
2120 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
2121 goto return_prx_err;
2122
2123 return_bad_res:
2124 txn->status = 502;
2125 /* fall through */
2126
2127 return_prx_err:
2128 http_reply_and_close(s, txn->status, http_error_message(s));
2129 /* fall through */
2130
2131 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002132 s->logs.t_data = -1; /* was not a valid response */
2133 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002134
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002135 if (!(s->flags & SF_ERR_MASK))
2136 s->flags |= SF_ERR_PRXCOND;
2137 if (!(s->flags & SF_FINST_MASK))
2138 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002139
2140 rep->analysers &= ~an_bit;
2141 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002142 DBG_TRACE_DEVEL("leaving on error",
2143 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002144 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002145
2146 return_prx_yield:
2147 channel_dont_close(rep);
2148 DBG_TRACE_DEVEL("waiting for more data",
2149 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2150 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002151}
2152
2153/* This function is an analyser which forwards response body (including chunk
2154 * sizes if any). It is called as soon as we must forward, even if we forward
2155 * zero byte. The only situation where it must not be called is when we're in
2156 * tunnel mode and we want to forward till the close. It's used both to forward
2157 * remaining data and to resync after end of body. It expects the msg_state to
2158 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2159 * read more data, or 1 once we can go on with next request or end the stream.
2160 *
2161 * It is capable of compressing response data both in content-length mode and
2162 * in chunked mode. The state machines follows different flows depending on
2163 * whether content-length and chunked modes are used, since there are no
2164 * trailers in content-length :
2165 *
2166 * chk-mode cl-mode
2167 * ,----- BODY -----.
2168 * / \
2169 * V size > 0 V chk-mode
2170 * .--> SIZE -------------> DATA -------------> CRLF
2171 * | | size == 0 | last byte |
2172 * | v final crlf v inspected |
2173 * | TRAILERS -----------> DONE |
2174 * | |
2175 * `----------------------------------------------'
2176 *
2177 * Compression only happens in the DATA state, and must be flushed in final
2178 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2179 * is performed at once on final states for all bytes parsed, or when leaving
2180 * on missing data.
2181 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002182int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002183{
2184 struct session *sess = s->sess;
2185 struct http_txn *txn = s->txn;
2186 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002187 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002188 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002189
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002190 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002191
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002192 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002193
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002194 if (htx->flags & HTX_FL_PARSING_ERROR)
2195 goto return_bad_res;
2196 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2197 goto return_int_err;
2198
Christopher Faulete0768eb2018-10-03 16:38:02 +02002199 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002200 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002201 /* Output closed while we were sending data. We must abort and
2202 * wake the other side up.
2203 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002204 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002205 http_end_response(s);
2206 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002207 DBG_TRACE_DEVEL("leaving on error",
2208 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002209 return 1;
2210 }
2211
Christopher Faulet9768c262018-10-22 09:34:31 +02002212 if (msg->msg_state == HTTP_MSG_BODY)
2213 msg->msg_state = HTTP_MSG_DATA;
2214
Christopher Faulete0768eb2018-10-03 16:38:02 +02002215 /* in most states, we should abort in case of early close */
2216 channel_auto_close(res);
2217
Christopher Faulete0768eb2018-10-03 16:38:02 +02002218 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002219 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002220 if (res->flags & CF_EOI)
2221 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002222 }
2223 else {
2224 /* We can't process the buffer's contents yet */
2225 res->flags |= CF_WAKE_WRITE;
2226 goto missing_data_or_waiting;
2227 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002228 }
2229
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002230 if (msg->msg_state >= HTTP_MSG_ENDING)
2231 goto ending;
2232
2233 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2234 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2235 msg->msg_state = HTTP_MSG_ENDING;
2236 goto ending;
2237 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002238
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002239 /* Forward input data. We get it by removing all outgoing data not
2240 * forwarded yet from HTX data size. If there are some data filters, we
2241 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002242 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002243 if (HAS_RSP_DATA_FILTERS(s)) {
2244 ret = flt_http_payload(s, msg, htx->data);
2245 if (ret < 0)
2246 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002247 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002248 }
2249 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002250 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002251 if (msg->flags & HTTP_MSGF_XFER_LEN)
2252 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002253 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002254
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002255 if (htx->data != co_data(res))
2256 goto missing_data_or_waiting;
2257
2258 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2259 msg->msg_state = HTTP_MSG_ENDING;
2260 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002261 }
2262
Christopher Faulet9768c262018-10-22 09:34:31 +02002263 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002264 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2265 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002266 */
2267 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2268 goto missing_data_or_waiting;
2269
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002270 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002271
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002272 ending:
2273 /* other states, ENDING...TUNNEL */
2274 if (msg->msg_state >= HTTP_MSG_DONE)
2275 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002276
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002277 if (HAS_RSP_DATA_FILTERS(s)) {
2278 ret = flt_http_end(s, msg);
2279 if (ret <= 0) {
2280 if (!ret)
2281 goto missing_data_or_waiting;
2282 goto return_bad_res;
2283 }
2284 }
2285
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002286 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2287 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2288 msg->msg_state = HTTP_MSG_TUNNEL;
2289 goto ending;
2290 }
2291 else {
2292 msg->msg_state = HTTP_MSG_DONE;
2293 res->to_forward = 0;
2294 }
2295
2296 done:
2297
2298 channel_dont_close(res);
2299
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002300 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002301 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002302 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002303 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2304 if (res->flags & CF_SHUTW) {
2305 /* response errors are most likely due to the
2306 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002307 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002308 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002309 goto return_bad_res;
2310 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002311 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002312 return 1;
2313 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002314 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2315 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002316 return 0;
2317
2318 missing_data_or_waiting:
2319 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002320 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002321
2322 /* stop waiting for data if the input is closed before the end. If the
2323 * client side was already closed, it means that the client has aborted,
2324 * so we don't want to count this as a server abort. Otherwise it's a
2325 * server abort.
2326 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002327 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002328 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002329 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002330 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002331 if (htx_is_empty(htx))
2332 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002333 }
2334
Christopher Faulete0768eb2018-10-03 16:38:02 +02002335 /* When TE: chunked is used, we need to get there again to parse
2336 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002337 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2338 * are filters registered on the stream, we don't want to forward a
2339 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002340 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002341 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002342 channel_dont_close(res);
2343
2344 /* We know that more data are expected, but we couldn't send more that
2345 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2346 * system knows it must not set a PUSH on this first part. Interactive
2347 * modes are already handled by the stream sock layer. We must not do
2348 * this in content-length mode because it could present the MSG_MORE
2349 * flag with the last block of forwarded data, which would cause an
2350 * additional delay to be observed by the receiver.
2351 */
2352 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2353 res->flags |= CF_EXPECT_MORE;
2354
2355 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002356 DBG_TRACE_DEVEL("waiting for more data to forward",
2357 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002358 return 0;
2359
Christopher Faulet93e02d82019-03-08 14:18:50 +01002360 return_srv_abort:
2361 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2362 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002363 if (objt_server(s->target))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002364 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1);
2365 if (!(s->flags & SF_ERR_MASK))
2366 s->flags |= SF_ERR_SRVCL;
2367 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002368
Christopher Faulet93e02d82019-03-08 14:18:50 +01002369 return_cli_abort:
2370 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2371 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002372 if (objt_server(s->target))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002373 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
2374 if (!(s->flags & SF_ERR_MASK))
2375 s->flags |= SF_ERR_CLICL;
2376 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002377
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002378 return_int_err:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002379 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2380 if (objt_server(s->target))
2381 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002382 if (!(s->flags & SF_ERR_MASK))
2383 s->flags |= SF_ERR_INTERNAL;
2384 goto return_error;
2385
Christopher Faulet93e02d82019-03-08 14:18:50 +01002386 return_bad_res:
2387 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2388 if (objt_server(s->target)) {
2389 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1);
2390 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2391 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002392 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002393 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002394 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002395
Christopher Faulet93e02d82019-03-08 14:18:50 +01002396 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002397 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002398 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002399 res->analysers &= AN_RES_FLT_END;
2400 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 +02002401 if (!(s->flags & SF_FINST_MASK))
2402 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002403 DBG_TRACE_DEVEL("leaving on error",
2404 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002405 return 0;
2406}
2407
Christopher Fauletf2824e62018-10-01 12:12:37 +02002408/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002409 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002410 * as too large a request to build a valid response.
2411 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002412int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002413{
Christopher Faulet99daf282018-11-28 22:58:13 +01002414 struct channel *req = &s->req;
2415 struct channel *res = &s->res;
2416 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002417 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002418 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002419 struct ist status, reason, location;
2420 unsigned int flags;
2421 size_t data;
Christopher Faulet08e66462019-05-23 16:44:59 +02002422 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002423
2424 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002425 if (!chunk) {
2426 if (!(s->flags & SF_ERR_MASK))
2427 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002428 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002429 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002430
Christopher Faulet99daf282018-11-28 22:58:13 +01002431 /*
2432 * Create the location
2433 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002434 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002435 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002436 case REDIRECT_TYPE_SCHEME: {
2437 struct http_hdr_ctx ctx;
2438 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002439
Christopher Faulet99daf282018-11-28 22:58:13 +01002440 host = ist("");
2441 ctx.blk = NULL;
2442 if (http_find_header(htx, ist("Host"), &ctx, 0))
2443 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002444
Christopher Faulet297fbb42019-05-13 14:41:27 +02002445 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002446 path = http_get_path(htx_sl_req_uri(sl));
2447 /* build message using path */
2448 if (path.ptr) {
2449 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2450 int qs = 0;
2451 while (qs < path.len) {
2452 if (*(path.ptr + qs) == '?') {
2453 path.len = qs;
2454 break;
2455 }
2456 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002457 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002458 }
2459 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002460 else
2461 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002462
Christopher Faulet99daf282018-11-28 22:58:13 +01002463 if (rule->rdr_str) { /* this is an old "redirect" rule */
2464 /* add scheme */
2465 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2466 goto fail;
2467 }
2468 else {
2469 /* add scheme with executing log format */
2470 chunk->data += build_logline(s, chunk->area + chunk->data,
2471 chunk->size - chunk->data,
2472 &rule->rdr_fmt);
2473 }
2474 /* add "://" + host + path */
2475 if (!chunk_memcat(chunk, "://", 3) ||
2476 !chunk_memcat(chunk, host.ptr, host.len) ||
2477 !chunk_memcat(chunk, path.ptr, path.len))
2478 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002479
Christopher Faulet99daf282018-11-28 22:58:13 +01002480 /* append a slash at the end of the location if needed and missing */
2481 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2482 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2483 if (chunk->data + 1 >= chunk->size)
2484 goto fail;
2485 chunk->area[chunk->data++] = '/';
2486 }
2487 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002488 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002489
Christopher Faulet99daf282018-11-28 22:58:13 +01002490 case REDIRECT_TYPE_PREFIX: {
2491 struct ist path;
2492
Christopher Faulet297fbb42019-05-13 14:41:27 +02002493 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002494 path = http_get_path(htx_sl_req_uri(sl));
2495 /* build message using path */
2496 if (path.ptr) {
2497 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2498 int qs = 0;
2499 while (qs < path.len) {
2500 if (*(path.ptr + qs) == '?') {
2501 path.len = qs;
2502 break;
2503 }
2504 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002505 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002506 }
2507 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002508 else
2509 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002510
Christopher Faulet99daf282018-11-28 22:58:13 +01002511 if (rule->rdr_str) { /* this is an old "redirect" rule */
2512 /* add prefix. Note that if prefix == "/", we don't want to
2513 * add anything, otherwise it makes it hard for the user to
2514 * configure a self-redirection.
2515 */
2516 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2517 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2518 goto fail;
2519 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002520 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002521 else {
2522 /* add prefix with executing log format */
2523 chunk->data += build_logline(s, chunk->area + chunk->data,
2524 chunk->size - chunk->data,
2525 &rule->rdr_fmt);
2526 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002527
Christopher Faulet99daf282018-11-28 22:58:13 +01002528 /* add path */
2529 if (!chunk_memcat(chunk, path.ptr, path.len))
2530 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002531
Christopher Faulet99daf282018-11-28 22:58:13 +01002532 /* append a slash at the end of the location if needed and missing */
2533 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2534 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2535 if (chunk->data + 1 >= chunk->size)
2536 goto fail;
2537 chunk->area[chunk->data++] = '/';
2538 }
2539 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002540 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002541 case REDIRECT_TYPE_LOCATION:
2542 default:
2543 if (rule->rdr_str) { /* this is an old "redirect" rule */
2544 /* add location */
2545 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2546 goto fail;
2547 }
2548 else {
2549 /* add location with executing log format */
2550 chunk->data += build_logline(s, chunk->area + chunk->data,
2551 chunk->size - chunk->data,
2552 &rule->rdr_fmt);
2553 }
2554 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002555 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002556 location = ist2(chunk->area, chunk->data);
2557
2558 /*
2559 * Create the 30x response
2560 */
2561 switch (rule->code) {
2562 case 308:
2563 status = ist("308");
2564 reason = ist("Permanent Redirect");
2565 break;
2566 case 307:
2567 status = ist("307");
2568 reason = ist("Temporary Redirect");
2569 break;
2570 case 303:
2571 status = ist("303");
2572 reason = ist("See Other");
2573 break;
2574 case 301:
2575 status = ist("301");
2576 reason = ist("Moved Permanently");
2577 break;
2578 case 302:
2579 default:
2580 status = ist("302");
2581 reason = ist("Found");
2582 break;
2583 }
2584
Christopher Faulet08e66462019-05-23 16:44:59 +02002585 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2586 close = 1;
2587
Christopher Faulet99daf282018-11-28 22:58:13 +01002588 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002589 /* Trim any possible response */
2590 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002591 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2592 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2593 if (!sl)
2594 goto fail;
2595 sl->info.res.status = rule->code;
2596 s->txn->status = rule->code;
2597
Christopher Faulet08e66462019-05-23 16:44:59 +02002598 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2599 goto fail;
2600
2601 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002602 !htx_add_header(htx, ist("Location"), location))
2603 goto fail;
2604
2605 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2606 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2607 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002608 }
2609
2610 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002611 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2612 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002613 }
2614
Christopher Faulet99daf282018-11-28 22:58:13 +01002615 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2616 goto fail;
2617
Kevin Zhu96b36392020-01-07 09:42:55 +01002618 htx_to_buf(htx, &res->buf);
2619
Christopher Fauletf2824e62018-10-01 12:12:37 +02002620 /* let's log the request time */
2621 s->logs.tv_request = now;
2622
Christopher Faulet06511812019-10-16 09:38:27 +02002623 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet99daf282018-11-28 22:58:13 +01002624 data = htx->data - co_data(res);
Christopher Faulet99daf282018-11-28 22:58:13 +01002625 c_adv(res, data);
2626 res->total += data;
2627
2628 channel_auto_read(req);
2629 channel_abort(req);
2630 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002631 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet99daf282018-11-28 22:58:13 +01002632
2633 res->wex = tick_add_ifset(now_ms, res->wto);
2634 channel_auto_read(res);
2635 channel_auto_close(res);
2636 channel_shutr_now(res);
2637
2638 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002639
2640 if (!(s->flags & SF_ERR_MASK))
2641 s->flags |= SF_ERR_LOCAL;
2642 if (!(s->flags & SF_FINST_MASK))
2643 s->flags |= SF_FINST_R;
2644
Christopher Faulet99daf282018-11-28 22:58:13 +01002645 free_trash_chunk(chunk);
2646 return 1;
2647
2648 fail:
2649 /* If an error occurred, remove the incomplete HTTP response from the
2650 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002651 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002652 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002653 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002654}
2655
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002656int http_transform_header_str(struct stream* s, struct channel *chn, struct htx *htx,
2657 struct ist name, const char *str, struct my_regex *re, int action)
Christopher Faulet72333522018-10-24 11:25:02 +02002658{
2659 struct http_hdr_ctx ctx;
2660 struct buffer *output = get_trash_chunk();
2661
2662 /* find full header is action is ACT_HTTP_REPLACE_HDR */
2663 ctx.blk = NULL;
2664 while (http_find_header(htx, name, &ctx, (action == ACT_HTTP_REPLACE_HDR))) {
2665 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2666 continue;
2667
2668 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2669 if (output->data == -1)
2670 return -1;
2671 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2672 return -1;
2673 }
2674 return 0;
2675}
2676
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002677static int http_transform_header(struct stream* s, struct channel *chn, struct htx *htx,
2678 const struct ist name, struct list *fmt, struct my_regex *re, int action)
Christopher Faulet72333522018-10-24 11:25:02 +02002679{
2680 struct buffer *replace;
2681 int ret = -1;
2682
2683 replace = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002684 if (!replace) {
2685 if (!(s->flags & SF_ERR_MASK))
2686 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet72333522018-10-24 11:25:02 +02002687 goto leave;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002688 }
Christopher Faulet72333522018-10-24 11:25:02 +02002689
2690 replace->data = build_logline(s, replace->area, replace->size, fmt);
2691 if (replace->data >= replace->size - 1)
2692 goto leave;
2693
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002694 ret = http_transform_header_str(s, chn, htx, name, replace->area, re, action);
Christopher Faulet72333522018-10-24 11:25:02 +02002695
2696 leave:
2697 free_trash_chunk(replace);
2698 return ret;
2699}
2700
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002701
2702/* Terminate a 103-Erly-hints response and send it to the client. It returns 0
2703 * on success and -1 on error. The response channel is updated accordingly.
2704 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002705static int http_reply_103_early_hints(struct channel *res)
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002706{
2707 struct htx *htx = htx_from_buf(&res->buf);
2708 size_t data;
2709
Christopher Faulet1d5ec092019-06-26 14:23:54 +02002710 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002711 /* If an error occurred during an Early-hint rule,
2712 * remove the incomplete HTTP 103 response from the
2713 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002714 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002715 return -1;
2716 }
2717
2718 data = htx->data - co_data(res);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002719 c_adv(res, data);
2720 res->total += data;
2721 return 0;
2722}
2723
Christopher Faulet6eb92892018-11-15 16:39:29 +01002724/*
2725 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
2726 * built according to <fmt> log line format.
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002727 * If <early_hints> is 0, it is starts a new response by adding the start
2728 * line. If an error occurred -1 is returned. On success 0 is returned. The
2729 * channel is not updated here. It must be done calling the function
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002730 * http_reply_103_early_hints().
Christopher Faulet6eb92892018-11-15 16:39:29 +01002731 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002732static 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 +01002733{
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002734 struct channel *res = &s->res;
2735 struct htx *htx = htx_from_buf(&res->buf);
2736 struct buffer *value = alloc_trash_chunk();
2737
Christopher Fauletb8a53712019-12-16 11:29:38 +01002738 if (!value) {
2739 if (!(s->flags & SF_ERR_MASK))
2740 s->flags |= SF_ERR_RESOURCE;
2741 goto fail;
2742 }
2743
Christopher Faulet6eb92892018-11-15 16:39:29 +01002744 if (!early_hints) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002745 struct htx_sl *sl;
2746 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
2747 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2748
2749 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
2750 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
2751 if (!sl)
Christopher Faulet6eb92892018-11-15 16:39:29 +01002752 goto fail;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002753 sl->info.res.status = 103;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002754 }
2755
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002756 value->data = build_logline(s, b_tail(value), b_room(value), fmt);
2757 if (!htx_add_header(htx, name, ist2(b_head(value), b_data(value))))
Christopher Faulet6eb92892018-11-15 16:39:29 +01002758 goto fail;
2759
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002760 free_trash_chunk(value);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002761 return 1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002762
2763 fail:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002764 /* If an error occurred during an Early-hint rule, remove the incomplete
2765 * HTTP 103 response from the buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002766 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002767 free_trash_chunk(value);
2768 return -1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002769}
2770
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002771/* This function executes one of the set-{method,path,query,uri} actions. It
2772 * takes the string from the variable 'replace' with length 'len', then modifies
2773 * the relevant part of the request line accordingly. Then it updates various
2774 * pointers to the next elements which were moved, and the total buffer length.
2775 * It finds the action to be performed in p[2], previously filled by function
2776 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2777 * error, though this can be revisited when this code is finally exploited.
2778 *
2779 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2780 * query string and 3 to replace uri.
2781 *
2782 * In query string case, the mark question '?' must be set at the start of the
2783 * string by the caller, event if the replacement query string is empty.
2784 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002785int http_req_replace_stline(int action, const char *replace, int len,
2786 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002787{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002788 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002789
2790 switch (action) {
2791 case 0: // method
2792 if (!http_replace_req_meth(htx, ist2(replace, len)))
2793 return -1;
2794 break;
2795
2796 case 1: // path
2797 if (!http_replace_req_path(htx, ist2(replace, len)))
2798 return -1;
2799 break;
2800
2801 case 2: // query
2802 if (!http_replace_req_query(htx, ist2(replace, len)))
2803 return -1;
2804 break;
2805
2806 case 3: // uri
2807 if (!http_replace_req_uri(htx, ist2(replace, len)))
2808 return -1;
2809 break;
2810
2811 default:
2812 return -1;
2813 }
2814 return 0;
2815}
2816
2817/* This function replace the HTTP status code and the associated message. The
2818 * variable <status> contains the new status code. This function never fails.
2819 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002820void http_res_set_status(unsigned int status, const char *reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002821{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002822 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002823 char *res;
2824
2825 chunk_reset(&trash);
2826 res = ultoa_o(status, trash.area, trash.size);
2827 trash.data = res - trash.area;
2828
2829 /* Do we have a custom reason format string? */
2830 if (reason == NULL)
2831 reason = http_get_reason(status);
2832
Christopher Faulet87a2c0d2018-12-13 21:58:18 +01002833 if (http_replace_res_status(htx, ist2(trash.area, trash.data)))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002834 http_replace_res_reason(htx, ist2(reason, strlen(reason)));
2835}
2836
Christopher Faulet3e964192018-10-24 11:39:23 +02002837/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2838 * transaction <txn>. Returns the verdict of the first rule that prevents
2839 * further processing of the request (auth, deny, ...), and defaults to
2840 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2841 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2842 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2843 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2844 * status.
2845 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002846static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
2847 struct stream *s, int *deny_status)
Christopher Faulet3e964192018-10-24 11:39:23 +02002848{
2849 struct session *sess = strm_sess(s);
2850 struct http_txn *txn = s->txn;
2851 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002852 struct act_rule *rule;
2853 struct http_hdr_ctx ctx;
2854 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002855 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2856 int act_flags = 0;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002857 int early_hints = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002858
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002859 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002860
2861 /* If "the current_rule_list" match the executed rule list, we are in
2862 * resume condition. If a resume is needed it is always in the action
2863 * and never in the ACL or converters. In this case, we initialise the
2864 * current rule, and go to the action execution point.
2865 */
2866 if (s->current_rule) {
2867 rule = s->current_rule;
2868 s->current_rule = NULL;
2869 if (s->current_rule_list == rules)
2870 goto resume_execution;
2871 }
2872 s->current_rule_list = rules;
2873
2874 list_for_each_entry(rule, rules, list) {
2875 /* check optional condition */
2876 if (rule->cond) {
2877 int ret;
2878
2879 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2880 ret = acl_pass(ret);
2881
2882 if (rule->cond->pol == ACL_COND_UNLESS)
2883 ret = !ret;
2884
2885 if (!ret) /* condition not matched */
2886 continue;
2887 }
2888
2889 act_flags |= ACT_FLAG_FIRST;
2890 resume_execution:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002891 if (early_hints && rule->action != ACT_HTTP_EARLY_HINT) {
2892 early_hints = 0;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002893 if (http_reply_103_early_hints(&s->res) == -1) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002894 rule_ret = HTTP_RULE_RES_BADREQ;
2895 goto end;
2896 }
2897 }
2898
Christopher Faulet3e964192018-10-24 11:39:23 +02002899 switch (rule->action) {
2900 case ACT_ACTION_ALLOW:
2901 rule_ret = HTTP_RULE_RES_STOP;
2902 goto end;
2903
2904 case ACT_ACTION_DENY:
2905 if (deny_status)
2906 *deny_status = rule->deny_status;
2907 rule_ret = HTTP_RULE_RES_DENY;
2908 goto end;
2909
2910 case ACT_HTTP_REQ_TARPIT:
2911 txn->flags |= TX_CLTARPIT;
2912 if (deny_status)
2913 *deny_status = rule->deny_status;
2914 rule_ret = HTTP_RULE_RES_DENY;
2915 goto end;
2916
2917 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002918 /* Auth might be performed on regular http-req rules as well as on stats */
2919 auth_realm = rule->arg.auth.realm;
2920 if (!auth_realm) {
2921 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2922 auth_realm = STATS_DEFAULT_REALM;
2923 else
2924 auth_realm = px->id;
2925 }
2926 /* send 401/407 depending on whether we use a proxy or not. We still
2927 * count one error, because normal browsing won't significantly
2928 * increase the counter but brute force attempts will.
2929 */
Christopher Faulet3e964192018-10-24 11:39:23 +02002930 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002931 if (http_reply_40x_unauthorized(s, auth_realm) == -1)
Christopher Faulet12c51e22018-11-28 15:59:42 +01002932 rule_ret = HTTP_RULE_RES_BADREQ;
2933 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02002934 goto end;
2935
2936 case ACT_HTTP_REDIR:
Christopher Faulet3e964192018-10-24 11:39:23 +02002937 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002938 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3e964192018-10-24 11:39:23 +02002939 rule_ret = HTTP_RULE_RES_BADREQ;
2940 goto end;
2941
2942 case ACT_HTTP_SET_NICE:
2943 s->task->nice = rule->arg.nice;
2944 break;
2945
2946 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01002947 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Christopher Faulet3e964192018-10-24 11:39:23 +02002948 break;
2949
2950 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01002951 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Christopher Faulet3e964192018-10-24 11:39:23 +02002952 break;
2953
2954 case ACT_HTTP_SET_LOGL:
2955 s->logs.level = rule->arg.loglevel;
2956 break;
2957
2958 case ACT_HTTP_REPLACE_HDR:
2959 case ACT_HTTP_REPLACE_VAL:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002960 if (http_transform_header(s, &s->req, htx,
2961 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
2962 &rule->arg.hdr_add.fmt,
2963 rule->arg.hdr_add.re, rule->action)) {
Christopher Faulet3e964192018-10-24 11:39:23 +02002964 rule_ret = HTTP_RULE_RES_BADREQ;
2965 goto end;
2966 }
2967 break;
2968
2969 case ACT_HTTP_DEL_HDR:
2970 /* remove all occurrences of the header */
2971 ctx.blk = NULL;
2972 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
2973 http_remove_header(htx, &ctx);
2974 break;
2975
2976 case ACT_HTTP_SET_HDR:
2977 case ACT_HTTP_ADD_HDR: {
2978 /* The scope of the trash buffer must be limited to this function. The
2979 * build_logline() function can execute a lot of other function which
2980 * can use the trash buffer. So for limiting the scope of this global
2981 * buffer, we build first the header value using build_logline, and
2982 * after we store the header name.
2983 */
2984 struct buffer *replace;
2985 struct ist n, v;
2986
2987 replace = alloc_trash_chunk();
2988 if (!replace) {
2989 rule_ret = HTTP_RULE_RES_BADREQ;
2990 goto end;
2991 }
2992
2993 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
2994 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
2995 v = ist2(replace->area, replace->data);
2996
2997 if (rule->action == ACT_HTTP_SET_HDR) {
2998 /* remove all occurrences of the header */
2999 ctx.blk = NULL;
3000 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3001 http_remove_header(htx, &ctx);
3002 }
3003
3004 if (!http_add_header(htx, n, v)) {
3005 static unsigned char rate_limit = 0;
3006
3007 if ((rate_limit++ & 255) == 0) {
3008 send_log(px, LOG_WARNING, "Proxy %s failed to add or set the request header '%.*s' for request #%u. You might need to increase tune.maxrewrite.", px->id, (int)n.len, n.ptr, s->uniq_id);
3009 }
3010
Olivier Houcharda798bf52019-03-08 18:52:00 +01003011 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003012 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003013 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003014 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003015 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003016 }
3017 free_trash_chunk(replace);
3018 break;
3019 }
3020
3021 case ACT_HTTP_DEL_ACL:
3022 case ACT_HTTP_DEL_MAP: {
3023 struct pat_ref *ref;
3024 struct buffer *key;
3025
3026 /* collect reference */
3027 ref = pat_ref_lookup(rule->arg.map.ref);
3028 if (!ref)
3029 continue;
3030
3031 /* allocate key */
3032 key = alloc_trash_chunk();
3033 if (!key) {
3034 rule_ret = HTTP_RULE_RES_BADREQ;
3035 goto end;
3036 }
3037
3038 /* collect key */
3039 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3040 key->area[key->data] = '\0';
3041
3042 /* perform update */
3043 /* returned code: 1=ok, 0=ko */
3044 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3045 pat_ref_delete(ref, key->area);
3046 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3047
3048 free_trash_chunk(key);
3049 break;
3050 }
3051
3052 case ACT_HTTP_ADD_ACL: {
3053 struct pat_ref *ref;
3054 struct buffer *key;
3055
3056 /* collect reference */
3057 ref = pat_ref_lookup(rule->arg.map.ref);
3058 if (!ref)
3059 continue;
3060
3061 /* allocate key */
3062 key = alloc_trash_chunk();
3063 if (!key) {
3064 rule_ret = HTTP_RULE_RES_BADREQ;
3065 goto end;
3066 }
3067
3068 /* collect key */
3069 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3070 key->area[key->data] = '\0';
3071
3072 /* perform update */
3073 /* add entry only if it does not already exist */
3074 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3075 if (pat_ref_find_elt(ref, key->area) == NULL)
3076 pat_ref_add(ref, key->area, NULL, NULL);
3077 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3078
3079 free_trash_chunk(key);
3080 break;
3081 }
3082
3083 case ACT_HTTP_SET_MAP: {
3084 struct pat_ref *ref;
3085 struct buffer *key, *value;
3086
3087 /* collect reference */
3088 ref = pat_ref_lookup(rule->arg.map.ref);
3089 if (!ref)
3090 continue;
3091
3092 /* allocate key */
3093 key = alloc_trash_chunk();
3094 if (!key) {
3095 rule_ret = HTTP_RULE_RES_BADREQ;
3096 goto end;
3097 }
3098
3099 /* allocate value */
3100 value = alloc_trash_chunk();
3101 if (!value) {
3102 free_trash_chunk(key);
3103 rule_ret = HTTP_RULE_RES_BADREQ;
3104 goto end;
3105 }
3106
3107 /* collect key */
3108 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3109 key->area[key->data] = '\0';
3110
3111 /* collect value */
3112 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3113 value->area[value->data] = '\0';
3114
3115 /* perform update */
Christopher Faulete84289e2019-04-19 14:50:55 +02003116 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003117 if (pat_ref_find_elt(ref, key->area) != NULL)
3118 /* update entry if it exists */
3119 pat_ref_set(ref, key->area, value->area, NULL);
3120 else
3121 /* insert a new entry */
3122 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02003123 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003124 free_trash_chunk(key);
3125 free_trash_chunk(value);
3126 break;
3127 }
3128
3129 case ACT_HTTP_EARLY_HINT:
3130 if (!(txn->req.flags & HTTP_MSGF_VER_11))
3131 break;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003132 early_hints = http_add_early_hint_header(s, early_hints,
3133 ist2(rule->arg.early_hint.name, rule->arg.early_hint.name_len),
3134 &rule->arg.early_hint.fmt);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003135 if (early_hints == -1) {
3136 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02003137 goto end;
3138 }
3139 break;
3140
3141 case ACT_CUSTOM:
3142 if ((s->req.flags & CF_READ_ERROR) ||
3143 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Christopher Faulet3e964192018-10-24 11:39:23 +02003144 (px->options & PR_O_ABRT_CLOSE)))
3145 act_flags |= ACT_FLAG_FINAL;
3146
3147 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
3148 case ACT_RET_ERR:
Christopher Faulet28160e72019-12-13 09:36:11 +01003149 rule_ret = HTTP_RULE_RES_BADREQ;
3150 goto end;
Christopher Faulet3e964192018-10-24 11:39:23 +02003151 case ACT_RET_CONT:
3152 break;
3153 case ACT_RET_STOP:
Christopher Faulet8f1aa772019-07-04 11:27:15 +02003154 rule_ret = HTTP_RULE_RES_STOP;
3155 goto end;
3156 case ACT_RET_DONE:
Christopher Faulet3e964192018-10-24 11:39:23 +02003157 rule_ret = HTTP_RULE_RES_DONE;
3158 goto end;
3159 case ACT_RET_YIELD:
3160 s->current_rule = rule;
3161 rule_ret = HTTP_RULE_RES_YIELD;
3162 goto end;
3163 }
3164 break;
3165
3166 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3167 /* Note: only the first valid tracking parameter of each
3168 * applies.
3169 */
3170
3171 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3172 struct stktable *t;
3173 struct stksess *ts;
3174 struct stktable_key *key;
3175 void *ptr1, *ptr2;
3176
3177 t = rule->arg.trk_ctr.table.t;
3178 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL,
3179 rule->arg.trk_ctr.expr, NULL);
3180
3181 if (key && (ts = stktable_get_entry(t, key))) {
3182 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3183
3184 /* let's count a new HTTP request as it's the first time we do it */
3185 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3186 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3187 if (ptr1 || ptr2) {
3188 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3189
3190 if (ptr1)
3191 stktable_data_cast(ptr1, http_req_cnt)++;
3192
3193 if (ptr2)
3194 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
3195 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3196
3197 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3198
3199 /* If data was modified, we need to touch to re-schedule sync */
3200 stktable_touch_local(t, ts, 0);
3201 }
3202
3203 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3204 if (sess->fe != s->be)
3205 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3206 }
3207 }
3208 break;
3209
Joseph Herlantc42c0e92018-11-25 10:43:27 -08003210 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003211 default:
3212 break;
3213 }
3214 }
3215
3216 end:
3217 if (early_hints) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003218 if (http_reply_103_early_hints(&s->res) == -1)
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003219 rule_ret = HTTP_RULE_RES_BADREQ;
Christopher Faulet3e964192018-10-24 11:39:23 +02003220 }
3221
3222 /* we reached the end of the rules, nothing to report */
3223 return rule_ret;
3224}
3225
3226/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3227 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3228 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3229 * is returned, the process can continue the evaluation of next rule list. If
3230 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3231 * is returned, it means the operation could not be processed and a server error
3232 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3233 * deny rule. If *YIELD is returned, the caller must call again the function
3234 * with the same context.
3235 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003236static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
3237 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02003238{
3239 struct session *sess = strm_sess(s);
3240 struct http_txn *txn = s->txn;
3241 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02003242 struct act_rule *rule;
3243 struct http_hdr_ctx ctx;
3244 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
3245 int act_flags = 0;
3246
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003247 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02003248
3249 /* If "the current_rule_list" match the executed rule list, we are in
3250 * resume condition. If a resume is needed it is always in the action
3251 * and never in the ACL or converters. In this case, we initialise the
3252 * current rule, and go to the action execution point.
3253 */
3254 if (s->current_rule) {
3255 rule = s->current_rule;
3256 s->current_rule = NULL;
3257 if (s->current_rule_list == rules)
3258 goto resume_execution;
3259 }
3260 s->current_rule_list = rules;
3261
3262 list_for_each_entry(rule, rules, list) {
3263 /* check optional condition */
3264 if (rule->cond) {
3265 int ret;
3266
3267 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3268 ret = acl_pass(ret);
3269
3270 if (rule->cond->pol == ACL_COND_UNLESS)
3271 ret = !ret;
3272
3273 if (!ret) /* condition not matched */
3274 continue;
3275 }
3276
3277 act_flags |= ACT_FLAG_FIRST;
3278resume_execution:
3279 switch (rule->action) {
3280 case ACT_ACTION_ALLOW:
3281 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3282 goto end;
3283
3284 case ACT_ACTION_DENY:
3285 txn->flags |= TX_SVDENY;
3286 rule_ret = HTTP_RULE_RES_STOP;
3287 goto end;
3288
3289 case ACT_HTTP_SET_NICE:
3290 s->task->nice = rule->arg.nice;
3291 break;
3292
3293 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003294 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Christopher Faulet3e964192018-10-24 11:39:23 +02003295 break;
3296
3297 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003298 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Christopher Faulet3e964192018-10-24 11:39:23 +02003299 break;
3300
3301 case ACT_HTTP_SET_LOGL:
3302 s->logs.level = rule->arg.loglevel;
3303 break;
3304
3305 case ACT_HTTP_REPLACE_HDR:
3306 case ACT_HTTP_REPLACE_VAL:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003307 if (http_transform_header(s, &s->res, htx,
3308 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
3309 &rule->arg.hdr_add.fmt,
3310 rule->arg.hdr_add.re, rule->action)) {
Christopher Faulet3e964192018-10-24 11:39:23 +02003311 rule_ret = HTTP_RULE_RES_BADREQ;
3312 goto end;
3313 }
3314 break;
3315
3316 case ACT_HTTP_DEL_HDR:
3317 /* remove all occurrences of the header */
3318 ctx.blk = NULL;
3319 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3320 http_remove_header(htx, &ctx);
3321 break;
3322
3323 case ACT_HTTP_SET_HDR:
3324 case ACT_HTTP_ADD_HDR: {
3325 struct buffer *replace;
3326 struct ist n, v;
3327
3328 replace = alloc_trash_chunk();
3329 if (!replace) {
3330 rule_ret = HTTP_RULE_RES_BADREQ;
3331 goto end;
3332 }
3333
3334 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
3335 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3336 v = ist2(replace->area, replace->data);
3337
3338 if (rule->action == ACT_HTTP_SET_HDR) {
3339 /* remove all occurrences of the header */
3340 ctx.blk = NULL;
3341 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3342 http_remove_header(htx, &ctx);
3343 }
3344
3345 if (!http_add_header(htx, n, v)) {
3346 static unsigned char rate_limit = 0;
3347
3348 if ((rate_limit++ & 255) == 0) {
3349 send_log(px, LOG_WARNING, "Proxy %s failed to add or set the response header '%.*s' for request #%u. You might need to increase tune.maxrewrite.", px->id, (int)n.len, n.ptr, s->uniq_id);
3350 }
3351
Olivier Houcharda798bf52019-03-08 18:52:00 +01003352 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003353 if (sess->fe != s->be)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003354 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003355 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003356 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003357 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01003358 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003359 }
3360 free_trash_chunk(replace);
3361 break;
3362 }
3363
3364 case ACT_HTTP_DEL_ACL:
3365 case ACT_HTTP_DEL_MAP: {
3366 struct pat_ref *ref;
3367 struct buffer *key;
3368
3369 /* collect reference */
3370 ref = pat_ref_lookup(rule->arg.map.ref);
3371 if (!ref)
3372 continue;
3373
3374 /* allocate key */
3375 key = alloc_trash_chunk();
3376 if (!key) {
3377 rule_ret = HTTP_RULE_RES_BADREQ;
3378 goto end;
3379 }
3380
3381 /* collect key */
3382 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3383 key->area[key->data] = '\0';
3384
3385 /* perform update */
3386 /* returned code: 1=ok, 0=ko */
3387 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3388 pat_ref_delete(ref, key->area);
3389 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3390
3391 free_trash_chunk(key);
3392 break;
3393 }
3394
3395 case ACT_HTTP_ADD_ACL: {
3396 struct pat_ref *ref;
3397 struct buffer *key;
3398
3399 /* collect reference */
3400 ref = pat_ref_lookup(rule->arg.map.ref);
3401 if (!ref)
3402 continue;
3403
3404 /* allocate key */
3405 key = alloc_trash_chunk();
3406 if (!key) {
3407 rule_ret = HTTP_RULE_RES_BADREQ;
3408 goto end;
3409 }
3410
3411 /* collect key */
3412 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3413 key->area[key->data] = '\0';
3414
3415 /* perform update */
3416 /* check if the entry already exists */
Christopher Faulete84289e2019-04-19 14:50:55 +02003417 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003418 if (pat_ref_find_elt(ref, key->area) == NULL)
3419 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02003420 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003421 free_trash_chunk(key);
3422 break;
3423 }
3424
3425 case ACT_HTTP_SET_MAP: {
3426 struct pat_ref *ref;
3427 struct buffer *key, *value;
3428
3429 /* collect reference */
3430 ref = pat_ref_lookup(rule->arg.map.ref);
3431 if (!ref)
3432 continue;
3433
3434 /* allocate key */
3435 key = alloc_trash_chunk();
3436 if (!key) {
3437 rule_ret = HTTP_RULE_RES_BADREQ;
3438 goto end;
3439 }
3440
3441 /* allocate value */
3442 value = alloc_trash_chunk();
3443 if (!value) {
3444 free_trash_chunk(key);
3445 rule_ret = HTTP_RULE_RES_BADREQ;
3446 goto end;
3447 }
3448
3449 /* collect key */
3450 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3451 key->area[key->data] = '\0';
3452
3453 /* collect value */
3454 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3455 value->area[value->data] = '\0';
3456
3457 /* perform update */
3458 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3459 if (pat_ref_find_elt(ref, key->area) != NULL)
3460 /* update entry if it exists */
3461 pat_ref_set(ref, key->area, value->area, NULL);
3462 else
3463 /* insert a new entry */
3464 pat_ref_add(ref, key->area, value->area, NULL);
3465 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3466 free_trash_chunk(key);
3467 free_trash_chunk(value);
3468 break;
3469 }
3470
3471 case ACT_HTTP_REDIR:
3472 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003473 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3e964192018-10-24 11:39:23 +02003474 rule_ret = HTTP_RULE_RES_BADREQ;
3475 goto end;
3476
3477 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3478 /* Note: only the first valid tracking parameter of each
3479 * applies.
3480 */
3481 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3482 struct stktable *t;
3483 struct stksess *ts;
3484 struct stktable_key *key;
3485 void *ptr;
3486
3487 t = rule->arg.trk_ctr.table.t;
3488 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL,
3489 rule->arg.trk_ctr.expr, NULL);
3490
3491 if (key && (ts = stktable_get_entry(t, key))) {
3492 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3493
3494 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3495
3496 /* let's count a new HTTP request as it's the first time we do it */
3497 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3498 if (ptr)
3499 stktable_data_cast(ptr, http_req_cnt)++;
3500
3501 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3502 if (ptr)
3503 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3504 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3505
3506 /* When the client triggers a 4xx from the server, it's most often due
3507 * to a missing object or permission. These events should be tracked
3508 * because if they happen often, it may indicate a brute force or a
3509 * vulnerability scan. Normally this is done when receiving the response
3510 * but here we're tracking after this ought to have been done so we have
3511 * to do it on purpose.
3512 */
3513 if ((unsigned)(txn->status - 400) < 100) {
3514 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3515 if (ptr)
3516 stktable_data_cast(ptr, http_err_cnt)++;
3517
3518 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3519 if (ptr)
3520 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3521 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3522 }
3523
3524 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3525
3526 /* If data was modified, we need to touch to re-schedule sync */
3527 stktable_touch_local(t, ts, 0);
3528
3529 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3530 if (sess->fe != s->be)
3531 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3532 }
3533 }
3534 break;
3535
3536 case ACT_CUSTOM:
3537 if ((s->req.flags & CF_READ_ERROR) ||
3538 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Christopher Faulet3e964192018-10-24 11:39:23 +02003539 (px->options & PR_O_ABRT_CLOSE)))
3540 act_flags |= ACT_FLAG_FINAL;
3541
3542 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
3543 case ACT_RET_ERR:
Christopher Faulet28160e72019-12-13 09:36:11 +01003544 rule_ret = HTTP_RULE_RES_BADREQ;
3545 goto end;
Christopher Faulet3e964192018-10-24 11:39:23 +02003546 case ACT_RET_CONT:
3547 break;
3548 case ACT_RET_STOP:
3549 rule_ret = HTTP_RULE_RES_STOP;
3550 goto end;
Christopher Faulet8f1aa772019-07-04 11:27:15 +02003551 case ACT_RET_DONE:
3552 rule_ret = HTTP_RULE_RES_DONE;
3553 goto end;
Christopher Faulet3e964192018-10-24 11:39:23 +02003554 case ACT_RET_YIELD:
3555 s->current_rule = rule;
3556 rule_ret = HTTP_RULE_RES_YIELD;
3557 goto end;
3558 }
3559 break;
3560
Joseph Herlantc42c0e92018-11-25 10:43:27 -08003561 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003562 default:
3563 break;
3564 }
3565 }
3566
3567 end:
3568 /* we reached the end of the rules, nothing to report */
3569 return rule_ret;
3570}
3571
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003572/*
3573 * Manage client-side cookie. It can impact performance by about 2% so it is
3574 * desirable to call it only when needed. This code is quite complex because
3575 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3576 * highly recommended not to touch this part without a good reason !
3577 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003578static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003579{
3580 struct session *sess = s->sess;
3581 struct http_txn *txn = s->txn;
3582 struct htx *htx;
3583 struct http_hdr_ctx ctx;
3584 char *hdr_beg, *hdr_end, *del_from;
3585 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3586 int preserve_hdr;
3587
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003588 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003589 ctx.blk = NULL;
3590 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003591 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003592 del_from = NULL; /* nothing to be deleted */
3593 preserve_hdr = 0; /* assume we may kill the whole header */
3594
3595 /* Now look for cookies. Conforming to RFC2109, we have to support
3596 * attributes whose name begin with a '$', and associate them with
3597 * the right cookie, if we want to delete this cookie.
3598 * So there are 3 cases for each cookie read :
3599 * 1) it's a special attribute, beginning with a '$' : ignore it.
3600 * 2) it's a server id cookie that we *MAY* want to delete : save
3601 * some pointers on it (last semi-colon, beginning of cookie...)
3602 * 3) it's an application cookie : we *MAY* have to delete a previous
3603 * "special" cookie.
3604 * At the end of loop, if a "special" cookie remains, we may have to
3605 * remove it. If no application cookie persists in the header, we
3606 * *MUST* delete it.
3607 *
3608 * Note: RFC2965 is unclear about the processing of spaces around
3609 * the equal sign in the ATTR=VALUE form. A careful inspection of
3610 * the RFC explicitly allows spaces before it, and not within the
3611 * tokens (attrs or values). An inspection of RFC2109 allows that
3612 * too but section 10.1.3 lets one think that spaces may be allowed
3613 * after the equal sign too, resulting in some (rare) buggy
3614 * implementations trying to do that. So let's do what servers do.
3615 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3616 * allowed quoted strings in values, with any possible character
3617 * after a backslash, including control chars and delimitors, which
3618 * causes parsing to become ambiguous. Browsers also allow spaces
3619 * within values even without quotes.
3620 *
3621 * We have to keep multiple pointers in order to support cookie
3622 * removal at the beginning, middle or end of header without
3623 * corrupting the header. All of these headers are valid :
3624 *
3625 * hdr_beg hdr_end
3626 * | |
3627 * v |
3628 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3629 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3630 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3631 * | | | | | | |
3632 * | | | | | | |
3633 * | | | | | | +--> next
3634 * | | | | | +----> val_end
3635 * | | | | +-----------> val_beg
3636 * | | | +--------------> equal
3637 * | | +----------------> att_end
3638 * | +---------------------> att_beg
3639 * +--------------------------> prev
3640 *
3641 */
3642 hdr_beg = ctx.value.ptr;
3643 hdr_end = hdr_beg + ctx.value.len;
3644 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3645 /* Iterate through all cookies on this line */
3646
3647 /* find att_beg */
3648 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003649 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003650 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003651 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003652
3653 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3654 att_beg++;
3655
3656 /* find att_end : this is the first character after the last non
3657 * space before the equal. It may be equal to hdr_end.
3658 */
3659 equal = att_end = att_beg;
3660 while (equal < hdr_end) {
3661 if (*equal == '=' || *equal == ',' || *equal == ';')
3662 break;
3663 if (HTTP_IS_SPHT(*equal++))
3664 continue;
3665 att_end = equal;
3666 }
3667
3668 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3669 * is between <att_beg> and <equal>, both may be identical.
3670 */
3671 /* look for end of cookie if there is an equal sign */
3672 if (equal < hdr_end && *equal == '=') {
3673 /* look for the beginning of the value */
3674 val_beg = equal + 1;
3675 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3676 val_beg++;
3677
3678 /* find the end of the value, respecting quotes */
3679 next = http_find_cookie_value_end(val_beg, hdr_end);
3680
3681 /* make val_end point to the first white space or delimitor after the value */
3682 val_end = next;
3683 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3684 val_end--;
3685 }
3686 else
3687 val_beg = val_end = next = equal;
3688
3689 /* We have nothing to do with attributes beginning with
3690 * '$'. However, they will automatically be removed if a
3691 * header before them is removed, since they're supposed
3692 * to be linked together.
3693 */
3694 if (*att_beg == '$')
3695 continue;
3696
3697 /* Ignore cookies with no equal sign */
3698 if (equal == next) {
3699 /* This is not our cookie, so we must preserve it. But if we already
3700 * scheduled another cookie for removal, we cannot remove the
3701 * complete header, but we can remove the previous block itself.
3702 */
3703 preserve_hdr = 1;
3704 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003705 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003706 val_end += delta;
3707 next += delta;
3708 hdr_end += delta;
3709 prev = del_from;
3710 del_from = NULL;
3711 }
3712 continue;
3713 }
3714
3715 /* if there are spaces around the equal sign, we need to
3716 * strip them otherwise we'll get trouble for cookie captures,
3717 * or even for rewrites. Since this happens extremely rarely,
3718 * it does not hurt performance.
3719 */
3720 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3721 int stripped_before = 0;
3722 int stripped_after = 0;
3723
3724 if (att_end != equal) {
3725 memmove(att_end, equal, hdr_end - equal);
3726 stripped_before = (att_end - equal);
3727 equal += stripped_before;
3728 val_beg += stripped_before;
3729 }
3730
3731 if (val_beg > equal + 1) {
3732 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3733 stripped_after = (equal + 1) - val_beg;
3734 val_beg += stripped_after;
3735 stripped_before += stripped_after;
3736 }
3737
3738 val_end += stripped_before;
3739 next += stripped_before;
3740 hdr_end += stripped_before;
3741 }
3742 /* now everything is as on the diagram above */
3743
3744 /* First, let's see if we want to capture this cookie. We check
3745 * that we don't already have a client side cookie, because we
3746 * can only capture one. Also as an optimisation, we ignore
3747 * cookies shorter than the declared name.
3748 */
3749 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3750 (val_end - att_beg >= sess->fe->capture_namelen) &&
3751 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3752 int log_len = val_end - att_beg;
3753
3754 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3755 ha_alert("HTTP logging : out of memory.\n");
3756 } else {
3757 if (log_len > sess->fe->capture_len)
3758 log_len = sess->fe->capture_len;
3759 memcpy(txn->cli_cookie, att_beg, log_len);
3760 txn->cli_cookie[log_len] = 0;
3761 }
3762 }
3763
3764 /* Persistence cookies in passive, rewrite or insert mode have the
3765 * following form :
3766 *
3767 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3768 *
3769 * For cookies in prefix mode, the form is :
3770 *
3771 * Cookie: NAME=SRV~VALUE
3772 */
3773 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3774 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3775 struct server *srv = s->be->srv;
3776 char *delim;
3777
3778 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3779 * have the server ID between val_beg and delim, and the original cookie between
3780 * delim+1 and val_end. Otherwise, delim==val_end :
3781 *
3782 * hdr_beg
3783 * |
3784 * v
3785 * NAME=SRV; # in all but prefix modes
3786 * NAME=SRV~OPAQUE ; # in prefix mode
3787 * || || | |+-> next
3788 * || || | +--> val_end
3789 * || || +---------> delim
3790 * || |+------------> val_beg
3791 * || +-------------> att_end = equal
3792 * |+-----------------> att_beg
3793 * +------------------> prev
3794 *
3795 */
3796 if (s->be->ck_opts & PR_CK_PFX) {
3797 for (delim = val_beg; delim < val_end; delim++)
3798 if (*delim == COOKIE_DELIM)
3799 break;
3800 }
3801 else {
3802 char *vbar1;
3803 delim = val_end;
3804 /* Now check if the cookie contains a date field, which would
3805 * appear after a vertical bar ('|') just after the server name
3806 * and before the delimiter.
3807 */
3808 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3809 if (vbar1) {
3810 /* OK, so left of the bar is the server's cookie and
3811 * right is the last seen date. It is a base64 encoded
3812 * 30-bit value representing the UNIX date since the
3813 * epoch in 4-second quantities.
3814 */
3815 int val;
3816 delim = vbar1++;
3817 if (val_end - vbar1 >= 5) {
3818 val = b64tos30(vbar1);
3819 if (val > 0)
3820 txn->cookie_last_date = val << 2;
3821 }
3822 /* look for a second vertical bar */
3823 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3824 if (vbar1 && (val_end - vbar1 > 5)) {
3825 val = b64tos30(vbar1 + 1);
3826 if (val > 0)
3827 txn->cookie_first_date = val << 2;
3828 }
3829 }
3830 }
3831
3832 /* if the cookie has an expiration date and the proxy wants to check
3833 * it, then we do that now. We first check if the cookie is too old,
3834 * then only if it has expired. We detect strict overflow because the
3835 * time resolution here is not great (4 seconds). Cookies with dates
3836 * in the future are ignored if their offset is beyond one day. This
3837 * allows an admin to fix timezone issues without expiring everyone
3838 * and at the same time avoids keeping unwanted side effects for too
3839 * long.
3840 */
3841 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3842 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3843 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3844 txn->flags &= ~TX_CK_MASK;
3845 txn->flags |= TX_CK_OLD;
3846 delim = val_beg; // let's pretend we have not found the cookie
3847 txn->cookie_first_date = 0;
3848 txn->cookie_last_date = 0;
3849 }
3850 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3851 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3852 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3853 txn->flags &= ~TX_CK_MASK;
3854 txn->flags |= TX_CK_EXPIRED;
3855 delim = val_beg; // let's pretend we have not found the cookie
3856 txn->cookie_first_date = 0;
3857 txn->cookie_last_date = 0;
3858 }
3859
3860 /* Here, we'll look for the first running server which supports the cookie.
3861 * This allows to share a same cookie between several servers, for example
3862 * to dedicate backup servers to specific servers only.
3863 * However, to prevent clients from sticking to cookie-less backup server
3864 * when they have incidentely learned an empty cookie, we simply ignore
3865 * empty cookies and mark them as invalid.
3866 * The same behaviour is applied when persistence must be ignored.
3867 */
3868 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3869 srv = NULL;
3870
3871 while (srv) {
3872 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3873 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3874 if ((srv->cur_state != SRV_ST_STOPPED) ||
3875 (s->be->options & PR_O_PERSIST) ||
3876 (s->flags & SF_FORCE_PRST)) {
3877 /* we found the server and we can use it */
3878 txn->flags &= ~TX_CK_MASK;
3879 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3880 s->flags |= SF_DIRECT | SF_ASSIGNED;
3881 s->target = &srv->obj_type;
3882 break;
3883 } else {
3884 /* we found a server, but it's down,
3885 * mark it as such and go on in case
3886 * another one is available.
3887 */
3888 txn->flags &= ~TX_CK_MASK;
3889 txn->flags |= TX_CK_DOWN;
3890 }
3891 }
3892 srv = srv->next;
3893 }
3894
3895 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3896 /* no server matched this cookie or we deliberately skipped it */
3897 txn->flags &= ~TX_CK_MASK;
3898 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3899 txn->flags |= TX_CK_UNUSED;
3900 else
3901 txn->flags |= TX_CK_INVALID;
3902 }
3903
3904 /* depending on the cookie mode, we may have to either :
3905 * - delete the complete cookie if we're in insert+indirect mode, so that
3906 * the server never sees it ;
3907 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003908 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003909 * if we're in cookie prefix mode
3910 */
3911 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3912 int delta; /* negative */
3913
3914 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3915 delta = val_beg - (delim + 1);
3916 val_end += delta;
3917 next += delta;
3918 hdr_end += delta;
3919 del_from = NULL;
3920 preserve_hdr = 1; /* we want to keep this cookie */
3921 }
3922 else if (del_from == NULL &&
3923 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3924 del_from = prev;
3925 }
3926 }
3927 else {
3928 /* This is not our cookie, so we must preserve it. But if we already
3929 * scheduled another cookie for removal, we cannot remove the
3930 * complete header, but we can remove the previous block itself.
3931 */
3932 preserve_hdr = 1;
3933
3934 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003935 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003936 if (att_beg >= del_from)
3937 att_beg += delta;
3938 if (att_end >= del_from)
3939 att_end += delta;
3940 val_beg += delta;
3941 val_end += delta;
3942 next += delta;
3943 hdr_end += delta;
3944 prev = del_from;
3945 del_from = NULL;
3946 }
3947 }
3948
3949 /* continue with next cookie on this header line */
3950 att_beg = next;
3951 } /* for each cookie */
3952
3953
3954 /* There are no more cookies on this line.
3955 * We may still have one (or several) marked for deletion at the
3956 * end of the line. We must do this now in two ways :
3957 * - if some cookies must be preserved, we only delete from the
3958 * mark to the end of line ;
3959 * - if nothing needs to be preserved, simply delete the whole header
3960 */
3961 if (del_from) {
3962 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3963 }
3964 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003965 if (hdr_beg != hdr_end)
3966 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003967 else
3968 http_remove_header(htx, &ctx);
3969 }
3970 } /* for each "Cookie header */
3971}
3972
3973/*
3974 * Manage server-side cookies. It can impact performance by about 2% so it is
3975 * desirable to call it only when needed. This function is also used when we
3976 * just need to know if there is a cookie (eg: for check-cache).
3977 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003978static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003979{
3980 struct session *sess = s->sess;
3981 struct http_txn *txn = s->txn;
3982 struct htx *htx;
3983 struct http_hdr_ctx ctx;
3984 struct server *srv;
3985 char *hdr_beg, *hdr_end;
3986 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003987 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003988
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003989 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003990
3991 ctx.blk = NULL;
3992 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003993 int is_first = 1;
3994
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003995 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3996 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3997 break;
3998 is_cookie2 = 1;
3999 }
4000
4001 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
4002 * <prev> points to the colon.
4003 */
4004 txn->flags |= TX_SCK_PRESENT;
4005
4006 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
4007 * check-cache is enabled) and we are not interested in checking
4008 * them. Warning, the cookie capture is declared in the frontend.
4009 */
4010 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
4011 break;
4012
4013 /* OK so now we know we have to process this response cookie.
4014 * The format of the Set-Cookie header is slightly different
4015 * from the format of the Cookie header in that it does not
4016 * support the comma as a cookie delimiter (thus the header
4017 * cannot be folded) because the Expires attribute described in
4018 * the original Netscape's spec may contain an unquoted date
4019 * with a comma inside. We have to live with this because
4020 * many browsers don't support Max-Age and some browsers don't
4021 * support quoted strings. However the Set-Cookie2 header is
4022 * clean.
4023 *
4024 * We have to keep multiple pointers in order to support cookie
4025 * removal at the beginning, middle or end of header without
4026 * corrupting the header (in case of set-cookie2). A special
4027 * pointer, <scav> points to the beginning of the set-cookie-av
4028 * fields after the first semi-colon. The <next> pointer points
4029 * either to the end of line (set-cookie) or next unquoted comma
4030 * (set-cookie2). All of these headers are valid :
4031 *
4032 * hdr_beg hdr_end
4033 * | |
4034 * v |
4035 * NAME1 = VALUE 1 ; Secure; Path="/" |
4036 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
4037 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
4038 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
4039 * | | | | | | | |
4040 * | | | | | | | +-> next
4041 * | | | | | | +------------> scav
4042 * | | | | | +--------------> val_end
4043 * | | | | +--------------------> val_beg
4044 * | | | +----------------------> equal
4045 * | | +------------------------> att_end
4046 * | +----------------------------> att_beg
4047 * +------------------------------> prev
4048 * -------------------------------> hdr_beg
4049 */
4050 hdr_beg = ctx.value.ptr;
4051 hdr_end = hdr_beg + ctx.value.len;
4052 for (prev = hdr_beg; prev < hdr_end; prev = next) {
4053
4054 /* Iterate through all cookies on this line */
4055
4056 /* find att_beg */
4057 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02004058 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004059 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02004060 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004061
4062 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
4063 att_beg++;
4064
4065 /* find att_end : this is the first character after the last non
4066 * space before the equal. It may be equal to hdr_end.
4067 */
4068 equal = att_end = att_beg;
4069
4070 while (equal < hdr_end) {
4071 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
4072 break;
4073 if (HTTP_IS_SPHT(*equal++))
4074 continue;
4075 att_end = equal;
4076 }
4077
4078 /* here, <equal> points to '=', a delimitor or the end. <att_end>
4079 * is between <att_beg> and <equal>, both may be identical.
4080 */
4081
4082 /* look for end of cookie if there is an equal sign */
4083 if (equal < hdr_end && *equal == '=') {
4084 /* look for the beginning of the value */
4085 val_beg = equal + 1;
4086 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
4087 val_beg++;
4088
4089 /* find the end of the value, respecting quotes */
4090 next = http_find_cookie_value_end(val_beg, hdr_end);
4091
4092 /* make val_end point to the first white space or delimitor after the value */
4093 val_end = next;
4094 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
4095 val_end--;
4096 }
4097 else {
4098 /* <equal> points to next comma, semi-colon or EOL */
4099 val_beg = val_end = next = equal;
4100 }
4101
4102 if (next < hdr_end) {
4103 /* Set-Cookie2 supports multiple cookies, and <next> points to
4104 * a colon or semi-colon before the end. So skip all attr-value
4105 * pairs and look for the next comma. For Set-Cookie, since
4106 * commas are permitted in values, skip to the end.
4107 */
4108 if (is_cookie2)
4109 next = http_find_hdr_value_end(next, hdr_end);
4110 else
4111 next = hdr_end;
4112 }
4113
4114 /* Now everything is as on the diagram above */
4115
4116 /* Ignore cookies with no equal sign */
4117 if (equal == val_end)
4118 continue;
4119
4120 /* If there are spaces around the equal sign, we need to
4121 * strip them otherwise we'll get trouble for cookie captures,
4122 * or even for rewrites. Since this happens extremely rarely,
4123 * it does not hurt performance.
4124 */
4125 if (unlikely(att_end != equal || val_beg > equal + 1)) {
4126 int stripped_before = 0;
4127 int stripped_after = 0;
4128
4129 if (att_end != equal) {
4130 memmove(att_end, equal, hdr_end - equal);
4131 stripped_before = (att_end - equal);
4132 equal += stripped_before;
4133 val_beg += stripped_before;
4134 }
4135
4136 if (val_beg > equal + 1) {
4137 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
4138 stripped_after = (equal + 1) - val_beg;
4139 val_beg += stripped_after;
4140 stripped_before += stripped_after;
4141 }
4142
4143 val_end += stripped_before;
4144 next += stripped_before;
4145 hdr_end += stripped_before;
4146
Christopher Faulet3e2638e2019-06-18 09:49:16 +02004147 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004148 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004149 }
4150
4151 /* First, let's see if we want to capture this cookie. We check
4152 * that we don't already have a server side cookie, because we
4153 * can only capture one. Also as an optimisation, we ignore
4154 * cookies shorter than the declared name.
4155 */
4156 if (sess->fe->capture_name != NULL &&
4157 txn->srv_cookie == NULL &&
4158 (val_end - att_beg >= sess->fe->capture_namelen) &&
4159 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
4160 int log_len = val_end - att_beg;
4161 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
4162 ha_alert("HTTP logging : out of memory.\n");
4163 }
4164 else {
4165 if (log_len > sess->fe->capture_len)
4166 log_len = sess->fe->capture_len;
4167 memcpy(txn->srv_cookie, att_beg, log_len);
4168 txn->srv_cookie[log_len] = 0;
4169 }
4170 }
4171
4172 srv = objt_server(s->target);
4173 /* now check if we need to process it for persistence */
4174 if (!(s->flags & SF_IGNORE_PRST) &&
4175 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
4176 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
4177 /* assume passive cookie by default */
4178 txn->flags &= ~TX_SCK_MASK;
4179 txn->flags |= TX_SCK_FOUND;
4180
4181 /* If the cookie is in insert mode on a known server, we'll delete
4182 * this occurrence because we'll insert another one later.
4183 * We'll delete it too if the "indirect" option is set and we're in
4184 * a direct access.
4185 */
4186 if (s->be->ck_opts & PR_CK_PSV) {
4187 /* The "preserve" flag was set, we don't want to touch the
4188 * server's cookie.
4189 */
4190 }
4191 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
4192 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
4193 /* this cookie must be deleted */
4194 if (prev == hdr_beg && next == hdr_end) {
4195 /* whole header */
4196 http_remove_header(htx, &ctx);
4197 /* note: while both invalid now, <next> and <hdr_end>
4198 * are still equal, so the for() will stop as expected.
4199 */
4200 } else {
4201 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004202 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004203 next = prev;
4204 hdr_end += delta;
4205 }
4206 txn->flags &= ~TX_SCK_MASK;
4207 txn->flags |= TX_SCK_DELETED;
4208 /* and go on with next cookie */
4209 }
4210 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
4211 /* replace bytes val_beg->val_end with the cookie name associated
4212 * with this server since we know it.
4213 */
4214 int sliding, delta;
4215
4216 ctx.value = ist2(val_beg, val_end - val_beg);
4217 ctx.lws_before = ctx.lws_after = 0;
4218 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
4219 delta = srv->cklen - (val_end - val_beg);
4220 sliding = (ctx.value.ptr - val_beg);
4221 hdr_beg += sliding;
4222 val_beg += sliding;
4223 next += sliding + delta;
4224 hdr_end += sliding + delta;
4225
4226 txn->flags &= ~TX_SCK_MASK;
4227 txn->flags |= TX_SCK_REPLACED;
4228 }
4229 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
4230 /* insert the cookie name associated with this server
4231 * before existing cookie, and insert a delimiter between them..
4232 */
4233 int sliding, delta;
4234 ctx.value = ist2(val_beg, 0);
4235 ctx.lws_before = ctx.lws_after = 0;
4236 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
4237 delta = srv->cklen + 1;
4238 sliding = (ctx.value.ptr - val_beg);
4239 hdr_beg += sliding;
4240 val_beg += sliding;
4241 next += sliding + delta;
4242 hdr_end += sliding + delta;
4243
4244 val_beg[srv->cklen] = COOKIE_DELIM;
4245 txn->flags &= ~TX_SCK_MASK;
4246 txn->flags |= TX_SCK_REPLACED;
4247 }
4248 }
4249 /* that's done for this cookie, check the next one on the same
4250 * line when next != hdr_end (only if is_cookie2).
4251 */
4252 }
4253 }
4254}
4255
Christopher Faulet25a02f62018-10-24 12:00:25 +02004256/*
4257 * Parses the Cache-Control and Pragma request header fields to determine if
4258 * the request may be served from the cache and/or if it is cacheable. Updates
4259 * s->txn->flags.
4260 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004261void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004262{
4263 struct http_txn *txn = s->txn;
4264 struct htx *htx;
4265 int32_t pos;
4266 int pragma_found, cc_found, i;
4267
4268 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
4269 return; /* nothing more to do here */
4270
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004271 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02004272 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02004273 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004274 struct htx_blk *blk = htx_get_blk(htx, pos);
4275 enum htx_blk_type type = htx_get_blk_type(blk);
4276 struct ist n, v;
4277
4278 if (type == HTX_BLK_EOH)
4279 break;
4280 if (type != HTX_BLK_HDR)
4281 continue;
4282
4283 n = htx_get_blk_name(htx, blk);
4284 v = htx_get_blk_value(htx, blk);
4285
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004286 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004287 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4288 pragma_found = 1;
4289 continue;
4290 }
4291 }
4292
4293 /* Don't use the cache and don't try to store if we found the
4294 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004295 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004296 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4297 txn->flags |= TX_CACHE_IGNORE;
4298 continue;
4299 }
4300
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004301 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004302 continue;
4303
4304 /* OK, right now we know we have a cache-control header */
4305 cc_found = 1;
4306 if (!v.len) /* no info */
4307 continue;
4308
4309 i = 0;
4310 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4311 !isspace((unsigned char)*(v.ptr+i)))
4312 i++;
4313
4314 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
4315 * values after max-age, max-stale nor min-fresh, we simply don't
4316 * use the cache when they're specified.
4317 */
4318 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
4319 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4320 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
4321 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
4322 txn->flags |= TX_CACHE_IGNORE;
4323 continue;
4324 }
4325
4326 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
4327 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4328 continue;
4329 }
4330 }
4331
4332 /* RFC7234#5.4:
4333 * When the Cache-Control header field is also present and
4334 * understood in a request, Pragma is ignored.
4335 * When the Cache-Control header field is not present in a
4336 * request, caches MUST consider the no-cache request
4337 * pragma-directive as having the same effect as if
4338 * "Cache-Control: no-cache" were present.
4339 */
4340 if (!cc_found && pragma_found)
4341 txn->flags |= TX_CACHE_IGNORE;
4342}
4343
4344/*
4345 * Check if response is cacheable or not. Updates s->txn->flags.
4346 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004347void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004348{
4349 struct http_txn *txn = s->txn;
4350 struct htx *htx;
4351 int32_t pos;
4352 int i;
4353
4354 if (txn->status < 200) {
4355 /* do not try to cache interim responses! */
4356 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4357 return;
4358 }
4359
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004360 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004361 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004362 struct htx_blk *blk = htx_get_blk(htx, pos);
4363 enum htx_blk_type type = htx_get_blk_type(blk);
4364 struct ist n, v;
4365
4366 if (type == HTX_BLK_EOH)
4367 break;
4368 if (type != HTX_BLK_HDR)
4369 continue;
4370
4371 n = htx_get_blk_name(htx, blk);
4372 v = htx_get_blk_value(htx, blk);
4373
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004374 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004375 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4376 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4377 return;
4378 }
4379 }
4380
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004381 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004382 continue;
4383
4384 /* OK, right now we know we have a cache-control header */
4385 if (!v.len) /* no info */
4386 continue;
4387
4388 i = 0;
4389 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4390 !isspace((unsigned char)*(v.ptr+i)))
4391 i++;
4392
4393 /* we have a complete value between v.ptr and (v.ptr+i) */
4394 if (i < v.len && *(v.ptr + i) == '=') {
4395 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
4396 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
4397 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4398 continue;
4399 }
4400
4401 /* we have something of the form no-cache="set-cookie" */
4402 if ((v.len >= 21) &&
4403 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
4404 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
4405 txn->flags &= ~TX_CACHE_COOK;
4406 continue;
4407 }
4408
4409 /* OK, so we know that either p2 points to the end of string or to a comma */
4410 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
4411 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4412 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
4413 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4414 return;
4415 }
4416
4417 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4418 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4419 continue;
4420 }
4421 }
4422}
4423
Christopher Faulet377c5a52018-10-24 21:21:30 +02004424/*
4425 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4426 * for the current backend.
4427 *
4428 * It is assumed that the request is either a HEAD, GET, or POST and that the
4429 * uri_auth field is valid.
4430 *
4431 * Returns 1 if stats should be provided, otherwise 0.
4432 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004433static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004434{
4435 struct uri_auth *uri_auth = backend->uri_auth;
4436 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004437 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004438 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004439
4440 if (!uri_auth)
4441 return 0;
4442
4443 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4444 return 0;
4445
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004446 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004447 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004448 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01004449 if (*uri_auth->uri_prefix == '/')
4450 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004451
4452 /* check URI size */
4453 if (uri_auth->uri_len > uri.len)
4454 return 0;
4455
4456 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4457 return 0;
4458
4459 return 1;
4460}
4461
4462/* This function prepares an applet to handle the stats. It can deal with the
4463 * "100-continue" expectation, check that admin rules are met for POST requests,
4464 * and program a response message if something was unexpected. It cannot fail
4465 * and always relies on the stats applet to complete the job. It does not touch
4466 * analysers nor counters, which are left to the caller. It does not touch
4467 * s->target which is supposed to already point to the stats applet. The caller
4468 * is expected to have already assigned an appctx to the stream.
4469 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004470static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004471{
4472 struct stats_admin_rule *stats_admin_rule;
4473 struct stream_interface *si = &s->si[1];
4474 struct session *sess = s->sess;
4475 struct http_txn *txn = s->txn;
4476 struct http_msg *msg = &txn->req;
4477 struct uri_auth *uri_auth = s->be->uri_auth;
4478 const char *h, *lookup, *end;
4479 struct appctx *appctx;
4480 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004481 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004482
4483 appctx = si_appctx(si);
4484 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4485 appctx->st1 = appctx->st2 = 0;
4486 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004487 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004488 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4489 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4490 appctx->ctx.stats.flags |= STAT_CHUNKED;
4491
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004492 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004493 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004494 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4495 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004496
4497 for (h = lookup; h <= end - 3; h++) {
4498 if (memcmp(h, ";up", 3) == 0) {
4499 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4500 break;
4501 }
4502 }
4503
4504 if (uri_auth->refresh) {
4505 for (h = lookup; h <= end - 10; h++) {
4506 if (memcmp(h, ";norefresh", 10) == 0) {
4507 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4508 break;
4509 }
4510 }
4511 }
4512
4513 for (h = lookup; h <= end - 4; h++) {
4514 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004515 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004516 break;
4517 }
4518 }
4519
4520 for (h = lookup; h <= end - 6; h++) {
4521 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004522 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004523 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4524 break;
4525 }
4526 }
4527
Christopher Faulet6338a082019-09-09 15:50:54 +02004528 for (h = lookup; h <= end - 5; h++) {
4529 if (memcmp(h, ";json", 5) == 0) {
4530 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4531 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4532 break;
4533 }
4534 }
4535
4536 for (h = lookup; h <= end - 12; h++) {
4537 if (memcmp(h, ";json-schema", 12) == 0) {
4538 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4539 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4540 break;
4541 }
4542 }
4543
Christopher Faulet377c5a52018-10-24 21:21:30 +02004544 for (h = lookup; h <= end - 8; h++) {
4545 if (memcmp(h, ";st=", 4) == 0) {
4546 int i;
4547 h += 4;
4548 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4549 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4550 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4551 appctx->ctx.stats.st_code = i;
4552 break;
4553 }
4554 }
4555 break;
4556 }
4557 }
4558
4559 appctx->ctx.stats.scope_str = 0;
4560 appctx->ctx.stats.scope_len = 0;
4561 for (h = lookup; h <= end - 8; h++) {
4562 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4563 int itx = 0;
4564 const char *h2;
4565 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4566 const char *err;
4567
4568 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4569 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004570 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4571 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004572 if (*h == ';' || *h == '&' || *h == ' ')
4573 break;
4574 itx++;
4575 h++;
4576 }
4577
4578 if (itx > STAT_SCOPE_TXT_MAXLEN)
4579 itx = STAT_SCOPE_TXT_MAXLEN;
4580 appctx->ctx.stats.scope_len = itx;
4581
4582 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4583 memcpy(scope_txt, h2, itx);
4584 scope_txt[itx] = '\0';
4585 err = invalid_char(scope_txt);
4586 if (err) {
4587 /* bad char in search text => clear scope */
4588 appctx->ctx.stats.scope_str = 0;
4589 appctx->ctx.stats.scope_len = 0;
4590 }
4591 break;
4592 }
4593 }
4594
4595 /* now check whether we have some admin rules for this request */
4596 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4597 int ret = 1;
4598
4599 if (stats_admin_rule->cond) {
4600 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4601 ret = acl_pass(ret);
4602 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4603 ret = !ret;
4604 }
4605
4606 if (ret) {
4607 /* no rule, or the rule matches */
4608 appctx->ctx.stats.flags |= STAT_ADMIN;
4609 break;
4610 }
4611 }
4612
Christopher Faulet5d45e382019-02-27 15:15:23 +01004613 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4614 appctx->st0 = STAT_HTTP_HEAD;
4615 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004616 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004617 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004618 if (msg->msg_state < HTTP_MSG_DATA)
4619 req->analysers |= AN_REQ_HTTP_BODY;
4620 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004621 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004622 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004623 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4624 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4625 appctx->st0 = STAT_HTTP_LAST;
4626 }
4627 }
4628 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004629 /* Unsupported method */
4630 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4631 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4632 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004633 }
4634
4635 s->task->nice = -32; /* small boost for HTTP statistics */
4636 return 1;
4637}
4638
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004639void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004640{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004641 struct channel *req = &s->req;
4642 struct channel *res = &s->res;
4643 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004644 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004645 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004646 struct ist path, location;
4647 unsigned int flags;
4648 size_t data;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004649
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004650 /*
4651 * Create the location
4652 */
4653 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004654
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004655 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004656 /* special prefix "/" means don't change URL */
4657 srv = __objt_server(s->target);
4658 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4659 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4660 return;
4661 }
4662
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004663 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004664 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004665 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004666 path = http_get_path(htx_sl_req_uri(sl));
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004667 if (!path.ptr)
4668 return;
4669
4670 if (!chunk_memcat(&trash, path.ptr, path.len))
4671 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004672 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004673
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004674 /*
4675 * Create the 302 respone
4676 */
4677 htx = htx_from_buf(&res->buf);
4678 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4679 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4680 ist("HTTP/1.1"), ist("302"), ist("Found"));
4681 if (!sl)
4682 goto fail;
4683 sl->info.res.status = 302;
4684 s->txn->status = 302;
4685
4686 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4687 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4688 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4689 !htx_add_header(htx, ist("Location"), location))
4690 goto fail;
4691
4692 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4693 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004694
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004695 /*
4696 * Send the message
4697 */
4698 data = htx->data - co_data(res);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004699 c_adv(res, data);
4700 res->total += data;
4701
4702 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004703 si_shutr(si);
4704 si_shutw(si);
4705 si->err_type = SI_ET_NONE;
4706 si->state = SI_ST_CLO;
4707
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004708 channel_auto_read(req);
4709 channel_abort(req);
4710 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004711 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004712 channel_auto_read(res);
4713 channel_auto_close(res);
4714
4715 if (!(s->flags & SF_ERR_MASK))
4716 s->flags |= SF_ERR_LOCAL;
4717 if (!(s->flags & SF_FINST_MASK))
4718 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004719
4720 /* FIXME: we should increase a counter of redirects per server and per backend. */
4721 srv_inc_sess_ctr(srv);
4722 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004723 return;
4724
4725 fail:
4726 /* If an error occurred, remove the incomplete HTTP response from the
4727 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004728 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004729}
4730
Christopher Fauletf2824e62018-10-01 12:12:37 +02004731/* This function terminates the request because it was completly analyzed or
4732 * because an error was triggered during the body forwarding.
4733 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004734static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004735{
4736 struct channel *chn = &s->req;
4737 struct http_txn *txn = s->txn;
4738
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004739 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004740
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004741 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4742 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004743 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004744 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004745 goto end;
4746 }
4747
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004748 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4749 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004750 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004751 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004752
4753 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004754 /* No need to read anymore, the request was completely parsed.
4755 * We can shut the read side unless we want to abort_on_close,
4756 * or we have a POST request. The issue with POST requests is
4757 * that some browsers still send a CRLF after the request, and
4758 * this CRLF must be read so that it does not remain in the kernel
4759 * buffers, otherwise a close could cause an RST on some systems
4760 * (eg: Linux).
4761 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004762 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004763 channel_dont_read(chn);
4764
4765 /* if the server closes the connection, we want to immediately react
4766 * and close the socket to save packets and syscalls.
4767 */
4768 s->si[1].flags |= SI_FL_NOHALF;
4769
4770 /* In any case we've finished parsing the request so we must
4771 * disable Nagle when sending data because 1) we're not going
4772 * to shut this side, and 2) the server is waiting for us to
4773 * send pending data.
4774 */
4775 chn->flags |= CF_NEVER_WAIT;
4776
Christopher Fauletd01ce402019-01-02 17:44:13 +01004777 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4778 /* The server has not finished to respond, so we
4779 * don't want to move in order not to upset it.
4780 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004781 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004782 return;
4783 }
4784
Christopher Fauletf2824e62018-10-01 12:12:37 +02004785 /* When we get here, it means that both the request and the
4786 * response have finished receiving. Depending on the connection
4787 * mode, we'll have to wait for the last bytes to leave in either
4788 * direction, and sometimes for a close to be effective.
4789 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004790 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004791 /* Tunnel mode will not have any analyser so it needs to
4792 * poll for reads.
4793 */
4794 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004795 if (b_data(&chn->buf)) {
4796 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004797 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004798 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004799 txn->req.msg_state = HTTP_MSG_TUNNEL;
4800 }
4801 else {
4802 /* we're not expecting any new data to come for this
4803 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004804 *
4805 * However, there is an exception if the response
4806 * length is undefined. In this case, we need to wait
4807 * the close from the server. The response will be
4808 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004809 */
4810 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4811 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004812 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004813
4814 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4815 channel_shutr_now(chn);
4816 channel_shutw_now(chn);
4817 }
4818 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004819 goto check_channel_flags;
4820 }
4821
4822 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4823 http_msg_closing:
4824 /* nothing else to forward, just waiting for the output buffer
4825 * to be empty and for the shutw_now to take effect.
4826 */
4827 if (channel_is_empty(chn)) {
4828 txn->req.msg_state = HTTP_MSG_CLOSED;
4829 goto http_msg_closed;
4830 }
4831 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004832 txn->req.msg_state = HTTP_MSG_ERROR;
4833 goto end;
4834 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004835 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004836 return;
4837 }
4838
4839 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4840 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004841 /* if we don't know whether the server will close, we need to hard close */
4842 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4843 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004844 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004845 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004846 channel_dont_read(chn);
4847 goto end;
4848 }
4849
4850 check_channel_flags:
4851 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4852 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4853 /* if we've just closed an output, let's switch */
4854 txn->req.msg_state = HTTP_MSG_CLOSING;
4855 goto http_msg_closing;
4856 }
4857
4858 end:
4859 chn->analysers &= AN_REQ_FLT_END;
4860 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4861 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4862 channel_auto_close(chn);
4863 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004864 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004865}
4866
4867
4868/* This function terminates the response because it was completly analyzed or
4869 * because an error was triggered during the body forwarding.
4870 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004871static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004872{
4873 struct channel *chn = &s->res;
4874 struct http_txn *txn = s->txn;
4875
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004876 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004877
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004878 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4879 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004880 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004881 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004882 goto end;
4883 }
4884
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004885 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4886 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004887 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004888 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004889
4890 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4891 /* In theory, we don't need to read anymore, but we must
4892 * still monitor the server connection for a possible close
4893 * while the request is being uploaded, so we don't disable
4894 * reading.
4895 */
4896 /* channel_dont_read(chn); */
4897
4898 if (txn->req.msg_state < HTTP_MSG_DONE) {
4899 /* The client seems to still be sending data, probably
4900 * because we got an error response during an upload.
4901 * We have the choice of either breaking the connection
4902 * or letting it pass through. Let's do the later.
4903 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004904 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004905 return;
4906 }
4907
4908 /* When we get here, it means that both the request and the
4909 * response have finished receiving. Depending on the connection
4910 * mode, we'll have to wait for the last bytes to leave in either
4911 * direction, and sometimes for a close to be effective.
4912 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004913 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004914 channel_auto_read(chn);
4915 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004916 if (b_data(&chn->buf)) {
4917 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004918 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004919 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004920 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4921 }
4922 else {
4923 /* we're not expecting any new data to come for this
4924 * transaction, so we can close it.
4925 */
4926 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4927 channel_shutr_now(chn);
4928 channel_shutw_now(chn);
4929 }
4930 }
4931 goto check_channel_flags;
4932 }
4933
4934 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4935 http_msg_closing:
4936 /* nothing else to forward, just waiting for the output buffer
4937 * to be empty and for the shutw_now to take effect.
4938 */
4939 if (channel_is_empty(chn)) {
4940 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4941 goto http_msg_closed;
4942 }
4943 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004944 txn->rsp.msg_state = HTTP_MSG_ERROR;
Olivier Houcharda798bf52019-03-08 18:52:00 +01004945 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004946 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01004947 _HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004948 goto end;
4949 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004950 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004951 return;
4952 }
4953
4954 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4955 http_msg_closed:
4956 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004957 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004958 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004959 goto end;
4960 }
4961
4962 check_channel_flags:
4963 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4964 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4965 /* if we've just closed an output, let's switch */
4966 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4967 goto http_msg_closing;
4968 }
4969
4970 end:
4971 chn->analysers &= AN_RES_FLT_END;
4972 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4973 chn->analysers |= AN_RES_FLT_XFER_DATA;
4974 channel_auto_close(chn);
4975 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004976 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004977}
4978
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004979void http_server_error(struct stream *s, struct stream_interface *si, int err,
4980 int finst, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004981{
4982 channel_auto_read(si_oc(si));
4983 channel_abort(si_oc(si));
4984 channel_auto_close(si_oc(si));
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004985 channel_htx_erase(si_oc(si), htxbuf(&(si_oc(si))->buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02004986 channel_auto_close(si_ic(si));
4987 channel_auto_read(si_ic(si));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004988
4989 /* <msg> is an HTX structure. So we copy it in the response's
4990 * channel */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004991 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004992 struct channel *chn = si_ic(si);
4993 struct htx *htx;
4994
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004995 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004996 chn->buf.data = msg->data;
4997 memcpy(chn->buf.area, msg->area, msg->data);
4998 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02004999 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02005000 c_adv(chn, htx->data);
5001 chn->total += htx->data;
5002 }
5003 if (!(s->flags & SF_ERR_MASK))
5004 s->flags |= err;
5005 if (!(s->flags & SF_FINST_MASK))
5006 s->flags |= finst;
5007}
5008
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005009void http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02005010{
5011 channel_auto_read(&s->req);
5012 channel_abort(&s->req);
5013 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005014 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
5015 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02005016
5017 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005018
5019 /* <msg> is an HTX structure. So we copy it in the response's
5020 * channel */
5021 /* FIXME: It is a problem for now if there is some outgoing data */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02005022 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02005023 struct channel *chn = &s->res;
5024 struct htx *htx;
5025
Christopher Fauletaed82cf2018-11-30 22:22:32 +01005026 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005027 chn->buf.data = msg->data;
5028 memcpy(chn->buf.area, msg->area, msg->data);
5029 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02005030 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02005031 c_adv(chn, htx->data);
5032 chn->total += htx->data;
5033 }
5034
5035 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
5036 channel_auto_read(&s->res);
5037 channel_auto_close(&s->res);
5038 channel_shutr_now(&s->res);
5039}
5040
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005041struct buffer *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005042{
5043 const int msgnum = http_get_status_idx(s->txn->status);
5044
5045 if (s->be->errmsg[msgnum].area)
5046 return &s->be->errmsg[msgnum];
5047 else if (strm_fe(s)->errmsg[msgnum].area)
5048 return &strm_fe(s)->errmsg[msgnum];
5049 else
Christopher Fauletf7346382019-07-17 22:02:08 +02005050 return &http_err_chunks[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005051}
5052
Christopher Faulet304cc402019-07-15 15:46:28 +02005053/* Return the error message corresponding to si->err_type. It is assumed
5054 * that the server side is closed. Note that err_type is actually a
5055 * bitmask, where almost only aborts may be cumulated with other
5056 * values. We consider that aborted operations are more important
5057 * than timeouts or errors due to the fact that nobody else in the
5058 * logs might explain incomplete retries. All others should avoid
5059 * being cumulated. It should normally not be possible to have multiple
5060 * aborts at once, but just in case, the first one in sequence is reported.
5061 * Note that connection errors appearing on the second request of a keep-alive
5062 * connection are not reported since this allows the client to retry.
5063 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005064void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02005065{
5066 int err_type = si->err_type;
5067
5068 /* set s->txn->status for http_error_message(s) */
5069 s->txn->status = 503;
5070
5071 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005072 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
5073 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005074 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005075 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
5076 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5077 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005078 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005079 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
5080 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005081 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005082 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
5083 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005084 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005085 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
5086 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5087 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005088 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005089 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
5090 (s->flags & SF_SRV_REUSED) ? NULL :
5091 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005092 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005093 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
5094 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5095 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005096 else { /* SI_ET_CONN_OTHER and others */
5097 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005098 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
5099 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005100 }
5101}
5102
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005103
Christopher Faulet4a28a532019-03-01 11:19:40 +01005104/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
5105 * on success and -1 on error.
5106 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005107static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01005108{
5109 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
5110 * then we must send an HTTP/1.1 100 Continue intermediate response.
5111 */
5112 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
5113 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
5114 struct ist hdr = { .ptr = "Expect", .len = 6 };
5115 struct http_hdr_ctx ctx;
5116
5117 ctx.blk = NULL;
5118 /* Expect is allowed in 1.1, look for it */
5119 if (http_find_header(htx, hdr, &ctx, 0) &&
5120 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005121 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01005122 return -1;
5123 http_remove_header(htx, &ctx);
5124 }
5125 }
5126 return 0;
5127}
5128
Christopher Faulet23a3c792018-11-28 10:01:23 +01005129/* Send a 100-Continue response to the client. It returns 0 on success and -1
5130 * on error. The response channel is updated accordingly.
5131 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005132static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01005133{
5134 struct channel *res = &s->res;
5135 struct htx *htx = htx_from_buf(&res->buf);
5136 struct htx_sl *sl;
5137 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
5138 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
5139 size_t data;
5140
5141 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5142 ist("HTTP/1.1"), ist("100"), ist("Continue"));
5143 if (!sl)
5144 goto fail;
5145 sl->info.res.status = 100;
5146
Christopher Faulet1d5ec092019-06-26 14:23:54 +02005147 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01005148 goto fail;
5149
5150 data = htx->data - co_data(res);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005151 c_adv(res, data);
5152 res->total += data;
5153 return 0;
5154
5155 fail:
5156 /* If an error occurred, remove the incomplete HTTP response from the
5157 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005158 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005159 return -1;
5160}
5161
Christopher Faulet12c51e22018-11-28 15:59:42 +01005162
5163/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
5164 * ont whether we use a proxy or not. It returns 0 on success and -1 on
5165 * error. The response channel is updated accordingly.
5166 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005167static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
Christopher Faulet12c51e22018-11-28 15:59:42 +01005168{
5169 struct channel *res = &s->res;
5170 struct htx *htx = htx_from_buf(&res->buf);
5171 struct htx_sl *sl;
5172 struct ist code, body;
5173 int status;
5174 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5175 size_t data;
5176
5177 if (!(s->txn->flags & TX_USE_PX_CONN)) {
5178 status = 401;
5179 code = ist("401");
5180 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
5181 "You need a valid user and password to access this content.\n"
5182 "</body></html>\n");
5183 }
5184 else {
5185 status = 407;
5186 code = ist("407");
5187 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
5188 "You need a valid user and password to access this content.\n"
5189 "</body></html>\n");
5190 }
5191
5192 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5193 ist("HTTP/1.1"), code, ist("Unauthorized"));
5194 if (!sl)
5195 goto fail;
5196 sl->info.res.status = status;
5197 s->txn->status = status;
5198
5199 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
5200 goto fail;
5201
Willy Tarreaub5ba2b02019-06-11 16:08:25 +02005202 if (!htx_add_header(htx, ist("Content-length"), ist("112")) ||
5203 !htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Faulet12c51e22018-11-28 15:59:42 +01005204 !htx_add_header(htx, ist("Connection"), ist("close")) ||
Jérôme Magnin86cef232018-12-28 14:49:08 +01005205 !htx_add_header(htx, ist("Content-Type"), ist("text/html")))
5206 goto fail;
5207 if (status == 401 && !htx_add_header(htx, ist("WWW-Authenticate"), ist2(trash.area, trash.data)))
5208 goto fail;
5209 if (status == 407 && !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005210 goto fail;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02005211 if (!htx_add_endof(htx, HTX_BLK_EOH))
5212 goto fail;
5213
5214 while (body.len) {
5215 size_t sent = htx_add_data(htx, body);
5216 if (!sent)
5217 goto fail;
5218 body.ptr += sent;
5219 body.len -= sent;
5220 }
5221
5222 if (!htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005223 goto fail;
5224
Christopher Faulet06511812019-10-16 09:38:27 +02005225 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet12c51e22018-11-28 15:59:42 +01005226 data = htx->data - co_data(res);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005227 c_adv(res, data);
5228 res->total += data;
5229
5230 channel_auto_read(&s->req);
5231 channel_abort(&s->req);
5232 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005233 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet12c51e22018-11-28 15:59:42 +01005234
5235 res->wex = tick_add_ifset(now_ms, res->wto);
5236 channel_auto_read(res);
5237 channel_auto_close(res);
5238 channel_shutr_now(res);
5239 return 0;
5240
5241 fail:
5242 /* If an error occurred, remove the incomplete HTTP response from the
5243 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005244 channel_htx_truncate(res, htx);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005245 return -1;
5246}
5247
Christopher Faulet0f226952018-10-22 09:29:56 +02005248/*
5249 * Capture headers from message <htx> according to header list <cap_hdr>, and
5250 * fill the <cap> pointers appropriately.
5251 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005252static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02005253{
5254 struct cap_hdr *h;
5255 int32_t pos;
5256
Christopher Fauleta3f15502019-05-13 15:27:23 +02005257 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02005258 struct htx_blk *blk = htx_get_blk(htx, pos);
5259 enum htx_blk_type type = htx_get_blk_type(blk);
5260 struct ist n, v;
5261
5262 if (type == HTX_BLK_EOH)
5263 break;
5264 if (type != HTX_BLK_HDR)
5265 continue;
5266
5267 n = htx_get_blk_name(htx, blk);
5268
5269 for (h = cap_hdr; h; h = h->next) {
5270 if (h->namelen && (h->namelen == n.len) &&
5271 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
5272 if (cap[h->index] == NULL)
5273 cap[h->index] =
5274 pool_alloc(h->pool);
5275
5276 if (cap[h->index] == NULL) {
5277 ha_alert("HTTP capture : out of memory.\n");
5278 break;
5279 }
5280
5281 v = htx_get_blk_value(htx, blk);
5282 if (v.len > h->len)
5283 v.len = h->len;
5284
5285 memcpy(cap[h->index], v.ptr, v.len);
5286 cap[h->index][v.len]=0;
5287 }
5288 }
5289 }
5290}
5291
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005292/* Delete a value in a header between delimiters <from> and <next>. The header
5293 * itself is delimited by <start> and <end> pointers. The number of characters
5294 * displaced is returned, and the pointer to the first delimiter is updated if
5295 * required. The function tries as much as possible to respect the following
5296 * principles :
5297 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5298 * in which case <next> is simply removed
5299 * - set exactly one space character after the new first delimiter, unless there
5300 * are not enough characters in the block being moved to do so.
5301 * - remove unneeded spaces before the previous delimiter and after the new
5302 * one.
5303 *
5304 * It is the caller's responsibility to ensure that :
5305 * - <from> points to a valid delimiter or <start> ;
5306 * - <next> points to a valid delimiter or <end> ;
5307 * - there are non-space chars before <from>.
5308 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005309static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005310{
5311 char *prev = *from;
5312
5313 if (prev == start) {
5314 /* We're removing the first value. eat the semicolon, if <next>
5315 * is lower than <end> */
5316 if (next < end)
5317 next++;
5318
5319 while (next < end && HTTP_IS_SPHT(*next))
5320 next++;
5321 }
5322 else {
5323 /* Remove useless spaces before the old delimiter. */
5324 while (HTTP_IS_SPHT(*(prev-1)))
5325 prev--;
5326 *from = prev;
5327
5328 /* copy the delimiter and if possible a space if we're
5329 * not at the end of the line.
5330 */
5331 if (next < end) {
5332 *prev++ = *next++;
5333 if (prev + 1 < next)
5334 *prev++ = ' ';
5335 while (next < end && HTTP_IS_SPHT(*next))
5336 next++;
5337 }
5338 }
5339 memmove(prev, next, end - next);
5340 return (prev - next);
5341}
5342
Christopher Faulet0f226952018-10-22 09:29:56 +02005343
5344/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005345 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005346 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005347static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005348{
5349 struct ist dst = ist2(str, 0);
5350
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005351 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005352 goto end;
5353 if (dst.len + 1 > len)
5354 goto end;
5355 dst.ptr[dst.len++] = ' ';
5356
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005357 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005358 goto end;
5359 if (dst.len + 1 > len)
5360 goto end;
5361 dst.ptr[dst.len++] = ' ';
5362
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005363 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005364 end:
5365 return dst.len;
5366}
5367
5368/*
5369 * Print a debug line with a start line.
5370 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005371static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005372{
5373 struct session *sess = strm_sess(s);
5374 int max;
5375
5376 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5377 dir,
5378 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5379 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5380
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005381 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005382 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005383 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005384 trash.area[trash.data++] = ' ';
5385
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005386 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005387 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005388 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005389 trash.area[trash.data++] = ' ';
5390
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005391 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005392 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005393 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005394 trash.area[trash.data++] = '\n';
5395
5396 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5397}
5398
5399/*
5400 * Print a debug line with a header.
5401 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005402static 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 +02005403{
5404 struct session *sess = strm_sess(s);
5405 int max;
5406
5407 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5408 dir,
5409 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5410 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5411
5412 max = n.len;
5413 UBOUND(max, trash.size - trash.data - 3);
5414 chunk_memcat(&trash, n.ptr, max);
5415 trash.area[trash.data++] = ':';
5416 trash.area[trash.data++] = ' ';
5417
5418 max = v.len;
5419 UBOUND(max, trash.size - trash.data - 1);
5420 chunk_memcat(&trash, v.ptr, max);
5421 trash.area[trash.data++] = '\n';
5422
5423 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5424}
5425
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005426/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5427 * In case of allocation failure, everything allocated is freed and NULL is
5428 * returned. Otherwise the new transaction is assigned to the stream and
5429 * returned.
5430 */
5431struct http_txn *http_alloc_txn(struct stream *s)
5432{
5433 struct http_txn *txn = s->txn;
5434
5435 if (txn)
5436 return txn;
5437
5438 txn = pool_alloc(pool_head_http_txn);
5439 if (!txn)
5440 return txn;
5441
5442 s->txn = txn;
5443 return txn;
5444}
5445
5446void http_txn_reset_req(struct http_txn *txn)
5447{
5448 txn->req.flags = 0;
5449 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5450}
5451
5452void http_txn_reset_res(struct http_txn *txn)
5453{
5454 txn->rsp.flags = 0;
5455 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5456}
5457
5458/*
5459 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5460 * the required fields are properly allocated and that we only need to (re)init
5461 * them. This should be used before processing any new request.
5462 */
5463void http_init_txn(struct stream *s)
5464{
5465 struct http_txn *txn = s->txn;
5466 struct conn_stream *cs = objt_cs(s->si[0].end);
5467
5468 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5469 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5470 : 0);
5471 txn->status = -1;
5472 *(unsigned int *)txn->cache_hash = 0;
5473
5474 txn->cookie_first_date = 0;
5475 txn->cookie_last_date = 0;
5476
5477 txn->srv_cookie = NULL;
5478 txn->cli_cookie = NULL;
5479 txn->uri = NULL;
5480
5481 http_txn_reset_req(txn);
5482 http_txn_reset_res(txn);
5483
5484 txn->req.chn = &s->req;
5485 txn->rsp.chn = &s->res;
5486
5487 txn->auth.method = HTTP_AUTH_UNKNOWN;
5488
5489 vars_init(&s->vars_txn, SCOPE_TXN);
5490 vars_init(&s->vars_reqres, SCOPE_REQ);
5491}
5492
5493/* to be used at the end of a transaction */
5494void http_end_txn(struct stream *s)
5495{
5496 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005497
5498 /* these ones will have been dynamically allocated */
5499 pool_free(pool_head_requri, txn->uri);
5500 pool_free(pool_head_capture, txn->cli_cookie);
5501 pool_free(pool_head_capture, txn->srv_cookie);
5502 pool_free(pool_head_uniqueid, s->unique_id);
5503
5504 s->unique_id = NULL;
5505 txn->uri = NULL;
5506 txn->srv_cookie = NULL;
5507 txn->cli_cookie = NULL;
5508
Christopher Faulet59399252019-11-07 14:27:52 +01005509 if (!LIST_ISEMPTY(&s->vars_txn.head))
5510 vars_prune(&s->vars_txn, s->sess, s);
5511 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5512 vars_prune(&s->vars_reqres, s->sess, s);
5513}
5514
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005515
5516DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
5517DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
Christopher Faulet0f226952018-10-22 09:29:56 +02005518
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005519__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005520static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005521{
5522}
5523
5524
5525/*
5526 * Local variables:
5527 * c-indent-level: 8
5528 * c-basic-offset: 8
5529 * End:
5530 */