blob: 171ddfdc26773f927502ccc17bff73f1f6ecea0b [file] [log] [blame]
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Christopher Faulete0768eb2018-10-03 16:38:02 +020013#include <common/base64.h>
14#include <common/config.h>
15#include <common/debug.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010016#include <common/htx.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020017#include <common/uri_auth.h>
18
Christopher Faulet0f226952018-10-22 09:29:56 +020019#include <types/capture.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020020
21#include <proto/acl.h>
Christopher Faulet3e964192018-10-24 11:39:23 +020022#include <proto/action.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020023#include <proto/channel.h>
24#include <proto/checks.h>
25#include <proto/connection.h>
26#include <proto/filters.h>
Christopher Faulet0f226952018-10-22 09:29:56 +020027#include <proto/http_htx.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020028#include <proto/log.h>
Christopher Faulet3e964192018-10-24 11:39:23 +020029#include <proto/pattern.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020030#include <proto/http_ana.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020031#include <proto/proxy.h>
Christopher Fauletfefc73d2018-10-24 21:18:04 +020032#include <proto/server.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020033#include <proto/stream.h>
34#include <proto/stream_interface.h>
35#include <proto/stats.h>
Christopher Fauleta8a46e22019-07-16 14:53:09 +020036#include <proto/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020037
Christopher Fauleteea8fc72019-11-05 16:18:10 +010038#define TRACE_SOURCE &trace_strm
39
Christopher Faulet377c5a52018-10-24 21:21:30 +020040extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020041
Christopher Fauleta8a46e22019-07-16 14:53:09 +020042struct pool_head *pool_head_requri = NULL;
43struct pool_head *pool_head_capture = NULL;
44
45
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020046static void http_end_request(struct stream *s);
47static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020048
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020049static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
50static int http_del_hdr_value(char *start, char *end, char **from, char *next);
51static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020052static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
53static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
Christopher Faulet0f226952018-10-22 09:29:56 +020054
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020055static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status);
56static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020057
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020058static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
59static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020060
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020061static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
62static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020063
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020064static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
65static int http_reply_100_continue(struct stream *s);
66static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm);
Christopher Faulet23a3c792018-11-28 10:01:23 +010067
Christopher Faulete0768eb2018-10-03 16:38:02 +020068/* This stream analyser waits for a complete HTTP request. It returns 1 if the
69 * processing can continue on next analysers, or zero if it either needs more
70 * data or wants to immediately abort the request (eg: timeout, error, ...). It
71 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
72 * when it has nothing left to do, and may remove any analyser when it wants to
73 * abort.
74 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020075int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020076{
Christopher Faulet9768c262018-10-22 09:34:31 +020077
Christopher Faulete0768eb2018-10-03 16:38:02 +020078 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020079 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020080 *
Christopher Faulet9768c262018-10-22 09:34:31 +020081 * Once the start line and all headers are received, we may perform a
82 * capture of the error (if any), and we will set a few fields. We also
83 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020084 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020085 struct session *sess = s->sess;
86 struct http_txn *txn = s->txn;
87 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020088 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010089 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020090
Christopher Fauleteea8fc72019-11-05 16:18:10 +010091 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020092
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010093 htx = htxbuf(&req->buf);
Christopher Faulet9768c262018-10-22 09:34:31 +020094
Willy Tarreau4236f032019-03-05 10:43:32 +010095 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +020096 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +010097 stream_inc_http_req_ctr(s);
98 stream_inc_http_err_ctr(s);
99 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200100 if (htx->flags & HTX_FL_PARSING_ERROR)
101 goto return_bad_req;
102 else
103 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100104 }
105
Christopher Faulete0768eb2018-10-03 16:38:02 +0200106 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200107 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200108
109 /* If there is data available for analysis, log the end of the idle time. */
Christopher Faulet870aad92018-11-29 15:23:46 +0100110 if (c_data(req) && s->logs.t_idle == -1) {
111 const struct cs_info *csinfo = si_get_cs_info(objt_cs(s->si[0].end));
112
113 s->logs.t_idle = ((csinfo)
114 ? csinfo->t_idle
115 : tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake);
116 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200117
Christopher Faulete0768eb2018-10-03 16:38:02 +0200118 /*
119 * Now we quickly check if we have found a full valid request.
120 * If not so, we check the FD and buffer states before leaving.
121 * A full request is indicated by the fact that we have seen
122 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
123 * requests are checked first. When waiting for a second request
124 * on a keep-alive stream, if we encounter and error, close, t/o,
125 * we note the error in the stream flags but don't set any state.
126 * Since the error will be noted there, it will not be counted by
127 * process_stream() as a frontend error.
128 * Last, we may increase some tracked counters' http request errors on
129 * the cases that are deliberately the client's fault. For instance,
130 * a timeout or connection reset is not counted as an error. However
131 * a bad request is.
132 */
Christopher Faulet29f17582019-05-23 11:03:26 +0200133 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200134 if (htx->flags & HTX_FL_UPGRADE)
135 goto failed_keep_alive;
136
Christopher Faulet9768c262018-10-22 09:34:31 +0200137 /* 1: have we encountered a read error ? */
138 if (req->flags & CF_READ_ERROR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200139 if (!(s->flags & SF_ERR_MASK))
140 s->flags |= SF_ERR_CLICL;
141
142 if (txn->flags & TX_WAIT_NEXT_RQ)
143 goto failed_keep_alive;
144
145 if (sess->fe->options & PR_O_IGNORE_PRB)
146 goto failed_keep_alive;
147
Christopher Faulet9768c262018-10-22 09:34:31 +0200148 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200149 stream_inc_http_req_ctr(s);
150 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100151 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200152 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100153 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200154
Christopher Faulet9768c262018-10-22 09:34:31 +0200155 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200156 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet9768c262018-10-22 09:34:31 +0200157 req->analysers &= AN_REQ_FLT_END;
158
Christopher Faulete0768eb2018-10-03 16:38:02 +0200159 if (!(s->flags & SF_FINST_MASK))
160 s->flags |= SF_FINST_R;
161 return 0;
162 }
163
Christopher Faulet9768c262018-10-22 09:34:31 +0200164 /* 2: has the read timeout expired ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200165 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
166 if (!(s->flags & SF_ERR_MASK))
167 s->flags |= SF_ERR_CLITO;
168
169 if (txn->flags & TX_WAIT_NEXT_RQ)
170 goto failed_keep_alive;
171
172 if (sess->fe->options & PR_O_IGNORE_PRB)
173 goto failed_keep_alive;
174
Christopher Faulet9768c262018-10-22 09:34:31 +0200175 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200176 stream_inc_http_req_ctr(s);
177 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100178 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200179 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100180 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200181
Christopher Faulet9768c262018-10-22 09:34:31 +0200182 txn->status = 408;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200183 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200184 req->analysers &= AN_REQ_FLT_END;
185
Christopher Faulete0768eb2018-10-03 16:38:02 +0200186 if (!(s->flags & SF_FINST_MASK))
187 s->flags |= SF_FINST_R;
188 return 0;
189 }
190
Christopher Faulet9768c262018-10-22 09:34:31 +0200191 /* 3: have we encountered a close ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200192 else if (req->flags & CF_SHUTR) {
193 if (!(s->flags & SF_ERR_MASK))
194 s->flags |= SF_ERR_CLICL;
195
196 if (txn->flags & TX_WAIT_NEXT_RQ)
197 goto failed_keep_alive;
198
199 if (sess->fe->options & PR_O_IGNORE_PRB)
200 goto failed_keep_alive;
201
Christopher Faulete0768eb2018-10-03 16:38:02 +0200202 stream_inc_http_err_ctr(s);
203 stream_inc_http_req_ctr(s);
204 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100205 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200206 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100207 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200208
Christopher Faulet9768c262018-10-22 09:34:31 +0200209 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200210 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200211 req->analysers &= AN_REQ_FLT_END;
212
Christopher Faulete0768eb2018-10-03 16:38:02 +0200213 if (!(s->flags & SF_FINST_MASK))
214 s->flags |= SF_FINST_R;
215 return 0;
216 }
217
218 channel_dont_connect(req);
219 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
220 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100221
Christopher Faulet9768c262018-10-22 09:34:31 +0200222 if (sess->listener->options & LI_O_NOQUICKACK && htx_is_not_empty(htx) &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200223 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
224 /* We need more data, we have to re-enable quick-ack in case we
225 * previously disabled it, otherwise we might cause the client
226 * to delay next data.
227 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100228 conn_set_quickack(objt_conn(sess->origin), 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200229 }
Willy Tarreau1a18b542018-12-11 16:37:42 +0100230
Christopher Faulet47365272018-10-31 17:40:50 +0100231 if ((req->flags & CF_READ_PARTIAL) && (txn->flags & TX_WAIT_NEXT_RQ)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200232 /* If the client starts to talk, let's fall back to
233 * request timeout processing.
234 */
235 txn->flags &= ~TX_WAIT_NEXT_RQ;
236 req->analyse_exp = TICK_ETERNITY;
237 }
238
239 /* just set the request timeout once at the beginning of the request */
240 if (!tick_isset(req->analyse_exp)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100241 if ((txn->flags & TX_WAIT_NEXT_RQ) && tick_isset(s->be->timeout.httpka))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200242 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
243 else
244 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
245 }
246
247 /* we're not ready yet */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100248 DBG_TRACE_DEVEL("waiting for the request",
249 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200250 return 0;
251
252 failed_keep_alive:
253 /* Here we process low-level errors for keep-alive requests. In
254 * short, if the request is not the first one and it experiences
255 * a timeout, read error or shutdown, we just silently close so
256 * that the client can try again.
257 */
258 txn->status = 0;
259 msg->msg_state = HTTP_MSG_RQBEFORE;
260 req->analysers &= AN_REQ_FLT_END;
261 s->logs.logwait = 0;
262 s->logs.level = 0;
263 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200264 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100265 DBG_TRACE_DEVEL("leaving by closing K/A connection",
266 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200267 return 0;
268 }
269
Christopher Faulet9768c262018-10-22 09:34:31 +0200270 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200271 stream_inc_http_req_ctr(s);
272 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
273
Christopher Faulet9768c262018-10-22 09:34:31 +0200274 /* kill the pending keep-alive timeout */
275 txn->flags &= ~TX_WAIT_NEXT_RQ;
276 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200277
Christopher Faulet29f17582019-05-23 11:03:26 +0200278 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200279 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100280
Christopher Faulet9768c262018-10-22 09:34:31 +0200281 /* 0: we might have to print this header in debug mode */
282 if (unlikely((global.mode & MODE_DEBUG) &&
283 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
284 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200285
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200286 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200287
Christopher Fauleta3f15502019-05-13 15:27:23 +0200288 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200289 struct htx_blk *blk = htx_get_blk(htx, pos);
290 enum htx_blk_type type = htx_get_blk_type(blk);
291
292 if (type == HTX_BLK_EOH)
293 break;
294 if (type != HTX_BLK_HDR)
295 continue;
296
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200297 http_debug_hdr("clihdr", s,
298 htx_get_blk_name(htx, blk),
299 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200300 }
301 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200302
303 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100304 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200305 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100306 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100307 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200308 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100309 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +0100310 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100311 if (sl->flags & HTX_SL_F_BODYLESS)
312 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200313
314 /* we can make use of server redirect on GET and HEAD */
315 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
316 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100317 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200318 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200319 goto return_bad_req;
320 }
321
322 /*
323 * 2: check if the URI matches the monitor_uri.
324 * We have to do this for every request which gets in, because
325 * the monitor-uri is defined by the frontend.
326 */
327 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100328 isteqi(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200329 /*
330 * We have found the monitor URI
331 */
332 struct acl_cond *cond;
333
334 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100335 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200336
337 /* Check if we want to fail this monitor request or not */
338 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
339 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
340
341 ret = acl_pass(ret);
342 if (cond->pol == ACL_COND_UNLESS)
343 ret = !ret;
344
345 if (ret) {
346 /* we fail this request, let's return 503 service unavail */
347 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200348 if (!(s->flags & SF_ERR_MASK))
349 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
350 goto return_prx_cond;
351 }
352 }
353
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800354 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200355 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200356 if (!(s->flags & SF_ERR_MASK))
357 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
358 goto return_prx_cond;
359 }
360
361 /*
362 * 3: Maybe we have to copy the original REQURI for the logs ?
363 * Note: we cannot log anymore if the request has been
364 * classified as invalid.
365 */
366 if (unlikely(s->logs.logwait & LW_REQ)) {
367 /* we have a complete HTTP request that we must log */
368 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200369 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200370
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200371 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200372 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200373
374 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
375 s->do_log(s);
376 } else {
377 ha_alert("HTTP logging : out of memory.\n");
378 }
379 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200380
Christopher Faulete0768eb2018-10-03 16:38:02 +0200381 /* if the frontend has "option http-use-proxy-header", we'll check if
382 * we have what looks like a proxied connection instead of a connection,
383 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
384 * Note that this is *not* RFC-compliant, however browsers and proxies
385 * happen to do that despite being non-standard :-(
386 * We consider that a request not beginning with either '/' or '*' is
387 * a proxied connection, which covers both "scheme://location" and
388 * CONNECT ip:port.
389 */
390 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100391 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200392 txn->flags |= TX_USE_PX_CONN;
393
Christopher Faulete0768eb2018-10-03 16:38:02 +0200394 /* 5: we may need to capture headers */
395 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200396 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200397
Christopher Faulete0768eb2018-10-03 16:38:02 +0200398 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200399 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200400 req->analysers |= AN_REQ_HTTP_BODY;
401
402 /*
403 * RFC7234#4:
404 * A cache MUST write through requests with methods
405 * that are unsafe (Section 4.2.1 of [RFC7231]) to
406 * the origin server; i.e., a cache is not allowed
407 * to generate a reply to such a request before
408 * having forwarded the request and having received
409 * a corresponding response.
410 *
411 * RFC7231#4.2.1:
412 * Of the request methods defined by this
413 * specification, the GET, HEAD, OPTIONS, and TRACE
414 * methods are defined to be safe.
415 */
416 if (likely(txn->meth == HTTP_METH_GET ||
417 txn->meth == HTTP_METH_HEAD ||
418 txn->meth == HTTP_METH_OPTIONS ||
419 txn->meth == HTTP_METH_TRACE))
420 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
421
422 /* end of job, return OK */
423 req->analysers &= ~an_bit;
424 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200425
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100426 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200427 return 1;
428
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200429 return_int_err:
430 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200431 if (!(s->flags & SF_ERR_MASK))
432 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100433 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200434 if (sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100435 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200436 goto return_prx_cond;
437
Christopher Faulete0768eb2018-10-03 16:38:02 +0200438 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200439 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100440 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200441 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100442 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200443 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200444
445 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200446 http_reply_and_close(s, txn->status, http_error_message(s));
447
Christopher Faulete0768eb2018-10-03 16:38:02 +0200448 if (!(s->flags & SF_ERR_MASK))
449 s->flags |= SF_ERR_PRXCOND;
450 if (!(s->flags & SF_FINST_MASK))
451 s->flags |= SF_FINST_R;
452
453 req->analysers &= AN_REQ_FLT_END;
454 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100455 DBG_TRACE_DEVEL("leaving on error",
456 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200457 return 0;
458}
459
460
461/* This stream analyser runs all HTTP request processing which is common to
462 * frontends and backends, which means blocking ACLs, filters, connection-close,
463 * reqadd, stats and redirects. This is performed for the designated proxy.
464 * It returns 1 if the processing can continue on next analysers, or zero if it
465 * either needs more data or wants to immediately abort the request (eg: deny,
466 * error, ...).
467 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200468int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200469{
470 struct session *sess = s->sess;
471 struct http_txn *txn = s->txn;
472 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200473 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200474 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200475 enum rule_result verdict;
476 int deny_status = HTTP_ERR_403;
477 struct connection *conn = objt_conn(sess->origin);
478
479 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
480 /* we need more data */
481 goto return_prx_yield;
482 }
483
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100484 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200485
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100486 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200487
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200488 /* just in case we have some per-backend tracking. Only called the first
489 * execution of the analyser. */
490 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
491 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200492
493 /* evaluate http-request rules */
494 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200495 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200496
497 switch (verdict) {
498 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
499 goto return_prx_yield;
500
501 case HTTP_RULE_RES_CONT:
502 case HTTP_RULE_RES_STOP: /* nothing to do */
503 break;
504
505 case HTTP_RULE_RES_DENY: /* deny or tarpit */
506 if (txn->flags & TX_CLTARPIT)
507 goto tarpit;
508 goto deny;
509
510 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
511 goto return_prx_cond;
512
513 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
514 goto done;
515
516 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
517 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100518
519 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
520 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200521 }
522 }
523
524 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
525 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200526 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200527
Christopher Fauletff2759f2018-10-24 11:13:16 +0200528 ctx.blk = NULL;
529 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
530 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100531 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200532 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200533 }
534
535 /* OK at this stage, we know that the request was accepted according to
536 * the http-request rules, we can check for the stats. Note that the
537 * URI is detected *before* the req* rules in order not to be affected
538 * by a possible reqrep, while they are processed *after* so that a
539 * reqdeny can still block them. This clearly needs to change in 1.6!
540 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200541 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200542 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100543 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200544 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200545 if (!(s->flags & SF_ERR_MASK))
546 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100547 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200548 }
549
550 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200551 http_handle_stats(s, req);
552 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200553 /* not all actions implemented: deny, allow, auth */
554
555 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
556 goto deny;
557
558 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
559 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100560
561 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
562 goto return_bad_req;
563
564 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
565 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200566 }
567
Christopher Faulet2571bc62019-03-01 11:44:26 +0100568 /* Proceed with the applets now. */
569 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200570 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100571 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200572
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200573 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100574 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100575
Christopher Faulete0768eb2018-10-03 16:38:02 +0200576 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
577 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
578 if (!(s->flags & SF_FINST_MASK))
579 s->flags |= SF_FINST_R;
580
581 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
582 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
583 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
584 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100585
586 req->flags |= CF_SEND_DONTWAIT;
587 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200588 goto done;
589 }
590
591 /* check whether we have some ACLs set to redirect this request */
592 list_for_each_entry(rule, &px->redirect_rules, list) {
593 if (rule->cond) {
594 int ret;
595
596 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
597 ret = acl_pass(ret);
598 if (rule->cond->pol == ACL_COND_UNLESS)
599 ret = !ret;
600 if (!ret)
601 continue;
602 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200603 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100604 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200605 goto done;
606 }
607
608 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
609 * If this happens, then the data will not come immediately, so we must
610 * send all what we have without waiting. Note that due to the small gain
611 * in waiting for the body of the request, it's easier to simply put the
612 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
613 * itself once used.
614 */
615 req->flags |= CF_SEND_DONTWAIT;
616
617 done: /* done with this analyser, continue with next ones that the calling
618 * points will have set, if any.
619 */
620 req->analyse_exp = TICK_ETERNITY;
621 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
622 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100623 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200624 return 1;
625
626 tarpit:
627 /* Allow cookie logging
628 */
629 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200630 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200631
632 /* When a connection is tarpitted, we use the tarpit timeout,
633 * which may be the same as the connect timeout if unspecified.
634 * If unset, then set it to zero because we really want it to
635 * eventually expire. We build the tarpit as an analyser.
636 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100637 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200638
639 /* wipe the request out so that we can drop the connection early
640 * if the client closes first.
641 */
642 channel_dont_connect(req);
643
644 txn->status = http_err_codes[deny_status];
645
646 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
647 req->analysers |= AN_REQ_HTTP_TARPIT;
648 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
649 if (!req->analyse_exp)
650 req->analyse_exp = tick_add(now_ms, 0);
651 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100652 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100653 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100654 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200655 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100656 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200657 goto done_without_exp;
658
659 deny: /* this request was blocked (denied) */
660
661 /* Allow cookie logging
662 */
663 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200664 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200665
666 txn->flags |= TX_CLDENY;
667 txn->status = http_err_codes[deny_status];
668 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200669 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100670 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100671 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100672 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200673 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100674 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100675 goto return_prx_err;
676
677 return_int_err:
678 txn->status = 500;
679 if (!(s->flags & SF_ERR_MASK))
680 s->flags |= SF_ERR_INTERNAL;
681 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100682 if (s->flags & SF_BE_ASSIGNED)
683 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100684 if (sess->listener->counters)
685 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
686 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200687
688 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200689 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100690 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200691 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100692 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100693 /* fall through */
694
695 return_prx_err:
696 http_reply_and_close(s, txn->status, http_error_message(s));
697 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200698
699 return_prx_cond:
700 if (!(s->flags & SF_ERR_MASK))
701 s->flags |= SF_ERR_PRXCOND;
702 if (!(s->flags & SF_FINST_MASK))
703 s->flags |= SF_FINST_R;
704
705 req->analysers &= AN_REQ_FLT_END;
706 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100707 DBG_TRACE_DEVEL("leaving on error",
708 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200709 return 0;
710
711 return_prx_yield:
712 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100713 DBG_TRACE_DEVEL("waiting for more data",
714 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200715 return 0;
716}
717
718/* This function performs all the processing enabled for the current request.
719 * It returns 1 if the processing can continue on next analysers, or zero if it
720 * needs more data, encounters an error, or wants to immediately abort the
721 * request. It relies on buffers flags, and updates s->req.analysers.
722 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200723int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200724{
725 struct session *sess = s->sess;
726 struct http_txn *txn = s->txn;
727 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200728 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200729 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
730
731 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
732 /* we need more data */
733 channel_dont_connect(req);
734 return 0;
735 }
736
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100737 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200738
739 /*
740 * Right now, we know that we have processed the entire headers
741 * and that unwanted requests have been filtered out. We can do
742 * whatever we want with the remaining request. Also, now we
743 * may have separate values for ->fe, ->be.
744 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100745 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200746
747 /*
748 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200749 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200750 */
751 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100752 struct htx_sl *sl;
753 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200754
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200755 if (!sockaddr_alloc(&s->target_addr)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200756 if (!(s->flags & SF_ERR_MASK))
757 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100758 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200759 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200760 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100761 uri = htx_sl_req_uri(sl);
762 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200763
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200764 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200765 goto return_bad_req;
766
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200767 s->target = &s->be->obj_type;
768 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
769
Christopher Faulete0768eb2018-10-03 16:38:02 +0200770 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200771 * uri.ptr and path.ptr (excluded). If it was not found, we need
772 * to replace from all the uri by a single "/".
773 *
774 * Instead of rewritting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100775 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200776 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200777 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100778 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200779 }
780
781 /*
782 * 7: Now we can work with the cookies.
783 * Note that doing so might move headers in the request, but
784 * the fields will stay coherent and the URI will not move.
785 * This should only be performed in the backend.
786 */
787 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200788 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200789
790 /* add unique-id if "header-unique-id" is specified */
791
792 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100793 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) {
794 if (!(s->flags & SF_ERR_MASK))
795 s->flags |= SF_ERR_RESOURCE;
796 goto return_int_err;
797 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200798 s->unique_id[0] = '\0';
799 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
800 }
801
802 if (sess->fe->header_unique_id && s->unique_id) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200803 struct ist n = ist2(sess->fe->header_unique_id, strlen(sess->fe->header_unique_id));
804 struct ist v = ist2(s->unique_id, strlen(s->unique_id));
805
806 if (unlikely(!http_add_header(htx, n, v)))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100807 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200808 }
809
810 /*
811 * 9: add X-Forwarded-For if either the frontend or the backend
812 * asks for it.
813 */
814 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200815 struct http_hdr_ctx ctx = { .blk = NULL };
816 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
817 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
818
Christopher Faulete0768eb2018-10-03 16:38:02 +0200819 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200820 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200821 /* The header is set to be added only if none is present
822 * and we found it, so don't do anything.
823 */
824 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200825 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200826 /* Add an X-Forwarded-For header unless the source IP is
827 * in the 'except' network range.
828 */
829 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200830 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200831 != sess->fe->except_net.s_addr) &&
832 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200833 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200834 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200835 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200836
837 /* Note: we rely on the backend to get the header name to be used for
838 * x-forwarded-for, because the header is really meant for the backends.
839 * However, if the backend did not specify any option, we have to rely
840 * on the frontend's header name.
841 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200842 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
843 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100844 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200845 }
846 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200847 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200848 /* FIXME: for the sake of completeness, we should also support
849 * 'except' here, although it is mostly useless in this case.
850 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200851 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200852
Christopher Faulete0768eb2018-10-03 16:38:02 +0200853 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200854 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200855 pn, sizeof(pn));
856
857 /* Note: we rely on the backend to get the header name to be used for
858 * x-forwarded-for, because the header is really meant for the backends.
859 * However, if the backend did not specify any option, we have to rely
860 * on the frontend's header name.
861 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200862 chunk_printf(&trash, "%s", pn);
863 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100864 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200865 }
866 }
867
868 /*
869 * 10: add X-Original-To if either the frontend or the backend
870 * asks for it.
871 */
872 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
873
874 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200875 if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET && conn_get_dst(cli_conn)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200876 /* Add an X-Original-To header unless the destination IP is
877 * in the 'except' network range.
878 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200879 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200880 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200881 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200882 != sess->fe->except_to.s_addr) &&
883 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200884 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200885 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200886 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200887 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200888
889 /* Note: we rely on the backend to get the header name to be used for
890 * x-original-to, because the header is really meant for the backends.
891 * However, if the backend did not specify any option, we have to rely
892 * on the frontend's header name.
893 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200894 if (s->be->orgto_hdr_len)
895 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
896 else
897 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200898
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200899 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
900 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100901 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200902 }
903 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200904 }
905
Christopher Faulete0768eb2018-10-03 16:38:02 +0200906 /* If we have no server assigned yet and we're balancing on url_param
907 * with a POST request, we may be interested in checking the body for
908 * that parameter. This will be done in another analyser.
909 */
910 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100911 s->txn->meth == HTTP_METH_POST &&
912 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200913 channel_dont_connect(req);
914 req->analysers |= AN_REQ_HTTP_BODY;
915 }
916
917 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
918 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100919
Christopher Faulete0768eb2018-10-03 16:38:02 +0200920 /* We expect some data from the client. Unless we know for sure
921 * we already have a full request, we have to re-enable quick-ack
922 * in case we previously disabled it, otherwise we might cause
923 * the client to delay further data.
924 */
925 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200926 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100927 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200928
929 /*************************************************************
930 * OK, that's finished for the headers. We have done what we *
931 * could. Let's switch to the DATA state. *
932 ************************************************************/
933 req->analyse_exp = TICK_ETERNITY;
934 req->analysers &= ~an_bit;
935
936 s->logs.tv_request = now;
937 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100938 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200939 return 1;
940
Christopher Fauletb8a53712019-12-16 11:29:38 +0100941 return_int_err:
942 txn->status = 500;
943 if (!(s->flags & SF_ERR_MASK))
944 s->flags |= SF_ERR_INTERNAL;
945 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100946 if (s->flags & SF_BE_ASSIGNED)
947 _HA_ATOMIC_ADD(&sess->fe->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100948 if (sess->listener->counters)
949 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
950 goto return_prx_cond;
951
Christopher Faulete0768eb2018-10-03 16:38:02 +0200952 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200953 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100954 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200955 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100956 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100957 /* fall through */
958
959 return_prx_cond:
960 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200961
962 if (!(s->flags & SF_ERR_MASK))
963 s->flags |= SF_ERR_PRXCOND;
964 if (!(s->flags & SF_FINST_MASK))
965 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100966
967 req->analysers &= AN_REQ_FLT_END;
968 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100969 DBG_TRACE_DEVEL("leaving on error",
970 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200971 return 0;
972}
973
974/* This function is an analyser which processes the HTTP tarpit. It always
975 * returns zero, at the beginning because it prevents any other processing
976 * from occurring, and at the end because it terminates the request.
977 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200978int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200979{
980 struct http_txn *txn = s->txn;
981
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100982 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200983 /* This connection is being tarpitted. The CLIENT side has
984 * already set the connect expiration date to the right
985 * timeout. We just have to check that the client is still
986 * there and that the timeout has not expired.
987 */
988 channel_dont_connect(req);
989 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100990 !tick_is_expired(req->analyse_exp, now_ms)) {
991 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
992 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200993 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100994 }
995
Christopher Faulete0768eb2018-10-03 16:38:02 +0200996
997 /* We will set the queue timer to the time spent, just for
998 * logging purposes. We fake a 500 server error, so that the
999 * attacker will not suspect his connection has been tarpitted.
1000 * It will not cause trouble to the logs because we can exclude
1001 * the tarpitted connections by filtering on the 'PT' status flags.
1002 */
1003 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1004
1005 if (!(req->flags & CF_READ_ERROR))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001006 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001007
1008 req->analysers &= AN_REQ_FLT_END;
1009 req->analyse_exp = TICK_ETERNITY;
1010
1011 if (!(s->flags & SF_ERR_MASK))
1012 s->flags |= SF_ERR_PRXCOND;
1013 if (!(s->flags & SF_FINST_MASK))
1014 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001015
1016 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001017 return 0;
1018}
1019
1020/* This function is an analyser which waits for the HTTP request body. It waits
1021 * for either the buffer to be full, or the full advertised contents to have
1022 * reached the buffer. It must only be called after the standard HTTP request
1023 * processing has occurred, because it expects the request to be parsed and will
1024 * look for the Expect header. It may send a 100-Continue interim response. It
1025 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
1026 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
1027 * needs to read more data, or 1 once it has completed its analysis.
1028 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001029int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001030{
1031 struct session *sess = s->sess;
1032 struct http_txn *txn = s->txn;
1033 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001034 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001035
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001036 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001037
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001038 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001039
Willy Tarreau4236f032019-03-05 10:43:32 +01001040 if (htx->flags & HTX_FL_PARSING_ERROR)
1041 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001042 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1043 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001044
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001045 if (msg->msg_state < HTTP_MSG_BODY)
1046 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001047
Christopher Faulete0768eb2018-10-03 16:38:02 +02001048 /* We have to parse the HTTP request body to find any required data.
1049 * "balance url_param check_post" should have been the only way to get
1050 * into this. We were brought here after HTTP header analysis, so all
1051 * related structures are ready.
1052 */
1053
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001054 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001055 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001056 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001057 }
1058
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001059 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001060
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001061 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1062 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001063 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001064 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001065 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001066 goto http_end;
1067
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001068 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001069 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1070 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001071 if (!(s->flags & SF_ERR_MASK))
1072 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001073 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1074 if (sess->listener->counters)
1075 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1076 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001077 }
1078
1079 /* we get here if we need to wait for more data */
1080 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1081 /* Not enough data. We'll re-use the http-request
1082 * timeout here. Ideally, we should set the timeout
1083 * relative to the accept() date. We just set the
1084 * request timeout once at the beginning of the
1085 * request.
1086 */
1087 channel_dont_connect(req);
1088 if (!tick_isset(req->analyse_exp))
1089 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001090 DBG_TRACE_DEVEL("waiting for more data",
1091 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001092 return 0;
1093 }
1094
1095 http_end:
1096 /* The situation will not evolve, so let's give up on the analysis. */
1097 s->logs.tv_request = now; /* update the request timer to reflect full request */
1098 req->analysers &= ~an_bit;
1099 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001100 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001101 return 1;
1102
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001103 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001104 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001105 if (!(s->flags & SF_ERR_MASK))
1106 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001107 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001108 if (s->flags & SF_BE_ASSIGNED)
1109 _HA_ATOMIC_ADD(&sess->fe->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001110 if (sess->listener->counters)
1111 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
1112 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001113
Christopher Faulete0768eb2018-10-03 16:38:02 +02001114 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001115 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001116 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1117 if (sess->listener->counters)
1118 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1119 /* fall through */
1120
1121 return_prx_cond:
1122 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001123
1124 if (!(s->flags & SF_ERR_MASK))
1125 s->flags |= SF_ERR_PRXCOND;
1126 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001127 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001128
Christopher Faulete0768eb2018-10-03 16:38:02 +02001129 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001130 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001131 DBG_TRACE_DEVEL("leaving on error",
1132 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001133 return 0;
1134}
1135
1136/* This function is an analyser which forwards request body (including chunk
1137 * sizes if any). It is called as soon as we must forward, even if we forward
1138 * zero byte. The only situation where it must not be called is when we're in
1139 * tunnel mode and we want to forward till the close. It's used both to forward
1140 * remaining data and to resync after end of body. It expects the msg_state to
1141 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1142 * read more data, or 1 once we can go on with next request or end the stream.
1143 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1144 * bytes of pending data + the headers if not already done.
1145 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001146int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001147{
1148 struct session *sess = s->sess;
1149 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001150 struct http_msg *msg = &txn->req;
1151 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001152 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001153 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001154
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001155 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001156
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001157 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001158
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001159 if (htx->flags & HTX_FL_PARSING_ERROR)
1160 goto return_bad_req;
1161 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1162 goto return_int_err;
1163
Christopher Faulete0768eb2018-10-03 16:38:02 +02001164 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1165 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1166 /* Output closed while we were sending data. We must abort and
1167 * wake the other side up.
1168 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001169
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001170 /* Don't abort yet if we had L7 retries activated and it
1171 * was a write error, we may recover.
1172 */
1173 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001174 (s->si[1].flags & SI_FL_L7_RETRY)) {
1175 DBG_TRACE_DEVEL("leaving on L7 retry",
1176 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001177 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001178 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001179 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001180 http_end_request(s);
1181 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001182 DBG_TRACE_DEVEL("leaving on error",
1183 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001184 return 1;
1185 }
1186
1187 /* Note that we don't have to send 100-continue back because we don't
1188 * need the data to complete our job, and it's up to the server to
1189 * decide whether to return 100, 417 or anything else in return of
1190 * an "Expect: 100-continue" header.
1191 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001192 if (msg->msg_state == HTTP_MSG_BODY)
1193 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001194
Christopher Faulete0768eb2018-10-03 16:38:02 +02001195 /* in most states, we should abort in case of early close */
1196 channel_auto_close(req);
1197
1198 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001199 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001200 if (req->flags & CF_EOI)
1201 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001202 }
1203 else {
1204 /* We can't process the buffer's contents yet */
1205 req->flags |= CF_WAKE_WRITE;
1206 goto missing_data_or_waiting;
1207 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001208 }
1209
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001210 if (msg->msg_state >= HTTP_MSG_ENDING)
1211 goto ending;
1212
1213 if (txn->meth == HTTP_METH_CONNECT) {
1214 msg->msg_state = HTTP_MSG_ENDING;
1215 goto ending;
1216 }
1217
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001218 /* Forward input data. We get it by removing all outgoing data not
1219 * forwarded yet from HTX data size. If there are some data filters, we
1220 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001221 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001222 if (HAS_REQ_DATA_FILTERS(s)) {
1223 ret = flt_http_payload(s, msg, htx->data);
1224 if (ret < 0)
1225 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001226 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001227 }
1228 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001229 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001230 if (msg->flags & HTTP_MSGF_XFER_LEN)
1231 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001232 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001233
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001234 if (htx->data != co_data(req))
1235 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001236
Christopher Faulet9768c262018-10-22 09:34:31 +02001237 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001238 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1239 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001240 */
1241 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1242 goto missing_data_or_waiting;
1243
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001244 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001245
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001246 ending:
1247 /* other states, ENDING...TUNNEL */
1248 if (msg->msg_state >= HTTP_MSG_DONE)
1249 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001250
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001251 if (HAS_REQ_DATA_FILTERS(s)) {
1252 ret = flt_http_end(s, msg);
1253 if (ret <= 0) {
1254 if (!ret)
1255 goto missing_data_or_waiting;
1256 goto return_bad_req;
1257 }
1258 }
1259
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001260 if (txn->meth == HTTP_METH_CONNECT)
1261 msg->msg_state = HTTP_MSG_TUNNEL;
1262 else {
1263 msg->msg_state = HTTP_MSG_DONE;
1264 req->to_forward = 0;
1265 }
1266
1267 done:
1268 /* we don't want to forward closes on DONE except in tunnel mode. */
1269 if (!(txn->flags & TX_CON_WANT_TUN))
1270 channel_dont_close(req);
1271
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001272 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001273 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001274 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001275 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1276 if (req->flags & CF_SHUTW) {
1277 /* request errors are most likely due to the
1278 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001279 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001280 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001281 goto return_bad_req;
1282 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001283 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001284 return 1;
1285 }
1286
1287 /* If "option abortonclose" is set on the backend, we want to monitor
1288 * the client's connection and forward any shutdown notification to the
1289 * server, which will decide whether to close or to go on processing the
1290 * request. We only do that in tunnel mode, and not in other modes since
1291 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001292 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001293 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001294 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001295 s->si[1].flags |= SI_FL_NOLINGER;
1296 channel_auto_close(req);
1297 }
1298 else if (s->txn->meth == HTTP_METH_POST) {
1299 /* POST requests may require to read extra CRLF sent by broken
1300 * browsers and which could cause an RST to be sent upon close
1301 * on some systems (eg: Linux). */
1302 channel_auto_read(req);
1303 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001304 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1305 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001306 return 0;
1307
1308 missing_data_or_waiting:
1309 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001310 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001311 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001312
1313 waiting:
1314 /* waiting for the last bits to leave the buffer */
1315 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001316 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001317
1318 /* When TE: chunked is used, we need to get there again to parse remaining
1319 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1320 * And when content-length is used, we never want to let the possible
1321 * shutdown be forwarded to the other side, as the state machine will
1322 * take care of it once the client responds. It's also important to
1323 * prevent TIME_WAITs from accumulating on the backend side, and for
1324 * HTTP/2 where the last frame comes with a shutdown.
1325 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001326 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001327 channel_dont_close(req);
1328
1329 /* We know that more data are expected, but we couldn't send more that
1330 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1331 * system knows it must not set a PUSH on this first part. Interactive
1332 * modes are already handled by the stream sock layer. We must not do
1333 * this in content-length mode because it could present the MSG_MORE
1334 * flag with the last block of forwarded data, which would cause an
1335 * additional delay to be observed by the receiver.
1336 */
1337 if (msg->flags & HTTP_MSGF_TE_CHNK)
1338 req->flags |= CF_EXPECT_MORE;
1339
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001340 DBG_TRACE_DEVEL("waiting for more data to forward",
1341 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001342 return 0;
1343
Christopher Faulet93e02d82019-03-08 14:18:50 +01001344 return_cli_abort:
1345 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1346 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001347 if (sess->listener->counters)
1348 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001349 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001350 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001351 if (!(s->flags & SF_ERR_MASK))
1352 s->flags |= SF_ERR_CLICL;
1353 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001354 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001355
1356 return_srv_abort:
1357 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1358 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001359 if (sess->listener->counters)
1360 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001361 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001362 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001363 if (!(s->flags & SF_ERR_MASK))
1364 s->flags |= SF_ERR_SRVCL;
1365 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001366 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001367
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001368 return_int_err:
1369 if (!(s->flags & SF_ERR_MASK))
1370 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001371 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001372 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001373 if (sess->listener->counters)
1374 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001375 if (objt_server(s->target))
1376 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001377 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001378 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001379
Christopher Faulet93e02d82019-03-08 14:18:50 +01001380 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001381 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001382 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001383 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001384 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001385 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001386
Christopher Fauletb8a53712019-12-16 11:29:38 +01001387 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001388 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001389 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001390 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001391 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001392 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001393 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001394 }
1395 req->analysers &= AN_REQ_FLT_END;
1396 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001397 if (!(s->flags & SF_ERR_MASK))
1398 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001399 if (!(s->flags & SF_FINST_MASK))
1400 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001401 DBG_TRACE_DEVEL("leaving on error ",
1402 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001403 return 0;
1404}
1405
Olivier Houcharda254a372019-04-05 15:30:12 +02001406/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1407/* Returns 0 if we can attempt to retry, -1 otherwise */
1408static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1409{
1410 struct channel *req, *res;
1411 int co_data;
1412
1413 si->conn_retries--;
1414 if (si->conn_retries < 0)
1415 return -1;
1416
Willy Tarreau223995e2019-05-04 10:38:31 +02001417 if (objt_server(s->target))
1418 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1419 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1420
Olivier Houcharda254a372019-04-05 15:30:12 +02001421 req = &s->req;
1422 res = &s->res;
1423 /* Remove any write error from the request, and read error from the response */
1424 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1425 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1426 res->analysers = 0;
1427 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001428 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001429 si->exp = TICK_ETERNITY;
1430 res->rex = TICK_ETERNITY;
1431 res->to_forward = 0;
1432 res->analyse_exp = TICK_ETERNITY;
1433 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001434 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001435 si_release_endpoint(&s->si[1]);
1436 b_free(&req->buf);
1437 /* Swap the L7 buffer with the channel buffer */
1438 /* We know we stored the co_data as b_data, so get it there */
1439 co_data = b_data(&si->l7_buffer);
1440 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1441 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1442
1443 co_set_data(req, co_data);
1444 b_reset(&res->buf);
1445 co_set_data(res, 0);
1446 return 0;
1447}
1448
Christopher Faulete0768eb2018-10-03 16:38:02 +02001449/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1450 * processing can continue on next analysers, or zero if it either needs more
1451 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1452 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1453 * when it has nothing left to do, and may remove any analyser when it wants to
1454 * abort.
1455 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001456int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001457{
Christopher Faulet9768c262018-10-22 09:34:31 +02001458 /*
1459 * We will analyze a complete HTTP response to check the its syntax.
1460 *
1461 * Once the start line and all headers are received, we may perform a
1462 * capture of the error (if any), and we will set a few fields. We also
1463 * logging and finally headers capture.
1464 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001465 struct session *sess = s->sess;
1466 struct http_txn *txn = s->txn;
1467 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001468 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001469 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001470 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001471 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001472 int n;
1473
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001474 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001475
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001476 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001477
Willy Tarreau4236f032019-03-05 10:43:32 +01001478 /* Parsing errors are caught here */
1479 if (htx->flags & HTX_FL_PARSING_ERROR)
1480 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001481 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1482 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001483
Christopher Faulete0768eb2018-10-03 16:38:02 +02001484 /*
1485 * Now we quickly check if we have found a full valid response.
1486 * If not so, we check the FD and buffer states before leaving.
1487 * A full response is indicated by the fact that we have seen
1488 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1489 * responses are checked first.
1490 *
1491 * Depending on whether the client is still there or not, we
1492 * may send an error response back or not. Note that normally
1493 * we should only check for HTTP status there, and check I/O
1494 * errors somewhere else.
1495 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001496 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001497 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001498 /* 1: have we encountered a read error ? */
1499 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001500 struct connection *conn = NULL;
1501
Olivier Houchard865d8392019-05-03 22:46:27 +02001502 if (objt_cs(s->si[1].end))
1503 conn = objt_cs(s->si[1].end)->conn;
1504
1505 if (si_b->flags & SI_FL_L7_RETRY &&
1506 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001507 /* If we arrive here, then CF_READ_ERROR was
1508 * set by si_cs_recv() because we matched a
1509 * status, overwise it would have removed
1510 * the SI_FL_L7_RETRY flag, so it's ok not
1511 * to check s->be->retry_type.
1512 */
1513 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1514 return 0;
1515 }
1516
Olivier Houchard6db16992019-05-17 15:40:49 +02001517 if (txn->flags & TX_NOT_FIRST)
1518 goto abort_keep_alive;
1519
Olivier Houcharda798bf52019-03-08 18:52:00 +01001520 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001521 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001522 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001523 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001524 }
1525
Christopher Faulete0768eb2018-10-03 16:38:02 +02001526 rep->analysers &= AN_RES_FLT_END;
1527 txn->status = 502;
1528
1529 /* Check to see if the server refused the early data.
1530 * If so, just send a 425
1531 */
Olivier Houchard865d8392019-05-03 22:46:27 +02001532 if (conn->err_code == CO_ER_SSL_EARLY_FAILED) {
1533 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001534 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001535 do_l7_retry(s, si_b) == 0) {
1536 DBG_TRACE_DEVEL("leaving on L7 retry",
1537 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001538 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001539 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001540 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001541 }
1542
1543 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001544 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001545
1546 if (!(s->flags & SF_ERR_MASK))
1547 s->flags |= SF_ERR_SRVCL;
1548 if (!(s->flags & SF_FINST_MASK))
1549 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001550 DBG_TRACE_DEVEL("leaving on error",
1551 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001552 return 0;
1553 }
1554
Christopher Faulet9768c262018-10-22 09:34:31 +02001555 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001556 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001557 if ((si_b->flags & SI_FL_L7_RETRY) &&
1558 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001559 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1560 DBG_TRACE_DEVEL("leaving on L7 retry",
1561 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001562 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001563 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001564 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001565 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001566 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001567 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001568 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001569 }
1570
Christopher Faulete0768eb2018-10-03 16:38:02 +02001571 rep->analysers &= AN_RES_FLT_END;
1572 txn->status = 504;
1573 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001574 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001575
1576 if (!(s->flags & SF_ERR_MASK))
1577 s->flags |= SF_ERR_SRVTO;
1578 if (!(s->flags & SF_FINST_MASK))
1579 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001580 DBG_TRACE_DEVEL("leaving on error",
1581 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001582 return 0;
1583 }
1584
Christopher Faulet9768c262018-10-22 09:34:31 +02001585 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001586 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001587 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1588 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001589 if (sess->listener->counters)
1590 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001591 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001592 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001593
1594 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001595 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001596 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001597
1598 if (!(s->flags & SF_ERR_MASK))
1599 s->flags |= SF_ERR_CLICL;
1600 if (!(s->flags & SF_FINST_MASK))
1601 s->flags |= SF_FINST_H;
1602
1603 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001604 DBG_TRACE_DEVEL("leaving on error",
1605 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001606 return 0;
1607 }
1608
Christopher Faulet9768c262018-10-22 09:34:31 +02001609 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001610 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001611 if ((si_b->flags & SI_FL_L7_RETRY) &&
1612 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001613 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1614 DBG_TRACE_DEVEL("leaving on L7 retry",
1615 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001616 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001617 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001618 }
1619
Olivier Houchard6db16992019-05-17 15:40:49 +02001620 if (txn->flags & TX_NOT_FIRST)
1621 goto abort_keep_alive;
1622
Olivier Houcharda798bf52019-03-08 18:52:00 +01001623 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001624 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001625 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001626 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001627 }
1628
Christopher Faulete0768eb2018-10-03 16:38:02 +02001629 rep->analysers &= AN_RES_FLT_END;
1630 txn->status = 502;
1631 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001632 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001633
1634 if (!(s->flags & SF_ERR_MASK))
1635 s->flags |= SF_ERR_SRVCL;
1636 if (!(s->flags & SF_FINST_MASK))
1637 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001638 DBG_TRACE_DEVEL("leaving on error",
1639 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001640 return 0;
1641 }
1642
Christopher Faulet9768c262018-10-22 09:34:31 +02001643 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001644 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001645 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001646 goto abort_keep_alive;
1647
Olivier Houcharda798bf52019-03-08 18:52:00 +01001648 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001649 if (objt_server(s->target))
1650 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001651 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001652
1653 if (!(s->flags & SF_ERR_MASK))
1654 s->flags |= SF_ERR_CLICL;
1655 if (!(s->flags & SF_FINST_MASK))
1656 s->flags |= SF_FINST_H;
1657
1658 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001659 DBG_TRACE_DEVEL("leaving on error",
1660 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001661 return 0;
1662 }
1663
1664 channel_dont_close(rep);
1665 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001666 DBG_TRACE_DEVEL("waiting for more data",
1667 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001668 return 0;
1669 }
1670
1671 /* More interesting part now : we know that we have a complete
1672 * response which at least looks like HTTP. We have an indicator
1673 * of each header's length, so we can parse them quickly.
1674 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001675 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001676 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001677 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001678
Christopher Faulet9768c262018-10-22 09:34:31 +02001679 /* 0: we might have to print this header in debug mode */
1680 if (unlikely((global.mode & MODE_DEBUG) &&
1681 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1682 int32_t pos;
1683
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001684 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001685
Christopher Fauleta3f15502019-05-13 15:27:23 +02001686 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001687 struct htx_blk *blk = htx_get_blk(htx, pos);
1688 enum htx_blk_type type = htx_get_blk_type(blk);
1689
1690 if (type == HTX_BLK_EOH)
1691 break;
1692 if (type != HTX_BLK_HDR)
1693 continue;
1694
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001695 http_debug_hdr("srvhdr", s,
1696 htx_get_blk_name(htx, blk),
1697 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001698 }
1699 }
1700
Christopher Faulet03599112018-11-27 11:21:21 +01001701 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001702 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001703 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001704 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001705 if (sl->flags & HTX_SL_F_XFER_LEN) {
1706 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001707 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001708 if (sl->flags & HTX_SL_F_BODYLESS)
1709 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001710 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001711
1712 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001713 if (n < 1 || n > 5)
1714 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001715
Christopher Faulete0768eb2018-10-03 16:38:02 +02001716 /* when the client triggers a 4xx from the server, it's most often due
1717 * to a missing object or permission. These events should be tracked
1718 * because if they happen often, it may indicate a brute force or a
1719 * vulnerability scan.
1720 */
1721 if (n == 4)
1722 stream_inc_http_err_ctr(s);
1723
1724 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001725 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001726
Christopher Faulete0768eb2018-10-03 16:38:02 +02001727 /* Adjust server's health based on status code. Note: status codes 501
1728 * and 505 are triggered on demand by client request, so we must not
1729 * count them as server failures.
1730 */
1731 if (objt_server(s->target)) {
1732 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001733 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001734 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001735 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001736 }
1737
1738 /*
1739 * We may be facing a 100-continue response, or any other informational
1740 * 1xx response which is non-final, in which case this is not the right
1741 * response, and we're waiting for the next one. Let's allow this response
1742 * to go to the client and wait for the next one. There's an exception for
1743 * 101 which is used later in the code to switch protocols.
1744 */
1745 if (txn->status < 200 &&
1746 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001747 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001748 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001749 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001750 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001751 txn->status = 0;
1752 s->logs.t_data = -1; /* was not a response yet */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001753 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001754 }
1755
1756 /*
1757 * 2: check for cacheability.
1758 */
1759
1760 switch (txn->status) {
1761 case 200:
1762 case 203:
1763 case 204:
1764 case 206:
1765 case 300:
1766 case 301:
1767 case 404:
1768 case 405:
1769 case 410:
1770 case 414:
1771 case 501:
1772 break;
1773 default:
1774 /* RFC7231#6.1:
1775 * Responses with status codes that are defined as
1776 * cacheable by default (e.g., 200, 203, 204, 206,
1777 * 300, 301, 404, 405, 410, 414, and 501 in this
1778 * specification) can be reused by a cache with
1779 * heuristic expiration unless otherwise indicated
1780 * by the method definition or explicit cache
1781 * controls [RFC7234]; all other status codes are
1782 * not cacheable by default.
1783 */
1784 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1785 break;
1786 }
1787
1788 /*
1789 * 3: we may need to capture headers
1790 */
1791 s->logs.logwait &= ~LW_RESP;
1792 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001793 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001794
Christopher Faulet9768c262018-10-22 09:34:31 +02001795 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001796 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1797 txn->status == 101)) {
1798 /* Either we've established an explicit tunnel, or we're
1799 * switching the protocol. In both cases, we're very unlikely
1800 * to understand the next protocols. We have to switch to tunnel
1801 * mode, so that we transfer the request and responses then let
1802 * this protocol pass unmodified. When we later implement specific
1803 * parsers for such protocols, we'll want to check the Upgrade
1804 * header which contains information about that protocol for
1805 * responses with status 101 (eg: see RFC2817 about TLS).
1806 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001807 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001808 }
1809
Christopher Faulet61608322018-11-23 16:23:45 +01001810 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1811 * 407 (Proxy-Authenticate) responses and set the connection to private
1812 */
1813 srv_conn = cs_conn(objt_cs(s->si[1].end));
1814 if (srv_conn) {
1815 struct ist hdr;
1816 struct http_hdr_ctx ctx;
1817
1818 if (txn->status == 401)
1819 hdr = ist("WWW-Authenticate");
1820 else if (txn->status == 407)
1821 hdr = ist("Proxy-Authenticate");
1822 else
1823 goto end;
1824
1825 ctx.blk = NULL;
1826 while (http_find_header(htx, hdr, &ctx, 0)) {
1827 if ((ctx.value.len >= 9 && word_match(ctx.value.ptr, ctx.value.len, "Negotiate", 9)) ||
Olivier Houchard250031e2019-05-29 15:01:50 +02001828 (ctx.value.len >= 4 && word_match(ctx.value.ptr, ctx.value.len, "NTLM", 4))) {
1829 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet61608322018-11-23 16:23:45 +01001830 srv_conn->flags |= CO_FL_PRIVATE;
Olivier Houchard250031e2019-05-29 15:01:50 +02001831 }
Christopher Faulet61608322018-11-23 16:23:45 +01001832 }
1833 }
1834
1835 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001836 /* we want to have the response time before we start processing it */
1837 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1838
1839 /* end of job, return OK */
1840 rep->analysers &= ~an_bit;
1841 rep->analyse_exp = TICK_ETERNITY;
1842 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001843 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001844 return 1;
1845
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001846 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001847 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001848 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001849 if (sess->listener->counters)
1850 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001851 if (objt_server(s->target))
1852 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001853 txn->status = 500;
1854 if (!(s->flags & SF_ERR_MASK))
1855 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001856 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001857
1858 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001859 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001860 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001861 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001862 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001863 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001864 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001865 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001866 do_l7_retry(s, si_b) == 0) {
1867 DBG_TRACE_DEVEL("leaving on L7 retry",
1868 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001869 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001870 }
Christopher Faulet47365272018-10-31 17:40:50 +01001871 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001872 /* fall through */
1873
Christopher Fauletb8a53712019-12-16 11:29:38 +01001874 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001875 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001876
1877 if (!(s->flags & SF_ERR_MASK))
1878 s->flags |= SF_ERR_PRXCOND;
1879 if (!(s->flags & SF_FINST_MASK))
1880 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001881
1882 s->si[1].flags |= SI_FL_NOLINGER;
1883 rep->analysers &= AN_RES_FLT_END;
1884 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001885 DBG_TRACE_DEVEL("leaving on error",
1886 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001887 return 0;
1888
Christopher Faulete0768eb2018-10-03 16:38:02 +02001889 abort_keep_alive:
1890 /* A keep-alive request to the server failed on a network error.
1891 * The client is required to retry. We need to close without returning
1892 * any other information so that the client retries.
1893 */
1894 txn->status = 0;
1895 rep->analysers &= AN_RES_FLT_END;
1896 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001897 s->logs.logwait = 0;
1898 s->logs.level = 0;
1899 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001900 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001901 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1902 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001903 return 0;
1904}
1905
1906/* This function performs all the processing enabled for the current response.
1907 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1908 * and updates s->res.analysers. It might make sense to explode it into several
1909 * other functions. It works like process_request (see indications above).
1910 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001911int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001912{
1913 struct session *sess = s->sess;
1914 struct http_txn *txn = s->txn;
1915 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001916 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001917 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001918 enum rule_result ret = HTTP_RULE_RES_CONT;
1919
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001920 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1921 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001922
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001923 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001924
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001925 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001926
1927 /* The stats applet needs to adjust the Connection header but we don't
1928 * apply any filter there.
1929 */
1930 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1931 rep->analysers &= ~an_bit;
1932 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001933 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001934 }
1935
1936 /*
1937 * We will have to evaluate the filters.
1938 * As opposed to version 1.2, now they will be evaluated in the
1939 * filters order and not in the header order. This means that
1940 * each filter has to be validated among all headers.
1941 *
1942 * Filters are tried with ->be first, then with ->fe if it is
1943 * different from ->be.
1944 *
1945 * Maybe we are in resume condiion. In this case I choose the
1946 * "struct proxy" which contains the rule list matching the resume
1947 * pointer. If none of theses "struct proxy" match, I initialise
1948 * the process with the first one.
1949 *
1950 * In fact, I check only correspondance betwwen the current list
1951 * pointer and the ->fe rule list. If it doesn't match, I initialize
1952 * the loop with the ->be.
1953 */
1954 if (s->current_rule_list == &sess->fe->http_res_rules)
1955 cur_proxy = sess->fe;
1956 else
1957 cur_proxy = s->be;
1958 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001959 /* evaluate http-response rules */
1960 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001961 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001962
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001963 switch (ret) {
1964 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1965 goto return_prx_yield;
1966
1967 case HTTP_RULE_RES_CONT:
1968 case HTTP_RULE_RES_STOP: /* nothing to do */
1969 break;
1970
1971 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1972 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001973
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001974 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1975 goto return_prx_cond;
1976
1977 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001978 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001979
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001980 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1981 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001982
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001983 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1984 goto return_int_err;
1985 }
1986
1987 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001988
Christopher Faulete0768eb2018-10-03 16:38:02 +02001989 /* check whether we're already working on the frontend */
1990 if (cur_proxy == sess->fe)
1991 break;
1992 cur_proxy = sess->fe;
1993 }
1994
1995 /* After this point, this anayzer can't return yield, so we can
1996 * remove the bit corresponding to this analyzer from the list.
1997 *
1998 * Note that the intermediate returns and goto found previously
1999 * reset the analyzers.
2000 */
2001 rep->analysers &= ~an_bit;
2002 rep->analyse_exp = TICK_ETERNITY;
2003
2004 /* OK that's all we can do for 1xx responses */
2005 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002006 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002007
2008 /*
2009 * Now check for a server cookie.
2010 */
2011 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002012 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002013
2014 /*
2015 * Check for cache-control or pragma headers if required.
2016 */
2017 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002018 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002019
2020 /*
2021 * Add server cookie in the response if needed
2022 */
2023 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
2024 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
2025 (!(s->flags & SF_DIRECT) ||
2026 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
2027 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
2028 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
2029 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
2030 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
2031 !(s->flags & SF_IGNORE_PRST)) {
2032 /* the server is known, it's not the one the client requested, or the
2033 * cookie's last seen date needs to be refreshed. We have to
2034 * insert a set-cookie here, except if we want to insert only on POST
2035 * requests and this one isn't. Note that servers which don't have cookies
2036 * (eg: some backup servers) will return a full cookie removal request.
2037 */
2038 if (!objt_server(s->target)->cookie) {
2039 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002040 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02002041 s->be->cookie_name);
2042 }
2043 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002044 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002045
2046 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2047 /* emit last_date, which is mandatory */
2048 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2049 s30tob64((date.tv_sec+3) >> 2,
2050 trash.area + trash.data);
2051 trash.data += 5;
2052
2053 if (s->be->cookie_maxlife) {
2054 /* emit first_date, which is either the original one or
2055 * the current date.
2056 */
2057 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2058 s30tob64(txn->cookie_first_date ?
2059 txn->cookie_first_date >> 2 :
2060 (date.tv_sec+3) >> 2,
2061 trash.area + trash.data);
2062 trash.data += 5;
2063 }
2064 }
2065 chunk_appendf(&trash, "; path=/");
2066 }
2067
2068 if (s->be->cookie_domain)
2069 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2070
2071 if (s->be->ck_opts & PR_CK_HTTPONLY)
2072 chunk_appendf(&trash, "; HttpOnly");
2073
2074 if (s->be->ck_opts & PR_CK_SECURE)
2075 chunk_appendf(&trash, "; Secure");
2076
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002077 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002078 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002079
2080 txn->flags &= ~TX_SCK_MASK;
2081 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2082 /* the server did not change, only the date was updated */
2083 txn->flags |= TX_SCK_UPDATED;
2084 else
2085 txn->flags |= TX_SCK_INSERTED;
2086
2087 /* Here, we will tell an eventual cache on the client side that we don't
2088 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2089 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2090 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2091 */
2092 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2093
2094 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2095
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002096 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002097 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002098 }
2099 }
2100
2101 /*
2102 * Check if result will be cacheable with a cookie.
2103 * We'll block the response if security checks have caught
2104 * nasty things such as a cacheable cookie.
2105 */
2106 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2107 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2108 (s->be->options & PR_O_CHK_CACHE)) {
2109 /* we're in presence of a cacheable response containing
2110 * a set-cookie header. We'll block it as requested by
2111 * the 'checkcache' option, and send an alert.
2112 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002113 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2114 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2115 send_log(s->be, LOG_ALERT,
2116 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2117 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002118 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002119 }
2120
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002121 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002122 /* Always enter in the body analyzer */
2123 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2124 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2125
2126 /* if the user wants to log as soon as possible, without counting
2127 * bytes from the server, then this is the right moment. We have
2128 * to temporarily assign bytes_out to log what we currently have.
2129 */
2130 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2131 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002132 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002133 s->do_log(s);
2134 s->logs.bytes_out = 0;
2135 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002136 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002137 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002138
Christopher Fauletb8a53712019-12-16 11:29:38 +01002139 done:
2140 rep->analysers &= ~an_bit;
2141 rep->analyse_exp = TICK_ETERNITY;
2142 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002143
Christopher Fauletb8a53712019-12-16 11:29:38 +01002144 deny:
2145 txn->flags |= TX_CLDENY;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002146 txn->status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002147 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002148 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002149 if (sess->listener->counters)
2150 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002151 if (objt_server(s->target))
2152 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002153 goto return_prx_err;
2154
2155 return_int_err:
2156 txn->status = 500;
2157 if (!(s->flags & SF_ERR_MASK))
2158 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002159 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002160 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2161 if (objt_server(s->target))
2162 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002163 if (objt_server(s->target))
2164 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002165 goto return_prx_err;
2166
2167 return_bad_res:
2168 txn->status = 502;
2169 /* fall through */
2170
2171 return_prx_err:
2172 http_reply_and_close(s, txn->status, http_error_message(s));
2173 /* fall through */
2174
2175 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002176 s->logs.t_data = -1; /* was not a valid response */
2177 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002178
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002179 if (!(s->flags & SF_ERR_MASK))
2180 s->flags |= SF_ERR_PRXCOND;
2181 if (!(s->flags & SF_FINST_MASK))
2182 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002183
2184 rep->analysers &= ~an_bit;
2185 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002186 DBG_TRACE_DEVEL("leaving on error",
2187 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002188 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002189
2190 return_prx_yield:
2191 channel_dont_close(rep);
2192 DBG_TRACE_DEVEL("waiting for more data",
2193 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2194 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002195}
2196
2197/* This function is an analyser which forwards response body (including chunk
2198 * sizes if any). It is called as soon as we must forward, even if we forward
2199 * zero byte. The only situation where it must not be called is when we're in
2200 * tunnel mode and we want to forward till the close. It's used both to forward
2201 * remaining data and to resync after end of body. It expects the msg_state to
2202 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2203 * read more data, or 1 once we can go on with next request or end the stream.
2204 *
2205 * It is capable of compressing response data both in content-length mode and
2206 * in chunked mode. The state machines follows different flows depending on
2207 * whether content-length and chunked modes are used, since there are no
2208 * trailers in content-length :
2209 *
2210 * chk-mode cl-mode
2211 * ,----- BODY -----.
2212 * / \
2213 * V size > 0 V chk-mode
2214 * .--> SIZE -------------> DATA -------------> CRLF
2215 * | | size == 0 | last byte |
2216 * | v final crlf v inspected |
2217 * | TRAILERS -----------> DONE |
2218 * | |
2219 * `----------------------------------------------'
2220 *
2221 * Compression only happens in the DATA state, and must be flushed in final
2222 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2223 * is performed at once on final states for all bytes parsed, or when leaving
2224 * on missing data.
2225 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002226int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002227{
2228 struct session *sess = s->sess;
2229 struct http_txn *txn = s->txn;
2230 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002231 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002232 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002233
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002234 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002235
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002236 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002237
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002238 if (htx->flags & HTX_FL_PARSING_ERROR)
2239 goto return_bad_res;
2240 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2241 goto return_int_err;
2242
Christopher Faulete0768eb2018-10-03 16:38:02 +02002243 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002244 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002245 /* Output closed while we were sending data. We must abort and
2246 * wake the other side up.
2247 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002248 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002249 http_end_response(s);
2250 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002251 DBG_TRACE_DEVEL("leaving on error",
2252 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002253 return 1;
2254 }
2255
Christopher Faulet9768c262018-10-22 09:34:31 +02002256 if (msg->msg_state == HTTP_MSG_BODY)
2257 msg->msg_state = HTTP_MSG_DATA;
2258
Christopher Faulete0768eb2018-10-03 16:38:02 +02002259 /* in most states, we should abort in case of early close */
2260 channel_auto_close(res);
2261
Christopher Faulete0768eb2018-10-03 16:38:02 +02002262 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002263 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002264 if (res->flags & CF_EOI)
2265 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002266 }
2267 else {
2268 /* We can't process the buffer's contents yet */
2269 res->flags |= CF_WAKE_WRITE;
2270 goto missing_data_or_waiting;
2271 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002272 }
2273
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002274 if (msg->msg_state >= HTTP_MSG_ENDING)
2275 goto ending;
2276
2277 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2278 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2279 msg->msg_state = HTTP_MSG_ENDING;
2280 goto ending;
2281 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002282
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002283 /* Forward input data. We get it by removing all outgoing data not
2284 * forwarded yet from HTX data size. If there are some data filters, we
2285 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002286 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002287 if (HAS_RSP_DATA_FILTERS(s)) {
2288 ret = flt_http_payload(s, msg, htx->data);
2289 if (ret < 0)
2290 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002291 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002292 }
2293 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002294 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002295 if (msg->flags & HTTP_MSGF_XFER_LEN)
2296 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002297 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002298
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002299 if (htx->data != co_data(res))
2300 goto missing_data_or_waiting;
2301
2302 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2303 msg->msg_state = HTTP_MSG_ENDING;
2304 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002305 }
2306
Christopher Faulet9768c262018-10-22 09:34:31 +02002307 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002308 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2309 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002310 */
2311 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2312 goto missing_data_or_waiting;
2313
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002314 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002315
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002316 ending:
2317 /* other states, ENDING...TUNNEL */
2318 if (msg->msg_state >= HTTP_MSG_DONE)
2319 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002320
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002321 if (HAS_RSP_DATA_FILTERS(s)) {
2322 ret = flt_http_end(s, msg);
2323 if (ret <= 0) {
2324 if (!ret)
2325 goto missing_data_or_waiting;
2326 goto return_bad_res;
2327 }
2328 }
2329
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002330 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2331 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2332 msg->msg_state = HTTP_MSG_TUNNEL;
2333 goto ending;
2334 }
2335 else {
2336 msg->msg_state = HTTP_MSG_DONE;
2337 res->to_forward = 0;
2338 }
2339
2340 done:
2341
2342 channel_dont_close(res);
2343
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002344 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002345 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002346 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002347 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2348 if (res->flags & CF_SHUTW) {
2349 /* response errors are most likely due to the
2350 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002351 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002352 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002353 goto return_bad_res;
2354 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002355 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002356 return 1;
2357 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002358 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2359 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002360 return 0;
2361
2362 missing_data_or_waiting:
2363 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002364 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002365
2366 /* stop waiting for data if the input is closed before the end. If the
2367 * client side was already closed, it means that the client has aborted,
2368 * so we don't want to count this as a server abort. Otherwise it's a
2369 * server abort.
2370 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002371 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002372 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002373 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002374 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002375 if (htx_is_empty(htx))
2376 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002377 }
2378
Christopher Faulete0768eb2018-10-03 16:38:02 +02002379 /* When TE: chunked is used, we need to get there again to parse
2380 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002381 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2382 * are filters registered on the stream, we don't want to forward a
2383 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002384 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002385 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002386 channel_dont_close(res);
2387
2388 /* We know that more data are expected, but we couldn't send more that
2389 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2390 * system knows it must not set a PUSH on this first part. Interactive
2391 * modes are already handled by the stream sock layer. We must not do
2392 * this in content-length mode because it could present the MSG_MORE
2393 * flag with the last block of forwarded data, which would cause an
2394 * additional delay to be observed by the receiver.
2395 */
2396 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2397 res->flags |= CF_EXPECT_MORE;
2398
2399 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002400 DBG_TRACE_DEVEL("waiting for more data to forward",
2401 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002402 return 0;
2403
Christopher Faulet93e02d82019-03-08 14:18:50 +01002404 return_srv_abort:
2405 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2406 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002407 if (sess->listener->counters)
2408 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002409 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002410 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002411 if (!(s->flags & SF_ERR_MASK))
2412 s->flags |= SF_ERR_SRVCL;
2413 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002414
Christopher Faulet93e02d82019-03-08 14:18:50 +01002415 return_cli_abort:
2416 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2417 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002418 if (sess->listener->counters)
2419 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002420 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002421 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002422 if (!(s->flags & SF_ERR_MASK))
2423 s->flags |= SF_ERR_CLICL;
2424 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002425
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002426 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002427 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002428 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002429 if (sess->listener->counters)
2430 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002431 if (objt_server(s->target))
2432 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002433 if (!(s->flags & SF_ERR_MASK))
2434 s->flags |= SF_ERR_INTERNAL;
2435 goto return_error;
2436
Christopher Faulet93e02d82019-03-08 14:18:50 +01002437 return_bad_res:
2438 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2439 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002440 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002441 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2442 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002443 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002444 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002445 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002446
Christopher Faulet93e02d82019-03-08 14:18:50 +01002447 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002448 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002449 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002450 res->analysers &= AN_RES_FLT_END;
2451 s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002452 if (!(s->flags & SF_FINST_MASK))
2453 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002454 DBG_TRACE_DEVEL("leaving on error",
2455 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002456 return 0;
2457}
2458
Christopher Fauletf2824e62018-10-01 12:12:37 +02002459/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002460 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002461 * as too large a request to build a valid response.
2462 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002463int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002464{
Christopher Faulet99daf282018-11-28 22:58:13 +01002465 struct channel *req = &s->req;
2466 struct channel *res = &s->res;
2467 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002468 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002469 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002470 struct ist status, reason, location;
2471 unsigned int flags;
2472 size_t data;
Christopher Faulet08e66462019-05-23 16:44:59 +02002473 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002474
2475 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002476 if (!chunk) {
2477 if (!(s->flags & SF_ERR_MASK))
2478 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002479 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002480 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002481
Christopher Faulet99daf282018-11-28 22:58:13 +01002482 /*
2483 * Create the location
2484 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002485 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002486 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002487 case REDIRECT_TYPE_SCHEME: {
2488 struct http_hdr_ctx ctx;
2489 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002490
Christopher Faulet99daf282018-11-28 22:58:13 +01002491 host = ist("");
2492 ctx.blk = NULL;
2493 if (http_find_header(htx, ist("Host"), &ctx, 0))
2494 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002495
Christopher Faulet297fbb42019-05-13 14:41:27 +02002496 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002497 path = http_get_path(htx_sl_req_uri(sl));
2498 /* build message using path */
2499 if (path.ptr) {
2500 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2501 int qs = 0;
2502 while (qs < path.len) {
2503 if (*(path.ptr + qs) == '?') {
2504 path.len = qs;
2505 break;
2506 }
2507 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002508 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002509 }
2510 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002511 else
2512 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002513
Christopher Faulet99daf282018-11-28 22:58:13 +01002514 if (rule->rdr_str) { /* this is an old "redirect" rule */
2515 /* add scheme */
2516 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2517 goto fail;
2518 }
2519 else {
2520 /* add scheme with executing log format */
2521 chunk->data += build_logline(s, chunk->area + chunk->data,
2522 chunk->size - chunk->data,
2523 &rule->rdr_fmt);
2524 }
2525 /* add "://" + host + path */
2526 if (!chunk_memcat(chunk, "://", 3) ||
2527 !chunk_memcat(chunk, host.ptr, host.len) ||
2528 !chunk_memcat(chunk, path.ptr, path.len))
2529 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002530
Christopher Faulet99daf282018-11-28 22:58:13 +01002531 /* append a slash at the end of the location if needed and missing */
2532 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2533 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2534 if (chunk->data + 1 >= chunk->size)
2535 goto fail;
2536 chunk->area[chunk->data++] = '/';
2537 }
2538 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002539 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002540
Christopher Faulet99daf282018-11-28 22:58:13 +01002541 case REDIRECT_TYPE_PREFIX: {
2542 struct ist path;
2543
Christopher Faulet297fbb42019-05-13 14:41:27 +02002544 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002545 path = http_get_path(htx_sl_req_uri(sl));
2546 /* build message using path */
2547 if (path.ptr) {
2548 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2549 int qs = 0;
2550 while (qs < path.len) {
2551 if (*(path.ptr + qs) == '?') {
2552 path.len = qs;
2553 break;
2554 }
2555 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002556 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002557 }
2558 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002559 else
2560 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002561
Christopher Faulet99daf282018-11-28 22:58:13 +01002562 if (rule->rdr_str) { /* this is an old "redirect" rule */
2563 /* add prefix. Note that if prefix == "/", we don't want to
2564 * add anything, otherwise it makes it hard for the user to
2565 * configure a self-redirection.
2566 */
2567 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2568 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2569 goto fail;
2570 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002571 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002572 else {
2573 /* add prefix with executing log format */
2574 chunk->data += build_logline(s, chunk->area + chunk->data,
2575 chunk->size - chunk->data,
2576 &rule->rdr_fmt);
2577 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002578
Christopher Faulet99daf282018-11-28 22:58:13 +01002579 /* add path */
2580 if (!chunk_memcat(chunk, path.ptr, path.len))
2581 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002582
Christopher Faulet99daf282018-11-28 22:58:13 +01002583 /* append a slash at the end of the location if needed and missing */
2584 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2585 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2586 if (chunk->data + 1 >= chunk->size)
2587 goto fail;
2588 chunk->area[chunk->data++] = '/';
2589 }
2590 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002591 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002592 case REDIRECT_TYPE_LOCATION:
2593 default:
2594 if (rule->rdr_str) { /* this is an old "redirect" rule */
2595 /* add location */
2596 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2597 goto fail;
2598 }
2599 else {
2600 /* add location with executing log format */
2601 chunk->data += build_logline(s, chunk->area + chunk->data,
2602 chunk->size - chunk->data,
2603 &rule->rdr_fmt);
2604 }
2605 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002606 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002607 location = ist2(chunk->area, chunk->data);
2608
2609 /*
2610 * Create the 30x response
2611 */
2612 switch (rule->code) {
2613 case 308:
2614 status = ist("308");
2615 reason = ist("Permanent Redirect");
2616 break;
2617 case 307:
2618 status = ist("307");
2619 reason = ist("Temporary Redirect");
2620 break;
2621 case 303:
2622 status = ist("303");
2623 reason = ist("See Other");
2624 break;
2625 case 301:
2626 status = ist("301");
2627 reason = ist("Moved Permanently");
2628 break;
2629 case 302:
2630 default:
2631 status = ist("302");
2632 reason = ist("Found");
2633 break;
2634 }
2635
Christopher Faulet08e66462019-05-23 16:44:59 +02002636 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2637 close = 1;
2638
Christopher Faulet99daf282018-11-28 22:58:13 +01002639 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002640 /* Trim any possible response */
2641 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002642 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2643 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2644 if (!sl)
2645 goto fail;
2646 sl->info.res.status = rule->code;
2647 s->txn->status = rule->code;
2648
Christopher Faulet08e66462019-05-23 16:44:59 +02002649 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2650 goto fail;
2651
2652 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002653 !htx_add_header(htx, ist("Location"), location))
2654 goto fail;
2655
2656 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2657 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2658 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002659 }
2660
2661 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002662 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2663 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002664 }
2665
Christopher Faulet99daf282018-11-28 22:58:13 +01002666 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2667 goto fail;
2668
Kevin Zhu96b36392020-01-07 09:42:55 +01002669 htx_to_buf(htx, &res->buf);
2670
Christopher Fauletf2824e62018-10-01 12:12:37 +02002671 /* let's log the request time */
2672 s->logs.tv_request = now;
2673
Christopher Faulet06511812019-10-16 09:38:27 +02002674 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet99daf282018-11-28 22:58:13 +01002675 data = htx->data - co_data(res);
Christopher Faulet99daf282018-11-28 22:58:13 +01002676 c_adv(res, data);
2677 res->total += data;
2678
2679 channel_auto_read(req);
2680 channel_abort(req);
2681 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002682 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet99daf282018-11-28 22:58:13 +01002683
2684 res->wex = tick_add_ifset(now_ms, res->wto);
2685 channel_auto_read(res);
2686 channel_auto_close(res);
2687 channel_shutr_now(res);
2688
2689 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002690
2691 if (!(s->flags & SF_ERR_MASK))
2692 s->flags |= SF_ERR_LOCAL;
2693 if (!(s->flags & SF_FINST_MASK))
2694 s->flags |= SF_FINST_R;
2695
Christopher Faulet99daf282018-11-28 22:58:13 +01002696 free_trash_chunk(chunk);
2697 return 1;
2698
2699 fail:
2700 /* If an error occurred, remove the incomplete HTTP response from the
2701 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002702 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002703 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002704 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002705}
2706
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002707int http_transform_header_str(struct stream* s, struct channel *chn, struct htx *htx,
2708 struct ist name, const char *str, struct my_regex *re, int action)
Christopher Faulet72333522018-10-24 11:25:02 +02002709{
2710 struct http_hdr_ctx ctx;
2711 struct buffer *output = get_trash_chunk();
2712
2713 /* find full header is action is ACT_HTTP_REPLACE_HDR */
2714 ctx.blk = NULL;
2715 while (http_find_header(htx, name, &ctx, (action == ACT_HTTP_REPLACE_HDR))) {
2716 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2717 continue;
2718
2719 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2720 if (output->data == -1)
2721 return -1;
2722 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2723 return -1;
2724 }
2725 return 0;
2726}
2727
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002728static int http_transform_header(struct stream* s, struct channel *chn, struct htx *htx,
2729 const struct ist name, struct list *fmt, struct my_regex *re, int action)
Christopher Faulet72333522018-10-24 11:25:02 +02002730{
2731 struct buffer *replace;
Christopher Faulete00d06c2019-12-16 17:18:42 +01002732 int ret = 0;
Christopher Faulet72333522018-10-24 11:25:02 +02002733
2734 replace = alloc_trash_chunk();
Christopher Faulete00d06c2019-12-16 17:18:42 +01002735 if (!replace)
2736 goto fail_alloc;
Christopher Faulet72333522018-10-24 11:25:02 +02002737
2738 replace->data = build_logline(s, replace->area, replace->size, fmt);
2739 if (replace->data >= replace->size - 1)
Christopher Faulete00d06c2019-12-16 17:18:42 +01002740 goto fail_rewrite;
Christopher Faulet72333522018-10-24 11:25:02 +02002741
Christopher Faulete00d06c2019-12-16 17:18:42 +01002742 if (http_transform_header_str(s, chn, htx, name, replace->area, re, action) == -1)
2743 goto fail_rewrite;
Christopher Faulet72333522018-10-24 11:25:02 +02002744
2745 leave:
2746 free_trash_chunk(replace);
2747 return ret;
Christopher Faulete00d06c2019-12-16 17:18:42 +01002748
2749 fail_alloc:
2750 if (!(s->flags & SF_ERR_MASK))
2751 s->flags |= SF_ERR_RESOURCE;
2752 ret = -1;
2753 goto leave;
2754
2755 fail_rewrite:
2756 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.failed_rewrites, 1);
2757 if (s->flags & SF_BE_ASSIGNED)
2758 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
2759 if (s->sess->listener->counters)
2760 _HA_ATOMIC_ADD(&s->sess->listener->counters->failed_rewrites, 1);
2761 if (objt_server(s->target))
2762 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
2763
2764 if (!(s->txn->req.flags & HTTP_MSGF_SOFT_RW))
2765 ret = -1;
2766 goto leave;
Christopher Faulet72333522018-10-24 11:25:02 +02002767}
2768
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002769
2770/* Terminate a 103-Erly-hints response and send it to the client. It returns 0
2771 * on success and -1 on error. The response channel is updated accordingly.
2772 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002773static int http_reply_103_early_hints(struct channel *res)
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002774{
2775 struct htx *htx = htx_from_buf(&res->buf);
2776 size_t data;
2777
Christopher Faulet1d5ec092019-06-26 14:23:54 +02002778 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002779 /* If an error occurred during an Early-hint rule,
2780 * remove the incomplete HTTP 103 response from the
2781 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002782 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002783 return -1;
2784 }
2785
2786 data = htx->data - co_data(res);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002787 c_adv(res, data);
2788 res->total += data;
2789 return 0;
2790}
2791
Christopher Faulet6eb92892018-11-15 16:39:29 +01002792/*
2793 * Build an HTTP Early Hint HTTP 103 response header with <name> as name and with a value
2794 * built according to <fmt> log line format.
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002795 * If <early_hints> is 0, it is starts a new response by adding the start
2796 * line. If an error occurred -1 is returned. On success 0 is returned. The
2797 * channel is not updated here. It must be done calling the function
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002798 * http_reply_103_early_hints().
Christopher Faulet6eb92892018-11-15 16:39:29 +01002799 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002800static 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 +01002801{
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002802 struct channel *res = &s->res;
2803 struct htx *htx = htx_from_buf(&res->buf);
2804 struct buffer *value = alloc_trash_chunk();
2805
Christopher Fauletb8a53712019-12-16 11:29:38 +01002806 if (!value) {
2807 if (!(s->flags & SF_ERR_MASK))
2808 s->flags |= SF_ERR_RESOURCE;
2809 goto fail;
2810 }
2811
Christopher Faulet6eb92892018-11-15 16:39:29 +01002812 if (!early_hints) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002813 struct htx_sl *sl;
2814 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
2815 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2816
2817 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
2818 ist("HTTP/1.1"), ist("103"), ist("Early Hints"));
2819 if (!sl)
Christopher Faulet6eb92892018-11-15 16:39:29 +01002820 goto fail;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002821 sl->info.res.status = 103;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002822 }
2823
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002824 value->data = build_logline(s, b_tail(value), b_room(value), fmt);
2825 if (!htx_add_header(htx, name, ist2(b_head(value), b_data(value))))
Christopher Faulet6eb92892018-11-15 16:39:29 +01002826 goto fail;
2827
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002828 free_trash_chunk(value);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002829 return 1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002830
2831 fail:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002832 /* If an error occurred during an Early-hint rule, remove the incomplete
2833 * HTTP 103 response from the buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002834 channel_htx_truncate(res, htx);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002835 free_trash_chunk(value);
2836 return -1;
Christopher Faulet6eb92892018-11-15 16:39:29 +01002837}
2838
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002839/* This function executes one of the set-{method,path,query,uri} actions. It
2840 * takes the string from the variable 'replace' with length 'len', then modifies
2841 * the relevant part of the request line accordingly. Then it updates various
2842 * pointers to the next elements which were moved, and the total buffer length.
2843 * It finds the action to be performed in p[2], previously filled by function
2844 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2845 * error, though this can be revisited when this code is finally exploited.
2846 *
2847 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2848 * query string and 3 to replace uri.
2849 *
2850 * In query string case, the mark question '?' must be set at the start of the
2851 * string by the caller, event if the replacement query string is empty.
2852 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002853int http_req_replace_stline(int action, const char *replace, int len,
2854 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002855{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002856 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002857
2858 switch (action) {
2859 case 0: // method
2860 if (!http_replace_req_meth(htx, ist2(replace, len)))
2861 return -1;
2862 break;
2863
2864 case 1: // path
2865 if (!http_replace_req_path(htx, ist2(replace, len)))
2866 return -1;
2867 break;
2868
2869 case 2: // query
2870 if (!http_replace_req_query(htx, ist2(replace, len)))
2871 return -1;
2872 break;
2873
2874 case 3: // uri
2875 if (!http_replace_req_uri(htx, ist2(replace, len)))
2876 return -1;
2877 break;
2878
2879 default:
2880 return -1;
2881 }
2882 return 0;
2883}
2884
2885/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002886 * variable <status> contains the new status code. This function never fails. It
2887 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002888 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002889int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002890{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002891 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002892 char *res;
2893
2894 chunk_reset(&trash);
2895 res = ultoa_o(status, trash.area, trash.size);
2896 trash.data = res - trash.area;
2897
2898 /* Do we have a custom reason format string? */
Christopher Faulet96bff762019-12-17 13:46:18 +01002899 if (reason.ptr == NULL) {
2900 const char *str = http_get_reason(status);
2901 reason = ist2(str, strlen(str));
2902 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002903
Christopher Faulete00d06c2019-12-16 17:18:42 +01002904 if (!http_replace_res_status(htx, ist2(trash.area, trash.data)))
2905 return -1;
Christopher Faulet96bff762019-12-17 13:46:18 +01002906 if (!http_replace_res_reason(htx, reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002907 return -1;
2908 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002909}
2910
Christopher Faulet3e964192018-10-24 11:39:23 +02002911/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2912 * transaction <txn>. Returns the verdict of the first rule that prevents
2913 * further processing of the request (auth, deny, ...), and defaults to
2914 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2915 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2916 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2917 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2918 * status.
2919 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002920static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
2921 struct stream *s, int *deny_status)
Christopher Faulet3e964192018-10-24 11:39:23 +02002922{
2923 struct session *sess = strm_sess(s);
2924 struct http_txn *txn = s->txn;
2925 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002926 struct act_rule *rule;
2927 struct http_hdr_ctx ctx;
2928 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002929 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002930 int act_opts = 0;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002931 int early_hints = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002932
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002933 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002934
2935 /* If "the current_rule_list" match the executed rule list, we are in
2936 * resume condition. If a resume is needed it is always in the action
2937 * and never in the ACL or converters. In this case, we initialise the
2938 * current rule, and go to the action execution point.
2939 */
2940 if (s->current_rule) {
2941 rule = s->current_rule;
2942 s->current_rule = NULL;
2943 if (s->current_rule_list == rules)
2944 goto resume_execution;
2945 }
2946 s->current_rule_list = rules;
2947
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002948 /* start the ruleset evaluation in strict mode */
2949 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002950
Christopher Faulet3e964192018-10-24 11:39:23 +02002951 list_for_each_entry(rule, rules, list) {
2952 /* check optional condition */
2953 if (rule->cond) {
2954 int ret;
2955
2956 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2957 ret = acl_pass(ret);
2958
2959 if (rule->cond->pol == ACL_COND_UNLESS)
2960 ret = !ret;
2961
2962 if (!ret) /* condition not matched */
2963 continue;
2964 }
2965
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002966 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002967 resume_execution:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002968 if (early_hints && rule->action != ACT_HTTP_EARLY_HINT) {
2969 early_hints = 0;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002970 if (http_reply_103_early_hints(&s->res) == -1) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002971 rule_ret = HTTP_RULE_RES_BADREQ;
2972 goto end;
2973 }
2974 }
2975
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002976 /* Always call the action function if defined */
2977 if (rule->action_ptr) {
2978 if ((s->req.flags & CF_READ_ERROR) ||
2979 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2980 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002981 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002982
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002983 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002984 case ACT_RET_CONT:
2985 break;
2986 case ACT_RET_STOP:
2987 rule_ret = HTTP_RULE_RES_STOP;
2988 goto end;
2989 case ACT_RET_YIELD:
2990 s->current_rule = rule;
2991 rule_ret = HTTP_RULE_RES_YIELD;
2992 goto end;
2993 case ACT_RET_ERR:
2994 rule_ret = HTTP_RULE_RES_ERROR;
2995 goto end;
2996 case ACT_RET_DONE:
2997 rule_ret = HTTP_RULE_RES_DONE;
2998 goto end;
2999 case ACT_RET_DENY:
3000 rule_ret = HTTP_RULE_RES_DENY;
3001 goto end;
3002 case ACT_RET_ABRT:
3003 rule_ret = HTTP_RULE_RES_ABRT;
3004 goto end;
3005 case ACT_RET_INV:
3006 rule_ret = HTTP_RULE_RES_BADREQ;
3007 goto end;
3008 }
3009 continue; /* eval the next rule */
3010 }
3011
3012 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003013 switch (rule->action) {
3014 case ACT_ACTION_ALLOW:
3015 rule_ret = HTTP_RULE_RES_STOP;
3016 goto end;
3017
3018 case ACT_ACTION_DENY:
3019 if (deny_status)
Christopher Faulet96bff762019-12-17 13:46:18 +01003020 *deny_status = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003021 rule_ret = HTTP_RULE_RES_DENY;
3022 goto end;
3023
3024 case ACT_HTTP_REQ_TARPIT:
3025 txn->flags |= TX_CLTARPIT;
3026 if (deny_status)
Christopher Faulet96bff762019-12-17 13:46:18 +01003027 *deny_status = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003028 rule_ret = HTTP_RULE_RES_DENY;
3029 goto end;
3030
3031 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02003032 /* Auth might be performed on regular http-req rules as well as on stats */
Christopher Faulet96bff762019-12-17 13:46:18 +01003033 auth_realm = rule->arg.http.str.ptr;
Christopher Faulet3e964192018-10-24 11:39:23 +02003034 if (!auth_realm) {
3035 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3036 auth_realm = STATS_DEFAULT_REALM;
3037 else
3038 auth_realm = px->id;
3039 }
3040 /* send 401/407 depending on whether we use a proxy or not. We still
3041 * count one error, because normal browsing won't significantly
3042 * increase the counter but brute force attempts will.
3043 */
Christopher Faulet3e964192018-10-24 11:39:23 +02003044 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003045 if (http_reply_40x_unauthorized(s, auth_realm) == -1)
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003046 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet12c51e22018-11-28 15:59:42 +01003047 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02003048 goto end;
3049
3050 case ACT_HTTP_REDIR:
Christopher Faulet3e964192018-10-24 11:39:23 +02003051 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003052 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003053 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003054 goto end;
3055
3056 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003057 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003058 break;
3059
3060 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003061 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003062 break;
3063
3064 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003065 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003066 break;
3067
3068 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003069 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003070 break;
3071
3072 case ACT_HTTP_REPLACE_HDR:
3073 case ACT_HTTP_REPLACE_VAL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003074 if (http_transform_header(s, &s->req, htx, rule->arg.http.str,
3075 &rule->arg.http.fmt,
3076 rule->arg.http.re, rule->action)) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003077 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003078 goto end;
3079 }
3080 break;
3081
3082 case ACT_HTTP_DEL_HDR:
3083 /* remove all occurrences of the header */
3084 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01003085 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02003086 http_remove_header(htx, &ctx);
3087 break;
3088
3089 case ACT_HTTP_SET_HDR:
3090 case ACT_HTTP_ADD_HDR: {
3091 /* The scope of the trash buffer must be limited to this function. The
3092 * build_logline() function can execute a lot of other function which
3093 * can use the trash buffer. So for limiting the scope of this global
3094 * buffer, we build first the header value using build_logline, and
3095 * after we store the header name.
3096 */
3097 struct buffer *replace;
3098 struct ist n, v;
3099
3100 replace = alloc_trash_chunk();
3101 if (!replace) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003102 if (!(s->flags & SF_ERR_MASK))
3103 s->flags |= SF_ERR_RESOURCE;
3104 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003105 goto end;
3106 }
3107
Christopher Faulet96bff762019-12-17 13:46:18 +01003108 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
3109 n = rule->arg.http.str;
Christopher Faulet3e964192018-10-24 11:39:23 +02003110 v = ist2(replace->area, replace->data);
3111
3112 if (rule->action == ACT_HTTP_SET_HDR) {
3113 /* remove all occurrences of the header */
3114 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01003115 while (http_find_header(htx, n, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02003116 http_remove_header(htx, &ctx);
3117 }
3118
3119 if (!http_add_header(htx, n, v)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003120 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01003121 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003122 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003123 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003124 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Fauleta00071e2019-12-12 16:41:00 +01003125
3126 if (!(txn->req.flags & HTTP_MSGF_SOFT_RW)) {
3127 rule_ret = HTTP_RULE_RES_ERROR;
3128 goto end;
3129 }
Christopher Faulet3e964192018-10-24 11:39:23 +02003130 }
3131 free_trash_chunk(replace);
3132 break;
3133 }
3134
3135 case ACT_HTTP_DEL_ACL:
3136 case ACT_HTTP_DEL_MAP: {
3137 struct pat_ref *ref;
3138 struct buffer *key;
3139
3140 /* collect reference */
3141 ref = pat_ref_lookup(rule->arg.map.ref);
3142 if (!ref)
3143 continue;
3144
3145 /* allocate key */
3146 key = alloc_trash_chunk();
3147 if (!key) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003148 if (!(s->flags & SF_ERR_MASK))
3149 s->flags |= SF_ERR_RESOURCE;
3150 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003151 goto end;
3152 }
3153
3154 /* collect key */
3155 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3156 key->area[key->data] = '\0';
3157
3158 /* perform update */
3159 /* returned code: 1=ok, 0=ko */
3160 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3161 pat_ref_delete(ref, key->area);
3162 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3163
3164 free_trash_chunk(key);
3165 break;
3166 }
3167
3168 case ACT_HTTP_ADD_ACL: {
3169 struct pat_ref *ref;
3170 struct buffer *key;
3171
3172 /* collect reference */
3173 ref = pat_ref_lookup(rule->arg.map.ref);
3174 if (!ref)
3175 continue;
3176
3177 /* allocate key */
3178 key = alloc_trash_chunk();
3179 if (!key) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003180 if (!(s->flags & SF_ERR_MASK))
3181 s->flags |= SF_ERR_RESOURCE;
3182 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003183 goto end;
3184 }
3185
3186 /* collect key */
3187 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3188 key->area[key->data] = '\0';
3189
3190 /* perform update */
3191 /* add entry only if it does not already exist */
3192 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3193 if (pat_ref_find_elt(ref, key->area) == NULL)
3194 pat_ref_add(ref, key->area, NULL, NULL);
3195 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3196
3197 free_trash_chunk(key);
3198 break;
3199 }
3200
3201 case ACT_HTTP_SET_MAP: {
3202 struct pat_ref *ref;
3203 struct buffer *key, *value;
3204
3205 /* collect reference */
3206 ref = pat_ref_lookup(rule->arg.map.ref);
3207 if (!ref)
3208 continue;
3209
3210 /* allocate key */
3211 key = alloc_trash_chunk();
3212 if (!key) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003213 if (!(s->flags & SF_ERR_MASK))
3214 s->flags |= SF_ERR_RESOURCE;
3215 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003216 goto end;
3217 }
3218
3219 /* allocate value */
3220 value = alloc_trash_chunk();
3221 if (!value) {
3222 free_trash_chunk(key);
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003223 if (!(s->flags & SF_ERR_MASK))
3224 s->flags |= SF_ERR_RESOURCE;
3225 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003226 goto end;
3227 }
3228
3229 /* collect key */
3230 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3231 key->area[key->data] = '\0';
3232
3233 /* collect value */
3234 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3235 value->area[value->data] = '\0';
3236
3237 /* perform update */
Christopher Faulete84289e2019-04-19 14:50:55 +02003238 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003239 if (pat_ref_find_elt(ref, key->area) != NULL)
3240 /* update entry if it exists */
3241 pat_ref_set(ref, key->area, value->area, NULL);
3242 else
3243 /* insert a new entry */
3244 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02003245 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003246 free_trash_chunk(key);
3247 free_trash_chunk(value);
3248 break;
3249 }
3250
3251 case ACT_HTTP_EARLY_HINT:
3252 if (!(txn->req.flags & HTTP_MSGF_VER_11))
3253 break;
Christopher Faulet96bff762019-12-17 13:46:18 +01003254 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 +01003255 if (early_hints == -1) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003256 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003257 goto end;
3258 }
3259 break;
3260
Christopher Faulet3e964192018-10-24 11:39:23 +02003261 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3262 /* Note: only the first valid tracking parameter of each
3263 * applies.
3264 */
3265
3266 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3267 struct stktable *t;
3268 struct stksess *ts;
3269 struct stktable_key *key;
3270 void *ptr1, *ptr2;
3271
3272 t = rule->arg.trk_ctr.table.t;
3273 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL,
3274 rule->arg.trk_ctr.expr, NULL);
3275
3276 if (key && (ts = stktable_get_entry(t, key))) {
3277 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3278
3279 /* let's count a new HTTP request as it's the first time we do it */
3280 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3281 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3282 if (ptr1 || ptr2) {
3283 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3284
3285 if (ptr1)
3286 stktable_data_cast(ptr1, http_req_cnt)++;
3287
3288 if (ptr2)
3289 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
3290 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3291
3292 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3293
3294 /* If data was modified, we need to touch to re-schedule sync */
3295 stktable_touch_local(t, ts, 0);
3296 }
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:
3312 if (early_hints) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003313 if (http_reply_103_early_hints(&s->res) == -1)
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003314 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003315 }
3316
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003317 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003318 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003319 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003320
Christopher Faulet3e964192018-10-24 11:39:23 +02003321 /* we reached the end of the rules, nothing to report */
3322 return rule_ret;
3323}
3324
3325/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3326 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3327 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3328 * is returned, the process can continue the evaluation of next rule list. If
3329 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3330 * is returned, it means the operation could not be processed and a server error
3331 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3332 * deny rule. If *YIELD is returned, the caller must call again the function
3333 * with the same context.
3334 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003335static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
3336 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02003337{
3338 struct session *sess = strm_sess(s);
3339 struct http_txn *txn = s->txn;
3340 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02003341 struct act_rule *rule;
3342 struct http_hdr_ctx ctx;
3343 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003344 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02003345
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003346 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02003347
3348 /* If "the current_rule_list" match the executed rule list, we are in
3349 * resume condition. If a resume is needed it is always in the action
3350 * and never in the ACL or converters. In this case, we initialise the
3351 * current rule, and go to the action execution point.
3352 */
3353 if (s->current_rule) {
3354 rule = s->current_rule;
3355 s->current_rule = NULL;
3356 if (s->current_rule_list == rules)
3357 goto resume_execution;
3358 }
3359 s->current_rule_list = rules;
3360
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003361 /* start the ruleset evaluation in strict mode */
3362 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003363
Christopher Faulet3e964192018-10-24 11:39:23 +02003364 list_for_each_entry(rule, rules, list) {
3365 /* check optional condition */
3366 if (rule->cond) {
3367 int ret;
3368
3369 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3370 ret = acl_pass(ret);
3371
3372 if (rule->cond->pol == ACL_COND_UNLESS)
3373 ret = !ret;
3374
3375 if (!ret) /* condition not matched */
3376 continue;
3377 }
3378
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003379 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02003380resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003381
3382 /* Always call the action function if defined */
3383 if (rule->action_ptr) {
3384 if ((s->req.flags & CF_READ_ERROR) ||
3385 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3386 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003387 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003388
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003389 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003390 case ACT_RET_CONT:
3391 break;
3392 case ACT_RET_STOP:
3393 rule_ret = HTTP_RULE_RES_STOP;
3394 goto end;
3395 case ACT_RET_YIELD:
3396 s->current_rule = rule;
3397 rule_ret = HTTP_RULE_RES_YIELD;
3398 goto end;
3399 case ACT_RET_ERR:
3400 rule_ret = HTTP_RULE_RES_ERROR;
3401 goto end;
3402 case ACT_RET_DONE:
3403 rule_ret = HTTP_RULE_RES_DONE;
3404 goto end;
3405 case ACT_RET_DENY:
3406 rule_ret = HTTP_RULE_RES_DENY;
3407 goto end;
3408 case ACT_RET_ABRT:
3409 rule_ret = HTTP_RULE_RES_ABRT;
3410 goto end;
3411 case ACT_RET_INV:
3412 rule_ret = HTTP_RULE_RES_BADREQ;
3413 goto end;
3414 }
3415 continue; /* eval the next rule */
3416 }
3417
3418 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003419 switch (rule->action) {
3420 case ACT_ACTION_ALLOW:
3421 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3422 goto end;
3423
3424 case ACT_ACTION_DENY:
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003425 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003426 goto end;
3427
3428 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003429 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003430 break;
3431
3432 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003433 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003434 break;
3435
3436 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003437 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003438 break;
3439
3440 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003441 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003442 break;
3443
3444 case ACT_HTTP_REPLACE_HDR:
3445 case ACT_HTTP_REPLACE_VAL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003446 if (http_transform_header(s, &s->res, htx, rule->arg.http.str,
3447 &rule->arg.http.fmt,
3448 rule->arg.http.re, rule->action)) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003449 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003450 goto end;
3451 }
3452 break;
3453
3454 case ACT_HTTP_DEL_HDR:
3455 /* remove all occurrences of the header */
3456 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01003457 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02003458 http_remove_header(htx, &ctx);
3459 break;
3460
3461 case ACT_HTTP_SET_HDR:
3462 case ACT_HTTP_ADD_HDR: {
3463 struct buffer *replace;
3464 struct ist n, v;
3465
3466 replace = alloc_trash_chunk();
3467 if (!replace) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003468 if (!(s->flags & SF_ERR_MASK))
3469 s->flags |= SF_ERR_RESOURCE;
3470 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003471 goto end;
3472 }
3473
Christopher Faulet96bff762019-12-17 13:46:18 +01003474 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.http.fmt);
3475 n = rule->arg.http.str;
Christopher Faulet3e964192018-10-24 11:39:23 +02003476 v = ist2(replace->area, replace->data);
3477
3478 if (rule->action == ACT_HTTP_SET_HDR) {
3479 /* remove all occurrences of the header */
3480 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01003481 while (http_find_header(htx, n, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02003482 http_remove_header(htx, &ctx);
3483 }
3484
3485 if (!http_add_header(htx, n, v)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003486 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01003487 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003488 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003489 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003490 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01003491 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
Christopher Fauleta00071e2019-12-12 16:41:00 +01003492
3493 if (!(txn->rsp.flags & HTTP_MSGF_SOFT_RW)) {
3494 rule_ret = HTTP_RULE_RES_ERROR;
3495 goto end;
3496 }
Christopher Faulet3e964192018-10-24 11:39:23 +02003497 }
3498 free_trash_chunk(replace);
3499 break;
3500 }
3501
3502 case ACT_HTTP_DEL_ACL:
3503 case ACT_HTTP_DEL_MAP: {
3504 struct pat_ref *ref;
3505 struct buffer *key;
3506
3507 /* collect reference */
3508 ref = pat_ref_lookup(rule->arg.map.ref);
3509 if (!ref)
3510 continue;
3511
3512 /* allocate key */
3513 key = alloc_trash_chunk();
3514 if (!key) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003515 if (!(s->flags & SF_ERR_MASK))
3516 s->flags |= SF_ERR_RESOURCE;
3517 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003518 goto end;
3519 }
3520
3521 /* collect key */
3522 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3523 key->area[key->data] = '\0';
3524
3525 /* perform update */
3526 /* returned code: 1=ok, 0=ko */
3527 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3528 pat_ref_delete(ref, key->area);
3529 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3530
3531 free_trash_chunk(key);
3532 break;
3533 }
3534
3535 case ACT_HTTP_ADD_ACL: {
3536 struct pat_ref *ref;
3537 struct buffer *key;
3538
3539 /* collect reference */
3540 ref = pat_ref_lookup(rule->arg.map.ref);
3541 if (!ref)
3542 continue;
3543
3544 /* allocate key */
3545 key = alloc_trash_chunk();
3546 if (!key) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003547 if (!(s->flags & SF_ERR_MASK))
3548 s->flags |= SF_ERR_RESOURCE;
3549 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003550 goto end;
3551 }
3552
3553 /* collect key */
3554 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3555 key->area[key->data] = '\0';
3556
3557 /* perform update */
3558 /* check if the entry already exists */
Christopher Faulete84289e2019-04-19 14:50:55 +02003559 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003560 if (pat_ref_find_elt(ref, key->area) == NULL)
3561 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02003562 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003563 free_trash_chunk(key);
3564 break;
3565 }
3566
3567 case ACT_HTTP_SET_MAP: {
3568 struct pat_ref *ref;
3569 struct buffer *key, *value;
3570
3571 /* collect reference */
3572 ref = pat_ref_lookup(rule->arg.map.ref);
3573 if (!ref)
3574 continue;
3575
3576 /* allocate key */
3577 key = alloc_trash_chunk();
3578 if (!key) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003579 if (!(s->flags & SF_ERR_MASK))
3580 s->flags |= SF_ERR_RESOURCE;
3581 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003582 goto end;
3583 }
3584
3585 /* allocate value */
3586 value = alloc_trash_chunk();
3587 if (!value) {
3588 free_trash_chunk(key);
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003589 if (!(s->flags & SF_ERR_MASK))
3590 s->flags |= SF_ERR_RESOURCE;
3591 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003592 goto end;
3593 }
3594
3595 /* collect key */
3596 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3597 key->area[key->data] = '\0';
3598
3599 /* collect value */
3600 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3601 value->area[value->data] = '\0';
3602
3603 /* perform update */
3604 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3605 if (pat_ref_find_elt(ref, key->area) != NULL)
3606 /* update entry if it exists */
3607 pat_ref_set(ref, key->area, value->area, NULL);
3608 else
3609 /* insert a new entry */
3610 pat_ref_add(ref, key->area, value->area, NULL);
3611 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3612 free_trash_chunk(key);
3613 free_trash_chunk(value);
3614 break;
3615 }
3616
3617 case ACT_HTTP_REDIR:
3618 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003619 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003620 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003621 goto end;
3622
3623 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3624 /* Note: only the first valid tracking parameter of each
3625 * applies.
3626 */
3627 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3628 struct stktable *t;
3629 struct stksess *ts;
3630 struct stktable_key *key;
3631 void *ptr;
3632
3633 t = rule->arg.trk_ctr.table.t;
3634 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL,
3635 rule->arg.trk_ctr.expr, NULL);
3636
3637 if (key && (ts = stktable_get_entry(t, key))) {
3638 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3639
3640 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3641
3642 /* let's count a new HTTP request as it's the first time we do it */
3643 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3644 if (ptr)
3645 stktable_data_cast(ptr, http_req_cnt)++;
3646
3647 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3648 if (ptr)
3649 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3650 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3651
3652 /* When the client triggers a 4xx from the server, it's most often due
3653 * to a missing object or permission. These events should be tracked
3654 * because if they happen often, it may indicate a brute force or a
3655 * vulnerability scan. Normally this is done when receiving the response
3656 * but here we're tracking after this ought to have been done so we have
3657 * to do it on purpose.
3658 */
3659 if ((unsigned)(txn->status - 400) < 100) {
3660 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3661 if (ptr)
3662 stktable_data_cast(ptr, http_err_cnt)++;
3663
3664 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3665 if (ptr)
3666 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3667 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3668 }
3669
3670 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3671
3672 /* If data was modified, we need to touch to re-schedule sync */
3673 stktable_touch_local(t, ts, 0);
3674
3675 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3676 if (sess->fe != s->be)
3677 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3678 }
3679 }
3680 break;
3681
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003682 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003683 default:
3684 break;
3685 }
3686 }
3687
3688 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003689 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003690 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003691 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003692
Christopher Faulet3e964192018-10-24 11:39:23 +02003693 /* we reached the end of the rules, nothing to report */
3694 return rule_ret;
3695}
3696
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003697/*
3698 * Manage client-side cookie. It can impact performance by about 2% so it is
3699 * desirable to call it only when needed. This code is quite complex because
3700 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3701 * highly recommended not to touch this part without a good reason !
3702 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003703static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003704{
3705 struct session *sess = s->sess;
3706 struct http_txn *txn = s->txn;
3707 struct htx *htx;
3708 struct http_hdr_ctx ctx;
3709 char *hdr_beg, *hdr_end, *del_from;
3710 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3711 int preserve_hdr;
3712
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003713 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003714 ctx.blk = NULL;
3715 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003716 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003717 del_from = NULL; /* nothing to be deleted */
3718 preserve_hdr = 0; /* assume we may kill the whole header */
3719
3720 /* Now look for cookies. Conforming to RFC2109, we have to support
3721 * attributes whose name begin with a '$', and associate them with
3722 * the right cookie, if we want to delete this cookie.
3723 * So there are 3 cases for each cookie read :
3724 * 1) it's a special attribute, beginning with a '$' : ignore it.
3725 * 2) it's a server id cookie that we *MAY* want to delete : save
3726 * some pointers on it (last semi-colon, beginning of cookie...)
3727 * 3) it's an application cookie : we *MAY* have to delete a previous
3728 * "special" cookie.
3729 * At the end of loop, if a "special" cookie remains, we may have to
3730 * remove it. If no application cookie persists in the header, we
3731 * *MUST* delete it.
3732 *
3733 * Note: RFC2965 is unclear about the processing of spaces around
3734 * the equal sign in the ATTR=VALUE form. A careful inspection of
3735 * the RFC explicitly allows spaces before it, and not within the
3736 * tokens (attrs or values). An inspection of RFC2109 allows that
3737 * too but section 10.1.3 lets one think that spaces may be allowed
3738 * after the equal sign too, resulting in some (rare) buggy
3739 * implementations trying to do that. So let's do what servers do.
3740 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3741 * allowed quoted strings in values, with any possible character
3742 * after a backslash, including control chars and delimitors, which
3743 * causes parsing to become ambiguous. Browsers also allow spaces
3744 * within values even without quotes.
3745 *
3746 * We have to keep multiple pointers in order to support cookie
3747 * removal at the beginning, middle or end of header without
3748 * corrupting the header. All of these headers are valid :
3749 *
3750 * hdr_beg hdr_end
3751 * | |
3752 * v |
3753 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3754 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3755 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3756 * | | | | | | |
3757 * | | | | | | |
3758 * | | | | | | +--> next
3759 * | | | | | +----> val_end
3760 * | | | | +-----------> val_beg
3761 * | | | +--------------> equal
3762 * | | +----------------> att_end
3763 * | +---------------------> att_beg
3764 * +--------------------------> prev
3765 *
3766 */
3767 hdr_beg = ctx.value.ptr;
3768 hdr_end = hdr_beg + ctx.value.len;
3769 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3770 /* Iterate through all cookies on this line */
3771
3772 /* find att_beg */
3773 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003774 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003775 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003776 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003777
3778 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3779 att_beg++;
3780
3781 /* find att_end : this is the first character after the last non
3782 * space before the equal. It may be equal to hdr_end.
3783 */
3784 equal = att_end = att_beg;
3785 while (equal < hdr_end) {
3786 if (*equal == '=' || *equal == ',' || *equal == ';')
3787 break;
3788 if (HTTP_IS_SPHT(*equal++))
3789 continue;
3790 att_end = equal;
3791 }
3792
3793 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3794 * is between <att_beg> and <equal>, both may be identical.
3795 */
3796 /* look for end of cookie if there is an equal sign */
3797 if (equal < hdr_end && *equal == '=') {
3798 /* look for the beginning of the value */
3799 val_beg = equal + 1;
3800 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3801 val_beg++;
3802
3803 /* find the end of the value, respecting quotes */
3804 next = http_find_cookie_value_end(val_beg, hdr_end);
3805
3806 /* make val_end point to the first white space or delimitor after the value */
3807 val_end = next;
3808 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3809 val_end--;
3810 }
3811 else
3812 val_beg = val_end = next = equal;
3813
3814 /* We have nothing to do with attributes beginning with
3815 * '$'. However, they will automatically be removed if a
3816 * header before them is removed, since they're supposed
3817 * to be linked together.
3818 */
3819 if (*att_beg == '$')
3820 continue;
3821
3822 /* Ignore cookies with no equal sign */
3823 if (equal == next) {
3824 /* This is not our cookie, so we must preserve it. But if we already
3825 * scheduled another cookie for removal, we cannot remove the
3826 * complete header, but we can remove the previous block itself.
3827 */
3828 preserve_hdr = 1;
3829 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003830 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003831 val_end += delta;
3832 next += delta;
3833 hdr_end += delta;
3834 prev = del_from;
3835 del_from = NULL;
3836 }
3837 continue;
3838 }
3839
3840 /* if there are spaces around the equal sign, we need to
3841 * strip them otherwise we'll get trouble for cookie captures,
3842 * or even for rewrites. Since this happens extremely rarely,
3843 * it does not hurt performance.
3844 */
3845 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3846 int stripped_before = 0;
3847 int stripped_after = 0;
3848
3849 if (att_end != equal) {
3850 memmove(att_end, equal, hdr_end - equal);
3851 stripped_before = (att_end - equal);
3852 equal += stripped_before;
3853 val_beg += stripped_before;
3854 }
3855
3856 if (val_beg > equal + 1) {
3857 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3858 stripped_after = (equal + 1) - val_beg;
3859 val_beg += stripped_after;
3860 stripped_before += stripped_after;
3861 }
3862
3863 val_end += stripped_before;
3864 next += stripped_before;
3865 hdr_end += stripped_before;
3866 }
3867 /* now everything is as on the diagram above */
3868
3869 /* First, let's see if we want to capture this cookie. We check
3870 * that we don't already have a client side cookie, because we
3871 * can only capture one. Also as an optimisation, we ignore
3872 * cookies shorter than the declared name.
3873 */
3874 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3875 (val_end - att_beg >= sess->fe->capture_namelen) &&
3876 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3877 int log_len = val_end - att_beg;
3878
3879 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3880 ha_alert("HTTP logging : out of memory.\n");
3881 } else {
3882 if (log_len > sess->fe->capture_len)
3883 log_len = sess->fe->capture_len;
3884 memcpy(txn->cli_cookie, att_beg, log_len);
3885 txn->cli_cookie[log_len] = 0;
3886 }
3887 }
3888
3889 /* Persistence cookies in passive, rewrite or insert mode have the
3890 * following form :
3891 *
3892 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3893 *
3894 * For cookies in prefix mode, the form is :
3895 *
3896 * Cookie: NAME=SRV~VALUE
3897 */
3898 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3899 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3900 struct server *srv = s->be->srv;
3901 char *delim;
3902
3903 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3904 * have the server ID between val_beg and delim, and the original cookie between
3905 * delim+1 and val_end. Otherwise, delim==val_end :
3906 *
3907 * hdr_beg
3908 * |
3909 * v
3910 * NAME=SRV; # in all but prefix modes
3911 * NAME=SRV~OPAQUE ; # in prefix mode
3912 * || || | |+-> next
3913 * || || | +--> val_end
3914 * || || +---------> delim
3915 * || |+------------> val_beg
3916 * || +-------------> att_end = equal
3917 * |+-----------------> att_beg
3918 * +------------------> prev
3919 *
3920 */
3921 if (s->be->ck_opts & PR_CK_PFX) {
3922 for (delim = val_beg; delim < val_end; delim++)
3923 if (*delim == COOKIE_DELIM)
3924 break;
3925 }
3926 else {
3927 char *vbar1;
3928 delim = val_end;
3929 /* Now check if the cookie contains a date field, which would
3930 * appear after a vertical bar ('|') just after the server name
3931 * and before the delimiter.
3932 */
3933 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3934 if (vbar1) {
3935 /* OK, so left of the bar is the server's cookie and
3936 * right is the last seen date. It is a base64 encoded
3937 * 30-bit value representing the UNIX date since the
3938 * epoch in 4-second quantities.
3939 */
3940 int val;
3941 delim = vbar1++;
3942 if (val_end - vbar1 >= 5) {
3943 val = b64tos30(vbar1);
3944 if (val > 0)
3945 txn->cookie_last_date = val << 2;
3946 }
3947 /* look for a second vertical bar */
3948 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3949 if (vbar1 && (val_end - vbar1 > 5)) {
3950 val = b64tos30(vbar1 + 1);
3951 if (val > 0)
3952 txn->cookie_first_date = val << 2;
3953 }
3954 }
3955 }
3956
3957 /* if the cookie has an expiration date and the proxy wants to check
3958 * it, then we do that now. We first check if the cookie is too old,
3959 * then only if it has expired. We detect strict overflow because the
3960 * time resolution here is not great (4 seconds). Cookies with dates
3961 * in the future are ignored if their offset is beyond one day. This
3962 * allows an admin to fix timezone issues without expiring everyone
3963 * and at the same time avoids keeping unwanted side effects for too
3964 * long.
3965 */
3966 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3967 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3968 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3969 txn->flags &= ~TX_CK_MASK;
3970 txn->flags |= TX_CK_OLD;
3971 delim = val_beg; // let's pretend we have not found the cookie
3972 txn->cookie_first_date = 0;
3973 txn->cookie_last_date = 0;
3974 }
3975 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3976 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3977 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3978 txn->flags &= ~TX_CK_MASK;
3979 txn->flags |= TX_CK_EXPIRED;
3980 delim = val_beg; // let's pretend we have not found the cookie
3981 txn->cookie_first_date = 0;
3982 txn->cookie_last_date = 0;
3983 }
3984
3985 /* Here, we'll look for the first running server which supports the cookie.
3986 * This allows to share a same cookie between several servers, for example
3987 * to dedicate backup servers to specific servers only.
3988 * However, to prevent clients from sticking to cookie-less backup server
3989 * when they have incidentely learned an empty cookie, we simply ignore
3990 * empty cookies and mark them as invalid.
3991 * The same behaviour is applied when persistence must be ignored.
3992 */
3993 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3994 srv = NULL;
3995
3996 while (srv) {
3997 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3998 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3999 if ((srv->cur_state != SRV_ST_STOPPED) ||
4000 (s->be->options & PR_O_PERSIST) ||
4001 (s->flags & SF_FORCE_PRST)) {
4002 /* we found the server and we can use it */
4003 txn->flags &= ~TX_CK_MASK;
4004 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
4005 s->flags |= SF_DIRECT | SF_ASSIGNED;
4006 s->target = &srv->obj_type;
4007 break;
4008 } else {
4009 /* we found a server, but it's down,
4010 * mark it as such and go on in case
4011 * another one is available.
4012 */
4013 txn->flags &= ~TX_CK_MASK;
4014 txn->flags |= TX_CK_DOWN;
4015 }
4016 }
4017 srv = srv->next;
4018 }
4019
4020 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
4021 /* no server matched this cookie or we deliberately skipped it */
4022 txn->flags &= ~TX_CK_MASK;
4023 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
4024 txn->flags |= TX_CK_UNUSED;
4025 else
4026 txn->flags |= TX_CK_INVALID;
4027 }
4028
4029 /* depending on the cookie mode, we may have to either :
4030 * - delete the complete cookie if we're in insert+indirect mode, so that
4031 * the server never sees it ;
4032 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08004033 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004034 * if we're in cookie prefix mode
4035 */
4036 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
4037 int delta; /* negative */
4038
4039 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
4040 delta = val_beg - (delim + 1);
4041 val_end += delta;
4042 next += delta;
4043 hdr_end += delta;
4044 del_from = NULL;
4045 preserve_hdr = 1; /* we want to keep this cookie */
4046 }
4047 else if (del_from == NULL &&
4048 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
4049 del_from = prev;
4050 }
4051 }
4052 else {
4053 /* This is not our cookie, so we must preserve it. But if we already
4054 * scheduled another cookie for removal, we cannot remove the
4055 * complete header, but we can remove the previous block itself.
4056 */
4057 preserve_hdr = 1;
4058
4059 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004060 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004061 if (att_beg >= del_from)
4062 att_beg += delta;
4063 if (att_end >= del_from)
4064 att_end += delta;
4065 val_beg += delta;
4066 val_end += delta;
4067 next += delta;
4068 hdr_end += delta;
4069 prev = del_from;
4070 del_from = NULL;
4071 }
4072 }
4073
4074 /* continue with next cookie on this header line */
4075 att_beg = next;
4076 } /* for each cookie */
4077
4078
4079 /* There are no more cookies on this line.
4080 * We may still have one (or several) marked for deletion at the
4081 * end of the line. We must do this now in two ways :
4082 * - if some cookies must be preserved, we only delete from the
4083 * mark to the end of line ;
4084 * - if nothing needs to be preserved, simply delete the whole header
4085 */
4086 if (del_from) {
4087 hdr_end = (preserve_hdr ? del_from : hdr_beg);
4088 }
4089 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02004090 if (hdr_beg != hdr_end)
4091 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004092 else
4093 http_remove_header(htx, &ctx);
4094 }
4095 } /* for each "Cookie header */
4096}
4097
4098/*
4099 * Manage server-side cookies. It can impact performance by about 2% so it is
4100 * desirable to call it only when needed. This function is also used when we
4101 * just need to know if there is a cookie (eg: for check-cache).
4102 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004103static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004104{
4105 struct session *sess = s->sess;
4106 struct http_txn *txn = s->txn;
4107 struct htx *htx;
4108 struct http_hdr_ctx ctx;
4109 struct server *srv;
4110 char *hdr_beg, *hdr_end;
4111 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02004112 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004113
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004114 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004115
4116 ctx.blk = NULL;
4117 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02004118 int is_first = 1;
4119
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004120 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
4121 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
4122 break;
4123 is_cookie2 = 1;
4124 }
4125
4126 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
4127 * <prev> points to the colon.
4128 */
4129 txn->flags |= TX_SCK_PRESENT;
4130
4131 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
4132 * check-cache is enabled) and we are not interested in checking
4133 * them. Warning, the cookie capture is declared in the frontend.
4134 */
4135 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
4136 break;
4137
4138 /* OK so now we know we have to process this response cookie.
4139 * The format of the Set-Cookie header is slightly different
4140 * from the format of the Cookie header in that it does not
4141 * support the comma as a cookie delimiter (thus the header
4142 * cannot be folded) because the Expires attribute described in
4143 * the original Netscape's spec may contain an unquoted date
4144 * with a comma inside. We have to live with this because
4145 * many browsers don't support Max-Age and some browsers don't
4146 * support quoted strings. However the Set-Cookie2 header is
4147 * clean.
4148 *
4149 * We have to keep multiple pointers in order to support cookie
4150 * removal at the beginning, middle or end of header without
4151 * corrupting the header (in case of set-cookie2). A special
4152 * pointer, <scav> points to the beginning of the set-cookie-av
4153 * fields after the first semi-colon. The <next> pointer points
4154 * either to the end of line (set-cookie) or next unquoted comma
4155 * (set-cookie2). All of these headers are valid :
4156 *
4157 * hdr_beg hdr_end
4158 * | |
4159 * v |
4160 * NAME1 = VALUE 1 ; Secure; Path="/" |
4161 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
4162 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
4163 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
4164 * | | | | | | | |
4165 * | | | | | | | +-> next
4166 * | | | | | | +------------> scav
4167 * | | | | | +--------------> val_end
4168 * | | | | +--------------------> val_beg
4169 * | | | +----------------------> equal
4170 * | | +------------------------> att_end
4171 * | +----------------------------> att_beg
4172 * +------------------------------> prev
4173 * -------------------------------> hdr_beg
4174 */
4175 hdr_beg = ctx.value.ptr;
4176 hdr_end = hdr_beg + ctx.value.len;
4177 for (prev = hdr_beg; prev < hdr_end; prev = next) {
4178
4179 /* Iterate through all cookies on this line */
4180
4181 /* find att_beg */
4182 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02004183 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004184 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02004185 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004186
4187 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
4188 att_beg++;
4189
4190 /* find att_end : this is the first character after the last non
4191 * space before the equal. It may be equal to hdr_end.
4192 */
4193 equal = att_end = att_beg;
4194
4195 while (equal < hdr_end) {
4196 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
4197 break;
4198 if (HTTP_IS_SPHT(*equal++))
4199 continue;
4200 att_end = equal;
4201 }
4202
4203 /* here, <equal> points to '=', a delimitor or the end. <att_end>
4204 * is between <att_beg> and <equal>, both may be identical.
4205 */
4206
4207 /* look for end of cookie if there is an equal sign */
4208 if (equal < hdr_end && *equal == '=') {
4209 /* look for the beginning of the value */
4210 val_beg = equal + 1;
4211 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
4212 val_beg++;
4213
4214 /* find the end of the value, respecting quotes */
4215 next = http_find_cookie_value_end(val_beg, hdr_end);
4216
4217 /* make val_end point to the first white space or delimitor after the value */
4218 val_end = next;
4219 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
4220 val_end--;
4221 }
4222 else {
4223 /* <equal> points to next comma, semi-colon or EOL */
4224 val_beg = val_end = next = equal;
4225 }
4226
4227 if (next < hdr_end) {
4228 /* Set-Cookie2 supports multiple cookies, and <next> points to
4229 * a colon or semi-colon before the end. So skip all attr-value
4230 * pairs and look for the next comma. For Set-Cookie, since
4231 * commas are permitted in values, skip to the end.
4232 */
4233 if (is_cookie2)
4234 next = http_find_hdr_value_end(next, hdr_end);
4235 else
4236 next = hdr_end;
4237 }
4238
4239 /* Now everything is as on the diagram above */
4240
4241 /* Ignore cookies with no equal sign */
4242 if (equal == val_end)
4243 continue;
4244
4245 /* If there are spaces around the equal sign, we need to
4246 * strip them otherwise we'll get trouble for cookie captures,
4247 * or even for rewrites. Since this happens extremely rarely,
4248 * it does not hurt performance.
4249 */
4250 if (unlikely(att_end != equal || val_beg > equal + 1)) {
4251 int stripped_before = 0;
4252 int stripped_after = 0;
4253
4254 if (att_end != equal) {
4255 memmove(att_end, equal, hdr_end - equal);
4256 stripped_before = (att_end - equal);
4257 equal += stripped_before;
4258 val_beg += stripped_before;
4259 }
4260
4261 if (val_beg > equal + 1) {
4262 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
4263 stripped_after = (equal + 1) - val_beg;
4264 val_beg += stripped_after;
4265 stripped_before += stripped_after;
4266 }
4267
4268 val_end += stripped_before;
4269 next += stripped_before;
4270 hdr_end += stripped_before;
4271
Christopher Faulet3e2638e2019-06-18 09:49:16 +02004272 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004273 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004274 }
4275
4276 /* First, let's see if we want to capture this cookie. We check
4277 * that we don't already have a server side cookie, because we
4278 * can only capture one. Also as an optimisation, we ignore
4279 * cookies shorter than the declared name.
4280 */
4281 if (sess->fe->capture_name != NULL &&
4282 txn->srv_cookie == NULL &&
4283 (val_end - att_beg >= sess->fe->capture_namelen) &&
4284 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
4285 int log_len = val_end - att_beg;
4286 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
4287 ha_alert("HTTP logging : out of memory.\n");
4288 }
4289 else {
4290 if (log_len > sess->fe->capture_len)
4291 log_len = sess->fe->capture_len;
4292 memcpy(txn->srv_cookie, att_beg, log_len);
4293 txn->srv_cookie[log_len] = 0;
4294 }
4295 }
4296
4297 srv = objt_server(s->target);
4298 /* now check if we need to process it for persistence */
4299 if (!(s->flags & SF_IGNORE_PRST) &&
4300 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
4301 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
4302 /* assume passive cookie by default */
4303 txn->flags &= ~TX_SCK_MASK;
4304 txn->flags |= TX_SCK_FOUND;
4305
4306 /* If the cookie is in insert mode on a known server, we'll delete
4307 * this occurrence because we'll insert another one later.
4308 * We'll delete it too if the "indirect" option is set and we're in
4309 * a direct access.
4310 */
4311 if (s->be->ck_opts & PR_CK_PSV) {
4312 /* The "preserve" flag was set, we don't want to touch the
4313 * server's cookie.
4314 */
4315 }
4316 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
4317 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
4318 /* this cookie must be deleted */
4319 if (prev == hdr_beg && next == hdr_end) {
4320 /* whole header */
4321 http_remove_header(htx, &ctx);
4322 /* note: while both invalid now, <next> and <hdr_end>
4323 * are still equal, so the for() will stop as expected.
4324 */
4325 } else {
4326 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004327 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004328 next = prev;
4329 hdr_end += delta;
4330 }
4331 txn->flags &= ~TX_SCK_MASK;
4332 txn->flags |= TX_SCK_DELETED;
4333 /* and go on with next cookie */
4334 }
4335 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
4336 /* replace bytes val_beg->val_end with the cookie name associated
4337 * with this server since we know it.
4338 */
4339 int sliding, delta;
4340
4341 ctx.value = ist2(val_beg, val_end - val_beg);
4342 ctx.lws_before = ctx.lws_after = 0;
4343 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
4344 delta = srv->cklen - (val_end - val_beg);
4345 sliding = (ctx.value.ptr - val_beg);
4346 hdr_beg += sliding;
4347 val_beg += sliding;
4348 next += sliding + delta;
4349 hdr_end += sliding + delta;
4350
4351 txn->flags &= ~TX_SCK_MASK;
4352 txn->flags |= TX_SCK_REPLACED;
4353 }
4354 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
4355 /* insert the cookie name associated with this server
4356 * before existing cookie, and insert a delimiter between them..
4357 */
4358 int sliding, delta;
4359 ctx.value = ist2(val_beg, 0);
4360 ctx.lws_before = ctx.lws_after = 0;
4361 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
4362 delta = srv->cklen + 1;
4363 sliding = (ctx.value.ptr - val_beg);
4364 hdr_beg += sliding;
4365 val_beg += sliding;
4366 next += sliding + delta;
4367 hdr_end += sliding + delta;
4368
4369 val_beg[srv->cklen] = COOKIE_DELIM;
4370 txn->flags &= ~TX_SCK_MASK;
4371 txn->flags |= TX_SCK_REPLACED;
4372 }
4373 }
4374 /* that's done for this cookie, check the next one on the same
4375 * line when next != hdr_end (only if is_cookie2).
4376 */
4377 }
4378 }
4379}
4380
Christopher Faulet25a02f62018-10-24 12:00:25 +02004381/*
4382 * Parses the Cache-Control and Pragma request header fields to determine if
4383 * the request may be served from the cache and/or if it is cacheable. Updates
4384 * s->txn->flags.
4385 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004386void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004387{
4388 struct http_txn *txn = s->txn;
4389 struct htx *htx;
4390 int32_t pos;
4391 int pragma_found, cc_found, i;
4392
4393 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
4394 return; /* nothing more to do here */
4395
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004396 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02004397 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02004398 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004399 struct htx_blk *blk = htx_get_blk(htx, pos);
4400 enum htx_blk_type type = htx_get_blk_type(blk);
4401 struct ist n, v;
4402
4403 if (type == HTX_BLK_EOH)
4404 break;
4405 if (type != HTX_BLK_HDR)
4406 continue;
4407
4408 n = htx_get_blk_name(htx, blk);
4409 v = htx_get_blk_value(htx, blk);
4410
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004411 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004412 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4413 pragma_found = 1;
4414 continue;
4415 }
4416 }
4417
4418 /* Don't use the cache and don't try to store if we found the
4419 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004420 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004421 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4422 txn->flags |= TX_CACHE_IGNORE;
4423 continue;
4424 }
4425
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004426 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004427 continue;
4428
4429 /* OK, right now we know we have a cache-control header */
4430 cc_found = 1;
4431 if (!v.len) /* no info */
4432 continue;
4433
4434 i = 0;
4435 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4436 !isspace((unsigned char)*(v.ptr+i)))
4437 i++;
4438
4439 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
4440 * values after max-age, max-stale nor min-fresh, we simply don't
4441 * use the cache when they're specified.
4442 */
4443 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
4444 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4445 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
4446 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
4447 txn->flags |= TX_CACHE_IGNORE;
4448 continue;
4449 }
4450
4451 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
4452 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4453 continue;
4454 }
4455 }
4456
4457 /* RFC7234#5.4:
4458 * When the Cache-Control header field is also present and
4459 * understood in a request, Pragma is ignored.
4460 * When the Cache-Control header field is not present in a
4461 * request, caches MUST consider the no-cache request
4462 * pragma-directive as having the same effect as if
4463 * "Cache-Control: no-cache" were present.
4464 */
4465 if (!cc_found && pragma_found)
4466 txn->flags |= TX_CACHE_IGNORE;
4467}
4468
4469/*
4470 * Check if response is cacheable or not. Updates s->txn->flags.
4471 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004472void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004473{
4474 struct http_txn *txn = s->txn;
4475 struct htx *htx;
4476 int32_t pos;
4477 int i;
4478
4479 if (txn->status < 200) {
4480 /* do not try to cache interim responses! */
4481 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4482 return;
4483 }
4484
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004485 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004486 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004487 struct htx_blk *blk = htx_get_blk(htx, pos);
4488 enum htx_blk_type type = htx_get_blk_type(blk);
4489 struct ist n, v;
4490
4491 if (type == HTX_BLK_EOH)
4492 break;
4493 if (type != HTX_BLK_HDR)
4494 continue;
4495
4496 n = htx_get_blk_name(htx, blk);
4497 v = htx_get_blk_value(htx, blk);
4498
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004499 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004500 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4501 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4502 return;
4503 }
4504 }
4505
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004506 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004507 continue;
4508
4509 /* OK, right now we know we have a cache-control header */
4510 if (!v.len) /* no info */
4511 continue;
4512
4513 i = 0;
4514 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4515 !isspace((unsigned char)*(v.ptr+i)))
4516 i++;
4517
4518 /* we have a complete value between v.ptr and (v.ptr+i) */
4519 if (i < v.len && *(v.ptr + i) == '=') {
4520 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
4521 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
4522 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4523 continue;
4524 }
4525
4526 /* we have something of the form no-cache="set-cookie" */
4527 if ((v.len >= 21) &&
4528 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
4529 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
4530 txn->flags &= ~TX_CACHE_COOK;
4531 continue;
4532 }
4533
4534 /* OK, so we know that either p2 points to the end of string or to a comma */
4535 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
4536 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4537 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
4538 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4539 return;
4540 }
4541
4542 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4543 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4544 continue;
4545 }
4546 }
4547}
4548
Christopher Faulet377c5a52018-10-24 21:21:30 +02004549/*
4550 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4551 * for the current backend.
4552 *
4553 * It is assumed that the request is either a HEAD, GET, or POST and that the
4554 * uri_auth field is valid.
4555 *
4556 * Returns 1 if stats should be provided, otherwise 0.
4557 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004558static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004559{
4560 struct uri_auth *uri_auth = backend->uri_auth;
4561 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004562 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004563 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004564
4565 if (!uri_auth)
4566 return 0;
4567
4568 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4569 return 0;
4570
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004571 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004572 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004573 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01004574 if (*uri_auth->uri_prefix == '/')
4575 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004576
4577 /* check URI size */
4578 if (uri_auth->uri_len > uri.len)
4579 return 0;
4580
4581 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4582 return 0;
4583
4584 return 1;
4585}
4586
4587/* This function prepares an applet to handle the stats. It can deal with the
4588 * "100-continue" expectation, check that admin rules are met for POST requests,
4589 * and program a response message if something was unexpected. It cannot fail
4590 * and always relies on the stats applet to complete the job. It does not touch
4591 * analysers nor counters, which are left to the caller. It does not touch
4592 * s->target which is supposed to already point to the stats applet. The caller
4593 * is expected to have already assigned an appctx to the stream.
4594 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004595static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004596{
4597 struct stats_admin_rule *stats_admin_rule;
4598 struct stream_interface *si = &s->si[1];
4599 struct session *sess = s->sess;
4600 struct http_txn *txn = s->txn;
4601 struct http_msg *msg = &txn->req;
4602 struct uri_auth *uri_auth = s->be->uri_auth;
4603 const char *h, *lookup, *end;
4604 struct appctx *appctx;
4605 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004606 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004607
4608 appctx = si_appctx(si);
4609 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4610 appctx->st1 = appctx->st2 = 0;
4611 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004612 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004613 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4614 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4615 appctx->ctx.stats.flags |= STAT_CHUNKED;
4616
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004617 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004618 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004619 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4620 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004621
4622 for (h = lookup; h <= end - 3; h++) {
4623 if (memcmp(h, ";up", 3) == 0) {
4624 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4625 break;
4626 }
4627 }
4628
4629 if (uri_auth->refresh) {
4630 for (h = lookup; h <= end - 10; h++) {
4631 if (memcmp(h, ";norefresh", 10) == 0) {
4632 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4633 break;
4634 }
4635 }
4636 }
4637
4638 for (h = lookup; h <= end - 4; h++) {
4639 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004640 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004641 break;
4642 }
4643 }
4644
4645 for (h = lookup; h <= end - 6; h++) {
4646 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004647 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004648 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4649 break;
4650 }
4651 }
4652
Christopher Faulet6338a082019-09-09 15:50:54 +02004653 for (h = lookup; h <= end - 5; h++) {
4654 if (memcmp(h, ";json", 5) == 0) {
4655 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4656 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4657 break;
4658 }
4659 }
4660
4661 for (h = lookup; h <= end - 12; h++) {
4662 if (memcmp(h, ";json-schema", 12) == 0) {
4663 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4664 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4665 break;
4666 }
4667 }
4668
Christopher Faulet377c5a52018-10-24 21:21:30 +02004669 for (h = lookup; h <= end - 8; h++) {
4670 if (memcmp(h, ";st=", 4) == 0) {
4671 int i;
4672 h += 4;
4673 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4674 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4675 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4676 appctx->ctx.stats.st_code = i;
4677 break;
4678 }
4679 }
4680 break;
4681 }
4682 }
4683
4684 appctx->ctx.stats.scope_str = 0;
4685 appctx->ctx.stats.scope_len = 0;
4686 for (h = lookup; h <= end - 8; h++) {
4687 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4688 int itx = 0;
4689 const char *h2;
4690 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4691 const char *err;
4692
4693 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4694 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004695 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4696 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004697 if (*h == ';' || *h == '&' || *h == ' ')
4698 break;
4699 itx++;
4700 h++;
4701 }
4702
4703 if (itx > STAT_SCOPE_TXT_MAXLEN)
4704 itx = STAT_SCOPE_TXT_MAXLEN;
4705 appctx->ctx.stats.scope_len = itx;
4706
4707 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4708 memcpy(scope_txt, h2, itx);
4709 scope_txt[itx] = '\0';
4710 err = invalid_char(scope_txt);
4711 if (err) {
4712 /* bad char in search text => clear scope */
4713 appctx->ctx.stats.scope_str = 0;
4714 appctx->ctx.stats.scope_len = 0;
4715 }
4716 break;
4717 }
4718 }
4719
4720 /* now check whether we have some admin rules for this request */
4721 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4722 int ret = 1;
4723
4724 if (stats_admin_rule->cond) {
4725 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4726 ret = acl_pass(ret);
4727 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4728 ret = !ret;
4729 }
4730
4731 if (ret) {
4732 /* no rule, or the rule matches */
4733 appctx->ctx.stats.flags |= STAT_ADMIN;
4734 break;
4735 }
4736 }
4737
Christopher Faulet5d45e382019-02-27 15:15:23 +01004738 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4739 appctx->st0 = STAT_HTTP_HEAD;
4740 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004741 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004742 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004743 if (msg->msg_state < HTTP_MSG_DATA)
4744 req->analysers |= AN_REQ_HTTP_BODY;
4745 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004746 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004747 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004748 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4749 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4750 appctx->st0 = STAT_HTTP_LAST;
4751 }
4752 }
4753 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004754 /* Unsupported method */
4755 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4756 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4757 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004758 }
4759
4760 s->task->nice = -32; /* small boost for HTTP statistics */
4761 return 1;
4762}
4763
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004764void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004765{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004766 struct channel *req = &s->req;
4767 struct channel *res = &s->res;
4768 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004769 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004770 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004771 struct ist path, location;
4772 unsigned int flags;
4773 size_t data;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004774
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004775 /*
4776 * Create the location
4777 */
4778 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004779
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004780 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004781 /* special prefix "/" means don't change URL */
4782 srv = __objt_server(s->target);
4783 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4784 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4785 return;
4786 }
4787
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004788 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004789 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004790 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004791 path = http_get_path(htx_sl_req_uri(sl));
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004792 if (!path.ptr)
4793 return;
4794
4795 if (!chunk_memcat(&trash, path.ptr, path.len))
4796 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004797 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004798
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004799 /*
4800 * Create the 302 respone
4801 */
4802 htx = htx_from_buf(&res->buf);
4803 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4804 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4805 ist("HTTP/1.1"), ist("302"), ist("Found"));
4806 if (!sl)
4807 goto fail;
4808 sl->info.res.status = 302;
4809 s->txn->status = 302;
4810
4811 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4812 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4813 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4814 !htx_add_header(htx, ist("Location"), location))
4815 goto fail;
4816
4817 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4818 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004819
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004820 /*
4821 * Send the message
4822 */
4823 data = htx->data - co_data(res);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004824 c_adv(res, data);
4825 res->total += data;
4826
4827 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004828 si_shutr(si);
4829 si_shutw(si);
4830 si->err_type = SI_ET_NONE;
4831 si->state = SI_ST_CLO;
4832
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004833 channel_auto_read(req);
4834 channel_abort(req);
4835 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004836 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004837 channel_auto_read(res);
4838 channel_auto_close(res);
4839
4840 if (!(s->flags & SF_ERR_MASK))
4841 s->flags |= SF_ERR_LOCAL;
4842 if (!(s->flags & SF_FINST_MASK))
4843 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004844
4845 /* FIXME: we should increase a counter of redirects per server and per backend. */
4846 srv_inc_sess_ctr(srv);
4847 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004848 return;
4849
4850 fail:
4851 /* If an error occurred, remove the incomplete HTTP response from the
4852 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004853 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004854}
4855
Christopher Fauletf2824e62018-10-01 12:12:37 +02004856/* This function terminates the request because it was completly analyzed or
4857 * because an error was triggered during the body forwarding.
4858 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004859static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004860{
4861 struct channel *chn = &s->req;
4862 struct http_txn *txn = s->txn;
4863
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004864 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004865
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004866 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4867 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004868 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004869 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004870 goto end;
4871 }
4872
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004873 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4874 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004875 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004876 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004877
4878 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004879 /* No need to read anymore, the request was completely parsed.
4880 * We can shut the read side unless we want to abort_on_close,
4881 * or we have a POST request. The issue with POST requests is
4882 * that some browsers still send a CRLF after the request, and
4883 * this CRLF must be read so that it does not remain in the kernel
4884 * buffers, otherwise a close could cause an RST on some systems
4885 * (eg: Linux).
4886 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004887 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004888 channel_dont_read(chn);
4889
4890 /* if the server closes the connection, we want to immediately react
4891 * and close the socket to save packets and syscalls.
4892 */
4893 s->si[1].flags |= SI_FL_NOHALF;
4894
4895 /* In any case we've finished parsing the request so we must
4896 * disable Nagle when sending data because 1) we're not going
4897 * to shut this side, and 2) the server is waiting for us to
4898 * send pending data.
4899 */
4900 chn->flags |= CF_NEVER_WAIT;
4901
Christopher Fauletd01ce402019-01-02 17:44:13 +01004902 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4903 /* The server has not finished to respond, so we
4904 * don't want to move in order not to upset it.
4905 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004906 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004907 return;
4908 }
4909
Christopher Fauletf2824e62018-10-01 12:12:37 +02004910 /* When we get here, it means that both the request and the
4911 * response have finished receiving. Depending on the connection
4912 * mode, we'll have to wait for the last bytes to leave in either
4913 * direction, and sometimes for a close to be effective.
4914 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004915 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004916 /* Tunnel mode will not have any analyser so it needs to
4917 * poll for reads.
4918 */
4919 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004920 if (b_data(&chn->buf)) {
4921 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004922 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004923 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004924 txn->req.msg_state = HTTP_MSG_TUNNEL;
4925 }
4926 else {
4927 /* we're not expecting any new data to come for this
4928 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004929 *
4930 * However, there is an exception if the response
4931 * length is undefined. In this case, we need to wait
4932 * the close from the server. The response will be
4933 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004934 */
4935 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4936 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004937 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004938
4939 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4940 channel_shutr_now(chn);
4941 channel_shutw_now(chn);
4942 }
4943 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004944 goto check_channel_flags;
4945 }
4946
4947 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4948 http_msg_closing:
4949 /* nothing else to forward, just waiting for the output buffer
4950 * to be empty and for the shutw_now to take effect.
4951 */
4952 if (channel_is_empty(chn)) {
4953 txn->req.msg_state = HTTP_MSG_CLOSED;
4954 goto http_msg_closed;
4955 }
4956 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004957 txn->req.msg_state = HTTP_MSG_ERROR;
4958 goto end;
4959 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004960 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004961 return;
4962 }
4963
4964 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4965 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004966 /* if we don't know whether the server will close, we need to hard close */
4967 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4968 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004969 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004970 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004971 channel_dont_read(chn);
4972 goto end;
4973 }
4974
4975 check_channel_flags:
4976 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4977 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4978 /* if we've just closed an output, let's switch */
4979 txn->req.msg_state = HTTP_MSG_CLOSING;
4980 goto http_msg_closing;
4981 }
4982
4983 end:
4984 chn->analysers &= AN_REQ_FLT_END;
4985 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4986 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4987 channel_auto_close(chn);
4988 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004989 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004990}
4991
4992
4993/* This function terminates the response because it was completly analyzed or
4994 * because an error was triggered during the body forwarding.
4995 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004996static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004997{
4998 struct channel *chn = &s->res;
4999 struct http_txn *txn = s->txn;
5000
Christopher Fauleteea8fc72019-11-05 16:18:10 +01005001 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005002
Christopher Fauletb42a8b62018-11-19 21:59:00 +01005003 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
5004 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005005 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02005006 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005007 goto end;
5008 }
5009
Christopher Fauleteea8fc72019-11-05 16:18:10 +01005010 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
5011 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005012 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01005013 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02005014
5015 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5016 /* In theory, we don't need to read anymore, but we must
5017 * still monitor the server connection for a possible close
5018 * while the request is being uploaded, so we don't disable
5019 * reading.
5020 */
5021 /* channel_dont_read(chn); */
5022
5023 if (txn->req.msg_state < HTTP_MSG_DONE) {
5024 /* The client seems to still be sending data, probably
5025 * because we got an error response during an upload.
5026 * We have the choice of either breaking the connection
5027 * or letting it pass through. Let's do the later.
5028 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01005029 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005030 return;
5031 }
5032
5033 /* When we get here, it means that both the request and the
5034 * response have finished receiving. Depending on the connection
5035 * mode, we'll have to wait for the last bytes to leave in either
5036 * direction, and sometimes for a close to be effective.
5037 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02005038 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02005039 channel_auto_read(chn);
5040 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01005041 if (b_data(&chn->buf)) {
5042 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02005043 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01005044 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02005045 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
5046 }
5047 else {
5048 /* we're not expecting any new data to come for this
5049 * transaction, so we can close it.
5050 */
5051 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5052 channel_shutr_now(chn);
5053 channel_shutw_now(chn);
5054 }
5055 }
5056 goto check_channel_flags;
5057 }
5058
5059 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5060 http_msg_closing:
5061 /* nothing else to forward, just waiting for the output buffer
5062 * to be empty and for the shutw_now to take effect.
5063 */
5064 if (channel_is_empty(chn)) {
5065 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5066 goto http_msg_closed;
5067 }
5068 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02005069 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01005070 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01005071 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01005072 if (strm_sess(s)->listener->counters)
5073 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005074 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01005075 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005076 goto end;
5077 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01005078 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005079 return;
5080 }
5081
5082 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5083 http_msg_closed:
5084 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005085 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02005086 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005087 goto end;
5088 }
5089
5090 check_channel_flags:
5091 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
5092 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
5093 /* if we've just closed an output, let's switch */
5094 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5095 goto http_msg_closing;
5096 }
5097
5098 end:
5099 chn->analysers &= AN_RES_FLT_END;
5100 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
5101 chn->analysers |= AN_RES_FLT_XFER_DATA;
5102 channel_auto_close(chn);
5103 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01005104 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005105}
5106
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005107void http_server_error(struct stream *s, struct stream_interface *si, int err,
5108 int finst, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02005109{
5110 channel_auto_read(si_oc(si));
5111 channel_abort(si_oc(si));
5112 channel_auto_close(si_oc(si));
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005113 channel_htx_erase(si_oc(si), htxbuf(&(si_oc(si))->buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02005114 channel_auto_close(si_ic(si));
5115 channel_auto_read(si_ic(si));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005116
5117 /* <msg> is an HTX structure. So we copy it in the response's
5118 * channel */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02005119 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02005120 struct channel *chn = si_ic(si);
5121 struct htx *htx;
5122
Christopher Fauletaed82cf2018-11-30 22:22:32 +01005123 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005124 chn->buf.data = msg->data;
5125 memcpy(chn->buf.area, msg->area, msg->data);
5126 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02005127 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02005128 c_adv(chn, htx->data);
5129 chn->total += htx->data;
5130 }
5131 if (!(s->flags & SF_ERR_MASK))
5132 s->flags |= err;
5133 if (!(s->flags & SF_FINST_MASK))
5134 s->flags |= finst;
5135}
5136
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005137void http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02005138{
5139 channel_auto_read(&s->req);
5140 channel_abort(&s->req);
5141 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005142 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
5143 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02005144
5145 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005146
5147 /* <msg> is an HTX structure. So we copy it in the response's
5148 * channel */
5149 /* FIXME: It is a problem for now if there is some outgoing data */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02005150 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02005151 struct channel *chn = &s->res;
5152 struct htx *htx;
5153
Christopher Fauletaed82cf2018-11-30 22:22:32 +01005154 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005155 chn->buf.data = msg->data;
5156 memcpy(chn->buf.area, msg->area, msg->data);
5157 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02005158 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02005159 c_adv(chn, htx->data);
5160 chn->total += htx->data;
5161 }
5162
5163 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
5164 channel_auto_read(&s->res);
5165 channel_auto_close(&s->res);
5166 channel_shutr_now(&s->res);
5167}
5168
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005169struct buffer *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005170{
5171 const int msgnum = http_get_status_idx(s->txn->status);
5172
5173 if (s->be->errmsg[msgnum].area)
5174 return &s->be->errmsg[msgnum];
5175 else if (strm_fe(s)->errmsg[msgnum].area)
5176 return &strm_fe(s)->errmsg[msgnum];
5177 else
Christopher Fauletf7346382019-07-17 22:02:08 +02005178 return &http_err_chunks[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005179}
5180
Christopher Faulet304cc402019-07-15 15:46:28 +02005181/* Return the error message corresponding to si->err_type. It is assumed
5182 * that the server side is closed. Note that err_type is actually a
5183 * bitmask, where almost only aborts may be cumulated with other
5184 * values. We consider that aborted operations are more important
5185 * than timeouts or errors due to the fact that nobody else in the
5186 * logs might explain incomplete retries. All others should avoid
5187 * being cumulated. It should normally not be possible to have multiple
5188 * aborts at once, but just in case, the first one in sequence is reported.
5189 * Note that connection errors appearing on the second request of a keep-alive
5190 * connection are not reported since this allows the client to retry.
5191 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005192void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02005193{
5194 int err_type = si->err_type;
5195
5196 /* set s->txn->status for http_error_message(s) */
5197 s->txn->status = 503;
5198
5199 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005200 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
5201 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005202 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005203 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
5204 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5205 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005206 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005207 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
5208 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005209 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005210 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
5211 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005212 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005213 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
5214 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5215 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005216 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005217 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
5218 (s->flags & SF_SRV_REUSED) ? NULL :
5219 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005220 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005221 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
5222 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5223 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005224 else { /* SI_ET_CONN_OTHER and others */
5225 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005226 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
5227 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005228 }
5229}
5230
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005231
Christopher Faulet4a28a532019-03-01 11:19:40 +01005232/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
5233 * on success and -1 on error.
5234 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005235static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01005236{
5237 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
5238 * then we must send an HTTP/1.1 100 Continue intermediate response.
5239 */
5240 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
5241 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
5242 struct ist hdr = { .ptr = "Expect", .len = 6 };
5243 struct http_hdr_ctx ctx;
5244
5245 ctx.blk = NULL;
5246 /* Expect is allowed in 1.1, look for it */
5247 if (http_find_header(htx, hdr, &ctx, 0) &&
5248 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005249 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01005250 return -1;
5251 http_remove_header(htx, &ctx);
5252 }
5253 }
5254 return 0;
5255}
5256
Christopher Faulet23a3c792018-11-28 10:01:23 +01005257/* Send a 100-Continue response to the client. It returns 0 on success and -1
5258 * on error. The response channel is updated accordingly.
5259 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005260static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01005261{
5262 struct channel *res = &s->res;
5263 struct htx *htx = htx_from_buf(&res->buf);
5264 struct htx_sl *sl;
5265 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
5266 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
5267 size_t data;
5268
5269 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5270 ist("HTTP/1.1"), ist("100"), ist("Continue"));
5271 if (!sl)
5272 goto fail;
5273 sl->info.res.status = 100;
5274
Christopher Faulet1d5ec092019-06-26 14:23:54 +02005275 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01005276 goto fail;
5277
5278 data = htx->data - co_data(res);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005279 c_adv(res, data);
5280 res->total += data;
5281 return 0;
5282
5283 fail:
5284 /* If an error occurred, remove the incomplete HTTP response from the
5285 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005286 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005287 return -1;
5288}
5289
Christopher Faulet12c51e22018-11-28 15:59:42 +01005290
5291/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
5292 * ont whether we use a proxy or not. It returns 0 on success and -1 on
5293 * error. The response channel is updated accordingly.
5294 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005295static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
Christopher Faulet12c51e22018-11-28 15:59:42 +01005296{
5297 struct channel *res = &s->res;
5298 struct htx *htx = htx_from_buf(&res->buf);
5299 struct htx_sl *sl;
5300 struct ist code, body;
5301 int status;
5302 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5303 size_t data;
5304
5305 if (!(s->txn->flags & TX_USE_PX_CONN)) {
5306 status = 401;
5307 code = ist("401");
5308 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
5309 "You need a valid user and password to access this content.\n"
5310 "</body></html>\n");
5311 }
5312 else {
5313 status = 407;
5314 code = ist("407");
5315 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
5316 "You need a valid user and password to access this content.\n"
5317 "</body></html>\n");
5318 }
5319
5320 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5321 ist("HTTP/1.1"), code, ist("Unauthorized"));
5322 if (!sl)
5323 goto fail;
5324 sl->info.res.status = status;
5325 s->txn->status = status;
5326
5327 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
5328 goto fail;
5329
Willy Tarreaub5ba2b02019-06-11 16:08:25 +02005330 if (!htx_add_header(htx, ist("Content-length"), ist("112")) ||
5331 !htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Faulet12c51e22018-11-28 15:59:42 +01005332 !htx_add_header(htx, ist("Connection"), ist("close")) ||
Jérôme Magnin86cef232018-12-28 14:49:08 +01005333 !htx_add_header(htx, ist("Content-Type"), ist("text/html")))
5334 goto fail;
5335 if (status == 401 && !htx_add_header(htx, ist("WWW-Authenticate"), ist2(trash.area, trash.data)))
5336 goto fail;
5337 if (status == 407 && !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005338 goto fail;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02005339 if (!htx_add_endof(htx, HTX_BLK_EOH))
5340 goto fail;
5341
5342 while (body.len) {
5343 size_t sent = htx_add_data(htx, body);
5344 if (!sent)
5345 goto fail;
5346 body.ptr += sent;
5347 body.len -= sent;
5348 }
5349
5350 if (!htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005351 goto fail;
5352
Christopher Faulet06511812019-10-16 09:38:27 +02005353 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet12c51e22018-11-28 15:59:42 +01005354 data = htx->data - co_data(res);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005355 c_adv(res, data);
5356 res->total += data;
5357
5358 channel_auto_read(&s->req);
5359 channel_abort(&s->req);
5360 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005361 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet12c51e22018-11-28 15:59:42 +01005362
5363 res->wex = tick_add_ifset(now_ms, res->wto);
5364 channel_auto_read(res);
5365 channel_auto_close(res);
5366 channel_shutr_now(res);
5367 return 0;
5368
5369 fail:
5370 /* If an error occurred, remove the incomplete HTTP response from the
5371 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005372 channel_htx_truncate(res, htx);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005373 return -1;
5374}
5375
Christopher Faulet0f226952018-10-22 09:29:56 +02005376/*
5377 * Capture headers from message <htx> according to header list <cap_hdr>, and
5378 * fill the <cap> pointers appropriately.
5379 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005380static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02005381{
5382 struct cap_hdr *h;
5383 int32_t pos;
5384
Christopher Fauleta3f15502019-05-13 15:27:23 +02005385 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02005386 struct htx_blk *blk = htx_get_blk(htx, pos);
5387 enum htx_blk_type type = htx_get_blk_type(blk);
5388 struct ist n, v;
5389
5390 if (type == HTX_BLK_EOH)
5391 break;
5392 if (type != HTX_BLK_HDR)
5393 continue;
5394
5395 n = htx_get_blk_name(htx, blk);
5396
5397 for (h = cap_hdr; h; h = h->next) {
5398 if (h->namelen && (h->namelen == n.len) &&
5399 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
5400 if (cap[h->index] == NULL)
5401 cap[h->index] =
5402 pool_alloc(h->pool);
5403
5404 if (cap[h->index] == NULL) {
5405 ha_alert("HTTP capture : out of memory.\n");
5406 break;
5407 }
5408
5409 v = htx_get_blk_value(htx, blk);
5410 if (v.len > h->len)
5411 v.len = h->len;
5412
5413 memcpy(cap[h->index], v.ptr, v.len);
5414 cap[h->index][v.len]=0;
5415 }
5416 }
5417 }
5418}
5419
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005420/* Delete a value in a header between delimiters <from> and <next>. The header
5421 * itself is delimited by <start> and <end> pointers. The number of characters
5422 * displaced is returned, and the pointer to the first delimiter is updated if
5423 * required. The function tries as much as possible to respect the following
5424 * principles :
5425 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5426 * in which case <next> is simply removed
5427 * - set exactly one space character after the new first delimiter, unless there
5428 * are not enough characters in the block being moved to do so.
5429 * - remove unneeded spaces before the previous delimiter and after the new
5430 * one.
5431 *
5432 * It is the caller's responsibility to ensure that :
5433 * - <from> points to a valid delimiter or <start> ;
5434 * - <next> points to a valid delimiter or <end> ;
5435 * - there are non-space chars before <from>.
5436 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005437static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005438{
5439 char *prev = *from;
5440
5441 if (prev == start) {
5442 /* We're removing the first value. eat the semicolon, if <next>
5443 * is lower than <end> */
5444 if (next < end)
5445 next++;
5446
5447 while (next < end && HTTP_IS_SPHT(*next))
5448 next++;
5449 }
5450 else {
5451 /* Remove useless spaces before the old delimiter. */
5452 while (HTTP_IS_SPHT(*(prev-1)))
5453 prev--;
5454 *from = prev;
5455
5456 /* copy the delimiter and if possible a space if we're
5457 * not at the end of the line.
5458 */
5459 if (next < end) {
5460 *prev++ = *next++;
5461 if (prev + 1 < next)
5462 *prev++ = ' ';
5463 while (next < end && HTTP_IS_SPHT(*next))
5464 next++;
5465 }
5466 }
5467 memmove(prev, next, end - next);
5468 return (prev - next);
5469}
5470
Christopher Faulet0f226952018-10-22 09:29:56 +02005471
5472/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005473 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005474 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005475static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005476{
5477 struct ist dst = ist2(str, 0);
5478
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005479 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005480 goto end;
5481 if (dst.len + 1 > len)
5482 goto end;
5483 dst.ptr[dst.len++] = ' ';
5484
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005485 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005486 goto end;
5487 if (dst.len + 1 > len)
5488 goto end;
5489 dst.ptr[dst.len++] = ' ';
5490
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005491 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005492 end:
5493 return dst.len;
5494}
5495
5496/*
5497 * Print a debug line with a start line.
5498 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005499static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005500{
5501 struct session *sess = strm_sess(s);
5502 int max;
5503
5504 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5505 dir,
5506 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5507 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5508
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005509 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005510 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005511 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005512 trash.area[trash.data++] = ' ';
5513
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005514 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005515 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005516 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005517 trash.area[trash.data++] = ' ';
5518
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005519 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005520 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005521 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005522 trash.area[trash.data++] = '\n';
5523
5524 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5525}
5526
5527/*
5528 * Print a debug line with a header.
5529 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005530static 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 +02005531{
5532 struct session *sess = strm_sess(s);
5533 int max;
5534
5535 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5536 dir,
5537 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5538 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5539
5540 max = n.len;
5541 UBOUND(max, trash.size - trash.data - 3);
5542 chunk_memcat(&trash, n.ptr, max);
5543 trash.area[trash.data++] = ':';
5544 trash.area[trash.data++] = ' ';
5545
5546 max = v.len;
5547 UBOUND(max, trash.size - trash.data - 1);
5548 chunk_memcat(&trash, v.ptr, max);
5549 trash.area[trash.data++] = '\n';
5550
5551 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5552}
5553
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005554/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5555 * In case of allocation failure, everything allocated is freed and NULL is
5556 * returned. Otherwise the new transaction is assigned to the stream and
5557 * returned.
5558 */
5559struct http_txn *http_alloc_txn(struct stream *s)
5560{
5561 struct http_txn *txn = s->txn;
5562
5563 if (txn)
5564 return txn;
5565
5566 txn = pool_alloc(pool_head_http_txn);
5567 if (!txn)
5568 return txn;
5569
5570 s->txn = txn;
5571 return txn;
5572}
5573
5574void http_txn_reset_req(struct http_txn *txn)
5575{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005576 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005577 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5578}
5579
5580void http_txn_reset_res(struct http_txn *txn)
5581{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005582 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005583 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5584}
5585
5586/*
5587 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5588 * the required fields are properly allocated and that we only need to (re)init
5589 * them. This should be used before processing any new request.
5590 */
5591void http_init_txn(struct stream *s)
5592{
5593 struct http_txn *txn = s->txn;
5594 struct conn_stream *cs = objt_cs(s->si[0].end);
5595
5596 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5597 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5598 : 0);
5599 txn->status = -1;
5600 *(unsigned int *)txn->cache_hash = 0;
5601
5602 txn->cookie_first_date = 0;
5603 txn->cookie_last_date = 0;
5604
5605 txn->srv_cookie = NULL;
5606 txn->cli_cookie = NULL;
5607 txn->uri = NULL;
5608
5609 http_txn_reset_req(txn);
5610 http_txn_reset_res(txn);
5611
5612 txn->req.chn = &s->req;
5613 txn->rsp.chn = &s->res;
5614
5615 txn->auth.method = HTTP_AUTH_UNKNOWN;
5616
5617 vars_init(&s->vars_txn, SCOPE_TXN);
5618 vars_init(&s->vars_reqres, SCOPE_REQ);
5619}
5620
5621/* to be used at the end of a transaction */
5622void http_end_txn(struct stream *s)
5623{
5624 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005625
5626 /* these ones will have been dynamically allocated */
5627 pool_free(pool_head_requri, txn->uri);
5628 pool_free(pool_head_capture, txn->cli_cookie);
5629 pool_free(pool_head_capture, txn->srv_cookie);
5630 pool_free(pool_head_uniqueid, s->unique_id);
5631
5632 s->unique_id = NULL;
5633 txn->uri = NULL;
5634 txn->srv_cookie = NULL;
5635 txn->cli_cookie = NULL;
5636
Christopher Faulet59399252019-11-07 14:27:52 +01005637 if (!LIST_ISEMPTY(&s->vars_txn.head))
5638 vars_prune(&s->vars_txn, s->sess, s);
5639 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5640 vars_prune(&s->vars_reqres, s->sess, s);
5641}
5642
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005643
5644DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
5645DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
Christopher Faulet0f226952018-10-22 09:29:56 +02005646
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005647__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005648static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005649{
5650}
5651
5652
5653/*
5654 * Local variables:
5655 * c-indent-level: 8
5656 * c-basic-offset: 8
5657 * End:
5658 */