blob: 1002dab4a7739501befcf0c0d4a05a0ec2e960dc [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 Fauletfc9cfe42019-07-16 14:54:53 +020029#include <proto/http_ana.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020030#include <proto/proxy.h>
Christopher Fauletfefc73d2018-10-24 21:18:04 +020031#include <proto/server.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020032#include <proto/stream.h>
33#include <proto/stream_interface.h>
34#include <proto/stats.h>
Christopher Fauleta8a46e22019-07-16 14:53:09 +020035#include <proto/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020036
Christopher Fauleteea8fc72019-11-05 16:18:10 +010037#define TRACE_SOURCE &trace_strm
38
Christopher Faulet377c5a52018-10-24 21:21:30 +020039extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020040
Christopher Fauleta8a46e22019-07-16 14:53:09 +020041struct pool_head *pool_head_requri = NULL;
42struct pool_head *pool_head_capture = NULL;
43
44
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020045static void http_end_request(struct stream *s);
46static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020047
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020048static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
49static int http_del_hdr_value(char *start, char *end, char **from, char *next);
50static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020051static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
52static 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 +020053
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020054static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status);
55static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020056
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020057static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
58static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020059
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020060static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
61static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020062
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020063static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
64static int http_reply_100_continue(struct stream *s);
65static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm);
Christopher Faulet23a3c792018-11-28 10:01:23 +010066
Christopher Faulete0768eb2018-10-03 16:38:02 +020067/* This stream analyser waits for a complete HTTP request. It returns 1 if the
68 * processing can continue on next analysers, or zero if it either needs more
69 * data or wants to immediately abort the request (eg: timeout, error, ...). It
70 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
71 * when it has nothing left to do, and may remove any analyser when it wants to
72 * abort.
73 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020074int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020075{
Christopher Faulet9768c262018-10-22 09:34:31 +020076
Christopher Faulete0768eb2018-10-03 16:38:02 +020077 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020078 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020079 *
Christopher Faulet9768c262018-10-22 09:34:31 +020080 * Once the start line and all headers are received, we may perform a
81 * capture of the error (if any), and we will set a few fields. We also
82 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020083 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020084 struct session *sess = s->sess;
85 struct http_txn *txn = s->txn;
86 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020087 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010088 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020089
Christopher Fauleteea8fc72019-11-05 16:18:10 +010090 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020091
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010092 htx = htxbuf(&req->buf);
Christopher Faulet9768c262018-10-22 09:34:31 +020093
Willy Tarreau4236f032019-03-05 10:43:32 +010094 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +020095 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +010096 stream_inc_http_req_ctr(s);
97 stream_inc_http_err_ctr(s);
98 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletb9a92f32019-09-09 10:15:21 +020099 if (htx->flags & HTX_FL_PARSING_ERROR)
100 goto return_bad_req;
101 else
102 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100103 }
104
Christopher Faulete0768eb2018-10-03 16:38:02 +0200105 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200106 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200107
108 /* If there is data available for analysis, log the end of the idle time. */
Christopher Faulet870aad92018-11-29 15:23:46 +0100109 if (c_data(req) && s->logs.t_idle == -1) {
110 const struct cs_info *csinfo = si_get_cs_info(objt_cs(s->si[0].end));
111
112 s->logs.t_idle = ((csinfo)
113 ? csinfo->t_idle
114 : tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake);
115 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200116
Christopher Faulete0768eb2018-10-03 16:38:02 +0200117 /*
118 * Now we quickly check if we have found a full valid request.
119 * If not so, we check the FD and buffer states before leaving.
120 * A full request is indicated by the fact that we have seen
121 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
122 * requests are checked first. When waiting for a second request
123 * on a keep-alive stream, if we encounter and error, close, t/o,
124 * we note the error in the stream flags but don't set any state.
125 * Since the error will be noted there, it will not be counted by
126 * process_stream() as a frontend error.
127 * Last, we may increase some tracked counters' http request errors on
128 * the cases that are deliberately the client's fault. For instance,
129 * a timeout or connection reset is not counted as an error. However
130 * a bad request is.
131 */
Christopher Faulet29f17582019-05-23 11:03:26 +0200132 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200133 if (htx->flags & HTX_FL_UPGRADE)
134 goto failed_keep_alive;
135
Christopher Faulet9768c262018-10-22 09:34:31 +0200136 /* 1: have we encountered a read error ? */
137 if (req->flags & CF_READ_ERROR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200138 if (!(s->flags & SF_ERR_MASK))
139 s->flags |= SF_ERR_CLICL;
140
141 if (txn->flags & TX_WAIT_NEXT_RQ)
142 goto failed_keep_alive;
143
144 if (sess->fe->options & PR_O_IGNORE_PRB)
145 goto failed_keep_alive;
146
Christopher Faulet9768c262018-10-22 09:34:31 +0200147 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200148 stream_inc_http_req_ctr(s);
149 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100150 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200151 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100152 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200153
Christopher Faulet9768c262018-10-22 09:34:31 +0200154 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200155 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet9768c262018-10-22 09:34:31 +0200156 req->analysers &= AN_REQ_FLT_END;
157
Christopher Faulete0768eb2018-10-03 16:38:02 +0200158 if (!(s->flags & SF_FINST_MASK))
159 s->flags |= SF_FINST_R;
160 return 0;
161 }
162
Christopher Faulet9768c262018-10-22 09:34:31 +0200163 /* 2: has the read timeout expired ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200164 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
165 if (!(s->flags & SF_ERR_MASK))
166 s->flags |= SF_ERR_CLITO;
167
168 if (txn->flags & TX_WAIT_NEXT_RQ)
169 goto failed_keep_alive;
170
171 if (sess->fe->options & PR_O_IGNORE_PRB)
172 goto failed_keep_alive;
173
Christopher Faulet9768c262018-10-22 09:34:31 +0200174 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200175 stream_inc_http_req_ctr(s);
176 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100177 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200178 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100179 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200180
Christopher Faulet9768c262018-10-22 09:34:31 +0200181 txn->status = 408;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200182 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200183 req->analysers &= AN_REQ_FLT_END;
184
Christopher Faulete0768eb2018-10-03 16:38:02 +0200185 if (!(s->flags & SF_FINST_MASK))
186 s->flags |= SF_FINST_R;
187 return 0;
188 }
189
Christopher Faulet9768c262018-10-22 09:34:31 +0200190 /* 3: have we encountered a close ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200191 else if (req->flags & CF_SHUTR) {
192 if (!(s->flags & SF_ERR_MASK))
193 s->flags |= SF_ERR_CLICL;
194
195 if (txn->flags & TX_WAIT_NEXT_RQ)
196 goto failed_keep_alive;
197
198 if (sess->fe->options & PR_O_IGNORE_PRB)
199 goto failed_keep_alive;
200
Christopher Faulete0768eb2018-10-03 16:38:02 +0200201 stream_inc_http_err_ctr(s);
202 stream_inc_http_req_ctr(s);
203 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100204 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200205 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100206 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200207
Christopher Faulet9768c262018-10-22 09:34:31 +0200208 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200209 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200210 req->analysers &= AN_REQ_FLT_END;
211
Christopher Faulete0768eb2018-10-03 16:38:02 +0200212 if (!(s->flags & SF_FINST_MASK))
213 s->flags |= SF_FINST_R;
214 return 0;
215 }
216
217 channel_dont_connect(req);
218 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
219 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100220
Christopher Faulet9768c262018-10-22 09:34:31 +0200221 if (sess->listener->options & LI_O_NOQUICKACK && htx_is_not_empty(htx) &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200222 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
223 /* We need more data, we have to re-enable quick-ack in case we
224 * previously disabled it, otherwise we might cause the client
225 * to delay next data.
226 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100227 conn_set_quickack(objt_conn(sess->origin), 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200228 }
Willy Tarreau1a18b542018-12-11 16:37:42 +0100229
Christopher Faulet47365272018-10-31 17:40:50 +0100230 if ((req->flags & CF_READ_PARTIAL) && (txn->flags & TX_WAIT_NEXT_RQ)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200231 /* If the client starts to talk, let's fall back to
232 * request timeout processing.
233 */
234 txn->flags &= ~TX_WAIT_NEXT_RQ;
235 req->analyse_exp = TICK_ETERNITY;
236 }
237
238 /* just set the request timeout once at the beginning of the request */
239 if (!tick_isset(req->analyse_exp)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100240 if ((txn->flags & TX_WAIT_NEXT_RQ) && tick_isset(s->be->timeout.httpka))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200241 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
242 else
243 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
244 }
245
246 /* we're not ready yet */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100247 DBG_TRACE_DEVEL("waiting for the request",
248 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200249 return 0;
250
251 failed_keep_alive:
252 /* Here we process low-level errors for keep-alive requests. In
253 * short, if the request is not the first one and it experiences
254 * a timeout, read error or shutdown, we just silently close so
255 * that the client can try again.
256 */
257 txn->status = 0;
258 msg->msg_state = HTTP_MSG_RQBEFORE;
259 req->analysers &= AN_REQ_FLT_END;
260 s->logs.logwait = 0;
261 s->logs.level = 0;
262 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200263 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100264 DBG_TRACE_DEVEL("leaving by closing K/A connection",
265 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200266 return 0;
267 }
268
Christopher Faulet9768c262018-10-22 09:34:31 +0200269 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200270 stream_inc_http_req_ctr(s);
271 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
272
Christopher Faulet9768c262018-10-22 09:34:31 +0200273 /* kill the pending keep-alive timeout */
274 txn->flags &= ~TX_WAIT_NEXT_RQ;
275 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200276
Christopher Faulet29f17582019-05-23 11:03:26 +0200277 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200278 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100279
Christopher Faulet9768c262018-10-22 09:34:31 +0200280 /* 0: we might have to print this header in debug mode */
281 if (unlikely((global.mode & MODE_DEBUG) &&
282 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
283 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200284
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200285 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200286
Christopher Fauleta3f15502019-05-13 15:27:23 +0200287 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200288 struct htx_blk *blk = htx_get_blk(htx, pos);
289 enum htx_blk_type type = htx_get_blk_type(blk);
290
291 if (type == HTX_BLK_EOH)
292 break;
293 if (type != HTX_BLK_HDR)
294 continue;
295
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200296 http_debug_hdr("clihdr", s,
297 htx_get_blk_name(htx, blk),
298 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200299 }
300 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200301
302 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100303 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200304 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100305 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100306 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200307 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100308 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +0100309 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100310 if (sl->flags & HTX_SL_F_BODYLESS)
311 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200312
313 /* we can make use of server redirect on GET and HEAD */
314 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
315 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100316 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200317 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200318 goto return_bad_req;
319 }
320
321 /*
322 * 2: check if the URI matches the monitor_uri.
323 * We have to do this for every request which gets in, because
324 * the monitor-uri is defined by the frontend.
325 */
326 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100327 isteqi(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200328 /*
329 * We have found the monitor URI
330 */
331 struct acl_cond *cond;
332
333 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100334 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200335
336 /* Check if we want to fail this monitor request or not */
337 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
338 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
339
340 ret = acl_pass(ret);
341 if (cond->pol == ACL_COND_UNLESS)
342 ret = !ret;
343
344 if (ret) {
345 /* we fail this request, let's return 503 service unavail */
346 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200347 if (!(s->flags & SF_ERR_MASK))
348 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
349 goto return_prx_cond;
350 }
351 }
352
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800353 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200354 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200355 if (!(s->flags & SF_ERR_MASK))
356 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
357 goto return_prx_cond;
358 }
359
360 /*
361 * 3: Maybe we have to copy the original REQURI for the logs ?
362 * Note: we cannot log anymore if the request has been
363 * classified as invalid.
364 */
365 if (unlikely(s->logs.logwait & LW_REQ)) {
366 /* we have a complete HTTP request that we must log */
367 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200368 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200369
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200370 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200371 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200372
373 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
374 s->do_log(s);
375 } else {
376 ha_alert("HTTP logging : out of memory.\n");
377 }
378 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200379
Christopher Faulete0768eb2018-10-03 16:38:02 +0200380 /* if the frontend has "option http-use-proxy-header", we'll check if
381 * we have what looks like a proxied connection instead of a connection,
382 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
383 * Note that this is *not* RFC-compliant, however browsers and proxies
384 * happen to do that despite being non-standard :-(
385 * We consider that a request not beginning with either '/' or '*' is
386 * a proxied connection, which covers both "scheme://location" and
387 * CONNECT ip:port.
388 */
389 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100390 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200391 txn->flags |= TX_USE_PX_CONN;
392
Christopher Faulete0768eb2018-10-03 16:38:02 +0200393 /* 5: we may need to capture headers */
394 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200395 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200396
Christopher Faulete0768eb2018-10-03 16:38:02 +0200397 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200398 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200399 req->analysers |= AN_REQ_HTTP_BODY;
400
401 /*
402 * RFC7234#4:
403 * A cache MUST write through requests with methods
404 * that are unsafe (Section 4.2.1 of [RFC7231]) to
405 * the origin server; i.e., a cache is not allowed
406 * to generate a reply to such a request before
407 * having forwarded the request and having received
408 * a corresponding response.
409 *
410 * RFC7231#4.2.1:
411 * Of the request methods defined by this
412 * specification, the GET, HEAD, OPTIONS, and TRACE
413 * methods are defined to be safe.
414 */
415 if (likely(txn->meth == HTTP_METH_GET ||
416 txn->meth == HTTP_METH_HEAD ||
417 txn->meth == HTTP_METH_OPTIONS ||
418 txn->meth == HTTP_METH_TRACE))
419 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
420
421 /* end of job, return OK */
422 req->analysers &= ~an_bit;
423 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200424
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100425 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200426 return 1;
427
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200428 return_int_err:
429 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200430 if (!(s->flags & SF_ERR_MASK))
431 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100432 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200433 if (sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100434 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200435 goto return_prx_cond;
436
Christopher Faulete0768eb2018-10-03 16:38:02 +0200437 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200438 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100439 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200440 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100441 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200442 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200443
444 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200445 http_reply_and_close(s, txn->status, http_error_message(s));
446
Christopher Faulete0768eb2018-10-03 16:38:02 +0200447 if (!(s->flags & SF_ERR_MASK))
448 s->flags |= SF_ERR_PRXCOND;
449 if (!(s->flags & SF_FINST_MASK))
450 s->flags |= SF_FINST_R;
451
452 req->analysers &= AN_REQ_FLT_END;
453 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100454 DBG_TRACE_DEVEL("leaving on error",
455 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200456 return 0;
457}
458
459
460/* This stream analyser runs all HTTP request processing which is common to
461 * frontends and backends, which means blocking ACLs, filters, connection-close,
462 * reqadd, stats and redirects. This is performed for the designated proxy.
463 * It returns 1 if the processing can continue on next analysers, or zero if it
464 * either needs more data or wants to immediately abort the request (eg: deny,
465 * error, ...).
466 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200467int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200468{
469 struct session *sess = s->sess;
470 struct http_txn *txn = s->txn;
471 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200472 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200473 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200474 enum rule_result verdict;
475 int deny_status = HTTP_ERR_403;
476 struct connection *conn = objt_conn(sess->origin);
477
478 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
479 /* we need more data */
480 goto return_prx_yield;
481 }
482
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100483 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200484
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100485 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200486
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200487 /* just in case we have some per-backend tracking. Only called the first
488 * execution of the analyser. */
489 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
490 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200491
492 /* evaluate http-request rules */
493 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200494 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200495
496 switch (verdict) {
497 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
498 goto return_prx_yield;
499
500 case HTTP_RULE_RES_CONT:
501 case HTTP_RULE_RES_STOP: /* nothing to do */
502 break;
503
504 case HTTP_RULE_RES_DENY: /* deny or tarpit */
505 if (txn->flags & TX_CLTARPIT)
506 goto tarpit;
507 goto deny;
508
509 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
510 goto return_prx_cond;
511
512 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
513 goto done;
514
515 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
516 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100517
518 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
519 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200520 }
521 }
522
523 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
524 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200525 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200526
Christopher Fauletff2759f2018-10-24 11:13:16 +0200527 ctx.blk = NULL;
528 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
529 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100530 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200531 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200532 }
533
534 /* OK at this stage, we know that the request was accepted according to
535 * the http-request rules, we can check for the stats. Note that the
536 * URI is detected *before* the req* rules in order not to be affected
537 * by a possible reqrep, while they are processed *after* so that a
538 * reqdeny can still block them. This clearly needs to change in 1.6!
539 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200540 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200541 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100542 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200543 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200544 if (!(s->flags & SF_ERR_MASK))
545 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100546 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200547 }
548
549 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200550 http_handle_stats(s, req);
551 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200552 /* not all actions implemented: deny, allow, auth */
553
554 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
555 goto deny;
556
557 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
558 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100559
560 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
561 goto return_bad_req;
562
563 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
564 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200565 }
566
Christopher Faulet2571bc62019-03-01 11:44:26 +0100567 /* Proceed with the applets now. */
568 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200569 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100570 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200571
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200572 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100573 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100574
Christopher Faulete0768eb2018-10-03 16:38:02 +0200575 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
576 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
577 if (!(s->flags & SF_FINST_MASK))
578 s->flags |= SF_FINST_R;
579
580 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
581 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
582 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
583 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100584
585 req->flags |= CF_SEND_DONTWAIT;
586 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200587 goto done;
588 }
589
590 /* check whether we have some ACLs set to redirect this request */
591 list_for_each_entry(rule, &px->redirect_rules, list) {
592 if (rule->cond) {
593 int ret;
594
595 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
596 ret = acl_pass(ret);
597 if (rule->cond->pol == ACL_COND_UNLESS)
598 ret = !ret;
599 if (!ret)
600 continue;
601 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200602 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100603 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200604 goto done;
605 }
606
607 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
608 * If this happens, then the data will not come immediately, so we must
609 * send all what we have without waiting. Note that due to the small gain
610 * in waiting for the body of the request, it's easier to simply put the
611 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
612 * itself once used.
613 */
614 req->flags |= CF_SEND_DONTWAIT;
615
616 done: /* done with this analyser, continue with next ones that the calling
617 * points will have set, if any.
618 */
619 req->analyse_exp = TICK_ETERNITY;
620 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
621 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100622 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200623 return 1;
624
625 tarpit:
626 /* Allow cookie logging
627 */
628 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200629 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200630
631 /* When a connection is tarpitted, we use the tarpit timeout,
632 * which may be the same as the connect timeout if unspecified.
633 * If unset, then set it to zero because we really want it to
634 * eventually expire. We build the tarpit as an analyser.
635 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100636 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200637
638 /* wipe the request out so that we can drop the connection early
639 * if the client closes first.
640 */
641 channel_dont_connect(req);
642
643 txn->status = http_err_codes[deny_status];
644
645 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
646 req->analysers |= AN_REQ_HTTP_TARPIT;
647 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
648 if (!req->analyse_exp)
649 req->analyse_exp = tick_add(now_ms, 0);
650 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100651 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100652 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100653 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200654 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100655 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200656 goto done_without_exp;
657
658 deny: /* this request was blocked (denied) */
659
660 /* Allow cookie logging
661 */
662 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200663 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200664
665 txn->flags |= TX_CLDENY;
666 txn->status = http_err_codes[deny_status];
667 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200668 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100669 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100670 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100671 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200672 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100673 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100674 goto return_prx_err;
675
676 return_int_err:
677 txn->status = 500;
678 if (!(s->flags & SF_ERR_MASK))
679 s->flags |= SF_ERR_INTERNAL;
680 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100681 if (s->flags & SF_BE_ASSIGNED)
682 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100683 if (sess->listener->counters)
684 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
685 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200686
687 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200688 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100689 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200690 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100691 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100692 /* fall through */
693
694 return_prx_err:
695 http_reply_and_close(s, txn->status, http_error_message(s));
696 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200697
698 return_prx_cond:
699 if (!(s->flags & SF_ERR_MASK))
700 s->flags |= SF_ERR_PRXCOND;
701 if (!(s->flags & SF_FINST_MASK))
702 s->flags |= SF_FINST_R;
703
704 req->analysers &= AN_REQ_FLT_END;
705 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100706 DBG_TRACE_DEVEL("leaving on error",
707 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200708 return 0;
709
710 return_prx_yield:
711 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100712 DBG_TRACE_DEVEL("waiting for more data",
713 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200714 return 0;
715}
716
717/* This function performs all the processing enabled for the current request.
718 * It returns 1 if the processing can continue on next analysers, or zero if it
719 * needs more data, encounters an error, or wants to immediately abort the
720 * request. It relies on buffers flags, and updates s->req.analysers.
721 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200722int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200723{
724 struct session *sess = s->sess;
725 struct http_txn *txn = s->txn;
726 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200727 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200728 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
729
730 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
731 /* we need more data */
732 channel_dont_connect(req);
733 return 0;
734 }
735
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100736 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200737
738 /*
739 * Right now, we know that we have processed the entire headers
740 * and that unwanted requests have been filtered out. We can do
741 * whatever we want with the remaining request. Also, now we
742 * may have separate values for ->fe, ->be.
743 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100744 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200745
746 /*
747 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200748 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200749 */
750 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100751 struct htx_sl *sl;
752 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200753
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200754 if (!sockaddr_alloc(&s->target_addr)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200755 if (!(s->flags & SF_ERR_MASK))
756 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100757 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200758 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200759 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100760 uri = htx_sl_req_uri(sl);
761 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200762
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200763 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200764 goto return_bad_req;
765
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200766 s->target = &s->be->obj_type;
767 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
768
Christopher Faulete0768eb2018-10-03 16:38:02 +0200769 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200770 * uri.ptr and path.ptr (excluded). If it was not found, we need
771 * to replace from all the uri by a single "/".
772 *
773 * Instead of rewritting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100774 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200775 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200776 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100777 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200778 }
779
780 /*
781 * 7: Now we can work with the cookies.
782 * Note that doing so might move headers in the request, but
783 * the fields will stay coherent and the URI will not move.
784 * This should only be performed in the backend.
785 */
786 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200787 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200788
789 /* add unique-id if "header-unique-id" is specified */
790
791 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100792 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) {
793 if (!(s->flags & SF_ERR_MASK))
794 s->flags |= SF_ERR_RESOURCE;
795 goto return_int_err;
796 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200797 s->unique_id[0] = '\0';
798 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
799 }
800
801 if (sess->fe->header_unique_id && s->unique_id) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200802 struct ist n = ist2(sess->fe->header_unique_id, strlen(sess->fe->header_unique_id));
803 struct ist v = ist2(s->unique_id, strlen(s->unique_id));
804
805 if (unlikely(!http_add_header(htx, n, v)))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100806 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200807 }
808
809 /*
810 * 9: add X-Forwarded-For if either the frontend or the backend
811 * asks for it.
812 */
813 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200814 struct http_hdr_ctx ctx = { .blk = NULL };
815 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
816 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
817
Christopher Faulete0768eb2018-10-03 16:38:02 +0200818 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200819 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200820 /* The header is set to be added only if none is present
821 * and we found it, so don't do anything.
822 */
823 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200824 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200825 /* Add an X-Forwarded-For header unless the source IP is
826 * in the 'except' network range.
827 */
828 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200829 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200830 != sess->fe->except_net.s_addr) &&
831 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200832 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200833 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200834 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200835
836 /* Note: we rely on the backend to get the header name to be used for
837 * x-forwarded-for, because the header is really meant for the backends.
838 * However, if the backend did not specify any option, we have to rely
839 * on the frontend's header name.
840 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200841 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
842 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100843 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200844 }
845 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200846 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200847 /* FIXME: for the sake of completeness, we should also support
848 * 'except' here, although it is mostly useless in this case.
849 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200850 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200851
Christopher Faulete0768eb2018-10-03 16:38:02 +0200852 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200853 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200854 pn, sizeof(pn));
855
856 /* Note: we rely on the backend to get the header name to be used for
857 * x-forwarded-for, because the header is really meant for the backends.
858 * However, if the backend did not specify any option, we have to rely
859 * on the frontend's header name.
860 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200861 chunk_printf(&trash, "%s", pn);
862 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100863 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200864 }
865 }
866
867 /*
868 * 10: add X-Original-To if either the frontend or the backend
869 * asks for it.
870 */
871 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
872
873 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200874 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 +0200875 /* Add an X-Original-To header unless the destination IP is
876 * in the 'except' network range.
877 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200878 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200879 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200880 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200881 != sess->fe->except_to.s_addr) &&
882 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200883 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200884 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200885 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200886 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200887
888 /* Note: we rely on the backend to get the header name to be used for
889 * x-original-to, because the header is really meant for the backends.
890 * However, if the backend did not specify any option, we have to rely
891 * on the frontend's header name.
892 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200893 if (s->be->orgto_hdr_len)
894 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
895 else
896 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200897
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200898 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
899 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100900 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200901 }
902 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200903 }
904
Christopher Faulete0768eb2018-10-03 16:38:02 +0200905 /* If we have no server assigned yet and we're balancing on url_param
906 * with a POST request, we may be interested in checking the body for
907 * that parameter. This will be done in another analyser.
908 */
909 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100910 s->txn->meth == HTTP_METH_POST &&
911 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200912 channel_dont_connect(req);
913 req->analysers |= AN_REQ_HTTP_BODY;
914 }
915
916 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
917 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100918
Christopher Faulete0768eb2018-10-03 16:38:02 +0200919 /* We expect some data from the client. Unless we know for sure
920 * we already have a full request, we have to re-enable quick-ack
921 * in case we previously disabled it, otherwise we might cause
922 * the client to delay further data.
923 */
924 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200925 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100926 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200927
928 /*************************************************************
929 * OK, that's finished for the headers. We have done what we *
930 * could. Let's switch to the DATA state. *
931 ************************************************************/
932 req->analyse_exp = TICK_ETERNITY;
933 req->analysers &= ~an_bit;
934
935 s->logs.tv_request = now;
936 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100937 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200938 return 1;
939
Christopher Fauletb8a53712019-12-16 11:29:38 +0100940 return_int_err:
941 txn->status = 500;
942 if (!(s->flags & SF_ERR_MASK))
943 s->flags |= SF_ERR_INTERNAL;
944 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100945 if (s->flags & SF_BE_ASSIGNED)
946 _HA_ATOMIC_ADD(&sess->fe->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100947 if (sess->listener->counters)
948 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
949 goto return_prx_cond;
950
Christopher Faulete0768eb2018-10-03 16:38:02 +0200951 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200952 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100953 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200954 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100955 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100956 /* fall through */
957
958 return_prx_cond:
959 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200960
961 if (!(s->flags & SF_ERR_MASK))
962 s->flags |= SF_ERR_PRXCOND;
963 if (!(s->flags & SF_FINST_MASK))
964 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100965
966 req->analysers &= AN_REQ_FLT_END;
967 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100968 DBG_TRACE_DEVEL("leaving on error",
969 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200970 return 0;
971}
972
973/* This function is an analyser which processes the HTTP tarpit. It always
974 * returns zero, at the beginning because it prevents any other processing
975 * from occurring, and at the end because it terminates the request.
976 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200977int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200978{
979 struct http_txn *txn = s->txn;
980
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100981 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200982 /* This connection is being tarpitted. The CLIENT side has
983 * already set the connect expiration date to the right
984 * timeout. We just have to check that the client is still
985 * there and that the timeout has not expired.
986 */
987 channel_dont_connect(req);
988 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100989 !tick_is_expired(req->analyse_exp, now_ms)) {
990 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
991 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200992 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100993 }
994
Christopher Faulete0768eb2018-10-03 16:38:02 +0200995
996 /* We will set the queue timer to the time spent, just for
997 * logging purposes. We fake a 500 server error, so that the
998 * attacker will not suspect his connection has been tarpitted.
999 * It will not cause trouble to the logs because we can exclude
1000 * the tarpitted connections by filtering on the 'PT' status flags.
1001 */
1002 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1003
1004 if (!(req->flags & CF_READ_ERROR))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001005 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001006
1007 req->analysers &= AN_REQ_FLT_END;
1008 req->analyse_exp = TICK_ETERNITY;
1009
1010 if (!(s->flags & SF_ERR_MASK))
1011 s->flags |= SF_ERR_PRXCOND;
1012 if (!(s->flags & SF_FINST_MASK))
1013 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001014
1015 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001016 return 0;
1017}
1018
1019/* This function is an analyser which waits for the HTTP request body. It waits
1020 * for either the buffer to be full, or the full advertised contents to have
1021 * reached the buffer. It must only be called after the standard HTTP request
1022 * processing has occurred, because it expects the request to be parsed and will
1023 * look for the Expect header. It may send a 100-Continue interim response. It
1024 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
1025 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
1026 * needs to read more data, or 1 once it has completed its analysis.
1027 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001028int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001029{
1030 struct session *sess = s->sess;
1031 struct http_txn *txn = s->txn;
1032 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001033 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001034
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001035 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001036
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001037 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001038
Willy Tarreau4236f032019-03-05 10:43:32 +01001039 if (htx->flags & HTX_FL_PARSING_ERROR)
1040 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001041 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1042 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001043
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001044 if (msg->msg_state < HTTP_MSG_BODY)
1045 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001046
Christopher Faulete0768eb2018-10-03 16:38:02 +02001047 /* We have to parse the HTTP request body to find any required data.
1048 * "balance url_param check_post" should have been the only way to get
1049 * into this. We were brought here after HTTP header analysis, so all
1050 * related structures are ready.
1051 */
1052
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001053 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001054 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001055 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001056 }
1057
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001058 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001059
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001060 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1061 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001062 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001063 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001064 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001065 goto http_end;
1066
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001067 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001068 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1069 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001070 if (!(s->flags & SF_ERR_MASK))
1071 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001072 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1073 if (sess->listener->counters)
1074 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1075 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001076 }
1077
1078 /* we get here if we need to wait for more data */
1079 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1080 /* Not enough data. We'll re-use the http-request
1081 * timeout here. Ideally, we should set the timeout
1082 * relative to the accept() date. We just set the
1083 * request timeout once at the beginning of the
1084 * request.
1085 */
1086 channel_dont_connect(req);
1087 if (!tick_isset(req->analyse_exp))
1088 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001089 DBG_TRACE_DEVEL("waiting for more data",
1090 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001091 return 0;
1092 }
1093
1094 http_end:
1095 /* The situation will not evolve, so let's give up on the analysis. */
1096 s->logs.tv_request = now; /* update the request timer to reflect full request */
1097 req->analysers &= ~an_bit;
1098 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001099 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001100 return 1;
1101
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001102 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001103 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001104 if (!(s->flags & SF_ERR_MASK))
1105 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001106 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001107 if (s->flags & SF_BE_ASSIGNED)
1108 _HA_ATOMIC_ADD(&sess->fe->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001109 if (sess->listener->counters)
1110 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
1111 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001112
Christopher Faulete0768eb2018-10-03 16:38:02 +02001113 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001114 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001115 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1116 if (sess->listener->counters)
1117 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1118 /* fall through */
1119
1120 return_prx_cond:
1121 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001122
1123 if (!(s->flags & SF_ERR_MASK))
1124 s->flags |= SF_ERR_PRXCOND;
1125 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001126 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001127
Christopher Faulete0768eb2018-10-03 16:38:02 +02001128 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001129 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001130 DBG_TRACE_DEVEL("leaving on error",
1131 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001132 return 0;
1133}
1134
1135/* This function is an analyser which forwards request body (including chunk
1136 * sizes if any). It is called as soon as we must forward, even if we forward
1137 * zero byte. The only situation where it must not be called is when we're in
1138 * tunnel mode and we want to forward till the close. It's used both to forward
1139 * remaining data and to resync after end of body. It expects the msg_state to
1140 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1141 * read more data, or 1 once we can go on with next request or end the stream.
1142 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1143 * bytes of pending data + the headers if not already done.
1144 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001145int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001146{
1147 struct session *sess = s->sess;
1148 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001149 struct http_msg *msg = &txn->req;
1150 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001151 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001152 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001153
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001154 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001155
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001156 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001157
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001158 if (htx->flags & HTX_FL_PARSING_ERROR)
1159 goto return_bad_req;
1160 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1161 goto return_int_err;
1162
Christopher Faulete0768eb2018-10-03 16:38:02 +02001163 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1164 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1165 /* Output closed while we were sending data. We must abort and
1166 * wake the other side up.
1167 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001168
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001169 /* Don't abort yet if we had L7 retries activated and it
1170 * was a write error, we may recover.
1171 */
1172 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001173 (s->si[1].flags & SI_FL_L7_RETRY)) {
1174 DBG_TRACE_DEVEL("leaving on L7 retry",
1175 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001176 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001177 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001178 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001179 http_end_request(s);
1180 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001181 DBG_TRACE_DEVEL("leaving on error",
1182 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001183 return 1;
1184 }
1185
1186 /* Note that we don't have to send 100-continue back because we don't
1187 * need the data to complete our job, and it's up to the server to
1188 * decide whether to return 100, 417 or anything else in return of
1189 * an "Expect: 100-continue" header.
1190 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001191 if (msg->msg_state == HTTP_MSG_BODY)
1192 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001193
Christopher Faulete0768eb2018-10-03 16:38:02 +02001194 /* in most states, we should abort in case of early close */
1195 channel_auto_close(req);
1196
1197 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001198 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001199 if (req->flags & CF_EOI)
1200 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001201 }
1202 else {
1203 /* We can't process the buffer's contents yet */
1204 req->flags |= CF_WAKE_WRITE;
1205 goto missing_data_or_waiting;
1206 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001207 }
1208
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001209 if (msg->msg_state >= HTTP_MSG_ENDING)
1210 goto ending;
1211
1212 if (txn->meth == HTTP_METH_CONNECT) {
1213 msg->msg_state = HTTP_MSG_ENDING;
1214 goto ending;
1215 }
1216
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001217 /* Forward input data. We get it by removing all outgoing data not
1218 * forwarded yet from HTX data size. If there are some data filters, we
1219 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001220 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001221 if (HAS_REQ_DATA_FILTERS(s)) {
1222 ret = flt_http_payload(s, msg, htx->data);
1223 if (ret < 0)
1224 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001225 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001226 }
1227 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001228 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001229 if (msg->flags & HTTP_MSGF_XFER_LEN)
1230 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001231 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001232
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001233 if (htx->data != co_data(req))
1234 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001235
Christopher Faulet9768c262018-10-22 09:34:31 +02001236 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001237 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1238 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001239 */
1240 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1241 goto missing_data_or_waiting;
1242
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001243 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001244
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001245 ending:
1246 /* other states, ENDING...TUNNEL */
1247 if (msg->msg_state >= HTTP_MSG_DONE)
1248 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001249
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001250 if (HAS_REQ_DATA_FILTERS(s)) {
1251 ret = flt_http_end(s, msg);
1252 if (ret <= 0) {
1253 if (!ret)
1254 goto missing_data_or_waiting;
1255 goto return_bad_req;
1256 }
1257 }
1258
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001259 if (txn->meth == HTTP_METH_CONNECT)
1260 msg->msg_state = HTTP_MSG_TUNNEL;
1261 else {
1262 msg->msg_state = HTTP_MSG_DONE;
1263 req->to_forward = 0;
1264 }
1265
1266 done:
1267 /* we don't want to forward closes on DONE except in tunnel mode. */
1268 if (!(txn->flags & TX_CON_WANT_TUN))
1269 channel_dont_close(req);
1270
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001271 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001272 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001273 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001274 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1275 if (req->flags & CF_SHUTW) {
1276 /* request errors are most likely due to the
1277 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001278 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001279 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001280 goto return_bad_req;
1281 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001282 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001283 return 1;
1284 }
1285
1286 /* If "option abortonclose" is set on the backend, we want to monitor
1287 * the client's connection and forward any shutdown notification to the
1288 * server, which will decide whether to close or to go on processing the
1289 * request. We only do that in tunnel mode, and not in other modes since
1290 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001291 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001292 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001293 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001294 s->si[1].flags |= SI_FL_NOLINGER;
1295 channel_auto_close(req);
1296 }
1297 else if (s->txn->meth == HTTP_METH_POST) {
1298 /* POST requests may require to read extra CRLF sent by broken
1299 * browsers and which could cause an RST to be sent upon close
1300 * on some systems (eg: Linux). */
1301 channel_auto_read(req);
1302 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001303 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1304 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001305 return 0;
1306
1307 missing_data_or_waiting:
1308 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001309 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001310 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001311
1312 waiting:
1313 /* waiting for the last bits to leave the buffer */
1314 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001315 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001316
1317 /* When TE: chunked is used, we need to get there again to parse remaining
1318 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1319 * And when content-length is used, we never want to let the possible
1320 * shutdown be forwarded to the other side, as the state machine will
1321 * take care of it once the client responds. It's also important to
1322 * prevent TIME_WAITs from accumulating on the backend side, and for
1323 * HTTP/2 where the last frame comes with a shutdown.
1324 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001325 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001326 channel_dont_close(req);
1327
1328 /* We know that more data are expected, but we couldn't send more that
1329 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1330 * system knows it must not set a PUSH on this first part. Interactive
1331 * modes are already handled by the stream sock layer. We must not do
1332 * this in content-length mode because it could present the MSG_MORE
1333 * flag with the last block of forwarded data, which would cause an
1334 * additional delay to be observed by the receiver.
1335 */
1336 if (msg->flags & HTTP_MSGF_TE_CHNK)
1337 req->flags |= CF_EXPECT_MORE;
1338
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001339 DBG_TRACE_DEVEL("waiting for more data to forward",
1340 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001341 return 0;
1342
Christopher Faulet93e02d82019-03-08 14:18:50 +01001343 return_cli_abort:
1344 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1345 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001346 if (sess->listener->counters)
1347 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001348 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001349 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001350 if (!(s->flags & SF_ERR_MASK))
1351 s->flags |= SF_ERR_CLICL;
1352 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001353 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001354
1355 return_srv_abort:
1356 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1357 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001358 if (sess->listener->counters)
1359 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001360 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001361 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001362 if (!(s->flags & SF_ERR_MASK))
1363 s->flags |= SF_ERR_SRVCL;
1364 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001365 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001366
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001367 return_int_err:
1368 if (!(s->flags & SF_ERR_MASK))
1369 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001370 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001371 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001372 if (sess->listener->counters)
1373 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001374 if (objt_server(s->target))
1375 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001376 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001377 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001378
Christopher Faulet93e02d82019-03-08 14:18:50 +01001379 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001380 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001381 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001382 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001383 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001384 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001385
Christopher Fauletb8a53712019-12-16 11:29:38 +01001386 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001387 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001388 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001389 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001390 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001391 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001392 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001393 }
1394 req->analysers &= AN_REQ_FLT_END;
1395 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 +01001396 if (!(s->flags & SF_ERR_MASK))
1397 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001398 if (!(s->flags & SF_FINST_MASK))
1399 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001400 DBG_TRACE_DEVEL("leaving on error ",
1401 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001402 return 0;
1403}
1404
Olivier Houcharda254a372019-04-05 15:30:12 +02001405/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1406/* Returns 0 if we can attempt to retry, -1 otherwise */
1407static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1408{
1409 struct channel *req, *res;
1410 int co_data;
1411
1412 si->conn_retries--;
1413 if (si->conn_retries < 0)
1414 return -1;
1415
Willy Tarreau223995e2019-05-04 10:38:31 +02001416 if (objt_server(s->target))
1417 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1418 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1419
Olivier Houcharda254a372019-04-05 15:30:12 +02001420 req = &s->req;
1421 res = &s->res;
1422 /* Remove any write error from the request, and read error from the response */
1423 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1424 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1425 res->analysers = 0;
1426 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001427 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001428 si->exp = TICK_ETERNITY;
1429 res->rex = TICK_ETERNITY;
1430 res->to_forward = 0;
1431 res->analyse_exp = TICK_ETERNITY;
1432 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001433 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001434 si_release_endpoint(&s->si[1]);
1435 b_free(&req->buf);
1436 /* Swap the L7 buffer with the channel buffer */
1437 /* We know we stored the co_data as b_data, so get it there */
1438 co_data = b_data(&si->l7_buffer);
1439 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1440 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1441
1442 co_set_data(req, co_data);
1443 b_reset(&res->buf);
1444 co_set_data(res, 0);
1445 return 0;
1446}
1447
Christopher Faulete0768eb2018-10-03 16:38:02 +02001448/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1449 * processing can continue on next analysers, or zero if it either needs more
1450 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1451 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1452 * when it has nothing left to do, and may remove any analyser when it wants to
1453 * abort.
1454 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001455int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001456{
Christopher Faulet9768c262018-10-22 09:34:31 +02001457 /*
1458 * We will analyze a complete HTTP response to check the its syntax.
1459 *
1460 * Once the start line and all headers are received, we may perform a
1461 * capture of the error (if any), and we will set a few fields. We also
1462 * logging and finally headers capture.
1463 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001464 struct session *sess = s->sess;
1465 struct http_txn *txn = s->txn;
1466 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001467 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001468 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001469 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001470 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001471 int n;
1472
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001473 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001474
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001475 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001476
Willy Tarreau4236f032019-03-05 10:43:32 +01001477 /* Parsing errors are caught here */
1478 if (htx->flags & HTX_FL_PARSING_ERROR)
1479 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001480 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1481 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001482
Christopher Faulete0768eb2018-10-03 16:38:02 +02001483 /*
1484 * Now we quickly check if we have found a full valid response.
1485 * If not so, we check the FD and buffer states before leaving.
1486 * A full response is indicated by the fact that we have seen
1487 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1488 * responses are checked first.
1489 *
1490 * Depending on whether the client is still there or not, we
1491 * may send an error response back or not. Note that normally
1492 * we should only check for HTTP status there, and check I/O
1493 * errors somewhere else.
1494 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001495 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001496 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001497 /* 1: have we encountered a read error ? */
1498 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001499 struct connection *conn = NULL;
1500
Olivier Houchard865d8392019-05-03 22:46:27 +02001501 if (objt_cs(s->si[1].end))
1502 conn = objt_cs(s->si[1].end)->conn;
1503
1504 if (si_b->flags & SI_FL_L7_RETRY &&
1505 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001506 /* If we arrive here, then CF_READ_ERROR was
1507 * set by si_cs_recv() because we matched a
1508 * status, overwise it would have removed
1509 * the SI_FL_L7_RETRY flag, so it's ok not
1510 * to check s->be->retry_type.
1511 */
1512 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1513 return 0;
1514 }
1515
Olivier Houchard6db16992019-05-17 15:40:49 +02001516 if (txn->flags & TX_NOT_FIRST)
1517 goto abort_keep_alive;
1518
Olivier Houcharda798bf52019-03-08 18:52:00 +01001519 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001520 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001521 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001522 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001523 }
1524
Christopher Faulete0768eb2018-10-03 16:38:02 +02001525 rep->analysers &= AN_RES_FLT_END;
1526 txn->status = 502;
1527
1528 /* Check to see if the server refused the early data.
1529 * If so, just send a 425
1530 */
Olivier Houchard865d8392019-05-03 22:46:27 +02001531 if (conn->err_code == CO_ER_SSL_EARLY_FAILED) {
1532 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001533 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001534 do_l7_retry(s, si_b) == 0) {
1535 DBG_TRACE_DEVEL("leaving on L7 retry",
1536 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001537 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001538 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001539 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001540 }
1541
1542 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001543 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001544
1545 if (!(s->flags & SF_ERR_MASK))
1546 s->flags |= SF_ERR_SRVCL;
1547 if (!(s->flags & SF_FINST_MASK))
1548 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001549 DBG_TRACE_DEVEL("leaving on error",
1550 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001551 return 0;
1552 }
1553
Christopher Faulet9768c262018-10-22 09:34:31 +02001554 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001555 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001556 if ((si_b->flags & SI_FL_L7_RETRY) &&
1557 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001558 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1559 DBG_TRACE_DEVEL("leaving on L7 retry",
1560 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001561 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001562 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001563 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001564 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001565 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001566 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001567 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001568 }
1569
Christopher Faulete0768eb2018-10-03 16:38:02 +02001570 rep->analysers &= AN_RES_FLT_END;
1571 txn->status = 504;
1572 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001573 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001574
1575 if (!(s->flags & SF_ERR_MASK))
1576 s->flags |= SF_ERR_SRVTO;
1577 if (!(s->flags & SF_FINST_MASK))
1578 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001579 DBG_TRACE_DEVEL("leaving on error",
1580 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001581 return 0;
1582 }
1583
Christopher Faulet9768c262018-10-22 09:34:31 +02001584 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001585 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001586 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1587 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001588 if (sess->listener->counters)
1589 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001590 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001591 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001592
1593 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001594 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001595 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001596
1597 if (!(s->flags & SF_ERR_MASK))
1598 s->flags |= SF_ERR_CLICL;
1599 if (!(s->flags & SF_FINST_MASK))
1600 s->flags |= SF_FINST_H;
1601
1602 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001603 DBG_TRACE_DEVEL("leaving on error",
1604 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001605 return 0;
1606 }
1607
Christopher Faulet9768c262018-10-22 09:34:31 +02001608 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001609 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001610 if ((si_b->flags & SI_FL_L7_RETRY) &&
1611 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001612 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1613 DBG_TRACE_DEVEL("leaving on L7 retry",
1614 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001615 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001616 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001617 }
1618
Olivier Houchard6db16992019-05-17 15:40:49 +02001619 if (txn->flags & TX_NOT_FIRST)
1620 goto abort_keep_alive;
1621
Olivier Houcharda798bf52019-03-08 18:52:00 +01001622 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001623 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001624 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001625 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001626 }
1627
Christopher Faulete0768eb2018-10-03 16:38:02 +02001628 rep->analysers &= AN_RES_FLT_END;
1629 txn->status = 502;
1630 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001631 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001632
1633 if (!(s->flags & SF_ERR_MASK))
1634 s->flags |= SF_ERR_SRVCL;
1635 if (!(s->flags & SF_FINST_MASK))
1636 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001637 DBG_TRACE_DEVEL("leaving on error",
1638 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001639 return 0;
1640 }
1641
Christopher Faulet9768c262018-10-22 09:34:31 +02001642 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001643 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001644 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001645 goto abort_keep_alive;
1646
Olivier Houcharda798bf52019-03-08 18:52:00 +01001647 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001648 if (objt_server(s->target))
1649 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001650 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001651
1652 if (!(s->flags & SF_ERR_MASK))
1653 s->flags |= SF_ERR_CLICL;
1654 if (!(s->flags & SF_FINST_MASK))
1655 s->flags |= SF_FINST_H;
1656
1657 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001658 DBG_TRACE_DEVEL("leaving on error",
1659 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001660 return 0;
1661 }
1662
1663 channel_dont_close(rep);
1664 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001665 DBG_TRACE_DEVEL("waiting for more data",
1666 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001667 return 0;
1668 }
1669
1670 /* More interesting part now : we know that we have a complete
1671 * response which at least looks like HTTP. We have an indicator
1672 * of each header's length, so we can parse them quickly.
1673 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001674 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001675 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001676 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001677
Christopher Faulet9768c262018-10-22 09:34:31 +02001678 /* 0: we might have to print this header in debug mode */
1679 if (unlikely((global.mode & MODE_DEBUG) &&
1680 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1681 int32_t pos;
1682
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001683 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001684
Christopher Fauleta3f15502019-05-13 15:27:23 +02001685 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001686 struct htx_blk *blk = htx_get_blk(htx, pos);
1687 enum htx_blk_type type = htx_get_blk_type(blk);
1688
1689 if (type == HTX_BLK_EOH)
1690 break;
1691 if (type != HTX_BLK_HDR)
1692 continue;
1693
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001694 http_debug_hdr("srvhdr", s,
1695 htx_get_blk_name(htx, blk),
1696 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001697 }
1698 }
1699
Christopher Faulet03599112018-11-27 11:21:21 +01001700 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001701 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001702 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001703 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001704 if (sl->flags & HTX_SL_F_XFER_LEN) {
1705 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001706 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001707 if (sl->flags & HTX_SL_F_BODYLESS)
1708 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001709 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001710
1711 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001712 if (n < 1 || n > 5)
1713 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001714
Christopher Faulete0768eb2018-10-03 16:38:02 +02001715 /* when the client triggers a 4xx from the server, it's most often due
1716 * to a missing object or permission. These events should be tracked
1717 * because if they happen often, it may indicate a brute force or a
1718 * vulnerability scan.
1719 */
1720 if (n == 4)
1721 stream_inc_http_err_ctr(s);
1722
1723 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001724 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001725
Christopher Faulete0768eb2018-10-03 16:38:02 +02001726 /* Adjust server's health based on status code. Note: status codes 501
1727 * and 505 are triggered on demand by client request, so we must not
1728 * count them as server failures.
1729 */
1730 if (objt_server(s->target)) {
1731 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001732 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001733 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001734 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001735 }
1736
1737 /*
1738 * We may be facing a 100-continue response, or any other informational
1739 * 1xx response which is non-final, in which case this is not the right
1740 * response, and we're waiting for the next one. Let's allow this response
1741 * to go to the client and wait for the next one. There's an exception for
1742 * 101 which is used later in the code to switch protocols.
1743 */
1744 if (txn->status < 200 &&
1745 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001746 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001747 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001748 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001749 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001750 txn->status = 0;
1751 s->logs.t_data = -1; /* was not a response yet */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001752 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001753 }
1754
1755 /*
1756 * 2: check for cacheability.
1757 */
1758
1759 switch (txn->status) {
1760 case 200:
1761 case 203:
1762 case 204:
1763 case 206:
1764 case 300:
1765 case 301:
1766 case 404:
1767 case 405:
1768 case 410:
1769 case 414:
1770 case 501:
1771 break;
1772 default:
1773 /* RFC7231#6.1:
1774 * Responses with status codes that are defined as
1775 * cacheable by default (e.g., 200, 203, 204, 206,
1776 * 300, 301, 404, 405, 410, 414, and 501 in this
1777 * specification) can be reused by a cache with
1778 * heuristic expiration unless otherwise indicated
1779 * by the method definition or explicit cache
1780 * controls [RFC7234]; all other status codes are
1781 * not cacheable by default.
1782 */
1783 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1784 break;
1785 }
1786
1787 /*
1788 * 3: we may need to capture headers
1789 */
1790 s->logs.logwait &= ~LW_RESP;
1791 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001792 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001793
Christopher Faulet9768c262018-10-22 09:34:31 +02001794 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001795 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1796 txn->status == 101)) {
1797 /* Either we've established an explicit tunnel, or we're
1798 * switching the protocol. In both cases, we're very unlikely
1799 * to understand the next protocols. We have to switch to tunnel
1800 * mode, so that we transfer the request and responses then let
1801 * this protocol pass unmodified. When we later implement specific
1802 * parsers for such protocols, we'll want to check the Upgrade
1803 * header which contains information about that protocol for
1804 * responses with status 101 (eg: see RFC2817 about TLS).
1805 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001806 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001807 }
1808
Christopher Faulet61608322018-11-23 16:23:45 +01001809 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1810 * 407 (Proxy-Authenticate) responses and set the connection to private
1811 */
1812 srv_conn = cs_conn(objt_cs(s->si[1].end));
1813 if (srv_conn) {
1814 struct ist hdr;
1815 struct http_hdr_ctx ctx;
1816
1817 if (txn->status == 401)
1818 hdr = ist("WWW-Authenticate");
1819 else if (txn->status == 407)
1820 hdr = ist("Proxy-Authenticate");
1821 else
1822 goto end;
1823
1824 ctx.blk = NULL;
1825 while (http_find_header(htx, hdr, &ctx, 0)) {
1826 if ((ctx.value.len >= 9 && word_match(ctx.value.ptr, ctx.value.len, "Negotiate", 9)) ||
Olivier Houchard250031e2019-05-29 15:01:50 +02001827 (ctx.value.len >= 4 && word_match(ctx.value.ptr, ctx.value.len, "NTLM", 4))) {
1828 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet61608322018-11-23 16:23:45 +01001829 srv_conn->flags |= CO_FL_PRIVATE;
Olivier Houchard250031e2019-05-29 15:01:50 +02001830 }
Christopher Faulet61608322018-11-23 16:23:45 +01001831 }
1832 }
1833
1834 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001835 /* we want to have the response time before we start processing it */
1836 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1837
1838 /* end of job, return OK */
1839 rep->analysers &= ~an_bit;
1840 rep->analyse_exp = TICK_ETERNITY;
1841 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001842 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001843 return 1;
1844
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001845 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001846 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001847 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001848 if (sess->listener->counters)
1849 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001850 if (objt_server(s->target))
1851 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001852 txn->status = 500;
1853 if (!(s->flags & SF_ERR_MASK))
1854 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001855 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001856
1857 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001858 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001859 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001860 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001861 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001862 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001863 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001864 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001865 do_l7_retry(s, si_b) == 0) {
1866 DBG_TRACE_DEVEL("leaving on L7 retry",
1867 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001868 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001869 }
Christopher Faulet47365272018-10-31 17:40:50 +01001870 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001871 /* fall through */
1872
Christopher Fauletb8a53712019-12-16 11:29:38 +01001873 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001874 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001875
1876 if (!(s->flags & SF_ERR_MASK))
1877 s->flags |= SF_ERR_PRXCOND;
1878 if (!(s->flags & SF_FINST_MASK))
1879 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001880
1881 s->si[1].flags |= SI_FL_NOLINGER;
1882 rep->analysers &= AN_RES_FLT_END;
1883 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001884 DBG_TRACE_DEVEL("leaving on error",
1885 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001886 return 0;
1887
Christopher Faulete0768eb2018-10-03 16:38:02 +02001888 abort_keep_alive:
1889 /* A keep-alive request to the server failed on a network error.
1890 * The client is required to retry. We need to close without returning
1891 * any other information so that the client retries.
1892 */
1893 txn->status = 0;
1894 rep->analysers &= AN_RES_FLT_END;
1895 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001896 s->logs.logwait = 0;
1897 s->logs.level = 0;
1898 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001899 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001900 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1901 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001902 return 0;
1903}
1904
1905/* This function performs all the processing enabled for the current response.
1906 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1907 * and updates s->res.analysers. It might make sense to explode it into several
1908 * other functions. It works like process_request (see indications above).
1909 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001910int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001911{
1912 struct session *sess = s->sess;
1913 struct http_txn *txn = s->txn;
1914 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001915 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001916 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001917 enum rule_result ret = HTTP_RULE_RES_CONT;
1918
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001919 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1920 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001921
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001922 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001923
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001924 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001925
1926 /* The stats applet needs to adjust the Connection header but we don't
1927 * apply any filter there.
1928 */
1929 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1930 rep->analysers &= ~an_bit;
1931 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001932 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001933 }
1934
1935 /*
1936 * We will have to evaluate the filters.
1937 * As opposed to version 1.2, now they will be evaluated in the
1938 * filters order and not in the header order. This means that
1939 * each filter has to be validated among all headers.
1940 *
1941 * Filters are tried with ->be first, then with ->fe if it is
1942 * different from ->be.
1943 *
1944 * Maybe we are in resume condiion. In this case I choose the
1945 * "struct proxy" which contains the rule list matching the resume
1946 * pointer. If none of theses "struct proxy" match, I initialise
1947 * the process with the first one.
1948 *
1949 * In fact, I check only correspondance betwwen the current list
1950 * pointer and the ->fe rule list. If it doesn't match, I initialize
1951 * the loop with the ->be.
1952 */
1953 if (s->current_rule_list == &sess->fe->http_res_rules)
1954 cur_proxy = sess->fe;
1955 else
1956 cur_proxy = s->be;
1957 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001958 /* evaluate http-response rules */
1959 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001960 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001961
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001962 switch (ret) {
1963 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1964 goto return_prx_yield;
1965
1966 case HTTP_RULE_RES_CONT:
1967 case HTTP_RULE_RES_STOP: /* nothing to do */
1968 break;
1969
1970 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1971 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001972
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001973 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1974 goto return_prx_cond;
1975
1976 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001977 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001978
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001979 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1980 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001981
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001982 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1983 goto return_int_err;
1984 }
1985
1986 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001987
Christopher Faulete0768eb2018-10-03 16:38:02 +02001988 /* check whether we're already working on the frontend */
1989 if (cur_proxy == sess->fe)
1990 break;
1991 cur_proxy = sess->fe;
1992 }
1993
1994 /* After this point, this anayzer can't return yield, so we can
1995 * remove the bit corresponding to this analyzer from the list.
1996 *
1997 * Note that the intermediate returns and goto found previously
1998 * reset the analyzers.
1999 */
2000 rep->analysers &= ~an_bit;
2001 rep->analyse_exp = TICK_ETERNITY;
2002
2003 /* OK that's all we can do for 1xx responses */
2004 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002005 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002006
2007 /*
2008 * Now check for a server cookie.
2009 */
2010 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002011 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002012
2013 /*
2014 * Check for cache-control or pragma headers if required.
2015 */
2016 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002017 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002018
2019 /*
2020 * Add server cookie in the response if needed
2021 */
2022 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
2023 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
2024 (!(s->flags & SF_DIRECT) ||
2025 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
2026 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
2027 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
2028 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
2029 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
2030 !(s->flags & SF_IGNORE_PRST)) {
2031 /* the server is known, it's not the one the client requested, or the
2032 * cookie's last seen date needs to be refreshed. We have to
2033 * insert a set-cookie here, except if we want to insert only on POST
2034 * requests and this one isn't. Note that servers which don't have cookies
2035 * (eg: some backup servers) will return a full cookie removal request.
2036 */
2037 if (!objt_server(s->target)->cookie) {
2038 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002039 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02002040 s->be->cookie_name);
2041 }
2042 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002043 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002044
2045 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2046 /* emit last_date, which is mandatory */
2047 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2048 s30tob64((date.tv_sec+3) >> 2,
2049 trash.area + trash.data);
2050 trash.data += 5;
2051
2052 if (s->be->cookie_maxlife) {
2053 /* emit first_date, which is either the original one or
2054 * the current date.
2055 */
2056 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2057 s30tob64(txn->cookie_first_date ?
2058 txn->cookie_first_date >> 2 :
2059 (date.tv_sec+3) >> 2,
2060 trash.area + trash.data);
2061 trash.data += 5;
2062 }
2063 }
2064 chunk_appendf(&trash, "; path=/");
2065 }
2066
2067 if (s->be->cookie_domain)
2068 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2069
2070 if (s->be->ck_opts & PR_CK_HTTPONLY)
2071 chunk_appendf(&trash, "; HttpOnly");
2072
2073 if (s->be->ck_opts & PR_CK_SECURE)
2074 chunk_appendf(&trash, "; Secure");
2075
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002076 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002077 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002078
2079 txn->flags &= ~TX_SCK_MASK;
2080 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2081 /* the server did not change, only the date was updated */
2082 txn->flags |= TX_SCK_UPDATED;
2083 else
2084 txn->flags |= TX_SCK_INSERTED;
2085
2086 /* Here, we will tell an eventual cache on the client side that we don't
2087 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2088 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2089 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2090 */
2091 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2092
2093 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2094
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002095 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002096 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002097 }
2098 }
2099
2100 /*
2101 * Check if result will be cacheable with a cookie.
2102 * We'll block the response if security checks have caught
2103 * nasty things such as a cacheable cookie.
2104 */
2105 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2106 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2107 (s->be->options & PR_O_CHK_CACHE)) {
2108 /* we're in presence of a cacheable response containing
2109 * a set-cookie header. We'll block it as requested by
2110 * the 'checkcache' option, and send an alert.
2111 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002112 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2113 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2114 send_log(s->be, LOG_ALERT,
2115 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2116 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002117 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002118 }
2119
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002120 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002121 /* Always enter in the body analyzer */
2122 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2123 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2124
2125 /* if the user wants to log as soon as possible, without counting
2126 * bytes from the server, then this is the right moment. We have
2127 * to temporarily assign bytes_out to log what we currently have.
2128 */
2129 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2130 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002131 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002132 s->do_log(s);
2133 s->logs.bytes_out = 0;
2134 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002135 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002136 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002137
Christopher Fauletb8a53712019-12-16 11:29:38 +01002138 done:
2139 rep->analysers &= ~an_bit;
2140 rep->analyse_exp = TICK_ETERNITY;
2141 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002142
Christopher Fauletb8a53712019-12-16 11:29:38 +01002143 deny:
2144 txn->flags |= TX_CLDENY;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002145 txn->status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002146 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002147 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002148 if (sess->listener->counters)
2149 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002150 if (objt_server(s->target))
2151 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002152 goto return_prx_err;
2153
2154 return_int_err:
2155 txn->status = 500;
2156 if (!(s->flags & SF_ERR_MASK))
2157 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002158 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002159 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2160 if (objt_server(s->target))
2161 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002162 if (objt_server(s->target))
2163 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002164 goto return_prx_err;
2165
2166 return_bad_res:
2167 txn->status = 502;
2168 /* fall through */
2169
2170 return_prx_err:
2171 http_reply_and_close(s, txn->status, http_error_message(s));
2172 /* fall through */
2173
2174 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002175 s->logs.t_data = -1; /* was not a valid response */
2176 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002177
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002178 if (!(s->flags & SF_ERR_MASK))
2179 s->flags |= SF_ERR_PRXCOND;
2180 if (!(s->flags & SF_FINST_MASK))
2181 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002182
2183 rep->analysers &= ~an_bit;
2184 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002185 DBG_TRACE_DEVEL("leaving on error",
2186 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002187 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002188
2189 return_prx_yield:
2190 channel_dont_close(rep);
2191 DBG_TRACE_DEVEL("waiting for more data",
2192 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2193 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002194}
2195
2196/* This function is an analyser which forwards response body (including chunk
2197 * sizes if any). It is called as soon as we must forward, even if we forward
2198 * zero byte. The only situation where it must not be called is when we're in
2199 * tunnel mode and we want to forward till the close. It's used both to forward
2200 * remaining data and to resync after end of body. It expects the msg_state to
2201 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2202 * read more data, or 1 once we can go on with next request or end the stream.
2203 *
2204 * It is capable of compressing response data both in content-length mode and
2205 * in chunked mode. The state machines follows different flows depending on
2206 * whether content-length and chunked modes are used, since there are no
2207 * trailers in content-length :
2208 *
2209 * chk-mode cl-mode
2210 * ,----- BODY -----.
2211 * / \
2212 * V size > 0 V chk-mode
2213 * .--> SIZE -------------> DATA -------------> CRLF
2214 * | | size == 0 | last byte |
2215 * | v final crlf v inspected |
2216 * | TRAILERS -----------> DONE |
2217 * | |
2218 * `----------------------------------------------'
2219 *
2220 * Compression only happens in the DATA state, and must be flushed in final
2221 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2222 * is performed at once on final states for all bytes parsed, or when leaving
2223 * on missing data.
2224 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002225int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002226{
2227 struct session *sess = s->sess;
2228 struct http_txn *txn = s->txn;
2229 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002230 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002231 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002232
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002233 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002234
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002235 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002236
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002237 if (htx->flags & HTX_FL_PARSING_ERROR)
2238 goto return_bad_res;
2239 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2240 goto return_int_err;
2241
Christopher Faulete0768eb2018-10-03 16:38:02 +02002242 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002243 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002244 /* Output closed while we were sending data. We must abort and
2245 * wake the other side up.
2246 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002247 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002248 http_end_response(s);
2249 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002250 DBG_TRACE_DEVEL("leaving on error",
2251 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002252 return 1;
2253 }
2254
Christopher Faulet9768c262018-10-22 09:34:31 +02002255 if (msg->msg_state == HTTP_MSG_BODY)
2256 msg->msg_state = HTTP_MSG_DATA;
2257
Christopher Faulete0768eb2018-10-03 16:38:02 +02002258 /* in most states, we should abort in case of early close */
2259 channel_auto_close(res);
2260
Christopher Faulete0768eb2018-10-03 16:38:02 +02002261 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002262 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002263 if (res->flags & CF_EOI)
2264 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002265 }
2266 else {
2267 /* We can't process the buffer's contents yet */
2268 res->flags |= CF_WAKE_WRITE;
2269 goto missing_data_or_waiting;
2270 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002271 }
2272
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002273 if (msg->msg_state >= HTTP_MSG_ENDING)
2274 goto ending;
2275
2276 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2277 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2278 msg->msg_state = HTTP_MSG_ENDING;
2279 goto ending;
2280 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002281
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002282 /* Forward input data. We get it by removing all outgoing data not
2283 * forwarded yet from HTX data size. If there are some data filters, we
2284 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002285 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002286 if (HAS_RSP_DATA_FILTERS(s)) {
2287 ret = flt_http_payload(s, msg, htx->data);
2288 if (ret < 0)
2289 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002290 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002291 }
2292 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002293 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002294 if (msg->flags & HTTP_MSGF_XFER_LEN)
2295 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002296 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002297
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002298 if (htx->data != co_data(res))
2299 goto missing_data_or_waiting;
2300
2301 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2302 msg->msg_state = HTTP_MSG_ENDING;
2303 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002304 }
2305
Christopher Faulet9768c262018-10-22 09:34:31 +02002306 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002307 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2308 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002309 */
2310 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2311 goto missing_data_or_waiting;
2312
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002313 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002314
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002315 ending:
2316 /* other states, ENDING...TUNNEL */
2317 if (msg->msg_state >= HTTP_MSG_DONE)
2318 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002319
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002320 if (HAS_RSP_DATA_FILTERS(s)) {
2321 ret = flt_http_end(s, msg);
2322 if (ret <= 0) {
2323 if (!ret)
2324 goto missing_data_or_waiting;
2325 goto return_bad_res;
2326 }
2327 }
2328
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002329 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2330 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2331 msg->msg_state = HTTP_MSG_TUNNEL;
2332 goto ending;
2333 }
2334 else {
2335 msg->msg_state = HTTP_MSG_DONE;
2336 res->to_forward = 0;
2337 }
2338
2339 done:
2340
2341 channel_dont_close(res);
2342
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002343 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002344 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002345 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002346 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2347 if (res->flags & CF_SHUTW) {
2348 /* response errors are most likely due to the
2349 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002350 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002351 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002352 goto return_bad_res;
2353 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002354 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002355 return 1;
2356 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002357 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2358 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002359 return 0;
2360
2361 missing_data_or_waiting:
2362 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002363 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002364
2365 /* stop waiting for data if the input is closed before the end. If the
2366 * client side was already closed, it means that the client has aborted,
2367 * so we don't want to count this as a server abort. Otherwise it's a
2368 * server abort.
2369 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002370 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002371 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002372 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002373 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002374 if (htx_is_empty(htx))
2375 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002376 }
2377
Christopher Faulete0768eb2018-10-03 16:38:02 +02002378 /* When TE: chunked is used, we need to get there again to parse
2379 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002380 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2381 * are filters registered on the stream, we don't want to forward a
2382 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002383 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002384 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002385 channel_dont_close(res);
2386
2387 /* We know that more data are expected, but we couldn't send more that
2388 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2389 * system knows it must not set a PUSH on this first part. Interactive
2390 * modes are already handled by the stream sock layer. We must not do
2391 * this in content-length mode because it could present the MSG_MORE
2392 * flag with the last block of forwarded data, which would cause an
2393 * additional delay to be observed by the receiver.
2394 */
2395 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2396 res->flags |= CF_EXPECT_MORE;
2397
2398 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002399 DBG_TRACE_DEVEL("waiting for more data to forward",
2400 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002401 return 0;
2402
Christopher Faulet93e02d82019-03-08 14:18:50 +01002403 return_srv_abort:
2404 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2405 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002406 if (sess->listener->counters)
2407 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002408 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002409 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002410 if (!(s->flags & SF_ERR_MASK))
2411 s->flags |= SF_ERR_SRVCL;
2412 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002413
Christopher Faulet93e02d82019-03-08 14:18:50 +01002414 return_cli_abort:
2415 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2416 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002417 if (sess->listener->counters)
2418 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002419 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002420 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002421 if (!(s->flags & SF_ERR_MASK))
2422 s->flags |= SF_ERR_CLICL;
2423 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002424
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002425 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002426 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002427 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002428 if (sess->listener->counters)
2429 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002430 if (objt_server(s->target))
2431 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002432 if (!(s->flags & SF_ERR_MASK))
2433 s->flags |= SF_ERR_INTERNAL;
2434 goto return_error;
2435
Christopher Faulet93e02d82019-03-08 14:18:50 +01002436 return_bad_res:
2437 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2438 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002439 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002440 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2441 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002442 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002443 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002444 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002445
Christopher Faulet93e02d82019-03-08 14:18:50 +01002446 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002447 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002448 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002449 res->analysers &= AN_RES_FLT_END;
2450 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 +02002451 if (!(s->flags & SF_FINST_MASK))
2452 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002453 DBG_TRACE_DEVEL("leaving on error",
2454 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002455 return 0;
2456}
2457
Christopher Fauletf2824e62018-10-01 12:12:37 +02002458/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002459 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002460 * as too large a request to build a valid response.
2461 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002462int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002463{
Christopher Faulet99daf282018-11-28 22:58:13 +01002464 struct channel *req = &s->req;
2465 struct channel *res = &s->res;
2466 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002467 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002468 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002469 struct ist status, reason, location;
2470 unsigned int flags;
2471 size_t data;
Christopher Faulet08e66462019-05-23 16:44:59 +02002472 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002473
2474 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002475 if (!chunk) {
2476 if (!(s->flags & SF_ERR_MASK))
2477 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002478 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002479 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002480
Christopher Faulet99daf282018-11-28 22:58:13 +01002481 /*
2482 * Create the location
2483 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002484 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002485 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002486 case REDIRECT_TYPE_SCHEME: {
2487 struct http_hdr_ctx ctx;
2488 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002489
Christopher Faulet99daf282018-11-28 22:58:13 +01002490 host = ist("");
2491 ctx.blk = NULL;
2492 if (http_find_header(htx, ist("Host"), &ctx, 0))
2493 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002494
Christopher Faulet297fbb42019-05-13 14:41:27 +02002495 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002496 path = http_get_path(htx_sl_req_uri(sl));
2497 /* build message using path */
2498 if (path.ptr) {
2499 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2500 int qs = 0;
2501 while (qs < path.len) {
2502 if (*(path.ptr + qs) == '?') {
2503 path.len = qs;
2504 break;
2505 }
2506 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002507 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002508 }
2509 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002510 else
2511 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002512
Christopher Faulet99daf282018-11-28 22:58:13 +01002513 if (rule->rdr_str) { /* this is an old "redirect" rule */
2514 /* add scheme */
2515 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2516 goto fail;
2517 }
2518 else {
2519 /* add scheme with executing log format */
2520 chunk->data += build_logline(s, chunk->area + chunk->data,
2521 chunk->size - chunk->data,
2522 &rule->rdr_fmt);
2523 }
2524 /* add "://" + host + path */
2525 if (!chunk_memcat(chunk, "://", 3) ||
2526 !chunk_memcat(chunk, host.ptr, host.len) ||
2527 !chunk_memcat(chunk, path.ptr, path.len))
2528 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002529
Christopher Faulet99daf282018-11-28 22:58:13 +01002530 /* append a slash at the end of the location if needed and missing */
2531 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2532 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2533 if (chunk->data + 1 >= chunk->size)
2534 goto fail;
2535 chunk->area[chunk->data++] = '/';
2536 }
2537 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002538 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002539
Christopher Faulet99daf282018-11-28 22:58:13 +01002540 case REDIRECT_TYPE_PREFIX: {
2541 struct ist path;
2542
Christopher Faulet297fbb42019-05-13 14:41:27 +02002543 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002544 path = http_get_path(htx_sl_req_uri(sl));
2545 /* build message using path */
2546 if (path.ptr) {
2547 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2548 int qs = 0;
2549 while (qs < path.len) {
2550 if (*(path.ptr + qs) == '?') {
2551 path.len = qs;
2552 break;
2553 }
2554 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002555 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002556 }
2557 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002558 else
2559 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002560
Christopher Faulet99daf282018-11-28 22:58:13 +01002561 if (rule->rdr_str) { /* this is an old "redirect" rule */
2562 /* add prefix. Note that if prefix == "/", we don't want to
2563 * add anything, otherwise it makes it hard for the user to
2564 * configure a self-redirection.
2565 */
2566 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2567 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2568 goto fail;
2569 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002570 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002571 else {
2572 /* add prefix with executing log format */
2573 chunk->data += build_logline(s, chunk->area + chunk->data,
2574 chunk->size - chunk->data,
2575 &rule->rdr_fmt);
2576 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002577
Christopher Faulet99daf282018-11-28 22:58:13 +01002578 /* add path */
2579 if (!chunk_memcat(chunk, path.ptr, path.len))
2580 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002581
Christopher Faulet99daf282018-11-28 22:58:13 +01002582 /* append a slash at the end of the location if needed and missing */
2583 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2584 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2585 if (chunk->data + 1 >= chunk->size)
2586 goto fail;
2587 chunk->area[chunk->data++] = '/';
2588 }
2589 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002590 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002591 case REDIRECT_TYPE_LOCATION:
2592 default:
2593 if (rule->rdr_str) { /* this is an old "redirect" rule */
2594 /* add location */
2595 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2596 goto fail;
2597 }
2598 else {
2599 /* add location with executing log format */
2600 chunk->data += build_logline(s, chunk->area + chunk->data,
2601 chunk->size - chunk->data,
2602 &rule->rdr_fmt);
2603 }
2604 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002605 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002606 location = ist2(chunk->area, chunk->data);
2607
2608 /*
2609 * Create the 30x response
2610 */
2611 switch (rule->code) {
2612 case 308:
2613 status = ist("308");
2614 reason = ist("Permanent Redirect");
2615 break;
2616 case 307:
2617 status = ist("307");
2618 reason = ist("Temporary Redirect");
2619 break;
2620 case 303:
2621 status = ist("303");
2622 reason = ist("See Other");
2623 break;
2624 case 301:
2625 status = ist("301");
2626 reason = ist("Moved Permanently");
2627 break;
2628 case 302:
2629 default:
2630 status = ist("302");
2631 reason = ist("Found");
2632 break;
2633 }
2634
Christopher Faulet08e66462019-05-23 16:44:59 +02002635 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2636 close = 1;
2637
Christopher Faulet99daf282018-11-28 22:58:13 +01002638 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002639 /* Trim any possible response */
2640 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002641 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2642 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2643 if (!sl)
2644 goto fail;
2645 sl->info.res.status = rule->code;
2646 s->txn->status = rule->code;
2647
Christopher Faulet08e66462019-05-23 16:44:59 +02002648 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2649 goto fail;
2650
2651 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002652 !htx_add_header(htx, ist("Location"), location))
2653 goto fail;
2654
2655 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2656 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2657 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002658 }
2659
2660 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002661 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2662 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002663 }
2664
Christopher Faulet99daf282018-11-28 22:58:13 +01002665 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2666 goto fail;
2667
Kevin Zhu96b36392020-01-07 09:42:55 +01002668 htx_to_buf(htx, &res->buf);
2669
Christopher Fauletf2824e62018-10-01 12:12:37 +02002670 /* let's log the request time */
2671 s->logs.tv_request = now;
2672
Christopher Faulet06511812019-10-16 09:38:27 +02002673 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet99daf282018-11-28 22:58:13 +01002674 data = htx->data - co_data(res);
Christopher Faulet99daf282018-11-28 22:58:13 +01002675 c_adv(res, data);
2676 res->total += data;
2677
2678 channel_auto_read(req);
2679 channel_abort(req);
2680 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002681 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet99daf282018-11-28 22:58:13 +01002682
2683 res->wex = tick_add_ifset(now_ms, res->wto);
2684 channel_auto_read(res);
2685 channel_auto_close(res);
2686 channel_shutr_now(res);
2687
2688 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002689
2690 if (!(s->flags & SF_ERR_MASK))
2691 s->flags |= SF_ERR_LOCAL;
2692 if (!(s->flags & SF_FINST_MASK))
2693 s->flags |= SF_FINST_R;
2694
Christopher Faulet99daf282018-11-28 22:58:13 +01002695 free_trash_chunk(chunk);
2696 return 1;
2697
2698 fail:
2699 /* If an error occurred, remove the incomplete HTTP response from the
2700 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002701 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002702 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002703 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002704}
2705
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002706/* Replace all headers matching the name <name>. The header value is replaced if
2707 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2708 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2709 * values are evaluated one by one. It returns 0 on success and -1 on error.
2710 */
2711int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2712 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002713{
2714 struct http_hdr_ctx ctx;
2715 struct buffer *output = get_trash_chunk();
2716
Christopher Faulet72333522018-10-24 11:25:02 +02002717 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002718 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002719 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2720 continue;
2721
2722 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2723 if (output->data == -1)
2724 return -1;
2725 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2726 return -1;
2727 }
2728 return 0;
2729}
2730
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002731/* Terminate a 103-Erly-hints response and send it to the client. It returns 0
2732 * on success and -1 on error. The response channel is updated accordingly.
2733 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002734static int http_reply_103_early_hints(struct channel *res)
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002735{
2736 struct htx *htx = htx_from_buf(&res->buf);
2737 size_t data;
2738
Christopher Faulet1d5ec092019-06-26 14:23:54 +02002739 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002740 /* If an error occurred during an Early-hint rule,
2741 * remove the incomplete HTTP 103 response from the
2742 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002743 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002744 return -1;
2745 }
2746
2747 data = htx->data - co_data(res);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002748 c_adv(res, data);
2749 res->total += data;
2750 return 0;
2751}
2752
Christopher Faulet6eb92892018-11-15 16:39:29 +01002753/*
2754 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
2755 * built according to <fmt> log line format.
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002756 * If <early_hints> is 0, it is starts a new response by adding the start
2757 * line. If an error occurred -1 is returned. On success 0 is returned. The
2758 * channel is not updated here. It must be done calling the function
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002759 * http_reply_103_early_hints().
Christopher Faulet6eb92892018-11-15 16:39:29 +01002760 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002761static 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 +01002762{
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002763 struct channel *res = &s->res;
2764 struct htx *htx = htx_from_buf(&res->buf);
2765 struct buffer *value = alloc_trash_chunk();
2766
Christopher Fauletb8a53712019-12-16 11:29:38 +01002767 if (!value) {
2768 if (!(s->flags & SF_ERR_MASK))
2769 s->flags |= SF_ERR_RESOURCE;
2770 goto fail;
2771 }
2772
Christopher Faulet6eb92892018-11-15 16:39:29 +01002773 if (!early_hints) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002774 struct htx_sl *sl;
2775 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
2776 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2777
2778 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
2779 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
2780 if (!sl)
Christopher Faulet6eb92892018-11-15 16:39:29 +01002781 goto fail;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002782 sl->info.res.status = 103;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002783 }
2784
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002785 value->data = build_logline(s, b_tail(value), b_room(value), fmt);
2786 if (!htx_add_header(htx, name, ist2(b_head(value), b_data(value))))
Christopher Faulet6eb92892018-11-15 16:39:29 +01002787 goto fail;
2788
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002789 free_trash_chunk(value);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002790 return 1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002791
2792 fail:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002793 /* If an error occurred during an Early-hint rule, remove the incomplete
2794 * HTTP 103 response from the buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002795 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002796 free_trash_chunk(value);
2797 return -1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002798}
2799
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002800/* This function executes one of the set-{method,path,query,uri} actions. It
2801 * takes the string from the variable 'replace' with length 'len', then modifies
2802 * the relevant part of the request line accordingly. Then it updates various
2803 * pointers to the next elements which were moved, and the total buffer length.
2804 * It finds the action to be performed in p[2], previously filled by function
2805 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2806 * error, though this can be revisited when this code is finally exploited.
2807 *
2808 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2809 * query string and 3 to replace uri.
2810 *
2811 * In query string case, the mark question '?' must be set at the start of the
2812 * string by the caller, event if the replacement query string is empty.
2813 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002814int http_req_replace_stline(int action, const char *replace, int len,
2815 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002816{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002817 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002818
2819 switch (action) {
2820 case 0: // method
2821 if (!http_replace_req_meth(htx, ist2(replace, len)))
2822 return -1;
2823 break;
2824
2825 case 1: // path
2826 if (!http_replace_req_path(htx, ist2(replace, len)))
2827 return -1;
2828 break;
2829
2830 case 2: // query
2831 if (!http_replace_req_query(htx, ist2(replace, len)))
2832 return -1;
2833 break;
2834
2835 case 3: // uri
2836 if (!http_replace_req_uri(htx, ist2(replace, len)))
2837 return -1;
2838 break;
2839
2840 default:
2841 return -1;
2842 }
2843 return 0;
2844}
2845
2846/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002847 * variable <status> contains the new status code. This function never fails. It
2848 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002849 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002850int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002851{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002852 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002853 char *res;
2854
2855 chunk_reset(&trash);
2856 res = ultoa_o(status, trash.area, trash.size);
2857 trash.data = res - trash.area;
2858
2859 /* Do we have a custom reason format string? */
Christopher Faulet96bff762019-12-17 13:46:18 +01002860 if (reason.ptr == NULL) {
2861 const char *str = http_get_reason(status);
2862 reason = ist2(str, strlen(str));
2863 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002864
Christopher Faulete00d06c2019-12-16 17:18:42 +01002865 if (!http_replace_res_status(htx, ist2(trash.area, trash.data)))
2866 return -1;
Christopher Faulet96bff762019-12-17 13:46:18 +01002867 if (!http_replace_res_reason(htx, reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002868 return -1;
2869 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002870}
2871
Christopher Faulet3e964192018-10-24 11:39:23 +02002872/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2873 * transaction <txn>. Returns the verdict of the first rule that prevents
2874 * further processing of the request (auth, deny, ...), and defaults to
2875 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2876 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2877 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2878 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2879 * status.
2880 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002881static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
2882 struct stream *s, int *deny_status)
Christopher Faulet3e964192018-10-24 11:39:23 +02002883{
2884 struct session *sess = strm_sess(s);
2885 struct http_txn *txn = s->txn;
2886 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002887 struct act_rule *rule;
2888 struct http_hdr_ctx ctx;
2889 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002890 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002891 int act_opts = 0;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002892 int early_hints = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002893
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002894 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002895
2896 /* If "the current_rule_list" match the executed rule list, we are in
2897 * resume condition. If a resume is needed it is always in the action
2898 * and never in the ACL or converters. In this case, we initialise the
2899 * current rule, and go to the action execution point.
2900 */
2901 if (s->current_rule) {
2902 rule = s->current_rule;
2903 s->current_rule = NULL;
2904 if (s->current_rule_list == rules)
2905 goto resume_execution;
2906 }
2907 s->current_rule_list = rules;
2908
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002909 /* start the ruleset evaluation in strict mode */
2910 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002911
Christopher Faulet3e964192018-10-24 11:39:23 +02002912 list_for_each_entry(rule, rules, list) {
2913 /* check optional condition */
2914 if (rule->cond) {
2915 int ret;
2916
2917 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2918 ret = acl_pass(ret);
2919
2920 if (rule->cond->pol == ACL_COND_UNLESS)
2921 ret = !ret;
2922
2923 if (!ret) /* condition not matched */
2924 continue;
2925 }
2926
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002927 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002928 resume_execution:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002929 if (early_hints && rule->action != ACT_HTTP_EARLY_HINT) {
2930 early_hints = 0;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002931 if (http_reply_103_early_hints(&s->res) == -1) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002932 rule_ret = HTTP_RULE_RES_BADREQ;
2933 goto end;
2934 }
2935 }
2936
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002937 /* Always call the action function if defined */
2938 if (rule->action_ptr) {
2939 if ((s->req.flags & CF_READ_ERROR) ||
2940 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2941 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002942 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002943
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002944 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002945 case ACT_RET_CONT:
2946 break;
2947 case ACT_RET_STOP:
2948 rule_ret = HTTP_RULE_RES_STOP;
2949 goto end;
2950 case ACT_RET_YIELD:
2951 s->current_rule = rule;
2952 rule_ret = HTTP_RULE_RES_YIELD;
2953 goto end;
2954 case ACT_RET_ERR:
2955 rule_ret = HTTP_RULE_RES_ERROR;
2956 goto end;
2957 case ACT_RET_DONE:
2958 rule_ret = HTTP_RULE_RES_DONE;
2959 goto end;
2960 case ACT_RET_DENY:
2961 rule_ret = HTTP_RULE_RES_DENY;
2962 goto end;
2963 case ACT_RET_ABRT:
2964 rule_ret = HTTP_RULE_RES_ABRT;
2965 goto end;
2966 case ACT_RET_INV:
2967 rule_ret = HTTP_RULE_RES_BADREQ;
2968 goto end;
2969 }
2970 continue; /* eval the next rule */
2971 }
2972
2973 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002974 switch (rule->action) {
2975 case ACT_ACTION_ALLOW:
2976 rule_ret = HTTP_RULE_RES_STOP;
2977 goto end;
2978
2979 case ACT_ACTION_DENY:
2980 if (deny_status)
Christopher Faulet96bff762019-12-17 13:46:18 +01002981 *deny_status = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002982 rule_ret = HTTP_RULE_RES_DENY;
2983 goto end;
2984
2985 case ACT_HTTP_REQ_TARPIT:
2986 txn->flags |= TX_CLTARPIT;
2987 if (deny_status)
Christopher Faulet96bff762019-12-17 13:46:18 +01002988 *deny_status = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002989 rule_ret = HTTP_RULE_RES_DENY;
2990 goto end;
2991
2992 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002993 /* Auth might be performed on regular http-req rules as well as on stats */
Christopher Faulet96bff762019-12-17 13:46:18 +01002994 auth_realm = rule->arg.http.str.ptr;
Christopher Faulet3e964192018-10-24 11:39:23 +02002995 if (!auth_realm) {
2996 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2997 auth_realm = STATS_DEFAULT_REALM;
2998 else
2999 auth_realm = px->id;
3000 }
3001 /* send 401/407 depending on whether we use a proxy or not. We still
3002 * count one error, because normal browsing won't significantly
3003 * increase the counter but brute force attempts will.
3004 */
Christopher Faulet3e964192018-10-24 11:39:23 +02003005 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003006 if (http_reply_40x_unauthorized(s, auth_realm) == -1)
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003007 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet12c51e22018-11-28 15:59:42 +01003008 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02003009 goto end;
3010
3011 case ACT_HTTP_REDIR:
Christopher Faulet3e964192018-10-24 11:39:23 +02003012 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003013 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003014 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003015 goto end;
3016
3017 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003018 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003019 break;
3020
3021 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003022 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003023 break;
3024
3025 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003026 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003027 break;
3028
3029 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003030 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003031 break;
3032
Christopher Faulet3e964192018-10-24 11:39:23 +02003033 case ACT_HTTP_DEL_HDR:
3034 /* remove all occurrences of the header */
3035 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01003036 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02003037 http_remove_header(htx, &ctx);
3038 break;
3039
Christopher Faulet3e964192018-10-24 11:39:23 +02003040 case ACT_HTTP_EARLY_HINT:
3041 if (!(txn->req.flags & HTTP_MSGF_VER_11))
3042 break;
Christopher Faulet96bff762019-12-17 13:46:18 +01003043 early_hints = http_add_early_hint_header(s, early_hints, rule->arg.http.str, &rule->arg.http.fmt);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003044 if (early_hints == -1) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003045 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003046 goto end;
3047 }
3048 break;
3049
Christopher Faulet3e964192018-10-24 11:39:23 +02003050 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3051 /* Note: only the first valid tracking parameter of each
3052 * applies.
3053 */
3054
3055 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3056 struct stktable *t;
3057 struct stksess *ts;
3058 struct stktable_key *key;
3059 void *ptr1, *ptr2;
3060
3061 t = rule->arg.trk_ctr.table.t;
3062 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL,
3063 rule->arg.trk_ctr.expr, NULL);
3064
3065 if (key && (ts = stktable_get_entry(t, key))) {
3066 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3067
3068 /* let's count a new HTTP request as it's the first time we do it */
3069 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3070 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3071 if (ptr1 || ptr2) {
3072 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3073
3074 if (ptr1)
3075 stktable_data_cast(ptr1, http_req_cnt)++;
3076
3077 if (ptr2)
3078 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
3079 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3080
3081 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3082
3083 /* If data was modified, we need to touch to re-schedule sync */
3084 stktable_touch_local(t, ts, 0);
3085 }
3086
3087 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3088 if (sess->fe != s->be)
3089 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3090 }
3091 }
3092 break;
3093
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003094 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003095 default:
3096 break;
3097 }
3098 }
3099
3100 end:
3101 if (early_hints) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003102 if (http_reply_103_early_hints(&s->res) == -1)
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003103 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003104 }
3105
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003106 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003107 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003108 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003109
Christopher Faulet3e964192018-10-24 11:39:23 +02003110 /* we reached the end of the rules, nothing to report */
3111 return rule_ret;
3112}
3113
3114/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3115 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3116 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3117 * is returned, the process can continue the evaluation of next rule list. If
3118 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3119 * is returned, it means the operation could not be processed and a server error
3120 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3121 * deny rule. If *YIELD is returned, the caller must call again the function
3122 * with the same context.
3123 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003124static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
3125 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02003126{
3127 struct session *sess = strm_sess(s);
3128 struct http_txn *txn = s->txn;
3129 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02003130 struct act_rule *rule;
3131 struct http_hdr_ctx ctx;
3132 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003133 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02003134
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003135 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02003136
3137 /* If "the current_rule_list" match the executed rule list, we are in
3138 * resume condition. If a resume is needed it is always in the action
3139 * and never in the ACL or converters. In this case, we initialise the
3140 * current rule, and go to the action execution point.
3141 */
3142 if (s->current_rule) {
3143 rule = s->current_rule;
3144 s->current_rule = NULL;
3145 if (s->current_rule_list == rules)
3146 goto resume_execution;
3147 }
3148 s->current_rule_list = rules;
3149
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003150 /* start the ruleset evaluation in strict mode */
3151 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003152
Christopher Faulet3e964192018-10-24 11:39:23 +02003153 list_for_each_entry(rule, rules, list) {
3154 /* check optional condition */
3155 if (rule->cond) {
3156 int ret;
3157
3158 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3159 ret = acl_pass(ret);
3160
3161 if (rule->cond->pol == ACL_COND_UNLESS)
3162 ret = !ret;
3163
3164 if (!ret) /* condition not matched */
3165 continue;
3166 }
3167
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003168 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02003169resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003170
3171 /* Always call the action function if defined */
3172 if (rule->action_ptr) {
3173 if ((s->req.flags & CF_READ_ERROR) ||
3174 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3175 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003176 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003177
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003178 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003179 case ACT_RET_CONT:
3180 break;
3181 case ACT_RET_STOP:
3182 rule_ret = HTTP_RULE_RES_STOP;
3183 goto end;
3184 case ACT_RET_YIELD:
3185 s->current_rule = rule;
3186 rule_ret = HTTP_RULE_RES_YIELD;
3187 goto end;
3188 case ACT_RET_ERR:
3189 rule_ret = HTTP_RULE_RES_ERROR;
3190 goto end;
3191 case ACT_RET_DONE:
3192 rule_ret = HTTP_RULE_RES_DONE;
3193 goto end;
3194 case ACT_RET_DENY:
3195 rule_ret = HTTP_RULE_RES_DENY;
3196 goto end;
3197 case ACT_RET_ABRT:
3198 rule_ret = HTTP_RULE_RES_ABRT;
3199 goto end;
3200 case ACT_RET_INV:
3201 rule_ret = HTTP_RULE_RES_BADREQ;
3202 goto end;
3203 }
3204 continue; /* eval the next rule */
3205 }
3206
3207 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003208 switch (rule->action) {
3209 case ACT_ACTION_ALLOW:
3210 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3211 goto end;
3212
3213 case ACT_ACTION_DENY:
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003214 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003215 goto end;
3216
3217 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003218 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003219 break;
3220
3221 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003222 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003223 break;
3224
3225 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003226 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003227 break;
3228
3229 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003230 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003231 break;
3232
Christopher Faulet3e964192018-10-24 11:39:23 +02003233 case ACT_HTTP_DEL_HDR:
3234 /* remove all occurrences of the header */
3235 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01003236 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02003237 http_remove_header(htx, &ctx);
3238 break;
3239
Christopher Faulet3e964192018-10-24 11:39:23 +02003240 case ACT_HTTP_REDIR:
3241 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003242 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003243 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003244 goto end;
3245
3246 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3247 /* Note: only the first valid tracking parameter of each
3248 * applies.
3249 */
3250 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3251 struct stktable *t;
3252 struct stksess *ts;
3253 struct stktable_key *key;
3254 void *ptr;
3255
3256 t = rule->arg.trk_ctr.table.t;
3257 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL,
3258 rule->arg.trk_ctr.expr, NULL);
3259
3260 if (key && (ts = stktable_get_entry(t, key))) {
3261 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3262
3263 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3264
3265 /* let's count a new HTTP request as it's the first time we do it */
3266 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3267 if (ptr)
3268 stktable_data_cast(ptr, http_req_cnt)++;
3269
3270 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3271 if (ptr)
3272 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3273 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3274
3275 /* When the client triggers a 4xx from the server, it's most often due
3276 * to a missing object or permission. These events should be tracked
3277 * because if they happen often, it may indicate a brute force or a
3278 * vulnerability scan. Normally this is done when receiving the response
3279 * but here we're tracking after this ought to have been done so we have
3280 * to do it on purpose.
3281 */
3282 if ((unsigned)(txn->status - 400) < 100) {
3283 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3284 if (ptr)
3285 stktable_data_cast(ptr, http_err_cnt)++;
3286
3287 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3288 if (ptr)
3289 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3290 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3291 }
3292
3293 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3294
3295 /* If data was modified, we need to touch to re-schedule sync */
3296 stktable_touch_local(t, ts, 0);
3297
3298 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3299 if (sess->fe != s->be)
3300 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3301 }
3302 }
3303 break;
3304
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003305 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003306 default:
3307 break;
3308 }
3309 }
3310
3311 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003312 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003313 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003314 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003315
Christopher Faulet3e964192018-10-24 11:39:23 +02003316 /* we reached the end of the rules, nothing to report */
3317 return rule_ret;
3318}
3319
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003320/*
3321 * Manage client-side cookie. It can impact performance by about 2% so it is
3322 * desirable to call it only when needed. This code is quite complex because
3323 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3324 * highly recommended not to touch this part without a good reason !
3325 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003326static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003327{
3328 struct session *sess = s->sess;
3329 struct http_txn *txn = s->txn;
3330 struct htx *htx;
3331 struct http_hdr_ctx ctx;
3332 char *hdr_beg, *hdr_end, *del_from;
3333 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3334 int preserve_hdr;
3335
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003336 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003337 ctx.blk = NULL;
3338 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003339 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003340 del_from = NULL; /* nothing to be deleted */
3341 preserve_hdr = 0; /* assume we may kill the whole header */
3342
3343 /* Now look for cookies. Conforming to RFC2109, we have to support
3344 * attributes whose name begin with a '$', and associate them with
3345 * the right cookie, if we want to delete this cookie.
3346 * So there are 3 cases for each cookie read :
3347 * 1) it's a special attribute, beginning with a '$' : ignore it.
3348 * 2) it's a server id cookie that we *MAY* want to delete : save
3349 * some pointers on it (last semi-colon, beginning of cookie...)
3350 * 3) it's an application cookie : we *MAY* have to delete a previous
3351 * "special" cookie.
3352 * At the end of loop, if a "special" cookie remains, we may have to
3353 * remove it. If no application cookie persists in the header, we
3354 * *MUST* delete it.
3355 *
3356 * Note: RFC2965 is unclear about the processing of spaces around
3357 * the equal sign in the ATTR=VALUE form. A careful inspection of
3358 * the RFC explicitly allows spaces before it, and not within the
3359 * tokens (attrs or values). An inspection of RFC2109 allows that
3360 * too but section 10.1.3 lets one think that spaces may be allowed
3361 * after the equal sign too, resulting in some (rare) buggy
3362 * implementations trying to do that. So let's do what servers do.
3363 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3364 * allowed quoted strings in values, with any possible character
3365 * after a backslash, including control chars and delimitors, which
3366 * causes parsing to become ambiguous. Browsers also allow spaces
3367 * within values even without quotes.
3368 *
3369 * We have to keep multiple pointers in order to support cookie
3370 * removal at the beginning, middle or end of header without
3371 * corrupting the header. All of these headers are valid :
3372 *
3373 * hdr_beg hdr_end
3374 * | |
3375 * v |
3376 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3377 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3378 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3379 * | | | | | | |
3380 * | | | | | | |
3381 * | | | | | | +--> next
3382 * | | | | | +----> val_end
3383 * | | | | +-----------> val_beg
3384 * | | | +--------------> equal
3385 * | | +----------------> att_end
3386 * | +---------------------> att_beg
3387 * +--------------------------> prev
3388 *
3389 */
3390 hdr_beg = ctx.value.ptr;
3391 hdr_end = hdr_beg + ctx.value.len;
3392 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3393 /* Iterate through all cookies on this line */
3394
3395 /* find att_beg */
3396 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003397 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003398 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003399 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003400
3401 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3402 att_beg++;
3403
3404 /* find att_end : this is the first character after the last non
3405 * space before the equal. It may be equal to hdr_end.
3406 */
3407 equal = att_end = att_beg;
3408 while (equal < hdr_end) {
3409 if (*equal == '=' || *equal == ',' || *equal == ';')
3410 break;
3411 if (HTTP_IS_SPHT(*equal++))
3412 continue;
3413 att_end = equal;
3414 }
3415
3416 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3417 * is between <att_beg> and <equal>, both may be identical.
3418 */
3419 /* look for end of cookie if there is an equal sign */
3420 if (equal < hdr_end && *equal == '=') {
3421 /* look for the beginning of the value */
3422 val_beg = equal + 1;
3423 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3424 val_beg++;
3425
3426 /* find the end of the value, respecting quotes */
3427 next = http_find_cookie_value_end(val_beg, hdr_end);
3428
3429 /* make val_end point to the first white space or delimitor after the value */
3430 val_end = next;
3431 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3432 val_end--;
3433 }
3434 else
3435 val_beg = val_end = next = equal;
3436
3437 /* We have nothing to do with attributes beginning with
3438 * '$'. However, they will automatically be removed if a
3439 * header before them is removed, since they're supposed
3440 * to be linked together.
3441 */
3442 if (*att_beg == '$')
3443 continue;
3444
3445 /* Ignore cookies with no equal sign */
3446 if (equal == next) {
3447 /* This is not our cookie, so we must preserve it. But if we already
3448 * scheduled another cookie for removal, we cannot remove the
3449 * complete header, but we can remove the previous block itself.
3450 */
3451 preserve_hdr = 1;
3452 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003453 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003454 val_end += delta;
3455 next += delta;
3456 hdr_end += delta;
3457 prev = del_from;
3458 del_from = NULL;
3459 }
3460 continue;
3461 }
3462
3463 /* if there are spaces around the equal sign, we need to
3464 * strip them otherwise we'll get trouble for cookie captures,
3465 * or even for rewrites. Since this happens extremely rarely,
3466 * it does not hurt performance.
3467 */
3468 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3469 int stripped_before = 0;
3470 int stripped_after = 0;
3471
3472 if (att_end != equal) {
3473 memmove(att_end, equal, hdr_end - equal);
3474 stripped_before = (att_end - equal);
3475 equal += stripped_before;
3476 val_beg += stripped_before;
3477 }
3478
3479 if (val_beg > equal + 1) {
3480 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3481 stripped_after = (equal + 1) - val_beg;
3482 val_beg += stripped_after;
3483 stripped_before += stripped_after;
3484 }
3485
3486 val_end += stripped_before;
3487 next += stripped_before;
3488 hdr_end += stripped_before;
3489 }
3490 /* now everything is as on the diagram above */
3491
3492 /* First, let's see if we want to capture this cookie. We check
3493 * that we don't already have a client side cookie, because we
3494 * can only capture one. Also as an optimisation, we ignore
3495 * cookies shorter than the declared name.
3496 */
3497 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3498 (val_end - att_beg >= sess->fe->capture_namelen) &&
3499 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3500 int log_len = val_end - att_beg;
3501
3502 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3503 ha_alert("HTTP logging : out of memory.\n");
3504 } else {
3505 if (log_len > sess->fe->capture_len)
3506 log_len = sess->fe->capture_len;
3507 memcpy(txn->cli_cookie, att_beg, log_len);
3508 txn->cli_cookie[log_len] = 0;
3509 }
3510 }
3511
3512 /* Persistence cookies in passive, rewrite or insert mode have the
3513 * following form :
3514 *
3515 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3516 *
3517 * For cookies in prefix mode, the form is :
3518 *
3519 * Cookie: NAME=SRV~VALUE
3520 */
3521 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3522 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3523 struct server *srv = s->be->srv;
3524 char *delim;
3525
3526 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3527 * have the server ID between val_beg and delim, and the original cookie between
3528 * delim+1 and val_end. Otherwise, delim==val_end :
3529 *
3530 * hdr_beg
3531 * |
3532 * v
3533 * NAME=SRV; # in all but prefix modes
3534 * NAME=SRV~OPAQUE ; # in prefix mode
3535 * || || | |+-> next
3536 * || || | +--> val_end
3537 * || || +---------> delim
3538 * || |+------------> val_beg
3539 * || +-------------> att_end = equal
3540 * |+-----------------> att_beg
3541 * +------------------> prev
3542 *
3543 */
3544 if (s->be->ck_opts & PR_CK_PFX) {
3545 for (delim = val_beg; delim < val_end; delim++)
3546 if (*delim == COOKIE_DELIM)
3547 break;
3548 }
3549 else {
3550 char *vbar1;
3551 delim = val_end;
3552 /* Now check if the cookie contains a date field, which would
3553 * appear after a vertical bar ('|') just after the server name
3554 * and before the delimiter.
3555 */
3556 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3557 if (vbar1) {
3558 /* OK, so left of the bar is the server's cookie and
3559 * right is the last seen date. It is a base64 encoded
3560 * 30-bit value representing the UNIX date since the
3561 * epoch in 4-second quantities.
3562 */
3563 int val;
3564 delim = vbar1++;
3565 if (val_end - vbar1 >= 5) {
3566 val = b64tos30(vbar1);
3567 if (val > 0)
3568 txn->cookie_last_date = val << 2;
3569 }
3570 /* look for a second vertical bar */
3571 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3572 if (vbar1 && (val_end - vbar1 > 5)) {
3573 val = b64tos30(vbar1 + 1);
3574 if (val > 0)
3575 txn->cookie_first_date = val << 2;
3576 }
3577 }
3578 }
3579
3580 /* if the cookie has an expiration date and the proxy wants to check
3581 * it, then we do that now. We first check if the cookie is too old,
3582 * then only if it has expired. We detect strict overflow because the
3583 * time resolution here is not great (4 seconds). Cookies with dates
3584 * in the future are ignored if their offset is beyond one day. This
3585 * allows an admin to fix timezone issues without expiring everyone
3586 * and at the same time avoids keeping unwanted side effects for too
3587 * long.
3588 */
3589 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3590 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3591 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3592 txn->flags &= ~TX_CK_MASK;
3593 txn->flags |= TX_CK_OLD;
3594 delim = val_beg; // let's pretend we have not found the cookie
3595 txn->cookie_first_date = 0;
3596 txn->cookie_last_date = 0;
3597 }
3598 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3599 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3600 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3601 txn->flags &= ~TX_CK_MASK;
3602 txn->flags |= TX_CK_EXPIRED;
3603 delim = val_beg; // let's pretend we have not found the cookie
3604 txn->cookie_first_date = 0;
3605 txn->cookie_last_date = 0;
3606 }
3607
3608 /* Here, we'll look for the first running server which supports the cookie.
3609 * This allows to share a same cookie between several servers, for example
3610 * to dedicate backup servers to specific servers only.
3611 * However, to prevent clients from sticking to cookie-less backup server
3612 * when they have incidentely learned an empty cookie, we simply ignore
3613 * empty cookies and mark them as invalid.
3614 * The same behaviour is applied when persistence must be ignored.
3615 */
3616 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3617 srv = NULL;
3618
3619 while (srv) {
3620 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3621 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3622 if ((srv->cur_state != SRV_ST_STOPPED) ||
3623 (s->be->options & PR_O_PERSIST) ||
3624 (s->flags & SF_FORCE_PRST)) {
3625 /* we found the server and we can use it */
3626 txn->flags &= ~TX_CK_MASK;
3627 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3628 s->flags |= SF_DIRECT | SF_ASSIGNED;
3629 s->target = &srv->obj_type;
3630 break;
3631 } else {
3632 /* we found a server, but it's down,
3633 * mark it as such and go on in case
3634 * another one is available.
3635 */
3636 txn->flags &= ~TX_CK_MASK;
3637 txn->flags |= TX_CK_DOWN;
3638 }
3639 }
3640 srv = srv->next;
3641 }
3642
3643 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3644 /* no server matched this cookie or we deliberately skipped it */
3645 txn->flags &= ~TX_CK_MASK;
3646 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3647 txn->flags |= TX_CK_UNUSED;
3648 else
3649 txn->flags |= TX_CK_INVALID;
3650 }
3651
3652 /* depending on the cookie mode, we may have to either :
3653 * - delete the complete cookie if we're in insert+indirect mode, so that
3654 * the server never sees it ;
3655 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003656 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003657 * if we're in cookie prefix mode
3658 */
3659 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3660 int delta; /* negative */
3661
3662 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3663 delta = val_beg - (delim + 1);
3664 val_end += delta;
3665 next += delta;
3666 hdr_end += delta;
3667 del_from = NULL;
3668 preserve_hdr = 1; /* we want to keep this cookie */
3669 }
3670 else if (del_from == NULL &&
3671 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3672 del_from = prev;
3673 }
3674 }
3675 else {
3676 /* This is not our cookie, so we must preserve it. But if we already
3677 * scheduled another cookie for removal, we cannot remove the
3678 * complete header, but we can remove the previous block itself.
3679 */
3680 preserve_hdr = 1;
3681
3682 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003683 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003684 if (att_beg >= del_from)
3685 att_beg += delta;
3686 if (att_end >= del_from)
3687 att_end += delta;
3688 val_beg += delta;
3689 val_end += delta;
3690 next += delta;
3691 hdr_end += delta;
3692 prev = del_from;
3693 del_from = NULL;
3694 }
3695 }
3696
3697 /* continue with next cookie on this header line */
3698 att_beg = next;
3699 } /* for each cookie */
3700
3701
3702 /* There are no more cookies on this line.
3703 * We may still have one (or several) marked for deletion at the
3704 * end of the line. We must do this now in two ways :
3705 * - if some cookies must be preserved, we only delete from the
3706 * mark to the end of line ;
3707 * - if nothing needs to be preserved, simply delete the whole header
3708 */
3709 if (del_from) {
3710 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3711 }
3712 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003713 if (hdr_beg != hdr_end)
3714 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003715 else
3716 http_remove_header(htx, &ctx);
3717 }
3718 } /* for each "Cookie header */
3719}
3720
3721/*
3722 * Manage server-side cookies. It can impact performance by about 2% so it is
3723 * desirable to call it only when needed. This function is also used when we
3724 * just need to know if there is a cookie (eg: for check-cache).
3725 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003726static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003727{
3728 struct session *sess = s->sess;
3729 struct http_txn *txn = s->txn;
3730 struct htx *htx;
3731 struct http_hdr_ctx ctx;
3732 struct server *srv;
3733 char *hdr_beg, *hdr_end;
3734 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003735 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003736
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003737 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003738
3739 ctx.blk = NULL;
3740 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003741 int is_first = 1;
3742
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003743 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3744 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3745 break;
3746 is_cookie2 = 1;
3747 }
3748
3749 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3750 * <prev> points to the colon.
3751 */
3752 txn->flags |= TX_SCK_PRESENT;
3753
3754 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3755 * check-cache is enabled) and we are not interested in checking
3756 * them. Warning, the cookie capture is declared in the frontend.
3757 */
3758 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3759 break;
3760
3761 /* OK so now we know we have to process this response cookie.
3762 * The format of the Set-Cookie header is slightly different
3763 * from the format of the Cookie header in that it does not
3764 * support the comma as a cookie delimiter (thus the header
3765 * cannot be folded) because the Expires attribute described in
3766 * the original Netscape's spec may contain an unquoted date
3767 * with a comma inside. We have to live with this because
3768 * many browsers don't support Max-Age and some browsers don't
3769 * support quoted strings. However the Set-Cookie2 header is
3770 * clean.
3771 *
3772 * We have to keep multiple pointers in order to support cookie
3773 * removal at the beginning, middle or end of header without
3774 * corrupting the header (in case of set-cookie2). A special
3775 * pointer, <scav> points to the beginning of the set-cookie-av
3776 * fields after the first semi-colon. The <next> pointer points
3777 * either to the end of line (set-cookie) or next unquoted comma
3778 * (set-cookie2). All of these headers are valid :
3779 *
3780 * hdr_beg hdr_end
3781 * | |
3782 * v |
3783 * NAME1 = VALUE 1 ; Secure; Path="/" |
3784 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3785 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3786 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3787 * | | | | | | | |
3788 * | | | | | | | +-> next
3789 * | | | | | | +------------> scav
3790 * | | | | | +--------------> val_end
3791 * | | | | +--------------------> val_beg
3792 * | | | +----------------------> equal
3793 * | | +------------------------> att_end
3794 * | +----------------------------> att_beg
3795 * +------------------------------> prev
3796 * -------------------------------> hdr_beg
3797 */
3798 hdr_beg = ctx.value.ptr;
3799 hdr_end = hdr_beg + ctx.value.len;
3800 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3801
3802 /* Iterate through all cookies on this line */
3803
3804 /* find att_beg */
3805 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003806 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003807 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003808 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003809
3810 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3811 att_beg++;
3812
3813 /* find att_end : this is the first character after the last non
3814 * space before the equal. It may be equal to hdr_end.
3815 */
3816 equal = att_end = att_beg;
3817
3818 while (equal < hdr_end) {
3819 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3820 break;
3821 if (HTTP_IS_SPHT(*equal++))
3822 continue;
3823 att_end = equal;
3824 }
3825
3826 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3827 * is between <att_beg> and <equal>, both may be identical.
3828 */
3829
3830 /* look for end of cookie if there is an equal sign */
3831 if (equal < hdr_end && *equal == '=') {
3832 /* look for the beginning of the value */
3833 val_beg = equal + 1;
3834 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3835 val_beg++;
3836
3837 /* find the end of the value, respecting quotes */
3838 next = http_find_cookie_value_end(val_beg, hdr_end);
3839
3840 /* make val_end point to the first white space or delimitor after the value */
3841 val_end = next;
3842 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3843 val_end--;
3844 }
3845 else {
3846 /* <equal> points to next comma, semi-colon or EOL */
3847 val_beg = val_end = next = equal;
3848 }
3849
3850 if (next < hdr_end) {
3851 /* Set-Cookie2 supports multiple cookies, and <next> points to
3852 * a colon or semi-colon before the end. So skip all attr-value
3853 * pairs and look for the next comma. For Set-Cookie, since
3854 * commas are permitted in values, skip to the end.
3855 */
3856 if (is_cookie2)
3857 next = http_find_hdr_value_end(next, hdr_end);
3858 else
3859 next = hdr_end;
3860 }
3861
3862 /* Now everything is as on the diagram above */
3863
3864 /* Ignore cookies with no equal sign */
3865 if (equal == val_end)
3866 continue;
3867
3868 /* If there are spaces around the equal sign, we need to
3869 * strip them otherwise we'll get trouble for cookie captures,
3870 * or even for rewrites. Since this happens extremely rarely,
3871 * it does not hurt performance.
3872 */
3873 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3874 int stripped_before = 0;
3875 int stripped_after = 0;
3876
3877 if (att_end != equal) {
3878 memmove(att_end, equal, hdr_end - equal);
3879 stripped_before = (att_end - equal);
3880 equal += stripped_before;
3881 val_beg += stripped_before;
3882 }
3883
3884 if (val_beg > equal + 1) {
3885 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3886 stripped_after = (equal + 1) - val_beg;
3887 val_beg += stripped_after;
3888 stripped_before += stripped_after;
3889 }
3890
3891 val_end += stripped_before;
3892 next += stripped_before;
3893 hdr_end += stripped_before;
3894
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003895 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003896 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003897 }
3898
3899 /* First, let's see if we want to capture this cookie. We check
3900 * that we don't already have a server side cookie, because we
3901 * can only capture one. Also as an optimisation, we ignore
3902 * cookies shorter than the declared name.
3903 */
3904 if (sess->fe->capture_name != NULL &&
3905 txn->srv_cookie == NULL &&
3906 (val_end - att_beg >= sess->fe->capture_namelen) &&
3907 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3908 int log_len = val_end - att_beg;
3909 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3910 ha_alert("HTTP logging : out of memory.\n");
3911 }
3912 else {
3913 if (log_len > sess->fe->capture_len)
3914 log_len = sess->fe->capture_len;
3915 memcpy(txn->srv_cookie, att_beg, log_len);
3916 txn->srv_cookie[log_len] = 0;
3917 }
3918 }
3919
3920 srv = objt_server(s->target);
3921 /* now check if we need to process it for persistence */
3922 if (!(s->flags & SF_IGNORE_PRST) &&
3923 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3924 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3925 /* assume passive cookie by default */
3926 txn->flags &= ~TX_SCK_MASK;
3927 txn->flags |= TX_SCK_FOUND;
3928
3929 /* If the cookie is in insert mode on a known server, we'll delete
3930 * this occurrence because we'll insert another one later.
3931 * We'll delete it too if the "indirect" option is set and we're in
3932 * a direct access.
3933 */
3934 if (s->be->ck_opts & PR_CK_PSV) {
3935 /* The "preserve" flag was set, we don't want to touch the
3936 * server's cookie.
3937 */
3938 }
3939 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3940 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3941 /* this cookie must be deleted */
3942 if (prev == hdr_beg && next == hdr_end) {
3943 /* whole header */
3944 http_remove_header(htx, &ctx);
3945 /* note: while both invalid now, <next> and <hdr_end>
3946 * are still equal, so the for() will stop as expected.
3947 */
3948 } else {
3949 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003950 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003951 next = prev;
3952 hdr_end += delta;
3953 }
3954 txn->flags &= ~TX_SCK_MASK;
3955 txn->flags |= TX_SCK_DELETED;
3956 /* and go on with next cookie */
3957 }
3958 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3959 /* replace bytes val_beg->val_end with the cookie name associated
3960 * with this server since we know it.
3961 */
3962 int sliding, delta;
3963
3964 ctx.value = ist2(val_beg, val_end - val_beg);
3965 ctx.lws_before = ctx.lws_after = 0;
3966 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3967 delta = srv->cklen - (val_end - val_beg);
3968 sliding = (ctx.value.ptr - val_beg);
3969 hdr_beg += sliding;
3970 val_beg += sliding;
3971 next += sliding + delta;
3972 hdr_end += sliding + delta;
3973
3974 txn->flags &= ~TX_SCK_MASK;
3975 txn->flags |= TX_SCK_REPLACED;
3976 }
3977 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3978 /* insert the cookie name associated with this server
3979 * before existing cookie, and insert a delimiter between them..
3980 */
3981 int sliding, delta;
3982 ctx.value = ist2(val_beg, 0);
3983 ctx.lws_before = ctx.lws_after = 0;
3984 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3985 delta = srv->cklen + 1;
3986 sliding = (ctx.value.ptr - val_beg);
3987 hdr_beg += sliding;
3988 val_beg += sliding;
3989 next += sliding + delta;
3990 hdr_end += sliding + delta;
3991
3992 val_beg[srv->cklen] = COOKIE_DELIM;
3993 txn->flags &= ~TX_SCK_MASK;
3994 txn->flags |= TX_SCK_REPLACED;
3995 }
3996 }
3997 /* that's done for this cookie, check the next one on the same
3998 * line when next != hdr_end (only if is_cookie2).
3999 */
4000 }
4001 }
4002}
4003
Christopher Faulet25a02f62018-10-24 12:00:25 +02004004/*
4005 * Parses the Cache-Control and Pragma request header fields to determine if
4006 * the request may be served from the cache and/or if it is cacheable. Updates
4007 * s->txn->flags.
4008 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004009void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004010{
4011 struct http_txn *txn = s->txn;
4012 struct htx *htx;
4013 int32_t pos;
4014 int pragma_found, cc_found, i;
4015
4016 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
4017 return; /* nothing more to do here */
4018
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004019 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02004020 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02004021 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004022 struct htx_blk *blk = htx_get_blk(htx, pos);
4023 enum htx_blk_type type = htx_get_blk_type(blk);
4024 struct ist n, v;
4025
4026 if (type == HTX_BLK_EOH)
4027 break;
4028 if (type != HTX_BLK_HDR)
4029 continue;
4030
4031 n = htx_get_blk_name(htx, blk);
4032 v = htx_get_blk_value(htx, blk);
4033
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004034 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004035 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4036 pragma_found = 1;
4037 continue;
4038 }
4039 }
4040
4041 /* Don't use the cache and don't try to store if we found the
4042 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004043 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004044 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4045 txn->flags |= TX_CACHE_IGNORE;
4046 continue;
4047 }
4048
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004049 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004050 continue;
4051
4052 /* OK, right now we know we have a cache-control header */
4053 cc_found = 1;
4054 if (!v.len) /* no info */
4055 continue;
4056
4057 i = 0;
4058 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4059 !isspace((unsigned char)*(v.ptr+i)))
4060 i++;
4061
4062 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
4063 * values after max-age, max-stale nor min-fresh, we simply don't
4064 * use the cache when they're specified.
4065 */
4066 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
4067 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4068 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
4069 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
4070 txn->flags |= TX_CACHE_IGNORE;
4071 continue;
4072 }
4073
4074 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
4075 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4076 continue;
4077 }
4078 }
4079
4080 /* RFC7234#5.4:
4081 * When the Cache-Control header field is also present and
4082 * understood in a request, Pragma is ignored.
4083 * When the Cache-Control header field is not present in a
4084 * request, caches MUST consider the no-cache request
4085 * pragma-directive as having the same effect as if
4086 * "Cache-Control: no-cache" were present.
4087 */
4088 if (!cc_found && pragma_found)
4089 txn->flags |= TX_CACHE_IGNORE;
4090}
4091
4092/*
4093 * Check if response is cacheable or not. Updates s->txn->flags.
4094 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004095void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004096{
4097 struct http_txn *txn = s->txn;
4098 struct htx *htx;
4099 int32_t pos;
4100 int i;
4101
4102 if (txn->status < 200) {
4103 /* do not try to cache interim responses! */
4104 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4105 return;
4106 }
4107
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004108 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004109 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004110 struct htx_blk *blk = htx_get_blk(htx, pos);
4111 enum htx_blk_type type = htx_get_blk_type(blk);
4112 struct ist n, v;
4113
4114 if (type == HTX_BLK_EOH)
4115 break;
4116 if (type != HTX_BLK_HDR)
4117 continue;
4118
4119 n = htx_get_blk_name(htx, blk);
4120 v = htx_get_blk_value(htx, blk);
4121
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004122 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004123 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4124 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4125 return;
4126 }
4127 }
4128
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004129 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004130 continue;
4131
4132 /* OK, right now we know we have a cache-control header */
4133 if (!v.len) /* no info */
4134 continue;
4135
4136 i = 0;
4137 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4138 !isspace((unsigned char)*(v.ptr+i)))
4139 i++;
4140
4141 /* we have a complete value between v.ptr and (v.ptr+i) */
4142 if (i < v.len && *(v.ptr + i) == '=') {
4143 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
4144 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
4145 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4146 continue;
4147 }
4148
4149 /* we have something of the form no-cache="set-cookie" */
4150 if ((v.len >= 21) &&
4151 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
4152 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
4153 txn->flags &= ~TX_CACHE_COOK;
4154 continue;
4155 }
4156
4157 /* OK, so we know that either p2 points to the end of string or to a comma */
4158 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
4159 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4160 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
4161 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4162 return;
4163 }
4164
4165 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4166 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4167 continue;
4168 }
4169 }
4170}
4171
Christopher Faulet377c5a52018-10-24 21:21:30 +02004172/*
4173 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4174 * for the current backend.
4175 *
4176 * It is assumed that the request is either a HEAD, GET, or POST and that the
4177 * uri_auth field is valid.
4178 *
4179 * Returns 1 if stats should be provided, otherwise 0.
4180 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004181static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004182{
4183 struct uri_auth *uri_auth = backend->uri_auth;
4184 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004185 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004186 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004187
4188 if (!uri_auth)
4189 return 0;
4190
4191 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4192 return 0;
4193
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004194 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004195 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004196 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01004197 if (*uri_auth->uri_prefix == '/')
4198 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004199
4200 /* check URI size */
4201 if (uri_auth->uri_len > uri.len)
4202 return 0;
4203
4204 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4205 return 0;
4206
4207 return 1;
4208}
4209
4210/* This function prepares an applet to handle the stats. It can deal with the
4211 * "100-continue" expectation, check that admin rules are met for POST requests,
4212 * and program a response message if something was unexpected. It cannot fail
4213 * and always relies on the stats applet to complete the job. It does not touch
4214 * analysers nor counters, which are left to the caller. It does not touch
4215 * s->target which is supposed to already point to the stats applet. The caller
4216 * is expected to have already assigned an appctx to the stream.
4217 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004218static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004219{
4220 struct stats_admin_rule *stats_admin_rule;
4221 struct stream_interface *si = &s->si[1];
4222 struct session *sess = s->sess;
4223 struct http_txn *txn = s->txn;
4224 struct http_msg *msg = &txn->req;
4225 struct uri_auth *uri_auth = s->be->uri_auth;
4226 const char *h, *lookup, *end;
4227 struct appctx *appctx;
4228 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004229 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004230
4231 appctx = si_appctx(si);
4232 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4233 appctx->st1 = appctx->st2 = 0;
4234 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004235 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004236 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4237 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4238 appctx->ctx.stats.flags |= STAT_CHUNKED;
4239
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004240 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004241 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004242 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4243 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004244
4245 for (h = lookup; h <= end - 3; h++) {
4246 if (memcmp(h, ";up", 3) == 0) {
4247 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4248 break;
4249 }
4250 }
4251
4252 if (uri_auth->refresh) {
4253 for (h = lookup; h <= end - 10; h++) {
4254 if (memcmp(h, ";norefresh", 10) == 0) {
4255 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4256 break;
4257 }
4258 }
4259 }
4260
4261 for (h = lookup; h <= end - 4; h++) {
4262 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004263 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004264 break;
4265 }
4266 }
4267
4268 for (h = lookup; h <= end - 6; h++) {
4269 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004270 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004271 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4272 break;
4273 }
4274 }
4275
Christopher Faulet6338a082019-09-09 15:50:54 +02004276 for (h = lookup; h <= end - 5; h++) {
4277 if (memcmp(h, ";json", 5) == 0) {
4278 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4279 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4280 break;
4281 }
4282 }
4283
4284 for (h = lookup; h <= end - 12; h++) {
4285 if (memcmp(h, ";json-schema", 12) == 0) {
4286 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4287 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4288 break;
4289 }
4290 }
4291
Christopher Faulet377c5a52018-10-24 21:21:30 +02004292 for (h = lookup; h <= end - 8; h++) {
4293 if (memcmp(h, ";st=", 4) == 0) {
4294 int i;
4295 h += 4;
4296 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4297 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4298 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4299 appctx->ctx.stats.st_code = i;
4300 break;
4301 }
4302 }
4303 break;
4304 }
4305 }
4306
4307 appctx->ctx.stats.scope_str = 0;
4308 appctx->ctx.stats.scope_len = 0;
4309 for (h = lookup; h <= end - 8; h++) {
4310 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4311 int itx = 0;
4312 const char *h2;
4313 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4314 const char *err;
4315
4316 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4317 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004318 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4319 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004320 if (*h == ';' || *h == '&' || *h == ' ')
4321 break;
4322 itx++;
4323 h++;
4324 }
4325
4326 if (itx > STAT_SCOPE_TXT_MAXLEN)
4327 itx = STAT_SCOPE_TXT_MAXLEN;
4328 appctx->ctx.stats.scope_len = itx;
4329
4330 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4331 memcpy(scope_txt, h2, itx);
4332 scope_txt[itx] = '\0';
4333 err = invalid_char(scope_txt);
4334 if (err) {
4335 /* bad char in search text => clear scope */
4336 appctx->ctx.stats.scope_str = 0;
4337 appctx->ctx.stats.scope_len = 0;
4338 }
4339 break;
4340 }
4341 }
4342
4343 /* now check whether we have some admin rules for this request */
4344 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4345 int ret = 1;
4346
4347 if (stats_admin_rule->cond) {
4348 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4349 ret = acl_pass(ret);
4350 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4351 ret = !ret;
4352 }
4353
4354 if (ret) {
4355 /* no rule, or the rule matches */
4356 appctx->ctx.stats.flags |= STAT_ADMIN;
4357 break;
4358 }
4359 }
4360
Christopher Faulet5d45e382019-02-27 15:15:23 +01004361 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4362 appctx->st0 = STAT_HTTP_HEAD;
4363 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004364 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004365 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004366 if (msg->msg_state < HTTP_MSG_DATA)
4367 req->analysers |= AN_REQ_HTTP_BODY;
4368 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004369 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004370 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004371 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4372 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4373 appctx->st0 = STAT_HTTP_LAST;
4374 }
4375 }
4376 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004377 /* Unsupported method */
4378 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4379 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4380 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004381 }
4382
4383 s->task->nice = -32; /* small boost for HTTP statistics */
4384 return 1;
4385}
4386
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004387void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004388{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004389 struct channel *req = &s->req;
4390 struct channel *res = &s->res;
4391 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004392 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004393 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004394 struct ist path, location;
4395 unsigned int flags;
4396 size_t data;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004397
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004398 /*
4399 * Create the location
4400 */
4401 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004402
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004403 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004404 /* special prefix "/" means don't change URL */
4405 srv = __objt_server(s->target);
4406 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4407 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4408 return;
4409 }
4410
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004411 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004412 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004413 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004414 path = http_get_path(htx_sl_req_uri(sl));
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004415 if (!path.ptr)
4416 return;
4417
4418 if (!chunk_memcat(&trash, path.ptr, path.len))
4419 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004420 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004421
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004422 /*
4423 * Create the 302 respone
4424 */
4425 htx = htx_from_buf(&res->buf);
4426 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4427 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4428 ist("HTTP/1.1"), ist("302"), ist("Found"));
4429 if (!sl)
4430 goto fail;
4431 sl->info.res.status = 302;
4432 s->txn->status = 302;
4433
4434 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4435 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4436 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4437 !htx_add_header(htx, ist("Location"), location))
4438 goto fail;
4439
4440 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4441 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004442
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004443 /*
4444 * Send the message
4445 */
4446 data = htx->data - co_data(res);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004447 c_adv(res, data);
4448 res->total += data;
4449
4450 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004451 si_shutr(si);
4452 si_shutw(si);
4453 si->err_type = SI_ET_NONE;
4454 si->state = SI_ST_CLO;
4455
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004456 channel_auto_read(req);
4457 channel_abort(req);
4458 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004459 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004460 channel_auto_read(res);
4461 channel_auto_close(res);
4462
4463 if (!(s->flags & SF_ERR_MASK))
4464 s->flags |= SF_ERR_LOCAL;
4465 if (!(s->flags & SF_FINST_MASK))
4466 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004467
4468 /* FIXME: we should increase a counter of redirects per server and per backend. */
4469 srv_inc_sess_ctr(srv);
4470 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004471 return;
4472
4473 fail:
4474 /* If an error occurred, remove the incomplete HTTP response from the
4475 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004476 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004477}
4478
Christopher Fauletf2824e62018-10-01 12:12:37 +02004479/* This function terminates the request because it was completly analyzed or
4480 * because an error was triggered during the body forwarding.
4481 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004482static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004483{
4484 struct channel *chn = &s->req;
4485 struct http_txn *txn = s->txn;
4486
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004487 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004488
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004489 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4490 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004491 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004492 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004493 goto end;
4494 }
4495
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004496 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4497 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004498 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004499 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004500
4501 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004502 /* No need to read anymore, the request was completely parsed.
4503 * We can shut the read side unless we want to abort_on_close,
4504 * or we have a POST request. The issue with POST requests is
4505 * that some browsers still send a CRLF after the request, and
4506 * this CRLF must be read so that it does not remain in the kernel
4507 * buffers, otherwise a close could cause an RST on some systems
4508 * (eg: Linux).
4509 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004510 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004511 channel_dont_read(chn);
4512
4513 /* if the server closes the connection, we want to immediately react
4514 * and close the socket to save packets and syscalls.
4515 */
4516 s->si[1].flags |= SI_FL_NOHALF;
4517
4518 /* In any case we've finished parsing the request so we must
4519 * disable Nagle when sending data because 1) we're not going
4520 * to shut this side, and 2) the server is waiting for us to
4521 * send pending data.
4522 */
4523 chn->flags |= CF_NEVER_WAIT;
4524
Christopher Fauletd01ce402019-01-02 17:44:13 +01004525 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4526 /* The server has not finished to respond, so we
4527 * don't want to move in order not to upset it.
4528 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004529 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004530 return;
4531 }
4532
Christopher Fauletf2824e62018-10-01 12:12:37 +02004533 /* When we get here, it means that both the request and the
4534 * response have finished receiving. Depending on the connection
4535 * mode, we'll have to wait for the last bytes to leave in either
4536 * direction, and sometimes for a close to be effective.
4537 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004538 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004539 /* Tunnel mode will not have any analyser so it needs to
4540 * poll for reads.
4541 */
4542 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004543 if (b_data(&chn->buf)) {
4544 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004545 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004546 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004547 txn->req.msg_state = HTTP_MSG_TUNNEL;
4548 }
4549 else {
4550 /* we're not expecting any new data to come for this
4551 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004552 *
4553 * However, there is an exception if the response
4554 * length is undefined. In this case, we need to wait
4555 * the close from the server. The response will be
4556 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004557 */
4558 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4559 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004560 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004561
4562 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4563 channel_shutr_now(chn);
4564 channel_shutw_now(chn);
4565 }
4566 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004567 goto check_channel_flags;
4568 }
4569
4570 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4571 http_msg_closing:
4572 /* nothing else to forward, just waiting for the output buffer
4573 * to be empty and for the shutw_now to take effect.
4574 */
4575 if (channel_is_empty(chn)) {
4576 txn->req.msg_state = HTTP_MSG_CLOSED;
4577 goto http_msg_closed;
4578 }
4579 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004580 txn->req.msg_state = HTTP_MSG_ERROR;
4581 goto end;
4582 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004583 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004584 return;
4585 }
4586
4587 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4588 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004589 /* if we don't know whether the server will close, we need to hard close */
4590 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4591 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004592 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004593 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004594 channel_dont_read(chn);
4595 goto end;
4596 }
4597
4598 check_channel_flags:
4599 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4600 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4601 /* if we've just closed an output, let's switch */
4602 txn->req.msg_state = HTTP_MSG_CLOSING;
4603 goto http_msg_closing;
4604 }
4605
4606 end:
4607 chn->analysers &= AN_REQ_FLT_END;
4608 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4609 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4610 channel_auto_close(chn);
4611 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004612 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004613}
4614
4615
4616/* This function terminates the response because it was completly analyzed or
4617 * because an error was triggered during the body forwarding.
4618 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004619static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004620{
4621 struct channel *chn = &s->res;
4622 struct http_txn *txn = s->txn;
4623
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004624 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004625
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004626 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4627 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004628 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004629 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004630 goto end;
4631 }
4632
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004633 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4634 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004635 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004636 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004637
4638 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4639 /* In theory, we don't need to read anymore, but we must
4640 * still monitor the server connection for a possible close
4641 * while the request is being uploaded, so we don't disable
4642 * reading.
4643 */
4644 /* channel_dont_read(chn); */
4645
4646 if (txn->req.msg_state < HTTP_MSG_DONE) {
4647 /* The client seems to still be sending data, probably
4648 * because we got an error response during an upload.
4649 * We have the choice of either breaking the connection
4650 * or letting it pass through. Let's do the later.
4651 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004652 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004653 return;
4654 }
4655
4656 /* When we get here, it means that both the request and the
4657 * response have finished receiving. Depending on the connection
4658 * mode, we'll have to wait for the last bytes to leave in either
4659 * direction, and sometimes for a close to be effective.
4660 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004661 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004662 channel_auto_read(chn);
4663 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004664 if (b_data(&chn->buf)) {
4665 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004666 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004667 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004668 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4669 }
4670 else {
4671 /* we're not expecting any new data to come for this
4672 * transaction, so we can close it.
4673 */
4674 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4675 channel_shutr_now(chn);
4676 channel_shutw_now(chn);
4677 }
4678 }
4679 goto check_channel_flags;
4680 }
4681
4682 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4683 http_msg_closing:
4684 /* nothing else to forward, just waiting for the output buffer
4685 * to be empty and for the shutw_now to take effect.
4686 */
4687 if (channel_is_empty(chn)) {
4688 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4689 goto http_msg_closed;
4690 }
4691 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004692 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004693 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004694 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004695 if (strm_sess(s)->listener->counters)
4696 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004697 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004698 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004699 goto end;
4700 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004701 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004702 return;
4703 }
4704
4705 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4706 http_msg_closed:
4707 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004708 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004709 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004710 goto end;
4711 }
4712
4713 check_channel_flags:
4714 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4715 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4716 /* if we've just closed an output, let's switch */
4717 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4718 goto http_msg_closing;
4719 }
4720
4721 end:
4722 chn->analysers &= AN_RES_FLT_END;
4723 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4724 chn->analysers |= AN_RES_FLT_XFER_DATA;
4725 channel_auto_close(chn);
4726 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004727 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004728}
4729
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004730void http_server_error(struct stream *s, struct stream_interface *si, int err,
4731 int finst, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004732{
4733 channel_auto_read(si_oc(si));
4734 channel_abort(si_oc(si));
4735 channel_auto_close(si_oc(si));
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004736 channel_htx_erase(si_oc(si), htxbuf(&(si_oc(si))->buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02004737 channel_auto_close(si_ic(si));
4738 channel_auto_read(si_ic(si));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004739
4740 /* <msg> is an HTX structure. So we copy it in the response's
4741 * channel */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004742 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004743 struct channel *chn = si_ic(si);
4744 struct htx *htx;
4745
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004746 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004747 chn->buf.data = msg->data;
4748 memcpy(chn->buf.area, msg->area, msg->data);
4749 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02004750 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02004751 c_adv(chn, htx->data);
4752 chn->total += htx->data;
4753 }
4754 if (!(s->flags & SF_ERR_MASK))
4755 s->flags |= err;
4756 if (!(s->flags & SF_FINST_MASK))
4757 s->flags |= finst;
4758}
4759
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004760void http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004761{
4762 channel_auto_read(&s->req);
4763 channel_abort(&s->req);
4764 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004765 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
4766 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02004767
4768 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004769
4770 /* <msg> is an HTX structure. So we copy it in the response's
4771 * channel */
4772 /* FIXME: It is a problem for now if there is some outgoing data */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004773 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004774 struct channel *chn = &s->res;
4775 struct htx *htx;
4776
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004777 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004778 chn->buf.data = msg->data;
4779 memcpy(chn->buf.area, msg->area, msg->data);
4780 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02004781 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02004782 c_adv(chn, htx->data);
4783 chn->total += htx->data;
4784 }
4785
4786 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
4787 channel_auto_read(&s->res);
4788 channel_auto_close(&s->res);
4789 channel_shutr_now(&s->res);
4790}
4791
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004792struct buffer *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004793{
4794 const int msgnum = http_get_status_idx(s->txn->status);
4795
4796 if (s->be->errmsg[msgnum].area)
4797 return &s->be->errmsg[msgnum];
4798 else if (strm_fe(s)->errmsg[msgnum].area)
4799 return &strm_fe(s)->errmsg[msgnum];
4800 else
Christopher Fauletf7346382019-07-17 22:02:08 +02004801 return &http_err_chunks[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004802}
4803
Christopher Faulet304cc402019-07-15 15:46:28 +02004804/* Return the error message corresponding to si->err_type. It is assumed
4805 * that the server side is closed. Note that err_type is actually a
4806 * bitmask, where almost only aborts may be cumulated with other
4807 * values. We consider that aborted operations are more important
4808 * than timeouts or errors due to the fact that nobody else in the
4809 * logs might explain incomplete retries. All others should avoid
4810 * being cumulated. It should normally not be possible to have multiple
4811 * aborts at once, but just in case, the first one in sequence is reported.
4812 * Note that connection errors appearing on the second request of a keep-alive
4813 * connection are not reported since this allows the client to retry.
4814 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004815void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004816{
4817 int err_type = si->err_type;
4818
4819 /* set s->txn->status for http_error_message(s) */
4820 s->txn->status = 503;
4821
4822 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004823 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4824 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004825 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004826 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4827 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4828 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004829 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004830 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4831 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004832 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004833 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4834 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004835 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004836 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4837 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4838 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004839 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004840 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4841 (s->flags & SF_SRV_REUSED) ? NULL :
4842 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004843 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004844 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4845 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4846 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004847 else { /* SI_ET_CONN_OTHER and others */
4848 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004849 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4850 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004851 }
4852}
4853
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004854
Christopher Faulet4a28a532019-03-01 11:19:40 +01004855/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4856 * on success and -1 on error.
4857 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004858static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004859{
4860 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4861 * then we must send an HTTP/1.1 100 Continue intermediate response.
4862 */
4863 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4864 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4865 struct ist hdr = { .ptr = "Expect", .len = 6 };
4866 struct http_hdr_ctx ctx;
4867
4868 ctx.blk = NULL;
4869 /* Expect is allowed in 1.1, look for it */
4870 if (http_find_header(htx, hdr, &ctx, 0) &&
4871 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004872 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004873 return -1;
4874 http_remove_header(htx, &ctx);
4875 }
4876 }
4877 return 0;
4878}
4879
Christopher Faulet23a3c792018-11-28 10:01:23 +01004880/* Send a 100-Continue response to the client. It returns 0 on success and -1
4881 * on error. The response channel is updated accordingly.
4882 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004883static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004884{
4885 struct channel *res = &s->res;
4886 struct htx *htx = htx_from_buf(&res->buf);
4887 struct htx_sl *sl;
4888 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4889 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4890 size_t data;
4891
4892 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4893 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4894 if (!sl)
4895 goto fail;
4896 sl->info.res.status = 100;
4897
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004898 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004899 goto fail;
4900
4901 data = htx->data - co_data(res);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004902 c_adv(res, data);
4903 res->total += data;
4904 return 0;
4905
4906 fail:
4907 /* If an error occurred, remove the incomplete HTTP response from the
4908 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004909 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004910 return -1;
4911}
4912
Christopher Faulet12c51e22018-11-28 15:59:42 +01004913
4914/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
4915 * ont whether we use a proxy or not. It returns 0 on success and -1 on
4916 * error. The response channel is updated accordingly.
4917 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004918static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
Christopher Faulet12c51e22018-11-28 15:59:42 +01004919{
4920 struct channel *res = &s->res;
4921 struct htx *htx = htx_from_buf(&res->buf);
4922 struct htx_sl *sl;
4923 struct ist code, body;
4924 int status;
4925 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4926 size_t data;
4927
4928 if (!(s->txn->flags & TX_USE_PX_CONN)) {
4929 status = 401;
4930 code = ist("401");
4931 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
4932 "You need a valid user and password to access this content.\n"
4933 "</body></html>\n");
4934 }
4935 else {
4936 status = 407;
4937 code = ist("407");
4938 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
4939 "You need a valid user and password to access this content.\n"
4940 "</body></html>\n");
4941 }
4942
4943 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4944 ist("HTTP/1.1"), code, ist("Unauthorized"));
4945 if (!sl)
4946 goto fail;
4947 sl->info.res.status = status;
4948 s->txn->status = status;
4949
4950 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
4951 goto fail;
4952
Willy Tarreaub5ba2b02019-06-11 16:08:25 +02004953 if (!htx_add_header(htx, ist("Content-length"), ist("112")) ||
4954 !htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Faulet12c51e22018-11-28 15:59:42 +01004955 !htx_add_header(htx, ist("Connection"), ist("close")) ||
Jérôme Magnin86cef232018-12-28 14:49:08 +01004956 !htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4957 goto fail;
4958 if (status == 401 && !htx_add_header(htx, ist("WWW-Authenticate"), ist2(trash.area, trash.data)))
4959 goto fail;
4960 if (status == 407 && !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
Christopher Faulet12c51e22018-11-28 15:59:42 +01004961 goto fail;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004962 if (!htx_add_endof(htx, HTX_BLK_EOH))
4963 goto fail;
4964
4965 while (body.len) {
4966 size_t sent = htx_add_data(htx, body);
4967 if (!sent)
4968 goto fail;
4969 body.ptr += sent;
4970 body.len -= sent;
4971 }
4972
4973 if (!htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet12c51e22018-11-28 15:59:42 +01004974 goto fail;
4975
Christopher Faulet06511812019-10-16 09:38:27 +02004976 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet12c51e22018-11-28 15:59:42 +01004977 data = htx->data - co_data(res);
Christopher Faulet12c51e22018-11-28 15:59:42 +01004978 c_adv(res, data);
4979 res->total += data;
4980
4981 channel_auto_read(&s->req);
4982 channel_abort(&s->req);
4983 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004984 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet12c51e22018-11-28 15:59:42 +01004985
4986 res->wex = tick_add_ifset(now_ms, res->wto);
4987 channel_auto_read(res);
4988 channel_auto_close(res);
4989 channel_shutr_now(res);
4990 return 0;
4991
4992 fail:
4993 /* If an error occurred, remove the incomplete HTTP response from the
4994 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004995 channel_htx_truncate(res, htx);
Christopher Faulet12c51e22018-11-28 15:59:42 +01004996 return -1;
4997}
4998
Christopher Faulet0f226952018-10-22 09:29:56 +02004999/*
5000 * Capture headers from message <htx> according to header list <cap_hdr>, and
5001 * fill the <cap> pointers appropriately.
5002 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005003static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02005004{
5005 struct cap_hdr *h;
5006 int32_t pos;
5007
Christopher Fauleta3f15502019-05-13 15:27:23 +02005008 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02005009 struct htx_blk *blk = htx_get_blk(htx, pos);
5010 enum htx_blk_type type = htx_get_blk_type(blk);
5011 struct ist n, v;
5012
5013 if (type == HTX_BLK_EOH)
5014 break;
5015 if (type != HTX_BLK_HDR)
5016 continue;
5017
5018 n = htx_get_blk_name(htx, blk);
5019
5020 for (h = cap_hdr; h; h = h->next) {
5021 if (h->namelen && (h->namelen == n.len) &&
5022 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
5023 if (cap[h->index] == NULL)
5024 cap[h->index] =
5025 pool_alloc(h->pool);
5026
5027 if (cap[h->index] == NULL) {
5028 ha_alert("HTTP capture : out of memory.\n");
5029 break;
5030 }
5031
5032 v = htx_get_blk_value(htx, blk);
5033 if (v.len > h->len)
5034 v.len = h->len;
5035
5036 memcpy(cap[h->index], v.ptr, v.len);
5037 cap[h->index][v.len]=0;
5038 }
5039 }
5040 }
5041}
5042
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005043/* Delete a value in a header between delimiters <from> and <next>. The header
5044 * itself is delimited by <start> and <end> pointers. The number of characters
5045 * displaced is returned, and the pointer to the first delimiter is updated if
5046 * required. The function tries as much as possible to respect the following
5047 * principles :
5048 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5049 * in which case <next> is simply removed
5050 * - set exactly one space character after the new first delimiter, unless there
5051 * are not enough characters in the block being moved to do so.
5052 * - remove unneeded spaces before the previous delimiter and after the new
5053 * one.
5054 *
5055 * It is the caller's responsibility to ensure that :
5056 * - <from> points to a valid delimiter or <start> ;
5057 * - <next> points to a valid delimiter or <end> ;
5058 * - there are non-space chars before <from>.
5059 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005060static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005061{
5062 char *prev = *from;
5063
5064 if (prev == start) {
5065 /* We're removing the first value. eat the semicolon, if <next>
5066 * is lower than <end> */
5067 if (next < end)
5068 next++;
5069
5070 while (next < end && HTTP_IS_SPHT(*next))
5071 next++;
5072 }
5073 else {
5074 /* Remove useless spaces before the old delimiter. */
5075 while (HTTP_IS_SPHT(*(prev-1)))
5076 prev--;
5077 *from = prev;
5078
5079 /* copy the delimiter and if possible a space if we're
5080 * not at the end of the line.
5081 */
5082 if (next < end) {
5083 *prev++ = *next++;
5084 if (prev + 1 < next)
5085 *prev++ = ' ';
5086 while (next < end && HTTP_IS_SPHT(*next))
5087 next++;
5088 }
5089 }
5090 memmove(prev, next, end - next);
5091 return (prev - next);
5092}
5093
Christopher Faulet0f226952018-10-22 09:29:56 +02005094
5095/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005096 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005097 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005098static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005099{
5100 struct ist dst = ist2(str, 0);
5101
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005102 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005103 goto end;
5104 if (dst.len + 1 > len)
5105 goto end;
5106 dst.ptr[dst.len++] = ' ';
5107
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005108 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005109 goto end;
5110 if (dst.len + 1 > len)
5111 goto end;
5112 dst.ptr[dst.len++] = ' ';
5113
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005114 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005115 end:
5116 return dst.len;
5117}
5118
5119/*
5120 * Print a debug line with a start line.
5121 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005122static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005123{
5124 struct session *sess = strm_sess(s);
5125 int max;
5126
5127 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5128 dir,
5129 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5130 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5131
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005132 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005133 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005134 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005135 trash.area[trash.data++] = ' ';
5136
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005137 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005138 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005139 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005140 trash.area[trash.data++] = ' ';
5141
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005142 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005143 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005144 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005145 trash.area[trash.data++] = '\n';
5146
5147 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5148}
5149
5150/*
5151 * Print a debug line with a header.
5152 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005153static 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 +02005154{
5155 struct session *sess = strm_sess(s);
5156 int max;
5157
5158 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5159 dir,
5160 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5161 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5162
5163 max = n.len;
5164 UBOUND(max, trash.size - trash.data - 3);
5165 chunk_memcat(&trash, n.ptr, max);
5166 trash.area[trash.data++] = ':';
5167 trash.area[trash.data++] = ' ';
5168
5169 max = v.len;
5170 UBOUND(max, trash.size - trash.data - 1);
5171 chunk_memcat(&trash, v.ptr, max);
5172 trash.area[trash.data++] = '\n';
5173
5174 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5175}
5176
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005177/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5178 * In case of allocation failure, everything allocated is freed and NULL is
5179 * returned. Otherwise the new transaction is assigned to the stream and
5180 * returned.
5181 */
5182struct http_txn *http_alloc_txn(struct stream *s)
5183{
5184 struct http_txn *txn = s->txn;
5185
5186 if (txn)
5187 return txn;
5188
5189 txn = pool_alloc(pool_head_http_txn);
5190 if (!txn)
5191 return txn;
5192
5193 s->txn = txn;
5194 return txn;
5195}
5196
5197void http_txn_reset_req(struct http_txn *txn)
5198{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005199 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005200 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5201}
5202
5203void http_txn_reset_res(struct http_txn *txn)
5204{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005205 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005206 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5207}
5208
5209/*
5210 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5211 * the required fields are properly allocated and that we only need to (re)init
5212 * them. This should be used before processing any new request.
5213 */
5214void http_init_txn(struct stream *s)
5215{
5216 struct http_txn *txn = s->txn;
5217 struct conn_stream *cs = objt_cs(s->si[0].end);
5218
5219 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5220 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5221 : 0);
5222 txn->status = -1;
5223 *(unsigned int *)txn->cache_hash = 0;
5224
5225 txn->cookie_first_date = 0;
5226 txn->cookie_last_date = 0;
5227
5228 txn->srv_cookie = NULL;
5229 txn->cli_cookie = NULL;
5230 txn->uri = NULL;
5231
5232 http_txn_reset_req(txn);
5233 http_txn_reset_res(txn);
5234
5235 txn->req.chn = &s->req;
5236 txn->rsp.chn = &s->res;
5237
5238 txn->auth.method = HTTP_AUTH_UNKNOWN;
5239
5240 vars_init(&s->vars_txn, SCOPE_TXN);
5241 vars_init(&s->vars_reqres, SCOPE_REQ);
5242}
5243
5244/* to be used at the end of a transaction */
5245void http_end_txn(struct stream *s)
5246{
5247 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005248
5249 /* these ones will have been dynamically allocated */
5250 pool_free(pool_head_requri, txn->uri);
5251 pool_free(pool_head_capture, txn->cli_cookie);
5252 pool_free(pool_head_capture, txn->srv_cookie);
5253 pool_free(pool_head_uniqueid, s->unique_id);
5254
5255 s->unique_id = NULL;
5256 txn->uri = NULL;
5257 txn->srv_cookie = NULL;
5258 txn->cli_cookie = NULL;
5259
Christopher Faulet59399252019-11-07 14:27:52 +01005260 if (!LIST_ISEMPTY(&s->vars_txn.head))
5261 vars_prune(&s->vars_txn, s->sess, s);
5262 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5263 vars_prune(&s->vars_reqres, s->sess, s);
5264}
5265
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005266
5267DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
5268DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
Christopher Faulet0f226952018-10-22 09:29:56 +02005269
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005270__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005271static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005272{
5273}
5274
5275
5276/*
5277 * Local variables:
5278 * c-indent-level: 8
5279 * c-basic-offset: 8
5280 * End:
5281 */