blob: 4b756ae9fb3396fb247b0bbb9f2d2c3a499c3c08 [file] [log] [blame]
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaudcc048a2020-06-04 19:11:43 +020013#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020014#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020016#include <haproxy/backend.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020017#include <haproxy/base64.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020018#include <haproxy/capture-t.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020019#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020020#include <haproxy/check.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020021#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020022#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020023#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020024#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020025#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020027#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020028#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020029#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020030#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020031#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020032#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020033#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020034#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020035#include <haproxy/stream_interface.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020036#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020037#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020038#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020039
Christopher Faulete0768eb2018-10-03 16:38:02 +020040
Christopher Fauleteea8fc72019-11-05 16:18:10 +010041#define TRACE_SOURCE &trace_strm
42
Christopher Faulet377c5a52018-10-24 21:21:30 +020043extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020044
Christopher Fauleta8a46e22019-07-16 14:53:09 +020045struct pool_head *pool_head_requri = NULL;
46struct pool_head *pool_head_capture = NULL;
47
48
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020049static void http_end_request(struct stream *s);
50static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020051
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020052static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
53static int http_del_hdr_value(char *start, char *end, char **from, char *next);
54static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020055static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
56static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
Christopher Faulet0f226952018-10-22 09:29:56 +020057
Christopher Fauletb58f62b2020-01-13 16:40:13 +010058static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020059static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020060
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020061static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
62static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020063
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020064static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
65static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020066
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020067static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
68static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010069
Christopher Faulete0768eb2018-10-03 16:38:02 +020070/* This stream analyser waits for a complete HTTP request. It returns 1 if the
71 * processing can continue on next analysers, or zero if it either needs more
72 * data or wants to immediately abort the request (eg: timeout, error, ...). It
73 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
74 * when it has nothing left to do, and may remove any analyser when it wants to
75 * abort.
76 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020077int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020078{
Christopher Faulet9768c262018-10-22 09:34:31 +020079
Christopher Faulete0768eb2018-10-03 16:38:02 +020080 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020081 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020082 *
Christopher Faulet9768c262018-10-22 09:34:31 +020083 * Once the start line and all headers are received, we may perform a
84 * capture of the error (if any), and we will set a few fields. We also
85 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020086 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020087 struct session *sess = s->sess;
88 struct http_txn *txn = s->txn;
89 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020090 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010091 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020092
Christopher Fauleteea8fc72019-11-05 16:18:10 +010093 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020094
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010095 htx = htxbuf(&req->buf);
Christopher Faulet9768c262018-10-22 09:34:31 +020096
Willy Tarreau4236f032019-03-05 10:43:32 +010097 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +020098 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +010099 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200100 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Faulete01e5832021-02-10 14:58:01 +0100101 if (htx->flags & HTX_FL_PARSING_ERROR) {
102 stream_inc_http_err_ctr(s);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200103 goto return_bad_req;
Christopher Faulete01e5832021-02-10 14:58:01 +0100104 }
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200105 else
106 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100107 }
108
Christopher Faulete0768eb2018-10-03 16:38:02 +0200109 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200110 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200111
112 /* If there is data available for analysis, log the end of the idle time. */
Christopher Faulet870aad92018-11-29 15:23:46 +0100113 if (c_data(req) && s->logs.t_idle == -1) {
114 const struct cs_info *csinfo = si_get_cs_info(objt_cs(s->si[0].end));
115
116 s->logs.t_idle = ((csinfo)
117 ? csinfo->t_idle
118 : tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake);
119 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200120
Christopher Faulete0768eb2018-10-03 16:38:02 +0200121 /*
122 * Now we quickly check if we have found a full valid request.
123 * If not so, we check the FD and buffer states before leaving.
124 * A full request is indicated by the fact that we have seen
125 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
126 * requests are checked first. When waiting for a second request
127 * on a keep-alive stream, if we encounter and error, close, t/o,
128 * we note the error in the stream flags but don't set any state.
129 * Since the error will be noted there, it will not be counted by
130 * process_stream() as a frontend error.
131 * Last, we may increase some tracked counters' http request errors on
132 * the cases that are deliberately the client's fault. For instance,
133 * a timeout or connection reset is not counted as an error. However
134 * a bad request is.
135 */
Christopher Faulet29f17582019-05-23 11:03:26 +0200136 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200137 if (htx->flags & HTX_FL_UPGRADE)
138 goto failed_keep_alive;
139
Christopher Faulet9768c262018-10-22 09:34:31 +0200140 /* 1: have we encountered a read error ? */
141 if (req->flags & CF_READ_ERROR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200142 if (!(s->flags & SF_ERR_MASK))
143 s->flags |= SF_ERR_CLICL;
144
145 if (txn->flags & TX_WAIT_NEXT_RQ)
146 goto failed_keep_alive;
147
148 if (sess->fe->options & PR_O_IGNORE_PRB)
149 goto failed_keep_alive;
150
Christopher Faulete0768eb2018-10-03 16:38:02 +0200151 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200152 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100153 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete3f9f2b2021-03-12 11:26:15 +0100154 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100155 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200156
Christopher Faulet9768c262018-10-22 09:34:31 +0200157 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200158 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet9768c262018-10-22 09:34:31 +0200159 req->analysers &= AN_REQ_FLT_END;
160
Christopher Faulete0768eb2018-10-03 16:38:02 +0200161 if (!(s->flags & SF_FINST_MASK))
162 s->flags |= SF_FINST_R;
163 return 0;
164 }
165
Christopher Faulet9768c262018-10-22 09:34:31 +0200166 /* 2: has the read timeout expired ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200167 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
168 if (!(s->flags & SF_ERR_MASK))
169 s->flags |= SF_ERR_CLITO;
170
171 if (txn->flags & TX_WAIT_NEXT_RQ)
172 goto failed_keep_alive;
173
174 if (sess->fe->options & PR_O_IGNORE_PRB)
175 goto failed_keep_alive;
176
Christopher Faulete0768eb2018-10-03 16:38:02 +0200177 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200178 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100179 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete3f9f2b2021-03-12 11:26:15 +0100180 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100181 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200182
Christopher Faulet9768c262018-10-22 09:34:31 +0200183 txn->status = 408;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200184 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200185 req->analysers &= AN_REQ_FLT_END;
186
Christopher Faulete0768eb2018-10-03 16:38:02 +0200187 if (!(s->flags & SF_FINST_MASK))
188 s->flags |= SF_FINST_R;
189 return 0;
190 }
191
Christopher Faulet9768c262018-10-22 09:34:31 +0200192 /* 3: have we encountered a close ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200193 else if (req->flags & CF_SHUTR) {
194 if (!(s->flags & SF_ERR_MASK))
195 s->flags |= SF_ERR_CLICL;
196
197 if (txn->flags & TX_WAIT_NEXT_RQ)
198 goto failed_keep_alive;
199
200 if (sess->fe->options & PR_O_IGNORE_PRB)
201 goto failed_keep_alive;
202
Christopher Faulete0768eb2018-10-03 16:38:02 +0200203 stream_inc_http_err_ctr(s);
204 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200205 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100206 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete3f9f2b2021-03-12 11:26:15 +0100207 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100208 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200209
Christopher Faulet9768c262018-10-22 09:34:31 +0200210 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200211 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200212 req->analysers &= AN_REQ_FLT_END;
213
Christopher Faulete0768eb2018-10-03 16:38:02 +0200214 if (!(s->flags & SF_FINST_MASK))
215 s->flags |= SF_FINST_R;
216 return 0;
217 }
218
219 channel_dont_connect(req);
220 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
221 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100222
Christopher Faulete3f9f2b2021-03-12 11:26:15 +0100223 if (sess->listener && (sess->listener->options & LI_O_NOQUICKACK) && htx_is_not_empty(htx) &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200224 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
225 /* We need more data, we have to re-enable quick-ack in case we
226 * previously disabled it, otherwise we might cause the client
227 * to delay next data.
228 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100229 conn_set_quickack(objt_conn(sess->origin), 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200230 }
Willy Tarreau1a18b542018-12-11 16:37:42 +0100231
Christopher Faulet47365272018-10-31 17:40:50 +0100232 if ((req->flags & CF_READ_PARTIAL) && (txn->flags & TX_WAIT_NEXT_RQ)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200233 /* If the client starts to talk, let's fall back to
234 * request timeout processing.
235 */
236 txn->flags &= ~TX_WAIT_NEXT_RQ;
237 req->analyse_exp = TICK_ETERNITY;
238 }
239
240 /* just set the request timeout once at the beginning of the request */
241 if (!tick_isset(req->analyse_exp)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100242 if ((txn->flags & TX_WAIT_NEXT_RQ) && tick_isset(s->be->timeout.httpka))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200243 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
244 else
245 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
246 }
247
248 /* we're not ready yet */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100249 DBG_TRACE_DEVEL("waiting for the request",
250 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200251 return 0;
252
253 failed_keep_alive:
254 /* Here we process low-level errors for keep-alive requests. In
255 * short, if the request is not the first one and it experiences
256 * a timeout, read error or shutdown, we just silently close so
257 * that the client can try again.
258 */
259 txn->status = 0;
260 msg->msg_state = HTTP_MSG_RQBEFORE;
261 req->analysers &= AN_REQ_FLT_END;
262 s->logs.logwait = 0;
263 s->logs.level = 0;
264 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200265 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100266 DBG_TRACE_DEVEL("leaving by closing K/A connection",
267 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200268 return 0;
269 }
270
Christopher Faulet9768c262018-10-22 09:34:31 +0200271 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200272 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200273 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200274
Christopher Faulet9768c262018-10-22 09:34:31 +0200275 /* kill the pending keep-alive timeout */
276 txn->flags &= ~TX_WAIT_NEXT_RQ;
277 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200278
Christopher Faulet29f17582019-05-23 11:03:26 +0200279 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200280 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100281
Christopher Faulet9768c262018-10-22 09:34:31 +0200282 /* 0: we might have to print this header in debug mode */
283 if (unlikely((global.mode & MODE_DEBUG) &&
284 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
285 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200286
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200287 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200288
Christopher Fauleta3f15502019-05-13 15:27:23 +0200289 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200290 struct htx_blk *blk = htx_get_blk(htx, pos);
291 enum htx_blk_type type = htx_get_blk_type(blk);
292
293 if (type == HTX_BLK_EOH)
294 break;
295 if (type != HTX_BLK_HDR)
296 continue;
297
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200298 http_debug_hdr("clihdr", s,
299 htx_get_blk_name(htx, blk),
300 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200301 }
302 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200303
304 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100305 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200306 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100307 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100308 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200309 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100310 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +0100311 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100312 if (sl->flags & HTX_SL_F_BODYLESS)
313 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200314
315 /* we can make use of server redirect on GET and HEAD */
316 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
317 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100318 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200319 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200320 goto return_bad_req;
321 }
322
323 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100324 * 2: check if the URI matches the monitor_uri. We have to do this for
325 * every request which gets in, because the monitor-uri is defined by
326 * the frontend. If the monitor-uri starts with a '/', the matching is
327 * done against the request's path. Otherwise, the request's uri is
328 * used. It is a workaround to let HTTP/2 health-checks work as
329 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200330 */
331 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Faulet6072beb2020-02-18 15:34:58 +0100332 ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)),
333 ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) ||
334 isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200335 /*
336 * We have found the monitor URI
337 */
338 struct acl_cond *cond;
339
340 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100341 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200342
343 /* Check if we want to fail this monitor request or not */
344 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
345 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
346
347 ret = acl_pass(ret);
348 if (cond->pol == ACL_COND_UNLESS)
349 ret = !ret;
350
351 if (ret) {
352 /* we fail this request, let's return 503 service unavail */
353 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200354 if (!(s->flags & SF_ERR_MASK))
355 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
356 goto return_prx_cond;
357 }
358 }
359
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800360 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200361 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200362 if (!(s->flags & SF_ERR_MASK))
363 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
364 goto return_prx_cond;
365 }
366
367 /*
368 * 3: Maybe we have to copy the original REQURI for the logs ?
369 * Note: we cannot log anymore if the request has been
370 * classified as invalid.
371 */
372 if (unlikely(s->logs.logwait & LW_REQ)) {
373 /* we have a complete HTTP request that we must log */
374 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200375 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200376
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200377 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200378 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200379
380 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
381 s->do_log(s);
382 } else {
383 ha_alert("HTTP logging : out of memory.\n");
384 }
385 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200386
Christopher Faulete0768eb2018-10-03 16:38:02 +0200387 /* if the frontend has "option http-use-proxy-header", we'll check if
388 * we have what looks like a proxied connection instead of a connection,
389 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
390 * Note that this is *not* RFC-compliant, however browsers and proxies
391 * happen to do that despite being non-standard :-(
392 * We consider that a request not beginning with either '/' or '*' is
393 * a proxied connection, which covers both "scheme://location" and
394 * CONNECT ip:port.
395 */
396 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100397 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200398 txn->flags |= TX_USE_PX_CONN;
399
Christopher Faulete0768eb2018-10-03 16:38:02 +0200400 /* 5: we may need to capture headers */
401 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200402 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200403
Christopher Faulete0768eb2018-10-03 16:38:02 +0200404 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200405 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200406 req->analysers |= AN_REQ_HTTP_BODY;
407
408 /*
409 * RFC7234#4:
410 * A cache MUST write through requests with methods
411 * that are unsafe (Section 4.2.1 of [RFC7231]) to
412 * the origin server; i.e., a cache is not allowed
413 * to generate a reply to such a request before
414 * having forwarded the request and having received
415 * a corresponding response.
416 *
417 * RFC7231#4.2.1:
418 * Of the request methods defined by this
419 * specification, the GET, HEAD, OPTIONS, and TRACE
420 * methods are defined to be safe.
421 */
422 if (likely(txn->meth == HTTP_METH_GET ||
423 txn->meth == HTTP_METH_HEAD ||
424 txn->meth == HTTP_METH_OPTIONS ||
425 txn->meth == HTTP_METH_TRACE))
426 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
427
428 /* end of job, return OK */
429 req->analysers &= ~an_bit;
430 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200431
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100432 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200433 return 1;
434
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200435 return_int_err:
436 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200437 if (!(s->flags & SF_ERR_MASK))
438 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100439 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
William Lallemand656730d2021-03-08 15:26:48 +0100440 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100441 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200442 goto return_prx_cond;
443
Christopher Faulete0768eb2018-10-03 16:38:02 +0200444 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200445 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100446 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand656730d2021-03-08 15:26:48 +0100447 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100448 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200449 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200450
451 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200452 http_reply_and_close(s, txn->status, http_error_message(s));
453
Christopher Faulete0768eb2018-10-03 16:38:02 +0200454 if (!(s->flags & SF_ERR_MASK))
455 s->flags |= SF_ERR_PRXCOND;
456 if (!(s->flags & SF_FINST_MASK))
457 s->flags |= SF_FINST_R;
458
459 req->analysers &= AN_REQ_FLT_END;
460 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100461 DBG_TRACE_DEVEL("leaving on error",
462 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200463 return 0;
464}
465
466
467/* This stream analyser runs all HTTP request processing which is common to
468 * frontends and backends, which means blocking ACLs, filters, connection-close,
469 * reqadd, stats and redirects. This is performed for the designated proxy.
470 * It returns 1 if the processing can continue on next analysers, or zero if it
471 * either needs more data or wants to immediately abort the request (eg: deny,
472 * error, ...).
473 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200474int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200475{
476 struct session *sess = s->sess;
477 struct http_txn *txn = s->txn;
478 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200479 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200480 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200481 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200482 struct connection *conn = objt_conn(sess->origin);
483
484 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
485 /* we need more data */
486 goto return_prx_yield;
487 }
488
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100489 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200490
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100491 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200492
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200493 /* just in case we have some per-backend tracking. Only called the first
494 * execution of the analyser. */
495 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
496 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200497
498 /* evaluate http-request rules */
499 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100500 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200501
502 switch (verdict) {
503 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
504 goto return_prx_yield;
505
506 case HTTP_RULE_RES_CONT:
507 case HTTP_RULE_RES_STOP: /* nothing to do */
508 break;
509
510 case HTTP_RULE_RES_DENY: /* deny or tarpit */
511 if (txn->flags & TX_CLTARPIT)
512 goto tarpit;
513 goto deny;
514
515 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
516 goto return_prx_cond;
517
518 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
519 goto done;
520
521 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
522 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100523
524 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
525 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200526 }
527 }
528
529 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100530 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200531 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200532
Christopher Fauletff2759f2018-10-24 11:13:16 +0200533 ctx.blk = NULL;
534 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
535 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100536 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200537 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200538 }
539
540 /* OK at this stage, we know that the request was accepted according to
541 * the http-request rules, we can check for the stats. Note that the
542 * URI is detected *before* the req* rules in order not to be affected
543 * by a possible reqrep, while they are processed *after* so that a
544 * reqdeny can still block them. This clearly needs to change in 1.6!
545 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200546 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200547 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100548 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200549 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200550 if (!(s->flags & SF_ERR_MASK))
551 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100552 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200553 }
554
555 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200556 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100557 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200558 /* not all actions implemented: deny, allow, auth */
559
560 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
561 goto deny;
562
563 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
564 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100565
566 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
567 goto return_bad_req;
568
569 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
570 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200571 }
572
Christopher Faulet2571bc62019-03-01 11:44:26 +0100573 /* Proceed with the applets now. */
574 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200575 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100576 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200577
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200578 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100579 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100580
Christopher Faulete0768eb2018-10-03 16:38:02 +0200581 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
582 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
583 if (!(s->flags & SF_FINST_MASK))
584 s->flags |= SF_FINST_R;
585
586 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
587 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
588 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
589 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100590
591 req->flags |= CF_SEND_DONTWAIT;
592 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200593 goto done;
594 }
595
596 /* check whether we have some ACLs set to redirect this request */
597 list_for_each_entry(rule, &px->redirect_rules, list) {
598 if (rule->cond) {
599 int ret;
600
601 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
602 ret = acl_pass(ret);
603 if (rule->cond->pol == ACL_COND_UNLESS)
604 ret = !ret;
605 if (!ret)
606 continue;
607 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200608 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100609 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200610 goto done;
611 }
612
613 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
614 * If this happens, then the data will not come immediately, so we must
615 * send all what we have without waiting. Note that due to the small gain
616 * in waiting for the body of the request, it's easier to simply put the
617 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
618 * itself once used.
619 */
620 req->flags |= CF_SEND_DONTWAIT;
621
622 done: /* done with this analyser, continue with next ones that the calling
623 * points will have set, if any.
624 */
625 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500626 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200627 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100628 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200629 return 1;
630
631 tarpit:
632 /* Allow cookie logging
633 */
634 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200635 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200636
637 /* When a connection is tarpitted, we use the tarpit timeout,
638 * which may be the same as the connect timeout if unspecified.
639 * If unset, then set it to zero because we really want it to
640 * eventually expire. We build the tarpit as an analyser.
641 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100642 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200643
644 /* wipe the request out so that we can drop the connection early
645 * if the client closes first.
646 */
647 channel_dont_connect(req);
648
Christopher Faulete0768eb2018-10-03 16:38:02 +0200649 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
650 req->analysers |= AN_REQ_HTTP_TARPIT;
651 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
652 if (!req->analyse_exp)
653 req->analyse_exp = tick_add(now_ms, 0);
654 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100655 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100656 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100657 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
William Lallemand656730d2021-03-08 15:26:48 +0100658 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100659 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200660 goto done_without_exp;
661
662 deny: /* this request was blocked (denied) */
663
664 /* Allow cookie logging
665 */
666 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200667 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200668
Christopher Faulete0768eb2018-10-03 16:38:02 +0200669 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200670 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100671 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100672 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100673 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
William Lallemand656730d2021-03-08 15:26:48 +0100674 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100675 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100676 goto return_prx_err;
677
678 return_int_err:
679 txn->status = 500;
680 if (!(s->flags & SF_ERR_MASK))
681 s->flags |= SF_ERR_INTERNAL;
682 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100683 if (s->flags & SF_BE_ASSIGNED)
684 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand656730d2021-03-08 15:26:48 +0100685 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100686 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
687 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200688
689 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200690 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100691 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand656730d2021-03-08 15:26:48 +0100692 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100693 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100694 /* fall through */
695
696 return_prx_err:
697 http_reply_and_close(s, txn->status, http_error_message(s));
698 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200699
700 return_prx_cond:
701 if (!(s->flags & SF_ERR_MASK))
702 s->flags |= SF_ERR_PRXCOND;
703 if (!(s->flags & SF_FINST_MASK))
704 s->flags |= SF_FINST_R;
705
706 req->analysers &= AN_REQ_FLT_END;
707 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100708 DBG_TRACE_DEVEL("leaving on error",
709 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200710 return 0;
711
712 return_prx_yield:
713 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100714 DBG_TRACE_DEVEL("waiting for more data",
715 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200716 return 0;
717}
718
719/* This function performs all the processing enabled for the current request.
720 * It returns 1 if the processing can continue on next analysers, or zero if it
721 * needs more data, encounters an error, or wants to immediately abort the
722 * request. It relies on buffers flags, and updates s->req.analysers.
723 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200724int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200725{
726 struct session *sess = s->sess;
727 struct http_txn *txn = s->txn;
728 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200729 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200730 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
731
732 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
733 /* we need more data */
734 channel_dont_connect(req);
735 return 0;
736 }
737
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100738 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200739
740 /*
741 * Right now, we know that we have processed the entire headers
742 * and that unwanted requests have been filtered out. We can do
743 * whatever we want with the remaining request. Also, now we
744 * may have separate values for ->fe, ->be.
745 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100746 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200747
748 /*
749 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200750 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200751 */
752 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100753 struct htx_sl *sl;
754 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200755
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200756 if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200757 if (!(s->flags & SF_ERR_MASK))
758 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100759 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200760 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200761 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100762 uri = htx_sl_req_uri(sl);
763 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200764
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200765 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200766 goto return_bad_req;
767
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200768 s->target = &s->be->obj_type;
769 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
770
Christopher Faulete0768eb2018-10-03 16:38:02 +0200771 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200772 * uri.ptr and path.ptr (excluded). If it was not found, we need
773 * to replace from all the uri by a single "/".
774 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500775 * Instead of rewriting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100776 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200777 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200778 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100779 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200780 }
781
782 /*
783 * 7: Now we can work with the cookies.
784 * Note that doing so might move headers in the request, but
785 * the fields will stay coherent and the URI will not move.
786 * This should only be performed in the backend.
787 */
788 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200789 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200790
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100791 /* 8: Generate unique ID if a "unique-id-format" is defined.
792 *
793 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
794 * fetches only available in the HTTP request processing stage.
795 */
796 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100797 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200798
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100799 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100800 if (!(s->flags & SF_ERR_MASK))
801 s->flags |= SF_ERR_RESOURCE;
802 goto return_int_err;
803 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200804
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100805 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100806 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100807 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100808 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200809 }
810
811 /*
812 * 9: add X-Forwarded-For if either the frontend or the backend
813 * asks for it.
814 */
815 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200816 struct http_hdr_ctx ctx = { .blk = NULL };
817 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
818 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
819
Christopher Faulete0768eb2018-10-03 16:38:02 +0200820 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200821 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200822 /* The header is set to be added only if none is present
823 * and we found it, so don't do anything.
824 */
825 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200826 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200827 /* Add an X-Forwarded-For header unless the source IP is
828 * in the 'except' network range.
829 */
830 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200831 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200832 != sess->fe->except_net.s_addr) &&
833 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200834 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200835 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200836 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200837
838 /* Note: we rely on the backend to get the header name to be used for
839 * x-forwarded-for, because the header is really meant for the backends.
840 * However, if the backend did not specify any option, we have to rely
841 * on the frontend's header name.
842 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200843 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
844 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100845 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200846 }
847 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200848 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200849 /* FIXME: for the sake of completeness, we should also support
850 * 'except' here, although it is mostly useless in this case.
851 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200852 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200853
Christopher Faulete0768eb2018-10-03 16:38:02 +0200854 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200855 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200856 pn, sizeof(pn));
857
858 /* Note: we rely on the backend to get the header name to be used for
859 * x-forwarded-for, because the header is really meant for the backends.
860 * However, if the backend did not specify any option, we have to rely
861 * on the frontend's header name.
862 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200863 chunk_printf(&trash, "%s", pn);
864 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100865 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200866 }
867 }
868
869 /*
870 * 10: add X-Original-To if either the frontend or the backend
871 * asks for it.
872 */
873 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
874
875 /* FIXME: don't know if IPv6 can handle that case too. */
Christopher Fauletdf82f0b2021-02-26 12:45:56 +0100876 if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200877 /* Add an X-Original-To header unless the destination IP is
878 * in the 'except' network range.
879 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200880 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200881 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200882 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200883 != sess->fe->except_to.s_addr) &&
884 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200885 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200886 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200887 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200888 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200889
890 /* Note: we rely on the backend to get the header name to be used for
891 * x-original-to, because the header is really meant for the backends.
892 * However, if the backend did not specify any option, we have to rely
893 * on the frontend's header name.
894 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200895 if (s->be->orgto_hdr_len)
896 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
897 else
898 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200899
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200900 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
901 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100902 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200903 }
904 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200905 }
906
Christopher Faulete0768eb2018-10-03 16:38:02 +0200907 /* If we have no server assigned yet and we're balancing on url_param
908 * with a POST request, we may be interested in checking the body for
909 * that parameter. This will be done in another analyser.
910 */
911 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100912 s->txn->meth == HTTP_METH_POST &&
913 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200914 channel_dont_connect(req);
915 req->analysers |= AN_REQ_HTTP_BODY;
916 }
917
918 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
919 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100920
Christopher Faulete0768eb2018-10-03 16:38:02 +0200921 /* We expect some data from the client. Unless we know for sure
922 * we already have a full request, we have to re-enable quick-ack
923 * in case we previously disabled it, otherwise we might cause
924 * the client to delay further data.
925 */
William Lallemand656730d2021-03-08 15:26:48 +0100926 if (sess->listener && (sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200927 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100928 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200929
930 /*************************************************************
931 * OK, that's finished for the headers. We have done what we *
932 * could. Let's switch to the DATA state. *
933 ************************************************************/
934 req->analyse_exp = TICK_ETERNITY;
935 req->analysers &= ~an_bit;
936
937 s->logs.tv_request = now;
938 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100939 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200940 return 1;
941
Christopher Fauletb8a53712019-12-16 11:29:38 +0100942 return_int_err:
943 txn->status = 500;
944 if (!(s->flags & SF_ERR_MASK))
945 s->flags |= SF_ERR_INTERNAL;
946 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100947 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100948 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand656730d2021-03-08 15:26:48 +0100949 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100950 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
951 goto return_prx_cond;
952
Christopher Faulete0768eb2018-10-03 16:38:02 +0200953 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200954 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100955 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand656730d2021-03-08 15:26:48 +0100956 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100957 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100958 /* fall through */
959
960 return_prx_cond:
961 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200962
963 if (!(s->flags & SF_ERR_MASK))
964 s->flags |= SF_ERR_PRXCOND;
965 if (!(s->flags & SF_FINST_MASK))
966 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100967
968 req->analysers &= AN_REQ_FLT_END;
969 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100970 DBG_TRACE_DEVEL("leaving on error",
971 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200972 return 0;
973}
974
975/* This function is an analyser which processes the HTTP tarpit. It always
976 * returns zero, at the beginning because it prevents any other processing
977 * from occurring, and at the end because it terminates the request.
978 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200979int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200980{
981 struct http_txn *txn = s->txn;
982
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100983 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200984 /* This connection is being tarpitted. The CLIENT side has
985 * already set the connect expiration date to the right
986 * timeout. We just have to check that the client is still
987 * there and that the timeout has not expired.
988 */
989 channel_dont_connect(req);
990 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100991 !tick_is_expired(req->analyse_exp, now_ms)) {
992 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
993 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200994 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100995 }
996
Christopher Faulete0768eb2018-10-03 16:38:02 +0200997
998 /* We will set the queue timer to the time spent, just for
999 * logging purposes. We fake a 500 server error, so that the
1000 * attacker will not suspect his connection has been tarpitted.
1001 * It will not cause trouble to the logs because we can exclude
1002 * the tarpitted connections by filtering on the 'PT' status flags.
1003 */
1004 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1005
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02001006 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001007
1008 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001009 req->analysers &= AN_REQ_FLT_END;
1010 req->analyse_exp = TICK_ETERNITY;
1011
1012 if (!(s->flags & SF_ERR_MASK))
1013 s->flags |= SF_ERR_PRXCOND;
1014 if (!(s->flags & SF_FINST_MASK))
1015 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001016
1017 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001018 return 0;
1019}
1020
1021/* This function is an analyser which waits for the HTTP request body. It waits
1022 * for either the buffer to be full, or the full advertised contents to have
1023 * reached the buffer. It must only be called after the standard HTTP request
1024 * processing has occurred, because it expects the request to be parsed and will
1025 * look for the Expect header. It may send a 100-Continue interim response. It
1026 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
1027 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
1028 * needs to read more data, or 1 once it has completed its analysis.
1029 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001030int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001031{
1032 struct session *sess = s->sess;
1033 struct http_txn *txn = s->txn;
1034 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001035 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001036
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001037 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001038
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001039 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001040
Willy Tarreau4236f032019-03-05 10:43:32 +01001041 if (htx->flags & HTX_FL_PARSING_ERROR)
1042 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001043 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1044 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001045
Christopher Faulet9ded6fa2020-11-16 16:03:35 +01001046 /* CONNECT requests have no body */
1047 if (txn->meth == HTTP_METH_CONNECT)
1048 goto http_end;
1049
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001050 if (msg->msg_state < HTTP_MSG_BODY)
1051 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001052
Christopher Faulete0768eb2018-10-03 16:38:02 +02001053 /* We have to parse the HTTP request body to find any required data.
1054 * "balance url_param check_post" should have been the only way to get
1055 * into this. We were brought here after HTTP header analysis, so all
1056 * related structures are ready.
1057 */
1058
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001059 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001060 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001061 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001062 }
1063
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001064 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001065
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001066 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1067 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001068 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001069 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001070 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001071 goto http_end;
1072
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001073 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001074 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1075 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001076 if (!(s->flags & SF_ERR_MASK))
1077 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001078 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand656730d2021-03-08 15:26:48 +01001079 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001080 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1081 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001082 }
1083
1084 /* we get here if we need to wait for more data */
1085 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1086 /* Not enough data. We'll re-use the http-request
1087 * timeout here. Ideally, we should set the timeout
1088 * relative to the accept() date. We just set the
1089 * request timeout once at the beginning of the
1090 * request.
1091 */
1092 channel_dont_connect(req);
1093 if (!tick_isset(req->analyse_exp))
1094 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001095 DBG_TRACE_DEVEL("waiting for more data",
1096 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001097 return 0;
1098 }
1099
1100 http_end:
1101 /* The situation will not evolve, so let's give up on the analysis. */
1102 s->logs.tv_request = now; /* update the request timer to reflect full request */
1103 req->analysers &= ~an_bit;
1104 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001105 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001106 return 1;
1107
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001108 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001109 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001110 if (!(s->flags & SF_ERR_MASK))
1111 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001112 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001113 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +01001114 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand656730d2021-03-08 15:26:48 +01001115 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001116 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
1117 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001118
Christopher Faulete0768eb2018-10-03 16:38:02 +02001119 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001120 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001121 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand656730d2021-03-08 15:26:48 +01001122 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001123 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1124 /* fall through */
1125
1126 return_prx_cond:
1127 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001128
1129 if (!(s->flags & SF_ERR_MASK))
1130 s->flags |= SF_ERR_PRXCOND;
1131 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001132 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001133
Christopher Faulete0768eb2018-10-03 16:38:02 +02001134 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001135 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001136 DBG_TRACE_DEVEL("leaving on error",
1137 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001138 return 0;
1139}
1140
1141/* This function is an analyser which forwards request body (including chunk
1142 * sizes if any). It is called as soon as we must forward, even if we forward
1143 * zero byte. The only situation where it must not be called is when we're in
1144 * tunnel mode and we want to forward till the close. It's used both to forward
1145 * remaining data and to resync after end of body. It expects the msg_state to
1146 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1147 * read more data, or 1 once we can go on with next request or end the stream.
1148 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1149 * bytes of pending data + the headers if not already done.
1150 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001151int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001152{
1153 struct session *sess = s->sess;
1154 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001155 struct http_msg *msg = &txn->req;
1156 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001157 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001158 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001159
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001160 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001161
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001162 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001163
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001164 if (htx->flags & HTX_FL_PARSING_ERROR)
1165 goto return_bad_req;
1166 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1167 goto return_int_err;
1168
Christopher Faulete0768eb2018-10-03 16:38:02 +02001169 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1170 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1171 /* Output closed while we were sending data. We must abort and
1172 * wake the other side up.
1173 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001174
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001175 /* Don't abort yet if we had L7 retries activated and it
1176 * was a write error, we may recover.
1177 */
1178 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001179 (s->si[1].flags & SI_FL_L7_RETRY)) {
1180 DBG_TRACE_DEVEL("leaving on L7 retry",
1181 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001182 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001183 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001184 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001185 http_end_request(s);
1186 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001187 DBG_TRACE_DEVEL("leaving on error",
1188 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001189 return 1;
1190 }
1191
1192 /* Note that we don't have to send 100-continue back because we don't
1193 * need the data to complete our job, and it's up to the server to
1194 * decide whether to return 100, 417 or anything else in return of
1195 * an "Expect: 100-continue" header.
1196 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001197 if (msg->msg_state == HTTP_MSG_BODY)
1198 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001199
Christopher Faulete0768eb2018-10-03 16:38:02 +02001200 /* in most states, we should abort in case of early close */
1201 channel_auto_close(req);
1202
1203 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001204 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001205 if (req->flags & CF_EOI)
1206 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001207 }
1208 else {
1209 /* We can't process the buffer's contents yet */
1210 req->flags |= CF_WAKE_WRITE;
1211 goto missing_data_or_waiting;
1212 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001213 }
1214
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001215 if (msg->msg_state >= HTTP_MSG_ENDING)
1216 goto ending;
1217
1218 if (txn->meth == HTTP_METH_CONNECT) {
1219 msg->msg_state = HTTP_MSG_ENDING;
1220 goto ending;
1221 }
1222
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001223 /* Forward input data. We get it by removing all outgoing data not
1224 * forwarded yet from HTX data size. If there are some data filters, we
1225 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001226 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001227 if (HAS_REQ_DATA_FILTERS(s)) {
1228 ret = flt_http_payload(s, msg, htx->data);
1229 if (ret < 0)
1230 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001231 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001232 }
1233 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001234 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001235 if (msg->flags & HTTP_MSGF_XFER_LEN)
1236 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001237 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001238
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001239 if (htx->data != co_data(req))
1240 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001241
Christopher Faulet9768c262018-10-22 09:34:31 +02001242 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001243 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1244 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001245 */
1246 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1247 goto missing_data_or_waiting;
1248
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001249 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001250
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001251 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001252 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1253
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001254 /* other states, ENDING...TUNNEL */
1255 if (msg->msg_state >= HTTP_MSG_DONE)
1256 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001257
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001258 if (HAS_REQ_DATA_FILTERS(s)) {
1259 ret = flt_http_end(s, msg);
1260 if (ret <= 0) {
1261 if (!ret)
1262 goto missing_data_or_waiting;
1263 goto return_bad_req;
1264 }
1265 }
1266
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001267 if (txn->meth == HTTP_METH_CONNECT)
1268 msg->msg_state = HTTP_MSG_TUNNEL;
1269 else {
1270 msg->msg_state = HTTP_MSG_DONE;
1271 req->to_forward = 0;
1272 }
1273
1274 done:
1275 /* we don't want to forward closes on DONE except in tunnel mode. */
1276 if (!(txn->flags & TX_CON_WANT_TUN))
1277 channel_dont_close(req);
1278
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001279 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001280 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001281 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001282 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1283 if (req->flags & CF_SHUTW) {
1284 /* request errors are most likely due to the
1285 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001286 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001287 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001288 goto return_bad_req;
1289 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001290 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001291 return 1;
1292 }
1293
1294 /* If "option abortonclose" is set on the backend, we want to monitor
1295 * the client's connection and forward any shutdown notification to the
1296 * server, which will decide whether to close or to go on processing the
1297 * request. We only do that in tunnel mode, and not in other modes since
1298 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001299 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001300 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001301 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001302 s->si[1].flags |= SI_FL_NOLINGER;
1303 channel_auto_close(req);
1304 }
1305 else if (s->txn->meth == HTTP_METH_POST) {
1306 /* POST requests may require to read extra CRLF sent by broken
1307 * browsers and which could cause an RST to be sent upon close
1308 * on some systems (eg: Linux). */
1309 channel_auto_read(req);
1310 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001311 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1312 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001313 return 0;
1314
1315 missing_data_or_waiting:
1316 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001317 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001318 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001319
1320 waiting:
1321 /* waiting for the last bits to leave the buffer */
1322 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001323 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001324
1325 /* When TE: chunked is used, we need to get there again to parse remaining
1326 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1327 * And when content-length is used, we never want to let the possible
1328 * shutdown be forwarded to the other side, as the state machine will
1329 * take care of it once the client responds. It's also important to
1330 * prevent TIME_WAITs from accumulating on the backend side, and for
1331 * HTTP/2 where the last frame comes with a shutdown.
1332 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001333 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001334 channel_dont_close(req);
1335
1336 /* We know that more data are expected, but we couldn't send more that
1337 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1338 * system knows it must not set a PUSH on this first part. Interactive
1339 * modes are already handled by the stream sock layer. We must not do
1340 * this in content-length mode because it could present the MSG_MORE
1341 * flag with the last block of forwarded data, which would cause an
1342 * additional delay to be observed by the receiver.
1343 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001344 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001345 req->flags |= CF_EXPECT_MORE;
1346
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001347 DBG_TRACE_DEVEL("waiting for more data to forward",
1348 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001349 return 0;
1350
Christopher Faulet93e02d82019-03-08 14:18:50 +01001351 return_cli_abort:
1352 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1353 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand656730d2021-03-08 15:26:48 +01001354 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001355 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001356 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001357 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001358 if (!(s->flags & SF_ERR_MASK))
1359 s->flags |= SF_ERR_CLICL;
1360 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001361 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001362
1363 return_srv_abort:
1364 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1365 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
William Lallemand656730d2021-03-08 15:26:48 +01001366 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001367 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001368 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001369 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001370 if (!(s->flags & SF_ERR_MASK))
1371 s->flags |= SF_ERR_SRVCL;
1372 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001373 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001374
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001375 return_int_err:
1376 if (!(s->flags & SF_ERR_MASK))
1377 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001378 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001379 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand656730d2021-03-08 15:26:48 +01001380 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001381 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001382 if (objt_server(s->target))
1383 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001384 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001385 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001386
Christopher Faulet93e02d82019-03-08 14:18:50 +01001387 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001388 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
William Lallemand656730d2021-03-08 15:26:48 +01001389 if (sess->listener && sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001390 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001391 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001392 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001393
Christopher Fauletb8a53712019-12-16 11:29:38 +01001394 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001395 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001396 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001397 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001398 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001399 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001400 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001401 }
1402 req->analysers &= AN_REQ_FLT_END;
1403 s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001404 if (!(s->flags & SF_ERR_MASK))
1405 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001406 if (!(s->flags & SF_FINST_MASK))
1407 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001408 DBG_TRACE_DEVEL("leaving on error ",
1409 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001410 return 0;
1411}
1412
Olivier Houcharda254a372019-04-05 15:30:12 +02001413/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1414/* Returns 0 if we can attempt to retry, -1 otherwise */
1415static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1416{
1417 struct channel *req, *res;
1418 int co_data;
1419
1420 si->conn_retries--;
1421 if (si->conn_retries < 0)
1422 return -1;
1423
Christopher Faulet669d6672021-05-05 18:23:59 +02001424 if (objt_server(s->target)) {
1425 if (s->flags & SF_CURR_SESS) {
1426 s->flags &= ~SF_CURR_SESS;
1427 _HA_ATOMIC_SUB(&__objt_server(s->target)->cur_sess, 1);
1428 }
Willy Tarreau223995e2019-05-04 10:38:31 +02001429 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
Christopher Faulet669d6672021-05-05 18:23:59 +02001430 }
Willy Tarreau223995e2019-05-04 10:38:31 +02001431 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1432
Olivier Houcharda254a372019-04-05 15:30:12 +02001433 req = &s->req;
1434 res = &s->res;
1435 /* Remove any write error from the request, and read error from the response */
1436 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1437 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1438 res->analysers = 0;
1439 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Christopher Fauletd2d264f2021-05-05 21:05:09 +02001440 si->err_type = SI_ET_NONE;
1441 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001442 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001443 si->exp = TICK_ETERNITY;
1444 res->rex = TICK_ETERNITY;
1445 res->to_forward = 0;
1446 res->analyse_exp = TICK_ETERNITY;
1447 res->total = 0;
Olivier Houcharda254a372019-04-05 15:30:12 +02001448 si_release_endpoint(&s->si[1]);
1449 b_free(&req->buf);
1450 /* Swap the L7 buffer with the channel buffer */
1451 /* We know we stored the co_data as b_data, so get it there */
1452 co_data = b_data(&si->l7_buffer);
1453 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1454 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1455
1456 co_set_data(req, co_data);
1457 b_reset(&res->buf);
1458 co_set_data(res, 0);
1459 return 0;
1460}
1461
Christopher Faulete0768eb2018-10-03 16:38:02 +02001462/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1463 * processing can continue on next analysers, or zero if it either needs more
1464 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1465 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1466 * when it has nothing left to do, and may remove any analyser when it wants to
1467 * abort.
1468 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001469int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001470{
Christopher Faulet9768c262018-10-22 09:34:31 +02001471 /*
1472 * We will analyze a complete HTTP response to check the its syntax.
1473 *
1474 * Once the start line and all headers are received, we may perform a
1475 * capture of the error (if any), and we will set a few fields. We also
1476 * logging and finally headers capture.
1477 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001478 struct session *sess = s->sess;
1479 struct http_txn *txn = s->txn;
1480 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001481 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001482 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001483 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001484 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001485 int n;
1486
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001487 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001488
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001489 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001490
Willy Tarreau4236f032019-03-05 10:43:32 +01001491 /* Parsing errors are caught here */
1492 if (htx->flags & HTX_FL_PARSING_ERROR)
1493 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001494 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1495 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001496
Christopher Faulete0768eb2018-10-03 16:38:02 +02001497 /*
1498 * Now we quickly check if we have found a full valid response.
1499 * If not so, we check the FD and buffer states before leaving.
1500 * A full response is indicated by the fact that we have seen
1501 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1502 * responses are checked first.
1503 *
1504 * Depending on whether the client is still there or not, we
1505 * may send an error response back or not. Note that normally
1506 * we should only check for HTTP status there, and check I/O
1507 * errors somewhere else.
1508 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001509 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001510 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001511 /* 1: have we encountered a read error ? */
1512 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001513 struct connection *conn = NULL;
1514
Olivier Houchard865d8392019-05-03 22:46:27 +02001515 if (objt_cs(s->si[1].end))
1516 conn = objt_cs(s->si[1].end)->conn;
1517
1518 if (si_b->flags & SI_FL_L7_RETRY &&
1519 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001520 /* If we arrive here, then CF_READ_ERROR was
1521 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001522 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001523 * the SI_FL_L7_RETRY flag, so it's ok not
1524 * to check s->be->retry_type.
1525 */
1526 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1527 return 0;
1528 }
1529
Olivier Houchard6db16992019-05-17 15:40:49 +02001530 if (txn->flags & TX_NOT_FIRST)
1531 goto abort_keep_alive;
1532
Olivier Houcharda798bf52019-03-08 18:52:00 +01001533 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001534 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001535 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001536 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001537 }
1538
Christopher Faulete0768eb2018-10-03 16:38:02 +02001539 rep->analysers &= AN_RES_FLT_END;
1540 txn->status = 502;
1541
1542 /* Check to see if the server refused the early data.
1543 * If so, just send a 425
1544 */
Willy Tarreauee99aaf2020-06-23 05:58:20 +02001545 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001546 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001547 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001548 do_l7_retry(s, si_b) == 0) {
1549 DBG_TRACE_DEVEL("leaving on L7 retry",
1550 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001551 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001552 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001553 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001554 }
1555
1556 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001557 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001558
1559 if (!(s->flags & SF_ERR_MASK))
1560 s->flags |= SF_ERR_SRVCL;
1561 if (!(s->flags & SF_FINST_MASK))
1562 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001563 DBG_TRACE_DEVEL("leaving on error",
1564 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001565 return 0;
1566 }
1567
Christopher Faulet9768c262018-10-22 09:34:31 +02001568 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001569 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001570 if ((si_b->flags & SI_FL_L7_RETRY) &&
1571 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001572 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1573 DBG_TRACE_DEVEL("leaving on L7 retry",
1574 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001575 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001576 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001577 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001578 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001579 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001580 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001581 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001582 }
1583
Christopher Faulete0768eb2018-10-03 16:38:02 +02001584 rep->analysers &= AN_RES_FLT_END;
1585 txn->status = 504;
1586 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001587 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001588
1589 if (!(s->flags & SF_ERR_MASK))
1590 s->flags |= SF_ERR_SRVTO;
1591 if (!(s->flags & SF_FINST_MASK))
1592 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001593 DBG_TRACE_DEVEL("leaving on error",
1594 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001595 return 0;
1596 }
1597
Christopher Faulet9768c262018-10-22 09:34:31 +02001598 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001599 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001600 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1601 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand656730d2021-03-08 15:26:48 +01001602 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001603 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001604 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001605 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001606
1607 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001608 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001609 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001610
1611 if (!(s->flags & SF_ERR_MASK))
1612 s->flags |= SF_ERR_CLICL;
1613 if (!(s->flags & SF_FINST_MASK))
1614 s->flags |= SF_FINST_H;
1615
1616 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001617 DBG_TRACE_DEVEL("leaving on error",
1618 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001619 return 0;
1620 }
1621
Christopher Faulet9768c262018-10-22 09:34:31 +02001622 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001623 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001624 if ((si_b->flags & SI_FL_L7_RETRY) &&
1625 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001626 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1627 DBG_TRACE_DEVEL("leaving on L7 retry",
1628 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001629 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001630 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001631 }
1632
Olivier Houchard6db16992019-05-17 15:40:49 +02001633 if (txn->flags & TX_NOT_FIRST)
1634 goto abort_keep_alive;
1635
Olivier Houcharda798bf52019-03-08 18:52:00 +01001636 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001637 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001638 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001639 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001640 }
1641
Christopher Faulete0768eb2018-10-03 16:38:02 +02001642 rep->analysers &= AN_RES_FLT_END;
1643 txn->status = 502;
1644 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001645 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001646
1647 if (!(s->flags & SF_ERR_MASK))
1648 s->flags |= SF_ERR_SRVCL;
1649 if (!(s->flags & SF_FINST_MASK))
1650 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001651 DBG_TRACE_DEVEL("leaving on error",
1652 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001653 return 0;
1654 }
1655
Christopher Faulet9768c262018-10-22 09:34:31 +02001656 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001657 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001658 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001659 goto abort_keep_alive;
1660
Olivier Houcharda798bf52019-03-08 18:52:00 +01001661 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001662 if (objt_server(s->target))
1663 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001664 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001665
1666 if (!(s->flags & SF_ERR_MASK))
1667 s->flags |= SF_ERR_CLICL;
1668 if (!(s->flags & SF_FINST_MASK))
1669 s->flags |= SF_FINST_H;
1670
1671 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001672 DBG_TRACE_DEVEL("leaving on error",
1673 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001674 return 0;
1675 }
1676
1677 channel_dont_close(rep);
1678 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001679 DBG_TRACE_DEVEL("waiting for more data",
1680 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001681 return 0;
1682 }
1683
1684 /* More interesting part now : we know that we have a complete
1685 * response which at least looks like HTTP. We have an indicator
1686 * of each header's length, so we can parse them quickly.
1687 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001688 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001689 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001690 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001691
Christopher Faulet9768c262018-10-22 09:34:31 +02001692 /* 0: we might have to print this header in debug mode */
1693 if (unlikely((global.mode & MODE_DEBUG) &&
1694 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1695 int32_t pos;
1696
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001697 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001698
Christopher Fauleta3f15502019-05-13 15:27:23 +02001699 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001700 struct htx_blk *blk = htx_get_blk(htx, pos);
1701 enum htx_blk_type type = htx_get_blk_type(blk);
1702
1703 if (type == HTX_BLK_EOH)
1704 break;
1705 if (type != HTX_BLK_HDR)
1706 continue;
1707
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001708 http_debug_hdr("srvhdr", s,
1709 htx_get_blk_name(htx, blk),
1710 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001711 }
1712 }
1713
Christopher Faulet03599112018-11-27 11:21:21 +01001714 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001715 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001716 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001717 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001718 if (sl->flags & HTX_SL_F_XFER_LEN) {
1719 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001720 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001721 if (sl->flags & HTX_SL_F_BODYLESS)
1722 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001723 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001724
1725 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001726 if (n < 1 || n > 5)
1727 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001728
Christopher Faulete0768eb2018-10-03 16:38:02 +02001729 /* when the client triggers a 4xx from the server, it's most often due
1730 * to a missing object or permission. These events should be tracked
1731 * because if they happen often, it may indicate a brute force or a
1732 * vulnerability scan.
1733 */
1734 if (n == 4)
1735 stream_inc_http_err_ctr(s);
1736
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001737 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001738 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001739 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1740 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001741
Christopher Faulete0768eb2018-10-03 16:38:02 +02001742 /* Adjust server's health based on status code. Note: status codes 501
1743 * and 505 are triggered on demand by client request, so we must not
1744 * count them as server failures.
1745 */
1746 if (objt_server(s->target)) {
1747 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001748 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001749 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001750 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001751 }
1752
1753 /*
1754 * We may be facing a 100-continue response, or any other informational
1755 * 1xx response which is non-final, in which case this is not the right
1756 * response, and we're waiting for the next one. Let's allow this response
1757 * to go to the client and wait for the next one. There's an exception for
1758 * 101 which is used later in the code to switch protocols.
1759 */
1760 if (txn->status < 200 &&
1761 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001762 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001763 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001764 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001765 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001766 txn->status = 0;
1767 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001768 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001769 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001770 }
1771
1772 /*
1773 * 2: check for cacheability.
1774 */
1775
1776 switch (txn->status) {
1777 case 200:
1778 case 203:
1779 case 204:
1780 case 206:
1781 case 300:
1782 case 301:
1783 case 404:
1784 case 405:
1785 case 410:
1786 case 414:
1787 case 501:
1788 break;
1789 default:
1790 /* RFC7231#6.1:
1791 * Responses with status codes that are defined as
1792 * cacheable by default (e.g., 200, 203, 204, 206,
1793 * 300, 301, 404, 405, 410, 414, and 501 in this
1794 * specification) can be reused by a cache with
1795 * heuristic expiration unless otherwise indicated
1796 * by the method definition or explicit cache
1797 * controls [RFC7234]; all other status codes are
1798 * not cacheable by default.
1799 */
1800 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1801 break;
1802 }
1803
1804 /*
1805 * 3: we may need to capture headers
1806 */
1807 s->logs.logwait &= ~LW_RESP;
1808 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001809 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001810
Christopher Faulet9768c262018-10-22 09:34:31 +02001811 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001812 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1813 txn->status == 101)) {
1814 /* Either we've established an explicit tunnel, or we're
1815 * switching the protocol. In both cases, we're very unlikely
1816 * to understand the next protocols. We have to switch to tunnel
1817 * mode, so that we transfer the request and responses then let
1818 * this protocol pass unmodified. When we later implement specific
1819 * parsers for such protocols, we'll want to check the Upgrade
1820 * header which contains information about that protocol for
1821 * responses with status 101 (eg: see RFC2817 about TLS).
1822 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001823 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001824 }
1825
Christopher Faulet61608322018-11-23 16:23:45 +01001826 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1827 * 407 (Proxy-Authenticate) responses and set the connection to private
1828 */
1829 srv_conn = cs_conn(objt_cs(s->si[1].end));
1830 if (srv_conn) {
1831 struct ist hdr;
1832 struct http_hdr_ctx ctx;
1833
1834 if (txn->status == 401)
1835 hdr = ist("WWW-Authenticate");
1836 else if (txn->status == 407)
1837 hdr = ist("Proxy-Authenticate");
1838 else
1839 goto end;
1840
1841 ctx.blk = NULL;
1842 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001843 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1844 * possibly followed by blanks and a base64 string, the connection
1845 * is private. Since it's a mess to deal with, we only check for
1846 * values starting with "NTLM" or "Nego". Note that often multiple
1847 * headers are sent by the server there.
1848 */
1849 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001850 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001851 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001852 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001853 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001854 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001855 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001856 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001857 }
Christopher Faulet61608322018-11-23 16:23:45 +01001858 }
1859 }
1860
1861 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001862 /* we want to have the response time before we start processing it */
1863 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1864
1865 /* end of job, return OK */
1866 rep->analysers &= ~an_bit;
1867 rep->analyse_exp = TICK_ETERNITY;
1868 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001869 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001870 return 1;
1871
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001872 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001873 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001874 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand656730d2021-03-08 15:26:48 +01001875 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01001876 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001877 if (objt_server(s->target))
1878 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001879 txn->status = 500;
1880 if (!(s->flags & SF_ERR_MASK))
1881 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001882 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001883
1884 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001885 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001886 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001887 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001888 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001889 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001890 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001891 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001892 do_l7_retry(s, si_b) == 0) {
1893 DBG_TRACE_DEVEL("leaving on L7 retry",
1894 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001895 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001896 }
Christopher Faulet47365272018-10-31 17:40:50 +01001897 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001898 /* fall through */
1899
Christopher Fauletb8a53712019-12-16 11:29:38 +01001900 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001901 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001902
1903 if (!(s->flags & SF_ERR_MASK))
1904 s->flags |= SF_ERR_PRXCOND;
1905 if (!(s->flags & SF_FINST_MASK))
1906 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001907
1908 s->si[1].flags |= SI_FL_NOLINGER;
1909 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001910 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001911 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001912 DBG_TRACE_DEVEL("leaving on error",
1913 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001914 return 0;
1915
Christopher Faulete0768eb2018-10-03 16:38:02 +02001916 abort_keep_alive:
1917 /* A keep-alive request to the server failed on a network error.
1918 * The client is required to retry. We need to close without returning
1919 * any other information so that the client retries.
1920 */
1921 txn->status = 0;
1922 rep->analysers &= AN_RES_FLT_END;
1923 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001924 s->logs.logwait = 0;
1925 s->logs.level = 0;
1926 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001927 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001928 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1929 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001930 return 0;
1931}
1932
1933/* This function performs all the processing enabled for the current response.
1934 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1935 * and updates s->res.analysers. It might make sense to explode it into several
1936 * other functions. It works like process_request (see indications above).
1937 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001938int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001939{
1940 struct session *sess = s->sess;
1941 struct http_txn *txn = s->txn;
1942 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001943 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001944 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001945 enum rule_result ret = HTTP_RULE_RES_CONT;
1946
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001947 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1948 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001949
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001950 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001951
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001952 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001953
1954 /* The stats applet needs to adjust the Connection header but we don't
1955 * apply any filter there.
1956 */
1957 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1958 rep->analysers &= ~an_bit;
1959 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001960 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001961 }
1962
1963 /*
1964 * We will have to evaluate the filters.
1965 * As opposed to version 1.2, now they will be evaluated in the
1966 * filters order and not in the header order. This means that
1967 * each filter has to be validated among all headers.
1968 *
1969 * Filters are tried with ->be first, then with ->fe if it is
1970 * different from ->be.
1971 *
1972 * Maybe we are in resume condiion. In this case I choose the
1973 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001974 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001975 * the process with the first one.
1976 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001977 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001978 * pointer and the ->fe rule list. If it doesn't match, I initialize
1979 * the loop with the ->be.
1980 */
1981 if (s->current_rule_list == &sess->fe->http_res_rules)
1982 cur_proxy = sess->fe;
1983 else
1984 cur_proxy = s->be;
1985 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001986 /* evaluate http-response rules */
1987 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001988 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001989
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001990 switch (ret) {
1991 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1992 goto return_prx_yield;
1993
1994 case HTTP_RULE_RES_CONT:
1995 case HTTP_RULE_RES_STOP: /* nothing to do */
1996 break;
1997
1998 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1999 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002000
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002001 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
2002 goto return_prx_cond;
2003
2004 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01002005 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002006
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002007 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2008 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002009
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002010 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
2011 goto return_int_err;
2012 }
2013
2014 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002015
Christopher Faulete0768eb2018-10-03 16:38:02 +02002016 /* check whether we're already working on the frontend */
2017 if (cur_proxy == sess->fe)
2018 break;
2019 cur_proxy = sess->fe;
2020 }
2021
Christopher Faulete0768eb2018-10-03 16:38:02 +02002022 /* OK that's all we can do for 1xx responses */
2023 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002024 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002025
2026 /*
2027 * Now check for a server cookie.
2028 */
2029 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002030 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002031
2032 /*
2033 * Check for cache-control or pragma headers if required.
2034 */
2035 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002036 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002037
2038 /*
2039 * Add server cookie in the response if needed
2040 */
2041 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
2042 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
2043 (!(s->flags & SF_DIRECT) ||
2044 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
2045 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
2046 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
2047 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
2048 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
2049 !(s->flags & SF_IGNORE_PRST)) {
2050 /* the server is known, it's not the one the client requested, or the
2051 * cookie's last seen date needs to be refreshed. We have to
2052 * insert a set-cookie here, except if we want to insert only on POST
2053 * requests and this one isn't. Note that servers which don't have cookies
2054 * (eg: some backup servers) will return a full cookie removal request.
2055 */
2056 if (!objt_server(s->target)->cookie) {
2057 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002058 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02002059 s->be->cookie_name);
2060 }
2061 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002062 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002063
2064 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2065 /* emit last_date, which is mandatory */
2066 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2067 s30tob64((date.tv_sec+3) >> 2,
2068 trash.area + trash.data);
2069 trash.data += 5;
2070
2071 if (s->be->cookie_maxlife) {
2072 /* emit first_date, which is either the original one or
2073 * the current date.
2074 */
2075 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2076 s30tob64(txn->cookie_first_date ?
2077 txn->cookie_first_date >> 2 :
2078 (date.tv_sec+3) >> 2,
2079 trash.area + trash.data);
2080 trash.data += 5;
2081 }
2082 }
2083 chunk_appendf(&trash, "; path=/");
2084 }
2085
2086 if (s->be->cookie_domain)
2087 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2088
2089 if (s->be->ck_opts & PR_CK_HTTPONLY)
2090 chunk_appendf(&trash, "; HttpOnly");
2091
2092 if (s->be->ck_opts & PR_CK_SECURE)
2093 chunk_appendf(&trash, "; Secure");
2094
Christopher Faulet2f533902020-01-21 11:06:48 +01002095 if (s->be->cookie_attrs)
2096 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
2097
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002098 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002099 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002100
2101 txn->flags &= ~TX_SCK_MASK;
2102 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2103 /* the server did not change, only the date was updated */
2104 txn->flags |= TX_SCK_UPDATED;
2105 else
2106 txn->flags |= TX_SCK_INSERTED;
2107
2108 /* Here, we will tell an eventual cache on the client side that we don't
2109 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2110 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2111 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2112 */
2113 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2114
2115 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2116
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002117 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002118 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002119 }
2120 }
2121
2122 /*
2123 * Check if result will be cacheable with a cookie.
2124 * We'll block the response if security checks have caught
2125 * nasty things such as a cacheable cookie.
2126 */
2127 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2128 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2129 (s->be->options & PR_O_CHK_CACHE)) {
2130 /* we're in presence of a cacheable response containing
2131 * a set-cookie header. We'll block it as requested by
2132 * the 'checkcache' option, and send an alert.
2133 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002134 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2135 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2136 send_log(s->be, LOG_ALERT,
2137 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2138 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002139 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002140 }
2141
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002142 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002143 /*
2144 * Evaluate after-response rules before forwarding the response. rules
2145 * from the backend are evaluated first, then one from the frontend if
2146 * it differs.
2147 */
2148 if (!http_eval_after_res_rules(s))
2149 goto return_int_err;
2150
Christopher Faulete0768eb2018-10-03 16:38:02 +02002151 /* Always enter in the body analyzer */
2152 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2153 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2154
2155 /* if the user wants to log as soon as possible, without counting
2156 * bytes from the server, then this is the right moment. We have
2157 * to temporarily assign bytes_out to log what we currently have.
2158 */
2159 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2160 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002161 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002162 s->do_log(s);
2163 s->logs.bytes_out = 0;
2164 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002165
Christopher Fauletb8a53712019-12-16 11:29:38 +01002166 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002167 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002168 rep->analysers &= ~an_bit;
2169 rep->analyse_exp = TICK_ETERNITY;
2170 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002171
Christopher Fauletb8a53712019-12-16 11:29:38 +01002172 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002173 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002174 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
William Lallemand656730d2021-03-08 15:26:48 +01002175 if (sess->listener && sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +01002176 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002177 if (objt_server(s->target))
2178 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002179 goto return_prx_err;
2180
2181 return_int_err:
2182 txn->status = 500;
2183 if (!(s->flags & SF_ERR_MASK))
2184 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002185 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002186 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2187 if (objt_server(s->target))
2188 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002189 if (objt_server(s->target))
2190 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002191 goto return_prx_err;
2192
2193 return_bad_res:
2194 txn->status = 502;
Christopher Fauleta20a6532020-02-05 10:16:41 +01002195 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2196 if (objt_server(s->target)) {
2197 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2198 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2199 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002200 /* fall through */
2201
2202 return_prx_err:
2203 http_reply_and_close(s, txn->status, http_error_message(s));
2204 /* fall through */
2205
2206 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002207 s->logs.t_data = -1; /* was not a valid response */
2208 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002209
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002210 if (!(s->flags & SF_ERR_MASK))
2211 s->flags |= SF_ERR_PRXCOND;
2212 if (!(s->flags & SF_FINST_MASK))
2213 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002214
Christopher Faulete58c0002020-03-02 16:21:01 +01002215 rep->analysers &= AN_RES_FLT_END;
2216 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002217 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002218 DBG_TRACE_DEVEL("leaving on error",
2219 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002220 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002221
2222 return_prx_yield:
2223 channel_dont_close(rep);
2224 DBG_TRACE_DEVEL("waiting for more data",
2225 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2226 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002227}
2228
2229/* This function is an analyser which forwards response body (including chunk
2230 * sizes if any). It is called as soon as we must forward, even if we forward
2231 * zero byte. The only situation where it must not be called is when we're in
2232 * tunnel mode and we want to forward till the close. It's used both to forward
2233 * remaining data and to resync after end of body. It expects the msg_state to
2234 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2235 * read more data, or 1 once we can go on with next request or end the stream.
2236 *
2237 * It is capable of compressing response data both in content-length mode and
2238 * in chunked mode. The state machines follows different flows depending on
2239 * whether content-length and chunked modes are used, since there are no
2240 * trailers in content-length :
2241 *
2242 * chk-mode cl-mode
2243 * ,----- BODY -----.
2244 * / \
2245 * V size > 0 V chk-mode
2246 * .--> SIZE -------------> DATA -------------> CRLF
2247 * | | size == 0 | last byte |
2248 * | v final crlf v inspected |
2249 * | TRAILERS -----------> DONE |
2250 * | |
2251 * `----------------------------------------------'
2252 *
2253 * Compression only happens in the DATA state, and must be flushed in final
2254 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2255 * is performed at once on final states for all bytes parsed, or when leaving
2256 * on missing data.
2257 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002258int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002259{
2260 struct session *sess = s->sess;
2261 struct http_txn *txn = s->txn;
2262 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002263 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002264 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002265
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002266 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002267
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002268 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002269
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002270 if (htx->flags & HTX_FL_PARSING_ERROR)
2271 goto return_bad_res;
2272 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2273 goto return_int_err;
2274
Christopher Faulete0768eb2018-10-03 16:38:02 +02002275 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002276 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002277 /* Output closed while we were sending data. We must abort and
2278 * wake the other side up.
2279 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002280 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002281 http_end_response(s);
2282 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002283 DBG_TRACE_DEVEL("leaving on error",
2284 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002285 return 1;
2286 }
2287
Christopher Faulet9768c262018-10-22 09:34:31 +02002288 if (msg->msg_state == HTTP_MSG_BODY)
2289 msg->msg_state = HTTP_MSG_DATA;
2290
Christopher Faulete0768eb2018-10-03 16:38:02 +02002291 /* in most states, we should abort in case of early close */
2292 channel_auto_close(res);
2293
Christopher Faulete0768eb2018-10-03 16:38:02 +02002294 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002295 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002296 if (res->flags & CF_EOI)
2297 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002298 }
2299 else {
2300 /* We can't process the buffer's contents yet */
2301 res->flags |= CF_WAKE_WRITE;
2302 goto missing_data_or_waiting;
2303 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002304 }
2305
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002306 if (msg->msg_state >= HTTP_MSG_ENDING)
2307 goto ending;
2308
2309 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2310 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2311 msg->msg_state = HTTP_MSG_ENDING;
2312 goto ending;
2313 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002314
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002315 /* Forward input data. We get it by removing all outgoing data not
2316 * forwarded yet from HTX data size. If there are some data filters, we
2317 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002318 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002319 if (HAS_RSP_DATA_FILTERS(s)) {
2320 ret = flt_http_payload(s, msg, htx->data);
2321 if (ret < 0)
2322 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002323 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002324 }
2325 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002326 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002327 if (msg->flags & HTTP_MSGF_XFER_LEN)
2328 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002329 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002330
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002331 if (htx->data != co_data(res))
2332 goto missing_data_or_waiting;
2333
2334 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2335 msg->msg_state = HTTP_MSG_ENDING;
2336 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002337 }
2338
Christopher Faulet9768c262018-10-22 09:34:31 +02002339 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002340 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2341 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002342 */
2343 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2344 goto missing_data_or_waiting;
2345
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002346 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002347
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002348 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002349 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2350
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002351 /* other states, ENDING...TUNNEL */
2352 if (msg->msg_state >= HTTP_MSG_DONE)
2353 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002354
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002355 if (HAS_RSP_DATA_FILTERS(s)) {
2356 ret = flt_http_end(s, msg);
2357 if (ret <= 0) {
2358 if (!ret)
2359 goto missing_data_or_waiting;
2360 goto return_bad_res;
2361 }
2362 }
2363
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002364 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2365 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2366 msg->msg_state = HTTP_MSG_TUNNEL;
2367 goto ending;
2368 }
2369 else {
2370 msg->msg_state = HTTP_MSG_DONE;
2371 res->to_forward = 0;
2372 }
2373
2374 done:
2375
2376 channel_dont_close(res);
2377
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002378 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002379 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002380 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002381 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2382 if (res->flags & CF_SHUTW) {
2383 /* response errors are most likely due to the
2384 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002385 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002386 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002387 goto return_bad_res;
2388 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002389 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002390 return 1;
2391 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002392 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2393 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002394 return 0;
2395
2396 missing_data_or_waiting:
2397 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002398 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002399
2400 /* stop waiting for data if the input is closed before the end. If the
2401 * client side was already closed, it means that the client has aborted,
2402 * so we don't want to count this as a server abort. Otherwise it's a
2403 * server abort.
2404 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002405 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002406 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002407 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002408 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002409 if (htx_is_empty(htx))
2410 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002411 }
2412
Christopher Faulete0768eb2018-10-03 16:38:02 +02002413 /* When TE: chunked is used, we need to get there again to parse
2414 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002415 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2416 * are filters registered on the stream, we don't want to forward a
2417 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002418 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002419 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002420 channel_dont_close(res);
2421
2422 /* We know that more data are expected, but we couldn't send more that
2423 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2424 * system knows it must not set a PUSH on this first part. Interactive
2425 * modes are already handled by the stream sock layer. We must not do
2426 * this in content-length mode because it could present the MSG_MORE
2427 * flag with the last block of forwarded data, which would cause an
2428 * additional delay to be observed by the receiver.
2429 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002430 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002431 res->flags |= CF_EXPECT_MORE;
2432
2433 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002434 DBG_TRACE_DEVEL("waiting for more data to forward",
2435 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002436 return 0;
2437
Christopher Faulet93e02d82019-03-08 14:18:50 +01002438 return_srv_abort:
2439 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2440 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
William Lallemand656730d2021-03-08 15:26:48 +01002441 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01002442 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002443 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002444 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002445 if (!(s->flags & SF_ERR_MASK))
2446 s->flags |= SF_ERR_SRVCL;
2447 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002448
Christopher Faulet93e02d82019-03-08 14:18:50 +01002449 return_cli_abort:
2450 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2451 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand656730d2021-03-08 15:26:48 +01002452 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01002453 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002454 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002455 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002456 if (!(s->flags & SF_ERR_MASK))
2457 s->flags |= SF_ERR_CLICL;
2458 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002459
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002460 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002461 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002462 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
William Lallemand656730d2021-03-08 15:26:48 +01002463 if (sess->listener && sess->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01002464 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002465 if (objt_server(s->target))
2466 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002467 if (!(s->flags & SF_ERR_MASK))
2468 s->flags |= SF_ERR_INTERNAL;
2469 goto return_error;
2470
Christopher Faulet93e02d82019-03-08 14:18:50 +01002471 return_bad_res:
2472 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2473 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002474 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002475 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2476 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002477 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002478 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002479 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002480
Christopher Faulet93e02d82019-03-08 14:18:50 +01002481 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002482 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002483 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002484 res->analysers &= AN_RES_FLT_END;
2485 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 +02002486 if (!(s->flags & SF_FINST_MASK))
2487 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002488 DBG_TRACE_DEVEL("leaving on error",
2489 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002490 return 0;
2491}
2492
Christopher Fauletf2824e62018-10-01 12:12:37 +02002493/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002494 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002495 * as too large a request to build a valid response.
2496 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002497int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002498{
Christopher Faulet99daf282018-11-28 22:58:13 +01002499 struct channel *req = &s->req;
2500 struct channel *res = &s->res;
2501 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002502 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002503 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002504 struct ist status, reason, location;
2505 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002506 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002507
2508 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002509 if (!chunk) {
2510 if (!(s->flags & SF_ERR_MASK))
2511 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002512 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002513 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002514
Christopher Faulet99daf282018-11-28 22:58:13 +01002515 /*
2516 * Create the location
2517 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002518 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002519 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002520 case REDIRECT_TYPE_SCHEME: {
2521 struct http_hdr_ctx ctx;
2522 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002523
Christopher Faulet99daf282018-11-28 22:58:13 +01002524 host = ist("");
2525 ctx.blk = NULL;
2526 if (http_find_header(htx, ist("Host"), &ctx, 0))
2527 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002528
Christopher Faulet297fbb42019-05-13 14:41:27 +02002529 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002530 path = http_get_path(htx_sl_req_uri(sl));
2531 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002532 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002533 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2534 int qs = 0;
2535 while (qs < path.len) {
2536 if (*(path.ptr + qs) == '?') {
2537 path.len = qs;
2538 break;
2539 }
2540 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002541 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002542 }
2543 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002544 else
2545 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002546
Christopher Faulet99daf282018-11-28 22:58:13 +01002547 if (rule->rdr_str) { /* this is an old "redirect" rule */
2548 /* add scheme */
2549 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2550 goto fail;
2551 }
2552 else {
2553 /* add scheme with executing log format */
2554 chunk->data += build_logline(s, chunk->area + chunk->data,
2555 chunk->size - chunk->data,
2556 &rule->rdr_fmt);
2557 }
2558 /* add "://" + host + path */
2559 if (!chunk_memcat(chunk, "://", 3) ||
2560 !chunk_memcat(chunk, host.ptr, host.len) ||
2561 !chunk_memcat(chunk, path.ptr, path.len))
2562 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002563
Christopher Faulet99daf282018-11-28 22:58:13 +01002564 /* append a slash at the end of the location if needed and missing */
2565 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2566 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2567 if (chunk->data + 1 >= chunk->size)
2568 goto fail;
2569 chunk->area[chunk->data++] = '/';
2570 }
2571 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002572 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002573
Christopher Faulet99daf282018-11-28 22:58:13 +01002574 case REDIRECT_TYPE_PREFIX: {
2575 struct ist path;
2576
Christopher Faulet297fbb42019-05-13 14:41:27 +02002577 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002578 path = http_get_path(htx_sl_req_uri(sl));
2579 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002580 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002581 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2582 int qs = 0;
2583 while (qs < path.len) {
2584 if (*(path.ptr + qs) == '?') {
2585 path.len = qs;
2586 break;
2587 }
2588 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002589 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002590 }
2591 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002592 else
2593 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002594
Christopher Faulet99daf282018-11-28 22:58:13 +01002595 if (rule->rdr_str) { /* this is an old "redirect" rule */
2596 /* add prefix. Note that if prefix == "/", we don't want to
2597 * add anything, otherwise it makes it hard for the user to
2598 * configure a self-redirection.
2599 */
2600 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2601 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2602 goto fail;
2603 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002604 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002605 else {
2606 /* add prefix with executing log format */
2607 chunk->data += build_logline(s, chunk->area + chunk->data,
2608 chunk->size - chunk->data,
2609 &rule->rdr_fmt);
2610 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002611
Christopher Faulet99daf282018-11-28 22:58:13 +01002612 /* add path */
2613 if (!chunk_memcat(chunk, path.ptr, path.len))
2614 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002615
Christopher Faulet99daf282018-11-28 22:58:13 +01002616 /* append a slash at the end of the location if needed and missing */
2617 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2618 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2619 if (chunk->data + 1 >= chunk->size)
2620 goto fail;
2621 chunk->area[chunk->data++] = '/';
2622 }
2623 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002624 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002625 case REDIRECT_TYPE_LOCATION:
2626 default:
2627 if (rule->rdr_str) { /* this is an old "redirect" rule */
2628 /* add location */
2629 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2630 goto fail;
2631 }
2632 else {
2633 /* add location with executing log format */
2634 chunk->data += build_logline(s, chunk->area + chunk->data,
2635 chunk->size - chunk->data,
2636 &rule->rdr_fmt);
2637 }
2638 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002639 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002640 location = ist2(chunk->area, chunk->data);
2641
2642 /*
2643 * Create the 30x response
2644 */
2645 switch (rule->code) {
2646 case 308:
2647 status = ist("308");
2648 reason = ist("Permanent Redirect");
2649 break;
2650 case 307:
2651 status = ist("307");
2652 reason = ist("Temporary Redirect");
2653 break;
2654 case 303:
2655 status = ist("303");
2656 reason = ist("See Other");
2657 break;
2658 case 301:
2659 status = ist("301");
2660 reason = ist("Moved Permanently");
2661 break;
2662 case 302:
2663 default:
2664 status = ist("302");
2665 reason = ist("Found");
2666 break;
2667 }
2668
Christopher Faulet08e66462019-05-23 16:44:59 +02002669 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2670 close = 1;
2671
Christopher Faulet99daf282018-11-28 22:58:13 +01002672 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002673 /* Trim any possible response */
2674 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002675 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2676 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2677 if (!sl)
2678 goto fail;
2679 sl->info.res.status = rule->code;
2680 s->txn->status = rule->code;
2681
Christopher Faulet08e66462019-05-23 16:44:59 +02002682 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2683 goto fail;
2684
2685 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002686 !htx_add_header(htx, ist("Location"), location))
2687 goto fail;
2688
2689 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2690 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2691 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002692 }
2693
2694 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002695 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2696 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002697 }
2698
Christopher Faulet99daf282018-11-28 22:58:13 +01002699 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2700 goto fail;
2701
Kevin Zhu96b36392020-01-07 09:42:55 +01002702 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002703 if (!http_forward_proxy_resp(s, 1))
2704 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002705
Christopher Faulet60b33a52020-01-28 09:18:10 +01002706 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2707 /* let's log the request time */
2708 s->logs.tv_request = now;
2709 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002710
Christopher Faulet60b33a52020-01-28 09:18:10 +01002711 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2712 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2713 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002714
2715 if (!(s->flags & SF_ERR_MASK))
2716 s->flags |= SF_ERR_LOCAL;
2717 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002718 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002719
Christopher Faulet99daf282018-11-28 22:58:13 +01002720 free_trash_chunk(chunk);
2721 return 1;
2722
2723 fail:
2724 /* If an error occurred, remove the incomplete HTTP response from the
2725 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002726 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002727 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002728 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002729}
2730
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002731/* Replace all headers matching the name <name>. The header value is replaced if
2732 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2733 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2734 * values are evaluated one by one. It returns 0 on success and -1 on error.
2735 */
2736int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2737 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002738{
2739 struct http_hdr_ctx ctx;
2740 struct buffer *output = get_trash_chunk();
2741
Christopher Faulet72333522018-10-24 11:25:02 +02002742 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002743 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002744 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2745 continue;
2746
2747 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2748 if (output->data == -1)
2749 return -1;
2750 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2751 return -1;
2752 }
2753 return 0;
2754}
2755
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002756/* This function executes one of the set-{method,path,query,uri} actions. It
2757 * takes the string from the variable 'replace' with length 'len', then modifies
2758 * the relevant part of the request line accordingly. Then it updates various
2759 * pointers to the next elements which were moved, and the total buffer length.
2760 * It finds the action to be performed in p[2], previously filled by function
2761 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2762 * error, though this can be revisited when this code is finally exploited.
2763 *
2764 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002765 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002766 *
2767 * In query string case, the mark question '?' must be set at the start of the
2768 * string by the caller, event if the replacement query string is empty.
2769 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002770int http_req_replace_stline(int action, const char *replace, int len,
2771 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002772{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002773 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002774
2775 switch (action) {
2776 case 0: // method
2777 if (!http_replace_req_meth(htx, ist2(replace, len)))
2778 return -1;
2779 break;
2780
2781 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002782 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002783 return -1;
2784 break;
2785
2786 case 2: // query
2787 if (!http_replace_req_query(htx, ist2(replace, len)))
2788 return -1;
2789 break;
2790
2791 case 3: // uri
2792 if (!http_replace_req_uri(htx, ist2(replace, len)))
2793 return -1;
2794 break;
2795
Christopher Faulet312294f2020-09-02 17:17:44 +02002796 case 4: // path + query
2797 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2798 return -1;
2799 break;
2800
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002801 default:
2802 return -1;
2803 }
2804 return 0;
2805}
2806
2807/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002808 * variable <status> contains the new status code. This function never fails. It
2809 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002810 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002811int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002812{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002813 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002814 char *res;
2815
2816 chunk_reset(&trash);
2817 res = ultoa_o(status, trash.area, trash.size);
2818 trash.data = res - trash.area;
2819
2820 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002821 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002822 const char *str = http_get_reason(status);
2823 reason = ist2(str, strlen(str));
2824 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002825
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002826 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002827 return -1;
2828 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002829}
2830
Christopher Faulet3e964192018-10-24 11:39:23 +02002831/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2832 * transaction <txn>. Returns the verdict of the first rule that prevents
2833 * further processing of the request (auth, deny, ...), and defaults to
2834 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2835 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2836 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2837 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2838 * status.
2839 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002840static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002841 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002842{
2843 struct session *sess = strm_sess(s);
2844 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002845 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002846 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002847 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002848
Christopher Faulet3e964192018-10-24 11:39:23 +02002849 /* If "the current_rule_list" match the executed rule list, we are in
2850 * resume condition. If a resume is needed it is always in the action
2851 * and never in the ACL or converters. In this case, we initialise the
2852 * current rule, and go to the action execution point.
2853 */
2854 if (s->current_rule) {
2855 rule = s->current_rule;
2856 s->current_rule = NULL;
2857 if (s->current_rule_list == rules)
2858 goto resume_execution;
2859 }
2860 s->current_rule_list = rules;
2861
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002862 /* start the ruleset evaluation in strict mode */
2863 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002864
Christopher Faulet3e964192018-10-24 11:39:23 +02002865 list_for_each_entry(rule, rules, list) {
2866 /* check optional condition */
2867 if (rule->cond) {
2868 int ret;
2869
2870 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2871 ret = acl_pass(ret);
2872
2873 if (rule->cond->pol == ACL_COND_UNLESS)
2874 ret = !ret;
2875
2876 if (!ret) /* condition not matched */
2877 continue;
2878 }
2879
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002880 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002881 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002882 /* Always call the action function if defined */
2883 if (rule->action_ptr) {
2884 if ((s->req.flags & CF_READ_ERROR) ||
2885 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2886 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002887 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002888
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002889 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002890 case ACT_RET_CONT:
2891 break;
2892 case ACT_RET_STOP:
2893 rule_ret = HTTP_RULE_RES_STOP;
2894 goto end;
2895 case ACT_RET_YIELD:
2896 s->current_rule = rule;
2897 rule_ret = HTTP_RULE_RES_YIELD;
2898 goto end;
2899 case ACT_RET_ERR:
2900 rule_ret = HTTP_RULE_RES_ERROR;
2901 goto end;
2902 case ACT_RET_DONE:
2903 rule_ret = HTTP_RULE_RES_DONE;
2904 goto end;
2905 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002906 if (txn->status == -1)
2907 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002908 rule_ret = HTTP_RULE_RES_DENY;
2909 goto end;
2910 case ACT_RET_ABRT:
2911 rule_ret = HTTP_RULE_RES_ABRT;
2912 goto end;
2913 case ACT_RET_INV:
2914 rule_ret = HTTP_RULE_RES_BADREQ;
2915 goto end;
2916 }
2917 continue; /* eval the next rule */
2918 }
2919
2920 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002921 switch (rule->action) {
2922 case ACT_ACTION_ALLOW:
2923 rule_ret = HTTP_RULE_RES_STOP;
2924 goto end;
2925
2926 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002927 txn->status = rule->arg.http_reply->status;
2928 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002929 rule_ret = HTTP_RULE_RES_DENY;
2930 goto end;
2931
2932 case ACT_HTTP_REQ_TARPIT:
2933 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002934 txn->status = rule->arg.http_reply->status;
2935 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002936 rule_ret = HTTP_RULE_RES_DENY;
2937 goto end;
2938
Christopher Faulet3e964192018-10-24 11:39:23 +02002939 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002940 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002941 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002942 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002943 goto end;
2944
2945 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002946 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002947 break;
2948
2949 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002950 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002951 break;
2952
2953 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002954 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002955 break;
2956
2957 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002958 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002959 break;
2960
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002961 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002962 default:
2963 break;
2964 }
2965 }
2966
2967 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002968 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002969 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002970 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002971
Christopher Faulet3e964192018-10-24 11:39:23 +02002972 /* we reached the end of the rules, nothing to report */
2973 return rule_ret;
2974}
2975
2976/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2977 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2978 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2979 * is returned, the process can continue the evaluation of next rule list. If
2980 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2981 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002982 * must be returned. If *YIELD is returned, the caller must call again the
2983 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002984 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002985static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2986 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002987{
2988 struct session *sess = strm_sess(s);
2989 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002990 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002991 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002992 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002993
Christopher Faulet3e964192018-10-24 11:39:23 +02002994 /* If "the current_rule_list" match the executed rule list, we are in
2995 * resume condition. If a resume is needed it is always in the action
2996 * and never in the ACL or converters. In this case, we initialise the
2997 * current rule, and go to the action execution point.
2998 */
2999 if (s->current_rule) {
3000 rule = s->current_rule;
3001 s->current_rule = NULL;
3002 if (s->current_rule_list == rules)
3003 goto resume_execution;
3004 }
3005 s->current_rule_list = rules;
3006
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003007 /* start the ruleset evaluation in strict mode */
3008 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003009
Christopher Faulet3e964192018-10-24 11:39:23 +02003010 list_for_each_entry(rule, rules, list) {
3011 /* check optional condition */
3012 if (rule->cond) {
3013 int ret;
3014
3015 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3016 ret = acl_pass(ret);
3017
3018 if (rule->cond->pol == ACL_COND_UNLESS)
3019 ret = !ret;
3020
3021 if (!ret) /* condition not matched */
3022 continue;
3023 }
3024
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003025 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02003026resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003027
3028 /* Always call the action function if defined */
3029 if (rule->action_ptr) {
3030 if ((s->req.flags & CF_READ_ERROR) ||
3031 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3032 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003033 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003034
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003035 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003036 case ACT_RET_CONT:
3037 break;
3038 case ACT_RET_STOP:
3039 rule_ret = HTTP_RULE_RES_STOP;
3040 goto end;
3041 case ACT_RET_YIELD:
3042 s->current_rule = rule;
3043 rule_ret = HTTP_RULE_RES_YIELD;
3044 goto end;
3045 case ACT_RET_ERR:
3046 rule_ret = HTTP_RULE_RES_ERROR;
3047 goto end;
3048 case ACT_RET_DONE:
3049 rule_ret = HTTP_RULE_RES_DONE;
3050 goto end;
3051 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01003052 if (txn->status == -1)
3053 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003054 rule_ret = HTTP_RULE_RES_DENY;
3055 goto end;
3056 case ACT_RET_ABRT:
3057 rule_ret = HTTP_RULE_RES_ABRT;
3058 goto end;
3059 case ACT_RET_INV:
3060 rule_ret = HTTP_RULE_RES_BADREQ;
3061 goto end;
3062 }
3063 continue; /* eval the next rule */
3064 }
3065
3066 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003067 switch (rule->action) {
3068 case ACT_ACTION_ALLOW:
3069 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3070 goto end;
3071
3072 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02003073 txn->status = rule->arg.http_reply->status;
3074 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003075 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003076 goto end;
3077
3078 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003079 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003080 break;
3081
3082 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003083 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003084 break;
3085
3086 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003087 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003088 break;
3089
3090 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003091 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003092 break;
3093
Christopher Faulet3e964192018-10-24 11:39:23 +02003094 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003095 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003096 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003097 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003098 goto end;
3099
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003100 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003101 default:
3102 break;
3103 }
3104 }
3105
3106 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003107 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003108 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003109 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003110
Christopher Faulet3e964192018-10-24 11:39:23 +02003111 /* we reached the end of the rules, nothing to report */
3112 return rule_ret;
3113}
3114
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003115/* Executes backend and frontend http-after-response rules for the stream <s>,
3116 * in that order. it return 1 on success and 0 on error. It is the caller
3117 * responsibility to catch error or ignore it. If it catches it, this function
3118 * may be called a second time, for the internal error.
3119 */
3120int http_eval_after_res_rules(struct stream *s)
3121{
3122 struct session *sess = s->sess;
3123 enum rule_result ret = HTTP_RULE_RES_CONT;
3124
Christopher Faulet507479b2020-05-15 12:29:46 +02003125 /* Eval after-response ruleset only if the reply is not const */
3126 if (s->txn->flags & TX_CONST_REPLY)
3127 goto end;
3128
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003129 /* prune the request variables if not already done and swap to the response variables. */
3130 if (s->vars_reqres.scope != SCOPE_RES) {
3131 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3132 vars_prune(&s->vars_reqres, s->sess, s);
3133 vars_init(&s->vars_reqres, SCOPE_RES);
3134 }
3135
3136 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3137 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3138 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3139
Christopher Faulet507479b2020-05-15 12:29:46 +02003140 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003141 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3142 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3143}
3144
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003145/*
3146 * Manage client-side cookie. It can impact performance by about 2% so it is
3147 * desirable to call it only when needed. This code is quite complex because
3148 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3149 * highly recommended not to touch this part without a good reason !
3150 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003151static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003152{
3153 struct session *sess = s->sess;
3154 struct http_txn *txn = s->txn;
3155 struct htx *htx;
3156 struct http_hdr_ctx ctx;
3157 char *hdr_beg, *hdr_end, *del_from;
3158 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3159 int preserve_hdr;
3160
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003161 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003162 ctx.blk = NULL;
3163 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003164 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003165 del_from = NULL; /* nothing to be deleted */
3166 preserve_hdr = 0; /* assume we may kill the whole header */
3167
3168 /* Now look for cookies. Conforming to RFC2109, we have to support
3169 * attributes whose name begin with a '$', and associate them with
3170 * the right cookie, if we want to delete this cookie.
3171 * So there are 3 cases for each cookie read :
3172 * 1) it's a special attribute, beginning with a '$' : ignore it.
3173 * 2) it's a server id cookie that we *MAY* want to delete : save
3174 * some pointers on it (last semi-colon, beginning of cookie...)
3175 * 3) it's an application cookie : we *MAY* have to delete a previous
3176 * "special" cookie.
3177 * At the end of loop, if a "special" cookie remains, we may have to
3178 * remove it. If no application cookie persists in the header, we
3179 * *MUST* delete it.
3180 *
3181 * Note: RFC2965 is unclear about the processing of spaces around
3182 * the equal sign in the ATTR=VALUE form. A careful inspection of
3183 * the RFC explicitly allows spaces before it, and not within the
3184 * tokens (attrs or values). An inspection of RFC2109 allows that
3185 * too but section 10.1.3 lets one think that spaces may be allowed
3186 * after the equal sign too, resulting in some (rare) buggy
3187 * implementations trying to do that. So let's do what servers do.
3188 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3189 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003190 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003191 * causes parsing to become ambiguous. Browsers also allow spaces
3192 * within values even without quotes.
3193 *
3194 * We have to keep multiple pointers in order to support cookie
3195 * removal at the beginning, middle or end of header without
3196 * corrupting the header. All of these headers are valid :
3197 *
3198 * hdr_beg hdr_end
3199 * | |
3200 * v |
3201 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3202 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3203 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3204 * | | | | | | |
3205 * | | | | | | |
3206 * | | | | | | +--> next
3207 * | | | | | +----> val_end
3208 * | | | | +-----------> val_beg
3209 * | | | +--------------> equal
3210 * | | +----------------> att_end
3211 * | +---------------------> att_beg
3212 * +--------------------------> prev
3213 *
3214 */
3215 hdr_beg = ctx.value.ptr;
3216 hdr_end = hdr_beg + ctx.value.len;
3217 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3218 /* Iterate through all cookies on this line */
3219
3220 /* find att_beg */
3221 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003222 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003223 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003224 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003225
3226 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3227 att_beg++;
3228
3229 /* find att_end : this is the first character after the last non
3230 * space before the equal. It may be equal to hdr_end.
3231 */
3232 equal = att_end = att_beg;
3233 while (equal < hdr_end) {
3234 if (*equal == '=' || *equal == ',' || *equal == ';')
3235 break;
3236 if (HTTP_IS_SPHT(*equal++))
3237 continue;
3238 att_end = equal;
3239 }
3240
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003241 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003242 * is between <att_beg> and <equal>, both may be identical.
3243 */
3244 /* look for end of cookie if there is an equal sign */
3245 if (equal < hdr_end && *equal == '=') {
3246 /* look for the beginning of the value */
3247 val_beg = equal + 1;
3248 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3249 val_beg++;
3250
3251 /* find the end of the value, respecting quotes */
3252 next = http_find_cookie_value_end(val_beg, hdr_end);
3253
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003254 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003255 val_end = next;
3256 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3257 val_end--;
3258 }
3259 else
3260 val_beg = val_end = next = equal;
3261
3262 /* We have nothing to do with attributes beginning with
3263 * '$'. However, they will automatically be removed if a
3264 * header before them is removed, since they're supposed
3265 * to be linked together.
3266 */
3267 if (*att_beg == '$')
3268 continue;
3269
3270 /* Ignore cookies with no equal sign */
3271 if (equal == next) {
3272 /* This is not our cookie, so we must preserve it. But if we already
3273 * scheduled another cookie for removal, we cannot remove the
3274 * complete header, but we can remove the previous block itself.
3275 */
3276 preserve_hdr = 1;
3277 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003278 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003279 val_end += delta;
3280 next += delta;
3281 hdr_end += delta;
3282 prev = del_from;
3283 del_from = NULL;
3284 }
3285 continue;
3286 }
3287
3288 /* if there are spaces around the equal sign, we need to
3289 * strip them otherwise we'll get trouble for cookie captures,
3290 * or even for rewrites. Since this happens extremely rarely,
3291 * it does not hurt performance.
3292 */
3293 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3294 int stripped_before = 0;
3295 int stripped_after = 0;
3296
3297 if (att_end != equal) {
3298 memmove(att_end, equal, hdr_end - equal);
3299 stripped_before = (att_end - equal);
3300 equal += stripped_before;
3301 val_beg += stripped_before;
3302 }
3303
3304 if (val_beg > equal + 1) {
3305 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3306 stripped_after = (equal + 1) - val_beg;
3307 val_beg += stripped_after;
3308 stripped_before += stripped_after;
3309 }
3310
3311 val_end += stripped_before;
3312 next += stripped_before;
3313 hdr_end += stripped_before;
3314 }
3315 /* now everything is as on the diagram above */
3316
3317 /* First, let's see if we want to capture this cookie. We check
3318 * that we don't already have a client side cookie, because we
3319 * can only capture one. Also as an optimisation, we ignore
3320 * cookies shorter than the declared name.
3321 */
3322 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3323 (val_end - att_beg >= sess->fe->capture_namelen) &&
3324 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3325 int log_len = val_end - att_beg;
3326
3327 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3328 ha_alert("HTTP logging : out of memory.\n");
3329 } else {
3330 if (log_len > sess->fe->capture_len)
3331 log_len = sess->fe->capture_len;
3332 memcpy(txn->cli_cookie, att_beg, log_len);
3333 txn->cli_cookie[log_len] = 0;
3334 }
3335 }
3336
3337 /* Persistence cookies in passive, rewrite or insert mode have the
3338 * following form :
3339 *
3340 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3341 *
3342 * For cookies in prefix mode, the form is :
3343 *
3344 * Cookie: NAME=SRV~VALUE
3345 */
3346 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3347 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3348 struct server *srv = s->be->srv;
3349 char *delim;
3350
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003351 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003352 * have the server ID between val_beg and delim, and the original cookie between
3353 * delim+1 and val_end. Otherwise, delim==val_end :
3354 *
3355 * hdr_beg
3356 * |
3357 * v
3358 * NAME=SRV; # in all but prefix modes
3359 * NAME=SRV~OPAQUE ; # in prefix mode
3360 * || || | |+-> next
3361 * || || | +--> val_end
3362 * || || +---------> delim
3363 * || |+------------> val_beg
3364 * || +-------------> att_end = equal
3365 * |+-----------------> att_beg
3366 * +------------------> prev
3367 *
3368 */
3369 if (s->be->ck_opts & PR_CK_PFX) {
3370 for (delim = val_beg; delim < val_end; delim++)
3371 if (*delim == COOKIE_DELIM)
3372 break;
3373 }
3374 else {
3375 char *vbar1;
3376 delim = val_end;
3377 /* Now check if the cookie contains a date field, which would
3378 * appear after a vertical bar ('|') just after the server name
3379 * and before the delimiter.
3380 */
3381 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3382 if (vbar1) {
3383 /* OK, so left of the bar is the server's cookie and
3384 * right is the last seen date. It is a base64 encoded
3385 * 30-bit value representing the UNIX date since the
3386 * epoch in 4-second quantities.
3387 */
3388 int val;
3389 delim = vbar1++;
3390 if (val_end - vbar1 >= 5) {
3391 val = b64tos30(vbar1);
3392 if (val > 0)
3393 txn->cookie_last_date = val << 2;
3394 }
3395 /* look for a second vertical bar */
3396 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3397 if (vbar1 && (val_end - vbar1 > 5)) {
3398 val = b64tos30(vbar1 + 1);
3399 if (val > 0)
3400 txn->cookie_first_date = val << 2;
3401 }
3402 }
3403 }
3404
3405 /* if the cookie has an expiration date and the proxy wants to check
3406 * it, then we do that now. We first check if the cookie is too old,
3407 * then only if it has expired. We detect strict overflow because the
3408 * time resolution here is not great (4 seconds). Cookies with dates
3409 * in the future are ignored if their offset is beyond one day. This
3410 * allows an admin to fix timezone issues without expiring everyone
3411 * and at the same time avoids keeping unwanted side effects for too
3412 * long.
3413 */
3414 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3415 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3416 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3417 txn->flags &= ~TX_CK_MASK;
3418 txn->flags |= TX_CK_OLD;
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 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3424 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3425 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3426 txn->flags &= ~TX_CK_MASK;
3427 txn->flags |= TX_CK_EXPIRED;
3428 delim = val_beg; // let's pretend we have not found the cookie
3429 txn->cookie_first_date = 0;
3430 txn->cookie_last_date = 0;
3431 }
3432
3433 /* Here, we'll look for the first running server which supports the cookie.
3434 * This allows to share a same cookie between several servers, for example
3435 * to dedicate backup servers to specific servers only.
3436 * However, to prevent clients from sticking to cookie-less backup server
3437 * when they have incidentely learned an empty cookie, we simply ignore
3438 * empty cookies and mark them as invalid.
3439 * The same behaviour is applied when persistence must be ignored.
3440 */
3441 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3442 srv = NULL;
3443
3444 while (srv) {
3445 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3446 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3447 if ((srv->cur_state != SRV_ST_STOPPED) ||
3448 (s->be->options & PR_O_PERSIST) ||
3449 (s->flags & SF_FORCE_PRST)) {
3450 /* we found the server and we can use it */
3451 txn->flags &= ~TX_CK_MASK;
3452 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3453 s->flags |= SF_DIRECT | SF_ASSIGNED;
3454 s->target = &srv->obj_type;
3455 break;
3456 } else {
3457 /* we found a server, but it's down,
3458 * mark it as such and go on in case
3459 * another one is available.
3460 */
3461 txn->flags &= ~TX_CK_MASK;
3462 txn->flags |= TX_CK_DOWN;
3463 }
3464 }
3465 srv = srv->next;
3466 }
3467
3468 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3469 /* no server matched this cookie or we deliberately skipped it */
3470 txn->flags &= ~TX_CK_MASK;
3471 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3472 txn->flags |= TX_CK_UNUSED;
3473 else
3474 txn->flags |= TX_CK_INVALID;
3475 }
3476
3477 /* depending on the cookie mode, we may have to either :
3478 * - delete the complete cookie if we're in insert+indirect mode, so that
3479 * the server never sees it ;
3480 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003481 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003482 * if we're in cookie prefix mode
3483 */
3484 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3485 int delta; /* negative */
3486
3487 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3488 delta = val_beg - (delim + 1);
3489 val_end += delta;
3490 next += delta;
3491 hdr_end += delta;
3492 del_from = NULL;
3493 preserve_hdr = 1; /* we want to keep this cookie */
3494 }
3495 else if (del_from == NULL &&
3496 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3497 del_from = prev;
3498 }
3499 }
3500 else {
3501 /* This is not our cookie, so we must preserve it. But if we already
3502 * scheduled another cookie for removal, we cannot remove the
3503 * complete header, but we can remove the previous block itself.
3504 */
3505 preserve_hdr = 1;
3506
3507 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003508 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003509 if (att_beg >= del_from)
3510 att_beg += delta;
3511 if (att_end >= del_from)
3512 att_end += delta;
3513 val_beg += delta;
3514 val_end += delta;
3515 next += delta;
3516 hdr_end += delta;
3517 prev = del_from;
3518 del_from = NULL;
3519 }
3520 }
3521
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003522 } /* for each cookie */
3523
3524
3525 /* There are no more cookies on this line.
3526 * We may still have one (or several) marked for deletion at the
3527 * end of the line. We must do this now in two ways :
3528 * - if some cookies must be preserved, we only delete from the
3529 * mark to the end of line ;
3530 * - if nothing needs to be preserved, simply delete the whole header
3531 */
3532 if (del_from) {
3533 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3534 }
3535 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003536 if (hdr_beg != hdr_end)
3537 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003538 else
3539 http_remove_header(htx, &ctx);
3540 }
3541 } /* for each "Cookie header */
3542}
3543
3544/*
3545 * Manage server-side cookies. It can impact performance by about 2% so it is
3546 * desirable to call it only when needed. This function is also used when we
3547 * just need to know if there is a cookie (eg: for check-cache).
3548 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003549static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003550{
3551 struct session *sess = s->sess;
3552 struct http_txn *txn = s->txn;
3553 struct htx *htx;
3554 struct http_hdr_ctx ctx;
3555 struct server *srv;
3556 char *hdr_beg, *hdr_end;
3557 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003558 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003559
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003560 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003561
3562 ctx.blk = NULL;
3563 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003564 int is_first = 1;
3565
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003566 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3567 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3568 break;
3569 is_cookie2 = 1;
3570 }
3571
3572 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3573 * <prev> points to the colon.
3574 */
3575 txn->flags |= TX_SCK_PRESENT;
3576
3577 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3578 * check-cache is enabled) and we are not interested in checking
3579 * them. Warning, the cookie capture is declared in the frontend.
3580 */
3581 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3582 break;
3583
3584 /* OK so now we know we have to process this response cookie.
3585 * The format of the Set-Cookie header is slightly different
3586 * from the format of the Cookie header in that it does not
3587 * support the comma as a cookie delimiter (thus the header
3588 * cannot be folded) because the Expires attribute described in
3589 * the original Netscape's spec may contain an unquoted date
3590 * with a comma inside. We have to live with this because
3591 * many browsers don't support Max-Age and some browsers don't
3592 * support quoted strings. However the Set-Cookie2 header is
3593 * clean.
3594 *
3595 * We have to keep multiple pointers in order to support cookie
3596 * removal at the beginning, middle or end of header without
3597 * corrupting the header (in case of set-cookie2). A special
3598 * pointer, <scav> points to the beginning of the set-cookie-av
3599 * fields after the first semi-colon. The <next> pointer points
3600 * either to the end of line (set-cookie) or next unquoted comma
3601 * (set-cookie2). All of these headers are valid :
3602 *
3603 * hdr_beg hdr_end
3604 * | |
3605 * v |
3606 * NAME1 = VALUE 1 ; Secure; Path="/" |
3607 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3608 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3609 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3610 * | | | | | | | |
3611 * | | | | | | | +-> next
3612 * | | | | | | +------------> scav
3613 * | | | | | +--------------> val_end
3614 * | | | | +--------------------> val_beg
3615 * | | | +----------------------> equal
3616 * | | +------------------------> att_end
3617 * | +----------------------------> att_beg
3618 * +------------------------------> prev
3619 * -------------------------------> hdr_beg
3620 */
3621 hdr_beg = ctx.value.ptr;
3622 hdr_end = hdr_beg + ctx.value.len;
3623 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3624
3625 /* Iterate through all cookies on this line */
3626
3627 /* find att_beg */
3628 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003629 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003630 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003631 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003632
3633 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3634 att_beg++;
3635
3636 /* find att_end : this is the first character after the last non
3637 * space before the equal. It may be equal to hdr_end.
3638 */
3639 equal = att_end = att_beg;
3640
3641 while (equal < hdr_end) {
3642 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3643 break;
3644 if (HTTP_IS_SPHT(*equal++))
3645 continue;
3646 att_end = equal;
3647 }
3648
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003649 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003650 * is between <att_beg> and <equal>, both may be identical.
3651 */
3652
3653 /* look for end of cookie if there is an equal sign */
3654 if (equal < hdr_end && *equal == '=') {
3655 /* look for the beginning of the value */
3656 val_beg = equal + 1;
3657 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3658 val_beg++;
3659
3660 /* find the end of the value, respecting quotes */
3661 next = http_find_cookie_value_end(val_beg, hdr_end);
3662
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003663 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003664 val_end = next;
3665 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3666 val_end--;
3667 }
3668 else {
3669 /* <equal> points to next comma, semi-colon or EOL */
3670 val_beg = val_end = next = equal;
3671 }
3672
3673 if (next < hdr_end) {
3674 /* Set-Cookie2 supports multiple cookies, and <next> points to
3675 * a colon or semi-colon before the end. So skip all attr-value
3676 * pairs and look for the next comma. For Set-Cookie, since
3677 * commas are permitted in values, skip to the end.
3678 */
3679 if (is_cookie2)
3680 next = http_find_hdr_value_end(next, hdr_end);
3681 else
3682 next = hdr_end;
3683 }
3684
3685 /* Now everything is as on the diagram above */
3686
3687 /* Ignore cookies with no equal sign */
3688 if (equal == val_end)
3689 continue;
3690
3691 /* If there are spaces around the equal sign, we need to
3692 * strip them otherwise we'll get trouble for cookie captures,
3693 * or even for rewrites. Since this happens extremely rarely,
3694 * it does not hurt performance.
3695 */
3696 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3697 int stripped_before = 0;
3698 int stripped_after = 0;
3699
3700 if (att_end != equal) {
3701 memmove(att_end, equal, hdr_end - equal);
3702 stripped_before = (att_end - equal);
3703 equal += stripped_before;
3704 val_beg += stripped_before;
3705 }
3706
3707 if (val_beg > equal + 1) {
3708 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3709 stripped_after = (equal + 1) - val_beg;
3710 val_beg += stripped_after;
3711 stripped_before += stripped_after;
3712 }
3713
3714 val_end += stripped_before;
3715 next += stripped_before;
3716 hdr_end += stripped_before;
3717
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003718 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003719 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003720 }
3721
3722 /* First, let's see if we want to capture this cookie. We check
3723 * that we don't already have a server side cookie, because we
3724 * can only capture one. Also as an optimisation, we ignore
3725 * cookies shorter than the declared name.
3726 */
3727 if (sess->fe->capture_name != NULL &&
3728 txn->srv_cookie == NULL &&
3729 (val_end - att_beg >= sess->fe->capture_namelen) &&
3730 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3731 int log_len = val_end - att_beg;
3732 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3733 ha_alert("HTTP logging : out of memory.\n");
3734 }
3735 else {
3736 if (log_len > sess->fe->capture_len)
3737 log_len = sess->fe->capture_len;
3738 memcpy(txn->srv_cookie, att_beg, log_len);
3739 txn->srv_cookie[log_len] = 0;
3740 }
3741 }
3742
3743 srv = objt_server(s->target);
3744 /* now check if we need to process it for persistence */
3745 if (!(s->flags & SF_IGNORE_PRST) &&
3746 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3747 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3748 /* assume passive cookie by default */
3749 txn->flags &= ~TX_SCK_MASK;
3750 txn->flags |= TX_SCK_FOUND;
3751
3752 /* If the cookie is in insert mode on a known server, we'll delete
3753 * this occurrence because we'll insert another one later.
3754 * We'll delete it too if the "indirect" option is set and we're in
3755 * a direct access.
3756 */
3757 if (s->be->ck_opts & PR_CK_PSV) {
3758 /* The "preserve" flag was set, we don't want to touch the
3759 * server's cookie.
3760 */
3761 }
3762 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3763 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3764 /* this cookie must be deleted */
3765 if (prev == hdr_beg && next == hdr_end) {
3766 /* whole header */
3767 http_remove_header(htx, &ctx);
3768 /* note: while both invalid now, <next> and <hdr_end>
3769 * are still equal, so the for() will stop as expected.
3770 */
3771 } else {
3772 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003773 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003774 next = prev;
3775 hdr_end += delta;
3776 }
3777 txn->flags &= ~TX_SCK_MASK;
3778 txn->flags |= TX_SCK_DELETED;
3779 /* and go on with next cookie */
3780 }
3781 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3782 /* replace bytes val_beg->val_end with the cookie name associated
3783 * with this server since we know it.
3784 */
3785 int sliding, delta;
3786
3787 ctx.value = ist2(val_beg, val_end - val_beg);
3788 ctx.lws_before = ctx.lws_after = 0;
3789 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3790 delta = srv->cklen - (val_end - val_beg);
3791 sliding = (ctx.value.ptr - val_beg);
3792 hdr_beg += sliding;
3793 val_beg += sliding;
3794 next += sliding + delta;
3795 hdr_end += sliding + delta;
3796
3797 txn->flags &= ~TX_SCK_MASK;
3798 txn->flags |= TX_SCK_REPLACED;
3799 }
3800 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3801 /* insert the cookie name associated with this server
3802 * before existing cookie, and insert a delimiter between them..
3803 */
3804 int sliding, delta;
3805 ctx.value = ist2(val_beg, 0);
3806 ctx.lws_before = ctx.lws_after = 0;
3807 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3808 delta = srv->cklen + 1;
3809 sliding = (ctx.value.ptr - val_beg);
3810 hdr_beg += sliding;
3811 val_beg += sliding;
3812 next += sliding + delta;
3813 hdr_end += sliding + delta;
3814
3815 val_beg[srv->cklen] = COOKIE_DELIM;
3816 txn->flags &= ~TX_SCK_MASK;
3817 txn->flags |= TX_SCK_REPLACED;
3818 }
3819 }
3820 /* that's done for this cookie, check the next one on the same
3821 * line when next != hdr_end (only if is_cookie2).
3822 */
3823 }
3824 }
3825}
3826
Christopher Faulet25a02f62018-10-24 12:00:25 +02003827/*
3828 * Parses the Cache-Control and Pragma request header fields to determine if
3829 * the request may be served from the cache and/or if it is cacheable. Updates
3830 * s->txn->flags.
3831 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003832void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003833{
3834 struct http_txn *txn = s->txn;
3835 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003836 struct http_hdr_ctx ctx = { .blk = NULL };
3837 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003838
3839 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3840 return; /* nothing more to do here */
3841
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003842 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003843 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003844
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003845 /* Check "pragma" header for HTTP/1.0 compatibility. */
3846 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3847 if (isteqi(ctx.value, ist("no-cache"))) {
3848 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003849 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003850 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003851
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003852 ctx.blk = NULL;
3853 /* Don't use the cache and don't try to store if we found the
3854 * Authorization header */
3855 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3856 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3857 txn->flags |= TX_CACHE_IGNORE;
3858 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003859
Christopher Faulet25a02f62018-10-24 12:00:25 +02003860
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003861 /* Look for "cache-control" header and iterate over all the values
3862 * until we find one that specifies that caching is possible or not. */
3863 ctx.blk = NULL;
3864 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003865 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003866 /* We don't check the values after max-age, max-stale nor min-fresh,
3867 * we simply don't use the cache when they're specified. */
3868 if (istmatchi(ctx.value, ist("max-age")) ||
3869 istmatchi(ctx.value, ist("no-cache")) ||
3870 istmatchi(ctx.value, ist("max-stale")) ||
3871 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003872 txn->flags |= TX_CACHE_IGNORE;
3873 continue;
3874 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003875 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003876 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3877 continue;
3878 }
3879 }
3880
3881 /* RFC7234#5.4:
3882 * When the Cache-Control header field is also present and
3883 * understood in a request, Pragma is ignored.
3884 * When the Cache-Control header field is not present in a
3885 * request, caches MUST consider the no-cache request
3886 * pragma-directive as having the same effect as if
3887 * "Cache-Control: no-cache" were present.
3888 */
3889 if (!cc_found && pragma_found)
3890 txn->flags |= TX_CACHE_IGNORE;
3891}
3892
3893/*
3894 * Check if response is cacheable or not. Updates s->txn->flags.
3895 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003896void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003897{
3898 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003899 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003900 struct htx *htx;
Remi Tricot-Le Breton87c2f252020-11-12 11:14:41 +01003901 int has_freshness_info = 0;
3902 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003903
3904 if (txn->status < 200) {
3905 /* do not try to cache interim responses! */
3906 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3907 return;
3908 }
3909
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003910 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003911 /* Check "pragma" header for HTTP/1.0 compatibility. */
3912 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3913 if (isteqi(ctx.value, ist("no-cache"))) {
3914 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3915 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003916 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003917 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003918
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003919 /* Look for "cache-control" header and iterate over all the values
3920 * until we find one that specifies that caching is possible or not. */
3921 ctx.blk = NULL;
3922 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3923 if (isteqi(ctx.value, ist("public"))) {
3924 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003925 continue;
3926 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003927 if (isteqi(ctx.value, ist("private")) ||
3928 isteqi(ctx.value, ist("no-cache")) ||
3929 isteqi(ctx.value, ist("no-store")) ||
3930 isteqi(ctx.value, ist("max-age=0")) ||
3931 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003932 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003933 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003934 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003935 /* We might have a no-cache="set-cookie" form. */
3936 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3937 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003938 continue;
3939 }
Remi Tricot-Le Breton87c2f252020-11-12 11:14:41 +01003940
3941 if (istmatchi(ctx.value, ist("s-maxage")) ||
3942 istmatchi(ctx.value, ist("max-age"))) {
3943 has_freshness_info = 1;
3944 continue;
3945 }
3946 }
3947
3948 /* If no freshness information could be found in Cache-Control values,
3949 * look for an Expires header. */
3950 if (!has_freshness_info) {
3951 ctx.blk = NULL;
3952 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003953 }
Remi Tricot-Le Breton87c2f252020-11-12 11:14:41 +01003954
3955 /* If no freshness information could be found in Cache-Control or Expires
3956 * values, look for an explicit validator. */
3957 if (!has_freshness_info) {
3958 ctx.blk = NULL;
3959 has_validator = 1;
3960 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3961 ctx.blk = NULL;
3962 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3963 has_validator = 0;
3964 }
3965 }
3966
3967 /* We won't store an entry that has neither a cache validator nor an
3968 * explicit expiration time, as suggested in RFC 7234#3. */
3969 if (!has_freshness_info && !has_validator)
3970 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003971}
3972
Christopher Faulet377c5a52018-10-24 21:21:30 +02003973/*
3974 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3975 * for the current backend.
3976 *
3977 * It is assumed that the request is either a HEAD, GET, or POST and that the
3978 * uri_auth field is valid.
3979 *
3980 * Returns 1 if stats should be provided, otherwise 0.
3981 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003982static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003983{
3984 struct uri_auth *uri_auth = backend->uri_auth;
3985 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003986 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003987 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003988
3989 if (!uri_auth)
3990 return 0;
3991
3992 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3993 return 0;
3994
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003995 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003996 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003997 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003998 if (*uri_auth->uri_prefix == '/')
3999 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004000
4001 /* check URI size */
4002 if (uri_auth->uri_len > uri.len)
4003 return 0;
4004
4005 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4006 return 0;
4007
4008 return 1;
4009}
4010
4011/* This function prepares an applet to handle the stats. It can deal with the
4012 * "100-continue" expectation, check that admin rules are met for POST requests,
4013 * and program a response message if something was unexpected. It cannot fail
4014 * and always relies on the stats applet to complete the job. It does not touch
4015 * analysers nor counters, which are left to the caller. It does not touch
4016 * s->target which is supposed to already point to the stats applet. The caller
4017 * is expected to have already assigned an appctx to the stream.
4018 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004019static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004020{
4021 struct stats_admin_rule *stats_admin_rule;
4022 struct stream_interface *si = &s->si[1];
4023 struct session *sess = s->sess;
4024 struct http_txn *txn = s->txn;
4025 struct http_msg *msg = &txn->req;
4026 struct uri_auth *uri_auth = s->be->uri_auth;
4027 const char *h, *lookup, *end;
4028 struct appctx *appctx;
4029 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004030 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004031
4032 appctx = si_appctx(si);
4033 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4034 appctx->st1 = appctx->st2 = 0;
4035 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004036 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004037 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4038 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4039 appctx->ctx.stats.flags |= STAT_CHUNKED;
4040
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004041 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004042 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004043 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4044 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004045
4046 for (h = lookup; h <= end - 3; h++) {
4047 if (memcmp(h, ";up", 3) == 0) {
4048 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4049 break;
4050 }
Amaury Denoyellee78aa872021-02-25 14:46:08 +01004051 }
4052
4053 for (h = lookup; h <= end - 9; h++) {
4054 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02004055 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
4056 break;
4057 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004058 }
4059
4060 if (uri_auth->refresh) {
4061 for (h = lookup; h <= end - 10; h++) {
4062 if (memcmp(h, ";norefresh", 10) == 0) {
4063 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4064 break;
4065 }
4066 }
4067 }
4068
4069 for (h = lookup; h <= end - 4; h++) {
4070 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004071 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004072 break;
4073 }
4074 }
4075
4076 for (h = lookup; h <= end - 6; h++) {
4077 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004078 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004079 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4080 break;
4081 }
4082 }
4083
Christopher Faulet6338a082019-09-09 15:50:54 +02004084 for (h = lookup; h <= end - 5; h++) {
4085 if (memcmp(h, ";json", 5) == 0) {
4086 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4087 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4088 break;
4089 }
4090 }
4091
4092 for (h = lookup; h <= end - 12; h++) {
4093 if (memcmp(h, ";json-schema", 12) == 0) {
4094 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4095 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4096 break;
4097 }
4098 }
4099
Christopher Faulet377c5a52018-10-24 21:21:30 +02004100 for (h = lookup; h <= end - 8; h++) {
4101 if (memcmp(h, ";st=", 4) == 0) {
4102 int i;
4103 h += 4;
4104 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4105 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4106 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4107 appctx->ctx.stats.st_code = i;
4108 break;
4109 }
4110 }
4111 break;
4112 }
4113 }
4114
4115 appctx->ctx.stats.scope_str = 0;
4116 appctx->ctx.stats.scope_len = 0;
4117 for (h = lookup; h <= end - 8; h++) {
4118 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4119 int itx = 0;
4120 const char *h2;
4121 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4122 const char *err;
4123
4124 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4125 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004126 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4127 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004128 if (*h == ';' || *h == '&' || *h == ' ')
4129 break;
4130 itx++;
4131 h++;
4132 }
4133
4134 if (itx > STAT_SCOPE_TXT_MAXLEN)
4135 itx = STAT_SCOPE_TXT_MAXLEN;
4136 appctx->ctx.stats.scope_len = itx;
4137
4138 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4139 memcpy(scope_txt, h2, itx);
4140 scope_txt[itx] = '\0';
4141 err = invalid_char(scope_txt);
4142 if (err) {
4143 /* bad char in search text => clear scope */
4144 appctx->ctx.stats.scope_str = 0;
4145 appctx->ctx.stats.scope_len = 0;
4146 }
4147 break;
4148 }
4149 }
4150
4151 /* now check whether we have some admin rules for this request */
4152 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4153 int ret = 1;
4154
4155 if (stats_admin_rule->cond) {
4156 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4157 ret = acl_pass(ret);
4158 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4159 ret = !ret;
4160 }
4161
4162 if (ret) {
4163 /* no rule, or the rule matches */
4164 appctx->ctx.stats.flags |= STAT_ADMIN;
4165 break;
4166 }
4167 }
4168
Christopher Faulet5d45e382019-02-27 15:15:23 +01004169 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4170 appctx->st0 = STAT_HTTP_HEAD;
4171 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004172 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004173 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004174 if (msg->msg_state < HTTP_MSG_DATA)
4175 req->analysers |= AN_REQ_HTTP_BODY;
4176 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004177 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004178 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004179 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4180 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4181 appctx->st0 = STAT_HTTP_LAST;
4182 }
4183 }
4184 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004185 /* Unsupported method */
4186 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4187 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4188 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004189 }
4190
4191 s->task->nice = -32; /* small boost for HTTP statistics */
4192 return 1;
4193}
4194
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004195void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004196{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004197 struct channel *req = &s->req;
4198 struct channel *res = &s->res;
4199 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004200 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004201 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004202 struct ist path, location;
4203 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004204
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004205 /*
4206 * Create the location
4207 */
4208 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004209
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004210 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004211 /* special prefix "/" means don't change URL */
4212 srv = __objt_server(s->target);
4213 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4214 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4215 return;
4216 }
4217
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004218 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004219 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004220 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004221 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004222 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004223 return;
4224
4225 if (!chunk_memcat(&trash, path.ptr, path.len))
4226 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004227 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004228
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004229 /*
4230 * Create the 302 respone
4231 */
4232 htx = htx_from_buf(&res->buf);
4233 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4234 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4235 ist("HTTP/1.1"), ist("302"), ist("Found"));
4236 if (!sl)
4237 goto fail;
4238 sl->info.res.status = 302;
4239 s->txn->status = 302;
4240
4241 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4242 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4243 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4244 !htx_add_header(htx, ist("Location"), location))
4245 goto fail;
4246
4247 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4248 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004249
Christopher Fauletc20afb82020-01-24 19:16:26 +01004250 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004251 if (!http_forward_proxy_resp(s, 1))
4252 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004253
4254 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004255 si_shutr(si);
4256 si_shutw(si);
4257 si->err_type = SI_ET_NONE;
4258 si->state = SI_ST_CLO;
4259
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004260 if (!(s->flags & SF_ERR_MASK))
4261 s->flags |= SF_ERR_LOCAL;
4262 if (!(s->flags & SF_FINST_MASK))
4263 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004264
4265 /* FIXME: we should increase a counter of redirects per server and per backend. */
4266 srv_inc_sess_ctr(srv);
4267 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004268 return;
4269
4270 fail:
4271 /* If an error occurred, remove the incomplete HTTP response from the
4272 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004273 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004274}
4275
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004276/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004277 * because an error was triggered during the body forwarding.
4278 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004279static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004280{
4281 struct channel *chn = &s->req;
4282 struct http_txn *txn = s->txn;
4283
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004284 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004285
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004286 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4287 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004288 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004289 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004290 goto end;
4291 }
4292
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004293 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4294 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004295 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004296 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004297
4298 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004299 /* No need to read anymore, the request was completely parsed.
4300 * We can shut the read side unless we want to abort_on_close,
4301 * or we have a POST request. The issue with POST requests is
4302 * that some browsers still send a CRLF after the request, and
4303 * this CRLF must be read so that it does not remain in the kernel
4304 * buffers, otherwise a close could cause an RST on some systems
4305 * (eg: Linux).
4306 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004307 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004308 channel_dont_read(chn);
4309
4310 /* if the server closes the connection, we want to immediately react
4311 * and close the socket to save packets and syscalls.
4312 */
4313 s->si[1].flags |= SI_FL_NOHALF;
4314
4315 /* In any case we've finished parsing the request so we must
4316 * disable Nagle when sending data because 1) we're not going
4317 * to shut this side, and 2) the server is waiting for us to
4318 * send pending data.
4319 */
4320 chn->flags |= CF_NEVER_WAIT;
4321
Christopher Fauletd01ce402019-01-02 17:44:13 +01004322 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4323 /* The server has not finished to respond, so we
4324 * don't want to move in order not to upset it.
4325 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004326 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004327 return;
4328 }
4329
Christopher Fauletf2824e62018-10-01 12:12:37 +02004330 /* When we get here, it means that both the request and the
4331 * response have finished receiving. Depending on the connection
4332 * mode, we'll have to wait for the last bytes to leave in either
4333 * direction, and sometimes for a close to be effective.
4334 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004335 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004336 /* Tunnel mode will not have any analyser so it needs to
4337 * poll for reads.
4338 */
4339 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004340 if (b_data(&chn->buf)) {
4341 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004342 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004343 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004344 txn->req.msg_state = HTTP_MSG_TUNNEL;
4345 }
4346 else {
4347 /* we're not expecting any new data to come for this
4348 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004349 *
4350 * However, there is an exception if the response
4351 * length is undefined. In this case, we need to wait
4352 * the close from the server. The response will be
4353 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004354 */
4355 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4356 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004357 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004358
4359 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4360 channel_shutr_now(chn);
4361 channel_shutw_now(chn);
4362 }
4363 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004364 goto check_channel_flags;
4365 }
4366
4367 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4368 http_msg_closing:
4369 /* nothing else to forward, just waiting for the output buffer
4370 * to be empty and for the shutw_now to take effect.
4371 */
4372 if (channel_is_empty(chn)) {
4373 txn->req.msg_state = HTTP_MSG_CLOSED;
4374 goto http_msg_closed;
4375 }
4376 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004377 txn->req.msg_state = HTTP_MSG_ERROR;
4378 goto end;
4379 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004380 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004381 return;
4382 }
4383
4384 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4385 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004386 /* if we don't know whether the server will close, we need to hard close */
4387 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4388 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004389 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004390 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004391 channel_dont_read(chn);
4392 goto end;
4393 }
4394
4395 check_channel_flags:
4396 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4397 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4398 /* if we've just closed an output, let's switch */
4399 txn->req.msg_state = HTTP_MSG_CLOSING;
4400 goto http_msg_closing;
4401 }
4402
4403 end:
4404 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet0d4da8c2020-12-15 13:32:55 +01004405 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4406 chn->flags |= CF_NEVER_WAIT;
4407 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004408 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet0d4da8c2020-12-15 13:32:55 +01004409 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004410 channel_auto_close(chn);
4411 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004412 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004413}
4414
4415
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004416/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004417 * because an error was triggered during the body forwarding.
4418 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004419static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004420{
4421 struct channel *chn = &s->res;
4422 struct http_txn *txn = s->txn;
4423
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004424 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004425
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004426 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4427 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004428 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004429 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004430 goto end;
4431 }
4432
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004433 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4434 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004435 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004436 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004437
4438 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4439 /* In theory, we don't need to read anymore, but we must
4440 * still monitor the server connection for a possible close
4441 * while the request is being uploaded, so we don't disable
4442 * reading.
4443 */
4444 /* channel_dont_read(chn); */
4445
4446 if (txn->req.msg_state < HTTP_MSG_DONE) {
4447 /* The client seems to still be sending data, probably
4448 * because we got an error response during an upload.
4449 * We have the choice of either breaking the connection
4450 * or letting it pass through. Let's do the later.
4451 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004452 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004453 return;
4454 }
4455
4456 /* When we get here, it means that both the request and the
4457 * response have finished receiving. Depending on the connection
4458 * mode, we'll have to wait for the last bytes to leave in either
4459 * direction, and sometimes for a close to be effective.
4460 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004461 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004462 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004463 if (b_data(&chn->buf)) {
4464 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004465 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004466 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004467 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4468 }
4469 else {
4470 /* we're not expecting any new data to come for this
4471 * transaction, so we can close it.
4472 */
4473 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4474 channel_shutr_now(chn);
4475 channel_shutw_now(chn);
4476 }
4477 }
4478 goto check_channel_flags;
4479 }
4480
4481 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4482 http_msg_closing:
4483 /* nothing else to forward, just waiting for the output buffer
4484 * to be empty and for the shutw_now to take effect.
4485 */
4486 if (channel_is_empty(chn)) {
4487 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4488 goto http_msg_closed;
4489 }
4490 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004491 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004492 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004493 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
William Lallemand656730d2021-03-08 15:26:48 +01004494 if (strm_sess(s)->listener && strm_sess(s)->listener->counters)
Christopher Fauletcff0f732019-12-16 16:13:44 +01004495 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004496 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004497 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004498 goto end;
4499 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004500 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004501 return;
4502 }
4503
4504 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4505 http_msg_closed:
4506 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004507 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004508 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004509 goto end;
4510 }
4511
4512 check_channel_flags:
4513 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4514 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4515 /* if we've just closed an output, let's switch */
4516 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4517 goto http_msg_closing;
4518 }
4519
4520 end:
4521 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet0d4da8c2020-12-15 13:32:55 +01004522 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4523 chn->flags |= CF_NEVER_WAIT;
4524 if (HAS_RSP_DATA_FILTERS(s))
4525 chn->analysers |= AN_RES_FLT_XFER_DATA;
4526 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004527 channel_auto_close(chn);
4528 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004529 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004530}
4531
Christopher Fauletef70e252020-01-28 09:26:19 +01004532/* Forward a response generated by HAProxy (error/redirect/return). This
4533 * function forwards all pending incoming data. If <final> is set to 0, nothing
4534 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004535 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004536 * returned. If an error occurred, 0 is returned. If it fails, this function
4537 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004538 */
4539int http_forward_proxy_resp(struct stream *s, int final)
4540{
4541 struct channel *req = &s->req;
4542 struct channel *res = &s->res;
4543 struct htx *htx = htxbuf(&res->buf);
4544 size_t data;
4545
4546 if (final) {
4547 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004548
Christopher Faulet32ccee12020-11-18 16:44:02 +01004549 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004550 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004551
Christopher Fauletd6c48362020-10-19 18:01:38 +02004552 if (s->txn->meth == HTTP_METH_HEAD)
4553 htx_skip_msg_payload(htx);
4554
Christopher Fauletef70e252020-01-28 09:26:19 +01004555 channel_auto_read(req);
4556 channel_abort(req);
4557 channel_auto_close(req);
4558 channel_htx_erase(req, htxbuf(&req->buf));
4559
4560 res->wex = tick_add_ifset(now_ms, res->wto);
4561 channel_auto_read(res);
4562 channel_auto_close(res);
4563 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004564 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet810df062020-07-22 16:20:34 +02004565 htxbuf(&res->buf)->flags |= HTX_FL_EOI; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004566 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004567 else {
4568 /* Send ASAP informational messages. Rely on CF_EOI for final
4569 * response.
4570 */
4571 res->flags |= CF_SEND_DONTWAIT;
4572 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004573
4574 data = htx->data - co_data(res);
4575 c_adv(res, data);
4576 htx->first = -1;
4577 res->total += data;
4578 return 1;
4579}
4580
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004581void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004582 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004583{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004584 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004585 if (!(s->flags & SF_ERR_MASK))
4586 s->flags |= err;
4587 if (!(s->flags & SF_FINST_MASK))
4588 s->flags |= finst;
4589}
4590
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004591void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004592{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004593 if (!msg) {
4594 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4595 goto end;
4596 }
4597
4598 if (http_reply_message(s, msg) == -1) {
4599 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004600 * it is already an internal error. If it was already a "const"
4601 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004602 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004603 if (s->txn->status == 500) {
4604 if (s->txn->flags & TX_CONST_REPLY)
4605 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004606 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004607 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004608 s->txn->status = 500;
4609 s->txn->http_reply = NULL;
4610 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4611 }
4612
4613end:
4614 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
4615 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
4616
Christopher Faulet0f226952018-10-22 09:29:56 +02004617 channel_auto_read(&s->req);
4618 channel_abort(&s->req);
4619 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004620 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004621 channel_auto_read(&s->res);
4622 channel_auto_close(&s->res);
4623 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004624}
4625
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004626struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004627{
4628 const int msgnum = http_get_status_idx(s->txn->status);
4629
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004630 if (s->txn->http_reply)
4631 return s->txn->http_reply;
4632 else if (s->be->replies[msgnum])
4633 return s->be->replies[msgnum];
4634 else if (strm_fe(s)->replies[msgnum])
4635 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004636 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004637 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004638}
4639
Christopher Faulet40e6b552020-06-25 16:04:50 +02004640/* Produces an HTX message from an http reply. Depending on the http reply type,
4641 * a, errorfile, an raw file or a log-format string is used. On success, it
4642 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4643 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004644 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004645int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004646{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004647 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004648 struct htx_sl *sl;
4649 struct buffer *body = NULL;
4650 const char *status, *reason, *clen, *ctype;
4651 unsigned int slflags;
4652 int ret = 0;
4653
Christopher Faulete29a97e2020-05-14 14:49:25 +02004654 /*
4655 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4656 *
4657 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4658 * as no payload if NULL. the TXN status code is set with the status
4659 * of the original reply.
4660 */
4661
4662 if (reply->type == HTTP_REPLY_INDIRECT) {
4663 if (reply->body.reply)
4664 reply = reply->body.reply;
4665 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004666 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4667 /* get default error message */
4668 if (reply == s->txn->http_reply)
4669 s->txn->http_reply = NULL;
4670 reply = http_error_message(s);
4671 if (reply->type == HTTP_REPLY_INDIRECT) {
4672 if (reply->body.reply)
4673 reply = reply->body.reply;
4674 }
4675 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004676
4677 if (reply->type == HTTP_REPLY_ERRMSG) {
4678 /* implicit or explicit error message*/
4679 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004680 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004681 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004682 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004683 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004684 }
4685 else {
4686 /* no payload, file or log-format string */
4687 if (reply->type == HTTP_REPLY_RAW) {
4688 /* file */
4689 body = &reply->body.obj;
4690 }
4691 else if (reply->type == HTTP_REPLY_LOGFMT) {
4692 /* log-format string */
4693 body = alloc_trash_chunk();
4694 if (!body)
4695 goto fail_alloc;
4696 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4697 }
4698 /* else no payload */
4699
4700 status = ultoa(reply->status);
4701 reason = http_get_reason(reply->status);
4702 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4703 if (!body || !b_data(body))
4704 slflags |= HTX_SL_F_BODYLESS;
4705 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4706 if (!sl)
4707 goto fail;
4708 sl->info.res.status = reply->status;
4709
4710 clen = (body ? ultoa(b_data(body)) : "0");
4711 ctype = reply->ctype;
4712
4713 if (!LIST_ISEMPTY(&reply->hdrs)) {
4714 struct http_reply_hdr *hdr;
4715 struct buffer *value = alloc_trash_chunk();
4716
4717 if (!value)
4718 goto fail;
4719
4720 list_for_each_entry(hdr, &reply->hdrs, list) {
4721 chunk_reset(value);
4722 value->data = build_logline(s, value->area, value->size, &hdr->value);
4723 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4724 free_trash_chunk(value);
4725 goto fail;
4726 }
4727 chunk_reset(value);
4728 }
4729 free_trash_chunk(value);
4730 }
4731
4732 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4733 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4734 !htx_add_endof(htx, HTX_BLK_EOH) ||
4735 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))) ||
4736 !htx_add_endof(htx, HTX_BLK_EOM))
4737 goto fail;
4738 }
4739
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004740 leave:
4741 if (reply->type == HTTP_REPLY_LOGFMT)
4742 free_trash_chunk(body);
4743 return ret;
4744
4745 fail_alloc:
4746 if (!(s->flags & SF_ERR_MASK))
4747 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004748 /* fall through */
4749 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004750 ret = -1;
4751 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004752}
4753
4754/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004755 * occurs -1 is returned and the response channel is truncated, removing this
4756 * way the faulty reply. This function may fail when the reply is formatted
4757 * (http_reply_to_htx) or when the reply is forwarded
4758 * (http_forward_proxy_resp). On the last case, it is because a
4759 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004760 */
4761int http_reply_message(struct stream *s, struct http_reply *reply)
4762{
4763 struct channel *res = &s->res;
4764 struct htx *htx = htx_from_buf(&res->buf);
4765
4766 if (s->txn->status == -1)
4767 s->txn->status = reply->status;
4768 channel_htx_truncate(res, htx);
4769
4770 if (http_reply_to_htx(s, htx, reply) == -1)
4771 goto fail;
4772
4773 htx_to_buf(htx, &s->res.buf);
4774 if (!http_forward_proxy_resp(s, 1))
4775 goto fail;
4776 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004777
4778 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004779 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004780 if (!(s->flags & SF_ERR_MASK))
4781 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004782 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004783}
4784
Christopher Faulet304cc402019-07-15 15:46:28 +02004785/* Return the error message corresponding to si->err_type. It is assumed
4786 * that the server side is closed. Note that err_type is actually a
4787 * bitmask, where almost only aborts may be cumulated with other
4788 * values. We consider that aborted operations are more important
4789 * than timeouts or errors due to the fact that nobody else in the
4790 * logs might explain incomplete retries. All others should avoid
4791 * being cumulated. It should normally not be possible to have multiple
4792 * aborts at once, but just in case, the first one in sequence is reported.
4793 * Note that connection errors appearing on the second request of a keep-alive
4794 * connection are not reported since this allows the client to retry.
4795 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004796void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004797{
4798 int err_type = si->err_type;
4799
4800 /* set s->txn->status for http_error_message(s) */
4801 s->txn->status = 503;
4802
4803 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004804 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4805 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004806 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004807 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4808 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4809 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004810 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004811 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4812 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004813 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004814 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4815 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004816 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004817 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4818 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4819 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004820 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004821 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4822 (s->flags & SF_SRV_REUSED) ? NULL :
4823 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004824 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004825 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4826 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4827 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004828 else { /* SI_ET_CONN_OTHER and others */
4829 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004830 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4831 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004832 }
4833}
4834
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004835
Christopher Faulet4a28a532019-03-01 11:19:40 +01004836/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4837 * on success and -1 on error.
4838 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004839static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004840{
4841 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4842 * then we must send an HTTP/1.1 100 Continue intermediate response.
4843 */
4844 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4845 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4846 struct ist hdr = { .ptr = "Expect", .len = 6 };
4847 struct http_hdr_ctx ctx;
4848
4849 ctx.blk = NULL;
4850 /* Expect is allowed in 1.1, look for it */
4851 if (http_find_header(htx, hdr, &ctx, 0) &&
4852 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004853 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004854 return -1;
4855 http_remove_header(htx, &ctx);
4856 }
4857 }
4858 return 0;
4859}
4860
Christopher Faulet23a3c792018-11-28 10:01:23 +01004861/* Send a 100-Continue response to the client. It returns 0 on success and -1
4862 * on error. The response channel is updated accordingly.
4863 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004864static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004865{
4866 struct channel *res = &s->res;
4867 struct htx *htx = htx_from_buf(&res->buf);
4868 struct htx_sl *sl;
4869 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4870 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004871
4872 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4873 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4874 if (!sl)
4875 goto fail;
4876 sl->info.res.status = 100;
4877
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004878 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004879 goto fail;
4880
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004881 if (!http_forward_proxy_resp(s, 0))
4882 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004883 return 0;
4884
4885 fail:
4886 /* If an error occurred, remove the incomplete HTTP response from the
4887 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004888 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004889 return -1;
4890}
4891
Christopher Faulet12c51e22018-11-28 15:59:42 +01004892
Christopher Faulet0f226952018-10-22 09:29:56 +02004893/*
4894 * Capture headers from message <htx> according to header list <cap_hdr>, and
4895 * fill the <cap> pointers appropriately.
4896 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004897static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004898{
4899 struct cap_hdr *h;
4900 int32_t pos;
4901
Christopher Fauleta3f15502019-05-13 15:27:23 +02004902 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004903 struct htx_blk *blk = htx_get_blk(htx, pos);
4904 enum htx_blk_type type = htx_get_blk_type(blk);
4905 struct ist n, v;
4906
4907 if (type == HTX_BLK_EOH)
4908 break;
4909 if (type != HTX_BLK_HDR)
4910 continue;
4911
4912 n = htx_get_blk_name(htx, blk);
4913
4914 for (h = cap_hdr; h; h = h->next) {
4915 if (h->namelen && (h->namelen == n.len) &&
4916 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4917 if (cap[h->index] == NULL)
4918 cap[h->index] =
4919 pool_alloc(h->pool);
4920
4921 if (cap[h->index] == NULL) {
4922 ha_alert("HTTP capture : out of memory.\n");
4923 break;
4924 }
4925
4926 v = htx_get_blk_value(htx, blk);
4927 if (v.len > h->len)
4928 v.len = h->len;
4929
4930 memcpy(cap[h->index], v.ptr, v.len);
4931 cap[h->index][v.len]=0;
4932 }
4933 }
4934 }
4935}
4936
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004937/* Delete a value in a header between delimiters <from> and <next>. The header
4938 * itself is delimited by <start> and <end> pointers. The number of characters
4939 * displaced is returned, and the pointer to the first delimiter is updated if
4940 * required. The function tries as much as possible to respect the following
4941 * principles :
4942 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4943 * in which case <next> is simply removed
4944 * - set exactly one space character after the new first delimiter, unless there
4945 * are not enough characters in the block being moved to do so.
4946 * - remove unneeded spaces before the previous delimiter and after the new
4947 * one.
4948 *
4949 * It is the caller's responsibility to ensure that :
4950 * - <from> points to a valid delimiter or <start> ;
4951 * - <next> points to a valid delimiter or <end> ;
4952 * - there are non-space chars before <from>.
4953 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004954static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004955{
4956 char *prev = *from;
4957
4958 if (prev == start) {
4959 /* We're removing the first value. eat the semicolon, if <next>
4960 * is lower than <end> */
4961 if (next < end)
4962 next++;
4963
4964 while (next < end && HTTP_IS_SPHT(*next))
4965 next++;
4966 }
4967 else {
4968 /* Remove useless spaces before the old delimiter. */
4969 while (HTTP_IS_SPHT(*(prev-1)))
4970 prev--;
4971 *from = prev;
4972
4973 /* copy the delimiter and if possible a space if we're
4974 * not at the end of the line.
4975 */
4976 if (next < end) {
4977 *prev++ = *next++;
4978 if (prev + 1 < next)
4979 *prev++ = ' ';
4980 while (next < end && HTTP_IS_SPHT(*next))
4981 next++;
4982 }
4983 }
4984 memmove(prev, next, end - next);
4985 return (prev - next);
4986}
4987
Christopher Faulet0f226952018-10-22 09:29:56 +02004988
4989/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004990 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004991 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004992static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004993{
4994 struct ist dst = ist2(str, 0);
4995
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004996 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004997 goto end;
4998 if (dst.len + 1 > len)
4999 goto end;
5000 dst.ptr[dst.len++] = ' ';
5001
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005002 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005003 goto end;
5004 if (dst.len + 1 > len)
5005 goto end;
5006 dst.ptr[dst.len++] = ' ';
5007
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005008 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005009 end:
5010 return dst.len;
5011}
5012
5013/*
5014 * Print a debug line with a start line.
5015 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005016static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005017{
5018 struct session *sess = strm_sess(s);
5019 int max;
5020
5021 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5022 dir,
5023 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5024 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5025
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005026 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005027 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005028 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005029 trash.area[trash.data++] = ' ';
5030
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005031 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005032 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005033 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005034 trash.area[trash.data++] = ' ';
5035
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005036 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005037 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005038 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005039 trash.area[trash.data++] = '\n';
5040
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005041 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005042}
5043
5044/*
5045 * Print a debug line with a header.
5046 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005047static 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 +02005048{
5049 struct session *sess = strm_sess(s);
5050 int max;
5051
5052 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5053 dir,
5054 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5055 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5056
5057 max = n.len;
5058 UBOUND(max, trash.size - trash.data - 3);
5059 chunk_memcat(&trash, n.ptr, max);
5060 trash.area[trash.data++] = ':';
5061 trash.area[trash.data++] = ' ';
5062
5063 max = v.len;
5064 UBOUND(max, trash.size - trash.data - 1);
5065 chunk_memcat(&trash, v.ptr, max);
5066 trash.area[trash.data++] = '\n';
5067
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005068 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005069}
5070
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005071/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5072 * In case of allocation failure, everything allocated is freed and NULL is
5073 * returned. Otherwise the new transaction is assigned to the stream and
5074 * returned.
5075 */
5076struct http_txn *http_alloc_txn(struct stream *s)
5077{
5078 struct http_txn *txn = s->txn;
5079
5080 if (txn)
5081 return txn;
5082
5083 txn = pool_alloc(pool_head_http_txn);
5084 if (!txn)
5085 return txn;
5086
5087 s->txn = txn;
5088 return txn;
5089}
5090
5091void http_txn_reset_req(struct http_txn *txn)
5092{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005093 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005094 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5095}
5096
5097void http_txn_reset_res(struct http_txn *txn)
5098{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005099 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005100 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5101}
5102
5103/*
5104 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5105 * the required fields are properly allocated and that we only need to (re)init
5106 * them. This should be used before processing any new request.
5107 */
5108void http_init_txn(struct stream *s)
5109{
5110 struct http_txn *txn = s->txn;
5111 struct conn_stream *cs = objt_cs(s->si[0].end);
5112
5113 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5114 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5115 : 0);
5116 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005117 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005118 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005119
5120 txn->cookie_first_date = 0;
5121 txn->cookie_last_date = 0;
5122
5123 txn->srv_cookie = NULL;
5124 txn->cli_cookie = NULL;
5125 txn->uri = NULL;
5126
5127 http_txn_reset_req(txn);
5128 http_txn_reset_res(txn);
5129
5130 txn->req.chn = &s->req;
5131 txn->rsp.chn = &s->res;
5132
5133 txn->auth.method = HTTP_AUTH_UNKNOWN;
5134
5135 vars_init(&s->vars_txn, SCOPE_TXN);
5136 vars_init(&s->vars_reqres, SCOPE_REQ);
5137}
5138
5139/* to be used at the end of a transaction */
5140void http_end_txn(struct stream *s)
5141{
5142 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005143
5144 /* these ones will have been dynamically allocated */
5145 pool_free(pool_head_requri, txn->uri);
5146 pool_free(pool_head_capture, txn->cli_cookie);
5147 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005148 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005149
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005150 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005151 txn->uri = NULL;
5152 txn->srv_cookie = NULL;
5153 txn->cli_cookie = NULL;
5154
Christopher Faulet59399252019-11-07 14:27:52 +01005155 if (!LIST_ISEMPTY(&s->vars_txn.head))
5156 vars_prune(&s->vars_txn, s->sess, s);
5157 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5158 vars_prune(&s->vars_reqres, s->sess, s);
5159}
5160
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005161
5162DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005163
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005164__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005165static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005166{
5167}
5168
5169
5170/*
5171 * Local variables:
5172 * c-indent-level: 8
5173 * c-basic-offset: 8
5174 * End:
5175 */