blob: f9dbfb129ebe289c719bab283374d7f6d2e3c7d0 [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 Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020016#include <haproxy/backend.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 Tarreau36979d92020-06-05 17:27:29 +020022#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020023#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020024#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020025#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020027#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020028#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020029#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020030#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020031#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020032#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020033#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020034#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020035#include <haproxy/stream_interface.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020036#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020037#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020038#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020039
Christopher Faulete0768eb2018-10-03 16:38:02 +020040
Christopher Fauleteea8fc72019-11-05 16:18:10 +010041#define TRACE_SOURCE &trace_strm
42
Christopher Faulet377c5a52018-10-24 21:21:30 +020043extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020044
Christopher Fauleta8a46e22019-07-16 14:53:09 +020045struct pool_head *pool_head_requri = NULL;
46struct pool_head *pool_head_capture = NULL;
47
48
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020049static void http_end_request(struct stream *s);
50static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020051
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020052static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
53static int http_del_hdr_value(char *start, char *end, char **from, char *next);
54static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020055static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
56static 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 +020057
Christopher Fauletb58f62b2020-01-13 16:40:13 +010058static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020059static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020060
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020061static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
62static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020063
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020064static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
65static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020066
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020067static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
68static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010069
Christopher Faulete0768eb2018-10-03 16:38:02 +020070/* This stream analyser waits for a complete HTTP request. It returns 1 if the
71 * processing can continue on next analysers, or zero if it either needs more
72 * data or wants to immediately abort the request (eg: timeout, error, ...). It
73 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
74 * when it has nothing left to do, and may remove any analyser when it wants to
75 * abort.
76 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020077int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020078{
Christopher Faulet9768c262018-10-22 09:34:31 +020079
Christopher Faulete0768eb2018-10-03 16:38:02 +020080 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020081 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020082 *
Christopher Faulet9768c262018-10-22 09:34:31 +020083 * Once the start line and all headers are received, we may perform a
84 * capture of the error (if any), and we will set a few fields. We also
85 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020086 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020087 struct session *sess = s->sess;
88 struct http_txn *txn = s->txn;
89 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020090 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010091 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020092
Christopher Fauleteea8fc72019-11-05 16:18:10 +010093 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020094
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010095 htx = htxbuf(&req->buf);
Christopher Faulet9768c262018-10-22 09:34:31 +020096
Willy Tarreau4236f032019-03-05 10:43:32 +010097 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +020098 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +010099 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200100 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Faulete01e5832021-02-10 14:58:01 +0100101 if (htx->flags & HTX_FL_PARSING_ERROR) {
102 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200103 goto return_bad_req;
Christopher Faulete01e5832021-02-10 14:58:01 +0100104 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200105 else
106 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100107 }
108
Christopher Faulete0768eb2018-10-03 16:38:02 +0200109 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200110 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200111
112 /* If there is data available for analysis, log the end of the idle time. */
Christopher Faulet870aad92018-11-29 15:23:46 +0100113 if (c_data(req) && s->logs.t_idle == -1) {
114 const struct cs_info *csinfo = si_get_cs_info(objt_cs(s->si[0].end));
115
116 s->logs.t_idle = ((csinfo)
117 ? csinfo->t_idle
118 : tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake);
119 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200120
Christopher Faulete0768eb2018-10-03 16:38:02 +0200121 /*
122 * Now we quickly check if we have found a full valid request.
123 * If not so, we check the FD and buffer states before leaving.
124 * A full request is indicated by the fact that we have seen
125 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
126 * requests are checked first. When waiting for a second request
127 * on a keep-alive stream, if we encounter and error, close, t/o,
128 * we note the error in the stream flags but don't set any state.
129 * Since the error will be noted there, it will not be counted by
130 * process_stream() as a frontend error.
131 * Last, we may increase some tracked counters' http request errors on
132 * the cases that are deliberately the client's fault. For instance,
133 * a timeout or connection reset is not counted as an error. However
134 * a bad request is.
135 */
Christopher Faulet29f17582019-05-23 11:03:26 +0200136 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200137 if (htx->flags & HTX_FL_UPGRADE)
138 goto failed_keep_alive;
139
Christopher Faulet9768c262018-10-22 09:34:31 +0200140 /* 1: have we encountered a read error ? */
141 if (req->flags & CF_READ_ERROR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200142 if (!(s->flags & SF_ERR_MASK))
143 s->flags |= SF_ERR_CLICL;
144
145 if (txn->flags & TX_WAIT_NEXT_RQ)
146 goto failed_keep_alive;
147
148 if (sess->fe->options & PR_O_IGNORE_PRB)
149 goto failed_keep_alive;
150
Christopher Faulete0768eb2018-10-03 16:38:02 +0200151 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200152 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100153 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200154 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100155 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200156
Christopher Faulet9768c262018-10-22 09:34:31 +0200157 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200158 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet9768c262018-10-22 09:34:31 +0200159 req->analysers &= AN_REQ_FLT_END;
160
Christopher Faulete0768eb2018-10-03 16:38:02 +0200161 if (!(s->flags & SF_FINST_MASK))
162 s->flags |= SF_FINST_R;
163 return 0;
164 }
165
Christopher Faulet9768c262018-10-22 09:34:31 +0200166 /* 2: has the read timeout expired ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200167 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
168 if (!(s->flags & SF_ERR_MASK))
169 s->flags |= SF_ERR_CLITO;
170
171 if (txn->flags & TX_WAIT_NEXT_RQ)
172 goto failed_keep_alive;
173
174 if (sess->fe->options & PR_O_IGNORE_PRB)
175 goto failed_keep_alive;
176
Christopher Faulete0768eb2018-10-03 16:38:02 +0200177 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200178 proxy_inc_fe_req_ctr(sess->listener, 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);
Emeric Brun28976442020-10-07 08:50:09 +0200205 proxy_inc_fe_req_ctr(sess->listener, 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);
Emeric Brun28976442020-10-07 08:50:09 +0200273 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200274
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 Tarreau9b7587a2020-10-15 07:32:10 +0200756 if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
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 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500775 * Instead of rewriting 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. */
Christopher Fauletdf82f0b2021-02-26 12:45:56 +0100876 if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET) {
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 Faulet9ded6fa2020-11-16 16:03:35 +01001046 /* CONNECT requests have no body */
1047 if (txn->meth == HTTP_METH_CONNECT)
1048 goto http_end;
1049
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001050 if (msg->msg_state < HTTP_MSG_BODY)
1051 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001052
Christopher Faulete0768eb2018-10-03 16:38:02 +02001053 /* We have to parse the HTTP request body to find any required data.
1054 * "balance url_param check_post" should have been the only way to get
1055 * into this. We were brought here after HTTP header analysis, so all
1056 * related structures are ready.
1057 */
1058
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001059 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001060 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001061 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001062 }
1063
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001064 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001065
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001066 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1067 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001068 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001069 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001070 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001071 goto http_end;
1072
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001073 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001074 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1075 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001076 if (!(s->flags & SF_ERR_MASK))
1077 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001078 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1079 if (sess->listener->counters)
1080 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1081 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001082 }
1083
1084 /* we get here if we need to wait for more data */
1085 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1086 /* Not enough data. We'll re-use the http-request
1087 * timeout here. Ideally, we should set the timeout
1088 * relative to the accept() date. We just set the
1089 * request timeout once at the beginning of the
1090 * request.
1091 */
1092 channel_dont_connect(req);
1093 if (!tick_isset(req->analyse_exp))
1094 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001095 DBG_TRACE_DEVEL("waiting for more data",
1096 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001097 return 0;
1098 }
1099
1100 http_end:
1101 /* The situation will not evolve, so let's give up on the analysis. */
1102 s->logs.tv_request = now; /* update the request timer to reflect full request */
1103 req->analysers &= ~an_bit;
1104 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001105 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001106 return 1;
1107
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001108 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001109 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001110 if (!(s->flags & SF_ERR_MASK))
1111 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001112 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001113 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +01001114 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001115 if (sess->listener->counters)
1116 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
1117 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001118
Christopher Faulete0768eb2018-10-03 16:38:02 +02001119 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001120 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001121 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1122 if (sess->listener->counters)
1123 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1124 /* fall through */
1125
1126 return_prx_cond:
1127 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001128
1129 if (!(s->flags & SF_ERR_MASK))
1130 s->flags |= SF_ERR_PRXCOND;
1131 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001132 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001133
Christopher Faulete0768eb2018-10-03 16:38:02 +02001134 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001135 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001136 DBG_TRACE_DEVEL("leaving on error",
1137 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001138 return 0;
1139}
1140
1141/* This function is an analyser which forwards request body (including chunk
1142 * sizes if any). It is called as soon as we must forward, even if we forward
1143 * zero byte. The only situation where it must not be called is when we're in
1144 * tunnel mode and we want to forward till the close. It's used both to forward
1145 * remaining data and to resync after end of body. It expects the msg_state to
1146 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1147 * read more data, or 1 once we can go on with next request or end the stream.
1148 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1149 * bytes of pending data + the headers if not already done.
1150 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001151int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001152{
1153 struct session *sess = s->sess;
1154 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001155 struct http_msg *msg = &txn->req;
1156 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001157 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001158 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001159
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001160 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001161
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001162 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001163
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001164 if (htx->flags & HTX_FL_PARSING_ERROR)
1165 goto return_bad_req;
1166 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1167 goto return_int_err;
1168
Christopher Faulete0768eb2018-10-03 16:38:02 +02001169 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1170 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1171 /* Output closed while we were sending data. We must abort and
1172 * wake the other side up.
1173 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001174
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001175 /* Don't abort yet if we had L7 retries activated and it
1176 * was a write error, we may recover.
1177 */
1178 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001179 (s->si[1].flags & SI_FL_L7_RETRY)) {
1180 DBG_TRACE_DEVEL("leaving on L7 retry",
1181 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001182 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001183 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001184 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001185 http_end_request(s);
1186 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001187 DBG_TRACE_DEVEL("leaving on error",
1188 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001189 return 1;
1190 }
1191
1192 /* Note that we don't have to send 100-continue back because we don't
1193 * need the data to complete our job, and it's up to the server to
1194 * decide whether to return 100, 417 or anything else in return of
1195 * an "Expect: 100-continue" header.
1196 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001197 if (msg->msg_state == HTTP_MSG_BODY)
1198 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001199
Christopher Faulete0768eb2018-10-03 16:38:02 +02001200 /* in most states, we should abort in case of early close */
1201 channel_auto_close(req);
1202
1203 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001204 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001205 if (req->flags & CF_EOI)
1206 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001207 }
1208 else {
1209 /* We can't process the buffer's contents yet */
1210 req->flags |= CF_WAKE_WRITE;
1211 goto missing_data_or_waiting;
1212 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001213 }
1214
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001215 if (msg->msg_state >= HTTP_MSG_ENDING)
1216 goto ending;
1217
1218 if (txn->meth == HTTP_METH_CONNECT) {
1219 msg->msg_state = HTTP_MSG_ENDING;
1220 goto ending;
1221 }
1222
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001223 /* Forward input data. We get it by removing all outgoing data not
1224 * forwarded yet from HTX data size. If there are some data filters, we
1225 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001226 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001227 if (HAS_REQ_DATA_FILTERS(s)) {
1228 ret = flt_http_payload(s, msg, htx->data);
1229 if (ret < 0)
1230 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001231 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001232 }
1233 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001234 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001235 if (msg->flags & HTTP_MSGF_XFER_LEN)
1236 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001237 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001238
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001239 if (htx->data != co_data(req))
1240 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001241
Christopher Faulet9768c262018-10-22 09:34:31 +02001242 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001243 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1244 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001245 */
1246 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1247 goto missing_data_or_waiting;
1248
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001249 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001250
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001251 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001252 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1253
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001254 /* other states, ENDING...TUNNEL */
1255 if (msg->msg_state >= HTTP_MSG_DONE)
1256 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001257
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001258 if (HAS_REQ_DATA_FILTERS(s)) {
1259 ret = flt_http_end(s, msg);
1260 if (ret <= 0) {
1261 if (!ret)
1262 goto missing_data_or_waiting;
1263 goto return_bad_req;
1264 }
1265 }
1266
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001267 if (txn->meth == HTTP_METH_CONNECT)
1268 msg->msg_state = HTTP_MSG_TUNNEL;
1269 else {
1270 msg->msg_state = HTTP_MSG_DONE;
1271 req->to_forward = 0;
1272 }
1273
1274 done:
1275 /* we don't want to forward closes on DONE except in tunnel mode. */
1276 if (!(txn->flags & TX_CON_WANT_TUN))
1277 channel_dont_close(req);
1278
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001279 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001280 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001281 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001282 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1283 if (req->flags & CF_SHUTW) {
1284 /* request errors are most likely due to the
1285 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001286 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001287 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001288 goto return_bad_req;
1289 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001290 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001291 return 1;
1292 }
1293
1294 /* If "option abortonclose" is set on the backend, we want to monitor
1295 * the client's connection and forward any shutdown notification to the
1296 * server, which will decide whether to close or to go on processing the
1297 * request. We only do that in tunnel mode, and not in other modes since
1298 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001299 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001300 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001301 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001302 s->si[1].flags |= SI_FL_NOLINGER;
1303 channel_auto_close(req);
1304 }
1305 else if (s->txn->meth == HTTP_METH_POST) {
1306 /* POST requests may require to read extra CRLF sent by broken
1307 * browsers and which could cause an RST to be sent upon close
1308 * on some systems (eg: Linux). */
1309 channel_auto_read(req);
1310 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001311 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1312 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001313 return 0;
1314
1315 missing_data_or_waiting:
1316 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001317 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001318 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001319
1320 waiting:
1321 /* waiting for the last bits to leave the buffer */
1322 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001323 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001324
1325 /* When TE: chunked is used, we need to get there again to parse remaining
1326 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1327 * And when content-length is used, we never want to let the possible
1328 * shutdown be forwarded to the other side, as the state machine will
1329 * take care of it once the client responds. It's also important to
1330 * prevent TIME_WAITs from accumulating on the backend side, and for
1331 * HTTP/2 where the last frame comes with a shutdown.
1332 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001333 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001334 channel_dont_close(req);
1335
1336 /* We know that more data are expected, but we couldn't send more that
1337 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1338 * system knows it must not set a PUSH on this first part. Interactive
1339 * modes are already handled by the stream sock layer. We must not do
1340 * this in content-length mode because it could present the MSG_MORE
1341 * flag with the last block of forwarded data, which would cause an
1342 * additional delay to be observed by the receiver.
1343 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001344 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001345 req->flags |= CF_EXPECT_MORE;
1346
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001347 DBG_TRACE_DEVEL("waiting for more data to forward",
1348 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001349 return 0;
1350
Christopher Faulet93e02d82019-03-08 14:18:50 +01001351 return_cli_abort:
1352 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1353 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001354 if (sess->listener->counters)
1355 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001356 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001357 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001358 if (!(s->flags & SF_ERR_MASK))
1359 s->flags |= SF_ERR_CLICL;
1360 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001361 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001362
1363 return_srv_abort:
1364 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1365 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001366 if (sess->listener->counters)
1367 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001368 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001369 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001370 if (!(s->flags & SF_ERR_MASK))
1371 s->flags |= SF_ERR_SRVCL;
1372 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001373 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001374
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001375 return_int_err:
1376 if (!(s->flags & SF_ERR_MASK))
1377 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001378 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001379 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001380 if (sess->listener->counters)
1381 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001382 if (objt_server(s->target))
1383 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001384 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001385 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001386
Christopher Faulet93e02d82019-03-08 14:18:50 +01001387 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001388 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001389 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001390 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001391 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001392 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001393
Christopher Fauletb8a53712019-12-16 11:29:38 +01001394 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001395 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001396 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001397 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001398 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001399 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001400 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001401 }
1402 req->analysers &= AN_REQ_FLT_END;
1403 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 +01001404 if (!(s->flags & SF_ERR_MASK))
1405 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001406 if (!(s->flags & SF_FINST_MASK))
1407 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001408 DBG_TRACE_DEVEL("leaving on error ",
1409 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001410 return 0;
1411}
1412
Olivier Houcharda254a372019-04-05 15:30:12 +02001413/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1414/* Returns 0 if we can attempt to retry, -1 otherwise */
1415static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1416{
1417 struct channel *req, *res;
1418 int co_data;
1419
1420 si->conn_retries--;
1421 if (si->conn_retries < 0)
1422 return -1;
1423
Willy Tarreau223995e2019-05-04 10:38:31 +02001424 if (objt_server(s->target))
1425 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1426 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1427
Olivier Houcharda254a372019-04-05 15:30:12 +02001428 req = &s->req;
1429 res = &s->res;
1430 /* Remove any write error from the request, and read error from the response */
1431 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1432 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1433 res->analysers = 0;
1434 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001435 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001436 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001437 si->exp = TICK_ETERNITY;
1438 res->rex = TICK_ETERNITY;
1439 res->to_forward = 0;
1440 res->analyse_exp = TICK_ETERNITY;
1441 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001442 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001443 si_release_endpoint(&s->si[1]);
1444 b_free(&req->buf);
1445 /* Swap the L7 buffer with the channel buffer */
1446 /* We know we stored the co_data as b_data, so get it there */
1447 co_data = b_data(&si->l7_buffer);
1448 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1449 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1450
1451 co_set_data(req, co_data);
1452 b_reset(&res->buf);
1453 co_set_data(res, 0);
1454 return 0;
1455}
1456
Christopher Faulete0768eb2018-10-03 16:38:02 +02001457/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1458 * processing can continue on next analysers, or zero if it either needs more
1459 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1460 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1461 * when it has nothing left to do, and may remove any analyser when it wants to
1462 * abort.
1463 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001464int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001465{
Christopher Faulet9768c262018-10-22 09:34:31 +02001466 /*
1467 * We will analyze a complete HTTP response to check the its syntax.
1468 *
1469 * Once the start line and all headers are received, we may perform a
1470 * capture of the error (if any), and we will set a few fields. We also
1471 * logging and finally headers capture.
1472 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001473 struct session *sess = s->sess;
1474 struct http_txn *txn = s->txn;
1475 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001476 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001477 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001478 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001479 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001480 int n;
1481
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001482 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001483
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001484 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001485
Willy Tarreau4236f032019-03-05 10:43:32 +01001486 /* Parsing errors are caught here */
1487 if (htx->flags & HTX_FL_PARSING_ERROR)
1488 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001489 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1490 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001491
Christopher Faulete0768eb2018-10-03 16:38:02 +02001492 /*
1493 * Now we quickly check if we have found a full valid response.
1494 * If not so, we check the FD and buffer states before leaving.
1495 * A full response is indicated by the fact that we have seen
1496 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1497 * responses are checked first.
1498 *
1499 * Depending on whether the client is still there or not, we
1500 * may send an error response back or not. Note that normally
1501 * we should only check for HTTP status there, and check I/O
1502 * errors somewhere else.
1503 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001504 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001505 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001506 /* 1: have we encountered a read error ? */
1507 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001508 struct connection *conn = NULL;
1509
Olivier Houchard865d8392019-05-03 22:46:27 +02001510 if (objt_cs(s->si[1].end))
1511 conn = objt_cs(s->si[1].end)->conn;
1512
1513 if (si_b->flags & SI_FL_L7_RETRY &&
1514 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001515 /* If we arrive here, then CF_READ_ERROR was
1516 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001517 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001518 * the SI_FL_L7_RETRY flag, so it's ok not
1519 * to check s->be->retry_type.
1520 */
1521 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1522 return 0;
1523 }
1524
Olivier Houchard6db16992019-05-17 15:40:49 +02001525 if (txn->flags & TX_NOT_FIRST)
1526 goto abort_keep_alive;
1527
Olivier Houcharda798bf52019-03-08 18:52:00 +01001528 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001529 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001530 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001531 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001532 }
1533
Christopher Faulete0768eb2018-10-03 16:38:02 +02001534 rep->analysers &= AN_RES_FLT_END;
1535 txn->status = 502;
1536
1537 /* Check to see if the server refused the early data.
1538 * If so, just send a 425
1539 */
Willy Tarreauee99aaf2020-06-23 05:58:20 +02001540 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001541 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001542 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001543 do_l7_retry(s, si_b) == 0) {
1544 DBG_TRACE_DEVEL("leaving on L7 retry",
1545 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001546 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001547 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001548 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001549 }
1550
1551 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001552 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001553
1554 if (!(s->flags & SF_ERR_MASK))
1555 s->flags |= SF_ERR_SRVCL;
1556 if (!(s->flags & SF_FINST_MASK))
1557 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001558 DBG_TRACE_DEVEL("leaving on error",
1559 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001560 return 0;
1561 }
1562
Christopher Faulet9768c262018-10-22 09:34:31 +02001563 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001564 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001565 if ((si_b->flags & SI_FL_L7_RETRY) &&
1566 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001567 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1568 DBG_TRACE_DEVEL("leaving on L7 retry",
1569 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001570 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001571 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001572 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001573 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001574 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001575 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001576 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001577 }
1578
Christopher Faulete0768eb2018-10-03 16:38:02 +02001579 rep->analysers &= AN_RES_FLT_END;
1580 txn->status = 504;
1581 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001582 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001583
1584 if (!(s->flags & SF_ERR_MASK))
1585 s->flags |= SF_ERR_SRVTO;
1586 if (!(s->flags & SF_FINST_MASK))
1587 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001588 DBG_TRACE_DEVEL("leaving on error",
1589 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001590 return 0;
1591 }
1592
Christopher Faulet9768c262018-10-22 09:34:31 +02001593 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001594 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001595 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1596 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001597 if (sess->listener->counters)
1598 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001599 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001600 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001601
1602 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001603 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001604 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001605
1606 if (!(s->flags & SF_ERR_MASK))
1607 s->flags |= SF_ERR_CLICL;
1608 if (!(s->flags & SF_FINST_MASK))
1609 s->flags |= SF_FINST_H;
1610
1611 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001612 DBG_TRACE_DEVEL("leaving on error",
1613 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001614 return 0;
1615 }
1616
Christopher Faulet9768c262018-10-22 09:34:31 +02001617 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001618 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001619 if ((si_b->flags & SI_FL_L7_RETRY) &&
1620 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001621 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1622 DBG_TRACE_DEVEL("leaving on L7 retry",
1623 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001624 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001625 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001626 }
1627
Olivier Houchard6db16992019-05-17 15:40:49 +02001628 if (txn->flags & TX_NOT_FIRST)
1629 goto abort_keep_alive;
1630
Olivier Houcharda798bf52019-03-08 18:52:00 +01001631 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001632 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001633 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001634 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001635 }
1636
Christopher Faulete0768eb2018-10-03 16:38:02 +02001637 rep->analysers &= AN_RES_FLT_END;
1638 txn->status = 502;
1639 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001640 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001641
1642 if (!(s->flags & SF_ERR_MASK))
1643 s->flags |= SF_ERR_SRVCL;
1644 if (!(s->flags & SF_FINST_MASK))
1645 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001646 DBG_TRACE_DEVEL("leaving on error",
1647 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001648 return 0;
1649 }
1650
Christopher Faulet9768c262018-10-22 09:34:31 +02001651 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001652 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001653 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001654 goto abort_keep_alive;
1655
Olivier Houcharda798bf52019-03-08 18:52:00 +01001656 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001657 if (objt_server(s->target))
1658 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001659 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001660
1661 if (!(s->flags & SF_ERR_MASK))
1662 s->flags |= SF_ERR_CLICL;
1663 if (!(s->flags & SF_FINST_MASK))
1664 s->flags |= SF_FINST_H;
1665
1666 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001667 DBG_TRACE_DEVEL("leaving on error",
1668 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001669 return 0;
1670 }
1671
1672 channel_dont_close(rep);
1673 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001674 DBG_TRACE_DEVEL("waiting for more data",
1675 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001676 return 0;
1677 }
1678
1679 /* More interesting part now : we know that we have a complete
1680 * response which at least looks like HTTP. We have an indicator
1681 * of each header's length, so we can parse them quickly.
1682 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001683 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001684 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001685 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001686
Christopher Faulet9768c262018-10-22 09:34:31 +02001687 /* 0: we might have to print this header in debug mode */
1688 if (unlikely((global.mode & MODE_DEBUG) &&
1689 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1690 int32_t pos;
1691
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001692 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001693
Christopher Fauleta3f15502019-05-13 15:27:23 +02001694 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001695 struct htx_blk *blk = htx_get_blk(htx, pos);
1696 enum htx_blk_type type = htx_get_blk_type(blk);
1697
1698 if (type == HTX_BLK_EOH)
1699 break;
1700 if (type != HTX_BLK_HDR)
1701 continue;
1702
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001703 http_debug_hdr("srvhdr", s,
1704 htx_get_blk_name(htx, blk),
1705 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001706 }
1707 }
1708
Christopher Faulet03599112018-11-27 11:21:21 +01001709 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001710 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001711 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001712 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001713 if (sl->flags & HTX_SL_F_XFER_LEN) {
1714 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001715 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001716 if (sl->flags & HTX_SL_F_BODYLESS)
1717 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001718 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001719
1720 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001721 if (n < 1 || n > 5)
1722 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001723
Christopher Faulete0768eb2018-10-03 16:38:02 +02001724 /* when the client triggers a 4xx from the server, it's most often due
1725 * to a missing object or permission. These events should be tracked
1726 * because if they happen often, it may indicate a brute force or a
1727 * vulnerability scan.
1728 */
1729 if (n == 4)
1730 stream_inc_http_err_ctr(s);
1731
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001732 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001733 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001734 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1735 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001736
Christopher Faulete0768eb2018-10-03 16:38:02 +02001737 /* Adjust server's health based on status code. Note: status codes 501
1738 * and 505 are triggered on demand by client request, so we must not
1739 * count them as server failures.
1740 */
1741 if (objt_server(s->target)) {
1742 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001743 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001744 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001745 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001746 }
1747
1748 /*
1749 * We may be facing a 100-continue response, or any other informational
1750 * 1xx response which is non-final, in which case this is not the right
1751 * response, and we're waiting for the next one. Let's allow this response
1752 * to go to the client and wait for the next one. There's an exception for
1753 * 101 which is used later in the code to switch protocols.
1754 */
1755 if (txn->status < 200 &&
1756 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001757 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001758 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001759 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001760 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001761 txn->status = 0;
1762 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001763 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001764 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001765 }
1766
1767 /*
1768 * 2: check for cacheability.
1769 */
1770
1771 switch (txn->status) {
1772 case 200:
1773 case 203:
1774 case 204:
1775 case 206:
1776 case 300:
1777 case 301:
1778 case 404:
1779 case 405:
1780 case 410:
1781 case 414:
1782 case 501:
1783 break;
1784 default:
1785 /* RFC7231#6.1:
1786 * Responses with status codes that are defined as
1787 * cacheable by default (e.g., 200, 203, 204, 206,
1788 * 300, 301, 404, 405, 410, 414, and 501 in this
1789 * specification) can be reused by a cache with
1790 * heuristic expiration unless otherwise indicated
1791 * by the method definition or explicit cache
1792 * controls [RFC7234]; all other status codes are
1793 * not cacheable by default.
1794 */
1795 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1796 break;
1797 }
1798
1799 /*
1800 * 3: we may need to capture headers
1801 */
1802 s->logs.logwait &= ~LW_RESP;
1803 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001804 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001805
Christopher Faulet9768c262018-10-22 09:34:31 +02001806 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001807 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1808 txn->status == 101)) {
1809 /* Either we've established an explicit tunnel, or we're
1810 * switching the protocol. In both cases, we're very unlikely
1811 * to understand the next protocols. We have to switch to tunnel
1812 * mode, so that we transfer the request and responses then let
1813 * this protocol pass unmodified. When we later implement specific
1814 * parsers for such protocols, we'll want to check the Upgrade
1815 * header which contains information about that protocol for
1816 * responses with status 101 (eg: see RFC2817 about TLS).
1817 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001818 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001819 }
1820
Christopher Faulet61608322018-11-23 16:23:45 +01001821 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1822 * 407 (Proxy-Authenticate) responses and set the connection to private
1823 */
1824 srv_conn = cs_conn(objt_cs(s->si[1].end));
1825 if (srv_conn) {
1826 struct ist hdr;
1827 struct http_hdr_ctx ctx;
1828
1829 if (txn->status == 401)
1830 hdr = ist("WWW-Authenticate");
1831 else if (txn->status == 407)
1832 hdr = ist("Proxy-Authenticate");
1833 else
1834 goto end;
1835
1836 ctx.blk = NULL;
1837 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001838 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1839 * possibly followed by blanks and a base64 string, the connection
1840 * is private. Since it's a mess to deal with, we only check for
1841 * values starting with "NTLM" or "Nego". Note that often multiple
1842 * headers are sent by the server there.
1843 */
1844 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001845 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001846 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001847 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001848 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001849 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001850 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001851 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001852 }
Christopher Faulet61608322018-11-23 16:23:45 +01001853 }
1854 }
1855
1856 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001857 /* we want to have the response time before we start processing it */
1858 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1859
1860 /* end of job, return OK */
1861 rep->analysers &= ~an_bit;
1862 rep->analyse_exp = TICK_ETERNITY;
1863 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001864 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001865 return 1;
1866
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001867 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001868 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001869 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001870 if (sess->listener->counters)
1871 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001872 if (objt_server(s->target))
1873 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001874 txn->status = 500;
1875 if (!(s->flags & SF_ERR_MASK))
1876 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001877 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001878
1879 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001880 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001881 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001882 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001883 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001884 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001885 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001886 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001887 do_l7_retry(s, si_b) == 0) {
1888 DBG_TRACE_DEVEL("leaving on L7 retry",
1889 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001890 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001891 }
Christopher Faulet47365272018-10-31 17:40:50 +01001892 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001893 /* fall through */
1894
Christopher Fauletb8a53712019-12-16 11:29:38 +01001895 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001896 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001897
1898 if (!(s->flags & SF_ERR_MASK))
1899 s->flags |= SF_ERR_PRXCOND;
1900 if (!(s->flags & SF_FINST_MASK))
1901 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001902
1903 s->si[1].flags |= SI_FL_NOLINGER;
1904 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001905 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001906 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001907 DBG_TRACE_DEVEL("leaving on error",
1908 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001909 return 0;
1910
Christopher Faulete0768eb2018-10-03 16:38:02 +02001911 abort_keep_alive:
1912 /* A keep-alive request to the server failed on a network error.
1913 * The client is required to retry. We need to close without returning
1914 * any other information so that the client retries.
1915 */
1916 txn->status = 0;
1917 rep->analysers &= AN_RES_FLT_END;
1918 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001919 s->logs.logwait = 0;
1920 s->logs.level = 0;
1921 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001922 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001923 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1924 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001925 return 0;
1926}
1927
1928/* This function performs all the processing enabled for the current response.
1929 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1930 * and updates s->res.analysers. It might make sense to explode it into several
1931 * other functions. It works like process_request (see indications above).
1932 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001933int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001934{
1935 struct session *sess = s->sess;
1936 struct http_txn *txn = s->txn;
1937 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001938 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001939 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001940 enum rule_result ret = HTTP_RULE_RES_CONT;
1941
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001942 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1943 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001944
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001945 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001946
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001947 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001948
1949 /* The stats applet needs to adjust the Connection header but we don't
1950 * apply any filter there.
1951 */
1952 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1953 rep->analysers &= ~an_bit;
1954 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001955 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001956 }
1957
1958 /*
1959 * We will have to evaluate the filters.
1960 * As opposed to version 1.2, now they will be evaluated in the
1961 * filters order and not in the header order. This means that
1962 * each filter has to be validated among all headers.
1963 *
1964 * Filters are tried with ->be first, then with ->fe if it is
1965 * different from ->be.
1966 *
1967 * Maybe we are in resume condiion. In this case I choose the
1968 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001969 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001970 * the process with the first one.
1971 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001972 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001973 * pointer and the ->fe rule list. If it doesn't match, I initialize
1974 * the loop with the ->be.
1975 */
1976 if (s->current_rule_list == &sess->fe->http_res_rules)
1977 cur_proxy = sess->fe;
1978 else
1979 cur_proxy = s->be;
1980 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001981 /* evaluate http-response rules */
1982 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001983 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001984
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001985 switch (ret) {
1986 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1987 goto return_prx_yield;
1988
1989 case HTTP_RULE_RES_CONT:
1990 case HTTP_RULE_RES_STOP: /* nothing to do */
1991 break;
1992
1993 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1994 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001995
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001996 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1997 goto return_prx_cond;
1998
1999 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01002000 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002001
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002002 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2003 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002004
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002005 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
2006 goto return_int_err;
2007 }
2008
2009 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002010
Christopher Faulete0768eb2018-10-03 16:38:02 +02002011 /* check whether we're already working on the frontend */
2012 if (cur_proxy == sess->fe)
2013 break;
2014 cur_proxy = sess->fe;
2015 }
2016
Christopher Faulete0768eb2018-10-03 16:38:02 +02002017 /* OK that's all we can do for 1xx responses */
2018 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002019 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002020
2021 /*
2022 * Now check for a server cookie.
2023 */
2024 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002025 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002026
2027 /*
2028 * Check for cache-control or pragma headers if required.
2029 */
2030 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002031 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002032
2033 /*
2034 * Add server cookie in the response if needed
2035 */
2036 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
2037 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
2038 (!(s->flags & SF_DIRECT) ||
2039 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
2040 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
2041 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
2042 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
2043 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
2044 !(s->flags & SF_IGNORE_PRST)) {
2045 /* the server is known, it's not the one the client requested, or the
2046 * cookie's last seen date needs to be refreshed. We have to
2047 * insert a set-cookie here, except if we want to insert only on POST
2048 * requests and this one isn't. Note that servers which don't have cookies
2049 * (eg: some backup servers) will return a full cookie removal request.
2050 */
2051 if (!objt_server(s->target)->cookie) {
2052 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002053 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02002054 s->be->cookie_name);
2055 }
2056 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002057 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002058
2059 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2060 /* emit last_date, which is mandatory */
2061 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2062 s30tob64((date.tv_sec+3) >> 2,
2063 trash.area + trash.data);
2064 trash.data += 5;
2065
2066 if (s->be->cookie_maxlife) {
2067 /* emit first_date, which is either the original one or
2068 * the current date.
2069 */
2070 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2071 s30tob64(txn->cookie_first_date ?
2072 txn->cookie_first_date >> 2 :
2073 (date.tv_sec+3) >> 2,
2074 trash.area + trash.data);
2075 trash.data += 5;
2076 }
2077 }
2078 chunk_appendf(&trash, "; path=/");
2079 }
2080
2081 if (s->be->cookie_domain)
2082 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2083
2084 if (s->be->ck_opts & PR_CK_HTTPONLY)
2085 chunk_appendf(&trash, "; HttpOnly");
2086
2087 if (s->be->ck_opts & PR_CK_SECURE)
2088 chunk_appendf(&trash, "; Secure");
2089
Christopher Faulet2f533902020-01-21 11:06:48 +01002090 if (s->be->cookie_attrs)
2091 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
2092
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002093 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002094 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002095
2096 txn->flags &= ~TX_SCK_MASK;
2097 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2098 /* the server did not change, only the date was updated */
2099 txn->flags |= TX_SCK_UPDATED;
2100 else
2101 txn->flags |= TX_SCK_INSERTED;
2102
2103 /* Here, we will tell an eventual cache on the client side that we don't
2104 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2105 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2106 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2107 */
2108 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2109
2110 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2111
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002112 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002113 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002114 }
2115 }
2116
2117 /*
2118 * Check if result will be cacheable with a cookie.
2119 * We'll block the response if security checks have caught
2120 * nasty things such as a cacheable cookie.
2121 */
2122 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2123 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2124 (s->be->options & PR_O_CHK_CACHE)) {
2125 /* we're in presence of a cacheable response containing
2126 * a set-cookie header. We'll block it as requested by
2127 * the 'checkcache' option, and send an alert.
2128 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002129 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2130 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2131 send_log(s->be, LOG_ALERT,
2132 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2133 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002134 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002135 }
2136
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002137 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002138 /*
2139 * Evaluate after-response rules before forwarding the response. rules
2140 * from the backend are evaluated first, then one from the frontend if
2141 * it differs.
2142 */
2143 if (!http_eval_after_res_rules(s))
2144 goto return_int_err;
2145
Christopher Faulete0768eb2018-10-03 16:38:02 +02002146 /* Always enter in the body analyzer */
2147 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2148 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2149
2150 /* if the user wants to log as soon as possible, without counting
2151 * bytes from the server, then this is the right moment. We have
2152 * to temporarily assign bytes_out to log what we currently have.
2153 */
2154 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2155 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002156 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002157 s->do_log(s);
2158 s->logs.bytes_out = 0;
2159 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002160
Christopher Fauletb8a53712019-12-16 11:29:38 +01002161 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002162 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002163 rep->analysers &= ~an_bit;
2164 rep->analyse_exp = TICK_ETERNITY;
2165 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002166
Christopher Fauletb8a53712019-12-16 11:29:38 +01002167 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002168 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002169 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002170 if (sess->listener->counters)
2171 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002172 if (objt_server(s->target))
2173 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002174 goto return_prx_err;
2175
2176 return_int_err:
2177 txn->status = 500;
2178 if (!(s->flags & SF_ERR_MASK))
2179 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002180 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002181 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2182 if (objt_server(s->target))
2183 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002184 if (objt_server(s->target))
2185 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002186 goto return_prx_err;
2187
2188 return_bad_res:
2189 txn->status = 502;
Christopher Fauleta20a6532020-02-05 10:16:41 +01002190 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2191 if (objt_server(s->target)) {
2192 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2193 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2194 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002195 /* fall through */
2196
2197 return_prx_err:
2198 http_reply_and_close(s, txn->status, http_error_message(s));
2199 /* fall through */
2200
2201 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002202 s->logs.t_data = -1; /* was not a valid response */
2203 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002204
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002205 if (!(s->flags & SF_ERR_MASK))
2206 s->flags |= SF_ERR_PRXCOND;
2207 if (!(s->flags & SF_FINST_MASK))
2208 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002209
Christopher Faulete58c0002020-03-02 16:21:01 +01002210 rep->analysers &= AN_RES_FLT_END;
2211 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002212 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002213 DBG_TRACE_DEVEL("leaving on error",
2214 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002215 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002216
2217 return_prx_yield:
2218 channel_dont_close(rep);
2219 DBG_TRACE_DEVEL("waiting for more data",
2220 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2221 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002222}
2223
2224/* This function is an analyser which forwards response body (including chunk
2225 * sizes if any). It is called as soon as we must forward, even if we forward
2226 * zero byte. The only situation where it must not be called is when we're in
2227 * tunnel mode and we want to forward till the close. It's used both to forward
2228 * remaining data and to resync after end of body. It expects the msg_state to
2229 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2230 * read more data, or 1 once we can go on with next request or end the stream.
2231 *
2232 * It is capable of compressing response data both in content-length mode and
2233 * in chunked mode. The state machines follows different flows depending on
2234 * whether content-length and chunked modes are used, since there are no
2235 * trailers in content-length :
2236 *
2237 * chk-mode cl-mode
2238 * ,----- BODY -----.
2239 * / \
2240 * V size > 0 V chk-mode
2241 * .--> SIZE -------------> DATA -------------> CRLF
2242 * | | size == 0 | last byte |
2243 * | v final crlf v inspected |
2244 * | TRAILERS -----------> DONE |
2245 * | |
2246 * `----------------------------------------------'
2247 *
2248 * Compression only happens in the DATA state, and must be flushed in final
2249 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2250 * is performed at once on final states for all bytes parsed, or when leaving
2251 * on missing data.
2252 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002253int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002254{
2255 struct session *sess = s->sess;
2256 struct http_txn *txn = s->txn;
2257 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002258 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002259 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002260
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002261 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002262
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002263 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002264
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002265 if (htx->flags & HTX_FL_PARSING_ERROR)
2266 goto return_bad_res;
2267 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2268 goto return_int_err;
2269
Christopher Faulete0768eb2018-10-03 16:38:02 +02002270 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002271 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002272 /* Output closed while we were sending data. We must abort and
2273 * wake the other side up.
2274 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002275 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002276 http_end_response(s);
2277 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002278 DBG_TRACE_DEVEL("leaving on error",
2279 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002280 return 1;
2281 }
2282
Christopher Faulet9768c262018-10-22 09:34:31 +02002283 if (msg->msg_state == HTTP_MSG_BODY)
2284 msg->msg_state = HTTP_MSG_DATA;
2285
Christopher Faulete0768eb2018-10-03 16:38:02 +02002286 /* in most states, we should abort in case of early close */
2287 channel_auto_close(res);
2288
Christopher Faulete0768eb2018-10-03 16:38:02 +02002289 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002290 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002291 if (res->flags & CF_EOI)
2292 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002293 }
2294 else {
2295 /* We can't process the buffer's contents yet */
2296 res->flags |= CF_WAKE_WRITE;
2297 goto missing_data_or_waiting;
2298 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002299 }
2300
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002301 if (msg->msg_state >= HTTP_MSG_ENDING)
2302 goto ending;
2303
2304 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2305 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2306 msg->msg_state = HTTP_MSG_ENDING;
2307 goto ending;
2308 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002309
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002310 /* Forward input data. We get it by removing all outgoing data not
2311 * forwarded yet from HTX data size. If there are some data filters, we
2312 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002313 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002314 if (HAS_RSP_DATA_FILTERS(s)) {
2315 ret = flt_http_payload(s, msg, htx->data);
2316 if (ret < 0)
2317 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002318 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002319 }
2320 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002321 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002322 if (msg->flags & HTTP_MSGF_XFER_LEN)
2323 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002324 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002325
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002326 if (htx->data != co_data(res))
2327 goto missing_data_or_waiting;
2328
2329 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2330 msg->msg_state = HTTP_MSG_ENDING;
2331 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002332 }
2333
Christopher Faulet9768c262018-10-22 09:34:31 +02002334 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002335 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2336 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002337 */
2338 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2339 goto missing_data_or_waiting;
2340
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002341 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002342
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002343 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002344 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2345
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002346 /* other states, ENDING...TUNNEL */
2347 if (msg->msg_state >= HTTP_MSG_DONE)
2348 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002349
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002350 if (HAS_RSP_DATA_FILTERS(s)) {
2351 ret = flt_http_end(s, msg);
2352 if (ret <= 0) {
2353 if (!ret)
2354 goto missing_data_or_waiting;
2355 goto return_bad_res;
2356 }
2357 }
2358
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002359 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2360 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2361 msg->msg_state = HTTP_MSG_TUNNEL;
2362 goto ending;
2363 }
2364 else {
2365 msg->msg_state = HTTP_MSG_DONE;
2366 res->to_forward = 0;
2367 }
2368
2369 done:
2370
2371 channel_dont_close(res);
2372
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002373 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002374 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002375 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002376 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2377 if (res->flags & CF_SHUTW) {
2378 /* response errors are most likely due to the
2379 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002380 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002381 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002382 goto return_bad_res;
2383 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002384 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002385 return 1;
2386 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002387 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2388 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002389 return 0;
2390
2391 missing_data_or_waiting:
2392 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002393 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002394
2395 /* stop waiting for data if the input is closed before the end. If the
2396 * client side was already closed, it means that the client has aborted,
2397 * so we don't want to count this as a server abort. Otherwise it's a
2398 * server abort.
2399 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002400 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002401 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002402 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002403 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002404 if (htx_is_empty(htx))
2405 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002406 }
2407
Christopher Faulete0768eb2018-10-03 16:38:02 +02002408 /* When TE: chunked is used, we need to get there again to parse
2409 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002410 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2411 * are filters registered on the stream, we don't want to forward a
2412 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002413 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002414 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002415 channel_dont_close(res);
2416
2417 /* We know that more data are expected, but we couldn't send more that
2418 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2419 * system knows it must not set a PUSH on this first part. Interactive
2420 * modes are already handled by the stream sock layer. We must not do
2421 * this in content-length mode because it could present the MSG_MORE
2422 * flag with the last block of forwarded data, which would cause an
2423 * additional delay to be observed by the receiver.
2424 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002425 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002426 res->flags |= CF_EXPECT_MORE;
2427
2428 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002429 DBG_TRACE_DEVEL("waiting for more data to forward",
2430 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002431 return 0;
2432
Christopher Faulet93e02d82019-03-08 14:18:50 +01002433 return_srv_abort:
2434 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2435 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002436 if (sess->listener->counters)
2437 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002438 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002439 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002440 if (!(s->flags & SF_ERR_MASK))
2441 s->flags |= SF_ERR_SRVCL;
2442 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002443
Christopher Faulet93e02d82019-03-08 14:18:50 +01002444 return_cli_abort:
2445 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2446 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002447 if (sess->listener->counters)
2448 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002449 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002450 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002451 if (!(s->flags & SF_ERR_MASK))
2452 s->flags |= SF_ERR_CLICL;
2453 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002454
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002455 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002456 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002457 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002458 if (sess->listener->counters)
2459 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002460 if (objt_server(s->target))
2461 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002462 if (!(s->flags & SF_ERR_MASK))
2463 s->flags |= SF_ERR_INTERNAL;
2464 goto return_error;
2465
Christopher Faulet93e02d82019-03-08 14:18:50 +01002466 return_bad_res:
2467 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2468 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002469 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002470 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2471 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002472 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002473 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002474 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002475
Christopher Faulet93e02d82019-03-08 14:18:50 +01002476 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002477 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002478 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002479 res->analysers &= AN_RES_FLT_END;
2480 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 +02002481 if (!(s->flags & SF_FINST_MASK))
2482 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002483 DBG_TRACE_DEVEL("leaving on error",
2484 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002485 return 0;
2486}
2487
Christopher Fauletf2824e62018-10-01 12:12:37 +02002488/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002489 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002490 * as too large a request to build a valid response.
2491 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002492int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002493{
Christopher Faulet99daf282018-11-28 22:58:13 +01002494 struct channel *req = &s->req;
2495 struct channel *res = &s->res;
2496 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002497 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002498 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002499 struct ist status, reason, location;
2500 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002501 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002502
2503 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002504 if (!chunk) {
2505 if (!(s->flags & SF_ERR_MASK))
2506 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002507 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002508 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002509
Christopher Faulet99daf282018-11-28 22:58:13 +01002510 /*
2511 * Create the location
2512 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002513 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002514 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002515 case REDIRECT_TYPE_SCHEME: {
2516 struct http_hdr_ctx ctx;
2517 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002518
Christopher Faulet99daf282018-11-28 22:58:13 +01002519 host = ist("");
2520 ctx.blk = NULL;
2521 if (http_find_header(htx, ist("Host"), &ctx, 0))
2522 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002523
Christopher Faulet297fbb42019-05-13 14:41:27 +02002524 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002525 path = http_get_path(htx_sl_req_uri(sl));
2526 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002527 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002528 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2529 int qs = 0;
2530 while (qs < path.len) {
2531 if (*(path.ptr + qs) == '?') {
2532 path.len = qs;
2533 break;
2534 }
2535 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002536 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002537 }
2538 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002539 else
2540 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002541
Christopher Faulet99daf282018-11-28 22:58:13 +01002542 if (rule->rdr_str) { /* this is an old "redirect" rule */
2543 /* add scheme */
2544 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2545 goto fail;
2546 }
2547 else {
2548 /* add scheme with executing log format */
2549 chunk->data += build_logline(s, chunk->area + chunk->data,
2550 chunk->size - chunk->data,
2551 &rule->rdr_fmt);
2552 }
2553 /* add "://" + host + path */
2554 if (!chunk_memcat(chunk, "://", 3) ||
2555 !chunk_memcat(chunk, host.ptr, host.len) ||
2556 !chunk_memcat(chunk, path.ptr, path.len))
2557 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002558
Christopher Faulet99daf282018-11-28 22:58:13 +01002559 /* append a slash at the end of the location if needed and missing */
2560 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2561 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2562 if (chunk->data + 1 >= chunk->size)
2563 goto fail;
2564 chunk->area[chunk->data++] = '/';
2565 }
2566 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002567 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002568
Christopher Faulet99daf282018-11-28 22:58:13 +01002569 case REDIRECT_TYPE_PREFIX: {
2570 struct ist path;
2571
Christopher Faulet297fbb42019-05-13 14:41:27 +02002572 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002573 path = http_get_path(htx_sl_req_uri(sl));
2574 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002575 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002576 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2577 int qs = 0;
2578 while (qs < path.len) {
2579 if (*(path.ptr + qs) == '?') {
2580 path.len = qs;
2581 break;
2582 }
2583 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002584 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002585 }
2586 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002587 else
2588 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002589
Christopher Faulet99daf282018-11-28 22:58:13 +01002590 if (rule->rdr_str) { /* this is an old "redirect" rule */
2591 /* add prefix. Note that if prefix == "/", we don't want to
2592 * add anything, otherwise it makes it hard for the user to
2593 * configure a self-redirection.
2594 */
2595 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2596 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2597 goto fail;
2598 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002599 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002600 else {
2601 /* add prefix with executing log format */
2602 chunk->data += build_logline(s, chunk->area + chunk->data,
2603 chunk->size - chunk->data,
2604 &rule->rdr_fmt);
2605 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002606
Christopher Faulet99daf282018-11-28 22:58:13 +01002607 /* add path */
2608 if (!chunk_memcat(chunk, path.ptr, path.len))
2609 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002610
Christopher Faulet99daf282018-11-28 22:58:13 +01002611 /* append a slash at the end of the location if needed and missing */
2612 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2613 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2614 if (chunk->data + 1 >= chunk->size)
2615 goto fail;
2616 chunk->area[chunk->data++] = '/';
2617 }
2618 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002619 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002620 case REDIRECT_TYPE_LOCATION:
2621 default:
2622 if (rule->rdr_str) { /* this is an old "redirect" rule */
2623 /* add location */
2624 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2625 goto fail;
2626 }
2627 else {
2628 /* add location with executing log format */
2629 chunk->data += build_logline(s, chunk->area + chunk->data,
2630 chunk->size - chunk->data,
2631 &rule->rdr_fmt);
2632 }
2633 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002634 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002635 location = ist2(chunk->area, chunk->data);
2636
2637 /*
2638 * Create the 30x response
2639 */
2640 switch (rule->code) {
2641 case 308:
2642 status = ist("308");
2643 reason = ist("Permanent Redirect");
2644 break;
2645 case 307:
2646 status = ist("307");
2647 reason = ist("Temporary Redirect");
2648 break;
2649 case 303:
2650 status = ist("303");
2651 reason = ist("See Other");
2652 break;
2653 case 301:
2654 status = ist("301");
2655 reason = ist("Moved Permanently");
2656 break;
2657 case 302:
2658 default:
2659 status = ist("302");
2660 reason = ist("Found");
2661 break;
2662 }
2663
Christopher Faulet08e66462019-05-23 16:44:59 +02002664 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2665 close = 1;
2666
Christopher Faulet99daf282018-11-28 22:58:13 +01002667 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002668 /* Trim any possible response */
2669 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002670 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2671 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2672 if (!sl)
2673 goto fail;
2674 sl->info.res.status = rule->code;
2675 s->txn->status = rule->code;
2676
Christopher Faulet08e66462019-05-23 16:44:59 +02002677 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2678 goto fail;
2679
2680 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002681 !htx_add_header(htx, ist("Location"), location))
2682 goto fail;
2683
2684 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2685 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2686 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002687 }
2688
2689 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002690 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2691 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002692 }
2693
Christopher Faulet99daf282018-11-28 22:58:13 +01002694 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2695 goto fail;
2696
Kevin Zhu96b36392020-01-07 09:42:55 +01002697 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002698 if (!http_forward_proxy_resp(s, 1))
2699 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002700
Christopher Faulet60b33a52020-01-28 09:18:10 +01002701 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2702 /* let's log the request time */
2703 s->logs.tv_request = now;
2704 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002705
Christopher Faulet60b33a52020-01-28 09:18:10 +01002706 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2707 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2708 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002709
2710 if (!(s->flags & SF_ERR_MASK))
2711 s->flags |= SF_ERR_LOCAL;
2712 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002713 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002714
Christopher Faulet99daf282018-11-28 22:58:13 +01002715 free_trash_chunk(chunk);
2716 return 1;
2717
2718 fail:
2719 /* If an error occurred, remove the incomplete HTTP response from the
2720 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002721 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002722 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002723 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002724}
2725
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002726/* Replace all headers matching the name <name>. The header value is replaced if
2727 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2728 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2729 * values are evaluated one by one. It returns 0 on success and -1 on error.
2730 */
2731int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2732 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002733{
2734 struct http_hdr_ctx ctx;
2735 struct buffer *output = get_trash_chunk();
2736
Christopher Faulet72333522018-10-24 11:25:02 +02002737 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002738 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002739 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2740 continue;
2741
2742 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2743 if (output->data == -1)
2744 return -1;
2745 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2746 return -1;
2747 }
2748 return 0;
2749}
2750
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002751/* This function executes one of the set-{method,path,query,uri} actions. It
2752 * takes the string from the variable 'replace' with length 'len', then modifies
2753 * the relevant part of the request line accordingly. Then it updates various
2754 * pointers to the next elements which were moved, and the total buffer length.
2755 * It finds the action to be performed in p[2], previously filled by function
2756 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2757 * error, though this can be revisited when this code is finally exploited.
2758 *
2759 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002760 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002761 *
2762 * In query string case, the mark question '?' must be set at the start of the
2763 * string by the caller, event if the replacement query string is empty.
2764 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002765int http_req_replace_stline(int action, const char *replace, int len,
2766 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002767{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002768 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002769
2770 switch (action) {
2771 case 0: // method
2772 if (!http_replace_req_meth(htx, ist2(replace, len)))
2773 return -1;
2774 break;
2775
2776 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002777 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002778 return -1;
2779 break;
2780
2781 case 2: // query
2782 if (!http_replace_req_query(htx, ist2(replace, len)))
2783 return -1;
2784 break;
2785
2786 case 3: // uri
2787 if (!http_replace_req_uri(htx, ist2(replace, len)))
2788 return -1;
2789 break;
2790
Christopher Faulet312294f2020-09-02 17:17:44 +02002791 case 4: // path + query
2792 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2793 return -1;
2794 break;
2795
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002796 default:
2797 return -1;
2798 }
2799 return 0;
2800}
2801
2802/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002803 * variable <status> contains the new status code. This function never fails. It
2804 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002805 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002806int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002807{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002808 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002809 char *res;
2810
2811 chunk_reset(&trash);
2812 res = ultoa_o(status, trash.area, trash.size);
2813 trash.data = res - trash.area;
2814
2815 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002816 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002817 const char *str = http_get_reason(status);
2818 reason = ist2(str, strlen(str));
2819 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002820
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002821 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002822 return -1;
2823 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002824}
2825
Christopher Faulet3e964192018-10-24 11:39:23 +02002826/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2827 * transaction <txn>. Returns the verdict of the first rule that prevents
2828 * further processing of the request (auth, deny, ...), and defaults to
2829 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2830 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2831 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2832 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2833 * status.
2834 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002835static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002836 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002837{
2838 struct session *sess = strm_sess(s);
2839 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002840 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002841 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002842 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002843
Christopher Faulet3e964192018-10-24 11:39:23 +02002844 /* If "the current_rule_list" match the executed rule list, we are in
2845 * resume condition. If a resume is needed it is always in the action
2846 * and never in the ACL or converters. In this case, we initialise the
2847 * current rule, and go to the action execution point.
2848 */
2849 if (s->current_rule) {
2850 rule = s->current_rule;
2851 s->current_rule = NULL;
2852 if (s->current_rule_list == rules)
2853 goto resume_execution;
2854 }
2855 s->current_rule_list = rules;
2856
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002857 /* start the ruleset evaluation in strict mode */
2858 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002859
Christopher Faulet3e964192018-10-24 11:39:23 +02002860 list_for_each_entry(rule, rules, list) {
2861 /* check optional condition */
2862 if (rule->cond) {
2863 int ret;
2864
2865 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2866 ret = acl_pass(ret);
2867
2868 if (rule->cond->pol == ACL_COND_UNLESS)
2869 ret = !ret;
2870
2871 if (!ret) /* condition not matched */
2872 continue;
2873 }
2874
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002875 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002876 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002877 /* Always call the action function if defined */
2878 if (rule->action_ptr) {
2879 if ((s->req.flags & CF_READ_ERROR) ||
2880 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2881 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002882 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002883
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002884 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002885 case ACT_RET_CONT:
2886 break;
2887 case ACT_RET_STOP:
2888 rule_ret = HTTP_RULE_RES_STOP;
2889 goto end;
2890 case ACT_RET_YIELD:
2891 s->current_rule = rule;
2892 rule_ret = HTTP_RULE_RES_YIELD;
2893 goto end;
2894 case ACT_RET_ERR:
2895 rule_ret = HTTP_RULE_RES_ERROR;
2896 goto end;
2897 case ACT_RET_DONE:
2898 rule_ret = HTTP_RULE_RES_DONE;
2899 goto end;
2900 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002901 if (txn->status == -1)
2902 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002903 rule_ret = HTTP_RULE_RES_DENY;
2904 goto end;
2905 case ACT_RET_ABRT:
2906 rule_ret = HTTP_RULE_RES_ABRT;
2907 goto end;
2908 case ACT_RET_INV:
2909 rule_ret = HTTP_RULE_RES_BADREQ;
2910 goto end;
2911 }
2912 continue; /* eval the next rule */
2913 }
2914
2915 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002916 switch (rule->action) {
2917 case ACT_ACTION_ALLOW:
2918 rule_ret = HTTP_RULE_RES_STOP;
2919 goto end;
2920
2921 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002922 txn->status = rule->arg.http_reply->status;
2923 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002924 rule_ret = HTTP_RULE_RES_DENY;
2925 goto end;
2926
2927 case ACT_HTTP_REQ_TARPIT:
2928 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002929 txn->status = rule->arg.http_reply->status;
2930 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002931 rule_ret = HTTP_RULE_RES_DENY;
2932 goto end;
2933
Christopher Faulet3e964192018-10-24 11:39:23 +02002934 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002935 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002936 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002937 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002938 goto end;
2939
2940 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002941 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002942 break;
2943
2944 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002945 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002946 break;
2947
2948 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002949 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002950 break;
2951
2952 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002953 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002954 break;
2955
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002956 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002957 default:
2958 break;
2959 }
2960 }
2961
2962 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002963 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002964 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002965 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002966
Christopher Faulet3e964192018-10-24 11:39:23 +02002967 /* we reached the end of the rules, nothing to report */
2968 return rule_ret;
2969}
2970
2971/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2972 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2973 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2974 * is returned, the process can continue the evaluation of next rule list. If
2975 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2976 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002977 * must be returned. If *YIELD is returned, the caller must call again the
2978 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002979 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002980static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2981 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002982{
2983 struct session *sess = strm_sess(s);
2984 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002985 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002986 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002987 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002988
Christopher Faulet3e964192018-10-24 11:39:23 +02002989 /* 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_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003090 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003091 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003092 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003093 goto end;
3094
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003095 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003096 default:
3097 break;
3098 }
3099 }
3100
3101 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003102 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003103 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003104 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003105
Christopher Faulet3e964192018-10-24 11:39:23 +02003106 /* we reached the end of the rules, nothing to report */
3107 return rule_ret;
3108}
3109
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003110/* Executes backend and frontend http-after-response rules for the stream <s>,
3111 * in that order. it return 1 on success and 0 on error. It is the caller
3112 * responsibility to catch error or ignore it. If it catches it, this function
3113 * may be called a second time, for the internal error.
3114 */
3115int http_eval_after_res_rules(struct stream *s)
3116{
3117 struct session *sess = s->sess;
3118 enum rule_result ret = HTTP_RULE_RES_CONT;
3119
Christopher Faulet507479b2020-05-15 12:29:46 +02003120 /* Eval after-response ruleset only if the reply is not const */
3121 if (s->txn->flags & TX_CONST_REPLY)
3122 goto end;
3123
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003124 /* prune the request variables if not already done and swap to the response variables. */
3125 if (s->vars_reqres.scope != SCOPE_RES) {
3126 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3127 vars_prune(&s->vars_reqres, s->sess, s);
3128 vars_init(&s->vars_reqres, SCOPE_RES);
3129 }
3130
3131 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3132 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3133 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3134
Christopher Faulet507479b2020-05-15 12:29:46 +02003135 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003136 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3137 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3138}
3139
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003140/*
3141 * Manage client-side cookie. It can impact performance by about 2% so it is
3142 * desirable to call it only when needed. This code is quite complex because
3143 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3144 * highly recommended not to touch this part without a good reason !
3145 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003146static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003147{
3148 struct session *sess = s->sess;
3149 struct http_txn *txn = s->txn;
3150 struct htx *htx;
3151 struct http_hdr_ctx ctx;
3152 char *hdr_beg, *hdr_end, *del_from;
3153 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3154 int preserve_hdr;
3155
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003156 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003157 ctx.blk = NULL;
3158 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003159 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003160 del_from = NULL; /* nothing to be deleted */
3161 preserve_hdr = 0; /* assume we may kill the whole header */
3162
3163 /* Now look for cookies. Conforming to RFC2109, we have to support
3164 * attributes whose name begin with a '$', and associate them with
3165 * the right cookie, if we want to delete this cookie.
3166 * So there are 3 cases for each cookie read :
3167 * 1) it's a special attribute, beginning with a '$' : ignore it.
3168 * 2) it's a server id cookie that we *MAY* want to delete : save
3169 * some pointers on it (last semi-colon, beginning of cookie...)
3170 * 3) it's an application cookie : we *MAY* have to delete a previous
3171 * "special" cookie.
3172 * At the end of loop, if a "special" cookie remains, we may have to
3173 * remove it. If no application cookie persists in the header, we
3174 * *MUST* delete it.
3175 *
3176 * Note: RFC2965 is unclear about the processing of spaces around
3177 * the equal sign in the ATTR=VALUE form. A careful inspection of
3178 * the RFC explicitly allows spaces before it, and not within the
3179 * tokens (attrs or values). An inspection of RFC2109 allows that
3180 * too but section 10.1.3 lets one think that spaces may be allowed
3181 * after the equal sign too, resulting in some (rare) buggy
3182 * implementations trying to do that. So let's do what servers do.
3183 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3184 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003185 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003186 * causes parsing to become ambiguous. Browsers also allow spaces
3187 * within values even without quotes.
3188 *
3189 * We have to keep multiple pointers in order to support cookie
3190 * removal at the beginning, middle or end of header without
3191 * corrupting the header. All of these headers are valid :
3192 *
3193 * hdr_beg hdr_end
3194 * | |
3195 * v |
3196 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3197 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3198 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3199 * | | | | | | |
3200 * | | | | | | |
3201 * | | | | | | +--> next
3202 * | | | | | +----> val_end
3203 * | | | | +-----------> val_beg
3204 * | | | +--------------> equal
3205 * | | +----------------> att_end
3206 * | +---------------------> att_beg
3207 * +--------------------------> prev
3208 *
3209 */
3210 hdr_beg = ctx.value.ptr;
3211 hdr_end = hdr_beg + ctx.value.len;
3212 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3213 /* Iterate through all cookies on this line */
3214
3215 /* find att_beg */
3216 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003217 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003218 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003219 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003220
3221 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3222 att_beg++;
3223
3224 /* find att_end : this is the first character after the last non
3225 * space before the equal. It may be equal to hdr_end.
3226 */
3227 equal = att_end = att_beg;
3228 while (equal < hdr_end) {
3229 if (*equal == '=' || *equal == ',' || *equal == ';')
3230 break;
3231 if (HTTP_IS_SPHT(*equal++))
3232 continue;
3233 att_end = equal;
3234 }
3235
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003236 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003237 * is between <att_beg> and <equal>, both may be identical.
3238 */
3239 /* look for end of cookie if there is an equal sign */
3240 if (equal < hdr_end && *equal == '=') {
3241 /* look for the beginning of the value */
3242 val_beg = equal + 1;
3243 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3244 val_beg++;
3245
3246 /* find the end of the value, respecting quotes */
3247 next = http_find_cookie_value_end(val_beg, hdr_end);
3248
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003249 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003250 val_end = next;
3251 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3252 val_end--;
3253 }
3254 else
3255 val_beg = val_end = next = equal;
3256
3257 /* We have nothing to do with attributes beginning with
3258 * '$'. However, they will automatically be removed if a
3259 * header before them is removed, since they're supposed
3260 * to be linked together.
3261 */
3262 if (*att_beg == '$')
3263 continue;
3264
3265 /* Ignore cookies with no equal sign */
3266 if (equal == next) {
3267 /* This is not our cookie, so we must preserve it. But if we already
3268 * scheduled another cookie for removal, we cannot remove the
3269 * complete header, but we can remove the previous block itself.
3270 */
3271 preserve_hdr = 1;
3272 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003273 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003274 val_end += delta;
3275 next += delta;
3276 hdr_end += delta;
3277 prev = del_from;
3278 del_from = NULL;
3279 }
3280 continue;
3281 }
3282
3283 /* if there are spaces around the equal sign, we need to
3284 * strip them otherwise we'll get trouble for cookie captures,
3285 * or even for rewrites. Since this happens extremely rarely,
3286 * it does not hurt performance.
3287 */
3288 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3289 int stripped_before = 0;
3290 int stripped_after = 0;
3291
3292 if (att_end != equal) {
3293 memmove(att_end, equal, hdr_end - equal);
3294 stripped_before = (att_end - equal);
3295 equal += stripped_before;
3296 val_beg += stripped_before;
3297 }
3298
3299 if (val_beg > equal + 1) {
3300 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3301 stripped_after = (equal + 1) - val_beg;
3302 val_beg += stripped_after;
3303 stripped_before += stripped_after;
3304 }
3305
3306 val_end += stripped_before;
3307 next += stripped_before;
3308 hdr_end += stripped_before;
3309 }
3310 /* now everything is as on the diagram above */
3311
3312 /* First, let's see if we want to capture this cookie. We check
3313 * that we don't already have a client side cookie, because we
3314 * can only capture one. Also as an optimisation, we ignore
3315 * cookies shorter than the declared name.
3316 */
3317 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3318 (val_end - att_beg >= sess->fe->capture_namelen) &&
3319 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3320 int log_len = val_end - att_beg;
3321
3322 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3323 ha_alert("HTTP logging : out of memory.\n");
3324 } else {
3325 if (log_len > sess->fe->capture_len)
3326 log_len = sess->fe->capture_len;
3327 memcpy(txn->cli_cookie, att_beg, log_len);
3328 txn->cli_cookie[log_len] = 0;
3329 }
3330 }
3331
3332 /* Persistence cookies in passive, rewrite or insert mode have the
3333 * following form :
3334 *
3335 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3336 *
3337 * For cookies in prefix mode, the form is :
3338 *
3339 * Cookie: NAME=SRV~VALUE
3340 */
3341 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3342 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3343 struct server *srv = s->be->srv;
3344 char *delim;
3345
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003346 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003347 * have the server ID between val_beg and delim, and the original cookie between
3348 * delim+1 and val_end. Otherwise, delim==val_end :
3349 *
3350 * hdr_beg
3351 * |
3352 * v
3353 * NAME=SRV; # in all but prefix modes
3354 * NAME=SRV~OPAQUE ; # in prefix mode
3355 * || || | |+-> next
3356 * || || | +--> val_end
3357 * || || +---------> delim
3358 * || |+------------> val_beg
3359 * || +-------------> att_end = equal
3360 * |+-----------------> att_beg
3361 * +------------------> prev
3362 *
3363 */
3364 if (s->be->ck_opts & PR_CK_PFX) {
3365 for (delim = val_beg; delim < val_end; delim++)
3366 if (*delim == COOKIE_DELIM)
3367 break;
3368 }
3369 else {
3370 char *vbar1;
3371 delim = val_end;
3372 /* Now check if the cookie contains a date field, which would
3373 * appear after a vertical bar ('|') just after the server name
3374 * and before the delimiter.
3375 */
3376 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3377 if (vbar1) {
3378 /* OK, so left of the bar is the server's cookie and
3379 * right is the last seen date. It is a base64 encoded
3380 * 30-bit value representing the UNIX date since the
3381 * epoch in 4-second quantities.
3382 */
3383 int val;
3384 delim = vbar1++;
3385 if (val_end - vbar1 >= 5) {
3386 val = b64tos30(vbar1);
3387 if (val > 0)
3388 txn->cookie_last_date = val << 2;
3389 }
3390 /* look for a second vertical bar */
3391 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3392 if (vbar1 && (val_end - vbar1 > 5)) {
3393 val = b64tos30(vbar1 + 1);
3394 if (val > 0)
3395 txn->cookie_first_date = val << 2;
3396 }
3397 }
3398 }
3399
3400 /* if the cookie has an expiration date and the proxy wants to check
3401 * it, then we do that now. We first check if the cookie is too old,
3402 * then only if it has expired. We detect strict overflow because the
3403 * time resolution here is not great (4 seconds). Cookies with dates
3404 * in the future are ignored if their offset is beyond one day. This
3405 * allows an admin to fix timezone issues without expiring everyone
3406 * and at the same time avoids keeping unwanted side effects for too
3407 * long.
3408 */
3409 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3410 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3411 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3412 txn->flags &= ~TX_CK_MASK;
3413 txn->flags |= TX_CK_OLD;
3414 delim = val_beg; // let's pretend we have not found the cookie
3415 txn->cookie_first_date = 0;
3416 txn->cookie_last_date = 0;
3417 }
3418 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3419 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3420 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3421 txn->flags &= ~TX_CK_MASK;
3422 txn->flags |= TX_CK_EXPIRED;
3423 delim = val_beg; // let's pretend we have not found the cookie
3424 txn->cookie_first_date = 0;
3425 txn->cookie_last_date = 0;
3426 }
3427
3428 /* Here, we'll look for the first running server which supports the cookie.
3429 * This allows to share a same cookie between several servers, for example
3430 * to dedicate backup servers to specific servers only.
3431 * However, to prevent clients from sticking to cookie-less backup server
3432 * when they have incidentely learned an empty cookie, we simply ignore
3433 * empty cookies and mark them as invalid.
3434 * The same behaviour is applied when persistence must be ignored.
3435 */
3436 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3437 srv = NULL;
3438
3439 while (srv) {
3440 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3441 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3442 if ((srv->cur_state != SRV_ST_STOPPED) ||
3443 (s->be->options & PR_O_PERSIST) ||
3444 (s->flags & SF_FORCE_PRST)) {
3445 /* we found the server and we can use it */
3446 txn->flags &= ~TX_CK_MASK;
3447 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3448 s->flags |= SF_DIRECT | SF_ASSIGNED;
3449 s->target = &srv->obj_type;
3450 break;
3451 } else {
3452 /* we found a server, but it's down,
3453 * mark it as such and go on in case
3454 * another one is available.
3455 */
3456 txn->flags &= ~TX_CK_MASK;
3457 txn->flags |= TX_CK_DOWN;
3458 }
3459 }
3460 srv = srv->next;
3461 }
3462
3463 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3464 /* no server matched this cookie or we deliberately skipped it */
3465 txn->flags &= ~TX_CK_MASK;
3466 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3467 txn->flags |= TX_CK_UNUSED;
3468 else
3469 txn->flags |= TX_CK_INVALID;
3470 }
3471
3472 /* depending on the cookie mode, we may have to either :
3473 * - delete the complete cookie if we're in insert+indirect mode, so that
3474 * the server never sees it ;
3475 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003476 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003477 * if we're in cookie prefix mode
3478 */
3479 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3480 int delta; /* negative */
3481
3482 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3483 delta = val_beg - (delim + 1);
3484 val_end += delta;
3485 next += delta;
3486 hdr_end += delta;
3487 del_from = NULL;
3488 preserve_hdr = 1; /* we want to keep this cookie */
3489 }
3490 else if (del_from == NULL &&
3491 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3492 del_from = prev;
3493 }
3494 }
3495 else {
3496 /* This is not our cookie, so we must preserve it. But if we already
3497 * scheduled another cookie for removal, we cannot remove the
3498 * complete header, but we can remove the previous block itself.
3499 */
3500 preserve_hdr = 1;
3501
3502 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003503 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003504 if (att_beg >= del_from)
3505 att_beg += delta;
3506 if (att_end >= del_from)
3507 att_end += delta;
3508 val_beg += delta;
3509 val_end += delta;
3510 next += delta;
3511 hdr_end += delta;
3512 prev = del_from;
3513 del_from = NULL;
3514 }
3515 }
3516
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003517 } /* for each cookie */
3518
3519
3520 /* There are no more cookies on this line.
3521 * We may still have one (or several) marked for deletion at the
3522 * end of the line. We must do this now in two ways :
3523 * - if some cookies must be preserved, we only delete from the
3524 * mark to the end of line ;
3525 * - if nothing needs to be preserved, simply delete the whole header
3526 */
3527 if (del_from) {
3528 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3529 }
3530 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003531 if (hdr_beg != hdr_end)
3532 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003533 else
3534 http_remove_header(htx, &ctx);
3535 }
3536 } /* for each "Cookie header */
3537}
3538
3539/*
3540 * Manage server-side cookies. It can impact performance by about 2% so it is
3541 * desirable to call it only when needed. This function is also used when we
3542 * just need to know if there is a cookie (eg: for check-cache).
3543 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003544static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003545{
3546 struct session *sess = s->sess;
3547 struct http_txn *txn = s->txn;
3548 struct htx *htx;
3549 struct http_hdr_ctx ctx;
3550 struct server *srv;
3551 char *hdr_beg, *hdr_end;
3552 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003553 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003554
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003555 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003556
3557 ctx.blk = NULL;
3558 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003559 int is_first = 1;
3560
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003561 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3562 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3563 break;
3564 is_cookie2 = 1;
3565 }
3566
3567 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3568 * <prev> points to the colon.
3569 */
3570 txn->flags |= TX_SCK_PRESENT;
3571
3572 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3573 * check-cache is enabled) and we are not interested in checking
3574 * them. Warning, the cookie capture is declared in the frontend.
3575 */
3576 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3577 break;
3578
3579 /* OK so now we know we have to process this response cookie.
3580 * The format of the Set-Cookie header is slightly different
3581 * from the format of the Cookie header in that it does not
3582 * support the comma as a cookie delimiter (thus the header
3583 * cannot be folded) because the Expires attribute described in
3584 * the original Netscape's spec may contain an unquoted date
3585 * with a comma inside. We have to live with this because
3586 * many browsers don't support Max-Age and some browsers don't
3587 * support quoted strings. However the Set-Cookie2 header is
3588 * clean.
3589 *
3590 * We have to keep multiple pointers in order to support cookie
3591 * removal at the beginning, middle or end of header without
3592 * corrupting the header (in case of set-cookie2). A special
3593 * pointer, <scav> points to the beginning of the set-cookie-av
3594 * fields after the first semi-colon. The <next> pointer points
3595 * either to the end of line (set-cookie) or next unquoted comma
3596 * (set-cookie2). All of these headers are valid :
3597 *
3598 * hdr_beg hdr_end
3599 * | |
3600 * v |
3601 * NAME1 = VALUE 1 ; Secure; Path="/" |
3602 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3603 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3604 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3605 * | | | | | | | |
3606 * | | | | | | | +-> next
3607 * | | | | | | +------------> scav
3608 * | | | | | +--------------> val_end
3609 * | | | | +--------------------> val_beg
3610 * | | | +----------------------> equal
3611 * | | +------------------------> att_end
3612 * | +----------------------------> att_beg
3613 * +------------------------------> prev
3614 * -------------------------------> hdr_beg
3615 */
3616 hdr_beg = ctx.value.ptr;
3617 hdr_end = hdr_beg + ctx.value.len;
3618 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3619
3620 /* Iterate through all cookies on this line */
3621
3622 /* find att_beg */
3623 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003624 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003625 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003626 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003627
3628 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3629 att_beg++;
3630
3631 /* find att_end : this is the first character after the last non
3632 * space before the equal. It may be equal to hdr_end.
3633 */
3634 equal = att_end = att_beg;
3635
3636 while (equal < hdr_end) {
3637 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3638 break;
3639 if (HTTP_IS_SPHT(*equal++))
3640 continue;
3641 att_end = equal;
3642 }
3643
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003644 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003645 * is between <att_beg> and <equal>, both may be identical.
3646 */
3647
3648 /* look for end of cookie if there is an equal sign */
3649 if (equal < hdr_end && *equal == '=') {
3650 /* look for the beginning of the value */
3651 val_beg = equal + 1;
3652 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3653 val_beg++;
3654
3655 /* find the end of the value, respecting quotes */
3656 next = http_find_cookie_value_end(val_beg, hdr_end);
3657
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003658 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003659 val_end = next;
3660 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3661 val_end--;
3662 }
3663 else {
3664 /* <equal> points to next comma, semi-colon or EOL */
3665 val_beg = val_end = next = equal;
3666 }
3667
3668 if (next < hdr_end) {
3669 /* Set-Cookie2 supports multiple cookies, and <next> points to
3670 * a colon or semi-colon before the end. So skip all attr-value
3671 * pairs and look for the next comma. For Set-Cookie, since
3672 * commas are permitted in values, skip to the end.
3673 */
3674 if (is_cookie2)
3675 next = http_find_hdr_value_end(next, hdr_end);
3676 else
3677 next = hdr_end;
3678 }
3679
3680 /* Now everything is as on the diagram above */
3681
3682 /* Ignore cookies with no equal sign */
3683 if (equal == val_end)
3684 continue;
3685
3686 /* If there are spaces around the equal sign, we need to
3687 * strip them otherwise we'll get trouble for cookie captures,
3688 * or even for rewrites. Since this happens extremely rarely,
3689 * it does not hurt performance.
3690 */
3691 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3692 int stripped_before = 0;
3693 int stripped_after = 0;
3694
3695 if (att_end != equal) {
3696 memmove(att_end, equal, hdr_end - equal);
3697 stripped_before = (att_end - equal);
3698 equal += stripped_before;
3699 val_beg += stripped_before;
3700 }
3701
3702 if (val_beg > equal + 1) {
3703 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3704 stripped_after = (equal + 1) - val_beg;
3705 val_beg += stripped_after;
3706 stripped_before += stripped_after;
3707 }
3708
3709 val_end += stripped_before;
3710 next += stripped_before;
3711 hdr_end += stripped_before;
3712
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003713 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003714 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003715 }
3716
3717 /* First, let's see if we want to capture this cookie. We check
3718 * that we don't already have a server side cookie, because we
3719 * can only capture one. Also as an optimisation, we ignore
3720 * cookies shorter than the declared name.
3721 */
3722 if (sess->fe->capture_name != NULL &&
3723 txn->srv_cookie == NULL &&
3724 (val_end - att_beg >= sess->fe->capture_namelen) &&
3725 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3726 int log_len = val_end - att_beg;
3727 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3728 ha_alert("HTTP logging : out of memory.\n");
3729 }
3730 else {
3731 if (log_len > sess->fe->capture_len)
3732 log_len = sess->fe->capture_len;
3733 memcpy(txn->srv_cookie, att_beg, log_len);
3734 txn->srv_cookie[log_len] = 0;
3735 }
3736 }
3737
3738 srv = objt_server(s->target);
3739 /* now check if we need to process it for persistence */
3740 if (!(s->flags & SF_IGNORE_PRST) &&
3741 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3742 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3743 /* assume passive cookie by default */
3744 txn->flags &= ~TX_SCK_MASK;
3745 txn->flags |= TX_SCK_FOUND;
3746
3747 /* If the cookie is in insert mode on a known server, we'll delete
3748 * this occurrence because we'll insert another one later.
3749 * We'll delete it too if the "indirect" option is set and we're in
3750 * a direct access.
3751 */
3752 if (s->be->ck_opts & PR_CK_PSV) {
3753 /* The "preserve" flag was set, we don't want to touch the
3754 * server's cookie.
3755 */
3756 }
3757 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3758 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3759 /* this cookie must be deleted */
3760 if (prev == hdr_beg && next == hdr_end) {
3761 /* whole header */
3762 http_remove_header(htx, &ctx);
3763 /* note: while both invalid now, <next> and <hdr_end>
3764 * are still equal, so the for() will stop as expected.
3765 */
3766 } else {
3767 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003768 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003769 next = prev;
3770 hdr_end += delta;
3771 }
3772 txn->flags &= ~TX_SCK_MASK;
3773 txn->flags |= TX_SCK_DELETED;
3774 /* and go on with next cookie */
3775 }
3776 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3777 /* replace bytes val_beg->val_end with the cookie name associated
3778 * with this server since we know it.
3779 */
3780 int sliding, delta;
3781
3782 ctx.value = ist2(val_beg, val_end - val_beg);
3783 ctx.lws_before = ctx.lws_after = 0;
3784 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3785 delta = srv->cklen - (val_end - val_beg);
3786 sliding = (ctx.value.ptr - val_beg);
3787 hdr_beg += sliding;
3788 val_beg += sliding;
3789 next += sliding + delta;
3790 hdr_end += sliding + delta;
3791
3792 txn->flags &= ~TX_SCK_MASK;
3793 txn->flags |= TX_SCK_REPLACED;
3794 }
3795 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3796 /* insert the cookie name associated with this server
3797 * before existing cookie, and insert a delimiter between them..
3798 */
3799 int sliding, delta;
3800 ctx.value = ist2(val_beg, 0);
3801 ctx.lws_before = ctx.lws_after = 0;
3802 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3803 delta = srv->cklen + 1;
3804 sliding = (ctx.value.ptr - val_beg);
3805 hdr_beg += sliding;
3806 val_beg += sliding;
3807 next += sliding + delta;
3808 hdr_end += sliding + delta;
3809
3810 val_beg[srv->cklen] = COOKIE_DELIM;
3811 txn->flags &= ~TX_SCK_MASK;
3812 txn->flags |= TX_SCK_REPLACED;
3813 }
3814 }
3815 /* that's done for this cookie, check the next one on the same
3816 * line when next != hdr_end (only if is_cookie2).
3817 */
3818 }
3819 }
3820}
3821
Christopher Faulet25a02f62018-10-24 12:00:25 +02003822/*
3823 * Parses the Cache-Control and Pragma request header fields to determine if
3824 * the request may be served from the cache and/or if it is cacheable. Updates
3825 * s->txn->flags.
3826 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003827void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003828{
3829 struct http_txn *txn = s->txn;
3830 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003831 struct http_hdr_ctx ctx = { .blk = NULL };
3832 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003833
3834 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3835 return; /* nothing more to do here */
3836
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003837 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003838 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003839
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003840 /* Check "pragma" header for HTTP/1.0 compatibility. */
3841 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3842 if (isteqi(ctx.value, ist("no-cache"))) {
3843 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003844 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003845 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003846
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003847 ctx.blk = NULL;
3848 /* Don't use the cache and don't try to store if we found the
3849 * Authorization header */
3850 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3851 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3852 txn->flags |= TX_CACHE_IGNORE;
3853 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003854
Christopher Faulet25a02f62018-10-24 12:00:25 +02003855
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003856 /* Look for "cache-control" header and iterate over all the values
3857 * until we find one that specifies that caching is possible or not. */
3858 ctx.blk = NULL;
3859 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003860 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003861 /* We don't check the values after max-age, max-stale nor min-fresh,
3862 * we simply don't use the cache when they're specified. */
3863 if (istmatchi(ctx.value, ist("max-age")) ||
3864 istmatchi(ctx.value, ist("no-cache")) ||
3865 istmatchi(ctx.value, ist("max-stale")) ||
3866 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003867 txn->flags |= TX_CACHE_IGNORE;
3868 continue;
3869 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003870 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003871 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3872 continue;
3873 }
3874 }
3875
3876 /* RFC7234#5.4:
3877 * When the Cache-Control header field is also present and
3878 * understood in a request, Pragma is ignored.
3879 * When the Cache-Control header field is not present in a
3880 * request, caches MUST consider the no-cache request
3881 * pragma-directive as having the same effect as if
3882 * "Cache-Control: no-cache" were present.
3883 */
3884 if (!cc_found && pragma_found)
3885 txn->flags |= TX_CACHE_IGNORE;
3886}
3887
3888/*
3889 * Check if response is cacheable or not. Updates s->txn->flags.
3890 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003891void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003892{
3893 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003894 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003895 struct htx *htx;
Remi Tricot-Le Breton87c2f252020-11-12 11:14:41 +01003896 int has_freshness_info = 0;
3897 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003898
3899 if (txn->status < 200) {
3900 /* do not try to cache interim responses! */
3901 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3902 return;
3903 }
3904
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003905 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003906 /* Check "pragma" header for HTTP/1.0 compatibility. */
3907 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3908 if (isteqi(ctx.value, ist("no-cache"))) {
3909 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3910 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003911 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003912 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003913
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003914 /* Look for "cache-control" header and iterate over all the values
3915 * until we find one that specifies that caching is possible or not. */
3916 ctx.blk = NULL;
3917 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3918 if (isteqi(ctx.value, ist("public"))) {
3919 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003920 continue;
3921 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003922 if (isteqi(ctx.value, ist("private")) ||
3923 isteqi(ctx.value, ist("no-cache")) ||
3924 isteqi(ctx.value, ist("no-store")) ||
3925 isteqi(ctx.value, ist("max-age=0")) ||
3926 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003927 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003928 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003929 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003930 /* We might have a no-cache="set-cookie" form. */
3931 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3932 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003933 continue;
3934 }
Remi Tricot-Le Breton87c2f252020-11-12 11:14:41 +01003935
3936 if (istmatchi(ctx.value, ist("s-maxage")) ||
3937 istmatchi(ctx.value, ist("max-age"))) {
3938 has_freshness_info = 1;
3939 continue;
3940 }
3941 }
3942
3943 /* If no freshness information could be found in Cache-Control values,
3944 * look for an Expires header. */
3945 if (!has_freshness_info) {
3946 ctx.blk = NULL;
3947 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003948 }
Remi Tricot-Le Breton87c2f252020-11-12 11:14:41 +01003949
3950 /* If no freshness information could be found in Cache-Control or Expires
3951 * values, look for an explicit validator. */
3952 if (!has_freshness_info) {
3953 ctx.blk = NULL;
3954 has_validator = 1;
3955 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3956 ctx.blk = NULL;
3957 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3958 has_validator = 0;
3959 }
3960 }
3961
3962 /* We won't store an entry that has neither a cache validator nor an
3963 * explicit expiration time, as suggested in RFC 7234#3. */
3964 if (!has_freshness_info && !has_validator)
3965 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003966}
3967
Christopher Faulet377c5a52018-10-24 21:21:30 +02003968/*
3969 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3970 * for the current backend.
3971 *
3972 * It is assumed that the request is either a HEAD, GET, or POST and that the
3973 * uri_auth field is valid.
3974 *
3975 * Returns 1 if stats should be provided, otherwise 0.
3976 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003977static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003978{
3979 struct uri_auth *uri_auth = backend->uri_auth;
3980 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003981 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003982 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003983
3984 if (!uri_auth)
3985 return 0;
3986
3987 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3988 return 0;
3989
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003990 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003991 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003992 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003993 if (*uri_auth->uri_prefix == '/')
3994 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003995
3996 /* check URI size */
3997 if (uri_auth->uri_len > uri.len)
3998 return 0;
3999
4000 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4001 return 0;
4002
4003 return 1;
4004}
4005
4006/* This function prepares an applet to handle the stats. It can deal with the
4007 * "100-continue" expectation, check that admin rules are met for POST requests,
4008 * and program a response message if something was unexpected. It cannot fail
4009 * and always relies on the stats applet to complete the job. It does not touch
4010 * analysers nor counters, which are left to the caller. It does not touch
4011 * s->target which is supposed to already point to the stats applet. The caller
4012 * is expected to have already assigned an appctx to the stream.
4013 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004014static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004015{
4016 struct stats_admin_rule *stats_admin_rule;
4017 struct stream_interface *si = &s->si[1];
4018 struct session *sess = s->sess;
4019 struct http_txn *txn = s->txn;
4020 struct http_msg *msg = &txn->req;
4021 struct uri_auth *uri_auth = s->be->uri_auth;
4022 const char *h, *lookup, *end;
4023 struct appctx *appctx;
4024 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004025 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004026
4027 appctx = si_appctx(si);
4028 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4029 appctx->st1 = appctx->st2 = 0;
4030 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004031 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004032 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4033 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4034 appctx->ctx.stats.flags |= STAT_CHUNKED;
4035
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004036 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004037 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004038 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4039 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004040
4041 for (h = lookup; h <= end - 3; h++) {
4042 if (memcmp(h, ";up", 3) == 0) {
4043 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4044 break;
4045 }
Amaury Denoyellee78aa872021-02-25 14:46:08 +01004046 }
4047
4048 for (h = lookup; h <= end - 9; h++) {
4049 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02004050 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
4051 break;
4052 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004053 }
4054
4055 if (uri_auth->refresh) {
4056 for (h = lookup; h <= end - 10; h++) {
4057 if (memcmp(h, ";norefresh", 10) == 0) {
4058 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4059 break;
4060 }
4061 }
4062 }
4063
4064 for (h = lookup; h <= end - 4; h++) {
4065 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004066 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004067 break;
4068 }
4069 }
4070
4071 for (h = lookup; h <= end - 6; h++) {
4072 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004073 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004074 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4075 break;
4076 }
4077 }
4078
Christopher Faulet6338a082019-09-09 15:50:54 +02004079 for (h = lookup; h <= end - 5; h++) {
4080 if (memcmp(h, ";json", 5) == 0) {
4081 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4082 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4083 break;
4084 }
4085 }
4086
4087 for (h = lookup; h <= end - 12; h++) {
4088 if (memcmp(h, ";json-schema", 12) == 0) {
4089 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4090 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4091 break;
4092 }
4093 }
4094
Christopher Faulet377c5a52018-10-24 21:21:30 +02004095 for (h = lookup; h <= end - 8; h++) {
4096 if (memcmp(h, ";st=", 4) == 0) {
4097 int i;
4098 h += 4;
4099 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4100 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4101 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4102 appctx->ctx.stats.st_code = i;
4103 break;
4104 }
4105 }
4106 break;
4107 }
4108 }
4109
4110 appctx->ctx.stats.scope_str = 0;
4111 appctx->ctx.stats.scope_len = 0;
4112 for (h = lookup; h <= end - 8; h++) {
4113 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4114 int itx = 0;
4115 const char *h2;
4116 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4117 const char *err;
4118
4119 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4120 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004121 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4122 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004123 if (*h == ';' || *h == '&' || *h == ' ')
4124 break;
4125 itx++;
4126 h++;
4127 }
4128
4129 if (itx > STAT_SCOPE_TXT_MAXLEN)
4130 itx = STAT_SCOPE_TXT_MAXLEN;
4131 appctx->ctx.stats.scope_len = itx;
4132
4133 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4134 memcpy(scope_txt, h2, itx);
4135 scope_txt[itx] = '\0';
4136 err = invalid_char(scope_txt);
4137 if (err) {
4138 /* bad char in search text => clear scope */
4139 appctx->ctx.stats.scope_str = 0;
4140 appctx->ctx.stats.scope_len = 0;
4141 }
4142 break;
4143 }
4144 }
4145
4146 /* now check whether we have some admin rules for this request */
4147 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4148 int ret = 1;
4149
4150 if (stats_admin_rule->cond) {
4151 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4152 ret = acl_pass(ret);
4153 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4154 ret = !ret;
4155 }
4156
4157 if (ret) {
4158 /* no rule, or the rule matches */
4159 appctx->ctx.stats.flags |= STAT_ADMIN;
4160 break;
4161 }
4162 }
4163
Christopher Faulet5d45e382019-02-27 15:15:23 +01004164 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4165 appctx->st0 = STAT_HTTP_HEAD;
4166 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004167 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004168 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004169 if (msg->msg_state < HTTP_MSG_DATA)
4170 req->analysers |= AN_REQ_HTTP_BODY;
4171 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004172 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004173 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004174 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4175 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4176 appctx->st0 = STAT_HTTP_LAST;
4177 }
4178 }
4179 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004180 /* Unsupported method */
4181 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4182 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4183 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004184 }
4185
4186 s->task->nice = -32; /* small boost for HTTP statistics */
4187 return 1;
4188}
4189
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004190void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004191{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004192 struct channel *req = &s->req;
4193 struct channel *res = &s->res;
4194 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004195 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004196 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004197 struct ist path, location;
4198 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004199
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004200 /*
4201 * Create the location
4202 */
4203 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004204
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004205 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004206 /* special prefix "/" means don't change URL */
4207 srv = __objt_server(s->target);
4208 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4209 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4210 return;
4211 }
4212
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004213 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004214 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004215 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004216 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004217 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004218 return;
4219
4220 if (!chunk_memcat(&trash, path.ptr, path.len))
4221 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004222 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004223
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004224 /*
4225 * Create the 302 respone
4226 */
4227 htx = htx_from_buf(&res->buf);
4228 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4229 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4230 ist("HTTP/1.1"), ist("302"), ist("Found"));
4231 if (!sl)
4232 goto fail;
4233 sl->info.res.status = 302;
4234 s->txn->status = 302;
4235
4236 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4237 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4238 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4239 !htx_add_header(htx, ist("Location"), location))
4240 goto fail;
4241
4242 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4243 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004244
Christopher Fauletc20afb82020-01-24 19:16:26 +01004245 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004246 if (!http_forward_proxy_resp(s, 1))
4247 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004248
4249 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004250 si_shutr(si);
4251 si_shutw(si);
4252 si->err_type = SI_ET_NONE;
4253 si->state = SI_ST_CLO;
4254
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004255 if (!(s->flags & SF_ERR_MASK))
4256 s->flags |= SF_ERR_LOCAL;
4257 if (!(s->flags & SF_FINST_MASK))
4258 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004259
4260 /* FIXME: we should increase a counter of redirects per server and per backend. */
4261 srv_inc_sess_ctr(srv);
4262 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004263 return;
4264
4265 fail:
4266 /* If an error occurred, remove the incomplete HTTP response from the
4267 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004268 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004269}
4270
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004271/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004272 * because an error was triggered during the body forwarding.
4273 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004274static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004275{
4276 struct channel *chn = &s->req;
4277 struct http_txn *txn = s->txn;
4278
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004279 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004280
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004281 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4282 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004283 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004284 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004285 goto end;
4286 }
4287
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004288 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4289 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004290 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004291 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004292
4293 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004294 /* No need to read anymore, the request was completely parsed.
4295 * We can shut the read side unless we want to abort_on_close,
4296 * or we have a POST request. The issue with POST requests is
4297 * that some browsers still send a CRLF after the request, and
4298 * this CRLF must be read so that it does not remain in the kernel
4299 * buffers, otherwise a close could cause an RST on some systems
4300 * (eg: Linux).
4301 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004302 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004303 channel_dont_read(chn);
4304
4305 /* if the server closes the connection, we want to immediately react
4306 * and close the socket to save packets and syscalls.
4307 */
4308 s->si[1].flags |= SI_FL_NOHALF;
4309
4310 /* In any case we've finished parsing the request so we must
4311 * disable Nagle when sending data because 1) we're not going
4312 * to shut this side, and 2) the server is waiting for us to
4313 * send pending data.
4314 */
4315 chn->flags |= CF_NEVER_WAIT;
4316
Christopher Fauletd01ce402019-01-02 17:44:13 +01004317 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4318 /* The server has not finished to respond, so we
4319 * don't want to move in order not to upset it.
4320 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004321 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004322 return;
4323 }
4324
Christopher Fauletf2824e62018-10-01 12:12:37 +02004325 /* When we get here, it means that both the request and the
4326 * response have finished receiving. Depending on the connection
4327 * mode, we'll have to wait for the last bytes to leave in either
4328 * direction, and sometimes for a close to be effective.
4329 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004330 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004331 /* Tunnel mode will not have any analyser so it needs to
4332 * poll for reads.
4333 */
4334 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004335 if (b_data(&chn->buf)) {
4336 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004337 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004338 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004339 txn->req.msg_state = HTTP_MSG_TUNNEL;
4340 }
4341 else {
4342 /* we're not expecting any new data to come for this
4343 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004344 *
4345 * However, there is an exception if the response
4346 * length is undefined. In this case, we need to wait
4347 * the close from the server. The response will be
4348 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004349 */
4350 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4351 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004352 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004353
4354 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4355 channel_shutr_now(chn);
4356 channel_shutw_now(chn);
4357 }
4358 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004359 goto check_channel_flags;
4360 }
4361
4362 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4363 http_msg_closing:
4364 /* nothing else to forward, just waiting for the output buffer
4365 * to be empty and for the shutw_now to take effect.
4366 */
4367 if (channel_is_empty(chn)) {
4368 txn->req.msg_state = HTTP_MSG_CLOSED;
4369 goto http_msg_closed;
4370 }
4371 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004372 txn->req.msg_state = HTTP_MSG_ERROR;
4373 goto end;
4374 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004375 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004376 return;
4377 }
4378
4379 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4380 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004381 /* if we don't know whether the server will close, we need to hard close */
4382 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4383 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004384 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004385 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004386 channel_dont_read(chn);
4387 goto end;
4388 }
4389
4390 check_channel_flags:
4391 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4392 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4393 /* if we've just closed an output, let's switch */
4394 txn->req.msg_state = HTTP_MSG_CLOSING;
4395 goto http_msg_closing;
4396 }
4397
4398 end:
4399 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet0d4da8c2020-12-15 13:32:55 +01004400 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4401 chn->flags |= CF_NEVER_WAIT;
4402 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004403 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet0d4da8c2020-12-15 13:32:55 +01004404 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004405 channel_auto_close(chn);
4406 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004407 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004408}
4409
4410
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004411/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004412 * because an error was triggered during the body forwarding.
4413 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004414static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004415{
4416 struct channel *chn = &s->res;
4417 struct http_txn *txn = s->txn;
4418
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004419 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004420
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004421 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4422 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004423 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004424 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004425 goto end;
4426 }
4427
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004428 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4429 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004430 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004431 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004432
4433 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4434 /* In theory, we don't need to read anymore, but we must
4435 * still monitor the server connection for a possible close
4436 * while the request is being uploaded, so we don't disable
4437 * reading.
4438 */
4439 /* channel_dont_read(chn); */
4440
4441 if (txn->req.msg_state < HTTP_MSG_DONE) {
4442 /* The client seems to still be sending data, probably
4443 * because we got an error response during an upload.
4444 * We have the choice of either breaking the connection
4445 * or letting it pass through. Let's do the later.
4446 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004447 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004448 return;
4449 }
4450
4451 /* When we get here, it means that both the request and the
4452 * response have finished receiving. Depending on the connection
4453 * mode, we'll have to wait for the last bytes to leave in either
4454 * direction, and sometimes for a close to be effective.
4455 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004456 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004457 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004458 if (b_data(&chn->buf)) {
4459 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004460 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004461 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004462 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4463 }
4464 else {
4465 /* we're not expecting any new data to come for this
4466 * transaction, so we can close it.
4467 */
4468 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4469 channel_shutr_now(chn);
4470 channel_shutw_now(chn);
4471 }
4472 }
4473 goto check_channel_flags;
4474 }
4475
4476 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4477 http_msg_closing:
4478 /* nothing else to forward, just waiting for the output buffer
4479 * to be empty and for the shutw_now to take effect.
4480 */
4481 if (channel_is_empty(chn)) {
4482 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4483 goto http_msg_closed;
4484 }
4485 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004486 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004487 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004488 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004489 if (strm_sess(s)->listener->counters)
4490 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004491 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004492 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004493 goto end;
4494 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004495 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004496 return;
4497 }
4498
4499 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4500 http_msg_closed:
4501 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004502 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004503 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004504 goto end;
4505 }
4506
4507 check_channel_flags:
4508 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4509 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4510 /* if we've just closed an output, let's switch */
4511 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4512 goto http_msg_closing;
4513 }
4514
4515 end:
4516 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet0d4da8c2020-12-15 13:32:55 +01004517 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4518 chn->flags |= CF_NEVER_WAIT;
4519 if (HAS_RSP_DATA_FILTERS(s))
4520 chn->analysers |= AN_RES_FLT_XFER_DATA;
4521 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004522 channel_auto_close(chn);
4523 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004524 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004525}
4526
Christopher Fauletef70e252020-01-28 09:26:19 +01004527/* Forward a response generated by HAProxy (error/redirect/return). This
4528 * function forwards all pending incoming data. If <final> is set to 0, nothing
4529 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004530 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004531 * returned. If an error occurred, 0 is returned. If it fails, this function
4532 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004533 */
4534int http_forward_proxy_resp(struct stream *s, int final)
4535{
4536 struct channel *req = &s->req;
4537 struct channel *res = &s->res;
4538 struct htx *htx = htxbuf(&res->buf);
4539 size_t data;
4540
4541 if (final) {
4542 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004543
Christopher Faulet32ccee12020-11-18 16:44:02 +01004544 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004545 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004546
Christopher Fauletd6c48362020-10-19 18:01:38 +02004547 if (s->txn->meth == HTTP_METH_HEAD)
4548 htx_skip_msg_payload(htx);
4549
Christopher Fauletef70e252020-01-28 09:26:19 +01004550 channel_auto_read(req);
4551 channel_abort(req);
4552 channel_auto_close(req);
4553 channel_htx_erase(req, htxbuf(&req->buf));
4554
4555 res->wex = tick_add_ifset(now_ms, res->wto);
4556 channel_auto_read(res);
4557 channel_auto_close(res);
4558 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004559 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet810df062020-07-22 16:20:34 +02004560 htxbuf(&res->buf)->flags |= HTX_FL_EOI; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004561 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004562 else {
4563 /* Send ASAP informational messages. Rely on CF_EOI for final
4564 * response.
4565 */
4566 res->flags |= CF_SEND_DONTWAIT;
4567 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004568
4569 data = htx->data - co_data(res);
4570 c_adv(res, data);
4571 htx->first = -1;
4572 res->total += data;
4573 return 1;
4574}
4575
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004576void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004577 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004578{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004579 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004580 if (!(s->flags & SF_ERR_MASK))
4581 s->flags |= err;
4582 if (!(s->flags & SF_FINST_MASK))
4583 s->flags |= finst;
4584}
4585
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004586void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004587{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004588 if (!msg) {
4589 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4590 goto end;
4591 }
4592
4593 if (http_reply_message(s, msg) == -1) {
4594 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004595 * it is already an internal error. If it was already a "const"
4596 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004597 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004598 if (s->txn->status == 500) {
4599 if (s->txn->flags & TX_CONST_REPLY)
4600 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004601 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004602 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004603 s->txn->status = 500;
4604 s->txn->http_reply = NULL;
4605 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4606 }
4607
4608end:
4609 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
4610 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
4611
Christopher Faulet0f226952018-10-22 09:29:56 +02004612 channel_auto_read(&s->req);
4613 channel_abort(&s->req);
4614 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004615 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004616 channel_auto_read(&s->res);
4617 channel_auto_close(&s->res);
4618 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004619}
4620
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004621struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004622{
4623 const int msgnum = http_get_status_idx(s->txn->status);
4624
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004625 if (s->txn->http_reply)
4626 return s->txn->http_reply;
4627 else if (s->be->replies[msgnum])
4628 return s->be->replies[msgnum];
4629 else if (strm_fe(s)->replies[msgnum])
4630 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004631 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004632 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004633}
4634
Christopher Faulet40e6b552020-06-25 16:04:50 +02004635/* Produces an HTX message from an http reply. Depending on the http reply type,
4636 * a, errorfile, an raw file or a log-format string is used. On success, it
4637 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4638 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004639 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004640int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004641{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004642 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004643 struct htx_sl *sl;
4644 struct buffer *body = NULL;
4645 const char *status, *reason, *clen, *ctype;
4646 unsigned int slflags;
4647 int ret = 0;
4648
Christopher Faulete29a97e2020-05-14 14:49:25 +02004649 /*
4650 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4651 *
4652 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4653 * as no payload if NULL. the TXN status code is set with the status
4654 * of the original reply.
4655 */
4656
4657 if (reply->type == HTTP_REPLY_INDIRECT) {
4658 if (reply->body.reply)
4659 reply = reply->body.reply;
4660 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004661 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4662 /* get default error message */
4663 if (reply == s->txn->http_reply)
4664 s->txn->http_reply = NULL;
4665 reply = http_error_message(s);
4666 if (reply->type == HTTP_REPLY_INDIRECT) {
4667 if (reply->body.reply)
4668 reply = reply->body.reply;
4669 }
4670 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004671
4672 if (reply->type == HTTP_REPLY_ERRMSG) {
4673 /* implicit or explicit error message*/
4674 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004675 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004676 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004677 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004678 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004679 }
4680 else {
4681 /* no payload, file or log-format string */
4682 if (reply->type == HTTP_REPLY_RAW) {
4683 /* file */
4684 body = &reply->body.obj;
4685 }
4686 else if (reply->type == HTTP_REPLY_LOGFMT) {
4687 /* log-format string */
4688 body = alloc_trash_chunk();
4689 if (!body)
4690 goto fail_alloc;
4691 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4692 }
4693 /* else no payload */
4694
4695 status = ultoa(reply->status);
4696 reason = http_get_reason(reply->status);
4697 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4698 if (!body || !b_data(body))
4699 slflags |= HTX_SL_F_BODYLESS;
4700 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4701 if (!sl)
4702 goto fail;
4703 sl->info.res.status = reply->status;
4704
4705 clen = (body ? ultoa(b_data(body)) : "0");
4706 ctype = reply->ctype;
4707
4708 if (!LIST_ISEMPTY(&reply->hdrs)) {
4709 struct http_reply_hdr *hdr;
4710 struct buffer *value = alloc_trash_chunk();
4711
4712 if (!value)
4713 goto fail;
4714
4715 list_for_each_entry(hdr, &reply->hdrs, list) {
4716 chunk_reset(value);
4717 value->data = build_logline(s, value->area, value->size, &hdr->value);
4718 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4719 free_trash_chunk(value);
4720 goto fail;
4721 }
4722 chunk_reset(value);
4723 }
4724 free_trash_chunk(value);
4725 }
4726
4727 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4728 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4729 !htx_add_endof(htx, HTX_BLK_EOH) ||
4730 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))) ||
4731 !htx_add_endof(htx, HTX_BLK_EOM))
4732 goto fail;
4733 }
4734
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004735 leave:
4736 if (reply->type == HTTP_REPLY_LOGFMT)
4737 free_trash_chunk(body);
4738 return ret;
4739
4740 fail_alloc:
4741 if (!(s->flags & SF_ERR_MASK))
4742 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004743 /* fall through */
4744 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004745 ret = -1;
4746 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004747}
4748
4749/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004750 * occurs -1 is returned and the response channel is truncated, removing this
4751 * way the faulty reply. This function may fail when the reply is formatted
4752 * (http_reply_to_htx) or when the reply is forwarded
4753 * (http_forward_proxy_resp). On the last case, it is because a
4754 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004755 */
4756int http_reply_message(struct stream *s, struct http_reply *reply)
4757{
4758 struct channel *res = &s->res;
4759 struct htx *htx = htx_from_buf(&res->buf);
4760
4761 if (s->txn->status == -1)
4762 s->txn->status = reply->status;
4763 channel_htx_truncate(res, htx);
4764
4765 if (http_reply_to_htx(s, htx, reply) == -1)
4766 goto fail;
4767
4768 htx_to_buf(htx, &s->res.buf);
4769 if (!http_forward_proxy_resp(s, 1))
4770 goto fail;
4771 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004772
4773 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004774 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004775 if (!(s->flags & SF_ERR_MASK))
4776 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004777 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004778}
4779
Christopher Faulet304cc402019-07-15 15:46:28 +02004780/* Return the error message corresponding to si->err_type. It is assumed
4781 * that the server side is closed. Note that err_type is actually a
4782 * bitmask, where almost only aborts may be cumulated with other
4783 * values. We consider that aborted operations are more important
4784 * than timeouts or errors due to the fact that nobody else in the
4785 * logs might explain incomplete retries. All others should avoid
4786 * being cumulated. It should normally not be possible to have multiple
4787 * aborts at once, but just in case, the first one in sequence is reported.
4788 * Note that connection errors appearing on the second request of a keep-alive
4789 * connection are not reported since this allows the client to retry.
4790 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004791void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004792{
4793 int err_type = si->err_type;
4794
4795 /* set s->txn->status for http_error_message(s) */
4796 s->txn->status = 503;
4797
4798 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004799 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4800 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004801 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004802 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4803 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4804 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004805 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004806 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4807 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004808 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004809 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4810 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004811 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004812 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4813 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4814 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004815 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004816 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4817 (s->flags & SF_SRV_REUSED) ? NULL :
4818 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004819 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004820 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4821 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4822 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004823 else { /* SI_ET_CONN_OTHER and others */
4824 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004825 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4826 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004827 }
4828}
4829
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004830
Christopher Faulet4a28a532019-03-01 11:19:40 +01004831/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4832 * on success and -1 on error.
4833 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004834static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004835{
4836 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4837 * then we must send an HTTP/1.1 100 Continue intermediate response.
4838 */
4839 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4840 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4841 struct ist hdr = { .ptr = "Expect", .len = 6 };
4842 struct http_hdr_ctx ctx;
4843
4844 ctx.blk = NULL;
4845 /* Expect is allowed in 1.1, look for it */
4846 if (http_find_header(htx, hdr, &ctx, 0) &&
4847 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004848 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004849 return -1;
4850 http_remove_header(htx, &ctx);
4851 }
4852 }
4853 return 0;
4854}
4855
Christopher Faulet23a3c792018-11-28 10:01:23 +01004856/* Send a 100-Continue response to the client. It returns 0 on success and -1
4857 * on error. The response channel is updated accordingly.
4858 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004859static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004860{
4861 struct channel *res = &s->res;
4862 struct htx *htx = htx_from_buf(&res->buf);
4863 struct htx_sl *sl;
4864 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4865 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004866
4867 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4868 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4869 if (!sl)
4870 goto fail;
4871 sl->info.res.status = 100;
4872
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004873 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004874 goto fail;
4875
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004876 if (!http_forward_proxy_resp(s, 0))
4877 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004878 return 0;
4879
4880 fail:
4881 /* If an error occurred, remove the incomplete HTTP response from the
4882 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004883 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004884 return -1;
4885}
4886
Christopher Faulet12c51e22018-11-28 15:59:42 +01004887
Christopher Faulet0f226952018-10-22 09:29:56 +02004888/*
4889 * Capture headers from message <htx> according to header list <cap_hdr>, and
4890 * fill the <cap> pointers appropriately.
4891 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004892static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004893{
4894 struct cap_hdr *h;
4895 int32_t pos;
4896
Christopher Fauleta3f15502019-05-13 15:27:23 +02004897 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004898 struct htx_blk *blk = htx_get_blk(htx, pos);
4899 enum htx_blk_type type = htx_get_blk_type(blk);
4900 struct ist n, v;
4901
4902 if (type == HTX_BLK_EOH)
4903 break;
4904 if (type != HTX_BLK_HDR)
4905 continue;
4906
4907 n = htx_get_blk_name(htx, blk);
4908
4909 for (h = cap_hdr; h; h = h->next) {
4910 if (h->namelen && (h->namelen == n.len) &&
4911 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4912 if (cap[h->index] == NULL)
4913 cap[h->index] =
4914 pool_alloc(h->pool);
4915
4916 if (cap[h->index] == NULL) {
4917 ha_alert("HTTP capture : out of memory.\n");
4918 break;
4919 }
4920
4921 v = htx_get_blk_value(htx, blk);
4922 if (v.len > h->len)
4923 v.len = h->len;
4924
4925 memcpy(cap[h->index], v.ptr, v.len);
4926 cap[h->index][v.len]=0;
4927 }
4928 }
4929 }
4930}
4931
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004932/* Delete a value in a header between delimiters <from> and <next>. The header
4933 * itself is delimited by <start> and <end> pointers. The number of characters
4934 * displaced is returned, and the pointer to the first delimiter is updated if
4935 * required. The function tries as much as possible to respect the following
4936 * principles :
4937 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4938 * in which case <next> is simply removed
4939 * - set exactly one space character after the new first delimiter, unless there
4940 * are not enough characters in the block being moved to do so.
4941 * - remove unneeded spaces before the previous delimiter and after the new
4942 * one.
4943 *
4944 * It is the caller's responsibility to ensure that :
4945 * - <from> points to a valid delimiter or <start> ;
4946 * - <next> points to a valid delimiter or <end> ;
4947 * - there are non-space chars before <from>.
4948 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004949static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004950{
4951 char *prev = *from;
4952
4953 if (prev == start) {
4954 /* We're removing the first value. eat the semicolon, if <next>
4955 * is lower than <end> */
4956 if (next < end)
4957 next++;
4958
4959 while (next < end && HTTP_IS_SPHT(*next))
4960 next++;
4961 }
4962 else {
4963 /* Remove useless spaces before the old delimiter. */
4964 while (HTTP_IS_SPHT(*(prev-1)))
4965 prev--;
4966 *from = prev;
4967
4968 /* copy the delimiter and if possible a space if we're
4969 * not at the end of the line.
4970 */
4971 if (next < end) {
4972 *prev++ = *next++;
4973 if (prev + 1 < next)
4974 *prev++ = ' ';
4975 while (next < end && HTTP_IS_SPHT(*next))
4976 next++;
4977 }
4978 }
4979 memmove(prev, next, end - next);
4980 return (prev - next);
4981}
4982
Christopher Faulet0f226952018-10-22 09:29:56 +02004983
4984/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004985 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004986 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004987static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004988{
4989 struct ist dst = ist2(str, 0);
4990
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004991 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004992 goto end;
4993 if (dst.len + 1 > len)
4994 goto end;
4995 dst.ptr[dst.len++] = ' ';
4996
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004997 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004998 goto end;
4999 if (dst.len + 1 > len)
5000 goto end;
5001 dst.ptr[dst.len++] = ' ';
5002
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005003 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005004 end:
5005 return dst.len;
5006}
5007
5008/*
5009 * Print a debug line with a start line.
5010 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005011static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005012{
5013 struct session *sess = strm_sess(s);
5014 int max;
5015
5016 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5017 dir,
5018 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5019 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5020
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005021 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005022 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005023 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005024 trash.area[trash.data++] = ' ';
5025
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005026 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005027 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005028 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005029 trash.area[trash.data++] = ' ';
5030
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005031 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005032 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005033 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005034 trash.area[trash.data++] = '\n';
5035
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005036 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005037}
5038
5039/*
5040 * Print a debug line with a header.
5041 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005042static 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 +02005043{
5044 struct session *sess = strm_sess(s);
5045 int max;
5046
5047 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5048 dir,
5049 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5050 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5051
5052 max = n.len;
5053 UBOUND(max, trash.size - trash.data - 3);
5054 chunk_memcat(&trash, n.ptr, max);
5055 trash.area[trash.data++] = ':';
5056 trash.area[trash.data++] = ' ';
5057
5058 max = v.len;
5059 UBOUND(max, trash.size - trash.data - 1);
5060 chunk_memcat(&trash, v.ptr, max);
5061 trash.area[trash.data++] = '\n';
5062
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005063 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005064}
5065
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005066/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5067 * In case of allocation failure, everything allocated is freed and NULL is
5068 * returned. Otherwise the new transaction is assigned to the stream and
5069 * returned.
5070 */
5071struct http_txn *http_alloc_txn(struct stream *s)
5072{
5073 struct http_txn *txn = s->txn;
5074
5075 if (txn)
5076 return txn;
5077
5078 txn = pool_alloc(pool_head_http_txn);
5079 if (!txn)
5080 return txn;
5081
5082 s->txn = txn;
5083 return txn;
5084}
5085
5086void http_txn_reset_req(struct http_txn *txn)
5087{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005088 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005089 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5090}
5091
5092void http_txn_reset_res(struct http_txn *txn)
5093{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005094 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005095 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5096}
5097
5098/*
5099 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5100 * the required fields are properly allocated and that we only need to (re)init
5101 * them. This should be used before processing any new request.
5102 */
5103void http_init_txn(struct stream *s)
5104{
5105 struct http_txn *txn = s->txn;
5106 struct conn_stream *cs = objt_cs(s->si[0].end);
5107
5108 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5109 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5110 : 0);
5111 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005112 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005113 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005114
5115 txn->cookie_first_date = 0;
5116 txn->cookie_last_date = 0;
5117
5118 txn->srv_cookie = NULL;
5119 txn->cli_cookie = NULL;
5120 txn->uri = NULL;
5121
5122 http_txn_reset_req(txn);
5123 http_txn_reset_res(txn);
5124
5125 txn->req.chn = &s->req;
5126 txn->rsp.chn = &s->res;
5127
5128 txn->auth.method = HTTP_AUTH_UNKNOWN;
5129
5130 vars_init(&s->vars_txn, SCOPE_TXN);
5131 vars_init(&s->vars_reqres, SCOPE_REQ);
5132}
5133
5134/* to be used at the end of a transaction */
5135void http_end_txn(struct stream *s)
5136{
5137 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005138
5139 /* these ones will have been dynamically allocated */
5140 pool_free(pool_head_requri, txn->uri);
5141 pool_free(pool_head_capture, txn->cli_cookie);
5142 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005143 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005144
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005145 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005146 txn->uri = NULL;
5147 txn->srv_cookie = NULL;
5148 txn->cli_cookie = NULL;
5149
Christopher Faulet59399252019-11-07 14:27:52 +01005150 if (!LIST_ISEMPTY(&s->vars_txn.head))
5151 vars_prune(&s->vars_txn, s->sess, s);
5152 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5153 vars_prune(&s->vars_reqres, s->sess, s);
5154}
5155
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005156
5157DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005158
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005159__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005160static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005161{
5162}
5163
5164
5165/*
5166 * Local variables:
5167 * c-indent-level: 8
5168 * c-basic-offset: 8
5169 * End:
5170 */