blob: c4973e8ed58f6932bf06ef69e67cefece8ae4a96 [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
Willy Tarreaudcc048a2020-06-04 19:11:43 +020013#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020014#include <haproxy/action-t.h>
Willy Tarreau49801602020-06-04 22:50:02 +020015#include <haproxy/backend.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020016#include <haproxy/api.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020017#include <haproxy/base64.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020018#include <haproxy/capture-t.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020019#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020020#include <haproxy/check.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020021#include <haproxy/connection.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020022#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020023#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020024#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020025#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020026#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020027#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020028#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020029#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020030#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020031#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020032#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020033#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020034#include <haproxy/stream_interface.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020035#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020036#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020037#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020038
Christopher Faulete0768eb2018-10-03 16:38:02 +020039
Christopher Fauleteea8fc72019-11-05 16:18:10 +010040#define TRACE_SOURCE &trace_strm
41
Christopher Faulet377c5a52018-10-24 21:21:30 +020042extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020043
Christopher Fauleta8a46e22019-07-16 14:53:09 +020044struct pool_head *pool_head_requri = NULL;
45struct pool_head *pool_head_capture = NULL;
46
47
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020048static void http_end_request(struct stream *s);
49static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020050
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020051static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
52static int http_del_hdr_value(char *start, char *end, char **from, char *next);
53static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020054static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
55static 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 +020056
Christopher Fauletb58f62b2020-01-13 16:40:13 +010057static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020058static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020059
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020060static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
61static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020062
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020063static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
64static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020065
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020066static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
67static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010068
Christopher Faulete0768eb2018-10-03 16:38:02 +020069/* This stream analyser waits for a complete HTTP request. It returns 1 if the
70 * processing can continue on next analysers, or zero if it either needs more
71 * data or wants to immediately abort the request (eg: timeout, error, ...). It
72 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
73 * when it has nothing left to do, and may remove any analyser when it wants to
74 * abort.
75 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020076int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020077{
Christopher Faulet9768c262018-10-22 09:34:31 +020078
Christopher Faulete0768eb2018-10-03 16:38:02 +020079 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020080 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020081 *
Christopher Faulet9768c262018-10-22 09:34:31 +020082 * Once the start line and all headers are received, we may perform a
83 * capture of the error (if any), and we will set a few fields. We also
84 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020085 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020086 struct session *sess = s->sess;
87 struct http_txn *txn = s->txn;
88 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020089 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010090 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020091
Christopher Fauleteea8fc72019-11-05 16:18:10 +010092 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020093
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010094 htx = htxbuf(&req->buf);
Christopher Faulet9768c262018-10-22 09:34:31 +020095
Willy Tarreau4236f032019-03-05 10:43:32 +010096 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +020097 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +010098 stream_inc_http_req_ctr(s);
99 stream_inc_http_err_ctr(s);
100 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200101 if (htx->flags & HTX_FL_PARSING_ERROR)
102 goto return_bad_req;
103 else
104 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100105 }
106
Christopher Faulete0768eb2018-10-03 16:38:02 +0200107 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200108 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200109
110 /* If there is data available for analysis, log the end of the idle time. */
Christopher Faulet870aad92018-11-29 15:23:46 +0100111 if (c_data(req) && s->logs.t_idle == -1) {
112 const struct cs_info *csinfo = si_get_cs_info(objt_cs(s->si[0].end));
113
114 s->logs.t_idle = ((csinfo)
115 ? csinfo->t_idle
116 : tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake);
117 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200118
Christopher Faulete0768eb2018-10-03 16:38:02 +0200119 /*
120 * Now we quickly check if we have found a full valid request.
121 * If not so, we check the FD and buffer states before leaving.
122 * A full request is indicated by the fact that we have seen
123 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
124 * requests are checked first. When waiting for a second request
125 * on a keep-alive stream, if we encounter and error, close, t/o,
126 * we note the error in the stream flags but don't set any state.
127 * Since the error will be noted there, it will not be counted by
128 * process_stream() as a frontend error.
129 * Last, we may increase some tracked counters' http request errors on
130 * the cases that are deliberately the client's fault. For instance,
131 * a timeout or connection reset is not counted as an error. However
132 * a bad request is.
133 */
Christopher Faulet29f17582019-05-23 11:03:26 +0200134 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200135 if (htx->flags & HTX_FL_UPGRADE)
136 goto failed_keep_alive;
137
Christopher Faulet9768c262018-10-22 09:34:31 +0200138 /* 1: have we encountered a read error ? */
139 if (req->flags & CF_READ_ERROR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200140 if (!(s->flags & SF_ERR_MASK))
141 s->flags |= SF_ERR_CLICL;
142
143 if (txn->flags & TX_WAIT_NEXT_RQ)
144 goto failed_keep_alive;
145
146 if (sess->fe->options & PR_O_IGNORE_PRB)
147 goto failed_keep_alive;
148
Christopher Faulet9768c262018-10-22 09:34:31 +0200149 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200150 stream_inc_http_req_ctr(s);
151 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100152 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200153 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100154 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200155
Christopher Faulet9768c262018-10-22 09:34:31 +0200156 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200157 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet9768c262018-10-22 09:34:31 +0200158 req->analysers &= AN_REQ_FLT_END;
159
Christopher Faulete0768eb2018-10-03 16:38:02 +0200160 if (!(s->flags & SF_FINST_MASK))
161 s->flags |= SF_FINST_R;
162 return 0;
163 }
164
Christopher Faulet9768c262018-10-22 09:34:31 +0200165 /* 2: has the read timeout expired ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200166 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
167 if (!(s->flags & SF_ERR_MASK))
168 s->flags |= SF_ERR_CLITO;
169
170 if (txn->flags & TX_WAIT_NEXT_RQ)
171 goto failed_keep_alive;
172
173 if (sess->fe->options & PR_O_IGNORE_PRB)
174 goto failed_keep_alive;
175
Christopher Faulet9768c262018-10-22 09:34:31 +0200176 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200177 stream_inc_http_req_ctr(s);
178 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100179 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200180 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100181 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200182
Christopher Faulet9768c262018-10-22 09:34:31 +0200183 txn->status = 408;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200184 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200185 req->analysers &= AN_REQ_FLT_END;
186
Christopher Faulete0768eb2018-10-03 16:38:02 +0200187 if (!(s->flags & SF_FINST_MASK))
188 s->flags |= SF_FINST_R;
189 return 0;
190 }
191
Christopher Faulet9768c262018-10-22 09:34:31 +0200192 /* 3: have we encountered a close ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200193 else if (req->flags & CF_SHUTR) {
194 if (!(s->flags & SF_ERR_MASK))
195 s->flags |= SF_ERR_CLICL;
196
197 if (txn->flags & TX_WAIT_NEXT_RQ)
198 goto failed_keep_alive;
199
200 if (sess->fe->options & PR_O_IGNORE_PRB)
201 goto failed_keep_alive;
202
Christopher Faulete0768eb2018-10-03 16:38:02 +0200203 stream_inc_http_err_ctr(s);
204 stream_inc_http_req_ctr(s);
205 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100206 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200207 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100208 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200209
Christopher Faulet9768c262018-10-22 09:34:31 +0200210 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200211 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200212 req->analysers &= AN_REQ_FLT_END;
213
Christopher Faulete0768eb2018-10-03 16:38:02 +0200214 if (!(s->flags & SF_FINST_MASK))
215 s->flags |= SF_FINST_R;
216 return 0;
217 }
218
219 channel_dont_connect(req);
220 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
221 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100222
Christopher Faulet9768c262018-10-22 09:34:31 +0200223 if (sess->listener->options & LI_O_NOQUICKACK && htx_is_not_empty(htx) &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200224 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
225 /* We need more data, we have to re-enable quick-ack in case we
226 * previously disabled it, otherwise we might cause the client
227 * to delay next data.
228 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100229 conn_set_quickack(objt_conn(sess->origin), 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200230 }
Willy Tarreau1a18b542018-12-11 16:37:42 +0100231
Christopher Faulet47365272018-10-31 17:40:50 +0100232 if ((req->flags & CF_READ_PARTIAL) && (txn->flags & TX_WAIT_NEXT_RQ)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200233 /* If the client starts to talk, let's fall back to
234 * request timeout processing.
235 */
236 txn->flags &= ~TX_WAIT_NEXT_RQ;
237 req->analyse_exp = TICK_ETERNITY;
238 }
239
240 /* just set the request timeout once at the beginning of the request */
241 if (!tick_isset(req->analyse_exp)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100242 if ((txn->flags & TX_WAIT_NEXT_RQ) && tick_isset(s->be->timeout.httpka))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200243 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
244 else
245 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
246 }
247
248 /* we're not ready yet */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100249 DBG_TRACE_DEVEL("waiting for the request",
250 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200251 return 0;
252
253 failed_keep_alive:
254 /* Here we process low-level errors for keep-alive requests. In
255 * short, if the request is not the first one and it experiences
256 * a timeout, read error or shutdown, we just silently close so
257 * that the client can try again.
258 */
259 txn->status = 0;
260 msg->msg_state = HTTP_MSG_RQBEFORE;
261 req->analysers &= AN_REQ_FLT_END;
262 s->logs.logwait = 0;
263 s->logs.level = 0;
264 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200265 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100266 DBG_TRACE_DEVEL("leaving by closing K/A connection",
267 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200268 return 0;
269 }
270
Christopher Faulet9768c262018-10-22 09:34:31 +0200271 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200272 stream_inc_http_req_ctr(s);
273 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
274
Christopher Faulet9768c262018-10-22 09:34:31 +0200275 /* kill the pending keep-alive timeout */
276 txn->flags &= ~TX_WAIT_NEXT_RQ;
277 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200278
Christopher Faulet29f17582019-05-23 11:03:26 +0200279 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200280 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100281
Christopher Faulet9768c262018-10-22 09:34:31 +0200282 /* 0: we might have to print this header in debug mode */
283 if (unlikely((global.mode & MODE_DEBUG) &&
284 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
285 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200286
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200287 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200288
Christopher Fauleta3f15502019-05-13 15:27:23 +0200289 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200290 struct htx_blk *blk = htx_get_blk(htx, pos);
291 enum htx_blk_type type = htx_get_blk_type(blk);
292
293 if (type == HTX_BLK_EOH)
294 break;
295 if (type != HTX_BLK_HDR)
296 continue;
297
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200298 http_debug_hdr("clihdr", s,
299 htx_get_blk_name(htx, blk),
300 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200301 }
302 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200303
304 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100305 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200306 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100307 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100308 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200309 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100310 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +0100311 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100312 if (sl->flags & HTX_SL_F_BODYLESS)
313 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200314
315 /* we can make use of server redirect on GET and HEAD */
316 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
317 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100318 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200319 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200320 goto return_bad_req;
321 }
322
323 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100324 * 2: check if the URI matches the monitor_uri. We have to do this for
325 * every request which gets in, because the monitor-uri is defined by
326 * the frontend. If the monitor-uri starts with a '/', the matching is
327 * done against the request's path. Otherwise, the request's uri is
328 * used. It is a workaround to let HTTP/2 health-checks work as
329 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200330 */
331 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Faulet6072beb2020-02-18 15:34:58 +0100332 ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)),
333 ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) ||
334 isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200335 /*
336 * We have found the monitor URI
337 */
338 struct acl_cond *cond;
339
340 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100341 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200342
343 /* Check if we want to fail this monitor request or not */
344 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
345 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
346
347 ret = acl_pass(ret);
348 if (cond->pol == ACL_COND_UNLESS)
349 ret = !ret;
350
351 if (ret) {
352 /* we fail this request, let's return 503 service unavail */
353 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200354 if (!(s->flags & SF_ERR_MASK))
355 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
356 goto return_prx_cond;
357 }
358 }
359
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800360 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200361 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200362 if (!(s->flags & SF_ERR_MASK))
363 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
364 goto return_prx_cond;
365 }
366
367 /*
368 * 3: Maybe we have to copy the original REQURI for the logs ?
369 * Note: we cannot log anymore if the request has been
370 * classified as invalid.
371 */
372 if (unlikely(s->logs.logwait & LW_REQ)) {
373 /* we have a complete HTTP request that we must log */
374 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200375 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200376
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200377 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200378 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200379
380 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
381 s->do_log(s);
382 } else {
383 ha_alert("HTTP logging : out of memory.\n");
384 }
385 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200386
Christopher Faulete0768eb2018-10-03 16:38:02 +0200387 /* if the frontend has "option http-use-proxy-header", we'll check if
388 * we have what looks like a proxied connection instead of a connection,
389 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
390 * Note that this is *not* RFC-compliant, however browsers and proxies
391 * happen to do that despite being non-standard :-(
392 * We consider that a request not beginning with either '/' or '*' is
393 * a proxied connection, which covers both "scheme://location" and
394 * CONNECT ip:port.
395 */
396 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100397 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200398 txn->flags |= TX_USE_PX_CONN;
399
Christopher Faulete0768eb2018-10-03 16:38:02 +0200400 /* 5: we may need to capture headers */
401 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200402 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200403
Christopher Faulete0768eb2018-10-03 16:38:02 +0200404 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200405 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200406 req->analysers |= AN_REQ_HTTP_BODY;
407
408 /*
409 * RFC7234#4:
410 * A cache MUST write through requests with methods
411 * that are unsafe (Section 4.2.1 of [RFC7231]) to
412 * the origin server; i.e., a cache is not allowed
413 * to generate a reply to such a request before
414 * having forwarded the request and having received
415 * a corresponding response.
416 *
417 * RFC7231#4.2.1:
418 * Of the request methods defined by this
419 * specification, the GET, HEAD, OPTIONS, and TRACE
420 * methods are defined to be safe.
421 */
422 if (likely(txn->meth == HTTP_METH_GET ||
423 txn->meth == HTTP_METH_HEAD ||
424 txn->meth == HTTP_METH_OPTIONS ||
425 txn->meth == HTTP_METH_TRACE))
426 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
427
428 /* end of job, return OK */
429 req->analysers &= ~an_bit;
430 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200431
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100432 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200433 return 1;
434
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200435 return_int_err:
436 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200437 if (!(s->flags & SF_ERR_MASK))
438 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100439 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200440 if (sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100441 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200442 goto return_prx_cond;
443
Christopher Faulete0768eb2018-10-03 16:38:02 +0200444 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200445 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100446 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200447 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100448 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200449 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200450
451 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200452 http_reply_and_close(s, txn->status, http_error_message(s));
453
Christopher Faulete0768eb2018-10-03 16:38:02 +0200454 if (!(s->flags & SF_ERR_MASK))
455 s->flags |= SF_ERR_PRXCOND;
456 if (!(s->flags & SF_FINST_MASK))
457 s->flags |= SF_FINST_R;
458
459 req->analysers &= AN_REQ_FLT_END;
460 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100461 DBG_TRACE_DEVEL("leaving on error",
462 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200463 return 0;
464}
465
466
467/* This stream analyser runs all HTTP request processing which is common to
468 * frontends and backends, which means blocking ACLs, filters, connection-close,
469 * reqadd, stats and redirects. This is performed for the designated proxy.
470 * It returns 1 if the processing can continue on next analysers, or zero if it
471 * either needs more data or wants to immediately abort the request (eg: deny,
472 * error, ...).
473 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200474int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200475{
476 struct session *sess = s->sess;
477 struct http_txn *txn = s->txn;
478 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200479 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200480 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200481 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200482 struct connection *conn = objt_conn(sess->origin);
483
484 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
485 /* we need more data */
486 goto return_prx_yield;
487 }
488
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100489 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200490
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100491 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200492
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200493 /* just in case we have some per-backend tracking. Only called the first
494 * execution of the analyser. */
495 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
496 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200497
498 /* evaluate http-request rules */
499 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100500 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200501
502 switch (verdict) {
503 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
504 goto return_prx_yield;
505
506 case HTTP_RULE_RES_CONT:
507 case HTTP_RULE_RES_STOP: /* nothing to do */
508 break;
509
510 case HTTP_RULE_RES_DENY: /* deny or tarpit */
511 if (txn->flags & TX_CLTARPIT)
512 goto tarpit;
513 goto deny;
514
515 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
516 goto return_prx_cond;
517
518 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
519 goto done;
520
521 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
522 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100523
524 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
525 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200526 }
527 }
528
529 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100530 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200531 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200532
Christopher Fauletff2759f2018-10-24 11:13:16 +0200533 ctx.blk = NULL;
534 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
535 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100536 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200537 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200538 }
539
540 /* OK at this stage, we know that the request was accepted according to
541 * the http-request rules, we can check for the stats. Note that the
542 * URI is detected *before* the req* rules in order not to be affected
543 * by a possible reqrep, while they are processed *after* so that a
544 * reqdeny can still block them. This clearly needs to change in 1.6!
545 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200546 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200547 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100548 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200549 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200550 if (!(s->flags & SF_ERR_MASK))
551 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100552 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200553 }
554
555 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200556 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100557 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200558 /* not all actions implemented: deny, allow, auth */
559
560 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
561 goto deny;
562
563 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
564 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100565
566 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
567 goto return_bad_req;
568
569 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
570 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200571 }
572
Christopher Faulet2571bc62019-03-01 11:44:26 +0100573 /* Proceed with the applets now. */
574 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200575 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100576 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200577
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200578 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100579 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100580
Christopher Faulete0768eb2018-10-03 16:38:02 +0200581 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
582 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
583 if (!(s->flags & SF_FINST_MASK))
584 s->flags |= SF_FINST_R;
585
586 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
587 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
588 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
589 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100590
591 req->flags |= CF_SEND_DONTWAIT;
592 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200593 goto done;
594 }
595
596 /* check whether we have some ACLs set to redirect this request */
597 list_for_each_entry(rule, &px->redirect_rules, list) {
598 if (rule->cond) {
599 int ret;
600
601 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
602 ret = acl_pass(ret);
603 if (rule->cond->pol == ACL_COND_UNLESS)
604 ret = !ret;
605 if (!ret)
606 continue;
607 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200608 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100609 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200610 goto done;
611 }
612
613 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
614 * If this happens, then the data will not come immediately, so we must
615 * send all what we have without waiting. Note that due to the small gain
616 * in waiting for the body of the request, it's easier to simply put the
617 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
618 * itself once used.
619 */
620 req->flags |= CF_SEND_DONTWAIT;
621
622 done: /* done with this analyser, continue with next ones that the calling
623 * points will have set, if any.
624 */
625 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500626 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200627 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100628 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200629 return 1;
630
631 tarpit:
632 /* Allow cookie logging
633 */
634 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200635 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200636
637 /* When a connection is tarpitted, we use the tarpit timeout,
638 * which may be the same as the connect timeout if unspecified.
639 * If unset, then set it to zero because we really want it to
640 * eventually expire. We build the tarpit as an analyser.
641 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100642 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200643
644 /* wipe the request out so that we can drop the connection early
645 * if the client closes first.
646 */
647 channel_dont_connect(req);
648
Christopher Faulete0768eb2018-10-03 16:38:02 +0200649 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
650 req->analysers |= AN_REQ_HTTP_TARPIT;
651 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
652 if (!req->analyse_exp)
653 req->analyse_exp = tick_add(now_ms, 0);
654 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100655 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100656 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100657 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200658 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100659 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200660 goto done_without_exp;
661
662 deny: /* this request was blocked (denied) */
663
664 /* Allow cookie logging
665 */
666 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200667 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200668
Christopher Faulete0768eb2018-10-03 16:38:02 +0200669 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200670 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100671 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100672 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100673 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200674 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100675 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100676 goto return_prx_err;
677
678 return_int_err:
679 txn->status = 500;
680 if (!(s->flags & SF_ERR_MASK))
681 s->flags |= SF_ERR_INTERNAL;
682 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100683 if (s->flags & SF_BE_ASSIGNED)
684 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100685 if (sess->listener->counters)
686 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
687 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200688
689 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200690 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100691 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200692 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100693 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100694 /* fall through */
695
696 return_prx_err:
697 http_reply_and_close(s, txn->status, http_error_message(s));
698 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200699
700 return_prx_cond:
701 if (!(s->flags & SF_ERR_MASK))
702 s->flags |= SF_ERR_PRXCOND;
703 if (!(s->flags & SF_FINST_MASK))
704 s->flags |= SF_FINST_R;
705
706 req->analysers &= AN_REQ_FLT_END;
707 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100708 DBG_TRACE_DEVEL("leaving on error",
709 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200710 return 0;
711
712 return_prx_yield:
713 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100714 DBG_TRACE_DEVEL("waiting for more data",
715 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200716 return 0;
717}
718
719/* This function performs all the processing enabled for the current request.
720 * It returns 1 if the processing can continue on next analysers, or zero if it
721 * needs more data, encounters an error, or wants to immediately abort the
722 * request. It relies on buffers flags, and updates s->req.analysers.
723 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200724int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200725{
726 struct session *sess = s->sess;
727 struct http_txn *txn = s->txn;
728 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200729 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200730 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
731
732 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
733 /* we need more data */
734 channel_dont_connect(req);
735 return 0;
736 }
737
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100738 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200739
740 /*
741 * Right now, we know that we have processed the entire headers
742 * and that unwanted requests have been filtered out. We can do
743 * whatever we want with the remaining request. Also, now we
744 * may have separate values for ->fe, ->be.
745 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100746 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200747
748 /*
749 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200750 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200751 */
752 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100753 struct htx_sl *sl;
754 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200755
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200756 if (!sockaddr_alloc(&s->target_addr)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200757 if (!(s->flags & SF_ERR_MASK))
758 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100759 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200760 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200761 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100762 uri = htx_sl_req_uri(sl);
763 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200764
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200765 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200766 goto return_bad_req;
767
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200768 s->target = &s->be->obj_type;
769 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
770
Christopher Faulete0768eb2018-10-03 16:38:02 +0200771 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200772 * uri.ptr and path.ptr (excluded). If it was not found, we need
773 * to replace from all the uri by a single "/".
774 *
775 * Instead of rewritting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100776 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200777 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200778 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100779 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200780 }
781
782 /*
783 * 7: Now we can work with the cookies.
784 * Note that doing so might move headers in the request, but
785 * the fields will stay coherent and the URI will not move.
786 * This should only be performed in the backend.
787 */
788 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200789 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200790
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100791 /* 8: Generate unique ID if a "unique-id-format" is defined.
792 *
793 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
794 * fetches only available in the HTTP request processing stage.
795 */
796 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100797 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200798
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100799 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100800 if (!(s->flags & SF_ERR_MASK))
801 s->flags |= SF_ERR_RESOURCE;
802 goto return_int_err;
803 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200804
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100805 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100806 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100807 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100808 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200809 }
810
811 /*
812 * 9: add X-Forwarded-For if either the frontend or the backend
813 * asks for it.
814 */
815 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200816 struct http_hdr_ctx ctx = { .blk = NULL };
817 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
818 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
819
Christopher Faulete0768eb2018-10-03 16:38:02 +0200820 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200821 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200822 /* The header is set to be added only if none is present
823 * and we found it, so don't do anything.
824 */
825 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200826 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200827 /* Add an X-Forwarded-For header unless the source IP is
828 * in the 'except' network range.
829 */
830 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200831 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200832 != sess->fe->except_net.s_addr) &&
833 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200834 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200835 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200836 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200837
838 /* Note: we rely on the backend to get the header name to be used for
839 * x-forwarded-for, because the header is really meant for the backends.
840 * However, if the backend did not specify any option, we have to rely
841 * on the frontend's header name.
842 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200843 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
844 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100845 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200846 }
847 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200848 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200849 /* FIXME: for the sake of completeness, we should also support
850 * 'except' here, although it is mostly useless in this case.
851 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200852 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200853
Christopher Faulete0768eb2018-10-03 16:38:02 +0200854 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200855 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200856 pn, sizeof(pn));
857
858 /* Note: we rely on the backend to get the header name to be used for
859 * x-forwarded-for, because the header is really meant for the backends.
860 * However, if the backend did not specify any option, we have to rely
861 * on the frontend's header name.
862 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200863 chunk_printf(&trash, "%s", pn);
864 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100865 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200866 }
867 }
868
869 /*
870 * 10: add X-Original-To if either the frontend or the backend
871 * asks for it.
872 */
873 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
874
875 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200876 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 +0200877 /* Add an X-Original-To header unless the destination IP is
878 * in the 'except' network range.
879 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200880 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200881 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200882 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200883 != sess->fe->except_to.s_addr) &&
884 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200885 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200886 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200887 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200888 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200889
890 /* Note: we rely on the backend to get the header name to be used for
891 * x-original-to, because the header is really meant for the backends.
892 * However, if the backend did not specify any option, we have to rely
893 * on the frontend's header name.
894 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200895 if (s->be->orgto_hdr_len)
896 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
897 else
898 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200899
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200900 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
901 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100902 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200903 }
904 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200905 }
906
Christopher Faulete0768eb2018-10-03 16:38:02 +0200907 /* If we have no server assigned yet and we're balancing on url_param
908 * with a POST request, we may be interested in checking the body for
909 * that parameter. This will be done in another analyser.
910 */
911 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100912 s->txn->meth == HTTP_METH_POST &&
913 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200914 channel_dont_connect(req);
915 req->analysers |= AN_REQ_HTTP_BODY;
916 }
917
918 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
919 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100920
Christopher Faulete0768eb2018-10-03 16:38:02 +0200921 /* We expect some data from the client. Unless we know for sure
922 * we already have a full request, we have to re-enable quick-ack
923 * in case we previously disabled it, otherwise we might cause
924 * the client to delay further data.
925 */
926 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200927 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100928 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200929
930 /*************************************************************
931 * OK, that's finished for the headers. We have done what we *
932 * could. Let's switch to the DATA state. *
933 ************************************************************/
934 req->analyse_exp = TICK_ETERNITY;
935 req->analysers &= ~an_bit;
936
937 s->logs.tv_request = now;
938 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100939 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200940 return 1;
941
Christopher Fauletb8a53712019-12-16 11:29:38 +0100942 return_int_err:
943 txn->status = 500;
944 if (!(s->flags & SF_ERR_MASK))
945 s->flags |= SF_ERR_INTERNAL;
946 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100947 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100948 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100949 if (sess->listener->counters)
950 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
951 goto return_prx_cond;
952
Christopher Faulete0768eb2018-10-03 16:38:02 +0200953 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200954 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100955 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200956 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100957 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100958 /* fall through */
959
960 return_prx_cond:
961 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200962
963 if (!(s->flags & SF_ERR_MASK))
964 s->flags |= SF_ERR_PRXCOND;
965 if (!(s->flags & SF_FINST_MASK))
966 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100967
968 req->analysers &= AN_REQ_FLT_END;
969 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100970 DBG_TRACE_DEVEL("leaving on error",
971 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200972 return 0;
973}
974
975/* This function is an analyser which processes the HTTP tarpit. It always
976 * returns zero, at the beginning because it prevents any other processing
977 * from occurring, and at the end because it terminates the request.
978 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200979int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200980{
981 struct http_txn *txn = s->txn;
982
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100983 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200984 /* This connection is being tarpitted. The CLIENT side has
985 * already set the connect expiration date to the right
986 * timeout. We just have to check that the client is still
987 * there and that the timeout has not expired.
988 */
989 channel_dont_connect(req);
990 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100991 !tick_is_expired(req->analyse_exp, now_ms)) {
992 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
993 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200994 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100995 }
996
Christopher Faulete0768eb2018-10-03 16:38:02 +0200997
998 /* We will set the queue timer to the time spent, just for
999 * logging purposes. We fake a 500 server error, so that the
1000 * attacker will not suspect his connection has been tarpitted.
1001 * It will not cause trouble to the logs because we can exclude
1002 * the tarpitted connections by filtering on the 'PT' status flags.
1003 */
1004 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1005
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02001006 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001007
1008 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001009 req->analysers &= AN_REQ_FLT_END;
1010 req->analyse_exp = TICK_ETERNITY;
1011
1012 if (!(s->flags & SF_ERR_MASK))
1013 s->flags |= SF_ERR_PRXCOND;
1014 if (!(s->flags & SF_FINST_MASK))
1015 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001016
1017 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001018 return 0;
1019}
1020
1021/* This function is an analyser which waits for the HTTP request body. It waits
1022 * for either the buffer to be full, or the full advertised contents to have
1023 * reached the buffer. It must only be called after the standard HTTP request
1024 * processing has occurred, because it expects the request to be parsed and will
1025 * look for the Expect header. It may send a 100-Continue interim response. It
1026 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
1027 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
1028 * needs to read more data, or 1 once it has completed its analysis.
1029 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001030int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001031{
1032 struct session *sess = s->sess;
1033 struct http_txn *txn = s->txn;
1034 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001035 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001036
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001037 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001038
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001039 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001040
Willy Tarreau4236f032019-03-05 10:43:32 +01001041 if (htx->flags & HTX_FL_PARSING_ERROR)
1042 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001043 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1044 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001045
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001046 if (msg->msg_state < HTTP_MSG_BODY)
1047 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001048
Christopher Faulete0768eb2018-10-03 16:38:02 +02001049 /* We have to parse the HTTP request body to find any required data.
1050 * "balance url_param check_post" should have been the only way to get
1051 * into this. We were brought here after HTTP header analysis, so all
1052 * related structures are ready.
1053 */
1054
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001055 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001056 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001057 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001058 }
1059
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001060 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001061
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001062 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1063 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001064 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001065 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001066 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001067 goto http_end;
1068
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001069 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001070 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1071 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001072 if (!(s->flags & SF_ERR_MASK))
1073 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001074 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1075 if (sess->listener->counters)
1076 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1077 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001078 }
1079
1080 /* we get here if we need to wait for more data */
1081 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1082 /* Not enough data. We'll re-use the http-request
1083 * timeout here. Ideally, we should set the timeout
1084 * relative to the accept() date. We just set the
1085 * request timeout once at the beginning of the
1086 * request.
1087 */
1088 channel_dont_connect(req);
1089 if (!tick_isset(req->analyse_exp))
1090 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001091 DBG_TRACE_DEVEL("waiting for more data",
1092 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001093 return 0;
1094 }
1095
1096 http_end:
1097 /* The situation will not evolve, so let's give up on the analysis. */
1098 s->logs.tv_request = now; /* update the request timer to reflect full request */
1099 req->analysers &= ~an_bit;
1100 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001101 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001102 return 1;
1103
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001104 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001105 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001106 if (!(s->flags & SF_ERR_MASK))
1107 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001108 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001109 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +01001110 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001111 if (sess->listener->counters)
1112 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
1113 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001114
Christopher Faulete0768eb2018-10-03 16:38:02 +02001115 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001116 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001117 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1118 if (sess->listener->counters)
1119 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1120 /* fall through */
1121
1122 return_prx_cond:
1123 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001124
1125 if (!(s->flags & SF_ERR_MASK))
1126 s->flags |= SF_ERR_PRXCOND;
1127 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001128 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001129
Christopher Faulete0768eb2018-10-03 16:38:02 +02001130 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001131 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001132 DBG_TRACE_DEVEL("leaving on error",
1133 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001134 return 0;
1135}
1136
1137/* This function is an analyser which forwards request body (including chunk
1138 * sizes if any). It is called as soon as we must forward, even if we forward
1139 * zero byte. The only situation where it must not be called is when we're in
1140 * tunnel mode and we want to forward till the close. It's used both to forward
1141 * remaining data and to resync after end of body. It expects the msg_state to
1142 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1143 * read more data, or 1 once we can go on with next request or end the stream.
1144 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1145 * bytes of pending data + the headers if not already done.
1146 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001147int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001148{
1149 struct session *sess = s->sess;
1150 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001151 struct http_msg *msg = &txn->req;
1152 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001153 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001154 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001155
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001156 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001157
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001158 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001159
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001160 if (htx->flags & HTX_FL_PARSING_ERROR)
1161 goto return_bad_req;
1162 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1163 goto return_int_err;
1164
Christopher Faulete0768eb2018-10-03 16:38:02 +02001165 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1166 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1167 /* Output closed while we were sending data. We must abort and
1168 * wake the other side up.
1169 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001170
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001171 /* Don't abort yet if we had L7 retries activated and it
1172 * was a write error, we may recover.
1173 */
1174 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001175 (s->si[1].flags & SI_FL_L7_RETRY)) {
1176 DBG_TRACE_DEVEL("leaving on L7 retry",
1177 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001178 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001179 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001180 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001181 http_end_request(s);
1182 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001183 DBG_TRACE_DEVEL("leaving on error",
1184 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001185 return 1;
1186 }
1187
1188 /* Note that we don't have to send 100-continue back because we don't
1189 * need the data to complete our job, and it's up to the server to
1190 * decide whether to return 100, 417 or anything else in return of
1191 * an "Expect: 100-continue" header.
1192 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001193 if (msg->msg_state == HTTP_MSG_BODY)
1194 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001195
Christopher Faulete0768eb2018-10-03 16:38:02 +02001196 /* in most states, we should abort in case of early close */
1197 channel_auto_close(req);
1198
1199 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001200 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001201 if (req->flags & CF_EOI)
1202 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001203 }
1204 else {
1205 /* We can't process the buffer's contents yet */
1206 req->flags |= CF_WAKE_WRITE;
1207 goto missing_data_or_waiting;
1208 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001209 }
1210
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001211 if (msg->msg_state >= HTTP_MSG_ENDING)
1212 goto ending;
1213
1214 if (txn->meth == HTTP_METH_CONNECT) {
1215 msg->msg_state = HTTP_MSG_ENDING;
1216 goto ending;
1217 }
1218
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001219 /* Forward input data. We get it by removing all outgoing data not
1220 * forwarded yet from HTX data size. If there are some data filters, we
1221 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001222 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001223 if (HAS_REQ_DATA_FILTERS(s)) {
1224 ret = flt_http_payload(s, msg, htx->data);
1225 if (ret < 0)
1226 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001227 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001228 }
1229 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001230 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001231 if (msg->flags & HTTP_MSGF_XFER_LEN)
1232 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001233 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001234
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001235 if (htx->data != co_data(req))
1236 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001237
Christopher Faulet9768c262018-10-22 09:34:31 +02001238 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001239 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1240 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001241 */
1242 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1243 goto missing_data_or_waiting;
1244
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001245 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001246
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001247 ending:
1248 /* other states, ENDING...TUNNEL */
1249 if (msg->msg_state >= HTTP_MSG_DONE)
1250 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001251
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001252 if (HAS_REQ_DATA_FILTERS(s)) {
1253 ret = flt_http_end(s, msg);
1254 if (ret <= 0) {
1255 if (!ret)
1256 goto missing_data_or_waiting;
1257 goto return_bad_req;
1258 }
1259 }
1260
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001261 if (txn->meth == HTTP_METH_CONNECT)
1262 msg->msg_state = HTTP_MSG_TUNNEL;
1263 else {
1264 msg->msg_state = HTTP_MSG_DONE;
1265 req->to_forward = 0;
1266 }
1267
1268 done:
1269 /* we don't want to forward closes on DONE except in tunnel mode. */
1270 if (!(txn->flags & TX_CON_WANT_TUN))
1271 channel_dont_close(req);
1272
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001273 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001274 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001275 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001276 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1277 if (req->flags & CF_SHUTW) {
1278 /* request errors are most likely due to the
1279 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001280 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001281 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001282 goto return_bad_req;
1283 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001284 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001285 return 1;
1286 }
1287
1288 /* If "option abortonclose" is set on the backend, we want to monitor
1289 * the client's connection and forward any shutdown notification to the
1290 * server, which will decide whether to close or to go on processing the
1291 * request. We only do that in tunnel mode, and not in other modes since
1292 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001293 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001294 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001295 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001296 s->si[1].flags |= SI_FL_NOLINGER;
1297 channel_auto_close(req);
1298 }
1299 else if (s->txn->meth == HTTP_METH_POST) {
1300 /* POST requests may require to read extra CRLF sent by broken
1301 * browsers and which could cause an RST to be sent upon close
1302 * on some systems (eg: Linux). */
1303 channel_auto_read(req);
1304 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001305 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1306 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001307 return 0;
1308
1309 missing_data_or_waiting:
1310 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001311 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001312 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001313
1314 waiting:
1315 /* waiting for the last bits to leave the buffer */
1316 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001317 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001318
1319 /* When TE: chunked is used, we need to get there again to parse remaining
1320 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1321 * And when content-length is used, we never want to let the possible
1322 * shutdown be forwarded to the other side, as the state machine will
1323 * take care of it once the client responds. It's also important to
1324 * prevent TIME_WAITs from accumulating on the backend side, and for
1325 * HTTP/2 where the last frame comes with a shutdown.
1326 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001327 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001328 channel_dont_close(req);
1329
1330 /* We know that more data are expected, but we couldn't send more that
1331 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1332 * system knows it must not set a PUSH on this first part. Interactive
1333 * modes are already handled by the stream sock layer. We must not do
1334 * this in content-length mode because it could present the MSG_MORE
1335 * flag with the last block of forwarded data, which would cause an
1336 * additional delay to be observed by the receiver.
1337 */
1338 if (msg->flags & HTTP_MSGF_TE_CHNK)
1339 req->flags |= CF_EXPECT_MORE;
1340
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001341 DBG_TRACE_DEVEL("waiting for more data to forward",
1342 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001343 return 0;
1344
Christopher Faulet93e02d82019-03-08 14:18:50 +01001345 return_cli_abort:
1346 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1347 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001348 if (sess->listener->counters)
1349 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001350 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001351 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001352 if (!(s->flags & SF_ERR_MASK))
1353 s->flags |= SF_ERR_CLICL;
1354 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001355 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001356
1357 return_srv_abort:
1358 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1359 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001360 if (sess->listener->counters)
1361 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001362 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001363 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001364 if (!(s->flags & SF_ERR_MASK))
1365 s->flags |= SF_ERR_SRVCL;
1366 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001367 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001368
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001369 return_int_err:
1370 if (!(s->flags & SF_ERR_MASK))
1371 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001372 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001373 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001374 if (sess->listener->counters)
1375 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001376 if (objt_server(s->target))
1377 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001378 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001379 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001380
Christopher Faulet93e02d82019-03-08 14:18:50 +01001381 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001382 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001383 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001384 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001385 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001386 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001387
Christopher Fauletb8a53712019-12-16 11:29:38 +01001388 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001389 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001390 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001391 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001392 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001393 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001394 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001395 }
1396 req->analysers &= AN_REQ_FLT_END;
1397 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 +01001398 if (!(s->flags & SF_ERR_MASK))
1399 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001400 if (!(s->flags & SF_FINST_MASK))
1401 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001402 DBG_TRACE_DEVEL("leaving on error ",
1403 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001404 return 0;
1405}
1406
Olivier Houcharda254a372019-04-05 15:30:12 +02001407/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1408/* Returns 0 if we can attempt to retry, -1 otherwise */
1409static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1410{
1411 struct channel *req, *res;
1412 int co_data;
1413
1414 si->conn_retries--;
1415 if (si->conn_retries < 0)
1416 return -1;
1417
Willy Tarreau223995e2019-05-04 10:38:31 +02001418 if (objt_server(s->target))
1419 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1420 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1421
Olivier Houcharda254a372019-04-05 15:30:12 +02001422 req = &s->req;
1423 res = &s->res;
1424 /* Remove any write error from the request, and read error from the response */
1425 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1426 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1427 res->analysers = 0;
1428 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001429 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001430 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001431 si->exp = TICK_ETERNITY;
1432 res->rex = TICK_ETERNITY;
1433 res->to_forward = 0;
1434 res->analyse_exp = TICK_ETERNITY;
1435 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001436 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001437 si_release_endpoint(&s->si[1]);
1438 b_free(&req->buf);
1439 /* Swap the L7 buffer with the channel buffer */
1440 /* We know we stored the co_data as b_data, so get it there */
1441 co_data = b_data(&si->l7_buffer);
1442 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1443 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1444
1445 co_set_data(req, co_data);
1446 b_reset(&res->buf);
1447 co_set_data(res, 0);
1448 return 0;
1449}
1450
Christopher Faulete0768eb2018-10-03 16:38:02 +02001451/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1452 * processing can continue on next analysers, or zero if it either needs more
1453 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1454 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1455 * when it has nothing left to do, and may remove any analyser when it wants to
1456 * abort.
1457 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001458int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001459{
Christopher Faulet9768c262018-10-22 09:34:31 +02001460 /*
1461 * We will analyze a complete HTTP response to check the its syntax.
1462 *
1463 * Once the start line and all headers are received, we may perform a
1464 * capture of the error (if any), and we will set a few fields. We also
1465 * logging and finally headers capture.
1466 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001467 struct session *sess = s->sess;
1468 struct http_txn *txn = s->txn;
1469 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001470 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001471 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001472 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001473 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001474 int n;
1475
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001476 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001477
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001478 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001479
Willy Tarreau4236f032019-03-05 10:43:32 +01001480 /* Parsing errors are caught here */
1481 if (htx->flags & HTX_FL_PARSING_ERROR)
1482 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001483 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1484 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001485
Christopher Faulete0768eb2018-10-03 16:38:02 +02001486 /*
1487 * Now we quickly check if we have found a full valid response.
1488 * If not so, we check the FD and buffer states before leaving.
1489 * A full response is indicated by the fact that we have seen
1490 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1491 * responses are checked first.
1492 *
1493 * Depending on whether the client is still there or not, we
1494 * may send an error response back or not. Note that normally
1495 * we should only check for HTTP status there, and check I/O
1496 * errors somewhere else.
1497 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001498 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001499 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001500 /* 1: have we encountered a read error ? */
1501 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001502 struct connection *conn = NULL;
1503
Olivier Houchard865d8392019-05-03 22:46:27 +02001504 if (objt_cs(s->si[1].end))
1505 conn = objt_cs(s->si[1].end)->conn;
1506
1507 if (si_b->flags & SI_FL_L7_RETRY &&
1508 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001509 /* If we arrive here, then CF_READ_ERROR was
1510 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001511 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001512 * the SI_FL_L7_RETRY flag, so it's ok not
1513 * to check s->be->retry_type.
1514 */
1515 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1516 return 0;
1517 }
1518
Olivier Houchard6db16992019-05-17 15:40:49 +02001519 if (txn->flags & TX_NOT_FIRST)
1520 goto abort_keep_alive;
1521
Olivier Houcharda798bf52019-03-08 18:52:00 +01001522 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001523 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001524 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001525 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001526 }
1527
Christopher Faulete0768eb2018-10-03 16:38:02 +02001528 rep->analysers &= AN_RES_FLT_END;
1529 txn->status = 502;
1530
1531 /* Check to see if the server refused the early data.
1532 * If so, just send a 425
1533 */
Olivier Houchard865d8392019-05-03 22:46:27 +02001534 if (conn->err_code == CO_ER_SSL_EARLY_FAILED) {
1535 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001536 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001537 do_l7_retry(s, si_b) == 0) {
1538 DBG_TRACE_DEVEL("leaving on L7 retry",
1539 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001540 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001541 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001542 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001543 }
1544
1545 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001546 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001547
1548 if (!(s->flags & SF_ERR_MASK))
1549 s->flags |= SF_ERR_SRVCL;
1550 if (!(s->flags & SF_FINST_MASK))
1551 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001552 DBG_TRACE_DEVEL("leaving on error",
1553 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001554 return 0;
1555 }
1556
Christopher Faulet9768c262018-10-22 09:34:31 +02001557 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001558 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001559 if ((si_b->flags & SI_FL_L7_RETRY) &&
1560 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001561 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1562 DBG_TRACE_DEVEL("leaving on L7 retry",
1563 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001564 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001565 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001566 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001567 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001568 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001569 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001570 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001571 }
1572
Christopher Faulete0768eb2018-10-03 16:38:02 +02001573 rep->analysers &= AN_RES_FLT_END;
1574 txn->status = 504;
1575 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001576 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001577
1578 if (!(s->flags & SF_ERR_MASK))
1579 s->flags |= SF_ERR_SRVTO;
1580 if (!(s->flags & SF_FINST_MASK))
1581 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001582 DBG_TRACE_DEVEL("leaving on error",
1583 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001584 return 0;
1585 }
1586
Christopher Faulet9768c262018-10-22 09:34:31 +02001587 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001588 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001589 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1590 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001591 if (sess->listener->counters)
1592 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001593 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001594 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001595
1596 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001597 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001598 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001599
1600 if (!(s->flags & SF_ERR_MASK))
1601 s->flags |= SF_ERR_CLICL;
1602 if (!(s->flags & SF_FINST_MASK))
1603 s->flags |= SF_FINST_H;
1604
1605 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001606 DBG_TRACE_DEVEL("leaving on error",
1607 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001608 return 0;
1609 }
1610
Christopher Faulet9768c262018-10-22 09:34:31 +02001611 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001612 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001613 if ((si_b->flags & SI_FL_L7_RETRY) &&
1614 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001615 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1616 DBG_TRACE_DEVEL("leaving on L7 retry",
1617 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001618 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001619 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001620 }
1621
Olivier Houchard6db16992019-05-17 15:40:49 +02001622 if (txn->flags & TX_NOT_FIRST)
1623 goto abort_keep_alive;
1624
Olivier Houcharda798bf52019-03-08 18:52:00 +01001625 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001626 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001627 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001628 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001629 }
1630
Christopher Faulete0768eb2018-10-03 16:38:02 +02001631 rep->analysers &= AN_RES_FLT_END;
1632 txn->status = 502;
1633 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001634 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001635
1636 if (!(s->flags & SF_ERR_MASK))
1637 s->flags |= SF_ERR_SRVCL;
1638 if (!(s->flags & SF_FINST_MASK))
1639 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001640 DBG_TRACE_DEVEL("leaving on error",
1641 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001642 return 0;
1643 }
1644
Christopher Faulet9768c262018-10-22 09:34:31 +02001645 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001646 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001647 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001648 goto abort_keep_alive;
1649
Olivier Houcharda798bf52019-03-08 18:52:00 +01001650 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001651 if (objt_server(s->target))
1652 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001653 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001654
1655 if (!(s->flags & SF_ERR_MASK))
1656 s->flags |= SF_ERR_CLICL;
1657 if (!(s->flags & SF_FINST_MASK))
1658 s->flags |= SF_FINST_H;
1659
1660 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001661 DBG_TRACE_DEVEL("leaving on error",
1662 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001663 return 0;
1664 }
1665
1666 channel_dont_close(rep);
1667 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001668 DBG_TRACE_DEVEL("waiting for more data",
1669 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001670 return 0;
1671 }
1672
1673 /* More interesting part now : we know that we have a complete
1674 * response which at least looks like HTTP. We have an indicator
1675 * of each header's length, so we can parse them quickly.
1676 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001677 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001678 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001679 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001680
Christopher Faulet9768c262018-10-22 09:34:31 +02001681 /* 0: we might have to print this header in debug mode */
1682 if (unlikely((global.mode & MODE_DEBUG) &&
1683 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1684 int32_t pos;
1685
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001686 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001687
Christopher Fauleta3f15502019-05-13 15:27:23 +02001688 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001689 struct htx_blk *blk = htx_get_blk(htx, pos);
1690 enum htx_blk_type type = htx_get_blk_type(blk);
1691
1692 if (type == HTX_BLK_EOH)
1693 break;
1694 if (type != HTX_BLK_HDR)
1695 continue;
1696
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001697 http_debug_hdr("srvhdr", s,
1698 htx_get_blk_name(htx, blk),
1699 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001700 }
1701 }
1702
Christopher Faulet03599112018-11-27 11:21:21 +01001703 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001704 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001705 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001706 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001707 if (sl->flags & HTX_SL_F_XFER_LEN) {
1708 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001709 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001710 if (sl->flags & HTX_SL_F_BODYLESS)
1711 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001712 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001713
1714 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001715 if (n < 1 || n > 5)
1716 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001717
Christopher Faulete0768eb2018-10-03 16:38:02 +02001718 /* when the client triggers a 4xx from the server, it's most often due
1719 * to a missing object or permission. These events should be tracked
1720 * because if they happen often, it may indicate a brute force or a
1721 * vulnerability scan.
1722 */
1723 if (n == 4)
1724 stream_inc_http_err_ctr(s);
1725
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001726 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001727 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001728 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1729 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001730
Christopher Faulete0768eb2018-10-03 16:38:02 +02001731 /* Adjust server's health based on status code. Note: status codes 501
1732 * and 505 are triggered on demand by client request, so we must not
1733 * count them as server failures.
1734 */
1735 if (objt_server(s->target)) {
1736 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001737 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001738 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001739 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001740 }
1741
1742 /*
1743 * We may be facing a 100-continue response, or any other informational
1744 * 1xx response which is non-final, in which case this is not the right
1745 * response, and we're waiting for the next one. Let's allow this response
1746 * to go to the client and wait for the next one. There's an exception for
1747 * 101 which is used later in the code to switch protocols.
1748 */
1749 if (txn->status < 200 &&
1750 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001751 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001752 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001753 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001754 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001755 txn->status = 0;
1756 s->logs.t_data = -1; /* was not a response yet */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001757 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001758 }
1759
1760 /*
1761 * 2: check for cacheability.
1762 */
1763
1764 switch (txn->status) {
1765 case 200:
1766 case 203:
1767 case 204:
1768 case 206:
1769 case 300:
1770 case 301:
1771 case 404:
1772 case 405:
1773 case 410:
1774 case 414:
1775 case 501:
1776 break;
1777 default:
1778 /* RFC7231#6.1:
1779 * Responses with status codes that are defined as
1780 * cacheable by default (e.g., 200, 203, 204, 206,
1781 * 300, 301, 404, 405, 410, 414, and 501 in this
1782 * specification) can be reused by a cache with
1783 * heuristic expiration unless otherwise indicated
1784 * by the method definition or explicit cache
1785 * controls [RFC7234]; all other status codes are
1786 * not cacheable by default.
1787 */
1788 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1789 break;
1790 }
1791
1792 /*
1793 * 3: we may need to capture headers
1794 */
1795 s->logs.logwait &= ~LW_RESP;
1796 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001797 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001798
Christopher Faulet9768c262018-10-22 09:34:31 +02001799 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001800 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1801 txn->status == 101)) {
1802 /* Either we've established an explicit tunnel, or we're
1803 * switching the protocol. In both cases, we're very unlikely
1804 * to understand the next protocols. We have to switch to tunnel
1805 * mode, so that we transfer the request and responses then let
1806 * this protocol pass unmodified. When we later implement specific
1807 * parsers for such protocols, we'll want to check the Upgrade
1808 * header which contains information about that protocol for
1809 * responses with status 101 (eg: see RFC2817 about TLS).
1810 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001811 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001812 }
1813
Christopher Faulet61608322018-11-23 16:23:45 +01001814 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1815 * 407 (Proxy-Authenticate) responses and set the connection to private
1816 */
1817 srv_conn = cs_conn(objt_cs(s->si[1].end));
1818 if (srv_conn) {
1819 struct ist hdr;
1820 struct http_hdr_ctx ctx;
1821
1822 if (txn->status == 401)
1823 hdr = ist("WWW-Authenticate");
1824 else if (txn->status == 407)
1825 hdr = ist("Proxy-Authenticate");
1826 else
1827 goto end;
1828
1829 ctx.blk = NULL;
1830 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001831 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1832 * possibly followed by blanks and a base64 string, the connection
1833 * is private. Since it's a mess to deal with, we only check for
1834 * values starting with "NTLM" or "Nego". Note that often multiple
1835 * headers are sent by the server there.
1836 */
1837 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001838 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001839 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet61608322018-11-23 16:23:45 +01001840 srv_conn->flags |= CO_FL_PRIVATE;
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001841 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001842 }
Christopher Faulet61608322018-11-23 16:23:45 +01001843 }
1844 }
1845
1846 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001847 /* we want to have the response time before we start processing it */
1848 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1849
1850 /* end of job, return OK */
1851 rep->analysers &= ~an_bit;
1852 rep->analyse_exp = TICK_ETERNITY;
1853 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001854 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001855 return 1;
1856
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001857 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001858 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001859 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001860 if (sess->listener->counters)
1861 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001862 if (objt_server(s->target))
1863 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001864 txn->status = 500;
1865 if (!(s->flags & SF_ERR_MASK))
1866 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001867 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001868
1869 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001870 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001871 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001872 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001873 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001874 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001875 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001876 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001877 do_l7_retry(s, si_b) == 0) {
1878 DBG_TRACE_DEVEL("leaving on L7 retry",
1879 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001880 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001881 }
Christopher Faulet47365272018-10-31 17:40:50 +01001882 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001883 /* fall through */
1884
Christopher Fauletb8a53712019-12-16 11:29:38 +01001885 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001886 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001887
1888 if (!(s->flags & SF_ERR_MASK))
1889 s->flags |= SF_ERR_PRXCOND;
1890 if (!(s->flags & SF_FINST_MASK))
1891 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001892
1893 s->si[1].flags |= SI_FL_NOLINGER;
1894 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001895 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001896 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001897 DBG_TRACE_DEVEL("leaving on error",
1898 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001899 return 0;
1900
Christopher Faulete0768eb2018-10-03 16:38:02 +02001901 abort_keep_alive:
1902 /* A keep-alive request to the server failed on a network error.
1903 * The client is required to retry. We need to close without returning
1904 * any other information so that the client retries.
1905 */
1906 txn->status = 0;
1907 rep->analysers &= AN_RES_FLT_END;
1908 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001909 s->logs.logwait = 0;
1910 s->logs.level = 0;
1911 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001912 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001913 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1914 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001915 return 0;
1916}
1917
1918/* This function performs all the processing enabled for the current response.
1919 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1920 * and updates s->res.analysers. It might make sense to explode it into several
1921 * other functions. It works like process_request (see indications above).
1922 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001923int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001924{
1925 struct session *sess = s->sess;
1926 struct http_txn *txn = s->txn;
1927 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001928 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001929 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001930 enum rule_result ret = HTTP_RULE_RES_CONT;
1931
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001932 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1933 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001934
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001935 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001936
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001937 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001938
1939 /* The stats applet needs to adjust the Connection header but we don't
1940 * apply any filter there.
1941 */
1942 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1943 rep->analysers &= ~an_bit;
1944 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001945 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001946 }
1947
1948 /*
1949 * We will have to evaluate the filters.
1950 * As opposed to version 1.2, now they will be evaluated in the
1951 * filters order and not in the header order. This means that
1952 * each filter has to be validated among all headers.
1953 *
1954 * Filters are tried with ->be first, then with ->fe if it is
1955 * different from ->be.
1956 *
1957 * Maybe we are in resume condiion. In this case I choose the
1958 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001959 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001960 * the process with the first one.
1961 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001962 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001963 * pointer and the ->fe rule list. If it doesn't match, I initialize
1964 * the loop with the ->be.
1965 */
1966 if (s->current_rule_list == &sess->fe->http_res_rules)
1967 cur_proxy = sess->fe;
1968 else
1969 cur_proxy = s->be;
1970 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001971 /* evaluate http-response rules */
1972 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001973 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001974
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001975 switch (ret) {
1976 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1977 goto return_prx_yield;
1978
1979 case HTTP_RULE_RES_CONT:
1980 case HTTP_RULE_RES_STOP: /* nothing to do */
1981 break;
1982
1983 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1984 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001985
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001986 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1987 goto return_prx_cond;
1988
1989 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001990 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001991
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001992 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1993 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001994
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001995 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1996 goto return_int_err;
1997 }
1998
1999 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002000
Christopher Faulete0768eb2018-10-03 16:38:02 +02002001 /* check whether we're already working on the frontend */
2002 if (cur_proxy == sess->fe)
2003 break;
2004 cur_proxy = sess->fe;
2005 }
2006
Christopher Faulete0768eb2018-10-03 16:38:02 +02002007 /* OK that's all we can do for 1xx responses */
2008 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002009 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002010
2011 /*
2012 * Now check for a server cookie.
2013 */
2014 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002015 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002016
2017 /*
2018 * Check for cache-control or pragma headers if required.
2019 */
2020 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002021 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002022
2023 /*
2024 * Add server cookie in the response if needed
2025 */
2026 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
2027 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
2028 (!(s->flags & SF_DIRECT) ||
2029 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
2030 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
2031 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
2032 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
2033 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
2034 !(s->flags & SF_IGNORE_PRST)) {
2035 /* the server is known, it's not the one the client requested, or the
2036 * cookie's last seen date needs to be refreshed. We have to
2037 * insert a set-cookie here, except if we want to insert only on POST
2038 * requests and this one isn't. Note that servers which don't have cookies
2039 * (eg: some backup servers) will return a full cookie removal request.
2040 */
2041 if (!objt_server(s->target)->cookie) {
2042 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002043 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02002044 s->be->cookie_name);
2045 }
2046 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002047 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002048
2049 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2050 /* emit last_date, which is mandatory */
2051 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2052 s30tob64((date.tv_sec+3) >> 2,
2053 trash.area + trash.data);
2054 trash.data += 5;
2055
2056 if (s->be->cookie_maxlife) {
2057 /* emit first_date, which is either the original one or
2058 * the current date.
2059 */
2060 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2061 s30tob64(txn->cookie_first_date ?
2062 txn->cookie_first_date >> 2 :
2063 (date.tv_sec+3) >> 2,
2064 trash.area + trash.data);
2065 trash.data += 5;
2066 }
2067 }
2068 chunk_appendf(&trash, "; path=/");
2069 }
2070
2071 if (s->be->cookie_domain)
2072 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2073
2074 if (s->be->ck_opts & PR_CK_HTTPONLY)
2075 chunk_appendf(&trash, "; HttpOnly");
2076
2077 if (s->be->ck_opts & PR_CK_SECURE)
2078 chunk_appendf(&trash, "; Secure");
2079
Christopher Faulet2f533902020-01-21 11:06:48 +01002080 if (s->be->cookie_attrs)
2081 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
2082
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002083 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002084 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002085
2086 txn->flags &= ~TX_SCK_MASK;
2087 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2088 /* the server did not change, only the date was updated */
2089 txn->flags |= TX_SCK_UPDATED;
2090 else
2091 txn->flags |= TX_SCK_INSERTED;
2092
2093 /* Here, we will tell an eventual cache on the client side that we don't
2094 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2095 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2096 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2097 */
2098 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2099
2100 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2101
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002102 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002103 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002104 }
2105 }
2106
2107 /*
2108 * Check if result will be cacheable with a cookie.
2109 * We'll block the response if security checks have caught
2110 * nasty things such as a cacheable cookie.
2111 */
2112 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2113 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2114 (s->be->options & PR_O_CHK_CACHE)) {
2115 /* we're in presence of a cacheable response containing
2116 * a set-cookie header. We'll block it as requested by
2117 * the 'checkcache' option, and send an alert.
2118 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002119 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2120 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2121 send_log(s->be, LOG_ALERT,
2122 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2123 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002124 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002125 }
2126
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002127 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002128 /*
2129 * Evaluate after-response rules before forwarding the response. rules
2130 * from the backend are evaluated first, then one from the frontend if
2131 * it differs.
2132 */
2133 if (!http_eval_after_res_rules(s))
2134 goto return_int_err;
2135
Christopher Faulete0768eb2018-10-03 16:38:02 +02002136 /* Always enter in the body analyzer */
2137 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2138 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2139
2140 /* if the user wants to log as soon as possible, without counting
2141 * bytes from the server, then this is the right moment. We have
2142 * to temporarily assign bytes_out to log what we currently have.
2143 */
2144 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2145 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002146 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002147 s->do_log(s);
2148 s->logs.bytes_out = 0;
2149 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002150
Christopher Fauletb8a53712019-12-16 11:29:38 +01002151 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002152 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002153 rep->analysers &= ~an_bit;
2154 rep->analyse_exp = TICK_ETERNITY;
2155 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002156
Christopher Fauletb8a53712019-12-16 11:29:38 +01002157 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002158 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002159 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002160 if (sess->listener->counters)
2161 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002162 if (objt_server(s->target))
2163 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002164 goto return_prx_err;
2165
2166 return_int_err:
2167 txn->status = 500;
2168 if (!(s->flags & SF_ERR_MASK))
2169 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002170 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002171 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2172 if (objt_server(s->target))
2173 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002174 if (objt_server(s->target))
2175 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002176 goto return_prx_err;
2177
2178 return_bad_res:
2179 txn->status = 502;
Christopher Fauleta20a6532020-02-05 10:16:41 +01002180 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2181 if (objt_server(s->target)) {
2182 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2183 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2184 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002185 /* fall through */
2186
2187 return_prx_err:
2188 http_reply_and_close(s, txn->status, http_error_message(s));
2189 /* fall through */
2190
2191 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002192 s->logs.t_data = -1; /* was not a valid response */
2193 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002194
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002195 if (!(s->flags & SF_ERR_MASK))
2196 s->flags |= SF_ERR_PRXCOND;
2197 if (!(s->flags & SF_FINST_MASK))
2198 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002199
Christopher Faulete58c0002020-03-02 16:21:01 +01002200 rep->analysers &= AN_RES_FLT_END;
2201 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002202 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002203 DBG_TRACE_DEVEL("leaving on error",
2204 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002205 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002206
2207 return_prx_yield:
2208 channel_dont_close(rep);
2209 DBG_TRACE_DEVEL("waiting for more data",
2210 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2211 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002212}
2213
2214/* This function is an analyser which forwards response body (including chunk
2215 * sizes if any). It is called as soon as we must forward, even if we forward
2216 * zero byte. The only situation where it must not be called is when we're in
2217 * tunnel mode and we want to forward till the close. It's used both to forward
2218 * remaining data and to resync after end of body. It expects the msg_state to
2219 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2220 * read more data, or 1 once we can go on with next request or end the stream.
2221 *
2222 * It is capable of compressing response data both in content-length mode and
2223 * in chunked mode. The state machines follows different flows depending on
2224 * whether content-length and chunked modes are used, since there are no
2225 * trailers in content-length :
2226 *
2227 * chk-mode cl-mode
2228 * ,----- BODY -----.
2229 * / \
2230 * V size > 0 V chk-mode
2231 * .--> SIZE -------------> DATA -------------> CRLF
2232 * | | size == 0 | last byte |
2233 * | v final crlf v inspected |
2234 * | TRAILERS -----------> DONE |
2235 * | |
2236 * `----------------------------------------------'
2237 *
2238 * Compression only happens in the DATA state, and must be flushed in final
2239 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2240 * is performed at once on final states for all bytes parsed, or when leaving
2241 * on missing data.
2242 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002243int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002244{
2245 struct session *sess = s->sess;
2246 struct http_txn *txn = s->txn;
2247 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002248 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002249 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002250
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002251 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002252
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002253 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002254
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002255 if (htx->flags & HTX_FL_PARSING_ERROR)
2256 goto return_bad_res;
2257 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2258 goto return_int_err;
2259
Christopher Faulete0768eb2018-10-03 16:38:02 +02002260 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002261 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002262 /* Output closed while we were sending data. We must abort and
2263 * wake the other side up.
2264 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002265 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002266 http_end_response(s);
2267 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002268 DBG_TRACE_DEVEL("leaving on error",
2269 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002270 return 1;
2271 }
2272
Christopher Faulet9768c262018-10-22 09:34:31 +02002273 if (msg->msg_state == HTTP_MSG_BODY)
2274 msg->msg_state = HTTP_MSG_DATA;
2275
Christopher Faulete0768eb2018-10-03 16:38:02 +02002276 /* in most states, we should abort in case of early close */
2277 channel_auto_close(res);
2278
Christopher Faulete0768eb2018-10-03 16:38:02 +02002279 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002280 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002281 if (res->flags & CF_EOI)
2282 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002283 }
2284 else {
2285 /* We can't process the buffer's contents yet */
2286 res->flags |= CF_WAKE_WRITE;
2287 goto missing_data_or_waiting;
2288 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002289 }
2290
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002291 if (msg->msg_state >= HTTP_MSG_ENDING)
2292 goto ending;
2293
2294 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2295 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2296 msg->msg_state = HTTP_MSG_ENDING;
2297 goto ending;
2298 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002299
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002300 /* Forward input data. We get it by removing all outgoing data not
2301 * forwarded yet from HTX data size. If there are some data filters, we
2302 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002303 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002304 if (HAS_RSP_DATA_FILTERS(s)) {
2305 ret = flt_http_payload(s, msg, htx->data);
2306 if (ret < 0)
2307 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002308 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002309 }
2310 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002311 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002312 if (msg->flags & HTTP_MSGF_XFER_LEN)
2313 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002314 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002315
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002316 if (htx->data != co_data(res))
2317 goto missing_data_or_waiting;
2318
2319 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2320 msg->msg_state = HTTP_MSG_ENDING;
2321 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002322 }
2323
Christopher Faulet9768c262018-10-22 09:34:31 +02002324 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002325 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2326 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002327 */
2328 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2329 goto missing_data_or_waiting;
2330
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002331 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002332
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002333 ending:
2334 /* other states, ENDING...TUNNEL */
2335 if (msg->msg_state >= HTTP_MSG_DONE)
2336 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002337
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002338 if (HAS_RSP_DATA_FILTERS(s)) {
2339 ret = flt_http_end(s, msg);
2340 if (ret <= 0) {
2341 if (!ret)
2342 goto missing_data_or_waiting;
2343 goto return_bad_res;
2344 }
2345 }
2346
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002347 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2348 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2349 msg->msg_state = HTTP_MSG_TUNNEL;
2350 goto ending;
2351 }
2352 else {
2353 msg->msg_state = HTTP_MSG_DONE;
2354 res->to_forward = 0;
2355 }
2356
2357 done:
2358
2359 channel_dont_close(res);
2360
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002361 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002362 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002363 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002364 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2365 if (res->flags & CF_SHUTW) {
2366 /* response errors are most likely due to the
2367 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002368 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002369 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002370 goto return_bad_res;
2371 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002372 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002373 return 1;
2374 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002375 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2376 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002377 return 0;
2378
2379 missing_data_or_waiting:
2380 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002381 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002382
2383 /* stop waiting for data if the input is closed before the end. If the
2384 * client side was already closed, it means that the client has aborted,
2385 * so we don't want to count this as a server abort. Otherwise it's a
2386 * server abort.
2387 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002388 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002389 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002390 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002391 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002392 if (htx_is_empty(htx))
2393 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002394 }
2395
Christopher Faulete0768eb2018-10-03 16:38:02 +02002396 /* When TE: chunked is used, we need to get there again to parse
2397 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002398 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2399 * are filters registered on the stream, we don't want to forward a
2400 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002401 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002402 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002403 channel_dont_close(res);
2404
2405 /* We know that more data are expected, but we couldn't send more that
2406 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2407 * system knows it must not set a PUSH on this first part. Interactive
2408 * modes are already handled by the stream sock layer. We must not do
2409 * this in content-length mode because it could present the MSG_MORE
2410 * flag with the last block of forwarded data, which would cause an
2411 * additional delay to be observed by the receiver.
2412 */
2413 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2414 res->flags |= CF_EXPECT_MORE;
2415
2416 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002417 DBG_TRACE_DEVEL("waiting for more data to forward",
2418 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002419 return 0;
2420
Christopher Faulet93e02d82019-03-08 14:18:50 +01002421 return_srv_abort:
2422 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2423 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002424 if (sess->listener->counters)
2425 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002426 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002427 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002428 if (!(s->flags & SF_ERR_MASK))
2429 s->flags |= SF_ERR_SRVCL;
2430 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002431
Christopher Faulet93e02d82019-03-08 14:18:50 +01002432 return_cli_abort:
2433 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2434 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002435 if (sess->listener->counters)
2436 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002437 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002438 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002439 if (!(s->flags & SF_ERR_MASK))
2440 s->flags |= SF_ERR_CLICL;
2441 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002442
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002443 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002444 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002445 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002446 if (sess->listener->counters)
2447 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002448 if (objt_server(s->target))
2449 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002450 if (!(s->flags & SF_ERR_MASK))
2451 s->flags |= SF_ERR_INTERNAL;
2452 goto return_error;
2453
Christopher Faulet93e02d82019-03-08 14:18:50 +01002454 return_bad_res:
2455 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2456 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002457 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002458 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2459 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002460 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002461 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002462 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002463
Christopher Faulet93e02d82019-03-08 14:18:50 +01002464 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002465 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002466 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002467 res->analysers &= AN_RES_FLT_END;
2468 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 +02002469 if (!(s->flags & SF_FINST_MASK))
2470 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002471 DBG_TRACE_DEVEL("leaving on error",
2472 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002473 return 0;
2474}
2475
Christopher Fauletf2824e62018-10-01 12:12:37 +02002476/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002477 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002478 * as too large a request to build a valid response.
2479 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002480int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002481{
Christopher Faulet99daf282018-11-28 22:58:13 +01002482 struct channel *req = &s->req;
2483 struct channel *res = &s->res;
2484 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002485 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002486 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002487 struct ist status, reason, location;
2488 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002489 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002490
2491 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002492 if (!chunk) {
2493 if (!(s->flags & SF_ERR_MASK))
2494 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002495 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002496 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002497
Christopher Faulet99daf282018-11-28 22:58:13 +01002498 /*
2499 * Create the location
2500 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002501 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002502 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002503 case REDIRECT_TYPE_SCHEME: {
2504 struct http_hdr_ctx ctx;
2505 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002506
Christopher Faulet99daf282018-11-28 22:58:13 +01002507 host = ist("");
2508 ctx.blk = NULL;
2509 if (http_find_header(htx, ist("Host"), &ctx, 0))
2510 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002511
Christopher Faulet297fbb42019-05-13 14:41:27 +02002512 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002513 path = http_get_path(htx_sl_req_uri(sl));
2514 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002515 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002516 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2517 int qs = 0;
2518 while (qs < path.len) {
2519 if (*(path.ptr + qs) == '?') {
2520 path.len = qs;
2521 break;
2522 }
2523 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002524 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002525 }
2526 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002527 else
2528 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002529
Christopher Faulet99daf282018-11-28 22:58:13 +01002530 if (rule->rdr_str) { /* this is an old "redirect" rule */
2531 /* add scheme */
2532 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2533 goto fail;
2534 }
2535 else {
2536 /* add scheme with executing log format */
2537 chunk->data += build_logline(s, chunk->area + chunk->data,
2538 chunk->size - chunk->data,
2539 &rule->rdr_fmt);
2540 }
2541 /* add "://" + host + path */
2542 if (!chunk_memcat(chunk, "://", 3) ||
2543 !chunk_memcat(chunk, host.ptr, host.len) ||
2544 !chunk_memcat(chunk, path.ptr, path.len))
2545 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002546
Christopher Faulet99daf282018-11-28 22:58:13 +01002547 /* append a slash at the end of the location if needed and missing */
2548 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2549 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2550 if (chunk->data + 1 >= chunk->size)
2551 goto fail;
2552 chunk->area[chunk->data++] = '/';
2553 }
2554 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002555 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002556
Christopher Faulet99daf282018-11-28 22:58:13 +01002557 case REDIRECT_TYPE_PREFIX: {
2558 struct ist path;
2559
Christopher Faulet297fbb42019-05-13 14:41:27 +02002560 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002561 path = http_get_path(htx_sl_req_uri(sl));
2562 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002563 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002564 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2565 int qs = 0;
2566 while (qs < path.len) {
2567 if (*(path.ptr + qs) == '?') {
2568 path.len = qs;
2569 break;
2570 }
2571 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002572 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002573 }
2574 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002575 else
2576 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002577
Christopher Faulet99daf282018-11-28 22:58:13 +01002578 if (rule->rdr_str) { /* this is an old "redirect" rule */
2579 /* add prefix. Note that if prefix == "/", we don't want to
2580 * add anything, otherwise it makes it hard for the user to
2581 * configure a self-redirection.
2582 */
2583 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2584 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2585 goto fail;
2586 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002587 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002588 else {
2589 /* add prefix with executing log format */
2590 chunk->data += build_logline(s, chunk->area + chunk->data,
2591 chunk->size - chunk->data,
2592 &rule->rdr_fmt);
2593 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002594
Christopher Faulet99daf282018-11-28 22:58:13 +01002595 /* add path */
2596 if (!chunk_memcat(chunk, path.ptr, path.len))
2597 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002598
Christopher Faulet99daf282018-11-28 22:58:13 +01002599 /* append a slash at the end of the location if needed and missing */
2600 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2601 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2602 if (chunk->data + 1 >= chunk->size)
2603 goto fail;
2604 chunk->area[chunk->data++] = '/';
2605 }
2606 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002607 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002608 case REDIRECT_TYPE_LOCATION:
2609 default:
2610 if (rule->rdr_str) { /* this is an old "redirect" rule */
2611 /* add location */
2612 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2613 goto fail;
2614 }
2615 else {
2616 /* add location with executing log format */
2617 chunk->data += build_logline(s, chunk->area + chunk->data,
2618 chunk->size - chunk->data,
2619 &rule->rdr_fmt);
2620 }
2621 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002622 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002623 location = ist2(chunk->area, chunk->data);
2624
2625 /*
2626 * Create the 30x response
2627 */
2628 switch (rule->code) {
2629 case 308:
2630 status = ist("308");
2631 reason = ist("Permanent Redirect");
2632 break;
2633 case 307:
2634 status = ist("307");
2635 reason = ist("Temporary Redirect");
2636 break;
2637 case 303:
2638 status = ist("303");
2639 reason = ist("See Other");
2640 break;
2641 case 301:
2642 status = ist("301");
2643 reason = ist("Moved Permanently");
2644 break;
2645 case 302:
2646 default:
2647 status = ist("302");
2648 reason = ist("Found");
2649 break;
2650 }
2651
Christopher Faulet08e66462019-05-23 16:44:59 +02002652 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2653 close = 1;
2654
Christopher Faulet99daf282018-11-28 22:58:13 +01002655 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002656 /* Trim any possible response */
2657 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002658 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2659 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2660 if (!sl)
2661 goto fail;
2662 sl->info.res.status = rule->code;
2663 s->txn->status = rule->code;
2664
Christopher Faulet08e66462019-05-23 16:44:59 +02002665 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2666 goto fail;
2667
2668 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002669 !htx_add_header(htx, ist("Location"), location))
2670 goto fail;
2671
2672 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2673 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2674 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002675 }
2676
2677 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002678 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2679 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002680 }
2681
Christopher Faulet99daf282018-11-28 22:58:13 +01002682 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2683 goto fail;
2684
Kevin Zhu96b36392020-01-07 09:42:55 +01002685 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002686 if (!http_forward_proxy_resp(s, 1))
2687 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002688
Christopher Faulet60b33a52020-01-28 09:18:10 +01002689 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2690 /* let's log the request time */
2691 s->logs.tv_request = now;
2692 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002693
Christopher Faulet60b33a52020-01-28 09:18:10 +01002694 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2695 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2696 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002697
2698 if (!(s->flags & SF_ERR_MASK))
2699 s->flags |= SF_ERR_LOCAL;
2700 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002701 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002702
Christopher Faulet99daf282018-11-28 22:58:13 +01002703 free_trash_chunk(chunk);
2704 return 1;
2705
2706 fail:
2707 /* If an error occurred, remove the incomplete HTTP response from the
2708 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002709 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002710 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002711 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002712}
2713
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002714/* Replace all headers matching the name <name>. The header value is replaced if
2715 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2716 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2717 * values are evaluated one by one. It returns 0 on success and -1 on error.
2718 */
2719int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2720 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002721{
2722 struct http_hdr_ctx ctx;
2723 struct buffer *output = get_trash_chunk();
2724
Christopher Faulet72333522018-10-24 11:25:02 +02002725 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002726 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002727 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2728 continue;
2729
2730 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2731 if (output->data == -1)
2732 return -1;
2733 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2734 return -1;
2735 }
2736 return 0;
2737}
2738
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002739/* This function executes one of the set-{method,path,query,uri} actions. It
2740 * takes the string from the variable 'replace' with length 'len', then modifies
2741 * the relevant part of the request line accordingly. Then it updates various
2742 * pointers to the next elements which were moved, and the total buffer length.
2743 * It finds the action to be performed in p[2], previously filled by function
2744 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2745 * error, though this can be revisited when this code is finally exploited.
2746 *
2747 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2748 * query string and 3 to replace uri.
2749 *
2750 * In query string case, the mark question '?' must be set at the start of the
2751 * string by the caller, event if the replacement query string is empty.
2752 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002753int http_req_replace_stline(int action, const char *replace, int len,
2754 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002755{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002756 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002757
2758 switch (action) {
2759 case 0: // method
2760 if (!http_replace_req_meth(htx, ist2(replace, len)))
2761 return -1;
2762 break;
2763
2764 case 1: // path
2765 if (!http_replace_req_path(htx, ist2(replace, len)))
2766 return -1;
2767 break;
2768
2769 case 2: // query
2770 if (!http_replace_req_query(htx, ist2(replace, len)))
2771 return -1;
2772 break;
2773
2774 case 3: // uri
2775 if (!http_replace_req_uri(htx, ist2(replace, len)))
2776 return -1;
2777 break;
2778
2779 default:
2780 return -1;
2781 }
2782 return 0;
2783}
2784
2785/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002786 * variable <status> contains the new status code. This function never fails. It
2787 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002788 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002789int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002790{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002791 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002792 char *res;
2793
2794 chunk_reset(&trash);
2795 res = ultoa_o(status, trash.area, trash.size);
2796 trash.data = res - trash.area;
2797
2798 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002799 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002800 const char *str = http_get_reason(status);
2801 reason = ist2(str, strlen(str));
2802 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002803
Christopher Faulete00d06c2019-12-16 17:18:42 +01002804 if (!http_replace_res_status(htx, ist2(trash.area, trash.data)))
2805 return -1;
Christopher Faulet96bff762019-12-17 13:46:18 +01002806 if (!http_replace_res_reason(htx, reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002807 return -1;
2808 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002809}
2810
Christopher Faulet3e964192018-10-24 11:39:23 +02002811/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2812 * transaction <txn>. Returns the verdict of the first rule that prevents
2813 * further processing of the request (auth, deny, ...), and defaults to
2814 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2815 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2816 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2817 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2818 * status.
2819 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002820static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002821 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002822{
2823 struct session *sess = strm_sess(s);
2824 struct http_txn *txn = s->txn;
2825 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002826 struct act_rule *rule;
2827 struct http_hdr_ctx ctx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002828 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002829 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002830
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002831 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002832
2833 /* If "the current_rule_list" match the executed rule list, we are in
2834 * resume condition. If a resume is needed it is always in the action
2835 * and never in the ACL or converters. In this case, we initialise the
2836 * current rule, and go to the action execution point.
2837 */
2838 if (s->current_rule) {
2839 rule = s->current_rule;
2840 s->current_rule = NULL;
2841 if (s->current_rule_list == rules)
2842 goto resume_execution;
2843 }
2844 s->current_rule_list = rules;
2845
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002846 /* start the ruleset evaluation in strict mode */
2847 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002848
Christopher Faulet3e964192018-10-24 11:39:23 +02002849 list_for_each_entry(rule, rules, list) {
2850 /* check optional condition */
2851 if (rule->cond) {
2852 int ret;
2853
2854 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2855 ret = acl_pass(ret);
2856
2857 if (rule->cond->pol == ACL_COND_UNLESS)
2858 ret = !ret;
2859
2860 if (!ret) /* condition not matched */
2861 continue;
2862 }
2863
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002864 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002865 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002866 /* Always call the action function if defined */
2867 if (rule->action_ptr) {
2868 if ((s->req.flags & CF_READ_ERROR) ||
2869 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2870 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002871 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002872
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002873 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002874 case ACT_RET_CONT:
2875 break;
2876 case ACT_RET_STOP:
2877 rule_ret = HTTP_RULE_RES_STOP;
2878 goto end;
2879 case ACT_RET_YIELD:
2880 s->current_rule = rule;
2881 rule_ret = HTTP_RULE_RES_YIELD;
2882 goto end;
2883 case ACT_RET_ERR:
2884 rule_ret = HTTP_RULE_RES_ERROR;
2885 goto end;
2886 case ACT_RET_DONE:
2887 rule_ret = HTTP_RULE_RES_DONE;
2888 goto end;
2889 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002890 if (txn->status == -1)
2891 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002892 rule_ret = HTTP_RULE_RES_DENY;
2893 goto end;
2894 case ACT_RET_ABRT:
2895 rule_ret = HTTP_RULE_RES_ABRT;
2896 goto end;
2897 case ACT_RET_INV:
2898 rule_ret = HTTP_RULE_RES_BADREQ;
2899 goto end;
2900 }
2901 continue; /* eval the next rule */
2902 }
2903
2904 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002905 switch (rule->action) {
2906 case ACT_ACTION_ALLOW:
2907 rule_ret = HTTP_RULE_RES_STOP;
2908 goto end;
2909
2910 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002911 txn->status = rule->arg.http_reply->status;
2912 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002913 rule_ret = HTTP_RULE_RES_DENY;
2914 goto end;
2915
2916 case ACT_HTTP_REQ_TARPIT:
2917 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002918 txn->status = rule->arg.http_reply->status;
2919 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002920 rule_ret = HTTP_RULE_RES_DENY;
2921 goto end;
2922
Christopher Faulet3e964192018-10-24 11:39:23 +02002923 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002924 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002925 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002926 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002927 goto end;
2928
2929 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002930 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002931 break;
2932
2933 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002934 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002935 break;
2936
2937 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002938 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002939 break;
2940
2941 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002942 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002943 break;
2944
Christopher Faulet3e964192018-10-24 11:39:23 +02002945 case ACT_HTTP_DEL_HDR:
2946 /* remove all occurrences of the header */
2947 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01002948 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02002949 http_remove_header(htx, &ctx);
2950 break;
2951
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002952 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002953 default:
2954 break;
2955 }
2956 }
2957
2958 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002959 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002960 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002961 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002962
Christopher Faulet3e964192018-10-24 11:39:23 +02002963 /* we reached the end of the rules, nothing to report */
2964 return rule_ret;
2965}
2966
2967/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2968 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2969 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2970 * is returned, the process can continue the evaluation of next rule list. If
2971 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2972 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002973 * must be returned. If *YIELD is returned, the caller must call again the
2974 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002975 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002976static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2977 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002978{
2979 struct session *sess = strm_sess(s);
2980 struct http_txn *txn = s->txn;
2981 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002982 struct act_rule *rule;
2983 struct http_hdr_ctx ctx;
2984 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002985 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002986
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002987 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002988
2989 /* If "the current_rule_list" match the executed rule list, we are in
2990 * resume condition. If a resume is needed it is always in the action
2991 * and never in the ACL or converters. In this case, we initialise the
2992 * current rule, and go to the action execution point.
2993 */
2994 if (s->current_rule) {
2995 rule = s->current_rule;
2996 s->current_rule = NULL;
2997 if (s->current_rule_list == rules)
2998 goto resume_execution;
2999 }
3000 s->current_rule_list = rules;
3001
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003002 /* start the ruleset evaluation in strict mode */
3003 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003004
Christopher Faulet3e964192018-10-24 11:39:23 +02003005 list_for_each_entry(rule, rules, list) {
3006 /* check optional condition */
3007 if (rule->cond) {
3008 int ret;
3009
3010 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3011 ret = acl_pass(ret);
3012
3013 if (rule->cond->pol == ACL_COND_UNLESS)
3014 ret = !ret;
3015
3016 if (!ret) /* condition not matched */
3017 continue;
3018 }
3019
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003020 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02003021resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003022
3023 /* Always call the action function if defined */
3024 if (rule->action_ptr) {
3025 if ((s->req.flags & CF_READ_ERROR) ||
3026 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3027 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003028 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003029
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003030 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003031 case ACT_RET_CONT:
3032 break;
3033 case ACT_RET_STOP:
3034 rule_ret = HTTP_RULE_RES_STOP;
3035 goto end;
3036 case ACT_RET_YIELD:
3037 s->current_rule = rule;
3038 rule_ret = HTTP_RULE_RES_YIELD;
3039 goto end;
3040 case ACT_RET_ERR:
3041 rule_ret = HTTP_RULE_RES_ERROR;
3042 goto end;
3043 case ACT_RET_DONE:
3044 rule_ret = HTTP_RULE_RES_DONE;
3045 goto end;
3046 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01003047 if (txn->status == -1)
3048 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003049 rule_ret = HTTP_RULE_RES_DENY;
3050 goto end;
3051 case ACT_RET_ABRT:
3052 rule_ret = HTTP_RULE_RES_ABRT;
3053 goto end;
3054 case ACT_RET_INV:
3055 rule_ret = HTTP_RULE_RES_BADREQ;
3056 goto end;
3057 }
3058 continue; /* eval the next rule */
3059 }
3060
3061 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003062 switch (rule->action) {
3063 case ACT_ACTION_ALLOW:
3064 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3065 goto end;
3066
3067 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02003068 txn->status = rule->arg.http_reply->status;
3069 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003070 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003071 goto end;
3072
3073 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003074 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003075 break;
3076
3077 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003078 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003079 break;
3080
3081 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003082 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003083 break;
3084
3085 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003086 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003087 break;
3088
Christopher Faulet3e964192018-10-24 11:39:23 +02003089 case ACT_HTTP_DEL_HDR:
3090 /* remove all occurrences of the header */
3091 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01003092 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02003093 http_remove_header(htx, &ctx);
3094 break;
3095
Christopher Faulet3e964192018-10-24 11:39:23 +02003096 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003097 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003098 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003099 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003100 goto end;
3101
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003102 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003103 default:
3104 break;
3105 }
3106 }
3107
3108 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003109 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003110 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003111 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003112
Christopher Faulet3e964192018-10-24 11:39:23 +02003113 /* we reached the end of the rules, nothing to report */
3114 return rule_ret;
3115}
3116
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003117/* Executes backend and frontend http-after-response rules for the stream <s>,
3118 * in that order. it return 1 on success and 0 on error. It is the caller
3119 * responsibility to catch error or ignore it. If it catches it, this function
3120 * may be called a second time, for the internal error.
3121 */
3122int http_eval_after_res_rules(struct stream *s)
3123{
3124 struct session *sess = s->sess;
3125 enum rule_result ret = HTTP_RULE_RES_CONT;
3126
Christopher Faulet507479b2020-05-15 12:29:46 +02003127 /* Eval after-response ruleset only if the reply is not const */
3128 if (s->txn->flags & TX_CONST_REPLY)
3129 goto end;
3130
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003131 /* prune the request variables if not already done and swap to the response variables. */
3132 if (s->vars_reqres.scope != SCOPE_RES) {
3133 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3134 vars_prune(&s->vars_reqres, s->sess, s);
3135 vars_init(&s->vars_reqres, SCOPE_RES);
3136 }
3137
3138 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3139 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3140 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3141
Christopher Faulet507479b2020-05-15 12:29:46 +02003142 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003143 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3144 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3145}
3146
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003147/*
3148 * Manage client-side cookie. It can impact performance by about 2% so it is
3149 * desirable to call it only when needed. This code is quite complex because
3150 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3151 * highly recommended not to touch this part without a good reason !
3152 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003153static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003154{
3155 struct session *sess = s->sess;
3156 struct http_txn *txn = s->txn;
3157 struct htx *htx;
3158 struct http_hdr_ctx ctx;
3159 char *hdr_beg, *hdr_end, *del_from;
3160 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3161 int preserve_hdr;
3162
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003163 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003164 ctx.blk = NULL;
3165 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003166 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003167 del_from = NULL; /* nothing to be deleted */
3168 preserve_hdr = 0; /* assume we may kill the whole header */
3169
3170 /* Now look for cookies. Conforming to RFC2109, we have to support
3171 * attributes whose name begin with a '$', and associate them with
3172 * the right cookie, if we want to delete this cookie.
3173 * So there are 3 cases for each cookie read :
3174 * 1) it's a special attribute, beginning with a '$' : ignore it.
3175 * 2) it's a server id cookie that we *MAY* want to delete : save
3176 * some pointers on it (last semi-colon, beginning of cookie...)
3177 * 3) it's an application cookie : we *MAY* have to delete a previous
3178 * "special" cookie.
3179 * At the end of loop, if a "special" cookie remains, we may have to
3180 * remove it. If no application cookie persists in the header, we
3181 * *MUST* delete it.
3182 *
3183 * Note: RFC2965 is unclear about the processing of spaces around
3184 * the equal sign in the ATTR=VALUE form. A careful inspection of
3185 * the RFC explicitly allows spaces before it, and not within the
3186 * tokens (attrs or values). An inspection of RFC2109 allows that
3187 * too but section 10.1.3 lets one think that spaces may be allowed
3188 * after the equal sign too, resulting in some (rare) buggy
3189 * implementations trying to do that. So let's do what servers do.
3190 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3191 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003192 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003193 * causes parsing to become ambiguous. Browsers also allow spaces
3194 * within values even without quotes.
3195 *
3196 * We have to keep multiple pointers in order to support cookie
3197 * removal at the beginning, middle or end of header without
3198 * corrupting the header. All of these headers are valid :
3199 *
3200 * hdr_beg hdr_end
3201 * | |
3202 * v |
3203 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3204 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3205 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3206 * | | | | | | |
3207 * | | | | | | |
3208 * | | | | | | +--> next
3209 * | | | | | +----> val_end
3210 * | | | | +-----------> val_beg
3211 * | | | +--------------> equal
3212 * | | +----------------> att_end
3213 * | +---------------------> att_beg
3214 * +--------------------------> prev
3215 *
3216 */
3217 hdr_beg = ctx.value.ptr;
3218 hdr_end = hdr_beg + ctx.value.len;
3219 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3220 /* Iterate through all cookies on this line */
3221
3222 /* find att_beg */
3223 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003224 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003225 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003226 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003227
3228 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3229 att_beg++;
3230
3231 /* find att_end : this is the first character after the last non
3232 * space before the equal. It may be equal to hdr_end.
3233 */
3234 equal = att_end = att_beg;
3235 while (equal < hdr_end) {
3236 if (*equal == '=' || *equal == ',' || *equal == ';')
3237 break;
3238 if (HTTP_IS_SPHT(*equal++))
3239 continue;
3240 att_end = equal;
3241 }
3242
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003243 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003244 * is between <att_beg> and <equal>, both may be identical.
3245 */
3246 /* look for end of cookie if there is an equal sign */
3247 if (equal < hdr_end && *equal == '=') {
3248 /* look for the beginning of the value */
3249 val_beg = equal + 1;
3250 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3251 val_beg++;
3252
3253 /* find the end of the value, respecting quotes */
3254 next = http_find_cookie_value_end(val_beg, hdr_end);
3255
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003256 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003257 val_end = next;
3258 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3259 val_end--;
3260 }
3261 else
3262 val_beg = val_end = next = equal;
3263
3264 /* We have nothing to do with attributes beginning with
3265 * '$'. However, they will automatically be removed if a
3266 * header before them is removed, since they're supposed
3267 * to be linked together.
3268 */
3269 if (*att_beg == '$')
3270 continue;
3271
3272 /* Ignore cookies with no equal sign */
3273 if (equal == next) {
3274 /* This is not our cookie, so we must preserve it. But if we already
3275 * scheduled another cookie for removal, we cannot remove the
3276 * complete header, but we can remove the previous block itself.
3277 */
3278 preserve_hdr = 1;
3279 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003280 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003281 val_end += delta;
3282 next += delta;
3283 hdr_end += delta;
3284 prev = del_from;
3285 del_from = NULL;
3286 }
3287 continue;
3288 }
3289
3290 /* if there are spaces around the equal sign, we need to
3291 * strip them otherwise we'll get trouble for cookie captures,
3292 * or even for rewrites. Since this happens extremely rarely,
3293 * it does not hurt performance.
3294 */
3295 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3296 int stripped_before = 0;
3297 int stripped_after = 0;
3298
3299 if (att_end != equal) {
3300 memmove(att_end, equal, hdr_end - equal);
3301 stripped_before = (att_end - equal);
3302 equal += stripped_before;
3303 val_beg += stripped_before;
3304 }
3305
3306 if (val_beg > equal + 1) {
3307 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3308 stripped_after = (equal + 1) - val_beg;
3309 val_beg += stripped_after;
3310 stripped_before += stripped_after;
3311 }
3312
3313 val_end += stripped_before;
3314 next += stripped_before;
3315 hdr_end += stripped_before;
3316 }
3317 /* now everything is as on the diagram above */
3318
3319 /* First, let's see if we want to capture this cookie. We check
3320 * that we don't already have a client side cookie, because we
3321 * can only capture one. Also as an optimisation, we ignore
3322 * cookies shorter than the declared name.
3323 */
3324 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3325 (val_end - att_beg >= sess->fe->capture_namelen) &&
3326 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3327 int log_len = val_end - att_beg;
3328
3329 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3330 ha_alert("HTTP logging : out of memory.\n");
3331 } else {
3332 if (log_len > sess->fe->capture_len)
3333 log_len = sess->fe->capture_len;
3334 memcpy(txn->cli_cookie, att_beg, log_len);
3335 txn->cli_cookie[log_len] = 0;
3336 }
3337 }
3338
3339 /* Persistence cookies in passive, rewrite or insert mode have the
3340 * following form :
3341 *
3342 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3343 *
3344 * For cookies in prefix mode, the form is :
3345 *
3346 * Cookie: NAME=SRV~VALUE
3347 */
3348 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3349 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3350 struct server *srv = s->be->srv;
3351 char *delim;
3352
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003353 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003354 * have the server ID between val_beg and delim, and the original cookie between
3355 * delim+1 and val_end. Otherwise, delim==val_end :
3356 *
3357 * hdr_beg
3358 * |
3359 * v
3360 * NAME=SRV; # in all but prefix modes
3361 * NAME=SRV~OPAQUE ; # in prefix mode
3362 * || || | |+-> next
3363 * || || | +--> val_end
3364 * || || +---------> delim
3365 * || |+------------> val_beg
3366 * || +-------------> att_end = equal
3367 * |+-----------------> att_beg
3368 * +------------------> prev
3369 *
3370 */
3371 if (s->be->ck_opts & PR_CK_PFX) {
3372 for (delim = val_beg; delim < val_end; delim++)
3373 if (*delim == COOKIE_DELIM)
3374 break;
3375 }
3376 else {
3377 char *vbar1;
3378 delim = val_end;
3379 /* Now check if the cookie contains a date field, which would
3380 * appear after a vertical bar ('|') just after the server name
3381 * and before the delimiter.
3382 */
3383 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3384 if (vbar1) {
3385 /* OK, so left of the bar is the server's cookie and
3386 * right is the last seen date. It is a base64 encoded
3387 * 30-bit value representing the UNIX date since the
3388 * epoch in 4-second quantities.
3389 */
3390 int val;
3391 delim = vbar1++;
3392 if (val_end - vbar1 >= 5) {
3393 val = b64tos30(vbar1);
3394 if (val > 0)
3395 txn->cookie_last_date = val << 2;
3396 }
3397 /* look for a second vertical bar */
3398 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3399 if (vbar1 && (val_end - vbar1 > 5)) {
3400 val = b64tos30(vbar1 + 1);
3401 if (val > 0)
3402 txn->cookie_first_date = val << 2;
3403 }
3404 }
3405 }
3406
3407 /* if the cookie has an expiration date and the proxy wants to check
3408 * it, then we do that now. We first check if the cookie is too old,
3409 * then only if it has expired. We detect strict overflow because the
3410 * time resolution here is not great (4 seconds). Cookies with dates
3411 * in the future are ignored if their offset is beyond one day. This
3412 * allows an admin to fix timezone issues without expiring everyone
3413 * and at the same time avoids keeping unwanted side effects for too
3414 * long.
3415 */
3416 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3417 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3418 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3419 txn->flags &= ~TX_CK_MASK;
3420 txn->flags |= TX_CK_OLD;
3421 delim = val_beg; // let's pretend we have not found the cookie
3422 txn->cookie_first_date = 0;
3423 txn->cookie_last_date = 0;
3424 }
3425 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3426 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3427 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3428 txn->flags &= ~TX_CK_MASK;
3429 txn->flags |= TX_CK_EXPIRED;
3430 delim = val_beg; // let's pretend we have not found the cookie
3431 txn->cookie_first_date = 0;
3432 txn->cookie_last_date = 0;
3433 }
3434
3435 /* Here, we'll look for the first running server which supports the cookie.
3436 * This allows to share a same cookie between several servers, for example
3437 * to dedicate backup servers to specific servers only.
3438 * However, to prevent clients from sticking to cookie-less backup server
3439 * when they have incidentely learned an empty cookie, we simply ignore
3440 * empty cookies and mark them as invalid.
3441 * The same behaviour is applied when persistence must be ignored.
3442 */
3443 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3444 srv = NULL;
3445
3446 while (srv) {
3447 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3448 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3449 if ((srv->cur_state != SRV_ST_STOPPED) ||
3450 (s->be->options & PR_O_PERSIST) ||
3451 (s->flags & SF_FORCE_PRST)) {
3452 /* we found the server and we can use it */
3453 txn->flags &= ~TX_CK_MASK;
3454 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3455 s->flags |= SF_DIRECT | SF_ASSIGNED;
3456 s->target = &srv->obj_type;
3457 break;
3458 } else {
3459 /* we found a server, but it's down,
3460 * mark it as such and go on in case
3461 * another one is available.
3462 */
3463 txn->flags &= ~TX_CK_MASK;
3464 txn->flags |= TX_CK_DOWN;
3465 }
3466 }
3467 srv = srv->next;
3468 }
3469
3470 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3471 /* no server matched this cookie or we deliberately skipped it */
3472 txn->flags &= ~TX_CK_MASK;
3473 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3474 txn->flags |= TX_CK_UNUSED;
3475 else
3476 txn->flags |= TX_CK_INVALID;
3477 }
3478
3479 /* depending on the cookie mode, we may have to either :
3480 * - delete the complete cookie if we're in insert+indirect mode, so that
3481 * the server never sees it ;
3482 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003483 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003484 * if we're in cookie prefix mode
3485 */
3486 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3487 int delta; /* negative */
3488
3489 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3490 delta = val_beg - (delim + 1);
3491 val_end += delta;
3492 next += delta;
3493 hdr_end += delta;
3494 del_from = NULL;
3495 preserve_hdr = 1; /* we want to keep this cookie */
3496 }
3497 else if (del_from == NULL &&
3498 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3499 del_from = prev;
3500 }
3501 }
3502 else {
3503 /* This is not our cookie, so we must preserve it. But if we already
3504 * scheduled another cookie for removal, we cannot remove the
3505 * complete header, but we can remove the previous block itself.
3506 */
3507 preserve_hdr = 1;
3508
3509 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003510 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003511 if (att_beg >= del_from)
3512 att_beg += delta;
3513 if (att_end >= del_from)
3514 att_end += delta;
3515 val_beg += delta;
3516 val_end += delta;
3517 next += delta;
3518 hdr_end += delta;
3519 prev = del_from;
3520 del_from = NULL;
3521 }
3522 }
3523
3524 /* continue with next cookie on this header line */
3525 att_beg = next;
3526 } /* for each cookie */
3527
3528
3529 /* There are no more cookies on this line.
3530 * We may still have one (or several) marked for deletion at the
3531 * end of the line. We must do this now in two ways :
3532 * - if some cookies must be preserved, we only delete from the
3533 * mark to the end of line ;
3534 * - if nothing needs to be preserved, simply delete the whole header
3535 */
3536 if (del_from) {
3537 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3538 }
3539 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003540 if (hdr_beg != hdr_end)
3541 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003542 else
3543 http_remove_header(htx, &ctx);
3544 }
3545 } /* for each "Cookie header */
3546}
3547
3548/*
3549 * Manage server-side cookies. It can impact performance by about 2% so it is
3550 * desirable to call it only when needed. This function is also used when we
3551 * just need to know if there is a cookie (eg: for check-cache).
3552 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003553static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003554{
3555 struct session *sess = s->sess;
3556 struct http_txn *txn = s->txn;
3557 struct htx *htx;
3558 struct http_hdr_ctx ctx;
3559 struct server *srv;
3560 char *hdr_beg, *hdr_end;
3561 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003562 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003563
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003564 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003565
3566 ctx.blk = NULL;
3567 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003568 int is_first = 1;
3569
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003570 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3571 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3572 break;
3573 is_cookie2 = 1;
3574 }
3575
3576 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3577 * <prev> points to the colon.
3578 */
3579 txn->flags |= TX_SCK_PRESENT;
3580
3581 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3582 * check-cache is enabled) and we are not interested in checking
3583 * them. Warning, the cookie capture is declared in the frontend.
3584 */
3585 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3586 break;
3587
3588 /* OK so now we know we have to process this response cookie.
3589 * The format of the Set-Cookie header is slightly different
3590 * from the format of the Cookie header in that it does not
3591 * support the comma as a cookie delimiter (thus the header
3592 * cannot be folded) because the Expires attribute described in
3593 * the original Netscape's spec may contain an unquoted date
3594 * with a comma inside. We have to live with this because
3595 * many browsers don't support Max-Age and some browsers don't
3596 * support quoted strings. However the Set-Cookie2 header is
3597 * clean.
3598 *
3599 * We have to keep multiple pointers in order to support cookie
3600 * removal at the beginning, middle or end of header without
3601 * corrupting the header (in case of set-cookie2). A special
3602 * pointer, <scav> points to the beginning of the set-cookie-av
3603 * fields after the first semi-colon. The <next> pointer points
3604 * either to the end of line (set-cookie) or next unquoted comma
3605 * (set-cookie2). All of these headers are valid :
3606 *
3607 * hdr_beg hdr_end
3608 * | |
3609 * v |
3610 * NAME1 = VALUE 1 ; Secure; Path="/" |
3611 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3612 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3613 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3614 * | | | | | | | |
3615 * | | | | | | | +-> next
3616 * | | | | | | +------------> scav
3617 * | | | | | +--------------> val_end
3618 * | | | | +--------------------> val_beg
3619 * | | | +----------------------> equal
3620 * | | +------------------------> att_end
3621 * | +----------------------------> att_beg
3622 * +------------------------------> prev
3623 * -------------------------------> hdr_beg
3624 */
3625 hdr_beg = ctx.value.ptr;
3626 hdr_end = hdr_beg + ctx.value.len;
3627 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3628
3629 /* Iterate through all cookies on this line */
3630
3631 /* find att_beg */
3632 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003633 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003634 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003635 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003636
3637 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3638 att_beg++;
3639
3640 /* find att_end : this is the first character after the last non
3641 * space before the equal. It may be equal to hdr_end.
3642 */
3643 equal = att_end = att_beg;
3644
3645 while (equal < hdr_end) {
3646 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3647 break;
3648 if (HTTP_IS_SPHT(*equal++))
3649 continue;
3650 att_end = equal;
3651 }
3652
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003653 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003654 * is between <att_beg> and <equal>, both may be identical.
3655 */
3656
3657 /* look for end of cookie if there is an equal sign */
3658 if (equal < hdr_end && *equal == '=') {
3659 /* look for the beginning of the value */
3660 val_beg = equal + 1;
3661 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3662 val_beg++;
3663
3664 /* find the end of the value, respecting quotes */
3665 next = http_find_cookie_value_end(val_beg, hdr_end);
3666
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003667 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003668 val_end = next;
3669 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3670 val_end--;
3671 }
3672 else {
3673 /* <equal> points to next comma, semi-colon or EOL */
3674 val_beg = val_end = next = equal;
3675 }
3676
3677 if (next < hdr_end) {
3678 /* Set-Cookie2 supports multiple cookies, and <next> points to
3679 * a colon or semi-colon before the end. So skip all attr-value
3680 * pairs and look for the next comma. For Set-Cookie, since
3681 * commas are permitted in values, skip to the end.
3682 */
3683 if (is_cookie2)
3684 next = http_find_hdr_value_end(next, hdr_end);
3685 else
3686 next = hdr_end;
3687 }
3688
3689 /* Now everything is as on the diagram above */
3690
3691 /* Ignore cookies with no equal sign */
3692 if (equal == val_end)
3693 continue;
3694
3695 /* If there are spaces around the equal sign, we need to
3696 * strip them otherwise we'll get trouble for cookie captures,
3697 * or even for rewrites. Since this happens extremely rarely,
3698 * it does not hurt performance.
3699 */
3700 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3701 int stripped_before = 0;
3702 int stripped_after = 0;
3703
3704 if (att_end != equal) {
3705 memmove(att_end, equal, hdr_end - equal);
3706 stripped_before = (att_end - equal);
3707 equal += stripped_before;
3708 val_beg += stripped_before;
3709 }
3710
3711 if (val_beg > equal + 1) {
3712 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3713 stripped_after = (equal + 1) - val_beg;
3714 val_beg += stripped_after;
3715 stripped_before += stripped_after;
3716 }
3717
3718 val_end += stripped_before;
3719 next += stripped_before;
3720 hdr_end += stripped_before;
3721
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003722 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003723 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003724 }
3725
3726 /* First, let's see if we want to capture this cookie. We check
3727 * that we don't already have a server side cookie, because we
3728 * can only capture one. Also as an optimisation, we ignore
3729 * cookies shorter than the declared name.
3730 */
3731 if (sess->fe->capture_name != NULL &&
3732 txn->srv_cookie == NULL &&
3733 (val_end - att_beg >= sess->fe->capture_namelen) &&
3734 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3735 int log_len = val_end - att_beg;
3736 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3737 ha_alert("HTTP logging : out of memory.\n");
3738 }
3739 else {
3740 if (log_len > sess->fe->capture_len)
3741 log_len = sess->fe->capture_len;
3742 memcpy(txn->srv_cookie, att_beg, log_len);
3743 txn->srv_cookie[log_len] = 0;
3744 }
3745 }
3746
3747 srv = objt_server(s->target);
3748 /* now check if we need to process it for persistence */
3749 if (!(s->flags & SF_IGNORE_PRST) &&
3750 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3751 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3752 /* assume passive cookie by default */
3753 txn->flags &= ~TX_SCK_MASK;
3754 txn->flags |= TX_SCK_FOUND;
3755
3756 /* If the cookie is in insert mode on a known server, we'll delete
3757 * this occurrence because we'll insert another one later.
3758 * We'll delete it too if the "indirect" option is set and we're in
3759 * a direct access.
3760 */
3761 if (s->be->ck_opts & PR_CK_PSV) {
3762 /* The "preserve" flag was set, we don't want to touch the
3763 * server's cookie.
3764 */
3765 }
3766 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3767 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3768 /* this cookie must be deleted */
3769 if (prev == hdr_beg && next == hdr_end) {
3770 /* whole header */
3771 http_remove_header(htx, &ctx);
3772 /* note: while both invalid now, <next> and <hdr_end>
3773 * are still equal, so the for() will stop as expected.
3774 */
3775 } else {
3776 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003777 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003778 next = prev;
3779 hdr_end += delta;
3780 }
3781 txn->flags &= ~TX_SCK_MASK;
3782 txn->flags |= TX_SCK_DELETED;
3783 /* and go on with next cookie */
3784 }
3785 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3786 /* replace bytes val_beg->val_end with the cookie name associated
3787 * with this server since we know it.
3788 */
3789 int sliding, delta;
3790
3791 ctx.value = ist2(val_beg, val_end - val_beg);
3792 ctx.lws_before = ctx.lws_after = 0;
3793 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3794 delta = srv->cklen - (val_end - val_beg);
3795 sliding = (ctx.value.ptr - val_beg);
3796 hdr_beg += sliding;
3797 val_beg += sliding;
3798 next += sliding + delta;
3799 hdr_end += sliding + delta;
3800
3801 txn->flags &= ~TX_SCK_MASK;
3802 txn->flags |= TX_SCK_REPLACED;
3803 }
3804 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3805 /* insert the cookie name associated with this server
3806 * before existing cookie, and insert a delimiter between them..
3807 */
3808 int sliding, delta;
3809 ctx.value = ist2(val_beg, 0);
3810 ctx.lws_before = ctx.lws_after = 0;
3811 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3812 delta = srv->cklen + 1;
3813 sliding = (ctx.value.ptr - val_beg);
3814 hdr_beg += sliding;
3815 val_beg += sliding;
3816 next += sliding + delta;
3817 hdr_end += sliding + delta;
3818
3819 val_beg[srv->cklen] = COOKIE_DELIM;
3820 txn->flags &= ~TX_SCK_MASK;
3821 txn->flags |= TX_SCK_REPLACED;
3822 }
3823 }
3824 /* that's done for this cookie, check the next one on the same
3825 * line when next != hdr_end (only if is_cookie2).
3826 */
3827 }
3828 }
3829}
3830
Christopher Faulet25a02f62018-10-24 12:00:25 +02003831/*
3832 * Parses the Cache-Control and Pragma request header fields to determine if
3833 * the request may be served from the cache and/or if it is cacheable. Updates
3834 * s->txn->flags.
3835 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003836void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003837{
3838 struct http_txn *txn = s->txn;
3839 struct htx *htx;
3840 int32_t pos;
3841 int pragma_found, cc_found, i;
3842
3843 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3844 return; /* nothing more to do here */
3845
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003846 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003847 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02003848 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003849 struct htx_blk *blk = htx_get_blk(htx, pos);
3850 enum htx_blk_type type = htx_get_blk_type(blk);
3851 struct ist n, v;
3852
3853 if (type == HTX_BLK_EOH)
3854 break;
3855 if (type != HTX_BLK_HDR)
3856 continue;
3857
3858 n = htx_get_blk_name(htx, blk);
3859 v = htx_get_blk_value(htx, blk);
3860
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003861 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003862 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3863 pragma_found = 1;
3864 continue;
3865 }
3866 }
3867
3868 /* Don't use the cache and don't try to store if we found the
3869 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003870 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003871 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3872 txn->flags |= TX_CACHE_IGNORE;
3873 continue;
3874 }
3875
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003876 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02003877 continue;
3878
3879 /* OK, right now we know we have a cache-control header */
3880 cc_found = 1;
3881 if (!v.len) /* no info */
3882 continue;
3883
3884 i = 0;
3885 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
3886 !isspace((unsigned char)*(v.ptr+i)))
3887 i++;
3888
3889 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
3890 * values after max-age, max-stale nor min-fresh, we simply don't
3891 * use the cache when they're specified.
3892 */
3893 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
3894 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
3895 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
3896 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
3897 txn->flags |= TX_CACHE_IGNORE;
3898 continue;
3899 }
3900
3901 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
3902 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3903 continue;
3904 }
3905 }
3906
3907 /* RFC7234#5.4:
3908 * When the Cache-Control header field is also present and
3909 * understood in a request, Pragma is ignored.
3910 * When the Cache-Control header field is not present in a
3911 * request, caches MUST consider the no-cache request
3912 * pragma-directive as having the same effect as if
3913 * "Cache-Control: no-cache" were present.
3914 */
3915 if (!cc_found && pragma_found)
3916 txn->flags |= TX_CACHE_IGNORE;
3917}
3918
3919/*
3920 * Check if response is cacheable or not. Updates s->txn->flags.
3921 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003922void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003923{
3924 struct http_txn *txn = s->txn;
3925 struct htx *htx;
3926 int32_t pos;
3927 int i;
3928
3929 if (txn->status < 200) {
3930 /* do not try to cache interim responses! */
3931 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3932 return;
3933 }
3934
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003935 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02003936 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003937 struct htx_blk *blk = htx_get_blk(htx, pos);
3938 enum htx_blk_type type = htx_get_blk_type(blk);
3939 struct ist n, v;
3940
3941 if (type == HTX_BLK_EOH)
3942 break;
3943 if (type != HTX_BLK_HDR)
3944 continue;
3945
3946 n = htx_get_blk_name(htx, blk);
3947 v = htx_get_blk_value(htx, blk);
3948
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003949 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003950 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3951 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3952 return;
3953 }
3954 }
3955
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003956 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02003957 continue;
3958
3959 /* OK, right now we know we have a cache-control header */
3960 if (!v.len) /* no info */
3961 continue;
3962
3963 i = 0;
3964 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
3965 !isspace((unsigned char)*(v.ptr+i)))
3966 i++;
3967
3968 /* we have a complete value between v.ptr and (v.ptr+i) */
3969 if (i < v.len && *(v.ptr + i) == '=') {
3970 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
3971 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
3972 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3973 continue;
3974 }
3975
3976 /* we have something of the form no-cache="set-cookie" */
3977 if ((v.len >= 21) &&
3978 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
3979 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
3980 txn->flags &= ~TX_CACHE_COOK;
3981 continue;
3982 }
3983
3984 /* OK, so we know that either p2 points to the end of string or to a comma */
3985 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
3986 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
3987 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
3988 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3989 return;
3990 }
3991
3992 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
3993 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
3994 continue;
3995 }
3996 }
3997}
3998
Christopher Faulet377c5a52018-10-24 21:21:30 +02003999/*
4000 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4001 * for the current backend.
4002 *
4003 * It is assumed that the request is either a HEAD, GET, or POST and that the
4004 * uri_auth field is valid.
4005 *
4006 * Returns 1 if stats should be provided, otherwise 0.
4007 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004008static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004009{
4010 struct uri_auth *uri_auth = backend->uri_auth;
4011 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004012 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004013 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004014
4015 if (!uri_auth)
4016 return 0;
4017
4018 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4019 return 0;
4020
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004021 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004022 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004023 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01004024 if (*uri_auth->uri_prefix == '/')
4025 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004026
4027 /* check URI size */
4028 if (uri_auth->uri_len > uri.len)
4029 return 0;
4030
4031 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4032 return 0;
4033
4034 return 1;
4035}
4036
4037/* This function prepares an applet to handle the stats. It can deal with the
4038 * "100-continue" expectation, check that admin rules are met for POST requests,
4039 * and program a response message if something was unexpected. It cannot fail
4040 * and always relies on the stats applet to complete the job. It does not touch
4041 * analysers nor counters, which are left to the caller. It does not touch
4042 * s->target which is supposed to already point to the stats applet. The caller
4043 * is expected to have already assigned an appctx to the stream.
4044 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004045static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004046{
4047 struct stats_admin_rule *stats_admin_rule;
4048 struct stream_interface *si = &s->si[1];
4049 struct session *sess = s->sess;
4050 struct http_txn *txn = s->txn;
4051 struct http_msg *msg = &txn->req;
4052 struct uri_auth *uri_auth = s->be->uri_auth;
4053 const char *h, *lookup, *end;
4054 struct appctx *appctx;
4055 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004056 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004057
4058 appctx = si_appctx(si);
4059 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4060 appctx->st1 = appctx->st2 = 0;
4061 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004062 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004063 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4064 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4065 appctx->ctx.stats.flags |= STAT_CHUNKED;
4066
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004067 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004068 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004069 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4070 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004071
4072 for (h = lookup; h <= end - 3; h++) {
4073 if (memcmp(h, ";up", 3) == 0) {
4074 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4075 break;
4076 }
4077 }
4078
4079 if (uri_auth->refresh) {
4080 for (h = lookup; h <= end - 10; h++) {
4081 if (memcmp(h, ";norefresh", 10) == 0) {
4082 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4083 break;
4084 }
4085 }
4086 }
4087
4088 for (h = lookup; h <= end - 4; h++) {
4089 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004090 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004091 break;
4092 }
4093 }
4094
4095 for (h = lookup; h <= end - 6; h++) {
4096 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004097 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004098 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4099 break;
4100 }
4101 }
4102
Christopher Faulet6338a082019-09-09 15:50:54 +02004103 for (h = lookup; h <= end - 5; h++) {
4104 if (memcmp(h, ";json", 5) == 0) {
4105 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4106 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4107 break;
4108 }
4109 }
4110
4111 for (h = lookup; h <= end - 12; h++) {
4112 if (memcmp(h, ";json-schema", 12) == 0) {
4113 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4114 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4115 break;
4116 }
4117 }
4118
Christopher Faulet377c5a52018-10-24 21:21:30 +02004119 for (h = lookup; h <= end - 8; h++) {
4120 if (memcmp(h, ";st=", 4) == 0) {
4121 int i;
4122 h += 4;
4123 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4124 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4125 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4126 appctx->ctx.stats.st_code = i;
4127 break;
4128 }
4129 }
4130 break;
4131 }
4132 }
4133
4134 appctx->ctx.stats.scope_str = 0;
4135 appctx->ctx.stats.scope_len = 0;
4136 for (h = lookup; h <= end - 8; h++) {
4137 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4138 int itx = 0;
4139 const char *h2;
4140 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4141 const char *err;
4142
4143 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4144 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004145 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4146 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004147 if (*h == ';' || *h == '&' || *h == ' ')
4148 break;
4149 itx++;
4150 h++;
4151 }
4152
4153 if (itx > STAT_SCOPE_TXT_MAXLEN)
4154 itx = STAT_SCOPE_TXT_MAXLEN;
4155 appctx->ctx.stats.scope_len = itx;
4156
4157 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4158 memcpy(scope_txt, h2, itx);
4159 scope_txt[itx] = '\0';
4160 err = invalid_char(scope_txt);
4161 if (err) {
4162 /* bad char in search text => clear scope */
4163 appctx->ctx.stats.scope_str = 0;
4164 appctx->ctx.stats.scope_len = 0;
4165 }
4166 break;
4167 }
4168 }
4169
4170 /* now check whether we have some admin rules for this request */
4171 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4172 int ret = 1;
4173
4174 if (stats_admin_rule->cond) {
4175 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4176 ret = acl_pass(ret);
4177 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4178 ret = !ret;
4179 }
4180
4181 if (ret) {
4182 /* no rule, or the rule matches */
4183 appctx->ctx.stats.flags |= STAT_ADMIN;
4184 break;
4185 }
4186 }
4187
Christopher Faulet5d45e382019-02-27 15:15:23 +01004188 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4189 appctx->st0 = STAT_HTTP_HEAD;
4190 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004191 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004192 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004193 if (msg->msg_state < HTTP_MSG_DATA)
4194 req->analysers |= AN_REQ_HTTP_BODY;
4195 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004196 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004197 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004198 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4199 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4200 appctx->st0 = STAT_HTTP_LAST;
4201 }
4202 }
4203 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004204 /* Unsupported method */
4205 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4206 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4207 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004208 }
4209
4210 s->task->nice = -32; /* small boost for HTTP statistics */
4211 return 1;
4212}
4213
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004214void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004215{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004216 struct channel *req = &s->req;
4217 struct channel *res = &s->res;
4218 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004219 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004220 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004221 struct ist path, location;
4222 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004223
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004224 /*
4225 * Create the location
4226 */
4227 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004228
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004229 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004230 /* special prefix "/" means don't change URL */
4231 srv = __objt_server(s->target);
4232 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4233 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4234 return;
4235 }
4236
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004237 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004238 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004239 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004240 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004241 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004242 return;
4243
4244 if (!chunk_memcat(&trash, path.ptr, path.len))
4245 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004246 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004247
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004248 /*
4249 * Create the 302 respone
4250 */
4251 htx = htx_from_buf(&res->buf);
4252 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4253 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4254 ist("HTTP/1.1"), ist("302"), ist("Found"));
4255 if (!sl)
4256 goto fail;
4257 sl->info.res.status = 302;
4258 s->txn->status = 302;
4259
4260 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4261 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4262 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4263 !htx_add_header(htx, ist("Location"), location))
4264 goto fail;
4265
4266 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4267 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004268
Christopher Fauletc20afb82020-01-24 19:16:26 +01004269 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004270 if (!http_forward_proxy_resp(s, 1))
4271 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004272
4273 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004274 si_shutr(si);
4275 si_shutw(si);
4276 si->err_type = SI_ET_NONE;
4277 si->state = SI_ST_CLO;
4278
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004279 if (!(s->flags & SF_ERR_MASK))
4280 s->flags |= SF_ERR_LOCAL;
4281 if (!(s->flags & SF_FINST_MASK))
4282 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004283
4284 /* FIXME: we should increase a counter of redirects per server and per backend. */
4285 srv_inc_sess_ctr(srv);
4286 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004287 return;
4288
4289 fail:
4290 /* If an error occurred, remove the incomplete HTTP response from the
4291 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004292 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004293}
4294
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004295/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004296 * because an error was triggered during the body forwarding.
4297 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004298static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004299{
4300 struct channel *chn = &s->req;
4301 struct http_txn *txn = s->txn;
4302
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004303 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004304
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004305 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4306 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004307 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004308 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004309 goto end;
4310 }
4311
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004312 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4313 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004314 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004315 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004316
4317 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004318 /* No need to read anymore, the request was completely parsed.
4319 * We can shut the read side unless we want to abort_on_close,
4320 * or we have a POST request. The issue with POST requests is
4321 * that some browsers still send a CRLF after the request, and
4322 * this CRLF must be read so that it does not remain in the kernel
4323 * buffers, otherwise a close could cause an RST on some systems
4324 * (eg: Linux).
4325 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004326 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004327 channel_dont_read(chn);
4328
4329 /* if the server closes the connection, we want to immediately react
4330 * and close the socket to save packets and syscalls.
4331 */
4332 s->si[1].flags |= SI_FL_NOHALF;
4333
4334 /* In any case we've finished parsing the request so we must
4335 * disable Nagle when sending data because 1) we're not going
4336 * to shut this side, and 2) the server is waiting for us to
4337 * send pending data.
4338 */
4339 chn->flags |= CF_NEVER_WAIT;
4340
Christopher Fauletd01ce402019-01-02 17:44:13 +01004341 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4342 /* The server has not finished to respond, so we
4343 * don't want to move in order not to upset it.
4344 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004345 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004346 return;
4347 }
4348
Christopher Fauletf2824e62018-10-01 12:12:37 +02004349 /* When we get here, it means that both the request and the
4350 * response have finished receiving. Depending on the connection
4351 * mode, we'll have to wait for the last bytes to leave in either
4352 * direction, and sometimes for a close to be effective.
4353 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004354 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004355 /* Tunnel mode will not have any analyser so it needs to
4356 * poll for reads.
4357 */
4358 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004359 if (b_data(&chn->buf)) {
4360 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004361 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004362 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004363 txn->req.msg_state = HTTP_MSG_TUNNEL;
4364 }
4365 else {
4366 /* we're not expecting any new data to come for this
4367 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004368 *
4369 * However, there is an exception if the response
4370 * length is undefined. In this case, we need to wait
4371 * the close from the server. The response will be
4372 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004373 */
4374 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4375 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004376 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004377
4378 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4379 channel_shutr_now(chn);
4380 channel_shutw_now(chn);
4381 }
4382 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004383 goto check_channel_flags;
4384 }
4385
4386 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4387 http_msg_closing:
4388 /* nothing else to forward, just waiting for the output buffer
4389 * to be empty and for the shutw_now to take effect.
4390 */
4391 if (channel_is_empty(chn)) {
4392 txn->req.msg_state = HTTP_MSG_CLOSED;
4393 goto http_msg_closed;
4394 }
4395 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004396 txn->req.msg_state = HTTP_MSG_ERROR;
4397 goto end;
4398 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004399 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004400 return;
4401 }
4402
4403 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4404 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004405 /* if we don't know whether the server will close, we need to hard close */
4406 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4407 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004408 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004409 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004410 channel_dont_read(chn);
4411 goto end;
4412 }
4413
4414 check_channel_flags:
4415 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4416 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4417 /* if we've just closed an output, let's switch */
4418 txn->req.msg_state = HTTP_MSG_CLOSING;
4419 goto http_msg_closing;
4420 }
4421
4422 end:
4423 chn->analysers &= AN_REQ_FLT_END;
4424 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4425 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4426 channel_auto_close(chn);
4427 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004428 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004429}
4430
4431
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004432/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004433 * because an error was triggered during the body forwarding.
4434 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004435static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004436{
4437 struct channel *chn = &s->res;
4438 struct http_txn *txn = s->txn;
4439
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004440 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004441
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004442 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4443 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004444 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004445 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004446 goto end;
4447 }
4448
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004449 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4450 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004451 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004452 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004453
4454 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4455 /* In theory, we don't need to read anymore, but we must
4456 * still monitor the server connection for a possible close
4457 * while the request is being uploaded, so we don't disable
4458 * reading.
4459 */
4460 /* channel_dont_read(chn); */
4461
4462 if (txn->req.msg_state < HTTP_MSG_DONE) {
4463 /* The client seems to still be sending data, probably
4464 * because we got an error response during an upload.
4465 * We have the choice of either breaking the connection
4466 * or letting it pass through. Let's do the later.
4467 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004468 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004469 return;
4470 }
4471
4472 /* When we get here, it means that both the request and the
4473 * response have finished receiving. Depending on the connection
4474 * mode, we'll have to wait for the last bytes to leave in either
4475 * direction, and sometimes for a close to be effective.
4476 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004477 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004478 channel_auto_read(chn);
4479 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004480 if (b_data(&chn->buf)) {
4481 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004482 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004483 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004484 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4485 }
4486 else {
4487 /* we're not expecting any new data to come for this
4488 * transaction, so we can close it.
4489 */
4490 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4491 channel_shutr_now(chn);
4492 channel_shutw_now(chn);
4493 }
4494 }
4495 goto check_channel_flags;
4496 }
4497
4498 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4499 http_msg_closing:
4500 /* nothing else to forward, just waiting for the output buffer
4501 * to be empty and for the shutw_now to take effect.
4502 */
4503 if (channel_is_empty(chn)) {
4504 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4505 goto http_msg_closed;
4506 }
4507 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004508 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004509 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004510 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004511 if (strm_sess(s)->listener->counters)
4512 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004513 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004514 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004515 goto end;
4516 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004517 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004518 return;
4519 }
4520
4521 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4522 http_msg_closed:
4523 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004524 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004525 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004526 goto end;
4527 }
4528
4529 check_channel_flags:
4530 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4531 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4532 /* if we've just closed an output, let's switch */
4533 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4534 goto http_msg_closing;
4535 }
4536
4537 end:
4538 chn->analysers &= AN_RES_FLT_END;
4539 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4540 chn->analysers |= AN_RES_FLT_XFER_DATA;
4541 channel_auto_close(chn);
4542 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004543 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004544}
4545
Christopher Fauletef70e252020-01-28 09:26:19 +01004546/* Forward a response generated by HAProxy (error/redirect/return). This
4547 * function forwards all pending incoming data. If <final> is set to 0, nothing
4548 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004549 * transaction is terminated and the request is emptied. On success 1 is
4550 * returned. If an error occurred, 0 is returned.
Christopher Fauletef70e252020-01-28 09:26:19 +01004551 */
4552int http_forward_proxy_resp(struct stream *s, int final)
4553{
4554 struct channel *req = &s->req;
4555 struct channel *res = &s->res;
4556 struct htx *htx = htxbuf(&res->buf);
4557 size_t data;
4558
4559 if (final) {
4560 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004561
4562 if (!http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004563 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004564
4565 channel_auto_read(req);
4566 channel_abort(req);
4567 channel_auto_close(req);
4568 channel_htx_erase(req, htxbuf(&req->buf));
4569
4570 res->wex = tick_add_ifset(now_ms, res->wto);
4571 channel_auto_read(res);
4572 channel_auto_close(res);
4573 channel_shutr_now(res);
4574 }
4575
4576 data = htx->data - co_data(res);
4577 c_adv(res, data);
4578 htx->first = -1;
4579 res->total += data;
4580 return 1;
4581}
4582
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004583void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004584 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004585{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004586 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004587 if (!(s->flags & SF_ERR_MASK))
4588 s->flags |= err;
4589 if (!(s->flags & SF_FINST_MASK))
4590 s->flags |= finst;
4591}
4592
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004593void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004594{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004595 if (!msg) {
4596 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4597 goto end;
4598 }
4599
4600 if (http_reply_message(s, msg) == -1) {
4601 /* On error, return a 500 error message, but don't rewrite it if
4602 * it is already an internal error.
4603 */
4604 if (s->txn->status == 500)
4605 s->txn->flags |= TX_CONST_REPLY;
4606 s->txn->status = 500;
4607 s->txn->http_reply = NULL;
4608 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4609 }
4610
4611end:
4612 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
4613 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
4614
Christopher Faulet0f226952018-10-22 09:29:56 +02004615 channel_auto_read(&s->req);
4616 channel_abort(&s->req);
4617 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004618 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004619 channel_auto_read(&s->res);
4620 channel_auto_close(&s->res);
4621 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004622}
4623
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004624struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004625{
4626 const int msgnum = http_get_status_idx(s->txn->status);
4627
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004628 if (s->txn->http_reply)
4629 return s->txn->http_reply;
4630 else if (s->be->replies[msgnum])
4631 return s->be->replies[msgnum];
4632 else if (strm_fe(s)->replies[msgnum])
4633 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004634 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004635 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004636}
4637
Christopher Faulet97e466c2020-05-15 15:12:47 +02004638/* Produces an HTX message from an http reply. Depending on the http reply type, a,
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004639 * errorfile, an raw file or a log-format string is used. On success, it returns
4640 * 0. If an error occurs -1 is returned.
4641 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004642int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004643{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004644 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004645 struct htx_sl *sl;
4646 struct buffer *body = NULL;
4647 const char *status, *reason, *clen, *ctype;
4648 unsigned int slflags;
4649 int ret = 0;
4650
Christopher Faulete29a97e2020-05-14 14:49:25 +02004651 /*
4652 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4653 *
4654 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4655 * as no payload if NULL. the TXN status code is set with the status
4656 * of the original reply.
4657 */
4658
4659 if (reply->type == HTTP_REPLY_INDIRECT) {
4660 if (reply->body.reply)
4661 reply = reply->body.reply;
4662 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004663 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4664 /* get default error message */
4665 if (reply == s->txn->http_reply)
4666 s->txn->http_reply = NULL;
4667 reply = http_error_message(s);
4668 if (reply->type == HTTP_REPLY_INDIRECT) {
4669 if (reply->body.reply)
4670 reply = reply->body.reply;
4671 }
4672 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004673
4674 if (reply->type == HTTP_REPLY_ERRMSG) {
4675 /* implicit or explicit error message*/
4676 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004677 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004678 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004679 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004680 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004681 }
4682 else {
4683 /* no payload, file or log-format string */
4684 if (reply->type == HTTP_REPLY_RAW) {
4685 /* file */
4686 body = &reply->body.obj;
4687 }
4688 else if (reply->type == HTTP_REPLY_LOGFMT) {
4689 /* log-format string */
4690 body = alloc_trash_chunk();
4691 if (!body)
4692 goto fail_alloc;
4693 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4694 }
4695 /* else no payload */
4696
4697 status = ultoa(reply->status);
4698 reason = http_get_reason(reply->status);
4699 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4700 if (!body || !b_data(body))
4701 slflags |= HTX_SL_F_BODYLESS;
4702 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4703 if (!sl)
4704 goto fail;
4705 sl->info.res.status = reply->status;
4706
4707 clen = (body ? ultoa(b_data(body)) : "0");
4708 ctype = reply->ctype;
4709
4710 if (!LIST_ISEMPTY(&reply->hdrs)) {
4711 struct http_reply_hdr *hdr;
4712 struct buffer *value = alloc_trash_chunk();
4713
4714 if (!value)
4715 goto fail;
4716
4717 list_for_each_entry(hdr, &reply->hdrs, list) {
4718 chunk_reset(value);
4719 value->data = build_logline(s, value->area, value->size, &hdr->value);
4720 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4721 free_trash_chunk(value);
4722 goto fail;
4723 }
4724 chunk_reset(value);
4725 }
4726 free_trash_chunk(value);
4727 }
4728
4729 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4730 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4731 !htx_add_endof(htx, HTX_BLK_EOH) ||
4732 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))) ||
4733 !htx_add_endof(htx, HTX_BLK_EOM))
4734 goto fail;
4735 }
4736
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004737 leave:
4738 if (reply->type == HTTP_REPLY_LOGFMT)
4739 free_trash_chunk(body);
4740 return ret;
4741
4742 fail_alloc:
4743 if (!(s->flags & SF_ERR_MASK))
4744 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004745 /* fall through */
4746 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004747 ret = -1;
4748 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004749}
4750
4751/* Send an http reply to the client. On success, it returns 0. If an error
4752 * occurs -1 is returned.
4753 */
4754int http_reply_message(struct stream *s, struct http_reply *reply)
4755{
4756 struct channel *res = &s->res;
4757 struct htx *htx = htx_from_buf(&res->buf);
4758
4759 if (s->txn->status == -1)
4760 s->txn->status = reply->status;
4761 channel_htx_truncate(res, htx);
4762
4763 if (http_reply_to_htx(s, htx, reply) == -1)
4764 goto fail;
4765
4766 htx_to_buf(htx, &s->res.buf);
4767 if (!http_forward_proxy_resp(s, 1))
4768 goto fail;
4769 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004770
4771 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004772 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004773 if (!(s->flags & SF_ERR_MASK))
4774 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004775 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004776}
4777
Christopher Faulet304cc402019-07-15 15:46:28 +02004778/* Return the error message corresponding to si->err_type. It is assumed
4779 * that the server side is closed. Note that err_type is actually a
4780 * bitmask, where almost only aborts may be cumulated with other
4781 * values. We consider that aborted operations are more important
4782 * than timeouts or errors due to the fact that nobody else in the
4783 * logs might explain incomplete retries. All others should avoid
4784 * being cumulated. It should normally not be possible to have multiple
4785 * aborts at once, but just in case, the first one in sequence is reported.
4786 * Note that connection errors appearing on the second request of a keep-alive
4787 * connection are not reported since this allows the client to retry.
4788 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004789void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004790{
4791 int err_type = si->err_type;
4792
4793 /* set s->txn->status for http_error_message(s) */
4794 s->txn->status = 503;
4795
4796 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004797 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4798 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004799 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004800 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4801 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4802 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004803 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004804 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4805 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004806 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004807 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4808 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004809 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004810 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4811 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4812 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004813 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004814 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4815 (s->flags & SF_SRV_REUSED) ? NULL :
4816 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004817 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004818 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4819 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4820 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004821 else { /* SI_ET_CONN_OTHER and others */
4822 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004823 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4824 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004825 }
4826}
4827
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004828
Christopher Faulet4a28a532019-03-01 11:19:40 +01004829/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4830 * on success and -1 on error.
4831 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004832static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004833{
4834 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4835 * then we must send an HTTP/1.1 100 Continue intermediate response.
4836 */
4837 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4838 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4839 struct ist hdr = { .ptr = "Expect", .len = 6 };
4840 struct http_hdr_ctx ctx;
4841
4842 ctx.blk = NULL;
4843 /* Expect is allowed in 1.1, look for it */
4844 if (http_find_header(htx, hdr, &ctx, 0) &&
4845 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004846 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004847 return -1;
4848 http_remove_header(htx, &ctx);
4849 }
4850 }
4851 return 0;
4852}
4853
Christopher Faulet23a3c792018-11-28 10:01:23 +01004854/* Send a 100-Continue response to the client. It returns 0 on success and -1
4855 * on error. The response channel is updated accordingly.
4856 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004857static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004858{
4859 struct channel *res = &s->res;
4860 struct htx *htx = htx_from_buf(&res->buf);
4861 struct htx_sl *sl;
4862 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4863 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004864
4865 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4866 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4867 if (!sl)
4868 goto fail;
4869 sl->info.res.status = 100;
4870
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004871 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004872 goto fail;
4873
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004874 if (!http_forward_proxy_resp(s, 0))
4875 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004876 return 0;
4877
4878 fail:
4879 /* If an error occurred, remove the incomplete HTTP response from the
4880 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004881 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004882 return -1;
4883}
4884
Christopher Faulet12c51e22018-11-28 15:59:42 +01004885
Christopher Faulet0f226952018-10-22 09:29:56 +02004886/*
4887 * Capture headers from message <htx> according to header list <cap_hdr>, and
4888 * fill the <cap> pointers appropriately.
4889 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004890static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004891{
4892 struct cap_hdr *h;
4893 int32_t pos;
4894
Christopher Fauleta3f15502019-05-13 15:27:23 +02004895 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004896 struct htx_blk *blk = htx_get_blk(htx, pos);
4897 enum htx_blk_type type = htx_get_blk_type(blk);
4898 struct ist n, v;
4899
4900 if (type == HTX_BLK_EOH)
4901 break;
4902 if (type != HTX_BLK_HDR)
4903 continue;
4904
4905 n = htx_get_blk_name(htx, blk);
4906
4907 for (h = cap_hdr; h; h = h->next) {
4908 if (h->namelen && (h->namelen == n.len) &&
4909 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4910 if (cap[h->index] == NULL)
4911 cap[h->index] =
4912 pool_alloc(h->pool);
4913
4914 if (cap[h->index] == NULL) {
4915 ha_alert("HTTP capture : out of memory.\n");
4916 break;
4917 }
4918
4919 v = htx_get_blk_value(htx, blk);
4920 if (v.len > h->len)
4921 v.len = h->len;
4922
4923 memcpy(cap[h->index], v.ptr, v.len);
4924 cap[h->index][v.len]=0;
4925 }
4926 }
4927 }
4928}
4929
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004930/* Delete a value in a header between delimiters <from> and <next>. The header
4931 * itself is delimited by <start> and <end> pointers. The number of characters
4932 * displaced is returned, and the pointer to the first delimiter is updated if
4933 * required. The function tries as much as possible to respect the following
4934 * principles :
4935 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4936 * in which case <next> is simply removed
4937 * - set exactly one space character after the new first delimiter, unless there
4938 * are not enough characters in the block being moved to do so.
4939 * - remove unneeded spaces before the previous delimiter and after the new
4940 * one.
4941 *
4942 * It is the caller's responsibility to ensure that :
4943 * - <from> points to a valid delimiter or <start> ;
4944 * - <next> points to a valid delimiter or <end> ;
4945 * - there are non-space chars before <from>.
4946 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004947static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004948{
4949 char *prev = *from;
4950
4951 if (prev == start) {
4952 /* We're removing the first value. eat the semicolon, if <next>
4953 * is lower than <end> */
4954 if (next < end)
4955 next++;
4956
4957 while (next < end && HTTP_IS_SPHT(*next))
4958 next++;
4959 }
4960 else {
4961 /* Remove useless spaces before the old delimiter. */
4962 while (HTTP_IS_SPHT(*(prev-1)))
4963 prev--;
4964 *from = prev;
4965
4966 /* copy the delimiter and if possible a space if we're
4967 * not at the end of the line.
4968 */
4969 if (next < end) {
4970 *prev++ = *next++;
4971 if (prev + 1 < next)
4972 *prev++ = ' ';
4973 while (next < end && HTTP_IS_SPHT(*next))
4974 next++;
4975 }
4976 }
4977 memmove(prev, next, end - next);
4978 return (prev - next);
4979}
4980
Christopher Faulet0f226952018-10-22 09:29:56 +02004981
4982/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004983 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004984 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004985static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004986{
4987 struct ist dst = ist2(str, 0);
4988
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004989 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004990 goto end;
4991 if (dst.len + 1 > len)
4992 goto end;
4993 dst.ptr[dst.len++] = ' ';
4994
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004995 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004996 goto end;
4997 if (dst.len + 1 > len)
4998 goto end;
4999 dst.ptr[dst.len++] = ' ';
5000
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005001 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005002 end:
5003 return dst.len;
5004}
5005
5006/*
5007 * Print a debug line with a start line.
5008 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005009static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005010{
5011 struct session *sess = strm_sess(s);
5012 int max;
5013
5014 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5015 dir,
5016 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5017 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5018
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005019 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005020 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005021 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005022 trash.area[trash.data++] = ' ';
5023
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005024 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005025 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005026 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005027 trash.area[trash.data++] = ' ';
5028
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005029 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005030 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005031 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005032 trash.area[trash.data++] = '\n';
5033
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005034 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005035}
5036
5037/*
5038 * Print a debug line with a header.
5039 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005040static 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 +02005041{
5042 struct session *sess = strm_sess(s);
5043 int max;
5044
5045 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5046 dir,
5047 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5048 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5049
5050 max = n.len;
5051 UBOUND(max, trash.size - trash.data - 3);
5052 chunk_memcat(&trash, n.ptr, max);
5053 trash.area[trash.data++] = ':';
5054 trash.area[trash.data++] = ' ';
5055
5056 max = v.len;
5057 UBOUND(max, trash.size - trash.data - 1);
5058 chunk_memcat(&trash, v.ptr, max);
5059 trash.area[trash.data++] = '\n';
5060
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005061 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005062}
5063
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005064/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5065 * In case of allocation failure, everything allocated is freed and NULL is
5066 * returned. Otherwise the new transaction is assigned to the stream and
5067 * returned.
5068 */
5069struct http_txn *http_alloc_txn(struct stream *s)
5070{
5071 struct http_txn *txn = s->txn;
5072
5073 if (txn)
5074 return txn;
5075
5076 txn = pool_alloc(pool_head_http_txn);
5077 if (!txn)
5078 return txn;
5079
5080 s->txn = txn;
5081 return txn;
5082}
5083
5084void http_txn_reset_req(struct http_txn *txn)
5085{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005086 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005087 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5088}
5089
5090void http_txn_reset_res(struct http_txn *txn)
5091{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005092 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005093 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5094}
5095
5096/*
5097 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5098 * the required fields are properly allocated and that we only need to (re)init
5099 * them. This should be used before processing any new request.
5100 */
5101void http_init_txn(struct stream *s)
5102{
5103 struct http_txn *txn = s->txn;
5104 struct conn_stream *cs = objt_cs(s->si[0].end);
5105
5106 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5107 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5108 : 0);
5109 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005110 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005111 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005112
5113 txn->cookie_first_date = 0;
5114 txn->cookie_last_date = 0;
5115
5116 txn->srv_cookie = NULL;
5117 txn->cli_cookie = NULL;
5118 txn->uri = NULL;
5119
5120 http_txn_reset_req(txn);
5121 http_txn_reset_res(txn);
5122
5123 txn->req.chn = &s->req;
5124 txn->rsp.chn = &s->res;
5125
5126 txn->auth.method = HTTP_AUTH_UNKNOWN;
5127
5128 vars_init(&s->vars_txn, SCOPE_TXN);
5129 vars_init(&s->vars_reqres, SCOPE_REQ);
5130}
5131
5132/* to be used at the end of a transaction */
5133void http_end_txn(struct stream *s)
5134{
5135 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005136
5137 /* these ones will have been dynamically allocated */
5138 pool_free(pool_head_requri, txn->uri);
5139 pool_free(pool_head_capture, txn->cli_cookie);
5140 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005141 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005142
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005143 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005144 txn->uri = NULL;
5145 txn->srv_cookie = NULL;
5146 txn->cli_cookie = NULL;
5147
Christopher Faulet59399252019-11-07 14:27:52 +01005148 if (!LIST_ISEMPTY(&s->vars_txn.head))
5149 vars_prune(&s->vars_txn, s->sess, s);
5150 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5151 vars_prune(&s->vars_reqres, s->sess, s);
5152}
5153
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005154
5155DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005156
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005157__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005158static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005159{
5160}
5161
5162
5163/*
5164 * Local variables:
5165 * c-indent-level: 8
5166 * c-basic-offset: 8
5167 * End:
5168 */