blob: 6659f7e7b36efbb881cb771bf5b1c421056eb37c [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);
100 stream_inc_http_err_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200101 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200102 if (htx->flags & HTX_FL_PARSING_ERROR)
103 goto return_bad_req;
104 else
105 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100106 }
107
Christopher Faulete0768eb2018-10-03 16:38:02 +0200108 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200109 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200110
111 /* If there is data available for analysis, log the end of the idle time. */
Christopher Faulet7a6c5132020-09-30 13:49:56 +0200112 if (c_data(req) && s->logs.t_idle == -1)
113 s->logs.t_idle = sess->t_idle;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200114
Christopher Faulete0768eb2018-10-03 16:38:02 +0200115 /*
116 * Now we quickly check if we have found a full valid request.
117 * If not so, we check the FD and buffer states before leaving.
118 * A full request is indicated by the fact that we have seen
119 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
120 * requests are checked first. When waiting for a second request
121 * on a keep-alive stream, if we encounter and error, close, t/o,
122 * we note the error in the stream flags but don't set any state.
123 * Since the error will be noted there, it will not be counted by
124 * process_stream() as a frontend error.
125 * Last, we may increase some tracked counters' http request errors on
126 * the cases that are deliberately the client's fault. For instance,
127 * a timeout or connection reset is not counted as an error. However
128 * a bad request is.
129 */
Christopher Faulet29f17582019-05-23 11:03:26 +0200130 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200131 if (htx->flags & HTX_FL_UPGRADE)
132 goto failed_keep_alive;
133
Christopher Faulet9768c262018-10-22 09:34:31 +0200134 /* 1: have we encountered a read error ? */
135 if (req->flags & CF_READ_ERROR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200136 if (!(s->flags & SF_ERR_MASK))
137 s->flags |= SF_ERR_CLICL;
138
139 if (txn->flags & TX_WAIT_NEXT_RQ)
140 goto failed_keep_alive;
141
142 if (sess->fe->options & PR_O_IGNORE_PRB)
143 goto failed_keep_alive;
144
Christopher Faulet9768c262018-10-22 09:34:31 +0200145 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200146 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200147 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100148 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200149 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100150 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200151
Christopher Faulet9768c262018-10-22 09:34:31 +0200152 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200153 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet9768c262018-10-22 09:34:31 +0200154 req->analysers &= AN_REQ_FLT_END;
155
Christopher Faulete0768eb2018-10-03 16:38:02 +0200156 if (!(s->flags & SF_FINST_MASK))
157 s->flags |= SF_FINST_R;
158 return 0;
159 }
160
Christopher Faulet9768c262018-10-22 09:34:31 +0200161 /* 2: has the read timeout expired ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200162 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
163 if (!(s->flags & SF_ERR_MASK))
164 s->flags |= SF_ERR_CLITO;
165
166 if (txn->flags & TX_WAIT_NEXT_RQ)
167 goto failed_keep_alive;
168
169 if (sess->fe->options & PR_O_IGNORE_PRB)
170 goto failed_keep_alive;
171
Christopher Faulet9768c262018-10-22 09:34:31 +0200172 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200173 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200174 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100175 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200176 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100177 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200178
Christopher Faulet9768c262018-10-22 09:34:31 +0200179 txn->status = 408;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200180 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200181 req->analysers &= AN_REQ_FLT_END;
182
Christopher Faulete0768eb2018-10-03 16:38:02 +0200183 if (!(s->flags & SF_FINST_MASK))
184 s->flags |= SF_FINST_R;
185 return 0;
186 }
187
Christopher Faulet9768c262018-10-22 09:34:31 +0200188 /* 3: have we encountered a close ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200189 else if (req->flags & CF_SHUTR) {
190 if (!(s->flags & SF_ERR_MASK))
191 s->flags |= SF_ERR_CLICL;
192
193 if (txn->flags & TX_WAIT_NEXT_RQ)
194 goto failed_keep_alive;
195
196 if (sess->fe->options & PR_O_IGNORE_PRB)
197 goto failed_keep_alive;
198
Christopher Faulete0768eb2018-10-03 16:38:02 +0200199 stream_inc_http_err_ctr(s);
200 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200201 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100202 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200203 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100204 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200205
Christopher Faulet9768c262018-10-22 09:34:31 +0200206 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200207 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200208 req->analysers &= AN_REQ_FLT_END;
209
Christopher Faulete0768eb2018-10-03 16:38:02 +0200210 if (!(s->flags & SF_FINST_MASK))
211 s->flags |= SF_FINST_R;
212 return 0;
213 }
214
215 channel_dont_connect(req);
216 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
217 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100218
Christopher Faulet9768c262018-10-22 09:34:31 +0200219 if (sess->listener->options & LI_O_NOQUICKACK && htx_is_not_empty(htx) &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200220 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
221 /* We need more data, we have to re-enable quick-ack in case we
222 * previously disabled it, otherwise we might cause the client
223 * to delay next data.
224 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100225 conn_set_quickack(objt_conn(sess->origin), 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200226 }
Willy Tarreau1a18b542018-12-11 16:37:42 +0100227
Christopher Faulet47365272018-10-31 17:40:50 +0100228 if ((req->flags & CF_READ_PARTIAL) && (txn->flags & TX_WAIT_NEXT_RQ)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200229 /* If the client starts to talk, let's fall back to
230 * request timeout processing.
231 */
232 txn->flags &= ~TX_WAIT_NEXT_RQ;
233 req->analyse_exp = TICK_ETERNITY;
234 }
235
236 /* just set the request timeout once at the beginning of the request */
237 if (!tick_isset(req->analyse_exp)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100238 if ((txn->flags & TX_WAIT_NEXT_RQ) && tick_isset(s->be->timeout.httpka))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200239 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
240 else
241 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
242 }
243
244 /* we're not ready yet */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100245 DBG_TRACE_DEVEL("waiting for the request",
246 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200247 return 0;
248
249 failed_keep_alive:
250 /* Here we process low-level errors for keep-alive requests. In
251 * short, if the request is not the first one and it experiences
252 * a timeout, read error or shutdown, we just silently close so
253 * that the client can try again.
254 */
255 txn->status = 0;
256 msg->msg_state = HTTP_MSG_RQBEFORE;
257 req->analysers &= AN_REQ_FLT_END;
258 s->logs.logwait = 0;
259 s->logs.level = 0;
260 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200261 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100262 DBG_TRACE_DEVEL("leaving by closing K/A connection",
263 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200264 return 0;
265 }
266
Christopher Faulet9768c262018-10-22 09:34:31 +0200267 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200268 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200269 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200270
Christopher Faulet9768c262018-10-22 09:34:31 +0200271 /* kill the pending keep-alive timeout */
272 txn->flags &= ~TX_WAIT_NEXT_RQ;
273 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200274
Christopher Faulet29f17582019-05-23 11:03:26 +0200275 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200276 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100277
Christopher Faulet9768c262018-10-22 09:34:31 +0200278 /* 0: we might have to print this header in debug mode */
279 if (unlikely((global.mode & MODE_DEBUG) &&
280 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
281 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200282
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200283 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200284
Christopher Fauleta3f15502019-05-13 15:27:23 +0200285 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200286 struct htx_blk *blk = htx_get_blk(htx, pos);
287 enum htx_blk_type type = htx_get_blk_type(blk);
288
289 if (type == HTX_BLK_EOH)
290 break;
291 if (type != HTX_BLK_HDR)
292 continue;
293
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200294 http_debug_hdr("clihdr", s,
295 htx_get_blk_name(htx, blk),
296 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200297 }
298 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200299
300 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100301 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200302 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100303 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100304 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200305 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100306 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +0100307 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100308 if (sl->flags & HTX_SL_F_BODYLESS)
309 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200310
311 /* we can make use of server redirect on GET and HEAD */
312 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
313 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100314 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200315 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200316 goto return_bad_req;
317 }
318
319 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100320 * 2: check if the URI matches the monitor_uri. We have to do this for
321 * every request which gets in, because the monitor-uri is defined by
322 * the frontend. If the monitor-uri starts with a '/', the matching is
323 * done against the request's path. Otherwise, the request's uri is
324 * used. It is a workaround to let HTTP/2 health-checks work as
325 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200326 */
327 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Faulet6072beb2020-02-18 15:34:58 +0100328 ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)),
329 ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) ||
330 isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200331 /*
332 * We have found the monitor URI
333 */
334 struct acl_cond *cond;
335
336 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100337 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200338
339 /* Check if we want to fail this monitor request or not */
340 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
341 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
342
343 ret = acl_pass(ret);
344 if (cond->pol == ACL_COND_UNLESS)
345 ret = !ret;
346
347 if (ret) {
348 /* we fail this request, let's return 503 service unavail */
349 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200350 if (!(s->flags & SF_ERR_MASK))
351 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
352 goto return_prx_cond;
353 }
354 }
355
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800356 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200357 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200358 if (!(s->flags & SF_ERR_MASK))
359 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
360 goto return_prx_cond;
361 }
362
363 /*
364 * 3: Maybe we have to copy the original REQURI for the logs ?
365 * Note: we cannot log anymore if the request has been
366 * classified as invalid.
367 */
368 if (unlikely(s->logs.logwait & LW_REQ)) {
369 /* we have a complete HTTP request that we must log */
370 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200371 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200372
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200373 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200374 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200375
376 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
377 s->do_log(s);
378 } else {
379 ha_alert("HTTP logging : out of memory.\n");
380 }
381 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200382
Christopher Faulete0768eb2018-10-03 16:38:02 +0200383 /* if the frontend has "option http-use-proxy-header", we'll check if
384 * we have what looks like a proxied connection instead of a connection,
385 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
386 * Note that this is *not* RFC-compliant, however browsers and proxies
387 * happen to do that despite being non-standard :-(
388 * We consider that a request not beginning with either '/' or '*' is
389 * a proxied connection, which covers both "scheme://location" and
390 * CONNECT ip:port.
391 */
392 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100393 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200394 txn->flags |= TX_USE_PX_CONN;
395
Christopher Faulete0768eb2018-10-03 16:38:02 +0200396 /* 5: we may need to capture headers */
397 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200398 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200399
Christopher Faulete0768eb2018-10-03 16:38:02 +0200400 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200401 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200402 req->analysers |= AN_REQ_HTTP_BODY;
403
404 /*
405 * RFC7234#4:
406 * A cache MUST write through requests with methods
407 * that are unsafe (Section 4.2.1 of [RFC7231]) to
408 * the origin server; i.e., a cache is not allowed
409 * to generate a reply to such a request before
410 * having forwarded the request and having received
411 * a corresponding response.
412 *
413 * RFC7231#4.2.1:
414 * Of the request methods defined by this
415 * specification, the GET, HEAD, OPTIONS, and TRACE
416 * methods are defined to be safe.
417 */
418 if (likely(txn->meth == HTTP_METH_GET ||
419 txn->meth == HTTP_METH_HEAD ||
420 txn->meth == HTTP_METH_OPTIONS ||
421 txn->meth == HTTP_METH_TRACE))
422 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
423
424 /* end of job, return OK */
425 req->analysers &= ~an_bit;
426 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200427
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100428 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200429 return 1;
430
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200431 return_int_err:
432 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200433 if (!(s->flags & SF_ERR_MASK))
434 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100435 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200436 if (sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100437 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200438 goto return_prx_cond;
439
Christopher Faulete0768eb2018-10-03 16:38:02 +0200440 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200441 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100442 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200443 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100444 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200445 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200446
447 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200448 http_reply_and_close(s, txn->status, http_error_message(s));
449
Christopher Faulete0768eb2018-10-03 16:38:02 +0200450 if (!(s->flags & SF_ERR_MASK))
451 s->flags |= SF_ERR_PRXCOND;
452 if (!(s->flags & SF_FINST_MASK))
453 s->flags |= SF_FINST_R;
454
455 req->analysers &= AN_REQ_FLT_END;
456 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100457 DBG_TRACE_DEVEL("leaving on error",
458 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200459 return 0;
460}
461
462
463/* This stream analyser runs all HTTP request processing which is common to
464 * frontends and backends, which means blocking ACLs, filters, connection-close,
465 * reqadd, stats and redirects. This is performed for the designated proxy.
466 * It returns 1 if the processing can continue on next analysers, or zero if it
467 * either needs more data or wants to immediately abort the request (eg: deny,
468 * error, ...).
469 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200470int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200471{
472 struct session *sess = s->sess;
473 struct http_txn *txn = s->txn;
474 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200475 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200476 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200477 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200478 struct connection *conn = objt_conn(sess->origin);
479
480 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
481 /* we need more data */
482 goto return_prx_yield;
483 }
484
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100485 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200486
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100487 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200488
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200489 /* just in case we have some per-backend tracking. Only called the first
490 * execution of the analyser. */
491 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
492 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200493
494 /* evaluate http-request rules */
495 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100496 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200497
498 switch (verdict) {
499 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
500 goto return_prx_yield;
501
502 case HTTP_RULE_RES_CONT:
503 case HTTP_RULE_RES_STOP: /* nothing to do */
504 break;
505
506 case HTTP_RULE_RES_DENY: /* deny or tarpit */
507 if (txn->flags & TX_CLTARPIT)
508 goto tarpit;
509 goto deny;
510
511 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
512 goto return_prx_cond;
513
514 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
515 goto done;
516
517 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
518 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100519
520 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
521 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200522 }
523 }
524
525 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100526 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200527 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200528
Christopher Fauletff2759f2018-10-24 11:13:16 +0200529 ctx.blk = NULL;
530 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
531 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100532 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200533 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200534 }
535
536 /* OK at this stage, we know that the request was accepted according to
537 * the http-request rules, we can check for the stats. Note that the
538 * URI is detected *before* the req* rules in order not to be affected
539 * by a possible reqrep, while they are processed *after* so that a
540 * reqdeny can still block them. This clearly needs to change in 1.6!
541 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200542 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200543 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100544 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200545 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200546 if (!(s->flags & SF_ERR_MASK))
547 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100548 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200549 }
550
551 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200552 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100553 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200554 /* not all actions implemented: deny, allow, auth */
555
556 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
557 goto deny;
558
559 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
560 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100561
562 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
563 goto return_bad_req;
564
565 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
566 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200567 }
568
Christopher Faulet2571bc62019-03-01 11:44:26 +0100569 /* Proceed with the applets now. */
570 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200571 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100572 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200573
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200574 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100575 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100576
Christopher Faulete0768eb2018-10-03 16:38:02 +0200577 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
578 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
579 if (!(s->flags & SF_FINST_MASK))
580 s->flags |= SF_FINST_R;
581
582 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
583 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
584 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
585 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100586
587 req->flags |= CF_SEND_DONTWAIT;
588 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200589 goto done;
590 }
591
592 /* check whether we have some ACLs set to redirect this request */
593 list_for_each_entry(rule, &px->redirect_rules, list) {
594 if (rule->cond) {
595 int ret;
596
597 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
598 ret = acl_pass(ret);
599 if (rule->cond->pol == ACL_COND_UNLESS)
600 ret = !ret;
601 if (!ret)
602 continue;
603 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200604 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100605 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200606 goto done;
607 }
608
609 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
610 * If this happens, then the data will not come immediately, so we must
611 * send all what we have without waiting. Note that due to the small gain
612 * in waiting for the body of the request, it's easier to simply put the
613 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
614 * itself once used.
615 */
616 req->flags |= CF_SEND_DONTWAIT;
617
618 done: /* done with this analyser, continue with next ones that the calling
619 * points will have set, if any.
620 */
621 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500622 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200623 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100624 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200625 return 1;
626
627 tarpit:
628 /* Allow cookie logging
629 */
630 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200631 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200632
633 /* When a connection is tarpitted, we use the tarpit timeout,
634 * which may be the same as the connect timeout if unspecified.
635 * If unset, then set it to zero because we really want it to
636 * eventually expire. We build the tarpit as an analyser.
637 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100638 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200639
640 /* wipe the request out so that we can drop the connection early
641 * if the client closes first.
642 */
643 channel_dont_connect(req);
644
Christopher Faulete0768eb2018-10-03 16:38:02 +0200645 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
646 req->analysers |= AN_REQ_HTTP_TARPIT;
647 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
648 if (!req->analyse_exp)
649 req->analyse_exp = tick_add(now_ms, 0);
650 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100651 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100652 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100653 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200654 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100655 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200656 goto done_without_exp;
657
658 deny: /* this request was blocked (denied) */
659
660 /* Allow cookie logging
661 */
662 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200663 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200664
Christopher Faulete0768eb2018-10-03 16:38:02 +0200665 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200666 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100667 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100668 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100669 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200670 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100671 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100672 goto return_prx_err;
673
674 return_int_err:
675 txn->status = 500;
676 if (!(s->flags & SF_ERR_MASK))
677 s->flags |= SF_ERR_INTERNAL;
678 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100679 if (s->flags & SF_BE_ASSIGNED)
680 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100681 if (sess->listener->counters)
682 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
683 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200684
685 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200686 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100687 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200688 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100689 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100690 /* fall through */
691
692 return_prx_err:
693 http_reply_and_close(s, txn->status, http_error_message(s));
694 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200695
696 return_prx_cond:
697 if (!(s->flags & SF_ERR_MASK))
698 s->flags |= SF_ERR_PRXCOND;
699 if (!(s->flags & SF_FINST_MASK))
700 s->flags |= SF_FINST_R;
701
702 req->analysers &= AN_REQ_FLT_END;
703 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100704 DBG_TRACE_DEVEL("leaving on error",
705 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200706 return 0;
707
708 return_prx_yield:
709 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100710 DBG_TRACE_DEVEL("waiting for more data",
711 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200712 return 0;
713}
714
715/* This function performs all the processing enabled for the current request.
716 * It returns 1 if the processing can continue on next analysers, or zero if it
717 * needs more data, encounters an error, or wants to immediately abort the
718 * request. It relies on buffers flags, and updates s->req.analysers.
719 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200720int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200721{
722 struct session *sess = s->sess;
723 struct http_txn *txn = s->txn;
724 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200725 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200726 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
727
728 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
729 /* we need more data */
730 channel_dont_connect(req);
731 return 0;
732 }
733
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100734 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200735
736 /*
737 * Right now, we know that we have processed the entire headers
738 * and that unwanted requests have been filtered out. We can do
739 * whatever we want with the remaining request. Also, now we
740 * may have separate values for ->fe, ->be.
741 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100742 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200743
744 /*
745 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200746 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200747 */
748 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100749 struct htx_sl *sl;
750 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200751
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200752 if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200753 if (!(s->flags & SF_ERR_MASK))
754 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100755 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200756 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200757 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100758 uri = htx_sl_req_uri(sl);
759 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200760
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200761 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200762 goto return_bad_req;
763
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200764 s->target = &s->be->obj_type;
765 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
766
Christopher Faulete0768eb2018-10-03 16:38:02 +0200767 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200768 * uri.ptr and path.ptr (excluded). If it was not found, we need
769 * to replace from all the uri by a single "/".
770 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500771 * Instead of rewriting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100772 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200773 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200774 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100775 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200776 }
777
778 /*
779 * 7: Now we can work with the cookies.
780 * Note that doing so might move headers in the request, but
781 * the fields will stay coherent and the URI will not move.
782 * This should only be performed in the backend.
783 */
784 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200785 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200786
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100787 /* 8: Generate unique ID if a "unique-id-format" is defined.
788 *
789 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
790 * fetches only available in the HTTP request processing stage.
791 */
792 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100793 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200794
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100795 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100796 if (!(s->flags & SF_ERR_MASK))
797 s->flags |= SF_ERR_RESOURCE;
798 goto return_int_err;
799 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200800
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100801 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100802 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100803 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100804 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200805 }
806
807 /*
808 * 9: add X-Forwarded-For if either the frontend or the backend
809 * asks for it.
810 */
811 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200812 struct http_hdr_ctx ctx = { .blk = NULL };
813 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
814 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
815
Christopher Faulete0768eb2018-10-03 16:38:02 +0200816 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200817 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200818 /* The header is set to be added only if none is present
819 * and we found it, so don't do anything.
820 */
821 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200822 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200823 /* Add an X-Forwarded-For header unless the source IP is
824 * in the 'except' network range.
825 */
826 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200827 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200828 != sess->fe->except_net.s_addr) &&
829 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200830 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200831 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200832 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200833
834 /* Note: we rely on the backend to get the header name to be used for
835 * x-forwarded-for, because the header is really meant for the backends.
836 * However, if the backend did not specify any option, we have to rely
837 * on the frontend's header name.
838 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200839 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
840 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100841 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200842 }
843 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200844 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200845 /* FIXME: for the sake of completeness, we should also support
846 * 'except' here, although it is mostly useless in this case.
847 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200848 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200849
Christopher Faulete0768eb2018-10-03 16:38:02 +0200850 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200851 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200852 pn, sizeof(pn));
853
854 /* Note: we rely on the backend to get the header name to be used for
855 * x-forwarded-for, because the header is really meant for the backends.
856 * However, if the backend did not specify any option, we have to rely
857 * on the frontend's header name.
858 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200859 chunk_printf(&trash, "%s", pn);
860 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100861 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200862 }
863 }
864
865 /*
866 * 10: add X-Original-To if either the frontend or the backend
867 * asks for it.
868 */
869 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
870
871 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200872 if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET && conn_get_dst(cli_conn)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200873 /* Add an X-Original-To header unless the destination IP is
874 * in the 'except' network range.
875 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200876 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200877 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200878 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200879 != sess->fe->except_to.s_addr) &&
880 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200881 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200882 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200883 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200884 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200885
886 /* Note: we rely on the backend to get the header name to be used for
887 * x-original-to, because the header is really meant for the backends.
888 * However, if the backend did not specify any option, we have to rely
889 * on the frontend's header name.
890 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200891 if (s->be->orgto_hdr_len)
892 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
893 else
894 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200895
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200896 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
897 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100898 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200899 }
900 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200901 }
902
Christopher Faulete0768eb2018-10-03 16:38:02 +0200903 /* If we have no server assigned yet and we're balancing on url_param
904 * with a POST request, we may be interested in checking the body for
905 * that parameter. This will be done in another analyser.
906 */
907 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100908 s->txn->meth == HTTP_METH_POST &&
909 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200910 channel_dont_connect(req);
911 req->analysers |= AN_REQ_HTTP_BODY;
912 }
913
914 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
915 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100916
Christopher Faulete0768eb2018-10-03 16:38:02 +0200917 /* We expect some data from the client. Unless we know for sure
918 * we already have a full request, we have to re-enable quick-ack
919 * in case we previously disabled it, otherwise we might cause
920 * the client to delay further data.
921 */
922 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200923 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100924 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200925
926 /*************************************************************
927 * OK, that's finished for the headers. We have done what we *
928 * could. Let's switch to the DATA state. *
929 ************************************************************/
930 req->analyse_exp = TICK_ETERNITY;
931 req->analysers &= ~an_bit;
932
933 s->logs.tv_request = now;
934 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100935 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200936 return 1;
937
Christopher Fauletb8a53712019-12-16 11:29:38 +0100938 return_int_err:
939 txn->status = 500;
940 if (!(s->flags & SF_ERR_MASK))
941 s->flags |= SF_ERR_INTERNAL;
942 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100943 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100944 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100945 if (sess->listener->counters)
946 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
947 goto return_prx_cond;
948
Christopher Faulete0768eb2018-10-03 16:38:02 +0200949 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200950 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100951 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200952 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100953 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100954 /* fall through */
955
956 return_prx_cond:
957 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200958
959 if (!(s->flags & SF_ERR_MASK))
960 s->flags |= SF_ERR_PRXCOND;
961 if (!(s->flags & SF_FINST_MASK))
962 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100963
964 req->analysers &= AN_REQ_FLT_END;
965 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100966 DBG_TRACE_DEVEL("leaving on error",
967 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200968 return 0;
969}
970
971/* This function is an analyser which processes the HTTP tarpit. It always
972 * returns zero, at the beginning because it prevents any other processing
973 * from occurring, and at the end because it terminates the request.
974 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200975int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200976{
977 struct http_txn *txn = s->txn;
978
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100979 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200980 /* This connection is being tarpitted. The CLIENT side has
981 * already set the connect expiration date to the right
982 * timeout. We just have to check that the client is still
983 * there and that the timeout has not expired.
984 */
985 channel_dont_connect(req);
986 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100987 !tick_is_expired(req->analyse_exp, now_ms)) {
988 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
989 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200990 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100991 }
992
Christopher Faulete0768eb2018-10-03 16:38:02 +0200993
994 /* We will set the queue timer to the time spent, just for
995 * logging purposes. We fake a 500 server error, so that the
996 * attacker will not suspect his connection has been tarpitted.
997 * It will not cause trouble to the logs because we can exclude
998 * the tarpitted connections by filtering on the 'PT' status flags.
999 */
1000 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1001
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02001002 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001003
1004 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001005 req->analysers &= AN_REQ_FLT_END;
1006 req->analyse_exp = TICK_ETERNITY;
1007
1008 if (!(s->flags & SF_ERR_MASK))
1009 s->flags |= SF_ERR_PRXCOND;
1010 if (!(s->flags & SF_FINST_MASK))
1011 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001012
1013 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001014 return 0;
1015}
1016
1017/* This function is an analyser which waits for the HTTP request body. It waits
1018 * for either the buffer to be full, or the full advertised contents to have
1019 * reached the buffer. It must only be called after the standard HTTP request
1020 * processing has occurred, because it expects the request to be parsed and will
1021 * look for the Expect header. It may send a 100-Continue interim response. It
1022 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
1023 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
1024 * needs to read more data, or 1 once it has completed its analysis.
1025 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001026int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001027{
1028 struct session *sess = s->sess;
1029 struct http_txn *txn = s->txn;
1030 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001031 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001032
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001033 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001034
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001035 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001036
Willy Tarreau4236f032019-03-05 10:43:32 +01001037 if (htx->flags & HTX_FL_PARSING_ERROR)
1038 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001039 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1040 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001041
Christopher Faulet63c69a92020-11-16 16:03:35 +01001042 /* CONNECT requests have no body */
1043 if (txn->meth == HTTP_METH_CONNECT)
1044 goto http_end;
1045
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001046 if (msg->msg_state < HTTP_MSG_BODY)
1047 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001048
Christopher Faulete0768eb2018-10-03 16:38:02 +02001049 /* We have to parse the HTTP request body to find any required data.
1050 * "balance url_param check_post" should have been the only way to get
1051 * into this. We were brought here after HTTP header analysis, so all
1052 * related structures are ready.
1053 */
1054
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001055 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001056 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001057 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001058 }
1059
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001060 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001061
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001062 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1063 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001064 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001065 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001066 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001067 goto http_end;
1068
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001069 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001070 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1071 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001072 if (!(s->flags & SF_ERR_MASK))
1073 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001074 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1075 if (sess->listener->counters)
1076 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1077 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001078 }
1079
1080 /* we get here if we need to wait for more data */
1081 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1082 /* Not enough data. We'll re-use the http-request
1083 * timeout here. Ideally, we should set the timeout
1084 * relative to the accept() date. We just set the
1085 * request timeout once at the beginning of the
1086 * request.
1087 */
1088 channel_dont_connect(req);
1089 if (!tick_isset(req->analyse_exp))
1090 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001091 DBG_TRACE_DEVEL("waiting for more data",
1092 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001093 return 0;
1094 }
1095
1096 http_end:
1097 /* The situation will not evolve, so let's give up on the analysis. */
1098 s->logs.tv_request = now; /* update the request timer to reflect full request */
1099 req->analysers &= ~an_bit;
1100 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001101 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001102 return 1;
1103
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001104 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001105 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001106 if (!(s->flags & SF_ERR_MASK))
1107 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001108 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001109 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +01001110 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001111 if (sess->listener->counters)
1112 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
1113 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001114
Christopher Faulete0768eb2018-10-03 16:38:02 +02001115 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001116 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001117 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1118 if (sess->listener->counters)
1119 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1120 /* fall through */
1121
1122 return_prx_cond:
1123 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001124
1125 if (!(s->flags & SF_ERR_MASK))
1126 s->flags |= SF_ERR_PRXCOND;
1127 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001128 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001129
Christopher Faulete0768eb2018-10-03 16:38:02 +02001130 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001131 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001132 DBG_TRACE_DEVEL("leaving on error",
1133 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001134 return 0;
1135}
1136
1137/* This function is an analyser which forwards request body (including chunk
1138 * sizes if any). It is called as soon as we must forward, even if we forward
1139 * zero byte. The only situation where it must not be called is when we're in
1140 * tunnel mode and we want to forward till the close. It's used both to forward
1141 * remaining data and to resync after end of body. It expects the msg_state to
1142 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1143 * read more data, or 1 once we can go on with next request or end the stream.
1144 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1145 * bytes of pending data + the headers if not already done.
1146 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001147int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001148{
1149 struct session *sess = s->sess;
1150 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001151 struct http_msg *msg = &txn->req;
1152 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001153 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001154 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001155
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001156 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001157
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001158 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001159
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001160 if (htx->flags & HTX_FL_PARSING_ERROR)
1161 goto return_bad_req;
1162 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1163 goto return_int_err;
1164
Christopher Faulete0768eb2018-10-03 16:38:02 +02001165 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1166 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1167 /* Output closed while we were sending data. We must abort and
1168 * wake the other side up.
1169 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001170
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001171 /* Don't abort yet if we had L7 retries activated and it
1172 * was a write error, we may recover.
1173 */
1174 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001175 (s->si[1].flags & SI_FL_L7_RETRY)) {
1176 DBG_TRACE_DEVEL("leaving on L7 retry",
1177 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001178 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001179 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001180 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001181 http_end_request(s);
1182 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001183 DBG_TRACE_DEVEL("leaving on error",
1184 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001185 return 1;
1186 }
1187
1188 /* Note that we don't have to send 100-continue back because we don't
1189 * need the data to complete our job, and it's up to the server to
1190 * decide whether to return 100, 417 or anything else in return of
1191 * an "Expect: 100-continue" header.
1192 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001193 if (msg->msg_state == HTTP_MSG_BODY)
1194 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001195
Christopher Faulete0768eb2018-10-03 16:38:02 +02001196 /* in most states, we should abort in case of early close */
1197 channel_auto_close(req);
1198
1199 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001200 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001201 if (req->flags & CF_EOI)
1202 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001203 }
1204 else {
1205 /* We can't process the buffer's contents yet */
1206 req->flags |= CF_WAKE_WRITE;
1207 goto missing_data_or_waiting;
1208 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001209 }
1210
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001211 if (msg->msg_state >= HTTP_MSG_ENDING)
1212 goto ending;
1213
1214 if (txn->meth == HTTP_METH_CONNECT) {
1215 msg->msg_state = HTTP_MSG_ENDING;
1216 goto ending;
1217 }
1218
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001219 /* Forward input data. We get it by removing all outgoing data not
1220 * forwarded yet from HTX data size. If there are some data filters, we
1221 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001222 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001223 if (HAS_REQ_DATA_FILTERS(s)) {
1224 ret = flt_http_payload(s, msg, htx->data);
1225 if (ret < 0)
1226 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001227 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001228 }
1229 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001230 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001231 if (msg->flags & HTTP_MSGF_XFER_LEN)
1232 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001233 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001234
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001235 if (htx->data != co_data(req))
1236 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001237
Christopher Faulet9768c262018-10-22 09:34:31 +02001238 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001239 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1240 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001241 */
1242 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1243 goto missing_data_or_waiting;
1244
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001245 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001246
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001247 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001248 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1249
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001250 /* other states, ENDING...TUNNEL */
1251 if (msg->msg_state >= HTTP_MSG_DONE)
1252 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001253
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001254 if (HAS_REQ_DATA_FILTERS(s)) {
1255 ret = flt_http_end(s, msg);
1256 if (ret <= 0) {
1257 if (!ret)
1258 goto missing_data_or_waiting;
1259 goto return_bad_req;
1260 }
1261 }
1262
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001263 if (txn->meth == HTTP_METH_CONNECT)
1264 msg->msg_state = HTTP_MSG_TUNNEL;
1265 else {
1266 msg->msg_state = HTTP_MSG_DONE;
1267 req->to_forward = 0;
1268 }
1269
1270 done:
1271 /* we don't want to forward closes on DONE except in tunnel mode. */
1272 if (!(txn->flags & TX_CON_WANT_TUN))
1273 channel_dont_close(req);
1274
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001275 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001276 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001277 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001278 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1279 if (req->flags & CF_SHUTW) {
1280 /* request errors are most likely due to the
1281 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001282 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001283 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001284 goto return_bad_req;
1285 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001286 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001287 return 1;
1288 }
1289
1290 /* If "option abortonclose" is set on the backend, we want to monitor
1291 * the client's connection and forward any shutdown notification to the
1292 * server, which will decide whether to close or to go on processing the
1293 * request. We only do that in tunnel mode, and not in other modes since
1294 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001295 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001296 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001297 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001298 s->si[1].flags |= SI_FL_NOLINGER;
1299 channel_auto_close(req);
1300 }
1301 else if (s->txn->meth == HTTP_METH_POST) {
1302 /* POST requests may require to read extra CRLF sent by broken
1303 * browsers and which could cause an RST to be sent upon close
1304 * on some systems (eg: Linux). */
1305 channel_auto_read(req);
1306 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001307 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1308 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001309 return 0;
1310
1311 missing_data_or_waiting:
1312 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001313 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001314 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001315
1316 waiting:
1317 /* waiting for the last bits to leave the buffer */
1318 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001319 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001320
1321 /* When TE: chunked is used, we need to get there again to parse remaining
1322 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1323 * And when content-length is used, we never want to let the possible
1324 * shutdown be forwarded to the other side, as the state machine will
1325 * take care of it once the client responds. It's also important to
1326 * prevent TIME_WAITs from accumulating on the backend side, and for
1327 * HTTP/2 where the last frame comes with a shutdown.
1328 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001329 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001330 channel_dont_close(req);
1331
1332 /* We know that more data are expected, but we couldn't send more that
1333 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1334 * system knows it must not set a PUSH on this first part. Interactive
1335 * modes are already handled by the stream sock layer. We must not do
1336 * this in content-length mode because it could present the MSG_MORE
1337 * flag with the last block of forwarded data, which would cause an
1338 * additional delay to be observed by the receiver.
1339 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001340 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001341 req->flags |= CF_EXPECT_MORE;
1342
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001343 DBG_TRACE_DEVEL("waiting for more data to forward",
1344 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001345 return 0;
1346
Christopher Faulet93e02d82019-03-08 14:18:50 +01001347 return_cli_abort:
1348 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1349 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001350 if (sess->listener->counters)
1351 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001352 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001353 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001354 if (!(s->flags & SF_ERR_MASK))
1355 s->flags |= SF_ERR_CLICL;
1356 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001357 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001358
1359 return_srv_abort:
1360 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1361 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001362 if (sess->listener->counters)
1363 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001364 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001365 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001366 if (!(s->flags & SF_ERR_MASK))
1367 s->flags |= SF_ERR_SRVCL;
1368 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001369 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001370
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001371 return_int_err:
1372 if (!(s->flags & SF_ERR_MASK))
1373 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001374 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001375 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001376 if (sess->listener->counters)
1377 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001378 if (objt_server(s->target))
1379 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001380 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001381 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001382
Christopher Faulet93e02d82019-03-08 14:18:50 +01001383 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001384 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001385 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001386 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001387 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001388 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001389
Christopher Fauletb8a53712019-12-16 11:29:38 +01001390 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001391 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001392 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001393 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001394 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001395 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001396 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001397 }
1398 req->analysers &= AN_REQ_FLT_END;
1399 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 +01001400 if (!(s->flags & SF_ERR_MASK))
1401 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001402 if (!(s->flags & SF_FINST_MASK))
1403 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001404 DBG_TRACE_DEVEL("leaving on error ",
1405 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001406 return 0;
1407}
1408
Olivier Houcharda254a372019-04-05 15:30:12 +02001409/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1410/* Returns 0 if we can attempt to retry, -1 otherwise */
1411static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1412{
1413 struct channel *req, *res;
1414 int co_data;
1415
1416 si->conn_retries--;
1417 if (si->conn_retries < 0)
1418 return -1;
1419
Willy Tarreau223995e2019-05-04 10:38:31 +02001420 if (objt_server(s->target))
1421 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1422 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1423
Olivier Houcharda254a372019-04-05 15:30:12 +02001424 req = &s->req;
1425 res = &s->res;
1426 /* Remove any write error from the request, and read error from the response */
1427 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1428 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1429 res->analysers = 0;
1430 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001431 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001432 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001433 si->exp = TICK_ETERNITY;
1434 res->rex = TICK_ETERNITY;
1435 res->to_forward = 0;
1436 res->analyse_exp = TICK_ETERNITY;
1437 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001438 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001439 si_release_endpoint(&s->si[1]);
1440 b_free(&req->buf);
1441 /* Swap the L7 buffer with the channel buffer */
1442 /* We know we stored the co_data as b_data, so get it there */
1443 co_data = b_data(&si->l7_buffer);
1444 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1445 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1446
1447 co_set_data(req, co_data);
1448 b_reset(&res->buf);
1449 co_set_data(res, 0);
1450 return 0;
1451}
1452
Christopher Faulete0768eb2018-10-03 16:38:02 +02001453/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1454 * processing can continue on next analysers, or zero if it either needs more
1455 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1456 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1457 * when it has nothing left to do, and may remove any analyser when it wants to
1458 * abort.
1459 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001460int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001461{
Christopher Faulet9768c262018-10-22 09:34:31 +02001462 /*
1463 * We will analyze a complete HTTP response to check the its syntax.
1464 *
1465 * Once the start line and all headers are received, we may perform a
1466 * capture of the error (if any), and we will set a few fields. We also
1467 * logging and finally headers capture.
1468 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001469 struct session *sess = s->sess;
1470 struct http_txn *txn = s->txn;
1471 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001472 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001473 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001474 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001475 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001476 int n;
1477
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001478 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001479
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001480 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001481
Willy Tarreau4236f032019-03-05 10:43:32 +01001482 /* Parsing errors are caught here */
1483 if (htx->flags & HTX_FL_PARSING_ERROR)
1484 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001485 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1486 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001487
Christopher Faulete0768eb2018-10-03 16:38:02 +02001488 /*
1489 * Now we quickly check if we have found a full valid response.
1490 * If not so, we check the FD and buffer states before leaving.
1491 * A full response is indicated by the fact that we have seen
1492 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1493 * responses are checked first.
1494 *
1495 * Depending on whether the client is still there or not, we
1496 * may send an error response back or not. Note that normally
1497 * we should only check for HTTP status there, and check I/O
1498 * errors somewhere else.
1499 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001500 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001501 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001502 /* 1: have we encountered a read error ? */
1503 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001504 struct connection *conn = NULL;
1505
Olivier Houchard865d8392019-05-03 22:46:27 +02001506 if (objt_cs(s->si[1].end))
1507 conn = objt_cs(s->si[1].end)->conn;
1508
1509 if (si_b->flags & SI_FL_L7_RETRY &&
1510 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001511 /* If we arrive here, then CF_READ_ERROR was
1512 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001513 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001514 * the SI_FL_L7_RETRY flag, so it's ok not
1515 * to check s->be->retry_type.
1516 */
1517 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1518 return 0;
1519 }
1520
Olivier Houchard6db16992019-05-17 15:40:49 +02001521 if (txn->flags & TX_NOT_FIRST)
1522 goto abort_keep_alive;
1523
Olivier Houcharda798bf52019-03-08 18:52:00 +01001524 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001525 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001526 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001527 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001528 }
1529
Christopher Faulete0768eb2018-10-03 16:38:02 +02001530 rep->analysers &= AN_RES_FLT_END;
1531 txn->status = 502;
1532
1533 /* Check to see if the server refused the early data.
1534 * If so, just send a 425
1535 */
Willy Tarreauee99aaf2020-06-23 05:58:20 +02001536 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001537 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001538 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001539 do_l7_retry(s, si_b) == 0) {
1540 DBG_TRACE_DEVEL("leaving on L7 retry",
1541 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001542 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001543 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001544 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001545 }
1546
1547 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001548 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001549
1550 if (!(s->flags & SF_ERR_MASK))
1551 s->flags |= SF_ERR_SRVCL;
1552 if (!(s->flags & SF_FINST_MASK))
1553 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001554 DBG_TRACE_DEVEL("leaving on error",
1555 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001556 return 0;
1557 }
1558
Christopher Faulet9768c262018-10-22 09:34:31 +02001559 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001560 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001561 if ((si_b->flags & SI_FL_L7_RETRY) &&
1562 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001563 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1564 DBG_TRACE_DEVEL("leaving on L7 retry",
1565 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001566 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001567 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001568 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001569 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001570 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001571 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001572 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001573 }
1574
Christopher Faulete0768eb2018-10-03 16:38:02 +02001575 rep->analysers &= AN_RES_FLT_END;
1576 txn->status = 504;
1577 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001578 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001579
1580 if (!(s->flags & SF_ERR_MASK))
1581 s->flags |= SF_ERR_SRVTO;
1582 if (!(s->flags & SF_FINST_MASK))
1583 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001584 DBG_TRACE_DEVEL("leaving on error",
1585 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001586 return 0;
1587 }
1588
Christopher Faulet9768c262018-10-22 09:34:31 +02001589 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001590 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001591 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1592 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001593 if (sess->listener->counters)
1594 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001595 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001596 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001597
1598 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001599 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001600 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001601
1602 if (!(s->flags & SF_ERR_MASK))
1603 s->flags |= SF_ERR_CLICL;
1604 if (!(s->flags & SF_FINST_MASK))
1605 s->flags |= SF_FINST_H;
1606
1607 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001608 DBG_TRACE_DEVEL("leaving on error",
1609 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001610 return 0;
1611 }
1612
Christopher Faulet9768c262018-10-22 09:34:31 +02001613 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001614 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001615 if ((si_b->flags & SI_FL_L7_RETRY) &&
1616 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001617 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1618 DBG_TRACE_DEVEL("leaving on L7 retry",
1619 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001620 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001621 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001622 }
1623
Olivier Houchard6db16992019-05-17 15:40:49 +02001624 if (txn->flags & TX_NOT_FIRST)
1625 goto abort_keep_alive;
1626
Olivier Houcharda798bf52019-03-08 18:52:00 +01001627 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001628 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001629 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001630 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001631 }
1632
Christopher Faulete0768eb2018-10-03 16:38:02 +02001633 rep->analysers &= AN_RES_FLT_END;
1634 txn->status = 502;
1635 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001636 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001637
1638 if (!(s->flags & SF_ERR_MASK))
1639 s->flags |= SF_ERR_SRVCL;
1640 if (!(s->flags & SF_FINST_MASK))
1641 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001642 DBG_TRACE_DEVEL("leaving on error",
1643 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001644 return 0;
1645 }
1646
Christopher Faulet9768c262018-10-22 09:34:31 +02001647 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001648 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001649 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001650 goto abort_keep_alive;
1651
Olivier Houcharda798bf52019-03-08 18:52:00 +01001652 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001653 if (objt_server(s->target))
1654 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001655 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001656
1657 if (!(s->flags & SF_ERR_MASK))
1658 s->flags |= SF_ERR_CLICL;
1659 if (!(s->flags & SF_FINST_MASK))
1660 s->flags |= SF_FINST_H;
1661
1662 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001663 DBG_TRACE_DEVEL("leaving on error",
1664 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001665 return 0;
1666 }
1667
1668 channel_dont_close(rep);
1669 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001670 DBG_TRACE_DEVEL("waiting for more data",
1671 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001672 return 0;
1673 }
1674
1675 /* More interesting part now : we know that we have a complete
1676 * response which at least looks like HTTP. We have an indicator
1677 * of each header's length, so we can parse them quickly.
1678 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001679 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001680 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001681 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001682
Christopher Faulet9768c262018-10-22 09:34:31 +02001683 /* 0: we might have to print this header in debug mode */
1684 if (unlikely((global.mode & MODE_DEBUG) &&
1685 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1686 int32_t pos;
1687
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001688 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001689
Christopher Fauleta3f15502019-05-13 15:27:23 +02001690 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001691 struct htx_blk *blk = htx_get_blk(htx, pos);
1692 enum htx_blk_type type = htx_get_blk_type(blk);
1693
1694 if (type == HTX_BLK_EOH)
1695 break;
1696 if (type != HTX_BLK_HDR)
1697 continue;
1698
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001699 http_debug_hdr("srvhdr", s,
1700 htx_get_blk_name(htx, blk),
1701 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001702 }
1703 }
1704
Christopher Faulet03599112018-11-27 11:21:21 +01001705 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001706 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001707 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001708 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001709 if (sl->flags & HTX_SL_F_XFER_LEN) {
1710 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001711 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001712 if (sl->flags & HTX_SL_F_BODYLESS)
1713 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001714 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001715
1716 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001717 if (n < 1 || n > 5)
1718 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001719
Christopher Faulete0768eb2018-10-03 16:38:02 +02001720 /* when the client triggers a 4xx from the server, it's most often due
1721 * to a missing object or permission. These events should be tracked
1722 * because if they happen often, it may indicate a brute force or a
1723 * vulnerability scan.
1724 */
1725 if (n == 4)
1726 stream_inc_http_err_ctr(s);
1727
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001728 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001729 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001730 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1731 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001732
Christopher Faulete0768eb2018-10-03 16:38:02 +02001733 /* Adjust server's health based on status code. Note: status codes 501
1734 * and 505 are triggered on demand by client request, so we must not
1735 * count them as server failures.
1736 */
1737 if (objt_server(s->target)) {
1738 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001739 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001740 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001741 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001742 }
1743
1744 /*
1745 * We may be facing a 100-continue response, or any other informational
1746 * 1xx response which is non-final, in which case this is not the right
1747 * response, and we're waiting for the next one. Let's allow this response
1748 * to go to the client and wait for the next one. There's an exception for
1749 * 101 which is used later in the code to switch protocols.
1750 */
1751 if (txn->status < 200 &&
1752 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001753 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001754 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001755 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001756 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001757 txn->status = 0;
1758 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001759 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001760 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001761 }
1762
1763 /*
1764 * 2: check for cacheability.
1765 */
1766
1767 switch (txn->status) {
1768 case 200:
1769 case 203:
1770 case 204:
1771 case 206:
1772 case 300:
1773 case 301:
1774 case 404:
1775 case 405:
1776 case 410:
1777 case 414:
1778 case 501:
1779 break;
1780 default:
1781 /* RFC7231#6.1:
1782 * Responses with status codes that are defined as
1783 * cacheable by default (e.g., 200, 203, 204, 206,
1784 * 300, 301, 404, 405, 410, 414, and 501 in this
1785 * specification) can be reused by a cache with
1786 * heuristic expiration unless otherwise indicated
1787 * by the method definition or explicit cache
1788 * controls [RFC7234]; all other status codes are
1789 * not cacheable by default.
1790 */
1791 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1792 break;
1793 }
1794
1795 /*
1796 * 3: we may need to capture headers
1797 */
1798 s->logs.logwait &= ~LW_RESP;
1799 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001800 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001801
Christopher Faulet9768c262018-10-22 09:34:31 +02001802 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001803 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1804 txn->status == 101)) {
1805 /* Either we've established an explicit tunnel, or we're
1806 * switching the protocol. In both cases, we're very unlikely
1807 * to understand the next protocols. We have to switch to tunnel
1808 * mode, so that we transfer the request and responses then let
1809 * this protocol pass unmodified. When we later implement specific
1810 * parsers for such protocols, we'll want to check the Upgrade
1811 * header which contains information about that protocol for
1812 * responses with status 101 (eg: see RFC2817 about TLS).
1813 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001814 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001815 }
1816
Christopher Faulet61608322018-11-23 16:23:45 +01001817 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1818 * 407 (Proxy-Authenticate) responses and set the connection to private
1819 */
1820 srv_conn = cs_conn(objt_cs(s->si[1].end));
1821 if (srv_conn) {
1822 struct ist hdr;
1823 struct http_hdr_ctx ctx;
1824
1825 if (txn->status == 401)
1826 hdr = ist("WWW-Authenticate");
1827 else if (txn->status == 407)
1828 hdr = ist("Proxy-Authenticate");
1829 else
1830 goto end;
1831
1832 ctx.blk = NULL;
1833 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001834 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1835 * possibly followed by blanks and a base64 string, the connection
1836 * is private. Since it's a mess to deal with, we only check for
1837 * values starting with "NTLM" or "Nego". Note that often multiple
1838 * headers are sent by the server there.
1839 */
1840 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001841 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001842 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001843 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001844 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001845 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001846 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001847 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001848 }
Christopher Faulet61608322018-11-23 16:23:45 +01001849 }
1850 }
1851
1852 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001853 /* we want to have the response time before we start processing it */
1854 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1855
1856 /* end of job, return OK */
1857 rep->analysers &= ~an_bit;
1858 rep->analyse_exp = TICK_ETERNITY;
1859 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001860 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001861 return 1;
1862
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001863 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001864 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001865 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001866 if (sess->listener->counters)
1867 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001868 if (objt_server(s->target))
1869 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001870 txn->status = 500;
1871 if (!(s->flags & SF_ERR_MASK))
1872 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001873 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001874
1875 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001876 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001877 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001878 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001879 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001880 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001881 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001882 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001883 do_l7_retry(s, si_b) == 0) {
1884 DBG_TRACE_DEVEL("leaving on L7 retry",
1885 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001886 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001887 }
Christopher Faulet47365272018-10-31 17:40:50 +01001888 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001889 /* fall through */
1890
Christopher Fauletb8a53712019-12-16 11:29:38 +01001891 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001892 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001893
1894 if (!(s->flags & SF_ERR_MASK))
1895 s->flags |= SF_ERR_PRXCOND;
1896 if (!(s->flags & SF_FINST_MASK))
1897 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001898
1899 s->si[1].flags |= SI_FL_NOLINGER;
1900 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001901 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001902 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001903 DBG_TRACE_DEVEL("leaving on error",
1904 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001905 return 0;
1906
Christopher Faulete0768eb2018-10-03 16:38:02 +02001907 abort_keep_alive:
1908 /* A keep-alive request to the server failed on a network error.
1909 * The client is required to retry. We need to close without returning
1910 * any other information so that the client retries.
1911 */
1912 txn->status = 0;
1913 rep->analysers &= AN_RES_FLT_END;
1914 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001915 s->logs.logwait = 0;
1916 s->logs.level = 0;
1917 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001918 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001919 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1920 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001921 return 0;
1922}
1923
1924/* This function performs all the processing enabled for the current response.
1925 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1926 * and updates s->res.analysers. It might make sense to explode it into several
1927 * other functions. It works like process_request (see indications above).
1928 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001929int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001930{
1931 struct session *sess = s->sess;
1932 struct http_txn *txn = s->txn;
1933 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001934 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001935 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001936 enum rule_result ret = HTTP_RULE_RES_CONT;
1937
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001938 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1939 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001940
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001941 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001942
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001943 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001944
1945 /* The stats applet needs to adjust the Connection header but we don't
1946 * apply any filter there.
1947 */
1948 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1949 rep->analysers &= ~an_bit;
1950 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001951 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001952 }
1953
1954 /*
1955 * We will have to evaluate the filters.
1956 * As opposed to version 1.2, now they will be evaluated in the
1957 * filters order and not in the header order. This means that
1958 * each filter has to be validated among all headers.
1959 *
1960 * Filters are tried with ->be first, then with ->fe if it is
1961 * different from ->be.
1962 *
1963 * Maybe we are in resume condiion. In this case I choose the
1964 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001965 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001966 * the process with the first one.
1967 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001968 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001969 * pointer and the ->fe rule list. If it doesn't match, I initialize
1970 * the loop with the ->be.
1971 */
1972 if (s->current_rule_list == &sess->fe->http_res_rules)
1973 cur_proxy = sess->fe;
1974 else
1975 cur_proxy = s->be;
1976 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001977 /* evaluate http-response rules */
1978 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001979 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001980
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001981 switch (ret) {
1982 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1983 goto return_prx_yield;
1984
1985 case HTTP_RULE_RES_CONT:
1986 case HTTP_RULE_RES_STOP: /* nothing to do */
1987 break;
1988
1989 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1990 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001991
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001992 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1993 goto return_prx_cond;
1994
1995 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001996 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001997
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001998 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1999 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002000
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002001 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
2002 goto return_int_err;
2003 }
2004
2005 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002006
Christopher Faulete0768eb2018-10-03 16:38:02 +02002007 /* check whether we're already working on the frontend */
2008 if (cur_proxy == sess->fe)
2009 break;
2010 cur_proxy = sess->fe;
2011 }
2012
Christopher Faulete0768eb2018-10-03 16:38:02 +02002013 /* OK that's all we can do for 1xx responses */
2014 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002015 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002016
2017 /*
2018 * Now check for a server cookie.
2019 */
2020 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002021 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002022
2023 /*
2024 * Check for cache-control or pragma headers if required.
2025 */
2026 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002027 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002028
2029 /*
2030 * Add server cookie in the response if needed
2031 */
2032 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
2033 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
2034 (!(s->flags & SF_DIRECT) ||
2035 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
2036 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
2037 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
2038 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
2039 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
2040 !(s->flags & SF_IGNORE_PRST)) {
2041 /* the server is known, it's not the one the client requested, or the
2042 * cookie's last seen date needs to be refreshed. We have to
2043 * insert a set-cookie here, except if we want to insert only on POST
2044 * requests and this one isn't. Note that servers which don't have cookies
2045 * (eg: some backup servers) will return a full cookie removal request.
2046 */
2047 if (!objt_server(s->target)->cookie) {
2048 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002049 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02002050 s->be->cookie_name);
2051 }
2052 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002053 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002054
2055 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2056 /* emit last_date, which is mandatory */
2057 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2058 s30tob64((date.tv_sec+3) >> 2,
2059 trash.area + trash.data);
2060 trash.data += 5;
2061
2062 if (s->be->cookie_maxlife) {
2063 /* emit first_date, which is either the original one or
2064 * the current date.
2065 */
2066 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2067 s30tob64(txn->cookie_first_date ?
2068 txn->cookie_first_date >> 2 :
2069 (date.tv_sec+3) >> 2,
2070 trash.area + trash.data);
2071 trash.data += 5;
2072 }
2073 }
2074 chunk_appendf(&trash, "; path=/");
2075 }
2076
2077 if (s->be->cookie_domain)
2078 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2079
2080 if (s->be->ck_opts & PR_CK_HTTPONLY)
2081 chunk_appendf(&trash, "; HttpOnly");
2082
2083 if (s->be->ck_opts & PR_CK_SECURE)
2084 chunk_appendf(&trash, "; Secure");
2085
Christopher Faulet2f533902020-01-21 11:06:48 +01002086 if (s->be->cookie_attrs)
2087 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
2088
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002089 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002090 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002091
2092 txn->flags &= ~TX_SCK_MASK;
2093 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2094 /* the server did not change, only the date was updated */
2095 txn->flags |= TX_SCK_UPDATED;
2096 else
2097 txn->flags |= TX_SCK_INSERTED;
2098
2099 /* Here, we will tell an eventual cache on the client side that we don't
2100 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2101 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2102 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2103 */
2104 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2105
2106 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2107
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002108 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002109 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002110 }
2111 }
2112
2113 /*
2114 * Check if result will be cacheable with a cookie.
2115 * We'll block the response if security checks have caught
2116 * nasty things such as a cacheable cookie.
2117 */
2118 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2119 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2120 (s->be->options & PR_O_CHK_CACHE)) {
2121 /* we're in presence of a cacheable response containing
2122 * a set-cookie header. We'll block it as requested by
2123 * the 'checkcache' option, and send an alert.
2124 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002125 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2126 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2127 send_log(s->be, LOG_ALERT,
2128 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2129 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002130 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002131 }
2132
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002133 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002134 /*
2135 * Evaluate after-response rules before forwarding the response. rules
2136 * from the backend are evaluated first, then one from the frontend if
2137 * it differs.
2138 */
2139 if (!http_eval_after_res_rules(s))
2140 goto return_int_err;
2141
Christopher Faulete0768eb2018-10-03 16:38:02 +02002142 /* Always enter in the body analyzer */
2143 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2144 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2145
2146 /* if the user wants to log as soon as possible, without counting
2147 * bytes from the server, then this is the right moment. We have
2148 * to temporarily assign bytes_out to log what we currently have.
2149 */
2150 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2151 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002152 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002153 s->do_log(s);
2154 s->logs.bytes_out = 0;
2155 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002156
Christopher Fauletb8a53712019-12-16 11:29:38 +01002157 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002158 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002159 rep->analysers &= ~an_bit;
2160 rep->analyse_exp = TICK_ETERNITY;
2161 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002162
Christopher Fauletb8a53712019-12-16 11:29:38 +01002163 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002164 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002165 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002166 if (sess->listener->counters)
2167 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002168 if (objt_server(s->target))
2169 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002170 goto return_prx_err;
2171
2172 return_int_err:
2173 txn->status = 500;
2174 if (!(s->flags & SF_ERR_MASK))
2175 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002176 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002177 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2178 if (objt_server(s->target))
2179 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002180 if (objt_server(s->target))
2181 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002182 goto return_prx_err;
2183
2184 return_bad_res:
2185 txn->status = 502;
Christopher Fauleta20a6532020-02-05 10:16:41 +01002186 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2187 if (objt_server(s->target)) {
2188 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2189 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2190 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002191 /* fall through */
2192
2193 return_prx_err:
2194 http_reply_and_close(s, txn->status, http_error_message(s));
2195 /* fall through */
2196
2197 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002198 s->logs.t_data = -1; /* was not a valid response */
2199 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002200
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002201 if (!(s->flags & SF_ERR_MASK))
2202 s->flags |= SF_ERR_PRXCOND;
2203 if (!(s->flags & SF_FINST_MASK))
2204 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002205
Christopher Faulete58c0002020-03-02 16:21:01 +01002206 rep->analysers &= AN_RES_FLT_END;
2207 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002208 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002209 DBG_TRACE_DEVEL("leaving on error",
2210 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002211 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002212
2213 return_prx_yield:
2214 channel_dont_close(rep);
2215 DBG_TRACE_DEVEL("waiting for more data",
2216 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2217 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002218}
2219
2220/* This function is an analyser which forwards response body (including chunk
2221 * sizes if any). It is called as soon as we must forward, even if we forward
2222 * zero byte. The only situation where it must not be called is when we're in
2223 * tunnel mode and we want to forward till the close. It's used both to forward
2224 * remaining data and to resync after end of body. It expects the msg_state to
2225 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2226 * read more data, or 1 once we can go on with next request or end the stream.
2227 *
2228 * It is capable of compressing response data both in content-length mode and
2229 * in chunked mode. The state machines follows different flows depending on
2230 * whether content-length and chunked modes are used, since there are no
2231 * trailers in content-length :
2232 *
2233 * chk-mode cl-mode
2234 * ,----- BODY -----.
2235 * / \
2236 * V size > 0 V chk-mode
2237 * .--> SIZE -------------> DATA -------------> CRLF
2238 * | | size == 0 | last byte |
2239 * | v final crlf v inspected |
2240 * | TRAILERS -----------> DONE |
2241 * | |
2242 * `----------------------------------------------'
2243 *
2244 * Compression only happens in the DATA state, and must be flushed in final
2245 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2246 * is performed at once on final states for all bytes parsed, or when leaving
2247 * on missing data.
2248 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002249int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002250{
2251 struct session *sess = s->sess;
2252 struct http_txn *txn = s->txn;
2253 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002254 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002255 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002256
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002257 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002258
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002259 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002260
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002261 if (htx->flags & HTX_FL_PARSING_ERROR)
2262 goto return_bad_res;
2263 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2264 goto return_int_err;
2265
Christopher Faulete0768eb2018-10-03 16:38:02 +02002266 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002267 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002268 /* Output closed while we were sending data. We must abort and
2269 * wake the other side up.
2270 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002271 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002272 http_end_response(s);
2273 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002274 DBG_TRACE_DEVEL("leaving on error",
2275 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002276 return 1;
2277 }
2278
Christopher Faulet9768c262018-10-22 09:34:31 +02002279 if (msg->msg_state == HTTP_MSG_BODY)
2280 msg->msg_state = HTTP_MSG_DATA;
2281
Christopher Faulete0768eb2018-10-03 16:38:02 +02002282 /* in most states, we should abort in case of early close */
2283 channel_auto_close(res);
2284
Christopher Faulete0768eb2018-10-03 16:38:02 +02002285 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002286 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002287 if (res->flags & CF_EOI)
2288 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002289 }
2290 else {
2291 /* We can't process the buffer's contents yet */
2292 res->flags |= CF_WAKE_WRITE;
2293 goto missing_data_or_waiting;
2294 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002295 }
2296
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002297 if (msg->msg_state >= HTTP_MSG_ENDING)
2298 goto ending;
2299
2300 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2301 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2302 msg->msg_state = HTTP_MSG_ENDING;
2303 goto ending;
2304 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002305
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002306 /* Forward input data. We get it by removing all outgoing data not
2307 * forwarded yet from HTX data size. If there are some data filters, we
2308 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002309 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002310 if (HAS_RSP_DATA_FILTERS(s)) {
2311 ret = flt_http_payload(s, msg, htx->data);
2312 if (ret < 0)
2313 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002314 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002315 }
2316 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002317 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002318 if (msg->flags & HTTP_MSGF_XFER_LEN)
2319 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002320 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002321
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002322 if (htx->data != co_data(res))
2323 goto missing_data_or_waiting;
2324
2325 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2326 msg->msg_state = HTTP_MSG_ENDING;
2327 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002328 }
2329
Christopher Faulet9768c262018-10-22 09:34:31 +02002330 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002331 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2332 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002333 */
2334 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2335 goto missing_data_or_waiting;
2336
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002337 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002338
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002339 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002340 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2341
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002342 /* other states, ENDING...TUNNEL */
2343 if (msg->msg_state >= HTTP_MSG_DONE)
2344 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002345
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002346 if (HAS_RSP_DATA_FILTERS(s)) {
2347 ret = flt_http_end(s, msg);
2348 if (ret <= 0) {
2349 if (!ret)
2350 goto missing_data_or_waiting;
2351 goto return_bad_res;
2352 }
2353 }
2354
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002355 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2356 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2357 msg->msg_state = HTTP_MSG_TUNNEL;
2358 goto ending;
2359 }
2360 else {
2361 msg->msg_state = HTTP_MSG_DONE;
2362 res->to_forward = 0;
2363 }
2364
2365 done:
2366
2367 channel_dont_close(res);
2368
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002369 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002370 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002371 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002372 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2373 if (res->flags & CF_SHUTW) {
2374 /* response errors are most likely due to the
2375 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002376 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002377 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002378 goto return_bad_res;
2379 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002380 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002381 return 1;
2382 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002383 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2384 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002385 return 0;
2386
2387 missing_data_or_waiting:
2388 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002389 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002390
2391 /* stop waiting for data if the input is closed before the end. If the
2392 * client side was already closed, it means that the client has aborted,
2393 * so we don't want to count this as a server abort. Otherwise it's a
2394 * server abort.
2395 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002396 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002397 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002398 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002399 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002400 if (htx_is_empty(htx))
2401 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002402 }
2403
Christopher Faulete0768eb2018-10-03 16:38:02 +02002404 /* When TE: chunked is used, we need to get there again to parse
2405 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002406 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2407 * are filters registered on the stream, we don't want to forward a
2408 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002409 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002410 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002411 channel_dont_close(res);
2412
2413 /* We know that more data are expected, but we couldn't send more that
2414 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2415 * system knows it must not set a PUSH on this first part. Interactive
2416 * modes are already handled by the stream sock layer. We must not do
2417 * this in content-length mode because it could present the MSG_MORE
2418 * flag with the last block of forwarded data, which would cause an
2419 * additional delay to be observed by the receiver.
2420 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002421 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002422 res->flags |= CF_EXPECT_MORE;
2423
2424 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002425 DBG_TRACE_DEVEL("waiting for more data to forward",
2426 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002427 return 0;
2428
Christopher Faulet93e02d82019-03-08 14:18:50 +01002429 return_srv_abort:
2430 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2431 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002432 if (sess->listener->counters)
2433 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002434 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002435 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002436 if (!(s->flags & SF_ERR_MASK))
2437 s->flags |= SF_ERR_SRVCL;
2438 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002439
Christopher Faulet93e02d82019-03-08 14:18:50 +01002440 return_cli_abort:
2441 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2442 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002443 if (sess->listener->counters)
2444 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002445 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002446 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002447 if (!(s->flags & SF_ERR_MASK))
2448 s->flags |= SF_ERR_CLICL;
2449 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002450
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002451 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002452 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002453 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002454 if (sess->listener->counters)
2455 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002456 if (objt_server(s->target))
2457 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002458 if (!(s->flags & SF_ERR_MASK))
2459 s->flags |= SF_ERR_INTERNAL;
2460 goto return_error;
2461
Christopher Faulet93e02d82019-03-08 14:18:50 +01002462 return_bad_res:
2463 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2464 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002465 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002466 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2467 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002468 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002469 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002470 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002471
Christopher Faulet93e02d82019-03-08 14:18:50 +01002472 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002473 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002474 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002475 res->analysers &= AN_RES_FLT_END;
2476 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 +02002477 if (!(s->flags & SF_FINST_MASK))
2478 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002479 DBG_TRACE_DEVEL("leaving on error",
2480 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002481 return 0;
2482}
2483
Christopher Fauletf2824e62018-10-01 12:12:37 +02002484/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002485 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002486 * as too large a request to build a valid response.
2487 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002488int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002489{
Christopher Faulet99daf282018-11-28 22:58:13 +01002490 struct channel *req = &s->req;
2491 struct channel *res = &s->res;
2492 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002493 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002494 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002495 struct ist status, reason, location;
2496 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002497 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002498
2499 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002500 if (!chunk) {
2501 if (!(s->flags & SF_ERR_MASK))
2502 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002503 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002504 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002505
Christopher Faulet99daf282018-11-28 22:58:13 +01002506 /*
2507 * Create the location
2508 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002509 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002510 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002511 case REDIRECT_TYPE_SCHEME: {
2512 struct http_hdr_ctx ctx;
2513 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002514
Christopher Faulet99daf282018-11-28 22:58:13 +01002515 host = ist("");
2516 ctx.blk = NULL;
2517 if (http_find_header(htx, ist("Host"), &ctx, 0))
2518 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002519
Christopher Faulet297fbb42019-05-13 14:41:27 +02002520 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002521 path = http_get_path(htx_sl_req_uri(sl));
2522 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002523 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002524 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2525 int qs = 0;
2526 while (qs < path.len) {
2527 if (*(path.ptr + qs) == '?') {
2528 path.len = qs;
2529 break;
2530 }
2531 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002532 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002533 }
2534 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002535 else
2536 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002537
Christopher Faulet99daf282018-11-28 22:58:13 +01002538 if (rule->rdr_str) { /* this is an old "redirect" rule */
2539 /* add scheme */
2540 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2541 goto fail;
2542 }
2543 else {
2544 /* add scheme with executing log format */
2545 chunk->data += build_logline(s, chunk->area + chunk->data,
2546 chunk->size - chunk->data,
2547 &rule->rdr_fmt);
2548 }
2549 /* add "://" + host + path */
2550 if (!chunk_memcat(chunk, "://", 3) ||
2551 !chunk_memcat(chunk, host.ptr, host.len) ||
2552 !chunk_memcat(chunk, path.ptr, path.len))
2553 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002554
Christopher Faulet99daf282018-11-28 22:58:13 +01002555 /* append a slash at the end of the location if needed and missing */
2556 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2557 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2558 if (chunk->data + 1 >= chunk->size)
2559 goto fail;
2560 chunk->area[chunk->data++] = '/';
2561 }
2562 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002563 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002564
Christopher Faulet99daf282018-11-28 22:58:13 +01002565 case REDIRECT_TYPE_PREFIX: {
2566 struct ist path;
2567
Christopher Faulet297fbb42019-05-13 14:41:27 +02002568 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002569 path = http_get_path(htx_sl_req_uri(sl));
2570 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002571 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002572 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2573 int qs = 0;
2574 while (qs < path.len) {
2575 if (*(path.ptr + qs) == '?') {
2576 path.len = qs;
2577 break;
2578 }
2579 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002580 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002581 }
2582 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002583 else
2584 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002585
Christopher Faulet99daf282018-11-28 22:58:13 +01002586 if (rule->rdr_str) { /* this is an old "redirect" rule */
2587 /* add prefix. Note that if prefix == "/", we don't want to
2588 * add anything, otherwise it makes it hard for the user to
2589 * configure a self-redirection.
2590 */
2591 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2592 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2593 goto fail;
2594 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002595 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002596 else {
2597 /* add prefix with executing log format */
2598 chunk->data += build_logline(s, chunk->area + chunk->data,
2599 chunk->size - chunk->data,
2600 &rule->rdr_fmt);
2601 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002602
Christopher Faulet99daf282018-11-28 22:58:13 +01002603 /* add path */
2604 if (!chunk_memcat(chunk, path.ptr, path.len))
2605 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002606
Christopher Faulet99daf282018-11-28 22:58:13 +01002607 /* append a slash at the end of the location if needed and missing */
2608 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2609 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2610 if (chunk->data + 1 >= chunk->size)
2611 goto fail;
2612 chunk->area[chunk->data++] = '/';
2613 }
2614 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002615 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002616 case REDIRECT_TYPE_LOCATION:
2617 default:
2618 if (rule->rdr_str) { /* this is an old "redirect" rule */
2619 /* add location */
2620 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2621 goto fail;
2622 }
2623 else {
2624 /* add location with executing log format */
2625 chunk->data += build_logline(s, chunk->area + chunk->data,
2626 chunk->size - chunk->data,
2627 &rule->rdr_fmt);
2628 }
2629 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002630 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002631 location = ist2(chunk->area, chunk->data);
2632
2633 /*
2634 * Create the 30x response
2635 */
2636 switch (rule->code) {
2637 case 308:
2638 status = ist("308");
2639 reason = ist("Permanent Redirect");
2640 break;
2641 case 307:
2642 status = ist("307");
2643 reason = ist("Temporary Redirect");
2644 break;
2645 case 303:
2646 status = ist("303");
2647 reason = ist("See Other");
2648 break;
2649 case 301:
2650 status = ist("301");
2651 reason = ist("Moved Permanently");
2652 break;
2653 case 302:
2654 default:
2655 status = ist("302");
2656 reason = ist("Found");
2657 break;
2658 }
2659
Christopher Faulet08e66462019-05-23 16:44:59 +02002660 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2661 close = 1;
2662
Christopher Faulet99daf282018-11-28 22:58:13 +01002663 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002664 /* Trim any possible response */
2665 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002666 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2667 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2668 if (!sl)
2669 goto fail;
2670 sl->info.res.status = rule->code;
2671 s->txn->status = rule->code;
2672
Christopher Faulet08e66462019-05-23 16:44:59 +02002673 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2674 goto fail;
2675
2676 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002677 !htx_add_header(htx, ist("Location"), location))
2678 goto fail;
2679
2680 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2681 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2682 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002683 }
2684
2685 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002686 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2687 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002688 }
2689
Christopher Faulet99daf282018-11-28 22:58:13 +01002690 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2691 goto fail;
2692
Kevin Zhu96b36392020-01-07 09:42:55 +01002693 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002694 if (!http_forward_proxy_resp(s, 1))
2695 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002696
Christopher Faulet60b33a52020-01-28 09:18:10 +01002697 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2698 /* let's log the request time */
2699 s->logs.tv_request = now;
2700 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002701
Christopher Faulet60b33a52020-01-28 09:18:10 +01002702 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2703 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2704 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002705
2706 if (!(s->flags & SF_ERR_MASK))
2707 s->flags |= SF_ERR_LOCAL;
2708 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002709 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002710
Christopher Faulet99daf282018-11-28 22:58:13 +01002711 free_trash_chunk(chunk);
2712 return 1;
2713
2714 fail:
2715 /* If an error occurred, remove the incomplete HTTP response from the
2716 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002717 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002718 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002719 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002720}
2721
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002722/* Replace all headers matching the name <name>. The header value is replaced if
2723 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2724 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2725 * values are evaluated one by one. It returns 0 on success and -1 on error.
2726 */
2727int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2728 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002729{
2730 struct http_hdr_ctx ctx;
2731 struct buffer *output = get_trash_chunk();
2732
Christopher Faulet72333522018-10-24 11:25:02 +02002733 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002734 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002735 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2736 continue;
2737
2738 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2739 if (output->data == -1)
2740 return -1;
2741 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2742 return -1;
2743 }
2744 return 0;
2745}
2746
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002747/* This function executes one of the set-{method,path,query,uri} actions. It
2748 * takes the string from the variable 'replace' with length 'len', then modifies
2749 * the relevant part of the request line accordingly. Then it updates various
2750 * pointers to the next elements which were moved, and the total buffer length.
2751 * It finds the action to be performed in p[2], previously filled by function
2752 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2753 * error, though this can be revisited when this code is finally exploited.
2754 *
2755 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002756 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002757 *
2758 * In query string case, the mark question '?' must be set at the start of the
2759 * string by the caller, event if the replacement query string is empty.
2760 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002761int http_req_replace_stline(int action, const char *replace, int len,
2762 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002763{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002764 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002765
2766 switch (action) {
2767 case 0: // method
2768 if (!http_replace_req_meth(htx, ist2(replace, len)))
2769 return -1;
2770 break;
2771
2772 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002773 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002774 return -1;
2775 break;
2776
2777 case 2: // query
2778 if (!http_replace_req_query(htx, ist2(replace, len)))
2779 return -1;
2780 break;
2781
2782 case 3: // uri
2783 if (!http_replace_req_uri(htx, ist2(replace, len)))
2784 return -1;
2785 break;
2786
Christopher Faulet312294f2020-09-02 17:17:44 +02002787 case 4: // path + query
2788 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2789 return -1;
2790 break;
2791
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002792 default:
2793 return -1;
2794 }
2795 return 0;
2796}
2797
2798/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002799 * variable <status> contains the new status code. This function never fails. It
2800 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002801 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002802int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002803{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002804 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002805 char *res;
2806
2807 chunk_reset(&trash);
2808 res = ultoa_o(status, trash.area, trash.size);
2809 trash.data = res - trash.area;
2810
2811 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002812 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002813 const char *str = http_get_reason(status);
2814 reason = ist2(str, strlen(str));
2815 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002816
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002817 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002818 return -1;
2819 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002820}
2821
Christopher Faulet3e964192018-10-24 11:39:23 +02002822/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2823 * transaction <txn>. Returns the verdict of the first rule that prevents
2824 * further processing of the request (auth, deny, ...), and defaults to
2825 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2826 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2827 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2828 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2829 * status.
2830 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002831static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002832 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002833{
2834 struct session *sess = strm_sess(s);
2835 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002836 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002837 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002838 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002839
Christopher Faulet3e964192018-10-24 11:39:23 +02002840 /* If "the current_rule_list" match the executed rule list, we are in
2841 * resume condition. If a resume is needed it is always in the action
2842 * and never in the ACL or converters. In this case, we initialise the
2843 * current rule, and go to the action execution point.
2844 */
2845 if (s->current_rule) {
2846 rule = s->current_rule;
2847 s->current_rule = NULL;
2848 if (s->current_rule_list == rules)
2849 goto resume_execution;
2850 }
2851 s->current_rule_list = rules;
2852
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002853 /* start the ruleset evaluation in strict mode */
2854 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002855
Christopher Faulet3e964192018-10-24 11:39:23 +02002856 list_for_each_entry(rule, rules, list) {
2857 /* check optional condition */
2858 if (rule->cond) {
2859 int ret;
2860
2861 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2862 ret = acl_pass(ret);
2863
2864 if (rule->cond->pol == ACL_COND_UNLESS)
2865 ret = !ret;
2866
2867 if (!ret) /* condition not matched */
2868 continue;
2869 }
2870
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002871 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002872 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002873 /* Always call the action function if defined */
2874 if (rule->action_ptr) {
2875 if ((s->req.flags & CF_READ_ERROR) ||
2876 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2877 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002878 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002879
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002880 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002881 case ACT_RET_CONT:
2882 break;
2883 case ACT_RET_STOP:
2884 rule_ret = HTTP_RULE_RES_STOP;
2885 goto end;
2886 case ACT_RET_YIELD:
2887 s->current_rule = rule;
2888 rule_ret = HTTP_RULE_RES_YIELD;
2889 goto end;
2890 case ACT_RET_ERR:
2891 rule_ret = HTTP_RULE_RES_ERROR;
2892 goto end;
2893 case ACT_RET_DONE:
2894 rule_ret = HTTP_RULE_RES_DONE;
2895 goto end;
2896 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002897 if (txn->status == -1)
2898 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002899 rule_ret = HTTP_RULE_RES_DENY;
2900 goto end;
2901 case ACT_RET_ABRT:
2902 rule_ret = HTTP_RULE_RES_ABRT;
2903 goto end;
2904 case ACT_RET_INV:
2905 rule_ret = HTTP_RULE_RES_BADREQ;
2906 goto end;
2907 }
2908 continue; /* eval the next rule */
2909 }
2910
2911 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002912 switch (rule->action) {
2913 case ACT_ACTION_ALLOW:
2914 rule_ret = HTTP_RULE_RES_STOP;
2915 goto end;
2916
2917 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002918 txn->status = rule->arg.http_reply->status;
2919 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002920 rule_ret = HTTP_RULE_RES_DENY;
2921 goto end;
2922
2923 case ACT_HTTP_REQ_TARPIT:
2924 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002925 txn->status = rule->arg.http_reply->status;
2926 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002927 rule_ret = HTTP_RULE_RES_DENY;
2928 goto end;
2929
Christopher Faulet3e964192018-10-24 11:39:23 +02002930 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002931 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002932 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002933 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002934 goto end;
2935
2936 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002937 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002938 break;
2939
2940 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002941 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002942 break;
2943
2944 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002945 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002946 break;
2947
2948 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002949 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002950 break;
2951
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002952 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002953 default:
2954 break;
2955 }
2956 }
2957
2958 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002959 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002960 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002961 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002962
Christopher Faulet3e964192018-10-24 11:39:23 +02002963 /* we reached the end of the rules, nothing to report */
2964 return rule_ret;
2965}
2966
2967/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2968 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2969 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2970 * is returned, the process can continue the evaluation of next rule list. If
2971 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2972 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002973 * must be returned. If *YIELD is returned, the caller must call again the
2974 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002975 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002976static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2977 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002978{
2979 struct session *sess = strm_sess(s);
2980 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002981 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002982 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002983 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002984
Christopher Faulet3e964192018-10-24 11:39:23 +02002985 /* If "the current_rule_list" match the executed rule list, we are in
2986 * resume condition. If a resume is needed it is always in the action
2987 * and never in the ACL or converters. In this case, we initialise the
2988 * current rule, and go to the action execution point.
2989 */
2990 if (s->current_rule) {
2991 rule = s->current_rule;
2992 s->current_rule = NULL;
2993 if (s->current_rule_list == rules)
2994 goto resume_execution;
2995 }
2996 s->current_rule_list = rules;
2997
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002998 /* start the ruleset evaluation in strict mode */
2999 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003000
Christopher Faulet3e964192018-10-24 11:39:23 +02003001 list_for_each_entry(rule, rules, list) {
3002 /* check optional condition */
3003 if (rule->cond) {
3004 int ret;
3005
3006 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3007 ret = acl_pass(ret);
3008
3009 if (rule->cond->pol == ACL_COND_UNLESS)
3010 ret = !ret;
3011
3012 if (!ret) /* condition not matched */
3013 continue;
3014 }
3015
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003016 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02003017resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003018
3019 /* Always call the action function if defined */
3020 if (rule->action_ptr) {
3021 if ((s->req.flags & CF_READ_ERROR) ||
3022 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3023 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003024 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003025
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003026 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003027 case ACT_RET_CONT:
3028 break;
3029 case ACT_RET_STOP:
3030 rule_ret = HTTP_RULE_RES_STOP;
3031 goto end;
3032 case ACT_RET_YIELD:
3033 s->current_rule = rule;
3034 rule_ret = HTTP_RULE_RES_YIELD;
3035 goto end;
3036 case ACT_RET_ERR:
3037 rule_ret = HTTP_RULE_RES_ERROR;
3038 goto end;
3039 case ACT_RET_DONE:
3040 rule_ret = HTTP_RULE_RES_DONE;
3041 goto end;
3042 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01003043 if (txn->status == -1)
3044 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003045 rule_ret = HTTP_RULE_RES_DENY;
3046 goto end;
3047 case ACT_RET_ABRT:
3048 rule_ret = HTTP_RULE_RES_ABRT;
3049 goto end;
3050 case ACT_RET_INV:
3051 rule_ret = HTTP_RULE_RES_BADREQ;
3052 goto end;
3053 }
3054 continue; /* eval the next rule */
3055 }
3056
3057 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003058 switch (rule->action) {
3059 case ACT_ACTION_ALLOW:
3060 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3061 goto end;
3062
3063 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02003064 txn->status = rule->arg.http_reply->status;
3065 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003066 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003067 goto end;
3068
3069 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003070 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003071 break;
3072
3073 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003074 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003075 break;
3076
3077 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003078 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003079 break;
3080
3081 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003082 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003083 break;
3084
Christopher Faulet3e964192018-10-24 11:39:23 +02003085 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003086 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003087 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003088 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003089 goto end;
3090
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003091 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003092 default:
3093 break;
3094 }
3095 }
3096
3097 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003098 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003099 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003100 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003101
Christopher Faulet3e964192018-10-24 11:39:23 +02003102 /* we reached the end of the rules, nothing to report */
3103 return rule_ret;
3104}
3105
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003106/* Executes backend and frontend http-after-response rules for the stream <s>,
3107 * in that order. it return 1 on success and 0 on error. It is the caller
3108 * responsibility to catch error or ignore it. If it catches it, this function
3109 * may be called a second time, for the internal error.
3110 */
3111int http_eval_after_res_rules(struct stream *s)
3112{
3113 struct session *sess = s->sess;
3114 enum rule_result ret = HTTP_RULE_RES_CONT;
3115
Christopher Faulet507479b2020-05-15 12:29:46 +02003116 /* Eval after-response ruleset only if the reply is not const */
3117 if (s->txn->flags & TX_CONST_REPLY)
3118 goto end;
3119
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003120 /* prune the request variables if not already done and swap to the response variables. */
3121 if (s->vars_reqres.scope != SCOPE_RES) {
3122 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3123 vars_prune(&s->vars_reqres, s->sess, s);
3124 vars_init(&s->vars_reqres, SCOPE_RES);
3125 }
3126
3127 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3128 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3129 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3130
Christopher Faulet507479b2020-05-15 12:29:46 +02003131 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003132 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3133 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3134}
3135
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003136/*
3137 * Manage client-side cookie. It can impact performance by about 2% so it is
3138 * desirable to call it only when needed. This code is quite complex because
3139 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3140 * highly recommended not to touch this part without a good reason !
3141 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003142static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003143{
3144 struct session *sess = s->sess;
3145 struct http_txn *txn = s->txn;
3146 struct htx *htx;
3147 struct http_hdr_ctx ctx;
3148 char *hdr_beg, *hdr_end, *del_from;
3149 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3150 int preserve_hdr;
3151
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003152 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003153 ctx.blk = NULL;
3154 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003155 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003156 del_from = NULL; /* nothing to be deleted */
3157 preserve_hdr = 0; /* assume we may kill the whole header */
3158
3159 /* Now look for cookies. Conforming to RFC2109, we have to support
3160 * attributes whose name begin with a '$', and associate them with
3161 * the right cookie, if we want to delete this cookie.
3162 * So there are 3 cases for each cookie read :
3163 * 1) it's a special attribute, beginning with a '$' : ignore it.
3164 * 2) it's a server id cookie that we *MAY* want to delete : save
3165 * some pointers on it (last semi-colon, beginning of cookie...)
3166 * 3) it's an application cookie : we *MAY* have to delete a previous
3167 * "special" cookie.
3168 * At the end of loop, if a "special" cookie remains, we may have to
3169 * remove it. If no application cookie persists in the header, we
3170 * *MUST* delete it.
3171 *
3172 * Note: RFC2965 is unclear about the processing of spaces around
3173 * the equal sign in the ATTR=VALUE form. A careful inspection of
3174 * the RFC explicitly allows spaces before it, and not within the
3175 * tokens (attrs or values). An inspection of RFC2109 allows that
3176 * too but section 10.1.3 lets one think that spaces may be allowed
3177 * after the equal sign too, resulting in some (rare) buggy
3178 * implementations trying to do that. So let's do what servers do.
3179 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3180 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003181 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003182 * causes parsing to become ambiguous. Browsers also allow spaces
3183 * within values even without quotes.
3184 *
3185 * We have to keep multiple pointers in order to support cookie
3186 * removal at the beginning, middle or end of header without
3187 * corrupting the header. All of these headers are valid :
3188 *
3189 * hdr_beg hdr_end
3190 * | |
3191 * v |
3192 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3193 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3194 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3195 * | | | | | | |
3196 * | | | | | | |
3197 * | | | | | | +--> next
3198 * | | | | | +----> val_end
3199 * | | | | +-----------> val_beg
3200 * | | | +--------------> equal
3201 * | | +----------------> att_end
3202 * | +---------------------> att_beg
3203 * +--------------------------> prev
3204 *
3205 */
3206 hdr_beg = ctx.value.ptr;
3207 hdr_end = hdr_beg + ctx.value.len;
3208 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3209 /* Iterate through all cookies on this line */
3210
3211 /* find att_beg */
3212 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003213 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003214 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003215 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003216
3217 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3218 att_beg++;
3219
3220 /* find att_end : this is the first character after the last non
3221 * space before the equal. It may be equal to hdr_end.
3222 */
3223 equal = att_end = att_beg;
3224 while (equal < hdr_end) {
3225 if (*equal == '=' || *equal == ',' || *equal == ';')
3226 break;
3227 if (HTTP_IS_SPHT(*equal++))
3228 continue;
3229 att_end = equal;
3230 }
3231
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003232 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003233 * is between <att_beg> and <equal>, both may be identical.
3234 */
3235 /* look for end of cookie if there is an equal sign */
3236 if (equal < hdr_end && *equal == '=') {
3237 /* look for the beginning of the value */
3238 val_beg = equal + 1;
3239 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3240 val_beg++;
3241
3242 /* find the end of the value, respecting quotes */
3243 next = http_find_cookie_value_end(val_beg, hdr_end);
3244
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003245 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003246 val_end = next;
3247 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3248 val_end--;
3249 }
3250 else
3251 val_beg = val_end = next = equal;
3252
3253 /* We have nothing to do with attributes beginning with
3254 * '$'. However, they will automatically be removed if a
3255 * header before them is removed, since they're supposed
3256 * to be linked together.
3257 */
3258 if (*att_beg == '$')
3259 continue;
3260
3261 /* Ignore cookies with no equal sign */
3262 if (equal == next) {
3263 /* This is not our cookie, so we must preserve it. But if we already
3264 * scheduled another cookie for removal, we cannot remove the
3265 * complete header, but we can remove the previous block itself.
3266 */
3267 preserve_hdr = 1;
3268 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003269 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003270 val_end += delta;
3271 next += delta;
3272 hdr_end += delta;
3273 prev = del_from;
3274 del_from = NULL;
3275 }
3276 continue;
3277 }
3278
3279 /* if there are spaces around the equal sign, we need to
3280 * strip them otherwise we'll get trouble for cookie captures,
3281 * or even for rewrites. Since this happens extremely rarely,
3282 * it does not hurt performance.
3283 */
3284 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3285 int stripped_before = 0;
3286 int stripped_after = 0;
3287
3288 if (att_end != equal) {
3289 memmove(att_end, equal, hdr_end - equal);
3290 stripped_before = (att_end - equal);
3291 equal += stripped_before;
3292 val_beg += stripped_before;
3293 }
3294
3295 if (val_beg > equal + 1) {
3296 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3297 stripped_after = (equal + 1) - val_beg;
3298 val_beg += stripped_after;
3299 stripped_before += stripped_after;
3300 }
3301
3302 val_end += stripped_before;
3303 next += stripped_before;
3304 hdr_end += stripped_before;
3305 }
3306 /* now everything is as on the diagram above */
3307
3308 /* First, let's see if we want to capture this cookie. We check
3309 * that we don't already have a client side cookie, because we
3310 * can only capture one. Also as an optimisation, we ignore
3311 * cookies shorter than the declared name.
3312 */
3313 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3314 (val_end - att_beg >= sess->fe->capture_namelen) &&
3315 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3316 int log_len = val_end - att_beg;
3317
3318 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3319 ha_alert("HTTP logging : out of memory.\n");
3320 } else {
3321 if (log_len > sess->fe->capture_len)
3322 log_len = sess->fe->capture_len;
3323 memcpy(txn->cli_cookie, att_beg, log_len);
3324 txn->cli_cookie[log_len] = 0;
3325 }
3326 }
3327
3328 /* Persistence cookies in passive, rewrite or insert mode have the
3329 * following form :
3330 *
3331 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3332 *
3333 * For cookies in prefix mode, the form is :
3334 *
3335 * Cookie: NAME=SRV~VALUE
3336 */
3337 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3338 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3339 struct server *srv = s->be->srv;
3340 char *delim;
3341
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003342 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003343 * have the server ID between val_beg and delim, and the original cookie between
3344 * delim+1 and val_end. Otherwise, delim==val_end :
3345 *
3346 * hdr_beg
3347 * |
3348 * v
3349 * NAME=SRV; # in all but prefix modes
3350 * NAME=SRV~OPAQUE ; # in prefix mode
3351 * || || | |+-> next
3352 * || || | +--> val_end
3353 * || || +---------> delim
3354 * || |+------------> val_beg
3355 * || +-------------> att_end = equal
3356 * |+-----------------> att_beg
3357 * +------------------> prev
3358 *
3359 */
3360 if (s->be->ck_opts & PR_CK_PFX) {
3361 for (delim = val_beg; delim < val_end; delim++)
3362 if (*delim == COOKIE_DELIM)
3363 break;
3364 }
3365 else {
3366 char *vbar1;
3367 delim = val_end;
3368 /* Now check if the cookie contains a date field, which would
3369 * appear after a vertical bar ('|') just after the server name
3370 * and before the delimiter.
3371 */
3372 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3373 if (vbar1) {
3374 /* OK, so left of the bar is the server's cookie and
3375 * right is the last seen date. It is a base64 encoded
3376 * 30-bit value representing the UNIX date since the
3377 * epoch in 4-second quantities.
3378 */
3379 int val;
3380 delim = vbar1++;
3381 if (val_end - vbar1 >= 5) {
3382 val = b64tos30(vbar1);
3383 if (val > 0)
3384 txn->cookie_last_date = val << 2;
3385 }
3386 /* look for a second vertical bar */
3387 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3388 if (vbar1 && (val_end - vbar1 > 5)) {
3389 val = b64tos30(vbar1 + 1);
3390 if (val > 0)
3391 txn->cookie_first_date = val << 2;
3392 }
3393 }
3394 }
3395
3396 /* if the cookie has an expiration date and the proxy wants to check
3397 * it, then we do that now. We first check if the cookie is too old,
3398 * then only if it has expired. We detect strict overflow because the
3399 * time resolution here is not great (4 seconds). Cookies with dates
3400 * in the future are ignored if their offset is beyond one day. This
3401 * allows an admin to fix timezone issues without expiring everyone
3402 * and at the same time avoids keeping unwanted side effects for too
3403 * long.
3404 */
3405 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3406 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3407 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3408 txn->flags &= ~TX_CK_MASK;
3409 txn->flags |= TX_CK_OLD;
3410 delim = val_beg; // let's pretend we have not found the cookie
3411 txn->cookie_first_date = 0;
3412 txn->cookie_last_date = 0;
3413 }
3414 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3415 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3416 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3417 txn->flags &= ~TX_CK_MASK;
3418 txn->flags |= TX_CK_EXPIRED;
3419 delim = val_beg; // let's pretend we have not found the cookie
3420 txn->cookie_first_date = 0;
3421 txn->cookie_last_date = 0;
3422 }
3423
3424 /* Here, we'll look for the first running server which supports the cookie.
3425 * This allows to share a same cookie between several servers, for example
3426 * to dedicate backup servers to specific servers only.
3427 * However, to prevent clients from sticking to cookie-less backup server
3428 * when they have incidentely learned an empty cookie, we simply ignore
3429 * empty cookies and mark them as invalid.
3430 * The same behaviour is applied when persistence must be ignored.
3431 */
3432 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3433 srv = NULL;
3434
3435 while (srv) {
3436 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3437 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3438 if ((srv->cur_state != SRV_ST_STOPPED) ||
3439 (s->be->options & PR_O_PERSIST) ||
3440 (s->flags & SF_FORCE_PRST)) {
3441 /* we found the server and we can use it */
3442 txn->flags &= ~TX_CK_MASK;
3443 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3444 s->flags |= SF_DIRECT | SF_ASSIGNED;
3445 s->target = &srv->obj_type;
3446 break;
3447 } else {
3448 /* we found a server, but it's down,
3449 * mark it as such and go on in case
3450 * another one is available.
3451 */
3452 txn->flags &= ~TX_CK_MASK;
3453 txn->flags |= TX_CK_DOWN;
3454 }
3455 }
3456 srv = srv->next;
3457 }
3458
3459 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3460 /* no server matched this cookie or we deliberately skipped it */
3461 txn->flags &= ~TX_CK_MASK;
3462 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3463 txn->flags |= TX_CK_UNUSED;
3464 else
3465 txn->flags |= TX_CK_INVALID;
3466 }
3467
3468 /* depending on the cookie mode, we may have to either :
3469 * - delete the complete cookie if we're in insert+indirect mode, so that
3470 * the server never sees it ;
3471 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003472 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003473 * if we're in cookie prefix mode
3474 */
3475 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3476 int delta; /* negative */
3477
3478 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3479 delta = val_beg - (delim + 1);
3480 val_end += delta;
3481 next += delta;
3482 hdr_end += delta;
3483 del_from = NULL;
3484 preserve_hdr = 1; /* we want to keep this cookie */
3485 }
3486 else if (del_from == NULL &&
3487 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3488 del_from = prev;
3489 }
3490 }
3491 else {
3492 /* This is not our cookie, so we must preserve it. But if we already
3493 * scheduled another cookie for removal, we cannot remove the
3494 * complete header, but we can remove the previous block itself.
3495 */
3496 preserve_hdr = 1;
3497
3498 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003499 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003500 if (att_beg >= del_from)
3501 att_beg += delta;
3502 if (att_end >= del_from)
3503 att_end += delta;
3504 val_beg += delta;
3505 val_end += delta;
3506 next += delta;
3507 hdr_end += delta;
3508 prev = del_from;
3509 del_from = NULL;
3510 }
3511 }
3512
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003513 } /* for each cookie */
3514
3515
3516 /* There are no more cookies on this line.
3517 * We may still have one (or several) marked for deletion at the
3518 * end of the line. We must do this now in two ways :
3519 * - if some cookies must be preserved, we only delete from the
3520 * mark to the end of line ;
3521 * - if nothing needs to be preserved, simply delete the whole header
3522 */
3523 if (del_from) {
3524 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3525 }
3526 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003527 if (hdr_beg != hdr_end)
3528 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003529 else
3530 http_remove_header(htx, &ctx);
3531 }
3532 } /* for each "Cookie header */
3533}
3534
3535/*
3536 * Manage server-side cookies. It can impact performance by about 2% so it is
3537 * desirable to call it only when needed. This function is also used when we
3538 * just need to know if there is a cookie (eg: for check-cache).
3539 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003540static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003541{
3542 struct session *sess = s->sess;
3543 struct http_txn *txn = s->txn;
3544 struct htx *htx;
3545 struct http_hdr_ctx ctx;
3546 struct server *srv;
3547 char *hdr_beg, *hdr_end;
3548 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003549 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003550
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003551 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003552
3553 ctx.blk = NULL;
3554 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003555 int is_first = 1;
3556
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003557 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3558 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3559 break;
3560 is_cookie2 = 1;
3561 }
3562
3563 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3564 * <prev> points to the colon.
3565 */
3566 txn->flags |= TX_SCK_PRESENT;
3567
3568 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3569 * check-cache is enabled) and we are not interested in checking
3570 * them. Warning, the cookie capture is declared in the frontend.
3571 */
3572 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3573 break;
3574
3575 /* OK so now we know we have to process this response cookie.
3576 * The format of the Set-Cookie header is slightly different
3577 * from the format of the Cookie header in that it does not
3578 * support the comma as a cookie delimiter (thus the header
3579 * cannot be folded) because the Expires attribute described in
3580 * the original Netscape's spec may contain an unquoted date
3581 * with a comma inside. We have to live with this because
3582 * many browsers don't support Max-Age and some browsers don't
3583 * support quoted strings. However the Set-Cookie2 header is
3584 * clean.
3585 *
3586 * We have to keep multiple pointers in order to support cookie
3587 * removal at the beginning, middle or end of header without
3588 * corrupting the header (in case of set-cookie2). A special
3589 * pointer, <scav> points to the beginning of the set-cookie-av
3590 * fields after the first semi-colon. The <next> pointer points
3591 * either to the end of line (set-cookie) or next unquoted comma
3592 * (set-cookie2). All of these headers are valid :
3593 *
3594 * hdr_beg hdr_end
3595 * | |
3596 * v |
3597 * NAME1 = VALUE 1 ; Secure; Path="/" |
3598 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3599 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3600 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3601 * | | | | | | | |
3602 * | | | | | | | +-> next
3603 * | | | | | | +------------> scav
3604 * | | | | | +--------------> val_end
3605 * | | | | +--------------------> val_beg
3606 * | | | +----------------------> equal
3607 * | | +------------------------> att_end
3608 * | +----------------------------> att_beg
3609 * +------------------------------> prev
3610 * -------------------------------> hdr_beg
3611 */
3612 hdr_beg = ctx.value.ptr;
3613 hdr_end = hdr_beg + ctx.value.len;
3614 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3615
3616 /* Iterate through all cookies on this line */
3617
3618 /* find att_beg */
3619 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003620 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003621 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003622 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003623
3624 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3625 att_beg++;
3626
3627 /* find att_end : this is the first character after the last non
3628 * space before the equal. It may be equal to hdr_end.
3629 */
3630 equal = att_end = att_beg;
3631
3632 while (equal < hdr_end) {
3633 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3634 break;
3635 if (HTTP_IS_SPHT(*equal++))
3636 continue;
3637 att_end = equal;
3638 }
3639
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003640 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003641 * is between <att_beg> and <equal>, both may be identical.
3642 */
3643
3644 /* look for end of cookie if there is an equal sign */
3645 if (equal < hdr_end && *equal == '=') {
3646 /* look for the beginning of the value */
3647 val_beg = equal + 1;
3648 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3649 val_beg++;
3650
3651 /* find the end of the value, respecting quotes */
3652 next = http_find_cookie_value_end(val_beg, hdr_end);
3653
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003654 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003655 val_end = next;
3656 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3657 val_end--;
3658 }
3659 else {
3660 /* <equal> points to next comma, semi-colon or EOL */
3661 val_beg = val_end = next = equal;
3662 }
3663
3664 if (next < hdr_end) {
3665 /* Set-Cookie2 supports multiple cookies, and <next> points to
3666 * a colon or semi-colon before the end. So skip all attr-value
3667 * pairs and look for the next comma. For Set-Cookie, since
3668 * commas are permitted in values, skip to the end.
3669 */
3670 if (is_cookie2)
3671 next = http_find_hdr_value_end(next, hdr_end);
3672 else
3673 next = hdr_end;
3674 }
3675
3676 /* Now everything is as on the diagram above */
3677
3678 /* Ignore cookies with no equal sign */
3679 if (equal == val_end)
3680 continue;
3681
3682 /* If there are spaces around the equal sign, we need to
3683 * strip them otherwise we'll get trouble for cookie captures,
3684 * or even for rewrites. Since this happens extremely rarely,
3685 * it does not hurt performance.
3686 */
3687 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3688 int stripped_before = 0;
3689 int stripped_after = 0;
3690
3691 if (att_end != equal) {
3692 memmove(att_end, equal, hdr_end - equal);
3693 stripped_before = (att_end - equal);
3694 equal += stripped_before;
3695 val_beg += stripped_before;
3696 }
3697
3698 if (val_beg > equal + 1) {
3699 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3700 stripped_after = (equal + 1) - val_beg;
3701 val_beg += stripped_after;
3702 stripped_before += stripped_after;
3703 }
3704
3705 val_end += stripped_before;
3706 next += stripped_before;
3707 hdr_end += stripped_before;
3708
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003709 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003710 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003711 }
3712
3713 /* First, let's see if we want to capture this cookie. We check
3714 * that we don't already have a server side cookie, because we
3715 * can only capture one. Also as an optimisation, we ignore
3716 * cookies shorter than the declared name.
3717 */
3718 if (sess->fe->capture_name != NULL &&
3719 txn->srv_cookie == NULL &&
3720 (val_end - att_beg >= sess->fe->capture_namelen) &&
3721 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3722 int log_len = val_end - att_beg;
3723 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3724 ha_alert("HTTP logging : out of memory.\n");
3725 }
3726 else {
3727 if (log_len > sess->fe->capture_len)
3728 log_len = sess->fe->capture_len;
3729 memcpy(txn->srv_cookie, att_beg, log_len);
3730 txn->srv_cookie[log_len] = 0;
3731 }
3732 }
3733
3734 srv = objt_server(s->target);
3735 /* now check if we need to process it for persistence */
3736 if (!(s->flags & SF_IGNORE_PRST) &&
3737 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3738 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3739 /* assume passive cookie by default */
3740 txn->flags &= ~TX_SCK_MASK;
3741 txn->flags |= TX_SCK_FOUND;
3742
3743 /* If the cookie is in insert mode on a known server, we'll delete
3744 * this occurrence because we'll insert another one later.
3745 * We'll delete it too if the "indirect" option is set and we're in
3746 * a direct access.
3747 */
3748 if (s->be->ck_opts & PR_CK_PSV) {
3749 /* The "preserve" flag was set, we don't want to touch the
3750 * server's cookie.
3751 */
3752 }
3753 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3754 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3755 /* this cookie must be deleted */
3756 if (prev == hdr_beg && next == hdr_end) {
3757 /* whole header */
3758 http_remove_header(htx, &ctx);
3759 /* note: while both invalid now, <next> and <hdr_end>
3760 * are still equal, so the for() will stop as expected.
3761 */
3762 } else {
3763 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003764 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003765 next = prev;
3766 hdr_end += delta;
3767 }
3768 txn->flags &= ~TX_SCK_MASK;
3769 txn->flags |= TX_SCK_DELETED;
3770 /* and go on with next cookie */
3771 }
3772 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3773 /* replace bytes val_beg->val_end with the cookie name associated
3774 * with this server since we know it.
3775 */
3776 int sliding, delta;
3777
3778 ctx.value = ist2(val_beg, val_end - val_beg);
3779 ctx.lws_before = ctx.lws_after = 0;
3780 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3781 delta = srv->cklen - (val_end - val_beg);
3782 sliding = (ctx.value.ptr - val_beg);
3783 hdr_beg += sliding;
3784 val_beg += sliding;
3785 next += sliding + delta;
3786 hdr_end += sliding + delta;
3787
3788 txn->flags &= ~TX_SCK_MASK;
3789 txn->flags |= TX_SCK_REPLACED;
3790 }
3791 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3792 /* insert the cookie name associated with this server
3793 * before existing cookie, and insert a delimiter between them..
3794 */
3795 int sliding, delta;
3796 ctx.value = ist2(val_beg, 0);
3797 ctx.lws_before = ctx.lws_after = 0;
3798 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3799 delta = srv->cklen + 1;
3800 sliding = (ctx.value.ptr - val_beg);
3801 hdr_beg += sliding;
3802 val_beg += sliding;
3803 next += sliding + delta;
3804 hdr_end += sliding + delta;
3805
3806 val_beg[srv->cklen] = COOKIE_DELIM;
3807 txn->flags &= ~TX_SCK_MASK;
3808 txn->flags |= TX_SCK_REPLACED;
3809 }
3810 }
3811 /* that's done for this cookie, check the next one on the same
3812 * line when next != hdr_end (only if is_cookie2).
3813 */
3814 }
3815 }
3816}
3817
Christopher Faulet25a02f62018-10-24 12:00:25 +02003818/*
3819 * Parses the Cache-Control and Pragma request header fields to determine if
3820 * the request may be served from the cache and/or if it is cacheable. Updates
3821 * s->txn->flags.
3822 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003823void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003824{
3825 struct http_txn *txn = s->txn;
3826 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003827 struct http_hdr_ctx ctx = { .blk = NULL };
3828 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003829
3830 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3831 return; /* nothing more to do here */
3832
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003833 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003834 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003835
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003836 /* Check "pragma" header for HTTP/1.0 compatibility. */
3837 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3838 if (isteqi(ctx.value, ist("no-cache"))) {
3839 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003840 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003841 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003842
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003843 ctx.blk = NULL;
3844 /* Don't use the cache and don't try to store if we found the
3845 * Authorization header */
3846 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3847 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3848 txn->flags |= TX_CACHE_IGNORE;
3849 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003850
Christopher Faulet25a02f62018-10-24 12:00:25 +02003851
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003852 /* Look for "cache-control" header and iterate over all the values
3853 * until we find one that specifies that caching is possible or not. */
3854 ctx.blk = NULL;
3855 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003856 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003857 /* We don't check the values after max-age, max-stale nor min-fresh,
3858 * we simply don't use the cache when they're specified. */
3859 if (istmatchi(ctx.value, ist("max-age")) ||
3860 istmatchi(ctx.value, ist("no-cache")) ||
3861 istmatchi(ctx.value, ist("max-stale")) ||
3862 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003863 txn->flags |= TX_CACHE_IGNORE;
3864 continue;
3865 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003866 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003867 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3868 continue;
3869 }
3870 }
3871
3872 /* RFC7234#5.4:
3873 * When the Cache-Control header field is also present and
3874 * understood in a request, Pragma is ignored.
3875 * When the Cache-Control header field is not present in a
3876 * request, caches MUST consider the no-cache request
3877 * pragma-directive as having the same effect as if
3878 * "Cache-Control: no-cache" were present.
3879 */
3880 if (!cc_found && pragma_found)
3881 txn->flags |= TX_CACHE_IGNORE;
3882}
3883
3884/*
3885 * Check if response is cacheable or not. Updates s->txn->flags.
3886 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003887void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003888{
3889 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003890 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003891 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003892 int has_freshness_info = 0;
3893 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003894
3895 if (txn->status < 200) {
3896 /* do not try to cache interim responses! */
3897 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3898 return;
3899 }
3900
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003901 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003902 /* Check "pragma" header for HTTP/1.0 compatibility. */
3903 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3904 if (isteqi(ctx.value, ist("no-cache"))) {
3905 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3906 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003907 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003908 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003909
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003910 /* Look for "cache-control" header and iterate over all the values
3911 * until we find one that specifies that caching is possible or not. */
3912 ctx.blk = NULL;
3913 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3914 if (isteqi(ctx.value, ist("public"))) {
3915 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003916 continue;
3917 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003918 if (isteqi(ctx.value, ist("private")) ||
3919 isteqi(ctx.value, ist("no-cache")) ||
3920 isteqi(ctx.value, ist("no-store")) ||
3921 isteqi(ctx.value, ist("max-age=0")) ||
3922 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003923 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003924 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003925 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003926 /* We might have a no-cache="set-cookie" form. */
3927 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3928 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003929 continue;
3930 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003931
3932 if (istmatchi(ctx.value, ist("s-maxage")) ||
3933 istmatchi(ctx.value, ist("max-age"))) {
3934 has_freshness_info = 1;
3935 continue;
3936 }
3937 }
3938
3939 /* If no freshness information could be found in Cache-Control values,
3940 * look for an Expires header. */
3941 if (!has_freshness_info) {
3942 ctx.blk = NULL;
3943 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003944 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003945
3946 /* If no freshness information could be found in Cache-Control or Expires
3947 * values, look for an explicit validator. */
3948 if (!has_freshness_info) {
3949 ctx.blk = NULL;
3950 has_validator = 1;
3951 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3952 ctx.blk = NULL;
3953 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3954 has_validator = 0;
3955 }
3956 }
3957
3958 /* We won't store an entry that has neither a cache validator nor an
3959 * explicit expiration time, as suggested in RFC 7234#3. */
3960 if (!has_freshness_info && !has_validator)
3961 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003962}
3963
Christopher Faulet377c5a52018-10-24 21:21:30 +02003964/*
3965 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3966 * for the current backend.
3967 *
3968 * It is assumed that the request is either a HEAD, GET, or POST and that the
3969 * uri_auth field is valid.
3970 *
3971 * Returns 1 if stats should be provided, otherwise 0.
3972 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003973static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003974{
3975 struct uri_auth *uri_auth = backend->uri_auth;
3976 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003977 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003978 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003979
3980 if (!uri_auth)
3981 return 0;
3982
3983 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3984 return 0;
3985
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003986 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003987 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003988 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003989 if (*uri_auth->uri_prefix == '/')
3990 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003991
3992 /* check URI size */
3993 if (uri_auth->uri_len > uri.len)
3994 return 0;
3995
3996 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
3997 return 0;
3998
3999 return 1;
4000}
4001
4002/* This function prepares an applet to handle the stats. It can deal with the
4003 * "100-continue" expectation, check that admin rules are met for POST requests,
4004 * and program a response message if something was unexpected. It cannot fail
4005 * and always relies on the stats applet to complete the job. It does not touch
4006 * analysers nor counters, which are left to the caller. It does not touch
4007 * s->target which is supposed to already point to the stats applet. The caller
4008 * is expected to have already assigned an appctx to the stream.
4009 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004010static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004011{
4012 struct stats_admin_rule *stats_admin_rule;
4013 struct stream_interface *si = &s->si[1];
4014 struct session *sess = s->sess;
4015 struct http_txn *txn = s->txn;
4016 struct http_msg *msg = &txn->req;
4017 struct uri_auth *uri_auth = s->be->uri_auth;
4018 const char *h, *lookup, *end;
4019 struct appctx *appctx;
4020 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004021 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004022
4023 appctx = si_appctx(si);
4024 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4025 appctx->st1 = appctx->st2 = 0;
4026 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004027 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004028 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4029 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4030 appctx->ctx.stats.flags |= STAT_CHUNKED;
4031
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004032 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004033 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004034 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4035 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004036
4037 for (h = lookup; h <= end - 3; h++) {
4038 if (memcmp(h, ";up", 3) == 0) {
4039 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4040 break;
4041 }
Willy Tarreau3e320362020-10-23 17:28:57 +02004042 if (memcmp(h, ";no-maint", 3) == 0) {
4043 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
4044 break;
4045 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004046 }
4047
4048 if (uri_auth->refresh) {
4049 for (h = lookup; h <= end - 10; h++) {
4050 if (memcmp(h, ";norefresh", 10) == 0) {
4051 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4052 break;
4053 }
4054 }
4055 }
4056
4057 for (h = lookup; h <= end - 4; h++) {
4058 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004059 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004060 break;
4061 }
4062 }
4063
4064 for (h = lookup; h <= end - 6; h++) {
4065 if (memcmp(h, ";typed", 6) == 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 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4068 break;
4069 }
4070 }
4071
Christopher Faulet6338a082019-09-09 15:50:54 +02004072 for (h = lookup; h <= end - 5; h++) {
4073 if (memcmp(h, ";json", 5) == 0) {
4074 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4075 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4076 break;
4077 }
4078 }
4079
4080 for (h = lookup; h <= end - 12; h++) {
4081 if (memcmp(h, ";json-schema", 12) == 0) {
4082 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4083 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4084 break;
4085 }
4086 }
4087
Christopher Faulet377c5a52018-10-24 21:21:30 +02004088 for (h = lookup; h <= end - 8; h++) {
4089 if (memcmp(h, ";st=", 4) == 0) {
4090 int i;
4091 h += 4;
4092 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4093 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4094 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4095 appctx->ctx.stats.st_code = i;
4096 break;
4097 }
4098 }
4099 break;
4100 }
4101 }
4102
4103 appctx->ctx.stats.scope_str = 0;
4104 appctx->ctx.stats.scope_len = 0;
4105 for (h = lookup; h <= end - 8; h++) {
4106 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4107 int itx = 0;
4108 const char *h2;
4109 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4110 const char *err;
4111
4112 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4113 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004114 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4115 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004116 if (*h == ';' || *h == '&' || *h == ' ')
4117 break;
4118 itx++;
4119 h++;
4120 }
4121
4122 if (itx > STAT_SCOPE_TXT_MAXLEN)
4123 itx = STAT_SCOPE_TXT_MAXLEN;
4124 appctx->ctx.stats.scope_len = itx;
4125
4126 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4127 memcpy(scope_txt, h2, itx);
4128 scope_txt[itx] = '\0';
4129 err = invalid_char(scope_txt);
4130 if (err) {
4131 /* bad char in search text => clear scope */
4132 appctx->ctx.stats.scope_str = 0;
4133 appctx->ctx.stats.scope_len = 0;
4134 }
4135 break;
4136 }
4137 }
4138
4139 /* now check whether we have some admin rules for this request */
4140 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4141 int ret = 1;
4142
4143 if (stats_admin_rule->cond) {
4144 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4145 ret = acl_pass(ret);
4146 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4147 ret = !ret;
4148 }
4149
4150 if (ret) {
4151 /* no rule, or the rule matches */
4152 appctx->ctx.stats.flags |= STAT_ADMIN;
4153 break;
4154 }
4155 }
4156
Christopher Faulet5d45e382019-02-27 15:15:23 +01004157 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4158 appctx->st0 = STAT_HTTP_HEAD;
4159 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004160 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004161 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004162 if (msg->msg_state < HTTP_MSG_DATA)
4163 req->analysers |= AN_REQ_HTTP_BODY;
4164 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004165 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004166 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004167 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4168 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4169 appctx->st0 = STAT_HTTP_LAST;
4170 }
4171 }
4172 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004173 /* Unsupported method */
4174 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4175 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4176 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004177 }
4178
4179 s->task->nice = -32; /* small boost for HTTP statistics */
4180 return 1;
4181}
4182
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004183void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004184{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004185 struct channel *req = &s->req;
4186 struct channel *res = &s->res;
4187 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004188 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004189 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004190 struct ist path, location;
4191 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004192
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004193 /*
4194 * Create the location
4195 */
4196 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004197
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004198 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004199 /* special prefix "/" means don't change URL */
4200 srv = __objt_server(s->target);
4201 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4202 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4203 return;
4204 }
4205
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004206 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004207 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004208 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004209 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004210 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004211 return;
4212
4213 if (!chunk_memcat(&trash, path.ptr, path.len))
4214 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004215 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004216
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004217 /*
4218 * Create the 302 respone
4219 */
4220 htx = htx_from_buf(&res->buf);
4221 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4222 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4223 ist("HTTP/1.1"), ist("302"), ist("Found"));
4224 if (!sl)
4225 goto fail;
4226 sl->info.res.status = 302;
4227 s->txn->status = 302;
4228
4229 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4230 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4231 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4232 !htx_add_header(htx, ist("Location"), location))
4233 goto fail;
4234
4235 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4236 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004237
Christopher Fauletc20afb82020-01-24 19:16:26 +01004238 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004239 if (!http_forward_proxy_resp(s, 1))
4240 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004241
4242 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004243 si_shutr(si);
4244 si_shutw(si);
4245 si->err_type = SI_ET_NONE;
4246 si->state = SI_ST_CLO;
4247
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004248 if (!(s->flags & SF_ERR_MASK))
4249 s->flags |= SF_ERR_LOCAL;
4250 if (!(s->flags & SF_FINST_MASK))
4251 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004252
4253 /* FIXME: we should increase a counter of redirects per server and per backend. */
4254 srv_inc_sess_ctr(srv);
4255 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004256 return;
4257
4258 fail:
4259 /* If an error occurred, remove the incomplete HTTP response from the
4260 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004261 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004262}
4263
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004264/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004265 * because an error was triggered during the body forwarding.
4266 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004267static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004268{
4269 struct channel *chn = &s->req;
4270 struct http_txn *txn = s->txn;
4271
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004272 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004273
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004274 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4275 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004276 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004277 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004278 goto end;
4279 }
4280
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004281 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4282 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004283 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004284 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004285
4286 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004287 /* No need to read anymore, the request was completely parsed.
4288 * We can shut the read side unless we want to abort_on_close,
4289 * or we have a POST request. The issue with POST requests is
4290 * that some browsers still send a CRLF after the request, and
4291 * this CRLF must be read so that it does not remain in the kernel
4292 * buffers, otherwise a close could cause an RST on some systems
4293 * (eg: Linux).
4294 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004295 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004296 channel_dont_read(chn);
4297
4298 /* if the server closes the connection, we want to immediately react
4299 * and close the socket to save packets and syscalls.
4300 */
4301 s->si[1].flags |= SI_FL_NOHALF;
4302
4303 /* In any case we've finished parsing the request so we must
4304 * disable Nagle when sending data because 1) we're not going
4305 * to shut this side, and 2) the server is waiting for us to
4306 * send pending data.
4307 */
4308 chn->flags |= CF_NEVER_WAIT;
4309
Christopher Fauletd01ce402019-01-02 17:44:13 +01004310 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4311 /* The server has not finished to respond, so we
4312 * don't want to move in order not to upset it.
4313 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004314 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004315 return;
4316 }
4317
Christopher Fauletf2824e62018-10-01 12:12:37 +02004318 /* When we get here, it means that both the request and the
4319 * response have finished receiving. Depending on the connection
4320 * mode, we'll have to wait for the last bytes to leave in either
4321 * direction, and sometimes for a close to be effective.
4322 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004323 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004324 /* Tunnel mode will not have any analyser so it needs to
4325 * poll for reads.
4326 */
4327 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004328 if (b_data(&chn->buf)) {
4329 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004330 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004331 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004332 txn->req.msg_state = HTTP_MSG_TUNNEL;
4333 }
4334 else {
4335 /* we're not expecting any new data to come for this
4336 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004337 *
4338 * However, there is an exception if the response
4339 * length is undefined. In this case, we need to wait
4340 * the close from the server. The response will be
4341 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004342 */
4343 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4344 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004345 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004346
4347 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4348 channel_shutr_now(chn);
4349 channel_shutw_now(chn);
4350 }
4351 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004352 goto check_channel_flags;
4353 }
4354
4355 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4356 http_msg_closing:
4357 /* nothing else to forward, just waiting for the output buffer
4358 * to be empty and for the shutw_now to take effect.
4359 */
4360 if (channel_is_empty(chn)) {
4361 txn->req.msg_state = HTTP_MSG_CLOSED;
4362 goto http_msg_closed;
4363 }
4364 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004365 txn->req.msg_state = HTTP_MSG_ERROR;
4366 goto end;
4367 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004368 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004369 return;
4370 }
4371
4372 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4373 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004374 /* if we don't know whether the server will close, we need to hard close */
4375 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4376 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004377 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004378 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004379 channel_dont_read(chn);
4380 goto end;
4381 }
4382
4383 check_channel_flags:
4384 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4385 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4386 /* if we've just closed an output, let's switch */
4387 txn->req.msg_state = HTTP_MSG_CLOSING;
4388 goto http_msg_closing;
4389 }
4390
4391 end:
4392 chn->analysers &= AN_REQ_FLT_END;
4393 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4394 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4395 channel_auto_close(chn);
4396 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004397 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004398}
4399
4400
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004401/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004402 * because an error was triggered during the body forwarding.
4403 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004404static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004405{
4406 struct channel *chn = &s->res;
4407 struct http_txn *txn = s->txn;
4408
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004409 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004410
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004411 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4412 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004413 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004414 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004415 goto end;
4416 }
4417
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004418 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4419 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004420 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004421 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004422
4423 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4424 /* In theory, we don't need to read anymore, but we must
4425 * still monitor the server connection for a possible close
4426 * while the request is being uploaded, so we don't disable
4427 * reading.
4428 */
4429 /* channel_dont_read(chn); */
4430
4431 if (txn->req.msg_state < HTTP_MSG_DONE) {
4432 /* The client seems to still be sending data, probably
4433 * because we got an error response during an upload.
4434 * We have the choice of either breaking the connection
4435 * or letting it pass through. Let's do the later.
4436 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004437 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004438 return;
4439 }
4440
4441 /* When we get here, it means that both the request and the
4442 * response have finished receiving. Depending on the connection
4443 * mode, we'll have to wait for the last bytes to leave in either
4444 * direction, and sometimes for a close to be effective.
4445 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004446 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004447 channel_auto_read(chn);
4448 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004449 if (b_data(&chn->buf)) {
4450 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004451 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004452 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004453 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4454 }
4455 else {
4456 /* we're not expecting any new data to come for this
4457 * transaction, so we can close it.
4458 */
4459 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4460 channel_shutr_now(chn);
4461 channel_shutw_now(chn);
4462 }
4463 }
4464 goto check_channel_flags;
4465 }
4466
4467 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4468 http_msg_closing:
4469 /* nothing else to forward, just waiting for the output buffer
4470 * to be empty and for the shutw_now to take effect.
4471 */
4472 if (channel_is_empty(chn)) {
4473 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4474 goto http_msg_closed;
4475 }
4476 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004477 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004478 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004479 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004480 if (strm_sess(s)->listener->counters)
4481 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004482 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004483 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004484 goto end;
4485 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004486 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004487 return;
4488 }
4489
4490 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4491 http_msg_closed:
4492 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004493 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004494 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004495 goto end;
4496 }
4497
4498 check_channel_flags:
4499 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4500 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4501 /* if we've just closed an output, let's switch */
4502 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4503 goto http_msg_closing;
4504 }
4505
4506 end:
4507 chn->analysers &= AN_RES_FLT_END;
4508 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4509 chn->analysers |= AN_RES_FLT_XFER_DATA;
4510 channel_auto_close(chn);
4511 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004512 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004513}
4514
Christopher Fauletef70e252020-01-28 09:26:19 +01004515/* Forward a response generated by HAProxy (error/redirect/return). This
4516 * function forwards all pending incoming data. If <final> is set to 0, nothing
4517 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004518 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004519 * returned. If an error occurred, 0 is returned. If it fails, this function
4520 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004521 */
4522int http_forward_proxy_resp(struct stream *s, int final)
4523{
4524 struct channel *req = &s->req;
4525 struct channel *res = &s->res;
4526 struct htx *htx = htxbuf(&res->buf);
4527 size_t data;
4528
4529 if (final) {
4530 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004531
Christopher Fauletaab1b672020-11-18 16:44:02 +01004532 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004533 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004534
Christopher Fauletd6c48362020-10-19 18:01:38 +02004535 if (s->txn->meth == HTTP_METH_HEAD)
4536 htx_skip_msg_payload(htx);
4537
Christopher Fauletef70e252020-01-28 09:26:19 +01004538 channel_auto_read(req);
4539 channel_abort(req);
4540 channel_auto_close(req);
4541 channel_htx_erase(req, htxbuf(&req->buf));
4542
4543 res->wex = tick_add_ifset(now_ms, res->wto);
4544 channel_auto_read(res);
4545 channel_auto_close(res);
4546 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004547 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet810df062020-07-22 16:20:34 +02004548 htxbuf(&res->buf)->flags |= HTX_FL_EOI; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004549 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004550 else {
4551 /* Send ASAP informational messages. Rely on CF_EOI for final
4552 * response.
4553 */
4554 res->flags |= CF_SEND_DONTWAIT;
4555 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004556
4557 data = htx->data - co_data(res);
4558 c_adv(res, data);
4559 htx->first = -1;
4560 res->total += data;
4561 return 1;
4562}
4563
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004564void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004565 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004566{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004567 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004568 if (!(s->flags & SF_ERR_MASK))
4569 s->flags |= err;
4570 if (!(s->flags & SF_FINST_MASK))
4571 s->flags |= finst;
4572}
4573
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004574void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004575{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004576 if (!msg) {
4577 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4578 goto end;
4579 }
4580
4581 if (http_reply_message(s, msg) == -1) {
4582 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004583 * it is already an internal error. If it was already a "const"
4584 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004585 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004586 if (s->txn->status == 500) {
4587 if (s->txn->flags & TX_CONST_REPLY)
4588 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004589 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004590 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004591 s->txn->status = 500;
4592 s->txn->http_reply = NULL;
4593 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4594 }
4595
4596end:
4597 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
4598 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
4599
Christopher Faulet0f226952018-10-22 09:29:56 +02004600 channel_auto_read(&s->req);
4601 channel_abort(&s->req);
4602 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004603 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004604 channel_auto_read(&s->res);
4605 channel_auto_close(&s->res);
4606 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004607}
4608
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004609struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004610{
4611 const int msgnum = http_get_status_idx(s->txn->status);
4612
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004613 if (s->txn->http_reply)
4614 return s->txn->http_reply;
4615 else if (s->be->replies[msgnum])
4616 return s->be->replies[msgnum];
4617 else if (strm_fe(s)->replies[msgnum])
4618 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004619 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004620 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004621}
4622
Christopher Faulet40e6b552020-06-25 16:04:50 +02004623/* Produces an HTX message from an http reply. Depending on the http reply type,
4624 * a, errorfile, an raw file or a log-format string is used. On success, it
4625 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4626 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004627 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004628int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004629{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004630 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004631 struct htx_sl *sl;
4632 struct buffer *body = NULL;
4633 const char *status, *reason, *clen, *ctype;
4634 unsigned int slflags;
4635 int ret = 0;
4636
Christopher Faulete29a97e2020-05-14 14:49:25 +02004637 /*
4638 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4639 *
4640 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4641 * as no payload if NULL. the TXN status code is set with the status
4642 * of the original reply.
4643 */
4644
4645 if (reply->type == HTTP_REPLY_INDIRECT) {
4646 if (reply->body.reply)
4647 reply = reply->body.reply;
4648 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004649 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4650 /* get default error message */
4651 if (reply == s->txn->http_reply)
4652 s->txn->http_reply = NULL;
4653 reply = http_error_message(s);
4654 if (reply->type == HTTP_REPLY_INDIRECT) {
4655 if (reply->body.reply)
4656 reply = reply->body.reply;
4657 }
4658 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004659
4660 if (reply->type == HTTP_REPLY_ERRMSG) {
4661 /* implicit or explicit error message*/
4662 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004663 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004664 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004665 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004666 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004667 }
4668 else {
4669 /* no payload, file or log-format string */
4670 if (reply->type == HTTP_REPLY_RAW) {
4671 /* file */
4672 body = &reply->body.obj;
4673 }
4674 else if (reply->type == HTTP_REPLY_LOGFMT) {
4675 /* log-format string */
4676 body = alloc_trash_chunk();
4677 if (!body)
4678 goto fail_alloc;
4679 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4680 }
4681 /* else no payload */
4682
4683 status = ultoa(reply->status);
4684 reason = http_get_reason(reply->status);
4685 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4686 if (!body || !b_data(body))
4687 slflags |= HTX_SL_F_BODYLESS;
4688 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4689 if (!sl)
4690 goto fail;
4691 sl->info.res.status = reply->status;
4692
4693 clen = (body ? ultoa(b_data(body)) : "0");
4694 ctype = reply->ctype;
4695
4696 if (!LIST_ISEMPTY(&reply->hdrs)) {
4697 struct http_reply_hdr *hdr;
4698 struct buffer *value = alloc_trash_chunk();
4699
4700 if (!value)
4701 goto fail;
4702
4703 list_for_each_entry(hdr, &reply->hdrs, list) {
4704 chunk_reset(value);
4705 value->data = build_logline(s, value->area, value->size, &hdr->value);
4706 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4707 free_trash_chunk(value);
4708 goto fail;
4709 }
4710 chunk_reset(value);
4711 }
4712 free_trash_chunk(value);
4713 }
4714
4715 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4716 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4717 !htx_add_endof(htx, HTX_BLK_EOH) ||
4718 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))) ||
4719 !htx_add_endof(htx, HTX_BLK_EOM))
4720 goto fail;
4721 }
4722
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004723 leave:
4724 if (reply->type == HTTP_REPLY_LOGFMT)
4725 free_trash_chunk(body);
4726 return ret;
4727
4728 fail_alloc:
4729 if (!(s->flags & SF_ERR_MASK))
4730 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004731 /* fall through */
4732 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004733 ret = -1;
4734 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004735}
4736
4737/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004738 * occurs -1 is returned and the response channel is truncated, removing this
4739 * way the faulty reply. This function may fail when the reply is formatted
4740 * (http_reply_to_htx) or when the reply is forwarded
4741 * (http_forward_proxy_resp). On the last case, it is because a
4742 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004743 */
4744int http_reply_message(struct stream *s, struct http_reply *reply)
4745{
4746 struct channel *res = &s->res;
4747 struct htx *htx = htx_from_buf(&res->buf);
4748
4749 if (s->txn->status == -1)
4750 s->txn->status = reply->status;
4751 channel_htx_truncate(res, htx);
4752
4753 if (http_reply_to_htx(s, htx, reply) == -1)
4754 goto fail;
4755
4756 htx_to_buf(htx, &s->res.buf);
4757 if (!http_forward_proxy_resp(s, 1))
4758 goto fail;
4759 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004760
4761 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004762 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004763 if (!(s->flags & SF_ERR_MASK))
4764 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004765 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004766}
4767
Christopher Faulet304cc402019-07-15 15:46:28 +02004768/* Return the error message corresponding to si->err_type. It is assumed
4769 * that the server side is closed. Note that err_type is actually a
4770 * bitmask, where almost only aborts may be cumulated with other
4771 * values. We consider that aborted operations are more important
4772 * than timeouts or errors due to the fact that nobody else in the
4773 * logs might explain incomplete retries. All others should avoid
4774 * being cumulated. It should normally not be possible to have multiple
4775 * aborts at once, but just in case, the first one in sequence is reported.
4776 * Note that connection errors appearing on the second request of a keep-alive
4777 * connection are not reported since this allows the client to retry.
4778 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004779void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004780{
4781 int err_type = si->err_type;
4782
4783 /* set s->txn->status for http_error_message(s) */
4784 s->txn->status = 503;
4785
4786 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004787 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4788 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004789 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004790 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4791 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4792 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004793 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004794 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4795 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004796 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004797 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4798 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004799 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004800 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4801 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4802 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004803 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004804 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4805 (s->flags & SF_SRV_REUSED) ? NULL :
4806 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004807 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004808 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4809 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4810 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004811 else { /* SI_ET_CONN_OTHER and others */
4812 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004813 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4814 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004815 }
4816}
4817
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004818
Christopher Faulet4a28a532019-03-01 11:19:40 +01004819/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4820 * on success and -1 on error.
4821 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004822static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004823{
4824 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4825 * then we must send an HTTP/1.1 100 Continue intermediate response.
4826 */
4827 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4828 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4829 struct ist hdr = { .ptr = "Expect", .len = 6 };
4830 struct http_hdr_ctx ctx;
4831
4832 ctx.blk = NULL;
4833 /* Expect is allowed in 1.1, look for it */
4834 if (http_find_header(htx, hdr, &ctx, 0) &&
4835 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004836 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004837 return -1;
4838 http_remove_header(htx, &ctx);
4839 }
4840 }
4841 return 0;
4842}
4843
Christopher Faulet23a3c792018-11-28 10:01:23 +01004844/* Send a 100-Continue response to the client. It returns 0 on success and -1
4845 * on error. The response channel is updated accordingly.
4846 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004847static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004848{
4849 struct channel *res = &s->res;
4850 struct htx *htx = htx_from_buf(&res->buf);
4851 struct htx_sl *sl;
4852 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4853 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004854
4855 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4856 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4857 if (!sl)
4858 goto fail;
4859 sl->info.res.status = 100;
4860
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004861 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004862 goto fail;
4863
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004864 if (!http_forward_proxy_resp(s, 0))
4865 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004866 return 0;
4867
4868 fail:
4869 /* If an error occurred, remove the incomplete HTTP response from the
4870 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004871 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004872 return -1;
4873}
4874
Christopher Faulet12c51e22018-11-28 15:59:42 +01004875
Christopher Faulet0f226952018-10-22 09:29:56 +02004876/*
4877 * Capture headers from message <htx> according to header list <cap_hdr>, and
4878 * fill the <cap> pointers appropriately.
4879 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004880static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004881{
4882 struct cap_hdr *h;
4883 int32_t pos;
4884
Christopher Fauleta3f15502019-05-13 15:27:23 +02004885 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004886 struct htx_blk *blk = htx_get_blk(htx, pos);
4887 enum htx_blk_type type = htx_get_blk_type(blk);
4888 struct ist n, v;
4889
4890 if (type == HTX_BLK_EOH)
4891 break;
4892 if (type != HTX_BLK_HDR)
4893 continue;
4894
4895 n = htx_get_blk_name(htx, blk);
4896
4897 for (h = cap_hdr; h; h = h->next) {
4898 if (h->namelen && (h->namelen == n.len) &&
4899 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4900 if (cap[h->index] == NULL)
4901 cap[h->index] =
4902 pool_alloc(h->pool);
4903
4904 if (cap[h->index] == NULL) {
4905 ha_alert("HTTP capture : out of memory.\n");
4906 break;
4907 }
4908
4909 v = htx_get_blk_value(htx, blk);
4910 if (v.len > h->len)
4911 v.len = h->len;
4912
4913 memcpy(cap[h->index], v.ptr, v.len);
4914 cap[h->index][v.len]=0;
4915 }
4916 }
4917 }
4918}
4919
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004920/* Delete a value in a header between delimiters <from> and <next>. The header
4921 * itself is delimited by <start> and <end> pointers. The number of characters
4922 * displaced is returned, and the pointer to the first delimiter is updated if
4923 * required. The function tries as much as possible to respect the following
4924 * principles :
4925 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4926 * in which case <next> is simply removed
4927 * - set exactly one space character after the new first delimiter, unless there
4928 * are not enough characters in the block being moved to do so.
4929 * - remove unneeded spaces before the previous delimiter and after the new
4930 * one.
4931 *
4932 * It is the caller's responsibility to ensure that :
4933 * - <from> points to a valid delimiter or <start> ;
4934 * - <next> points to a valid delimiter or <end> ;
4935 * - there are non-space chars before <from>.
4936 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004937static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004938{
4939 char *prev = *from;
4940
4941 if (prev == start) {
4942 /* We're removing the first value. eat the semicolon, if <next>
4943 * is lower than <end> */
4944 if (next < end)
4945 next++;
4946
4947 while (next < end && HTTP_IS_SPHT(*next))
4948 next++;
4949 }
4950 else {
4951 /* Remove useless spaces before the old delimiter. */
4952 while (HTTP_IS_SPHT(*(prev-1)))
4953 prev--;
4954 *from = prev;
4955
4956 /* copy the delimiter and if possible a space if we're
4957 * not at the end of the line.
4958 */
4959 if (next < end) {
4960 *prev++ = *next++;
4961 if (prev + 1 < next)
4962 *prev++ = ' ';
4963 while (next < end && HTTP_IS_SPHT(*next))
4964 next++;
4965 }
4966 }
4967 memmove(prev, next, end - next);
4968 return (prev - next);
4969}
4970
Christopher Faulet0f226952018-10-22 09:29:56 +02004971
4972/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004973 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004974 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004975static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004976{
4977 struct ist dst = ist2(str, 0);
4978
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004979 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004980 goto end;
4981 if (dst.len + 1 > len)
4982 goto end;
4983 dst.ptr[dst.len++] = ' ';
4984
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004985 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004986 goto end;
4987 if (dst.len + 1 > len)
4988 goto end;
4989 dst.ptr[dst.len++] = ' ';
4990
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004991 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004992 end:
4993 return dst.len;
4994}
4995
4996/*
4997 * Print a debug line with a start line.
4998 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004999static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005000{
5001 struct session *sess = strm_sess(s);
5002 int max;
5003
5004 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5005 dir,
5006 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5007 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5008
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005009 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005010 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005011 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005012 trash.area[trash.data++] = ' ';
5013
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005014 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005015 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005016 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005017 trash.area[trash.data++] = ' ';
5018
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005019 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005020 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005021 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005022 trash.area[trash.data++] = '\n';
5023
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005024 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005025}
5026
5027/*
5028 * Print a debug line with a header.
5029 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005030static 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 +02005031{
5032 struct session *sess = strm_sess(s);
5033 int max;
5034
5035 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5036 dir,
5037 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5038 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5039
5040 max = n.len;
5041 UBOUND(max, trash.size - trash.data - 3);
5042 chunk_memcat(&trash, n.ptr, max);
5043 trash.area[trash.data++] = ':';
5044 trash.area[trash.data++] = ' ';
5045
5046 max = v.len;
5047 UBOUND(max, trash.size - trash.data - 1);
5048 chunk_memcat(&trash, v.ptr, max);
5049 trash.area[trash.data++] = '\n';
5050
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005051 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005052}
5053
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005054/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5055 * In case of allocation failure, everything allocated is freed and NULL is
5056 * returned. Otherwise the new transaction is assigned to the stream and
5057 * returned.
5058 */
5059struct http_txn *http_alloc_txn(struct stream *s)
5060{
5061 struct http_txn *txn = s->txn;
5062
5063 if (txn)
5064 return txn;
5065
5066 txn = pool_alloc(pool_head_http_txn);
5067 if (!txn)
5068 return txn;
5069
5070 s->txn = txn;
5071 return txn;
5072}
5073
5074void http_txn_reset_req(struct http_txn *txn)
5075{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005076 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005077 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5078}
5079
5080void http_txn_reset_res(struct http_txn *txn)
5081{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005082 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005083 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5084}
5085
5086/*
5087 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5088 * the required fields are properly allocated and that we only need to (re)init
5089 * them. This should be used before processing any new request.
5090 */
5091void http_init_txn(struct stream *s)
5092{
5093 struct http_txn *txn = s->txn;
5094 struct conn_stream *cs = objt_cs(s->si[0].end);
5095
5096 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5097 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5098 : 0);
5099 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005100 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005101 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005102
5103 txn->cookie_first_date = 0;
5104 txn->cookie_last_date = 0;
5105
5106 txn->srv_cookie = NULL;
5107 txn->cli_cookie = NULL;
5108 txn->uri = NULL;
5109
5110 http_txn_reset_req(txn);
5111 http_txn_reset_res(txn);
5112
5113 txn->req.chn = &s->req;
5114 txn->rsp.chn = &s->res;
5115
5116 txn->auth.method = HTTP_AUTH_UNKNOWN;
5117
5118 vars_init(&s->vars_txn, SCOPE_TXN);
5119 vars_init(&s->vars_reqres, SCOPE_REQ);
5120}
5121
5122/* to be used at the end of a transaction */
5123void http_end_txn(struct stream *s)
5124{
5125 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005126
5127 /* these ones will have been dynamically allocated */
5128 pool_free(pool_head_requri, txn->uri);
5129 pool_free(pool_head_capture, txn->cli_cookie);
5130 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005131 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005132
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005133 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005134 txn->uri = NULL;
5135 txn->srv_cookie = NULL;
5136 txn->cli_cookie = NULL;
5137
Christopher Faulet59399252019-11-07 14:27:52 +01005138 if (!LIST_ISEMPTY(&s->vars_txn.head))
5139 vars_prune(&s->vars_txn, s->sess, s);
5140 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5141 vars_prune(&s->vars_reqres, s->sess, s);
5142}
5143
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005144
5145DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005146
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005147__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005148static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005149{
5150}
5151
5152
5153/*
5154 * Local variables:
5155 * c-indent-level: 8
5156 * c-basic-offset: 8
5157 * End:
5158 */