blob: fa418bb5d0cea2f1390a6d6a2a342b8d989fddc3 [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 Faulete00d06c2019-12-16 17:18:42 +01002889int http_res_set_status(unsigned int status, const char *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? */
2899 if (reason == NULL)
2900 reason = http_get_reason(status);
2901
Christopher Faulete00d06c2019-12-16 17:18:42 +01002902 if (!http_replace_res_status(htx, ist2(trash.area, trash.data)))
2903 return -1;
2904 if (!http_replace_res_reason(htx, ist2(reason, strlen(reason))))
2905 return -1;
2906 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002907}
2908
Christopher Faulet3e964192018-10-24 11:39:23 +02002909/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2910 * transaction <txn>. Returns the verdict of the first rule that prevents
2911 * further processing of the request (auth, deny, ...), and defaults to
2912 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2913 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2914 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2915 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2916 * status.
2917 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002918static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
2919 struct stream *s, int *deny_status)
Christopher Faulet3e964192018-10-24 11:39:23 +02002920{
2921 struct session *sess = strm_sess(s);
2922 struct http_txn *txn = s->txn;
2923 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002924 struct act_rule *rule;
2925 struct http_hdr_ctx ctx;
2926 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002927 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
2928 int act_flags = 0;
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002929 int early_hints = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002930
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002931 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002932
2933 /* If "the current_rule_list" match the executed rule list, we are in
2934 * resume condition. If a resume is needed it is always in the action
2935 * and never in the ACL or converters. In this case, we initialise the
2936 * current rule, and go to the action execution point.
2937 */
2938 if (s->current_rule) {
2939 rule = s->current_rule;
2940 s->current_rule = NULL;
2941 if (s->current_rule_list == rules)
2942 goto resume_execution;
2943 }
2944 s->current_rule_list = rules;
2945
Christopher Faulet46f95542019-12-20 10:07:22 +01002946 /* start the ruleset evaluation in soft mode */
2947 txn->req.flags |= HTTP_MSGF_SOFT_RW;
2948
Christopher Faulet3e964192018-10-24 11:39:23 +02002949 list_for_each_entry(rule, rules, list) {
2950 /* check optional condition */
2951 if (rule->cond) {
2952 int ret;
2953
2954 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2955 ret = acl_pass(ret);
2956
2957 if (rule->cond->pol == ACL_COND_UNLESS)
2958 ret = !ret;
2959
2960 if (!ret) /* condition not matched */
2961 continue;
2962 }
2963
2964 act_flags |= ACT_FLAG_FIRST;
2965 resume_execution:
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002966 if (early_hints && rule->action != ACT_HTTP_EARLY_HINT) {
2967 early_hints = 0;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002968 if (http_reply_103_early_hints(&s->res) == -1) {
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01002969 rule_ret = HTTP_RULE_RES_BADREQ;
2970 goto end;
2971 }
2972 }
2973
Christopher Faulet3e964192018-10-24 11:39:23 +02002974 switch (rule->action) {
2975 case ACT_ACTION_ALLOW:
2976 rule_ret = HTTP_RULE_RES_STOP;
2977 goto end;
2978
2979 case ACT_ACTION_DENY:
2980 if (deny_status)
2981 *deny_status = rule->deny_status;
2982 rule_ret = HTTP_RULE_RES_DENY;
2983 goto end;
2984
2985 case ACT_HTTP_REQ_TARPIT:
2986 txn->flags |= TX_CLTARPIT;
2987 if (deny_status)
2988 *deny_status = rule->deny_status;
2989 rule_ret = HTTP_RULE_RES_DENY;
2990 goto end;
2991
2992 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002993 /* Auth might be performed on regular http-req rules as well as on stats */
2994 auth_realm = rule->arg.auth.realm;
2995 if (!auth_realm) {
2996 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2997 auth_realm = STATS_DEFAULT_REALM;
2998 else
2999 auth_realm = px->id;
3000 }
3001 /* send 401/407 depending on whether we use a proxy or not. We still
3002 * count one error, because normal browsing won't significantly
3003 * increase the counter but brute force attempts will.
3004 */
Christopher Faulet3e964192018-10-24 11:39:23 +02003005 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003006 if (http_reply_40x_unauthorized(s, auth_realm) == -1)
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003007 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet12c51e22018-11-28 15:59:42 +01003008 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02003009 goto end;
3010
3011 case ACT_HTTP_REDIR:
Christopher Faulet3e964192018-10-24 11:39:23 +02003012 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003013 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003014 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003015 goto end;
3016
3017 case ACT_HTTP_SET_NICE:
3018 s->task->nice = rule->arg.nice;
3019 break;
3020
3021 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003022 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Christopher Faulet3e964192018-10-24 11:39:23 +02003023 break;
3024
3025 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003026 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Christopher Faulet3e964192018-10-24 11:39:23 +02003027 break;
3028
3029 case ACT_HTTP_SET_LOGL:
3030 s->logs.level = rule->arg.loglevel;
3031 break;
3032
3033 case ACT_HTTP_REPLACE_HDR:
3034 case ACT_HTTP_REPLACE_VAL:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003035 if (http_transform_header(s, &s->req, htx,
3036 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
3037 &rule->arg.hdr_add.fmt,
3038 rule->arg.hdr_add.re, rule->action)) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003039 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003040 goto end;
3041 }
3042 break;
3043
3044 case ACT_HTTP_DEL_HDR:
3045 /* remove all occurrences of the header */
3046 ctx.blk = NULL;
3047 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3048 http_remove_header(htx, &ctx);
3049 break;
3050
3051 case ACT_HTTP_SET_HDR:
3052 case ACT_HTTP_ADD_HDR: {
3053 /* The scope of the trash buffer must be limited to this function. The
3054 * build_logline() function can execute a lot of other function which
3055 * can use the trash buffer. So for limiting the scope of this global
3056 * buffer, we build first the header value using build_logline, and
3057 * after we store the header name.
3058 */
3059 struct buffer *replace;
3060 struct ist n, v;
3061
3062 replace = alloc_trash_chunk();
3063 if (!replace) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003064 if (!(s->flags & SF_ERR_MASK))
3065 s->flags |= SF_ERR_RESOURCE;
3066 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003067 goto end;
3068 }
3069
3070 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
3071 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3072 v = ist2(replace->area, replace->data);
3073
3074 if (rule->action == ACT_HTTP_SET_HDR) {
3075 /* remove all occurrences of the header */
3076 ctx.blk = NULL;
3077 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3078 http_remove_header(htx, &ctx);
3079 }
3080
3081 if (!http_add_header(htx, n, v)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003082 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01003083 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003084 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003085 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003086 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Fauleta00071e2019-12-12 16:41:00 +01003087
3088 if (!(txn->req.flags & HTTP_MSGF_SOFT_RW)) {
3089 rule_ret = HTTP_RULE_RES_ERROR;
3090 goto end;
3091 }
Christopher Faulet3e964192018-10-24 11:39:23 +02003092 }
3093 free_trash_chunk(replace);
3094 break;
3095 }
3096
3097 case ACT_HTTP_DEL_ACL:
3098 case ACT_HTTP_DEL_MAP: {
3099 struct pat_ref *ref;
3100 struct buffer *key;
3101
3102 /* collect reference */
3103 ref = pat_ref_lookup(rule->arg.map.ref);
3104 if (!ref)
3105 continue;
3106
3107 /* allocate key */
3108 key = alloc_trash_chunk();
3109 if (!key) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003110 if (!(s->flags & SF_ERR_MASK))
3111 s->flags |= SF_ERR_RESOURCE;
3112 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003113 goto end;
3114 }
3115
3116 /* collect key */
3117 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3118 key->area[key->data] = '\0';
3119
3120 /* perform update */
3121 /* returned code: 1=ok, 0=ko */
3122 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3123 pat_ref_delete(ref, key->area);
3124 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3125
3126 free_trash_chunk(key);
3127 break;
3128 }
3129
3130 case ACT_HTTP_ADD_ACL: {
3131 struct pat_ref *ref;
3132 struct buffer *key;
3133
3134 /* collect reference */
3135 ref = pat_ref_lookup(rule->arg.map.ref);
3136 if (!ref)
3137 continue;
3138
3139 /* allocate key */
3140 key = alloc_trash_chunk();
3141 if (!key) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003142 if (!(s->flags & SF_ERR_MASK))
3143 s->flags |= SF_ERR_RESOURCE;
3144 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003145 goto end;
3146 }
3147
3148 /* collect key */
3149 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3150 key->area[key->data] = '\0';
3151
3152 /* perform update */
3153 /* add entry only if it does not already exist */
3154 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3155 if (pat_ref_find_elt(ref, key->area) == NULL)
3156 pat_ref_add(ref, key->area, NULL, NULL);
3157 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3158
3159 free_trash_chunk(key);
3160 break;
3161 }
3162
3163 case ACT_HTTP_SET_MAP: {
3164 struct pat_ref *ref;
3165 struct buffer *key, *value;
3166
3167 /* collect reference */
3168 ref = pat_ref_lookup(rule->arg.map.ref);
3169 if (!ref)
3170 continue;
3171
3172 /* allocate key */
3173 key = alloc_trash_chunk();
3174 if (!key) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003175 if (!(s->flags & SF_ERR_MASK))
3176 s->flags |= SF_ERR_RESOURCE;
3177 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003178 goto end;
3179 }
3180
3181 /* allocate value */
3182 value = alloc_trash_chunk();
3183 if (!value) {
3184 free_trash_chunk(key);
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003185 if (!(s->flags & SF_ERR_MASK))
3186 s->flags |= SF_ERR_RESOURCE;
3187 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003188 goto end;
3189 }
3190
3191 /* collect key */
3192 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3193 key->area[key->data] = '\0';
3194
3195 /* collect value */
3196 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3197 value->area[value->data] = '\0';
3198
3199 /* perform update */
Christopher Faulete84289e2019-04-19 14:50:55 +02003200 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003201 if (pat_ref_find_elt(ref, key->area) != NULL)
3202 /* update entry if it exists */
3203 pat_ref_set(ref, key->area, value->area, NULL);
3204 else
3205 /* insert a new entry */
3206 pat_ref_add(ref, key->area, value->area, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02003207 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003208 free_trash_chunk(key);
3209 free_trash_chunk(value);
3210 break;
3211 }
3212
3213 case ACT_HTTP_EARLY_HINT:
3214 if (!(txn->req.flags & HTTP_MSGF_VER_11))
3215 break;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003216 early_hints = http_add_early_hint_header(s, early_hints,
3217 ist2(rule->arg.early_hint.name, rule->arg.early_hint.name_len),
3218 &rule->arg.early_hint.fmt);
Christopher Fauletee9b5bf2018-11-28 13:55:14 +01003219 if (early_hints == -1) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003220 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003221 goto end;
3222 }
3223 break;
3224
3225 case ACT_CUSTOM:
3226 if ((s->req.flags & CF_READ_ERROR) ||
3227 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Christopher Faulet3e964192018-10-24 11:39:23 +02003228 (px->options & PR_O_ABRT_CLOSE)))
3229 act_flags |= ACT_FLAG_FINAL;
3230
3231 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Christopher Faulet3e964192018-10-24 11:39:23 +02003232 case ACT_RET_CONT:
3233 break;
3234 case ACT_RET_STOP:
Christopher Faulet8f1aa772019-07-04 11:27:15 +02003235 rule_ret = HTTP_RULE_RES_STOP;
3236 goto end;
Christopher Faulet3e964192018-10-24 11:39:23 +02003237 case ACT_RET_YIELD:
3238 s->current_rule = rule;
3239 rule_ret = HTTP_RULE_RES_YIELD;
3240 goto end;
Christopher Faulet74f67af2019-12-16 13:07:14 +01003241 case ACT_RET_ERR:
3242 rule_ret = HTTP_RULE_RES_ERROR;
3243 goto end;
3244 case ACT_RET_DONE:
3245 rule_ret = HTTP_RULE_RES_DONE;
3246 goto end;
3247 case ACT_RET_DENY:
3248 rule_ret = HTTP_RULE_RES_DENY;
3249 goto end;
3250 case ACT_RET_ABRT:
3251 rule_ret = HTTP_RULE_RES_ABRT;
3252 goto end;
3253 case ACT_RET_INV:
3254 rule_ret = HTTP_RULE_RES_BADREQ;
3255 goto end;
Christopher Faulet3e964192018-10-24 11:39:23 +02003256 }
3257 break;
3258
3259 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3260 /* Note: only the first valid tracking parameter of each
3261 * applies.
3262 */
3263
3264 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3265 struct stktable *t;
3266 struct stksess *ts;
3267 struct stktable_key *key;
3268 void *ptr1, *ptr2;
3269
3270 t = rule->arg.trk_ctr.table.t;
3271 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | SMP_OPT_FINAL,
3272 rule->arg.trk_ctr.expr, NULL);
3273
3274 if (key && (ts = stktable_get_entry(t, key))) {
3275 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3276
3277 /* let's count a new HTTP request as it's the first time we do it */
3278 ptr1 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3279 ptr2 = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3280 if (ptr1 || ptr2) {
3281 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3282
3283 if (ptr1)
3284 stktable_data_cast(ptr1, http_req_cnt)++;
3285
3286 if (ptr2)
3287 update_freq_ctr_period(&stktable_data_cast(ptr2, http_req_rate),
3288 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3289
3290 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3291
3292 /* If data was modified, we need to touch to re-schedule sync */
3293 stktable_touch_local(t, ts, 0);
3294 }
3295
3296 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3297 if (sess->fe != s->be)
3298 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3299 }
3300 }
3301 break;
3302
Joseph Herlantc42c0e92018-11-25 10:43:27 -08003303 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003304 default:
3305 break;
3306 }
3307 }
3308
3309 end:
3310 if (early_hints) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003311 if (http_reply_103_early_hints(&s->res) == -1)
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003312 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003313 }
3314
Christopher Faulet46f95542019-12-20 10:07:22 +01003315 /* if the ruleset evaluation is finished reset the soft mode */
3316 if (rule_ret != HTTP_RULE_RES_YIELD)
3317 txn->req.flags |= HTTP_MSGF_SOFT_RW;
3318
Christopher Faulet3e964192018-10-24 11:39:23 +02003319 /* we reached the end of the rules, nothing to report */
3320 return rule_ret;
3321}
3322
3323/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
3324 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
3325 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
3326 * is returned, the process can continue the evaluation of next rule list. If
3327 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
3328 * is returned, it means the operation could not be processed and a server error
3329 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
3330 * deny rule. If *YIELD is returned, the caller must call again the function
3331 * with the same context.
3332 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003333static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
3334 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02003335{
3336 struct session *sess = strm_sess(s);
3337 struct http_txn *txn = s->txn;
3338 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02003339 struct act_rule *rule;
3340 struct http_hdr_ctx ctx;
3341 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
3342 int act_flags = 0;
3343
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003344 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02003345
3346 /* If "the current_rule_list" match the executed rule list, we are in
3347 * resume condition. If a resume is needed it is always in the action
3348 * and never in the ACL or converters. In this case, we initialise the
3349 * current rule, and go to the action execution point.
3350 */
3351 if (s->current_rule) {
3352 rule = s->current_rule;
3353 s->current_rule = NULL;
3354 if (s->current_rule_list == rules)
3355 goto resume_execution;
3356 }
3357 s->current_rule_list = rules;
3358
Christopher Faulet46f95542019-12-20 10:07:22 +01003359 /* start the ruleset evaluation in soft mode */
3360 txn->rsp.flags |= HTTP_MSGF_SOFT_RW;
3361
Christopher Faulet3e964192018-10-24 11:39:23 +02003362 list_for_each_entry(rule, rules, list) {
3363 /* check optional condition */
3364 if (rule->cond) {
3365 int ret;
3366
3367 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3368 ret = acl_pass(ret);
3369
3370 if (rule->cond->pol == ACL_COND_UNLESS)
3371 ret = !ret;
3372
3373 if (!ret) /* condition not matched */
3374 continue;
3375 }
3376
3377 act_flags |= ACT_FLAG_FIRST;
3378resume_execution:
3379 switch (rule->action) {
3380 case ACT_ACTION_ALLOW:
3381 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3382 goto end;
3383
3384 case ACT_ACTION_DENY:
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003385 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003386 goto end;
3387
3388 case ACT_HTTP_SET_NICE:
3389 s->task->nice = rule->arg.nice;
3390 break;
3391
3392 case ACT_HTTP_SET_TOS:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003393 conn_set_tos(objt_conn(sess->origin), rule->arg.tos);
Christopher Faulet3e964192018-10-24 11:39:23 +02003394 break;
3395
3396 case ACT_HTTP_SET_MARK:
Willy Tarreau1a18b542018-12-11 16:37:42 +01003397 conn_set_mark(objt_conn(sess->origin), rule->arg.mark);
Christopher Faulet3e964192018-10-24 11:39:23 +02003398 break;
3399
3400 case ACT_HTTP_SET_LOGL:
3401 s->logs.level = rule->arg.loglevel;
3402 break;
3403
3404 case ACT_HTTP_REPLACE_HDR:
3405 case ACT_HTTP_REPLACE_VAL:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003406 if (http_transform_header(s, &s->res, htx,
3407 ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len),
3408 &rule->arg.hdr_add.fmt,
3409 rule->arg.hdr_add.re, rule->action)) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003410 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003411 goto end;
3412 }
3413 break;
3414
3415 case ACT_HTTP_DEL_HDR:
3416 /* remove all occurrences of the header */
3417 ctx.blk = NULL;
3418 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3419 http_remove_header(htx, &ctx);
3420 break;
3421
3422 case ACT_HTTP_SET_HDR:
3423 case ACT_HTTP_ADD_HDR: {
3424 struct buffer *replace;
3425 struct ist n, v;
3426
3427 replace = alloc_trash_chunk();
3428 if (!replace) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003429 if (!(s->flags & SF_ERR_MASK))
3430 s->flags |= SF_ERR_RESOURCE;
3431 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003432 goto end;
3433 }
3434
3435 replace->data = build_logline(s, replace->area, replace->size, &rule->arg.hdr_add.fmt);
3436 n = ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3437 v = ist2(replace->area, replace->data);
3438
3439 if (rule->action == ACT_HTTP_SET_HDR) {
3440 /* remove all occurrences of the header */
3441 ctx.blk = NULL;
3442 while (http_find_header(htx, ist2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len), &ctx, 1))
3443 http_remove_header(htx, &ctx);
3444 }
3445
3446 if (!http_add_header(htx, n, v)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01003447 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_rewrites, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01003448 _HA_ATOMIC_ADD(&s->be->be_counters.failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003449 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01003450 _HA_ATOMIC_ADD(&sess->listener->counters->failed_rewrites, 1);
Christopher Faulet3e964192018-10-24 11:39:23 +02003451 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01003452 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_rewrites, 1);
Christopher Fauleta00071e2019-12-12 16:41:00 +01003453
3454 if (!(txn->rsp.flags & HTTP_MSGF_SOFT_RW)) {
3455 rule_ret = HTTP_RULE_RES_ERROR;
3456 goto end;
3457 }
Christopher Faulet3e964192018-10-24 11:39:23 +02003458 }
3459 free_trash_chunk(replace);
3460 break;
3461 }
3462
3463 case ACT_HTTP_DEL_ACL:
3464 case ACT_HTTP_DEL_MAP: {
3465 struct pat_ref *ref;
3466 struct buffer *key;
3467
3468 /* collect reference */
3469 ref = pat_ref_lookup(rule->arg.map.ref);
3470 if (!ref)
3471 continue;
3472
3473 /* allocate key */
3474 key = alloc_trash_chunk();
3475 if (!key) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003476 if (!(s->flags & SF_ERR_MASK))
3477 s->flags |= SF_ERR_RESOURCE;
3478 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003479 goto end;
3480 }
3481
3482 /* collect key */
3483 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3484 key->area[key->data] = '\0';
3485
3486 /* perform update */
3487 /* returned code: 1=ok, 0=ko */
3488 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3489 pat_ref_delete(ref, key->area);
3490 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3491
3492 free_trash_chunk(key);
3493 break;
3494 }
3495
3496 case ACT_HTTP_ADD_ACL: {
3497 struct pat_ref *ref;
3498 struct buffer *key;
3499
3500 /* collect reference */
3501 ref = pat_ref_lookup(rule->arg.map.ref);
3502 if (!ref)
3503 continue;
3504
3505 /* allocate key */
3506 key = alloc_trash_chunk();
3507 if (!key) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003508 if (!(s->flags & SF_ERR_MASK))
3509 s->flags |= SF_ERR_RESOURCE;
3510 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003511 goto end;
3512 }
3513
3514 /* collect key */
3515 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3516 key->area[key->data] = '\0';
3517
3518 /* perform update */
3519 /* check if the entry already exists */
Christopher Faulete84289e2019-04-19 14:50:55 +02003520 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003521 if (pat_ref_find_elt(ref, key->area) == NULL)
3522 pat_ref_add(ref, key->area, NULL, NULL);
Christopher Faulete84289e2019-04-19 14:50:55 +02003523 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Christopher Faulet3e964192018-10-24 11:39:23 +02003524 free_trash_chunk(key);
3525 break;
3526 }
3527
3528 case ACT_HTTP_SET_MAP: {
3529 struct pat_ref *ref;
3530 struct buffer *key, *value;
3531
3532 /* collect reference */
3533 ref = pat_ref_lookup(rule->arg.map.ref);
3534 if (!ref)
3535 continue;
3536
3537 /* allocate key */
3538 key = alloc_trash_chunk();
3539 if (!key) {
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003540 if (!(s->flags & SF_ERR_MASK))
3541 s->flags |= SF_ERR_RESOURCE;
3542 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003543 goto end;
3544 }
3545
3546 /* allocate value */
3547 value = alloc_trash_chunk();
3548 if (!value) {
3549 free_trash_chunk(key);
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003550 if (!(s->flags & SF_ERR_MASK))
3551 s->flags |= SF_ERR_RESOURCE;
3552 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003553 goto end;
3554 }
3555
3556 /* collect key */
3557 key->data = build_logline(s, key->area, key->size, &rule->arg.map.key);
3558 key->area[key->data] = '\0';
3559
3560 /* collect value */
3561 value->data = build_logline(s, value->area, value->size, &rule->arg.map.value);
3562 value->area[value->data] = '\0';
3563
3564 /* perform update */
3565 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
3566 if (pat_ref_find_elt(ref, key->area) != NULL)
3567 /* update entry if it exists */
3568 pat_ref_set(ref, key->area, value->area, NULL);
3569 else
3570 /* insert a new entry */
3571 pat_ref_add(ref, key->area, value->area, NULL);
3572 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
3573 free_trash_chunk(key);
3574 free_trash_chunk(value);
3575 break;
3576 }
3577
3578 case ACT_HTTP_REDIR:
3579 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003580 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003581 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003582 goto end;
3583
3584 case ACT_ACTION_TRK_SC0 ... ACT_ACTION_TRK_SCMAX:
3585 /* Note: only the first valid tracking parameter of each
3586 * applies.
3587 */
3588 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]) == NULL) {
3589 struct stktable *t;
3590 struct stksess *ts;
3591 struct stktable_key *key;
3592 void *ptr;
3593
3594 t = rule->arg.trk_ctr.table.t;
3595 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_RES | SMP_OPT_FINAL,
3596 rule->arg.trk_ctr.expr, NULL);
3597
3598 if (key && (ts = stktable_get_entry(t, key))) {
3599 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
3600
3601 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
3602
3603 /* let's count a new HTTP request as it's the first time we do it */
3604 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3605 if (ptr)
3606 stktable_data_cast(ptr, http_req_cnt)++;
3607
3608 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3609 if (ptr)
3610 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3611 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3612
3613 /* When the client triggers a 4xx from the server, it's most often due
3614 * to a missing object or permission. These events should be tracked
3615 * because if they happen often, it may indicate a brute force or a
3616 * vulnerability scan. Normally this is done when receiving the response
3617 * but here we're tracking after this ought to have been done so we have
3618 * to do it on purpose.
3619 */
3620 if ((unsigned)(txn->status - 400) < 100) {
3621 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
3622 if (ptr)
3623 stktable_data_cast(ptr, http_err_cnt)++;
3624
3625 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
3626 if (ptr)
3627 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
3628 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
3629 }
3630
3631 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
3632
3633 /* If data was modified, we need to touch to re-schedule sync */
3634 stktable_touch_local(t, ts, 0);
3635
3636 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3637 if (sess->fe != s->be)
3638 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3639 }
3640 }
3641 break;
3642
3643 case ACT_CUSTOM:
3644 if ((s->req.flags & CF_READ_ERROR) ||
3645 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
Christopher Faulet3e964192018-10-24 11:39:23 +02003646 (px->options & PR_O_ABRT_CLOSE)))
3647 act_flags |= ACT_FLAG_FINAL;
3648
3649 switch (rule->action_ptr(rule, px, s->sess, s, act_flags)) {
Christopher Faulet3e964192018-10-24 11:39:23 +02003650 case ACT_RET_CONT:
3651 break;
3652 case ACT_RET_STOP:
3653 rule_ret = HTTP_RULE_RES_STOP;
3654 goto end;
3655 case ACT_RET_YIELD:
3656 s->current_rule = rule;
3657 rule_ret = HTTP_RULE_RES_YIELD;
3658 goto end;
Christopher Faulet74f67af2019-12-16 13:07:14 +01003659 case ACT_RET_ERR:
3660 rule_ret = HTTP_RULE_RES_ERROR;
3661 goto end;
3662 case ACT_RET_DONE:
3663 rule_ret = HTTP_RULE_RES_DONE;
3664 goto end;
3665 case ACT_RET_DENY:
3666 rule_ret = HTTP_RULE_RES_DENY;
3667 goto end;
3668 case ACT_RET_ABRT:
3669 rule_ret = HTTP_RULE_RES_ABRT;
3670 goto end;
3671 case ACT_RET_INV:
3672 rule_ret = HTTP_RULE_RES_BADREQ;
3673 goto end;
Christopher Faulet3e964192018-10-24 11:39:23 +02003674 }
3675 break;
3676
Joseph Herlantc42c0e92018-11-25 10:43:27 -08003677 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003678 default:
3679 break;
3680 }
3681 }
3682
3683 end:
Christopher Faulet46f95542019-12-20 10:07:22 +01003684 /* if the ruleset evaluation is finished reset the soft mode */
3685 if (rule_ret != HTTP_RULE_RES_YIELD)
3686 txn->rsp.flags |= HTTP_MSGF_SOFT_RW;
3687
Christopher Faulet3e964192018-10-24 11:39:23 +02003688 /* we reached the end of the rules, nothing to report */
3689 return rule_ret;
3690}
3691
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003692/*
3693 * Manage client-side cookie. It can impact performance by about 2% so it is
3694 * desirable to call it only when needed. This code is quite complex because
3695 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3696 * highly recommended not to touch this part without a good reason !
3697 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003698static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003699{
3700 struct session *sess = s->sess;
3701 struct http_txn *txn = s->txn;
3702 struct htx *htx;
3703 struct http_hdr_ctx ctx;
3704 char *hdr_beg, *hdr_end, *del_from;
3705 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3706 int preserve_hdr;
3707
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003708 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003709 ctx.blk = NULL;
3710 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003711 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003712 del_from = NULL; /* nothing to be deleted */
3713 preserve_hdr = 0; /* assume we may kill the whole header */
3714
3715 /* Now look for cookies. Conforming to RFC2109, we have to support
3716 * attributes whose name begin with a '$', and associate them with
3717 * the right cookie, if we want to delete this cookie.
3718 * So there are 3 cases for each cookie read :
3719 * 1) it's a special attribute, beginning with a '$' : ignore it.
3720 * 2) it's a server id cookie that we *MAY* want to delete : save
3721 * some pointers on it (last semi-colon, beginning of cookie...)
3722 * 3) it's an application cookie : we *MAY* have to delete a previous
3723 * "special" cookie.
3724 * At the end of loop, if a "special" cookie remains, we may have to
3725 * remove it. If no application cookie persists in the header, we
3726 * *MUST* delete it.
3727 *
3728 * Note: RFC2965 is unclear about the processing of spaces around
3729 * the equal sign in the ATTR=VALUE form. A careful inspection of
3730 * the RFC explicitly allows spaces before it, and not within the
3731 * tokens (attrs or values). An inspection of RFC2109 allows that
3732 * too but section 10.1.3 lets one think that spaces may be allowed
3733 * after the equal sign too, resulting in some (rare) buggy
3734 * implementations trying to do that. So let's do what servers do.
3735 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3736 * allowed quoted strings in values, with any possible character
3737 * after a backslash, including control chars and delimitors, which
3738 * causes parsing to become ambiguous. Browsers also allow spaces
3739 * within values even without quotes.
3740 *
3741 * We have to keep multiple pointers in order to support cookie
3742 * removal at the beginning, middle or end of header without
3743 * corrupting the header. All of these headers are valid :
3744 *
3745 * hdr_beg hdr_end
3746 * | |
3747 * v |
3748 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3749 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3750 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3751 * | | | | | | |
3752 * | | | | | | |
3753 * | | | | | | +--> next
3754 * | | | | | +----> val_end
3755 * | | | | +-----------> val_beg
3756 * | | | +--------------> equal
3757 * | | +----------------> att_end
3758 * | +---------------------> att_beg
3759 * +--------------------------> prev
3760 *
3761 */
3762 hdr_beg = ctx.value.ptr;
3763 hdr_end = hdr_beg + ctx.value.len;
3764 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3765 /* Iterate through all cookies on this line */
3766
3767 /* find att_beg */
3768 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003769 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003770 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003771 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003772
3773 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3774 att_beg++;
3775
3776 /* find att_end : this is the first character after the last non
3777 * space before the equal. It may be equal to hdr_end.
3778 */
3779 equal = att_end = att_beg;
3780 while (equal < hdr_end) {
3781 if (*equal == '=' || *equal == ',' || *equal == ';')
3782 break;
3783 if (HTTP_IS_SPHT(*equal++))
3784 continue;
3785 att_end = equal;
3786 }
3787
3788 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3789 * is between <att_beg> and <equal>, both may be identical.
3790 */
3791 /* look for end of cookie if there is an equal sign */
3792 if (equal < hdr_end && *equal == '=') {
3793 /* look for the beginning of the value */
3794 val_beg = equal + 1;
3795 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3796 val_beg++;
3797
3798 /* find the end of the value, respecting quotes */
3799 next = http_find_cookie_value_end(val_beg, hdr_end);
3800
3801 /* make val_end point to the first white space or delimitor after the value */
3802 val_end = next;
3803 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3804 val_end--;
3805 }
3806 else
3807 val_beg = val_end = next = equal;
3808
3809 /* We have nothing to do with attributes beginning with
3810 * '$'. However, they will automatically be removed if a
3811 * header before them is removed, since they're supposed
3812 * to be linked together.
3813 */
3814 if (*att_beg == '$')
3815 continue;
3816
3817 /* Ignore cookies with no equal sign */
3818 if (equal == next) {
3819 /* This is not our cookie, so we must preserve it. But if we already
3820 * scheduled another cookie for removal, we cannot remove the
3821 * complete header, but we can remove the previous block itself.
3822 */
3823 preserve_hdr = 1;
3824 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003825 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003826 val_end += delta;
3827 next += delta;
3828 hdr_end += delta;
3829 prev = del_from;
3830 del_from = NULL;
3831 }
3832 continue;
3833 }
3834
3835 /* if there are spaces around the equal sign, we need to
3836 * strip them otherwise we'll get trouble for cookie captures,
3837 * or even for rewrites. Since this happens extremely rarely,
3838 * it does not hurt performance.
3839 */
3840 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3841 int stripped_before = 0;
3842 int stripped_after = 0;
3843
3844 if (att_end != equal) {
3845 memmove(att_end, equal, hdr_end - equal);
3846 stripped_before = (att_end - equal);
3847 equal += stripped_before;
3848 val_beg += stripped_before;
3849 }
3850
3851 if (val_beg > equal + 1) {
3852 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3853 stripped_after = (equal + 1) - val_beg;
3854 val_beg += stripped_after;
3855 stripped_before += stripped_after;
3856 }
3857
3858 val_end += stripped_before;
3859 next += stripped_before;
3860 hdr_end += stripped_before;
3861 }
3862 /* now everything is as on the diagram above */
3863
3864 /* First, let's see if we want to capture this cookie. We check
3865 * that we don't already have a client side cookie, because we
3866 * can only capture one. Also as an optimisation, we ignore
3867 * cookies shorter than the declared name.
3868 */
3869 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3870 (val_end - att_beg >= sess->fe->capture_namelen) &&
3871 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3872 int log_len = val_end - att_beg;
3873
3874 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3875 ha_alert("HTTP logging : out of memory.\n");
3876 } else {
3877 if (log_len > sess->fe->capture_len)
3878 log_len = sess->fe->capture_len;
3879 memcpy(txn->cli_cookie, att_beg, log_len);
3880 txn->cli_cookie[log_len] = 0;
3881 }
3882 }
3883
3884 /* Persistence cookies in passive, rewrite or insert mode have the
3885 * following form :
3886 *
3887 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3888 *
3889 * For cookies in prefix mode, the form is :
3890 *
3891 * Cookie: NAME=SRV~VALUE
3892 */
3893 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3894 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3895 struct server *srv = s->be->srv;
3896 char *delim;
3897
3898 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3899 * have the server ID between val_beg and delim, and the original cookie between
3900 * delim+1 and val_end. Otherwise, delim==val_end :
3901 *
3902 * hdr_beg
3903 * |
3904 * v
3905 * NAME=SRV; # in all but prefix modes
3906 * NAME=SRV~OPAQUE ; # in prefix mode
3907 * || || | |+-> next
3908 * || || | +--> val_end
3909 * || || +---------> delim
3910 * || |+------------> val_beg
3911 * || +-------------> att_end = equal
3912 * |+-----------------> att_beg
3913 * +------------------> prev
3914 *
3915 */
3916 if (s->be->ck_opts & PR_CK_PFX) {
3917 for (delim = val_beg; delim < val_end; delim++)
3918 if (*delim == COOKIE_DELIM)
3919 break;
3920 }
3921 else {
3922 char *vbar1;
3923 delim = val_end;
3924 /* Now check if the cookie contains a date field, which would
3925 * appear after a vertical bar ('|') just after the server name
3926 * and before the delimiter.
3927 */
3928 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3929 if (vbar1) {
3930 /* OK, so left of the bar is the server's cookie and
3931 * right is the last seen date. It is a base64 encoded
3932 * 30-bit value representing the UNIX date since the
3933 * epoch in 4-second quantities.
3934 */
3935 int val;
3936 delim = vbar1++;
3937 if (val_end - vbar1 >= 5) {
3938 val = b64tos30(vbar1);
3939 if (val > 0)
3940 txn->cookie_last_date = val << 2;
3941 }
3942 /* look for a second vertical bar */
3943 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3944 if (vbar1 && (val_end - vbar1 > 5)) {
3945 val = b64tos30(vbar1 + 1);
3946 if (val > 0)
3947 txn->cookie_first_date = val << 2;
3948 }
3949 }
3950 }
3951
3952 /* if the cookie has an expiration date and the proxy wants to check
3953 * it, then we do that now. We first check if the cookie is too old,
3954 * then only if it has expired. We detect strict overflow because the
3955 * time resolution here is not great (4 seconds). Cookies with dates
3956 * in the future are ignored if their offset is beyond one day. This
3957 * allows an admin to fix timezone issues without expiring everyone
3958 * and at the same time avoids keeping unwanted side effects for too
3959 * long.
3960 */
3961 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3962 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3963 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3964 txn->flags &= ~TX_CK_MASK;
3965 txn->flags |= TX_CK_OLD;
3966 delim = val_beg; // let's pretend we have not found the cookie
3967 txn->cookie_first_date = 0;
3968 txn->cookie_last_date = 0;
3969 }
3970 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3971 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3972 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3973 txn->flags &= ~TX_CK_MASK;
3974 txn->flags |= TX_CK_EXPIRED;
3975 delim = val_beg; // let's pretend we have not found the cookie
3976 txn->cookie_first_date = 0;
3977 txn->cookie_last_date = 0;
3978 }
3979
3980 /* Here, we'll look for the first running server which supports the cookie.
3981 * This allows to share a same cookie between several servers, for example
3982 * to dedicate backup servers to specific servers only.
3983 * However, to prevent clients from sticking to cookie-less backup server
3984 * when they have incidentely learned an empty cookie, we simply ignore
3985 * empty cookies and mark them as invalid.
3986 * The same behaviour is applied when persistence must be ignored.
3987 */
3988 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3989 srv = NULL;
3990
3991 while (srv) {
3992 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3993 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3994 if ((srv->cur_state != SRV_ST_STOPPED) ||
3995 (s->be->options & PR_O_PERSIST) ||
3996 (s->flags & SF_FORCE_PRST)) {
3997 /* we found the server and we can use it */
3998 txn->flags &= ~TX_CK_MASK;
3999 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
4000 s->flags |= SF_DIRECT | SF_ASSIGNED;
4001 s->target = &srv->obj_type;
4002 break;
4003 } else {
4004 /* we found a server, but it's down,
4005 * mark it as such and go on in case
4006 * another one is available.
4007 */
4008 txn->flags &= ~TX_CK_MASK;
4009 txn->flags |= TX_CK_DOWN;
4010 }
4011 }
4012 srv = srv->next;
4013 }
4014
4015 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
4016 /* no server matched this cookie or we deliberately skipped it */
4017 txn->flags &= ~TX_CK_MASK;
4018 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
4019 txn->flags |= TX_CK_UNUSED;
4020 else
4021 txn->flags |= TX_CK_INVALID;
4022 }
4023
4024 /* depending on the cookie mode, we may have to either :
4025 * - delete the complete cookie if we're in insert+indirect mode, so that
4026 * the server never sees it ;
4027 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08004028 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004029 * if we're in cookie prefix mode
4030 */
4031 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
4032 int delta; /* negative */
4033
4034 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
4035 delta = val_beg - (delim + 1);
4036 val_end += delta;
4037 next += delta;
4038 hdr_end += delta;
4039 del_from = NULL;
4040 preserve_hdr = 1; /* we want to keep this cookie */
4041 }
4042 else if (del_from == NULL &&
4043 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
4044 del_from = prev;
4045 }
4046 }
4047 else {
4048 /* This is not our cookie, so we must preserve it. But if we already
4049 * scheduled another cookie for removal, we cannot remove the
4050 * complete header, but we can remove the previous block itself.
4051 */
4052 preserve_hdr = 1;
4053
4054 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004055 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004056 if (att_beg >= del_from)
4057 att_beg += delta;
4058 if (att_end >= del_from)
4059 att_end += delta;
4060 val_beg += delta;
4061 val_end += delta;
4062 next += delta;
4063 hdr_end += delta;
4064 prev = del_from;
4065 del_from = NULL;
4066 }
4067 }
4068
4069 /* continue with next cookie on this header line */
4070 att_beg = next;
4071 } /* for each cookie */
4072
4073
4074 /* There are no more cookies on this line.
4075 * We may still have one (or several) marked for deletion at the
4076 * end of the line. We must do this now in two ways :
4077 * - if some cookies must be preserved, we only delete from the
4078 * mark to the end of line ;
4079 * - if nothing needs to be preserved, simply delete the whole header
4080 */
4081 if (del_from) {
4082 hdr_end = (preserve_hdr ? del_from : hdr_beg);
4083 }
4084 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02004085 if (hdr_beg != hdr_end)
4086 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004087 else
4088 http_remove_header(htx, &ctx);
4089 }
4090 } /* for each "Cookie header */
4091}
4092
4093/*
4094 * Manage server-side cookies. It can impact performance by about 2% so it is
4095 * desirable to call it only when needed. This function is also used when we
4096 * just need to know if there is a cookie (eg: for check-cache).
4097 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004098static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004099{
4100 struct session *sess = s->sess;
4101 struct http_txn *txn = s->txn;
4102 struct htx *htx;
4103 struct http_hdr_ctx ctx;
4104 struct server *srv;
4105 char *hdr_beg, *hdr_end;
4106 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02004107 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004108
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004109 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004110
4111 ctx.blk = NULL;
4112 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02004113 int is_first = 1;
4114
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004115 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
4116 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
4117 break;
4118 is_cookie2 = 1;
4119 }
4120
4121 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
4122 * <prev> points to the colon.
4123 */
4124 txn->flags |= TX_SCK_PRESENT;
4125
4126 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
4127 * check-cache is enabled) and we are not interested in checking
4128 * them. Warning, the cookie capture is declared in the frontend.
4129 */
4130 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
4131 break;
4132
4133 /* OK so now we know we have to process this response cookie.
4134 * The format of the Set-Cookie header is slightly different
4135 * from the format of the Cookie header in that it does not
4136 * support the comma as a cookie delimiter (thus the header
4137 * cannot be folded) because the Expires attribute described in
4138 * the original Netscape's spec may contain an unquoted date
4139 * with a comma inside. We have to live with this because
4140 * many browsers don't support Max-Age and some browsers don't
4141 * support quoted strings. However the Set-Cookie2 header is
4142 * clean.
4143 *
4144 * We have to keep multiple pointers in order to support cookie
4145 * removal at the beginning, middle or end of header without
4146 * corrupting the header (in case of set-cookie2). A special
4147 * pointer, <scav> points to the beginning of the set-cookie-av
4148 * fields after the first semi-colon. The <next> pointer points
4149 * either to the end of line (set-cookie) or next unquoted comma
4150 * (set-cookie2). All of these headers are valid :
4151 *
4152 * hdr_beg hdr_end
4153 * | |
4154 * v |
4155 * NAME1 = VALUE 1 ; Secure; Path="/" |
4156 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
4157 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
4158 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
4159 * | | | | | | | |
4160 * | | | | | | | +-> next
4161 * | | | | | | +------------> scav
4162 * | | | | | +--------------> val_end
4163 * | | | | +--------------------> val_beg
4164 * | | | +----------------------> equal
4165 * | | +------------------------> att_end
4166 * | +----------------------------> att_beg
4167 * +------------------------------> prev
4168 * -------------------------------> hdr_beg
4169 */
4170 hdr_beg = ctx.value.ptr;
4171 hdr_end = hdr_beg + ctx.value.len;
4172 for (prev = hdr_beg; prev < hdr_end; prev = next) {
4173
4174 /* Iterate through all cookies on this line */
4175
4176 /* find att_beg */
4177 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02004178 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004179 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02004180 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004181
4182 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
4183 att_beg++;
4184
4185 /* find att_end : this is the first character after the last non
4186 * space before the equal. It may be equal to hdr_end.
4187 */
4188 equal = att_end = att_beg;
4189
4190 while (equal < hdr_end) {
4191 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
4192 break;
4193 if (HTTP_IS_SPHT(*equal++))
4194 continue;
4195 att_end = equal;
4196 }
4197
4198 /* here, <equal> points to '=', a delimitor or the end. <att_end>
4199 * is between <att_beg> and <equal>, both may be identical.
4200 */
4201
4202 /* look for end of cookie if there is an equal sign */
4203 if (equal < hdr_end && *equal == '=') {
4204 /* look for the beginning of the value */
4205 val_beg = equal + 1;
4206 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
4207 val_beg++;
4208
4209 /* find the end of the value, respecting quotes */
4210 next = http_find_cookie_value_end(val_beg, hdr_end);
4211
4212 /* make val_end point to the first white space or delimitor after the value */
4213 val_end = next;
4214 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
4215 val_end--;
4216 }
4217 else {
4218 /* <equal> points to next comma, semi-colon or EOL */
4219 val_beg = val_end = next = equal;
4220 }
4221
4222 if (next < hdr_end) {
4223 /* Set-Cookie2 supports multiple cookies, and <next> points to
4224 * a colon or semi-colon before the end. So skip all attr-value
4225 * pairs and look for the next comma. For Set-Cookie, since
4226 * commas are permitted in values, skip to the end.
4227 */
4228 if (is_cookie2)
4229 next = http_find_hdr_value_end(next, hdr_end);
4230 else
4231 next = hdr_end;
4232 }
4233
4234 /* Now everything is as on the diagram above */
4235
4236 /* Ignore cookies with no equal sign */
4237 if (equal == val_end)
4238 continue;
4239
4240 /* If there are spaces around the equal sign, we need to
4241 * strip them otherwise we'll get trouble for cookie captures,
4242 * or even for rewrites. Since this happens extremely rarely,
4243 * it does not hurt performance.
4244 */
4245 if (unlikely(att_end != equal || val_beg > equal + 1)) {
4246 int stripped_before = 0;
4247 int stripped_after = 0;
4248
4249 if (att_end != equal) {
4250 memmove(att_end, equal, hdr_end - equal);
4251 stripped_before = (att_end - equal);
4252 equal += stripped_before;
4253 val_beg += stripped_before;
4254 }
4255
4256 if (val_beg > equal + 1) {
4257 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
4258 stripped_after = (equal + 1) - val_beg;
4259 val_beg += stripped_after;
4260 stripped_before += stripped_after;
4261 }
4262
4263 val_end += stripped_before;
4264 next += stripped_before;
4265 hdr_end += stripped_before;
4266
Christopher Faulet3e2638e2019-06-18 09:49:16 +02004267 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004268 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004269 }
4270
4271 /* First, let's see if we want to capture this cookie. We check
4272 * that we don't already have a server side cookie, because we
4273 * can only capture one. Also as an optimisation, we ignore
4274 * cookies shorter than the declared name.
4275 */
4276 if (sess->fe->capture_name != NULL &&
4277 txn->srv_cookie == NULL &&
4278 (val_end - att_beg >= sess->fe->capture_namelen) &&
4279 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
4280 int log_len = val_end - att_beg;
4281 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
4282 ha_alert("HTTP logging : out of memory.\n");
4283 }
4284 else {
4285 if (log_len > sess->fe->capture_len)
4286 log_len = sess->fe->capture_len;
4287 memcpy(txn->srv_cookie, att_beg, log_len);
4288 txn->srv_cookie[log_len] = 0;
4289 }
4290 }
4291
4292 srv = objt_server(s->target);
4293 /* now check if we need to process it for persistence */
4294 if (!(s->flags & SF_IGNORE_PRST) &&
4295 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
4296 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
4297 /* assume passive cookie by default */
4298 txn->flags &= ~TX_SCK_MASK;
4299 txn->flags |= TX_SCK_FOUND;
4300
4301 /* If the cookie is in insert mode on a known server, we'll delete
4302 * this occurrence because we'll insert another one later.
4303 * We'll delete it too if the "indirect" option is set and we're in
4304 * a direct access.
4305 */
4306 if (s->be->ck_opts & PR_CK_PSV) {
4307 /* The "preserve" flag was set, we don't want to touch the
4308 * server's cookie.
4309 */
4310 }
4311 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
4312 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
4313 /* this cookie must be deleted */
4314 if (prev == hdr_beg && next == hdr_end) {
4315 /* whole header */
4316 http_remove_header(htx, &ctx);
4317 /* note: while both invalid now, <next> and <hdr_end>
4318 * are still equal, so the for() will stop as expected.
4319 */
4320 } else {
4321 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004322 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02004323 next = prev;
4324 hdr_end += delta;
4325 }
4326 txn->flags &= ~TX_SCK_MASK;
4327 txn->flags |= TX_SCK_DELETED;
4328 /* and go on with next cookie */
4329 }
4330 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
4331 /* replace bytes val_beg->val_end with the cookie name associated
4332 * with this server since we know it.
4333 */
4334 int sliding, delta;
4335
4336 ctx.value = ist2(val_beg, val_end - val_beg);
4337 ctx.lws_before = ctx.lws_after = 0;
4338 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
4339 delta = srv->cklen - (val_end - val_beg);
4340 sliding = (ctx.value.ptr - val_beg);
4341 hdr_beg += sliding;
4342 val_beg += sliding;
4343 next += sliding + delta;
4344 hdr_end += sliding + delta;
4345
4346 txn->flags &= ~TX_SCK_MASK;
4347 txn->flags |= TX_SCK_REPLACED;
4348 }
4349 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
4350 /* insert the cookie name associated with this server
4351 * before existing cookie, and insert a delimiter between them..
4352 */
4353 int sliding, delta;
4354 ctx.value = ist2(val_beg, 0);
4355 ctx.lws_before = ctx.lws_after = 0;
4356 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
4357 delta = srv->cklen + 1;
4358 sliding = (ctx.value.ptr - val_beg);
4359 hdr_beg += sliding;
4360 val_beg += sliding;
4361 next += sliding + delta;
4362 hdr_end += sliding + delta;
4363
4364 val_beg[srv->cklen] = COOKIE_DELIM;
4365 txn->flags &= ~TX_SCK_MASK;
4366 txn->flags |= TX_SCK_REPLACED;
4367 }
4368 }
4369 /* that's done for this cookie, check the next one on the same
4370 * line when next != hdr_end (only if is_cookie2).
4371 */
4372 }
4373 }
4374}
4375
Christopher Faulet25a02f62018-10-24 12:00:25 +02004376/*
4377 * Parses the Cache-Control and Pragma request header fields to determine if
4378 * the request may be served from the cache and/or if it is cacheable. Updates
4379 * s->txn->flags.
4380 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004381void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004382{
4383 struct http_txn *txn = s->txn;
4384 struct htx *htx;
4385 int32_t pos;
4386 int pragma_found, cc_found, i;
4387
4388 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
4389 return; /* nothing more to do here */
4390
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004391 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02004392 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02004393 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004394 struct htx_blk *blk = htx_get_blk(htx, pos);
4395 enum htx_blk_type type = htx_get_blk_type(blk);
4396 struct ist n, v;
4397
4398 if (type == HTX_BLK_EOH)
4399 break;
4400 if (type != HTX_BLK_HDR)
4401 continue;
4402
4403 n = htx_get_blk_name(htx, blk);
4404 v = htx_get_blk_value(htx, blk);
4405
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004406 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004407 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4408 pragma_found = 1;
4409 continue;
4410 }
4411 }
4412
4413 /* Don't use the cache and don't try to store if we found the
4414 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004415 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004416 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4417 txn->flags |= TX_CACHE_IGNORE;
4418 continue;
4419 }
4420
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004421 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004422 continue;
4423
4424 /* OK, right now we know we have a cache-control header */
4425 cc_found = 1;
4426 if (!v.len) /* no info */
4427 continue;
4428
4429 i = 0;
4430 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4431 !isspace((unsigned char)*(v.ptr+i)))
4432 i++;
4433
4434 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
4435 * values after max-age, max-stale nor min-fresh, we simply don't
4436 * use the cache when they're specified.
4437 */
4438 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
4439 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4440 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
4441 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
4442 txn->flags |= TX_CACHE_IGNORE;
4443 continue;
4444 }
4445
4446 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
4447 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4448 continue;
4449 }
4450 }
4451
4452 /* RFC7234#5.4:
4453 * When the Cache-Control header field is also present and
4454 * understood in a request, Pragma is ignored.
4455 * When the Cache-Control header field is not present in a
4456 * request, caches MUST consider the no-cache request
4457 * pragma-directive as having the same effect as if
4458 * "Cache-Control: no-cache" were present.
4459 */
4460 if (!cc_found && pragma_found)
4461 txn->flags |= TX_CACHE_IGNORE;
4462}
4463
4464/*
4465 * Check if response is cacheable or not. Updates s->txn->flags.
4466 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004467void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02004468{
4469 struct http_txn *txn = s->txn;
4470 struct htx *htx;
4471 int32_t pos;
4472 int i;
4473
4474 if (txn->status < 200) {
4475 /* do not try to cache interim responses! */
4476 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4477 return;
4478 }
4479
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004480 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004481 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004482 struct htx_blk *blk = htx_get_blk(htx, pos);
4483 enum htx_blk_type type = htx_get_blk_type(blk);
4484 struct ist n, v;
4485
4486 if (type == HTX_BLK_EOH)
4487 break;
4488 if (type != HTX_BLK_HDR)
4489 continue;
4490
4491 n = htx_get_blk_name(htx, blk);
4492 v = htx_get_blk_value(htx, blk);
4493
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004494 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02004495 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
4496 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4497 return;
4498 }
4499 }
4500
Willy Tarreau2e754bf2018-12-07 11:38:03 +01004501 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02004502 continue;
4503
4504 /* OK, right now we know we have a cache-control header */
4505 if (!v.len) /* no info */
4506 continue;
4507
4508 i = 0;
4509 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
4510 !isspace((unsigned char)*(v.ptr+i)))
4511 i++;
4512
4513 /* we have a complete value between v.ptr and (v.ptr+i) */
4514 if (i < v.len && *(v.ptr + i) == '=') {
4515 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
4516 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
4517 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4518 continue;
4519 }
4520
4521 /* we have something of the form no-cache="set-cookie" */
4522 if ((v.len >= 21) &&
4523 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
4524 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
4525 txn->flags &= ~TX_CACHE_COOK;
4526 continue;
4527 }
4528
4529 /* OK, so we know that either p2 points to the end of string or to a comma */
4530 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
4531 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
4532 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
4533 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
4534 return;
4535 }
4536
4537 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4538 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4539 continue;
4540 }
4541 }
4542}
4543
Christopher Faulet377c5a52018-10-24 21:21:30 +02004544/*
4545 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4546 * for the current backend.
4547 *
4548 * It is assumed that the request is either a HEAD, GET, or POST and that the
4549 * uri_auth field is valid.
4550 *
4551 * Returns 1 if stats should be provided, otherwise 0.
4552 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004553static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004554{
4555 struct uri_auth *uri_auth = backend->uri_auth;
4556 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004557 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004558 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004559
4560 if (!uri_auth)
4561 return 0;
4562
4563 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4564 return 0;
4565
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004566 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004567 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004568 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01004569 if (*uri_auth->uri_prefix == '/')
4570 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004571
4572 /* check URI size */
4573 if (uri_auth->uri_len > uri.len)
4574 return 0;
4575
4576 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4577 return 0;
4578
4579 return 1;
4580}
4581
4582/* This function prepares an applet to handle the stats. It can deal with the
4583 * "100-continue" expectation, check that admin rules are met for POST requests,
4584 * and program a response message if something was unexpected. It cannot fail
4585 * and always relies on the stats applet to complete the job. It does not touch
4586 * analysers nor counters, which are left to the caller. It does not touch
4587 * s->target which is supposed to already point to the stats applet. The caller
4588 * is expected to have already assigned an appctx to the stream.
4589 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004590static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004591{
4592 struct stats_admin_rule *stats_admin_rule;
4593 struct stream_interface *si = &s->si[1];
4594 struct session *sess = s->sess;
4595 struct http_txn *txn = s->txn;
4596 struct http_msg *msg = &txn->req;
4597 struct uri_auth *uri_auth = s->be->uri_auth;
4598 const char *h, *lookup, *end;
4599 struct appctx *appctx;
4600 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004601 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004602
4603 appctx = si_appctx(si);
4604 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4605 appctx->st1 = appctx->st2 = 0;
4606 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004607 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004608 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4609 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4610 appctx->ctx.stats.flags |= STAT_CHUNKED;
4611
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004612 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004613 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004614 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4615 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004616
4617 for (h = lookup; h <= end - 3; h++) {
4618 if (memcmp(h, ";up", 3) == 0) {
4619 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4620 break;
4621 }
4622 }
4623
4624 if (uri_auth->refresh) {
4625 for (h = lookup; h <= end - 10; h++) {
4626 if (memcmp(h, ";norefresh", 10) == 0) {
4627 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4628 break;
4629 }
4630 }
4631 }
4632
4633 for (h = lookup; h <= end - 4; h++) {
4634 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004635 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004636 break;
4637 }
4638 }
4639
4640 for (h = lookup; h <= end - 6; h++) {
4641 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004642 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004643 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4644 break;
4645 }
4646 }
4647
Christopher Faulet6338a082019-09-09 15:50:54 +02004648 for (h = lookup; h <= end - 5; h++) {
4649 if (memcmp(h, ";json", 5) == 0) {
4650 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4651 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4652 break;
4653 }
4654 }
4655
4656 for (h = lookup; h <= end - 12; h++) {
4657 if (memcmp(h, ";json-schema", 12) == 0) {
4658 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4659 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4660 break;
4661 }
4662 }
4663
Christopher Faulet377c5a52018-10-24 21:21:30 +02004664 for (h = lookup; h <= end - 8; h++) {
4665 if (memcmp(h, ";st=", 4) == 0) {
4666 int i;
4667 h += 4;
4668 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4669 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4670 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4671 appctx->ctx.stats.st_code = i;
4672 break;
4673 }
4674 }
4675 break;
4676 }
4677 }
4678
4679 appctx->ctx.stats.scope_str = 0;
4680 appctx->ctx.stats.scope_len = 0;
4681 for (h = lookup; h <= end - 8; h++) {
4682 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4683 int itx = 0;
4684 const char *h2;
4685 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4686 const char *err;
4687
4688 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4689 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004690 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4691 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004692 if (*h == ';' || *h == '&' || *h == ' ')
4693 break;
4694 itx++;
4695 h++;
4696 }
4697
4698 if (itx > STAT_SCOPE_TXT_MAXLEN)
4699 itx = STAT_SCOPE_TXT_MAXLEN;
4700 appctx->ctx.stats.scope_len = itx;
4701
4702 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4703 memcpy(scope_txt, h2, itx);
4704 scope_txt[itx] = '\0';
4705 err = invalid_char(scope_txt);
4706 if (err) {
4707 /* bad char in search text => clear scope */
4708 appctx->ctx.stats.scope_str = 0;
4709 appctx->ctx.stats.scope_len = 0;
4710 }
4711 break;
4712 }
4713 }
4714
4715 /* now check whether we have some admin rules for this request */
4716 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4717 int ret = 1;
4718
4719 if (stats_admin_rule->cond) {
4720 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4721 ret = acl_pass(ret);
4722 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4723 ret = !ret;
4724 }
4725
4726 if (ret) {
4727 /* no rule, or the rule matches */
4728 appctx->ctx.stats.flags |= STAT_ADMIN;
4729 break;
4730 }
4731 }
4732
Christopher Faulet5d45e382019-02-27 15:15:23 +01004733 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4734 appctx->st0 = STAT_HTTP_HEAD;
4735 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004736 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004737 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004738 if (msg->msg_state < HTTP_MSG_DATA)
4739 req->analysers |= AN_REQ_HTTP_BODY;
4740 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004741 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004742 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004743 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4744 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4745 appctx->st0 = STAT_HTTP_LAST;
4746 }
4747 }
4748 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004749 /* Unsupported method */
4750 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4751 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4752 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004753 }
4754
4755 s->task->nice = -32; /* small boost for HTTP statistics */
4756 return 1;
4757}
4758
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004759void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004760{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004761 struct channel *req = &s->req;
4762 struct channel *res = &s->res;
4763 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004764 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004765 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004766 struct ist path, location;
4767 unsigned int flags;
4768 size_t data;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004769
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004770 /*
4771 * Create the location
4772 */
4773 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004774
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004775 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004776 /* special prefix "/" means don't change URL */
4777 srv = __objt_server(s->target);
4778 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4779 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4780 return;
4781 }
4782
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004783 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004784 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004785 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004786 path = http_get_path(htx_sl_req_uri(sl));
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004787 if (!path.ptr)
4788 return;
4789
4790 if (!chunk_memcat(&trash, path.ptr, path.len))
4791 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004792 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004793
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004794 /*
4795 * Create the 302 respone
4796 */
4797 htx = htx_from_buf(&res->buf);
4798 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4799 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4800 ist("HTTP/1.1"), ist("302"), ist("Found"));
4801 if (!sl)
4802 goto fail;
4803 sl->info.res.status = 302;
4804 s->txn->status = 302;
4805
4806 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4807 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4808 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4809 !htx_add_header(htx, ist("Location"), location))
4810 goto fail;
4811
4812 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4813 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004814
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004815 /*
4816 * Send the message
4817 */
4818 data = htx->data - co_data(res);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004819 c_adv(res, data);
4820 res->total += data;
4821
4822 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004823 si_shutr(si);
4824 si_shutw(si);
4825 si->err_type = SI_ET_NONE;
4826 si->state = SI_ST_CLO;
4827
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004828 channel_auto_read(req);
4829 channel_abort(req);
4830 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004831 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004832 channel_auto_read(res);
4833 channel_auto_close(res);
4834
4835 if (!(s->flags & SF_ERR_MASK))
4836 s->flags |= SF_ERR_LOCAL;
4837 if (!(s->flags & SF_FINST_MASK))
4838 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004839
4840 /* FIXME: we should increase a counter of redirects per server and per backend. */
4841 srv_inc_sess_ctr(srv);
4842 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004843 return;
4844
4845 fail:
4846 /* If an error occurred, remove the incomplete HTTP response from the
4847 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004848 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004849}
4850
Christopher Fauletf2824e62018-10-01 12:12:37 +02004851/* This function terminates the request because it was completly analyzed or
4852 * because an error was triggered during the body forwarding.
4853 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004854static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004855{
4856 struct channel *chn = &s->req;
4857 struct http_txn *txn = s->txn;
4858
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004859 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004860
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004861 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4862 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004863 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004864 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004865 goto end;
4866 }
4867
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004868 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4869 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004870 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004871 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004872
4873 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004874 /* No need to read anymore, the request was completely parsed.
4875 * We can shut the read side unless we want to abort_on_close,
4876 * or we have a POST request. The issue with POST requests is
4877 * that some browsers still send a CRLF after the request, and
4878 * this CRLF must be read so that it does not remain in the kernel
4879 * buffers, otherwise a close could cause an RST on some systems
4880 * (eg: Linux).
4881 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004882 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004883 channel_dont_read(chn);
4884
4885 /* if the server closes the connection, we want to immediately react
4886 * and close the socket to save packets and syscalls.
4887 */
4888 s->si[1].flags |= SI_FL_NOHALF;
4889
4890 /* In any case we've finished parsing the request so we must
4891 * disable Nagle when sending data because 1) we're not going
4892 * to shut this side, and 2) the server is waiting for us to
4893 * send pending data.
4894 */
4895 chn->flags |= CF_NEVER_WAIT;
4896
Christopher Fauletd01ce402019-01-02 17:44:13 +01004897 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4898 /* The server has not finished to respond, so we
4899 * don't want to move in order not to upset it.
4900 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004901 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004902 return;
4903 }
4904
Christopher Fauletf2824e62018-10-01 12:12:37 +02004905 /* When we get here, it means that both the request and the
4906 * response have finished receiving. Depending on the connection
4907 * mode, we'll have to wait for the last bytes to leave in either
4908 * direction, and sometimes for a close to be effective.
4909 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004910 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004911 /* Tunnel mode will not have any analyser so it needs to
4912 * poll for reads.
4913 */
4914 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004915 if (b_data(&chn->buf)) {
4916 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004917 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004918 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004919 txn->req.msg_state = HTTP_MSG_TUNNEL;
4920 }
4921 else {
4922 /* we're not expecting any new data to come for this
4923 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004924 *
4925 * However, there is an exception if the response
4926 * length is undefined. In this case, we need to wait
4927 * the close from the server. The response will be
4928 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004929 */
4930 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4931 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004932 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004933
4934 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4935 channel_shutr_now(chn);
4936 channel_shutw_now(chn);
4937 }
4938 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004939 goto check_channel_flags;
4940 }
4941
4942 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4943 http_msg_closing:
4944 /* nothing else to forward, just waiting for the output buffer
4945 * to be empty and for the shutw_now to take effect.
4946 */
4947 if (channel_is_empty(chn)) {
4948 txn->req.msg_state = HTTP_MSG_CLOSED;
4949 goto http_msg_closed;
4950 }
4951 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004952 txn->req.msg_state = HTTP_MSG_ERROR;
4953 goto end;
4954 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004955 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004956 return;
4957 }
4958
4959 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4960 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004961 /* if we don't know whether the server will close, we need to hard close */
4962 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4963 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004964 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004965 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004966 channel_dont_read(chn);
4967 goto end;
4968 }
4969
4970 check_channel_flags:
4971 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4972 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4973 /* if we've just closed an output, let's switch */
4974 txn->req.msg_state = HTTP_MSG_CLOSING;
4975 goto http_msg_closing;
4976 }
4977
4978 end:
4979 chn->analysers &= AN_REQ_FLT_END;
4980 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4981 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4982 channel_auto_close(chn);
4983 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004984 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004985}
4986
4987
4988/* This function terminates the response because it was completly analyzed or
4989 * because an error was triggered during the body forwarding.
4990 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004991static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004992{
4993 struct channel *chn = &s->res;
4994 struct http_txn *txn = s->txn;
4995
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004996 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004997
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004998 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4999 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005000 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02005001 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005002 goto end;
5003 }
5004
Christopher Fauleteea8fc72019-11-05 16:18:10 +01005005 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
5006 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005007 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01005008 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02005009
5010 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5011 /* In theory, we don't need to read anymore, but we must
5012 * still monitor the server connection for a possible close
5013 * while the request is being uploaded, so we don't disable
5014 * reading.
5015 */
5016 /* channel_dont_read(chn); */
5017
5018 if (txn->req.msg_state < HTTP_MSG_DONE) {
5019 /* The client seems to still be sending data, probably
5020 * because we got an error response during an upload.
5021 * We have the choice of either breaking the connection
5022 * or letting it pass through. Let's do the later.
5023 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01005024 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005025 return;
5026 }
5027
5028 /* When we get here, it means that both the request and the
5029 * response have finished receiving. Depending on the connection
5030 * mode, we'll have to wait for the last bytes to leave in either
5031 * direction, and sometimes for a close to be effective.
5032 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02005033 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02005034 channel_auto_read(chn);
5035 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01005036 if (b_data(&chn->buf)) {
5037 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02005038 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01005039 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02005040 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
5041 }
5042 else {
5043 /* we're not expecting any new data to come for this
5044 * transaction, so we can close it.
5045 */
5046 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5047 channel_shutr_now(chn);
5048 channel_shutw_now(chn);
5049 }
5050 }
5051 goto check_channel_flags;
5052 }
5053
5054 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5055 http_msg_closing:
5056 /* nothing else to forward, just waiting for the output buffer
5057 * to be empty and for the shutw_now to take effect.
5058 */
5059 if (channel_is_empty(chn)) {
5060 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5061 goto http_msg_closed;
5062 }
5063 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02005064 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01005065 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01005066 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01005067 if (strm_sess(s)->listener->counters)
5068 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005069 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01005070 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005071 goto end;
5072 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01005073 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005074 return;
5075 }
5076
5077 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5078 http_msg_closed:
5079 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005080 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02005081 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005082 goto end;
5083 }
5084
5085 check_channel_flags:
5086 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
5087 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
5088 /* if we've just closed an output, let's switch */
5089 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5090 goto http_msg_closing;
5091 }
5092
5093 end:
5094 chn->analysers &= AN_RES_FLT_END;
5095 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
5096 chn->analysers |= AN_RES_FLT_XFER_DATA;
5097 channel_auto_close(chn);
5098 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01005099 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02005100}
5101
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005102void http_server_error(struct stream *s, struct stream_interface *si, int err,
5103 int finst, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02005104{
5105 channel_auto_read(si_oc(si));
5106 channel_abort(si_oc(si));
5107 channel_auto_close(si_oc(si));
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005108 channel_htx_erase(si_oc(si), htxbuf(&(si_oc(si))->buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02005109 channel_auto_close(si_ic(si));
5110 channel_auto_read(si_ic(si));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005111
5112 /* <msg> is an HTX structure. So we copy it in the response's
5113 * channel */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02005114 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02005115 struct channel *chn = si_ic(si);
5116 struct htx *htx;
5117
Christopher Fauletaed82cf2018-11-30 22:22:32 +01005118 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005119 chn->buf.data = msg->data;
5120 memcpy(chn->buf.area, msg->area, msg->data);
5121 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02005122 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02005123 c_adv(chn, htx->data);
5124 chn->total += htx->data;
5125 }
5126 if (!(s->flags & SF_ERR_MASK))
5127 s->flags |= err;
5128 if (!(s->flags & SF_FINST_MASK))
5129 s->flags |= finst;
5130}
5131
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005132void http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02005133{
5134 channel_auto_read(&s->req);
5135 channel_abort(&s->req);
5136 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005137 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
5138 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02005139
5140 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005141
5142 /* <msg> is an HTX structure. So we copy it in the response's
5143 * channel */
5144 /* FIXME: It is a problem for now if there is some outgoing data */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02005145 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02005146 struct channel *chn = &s->res;
5147 struct htx *htx;
5148
Christopher Fauletaed82cf2018-11-30 22:22:32 +01005149 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005150 chn->buf.data = msg->data;
5151 memcpy(chn->buf.area, msg->area, msg->data);
5152 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02005153 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02005154 c_adv(chn, htx->data);
5155 chn->total += htx->data;
5156 }
5157
5158 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
5159 channel_auto_read(&s->res);
5160 channel_auto_close(&s->res);
5161 channel_shutr_now(&s->res);
5162}
5163
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005164struct buffer *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005165{
5166 const int msgnum = http_get_status_idx(s->txn->status);
5167
5168 if (s->be->errmsg[msgnum].area)
5169 return &s->be->errmsg[msgnum];
5170 else if (strm_fe(s)->errmsg[msgnum].area)
5171 return &strm_fe(s)->errmsg[msgnum];
5172 else
Christopher Fauletf7346382019-07-17 22:02:08 +02005173 return &http_err_chunks[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005174}
5175
Christopher Faulet304cc402019-07-15 15:46:28 +02005176/* Return the error message corresponding to si->err_type. It is assumed
5177 * that the server side is closed. Note that err_type is actually a
5178 * bitmask, where almost only aborts may be cumulated with other
5179 * values. We consider that aborted operations are more important
5180 * than timeouts or errors due to the fact that nobody else in the
5181 * logs might explain incomplete retries. All others should avoid
5182 * being cumulated. It should normally not be possible to have multiple
5183 * aborts at once, but just in case, the first one in sequence is reported.
5184 * Note that connection errors appearing on the second request of a keep-alive
5185 * connection are not reported since this allows the client to retry.
5186 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005187void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02005188{
5189 int err_type = si->err_type;
5190
5191 /* set s->txn->status for http_error_message(s) */
5192 s->txn->status = 503;
5193
5194 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005195 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
5196 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005197 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005198 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
5199 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5200 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005201 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005202 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
5203 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005204 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005205 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
5206 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005207 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005208 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
5209 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5210 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005211 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005212 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
5213 (s->flags & SF_SRV_REUSED) ? NULL :
5214 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005215 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005216 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
5217 (s->txn->flags & TX_NOT_FIRST) ? NULL :
5218 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005219 else { /* SI_ET_CONN_OTHER and others */
5220 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005221 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
5222 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02005223 }
5224}
5225
Christopher Fauleta7b677c2018-11-29 16:48:49 +01005226
Christopher Faulet4a28a532019-03-01 11:19:40 +01005227/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
5228 * on success and -1 on error.
5229 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005230static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01005231{
5232 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
5233 * then we must send an HTTP/1.1 100 Continue intermediate response.
5234 */
5235 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
5236 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
5237 struct ist hdr = { .ptr = "Expect", .len = 6 };
5238 struct http_hdr_ctx ctx;
5239
5240 ctx.blk = NULL;
5241 /* Expect is allowed in 1.1, look for it */
5242 if (http_find_header(htx, hdr, &ctx, 0) &&
5243 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005244 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01005245 return -1;
5246 http_remove_header(htx, &ctx);
5247 }
5248 }
5249 return 0;
5250}
5251
Christopher Faulet23a3c792018-11-28 10:01:23 +01005252/* Send a 100-Continue response to the client. It returns 0 on success and -1
5253 * on error. The response channel is updated accordingly.
5254 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005255static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01005256{
5257 struct channel *res = &s->res;
5258 struct htx *htx = htx_from_buf(&res->buf);
5259 struct htx_sl *sl;
5260 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
5261 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
5262 size_t data;
5263
5264 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5265 ist("HTTP/1.1"), ist("100"), ist("Continue"));
5266 if (!sl)
5267 goto fail;
5268 sl->info.res.status = 100;
5269
Christopher Faulet1d5ec092019-06-26 14:23:54 +02005270 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01005271 goto fail;
5272
5273 data = htx->data - co_data(res);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005274 c_adv(res, data);
5275 res->total += data;
5276 return 0;
5277
5278 fail:
5279 /* If an error occurred, remove the incomplete HTTP response from the
5280 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005281 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01005282 return -1;
5283}
5284
Christopher Faulet12c51e22018-11-28 15:59:42 +01005285
5286/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
5287 * ont whether we use a proxy or not. It returns 0 on success and -1 on
5288 * error. The response channel is updated accordingly.
5289 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005290static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
Christopher Faulet12c51e22018-11-28 15:59:42 +01005291{
5292 struct channel *res = &s->res;
5293 struct htx *htx = htx_from_buf(&res->buf);
5294 struct htx_sl *sl;
5295 struct ist code, body;
5296 int status;
5297 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5298 size_t data;
5299
5300 if (!(s->txn->flags & TX_USE_PX_CONN)) {
5301 status = 401;
5302 code = ist("401");
5303 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
5304 "You need a valid user and password to access this content.\n"
5305 "</body></html>\n");
5306 }
5307 else {
5308 status = 407;
5309 code = ist("407");
5310 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
5311 "You need a valid user and password to access this content.\n"
5312 "</body></html>\n");
5313 }
5314
5315 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
5316 ist("HTTP/1.1"), code, ist("Unauthorized"));
5317 if (!sl)
5318 goto fail;
5319 sl->info.res.status = status;
5320 s->txn->status = status;
5321
5322 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
5323 goto fail;
5324
Willy Tarreaub5ba2b02019-06-11 16:08:25 +02005325 if (!htx_add_header(htx, ist("Content-length"), ist("112")) ||
5326 !htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Faulet12c51e22018-11-28 15:59:42 +01005327 !htx_add_header(htx, ist("Connection"), ist("close")) ||
Jérôme Magnin86cef232018-12-28 14:49:08 +01005328 !htx_add_header(htx, ist("Content-Type"), ist("text/html")))
5329 goto fail;
5330 if (status == 401 && !htx_add_header(htx, ist("WWW-Authenticate"), ist2(trash.area, trash.data)))
5331 goto fail;
5332 if (status == 407 && !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005333 goto fail;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02005334 if (!htx_add_endof(htx, HTX_BLK_EOH))
5335 goto fail;
5336
5337 while (body.len) {
5338 size_t sent = htx_add_data(htx, body);
5339 if (!sent)
5340 goto fail;
5341 body.ptr += sent;
5342 body.len -= sent;
5343 }
5344
5345 if (!htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet12c51e22018-11-28 15:59:42 +01005346 goto fail;
5347
Christopher Faulet06511812019-10-16 09:38:27 +02005348 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet12c51e22018-11-28 15:59:42 +01005349 data = htx->data - co_data(res);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005350 c_adv(res, data);
5351 res->total += data;
5352
5353 channel_auto_read(&s->req);
5354 channel_abort(&s->req);
5355 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005356 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet12c51e22018-11-28 15:59:42 +01005357
5358 res->wex = tick_add_ifset(now_ms, res->wto);
5359 channel_auto_read(res);
5360 channel_auto_close(res);
5361 channel_shutr_now(res);
5362 return 0;
5363
5364 fail:
5365 /* If an error occurred, remove the incomplete HTTP response from the
5366 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01005367 channel_htx_truncate(res, htx);
Christopher Faulet12c51e22018-11-28 15:59:42 +01005368 return -1;
5369}
5370
Christopher Faulet0f226952018-10-22 09:29:56 +02005371/*
5372 * Capture headers from message <htx> according to header list <cap_hdr>, and
5373 * fill the <cap> pointers appropriately.
5374 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005375static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02005376{
5377 struct cap_hdr *h;
5378 int32_t pos;
5379
Christopher Fauleta3f15502019-05-13 15:27:23 +02005380 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02005381 struct htx_blk *blk = htx_get_blk(htx, pos);
5382 enum htx_blk_type type = htx_get_blk_type(blk);
5383 struct ist n, v;
5384
5385 if (type == HTX_BLK_EOH)
5386 break;
5387 if (type != HTX_BLK_HDR)
5388 continue;
5389
5390 n = htx_get_blk_name(htx, blk);
5391
5392 for (h = cap_hdr; h; h = h->next) {
5393 if (h->namelen && (h->namelen == n.len) &&
5394 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
5395 if (cap[h->index] == NULL)
5396 cap[h->index] =
5397 pool_alloc(h->pool);
5398
5399 if (cap[h->index] == NULL) {
5400 ha_alert("HTTP capture : out of memory.\n");
5401 break;
5402 }
5403
5404 v = htx_get_blk_value(htx, blk);
5405 if (v.len > h->len)
5406 v.len = h->len;
5407
5408 memcpy(cap[h->index], v.ptr, v.len);
5409 cap[h->index][v.len]=0;
5410 }
5411 }
5412 }
5413}
5414
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005415/* Delete a value in a header between delimiters <from> and <next>. The header
5416 * itself is delimited by <start> and <end> pointers. The number of characters
5417 * displaced is returned, and the pointer to the first delimiter is updated if
5418 * required. The function tries as much as possible to respect the following
5419 * principles :
5420 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
5421 * in which case <next> is simply removed
5422 * - set exactly one space character after the new first delimiter, unless there
5423 * are not enough characters in the block being moved to do so.
5424 * - remove unneeded spaces before the previous delimiter and after the new
5425 * one.
5426 *
5427 * It is the caller's responsibility to ensure that :
5428 * - <from> points to a valid delimiter or <start> ;
5429 * - <next> points to a valid delimiter or <end> ;
5430 * - there are non-space chars before <from>.
5431 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005432static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02005433{
5434 char *prev = *from;
5435
5436 if (prev == start) {
5437 /* We're removing the first value. eat the semicolon, if <next>
5438 * is lower than <end> */
5439 if (next < end)
5440 next++;
5441
5442 while (next < end && HTTP_IS_SPHT(*next))
5443 next++;
5444 }
5445 else {
5446 /* Remove useless spaces before the old delimiter. */
5447 while (HTTP_IS_SPHT(*(prev-1)))
5448 prev--;
5449 *from = prev;
5450
5451 /* copy the delimiter and if possible a space if we're
5452 * not at the end of the line.
5453 */
5454 if (next < end) {
5455 *prev++ = *next++;
5456 if (prev + 1 < next)
5457 *prev++ = ' ';
5458 while (next < end && HTTP_IS_SPHT(*next))
5459 next++;
5460 }
5461 }
5462 memmove(prev, next, end - next);
5463 return (prev - next);
5464}
5465
Christopher Faulet0f226952018-10-22 09:29:56 +02005466
5467/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005468 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005469 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005470static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005471{
5472 struct ist dst = ist2(str, 0);
5473
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005474 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005475 goto end;
5476 if (dst.len + 1 > len)
5477 goto end;
5478 dst.ptr[dst.len++] = ' ';
5479
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005480 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005481 goto end;
5482 if (dst.len + 1 > len)
5483 goto end;
5484 dst.ptr[dst.len++] = ' ';
5485
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005486 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005487 end:
5488 return dst.len;
5489}
5490
5491/*
5492 * Print a debug line with a start line.
5493 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005494static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005495{
5496 struct session *sess = strm_sess(s);
5497 int max;
5498
5499 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5500 dir,
5501 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5502 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5503
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005504 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005505 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005506 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005507 trash.area[trash.data++] = ' ';
5508
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005509 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005510 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005511 chunk_memcat(&trash, HTX_SL_P2_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_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005515 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005516 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005517 trash.area[trash.data++] = '\n';
5518
5519 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5520}
5521
5522/*
5523 * Print a debug line with a header.
5524 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005525static 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 +02005526{
5527 struct session *sess = strm_sess(s);
5528 int max;
5529
5530 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5531 dir,
5532 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5533 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5534
5535 max = n.len;
5536 UBOUND(max, trash.size - trash.data - 3);
5537 chunk_memcat(&trash, n.ptr, max);
5538 trash.area[trash.data++] = ':';
5539 trash.area[trash.data++] = ' ';
5540
5541 max = v.len;
5542 UBOUND(max, trash.size - trash.data - 1);
5543 chunk_memcat(&trash, v.ptr, max);
5544 trash.area[trash.data++] = '\n';
5545
5546 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
5547}
5548
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005549/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5550 * In case of allocation failure, everything allocated is freed and NULL is
5551 * returned. Otherwise the new transaction is assigned to the stream and
5552 * returned.
5553 */
5554struct http_txn *http_alloc_txn(struct stream *s)
5555{
5556 struct http_txn *txn = s->txn;
5557
5558 if (txn)
5559 return txn;
5560
5561 txn = pool_alloc(pool_head_http_txn);
5562 if (!txn)
5563 return txn;
5564
5565 s->txn = txn;
5566 return txn;
5567}
5568
5569void http_txn_reset_req(struct http_txn *txn)
5570{
Christopher Fauleta00071e2019-12-12 16:41:00 +01005571 txn->req.flags = HTTP_MSGF_SOFT_RW;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005572 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5573}
5574
5575void http_txn_reset_res(struct http_txn *txn)
5576{
Christopher Fauleta00071e2019-12-12 16:41:00 +01005577 txn->rsp.flags = HTTP_MSGF_SOFT_RW;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005578 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5579}
5580
5581/*
5582 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5583 * the required fields are properly allocated and that we only need to (re)init
5584 * them. This should be used before processing any new request.
5585 */
5586void http_init_txn(struct stream *s)
5587{
5588 struct http_txn *txn = s->txn;
5589 struct conn_stream *cs = objt_cs(s->si[0].end);
5590
5591 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5592 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5593 : 0);
5594 txn->status = -1;
5595 *(unsigned int *)txn->cache_hash = 0;
5596
5597 txn->cookie_first_date = 0;
5598 txn->cookie_last_date = 0;
5599
5600 txn->srv_cookie = NULL;
5601 txn->cli_cookie = NULL;
5602 txn->uri = NULL;
5603
5604 http_txn_reset_req(txn);
5605 http_txn_reset_res(txn);
5606
5607 txn->req.chn = &s->req;
5608 txn->rsp.chn = &s->res;
5609
5610 txn->auth.method = HTTP_AUTH_UNKNOWN;
5611
5612 vars_init(&s->vars_txn, SCOPE_TXN);
5613 vars_init(&s->vars_reqres, SCOPE_REQ);
5614}
5615
5616/* to be used at the end of a transaction */
5617void http_end_txn(struct stream *s)
5618{
5619 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005620
5621 /* these ones will have been dynamically allocated */
5622 pool_free(pool_head_requri, txn->uri);
5623 pool_free(pool_head_capture, txn->cli_cookie);
5624 pool_free(pool_head_capture, txn->srv_cookie);
5625 pool_free(pool_head_uniqueid, s->unique_id);
5626
5627 s->unique_id = NULL;
5628 txn->uri = NULL;
5629 txn->srv_cookie = NULL;
5630 txn->cli_cookie = NULL;
5631
Christopher Faulet59399252019-11-07 14:27:52 +01005632 if (!LIST_ISEMPTY(&s->vars_txn.head))
5633 vars_prune(&s->vars_txn, s->sess, s);
5634 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5635 vars_prune(&s->vars_reqres, s->sess, s);
5636}
5637
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005638
5639DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
5640DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
Christopher Faulet0f226952018-10-22 09:29:56 +02005641
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005642__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005643static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005644{
5645}
5646
5647
5648/*
5649 * Local variables:
5650 * c-indent-level: 8
5651 * c-basic-offset: 8
5652 * End:
5653 */