blob: f91388719ee322756d0cf384da27137dde384671 [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 Faulet9768c262018-10-22 09:34:31 +0200151 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200152 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200153 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100154 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200155 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100156 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200157
Christopher Faulet9768c262018-10-22 09:34:31 +0200158 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200159 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet9768c262018-10-22 09:34:31 +0200160 req->analysers &= AN_REQ_FLT_END;
161
Christopher Faulete0768eb2018-10-03 16:38:02 +0200162 if (!(s->flags & SF_FINST_MASK))
163 s->flags |= SF_FINST_R;
164 return 0;
165 }
166
Christopher Faulet9768c262018-10-22 09:34:31 +0200167 /* 2: has the read timeout expired ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200168 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
169 if (!(s->flags & SF_ERR_MASK))
170 s->flags |= SF_ERR_CLITO;
171
172 if (txn->flags & TX_WAIT_NEXT_RQ)
173 goto failed_keep_alive;
174
175 if (sess->fe->options & PR_O_IGNORE_PRB)
176 goto failed_keep_alive;
177
Christopher Faulet9768c262018-10-22 09:34:31 +0200178 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200179 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200180 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100181 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200182 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100183 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200184
Christopher Faulet9768c262018-10-22 09:34:31 +0200185 txn->status = 408;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200186 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200187 req->analysers &= AN_REQ_FLT_END;
188
Christopher Faulete0768eb2018-10-03 16:38:02 +0200189 if (!(s->flags & SF_FINST_MASK))
190 s->flags |= SF_FINST_R;
191 return 0;
192 }
193
Christopher Faulet9768c262018-10-22 09:34:31 +0200194 /* 3: have we encountered a close ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200195 else if (req->flags & CF_SHUTR) {
196 if (!(s->flags & SF_ERR_MASK))
197 s->flags |= SF_ERR_CLICL;
198
199 if (txn->flags & TX_WAIT_NEXT_RQ)
200 goto failed_keep_alive;
201
202 if (sess->fe->options & PR_O_IGNORE_PRB)
203 goto failed_keep_alive;
204
Christopher Faulete0768eb2018-10-03 16:38:02 +0200205 stream_inc_http_err_ctr(s);
206 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200207 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100208 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200209 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100210 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200211
Christopher Faulet9768c262018-10-22 09:34:31 +0200212 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200213 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200214 req->analysers &= AN_REQ_FLT_END;
215
Christopher Faulete0768eb2018-10-03 16:38:02 +0200216 if (!(s->flags & SF_FINST_MASK))
217 s->flags |= SF_FINST_R;
218 return 0;
219 }
220
221 channel_dont_connect(req);
222 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
223 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100224
Christopher Faulet9768c262018-10-22 09:34:31 +0200225 if (sess->listener->options & LI_O_NOQUICKACK && htx_is_not_empty(htx) &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200226 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
227 /* We need more data, we have to re-enable quick-ack in case we
228 * previously disabled it, otherwise we might cause the client
229 * to delay next data.
230 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100231 conn_set_quickack(objt_conn(sess->origin), 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200232 }
Willy Tarreau1a18b542018-12-11 16:37:42 +0100233
Christopher Faulet47365272018-10-31 17:40:50 +0100234 if ((req->flags & CF_READ_PARTIAL) && (txn->flags & TX_WAIT_NEXT_RQ)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200235 /* If the client starts to talk, let's fall back to
236 * request timeout processing.
237 */
238 txn->flags &= ~TX_WAIT_NEXT_RQ;
239 req->analyse_exp = TICK_ETERNITY;
240 }
241
242 /* just set the request timeout once at the beginning of the request */
243 if (!tick_isset(req->analyse_exp)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100244 if ((txn->flags & TX_WAIT_NEXT_RQ) && tick_isset(s->be->timeout.httpka))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200245 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
246 else
247 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
248 }
249
250 /* we're not ready yet */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100251 DBG_TRACE_DEVEL("waiting for the request",
252 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200253 return 0;
254
255 failed_keep_alive:
256 /* Here we process low-level errors for keep-alive requests. In
257 * short, if the request is not the first one and it experiences
258 * a timeout, read error or shutdown, we just silently close so
259 * that the client can try again.
260 */
261 txn->status = 0;
262 msg->msg_state = HTTP_MSG_RQBEFORE;
263 req->analysers &= AN_REQ_FLT_END;
264 s->logs.logwait = 0;
265 s->logs.level = 0;
266 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200267 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100268 DBG_TRACE_DEVEL("leaving by closing K/A connection",
269 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200270 return 0;
271 }
272
Christopher Faulet9768c262018-10-22 09:34:31 +0200273 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200274 stream_inc_http_req_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200275 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200276
Christopher Faulet9768c262018-10-22 09:34:31 +0200277 /* kill the pending keep-alive timeout */
278 txn->flags &= ~TX_WAIT_NEXT_RQ;
279 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200280
Christopher Faulet29f17582019-05-23 11:03:26 +0200281 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200282 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100283
Christopher Faulet9768c262018-10-22 09:34:31 +0200284 /* 0: we might have to print this header in debug mode */
285 if (unlikely((global.mode & MODE_DEBUG) &&
286 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
287 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200288
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200289 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200290
Christopher Fauleta3f15502019-05-13 15:27:23 +0200291 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200292 struct htx_blk *blk = htx_get_blk(htx, pos);
293 enum htx_blk_type type = htx_get_blk_type(blk);
294
295 if (type == HTX_BLK_EOH)
296 break;
297 if (type != HTX_BLK_HDR)
298 continue;
299
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200300 http_debug_hdr("clihdr", s,
301 htx_get_blk_name(htx, blk),
302 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200303 }
304 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200305
306 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100307 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200308 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100309 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100310 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200311 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100312 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +0100313 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100314 if (sl->flags & HTX_SL_F_BODYLESS)
315 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200316
317 /* we can make use of server redirect on GET and HEAD */
318 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
319 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100320 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200321 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200322 goto return_bad_req;
323 }
324
325 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100326 * 2: check if the URI matches the monitor_uri. We have to do this for
327 * every request which gets in, because the monitor-uri is defined by
328 * the frontend. If the monitor-uri starts with a '/', the matching is
329 * done against the request's path. Otherwise, the request's uri is
330 * used. It is a workaround to let HTTP/2 health-checks work as
331 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200332 */
333 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Faulet6072beb2020-02-18 15:34:58 +0100334 ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)),
335 ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) ||
336 isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200337 /*
338 * We have found the monitor URI
339 */
340 struct acl_cond *cond;
341
342 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100343 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200344
345 /* Check if we want to fail this monitor request or not */
346 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
347 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
348
349 ret = acl_pass(ret);
350 if (cond->pol == ACL_COND_UNLESS)
351 ret = !ret;
352
353 if (ret) {
354 /* we fail this request, let's return 503 service unavail */
355 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200356 if (!(s->flags & SF_ERR_MASK))
357 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
358 goto return_prx_cond;
359 }
360 }
361
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800362 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200363 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200364 if (!(s->flags & SF_ERR_MASK))
365 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
366 goto return_prx_cond;
367 }
368
369 /*
370 * 3: Maybe we have to copy the original REQURI for the logs ?
371 * Note: we cannot log anymore if the request has been
372 * classified as invalid.
373 */
374 if (unlikely(s->logs.logwait & LW_REQ)) {
375 /* we have a complete HTTP request that we must log */
376 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200377 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200378
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200379 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200380 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200381
382 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
383 s->do_log(s);
384 } else {
385 ha_alert("HTTP logging : out of memory.\n");
386 }
387 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200388
Christopher Faulete0768eb2018-10-03 16:38:02 +0200389 /* if the frontend has "option http-use-proxy-header", we'll check if
390 * we have what looks like a proxied connection instead of a connection,
391 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
392 * Note that this is *not* RFC-compliant, however browsers and proxies
393 * happen to do that despite being non-standard :-(
394 * We consider that a request not beginning with either '/' or '*' is
395 * a proxied connection, which covers both "scheme://location" and
396 * CONNECT ip:port.
397 */
398 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100399 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200400 txn->flags |= TX_USE_PX_CONN;
401
Christopher Faulete0768eb2018-10-03 16:38:02 +0200402 /* 5: we may need to capture headers */
403 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200404 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200405
Christopher Faulete0768eb2018-10-03 16:38:02 +0200406 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200407 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200408 req->analysers |= AN_REQ_HTTP_BODY;
409
410 /*
411 * RFC7234#4:
412 * A cache MUST write through requests with methods
413 * that are unsafe (Section 4.2.1 of [RFC7231]) to
414 * the origin server; i.e., a cache is not allowed
415 * to generate a reply to such a request before
416 * having forwarded the request and having received
417 * a corresponding response.
418 *
419 * RFC7231#4.2.1:
420 * Of the request methods defined by this
421 * specification, the GET, HEAD, OPTIONS, and TRACE
422 * methods are defined to be safe.
423 */
424 if (likely(txn->meth == HTTP_METH_GET ||
425 txn->meth == HTTP_METH_HEAD ||
426 txn->meth == HTTP_METH_OPTIONS ||
427 txn->meth == HTTP_METH_TRACE))
428 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
429
430 /* end of job, return OK */
431 req->analysers &= ~an_bit;
432 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200433
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100434 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200435 return 1;
436
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200437 return_int_err:
438 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200439 if (!(s->flags & SF_ERR_MASK))
440 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100441 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200442 if (sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100443 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200444 goto return_prx_cond;
445
Christopher Faulete0768eb2018-10-03 16:38:02 +0200446 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200447 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100448 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200449 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100450 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200451 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200452
453 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200454 http_reply_and_close(s, txn->status, http_error_message(s));
455
Christopher Faulete0768eb2018-10-03 16:38:02 +0200456 if (!(s->flags & SF_ERR_MASK))
457 s->flags |= SF_ERR_PRXCOND;
458 if (!(s->flags & SF_FINST_MASK))
459 s->flags |= SF_FINST_R;
460
461 req->analysers &= AN_REQ_FLT_END;
462 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100463 DBG_TRACE_DEVEL("leaving on error",
464 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200465 return 0;
466}
467
468
469/* This stream analyser runs all HTTP request processing which is common to
470 * frontends and backends, which means blocking ACLs, filters, connection-close,
471 * reqadd, stats and redirects. This is performed for the designated proxy.
472 * It returns 1 if the processing can continue on next analysers, or zero if it
473 * either needs more data or wants to immediately abort the request (eg: deny,
474 * error, ...).
475 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200476int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200477{
478 struct session *sess = s->sess;
479 struct http_txn *txn = s->txn;
480 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200481 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200482 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200483 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200484 struct connection *conn = objt_conn(sess->origin);
485
486 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
487 /* we need more data */
488 goto return_prx_yield;
489 }
490
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100491 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200492
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100493 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200494
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200495 /* just in case we have some per-backend tracking. Only called the first
496 * execution of the analyser. */
497 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
498 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200499
500 /* evaluate http-request rules */
501 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100502 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200503
504 switch (verdict) {
505 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
506 goto return_prx_yield;
507
508 case HTTP_RULE_RES_CONT:
509 case HTTP_RULE_RES_STOP: /* nothing to do */
510 break;
511
512 case HTTP_RULE_RES_DENY: /* deny or tarpit */
513 if (txn->flags & TX_CLTARPIT)
514 goto tarpit;
515 goto deny;
516
517 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
518 goto return_prx_cond;
519
520 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
521 goto done;
522
523 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
524 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100525
526 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
527 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200528 }
529 }
530
531 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100532 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200533 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200534
Christopher Fauletff2759f2018-10-24 11:13:16 +0200535 ctx.blk = NULL;
536 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
537 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100538 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200539 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200540 }
541
542 /* OK at this stage, we know that the request was accepted according to
543 * the http-request rules, we can check for the stats. Note that the
544 * URI is detected *before* the req* rules in order not to be affected
545 * by a possible reqrep, while they are processed *after* so that a
546 * reqdeny can still block them. This clearly needs to change in 1.6!
547 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200548 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200549 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100550 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200551 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200552 if (!(s->flags & SF_ERR_MASK))
553 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100554 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200555 }
556
557 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200558 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100559 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200560 /* not all actions implemented: deny, allow, auth */
561
562 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
563 goto deny;
564
565 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
566 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100567
568 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
569 goto return_bad_req;
570
571 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
572 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200573 }
574
Christopher Faulet2571bc62019-03-01 11:44:26 +0100575 /* Proceed with the applets now. */
576 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200577 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100578 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200579
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200580 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100581 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100582
Christopher Faulete0768eb2018-10-03 16:38:02 +0200583 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
584 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
585 if (!(s->flags & SF_FINST_MASK))
586 s->flags |= SF_FINST_R;
587
588 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
589 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
590 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
591 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100592
593 req->flags |= CF_SEND_DONTWAIT;
594 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200595 goto done;
596 }
597
598 /* check whether we have some ACLs set to redirect this request */
599 list_for_each_entry(rule, &px->redirect_rules, list) {
600 if (rule->cond) {
601 int ret;
602
603 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
604 ret = acl_pass(ret);
605 if (rule->cond->pol == ACL_COND_UNLESS)
606 ret = !ret;
607 if (!ret)
608 continue;
609 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200610 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100611 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200612 goto done;
613 }
614
615 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
616 * If this happens, then the data will not come immediately, so we must
617 * send all what we have without waiting. Note that due to the small gain
618 * in waiting for the body of the request, it's easier to simply put the
619 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
620 * itself once used.
621 */
622 req->flags |= CF_SEND_DONTWAIT;
623
624 done: /* done with this analyser, continue with next ones that the calling
625 * points will have set, if any.
626 */
627 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500628 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200629 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100630 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200631 return 1;
632
633 tarpit:
634 /* Allow cookie logging
635 */
636 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200637 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200638
639 /* When a connection is tarpitted, we use the tarpit timeout,
640 * which may be the same as the connect timeout if unspecified.
641 * If unset, then set it to zero because we really want it to
642 * eventually expire. We build the tarpit as an analyser.
643 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100644 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200645
646 /* wipe the request out so that we can drop the connection early
647 * if the client closes first.
648 */
649 channel_dont_connect(req);
650
Christopher Faulete0768eb2018-10-03 16:38:02 +0200651 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
652 req->analysers |= AN_REQ_HTTP_TARPIT;
653 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
654 if (!req->analyse_exp)
655 req->analyse_exp = tick_add(now_ms, 0);
656 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100657 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100658 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100659 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200660 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100661 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200662 goto done_without_exp;
663
664 deny: /* this request was blocked (denied) */
665
666 /* Allow cookie logging
667 */
668 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200669 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200670
Christopher Faulete0768eb2018-10-03 16:38:02 +0200671 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200672 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100673 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100674 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100675 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200676 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100677 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100678 goto return_prx_err;
679
680 return_int_err:
681 txn->status = 500;
682 if (!(s->flags & SF_ERR_MASK))
683 s->flags |= SF_ERR_INTERNAL;
684 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100685 if (s->flags & SF_BE_ASSIGNED)
686 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100687 if (sess->listener->counters)
688 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
689 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200690
691 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200692 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100693 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200694 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100695 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100696 /* fall through */
697
698 return_prx_err:
699 http_reply_and_close(s, txn->status, http_error_message(s));
700 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200701
702 return_prx_cond:
703 if (!(s->flags & SF_ERR_MASK))
704 s->flags |= SF_ERR_PRXCOND;
705 if (!(s->flags & SF_FINST_MASK))
706 s->flags |= SF_FINST_R;
707
708 req->analysers &= AN_REQ_FLT_END;
709 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100710 DBG_TRACE_DEVEL("leaving on error",
711 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200712 return 0;
713
714 return_prx_yield:
715 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100716 DBG_TRACE_DEVEL("waiting for more data",
717 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200718 return 0;
719}
720
721/* This function performs all the processing enabled for the current request.
722 * It returns 1 if the processing can continue on next analysers, or zero if it
723 * needs more data, encounters an error, or wants to immediately abort the
724 * request. It relies on buffers flags, and updates s->req.analysers.
725 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200726int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200727{
728 struct session *sess = s->sess;
729 struct http_txn *txn = s->txn;
730 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200731 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200732 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
733
734 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
735 /* we need more data */
736 channel_dont_connect(req);
737 return 0;
738 }
739
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100740 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200741
742 /*
743 * Right now, we know that we have processed the entire headers
744 * and that unwanted requests have been filtered out. We can do
745 * whatever we want with the remaining request. Also, now we
746 * may have separate values for ->fe, ->be.
747 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100748 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200749
750 /*
751 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200752 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200753 */
754 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100755 struct htx_sl *sl;
756 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200757
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200758 if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200759 if (!(s->flags & SF_ERR_MASK))
760 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100761 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200762 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200763 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100764 uri = htx_sl_req_uri(sl);
765 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200766
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200767 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200768 goto return_bad_req;
769
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200770 s->target = &s->be->obj_type;
771 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
772
Christopher Faulete0768eb2018-10-03 16:38:02 +0200773 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200774 * uri.ptr and path.ptr (excluded). If it was not found, we need
775 * to replace from all the uri by a single "/".
776 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500777 * Instead of rewriting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100778 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200779 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200780 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100781 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200782 }
783
784 /*
785 * 7: Now we can work with the cookies.
786 * Note that doing so might move headers in the request, but
787 * the fields will stay coherent and the URI will not move.
788 * This should only be performed in the backend.
789 */
790 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200791 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200792
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100793 /* 8: Generate unique ID if a "unique-id-format" is defined.
794 *
795 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
796 * fetches only available in the HTTP request processing stage.
797 */
798 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100799 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200800
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100801 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100802 if (!(s->flags & SF_ERR_MASK))
803 s->flags |= SF_ERR_RESOURCE;
804 goto return_int_err;
805 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200806
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100807 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100808 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100809 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100810 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200811 }
812
813 /*
814 * 9: add X-Forwarded-For if either the frontend or the backend
815 * asks for it.
816 */
817 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200818 struct http_hdr_ctx ctx = { .blk = NULL };
819 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
820 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
821
Christopher Faulete0768eb2018-10-03 16:38:02 +0200822 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200823 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200824 /* The header is set to be added only if none is present
825 * and we found it, so don't do anything.
826 */
827 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200828 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200829 /* Add an X-Forwarded-For header unless the source IP is
830 * in the 'except' network range.
831 */
832 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200833 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200834 != sess->fe->except_net.s_addr) &&
835 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200836 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200837 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200838 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200839
840 /* Note: we rely on the backend to get the header name to be used for
841 * x-forwarded-for, because the header is really meant for the backends.
842 * However, if the backend did not specify any option, we have to rely
843 * on the frontend's header name.
844 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200845 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
846 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100847 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200848 }
849 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200850 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200851 /* FIXME: for the sake of completeness, we should also support
852 * 'except' here, although it is mostly useless in this case.
853 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200854 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200855
Christopher Faulete0768eb2018-10-03 16:38:02 +0200856 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200857 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200858 pn, sizeof(pn));
859
860 /* Note: we rely on the backend to get the header name to be used for
861 * x-forwarded-for, because the header is really meant for the backends.
862 * However, if the backend did not specify any option, we have to rely
863 * on the frontend's header name.
864 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200865 chunk_printf(&trash, "%s", pn);
866 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100867 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200868 }
869 }
870
871 /*
872 * 10: add X-Original-To if either the frontend or the backend
873 * asks for it.
874 */
875 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
876
877 /* FIXME: don't know if IPv6 can handle that case too. */
Christopher Fauletdf82f0b2021-02-26 12:45:56 +0100878 if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200879 /* Add an X-Original-To header unless the destination IP is
880 * in the 'except' network range.
881 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200882 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200883 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200884 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200885 != sess->fe->except_to.s_addr) &&
886 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200887 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200888 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200889 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200890 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200891
892 /* Note: we rely on the backend to get the header name to be used for
893 * x-original-to, because the header is really meant for the backends.
894 * However, if the backend did not specify any option, we have to rely
895 * on the frontend's header name.
896 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200897 if (s->be->orgto_hdr_len)
898 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
899 else
900 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200901
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200902 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
903 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100904 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200905 }
906 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200907 }
908
Christopher Faulete0768eb2018-10-03 16:38:02 +0200909 /* If we have no server assigned yet and we're balancing on url_param
910 * with a POST request, we may be interested in checking the body for
911 * that parameter. This will be done in another analyser.
912 */
913 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100914 s->txn->meth == HTTP_METH_POST &&
915 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200916 channel_dont_connect(req);
917 req->analysers |= AN_REQ_HTTP_BODY;
918 }
919
920 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
921 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100922
Christopher Faulete0768eb2018-10-03 16:38:02 +0200923 /* We expect some data from the client. Unless we know for sure
924 * we already have a full request, we have to re-enable quick-ack
925 * in case we previously disabled it, otherwise we might cause
926 * the client to delay further data.
927 */
928 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200929 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100930 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200931
932 /*************************************************************
933 * OK, that's finished for the headers. We have done what we *
934 * could. Let's switch to the DATA state. *
935 ************************************************************/
936 req->analyse_exp = TICK_ETERNITY;
937 req->analysers &= ~an_bit;
938
939 s->logs.tv_request = now;
940 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100941 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200942 return 1;
943
Christopher Fauletb8a53712019-12-16 11:29:38 +0100944 return_int_err:
945 txn->status = 500;
946 if (!(s->flags & SF_ERR_MASK))
947 s->flags |= SF_ERR_INTERNAL;
948 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100949 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100950 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100951 if (sess->listener->counters)
952 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
953 goto return_prx_cond;
954
Christopher Faulete0768eb2018-10-03 16:38:02 +0200955 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200956 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100957 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200958 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100959 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100960 /* fall through */
961
962 return_prx_cond:
963 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200964
965 if (!(s->flags & SF_ERR_MASK))
966 s->flags |= SF_ERR_PRXCOND;
967 if (!(s->flags & SF_FINST_MASK))
968 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100969
970 req->analysers &= AN_REQ_FLT_END;
971 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100972 DBG_TRACE_DEVEL("leaving on error",
973 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200974 return 0;
975}
976
977/* This function is an analyser which processes the HTTP tarpit. It always
978 * returns zero, at the beginning because it prevents any other processing
979 * from occurring, and at the end because it terminates the request.
980 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200981int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200982{
983 struct http_txn *txn = s->txn;
984
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100985 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200986 /* This connection is being tarpitted. The CLIENT side has
987 * already set the connect expiration date to the right
988 * timeout. We just have to check that the client is still
989 * there and that the timeout has not expired.
990 */
991 channel_dont_connect(req);
992 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100993 !tick_is_expired(req->analyse_exp, now_ms)) {
994 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
995 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200996 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100997 }
998
Christopher Faulete0768eb2018-10-03 16:38:02 +0200999
1000 /* We will set the queue timer to the time spent, just for
1001 * logging purposes. We fake a 500 server error, so that the
1002 * attacker will not suspect his connection has been tarpitted.
1003 * It will not cause trouble to the logs because we can exclude
1004 * the tarpitted connections by filtering on the 'PT' status flags.
1005 */
1006 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1007
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02001008 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001009
1010 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001011 req->analysers &= AN_REQ_FLT_END;
1012 req->analyse_exp = TICK_ETERNITY;
1013
1014 if (!(s->flags & SF_ERR_MASK))
1015 s->flags |= SF_ERR_PRXCOND;
1016 if (!(s->flags & SF_FINST_MASK))
1017 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001018
1019 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001020 return 0;
1021}
1022
1023/* This function is an analyser which waits for the HTTP request body. It waits
1024 * for either the buffer to be full, or the full advertised contents to have
1025 * reached the buffer. It must only be called after the standard HTTP request
1026 * processing has occurred, because it expects the request to be parsed and will
1027 * look for the Expect header. It may send a 100-Continue interim response. It
1028 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
1029 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
1030 * needs to read more data, or 1 once it has completed its analysis.
1031 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001032int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001033{
1034 struct session *sess = s->sess;
1035 struct http_txn *txn = s->txn;
1036 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001037 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001038
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001039 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001040
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001041 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001042
Willy Tarreau4236f032019-03-05 10:43:32 +01001043 if (htx->flags & HTX_FL_PARSING_ERROR)
1044 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001045 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1046 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001047
Christopher Faulet9ded6fa2020-11-16 16:03:35 +01001048 /* CONNECT requests have no body */
1049 if (txn->meth == HTTP_METH_CONNECT)
1050 goto http_end;
1051
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001052 if (msg->msg_state < HTTP_MSG_BODY)
1053 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001054
Christopher Faulete0768eb2018-10-03 16:38:02 +02001055 /* We have to parse the HTTP request body to find any required data.
1056 * "balance url_param check_post" should have been the only way to get
1057 * into this. We were brought here after HTTP header analysis, so all
1058 * related structures are ready.
1059 */
1060
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001061 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001062 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001063 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001064 }
1065
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001066 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001067
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001068 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1069 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001070 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001071 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001072 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001073 goto http_end;
1074
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001075 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001076 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1077 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001078 if (!(s->flags & SF_ERR_MASK))
1079 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001080 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1081 if (sess->listener->counters)
1082 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1083 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001084 }
1085
1086 /* we get here if we need to wait for more data */
1087 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1088 /* Not enough data. We'll re-use the http-request
1089 * timeout here. Ideally, we should set the timeout
1090 * relative to the accept() date. We just set the
1091 * request timeout once at the beginning of the
1092 * request.
1093 */
1094 channel_dont_connect(req);
1095 if (!tick_isset(req->analyse_exp))
1096 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001097 DBG_TRACE_DEVEL("waiting for more data",
1098 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001099 return 0;
1100 }
1101
1102 http_end:
1103 /* The situation will not evolve, so let's give up on the analysis. */
1104 s->logs.tv_request = now; /* update the request timer to reflect full request */
1105 req->analysers &= ~an_bit;
1106 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001107 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001108 return 1;
1109
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001110 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001111 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001112 if (!(s->flags & SF_ERR_MASK))
1113 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001114 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001115 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +01001116 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001117 if (sess->listener->counters)
1118 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
1119 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001120
Christopher Faulete0768eb2018-10-03 16:38:02 +02001121 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001122 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001123 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1124 if (sess->listener->counters)
1125 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1126 /* fall through */
1127
1128 return_prx_cond:
1129 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001130
1131 if (!(s->flags & SF_ERR_MASK))
1132 s->flags |= SF_ERR_PRXCOND;
1133 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001134 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001135
Christopher Faulete0768eb2018-10-03 16:38:02 +02001136 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001137 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001138 DBG_TRACE_DEVEL("leaving on error",
1139 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001140 return 0;
1141}
1142
1143/* This function is an analyser which forwards request body (including chunk
1144 * sizes if any). It is called as soon as we must forward, even if we forward
1145 * zero byte. The only situation where it must not be called is when we're in
1146 * tunnel mode and we want to forward till the close. It's used both to forward
1147 * remaining data and to resync after end of body. It expects the msg_state to
1148 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1149 * read more data, or 1 once we can go on with next request or end the stream.
1150 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1151 * bytes of pending data + the headers if not already done.
1152 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001153int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001154{
1155 struct session *sess = s->sess;
1156 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001157 struct http_msg *msg = &txn->req;
1158 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001159 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001160 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001161
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001162 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001163
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001164 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001165
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001166 if (htx->flags & HTX_FL_PARSING_ERROR)
1167 goto return_bad_req;
1168 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1169 goto return_int_err;
1170
Christopher Faulete0768eb2018-10-03 16:38:02 +02001171 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1172 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1173 /* Output closed while we were sending data. We must abort and
1174 * wake the other side up.
1175 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001176
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001177 /* Don't abort yet if we had L7 retries activated and it
1178 * was a write error, we may recover.
1179 */
1180 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001181 (s->si[1].flags & SI_FL_L7_RETRY)) {
1182 DBG_TRACE_DEVEL("leaving on L7 retry",
1183 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001184 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001185 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001186 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001187 http_end_request(s);
1188 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001189 DBG_TRACE_DEVEL("leaving on error",
1190 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001191 return 1;
1192 }
1193
1194 /* Note that we don't have to send 100-continue back because we don't
1195 * need the data to complete our job, and it's up to the server to
1196 * decide whether to return 100, 417 or anything else in return of
1197 * an "Expect: 100-continue" header.
1198 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001199 if (msg->msg_state == HTTP_MSG_BODY)
1200 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001201
Christopher Faulete0768eb2018-10-03 16:38:02 +02001202 /* in most states, we should abort in case of early close */
1203 channel_auto_close(req);
1204
1205 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001206 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001207 if (req->flags & CF_EOI)
1208 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001209 }
1210 else {
1211 /* We can't process the buffer's contents yet */
1212 req->flags |= CF_WAKE_WRITE;
1213 goto missing_data_or_waiting;
1214 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001215 }
1216
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001217 if (msg->msg_state >= HTTP_MSG_ENDING)
1218 goto ending;
1219
1220 if (txn->meth == HTTP_METH_CONNECT) {
1221 msg->msg_state = HTTP_MSG_ENDING;
1222 goto ending;
1223 }
1224
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001225 /* Forward input data. We get it by removing all outgoing data not
1226 * forwarded yet from HTX data size. If there are some data filters, we
1227 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001228 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001229 if (HAS_REQ_DATA_FILTERS(s)) {
1230 ret = flt_http_payload(s, msg, htx->data);
1231 if (ret < 0)
1232 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001233 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001234 }
1235 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001236 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001237 if (msg->flags & HTTP_MSGF_XFER_LEN)
1238 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001239 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001240
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001241 if (htx->data != co_data(req))
1242 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001243
Christopher Faulet9768c262018-10-22 09:34:31 +02001244 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001245 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1246 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001247 */
1248 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1249 goto missing_data_or_waiting;
1250
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001251 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001252
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001253 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001254 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1255
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001256 /* other states, ENDING...TUNNEL */
1257 if (msg->msg_state >= HTTP_MSG_DONE)
1258 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001259
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001260 if (HAS_REQ_DATA_FILTERS(s)) {
1261 ret = flt_http_end(s, msg);
1262 if (ret <= 0) {
1263 if (!ret)
1264 goto missing_data_or_waiting;
1265 goto return_bad_req;
1266 }
1267 }
1268
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001269 if (txn->meth == HTTP_METH_CONNECT)
1270 msg->msg_state = HTTP_MSG_TUNNEL;
1271 else {
1272 msg->msg_state = HTTP_MSG_DONE;
1273 req->to_forward = 0;
1274 }
1275
1276 done:
1277 /* we don't want to forward closes on DONE except in tunnel mode. */
1278 if (!(txn->flags & TX_CON_WANT_TUN))
1279 channel_dont_close(req);
1280
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001281 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001282 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001283 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001284 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1285 if (req->flags & CF_SHUTW) {
1286 /* request errors are most likely due to the
1287 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001288 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001289 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001290 goto return_bad_req;
1291 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001292 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001293 return 1;
1294 }
1295
1296 /* If "option abortonclose" is set on the backend, we want to monitor
1297 * the client's connection and forward any shutdown notification to the
1298 * server, which will decide whether to close or to go on processing the
1299 * request. We only do that in tunnel mode, and not in other modes since
1300 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001301 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001302 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001303 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001304 s->si[1].flags |= SI_FL_NOLINGER;
1305 channel_auto_close(req);
1306 }
1307 else if (s->txn->meth == HTTP_METH_POST) {
1308 /* POST requests may require to read extra CRLF sent by broken
1309 * browsers and which could cause an RST to be sent upon close
1310 * on some systems (eg: Linux). */
1311 channel_auto_read(req);
1312 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001313 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1314 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001315 return 0;
1316
1317 missing_data_or_waiting:
1318 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001319 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001320 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001321
1322 waiting:
1323 /* waiting for the last bits to leave the buffer */
1324 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001325 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001326
1327 /* When TE: chunked is used, we need to get there again to parse remaining
1328 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1329 * And when content-length is used, we never want to let the possible
1330 * shutdown be forwarded to the other side, as the state machine will
1331 * take care of it once the client responds. It's also important to
1332 * prevent TIME_WAITs from accumulating on the backend side, and for
1333 * HTTP/2 where the last frame comes with a shutdown.
1334 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001335 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001336 channel_dont_close(req);
1337
1338 /* We know that more data are expected, but we couldn't send more that
1339 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1340 * system knows it must not set a PUSH on this first part. Interactive
1341 * modes are already handled by the stream sock layer. We must not do
1342 * this in content-length mode because it could present the MSG_MORE
1343 * flag with the last block of forwarded data, which would cause an
1344 * additional delay to be observed by the receiver.
1345 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001346 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001347 req->flags |= CF_EXPECT_MORE;
1348
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001349 DBG_TRACE_DEVEL("waiting for more data to forward",
1350 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001351 return 0;
1352
Christopher Faulet93e02d82019-03-08 14:18:50 +01001353 return_cli_abort:
1354 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1355 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001356 if (sess->listener->counters)
1357 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001358 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001359 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001360 if (!(s->flags & SF_ERR_MASK))
1361 s->flags |= SF_ERR_CLICL;
1362 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001363 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001364
1365 return_srv_abort:
1366 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1367 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001368 if (sess->listener->counters)
1369 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001370 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001371 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001372 if (!(s->flags & SF_ERR_MASK))
1373 s->flags |= SF_ERR_SRVCL;
1374 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001375 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001376
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001377 return_int_err:
1378 if (!(s->flags & SF_ERR_MASK))
1379 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001380 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001381 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001382 if (sess->listener->counters)
1383 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001384 if (objt_server(s->target))
1385 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001386 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001387 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001388
Christopher Faulet93e02d82019-03-08 14:18:50 +01001389 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001390 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001391 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001392 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001393 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001394 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001395
Christopher Fauletb8a53712019-12-16 11:29:38 +01001396 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001397 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001398 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001399 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001400 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001401 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001402 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001403 }
1404 req->analysers &= AN_REQ_FLT_END;
1405 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 +01001406 if (!(s->flags & SF_ERR_MASK))
1407 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001408 if (!(s->flags & SF_FINST_MASK))
1409 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001410 DBG_TRACE_DEVEL("leaving on error ",
1411 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001412 return 0;
1413}
1414
Olivier Houcharda254a372019-04-05 15:30:12 +02001415/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1416/* Returns 0 if we can attempt to retry, -1 otherwise */
1417static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1418{
1419 struct channel *req, *res;
1420 int co_data;
1421
1422 si->conn_retries--;
1423 if (si->conn_retries < 0)
1424 return -1;
1425
Willy Tarreau223995e2019-05-04 10:38:31 +02001426 if (objt_server(s->target))
1427 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1428 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1429
Olivier Houcharda254a372019-04-05 15:30:12 +02001430 req = &s->req;
1431 res = &s->res;
1432 /* Remove any write error from the request, and read error from the response */
1433 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1434 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1435 res->analysers = 0;
1436 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001437 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001438 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001439 si->exp = TICK_ETERNITY;
1440 res->rex = TICK_ETERNITY;
1441 res->to_forward = 0;
1442 res->analyse_exp = TICK_ETERNITY;
1443 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001444 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001445 si_release_endpoint(&s->si[1]);
1446 b_free(&req->buf);
1447 /* Swap the L7 buffer with the channel buffer */
1448 /* We know we stored the co_data as b_data, so get it there */
1449 co_data = b_data(&si->l7_buffer);
1450 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1451 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1452
1453 co_set_data(req, co_data);
1454 b_reset(&res->buf);
1455 co_set_data(res, 0);
1456 return 0;
1457}
1458
Christopher Faulete0768eb2018-10-03 16:38:02 +02001459/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1460 * processing can continue on next analysers, or zero if it either needs more
1461 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1462 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1463 * when it has nothing left to do, and may remove any analyser when it wants to
1464 * abort.
1465 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001466int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001467{
Christopher Faulet9768c262018-10-22 09:34:31 +02001468 /*
1469 * We will analyze a complete HTTP response to check the its syntax.
1470 *
1471 * Once the start line and all headers are received, we may perform a
1472 * capture of the error (if any), and we will set a few fields. We also
1473 * logging and finally headers capture.
1474 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001475 struct session *sess = s->sess;
1476 struct http_txn *txn = s->txn;
1477 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001478 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001479 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001480 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001481 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001482 int n;
1483
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001484 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001485
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001486 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001487
Willy Tarreau4236f032019-03-05 10:43:32 +01001488 /* Parsing errors are caught here */
1489 if (htx->flags & HTX_FL_PARSING_ERROR)
1490 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001491 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1492 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001493
Christopher Faulete0768eb2018-10-03 16:38:02 +02001494 /*
1495 * Now we quickly check if we have found a full valid response.
1496 * If not so, we check the FD and buffer states before leaving.
1497 * A full response is indicated by the fact that we have seen
1498 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1499 * responses are checked first.
1500 *
1501 * Depending on whether the client is still there or not, we
1502 * may send an error response back or not. Note that normally
1503 * we should only check for HTTP status there, and check I/O
1504 * errors somewhere else.
1505 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001506 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001507 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001508 /* 1: have we encountered a read error ? */
1509 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001510 struct connection *conn = NULL;
1511
Olivier Houchard865d8392019-05-03 22:46:27 +02001512 if (objt_cs(s->si[1].end))
1513 conn = objt_cs(s->si[1].end)->conn;
1514
1515 if (si_b->flags & SI_FL_L7_RETRY &&
1516 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001517 /* If we arrive here, then CF_READ_ERROR was
1518 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001519 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001520 * the SI_FL_L7_RETRY flag, so it's ok not
1521 * to check s->be->retry_type.
1522 */
1523 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1524 return 0;
1525 }
1526
Olivier Houchard6db16992019-05-17 15:40:49 +02001527 if (txn->flags & TX_NOT_FIRST)
1528 goto abort_keep_alive;
1529
Olivier Houcharda798bf52019-03-08 18:52:00 +01001530 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001531 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001532 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001533 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001534 }
1535
Christopher Faulete0768eb2018-10-03 16:38:02 +02001536 rep->analysers &= AN_RES_FLT_END;
1537 txn->status = 502;
1538
1539 /* Check to see if the server refused the early data.
1540 * If so, just send a 425
1541 */
Willy Tarreauee99aaf2020-06-23 05:58:20 +02001542 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001543 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001544 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001545 do_l7_retry(s, si_b) == 0) {
1546 DBG_TRACE_DEVEL("leaving on L7 retry",
1547 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001548 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001549 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001550 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001551 }
1552
1553 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001554 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001555
1556 if (!(s->flags & SF_ERR_MASK))
1557 s->flags |= SF_ERR_SRVCL;
1558 if (!(s->flags & SF_FINST_MASK))
1559 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001560 DBG_TRACE_DEVEL("leaving on error",
1561 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001562 return 0;
1563 }
1564
Christopher Faulet9768c262018-10-22 09:34:31 +02001565 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001566 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001567 if ((si_b->flags & SI_FL_L7_RETRY) &&
1568 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001569 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1570 DBG_TRACE_DEVEL("leaving on L7 retry",
1571 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001572 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001573 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001574 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001575 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001576 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001577 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001578 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001579 }
1580
Christopher Faulete0768eb2018-10-03 16:38:02 +02001581 rep->analysers &= AN_RES_FLT_END;
1582 txn->status = 504;
1583 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001584 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001585
1586 if (!(s->flags & SF_ERR_MASK))
1587 s->flags |= SF_ERR_SRVTO;
1588 if (!(s->flags & SF_FINST_MASK))
1589 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001590 DBG_TRACE_DEVEL("leaving on error",
1591 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001592 return 0;
1593 }
1594
Christopher Faulet9768c262018-10-22 09:34:31 +02001595 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001596 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001597 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1598 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001599 if (sess->listener->counters)
1600 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001601 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001602 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001603
1604 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001605 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001606 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001607
1608 if (!(s->flags & SF_ERR_MASK))
1609 s->flags |= SF_ERR_CLICL;
1610 if (!(s->flags & SF_FINST_MASK))
1611 s->flags |= SF_FINST_H;
1612
1613 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001614 DBG_TRACE_DEVEL("leaving on error",
1615 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001616 return 0;
1617 }
1618
Christopher Faulet9768c262018-10-22 09:34:31 +02001619 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001620 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001621 if ((si_b->flags & SI_FL_L7_RETRY) &&
1622 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001623 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1624 DBG_TRACE_DEVEL("leaving on L7 retry",
1625 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001626 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001627 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001628 }
1629
Olivier Houchard6db16992019-05-17 15:40:49 +02001630 if (txn->flags & TX_NOT_FIRST)
1631 goto abort_keep_alive;
1632
Olivier Houcharda798bf52019-03-08 18:52:00 +01001633 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001634 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001635 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001636 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001637 }
1638
Christopher Faulete0768eb2018-10-03 16:38:02 +02001639 rep->analysers &= AN_RES_FLT_END;
1640 txn->status = 502;
1641 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001642 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001643
1644 if (!(s->flags & SF_ERR_MASK))
1645 s->flags |= SF_ERR_SRVCL;
1646 if (!(s->flags & SF_FINST_MASK))
1647 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001648 DBG_TRACE_DEVEL("leaving on error",
1649 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001650 return 0;
1651 }
1652
Christopher Faulet9768c262018-10-22 09:34:31 +02001653 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001654 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001655 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001656 goto abort_keep_alive;
1657
Olivier Houcharda798bf52019-03-08 18:52:00 +01001658 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001659 if (objt_server(s->target))
1660 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001661 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001662
1663 if (!(s->flags & SF_ERR_MASK))
1664 s->flags |= SF_ERR_CLICL;
1665 if (!(s->flags & SF_FINST_MASK))
1666 s->flags |= SF_FINST_H;
1667
1668 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001669 DBG_TRACE_DEVEL("leaving on error",
1670 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001671 return 0;
1672 }
1673
1674 channel_dont_close(rep);
1675 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001676 DBG_TRACE_DEVEL("waiting for more data",
1677 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001678 return 0;
1679 }
1680
1681 /* More interesting part now : we know that we have a complete
1682 * response which at least looks like HTTP. We have an indicator
1683 * of each header's length, so we can parse them quickly.
1684 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001685 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001686 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001687 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001688
Christopher Faulet9768c262018-10-22 09:34:31 +02001689 /* 0: we might have to print this header in debug mode */
1690 if (unlikely((global.mode & MODE_DEBUG) &&
1691 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1692 int32_t pos;
1693
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001694 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001695
Christopher Fauleta3f15502019-05-13 15:27:23 +02001696 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001697 struct htx_blk *blk = htx_get_blk(htx, pos);
1698 enum htx_blk_type type = htx_get_blk_type(blk);
1699
1700 if (type == HTX_BLK_EOH)
1701 break;
1702 if (type != HTX_BLK_HDR)
1703 continue;
1704
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001705 http_debug_hdr("srvhdr", s,
1706 htx_get_blk_name(htx, blk),
1707 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001708 }
1709 }
1710
Christopher Faulet03599112018-11-27 11:21:21 +01001711 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001712 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001713 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001714 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001715 if (sl->flags & HTX_SL_F_XFER_LEN) {
1716 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001717 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001718 if (sl->flags & HTX_SL_F_BODYLESS)
1719 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001720 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001721
1722 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001723 if (n < 1 || n > 5)
1724 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001725
Christopher Faulete0768eb2018-10-03 16:38:02 +02001726 /* when the client triggers a 4xx from the server, it's most often due
1727 * to a missing object or permission. These events should be tracked
1728 * because if they happen often, it may indicate a brute force or a
1729 * vulnerability scan.
1730 */
1731 if (n == 4)
1732 stream_inc_http_err_ctr(s);
1733
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001734 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001735 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001736 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1737 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001738
Christopher Faulete0768eb2018-10-03 16:38:02 +02001739 /* Adjust server's health based on status code. Note: status codes 501
1740 * and 505 are triggered on demand by client request, so we must not
1741 * count them as server failures.
1742 */
1743 if (objt_server(s->target)) {
1744 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001745 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001746 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001747 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001748 }
1749
1750 /*
1751 * We may be facing a 100-continue response, or any other informational
1752 * 1xx response which is non-final, in which case this is not the right
1753 * response, and we're waiting for the next one. Let's allow this response
1754 * to go to the client and wait for the next one. There's an exception for
1755 * 101 which is used later in the code to switch protocols.
1756 */
1757 if (txn->status < 200 &&
1758 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001759 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001760 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001761 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001762 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001763 txn->status = 0;
1764 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001765 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001766 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001767 }
1768
1769 /*
1770 * 2: check for cacheability.
1771 */
1772
1773 switch (txn->status) {
1774 case 200:
1775 case 203:
1776 case 204:
1777 case 206:
1778 case 300:
1779 case 301:
1780 case 404:
1781 case 405:
1782 case 410:
1783 case 414:
1784 case 501:
1785 break;
1786 default:
1787 /* RFC7231#6.1:
1788 * Responses with status codes that are defined as
1789 * cacheable by default (e.g., 200, 203, 204, 206,
1790 * 300, 301, 404, 405, 410, 414, and 501 in this
1791 * specification) can be reused by a cache with
1792 * heuristic expiration unless otherwise indicated
1793 * by the method definition or explicit cache
1794 * controls [RFC7234]; all other status codes are
1795 * not cacheable by default.
1796 */
1797 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1798 break;
1799 }
1800
1801 /*
1802 * 3: we may need to capture headers
1803 */
1804 s->logs.logwait &= ~LW_RESP;
1805 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001806 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001807
Christopher Faulet9768c262018-10-22 09:34:31 +02001808 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001809 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1810 txn->status == 101)) {
1811 /* Either we've established an explicit tunnel, or we're
1812 * switching the protocol. In both cases, we're very unlikely
1813 * to understand the next protocols. We have to switch to tunnel
1814 * mode, so that we transfer the request and responses then let
1815 * this protocol pass unmodified. When we later implement specific
1816 * parsers for such protocols, we'll want to check the Upgrade
1817 * header which contains information about that protocol for
1818 * responses with status 101 (eg: see RFC2817 about TLS).
1819 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001820 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001821 }
1822
Christopher Faulet61608322018-11-23 16:23:45 +01001823 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1824 * 407 (Proxy-Authenticate) responses and set the connection to private
1825 */
1826 srv_conn = cs_conn(objt_cs(s->si[1].end));
1827 if (srv_conn) {
1828 struct ist hdr;
1829 struct http_hdr_ctx ctx;
1830
1831 if (txn->status == 401)
1832 hdr = ist("WWW-Authenticate");
1833 else if (txn->status == 407)
1834 hdr = ist("Proxy-Authenticate");
1835 else
1836 goto end;
1837
1838 ctx.blk = NULL;
1839 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001840 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1841 * possibly followed by blanks and a base64 string, the connection
1842 * is private. Since it's a mess to deal with, we only check for
1843 * values starting with "NTLM" or "Nego". Note that often multiple
1844 * headers are sent by the server there.
1845 */
1846 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001847 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001848 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001849 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001850 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001851 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001852 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001853 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001854 }
Christopher Faulet61608322018-11-23 16:23:45 +01001855 }
1856 }
1857
1858 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001859 /* we want to have the response time before we start processing it */
1860 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1861
1862 /* end of job, return OK */
1863 rep->analysers &= ~an_bit;
1864 rep->analyse_exp = TICK_ETERNITY;
1865 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001866 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001867 return 1;
1868
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001869 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001870 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001871 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001872 if (sess->listener->counters)
1873 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001874 if (objt_server(s->target))
1875 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001876 txn->status = 500;
1877 if (!(s->flags & SF_ERR_MASK))
1878 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001879 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001880
1881 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001882 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001883 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001884 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001885 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001886 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001887 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001888 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001889 do_l7_retry(s, si_b) == 0) {
1890 DBG_TRACE_DEVEL("leaving on L7 retry",
1891 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001892 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001893 }
Christopher Faulet47365272018-10-31 17:40:50 +01001894 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001895 /* fall through */
1896
Christopher Fauletb8a53712019-12-16 11:29:38 +01001897 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001898 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001899
1900 if (!(s->flags & SF_ERR_MASK))
1901 s->flags |= SF_ERR_PRXCOND;
1902 if (!(s->flags & SF_FINST_MASK))
1903 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001904
1905 s->si[1].flags |= SI_FL_NOLINGER;
1906 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001907 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001908 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001909 DBG_TRACE_DEVEL("leaving on error",
1910 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001911 return 0;
1912
Christopher Faulete0768eb2018-10-03 16:38:02 +02001913 abort_keep_alive:
1914 /* A keep-alive request to the server failed on a network error.
1915 * The client is required to retry. We need to close without returning
1916 * any other information so that the client retries.
1917 */
1918 txn->status = 0;
1919 rep->analysers &= AN_RES_FLT_END;
1920 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001921 s->logs.logwait = 0;
1922 s->logs.level = 0;
1923 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001924 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001925 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1926 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001927 return 0;
1928}
1929
1930/* This function performs all the processing enabled for the current response.
1931 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1932 * and updates s->res.analysers. It might make sense to explode it into several
1933 * other functions. It works like process_request (see indications above).
1934 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001935int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001936{
1937 struct session *sess = s->sess;
1938 struct http_txn *txn = s->txn;
1939 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001940 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001941 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001942 enum rule_result ret = HTTP_RULE_RES_CONT;
1943
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001944 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1945 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001946
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001947 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001948
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001949 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001950
1951 /* The stats applet needs to adjust the Connection header but we don't
1952 * apply any filter there.
1953 */
1954 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1955 rep->analysers &= ~an_bit;
1956 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001957 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001958 }
1959
1960 /*
1961 * We will have to evaluate the filters.
1962 * As opposed to version 1.2, now they will be evaluated in the
1963 * filters order and not in the header order. This means that
1964 * each filter has to be validated among all headers.
1965 *
1966 * Filters are tried with ->be first, then with ->fe if it is
1967 * different from ->be.
1968 *
1969 * Maybe we are in resume condiion. In this case I choose the
1970 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001971 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001972 * the process with the first one.
1973 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001974 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001975 * pointer and the ->fe rule list. If it doesn't match, I initialize
1976 * the loop with the ->be.
1977 */
1978 if (s->current_rule_list == &sess->fe->http_res_rules)
1979 cur_proxy = sess->fe;
1980 else
1981 cur_proxy = s->be;
1982 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001983 /* evaluate http-response rules */
1984 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001985 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001986
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001987 switch (ret) {
1988 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1989 goto return_prx_yield;
1990
1991 case HTTP_RULE_RES_CONT:
1992 case HTTP_RULE_RES_STOP: /* nothing to do */
1993 break;
1994
1995 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1996 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001997
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001998 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1999 goto return_prx_cond;
2000
2001 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01002002 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002003
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002004 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2005 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002006
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002007 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
2008 goto return_int_err;
2009 }
2010
2011 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002012
Christopher Faulete0768eb2018-10-03 16:38:02 +02002013 /* check whether we're already working on the frontend */
2014 if (cur_proxy == sess->fe)
2015 break;
2016 cur_proxy = sess->fe;
2017 }
2018
Christopher Faulete0768eb2018-10-03 16:38:02 +02002019 /* OK that's all we can do for 1xx responses */
2020 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002021 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002022
2023 /*
2024 * Now check for a server cookie.
2025 */
2026 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002027 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002028
2029 /*
2030 * Check for cache-control or pragma headers if required.
2031 */
2032 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002033 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002034
2035 /*
2036 * Add server cookie in the response if needed
2037 */
2038 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
2039 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
2040 (!(s->flags & SF_DIRECT) ||
2041 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
2042 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
2043 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
2044 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
2045 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
2046 !(s->flags & SF_IGNORE_PRST)) {
2047 /* the server is known, it's not the one the client requested, or the
2048 * cookie's last seen date needs to be refreshed. We have to
2049 * insert a set-cookie here, except if we want to insert only on POST
2050 * requests and this one isn't. Note that servers which don't have cookies
2051 * (eg: some backup servers) will return a full cookie removal request.
2052 */
2053 if (!objt_server(s->target)->cookie) {
2054 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002055 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02002056 s->be->cookie_name);
2057 }
2058 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002059 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002060
2061 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2062 /* emit last_date, which is mandatory */
2063 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2064 s30tob64((date.tv_sec+3) >> 2,
2065 trash.area + trash.data);
2066 trash.data += 5;
2067
2068 if (s->be->cookie_maxlife) {
2069 /* emit first_date, which is either the original one or
2070 * the current date.
2071 */
2072 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2073 s30tob64(txn->cookie_first_date ?
2074 txn->cookie_first_date >> 2 :
2075 (date.tv_sec+3) >> 2,
2076 trash.area + trash.data);
2077 trash.data += 5;
2078 }
2079 }
2080 chunk_appendf(&trash, "; path=/");
2081 }
2082
2083 if (s->be->cookie_domain)
2084 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2085
2086 if (s->be->ck_opts & PR_CK_HTTPONLY)
2087 chunk_appendf(&trash, "; HttpOnly");
2088
2089 if (s->be->ck_opts & PR_CK_SECURE)
2090 chunk_appendf(&trash, "; Secure");
2091
Christopher Faulet2f533902020-01-21 11:06:48 +01002092 if (s->be->cookie_attrs)
2093 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
2094
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002095 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002096 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002097
2098 txn->flags &= ~TX_SCK_MASK;
2099 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2100 /* the server did not change, only the date was updated */
2101 txn->flags |= TX_SCK_UPDATED;
2102 else
2103 txn->flags |= TX_SCK_INSERTED;
2104
2105 /* Here, we will tell an eventual cache on the client side that we don't
2106 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2107 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2108 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2109 */
2110 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2111
2112 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2113
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002114 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002115 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002116 }
2117 }
2118
2119 /*
2120 * Check if result will be cacheable with a cookie.
2121 * We'll block the response if security checks have caught
2122 * nasty things such as a cacheable cookie.
2123 */
2124 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2125 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2126 (s->be->options & PR_O_CHK_CACHE)) {
2127 /* we're in presence of a cacheable response containing
2128 * a set-cookie header. We'll block it as requested by
2129 * the 'checkcache' option, and send an alert.
2130 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002131 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2132 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2133 send_log(s->be, LOG_ALERT,
2134 "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>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002136 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002137 }
2138
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002139 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002140 /*
2141 * Evaluate after-response rules before forwarding the response. rules
2142 * from the backend are evaluated first, then one from the frontend if
2143 * it differs.
2144 */
2145 if (!http_eval_after_res_rules(s))
2146 goto return_int_err;
2147
Christopher Faulete0768eb2018-10-03 16:38:02 +02002148 /* Always enter in the body analyzer */
2149 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2150 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2151
2152 /* if the user wants to log as soon as possible, without counting
2153 * bytes from the server, then this is the right moment. We have
2154 * to temporarily assign bytes_out to log what we currently have.
2155 */
2156 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2157 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002158 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002159 s->do_log(s);
2160 s->logs.bytes_out = 0;
2161 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002162
Christopher Fauletb8a53712019-12-16 11:29:38 +01002163 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002164 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002165 rep->analysers &= ~an_bit;
2166 rep->analyse_exp = TICK_ETERNITY;
2167 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002168
Christopher Fauletb8a53712019-12-16 11:29:38 +01002169 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002170 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002171 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002172 if (sess->listener->counters)
2173 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002174 if (objt_server(s->target))
2175 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002176 goto return_prx_err;
2177
2178 return_int_err:
2179 txn->status = 500;
2180 if (!(s->flags & SF_ERR_MASK))
2181 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002182 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002183 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2184 if (objt_server(s->target))
2185 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002186 if (objt_server(s->target))
2187 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002188 goto return_prx_err;
2189
2190 return_bad_res:
2191 txn->status = 502;
Christopher Fauleta20a6532020-02-05 10:16:41 +01002192 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2193 if (objt_server(s->target)) {
2194 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2195 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2196 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002197 /* fall through */
2198
2199 return_prx_err:
2200 http_reply_and_close(s, txn->status, http_error_message(s));
2201 /* fall through */
2202
2203 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002204 s->logs.t_data = -1; /* was not a valid response */
2205 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002206
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002207 if (!(s->flags & SF_ERR_MASK))
2208 s->flags |= SF_ERR_PRXCOND;
2209 if (!(s->flags & SF_FINST_MASK))
2210 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002211
Christopher Faulete58c0002020-03-02 16:21:01 +01002212 rep->analysers &= AN_RES_FLT_END;
2213 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002214 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002215 DBG_TRACE_DEVEL("leaving on error",
2216 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002217 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002218
2219 return_prx_yield:
2220 channel_dont_close(rep);
2221 DBG_TRACE_DEVEL("waiting for more data",
2222 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2223 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002224}
2225
2226/* This function is an analyser which forwards response body (including chunk
2227 * sizes if any). It is called as soon as we must forward, even if we forward
2228 * zero byte. The only situation where it must not be called is when we're in
2229 * tunnel mode and we want to forward till the close. It's used both to forward
2230 * remaining data and to resync after end of body. It expects the msg_state to
2231 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2232 * read more data, or 1 once we can go on with next request or end the stream.
2233 *
2234 * It is capable of compressing response data both in content-length mode and
2235 * in chunked mode. The state machines follows different flows depending on
2236 * whether content-length and chunked modes are used, since there are no
2237 * trailers in content-length :
2238 *
2239 * chk-mode cl-mode
2240 * ,----- BODY -----.
2241 * / \
2242 * V size > 0 V chk-mode
2243 * .--> SIZE -------------> DATA -------------> CRLF
2244 * | | size == 0 | last byte |
2245 * | v final crlf v inspected |
2246 * | TRAILERS -----------> DONE |
2247 * | |
2248 * `----------------------------------------------'
2249 *
2250 * Compression only happens in the DATA state, and must be flushed in final
2251 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2252 * is performed at once on final states for all bytes parsed, or when leaving
2253 * on missing data.
2254 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002255int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002256{
2257 struct session *sess = s->sess;
2258 struct http_txn *txn = s->txn;
2259 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002260 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002261 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002262
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002263 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002264
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002265 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002266
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002267 if (htx->flags & HTX_FL_PARSING_ERROR)
2268 goto return_bad_res;
2269 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2270 goto return_int_err;
2271
Christopher Faulete0768eb2018-10-03 16:38:02 +02002272 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002273 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002274 /* Output closed while we were sending data. We must abort and
2275 * wake the other side up.
2276 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002277 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002278 http_end_response(s);
2279 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002280 DBG_TRACE_DEVEL("leaving on error",
2281 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002282 return 1;
2283 }
2284
Christopher Faulet9768c262018-10-22 09:34:31 +02002285 if (msg->msg_state == HTTP_MSG_BODY)
2286 msg->msg_state = HTTP_MSG_DATA;
2287
Christopher Faulete0768eb2018-10-03 16:38:02 +02002288 /* in most states, we should abort in case of early close */
2289 channel_auto_close(res);
2290
Christopher Faulete0768eb2018-10-03 16:38:02 +02002291 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002292 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002293 if (res->flags & CF_EOI)
2294 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002295 }
2296 else {
2297 /* We can't process the buffer's contents yet */
2298 res->flags |= CF_WAKE_WRITE;
2299 goto missing_data_or_waiting;
2300 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002301 }
2302
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002303 if (msg->msg_state >= HTTP_MSG_ENDING)
2304 goto ending;
2305
2306 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2307 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2308 msg->msg_state = HTTP_MSG_ENDING;
2309 goto ending;
2310 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002311
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002312 /* Forward input data. We get it by removing all outgoing data not
2313 * forwarded yet from HTX data size. If there are some data filters, we
2314 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002315 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002316 if (HAS_RSP_DATA_FILTERS(s)) {
2317 ret = flt_http_payload(s, msg, htx->data);
2318 if (ret < 0)
2319 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002320 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002321 }
2322 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002323 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002324 if (msg->flags & HTTP_MSGF_XFER_LEN)
2325 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002326 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002327
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002328 if (htx->data != co_data(res))
2329 goto missing_data_or_waiting;
2330
2331 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2332 msg->msg_state = HTTP_MSG_ENDING;
2333 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002334 }
2335
Christopher Faulet9768c262018-10-22 09:34:31 +02002336 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002337 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2338 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002339 */
2340 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2341 goto missing_data_or_waiting;
2342
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002343 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002344
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002345 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002346 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2347
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002348 /* other states, ENDING...TUNNEL */
2349 if (msg->msg_state >= HTTP_MSG_DONE)
2350 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002351
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002352 if (HAS_RSP_DATA_FILTERS(s)) {
2353 ret = flt_http_end(s, msg);
2354 if (ret <= 0) {
2355 if (!ret)
2356 goto missing_data_or_waiting;
2357 goto return_bad_res;
2358 }
2359 }
2360
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002361 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2362 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2363 msg->msg_state = HTTP_MSG_TUNNEL;
2364 goto ending;
2365 }
2366 else {
2367 msg->msg_state = HTTP_MSG_DONE;
2368 res->to_forward = 0;
2369 }
2370
2371 done:
2372
2373 channel_dont_close(res);
2374
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002375 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002376 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002377 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002378 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2379 if (res->flags & CF_SHUTW) {
2380 /* response errors are most likely due to the
2381 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002382 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002383 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002384 goto return_bad_res;
2385 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002386 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002387 return 1;
2388 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002389 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2390 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002391 return 0;
2392
2393 missing_data_or_waiting:
2394 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002395 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002396
2397 /* stop waiting for data if the input is closed before the end. If the
2398 * client side was already closed, it means that the client has aborted,
2399 * so we don't want to count this as a server abort. Otherwise it's a
2400 * server abort.
2401 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002402 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002403 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002404 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002405 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002406 if (htx_is_empty(htx))
2407 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002408 }
2409
Christopher Faulete0768eb2018-10-03 16:38:02 +02002410 /* When TE: chunked is used, we need to get there again to parse
2411 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002412 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2413 * are filters registered on the stream, we don't want to forward a
2414 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002415 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002416 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002417 channel_dont_close(res);
2418
2419 /* We know that more data are expected, but we couldn't send more that
2420 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2421 * system knows it must not set a PUSH on this first part. Interactive
2422 * modes are already handled by the stream sock layer. We must not do
2423 * this in content-length mode because it could present the MSG_MORE
2424 * flag with the last block of forwarded data, which would cause an
2425 * additional delay to be observed by the receiver.
2426 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002427 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002428 res->flags |= CF_EXPECT_MORE;
2429
2430 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002431 DBG_TRACE_DEVEL("waiting for more data to forward",
2432 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002433 return 0;
2434
Christopher Faulet93e02d82019-03-08 14:18:50 +01002435 return_srv_abort:
2436 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2437 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002438 if (sess->listener->counters)
2439 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002440 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002441 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002442 if (!(s->flags & SF_ERR_MASK))
2443 s->flags |= SF_ERR_SRVCL;
2444 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002445
Christopher Faulet93e02d82019-03-08 14:18:50 +01002446 return_cli_abort:
2447 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2448 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002449 if (sess->listener->counters)
2450 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002451 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002452 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002453 if (!(s->flags & SF_ERR_MASK))
2454 s->flags |= SF_ERR_CLICL;
2455 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002456
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002457 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002458 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002459 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002460 if (sess->listener->counters)
2461 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002462 if (objt_server(s->target))
2463 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002464 if (!(s->flags & SF_ERR_MASK))
2465 s->flags |= SF_ERR_INTERNAL;
2466 goto return_error;
2467
Christopher Faulet93e02d82019-03-08 14:18:50 +01002468 return_bad_res:
2469 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2470 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002471 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002472 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2473 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002474 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002475 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002476 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002477
Christopher Faulet93e02d82019-03-08 14:18:50 +01002478 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002479 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002480 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002481 res->analysers &= AN_RES_FLT_END;
2482 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 +02002483 if (!(s->flags & SF_FINST_MASK))
2484 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002485 DBG_TRACE_DEVEL("leaving on error",
2486 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002487 return 0;
2488}
2489
Christopher Fauletf2824e62018-10-01 12:12:37 +02002490/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002491 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002492 * as too large a request to build a valid response.
2493 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002494int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002495{
Christopher Faulet99daf282018-11-28 22:58:13 +01002496 struct channel *req = &s->req;
2497 struct channel *res = &s->res;
2498 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002499 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002500 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002501 struct ist status, reason, location;
2502 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002503 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002504
2505 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002506 if (!chunk) {
2507 if (!(s->flags & SF_ERR_MASK))
2508 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002509 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002510 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002511
Christopher Faulet99daf282018-11-28 22:58:13 +01002512 /*
2513 * Create the location
2514 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002515 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002516 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002517 case REDIRECT_TYPE_SCHEME: {
2518 struct http_hdr_ctx ctx;
2519 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002520
Christopher Faulet99daf282018-11-28 22:58:13 +01002521 host = ist("");
2522 ctx.blk = NULL;
2523 if (http_find_header(htx, ist("Host"), &ctx, 0))
2524 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002525
Christopher Faulet297fbb42019-05-13 14:41:27 +02002526 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002527 path = http_get_path(htx_sl_req_uri(sl));
2528 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002529 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002530 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2531 int qs = 0;
2532 while (qs < path.len) {
2533 if (*(path.ptr + qs) == '?') {
2534 path.len = qs;
2535 break;
2536 }
2537 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002538 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002539 }
2540 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002541 else
2542 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002543
Christopher Faulet99daf282018-11-28 22:58:13 +01002544 if (rule->rdr_str) { /* this is an old "redirect" rule */
2545 /* add scheme */
2546 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2547 goto fail;
2548 }
2549 else {
2550 /* add scheme with executing log format */
2551 chunk->data += build_logline(s, chunk->area + chunk->data,
2552 chunk->size - chunk->data,
2553 &rule->rdr_fmt);
2554 }
2555 /* add "://" + host + path */
2556 if (!chunk_memcat(chunk, "://", 3) ||
2557 !chunk_memcat(chunk, host.ptr, host.len) ||
2558 !chunk_memcat(chunk, path.ptr, path.len))
2559 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002560
Christopher Faulet99daf282018-11-28 22:58:13 +01002561 /* append a slash at the end of the location if needed and missing */
2562 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2563 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2564 if (chunk->data + 1 >= chunk->size)
2565 goto fail;
2566 chunk->area[chunk->data++] = '/';
2567 }
2568 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002569 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002570
Christopher Faulet99daf282018-11-28 22:58:13 +01002571 case REDIRECT_TYPE_PREFIX: {
2572 struct ist path;
2573
Christopher Faulet297fbb42019-05-13 14:41:27 +02002574 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002575 path = http_get_path(htx_sl_req_uri(sl));
2576 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002577 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002578 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2579 int qs = 0;
2580 while (qs < path.len) {
2581 if (*(path.ptr + qs) == '?') {
2582 path.len = qs;
2583 break;
2584 }
2585 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002586 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002587 }
2588 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002589 else
2590 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002591
Christopher Faulet99daf282018-11-28 22:58:13 +01002592 if (rule->rdr_str) { /* this is an old "redirect" rule */
2593 /* add prefix. Note that if prefix == "/", we don't want to
2594 * add anything, otherwise it makes it hard for the user to
2595 * configure a self-redirection.
2596 */
2597 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2598 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2599 goto fail;
2600 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002601 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002602 else {
2603 /* add prefix with executing log format */
2604 chunk->data += build_logline(s, chunk->area + chunk->data,
2605 chunk->size - chunk->data,
2606 &rule->rdr_fmt);
2607 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002608
Christopher Faulet99daf282018-11-28 22:58:13 +01002609 /* add path */
2610 if (!chunk_memcat(chunk, path.ptr, path.len))
2611 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002612
Christopher Faulet99daf282018-11-28 22:58:13 +01002613 /* append a slash at the end of the location if needed and missing */
2614 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2615 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2616 if (chunk->data + 1 >= chunk->size)
2617 goto fail;
2618 chunk->area[chunk->data++] = '/';
2619 }
2620 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002621 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002622 case REDIRECT_TYPE_LOCATION:
2623 default:
2624 if (rule->rdr_str) { /* this is an old "redirect" rule */
2625 /* add location */
2626 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2627 goto fail;
2628 }
2629 else {
2630 /* add location with executing log format */
2631 chunk->data += build_logline(s, chunk->area + chunk->data,
2632 chunk->size - chunk->data,
2633 &rule->rdr_fmt);
2634 }
2635 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002636 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002637 location = ist2(chunk->area, chunk->data);
2638
2639 /*
2640 * Create the 30x response
2641 */
2642 switch (rule->code) {
2643 case 308:
2644 status = ist("308");
2645 reason = ist("Permanent Redirect");
2646 break;
2647 case 307:
2648 status = ist("307");
2649 reason = ist("Temporary Redirect");
2650 break;
2651 case 303:
2652 status = ist("303");
2653 reason = ist("See Other");
2654 break;
2655 case 301:
2656 status = ist("301");
2657 reason = ist("Moved Permanently");
2658 break;
2659 case 302:
2660 default:
2661 status = ist("302");
2662 reason = ist("Found");
2663 break;
2664 }
2665
Christopher Faulet08e66462019-05-23 16:44:59 +02002666 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2667 close = 1;
2668
Christopher Faulet99daf282018-11-28 22:58:13 +01002669 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002670 /* Trim any possible response */
2671 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002672 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2673 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2674 if (!sl)
2675 goto fail;
2676 sl->info.res.status = rule->code;
2677 s->txn->status = rule->code;
2678
Christopher Faulet08e66462019-05-23 16:44:59 +02002679 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2680 goto fail;
2681
2682 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002683 !htx_add_header(htx, ist("Location"), location))
2684 goto fail;
2685
2686 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2687 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2688 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002689 }
2690
2691 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002692 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2693 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002694 }
2695
Christopher Faulet99daf282018-11-28 22:58:13 +01002696 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2697 goto fail;
2698
Kevin Zhu96b36392020-01-07 09:42:55 +01002699 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002700 if (!http_forward_proxy_resp(s, 1))
2701 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002702
Christopher Faulet60b33a52020-01-28 09:18:10 +01002703 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2704 /* let's log the request time */
2705 s->logs.tv_request = now;
2706 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002707
Christopher Faulet60b33a52020-01-28 09:18:10 +01002708 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2709 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2710 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002711
2712 if (!(s->flags & SF_ERR_MASK))
2713 s->flags |= SF_ERR_LOCAL;
2714 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002715 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002716
Christopher Faulet99daf282018-11-28 22:58:13 +01002717 free_trash_chunk(chunk);
2718 return 1;
2719
2720 fail:
2721 /* If an error occurred, remove the incomplete HTTP response from the
2722 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002723 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002724 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002725 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002726}
2727
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002728/* Replace all headers matching the name <name>. The header value is replaced if
2729 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2730 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2731 * values are evaluated one by one. It returns 0 on success and -1 on error.
2732 */
2733int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2734 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002735{
2736 struct http_hdr_ctx ctx;
2737 struct buffer *output = get_trash_chunk();
2738
Christopher Faulet72333522018-10-24 11:25:02 +02002739 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002740 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002741 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2742 continue;
2743
2744 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2745 if (output->data == -1)
2746 return -1;
2747 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2748 return -1;
2749 }
2750 return 0;
2751}
2752
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002753/* This function executes one of the set-{method,path,query,uri} actions. It
2754 * takes the string from the variable 'replace' with length 'len', then modifies
2755 * the relevant part of the request line accordingly. Then it updates various
2756 * pointers to the next elements which were moved, and the total buffer length.
2757 * It finds the action to be performed in p[2], previously filled by function
2758 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2759 * error, though this can be revisited when this code is finally exploited.
2760 *
2761 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002762 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002763 *
2764 * In query string case, the mark question '?' must be set at the start of the
2765 * string by the caller, event if the replacement query string is empty.
2766 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002767int http_req_replace_stline(int action, const char *replace, int len,
2768 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002769{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002770 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002771
2772 switch (action) {
2773 case 0: // method
2774 if (!http_replace_req_meth(htx, ist2(replace, len)))
2775 return -1;
2776 break;
2777
2778 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002779 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002780 return -1;
2781 break;
2782
2783 case 2: // query
2784 if (!http_replace_req_query(htx, ist2(replace, len)))
2785 return -1;
2786 break;
2787
2788 case 3: // uri
2789 if (!http_replace_req_uri(htx, ist2(replace, len)))
2790 return -1;
2791 break;
2792
Christopher Faulet312294f2020-09-02 17:17:44 +02002793 case 4: // path + query
2794 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2795 return -1;
2796 break;
2797
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002798 default:
2799 return -1;
2800 }
2801 return 0;
2802}
2803
2804/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002805 * variable <status> contains the new status code. This function never fails. It
2806 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002807 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002808int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002809{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002810 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002811 char *res;
2812
2813 chunk_reset(&trash);
2814 res = ultoa_o(status, trash.area, trash.size);
2815 trash.data = res - trash.area;
2816
2817 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002818 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002819 const char *str = http_get_reason(status);
2820 reason = ist2(str, strlen(str));
2821 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002822
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002823 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002824 return -1;
2825 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002826}
2827
Christopher Faulet3e964192018-10-24 11:39:23 +02002828/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2829 * transaction <txn>. Returns the verdict of the first rule that prevents
2830 * further processing of the request (auth, deny, ...), and defaults to
2831 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2832 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2833 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2834 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2835 * status.
2836 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002837static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002838 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002839{
2840 struct session *sess = strm_sess(s);
2841 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002842 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002843 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002844 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002845
Christopher Faulet3e964192018-10-24 11:39:23 +02002846 /* If "the current_rule_list" match the executed rule list, we are in
2847 * resume condition. If a resume is needed it is always in the action
2848 * and never in the ACL or converters. In this case, we initialise the
2849 * current rule, and go to the action execution point.
2850 */
2851 if (s->current_rule) {
2852 rule = s->current_rule;
2853 s->current_rule = NULL;
2854 if (s->current_rule_list == rules)
2855 goto resume_execution;
2856 }
2857 s->current_rule_list = rules;
2858
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002859 /* start the ruleset evaluation in strict mode */
2860 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002861
Christopher Faulet3e964192018-10-24 11:39:23 +02002862 list_for_each_entry(rule, rules, list) {
2863 /* check optional condition */
2864 if (rule->cond) {
2865 int ret;
2866
2867 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2868 ret = acl_pass(ret);
2869
2870 if (rule->cond->pol == ACL_COND_UNLESS)
2871 ret = !ret;
2872
2873 if (!ret) /* condition not matched */
2874 continue;
2875 }
2876
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002877 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002878 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002879 /* Always call the action function if defined */
2880 if (rule->action_ptr) {
2881 if ((s->req.flags & CF_READ_ERROR) ||
2882 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2883 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002884 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002885
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002886 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002887 case ACT_RET_CONT:
2888 break;
2889 case ACT_RET_STOP:
2890 rule_ret = HTTP_RULE_RES_STOP;
2891 goto end;
2892 case ACT_RET_YIELD:
2893 s->current_rule = rule;
2894 rule_ret = HTTP_RULE_RES_YIELD;
2895 goto end;
2896 case ACT_RET_ERR:
2897 rule_ret = HTTP_RULE_RES_ERROR;
2898 goto end;
2899 case ACT_RET_DONE:
2900 rule_ret = HTTP_RULE_RES_DONE;
2901 goto end;
2902 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002903 if (txn->status == -1)
2904 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002905 rule_ret = HTTP_RULE_RES_DENY;
2906 goto end;
2907 case ACT_RET_ABRT:
2908 rule_ret = HTTP_RULE_RES_ABRT;
2909 goto end;
2910 case ACT_RET_INV:
2911 rule_ret = HTTP_RULE_RES_BADREQ;
2912 goto end;
2913 }
2914 continue; /* eval the next rule */
2915 }
2916
2917 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002918 switch (rule->action) {
2919 case ACT_ACTION_ALLOW:
2920 rule_ret = HTTP_RULE_RES_STOP;
2921 goto end;
2922
2923 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002924 txn->status = rule->arg.http_reply->status;
2925 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002926 rule_ret = HTTP_RULE_RES_DENY;
2927 goto end;
2928
2929 case ACT_HTTP_REQ_TARPIT:
2930 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002931 txn->status = rule->arg.http_reply->status;
2932 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002933 rule_ret = HTTP_RULE_RES_DENY;
2934 goto end;
2935
Christopher Faulet3e964192018-10-24 11:39:23 +02002936 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002937 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002938 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002939 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002940 goto end;
2941
2942 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002943 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002944 break;
2945
2946 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002947 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002948 break;
2949
2950 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002951 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002952 break;
2953
2954 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002955 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002956 break;
2957
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002958 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002959 default:
2960 break;
2961 }
2962 }
2963
2964 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002965 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002966 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002967 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002968
Christopher Faulet3e964192018-10-24 11:39:23 +02002969 /* we reached the end of the rules, nothing to report */
2970 return rule_ret;
2971}
2972
2973/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2974 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2975 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2976 * is returned, the process can continue the evaluation of next rule list. If
2977 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2978 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002979 * must be returned. If *YIELD is returned, the caller must call again the
2980 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002981 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002982static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2983 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002984{
2985 struct session *sess = strm_sess(s);
2986 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002987 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002988 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002989 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002990
Christopher Faulet3e964192018-10-24 11:39:23 +02002991 /* If "the current_rule_list" match the executed rule list, we are in
2992 * resume condition. If a resume is needed it is always in the action
2993 * and never in the ACL or converters. In this case, we initialise the
2994 * current rule, and go to the action execution point.
2995 */
2996 if (s->current_rule) {
2997 rule = s->current_rule;
2998 s->current_rule = NULL;
2999 if (s->current_rule_list == rules)
3000 goto resume_execution;
3001 }
3002 s->current_rule_list = rules;
3003
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003004 /* start the ruleset evaluation in strict mode */
3005 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003006
Christopher Faulet3e964192018-10-24 11:39:23 +02003007 list_for_each_entry(rule, rules, list) {
3008 /* check optional condition */
3009 if (rule->cond) {
3010 int ret;
3011
3012 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3013 ret = acl_pass(ret);
3014
3015 if (rule->cond->pol == ACL_COND_UNLESS)
3016 ret = !ret;
3017
3018 if (!ret) /* condition not matched */
3019 continue;
3020 }
3021
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003022 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02003023resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003024
3025 /* Always call the action function if defined */
3026 if (rule->action_ptr) {
3027 if ((s->req.flags & CF_READ_ERROR) ||
3028 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3029 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003030 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003031
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003032 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003033 case ACT_RET_CONT:
3034 break;
3035 case ACT_RET_STOP:
3036 rule_ret = HTTP_RULE_RES_STOP;
3037 goto end;
3038 case ACT_RET_YIELD:
3039 s->current_rule = rule;
3040 rule_ret = HTTP_RULE_RES_YIELD;
3041 goto end;
3042 case ACT_RET_ERR:
3043 rule_ret = HTTP_RULE_RES_ERROR;
3044 goto end;
3045 case ACT_RET_DONE:
3046 rule_ret = HTTP_RULE_RES_DONE;
3047 goto end;
3048 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01003049 if (txn->status == -1)
3050 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003051 rule_ret = HTTP_RULE_RES_DENY;
3052 goto end;
3053 case ACT_RET_ABRT:
3054 rule_ret = HTTP_RULE_RES_ABRT;
3055 goto end;
3056 case ACT_RET_INV:
3057 rule_ret = HTTP_RULE_RES_BADREQ;
3058 goto end;
3059 }
3060 continue; /* eval the next rule */
3061 }
3062
3063 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003064 switch (rule->action) {
3065 case ACT_ACTION_ALLOW:
3066 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3067 goto end;
3068
3069 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02003070 txn->status = rule->arg.http_reply->status;
3071 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003072 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003073 goto end;
3074
3075 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003076 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003077 break;
3078
3079 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003080 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003081 break;
3082
3083 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003084 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003085 break;
3086
3087 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003088 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003089 break;
3090
Christopher Faulet3e964192018-10-24 11:39:23 +02003091 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003092 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003093 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003094 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003095 goto end;
3096
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003097 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003098 default:
3099 break;
3100 }
3101 }
3102
3103 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003104 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003105 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003106 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003107
Christopher Faulet3e964192018-10-24 11:39:23 +02003108 /* we reached the end of the rules, nothing to report */
3109 return rule_ret;
3110}
3111
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003112/* Executes backend and frontend http-after-response rules for the stream <s>,
3113 * in that order. it return 1 on success and 0 on error. It is the caller
3114 * responsibility to catch error or ignore it. If it catches it, this function
3115 * may be called a second time, for the internal error.
3116 */
3117int http_eval_after_res_rules(struct stream *s)
3118{
3119 struct session *sess = s->sess;
3120 enum rule_result ret = HTTP_RULE_RES_CONT;
3121
Christopher Faulet507479b2020-05-15 12:29:46 +02003122 /* Eval after-response ruleset only if the reply is not const */
3123 if (s->txn->flags & TX_CONST_REPLY)
3124 goto end;
3125
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003126 /* prune the request variables if not already done and swap to the response variables. */
3127 if (s->vars_reqres.scope != SCOPE_RES) {
3128 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3129 vars_prune(&s->vars_reqres, s->sess, s);
3130 vars_init(&s->vars_reqres, SCOPE_RES);
3131 }
3132
3133 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3134 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3135 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3136
Christopher Faulet507479b2020-05-15 12:29:46 +02003137 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003138 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3139 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3140}
3141
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003142/*
3143 * Manage client-side cookie. It can impact performance by about 2% so it is
3144 * desirable to call it only when needed. This code is quite complex because
3145 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3146 * highly recommended not to touch this part without a good reason !
3147 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003148static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003149{
3150 struct session *sess = s->sess;
3151 struct http_txn *txn = s->txn;
3152 struct htx *htx;
3153 struct http_hdr_ctx ctx;
3154 char *hdr_beg, *hdr_end, *del_from;
3155 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3156 int preserve_hdr;
3157
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003158 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003159 ctx.blk = NULL;
3160 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003161 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003162 del_from = NULL; /* nothing to be deleted */
3163 preserve_hdr = 0; /* assume we may kill the whole header */
3164
3165 /* Now look for cookies. Conforming to RFC2109, we have to support
3166 * attributes whose name begin with a '$', and associate them with
3167 * the right cookie, if we want to delete this cookie.
3168 * So there are 3 cases for each cookie read :
3169 * 1) it's a special attribute, beginning with a '$' : ignore it.
3170 * 2) it's a server id cookie that we *MAY* want to delete : save
3171 * some pointers on it (last semi-colon, beginning of cookie...)
3172 * 3) it's an application cookie : we *MAY* have to delete a previous
3173 * "special" cookie.
3174 * At the end of loop, if a "special" cookie remains, we may have to
3175 * remove it. If no application cookie persists in the header, we
3176 * *MUST* delete it.
3177 *
3178 * Note: RFC2965 is unclear about the processing of spaces around
3179 * the equal sign in the ATTR=VALUE form. A careful inspection of
3180 * the RFC explicitly allows spaces before it, and not within the
3181 * tokens (attrs or values). An inspection of RFC2109 allows that
3182 * too but section 10.1.3 lets one think that spaces may be allowed
3183 * after the equal sign too, resulting in some (rare) buggy
3184 * implementations trying to do that. So let's do what servers do.
3185 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3186 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003187 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003188 * causes parsing to become ambiguous. Browsers also allow spaces
3189 * within values even without quotes.
3190 *
3191 * We have to keep multiple pointers in order to support cookie
3192 * removal at the beginning, middle or end of header without
3193 * corrupting the header. All of these headers are valid :
3194 *
3195 * hdr_beg hdr_end
3196 * | |
3197 * v |
3198 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3199 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3200 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3201 * | | | | | | |
3202 * | | | | | | |
3203 * | | | | | | +--> next
3204 * | | | | | +----> val_end
3205 * | | | | +-----------> val_beg
3206 * | | | +--------------> equal
3207 * | | +----------------> att_end
3208 * | +---------------------> att_beg
3209 * +--------------------------> prev
3210 *
3211 */
3212 hdr_beg = ctx.value.ptr;
3213 hdr_end = hdr_beg + ctx.value.len;
3214 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3215 /* Iterate through all cookies on this line */
3216
3217 /* find att_beg */
3218 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003219 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003220 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003221 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003222
3223 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3224 att_beg++;
3225
3226 /* find att_end : this is the first character after the last non
3227 * space before the equal. It may be equal to hdr_end.
3228 */
3229 equal = att_end = att_beg;
3230 while (equal < hdr_end) {
3231 if (*equal == '=' || *equal == ',' || *equal == ';')
3232 break;
3233 if (HTTP_IS_SPHT(*equal++))
3234 continue;
3235 att_end = equal;
3236 }
3237
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003238 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003239 * is between <att_beg> and <equal>, both may be identical.
3240 */
3241 /* look for end of cookie if there is an equal sign */
3242 if (equal < hdr_end && *equal == '=') {
3243 /* look for the beginning of the value */
3244 val_beg = equal + 1;
3245 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3246 val_beg++;
3247
3248 /* find the end of the value, respecting quotes */
3249 next = http_find_cookie_value_end(val_beg, hdr_end);
3250
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003251 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003252 val_end = next;
3253 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3254 val_end--;
3255 }
3256 else
3257 val_beg = val_end = next = equal;
3258
3259 /* We have nothing to do with attributes beginning with
3260 * '$'. However, they will automatically be removed if a
3261 * header before them is removed, since they're supposed
3262 * to be linked together.
3263 */
3264 if (*att_beg == '$')
3265 continue;
3266
3267 /* Ignore cookies with no equal sign */
3268 if (equal == next) {
3269 /* This is not our cookie, so we must preserve it. But if we already
3270 * scheduled another cookie for removal, we cannot remove the
3271 * complete header, but we can remove the previous block itself.
3272 */
3273 preserve_hdr = 1;
3274 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003275 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003276 val_end += delta;
3277 next += delta;
3278 hdr_end += delta;
3279 prev = del_from;
3280 del_from = NULL;
3281 }
3282 continue;
3283 }
3284
3285 /* if there are spaces around the equal sign, we need to
3286 * strip them otherwise we'll get trouble for cookie captures,
3287 * or even for rewrites. Since this happens extremely rarely,
3288 * it does not hurt performance.
3289 */
3290 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3291 int stripped_before = 0;
3292 int stripped_after = 0;
3293
3294 if (att_end != equal) {
3295 memmove(att_end, equal, hdr_end - equal);
3296 stripped_before = (att_end - equal);
3297 equal += stripped_before;
3298 val_beg += stripped_before;
3299 }
3300
3301 if (val_beg > equal + 1) {
3302 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3303 stripped_after = (equal + 1) - val_beg;
3304 val_beg += stripped_after;
3305 stripped_before += stripped_after;
3306 }
3307
3308 val_end += stripped_before;
3309 next += stripped_before;
3310 hdr_end += stripped_before;
3311 }
3312 /* now everything is as on the diagram above */
3313
3314 /* First, let's see if we want to capture this cookie. We check
3315 * that we don't already have a client side cookie, because we
3316 * can only capture one. Also as an optimisation, we ignore
3317 * cookies shorter than the declared name.
3318 */
3319 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3320 (val_end - att_beg >= sess->fe->capture_namelen) &&
3321 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3322 int log_len = val_end - att_beg;
3323
3324 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3325 ha_alert("HTTP logging : out of memory.\n");
3326 } else {
3327 if (log_len > sess->fe->capture_len)
3328 log_len = sess->fe->capture_len;
3329 memcpy(txn->cli_cookie, att_beg, log_len);
3330 txn->cli_cookie[log_len] = 0;
3331 }
3332 }
3333
3334 /* Persistence cookies in passive, rewrite or insert mode have the
3335 * following form :
3336 *
3337 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3338 *
3339 * For cookies in prefix mode, the form is :
3340 *
3341 * Cookie: NAME=SRV~VALUE
3342 */
3343 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3344 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3345 struct server *srv = s->be->srv;
3346 char *delim;
3347
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003348 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003349 * have the server ID between val_beg and delim, and the original cookie between
3350 * delim+1 and val_end. Otherwise, delim==val_end :
3351 *
3352 * hdr_beg
3353 * |
3354 * v
3355 * NAME=SRV; # in all but prefix modes
3356 * NAME=SRV~OPAQUE ; # in prefix mode
3357 * || || | |+-> next
3358 * || || | +--> val_end
3359 * || || +---------> delim
3360 * || |+------------> val_beg
3361 * || +-------------> att_end = equal
3362 * |+-----------------> att_beg
3363 * +------------------> prev
3364 *
3365 */
3366 if (s->be->ck_opts & PR_CK_PFX) {
3367 for (delim = val_beg; delim < val_end; delim++)
3368 if (*delim == COOKIE_DELIM)
3369 break;
3370 }
3371 else {
3372 char *vbar1;
3373 delim = val_end;
3374 /* Now check if the cookie contains a date field, which would
3375 * appear after a vertical bar ('|') just after the server name
3376 * and before the delimiter.
3377 */
3378 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3379 if (vbar1) {
3380 /* OK, so left of the bar is the server's cookie and
3381 * right is the last seen date. It is a base64 encoded
3382 * 30-bit value representing the UNIX date since the
3383 * epoch in 4-second quantities.
3384 */
3385 int val;
3386 delim = vbar1++;
3387 if (val_end - vbar1 >= 5) {
3388 val = b64tos30(vbar1);
3389 if (val > 0)
3390 txn->cookie_last_date = val << 2;
3391 }
3392 /* look for a second vertical bar */
3393 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3394 if (vbar1 && (val_end - vbar1 > 5)) {
3395 val = b64tos30(vbar1 + 1);
3396 if (val > 0)
3397 txn->cookie_first_date = val << 2;
3398 }
3399 }
3400 }
3401
3402 /* if the cookie has an expiration date and the proxy wants to check
3403 * it, then we do that now. We first check if the cookie is too old,
3404 * then only if it has expired. We detect strict overflow because the
3405 * time resolution here is not great (4 seconds). Cookies with dates
3406 * in the future are ignored if their offset is beyond one day. This
3407 * allows an admin to fix timezone issues without expiring everyone
3408 * and at the same time avoids keeping unwanted side effects for too
3409 * long.
3410 */
3411 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3412 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3413 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3414 txn->flags &= ~TX_CK_MASK;
3415 txn->flags |= TX_CK_OLD;
3416 delim = val_beg; // let's pretend we have not found the cookie
3417 txn->cookie_first_date = 0;
3418 txn->cookie_last_date = 0;
3419 }
3420 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3421 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3422 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3423 txn->flags &= ~TX_CK_MASK;
3424 txn->flags |= TX_CK_EXPIRED;
3425 delim = val_beg; // let's pretend we have not found the cookie
3426 txn->cookie_first_date = 0;
3427 txn->cookie_last_date = 0;
3428 }
3429
3430 /* Here, we'll look for the first running server which supports the cookie.
3431 * This allows to share a same cookie between several servers, for example
3432 * to dedicate backup servers to specific servers only.
3433 * However, to prevent clients from sticking to cookie-less backup server
3434 * when they have incidentely learned an empty cookie, we simply ignore
3435 * empty cookies and mark them as invalid.
3436 * The same behaviour is applied when persistence must be ignored.
3437 */
3438 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3439 srv = NULL;
3440
3441 while (srv) {
3442 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3443 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3444 if ((srv->cur_state != SRV_ST_STOPPED) ||
3445 (s->be->options & PR_O_PERSIST) ||
3446 (s->flags & SF_FORCE_PRST)) {
3447 /* we found the server and we can use it */
3448 txn->flags &= ~TX_CK_MASK;
3449 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3450 s->flags |= SF_DIRECT | SF_ASSIGNED;
3451 s->target = &srv->obj_type;
3452 break;
3453 } else {
3454 /* we found a server, but it's down,
3455 * mark it as such and go on in case
3456 * another one is available.
3457 */
3458 txn->flags &= ~TX_CK_MASK;
3459 txn->flags |= TX_CK_DOWN;
3460 }
3461 }
3462 srv = srv->next;
3463 }
3464
3465 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3466 /* no server matched this cookie or we deliberately skipped it */
3467 txn->flags &= ~TX_CK_MASK;
3468 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3469 txn->flags |= TX_CK_UNUSED;
3470 else
3471 txn->flags |= TX_CK_INVALID;
3472 }
3473
3474 /* depending on the cookie mode, we may have to either :
3475 * - delete the complete cookie if we're in insert+indirect mode, so that
3476 * the server never sees it ;
3477 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003478 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003479 * if we're in cookie prefix mode
3480 */
3481 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3482 int delta; /* negative */
3483
3484 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3485 delta = val_beg - (delim + 1);
3486 val_end += delta;
3487 next += delta;
3488 hdr_end += delta;
3489 del_from = NULL;
3490 preserve_hdr = 1; /* we want to keep this cookie */
3491 }
3492 else if (del_from == NULL &&
3493 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3494 del_from = prev;
3495 }
3496 }
3497 else {
3498 /* This is not our cookie, so we must preserve it. But if we already
3499 * scheduled another cookie for removal, we cannot remove the
3500 * complete header, but we can remove the previous block itself.
3501 */
3502 preserve_hdr = 1;
3503
3504 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003505 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003506 if (att_beg >= del_from)
3507 att_beg += delta;
3508 if (att_end >= del_from)
3509 att_end += delta;
3510 val_beg += delta;
3511 val_end += delta;
3512 next += delta;
3513 hdr_end += delta;
3514 prev = del_from;
3515 del_from = NULL;
3516 }
3517 }
3518
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003519 } /* for each cookie */
3520
3521
3522 /* There are no more cookies on this line.
3523 * We may still have one (or several) marked for deletion at the
3524 * end of the line. We must do this now in two ways :
3525 * - if some cookies must be preserved, we only delete from the
3526 * mark to the end of line ;
3527 * - if nothing needs to be preserved, simply delete the whole header
3528 */
3529 if (del_from) {
3530 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3531 }
3532 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003533 if (hdr_beg != hdr_end)
3534 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003535 else
3536 http_remove_header(htx, &ctx);
3537 }
3538 } /* for each "Cookie header */
3539}
3540
3541/*
3542 * Manage server-side cookies. It can impact performance by about 2% so it is
3543 * desirable to call it only when needed. This function is also used when we
3544 * just need to know if there is a cookie (eg: for check-cache).
3545 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003546static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003547{
3548 struct session *sess = s->sess;
3549 struct http_txn *txn = s->txn;
3550 struct htx *htx;
3551 struct http_hdr_ctx ctx;
3552 struct server *srv;
3553 char *hdr_beg, *hdr_end;
3554 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003555 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003556
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003557 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003558
3559 ctx.blk = NULL;
3560 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003561 int is_first = 1;
3562
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003563 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3564 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3565 break;
3566 is_cookie2 = 1;
3567 }
3568
3569 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3570 * <prev> points to the colon.
3571 */
3572 txn->flags |= TX_SCK_PRESENT;
3573
3574 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3575 * check-cache is enabled) and we are not interested in checking
3576 * them. Warning, the cookie capture is declared in the frontend.
3577 */
3578 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3579 break;
3580
3581 /* OK so now we know we have to process this response cookie.
3582 * The format of the Set-Cookie header is slightly different
3583 * from the format of the Cookie header in that it does not
3584 * support the comma as a cookie delimiter (thus the header
3585 * cannot be folded) because the Expires attribute described in
3586 * the original Netscape's spec may contain an unquoted date
3587 * with a comma inside. We have to live with this because
3588 * many browsers don't support Max-Age and some browsers don't
3589 * support quoted strings. However the Set-Cookie2 header is
3590 * clean.
3591 *
3592 * We have to keep multiple pointers in order to support cookie
3593 * removal at the beginning, middle or end of header without
3594 * corrupting the header (in case of set-cookie2). A special
3595 * pointer, <scav> points to the beginning of the set-cookie-av
3596 * fields after the first semi-colon. The <next> pointer points
3597 * either to the end of line (set-cookie) or next unquoted comma
3598 * (set-cookie2). All of these headers are valid :
3599 *
3600 * hdr_beg hdr_end
3601 * | |
3602 * v |
3603 * NAME1 = VALUE 1 ; Secure; Path="/" |
3604 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3605 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3606 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3607 * | | | | | | | |
3608 * | | | | | | | +-> next
3609 * | | | | | | +------------> scav
3610 * | | | | | +--------------> val_end
3611 * | | | | +--------------------> val_beg
3612 * | | | +----------------------> equal
3613 * | | +------------------------> att_end
3614 * | +----------------------------> att_beg
3615 * +------------------------------> prev
3616 * -------------------------------> hdr_beg
3617 */
3618 hdr_beg = ctx.value.ptr;
3619 hdr_end = hdr_beg + ctx.value.len;
3620 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3621
3622 /* Iterate through all cookies on this line */
3623
3624 /* find att_beg */
3625 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003626 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003627 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003628 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003629
3630 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3631 att_beg++;
3632
3633 /* find att_end : this is the first character after the last non
3634 * space before the equal. It may be equal to hdr_end.
3635 */
3636 equal = att_end = att_beg;
3637
3638 while (equal < hdr_end) {
3639 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3640 break;
3641 if (HTTP_IS_SPHT(*equal++))
3642 continue;
3643 att_end = equal;
3644 }
3645
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003646 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003647 * is between <att_beg> and <equal>, both may be identical.
3648 */
3649
3650 /* look for end of cookie if there is an equal sign */
3651 if (equal < hdr_end && *equal == '=') {
3652 /* look for the beginning of the value */
3653 val_beg = equal + 1;
3654 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3655 val_beg++;
3656
3657 /* find the end of the value, respecting quotes */
3658 next = http_find_cookie_value_end(val_beg, hdr_end);
3659
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003660 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003661 val_end = next;
3662 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3663 val_end--;
3664 }
3665 else {
3666 /* <equal> points to next comma, semi-colon or EOL */
3667 val_beg = val_end = next = equal;
3668 }
3669
3670 if (next < hdr_end) {
3671 /* Set-Cookie2 supports multiple cookies, and <next> points to
3672 * a colon or semi-colon before the end. So skip all attr-value
3673 * pairs and look for the next comma. For Set-Cookie, since
3674 * commas are permitted in values, skip to the end.
3675 */
3676 if (is_cookie2)
3677 next = http_find_hdr_value_end(next, hdr_end);
3678 else
3679 next = hdr_end;
3680 }
3681
3682 /* Now everything is as on the diagram above */
3683
3684 /* Ignore cookies with no equal sign */
3685 if (equal == val_end)
3686 continue;
3687
3688 /* If there are spaces around the equal sign, we need to
3689 * strip them otherwise we'll get trouble for cookie captures,
3690 * or even for rewrites. Since this happens extremely rarely,
3691 * it does not hurt performance.
3692 */
3693 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3694 int stripped_before = 0;
3695 int stripped_after = 0;
3696
3697 if (att_end != equal) {
3698 memmove(att_end, equal, hdr_end - equal);
3699 stripped_before = (att_end - equal);
3700 equal += stripped_before;
3701 val_beg += stripped_before;
3702 }
3703
3704 if (val_beg > equal + 1) {
3705 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3706 stripped_after = (equal + 1) - val_beg;
3707 val_beg += stripped_after;
3708 stripped_before += stripped_after;
3709 }
3710
3711 val_end += stripped_before;
3712 next += stripped_before;
3713 hdr_end += stripped_before;
3714
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003715 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003716 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003717 }
3718
3719 /* First, let's see if we want to capture this cookie. We check
3720 * that we don't already have a server side cookie, because we
3721 * can only capture one. Also as an optimisation, we ignore
3722 * cookies shorter than the declared name.
3723 */
3724 if (sess->fe->capture_name != NULL &&
3725 txn->srv_cookie == NULL &&
3726 (val_end - att_beg >= sess->fe->capture_namelen) &&
3727 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3728 int log_len = val_end - att_beg;
3729 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3730 ha_alert("HTTP logging : out of memory.\n");
3731 }
3732 else {
3733 if (log_len > sess->fe->capture_len)
3734 log_len = sess->fe->capture_len;
3735 memcpy(txn->srv_cookie, att_beg, log_len);
3736 txn->srv_cookie[log_len] = 0;
3737 }
3738 }
3739
3740 srv = objt_server(s->target);
3741 /* now check if we need to process it for persistence */
3742 if (!(s->flags & SF_IGNORE_PRST) &&
3743 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3744 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3745 /* assume passive cookie by default */
3746 txn->flags &= ~TX_SCK_MASK;
3747 txn->flags |= TX_SCK_FOUND;
3748
3749 /* If the cookie is in insert mode on a known server, we'll delete
3750 * this occurrence because we'll insert another one later.
3751 * We'll delete it too if the "indirect" option is set and we're in
3752 * a direct access.
3753 */
3754 if (s->be->ck_opts & PR_CK_PSV) {
3755 /* The "preserve" flag was set, we don't want to touch the
3756 * server's cookie.
3757 */
3758 }
3759 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3760 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3761 /* this cookie must be deleted */
3762 if (prev == hdr_beg && next == hdr_end) {
3763 /* whole header */
3764 http_remove_header(htx, &ctx);
3765 /* note: while both invalid now, <next> and <hdr_end>
3766 * are still equal, so the for() will stop as expected.
3767 */
3768 } else {
3769 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003770 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003771 next = prev;
3772 hdr_end += delta;
3773 }
3774 txn->flags &= ~TX_SCK_MASK;
3775 txn->flags |= TX_SCK_DELETED;
3776 /* and go on with next cookie */
3777 }
3778 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3779 /* replace bytes val_beg->val_end with the cookie name associated
3780 * with this server since we know it.
3781 */
3782 int sliding, delta;
3783
3784 ctx.value = ist2(val_beg, val_end - val_beg);
3785 ctx.lws_before = ctx.lws_after = 0;
3786 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3787 delta = srv->cklen - (val_end - val_beg);
3788 sliding = (ctx.value.ptr - val_beg);
3789 hdr_beg += sliding;
3790 val_beg += sliding;
3791 next += sliding + delta;
3792 hdr_end += sliding + delta;
3793
3794 txn->flags &= ~TX_SCK_MASK;
3795 txn->flags |= TX_SCK_REPLACED;
3796 }
3797 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3798 /* insert the cookie name associated with this server
3799 * before existing cookie, and insert a delimiter between them..
3800 */
3801 int sliding, delta;
3802 ctx.value = ist2(val_beg, 0);
3803 ctx.lws_before = ctx.lws_after = 0;
3804 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3805 delta = srv->cklen + 1;
3806 sliding = (ctx.value.ptr - val_beg);
3807 hdr_beg += sliding;
3808 val_beg += sliding;
3809 next += sliding + delta;
3810 hdr_end += sliding + delta;
3811
3812 val_beg[srv->cklen] = COOKIE_DELIM;
3813 txn->flags &= ~TX_SCK_MASK;
3814 txn->flags |= TX_SCK_REPLACED;
3815 }
3816 }
3817 /* that's done for this cookie, check the next one on the same
3818 * line when next != hdr_end (only if is_cookie2).
3819 */
3820 }
3821 }
3822}
3823
Christopher Faulet25a02f62018-10-24 12:00:25 +02003824/*
3825 * Parses the Cache-Control and Pragma request header fields to determine if
3826 * the request may be served from the cache and/or if it is cacheable. Updates
3827 * s->txn->flags.
3828 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003829void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003830{
3831 struct http_txn *txn = s->txn;
3832 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003833 struct http_hdr_ctx ctx = { .blk = NULL };
3834 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003835
3836 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3837 return; /* nothing more to do here */
3838
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003839 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003840 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003841
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003842 /* Check "pragma" header for HTTP/1.0 compatibility. */
3843 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3844 if (isteqi(ctx.value, ist("no-cache"))) {
3845 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003846 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003847 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003848
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003849 ctx.blk = NULL;
3850 /* Don't use the cache and don't try to store if we found the
3851 * Authorization header */
3852 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3853 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3854 txn->flags |= TX_CACHE_IGNORE;
3855 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003856
Christopher Faulet25a02f62018-10-24 12:00:25 +02003857
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003858 /* Look for "cache-control" header and iterate over all the values
3859 * until we find one that specifies that caching is possible or not. */
3860 ctx.blk = NULL;
3861 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003862 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003863 /* We don't check the values after max-age, max-stale nor min-fresh,
3864 * we simply don't use the cache when they're specified. */
3865 if (istmatchi(ctx.value, ist("max-age")) ||
3866 istmatchi(ctx.value, ist("no-cache")) ||
3867 istmatchi(ctx.value, ist("max-stale")) ||
3868 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003869 txn->flags |= TX_CACHE_IGNORE;
3870 continue;
3871 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003872 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003873 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3874 continue;
3875 }
3876 }
3877
3878 /* RFC7234#5.4:
3879 * When the Cache-Control header field is also present and
3880 * understood in a request, Pragma is ignored.
3881 * When the Cache-Control header field is not present in a
3882 * request, caches MUST consider the no-cache request
3883 * pragma-directive as having the same effect as if
3884 * "Cache-Control: no-cache" were present.
3885 */
3886 if (!cc_found && pragma_found)
3887 txn->flags |= TX_CACHE_IGNORE;
3888}
3889
3890/*
3891 * Check if response is cacheable or not. Updates s->txn->flags.
3892 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003893void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003894{
3895 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003896 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003897 struct htx *htx;
Remi Tricot-Le Breton87c2f252020-11-12 11:14:41 +01003898 int has_freshness_info = 0;
3899 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003900
3901 if (txn->status < 200) {
3902 /* do not try to cache interim responses! */
3903 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3904 return;
3905 }
3906
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003907 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003908 /* Check "pragma" header for HTTP/1.0 compatibility. */
3909 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3910 if (isteqi(ctx.value, ist("no-cache"))) {
3911 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3912 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003913 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003914 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003915
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003916 /* Look for "cache-control" header and iterate over all the values
3917 * until we find one that specifies that caching is possible or not. */
3918 ctx.blk = NULL;
3919 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3920 if (isteqi(ctx.value, ist("public"))) {
3921 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003922 continue;
3923 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003924 if (isteqi(ctx.value, ist("private")) ||
3925 isteqi(ctx.value, ist("no-cache")) ||
3926 isteqi(ctx.value, ist("no-store")) ||
3927 isteqi(ctx.value, ist("max-age=0")) ||
3928 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003929 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003930 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003931 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003932 /* We might have a no-cache="set-cookie" form. */
3933 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3934 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003935 continue;
3936 }
Remi Tricot-Le Breton87c2f252020-11-12 11:14:41 +01003937
3938 if (istmatchi(ctx.value, ist("s-maxage")) ||
3939 istmatchi(ctx.value, ist("max-age"))) {
3940 has_freshness_info = 1;
3941 continue;
3942 }
3943 }
3944
3945 /* If no freshness information could be found in Cache-Control values,
3946 * look for an Expires header. */
3947 if (!has_freshness_info) {
3948 ctx.blk = NULL;
3949 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003950 }
Remi Tricot-Le Breton87c2f252020-11-12 11:14:41 +01003951
3952 /* If no freshness information could be found in Cache-Control or Expires
3953 * values, look for an explicit validator. */
3954 if (!has_freshness_info) {
3955 ctx.blk = NULL;
3956 has_validator = 1;
3957 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3958 ctx.blk = NULL;
3959 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3960 has_validator = 0;
3961 }
3962 }
3963
3964 /* We won't store an entry that has neither a cache validator nor an
3965 * explicit expiration time, as suggested in RFC 7234#3. */
3966 if (!has_freshness_info && !has_validator)
3967 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003968}
3969
Christopher Faulet377c5a52018-10-24 21:21:30 +02003970/*
3971 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3972 * for the current backend.
3973 *
3974 * It is assumed that the request is either a HEAD, GET, or POST and that the
3975 * uri_auth field is valid.
3976 *
3977 * Returns 1 if stats should be provided, otherwise 0.
3978 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003979static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003980{
3981 struct uri_auth *uri_auth = backend->uri_auth;
3982 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003983 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003984 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003985
3986 if (!uri_auth)
3987 return 0;
3988
3989 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3990 return 0;
3991
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003992 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003993 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003994 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003995 if (*uri_auth->uri_prefix == '/')
3996 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003997
3998 /* check URI size */
3999 if (uri_auth->uri_len > uri.len)
4000 return 0;
4001
4002 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4003 return 0;
4004
4005 return 1;
4006}
4007
4008/* This function prepares an applet to handle the stats. It can deal with the
4009 * "100-continue" expectation, check that admin rules are met for POST requests,
4010 * and program a response message if something was unexpected. It cannot fail
4011 * and always relies on the stats applet to complete the job. It does not touch
4012 * analysers nor counters, which are left to the caller. It does not touch
4013 * s->target which is supposed to already point to the stats applet. The caller
4014 * is expected to have already assigned an appctx to the stream.
4015 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004016static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004017{
4018 struct stats_admin_rule *stats_admin_rule;
4019 struct stream_interface *si = &s->si[1];
4020 struct session *sess = s->sess;
4021 struct http_txn *txn = s->txn;
4022 struct http_msg *msg = &txn->req;
4023 struct uri_auth *uri_auth = s->be->uri_auth;
4024 const char *h, *lookup, *end;
4025 struct appctx *appctx;
4026 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004027 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004028
4029 appctx = si_appctx(si);
4030 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4031 appctx->st1 = appctx->st2 = 0;
4032 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004033 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004034 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4035 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4036 appctx->ctx.stats.flags |= STAT_CHUNKED;
4037
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004038 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004039 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004040 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4041 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004042
4043 for (h = lookup; h <= end - 3; h++) {
4044 if (memcmp(h, ";up", 3) == 0) {
4045 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4046 break;
4047 }
Amaury Denoyellee78aa872021-02-25 14:46:08 +01004048 }
4049
4050 for (h = lookup; h <= end - 9; h++) {
4051 if (memcmp(h, ";no-maint", 9) == 0) {
Willy Tarreau3e320362020-10-23 17:28:57 +02004052 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
4053 break;
4054 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004055 }
4056
4057 if (uri_auth->refresh) {
4058 for (h = lookup; h <= end - 10; h++) {
4059 if (memcmp(h, ";norefresh", 10) == 0) {
4060 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4061 break;
4062 }
4063 }
4064 }
4065
4066 for (h = lookup; h <= end - 4; h++) {
4067 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004068 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004069 break;
4070 }
4071 }
4072
4073 for (h = lookup; h <= end - 6; h++) {
4074 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004075 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004076 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4077 break;
4078 }
4079 }
4080
Christopher Faulet6338a082019-09-09 15:50:54 +02004081 for (h = lookup; h <= end - 5; h++) {
4082 if (memcmp(h, ";json", 5) == 0) {
4083 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4084 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4085 break;
4086 }
4087 }
4088
4089 for (h = lookup; h <= end - 12; h++) {
4090 if (memcmp(h, ";json-schema", 12) == 0) {
4091 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4092 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4093 break;
4094 }
4095 }
4096
Christopher Faulet377c5a52018-10-24 21:21:30 +02004097 for (h = lookup; h <= end - 8; h++) {
4098 if (memcmp(h, ";st=", 4) == 0) {
4099 int i;
4100 h += 4;
4101 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4102 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4103 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4104 appctx->ctx.stats.st_code = i;
4105 break;
4106 }
4107 }
4108 break;
4109 }
4110 }
4111
4112 appctx->ctx.stats.scope_str = 0;
4113 appctx->ctx.stats.scope_len = 0;
4114 for (h = lookup; h <= end - 8; h++) {
4115 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4116 int itx = 0;
4117 const char *h2;
4118 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4119 const char *err;
4120
4121 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4122 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004123 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4124 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004125 if (*h == ';' || *h == '&' || *h == ' ')
4126 break;
4127 itx++;
4128 h++;
4129 }
4130
4131 if (itx > STAT_SCOPE_TXT_MAXLEN)
4132 itx = STAT_SCOPE_TXT_MAXLEN;
4133 appctx->ctx.stats.scope_len = itx;
4134
4135 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4136 memcpy(scope_txt, h2, itx);
4137 scope_txt[itx] = '\0';
4138 err = invalid_char(scope_txt);
4139 if (err) {
4140 /* bad char in search text => clear scope */
4141 appctx->ctx.stats.scope_str = 0;
4142 appctx->ctx.stats.scope_len = 0;
4143 }
4144 break;
4145 }
4146 }
4147
4148 /* now check whether we have some admin rules for this request */
4149 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4150 int ret = 1;
4151
4152 if (stats_admin_rule->cond) {
4153 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4154 ret = acl_pass(ret);
4155 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4156 ret = !ret;
4157 }
4158
4159 if (ret) {
4160 /* no rule, or the rule matches */
4161 appctx->ctx.stats.flags |= STAT_ADMIN;
4162 break;
4163 }
4164 }
4165
Christopher Faulet5d45e382019-02-27 15:15:23 +01004166 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4167 appctx->st0 = STAT_HTTP_HEAD;
4168 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004169 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004170 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004171 if (msg->msg_state < HTTP_MSG_DATA)
4172 req->analysers |= AN_REQ_HTTP_BODY;
4173 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004174 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004175 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004176 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4177 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4178 appctx->st0 = STAT_HTTP_LAST;
4179 }
4180 }
4181 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004182 /* Unsupported method */
4183 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4184 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4185 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004186 }
4187
4188 s->task->nice = -32; /* small boost for HTTP statistics */
4189 return 1;
4190}
4191
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004192void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004193{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004194 struct channel *req = &s->req;
4195 struct channel *res = &s->res;
4196 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004197 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004198 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004199 struct ist path, location;
4200 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004201
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004202 /*
4203 * Create the location
4204 */
4205 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004206
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004207 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004208 /* special prefix "/" means don't change URL */
4209 srv = __objt_server(s->target);
4210 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4211 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4212 return;
4213 }
4214
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004215 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004216 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004217 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004218 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004219 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004220 return;
4221
4222 if (!chunk_memcat(&trash, path.ptr, path.len))
4223 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004224 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004225
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004226 /*
4227 * Create the 302 respone
4228 */
4229 htx = htx_from_buf(&res->buf);
4230 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4231 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4232 ist("HTTP/1.1"), ist("302"), ist("Found"));
4233 if (!sl)
4234 goto fail;
4235 sl->info.res.status = 302;
4236 s->txn->status = 302;
4237
4238 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4239 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4240 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4241 !htx_add_header(htx, ist("Location"), location))
4242 goto fail;
4243
4244 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4245 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004246
Christopher Fauletc20afb82020-01-24 19:16:26 +01004247 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004248 if (!http_forward_proxy_resp(s, 1))
4249 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004250
4251 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004252 si_shutr(si);
4253 si_shutw(si);
4254 si->err_type = SI_ET_NONE;
4255 si->state = SI_ST_CLO;
4256
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004257 if (!(s->flags & SF_ERR_MASK))
4258 s->flags |= SF_ERR_LOCAL;
4259 if (!(s->flags & SF_FINST_MASK))
4260 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004261
4262 /* FIXME: we should increase a counter of redirects per server and per backend. */
4263 srv_inc_sess_ctr(srv);
4264 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004265 return;
4266
4267 fail:
4268 /* If an error occurred, remove the incomplete HTTP response from the
4269 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004270 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004271}
4272
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004273/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004274 * because an error was triggered during the body forwarding.
4275 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004276static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004277{
4278 struct channel *chn = &s->req;
4279 struct http_txn *txn = s->txn;
4280
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004281 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004282
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004283 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4284 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004285 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004286 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004287 goto end;
4288 }
4289
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004290 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4291 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004292 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004293 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004294
4295 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004296 /* No need to read anymore, the request was completely parsed.
4297 * We can shut the read side unless we want to abort_on_close,
4298 * or we have a POST request. The issue with POST requests is
4299 * that some browsers still send a CRLF after the request, and
4300 * this CRLF must be read so that it does not remain in the kernel
4301 * buffers, otherwise a close could cause an RST on some systems
4302 * (eg: Linux).
4303 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004304 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004305 channel_dont_read(chn);
4306
4307 /* if the server closes the connection, we want to immediately react
4308 * and close the socket to save packets and syscalls.
4309 */
4310 s->si[1].flags |= SI_FL_NOHALF;
4311
4312 /* In any case we've finished parsing the request so we must
4313 * disable Nagle when sending data because 1) we're not going
4314 * to shut this side, and 2) the server is waiting for us to
4315 * send pending data.
4316 */
4317 chn->flags |= CF_NEVER_WAIT;
4318
Christopher Fauletd01ce402019-01-02 17:44:13 +01004319 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4320 /* The server has not finished to respond, so we
4321 * don't want to move in order not to upset it.
4322 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004323 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004324 return;
4325 }
4326
Christopher Fauletf2824e62018-10-01 12:12:37 +02004327 /* When we get here, it means that both the request and the
4328 * response have finished receiving. Depending on the connection
4329 * mode, we'll have to wait for the last bytes to leave in either
4330 * direction, and sometimes for a close to be effective.
4331 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004332 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004333 /* Tunnel mode will not have any analyser so it needs to
4334 * poll for reads.
4335 */
4336 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004337 if (b_data(&chn->buf)) {
4338 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004339 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004340 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004341 txn->req.msg_state = HTTP_MSG_TUNNEL;
4342 }
4343 else {
4344 /* we're not expecting any new data to come for this
4345 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004346 *
4347 * However, there is an exception if the response
4348 * length is undefined. In this case, we need to wait
4349 * the close from the server. The response will be
4350 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004351 */
4352 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4353 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004354 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004355
4356 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4357 channel_shutr_now(chn);
4358 channel_shutw_now(chn);
4359 }
4360 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004361 goto check_channel_flags;
4362 }
4363
4364 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4365 http_msg_closing:
4366 /* nothing else to forward, just waiting for the output buffer
4367 * to be empty and for the shutw_now to take effect.
4368 */
4369 if (channel_is_empty(chn)) {
4370 txn->req.msg_state = HTTP_MSG_CLOSED;
4371 goto http_msg_closed;
4372 }
4373 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004374 txn->req.msg_state = HTTP_MSG_ERROR;
4375 goto end;
4376 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004377 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004378 return;
4379 }
4380
4381 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4382 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004383 /* if we don't know whether the server will close, we need to hard close */
4384 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4385 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004386 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004387 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004388 channel_dont_read(chn);
4389 goto end;
4390 }
4391
4392 check_channel_flags:
4393 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4394 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4395 /* if we've just closed an output, let's switch */
4396 txn->req.msg_state = HTTP_MSG_CLOSING;
4397 goto http_msg_closing;
4398 }
4399
4400 end:
4401 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet0d4da8c2020-12-15 13:32:55 +01004402 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4403 chn->flags |= CF_NEVER_WAIT;
4404 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004405 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet0d4da8c2020-12-15 13:32:55 +01004406 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004407 channel_auto_close(chn);
4408 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004409 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004410}
4411
4412
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004413/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004414 * because an error was triggered during the body forwarding.
4415 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004416static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004417{
4418 struct channel *chn = &s->res;
4419 struct http_txn *txn = s->txn;
4420
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004421 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004422
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004423 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4424 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004425 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004426 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004427 goto end;
4428 }
4429
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004430 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4431 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004432 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004433 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004434
4435 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4436 /* In theory, we don't need to read anymore, but we must
4437 * still monitor the server connection for a possible close
4438 * while the request is being uploaded, so we don't disable
4439 * reading.
4440 */
4441 /* channel_dont_read(chn); */
4442
4443 if (txn->req.msg_state < HTTP_MSG_DONE) {
4444 /* The client seems to still be sending data, probably
4445 * because we got an error response during an upload.
4446 * We have the choice of either breaking the connection
4447 * or letting it pass through. Let's do the later.
4448 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004449 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004450 return;
4451 }
4452
4453 /* When we get here, it means that both the request and the
4454 * response have finished receiving. Depending on the connection
4455 * mode, we'll have to wait for the last bytes to leave in either
4456 * direction, and sometimes for a close to be effective.
4457 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004458 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004459 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004460 if (b_data(&chn->buf)) {
4461 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004462 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004463 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004464 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4465 }
4466 else {
4467 /* we're not expecting any new data to come for this
4468 * transaction, so we can close it.
4469 */
4470 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4471 channel_shutr_now(chn);
4472 channel_shutw_now(chn);
4473 }
4474 }
4475 goto check_channel_flags;
4476 }
4477
4478 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4479 http_msg_closing:
4480 /* nothing else to forward, just waiting for the output buffer
4481 * to be empty and for the shutw_now to take effect.
4482 */
4483 if (channel_is_empty(chn)) {
4484 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4485 goto http_msg_closed;
4486 }
4487 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004488 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004489 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004490 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004491 if (strm_sess(s)->listener->counters)
4492 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004493 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004494 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004495 goto end;
4496 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004497 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004498 return;
4499 }
4500
4501 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4502 http_msg_closed:
4503 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004504 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004505 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004506 goto end;
4507 }
4508
4509 check_channel_flags:
4510 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4511 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4512 /* if we've just closed an output, let's switch */
4513 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4514 goto http_msg_closing;
4515 }
4516
4517 end:
4518 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet0d4da8c2020-12-15 13:32:55 +01004519 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4520 chn->flags |= CF_NEVER_WAIT;
4521 if (HAS_RSP_DATA_FILTERS(s))
4522 chn->analysers |= AN_RES_FLT_XFER_DATA;
4523 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004524 channel_auto_close(chn);
4525 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004526 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004527}
4528
Christopher Fauletef70e252020-01-28 09:26:19 +01004529/* Forward a response generated by HAProxy (error/redirect/return). This
4530 * function forwards all pending incoming data. If <final> is set to 0, nothing
4531 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004532 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004533 * returned. If an error occurred, 0 is returned. If it fails, this function
4534 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004535 */
4536int http_forward_proxy_resp(struct stream *s, int final)
4537{
4538 struct channel *req = &s->req;
4539 struct channel *res = &s->res;
4540 struct htx *htx = htxbuf(&res->buf);
4541 size_t data;
4542
4543 if (final) {
4544 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004545
Christopher Faulet32ccee12020-11-18 16:44:02 +01004546 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004547 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004548
Christopher Fauletd6c48362020-10-19 18:01:38 +02004549 if (s->txn->meth == HTTP_METH_HEAD)
4550 htx_skip_msg_payload(htx);
4551
Christopher Fauletef70e252020-01-28 09:26:19 +01004552 channel_auto_read(req);
4553 channel_abort(req);
4554 channel_auto_close(req);
4555 channel_htx_erase(req, htxbuf(&req->buf));
4556
4557 res->wex = tick_add_ifset(now_ms, res->wto);
4558 channel_auto_read(res);
4559 channel_auto_close(res);
4560 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004561 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet810df062020-07-22 16:20:34 +02004562 htxbuf(&res->buf)->flags |= HTX_FL_EOI; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004563 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004564 else {
4565 /* Send ASAP informational messages. Rely on CF_EOI for final
4566 * response.
4567 */
4568 res->flags |= CF_SEND_DONTWAIT;
4569 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004570
4571 data = htx->data - co_data(res);
4572 c_adv(res, data);
4573 htx->first = -1;
4574 res->total += data;
4575 return 1;
4576}
4577
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004578void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004579 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004580{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004581 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004582 if (!(s->flags & SF_ERR_MASK))
4583 s->flags |= err;
4584 if (!(s->flags & SF_FINST_MASK))
4585 s->flags |= finst;
4586}
4587
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004588void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004589{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004590 if (!msg) {
4591 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4592 goto end;
4593 }
4594
4595 if (http_reply_message(s, msg) == -1) {
4596 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004597 * it is already an internal error. If it was already a "const"
4598 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004599 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004600 if (s->txn->status == 500) {
4601 if (s->txn->flags & TX_CONST_REPLY)
4602 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004603 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004604 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004605 s->txn->status = 500;
4606 s->txn->http_reply = NULL;
4607 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4608 }
4609
4610end:
4611 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
4612 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
4613
Christopher Faulet0f226952018-10-22 09:29:56 +02004614 channel_auto_read(&s->req);
4615 channel_abort(&s->req);
4616 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004617 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004618 channel_auto_read(&s->res);
4619 channel_auto_close(&s->res);
4620 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004621}
4622
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004623struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004624{
4625 const int msgnum = http_get_status_idx(s->txn->status);
4626
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004627 if (s->txn->http_reply)
4628 return s->txn->http_reply;
4629 else if (s->be->replies[msgnum])
4630 return s->be->replies[msgnum];
4631 else if (strm_fe(s)->replies[msgnum])
4632 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004633 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004634 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004635}
4636
Christopher Faulet40e6b552020-06-25 16:04:50 +02004637/* Produces an HTX message from an http reply. Depending on the http reply type,
4638 * a, errorfile, an raw file or a log-format string is used. On success, it
4639 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4640 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004641 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004642int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004643{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004644 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004645 struct htx_sl *sl;
4646 struct buffer *body = NULL;
4647 const char *status, *reason, *clen, *ctype;
4648 unsigned int slflags;
4649 int ret = 0;
4650
Christopher Faulete29a97e2020-05-14 14:49:25 +02004651 /*
4652 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4653 *
4654 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4655 * as no payload if NULL. the TXN status code is set with the status
4656 * of the original reply.
4657 */
4658
4659 if (reply->type == HTTP_REPLY_INDIRECT) {
4660 if (reply->body.reply)
4661 reply = reply->body.reply;
4662 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004663 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4664 /* get default error message */
4665 if (reply == s->txn->http_reply)
4666 s->txn->http_reply = NULL;
4667 reply = http_error_message(s);
4668 if (reply->type == HTTP_REPLY_INDIRECT) {
4669 if (reply->body.reply)
4670 reply = reply->body.reply;
4671 }
4672 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004673
4674 if (reply->type == HTTP_REPLY_ERRMSG) {
4675 /* implicit or explicit error message*/
4676 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004677 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004678 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004679 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004680 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004681 }
4682 else {
4683 /* no payload, file or log-format string */
4684 if (reply->type == HTTP_REPLY_RAW) {
4685 /* file */
4686 body = &reply->body.obj;
4687 }
4688 else if (reply->type == HTTP_REPLY_LOGFMT) {
4689 /* log-format string */
4690 body = alloc_trash_chunk();
4691 if (!body)
4692 goto fail_alloc;
4693 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4694 }
4695 /* else no payload */
4696
4697 status = ultoa(reply->status);
4698 reason = http_get_reason(reply->status);
4699 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4700 if (!body || !b_data(body))
4701 slflags |= HTX_SL_F_BODYLESS;
4702 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4703 if (!sl)
4704 goto fail;
4705 sl->info.res.status = reply->status;
4706
4707 clen = (body ? ultoa(b_data(body)) : "0");
4708 ctype = reply->ctype;
4709
4710 if (!LIST_ISEMPTY(&reply->hdrs)) {
4711 struct http_reply_hdr *hdr;
4712 struct buffer *value = alloc_trash_chunk();
4713
4714 if (!value)
4715 goto fail;
4716
4717 list_for_each_entry(hdr, &reply->hdrs, list) {
4718 chunk_reset(value);
4719 value->data = build_logline(s, value->area, value->size, &hdr->value);
4720 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4721 free_trash_chunk(value);
4722 goto fail;
4723 }
4724 chunk_reset(value);
4725 }
4726 free_trash_chunk(value);
4727 }
4728
4729 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4730 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4731 !htx_add_endof(htx, HTX_BLK_EOH) ||
4732 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))) ||
4733 !htx_add_endof(htx, HTX_BLK_EOM))
4734 goto fail;
4735 }
4736
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004737 leave:
4738 if (reply->type == HTTP_REPLY_LOGFMT)
4739 free_trash_chunk(body);
4740 return ret;
4741
4742 fail_alloc:
4743 if (!(s->flags & SF_ERR_MASK))
4744 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004745 /* fall through */
4746 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004747 ret = -1;
4748 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004749}
4750
4751/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004752 * occurs -1 is returned and the response channel is truncated, removing this
4753 * way the faulty reply. This function may fail when the reply is formatted
4754 * (http_reply_to_htx) or when the reply is forwarded
4755 * (http_forward_proxy_resp). On the last case, it is because a
4756 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004757 */
4758int http_reply_message(struct stream *s, struct http_reply *reply)
4759{
4760 struct channel *res = &s->res;
4761 struct htx *htx = htx_from_buf(&res->buf);
4762
4763 if (s->txn->status == -1)
4764 s->txn->status = reply->status;
4765 channel_htx_truncate(res, htx);
4766
4767 if (http_reply_to_htx(s, htx, reply) == -1)
4768 goto fail;
4769
4770 htx_to_buf(htx, &s->res.buf);
4771 if (!http_forward_proxy_resp(s, 1))
4772 goto fail;
4773 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004774
4775 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004776 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004777 if (!(s->flags & SF_ERR_MASK))
4778 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004779 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004780}
4781
Christopher Faulet304cc402019-07-15 15:46:28 +02004782/* Return the error message corresponding to si->err_type. It is assumed
4783 * that the server side is closed. Note that err_type is actually a
4784 * bitmask, where almost only aborts may be cumulated with other
4785 * values. We consider that aborted operations are more important
4786 * than timeouts or errors due to the fact that nobody else in the
4787 * logs might explain incomplete retries. All others should avoid
4788 * being cumulated. It should normally not be possible to have multiple
4789 * aborts at once, but just in case, the first one in sequence is reported.
4790 * Note that connection errors appearing on the second request of a keep-alive
4791 * connection are not reported since this allows the client to retry.
4792 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004793void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004794{
4795 int err_type = si->err_type;
4796
4797 /* set s->txn->status for http_error_message(s) */
4798 s->txn->status = 503;
4799
4800 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004801 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4802 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004803 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004804 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4805 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4806 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004807 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004808 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4809 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004810 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004811 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4812 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004813 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004814 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4815 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4816 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004817 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004818 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4819 (s->flags & SF_SRV_REUSED) ? NULL :
4820 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004821 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004822 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4823 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4824 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004825 else { /* SI_ET_CONN_OTHER and others */
4826 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004827 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4828 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004829 }
4830}
4831
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004832
Christopher Faulet4a28a532019-03-01 11:19:40 +01004833/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4834 * on success and -1 on error.
4835 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004836static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004837{
4838 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4839 * then we must send an HTTP/1.1 100 Continue intermediate response.
4840 */
4841 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4842 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4843 struct ist hdr = { .ptr = "Expect", .len = 6 };
4844 struct http_hdr_ctx ctx;
4845
4846 ctx.blk = NULL;
4847 /* Expect is allowed in 1.1, look for it */
4848 if (http_find_header(htx, hdr, &ctx, 0) &&
4849 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004850 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004851 return -1;
4852 http_remove_header(htx, &ctx);
4853 }
4854 }
4855 return 0;
4856}
4857
Christopher Faulet23a3c792018-11-28 10:01:23 +01004858/* Send a 100-Continue response to the client. It returns 0 on success and -1
4859 * on error. The response channel is updated accordingly.
4860 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004861static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004862{
4863 struct channel *res = &s->res;
4864 struct htx *htx = htx_from_buf(&res->buf);
4865 struct htx_sl *sl;
4866 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4867 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004868
4869 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4870 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4871 if (!sl)
4872 goto fail;
4873 sl->info.res.status = 100;
4874
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004875 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004876 goto fail;
4877
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004878 if (!http_forward_proxy_resp(s, 0))
4879 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004880 return 0;
4881
4882 fail:
4883 /* If an error occurred, remove the incomplete HTTP response from the
4884 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004885 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004886 return -1;
4887}
4888
Christopher Faulet12c51e22018-11-28 15:59:42 +01004889
Christopher Faulet0f226952018-10-22 09:29:56 +02004890/*
4891 * Capture headers from message <htx> according to header list <cap_hdr>, and
4892 * fill the <cap> pointers appropriately.
4893 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004894static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004895{
4896 struct cap_hdr *h;
4897 int32_t pos;
4898
Christopher Fauleta3f15502019-05-13 15:27:23 +02004899 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004900 struct htx_blk *blk = htx_get_blk(htx, pos);
4901 enum htx_blk_type type = htx_get_blk_type(blk);
4902 struct ist n, v;
4903
4904 if (type == HTX_BLK_EOH)
4905 break;
4906 if (type != HTX_BLK_HDR)
4907 continue;
4908
4909 n = htx_get_blk_name(htx, blk);
4910
4911 for (h = cap_hdr; h; h = h->next) {
4912 if (h->namelen && (h->namelen == n.len) &&
4913 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4914 if (cap[h->index] == NULL)
4915 cap[h->index] =
4916 pool_alloc(h->pool);
4917
4918 if (cap[h->index] == NULL) {
4919 ha_alert("HTTP capture : out of memory.\n");
4920 break;
4921 }
4922
4923 v = htx_get_blk_value(htx, blk);
4924 if (v.len > h->len)
4925 v.len = h->len;
4926
4927 memcpy(cap[h->index], v.ptr, v.len);
4928 cap[h->index][v.len]=0;
4929 }
4930 }
4931 }
4932}
4933
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004934/* Delete a value in a header between delimiters <from> and <next>. The header
4935 * itself is delimited by <start> and <end> pointers. The number of characters
4936 * displaced is returned, and the pointer to the first delimiter is updated if
4937 * required. The function tries as much as possible to respect the following
4938 * principles :
4939 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4940 * in which case <next> is simply removed
4941 * - set exactly one space character after the new first delimiter, unless there
4942 * are not enough characters in the block being moved to do so.
4943 * - remove unneeded spaces before the previous delimiter and after the new
4944 * one.
4945 *
4946 * It is the caller's responsibility to ensure that :
4947 * - <from> points to a valid delimiter or <start> ;
4948 * - <next> points to a valid delimiter or <end> ;
4949 * - there are non-space chars before <from>.
4950 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004951static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004952{
4953 char *prev = *from;
4954
4955 if (prev == start) {
4956 /* We're removing the first value. eat the semicolon, if <next>
4957 * is lower than <end> */
4958 if (next < end)
4959 next++;
4960
4961 while (next < end && HTTP_IS_SPHT(*next))
4962 next++;
4963 }
4964 else {
4965 /* Remove useless spaces before the old delimiter. */
4966 while (HTTP_IS_SPHT(*(prev-1)))
4967 prev--;
4968 *from = prev;
4969
4970 /* copy the delimiter and if possible a space if we're
4971 * not at the end of the line.
4972 */
4973 if (next < end) {
4974 *prev++ = *next++;
4975 if (prev + 1 < next)
4976 *prev++ = ' ';
4977 while (next < end && HTTP_IS_SPHT(*next))
4978 next++;
4979 }
4980 }
4981 memmove(prev, next, end - next);
4982 return (prev - next);
4983}
4984
Christopher Faulet0f226952018-10-22 09:29:56 +02004985
4986/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004987 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004988 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004989static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004990{
4991 struct ist dst = ist2(str, 0);
4992
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004993 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004994 goto end;
4995 if (dst.len + 1 > len)
4996 goto end;
4997 dst.ptr[dst.len++] = ' ';
4998
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004999 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005000 goto end;
5001 if (dst.len + 1 > len)
5002 goto end;
5003 dst.ptr[dst.len++] = ' ';
5004
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005005 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005006 end:
5007 return dst.len;
5008}
5009
5010/*
5011 * Print a debug line with a start line.
5012 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005013static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005014{
5015 struct session *sess = strm_sess(s);
5016 int max;
5017
5018 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5019 dir,
5020 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5021 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5022
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005023 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005024 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005025 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005026 trash.area[trash.data++] = ' ';
5027
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005028 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005029 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005030 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005031 trash.area[trash.data++] = ' ';
5032
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005033 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005034 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005035 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005036 trash.area[trash.data++] = '\n';
5037
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005038 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005039}
5040
5041/*
5042 * Print a debug line with a header.
5043 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005044static 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 +02005045{
5046 struct session *sess = strm_sess(s);
5047 int max;
5048
5049 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5050 dir,
5051 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5052 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5053
5054 max = n.len;
5055 UBOUND(max, trash.size - trash.data - 3);
5056 chunk_memcat(&trash, n.ptr, max);
5057 trash.area[trash.data++] = ':';
5058 trash.area[trash.data++] = ' ';
5059
5060 max = v.len;
5061 UBOUND(max, trash.size - trash.data - 1);
5062 chunk_memcat(&trash, v.ptr, max);
5063 trash.area[trash.data++] = '\n';
5064
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005065 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005066}
5067
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005068/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5069 * In case of allocation failure, everything allocated is freed and NULL is
5070 * returned. Otherwise the new transaction is assigned to the stream and
5071 * returned.
5072 */
5073struct http_txn *http_alloc_txn(struct stream *s)
5074{
5075 struct http_txn *txn = s->txn;
5076
5077 if (txn)
5078 return txn;
5079
5080 txn = pool_alloc(pool_head_http_txn);
5081 if (!txn)
5082 return txn;
5083
5084 s->txn = txn;
5085 return txn;
5086}
5087
5088void http_txn_reset_req(struct http_txn *txn)
5089{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005090 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005091 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5092}
5093
5094void http_txn_reset_res(struct http_txn *txn)
5095{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005096 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005097 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5098}
5099
5100/*
5101 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5102 * the required fields are properly allocated and that we only need to (re)init
5103 * them. This should be used before processing any new request.
5104 */
5105void http_init_txn(struct stream *s)
5106{
5107 struct http_txn *txn = s->txn;
5108 struct conn_stream *cs = objt_cs(s->si[0].end);
5109
5110 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5111 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5112 : 0);
5113 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005114 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005115 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005116
5117 txn->cookie_first_date = 0;
5118 txn->cookie_last_date = 0;
5119
5120 txn->srv_cookie = NULL;
5121 txn->cli_cookie = NULL;
5122 txn->uri = NULL;
5123
5124 http_txn_reset_req(txn);
5125 http_txn_reset_res(txn);
5126
5127 txn->req.chn = &s->req;
5128 txn->rsp.chn = &s->res;
5129
5130 txn->auth.method = HTTP_AUTH_UNKNOWN;
5131
5132 vars_init(&s->vars_txn, SCOPE_TXN);
5133 vars_init(&s->vars_reqres, SCOPE_REQ);
5134}
5135
5136/* to be used at the end of a transaction */
5137void http_end_txn(struct stream *s)
5138{
5139 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005140
5141 /* these ones will have been dynamically allocated */
5142 pool_free(pool_head_requri, txn->uri);
5143 pool_free(pool_head_capture, txn->cli_cookie);
5144 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005145 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005146
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005147 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005148 txn->uri = NULL;
5149 txn->srv_cookie = NULL;
5150 txn->cli_cookie = NULL;
5151
Christopher Faulet59399252019-11-07 14:27:52 +01005152 if (!LIST_ISEMPTY(&s->vars_txn.head))
5153 vars_prune(&s->vars_txn, s->sess, s);
5154 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5155 vars_prune(&s->vars_reqres, s->sess, s);
5156}
5157
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005158
5159DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005160
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005161__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005162static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005163{
5164}
5165
5166
5167/*
5168 * Local variables:
5169 * c-indent-level: 8
5170 * c-basic-offset: 8
5171 * End:
5172 */