blob: 938ae2dbeed1c37658b4206246e02a3f465ca805 [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. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200878 if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET && conn_get_dst(cli_conn)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +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 }
Willy Tarreau3e320362020-10-23 17:28:57 +02004048 if (memcmp(h, ";no-maint", 3) == 0) {
4049 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
4050 break;
4051 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004052 }
4053
4054 if (uri_auth->refresh) {
4055 for (h = lookup; h <= end - 10; h++) {
4056 if (memcmp(h, ";norefresh", 10) == 0) {
4057 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4058 break;
4059 }
4060 }
4061 }
4062
4063 for (h = lookup; h <= end - 4; h++) {
4064 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004065 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004066 break;
4067 }
4068 }
4069
4070 for (h = lookup; h <= end - 6; h++) {
4071 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004072 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004073 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4074 break;
4075 }
4076 }
4077
Christopher Faulet6338a082019-09-09 15:50:54 +02004078 for (h = lookup; h <= end - 5; h++) {
4079 if (memcmp(h, ";json", 5) == 0) {
4080 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4081 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4082 break;
4083 }
4084 }
4085
4086 for (h = lookup; h <= end - 12; h++) {
4087 if (memcmp(h, ";json-schema", 12) == 0) {
4088 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4089 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4090 break;
4091 }
4092 }
4093
Christopher Faulet377c5a52018-10-24 21:21:30 +02004094 for (h = lookup; h <= end - 8; h++) {
4095 if (memcmp(h, ";st=", 4) == 0) {
4096 int i;
4097 h += 4;
4098 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4099 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4100 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4101 appctx->ctx.stats.st_code = i;
4102 break;
4103 }
4104 }
4105 break;
4106 }
4107 }
4108
4109 appctx->ctx.stats.scope_str = 0;
4110 appctx->ctx.stats.scope_len = 0;
4111 for (h = lookup; h <= end - 8; h++) {
4112 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4113 int itx = 0;
4114 const char *h2;
4115 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4116 const char *err;
4117
4118 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4119 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004120 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4121 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004122 if (*h == ';' || *h == '&' || *h == ' ')
4123 break;
4124 itx++;
4125 h++;
4126 }
4127
4128 if (itx > STAT_SCOPE_TXT_MAXLEN)
4129 itx = STAT_SCOPE_TXT_MAXLEN;
4130 appctx->ctx.stats.scope_len = itx;
4131
4132 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4133 memcpy(scope_txt, h2, itx);
4134 scope_txt[itx] = '\0';
4135 err = invalid_char(scope_txt);
4136 if (err) {
4137 /* bad char in search text => clear scope */
4138 appctx->ctx.stats.scope_str = 0;
4139 appctx->ctx.stats.scope_len = 0;
4140 }
4141 break;
4142 }
4143 }
4144
4145 /* now check whether we have some admin rules for this request */
4146 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4147 int ret = 1;
4148
4149 if (stats_admin_rule->cond) {
4150 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4151 ret = acl_pass(ret);
4152 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4153 ret = !ret;
4154 }
4155
4156 if (ret) {
4157 /* no rule, or the rule matches */
4158 appctx->ctx.stats.flags |= STAT_ADMIN;
4159 break;
4160 }
4161 }
4162
Christopher Faulet5d45e382019-02-27 15:15:23 +01004163 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4164 appctx->st0 = STAT_HTTP_HEAD;
4165 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004166 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004167 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004168 if (msg->msg_state < HTTP_MSG_DATA)
4169 req->analysers |= AN_REQ_HTTP_BODY;
4170 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004171 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004172 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004173 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4174 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4175 appctx->st0 = STAT_HTTP_LAST;
4176 }
4177 }
4178 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004179 /* Unsupported method */
4180 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4181 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4182 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004183 }
4184
4185 s->task->nice = -32; /* small boost for HTTP statistics */
4186 return 1;
4187}
4188
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004189void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004190{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004191 struct channel *req = &s->req;
4192 struct channel *res = &s->res;
4193 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004194 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004195 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004196 struct ist path, location;
4197 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004198
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004199 /*
4200 * Create the location
4201 */
4202 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004203
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004204 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004205 /* special prefix "/" means don't change URL */
4206 srv = __objt_server(s->target);
4207 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4208 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4209 return;
4210 }
4211
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004212 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004213 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004214 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004215 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004216 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004217 return;
4218
4219 if (!chunk_memcat(&trash, path.ptr, path.len))
4220 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004221 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004222
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004223 /*
4224 * Create the 302 respone
4225 */
4226 htx = htx_from_buf(&res->buf);
4227 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4228 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4229 ist("HTTP/1.1"), ist("302"), ist("Found"));
4230 if (!sl)
4231 goto fail;
4232 sl->info.res.status = 302;
4233 s->txn->status = 302;
4234
4235 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4236 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4237 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4238 !htx_add_header(htx, ist("Location"), location))
4239 goto fail;
4240
4241 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4242 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004243
Christopher Fauletc20afb82020-01-24 19:16:26 +01004244 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004245 if (!http_forward_proxy_resp(s, 1))
4246 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004247
4248 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004249 si_shutr(si);
4250 si_shutw(si);
4251 si->err_type = SI_ET_NONE;
4252 si->state = SI_ST_CLO;
4253
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004254 if (!(s->flags & SF_ERR_MASK))
4255 s->flags |= SF_ERR_LOCAL;
4256 if (!(s->flags & SF_FINST_MASK))
4257 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004258
4259 /* FIXME: we should increase a counter of redirects per server and per backend. */
4260 srv_inc_sess_ctr(srv);
4261 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004262 return;
4263
4264 fail:
4265 /* If an error occurred, remove the incomplete HTTP response from the
4266 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004267 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004268}
4269
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004270/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004271 * because an error was triggered during the body forwarding.
4272 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004273static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004274{
4275 struct channel *chn = &s->req;
4276 struct http_txn *txn = s->txn;
4277
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004278 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004279
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004280 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4281 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004282 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004283 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004284 goto end;
4285 }
4286
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004287 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4288 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004289 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004290 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004291
4292 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004293 /* No need to read anymore, the request was completely parsed.
4294 * We can shut the read side unless we want to abort_on_close,
4295 * or we have a POST request. The issue with POST requests is
4296 * that some browsers still send a CRLF after the request, and
4297 * this CRLF must be read so that it does not remain in the kernel
4298 * buffers, otherwise a close could cause an RST on some systems
4299 * (eg: Linux).
4300 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004301 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004302 channel_dont_read(chn);
4303
4304 /* if the server closes the connection, we want to immediately react
4305 * and close the socket to save packets and syscalls.
4306 */
4307 s->si[1].flags |= SI_FL_NOHALF;
4308
4309 /* In any case we've finished parsing the request so we must
4310 * disable Nagle when sending data because 1) we're not going
4311 * to shut this side, and 2) the server is waiting for us to
4312 * send pending data.
4313 */
4314 chn->flags |= CF_NEVER_WAIT;
4315
Christopher Fauletd01ce402019-01-02 17:44:13 +01004316 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4317 /* The server has not finished to respond, so we
4318 * don't want to move in order not to upset it.
4319 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004320 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004321 return;
4322 }
4323
Christopher Fauletf2824e62018-10-01 12:12:37 +02004324 /* When we get here, it means that both the request and the
4325 * response have finished receiving. Depending on the connection
4326 * mode, we'll have to wait for the last bytes to leave in either
4327 * direction, and sometimes for a close to be effective.
4328 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004329 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004330 /* Tunnel mode will not have any analyser so it needs to
4331 * poll for reads.
4332 */
4333 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004334 if (b_data(&chn->buf)) {
4335 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004336 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004337 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004338 txn->req.msg_state = HTTP_MSG_TUNNEL;
4339 }
4340 else {
4341 /* we're not expecting any new data to come for this
4342 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004343 *
4344 * However, there is an exception if the response
4345 * length is undefined. In this case, we need to wait
4346 * the close from the server. The response will be
4347 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004348 */
4349 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4350 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004351 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004352
4353 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4354 channel_shutr_now(chn);
4355 channel_shutw_now(chn);
4356 }
4357 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004358 goto check_channel_flags;
4359 }
4360
4361 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4362 http_msg_closing:
4363 /* nothing else to forward, just waiting for the output buffer
4364 * to be empty and for the shutw_now to take effect.
4365 */
4366 if (channel_is_empty(chn)) {
4367 txn->req.msg_state = HTTP_MSG_CLOSED;
4368 goto http_msg_closed;
4369 }
4370 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004371 txn->req.msg_state = HTTP_MSG_ERROR;
4372 goto end;
4373 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004374 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004375 return;
4376 }
4377
4378 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4379 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004380 /* if we don't know whether the server will close, we need to hard close */
4381 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4382 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004383 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004384 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004385 channel_dont_read(chn);
4386 goto end;
4387 }
4388
4389 check_channel_flags:
4390 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4391 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4392 /* if we've just closed an output, let's switch */
4393 txn->req.msg_state = HTTP_MSG_CLOSING;
4394 goto http_msg_closing;
4395 }
4396
4397 end:
4398 chn->analysers &= AN_REQ_FLT_END;
Christopher Faulet0d4da8c2020-12-15 13:32:55 +01004399 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4400 chn->flags |= CF_NEVER_WAIT;
4401 if (HAS_REQ_DATA_FILTERS(s))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004402 chn->analysers |= AN_REQ_FLT_XFER_DATA;
Christopher Faulet0d4da8c2020-12-15 13:32:55 +01004403 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004404 channel_auto_close(chn);
4405 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004406 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004407}
4408
4409
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004410/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004411 * because an error was triggered during the body forwarding.
4412 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004413static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004414{
4415 struct channel *chn = &s->res;
4416 struct http_txn *txn = s->txn;
4417
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004418 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004419
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004420 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4421 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004422 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004423 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004424 goto end;
4425 }
4426
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004427 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4428 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004429 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004430 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004431
4432 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4433 /* In theory, we don't need to read anymore, but we must
4434 * still monitor the server connection for a possible close
4435 * while the request is being uploaded, so we don't disable
4436 * reading.
4437 */
4438 /* channel_dont_read(chn); */
4439
4440 if (txn->req.msg_state < HTTP_MSG_DONE) {
4441 /* The client seems to still be sending data, probably
4442 * because we got an error response during an upload.
4443 * We have the choice of either breaking the connection
4444 * or letting it pass through. Let's do the later.
4445 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004446 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004447 return;
4448 }
4449
4450 /* When we get here, it means that both the request and the
4451 * response have finished receiving. Depending on the connection
4452 * mode, we'll have to wait for the last bytes to leave in either
4453 * direction, and sometimes for a close to be effective.
4454 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004455 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004456 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004457 if (b_data(&chn->buf)) {
4458 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004459 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004460 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004461 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4462 }
4463 else {
4464 /* we're not expecting any new data to come for this
4465 * transaction, so we can close it.
4466 */
4467 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4468 channel_shutr_now(chn);
4469 channel_shutw_now(chn);
4470 }
4471 }
4472 goto check_channel_flags;
4473 }
4474
4475 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4476 http_msg_closing:
4477 /* nothing else to forward, just waiting for the output buffer
4478 * to be empty and for the shutw_now to take effect.
4479 */
4480 if (channel_is_empty(chn)) {
4481 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4482 goto http_msg_closed;
4483 }
4484 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004485 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004486 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004487 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004488 if (strm_sess(s)->listener->counters)
4489 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004490 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004491 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004492 goto end;
4493 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004494 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004495 return;
4496 }
4497
4498 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4499 http_msg_closed:
4500 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004501 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004502 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004503 goto end;
4504 }
4505
4506 check_channel_flags:
4507 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4508 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4509 /* if we've just closed an output, let's switch */
4510 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4511 goto http_msg_closing;
4512 }
4513
4514 end:
4515 chn->analysers &= AN_RES_FLT_END;
Christopher Faulet0d4da8c2020-12-15 13:32:55 +01004516 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4517 chn->flags |= CF_NEVER_WAIT;
4518 if (HAS_RSP_DATA_FILTERS(s))
4519 chn->analysers |= AN_RES_FLT_XFER_DATA;
4520 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004521 channel_auto_close(chn);
4522 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004523 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004524}
4525
Christopher Fauletef70e252020-01-28 09:26:19 +01004526/* Forward a response generated by HAProxy (error/redirect/return). This
4527 * function forwards all pending incoming data. If <final> is set to 0, nothing
4528 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004529 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004530 * returned. If an error occurred, 0 is returned. If it fails, this function
4531 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004532 */
4533int http_forward_proxy_resp(struct stream *s, int final)
4534{
4535 struct channel *req = &s->req;
4536 struct channel *res = &s->res;
4537 struct htx *htx = htxbuf(&res->buf);
4538 size_t data;
4539
4540 if (final) {
4541 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004542
Christopher Faulet32ccee12020-11-18 16:44:02 +01004543 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004544 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004545
Christopher Fauletd6c48362020-10-19 18:01:38 +02004546 if (s->txn->meth == HTTP_METH_HEAD)
4547 htx_skip_msg_payload(htx);
4548
Christopher Fauletef70e252020-01-28 09:26:19 +01004549 channel_auto_read(req);
4550 channel_abort(req);
4551 channel_auto_close(req);
4552 channel_htx_erase(req, htxbuf(&req->buf));
4553
4554 res->wex = tick_add_ifset(now_ms, res->wto);
4555 channel_auto_read(res);
4556 channel_auto_close(res);
4557 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004558 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet810df062020-07-22 16:20:34 +02004559 htxbuf(&res->buf)->flags |= HTX_FL_EOI; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004560 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004561 else {
4562 /* Send ASAP informational messages. Rely on CF_EOI for final
4563 * response.
4564 */
4565 res->flags |= CF_SEND_DONTWAIT;
4566 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004567
4568 data = htx->data - co_data(res);
4569 c_adv(res, data);
4570 htx->first = -1;
4571 res->total += data;
4572 return 1;
4573}
4574
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004575void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004576 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004577{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004578 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004579 if (!(s->flags & SF_ERR_MASK))
4580 s->flags |= err;
4581 if (!(s->flags & SF_FINST_MASK))
4582 s->flags |= finst;
4583}
4584
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004585void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004586{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004587 if (!msg) {
4588 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4589 goto end;
4590 }
4591
4592 if (http_reply_message(s, msg) == -1) {
4593 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004594 * it is already an internal error. If it was already a "const"
4595 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004596 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004597 if (s->txn->status == 500) {
4598 if (s->txn->flags & TX_CONST_REPLY)
4599 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004600 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004601 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004602 s->txn->status = 500;
4603 s->txn->http_reply = NULL;
4604 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4605 }
4606
4607end:
4608 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
4609 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
4610
Christopher Faulet0f226952018-10-22 09:29:56 +02004611 channel_auto_read(&s->req);
4612 channel_abort(&s->req);
4613 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004614 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004615 channel_auto_read(&s->res);
4616 channel_auto_close(&s->res);
4617 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004618}
4619
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004620struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004621{
4622 const int msgnum = http_get_status_idx(s->txn->status);
4623
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004624 if (s->txn->http_reply)
4625 return s->txn->http_reply;
4626 else if (s->be->replies[msgnum])
4627 return s->be->replies[msgnum];
4628 else if (strm_fe(s)->replies[msgnum])
4629 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004630 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004631 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004632}
4633
Christopher Faulet40e6b552020-06-25 16:04:50 +02004634/* Produces an HTX message from an http reply. Depending on the http reply type,
4635 * a, errorfile, an raw file or a log-format string is used. On success, it
4636 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4637 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004638 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004639int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004640{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004641 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004642 struct htx_sl *sl;
4643 struct buffer *body = NULL;
4644 const char *status, *reason, *clen, *ctype;
4645 unsigned int slflags;
4646 int ret = 0;
4647
Christopher Faulete29a97e2020-05-14 14:49:25 +02004648 /*
4649 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4650 *
4651 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4652 * as no payload if NULL. the TXN status code is set with the status
4653 * of the original reply.
4654 */
4655
4656 if (reply->type == HTTP_REPLY_INDIRECT) {
4657 if (reply->body.reply)
4658 reply = reply->body.reply;
4659 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004660 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4661 /* get default error message */
4662 if (reply == s->txn->http_reply)
4663 s->txn->http_reply = NULL;
4664 reply = http_error_message(s);
4665 if (reply->type == HTTP_REPLY_INDIRECT) {
4666 if (reply->body.reply)
4667 reply = reply->body.reply;
4668 }
4669 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004670
4671 if (reply->type == HTTP_REPLY_ERRMSG) {
4672 /* implicit or explicit error message*/
4673 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004674 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004675 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004676 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004677 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004678 }
4679 else {
4680 /* no payload, file or log-format string */
4681 if (reply->type == HTTP_REPLY_RAW) {
4682 /* file */
4683 body = &reply->body.obj;
4684 }
4685 else if (reply->type == HTTP_REPLY_LOGFMT) {
4686 /* log-format string */
4687 body = alloc_trash_chunk();
4688 if (!body)
4689 goto fail_alloc;
4690 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4691 }
4692 /* else no payload */
4693
4694 status = ultoa(reply->status);
4695 reason = http_get_reason(reply->status);
4696 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4697 if (!body || !b_data(body))
4698 slflags |= HTX_SL_F_BODYLESS;
4699 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4700 if (!sl)
4701 goto fail;
4702 sl->info.res.status = reply->status;
4703
4704 clen = (body ? ultoa(b_data(body)) : "0");
4705 ctype = reply->ctype;
4706
4707 if (!LIST_ISEMPTY(&reply->hdrs)) {
4708 struct http_reply_hdr *hdr;
4709 struct buffer *value = alloc_trash_chunk();
4710
4711 if (!value)
4712 goto fail;
4713
4714 list_for_each_entry(hdr, &reply->hdrs, list) {
4715 chunk_reset(value);
4716 value->data = build_logline(s, value->area, value->size, &hdr->value);
4717 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4718 free_trash_chunk(value);
4719 goto fail;
4720 }
4721 chunk_reset(value);
4722 }
4723 free_trash_chunk(value);
4724 }
4725
4726 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4727 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4728 !htx_add_endof(htx, HTX_BLK_EOH) ||
4729 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))) ||
4730 !htx_add_endof(htx, HTX_BLK_EOM))
4731 goto fail;
4732 }
4733
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004734 leave:
4735 if (reply->type == HTTP_REPLY_LOGFMT)
4736 free_trash_chunk(body);
4737 return ret;
4738
4739 fail_alloc:
4740 if (!(s->flags & SF_ERR_MASK))
4741 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004742 /* fall through */
4743 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004744 ret = -1;
4745 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004746}
4747
4748/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004749 * occurs -1 is returned and the response channel is truncated, removing this
4750 * way the faulty reply. This function may fail when the reply is formatted
4751 * (http_reply_to_htx) or when the reply is forwarded
4752 * (http_forward_proxy_resp). On the last case, it is because a
4753 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004754 */
4755int http_reply_message(struct stream *s, struct http_reply *reply)
4756{
4757 struct channel *res = &s->res;
4758 struct htx *htx = htx_from_buf(&res->buf);
4759
4760 if (s->txn->status == -1)
4761 s->txn->status = reply->status;
4762 channel_htx_truncate(res, htx);
4763
4764 if (http_reply_to_htx(s, htx, reply) == -1)
4765 goto fail;
4766
4767 htx_to_buf(htx, &s->res.buf);
4768 if (!http_forward_proxy_resp(s, 1))
4769 goto fail;
4770 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004771
4772 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004773 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004774 if (!(s->flags & SF_ERR_MASK))
4775 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004776 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004777}
4778
Christopher Faulet304cc402019-07-15 15:46:28 +02004779/* Return the error message corresponding to si->err_type. It is assumed
4780 * that the server side is closed. Note that err_type is actually a
4781 * bitmask, where almost only aborts may be cumulated with other
4782 * values. We consider that aborted operations are more important
4783 * than timeouts or errors due to the fact that nobody else in the
4784 * logs might explain incomplete retries. All others should avoid
4785 * being cumulated. It should normally not be possible to have multiple
4786 * aborts at once, but just in case, the first one in sequence is reported.
4787 * Note that connection errors appearing on the second request of a keep-alive
4788 * connection are not reported since this allows the client to retry.
4789 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004790void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004791{
4792 int err_type = si->err_type;
4793
4794 /* set s->txn->status for http_error_message(s) */
4795 s->txn->status = 503;
4796
4797 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004798 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4799 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004800 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004801 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4802 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4803 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004804 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004805 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4806 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004807 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004808 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4809 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004810 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004811 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4812 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4813 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004814 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004815 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4816 (s->flags & SF_SRV_REUSED) ? NULL :
4817 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004818 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004819 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4820 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4821 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004822 else { /* SI_ET_CONN_OTHER and others */
4823 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004824 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4825 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004826 }
4827}
4828
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004829
Christopher Faulet4a28a532019-03-01 11:19:40 +01004830/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4831 * on success and -1 on error.
4832 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004833static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004834{
4835 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4836 * then we must send an HTTP/1.1 100 Continue intermediate response.
4837 */
4838 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4839 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4840 struct ist hdr = { .ptr = "Expect", .len = 6 };
4841 struct http_hdr_ctx ctx;
4842
4843 ctx.blk = NULL;
4844 /* Expect is allowed in 1.1, look for it */
4845 if (http_find_header(htx, hdr, &ctx, 0) &&
4846 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004847 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004848 return -1;
4849 http_remove_header(htx, &ctx);
4850 }
4851 }
4852 return 0;
4853}
4854
Christopher Faulet23a3c792018-11-28 10:01:23 +01004855/* Send a 100-Continue response to the client. It returns 0 on success and -1
4856 * on error. The response channel is updated accordingly.
4857 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004858static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004859{
4860 struct channel *res = &s->res;
4861 struct htx *htx = htx_from_buf(&res->buf);
4862 struct htx_sl *sl;
4863 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4864 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004865
4866 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4867 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4868 if (!sl)
4869 goto fail;
4870 sl->info.res.status = 100;
4871
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004872 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004873 goto fail;
4874
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004875 if (!http_forward_proxy_resp(s, 0))
4876 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004877 return 0;
4878
4879 fail:
4880 /* If an error occurred, remove the incomplete HTTP response from the
4881 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004882 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004883 return -1;
4884}
4885
Christopher Faulet12c51e22018-11-28 15:59:42 +01004886
Christopher Faulet0f226952018-10-22 09:29:56 +02004887/*
4888 * Capture headers from message <htx> according to header list <cap_hdr>, and
4889 * fill the <cap> pointers appropriately.
4890 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004891static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004892{
4893 struct cap_hdr *h;
4894 int32_t pos;
4895
Christopher Fauleta3f15502019-05-13 15:27:23 +02004896 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004897 struct htx_blk *blk = htx_get_blk(htx, pos);
4898 enum htx_blk_type type = htx_get_blk_type(blk);
4899 struct ist n, v;
4900
4901 if (type == HTX_BLK_EOH)
4902 break;
4903 if (type != HTX_BLK_HDR)
4904 continue;
4905
4906 n = htx_get_blk_name(htx, blk);
4907
4908 for (h = cap_hdr; h; h = h->next) {
4909 if (h->namelen && (h->namelen == n.len) &&
4910 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4911 if (cap[h->index] == NULL)
4912 cap[h->index] =
4913 pool_alloc(h->pool);
4914
4915 if (cap[h->index] == NULL) {
4916 ha_alert("HTTP capture : out of memory.\n");
4917 break;
4918 }
4919
4920 v = htx_get_blk_value(htx, blk);
4921 if (v.len > h->len)
4922 v.len = h->len;
4923
4924 memcpy(cap[h->index], v.ptr, v.len);
4925 cap[h->index][v.len]=0;
4926 }
4927 }
4928 }
4929}
4930
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004931/* Delete a value in a header between delimiters <from> and <next>. The header
4932 * itself is delimited by <start> and <end> pointers. The number of characters
4933 * displaced is returned, and the pointer to the first delimiter is updated if
4934 * required. The function tries as much as possible to respect the following
4935 * principles :
4936 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4937 * in which case <next> is simply removed
4938 * - set exactly one space character after the new first delimiter, unless there
4939 * are not enough characters in the block being moved to do so.
4940 * - remove unneeded spaces before the previous delimiter and after the new
4941 * one.
4942 *
4943 * It is the caller's responsibility to ensure that :
4944 * - <from> points to a valid delimiter or <start> ;
4945 * - <next> points to a valid delimiter or <end> ;
4946 * - there are non-space chars before <from>.
4947 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004948static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004949{
4950 char *prev = *from;
4951
4952 if (prev == start) {
4953 /* We're removing the first value. eat the semicolon, if <next>
4954 * is lower than <end> */
4955 if (next < end)
4956 next++;
4957
4958 while (next < end && HTTP_IS_SPHT(*next))
4959 next++;
4960 }
4961 else {
4962 /* Remove useless spaces before the old delimiter. */
4963 while (HTTP_IS_SPHT(*(prev-1)))
4964 prev--;
4965 *from = prev;
4966
4967 /* copy the delimiter and if possible a space if we're
4968 * not at the end of the line.
4969 */
4970 if (next < end) {
4971 *prev++ = *next++;
4972 if (prev + 1 < next)
4973 *prev++ = ' ';
4974 while (next < end && HTTP_IS_SPHT(*next))
4975 next++;
4976 }
4977 }
4978 memmove(prev, next, end - next);
4979 return (prev - next);
4980}
4981
Christopher Faulet0f226952018-10-22 09:29:56 +02004982
4983/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004984 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004985 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004986static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004987{
4988 struct ist dst = ist2(str, 0);
4989
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004990 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004991 goto end;
4992 if (dst.len + 1 > len)
4993 goto end;
4994 dst.ptr[dst.len++] = ' ';
4995
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004996 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004997 goto end;
4998 if (dst.len + 1 > len)
4999 goto end;
5000 dst.ptr[dst.len++] = ' ';
5001
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005002 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005003 end:
5004 return dst.len;
5005}
5006
5007/*
5008 * Print a debug line with a start line.
5009 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005010static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005011{
5012 struct session *sess = strm_sess(s);
5013 int max;
5014
5015 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5016 dir,
5017 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5018 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5019
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005020 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005021 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005022 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005023 trash.area[trash.data++] = ' ';
5024
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005025 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005026 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005027 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005028 trash.area[trash.data++] = ' ';
5029
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005030 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005031 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005032 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005033 trash.area[trash.data++] = '\n';
5034
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005035 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005036}
5037
5038/*
5039 * Print a debug line with a header.
5040 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005041static 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 +02005042{
5043 struct session *sess = strm_sess(s);
5044 int max;
5045
5046 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5047 dir,
5048 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5049 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5050
5051 max = n.len;
5052 UBOUND(max, trash.size - trash.data - 3);
5053 chunk_memcat(&trash, n.ptr, max);
5054 trash.area[trash.data++] = ':';
5055 trash.area[trash.data++] = ' ';
5056
5057 max = v.len;
5058 UBOUND(max, trash.size - trash.data - 1);
5059 chunk_memcat(&trash, v.ptr, max);
5060 trash.area[trash.data++] = '\n';
5061
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005062 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005063}
5064
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005065/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5066 * In case of allocation failure, everything allocated is freed and NULL is
5067 * returned. Otherwise the new transaction is assigned to the stream and
5068 * returned.
5069 */
5070struct http_txn *http_alloc_txn(struct stream *s)
5071{
5072 struct http_txn *txn = s->txn;
5073
5074 if (txn)
5075 return txn;
5076
5077 txn = pool_alloc(pool_head_http_txn);
5078 if (!txn)
5079 return txn;
5080
5081 s->txn = txn;
5082 return txn;
5083}
5084
5085void http_txn_reset_req(struct http_txn *txn)
5086{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005087 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005088 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5089}
5090
5091void http_txn_reset_res(struct http_txn *txn)
5092{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005093 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005094 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5095}
5096
5097/*
5098 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5099 * the required fields are properly allocated and that we only need to (re)init
5100 * them. This should be used before processing any new request.
5101 */
5102void http_init_txn(struct stream *s)
5103{
5104 struct http_txn *txn = s->txn;
5105 struct conn_stream *cs = objt_cs(s->si[0].end);
5106
5107 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5108 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5109 : 0);
5110 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005111 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005112 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005113
5114 txn->cookie_first_date = 0;
5115 txn->cookie_last_date = 0;
5116
5117 txn->srv_cookie = NULL;
5118 txn->cli_cookie = NULL;
5119 txn->uri = NULL;
5120
5121 http_txn_reset_req(txn);
5122 http_txn_reset_res(txn);
5123
5124 txn->req.chn = &s->req;
5125 txn->rsp.chn = &s->res;
5126
5127 txn->auth.method = HTTP_AUTH_UNKNOWN;
5128
5129 vars_init(&s->vars_txn, SCOPE_TXN);
5130 vars_init(&s->vars_reqres, SCOPE_REQ);
5131}
5132
5133/* to be used at the end of a transaction */
5134void http_end_txn(struct stream *s)
5135{
5136 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005137
5138 /* these ones will have been dynamically allocated */
5139 pool_free(pool_head_requri, txn->uri);
5140 pool_free(pool_head_capture, txn->cli_cookie);
5141 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005142 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005143
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005144 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005145 txn->uri = NULL;
5146 txn->srv_cookie = NULL;
5147 txn->cli_cookie = NULL;
5148
Christopher Faulet59399252019-11-07 14:27:52 +01005149 if (!LIST_ISEMPTY(&s->vars_txn.head))
5150 vars_prune(&s->vars_txn, s->sess, s);
5151 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5152 vars_prune(&s->vars_reqres, s->sess, s);
5153}
5154
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005155
5156DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005157
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005158__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005159static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005160{
5161}
5162
5163
5164/*
5165 * Local variables:
5166 * c-indent-level: 8
5167 * c-basic-offset: 8
5168 * End:
5169 */