blob: 1334118914f4c78a0854f741c5a6a75baf7fe3a0 [file] [log] [blame]
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaudcc048a2020-06-04 19:11:43 +020013#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020014#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020016#include <haproxy/backend.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020017#include <haproxy/base64.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020018#include <haproxy/capture-t.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020019#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020020#include <haproxy/check.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020021#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020022#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020023#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020024#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020025#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020027#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020028#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020029#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020030#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020031#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020032#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020033#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020034#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020035#include <haproxy/stream_interface.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020036#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020037#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020038#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020039
Christopher Faulete0768eb2018-10-03 16:38:02 +020040
Christopher Fauleteea8fc72019-11-05 16:18:10 +010041#define TRACE_SOURCE &trace_strm
42
Christopher Faulet377c5a52018-10-24 21:21:30 +020043extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020044
Christopher Fauleta8a46e22019-07-16 14:53:09 +020045struct pool_head *pool_head_requri = NULL;
46struct pool_head *pool_head_capture = NULL;
47
48
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020049static void http_end_request(struct stream *s);
50static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020051
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020052static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
53static int http_del_hdr_value(char *start, char *end, char **from, char *next);
54static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020055static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
56static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
Christopher Faulet0f226952018-10-22 09:29:56 +020057
Christopher Fauletb58f62b2020-01-13 16:40:13 +010058static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020059static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020060
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020061static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
62static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020063
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020064static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
65static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020066
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020067static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
68static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010069
Christopher Faulete0768eb2018-10-03 16:38:02 +020070/* This stream analyser waits for a complete HTTP request. It returns 1 if the
71 * processing can continue on next analysers, or zero if it either needs more
72 * data or wants to immediately abort the request (eg: timeout, error, ...). It
73 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
74 * when it has nothing left to do, and may remove any analyser when it wants to
75 * abort.
76 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020077int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020078{
Christopher Faulet9768c262018-10-22 09:34:31 +020079
Christopher Faulete0768eb2018-10-03 16:38:02 +020080 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020081 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020082 *
Christopher Faulet9768c262018-10-22 09:34:31 +020083 * Once the start line and all headers are received, we may perform a
84 * capture of the error (if any), and we will set a few fields. We also
85 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020086 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020087 struct session *sess = s->sess;
88 struct http_txn *txn = s->txn;
89 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020090 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010091 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020092
Christopher Fauleteea8fc72019-11-05 16:18:10 +010093 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020094
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010095 htx = htxbuf(&req->buf);
Christopher Faulet9768c262018-10-22 09:34:31 +020096
Willy Tarreau4236f032019-03-05 10:43:32 +010097 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +020098 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +010099 stream_inc_http_req_ctr(s);
100 stream_inc_http_err_ctr(s);
101 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200102 if (htx->flags & HTX_FL_PARSING_ERROR)
103 goto return_bad_req;
104 else
105 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100106 }
107
Christopher Faulete0768eb2018-10-03 16:38:02 +0200108 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200109 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200110
111 /* If there is data available for analysis, log the end of the idle time. */
Christopher Faulet870aad92018-11-29 15:23:46 +0100112 if (c_data(req) && s->logs.t_idle == -1) {
113 const struct cs_info *csinfo = si_get_cs_info(objt_cs(s->si[0].end));
114
115 s->logs.t_idle = ((csinfo)
116 ? csinfo->t_idle
117 : tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake);
118 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200119
Christopher Faulete0768eb2018-10-03 16:38:02 +0200120 /*
121 * Now we quickly check if we have found a full valid request.
122 * If not so, we check the FD and buffer states before leaving.
123 * A full request is indicated by the fact that we have seen
124 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
125 * requests are checked first. When waiting for a second request
126 * on a keep-alive stream, if we encounter and error, close, t/o,
127 * we note the error in the stream flags but don't set any state.
128 * Since the error will be noted there, it will not be counted by
129 * process_stream() as a frontend error.
130 * Last, we may increase some tracked counters' http request errors on
131 * the cases that are deliberately the client's fault. For instance,
132 * a timeout or connection reset is not counted as an error. However
133 * a bad request is.
134 */
Christopher Faulet29f17582019-05-23 11:03:26 +0200135 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200136 if (htx->flags & HTX_FL_UPGRADE)
137 goto failed_keep_alive;
138
Christopher Faulet9768c262018-10-22 09:34:31 +0200139 /* 1: have we encountered a read error ? */
140 if (req->flags & CF_READ_ERROR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200141 if (!(s->flags & SF_ERR_MASK))
142 s->flags |= SF_ERR_CLICL;
143
144 if (txn->flags & TX_WAIT_NEXT_RQ)
145 goto failed_keep_alive;
146
147 if (sess->fe->options & PR_O_IGNORE_PRB)
148 goto failed_keep_alive;
149
Christopher Faulet9768c262018-10-22 09:34:31 +0200150 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200151 stream_inc_http_req_ctr(s);
152 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100153 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200154 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100155 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200156
Christopher Faulet9768c262018-10-22 09:34:31 +0200157 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200158 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet9768c262018-10-22 09:34:31 +0200159 req->analysers &= AN_REQ_FLT_END;
160
Christopher Faulete0768eb2018-10-03 16:38:02 +0200161 if (!(s->flags & SF_FINST_MASK))
162 s->flags |= SF_FINST_R;
163 return 0;
164 }
165
Christopher Faulet9768c262018-10-22 09:34:31 +0200166 /* 2: has the read timeout expired ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200167 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
168 if (!(s->flags & SF_ERR_MASK))
169 s->flags |= SF_ERR_CLITO;
170
171 if (txn->flags & TX_WAIT_NEXT_RQ)
172 goto failed_keep_alive;
173
174 if (sess->fe->options & PR_O_IGNORE_PRB)
175 goto failed_keep_alive;
176
Christopher Faulet9768c262018-10-22 09:34:31 +0200177 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200178 stream_inc_http_req_ctr(s);
179 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100180 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200181 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100182 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200183
Christopher Faulet9768c262018-10-22 09:34:31 +0200184 txn->status = 408;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200185 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200186 req->analysers &= AN_REQ_FLT_END;
187
Christopher Faulete0768eb2018-10-03 16:38:02 +0200188 if (!(s->flags & SF_FINST_MASK))
189 s->flags |= SF_FINST_R;
190 return 0;
191 }
192
Christopher Faulet9768c262018-10-22 09:34:31 +0200193 /* 3: have we encountered a close ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200194 else if (req->flags & CF_SHUTR) {
195 if (!(s->flags & SF_ERR_MASK))
196 s->flags |= SF_ERR_CLICL;
197
198 if (txn->flags & TX_WAIT_NEXT_RQ)
199 goto failed_keep_alive;
200
201 if (sess->fe->options & PR_O_IGNORE_PRB)
202 goto failed_keep_alive;
203
Christopher Faulete0768eb2018-10-03 16:38:02 +0200204 stream_inc_http_err_ctr(s);
205 stream_inc_http_req_ctr(s);
206 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100207 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200208 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100209 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200210
Christopher Faulet9768c262018-10-22 09:34:31 +0200211 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200212 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200213 req->analysers &= AN_REQ_FLT_END;
214
Christopher Faulete0768eb2018-10-03 16:38:02 +0200215 if (!(s->flags & SF_FINST_MASK))
216 s->flags |= SF_FINST_R;
217 return 0;
218 }
219
220 channel_dont_connect(req);
221 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
222 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100223
Christopher Faulet9768c262018-10-22 09:34:31 +0200224 if (sess->listener->options & LI_O_NOQUICKACK && htx_is_not_empty(htx) &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200225 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
226 /* We need more data, we have to re-enable quick-ack in case we
227 * previously disabled it, otherwise we might cause the client
228 * to delay next data.
229 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100230 conn_set_quickack(objt_conn(sess->origin), 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200231 }
Willy Tarreau1a18b542018-12-11 16:37:42 +0100232
Christopher Faulet47365272018-10-31 17:40:50 +0100233 if ((req->flags & CF_READ_PARTIAL) && (txn->flags & TX_WAIT_NEXT_RQ)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200234 /* If the client starts to talk, let's fall back to
235 * request timeout processing.
236 */
237 txn->flags &= ~TX_WAIT_NEXT_RQ;
238 req->analyse_exp = TICK_ETERNITY;
239 }
240
241 /* just set the request timeout once at the beginning of the request */
242 if (!tick_isset(req->analyse_exp)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100243 if ((txn->flags & TX_WAIT_NEXT_RQ) && tick_isset(s->be->timeout.httpka))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200244 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
245 else
246 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
247 }
248
249 /* we're not ready yet */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100250 DBG_TRACE_DEVEL("waiting for the request",
251 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200252 return 0;
253
254 failed_keep_alive:
255 /* Here we process low-level errors for keep-alive requests. In
256 * short, if the request is not the first one and it experiences
257 * a timeout, read error or shutdown, we just silently close so
258 * that the client can try again.
259 */
260 txn->status = 0;
261 msg->msg_state = HTTP_MSG_RQBEFORE;
262 req->analysers &= AN_REQ_FLT_END;
263 s->logs.logwait = 0;
264 s->logs.level = 0;
265 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200266 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100267 DBG_TRACE_DEVEL("leaving by closing K/A connection",
268 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200269 return 0;
270 }
271
Christopher Faulet9768c262018-10-22 09:34:31 +0200272 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200273 stream_inc_http_req_ctr(s);
274 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
275
Christopher Faulet9768c262018-10-22 09:34:31 +0200276 /* kill the pending keep-alive timeout */
277 txn->flags &= ~TX_WAIT_NEXT_RQ;
278 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200279
Christopher Faulet29f17582019-05-23 11:03:26 +0200280 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200281 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100282
Christopher Faulet9768c262018-10-22 09:34:31 +0200283 /* 0: we might have to print this header in debug mode */
284 if (unlikely((global.mode & MODE_DEBUG) &&
285 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
286 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200287
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200288 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200289
Christopher Fauleta3f15502019-05-13 15:27:23 +0200290 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200291 struct htx_blk *blk = htx_get_blk(htx, pos);
292 enum htx_blk_type type = htx_get_blk_type(blk);
293
294 if (type == HTX_BLK_EOH)
295 break;
296 if (type != HTX_BLK_HDR)
297 continue;
298
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200299 http_debug_hdr("clihdr", s,
300 htx_get_blk_name(htx, blk),
301 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200302 }
303 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200304
305 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100306 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200307 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100308 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100309 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200310 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100311 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +0100312 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100313 if (sl->flags & HTX_SL_F_BODYLESS)
314 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200315
316 /* we can make use of server redirect on GET and HEAD */
317 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
318 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100319 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200320 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200321 goto return_bad_req;
322 }
323
324 /*
Christopher Faulet6072beb2020-02-18 15:34:58 +0100325 * 2: check if the URI matches the monitor_uri. We have to do this for
326 * every request which gets in, because the monitor-uri is defined by
327 * the frontend. If the monitor-uri starts with a '/', the matching is
328 * done against the request's path. Otherwise, the request's uri is
329 * used. It is a workaround to let HTTP/2 health-checks work as
330 * expected.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200331 */
332 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Faulet6072beb2020-02-18 15:34:58 +0100333 ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)),
334 ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) ||
335 isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200336 /*
337 * We have found the monitor URI
338 */
339 struct acl_cond *cond;
340
341 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100342 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200343
344 /* Check if we want to fail this monitor request or not */
345 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
346 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
347
348 ret = acl_pass(ret);
349 if (cond->pol == ACL_COND_UNLESS)
350 ret = !ret;
351
352 if (ret) {
353 /* we fail this request, let's return 503 service unavail */
354 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200355 if (!(s->flags & SF_ERR_MASK))
356 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
357 goto return_prx_cond;
358 }
359 }
360
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800361 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200362 txn->status = 200;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200363 if (!(s->flags & SF_ERR_MASK))
364 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
365 goto return_prx_cond;
366 }
367
368 /*
369 * 3: Maybe we have to copy the original REQURI for the logs ?
370 * Note: we cannot log anymore if the request has been
371 * classified as invalid.
372 */
373 if (unlikely(s->logs.logwait & LW_REQ)) {
374 /* we have a complete HTTP request that we must log */
375 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200376 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200377
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200378 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200379 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200380
381 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
382 s->do_log(s);
383 } else {
384 ha_alert("HTTP logging : out of memory.\n");
385 }
386 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200387
Christopher Faulete0768eb2018-10-03 16:38:02 +0200388 /* if the frontend has "option http-use-proxy-header", we'll check if
389 * we have what looks like a proxied connection instead of a connection,
390 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
391 * Note that this is *not* RFC-compliant, however browsers and proxies
392 * happen to do that despite being non-standard :-(
393 * We consider that a request not beginning with either '/' or '*' is
394 * a proxied connection, which covers both "scheme://location" and
395 * CONNECT ip:port.
396 */
397 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100398 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200399 txn->flags |= TX_USE_PX_CONN;
400
Christopher Faulete0768eb2018-10-03 16:38:02 +0200401 /* 5: we may need to capture headers */
402 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200403 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200404
Christopher Faulete0768eb2018-10-03 16:38:02 +0200405 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200406 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200407 req->analysers |= AN_REQ_HTTP_BODY;
408
409 /*
410 * RFC7234#4:
411 * A cache MUST write through requests with methods
412 * that are unsafe (Section 4.2.1 of [RFC7231]) to
413 * the origin server; i.e., a cache is not allowed
414 * to generate a reply to such a request before
415 * having forwarded the request and having received
416 * a corresponding response.
417 *
418 * RFC7231#4.2.1:
419 * Of the request methods defined by this
420 * specification, the GET, HEAD, OPTIONS, and TRACE
421 * methods are defined to be safe.
422 */
423 if (likely(txn->meth == HTTP_METH_GET ||
424 txn->meth == HTTP_METH_HEAD ||
425 txn->meth == HTTP_METH_OPTIONS ||
426 txn->meth == HTTP_METH_TRACE))
427 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
428
429 /* end of job, return OK */
430 req->analysers &= ~an_bit;
431 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200432
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100433 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200434 return 1;
435
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200436 return_int_err:
437 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200438 if (!(s->flags & SF_ERR_MASK))
439 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100440 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200441 if (sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100442 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200443 goto return_prx_cond;
444
Christopher Faulete0768eb2018-10-03 16:38:02 +0200445 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200446 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100447 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200448 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100449 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200450 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200451
452 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200453 http_reply_and_close(s, txn->status, http_error_message(s));
454
Christopher Faulete0768eb2018-10-03 16:38:02 +0200455 if (!(s->flags & SF_ERR_MASK))
456 s->flags |= SF_ERR_PRXCOND;
457 if (!(s->flags & SF_FINST_MASK))
458 s->flags |= SF_FINST_R;
459
460 req->analysers &= AN_REQ_FLT_END;
461 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100462 DBG_TRACE_DEVEL("leaving on error",
463 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200464 return 0;
465}
466
467
468/* This stream analyser runs all HTTP request processing which is common to
469 * frontends and backends, which means blocking ACLs, filters, connection-close,
470 * reqadd, stats and redirects. This is performed for the designated proxy.
471 * It returns 1 if the processing can continue on next analysers, or zero if it
472 * either needs more data or wants to immediately abort the request (eg: deny,
473 * error, ...).
474 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200475int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200476{
477 struct session *sess = s->sess;
478 struct http_txn *txn = s->txn;
479 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200480 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200481 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200482 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200483 struct connection *conn = objt_conn(sess->origin);
484
485 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
486 /* we need more data */
487 goto return_prx_yield;
488 }
489
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100490 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200491
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100492 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200493
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200494 /* just in case we have some per-backend tracking. Only called the first
495 * execution of the analyser. */
496 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
497 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200498
499 /* evaluate http-request rules */
500 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100501 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200502
503 switch (verdict) {
504 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
505 goto return_prx_yield;
506
507 case HTTP_RULE_RES_CONT:
508 case HTTP_RULE_RES_STOP: /* nothing to do */
509 break;
510
511 case HTTP_RULE_RES_DENY: /* deny or tarpit */
512 if (txn->flags & TX_CLTARPIT)
513 goto tarpit;
514 goto deny;
515
516 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
517 goto return_prx_cond;
518
519 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
520 goto done;
521
522 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
523 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100524
525 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
526 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200527 }
528 }
529
530 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +0100531 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200532 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200533
Christopher Fauletff2759f2018-10-24 11:13:16 +0200534 ctx.blk = NULL;
535 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
536 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100537 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200538 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200539 }
540
541 /* OK at this stage, we know that the request was accepted according to
542 * the http-request rules, we can check for the stats. Note that the
543 * URI is detected *before* the req* rules in order not to be affected
544 * by a possible reqrep, while they are processed *after* so that a
545 * reqdeny can still block them. This clearly needs to change in 1.6!
546 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200547 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200548 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100549 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200550 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200551 if (!(s->flags & SF_ERR_MASK))
552 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100553 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200554 }
555
556 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200557 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100558 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200559 /* not all actions implemented: deny, allow, auth */
560
561 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
562 goto deny;
563
564 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
565 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100566
567 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
568 goto return_bad_req;
569
570 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
571 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200572 }
573
Christopher Faulet2571bc62019-03-01 11:44:26 +0100574 /* Proceed with the applets now. */
575 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200576 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100577 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200578
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200579 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100580 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100581
Christopher Faulete0768eb2018-10-03 16:38:02 +0200582 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
583 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
584 if (!(s->flags & SF_FINST_MASK))
585 s->flags |= SF_FINST_R;
586
587 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
588 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
589 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
590 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100591
592 req->flags |= CF_SEND_DONTWAIT;
593 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200594 goto done;
595 }
596
597 /* check whether we have some ACLs set to redirect this request */
598 list_for_each_entry(rule, &px->redirect_rules, list) {
599 if (rule->cond) {
600 int ret;
601
602 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
603 ret = acl_pass(ret);
604 if (rule->cond->pol == ACL_COND_UNLESS)
605 ret = !ret;
606 if (!ret)
607 continue;
608 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200609 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100610 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200611 goto done;
612 }
613
614 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
615 * If this happens, then the data will not come immediately, so we must
616 * send all what we have without waiting. Note that due to the small gain
617 * in waiting for the body of the request, it's easier to simply put the
618 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
619 * itself once used.
620 */
621 req->flags |= CF_SEND_DONTWAIT;
622
623 done: /* done with this analyser, continue with next ones that the calling
624 * points will have set, if any.
625 */
626 req->analyse_exp = TICK_ETERNITY;
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500627 done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200628 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100629 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200630 return 1;
631
632 tarpit:
633 /* Allow cookie logging
634 */
635 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200636 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200637
638 /* When a connection is tarpitted, we use the tarpit timeout,
639 * which may be the same as the connect timeout if unspecified.
640 * If unset, then set it to zero because we really want it to
641 * eventually expire. We build the tarpit as an analyser.
642 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100643 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200644
645 /* wipe the request out so that we can drop the connection early
646 * if the client closes first.
647 */
648 channel_dont_connect(req);
649
Christopher Faulete0768eb2018-10-03 16:38:02 +0200650 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
651 req->analysers |= AN_REQ_HTTP_TARPIT;
652 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
653 if (!req->analyse_exp)
654 req->analyse_exp = tick_add(now_ms, 0);
655 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100656 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100657 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100658 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200659 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100660 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200661 goto done_without_exp;
662
663 deny: /* this request was blocked (denied) */
664
665 /* Allow cookie logging
666 */
667 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200668 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200669
Christopher Faulete0768eb2018-10-03 16:38:02 +0200670 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200671 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100672 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100673 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100674 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200675 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100676 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100677 goto return_prx_err;
678
679 return_int_err:
680 txn->status = 500;
681 if (!(s->flags & SF_ERR_MASK))
682 s->flags |= SF_ERR_INTERNAL;
683 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100684 if (s->flags & SF_BE_ASSIGNED)
685 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100686 if (sess->listener->counters)
687 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
688 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200689
690 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200691 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100692 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200693 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100694 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100695 /* fall through */
696
697 return_prx_err:
698 http_reply_and_close(s, txn->status, http_error_message(s));
699 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200700
701 return_prx_cond:
702 if (!(s->flags & SF_ERR_MASK))
703 s->flags |= SF_ERR_PRXCOND;
704 if (!(s->flags & SF_FINST_MASK))
705 s->flags |= SF_FINST_R;
706
707 req->analysers &= AN_REQ_FLT_END;
708 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100709 DBG_TRACE_DEVEL("leaving on error",
710 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200711 return 0;
712
713 return_prx_yield:
714 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100715 DBG_TRACE_DEVEL("waiting for more data",
716 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200717 return 0;
718}
719
720/* This function performs all the processing enabled for the current request.
721 * It returns 1 if the processing can continue on next analysers, or zero if it
722 * needs more data, encounters an error, or wants to immediately abort the
723 * request. It relies on buffers flags, and updates s->req.analysers.
724 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200725int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200726{
727 struct session *sess = s->sess;
728 struct http_txn *txn = s->txn;
729 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200730 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200731 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
732
733 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
734 /* we need more data */
735 channel_dont_connect(req);
736 return 0;
737 }
738
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100739 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200740
741 /*
742 * Right now, we know that we have processed the entire headers
743 * and that unwanted requests have been filtered out. We can do
744 * whatever we want with the remaining request. Also, now we
745 * may have separate values for ->fe, ->be.
746 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100747 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200748
749 /*
750 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200751 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200752 */
753 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100754 struct htx_sl *sl;
755 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200756
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200757 if (!sockaddr_alloc(&s->target_addr)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200758 if (!(s->flags & SF_ERR_MASK))
759 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100760 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200761 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200762 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100763 uri = htx_sl_req_uri(sl);
764 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200765
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200766 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200767 goto return_bad_req;
768
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200769 s->target = &s->be->obj_type;
770 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
771
Christopher Faulete0768eb2018-10-03 16:38:02 +0200772 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200773 * uri.ptr and path.ptr (excluded). If it was not found, we need
774 * to replace from all the uri by a single "/".
775 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500776 * Instead of rewriting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100777 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200778 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200779 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100780 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200781 }
782
783 /*
784 * 7: Now we can work with the cookies.
785 * Note that doing so might move headers in the request, but
786 * the fields will stay coherent and the URI will not move.
787 * This should only be performed in the backend.
788 */
789 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200790 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200791
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100792 /* 8: Generate unique ID if a "unique-id-format" is defined.
793 *
794 * A unique ID is generated even when it is not sent to ensure that the ID can make use of
795 * fetches only available in the HTTP request processing stage.
796 */
797 if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) {
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100798 struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200799
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100800 if (!isttest(unique_id)) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100801 if (!(s->flags & SF_ERR_MASK))
802 s->flags |= SF_ERR_RESOURCE;
803 goto return_int_err;
804 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200805
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100806 /* send unique ID if a "unique-id-header" is defined */
Tim Duesterhus0643b0e2020-03-05 17:56:35 +0100807 if (isttest(sess->fe->header_unique_id) &&
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100808 unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id)))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100809 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200810 }
811
812 /*
813 * 9: add X-Forwarded-For if either the frontend or the backend
814 * asks for it.
815 */
816 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200817 struct http_hdr_ctx ctx = { .blk = NULL };
818 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
819 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
820
Christopher Faulete0768eb2018-10-03 16:38:02 +0200821 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200822 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200823 /* The header is set to be added only if none is present
824 * and we found it, so don't do anything.
825 */
826 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200827 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200828 /* Add an X-Forwarded-For header unless the source IP is
829 * in the 'except' network range.
830 */
831 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200832 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200833 != sess->fe->except_net.s_addr) &&
834 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200835 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200836 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200837 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200838
839 /* Note: we rely on the backend to get the header name to be used for
840 * x-forwarded-for, because the header is really meant for the backends.
841 * However, if the backend did not specify any option, we have to rely
842 * on the frontend's header name.
843 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200844 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
845 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100846 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200847 }
848 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200849 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200850 /* FIXME: for the sake of completeness, we should also support
851 * 'except' here, although it is mostly useless in this case.
852 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200853 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200854
Christopher Faulete0768eb2018-10-03 16:38:02 +0200855 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200856 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200857 pn, sizeof(pn));
858
859 /* Note: we rely on the backend to get the header name to be used for
860 * x-forwarded-for, because the header is really meant for the backends.
861 * However, if the backend did not specify any option, we have to rely
862 * on the frontend's header name.
863 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200864 chunk_printf(&trash, "%s", pn);
865 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100866 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200867 }
868 }
869
870 /*
871 * 10: add X-Original-To if either the frontend or the backend
872 * asks for it.
873 */
874 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
875
876 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200877 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 +0200878 /* Add an X-Original-To header unless the destination IP is
879 * in the 'except' network range.
880 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200881 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200882 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200883 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200884 != sess->fe->except_to.s_addr) &&
885 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200886 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200887 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200888 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200889 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200890
891 /* Note: we rely on the backend to get the header name to be used for
892 * x-original-to, because the header is really meant for the backends.
893 * However, if the backend did not specify any option, we have to rely
894 * on the frontend's header name.
895 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200896 if (s->be->orgto_hdr_len)
897 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
898 else
899 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200900
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200901 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
902 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100903 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200904 }
905 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200906 }
907
Christopher Faulete0768eb2018-10-03 16:38:02 +0200908 /* If we have no server assigned yet and we're balancing on url_param
909 * with a POST request, we may be interested in checking the body for
910 * that parameter. This will be done in another analyser.
911 */
912 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100913 s->txn->meth == HTTP_METH_POST &&
914 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200915 channel_dont_connect(req);
916 req->analysers |= AN_REQ_HTTP_BODY;
917 }
918
919 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
920 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100921
Christopher Faulete0768eb2018-10-03 16:38:02 +0200922 /* We expect some data from the client. Unless we know for sure
923 * we already have a full request, we have to re-enable quick-ack
924 * in case we previously disabled it, otherwise we might cause
925 * the client to delay further data.
926 */
927 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200928 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100929 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200930
931 /*************************************************************
932 * OK, that's finished for the headers. We have done what we *
933 * could. Let's switch to the DATA state. *
934 ************************************************************/
935 req->analyse_exp = TICK_ETERNITY;
936 req->analysers &= ~an_bit;
937
938 s->logs.tv_request = now;
939 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100940 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200941 return 1;
942
Christopher Fauletb8a53712019-12-16 11:29:38 +0100943 return_int_err:
944 txn->status = 500;
945 if (!(s->flags & SF_ERR_MASK))
946 s->flags |= SF_ERR_INTERNAL;
947 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100948 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +0100949 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100950 if (sess->listener->counters)
951 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
952 goto return_prx_cond;
953
Christopher Faulete0768eb2018-10-03 16:38:02 +0200954 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200955 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100956 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200957 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100958 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100959 /* fall through */
960
961 return_prx_cond:
962 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200963
964 if (!(s->flags & SF_ERR_MASK))
965 s->flags |= SF_ERR_PRXCOND;
966 if (!(s->flags & SF_FINST_MASK))
967 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100968
969 req->analysers &= AN_REQ_FLT_END;
970 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100971 DBG_TRACE_DEVEL("leaving on error",
972 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200973 return 0;
974}
975
976/* This function is an analyser which processes the HTTP tarpit. It always
977 * returns zero, at the beginning because it prevents any other processing
978 * from occurring, and at the end because it terminates the request.
979 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200980int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200981{
982 struct http_txn *txn = s->txn;
983
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100984 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200985 /* This connection is being tarpitted. The CLIENT side has
986 * already set the connect expiration date to the right
987 * timeout. We just have to check that the client is still
988 * there and that the timeout has not expired.
989 */
990 channel_dont_connect(req);
991 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100992 !tick_is_expired(req->analyse_exp, now_ms)) {
993 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
994 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200995 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100996 }
997
Christopher Faulete0768eb2018-10-03 16:38:02 +0200998
999 /* We will set the queue timer to the time spent, just for
1000 * logging purposes. We fake a 500 server error, so that the
1001 * attacker will not suspect his connection has been tarpitted.
1002 * It will not cause trouble to the logs because we can exclude
1003 * the tarpitted connections by filtering on the 'PT' status flags.
1004 */
1005 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1006
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02001007 http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL));
Christopher Faulet5cb513a2020-05-13 17:56:56 +02001008
1009 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001010 req->analysers &= AN_REQ_FLT_END;
1011 req->analyse_exp = TICK_ETERNITY;
1012
1013 if (!(s->flags & SF_ERR_MASK))
1014 s->flags |= SF_ERR_PRXCOND;
1015 if (!(s->flags & SF_FINST_MASK))
1016 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001017
1018 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001019 return 0;
1020}
1021
1022/* This function is an analyser which waits for the HTTP request body. It waits
1023 * for either the buffer to be full, or the full advertised contents to have
1024 * reached the buffer. It must only be called after the standard HTTP request
1025 * processing has occurred, because it expects the request to be parsed and will
1026 * look for the Expect header. It may send a 100-Continue interim response. It
1027 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
1028 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
1029 * needs to read more data, or 1 once it has completed its analysis.
1030 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001031int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001032{
1033 struct session *sess = s->sess;
1034 struct http_txn *txn = s->txn;
1035 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001036 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001037
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001038 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001039
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001040 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001041
Willy Tarreau4236f032019-03-05 10:43:32 +01001042 if (htx->flags & HTX_FL_PARSING_ERROR)
1043 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001044 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1045 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001046
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001047 if (msg->msg_state < HTTP_MSG_BODY)
1048 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001049
Christopher Faulete0768eb2018-10-03 16:38:02 +02001050 /* We have to parse the HTTP request body to find any required data.
1051 * "balance url_param check_post" should have been the only way to get
1052 * into this. We were brought here after HTTP header analysis, so all
1053 * related structures are ready.
1054 */
1055
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001056 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001057 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001058 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001059 }
1060
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001061 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001062
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001063 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1064 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001065 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001066 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001067 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001068 goto http_end;
1069
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001070 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001071 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1072 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001073 if (!(s->flags & SF_ERR_MASK))
1074 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001075 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1076 if (sess->listener->counters)
1077 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1078 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001079 }
1080
1081 /* we get here if we need to wait for more data */
1082 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1083 /* Not enough data. We'll re-use the http-request
1084 * timeout here. Ideally, we should set the timeout
1085 * relative to the accept() date. We just set the
1086 * request timeout once at the beginning of the
1087 * request.
1088 */
1089 channel_dont_connect(req);
1090 if (!tick_isset(req->analyse_exp))
1091 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001092 DBG_TRACE_DEVEL("waiting for more data",
1093 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001094 return 0;
1095 }
1096
1097 http_end:
1098 /* The situation will not evolve, so let's give up on the analysis. */
1099 s->logs.tv_request = now; /* update the request timer to reflect full request */
1100 req->analysers &= ~an_bit;
1101 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001102 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001103 return 1;
1104
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001105 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001106 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001107 if (!(s->flags & SF_ERR_MASK))
1108 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001109 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001110 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +01001111 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001112 if (sess->listener->counters)
1113 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
1114 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001115
Christopher Faulete0768eb2018-10-03 16:38:02 +02001116 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001117 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001118 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1119 if (sess->listener->counters)
1120 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1121 /* fall through */
1122
1123 return_prx_cond:
1124 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001125
1126 if (!(s->flags & SF_ERR_MASK))
1127 s->flags |= SF_ERR_PRXCOND;
1128 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001129 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001130
Christopher Faulete0768eb2018-10-03 16:38:02 +02001131 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001132 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001133 DBG_TRACE_DEVEL("leaving on error",
1134 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001135 return 0;
1136}
1137
1138/* This function is an analyser which forwards request body (including chunk
1139 * sizes if any). It is called as soon as we must forward, even if we forward
1140 * zero byte. The only situation where it must not be called is when we're in
1141 * tunnel mode and we want to forward till the close. It's used both to forward
1142 * remaining data and to resync after end of body. It expects the msg_state to
1143 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1144 * read more data, or 1 once we can go on with next request or end the stream.
1145 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1146 * bytes of pending data + the headers if not already done.
1147 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001148int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001149{
1150 struct session *sess = s->sess;
1151 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001152 struct http_msg *msg = &txn->req;
1153 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001154 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001155 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001156
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001157 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001158
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001159 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001160
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001161 if (htx->flags & HTX_FL_PARSING_ERROR)
1162 goto return_bad_req;
1163 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1164 goto return_int_err;
1165
Christopher Faulete0768eb2018-10-03 16:38:02 +02001166 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1167 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1168 /* Output closed while we were sending data. We must abort and
1169 * wake the other side up.
1170 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001171
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001172 /* Don't abort yet if we had L7 retries activated and it
1173 * was a write error, we may recover.
1174 */
1175 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001176 (s->si[1].flags & SI_FL_L7_RETRY)) {
1177 DBG_TRACE_DEVEL("leaving on L7 retry",
1178 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001179 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001180 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001181 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001182 http_end_request(s);
1183 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001184 DBG_TRACE_DEVEL("leaving on error",
1185 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001186 return 1;
1187 }
1188
1189 /* Note that we don't have to send 100-continue back because we don't
1190 * need the data to complete our job, and it's up to the server to
1191 * decide whether to return 100, 417 or anything else in return of
1192 * an "Expect: 100-continue" header.
1193 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001194 if (msg->msg_state == HTTP_MSG_BODY)
1195 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001196
Christopher Faulete0768eb2018-10-03 16:38:02 +02001197 /* in most states, we should abort in case of early close */
1198 channel_auto_close(req);
1199
1200 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001201 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001202 if (req->flags & CF_EOI)
1203 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001204 }
1205 else {
1206 /* We can't process the buffer's contents yet */
1207 req->flags |= CF_WAKE_WRITE;
1208 goto missing_data_or_waiting;
1209 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001210 }
1211
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001212 if (msg->msg_state >= HTTP_MSG_ENDING)
1213 goto ending;
1214
1215 if (txn->meth == HTTP_METH_CONNECT) {
1216 msg->msg_state = HTTP_MSG_ENDING;
1217 goto ending;
1218 }
1219
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001220 /* Forward input data. We get it by removing all outgoing data not
1221 * forwarded yet from HTX data size. If there are some data filters, we
1222 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001223 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001224 if (HAS_REQ_DATA_FILTERS(s)) {
1225 ret = flt_http_payload(s, msg, htx->data);
1226 if (ret < 0)
1227 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001228 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001229 }
1230 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001231 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001232 if (msg->flags & HTTP_MSGF_XFER_LEN)
1233 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001234 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001235
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001236 if (htx->data != co_data(req))
1237 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001238
Christopher Faulet9768c262018-10-22 09:34:31 +02001239 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001240 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1241 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001242 */
1243 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1244 goto missing_data_or_waiting;
1245
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001246 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001247
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001248 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001249 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1250
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001251 /* other states, ENDING...TUNNEL */
1252 if (msg->msg_state >= HTTP_MSG_DONE)
1253 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001254
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001255 if (HAS_REQ_DATA_FILTERS(s)) {
1256 ret = flt_http_end(s, msg);
1257 if (ret <= 0) {
1258 if (!ret)
1259 goto missing_data_or_waiting;
1260 goto return_bad_req;
1261 }
1262 }
1263
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001264 if (txn->meth == HTTP_METH_CONNECT)
1265 msg->msg_state = HTTP_MSG_TUNNEL;
1266 else {
1267 msg->msg_state = HTTP_MSG_DONE;
1268 req->to_forward = 0;
1269 }
1270
1271 done:
1272 /* we don't want to forward closes on DONE except in tunnel mode. */
1273 if (!(txn->flags & TX_CON_WANT_TUN))
1274 channel_dont_close(req);
1275
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001276 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001277 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001278 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001279 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1280 if (req->flags & CF_SHUTW) {
1281 /* request errors are most likely due to the
1282 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001283 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001284 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001285 goto return_bad_req;
1286 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001287 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001288 return 1;
1289 }
1290
1291 /* If "option abortonclose" is set on the backend, we want to monitor
1292 * the client's connection and forward any shutdown notification to the
1293 * server, which will decide whether to close or to go on processing the
1294 * request. We only do that in tunnel mode, and not in other modes since
1295 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001296 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001297 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001298 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001299 s->si[1].flags |= SI_FL_NOLINGER;
1300 channel_auto_close(req);
1301 }
1302 else if (s->txn->meth == HTTP_METH_POST) {
1303 /* POST requests may require to read extra CRLF sent by broken
1304 * browsers and which could cause an RST to be sent upon close
1305 * on some systems (eg: Linux). */
1306 channel_auto_read(req);
1307 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001308 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1309 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001310 return 0;
1311
1312 missing_data_or_waiting:
1313 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001314 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001315 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001316
1317 waiting:
1318 /* waiting for the last bits to leave the buffer */
1319 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001320 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001321
1322 /* When TE: chunked is used, we need to get there again to parse remaining
1323 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1324 * And when content-length is used, we never want to let the possible
1325 * shutdown be forwarded to the other side, as the state machine will
1326 * take care of it once the client responds. It's also important to
1327 * prevent TIME_WAITs from accumulating on the backend side, and for
1328 * HTTP/2 where the last frame comes with a shutdown.
1329 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001330 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001331 channel_dont_close(req);
1332
1333 /* We know that more data are expected, but we couldn't send more that
1334 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1335 * system knows it must not set a PUSH on this first part. Interactive
1336 * modes are already handled by the stream sock layer. We must not do
1337 * this in content-length mode because it could present the MSG_MORE
1338 * flag with the last block of forwarded data, which would cause an
1339 * additional delay to be observed by the receiver.
1340 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001341 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001342 req->flags |= CF_EXPECT_MORE;
1343
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001344 DBG_TRACE_DEVEL("waiting for more data to forward",
1345 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001346 return 0;
1347
Christopher Faulet93e02d82019-03-08 14:18:50 +01001348 return_cli_abort:
1349 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1350 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001351 if (sess->listener->counters)
1352 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001353 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001354 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001355 if (!(s->flags & SF_ERR_MASK))
1356 s->flags |= SF_ERR_CLICL;
1357 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001358 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001359
1360 return_srv_abort:
1361 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1362 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001363 if (sess->listener->counters)
1364 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001365 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001366 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001367 if (!(s->flags & SF_ERR_MASK))
1368 s->flags |= SF_ERR_SRVCL;
1369 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001370 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001371
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001372 return_int_err:
1373 if (!(s->flags & SF_ERR_MASK))
1374 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001375 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001376 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001377 if (sess->listener->counters)
1378 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001379 if (objt_server(s->target))
1380 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001381 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001382 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001383
Christopher Faulet93e02d82019-03-08 14:18:50 +01001384 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001385 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001386 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001387 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001388 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001389 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001390
Christopher Fauletb8a53712019-12-16 11:29:38 +01001391 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001392 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001393 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001394 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001395 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001396 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001397 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001398 }
1399 req->analysers &= AN_REQ_FLT_END;
1400 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 +01001401 if (!(s->flags & SF_ERR_MASK))
1402 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001403 if (!(s->flags & SF_FINST_MASK))
1404 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001405 DBG_TRACE_DEVEL("leaving on error ",
1406 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001407 return 0;
1408}
1409
Olivier Houcharda254a372019-04-05 15:30:12 +02001410/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1411/* Returns 0 if we can attempt to retry, -1 otherwise */
1412static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1413{
1414 struct channel *req, *res;
1415 int co_data;
1416
1417 si->conn_retries--;
1418 if (si->conn_retries < 0)
1419 return -1;
1420
Willy Tarreau223995e2019-05-04 10:38:31 +02001421 if (objt_server(s->target))
1422 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1423 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1424
Olivier Houcharda254a372019-04-05 15:30:12 +02001425 req = &s->req;
1426 res = &s->res;
1427 /* Remove any write error from the request, and read error from the response */
1428 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1429 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1430 res->analysers = 0;
1431 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001432 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001433 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001434 si->exp = TICK_ETERNITY;
1435 res->rex = TICK_ETERNITY;
1436 res->to_forward = 0;
1437 res->analyse_exp = TICK_ETERNITY;
1438 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001439 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001440 si_release_endpoint(&s->si[1]);
1441 b_free(&req->buf);
1442 /* Swap the L7 buffer with the channel buffer */
1443 /* We know we stored the co_data as b_data, so get it there */
1444 co_data = b_data(&si->l7_buffer);
1445 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1446 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1447
1448 co_set_data(req, co_data);
1449 b_reset(&res->buf);
1450 co_set_data(res, 0);
1451 return 0;
1452}
1453
Christopher Faulete0768eb2018-10-03 16:38:02 +02001454/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1455 * processing can continue on next analysers, or zero if it either needs more
1456 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1457 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1458 * when it has nothing left to do, and may remove any analyser when it wants to
1459 * abort.
1460 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001461int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001462{
Christopher Faulet9768c262018-10-22 09:34:31 +02001463 /*
1464 * We will analyze a complete HTTP response to check the its syntax.
1465 *
1466 * Once the start line and all headers are received, we may perform a
1467 * capture of the error (if any), and we will set a few fields. We also
1468 * logging and finally headers capture.
1469 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001470 struct session *sess = s->sess;
1471 struct http_txn *txn = s->txn;
1472 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001473 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001474 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001475 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001476 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001477 int n;
1478
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001479 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001480
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001481 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001482
Willy Tarreau4236f032019-03-05 10:43:32 +01001483 /* Parsing errors are caught here */
1484 if (htx->flags & HTX_FL_PARSING_ERROR)
1485 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001486 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1487 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001488
Christopher Faulete0768eb2018-10-03 16:38:02 +02001489 /*
1490 * Now we quickly check if we have found a full valid response.
1491 * If not so, we check the FD and buffer states before leaving.
1492 * A full response is indicated by the fact that we have seen
1493 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1494 * responses are checked first.
1495 *
1496 * Depending on whether the client is still there or not, we
1497 * may send an error response back or not. Note that normally
1498 * we should only check for HTTP status there, and check I/O
1499 * errors somewhere else.
1500 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001501 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001502 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001503 /* 1: have we encountered a read error ? */
1504 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001505 struct connection *conn = NULL;
1506
Olivier Houchard865d8392019-05-03 22:46:27 +02001507 if (objt_cs(s->si[1].end))
1508 conn = objt_cs(s->si[1].end)->conn;
1509
1510 if (si_b->flags & SI_FL_L7_RETRY &&
1511 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001512 /* If we arrive here, then CF_READ_ERROR was
1513 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001514 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001515 * the SI_FL_L7_RETRY flag, so it's ok not
1516 * to check s->be->retry_type.
1517 */
1518 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1519 return 0;
1520 }
1521
Olivier Houchard6db16992019-05-17 15:40:49 +02001522 if (txn->flags & TX_NOT_FIRST)
1523 goto abort_keep_alive;
1524
Olivier Houcharda798bf52019-03-08 18:52:00 +01001525 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001526 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001527 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001528 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001529 }
1530
Christopher Faulete0768eb2018-10-03 16:38:02 +02001531 rep->analysers &= AN_RES_FLT_END;
1532 txn->status = 502;
1533
1534 /* Check to see if the server refused the early data.
1535 * If so, just send a 425
1536 */
Willy Tarreauee99aaf2020-06-23 05:58:20 +02001537 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001538 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001539 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001540 do_l7_retry(s, si_b) == 0) {
1541 DBG_TRACE_DEVEL("leaving on L7 retry",
1542 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001543 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001544 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001545 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001546 }
1547
1548 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001549 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001550
1551 if (!(s->flags & SF_ERR_MASK))
1552 s->flags |= SF_ERR_SRVCL;
1553 if (!(s->flags & SF_FINST_MASK))
1554 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001555 DBG_TRACE_DEVEL("leaving on error",
1556 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001557 return 0;
1558 }
1559
Christopher Faulet9768c262018-10-22 09:34:31 +02001560 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001561 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001562 if ((si_b->flags & SI_FL_L7_RETRY) &&
1563 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001564 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1565 DBG_TRACE_DEVEL("leaving on L7 retry",
1566 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001567 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001568 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001569 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001570 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001571 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001572 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001573 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001574 }
1575
Christopher Faulete0768eb2018-10-03 16:38:02 +02001576 rep->analysers &= AN_RES_FLT_END;
1577 txn->status = 504;
1578 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001579 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001580
1581 if (!(s->flags & SF_ERR_MASK))
1582 s->flags |= SF_ERR_SRVTO;
1583 if (!(s->flags & SF_FINST_MASK))
1584 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001585 DBG_TRACE_DEVEL("leaving on error",
1586 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001587 return 0;
1588 }
1589
Christopher Faulet9768c262018-10-22 09:34:31 +02001590 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001591 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001592 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1593 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001594 if (sess->listener->counters)
1595 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001596 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001597 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001598
1599 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001600 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001601 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001602
1603 if (!(s->flags & SF_ERR_MASK))
1604 s->flags |= SF_ERR_CLICL;
1605 if (!(s->flags & SF_FINST_MASK))
1606 s->flags |= SF_FINST_H;
1607
1608 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001609 DBG_TRACE_DEVEL("leaving on error",
1610 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001611 return 0;
1612 }
1613
Christopher Faulet9768c262018-10-22 09:34:31 +02001614 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001615 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001616 if ((si_b->flags & SI_FL_L7_RETRY) &&
1617 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001618 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1619 DBG_TRACE_DEVEL("leaving on L7 retry",
1620 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001621 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001622 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001623 }
1624
Olivier Houchard6db16992019-05-17 15:40:49 +02001625 if (txn->flags & TX_NOT_FIRST)
1626 goto abort_keep_alive;
1627
Olivier Houcharda798bf52019-03-08 18:52:00 +01001628 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001629 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001630 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001631 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001632 }
1633
Christopher Faulete0768eb2018-10-03 16:38:02 +02001634 rep->analysers &= AN_RES_FLT_END;
1635 txn->status = 502;
1636 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001637 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001638
1639 if (!(s->flags & SF_ERR_MASK))
1640 s->flags |= SF_ERR_SRVCL;
1641 if (!(s->flags & SF_FINST_MASK))
1642 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001643 DBG_TRACE_DEVEL("leaving on error",
1644 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001645 return 0;
1646 }
1647
Christopher Faulet9768c262018-10-22 09:34:31 +02001648 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001649 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001650 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001651 goto abort_keep_alive;
1652
Olivier Houcharda798bf52019-03-08 18:52:00 +01001653 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001654 if (objt_server(s->target))
1655 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001656 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001657
1658 if (!(s->flags & SF_ERR_MASK))
1659 s->flags |= SF_ERR_CLICL;
1660 if (!(s->flags & SF_FINST_MASK))
1661 s->flags |= SF_FINST_H;
1662
1663 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001664 DBG_TRACE_DEVEL("leaving on error",
1665 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001666 return 0;
1667 }
1668
1669 channel_dont_close(rep);
1670 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001671 DBG_TRACE_DEVEL("waiting for more data",
1672 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001673 return 0;
1674 }
1675
1676 /* More interesting part now : we know that we have a complete
1677 * response which at least looks like HTTP. We have an indicator
1678 * of each header's length, so we can parse them quickly.
1679 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001680 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001681 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001682 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001683
Christopher Faulet9768c262018-10-22 09:34:31 +02001684 /* 0: we might have to print this header in debug mode */
1685 if (unlikely((global.mode & MODE_DEBUG) &&
1686 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1687 int32_t pos;
1688
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001689 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001690
Christopher Fauleta3f15502019-05-13 15:27:23 +02001691 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001692 struct htx_blk *blk = htx_get_blk(htx, pos);
1693 enum htx_blk_type type = htx_get_blk_type(blk);
1694
1695 if (type == HTX_BLK_EOH)
1696 break;
1697 if (type != HTX_BLK_HDR)
1698 continue;
1699
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001700 http_debug_hdr("srvhdr", s,
1701 htx_get_blk_name(htx, blk),
1702 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001703 }
1704 }
1705
Christopher Faulet03599112018-11-27 11:21:21 +01001706 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001707 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001708 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001709 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001710 if (sl->flags & HTX_SL_F_XFER_LEN) {
1711 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001712 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001713 if (sl->flags & HTX_SL_F_BODYLESS)
1714 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001715 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001716
1717 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001718 if (n < 1 || n > 5)
1719 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001720
Christopher Faulete0768eb2018-10-03 16:38:02 +02001721 /* when the client triggers a 4xx from the server, it's most often due
1722 * to a missing object or permission. These events should be tracked
1723 * because if they happen often, it may indicate a brute force or a
1724 * vulnerability scan.
1725 */
1726 if (n == 4)
1727 stream_inc_http_err_ctr(s);
1728
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001729 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001730 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001731 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1732 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001733
Christopher Faulete0768eb2018-10-03 16:38:02 +02001734 /* Adjust server's health based on status code. Note: status codes 501
1735 * and 505 are triggered on demand by client request, so we must not
1736 * count them as server failures.
1737 */
1738 if (objt_server(s->target)) {
1739 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001740 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001741 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001742 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001743 }
1744
1745 /*
1746 * We may be facing a 100-continue response, or any other informational
1747 * 1xx response which is non-final, in which case this is not the right
1748 * response, and we're waiting for the next one. Let's allow this response
1749 * to go to the client and wait for the next one. There's an exception for
1750 * 101 which is used later in the code to switch protocols.
1751 */
1752 if (txn->status < 200 &&
1753 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001754 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001755 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001756 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001757 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001758 txn->status = 0;
1759 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001760 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001761 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001762 }
1763
1764 /*
1765 * 2: check for cacheability.
1766 */
1767
1768 switch (txn->status) {
1769 case 200:
1770 case 203:
1771 case 204:
1772 case 206:
1773 case 300:
1774 case 301:
1775 case 404:
1776 case 405:
1777 case 410:
1778 case 414:
1779 case 501:
1780 break;
1781 default:
1782 /* RFC7231#6.1:
1783 * Responses with status codes that are defined as
1784 * cacheable by default (e.g., 200, 203, 204, 206,
1785 * 300, 301, 404, 405, 410, 414, and 501 in this
1786 * specification) can be reused by a cache with
1787 * heuristic expiration unless otherwise indicated
1788 * by the method definition or explicit cache
1789 * controls [RFC7234]; all other status codes are
1790 * not cacheable by default.
1791 */
1792 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1793 break;
1794 }
1795
1796 /*
1797 * 3: we may need to capture headers
1798 */
1799 s->logs.logwait &= ~LW_RESP;
1800 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001801 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001802
Christopher Faulet9768c262018-10-22 09:34:31 +02001803 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001804 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1805 txn->status == 101)) {
1806 /* Either we've established an explicit tunnel, or we're
1807 * switching the protocol. In both cases, we're very unlikely
1808 * to understand the next protocols. We have to switch to tunnel
1809 * mode, so that we transfer the request and responses then let
1810 * this protocol pass unmodified. When we later implement specific
1811 * parsers for such protocols, we'll want to check the Upgrade
1812 * header which contains information about that protocol for
1813 * responses with status 101 (eg: see RFC2817 about TLS).
1814 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001815 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001816 }
1817
Christopher Faulet61608322018-11-23 16:23:45 +01001818 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1819 * 407 (Proxy-Authenticate) responses and set the connection to private
1820 */
1821 srv_conn = cs_conn(objt_cs(s->si[1].end));
1822 if (srv_conn) {
1823 struct ist hdr;
1824 struct http_hdr_ctx ctx;
1825
1826 if (txn->status == 401)
1827 hdr = ist("WWW-Authenticate");
1828 else if (txn->status == 407)
1829 hdr = ist("Proxy-Authenticate");
1830 else
1831 goto end;
1832
1833 ctx.blk = NULL;
1834 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001835 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1836 * possibly followed by blanks and a base64 string, the connection
1837 * is private. Since it's a mess to deal with, we only check for
1838 * values starting with "NTLM" or "Nego". Note that often multiple
1839 * headers are sent by the server there.
1840 */
1841 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001842 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001843 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001844 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001845 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001846 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001847 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001848 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001849 }
Christopher Faulet61608322018-11-23 16:23:45 +01001850 }
1851 }
1852
1853 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001854 /* we want to have the response time before we start processing it */
1855 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1856
1857 /* end of job, return OK */
1858 rep->analysers &= ~an_bit;
1859 rep->analyse_exp = TICK_ETERNITY;
1860 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001861 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001862 return 1;
1863
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001864 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001865 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001866 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001867 if (sess->listener->counters)
1868 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001869 if (objt_server(s->target))
1870 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001871 txn->status = 500;
1872 if (!(s->flags & SF_ERR_MASK))
1873 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001874 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001875
1876 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001877 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001878 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001879 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001880 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001881 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001882 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001883 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001884 do_l7_retry(s, si_b) == 0) {
1885 DBG_TRACE_DEVEL("leaving on L7 retry",
1886 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001887 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001888 }
Christopher Faulet47365272018-10-31 17:40:50 +01001889 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001890 /* fall through */
1891
Christopher Fauletb8a53712019-12-16 11:29:38 +01001892 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001893 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001894
1895 if (!(s->flags & SF_ERR_MASK))
1896 s->flags |= SF_ERR_PRXCOND;
1897 if (!(s->flags & SF_FINST_MASK))
1898 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001899
1900 s->si[1].flags |= SI_FL_NOLINGER;
1901 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001902 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001903 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001904 DBG_TRACE_DEVEL("leaving on error",
1905 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001906 return 0;
1907
Christopher Faulete0768eb2018-10-03 16:38:02 +02001908 abort_keep_alive:
1909 /* A keep-alive request to the server failed on a network error.
1910 * The client is required to retry. We need to close without returning
1911 * any other information so that the client retries.
1912 */
1913 txn->status = 0;
1914 rep->analysers &= AN_RES_FLT_END;
1915 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001916 s->logs.logwait = 0;
1917 s->logs.level = 0;
1918 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001919 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001920 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1921 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001922 return 0;
1923}
1924
1925/* This function performs all the processing enabled for the current response.
1926 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1927 * and updates s->res.analysers. It might make sense to explode it into several
1928 * other functions. It works like process_request (see indications above).
1929 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001930int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001931{
1932 struct session *sess = s->sess;
1933 struct http_txn *txn = s->txn;
1934 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001935 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001936 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001937 enum rule_result ret = HTTP_RULE_RES_CONT;
1938
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001939 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1940 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001941
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001942 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001943
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001944 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001945
1946 /* The stats applet needs to adjust the Connection header but we don't
1947 * apply any filter there.
1948 */
1949 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1950 rep->analysers &= ~an_bit;
1951 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001952 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001953 }
1954
1955 /*
1956 * We will have to evaluate the filters.
1957 * As opposed to version 1.2, now they will be evaluated in the
1958 * filters order and not in the header order. This means that
1959 * each filter has to be validated among all headers.
1960 *
1961 * Filters are tried with ->be first, then with ->fe if it is
1962 * different from ->be.
1963 *
1964 * Maybe we are in resume condiion. In this case I choose the
1965 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001966 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001967 * the process with the first one.
1968 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001969 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001970 * pointer and the ->fe rule list. If it doesn't match, I initialize
1971 * the loop with the ->be.
1972 */
1973 if (s->current_rule_list == &sess->fe->http_res_rules)
1974 cur_proxy = sess->fe;
1975 else
1976 cur_proxy = s->be;
1977 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001978 /* evaluate http-response rules */
1979 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001980 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001981
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001982 switch (ret) {
1983 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1984 goto return_prx_yield;
1985
1986 case HTTP_RULE_RES_CONT:
1987 case HTTP_RULE_RES_STOP: /* nothing to do */
1988 break;
1989
1990 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1991 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001992
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001993 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1994 goto return_prx_cond;
1995
1996 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001997 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001998
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001999 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2000 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002001
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002002 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
2003 goto return_int_err;
2004 }
2005
2006 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002007
Christopher Faulete0768eb2018-10-03 16:38:02 +02002008 /* check whether we're already working on the frontend */
2009 if (cur_proxy == sess->fe)
2010 break;
2011 cur_proxy = sess->fe;
2012 }
2013
Christopher Faulete0768eb2018-10-03 16:38:02 +02002014 /* OK that's all we can do for 1xx responses */
2015 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002016 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002017
2018 /*
2019 * Now check for a server cookie.
2020 */
2021 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002022 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002023
2024 /*
2025 * Check for cache-control or pragma headers if required.
2026 */
2027 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002028 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002029
2030 /*
2031 * Add server cookie in the response if needed
2032 */
2033 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
2034 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
2035 (!(s->flags & SF_DIRECT) ||
2036 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
2037 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
2038 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
2039 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
2040 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
2041 !(s->flags & SF_IGNORE_PRST)) {
2042 /* the server is known, it's not the one the client requested, or the
2043 * cookie's last seen date needs to be refreshed. We have to
2044 * insert a set-cookie here, except if we want to insert only on POST
2045 * requests and this one isn't. Note that servers which don't have cookies
2046 * (eg: some backup servers) will return a full cookie removal request.
2047 */
2048 if (!objt_server(s->target)->cookie) {
2049 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002050 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02002051 s->be->cookie_name);
2052 }
2053 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002054 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002055
2056 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2057 /* emit last_date, which is mandatory */
2058 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2059 s30tob64((date.tv_sec+3) >> 2,
2060 trash.area + trash.data);
2061 trash.data += 5;
2062
2063 if (s->be->cookie_maxlife) {
2064 /* emit first_date, which is either the original one or
2065 * the current date.
2066 */
2067 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2068 s30tob64(txn->cookie_first_date ?
2069 txn->cookie_first_date >> 2 :
2070 (date.tv_sec+3) >> 2,
2071 trash.area + trash.data);
2072 trash.data += 5;
2073 }
2074 }
2075 chunk_appendf(&trash, "; path=/");
2076 }
2077
2078 if (s->be->cookie_domain)
2079 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2080
2081 if (s->be->ck_opts & PR_CK_HTTPONLY)
2082 chunk_appendf(&trash, "; HttpOnly");
2083
2084 if (s->be->ck_opts & PR_CK_SECURE)
2085 chunk_appendf(&trash, "; Secure");
2086
Christopher Faulet2f533902020-01-21 11:06:48 +01002087 if (s->be->cookie_attrs)
2088 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
2089
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002090 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002091 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002092
2093 txn->flags &= ~TX_SCK_MASK;
2094 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2095 /* the server did not change, only the date was updated */
2096 txn->flags |= TX_SCK_UPDATED;
2097 else
2098 txn->flags |= TX_SCK_INSERTED;
2099
2100 /* Here, we will tell an eventual cache on the client side that we don't
2101 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2102 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2103 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2104 */
2105 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2106
2107 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2108
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002109 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002110 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002111 }
2112 }
2113
2114 /*
2115 * Check if result will be cacheable with a cookie.
2116 * We'll block the response if security checks have caught
2117 * nasty things such as a cacheable cookie.
2118 */
2119 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2120 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2121 (s->be->options & PR_O_CHK_CACHE)) {
2122 /* we're in presence of a cacheable response containing
2123 * a set-cookie header. We'll block it as requested by
2124 * the 'checkcache' option, and send an alert.
2125 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002126 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2127 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2128 send_log(s->be, LOG_ALERT,
2129 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2130 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002131 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002132 }
2133
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002134 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002135 /*
2136 * Evaluate after-response rules before forwarding the response. rules
2137 * from the backend are evaluated first, then one from the frontend if
2138 * it differs.
2139 */
2140 if (!http_eval_after_res_rules(s))
2141 goto return_int_err;
2142
Christopher Faulete0768eb2018-10-03 16:38:02 +02002143 /* Always enter in the body analyzer */
2144 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2145 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2146
2147 /* if the user wants to log as soon as possible, without counting
2148 * bytes from the server, then this is the right moment. We have
2149 * to temporarily assign bytes_out to log what we currently have.
2150 */
2151 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2152 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002153 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002154 s->do_log(s);
2155 s->logs.bytes_out = 0;
2156 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002157
Christopher Fauletb8a53712019-12-16 11:29:38 +01002158 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002159 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002160 rep->analysers &= ~an_bit;
2161 rep->analyse_exp = TICK_ETERNITY;
2162 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002163
Christopher Fauletb8a53712019-12-16 11:29:38 +01002164 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002165 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002166 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002167 if (sess->listener->counters)
2168 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002169 if (objt_server(s->target))
2170 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002171 goto return_prx_err;
2172
2173 return_int_err:
2174 txn->status = 500;
2175 if (!(s->flags & SF_ERR_MASK))
2176 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002177 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002178 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2179 if (objt_server(s->target))
2180 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002181 if (objt_server(s->target))
2182 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002183 goto return_prx_err;
2184
2185 return_bad_res:
2186 txn->status = 502;
Christopher Fauleta20a6532020-02-05 10:16:41 +01002187 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2188 if (objt_server(s->target)) {
2189 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2190 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2191 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002192 /* fall through */
2193
2194 return_prx_err:
2195 http_reply_and_close(s, txn->status, http_error_message(s));
2196 /* fall through */
2197
2198 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002199 s->logs.t_data = -1; /* was not a valid response */
2200 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002201
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002202 if (!(s->flags & SF_ERR_MASK))
2203 s->flags |= SF_ERR_PRXCOND;
2204 if (!(s->flags & SF_FINST_MASK))
2205 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002206
Christopher Faulete58c0002020-03-02 16:21:01 +01002207 rep->analysers &= AN_RES_FLT_END;
2208 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002209 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002210 DBG_TRACE_DEVEL("leaving on error",
2211 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002212 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002213
2214 return_prx_yield:
2215 channel_dont_close(rep);
2216 DBG_TRACE_DEVEL("waiting for more data",
2217 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2218 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002219}
2220
2221/* This function is an analyser which forwards response body (including chunk
2222 * sizes if any). It is called as soon as we must forward, even if we forward
2223 * zero byte. The only situation where it must not be called is when we're in
2224 * tunnel mode and we want to forward till the close. It's used both to forward
2225 * remaining data and to resync after end of body. It expects the msg_state to
2226 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2227 * read more data, or 1 once we can go on with next request or end the stream.
2228 *
2229 * It is capable of compressing response data both in content-length mode and
2230 * in chunked mode. The state machines follows different flows depending on
2231 * whether content-length and chunked modes are used, since there are no
2232 * trailers in content-length :
2233 *
2234 * chk-mode cl-mode
2235 * ,----- BODY -----.
2236 * / \
2237 * V size > 0 V chk-mode
2238 * .--> SIZE -------------> DATA -------------> CRLF
2239 * | | size == 0 | last byte |
2240 * | v final crlf v inspected |
2241 * | TRAILERS -----------> DONE |
2242 * | |
2243 * `----------------------------------------------'
2244 *
2245 * Compression only happens in the DATA state, and must be flushed in final
2246 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2247 * is performed at once on final states for all bytes parsed, or when leaving
2248 * on missing data.
2249 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002250int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002251{
2252 struct session *sess = s->sess;
2253 struct http_txn *txn = s->txn;
2254 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002255 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002256 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002257
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002258 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002259
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002260 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002261
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002262 if (htx->flags & HTX_FL_PARSING_ERROR)
2263 goto return_bad_res;
2264 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2265 goto return_int_err;
2266
Christopher Faulete0768eb2018-10-03 16:38:02 +02002267 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002268 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002269 /* Output closed while we were sending data. We must abort and
2270 * wake the other side up.
2271 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002272 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002273 http_end_response(s);
2274 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002275 DBG_TRACE_DEVEL("leaving on error",
2276 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002277 return 1;
2278 }
2279
Christopher Faulet9768c262018-10-22 09:34:31 +02002280 if (msg->msg_state == HTTP_MSG_BODY)
2281 msg->msg_state = HTTP_MSG_DATA;
2282
Christopher Faulete0768eb2018-10-03 16:38:02 +02002283 /* in most states, we should abort in case of early close */
2284 channel_auto_close(res);
2285
Christopher Faulete0768eb2018-10-03 16:38:02 +02002286 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002287 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002288 if (res->flags & CF_EOI)
2289 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002290 }
2291 else {
2292 /* We can't process the buffer's contents yet */
2293 res->flags |= CF_WAKE_WRITE;
2294 goto missing_data_or_waiting;
2295 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002296 }
2297
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002298 if (msg->msg_state >= HTTP_MSG_ENDING)
2299 goto ending;
2300
2301 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2302 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2303 msg->msg_state = HTTP_MSG_ENDING;
2304 goto ending;
2305 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002306
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002307 /* Forward input data. We get it by removing all outgoing data not
2308 * forwarded yet from HTX data size. If there are some data filters, we
2309 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002310 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002311 if (HAS_RSP_DATA_FILTERS(s)) {
2312 ret = flt_http_payload(s, msg, htx->data);
2313 if (ret < 0)
2314 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002315 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002316 }
2317 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002318 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002319 if (msg->flags & HTTP_MSGF_XFER_LEN)
2320 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002321 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002322
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002323 if (htx->data != co_data(res))
2324 goto missing_data_or_waiting;
2325
2326 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2327 msg->msg_state = HTTP_MSG_ENDING;
2328 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002329 }
2330
Christopher Faulet9768c262018-10-22 09:34:31 +02002331 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002332 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2333 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002334 */
2335 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2336 goto missing_data_or_waiting;
2337
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002338 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002339
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002340 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002341 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2342
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002343 /* other states, ENDING...TUNNEL */
2344 if (msg->msg_state >= HTTP_MSG_DONE)
2345 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002346
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002347 if (HAS_RSP_DATA_FILTERS(s)) {
2348 ret = flt_http_end(s, msg);
2349 if (ret <= 0) {
2350 if (!ret)
2351 goto missing_data_or_waiting;
2352 goto return_bad_res;
2353 }
2354 }
2355
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002356 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2357 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2358 msg->msg_state = HTTP_MSG_TUNNEL;
2359 goto ending;
2360 }
2361 else {
2362 msg->msg_state = HTTP_MSG_DONE;
2363 res->to_forward = 0;
2364 }
2365
2366 done:
2367
2368 channel_dont_close(res);
2369
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002370 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002371 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002372 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002373 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2374 if (res->flags & CF_SHUTW) {
2375 /* response errors are most likely due to the
2376 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002377 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002378 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002379 goto return_bad_res;
2380 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002381 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002382 return 1;
2383 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002384 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2385 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002386 return 0;
2387
2388 missing_data_or_waiting:
2389 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002390 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002391
2392 /* stop waiting for data if the input is closed before the end. If the
2393 * client side was already closed, it means that the client has aborted,
2394 * so we don't want to count this as a server abort. Otherwise it's a
2395 * server abort.
2396 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002397 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002398 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002399 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002400 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002401 if (htx_is_empty(htx))
2402 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002403 }
2404
Christopher Faulete0768eb2018-10-03 16:38:02 +02002405 /* When TE: chunked is used, we need to get there again to parse
2406 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002407 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2408 * are filters registered on the stream, we don't want to forward a
2409 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002410 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002411 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002412 channel_dont_close(res);
2413
2414 /* We know that more data are expected, but we couldn't send more that
2415 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2416 * system knows it must not set a PUSH on this first part. Interactive
2417 * modes are already handled by the stream sock layer. We must not do
2418 * this in content-length mode because it could present the MSG_MORE
2419 * flag with the last block of forwarded data, which would cause an
2420 * additional delay to be observed by the receiver.
2421 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002422 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002423 res->flags |= CF_EXPECT_MORE;
2424
2425 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002426 DBG_TRACE_DEVEL("waiting for more data to forward",
2427 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002428 return 0;
2429
Christopher Faulet93e02d82019-03-08 14:18:50 +01002430 return_srv_abort:
2431 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2432 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002433 if (sess->listener->counters)
2434 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002435 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002436 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002437 if (!(s->flags & SF_ERR_MASK))
2438 s->flags |= SF_ERR_SRVCL;
2439 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002440
Christopher Faulet93e02d82019-03-08 14:18:50 +01002441 return_cli_abort:
2442 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2443 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002444 if (sess->listener->counters)
2445 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002446 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002447 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002448 if (!(s->flags & SF_ERR_MASK))
2449 s->flags |= SF_ERR_CLICL;
2450 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002451
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002452 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002453 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002454 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002455 if (sess->listener->counters)
2456 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002457 if (objt_server(s->target))
2458 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002459 if (!(s->flags & SF_ERR_MASK))
2460 s->flags |= SF_ERR_INTERNAL;
2461 goto return_error;
2462
Christopher Faulet93e02d82019-03-08 14:18:50 +01002463 return_bad_res:
2464 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2465 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002466 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002467 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2468 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002469 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002470 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002471 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002472
Christopher Faulet93e02d82019-03-08 14:18:50 +01002473 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002474 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002475 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002476 res->analysers &= AN_RES_FLT_END;
2477 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 +02002478 if (!(s->flags & SF_FINST_MASK))
2479 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002480 DBG_TRACE_DEVEL("leaving on error",
2481 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002482 return 0;
2483}
2484
Christopher Fauletf2824e62018-10-01 12:12:37 +02002485/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002486 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002487 * as too large a request to build a valid response.
2488 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002489int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002490{
Christopher Faulet99daf282018-11-28 22:58:13 +01002491 struct channel *req = &s->req;
2492 struct channel *res = &s->res;
2493 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002494 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002495 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002496 struct ist status, reason, location;
2497 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002498 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002499
2500 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002501 if (!chunk) {
2502 if (!(s->flags & SF_ERR_MASK))
2503 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002504 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002505 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002506
Christopher Faulet99daf282018-11-28 22:58:13 +01002507 /*
2508 * Create the location
2509 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002510 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002511 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002512 case REDIRECT_TYPE_SCHEME: {
2513 struct http_hdr_ctx ctx;
2514 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002515
Christopher Faulet99daf282018-11-28 22:58:13 +01002516 host = ist("");
2517 ctx.blk = NULL;
2518 if (http_find_header(htx, ist("Host"), &ctx, 0))
2519 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002520
Christopher Faulet297fbb42019-05-13 14:41:27 +02002521 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002522 path = http_get_path(htx_sl_req_uri(sl));
2523 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002524 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002525 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2526 int qs = 0;
2527 while (qs < path.len) {
2528 if (*(path.ptr + qs) == '?') {
2529 path.len = qs;
2530 break;
2531 }
2532 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002533 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002534 }
2535 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002536 else
2537 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002538
Christopher Faulet99daf282018-11-28 22:58:13 +01002539 if (rule->rdr_str) { /* this is an old "redirect" rule */
2540 /* add scheme */
2541 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2542 goto fail;
2543 }
2544 else {
2545 /* add scheme with executing log format */
2546 chunk->data += build_logline(s, chunk->area + chunk->data,
2547 chunk->size - chunk->data,
2548 &rule->rdr_fmt);
2549 }
2550 /* add "://" + host + path */
2551 if (!chunk_memcat(chunk, "://", 3) ||
2552 !chunk_memcat(chunk, host.ptr, host.len) ||
2553 !chunk_memcat(chunk, path.ptr, path.len))
2554 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002555
Christopher Faulet99daf282018-11-28 22:58:13 +01002556 /* append a slash at the end of the location if needed and missing */
2557 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2558 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2559 if (chunk->data + 1 >= chunk->size)
2560 goto fail;
2561 chunk->area[chunk->data++] = '/';
2562 }
2563 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002564 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002565
Christopher Faulet99daf282018-11-28 22:58:13 +01002566 case REDIRECT_TYPE_PREFIX: {
2567 struct ist path;
2568
Christopher Faulet297fbb42019-05-13 14:41:27 +02002569 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002570 path = http_get_path(htx_sl_req_uri(sl));
2571 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002572 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002573 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2574 int qs = 0;
2575 while (qs < path.len) {
2576 if (*(path.ptr + qs) == '?') {
2577 path.len = qs;
2578 break;
2579 }
2580 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002581 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002582 }
2583 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002584 else
2585 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002586
Christopher Faulet99daf282018-11-28 22:58:13 +01002587 if (rule->rdr_str) { /* this is an old "redirect" rule */
2588 /* add prefix. Note that if prefix == "/", we don't want to
2589 * add anything, otherwise it makes it hard for the user to
2590 * configure a self-redirection.
2591 */
2592 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2593 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2594 goto fail;
2595 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002596 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002597 else {
2598 /* add prefix with executing log format */
2599 chunk->data += build_logline(s, chunk->area + chunk->data,
2600 chunk->size - chunk->data,
2601 &rule->rdr_fmt);
2602 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002603
Christopher Faulet99daf282018-11-28 22:58:13 +01002604 /* add path */
2605 if (!chunk_memcat(chunk, path.ptr, path.len))
2606 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002607
Christopher Faulet99daf282018-11-28 22:58:13 +01002608 /* append a slash at the end of the location if needed and missing */
2609 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2610 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2611 if (chunk->data + 1 >= chunk->size)
2612 goto fail;
2613 chunk->area[chunk->data++] = '/';
2614 }
2615 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002616 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002617 case REDIRECT_TYPE_LOCATION:
2618 default:
2619 if (rule->rdr_str) { /* this is an old "redirect" rule */
2620 /* add location */
2621 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2622 goto fail;
2623 }
2624 else {
2625 /* add location with executing log format */
2626 chunk->data += build_logline(s, chunk->area + chunk->data,
2627 chunk->size - chunk->data,
2628 &rule->rdr_fmt);
2629 }
2630 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002631 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002632 location = ist2(chunk->area, chunk->data);
2633
2634 /*
2635 * Create the 30x response
2636 */
2637 switch (rule->code) {
2638 case 308:
2639 status = ist("308");
2640 reason = ist("Permanent Redirect");
2641 break;
2642 case 307:
2643 status = ist("307");
2644 reason = ist("Temporary Redirect");
2645 break;
2646 case 303:
2647 status = ist("303");
2648 reason = ist("See Other");
2649 break;
2650 case 301:
2651 status = ist("301");
2652 reason = ist("Moved Permanently");
2653 break;
2654 case 302:
2655 default:
2656 status = ist("302");
2657 reason = ist("Found");
2658 break;
2659 }
2660
Christopher Faulet08e66462019-05-23 16:44:59 +02002661 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2662 close = 1;
2663
Christopher Faulet99daf282018-11-28 22:58:13 +01002664 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002665 /* Trim any possible response */
2666 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002667 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2668 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2669 if (!sl)
2670 goto fail;
2671 sl->info.res.status = rule->code;
2672 s->txn->status = rule->code;
2673
Christopher Faulet08e66462019-05-23 16:44:59 +02002674 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2675 goto fail;
2676
2677 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002678 !htx_add_header(htx, ist("Location"), location))
2679 goto fail;
2680
2681 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2682 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2683 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002684 }
2685
2686 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002687 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2688 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002689 }
2690
Christopher Faulet99daf282018-11-28 22:58:13 +01002691 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2692 goto fail;
2693
Kevin Zhu96b36392020-01-07 09:42:55 +01002694 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002695 if (!http_forward_proxy_resp(s, 1))
2696 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002697
Christopher Faulet60b33a52020-01-28 09:18:10 +01002698 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2699 /* let's log the request time */
2700 s->logs.tv_request = now;
2701 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002702
Christopher Faulet60b33a52020-01-28 09:18:10 +01002703 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2704 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2705 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002706
2707 if (!(s->flags & SF_ERR_MASK))
2708 s->flags |= SF_ERR_LOCAL;
2709 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002710 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002711
Christopher Faulet99daf282018-11-28 22:58:13 +01002712 free_trash_chunk(chunk);
2713 return 1;
2714
2715 fail:
2716 /* If an error occurred, remove the incomplete HTTP response from the
2717 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002718 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002719 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002720 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002721}
2722
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002723/* Replace all headers matching the name <name>. The header value is replaced if
2724 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2725 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2726 * values are evaluated one by one. It returns 0 on success and -1 on error.
2727 */
2728int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2729 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002730{
2731 struct http_hdr_ctx ctx;
2732 struct buffer *output = get_trash_chunk();
2733
Christopher Faulet72333522018-10-24 11:25:02 +02002734 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002735 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002736 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2737 continue;
2738
2739 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2740 if (output->data == -1)
2741 return -1;
2742 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2743 return -1;
2744 }
2745 return 0;
2746}
2747
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002748/* This function executes one of the set-{method,path,query,uri} actions. It
2749 * takes the string from the variable 'replace' with length 'len', then modifies
2750 * the relevant part of the request line accordingly. Then it updates various
2751 * pointers to the next elements which were moved, and the total buffer length.
2752 * It finds the action to be performed in p[2], previously filled by function
2753 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2754 * error, though this can be revisited when this code is finally exploited.
2755 *
2756 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2757 * query string and 3 to replace uri.
2758 *
2759 * In query string case, the mark question '?' must be set at the start of the
2760 * string by the caller, event if the replacement query string is empty.
2761 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002762int http_req_replace_stline(int action, const char *replace, int len,
2763 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002764{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002765 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002766
2767 switch (action) {
2768 case 0: // method
2769 if (!http_replace_req_meth(htx, ist2(replace, len)))
2770 return -1;
2771 break;
2772
2773 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002774 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002775 return -1;
2776 break;
2777
2778 case 2: // query
2779 if (!http_replace_req_query(htx, ist2(replace, len)))
2780 return -1;
2781 break;
2782
2783 case 3: // uri
2784 if (!http_replace_req_uri(htx, ist2(replace, len)))
2785 return -1;
2786 break;
2787
2788 default:
2789 return -1;
2790 }
2791 return 0;
2792}
2793
2794/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002795 * variable <status> contains the new status code. This function never fails. It
2796 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002797 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002798int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002799{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002800 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002801 char *res;
2802
2803 chunk_reset(&trash);
2804 res = ultoa_o(status, trash.area, trash.size);
2805 trash.data = res - trash.area;
2806
2807 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002808 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002809 const char *str = http_get_reason(status);
2810 reason = ist2(str, strlen(str));
2811 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002812
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002813 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002814 return -1;
2815 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002816}
2817
Christopher Faulet3e964192018-10-24 11:39:23 +02002818/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2819 * transaction <txn>. Returns the verdict of the first rule that prevents
2820 * further processing of the request (auth, deny, ...), and defaults to
2821 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2822 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2823 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2824 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2825 * status.
2826 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002827static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002828 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002829{
2830 struct session *sess = strm_sess(s);
2831 struct http_txn *txn = s->txn;
2832 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002833 struct act_rule *rule;
2834 struct http_hdr_ctx ctx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002835 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002836 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002837
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002838 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002839
2840 /* If "the current_rule_list" match the executed rule list, we are in
2841 * resume condition. If a resume is needed it is always in the action
2842 * and never in the ACL or converters. In this case, we initialise the
2843 * current rule, and go to the action execution point.
2844 */
2845 if (s->current_rule) {
2846 rule = s->current_rule;
2847 s->current_rule = NULL;
2848 if (s->current_rule_list == rules)
2849 goto resume_execution;
2850 }
2851 s->current_rule_list = rules;
2852
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002853 /* start the ruleset evaluation in strict mode */
2854 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002855
Christopher Faulet3e964192018-10-24 11:39:23 +02002856 list_for_each_entry(rule, rules, list) {
2857 /* check optional condition */
2858 if (rule->cond) {
2859 int ret;
2860
2861 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2862 ret = acl_pass(ret);
2863
2864 if (rule->cond->pol == ACL_COND_UNLESS)
2865 ret = !ret;
2866
2867 if (!ret) /* condition not matched */
2868 continue;
2869 }
2870
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002871 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002872 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002873 /* Always call the action function if defined */
2874 if (rule->action_ptr) {
2875 if ((s->req.flags & CF_READ_ERROR) ||
2876 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2877 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002878 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002879
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002880 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002881 case ACT_RET_CONT:
2882 break;
2883 case ACT_RET_STOP:
2884 rule_ret = HTTP_RULE_RES_STOP;
2885 goto end;
2886 case ACT_RET_YIELD:
2887 s->current_rule = rule;
2888 rule_ret = HTTP_RULE_RES_YIELD;
2889 goto end;
2890 case ACT_RET_ERR:
2891 rule_ret = HTTP_RULE_RES_ERROR;
2892 goto end;
2893 case ACT_RET_DONE:
2894 rule_ret = HTTP_RULE_RES_DONE;
2895 goto end;
2896 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002897 if (txn->status == -1)
2898 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002899 rule_ret = HTTP_RULE_RES_DENY;
2900 goto end;
2901 case ACT_RET_ABRT:
2902 rule_ret = HTTP_RULE_RES_ABRT;
2903 goto end;
2904 case ACT_RET_INV:
2905 rule_ret = HTTP_RULE_RES_BADREQ;
2906 goto end;
2907 }
2908 continue; /* eval the next rule */
2909 }
2910
2911 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002912 switch (rule->action) {
2913 case ACT_ACTION_ALLOW:
2914 rule_ret = HTTP_RULE_RES_STOP;
2915 goto end;
2916
2917 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002918 txn->status = rule->arg.http_reply->status;
2919 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002920 rule_ret = HTTP_RULE_RES_DENY;
2921 goto end;
2922
2923 case ACT_HTTP_REQ_TARPIT:
2924 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002925 txn->status = rule->arg.http_reply->status;
2926 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002927 rule_ret = HTTP_RULE_RES_DENY;
2928 goto end;
2929
Christopher Faulet3e964192018-10-24 11:39:23 +02002930 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002931 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002932 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002933 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002934 goto end;
2935
2936 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002937 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002938 break;
2939
2940 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002941 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002942 break;
2943
2944 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002945 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002946 break;
2947
2948 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002949 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002950 break;
2951
Christopher Faulet3e964192018-10-24 11:39:23 +02002952 case ACT_HTTP_DEL_HDR:
2953 /* remove all occurrences of the header */
2954 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01002955 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02002956 http_remove_header(htx, &ctx);
2957 break;
2958
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002959 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002960 default:
2961 break;
2962 }
2963 }
2964
2965 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002966 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002967 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002968 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002969
Christopher Faulet3e964192018-10-24 11:39:23 +02002970 /* we reached the end of the rules, nothing to report */
2971 return rule_ret;
2972}
2973
2974/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2975 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2976 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2977 * is returned, the process can continue the evaluation of next rule list. If
2978 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2979 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002980 * must be returned. If *YIELD is returned, the caller must call again the
2981 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002982 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002983static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2984 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002985{
2986 struct session *sess = strm_sess(s);
2987 struct http_txn *txn = s->txn;
2988 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002989 struct act_rule *rule;
2990 struct http_hdr_ctx ctx;
2991 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002992 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002993
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002994 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002995
2996 /* If "the current_rule_list" match the executed rule list, we are in
2997 * resume condition. If a resume is needed it is always in the action
2998 * and never in the ACL or converters. In this case, we initialise the
2999 * current rule, and go to the action execution point.
3000 */
3001 if (s->current_rule) {
3002 rule = s->current_rule;
3003 s->current_rule = NULL;
3004 if (s->current_rule_list == rules)
3005 goto resume_execution;
3006 }
3007 s->current_rule_list = rules;
3008
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003009 /* start the ruleset evaluation in strict mode */
3010 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003011
Christopher Faulet3e964192018-10-24 11:39:23 +02003012 list_for_each_entry(rule, rules, list) {
3013 /* check optional condition */
3014 if (rule->cond) {
3015 int ret;
3016
3017 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3018 ret = acl_pass(ret);
3019
3020 if (rule->cond->pol == ACL_COND_UNLESS)
3021 ret = !ret;
3022
3023 if (!ret) /* condition not matched */
3024 continue;
3025 }
3026
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003027 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02003028resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003029
3030 /* Always call the action function if defined */
3031 if (rule->action_ptr) {
3032 if ((s->req.flags & CF_READ_ERROR) ||
3033 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3034 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003035 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003036
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003037 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003038 case ACT_RET_CONT:
3039 break;
3040 case ACT_RET_STOP:
3041 rule_ret = HTTP_RULE_RES_STOP;
3042 goto end;
3043 case ACT_RET_YIELD:
3044 s->current_rule = rule;
3045 rule_ret = HTTP_RULE_RES_YIELD;
3046 goto end;
3047 case ACT_RET_ERR:
3048 rule_ret = HTTP_RULE_RES_ERROR;
3049 goto end;
3050 case ACT_RET_DONE:
3051 rule_ret = HTTP_RULE_RES_DONE;
3052 goto end;
3053 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01003054 if (txn->status == -1)
3055 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003056 rule_ret = HTTP_RULE_RES_DENY;
3057 goto end;
3058 case ACT_RET_ABRT:
3059 rule_ret = HTTP_RULE_RES_ABRT;
3060 goto end;
3061 case ACT_RET_INV:
3062 rule_ret = HTTP_RULE_RES_BADREQ;
3063 goto end;
3064 }
3065 continue; /* eval the next rule */
3066 }
3067
3068 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003069 switch (rule->action) {
3070 case ACT_ACTION_ALLOW:
3071 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3072 goto end;
3073
3074 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02003075 txn->status = rule->arg.http_reply->status;
3076 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003077 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003078 goto end;
3079
3080 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003081 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003082 break;
3083
3084 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003085 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003086 break;
3087
3088 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003089 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003090 break;
3091
3092 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003093 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003094 break;
3095
Christopher Faulet3e964192018-10-24 11:39:23 +02003096 case ACT_HTTP_DEL_HDR:
3097 /* remove all occurrences of the header */
3098 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01003099 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02003100 http_remove_header(htx, &ctx);
3101 break;
3102
Christopher Faulet3e964192018-10-24 11:39:23 +02003103 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003104 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003105 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003106 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003107 goto end;
3108
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003109 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003110 default:
3111 break;
3112 }
3113 }
3114
3115 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003116 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003117 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003118 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003119
Christopher Faulet3e964192018-10-24 11:39:23 +02003120 /* we reached the end of the rules, nothing to report */
3121 return rule_ret;
3122}
3123
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003124/* Executes backend and frontend http-after-response rules for the stream <s>,
3125 * in that order. it return 1 on success and 0 on error. It is the caller
3126 * responsibility to catch error or ignore it. If it catches it, this function
3127 * may be called a second time, for the internal error.
3128 */
3129int http_eval_after_res_rules(struct stream *s)
3130{
3131 struct session *sess = s->sess;
3132 enum rule_result ret = HTTP_RULE_RES_CONT;
3133
Christopher Faulet507479b2020-05-15 12:29:46 +02003134 /* Eval after-response ruleset only if the reply is not const */
3135 if (s->txn->flags & TX_CONST_REPLY)
3136 goto end;
3137
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003138 /* prune the request variables if not already done and swap to the response variables. */
3139 if (s->vars_reqres.scope != SCOPE_RES) {
3140 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3141 vars_prune(&s->vars_reqres, s->sess, s);
3142 vars_init(&s->vars_reqres, SCOPE_RES);
3143 }
3144
3145 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3146 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3147 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3148
Christopher Faulet507479b2020-05-15 12:29:46 +02003149 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003150 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3151 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3152}
3153
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003154/*
3155 * Manage client-side cookie. It can impact performance by about 2% so it is
3156 * desirable to call it only when needed. This code is quite complex because
3157 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3158 * highly recommended not to touch this part without a good reason !
3159 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003160static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003161{
3162 struct session *sess = s->sess;
3163 struct http_txn *txn = s->txn;
3164 struct htx *htx;
3165 struct http_hdr_ctx ctx;
3166 char *hdr_beg, *hdr_end, *del_from;
3167 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3168 int preserve_hdr;
3169
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003170 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003171 ctx.blk = NULL;
3172 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003173 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003174 del_from = NULL; /* nothing to be deleted */
3175 preserve_hdr = 0; /* assume we may kill the whole header */
3176
3177 /* Now look for cookies. Conforming to RFC2109, we have to support
3178 * attributes whose name begin with a '$', and associate them with
3179 * the right cookie, if we want to delete this cookie.
3180 * So there are 3 cases for each cookie read :
3181 * 1) it's a special attribute, beginning with a '$' : ignore it.
3182 * 2) it's a server id cookie that we *MAY* want to delete : save
3183 * some pointers on it (last semi-colon, beginning of cookie...)
3184 * 3) it's an application cookie : we *MAY* have to delete a previous
3185 * "special" cookie.
3186 * At the end of loop, if a "special" cookie remains, we may have to
3187 * remove it. If no application cookie persists in the header, we
3188 * *MUST* delete it.
3189 *
3190 * Note: RFC2965 is unclear about the processing of spaces around
3191 * the equal sign in the ATTR=VALUE form. A careful inspection of
3192 * the RFC explicitly allows spaces before it, and not within the
3193 * tokens (attrs or values). An inspection of RFC2109 allows that
3194 * too but section 10.1.3 lets one think that spaces may be allowed
3195 * after the equal sign too, resulting in some (rare) buggy
3196 * implementations trying to do that. So let's do what servers do.
3197 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3198 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003199 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003200 * causes parsing to become ambiguous. Browsers also allow spaces
3201 * within values even without quotes.
3202 *
3203 * We have to keep multiple pointers in order to support cookie
3204 * removal at the beginning, middle or end of header without
3205 * corrupting the header. All of these headers are valid :
3206 *
3207 * hdr_beg hdr_end
3208 * | |
3209 * v |
3210 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3211 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3212 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3213 * | | | | | | |
3214 * | | | | | | |
3215 * | | | | | | +--> next
3216 * | | | | | +----> val_end
3217 * | | | | +-----------> val_beg
3218 * | | | +--------------> equal
3219 * | | +----------------> att_end
3220 * | +---------------------> att_beg
3221 * +--------------------------> prev
3222 *
3223 */
3224 hdr_beg = ctx.value.ptr;
3225 hdr_end = hdr_beg + ctx.value.len;
3226 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3227 /* Iterate through all cookies on this line */
3228
3229 /* find att_beg */
3230 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003231 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003232 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003233 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003234
3235 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3236 att_beg++;
3237
3238 /* find att_end : this is the first character after the last non
3239 * space before the equal. It may be equal to hdr_end.
3240 */
3241 equal = att_end = att_beg;
3242 while (equal < hdr_end) {
3243 if (*equal == '=' || *equal == ',' || *equal == ';')
3244 break;
3245 if (HTTP_IS_SPHT(*equal++))
3246 continue;
3247 att_end = equal;
3248 }
3249
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003250 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003251 * is between <att_beg> and <equal>, both may be identical.
3252 */
3253 /* look for end of cookie if there is an equal sign */
3254 if (equal < hdr_end && *equal == '=') {
3255 /* look for the beginning of the value */
3256 val_beg = equal + 1;
3257 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3258 val_beg++;
3259
3260 /* find the end of the value, respecting quotes */
3261 next = http_find_cookie_value_end(val_beg, hdr_end);
3262
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003263 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003264 val_end = next;
3265 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3266 val_end--;
3267 }
3268 else
3269 val_beg = val_end = next = equal;
3270
3271 /* We have nothing to do with attributes beginning with
3272 * '$'. However, they will automatically be removed if a
3273 * header before them is removed, since they're supposed
3274 * to be linked together.
3275 */
3276 if (*att_beg == '$')
3277 continue;
3278
3279 /* Ignore cookies with no equal sign */
3280 if (equal == next) {
3281 /* This is not our cookie, so we must preserve it. But if we already
3282 * scheduled another cookie for removal, we cannot remove the
3283 * complete header, but we can remove the previous block itself.
3284 */
3285 preserve_hdr = 1;
3286 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003287 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003288 val_end += delta;
3289 next += delta;
3290 hdr_end += delta;
3291 prev = del_from;
3292 del_from = NULL;
3293 }
3294 continue;
3295 }
3296
3297 /* if there are spaces around the equal sign, we need to
3298 * strip them otherwise we'll get trouble for cookie captures,
3299 * or even for rewrites. Since this happens extremely rarely,
3300 * it does not hurt performance.
3301 */
3302 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3303 int stripped_before = 0;
3304 int stripped_after = 0;
3305
3306 if (att_end != equal) {
3307 memmove(att_end, equal, hdr_end - equal);
3308 stripped_before = (att_end - equal);
3309 equal += stripped_before;
3310 val_beg += stripped_before;
3311 }
3312
3313 if (val_beg > equal + 1) {
3314 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3315 stripped_after = (equal + 1) - val_beg;
3316 val_beg += stripped_after;
3317 stripped_before += stripped_after;
3318 }
3319
3320 val_end += stripped_before;
3321 next += stripped_before;
3322 hdr_end += stripped_before;
3323 }
3324 /* now everything is as on the diagram above */
3325
3326 /* First, let's see if we want to capture this cookie. We check
3327 * that we don't already have a client side cookie, because we
3328 * can only capture one. Also as an optimisation, we ignore
3329 * cookies shorter than the declared name.
3330 */
3331 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3332 (val_end - att_beg >= sess->fe->capture_namelen) &&
3333 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3334 int log_len = val_end - att_beg;
3335
3336 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3337 ha_alert("HTTP logging : out of memory.\n");
3338 } else {
3339 if (log_len > sess->fe->capture_len)
3340 log_len = sess->fe->capture_len;
3341 memcpy(txn->cli_cookie, att_beg, log_len);
3342 txn->cli_cookie[log_len] = 0;
3343 }
3344 }
3345
3346 /* Persistence cookies in passive, rewrite or insert mode have the
3347 * following form :
3348 *
3349 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3350 *
3351 * For cookies in prefix mode, the form is :
3352 *
3353 * Cookie: NAME=SRV~VALUE
3354 */
3355 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3356 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3357 struct server *srv = s->be->srv;
3358 char *delim;
3359
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003360 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003361 * have the server ID between val_beg and delim, and the original cookie between
3362 * delim+1 and val_end. Otherwise, delim==val_end :
3363 *
3364 * hdr_beg
3365 * |
3366 * v
3367 * NAME=SRV; # in all but prefix modes
3368 * NAME=SRV~OPAQUE ; # in prefix mode
3369 * || || | |+-> next
3370 * || || | +--> val_end
3371 * || || +---------> delim
3372 * || |+------------> val_beg
3373 * || +-------------> att_end = equal
3374 * |+-----------------> att_beg
3375 * +------------------> prev
3376 *
3377 */
3378 if (s->be->ck_opts & PR_CK_PFX) {
3379 for (delim = val_beg; delim < val_end; delim++)
3380 if (*delim == COOKIE_DELIM)
3381 break;
3382 }
3383 else {
3384 char *vbar1;
3385 delim = val_end;
3386 /* Now check if the cookie contains a date field, which would
3387 * appear after a vertical bar ('|') just after the server name
3388 * and before the delimiter.
3389 */
3390 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3391 if (vbar1) {
3392 /* OK, so left of the bar is the server's cookie and
3393 * right is the last seen date. It is a base64 encoded
3394 * 30-bit value representing the UNIX date since the
3395 * epoch in 4-second quantities.
3396 */
3397 int val;
3398 delim = vbar1++;
3399 if (val_end - vbar1 >= 5) {
3400 val = b64tos30(vbar1);
3401 if (val > 0)
3402 txn->cookie_last_date = val << 2;
3403 }
3404 /* look for a second vertical bar */
3405 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3406 if (vbar1 && (val_end - vbar1 > 5)) {
3407 val = b64tos30(vbar1 + 1);
3408 if (val > 0)
3409 txn->cookie_first_date = val << 2;
3410 }
3411 }
3412 }
3413
3414 /* if the cookie has an expiration date and the proxy wants to check
3415 * it, then we do that now. We first check if the cookie is too old,
3416 * then only if it has expired. We detect strict overflow because the
3417 * time resolution here is not great (4 seconds). Cookies with dates
3418 * in the future are ignored if their offset is beyond one day. This
3419 * allows an admin to fix timezone issues without expiring everyone
3420 * and at the same time avoids keeping unwanted side effects for too
3421 * long.
3422 */
3423 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3424 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3425 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3426 txn->flags &= ~TX_CK_MASK;
3427 txn->flags |= TX_CK_OLD;
3428 delim = val_beg; // let's pretend we have not found the cookie
3429 txn->cookie_first_date = 0;
3430 txn->cookie_last_date = 0;
3431 }
3432 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3433 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3434 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3435 txn->flags &= ~TX_CK_MASK;
3436 txn->flags |= TX_CK_EXPIRED;
3437 delim = val_beg; // let's pretend we have not found the cookie
3438 txn->cookie_first_date = 0;
3439 txn->cookie_last_date = 0;
3440 }
3441
3442 /* Here, we'll look for the first running server which supports the cookie.
3443 * This allows to share a same cookie between several servers, for example
3444 * to dedicate backup servers to specific servers only.
3445 * However, to prevent clients from sticking to cookie-less backup server
3446 * when they have incidentely learned an empty cookie, we simply ignore
3447 * empty cookies and mark them as invalid.
3448 * The same behaviour is applied when persistence must be ignored.
3449 */
3450 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3451 srv = NULL;
3452
3453 while (srv) {
3454 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3455 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3456 if ((srv->cur_state != SRV_ST_STOPPED) ||
3457 (s->be->options & PR_O_PERSIST) ||
3458 (s->flags & SF_FORCE_PRST)) {
3459 /* we found the server and we can use it */
3460 txn->flags &= ~TX_CK_MASK;
3461 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3462 s->flags |= SF_DIRECT | SF_ASSIGNED;
3463 s->target = &srv->obj_type;
3464 break;
3465 } else {
3466 /* we found a server, but it's down,
3467 * mark it as such and go on in case
3468 * another one is available.
3469 */
3470 txn->flags &= ~TX_CK_MASK;
3471 txn->flags |= TX_CK_DOWN;
3472 }
3473 }
3474 srv = srv->next;
3475 }
3476
3477 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3478 /* no server matched this cookie or we deliberately skipped it */
3479 txn->flags &= ~TX_CK_MASK;
3480 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3481 txn->flags |= TX_CK_UNUSED;
3482 else
3483 txn->flags |= TX_CK_INVALID;
3484 }
3485
3486 /* depending on the cookie mode, we may have to either :
3487 * - delete the complete cookie if we're in insert+indirect mode, so that
3488 * the server never sees it ;
3489 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003490 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003491 * if we're in cookie prefix mode
3492 */
3493 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3494 int delta; /* negative */
3495
3496 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3497 delta = val_beg - (delim + 1);
3498 val_end += delta;
3499 next += delta;
3500 hdr_end += delta;
3501 del_from = NULL;
3502 preserve_hdr = 1; /* we want to keep this cookie */
3503 }
3504 else if (del_from == NULL &&
3505 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3506 del_from = prev;
3507 }
3508 }
3509 else {
3510 /* This is not our cookie, so we must preserve it. But if we already
3511 * scheduled another cookie for removal, we cannot remove the
3512 * complete header, but we can remove the previous block itself.
3513 */
3514 preserve_hdr = 1;
3515
3516 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003517 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003518 if (att_beg >= del_from)
3519 att_beg += delta;
3520 if (att_end >= del_from)
3521 att_end += delta;
3522 val_beg += delta;
3523 val_end += delta;
3524 next += delta;
3525 hdr_end += delta;
3526 prev = del_from;
3527 del_from = NULL;
3528 }
3529 }
3530
3531 /* continue with next cookie on this header line */
3532 att_beg = next;
3533 } /* for each cookie */
3534
3535
3536 /* There are no more cookies on this line.
3537 * We may still have one (or several) marked for deletion at the
3538 * end of the line. We must do this now in two ways :
3539 * - if some cookies must be preserved, we only delete from the
3540 * mark to the end of line ;
3541 * - if nothing needs to be preserved, simply delete the whole header
3542 */
3543 if (del_from) {
3544 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3545 }
3546 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003547 if (hdr_beg != hdr_end)
3548 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003549 else
3550 http_remove_header(htx, &ctx);
3551 }
3552 } /* for each "Cookie header */
3553}
3554
3555/*
3556 * Manage server-side cookies. It can impact performance by about 2% so it is
3557 * desirable to call it only when needed. This function is also used when we
3558 * just need to know if there is a cookie (eg: for check-cache).
3559 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003560static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003561{
3562 struct session *sess = s->sess;
3563 struct http_txn *txn = s->txn;
3564 struct htx *htx;
3565 struct http_hdr_ctx ctx;
3566 struct server *srv;
3567 char *hdr_beg, *hdr_end;
3568 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003569 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003570
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003571 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003572
3573 ctx.blk = NULL;
3574 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003575 int is_first = 1;
3576
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003577 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3578 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3579 break;
3580 is_cookie2 = 1;
3581 }
3582
3583 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3584 * <prev> points to the colon.
3585 */
3586 txn->flags |= TX_SCK_PRESENT;
3587
3588 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3589 * check-cache is enabled) and we are not interested in checking
3590 * them. Warning, the cookie capture is declared in the frontend.
3591 */
3592 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3593 break;
3594
3595 /* OK so now we know we have to process this response cookie.
3596 * The format of the Set-Cookie header is slightly different
3597 * from the format of the Cookie header in that it does not
3598 * support the comma as a cookie delimiter (thus the header
3599 * cannot be folded) because the Expires attribute described in
3600 * the original Netscape's spec may contain an unquoted date
3601 * with a comma inside. We have to live with this because
3602 * many browsers don't support Max-Age and some browsers don't
3603 * support quoted strings. However the Set-Cookie2 header is
3604 * clean.
3605 *
3606 * We have to keep multiple pointers in order to support cookie
3607 * removal at the beginning, middle or end of header without
3608 * corrupting the header (in case of set-cookie2). A special
3609 * pointer, <scav> points to the beginning of the set-cookie-av
3610 * fields after the first semi-colon. The <next> pointer points
3611 * either to the end of line (set-cookie) or next unquoted comma
3612 * (set-cookie2). All of these headers are valid :
3613 *
3614 * hdr_beg hdr_end
3615 * | |
3616 * v |
3617 * NAME1 = VALUE 1 ; Secure; Path="/" |
3618 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3619 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3620 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3621 * | | | | | | | |
3622 * | | | | | | | +-> next
3623 * | | | | | | +------------> scav
3624 * | | | | | +--------------> val_end
3625 * | | | | +--------------------> val_beg
3626 * | | | +----------------------> equal
3627 * | | +------------------------> att_end
3628 * | +----------------------------> att_beg
3629 * +------------------------------> prev
3630 * -------------------------------> hdr_beg
3631 */
3632 hdr_beg = ctx.value.ptr;
3633 hdr_end = hdr_beg + ctx.value.len;
3634 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3635
3636 /* Iterate through all cookies on this line */
3637
3638 /* find att_beg */
3639 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003640 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003641 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003642 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003643
3644 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3645 att_beg++;
3646
3647 /* find att_end : this is the first character after the last non
3648 * space before the equal. It may be equal to hdr_end.
3649 */
3650 equal = att_end = att_beg;
3651
3652 while (equal < hdr_end) {
3653 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3654 break;
3655 if (HTTP_IS_SPHT(*equal++))
3656 continue;
3657 att_end = equal;
3658 }
3659
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003660 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003661 * is between <att_beg> and <equal>, both may be identical.
3662 */
3663
3664 /* look for end of cookie if there is an equal sign */
3665 if (equal < hdr_end && *equal == '=') {
3666 /* look for the beginning of the value */
3667 val_beg = equal + 1;
3668 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3669 val_beg++;
3670
3671 /* find the end of the value, respecting quotes */
3672 next = http_find_cookie_value_end(val_beg, hdr_end);
3673
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003674 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003675 val_end = next;
3676 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3677 val_end--;
3678 }
3679 else {
3680 /* <equal> points to next comma, semi-colon or EOL */
3681 val_beg = val_end = next = equal;
3682 }
3683
3684 if (next < hdr_end) {
3685 /* Set-Cookie2 supports multiple cookies, and <next> points to
3686 * a colon or semi-colon before the end. So skip all attr-value
3687 * pairs and look for the next comma. For Set-Cookie, since
3688 * commas are permitted in values, skip to the end.
3689 */
3690 if (is_cookie2)
3691 next = http_find_hdr_value_end(next, hdr_end);
3692 else
3693 next = hdr_end;
3694 }
3695
3696 /* Now everything is as on the diagram above */
3697
3698 /* Ignore cookies with no equal sign */
3699 if (equal == val_end)
3700 continue;
3701
3702 /* If there are spaces around the equal sign, we need to
3703 * strip them otherwise we'll get trouble for cookie captures,
3704 * or even for rewrites. Since this happens extremely rarely,
3705 * it does not hurt performance.
3706 */
3707 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3708 int stripped_before = 0;
3709 int stripped_after = 0;
3710
3711 if (att_end != equal) {
3712 memmove(att_end, equal, hdr_end - equal);
3713 stripped_before = (att_end - equal);
3714 equal += stripped_before;
3715 val_beg += stripped_before;
3716 }
3717
3718 if (val_beg > equal + 1) {
3719 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3720 stripped_after = (equal + 1) - val_beg;
3721 val_beg += stripped_after;
3722 stripped_before += stripped_after;
3723 }
3724
3725 val_end += stripped_before;
3726 next += stripped_before;
3727 hdr_end += stripped_before;
3728
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003729 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003730 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003731 }
3732
3733 /* First, let's see if we want to capture this cookie. We check
3734 * that we don't already have a server side cookie, because we
3735 * can only capture one. Also as an optimisation, we ignore
3736 * cookies shorter than the declared name.
3737 */
3738 if (sess->fe->capture_name != NULL &&
3739 txn->srv_cookie == NULL &&
3740 (val_end - att_beg >= sess->fe->capture_namelen) &&
3741 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3742 int log_len = val_end - att_beg;
3743 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3744 ha_alert("HTTP logging : out of memory.\n");
3745 }
3746 else {
3747 if (log_len > sess->fe->capture_len)
3748 log_len = sess->fe->capture_len;
3749 memcpy(txn->srv_cookie, att_beg, log_len);
3750 txn->srv_cookie[log_len] = 0;
3751 }
3752 }
3753
3754 srv = objt_server(s->target);
3755 /* now check if we need to process it for persistence */
3756 if (!(s->flags & SF_IGNORE_PRST) &&
3757 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3758 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3759 /* assume passive cookie by default */
3760 txn->flags &= ~TX_SCK_MASK;
3761 txn->flags |= TX_SCK_FOUND;
3762
3763 /* If the cookie is in insert mode on a known server, we'll delete
3764 * this occurrence because we'll insert another one later.
3765 * We'll delete it too if the "indirect" option is set and we're in
3766 * a direct access.
3767 */
3768 if (s->be->ck_opts & PR_CK_PSV) {
3769 /* The "preserve" flag was set, we don't want to touch the
3770 * server's cookie.
3771 */
3772 }
3773 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3774 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3775 /* this cookie must be deleted */
3776 if (prev == hdr_beg && next == hdr_end) {
3777 /* whole header */
3778 http_remove_header(htx, &ctx);
3779 /* note: while both invalid now, <next> and <hdr_end>
3780 * are still equal, so the for() will stop as expected.
3781 */
3782 } else {
3783 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003784 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003785 next = prev;
3786 hdr_end += delta;
3787 }
3788 txn->flags &= ~TX_SCK_MASK;
3789 txn->flags |= TX_SCK_DELETED;
3790 /* and go on with next cookie */
3791 }
3792 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3793 /* replace bytes val_beg->val_end with the cookie name associated
3794 * with this server since we know it.
3795 */
3796 int sliding, delta;
3797
3798 ctx.value = ist2(val_beg, val_end - val_beg);
3799 ctx.lws_before = ctx.lws_after = 0;
3800 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3801 delta = srv->cklen - (val_end - val_beg);
3802 sliding = (ctx.value.ptr - val_beg);
3803 hdr_beg += sliding;
3804 val_beg += sliding;
3805 next += sliding + delta;
3806 hdr_end += sliding + delta;
3807
3808 txn->flags &= ~TX_SCK_MASK;
3809 txn->flags |= TX_SCK_REPLACED;
3810 }
3811 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3812 /* insert the cookie name associated with this server
3813 * before existing cookie, and insert a delimiter between them..
3814 */
3815 int sliding, delta;
3816 ctx.value = ist2(val_beg, 0);
3817 ctx.lws_before = ctx.lws_after = 0;
3818 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3819 delta = srv->cklen + 1;
3820 sliding = (ctx.value.ptr - val_beg);
3821 hdr_beg += sliding;
3822 val_beg += sliding;
3823 next += sliding + delta;
3824 hdr_end += sliding + delta;
3825
3826 val_beg[srv->cklen] = COOKIE_DELIM;
3827 txn->flags &= ~TX_SCK_MASK;
3828 txn->flags |= TX_SCK_REPLACED;
3829 }
3830 }
3831 /* that's done for this cookie, check the next one on the same
3832 * line when next != hdr_end (only if is_cookie2).
3833 */
3834 }
3835 }
3836}
3837
Christopher Faulet25a02f62018-10-24 12:00:25 +02003838/*
3839 * Parses the Cache-Control and Pragma request header fields to determine if
3840 * the request may be served from the cache and/or if it is cacheable. Updates
3841 * s->txn->flags.
3842 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003843void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003844{
3845 struct http_txn *txn = s->txn;
3846 struct htx *htx;
3847 int32_t pos;
3848 int pragma_found, cc_found, i;
3849
3850 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3851 return; /* nothing more to do here */
3852
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003853 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003854 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02003855 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003856 struct htx_blk *blk = htx_get_blk(htx, pos);
3857 enum htx_blk_type type = htx_get_blk_type(blk);
3858 struct ist n, v;
3859
3860 if (type == HTX_BLK_EOH)
3861 break;
3862 if (type != HTX_BLK_HDR)
3863 continue;
3864
3865 n = htx_get_blk_name(htx, blk);
3866 v = htx_get_blk_value(htx, blk);
3867
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003868 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003869 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3870 pragma_found = 1;
3871 continue;
3872 }
3873 }
3874
3875 /* Don't use the cache and don't try to store if we found the
3876 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003877 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003878 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3879 txn->flags |= TX_CACHE_IGNORE;
3880 continue;
3881 }
3882
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003883 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02003884 continue;
3885
3886 /* OK, right now we know we have a cache-control header */
3887 cc_found = 1;
3888 if (!v.len) /* no info */
3889 continue;
3890
3891 i = 0;
3892 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
3893 !isspace((unsigned char)*(v.ptr+i)))
3894 i++;
3895
3896 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
3897 * values after max-age, max-stale nor min-fresh, we simply don't
3898 * use the cache when they're specified.
3899 */
3900 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
3901 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
3902 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
3903 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
3904 txn->flags |= TX_CACHE_IGNORE;
3905 continue;
3906 }
3907
3908 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
3909 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3910 continue;
3911 }
3912 }
3913
3914 /* RFC7234#5.4:
3915 * When the Cache-Control header field is also present and
3916 * understood in a request, Pragma is ignored.
3917 * When the Cache-Control header field is not present in a
3918 * request, caches MUST consider the no-cache request
3919 * pragma-directive as having the same effect as if
3920 * "Cache-Control: no-cache" were present.
3921 */
3922 if (!cc_found && pragma_found)
3923 txn->flags |= TX_CACHE_IGNORE;
3924}
3925
3926/*
3927 * Check if response is cacheable or not. Updates s->txn->flags.
3928 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003929void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003930{
3931 struct http_txn *txn = s->txn;
3932 struct htx *htx;
3933 int32_t pos;
3934 int i;
3935
3936 if (txn->status < 200) {
3937 /* do not try to cache interim responses! */
3938 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3939 return;
3940 }
3941
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003942 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02003943 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003944 struct htx_blk *blk = htx_get_blk(htx, pos);
3945 enum htx_blk_type type = htx_get_blk_type(blk);
3946 struct ist n, v;
3947
3948 if (type == HTX_BLK_EOH)
3949 break;
3950 if (type != HTX_BLK_HDR)
3951 continue;
3952
3953 n = htx_get_blk_name(htx, blk);
3954 v = htx_get_blk_value(htx, blk);
3955
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003956 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003957 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3958 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3959 return;
3960 }
3961 }
3962
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003963 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02003964 continue;
3965
3966 /* OK, right now we know we have a cache-control header */
3967 if (!v.len) /* no info */
3968 continue;
3969
3970 i = 0;
3971 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
3972 !isspace((unsigned char)*(v.ptr+i)))
3973 i++;
3974
3975 /* we have a complete value between v.ptr and (v.ptr+i) */
3976 if (i < v.len && *(v.ptr + i) == '=') {
3977 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
3978 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
3979 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3980 continue;
3981 }
3982
3983 /* we have something of the form no-cache="set-cookie" */
3984 if ((v.len >= 21) &&
3985 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
3986 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
3987 txn->flags &= ~TX_CACHE_COOK;
3988 continue;
3989 }
3990
3991 /* OK, so we know that either p2 points to the end of string or to a comma */
3992 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
3993 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
3994 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
3995 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3996 return;
3997 }
3998
3999 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
4000 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4001 continue;
4002 }
4003 }
4004}
4005
Christopher Faulet377c5a52018-10-24 21:21:30 +02004006/*
4007 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
4008 * for the current backend.
4009 *
4010 * It is assumed that the request is either a HEAD, GET, or POST and that the
4011 * uri_auth field is valid.
4012 *
4013 * Returns 1 if stats should be provided, otherwise 0.
4014 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004015static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004016{
4017 struct uri_auth *uri_auth = backend->uri_auth;
4018 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004019 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004020 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004021
4022 if (!uri_auth)
4023 return 0;
4024
4025 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4026 return 0;
4027
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004028 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004029 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004030 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01004031 if (*uri_auth->uri_prefix == '/')
4032 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004033
4034 /* check URI size */
4035 if (uri_auth->uri_len > uri.len)
4036 return 0;
4037
4038 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4039 return 0;
4040
4041 return 1;
4042}
4043
4044/* This function prepares an applet to handle the stats. It can deal with the
4045 * "100-continue" expectation, check that admin rules are met for POST requests,
4046 * and program a response message if something was unexpected. It cannot fail
4047 * and always relies on the stats applet to complete the job. It does not touch
4048 * analysers nor counters, which are left to the caller. It does not touch
4049 * s->target which is supposed to already point to the stats applet. The caller
4050 * is expected to have already assigned an appctx to the stream.
4051 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004052static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004053{
4054 struct stats_admin_rule *stats_admin_rule;
4055 struct stream_interface *si = &s->si[1];
4056 struct session *sess = s->sess;
4057 struct http_txn *txn = s->txn;
4058 struct http_msg *msg = &txn->req;
4059 struct uri_auth *uri_auth = s->be->uri_auth;
4060 const char *h, *lookup, *end;
4061 struct appctx *appctx;
4062 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004063 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004064
4065 appctx = si_appctx(si);
4066 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4067 appctx->st1 = appctx->st2 = 0;
4068 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004069 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004070 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4071 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4072 appctx->ctx.stats.flags |= STAT_CHUNKED;
4073
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004074 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004075 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004076 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4077 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004078
4079 for (h = lookup; h <= end - 3; h++) {
4080 if (memcmp(h, ";up", 3) == 0) {
4081 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4082 break;
4083 }
4084 }
4085
4086 if (uri_auth->refresh) {
4087 for (h = lookup; h <= end - 10; h++) {
4088 if (memcmp(h, ";norefresh", 10) == 0) {
4089 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4090 break;
4091 }
4092 }
4093 }
4094
4095 for (h = lookup; h <= end - 4; h++) {
4096 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004097 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004098 break;
4099 }
4100 }
4101
4102 for (h = lookup; h <= end - 6; h++) {
4103 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004104 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004105 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4106 break;
4107 }
4108 }
4109
Christopher Faulet6338a082019-09-09 15:50:54 +02004110 for (h = lookup; h <= end - 5; h++) {
4111 if (memcmp(h, ";json", 5) == 0) {
4112 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4113 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4114 break;
4115 }
4116 }
4117
4118 for (h = lookup; h <= end - 12; h++) {
4119 if (memcmp(h, ";json-schema", 12) == 0) {
4120 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4121 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4122 break;
4123 }
4124 }
4125
Christopher Faulet377c5a52018-10-24 21:21:30 +02004126 for (h = lookup; h <= end - 8; h++) {
4127 if (memcmp(h, ";st=", 4) == 0) {
4128 int i;
4129 h += 4;
4130 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4131 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4132 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4133 appctx->ctx.stats.st_code = i;
4134 break;
4135 }
4136 }
4137 break;
4138 }
4139 }
4140
4141 appctx->ctx.stats.scope_str = 0;
4142 appctx->ctx.stats.scope_len = 0;
4143 for (h = lookup; h <= end - 8; h++) {
4144 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4145 int itx = 0;
4146 const char *h2;
4147 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4148 const char *err;
4149
4150 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4151 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004152 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4153 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004154 if (*h == ';' || *h == '&' || *h == ' ')
4155 break;
4156 itx++;
4157 h++;
4158 }
4159
4160 if (itx > STAT_SCOPE_TXT_MAXLEN)
4161 itx = STAT_SCOPE_TXT_MAXLEN;
4162 appctx->ctx.stats.scope_len = itx;
4163
4164 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4165 memcpy(scope_txt, h2, itx);
4166 scope_txt[itx] = '\0';
4167 err = invalid_char(scope_txt);
4168 if (err) {
4169 /* bad char in search text => clear scope */
4170 appctx->ctx.stats.scope_str = 0;
4171 appctx->ctx.stats.scope_len = 0;
4172 }
4173 break;
4174 }
4175 }
4176
4177 /* now check whether we have some admin rules for this request */
4178 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4179 int ret = 1;
4180
4181 if (stats_admin_rule->cond) {
4182 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4183 ret = acl_pass(ret);
4184 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4185 ret = !ret;
4186 }
4187
4188 if (ret) {
4189 /* no rule, or the rule matches */
4190 appctx->ctx.stats.flags |= STAT_ADMIN;
4191 break;
4192 }
4193 }
4194
Christopher Faulet5d45e382019-02-27 15:15:23 +01004195 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4196 appctx->st0 = STAT_HTTP_HEAD;
4197 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004198 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004199 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004200 if (msg->msg_state < HTTP_MSG_DATA)
4201 req->analysers |= AN_REQ_HTTP_BODY;
4202 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004203 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004204 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004205 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4206 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4207 appctx->st0 = STAT_HTTP_LAST;
4208 }
4209 }
4210 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004211 /* Unsupported method */
4212 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4213 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4214 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004215 }
4216
4217 s->task->nice = -32; /* small boost for HTTP statistics */
4218 return 1;
4219}
4220
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004221void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004222{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004223 struct channel *req = &s->req;
4224 struct channel *res = &s->res;
4225 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004226 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004227 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004228 struct ist path, location;
4229 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004230
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004231 /*
4232 * Create the location
4233 */
4234 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004235
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004236 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004237 /* special prefix "/" means don't change URL */
4238 srv = __objt_server(s->target);
4239 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4240 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4241 return;
4242 }
4243
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004244 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004245 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004246 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004247 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004248 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004249 return;
4250
4251 if (!chunk_memcat(&trash, path.ptr, path.len))
4252 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004253 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004254
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004255 /*
4256 * Create the 302 respone
4257 */
4258 htx = htx_from_buf(&res->buf);
4259 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4260 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4261 ist("HTTP/1.1"), ist("302"), ist("Found"));
4262 if (!sl)
4263 goto fail;
4264 sl->info.res.status = 302;
4265 s->txn->status = 302;
4266
4267 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4268 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4269 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4270 !htx_add_header(htx, ist("Location"), location))
4271 goto fail;
4272
4273 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4274 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004275
Christopher Fauletc20afb82020-01-24 19:16:26 +01004276 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004277 if (!http_forward_proxy_resp(s, 1))
4278 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004279
4280 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004281 si_shutr(si);
4282 si_shutw(si);
4283 si->err_type = SI_ET_NONE;
4284 si->state = SI_ST_CLO;
4285
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004286 if (!(s->flags & SF_ERR_MASK))
4287 s->flags |= SF_ERR_LOCAL;
4288 if (!(s->flags & SF_FINST_MASK))
4289 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004290
4291 /* FIXME: we should increase a counter of redirects per server and per backend. */
4292 srv_inc_sess_ctr(srv);
4293 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004294 return;
4295
4296 fail:
4297 /* If an error occurred, remove the incomplete HTTP response from the
4298 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004299 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004300}
4301
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004302/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004303 * because an error was triggered during the body forwarding.
4304 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004305static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004306{
4307 struct channel *chn = &s->req;
4308 struct http_txn *txn = s->txn;
4309
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004310 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004311
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004312 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4313 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004314 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004315 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004316 goto end;
4317 }
4318
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004319 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4320 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004321 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004322 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004323
4324 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004325 /* No need to read anymore, the request was completely parsed.
4326 * We can shut the read side unless we want to abort_on_close,
4327 * or we have a POST request. The issue with POST requests is
4328 * that some browsers still send a CRLF after the request, and
4329 * this CRLF must be read so that it does not remain in the kernel
4330 * buffers, otherwise a close could cause an RST on some systems
4331 * (eg: Linux).
4332 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004333 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004334 channel_dont_read(chn);
4335
4336 /* if the server closes the connection, we want to immediately react
4337 * and close the socket to save packets and syscalls.
4338 */
4339 s->si[1].flags |= SI_FL_NOHALF;
4340
4341 /* In any case we've finished parsing the request so we must
4342 * disable Nagle when sending data because 1) we're not going
4343 * to shut this side, and 2) the server is waiting for us to
4344 * send pending data.
4345 */
4346 chn->flags |= CF_NEVER_WAIT;
4347
Christopher Fauletd01ce402019-01-02 17:44:13 +01004348 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4349 /* The server has not finished to respond, so we
4350 * don't want to move in order not to upset it.
4351 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004352 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004353 return;
4354 }
4355
Christopher Fauletf2824e62018-10-01 12:12:37 +02004356 /* When we get here, it means that both the request and the
4357 * response have finished receiving. Depending on the connection
4358 * mode, we'll have to wait for the last bytes to leave in either
4359 * direction, and sometimes for a close to be effective.
4360 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004361 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004362 /* Tunnel mode will not have any analyser so it needs to
4363 * poll for reads.
4364 */
4365 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004366 if (b_data(&chn->buf)) {
4367 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004368 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004369 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004370 txn->req.msg_state = HTTP_MSG_TUNNEL;
4371 }
4372 else {
4373 /* we're not expecting any new data to come for this
4374 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004375 *
4376 * However, there is an exception if the response
4377 * length is undefined. In this case, we need to wait
4378 * the close from the server. The response will be
4379 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004380 */
4381 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4382 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004383 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004384
4385 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4386 channel_shutr_now(chn);
4387 channel_shutw_now(chn);
4388 }
4389 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004390 goto check_channel_flags;
4391 }
4392
4393 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4394 http_msg_closing:
4395 /* nothing else to forward, just waiting for the output buffer
4396 * to be empty and for the shutw_now to take effect.
4397 */
4398 if (channel_is_empty(chn)) {
4399 txn->req.msg_state = HTTP_MSG_CLOSED;
4400 goto http_msg_closed;
4401 }
4402 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004403 txn->req.msg_state = HTTP_MSG_ERROR;
4404 goto end;
4405 }
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 return;
4408 }
4409
4410 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4411 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004412 /* if we don't know whether the server will close, we need to hard close */
4413 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4414 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004415 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004416 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004417 channel_dont_read(chn);
4418 goto end;
4419 }
4420
4421 check_channel_flags:
4422 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4423 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4424 /* if we've just closed an output, let's switch */
4425 txn->req.msg_state = HTTP_MSG_CLOSING;
4426 goto http_msg_closing;
4427 }
4428
4429 end:
4430 chn->analysers &= AN_REQ_FLT_END;
4431 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4432 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4433 channel_auto_close(chn);
4434 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004435 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004436}
4437
4438
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004439/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004440 * because an error was triggered during the body forwarding.
4441 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004442static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004443{
4444 struct channel *chn = &s->res;
4445 struct http_txn *txn = s->txn;
4446
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004447 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004448
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004449 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4450 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004451 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004452 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004453 goto end;
4454 }
4455
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004456 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4457 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004458 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004459 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004460
4461 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4462 /* In theory, we don't need to read anymore, but we must
4463 * still monitor the server connection for a possible close
4464 * while the request is being uploaded, so we don't disable
4465 * reading.
4466 */
4467 /* channel_dont_read(chn); */
4468
4469 if (txn->req.msg_state < HTTP_MSG_DONE) {
4470 /* The client seems to still be sending data, probably
4471 * because we got an error response during an upload.
4472 * We have the choice of either breaking the connection
4473 * or letting it pass through. Let's do the later.
4474 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004475 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004476 return;
4477 }
4478
4479 /* When we get here, it means that both the request and the
4480 * response have finished receiving. Depending on the connection
4481 * mode, we'll have to wait for the last bytes to leave in either
4482 * direction, and sometimes for a close to be effective.
4483 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004484 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004485 channel_auto_read(chn);
4486 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004487 if (b_data(&chn->buf)) {
4488 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004489 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004490 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004491 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4492 }
4493 else {
4494 /* we're not expecting any new data to come for this
4495 * transaction, so we can close it.
4496 */
4497 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4498 channel_shutr_now(chn);
4499 channel_shutw_now(chn);
4500 }
4501 }
4502 goto check_channel_flags;
4503 }
4504
4505 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4506 http_msg_closing:
4507 /* nothing else to forward, just waiting for the output buffer
4508 * to be empty and for the shutw_now to take effect.
4509 */
4510 if (channel_is_empty(chn)) {
4511 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4512 goto http_msg_closed;
4513 }
4514 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004515 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004516 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004517 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004518 if (strm_sess(s)->listener->counters)
4519 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004520 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004521 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004522 goto end;
4523 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004524 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004525 return;
4526 }
4527
4528 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4529 http_msg_closed:
4530 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004531 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004532 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004533 goto end;
4534 }
4535
4536 check_channel_flags:
4537 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4538 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4539 /* if we've just closed an output, let's switch */
4540 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4541 goto http_msg_closing;
4542 }
4543
4544 end:
4545 chn->analysers &= AN_RES_FLT_END;
4546 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4547 chn->analysers |= AN_RES_FLT_XFER_DATA;
4548 channel_auto_close(chn);
4549 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004550 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004551}
4552
Christopher Fauletef70e252020-01-28 09:26:19 +01004553/* Forward a response generated by HAProxy (error/redirect/return). This
4554 * function forwards all pending incoming data. If <final> is set to 0, nothing
4555 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004556 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004557 * returned. If an error occurred, 0 is returned. If it fails, this function
4558 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004559 */
4560int http_forward_proxy_resp(struct stream *s, int final)
4561{
4562 struct channel *req = &s->req;
4563 struct channel *res = &s->res;
4564 struct htx *htx = htxbuf(&res->buf);
4565 size_t data;
4566
4567 if (final) {
4568 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004569
4570 if (!http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004571 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004572
4573 channel_auto_read(req);
4574 channel_abort(req);
4575 channel_auto_close(req);
4576 channel_htx_erase(req, htxbuf(&req->buf));
4577
4578 res->wex = tick_add_ifset(now_ms, res->wto);
4579 channel_auto_read(res);
4580 channel_auto_close(res);
4581 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004582 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet810df062020-07-22 16:20:34 +02004583 htxbuf(&res->buf)->flags |= HTX_FL_EOI; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004584 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004585 else {
4586 /* Send ASAP informational messages. Rely on CF_EOI for final
4587 * response.
4588 */
4589 res->flags |= CF_SEND_DONTWAIT;
4590 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004591
4592 data = htx->data - co_data(res);
4593 c_adv(res, data);
4594 htx->first = -1;
4595 res->total += data;
4596 return 1;
4597}
4598
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004599void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004600 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004601{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004602 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004603 if (!(s->flags & SF_ERR_MASK))
4604 s->flags |= err;
4605 if (!(s->flags & SF_FINST_MASK))
4606 s->flags |= finst;
4607}
4608
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004609void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004610{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004611 if (!msg) {
4612 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4613 goto end;
4614 }
4615
4616 if (http_reply_message(s, msg) == -1) {
4617 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004618 * it is already an internal error. If it was already a "const"
4619 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004620 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004621 if (s->txn->status == 500) {
4622 if (s->txn->flags & TX_CONST_REPLY)
4623 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004624 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004625 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004626 s->txn->status = 500;
4627 s->txn->http_reply = NULL;
4628 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4629 }
4630
4631end:
4632 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
4633 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
4634
Christopher Faulet0f226952018-10-22 09:29:56 +02004635 channel_auto_read(&s->req);
4636 channel_abort(&s->req);
4637 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004638 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004639 channel_auto_read(&s->res);
4640 channel_auto_close(&s->res);
4641 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004642}
4643
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004644struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004645{
4646 const int msgnum = http_get_status_idx(s->txn->status);
4647
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004648 if (s->txn->http_reply)
4649 return s->txn->http_reply;
4650 else if (s->be->replies[msgnum])
4651 return s->be->replies[msgnum];
4652 else if (strm_fe(s)->replies[msgnum])
4653 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004654 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004655 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004656}
4657
Christopher Faulet40e6b552020-06-25 16:04:50 +02004658/* Produces an HTX message from an http reply. Depending on the http reply type,
4659 * a, errorfile, an raw file or a log-format string is used. On success, it
4660 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4661 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004662 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004663int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004664{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004665 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004666 struct htx_sl *sl;
4667 struct buffer *body = NULL;
4668 const char *status, *reason, *clen, *ctype;
4669 unsigned int slflags;
4670 int ret = 0;
4671
Christopher Faulete29a97e2020-05-14 14:49:25 +02004672 /*
4673 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4674 *
4675 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4676 * as no payload if NULL. the TXN status code is set with the status
4677 * of the original reply.
4678 */
4679
4680 if (reply->type == HTTP_REPLY_INDIRECT) {
4681 if (reply->body.reply)
4682 reply = reply->body.reply;
4683 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004684 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4685 /* get default error message */
4686 if (reply == s->txn->http_reply)
4687 s->txn->http_reply = NULL;
4688 reply = http_error_message(s);
4689 if (reply->type == HTTP_REPLY_INDIRECT) {
4690 if (reply->body.reply)
4691 reply = reply->body.reply;
4692 }
4693 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004694
4695 if (reply->type == HTTP_REPLY_ERRMSG) {
4696 /* implicit or explicit error message*/
4697 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004698 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004699 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004700 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004701 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004702 }
4703 else {
4704 /* no payload, file or log-format string */
4705 if (reply->type == HTTP_REPLY_RAW) {
4706 /* file */
4707 body = &reply->body.obj;
4708 }
4709 else if (reply->type == HTTP_REPLY_LOGFMT) {
4710 /* log-format string */
4711 body = alloc_trash_chunk();
4712 if (!body)
4713 goto fail_alloc;
4714 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4715 }
4716 /* else no payload */
4717
4718 status = ultoa(reply->status);
4719 reason = http_get_reason(reply->status);
4720 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4721 if (!body || !b_data(body))
4722 slflags |= HTX_SL_F_BODYLESS;
4723 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4724 if (!sl)
4725 goto fail;
4726 sl->info.res.status = reply->status;
4727
4728 clen = (body ? ultoa(b_data(body)) : "0");
4729 ctype = reply->ctype;
4730
4731 if (!LIST_ISEMPTY(&reply->hdrs)) {
4732 struct http_reply_hdr *hdr;
4733 struct buffer *value = alloc_trash_chunk();
4734
4735 if (!value)
4736 goto fail;
4737
4738 list_for_each_entry(hdr, &reply->hdrs, list) {
4739 chunk_reset(value);
4740 value->data = build_logline(s, value->area, value->size, &hdr->value);
4741 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4742 free_trash_chunk(value);
4743 goto fail;
4744 }
4745 chunk_reset(value);
4746 }
4747 free_trash_chunk(value);
4748 }
4749
4750 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4751 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4752 !htx_add_endof(htx, HTX_BLK_EOH) ||
4753 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))) ||
4754 !htx_add_endof(htx, HTX_BLK_EOM))
4755 goto fail;
4756 }
4757
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004758 leave:
4759 if (reply->type == HTTP_REPLY_LOGFMT)
4760 free_trash_chunk(body);
4761 return ret;
4762
4763 fail_alloc:
4764 if (!(s->flags & SF_ERR_MASK))
4765 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004766 /* fall through */
4767 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004768 ret = -1;
4769 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004770}
4771
4772/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004773 * occurs -1 is returned and the response channel is truncated, removing this
4774 * way the faulty reply. This function may fail when the reply is formatted
4775 * (http_reply_to_htx) or when the reply is forwarded
4776 * (http_forward_proxy_resp). On the last case, it is because a
4777 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004778 */
4779int http_reply_message(struct stream *s, struct http_reply *reply)
4780{
4781 struct channel *res = &s->res;
4782 struct htx *htx = htx_from_buf(&res->buf);
4783
4784 if (s->txn->status == -1)
4785 s->txn->status = reply->status;
4786 channel_htx_truncate(res, htx);
4787
4788 if (http_reply_to_htx(s, htx, reply) == -1)
4789 goto fail;
4790
4791 htx_to_buf(htx, &s->res.buf);
4792 if (!http_forward_proxy_resp(s, 1))
4793 goto fail;
4794 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004795
4796 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004797 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004798 if (!(s->flags & SF_ERR_MASK))
4799 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004800 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004801}
4802
Christopher Faulet304cc402019-07-15 15:46:28 +02004803/* Return the error message corresponding to si->err_type. It is assumed
4804 * that the server side is closed. Note that err_type is actually a
4805 * bitmask, where almost only aborts may be cumulated with other
4806 * values. We consider that aborted operations are more important
4807 * than timeouts or errors due to the fact that nobody else in the
4808 * logs might explain incomplete retries. All others should avoid
4809 * being cumulated. It should normally not be possible to have multiple
4810 * aborts at once, but just in case, the first one in sequence is reported.
4811 * Note that connection errors appearing on the second request of a keep-alive
4812 * connection are not reported since this allows the client to retry.
4813 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004814void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004815{
4816 int err_type = si->err_type;
4817
4818 /* set s->txn->status for http_error_message(s) */
4819 s->txn->status = 503;
4820
4821 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004822 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4823 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004824 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004825 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4826 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4827 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004828 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004829 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4830 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004831 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004832 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4833 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004834 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004835 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4836 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4837 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004838 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004839 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4840 (s->flags & SF_SRV_REUSED) ? NULL :
4841 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004842 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004843 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4844 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4845 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004846 else { /* SI_ET_CONN_OTHER and others */
4847 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004848 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4849 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004850 }
4851}
4852
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004853
Christopher Faulet4a28a532019-03-01 11:19:40 +01004854/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4855 * on success and -1 on error.
4856 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004857static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004858{
4859 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4860 * then we must send an HTTP/1.1 100 Continue intermediate response.
4861 */
4862 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4863 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4864 struct ist hdr = { .ptr = "Expect", .len = 6 };
4865 struct http_hdr_ctx ctx;
4866
4867 ctx.blk = NULL;
4868 /* Expect is allowed in 1.1, look for it */
4869 if (http_find_header(htx, hdr, &ctx, 0) &&
4870 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004871 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004872 return -1;
4873 http_remove_header(htx, &ctx);
4874 }
4875 }
4876 return 0;
4877}
4878
Christopher Faulet23a3c792018-11-28 10:01:23 +01004879/* Send a 100-Continue response to the client. It returns 0 on success and -1
4880 * on error. The response channel is updated accordingly.
4881 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004882static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004883{
4884 struct channel *res = &s->res;
4885 struct htx *htx = htx_from_buf(&res->buf);
4886 struct htx_sl *sl;
4887 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4888 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004889
4890 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4891 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4892 if (!sl)
4893 goto fail;
4894 sl->info.res.status = 100;
4895
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004896 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004897 goto fail;
4898
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004899 if (!http_forward_proxy_resp(s, 0))
4900 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004901 return 0;
4902
4903 fail:
4904 /* If an error occurred, remove the incomplete HTTP response from the
4905 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004906 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004907 return -1;
4908}
4909
Christopher Faulet12c51e22018-11-28 15:59:42 +01004910
Christopher Faulet0f226952018-10-22 09:29:56 +02004911/*
4912 * Capture headers from message <htx> according to header list <cap_hdr>, and
4913 * fill the <cap> pointers appropriately.
4914 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004915static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004916{
4917 struct cap_hdr *h;
4918 int32_t pos;
4919
Christopher Fauleta3f15502019-05-13 15:27:23 +02004920 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004921 struct htx_blk *blk = htx_get_blk(htx, pos);
4922 enum htx_blk_type type = htx_get_blk_type(blk);
4923 struct ist n, v;
4924
4925 if (type == HTX_BLK_EOH)
4926 break;
4927 if (type != HTX_BLK_HDR)
4928 continue;
4929
4930 n = htx_get_blk_name(htx, blk);
4931
4932 for (h = cap_hdr; h; h = h->next) {
4933 if (h->namelen && (h->namelen == n.len) &&
4934 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4935 if (cap[h->index] == NULL)
4936 cap[h->index] =
4937 pool_alloc(h->pool);
4938
4939 if (cap[h->index] == NULL) {
4940 ha_alert("HTTP capture : out of memory.\n");
4941 break;
4942 }
4943
4944 v = htx_get_blk_value(htx, blk);
4945 if (v.len > h->len)
4946 v.len = h->len;
4947
4948 memcpy(cap[h->index], v.ptr, v.len);
4949 cap[h->index][v.len]=0;
4950 }
4951 }
4952 }
4953}
4954
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004955/* Delete a value in a header between delimiters <from> and <next>. The header
4956 * itself is delimited by <start> and <end> pointers. The number of characters
4957 * displaced is returned, and the pointer to the first delimiter is updated if
4958 * required. The function tries as much as possible to respect the following
4959 * principles :
4960 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4961 * in which case <next> is simply removed
4962 * - set exactly one space character after the new first delimiter, unless there
4963 * are not enough characters in the block being moved to do so.
4964 * - remove unneeded spaces before the previous delimiter and after the new
4965 * one.
4966 *
4967 * It is the caller's responsibility to ensure that :
4968 * - <from> points to a valid delimiter or <start> ;
4969 * - <next> points to a valid delimiter or <end> ;
4970 * - there are non-space chars before <from>.
4971 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004972static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004973{
4974 char *prev = *from;
4975
4976 if (prev == start) {
4977 /* We're removing the first value. eat the semicolon, if <next>
4978 * is lower than <end> */
4979 if (next < end)
4980 next++;
4981
4982 while (next < end && HTTP_IS_SPHT(*next))
4983 next++;
4984 }
4985 else {
4986 /* Remove useless spaces before the old delimiter. */
4987 while (HTTP_IS_SPHT(*(prev-1)))
4988 prev--;
4989 *from = prev;
4990
4991 /* copy the delimiter and if possible a space if we're
4992 * not at the end of the line.
4993 */
4994 if (next < end) {
4995 *prev++ = *next++;
4996 if (prev + 1 < next)
4997 *prev++ = ' ';
4998 while (next < end && HTTP_IS_SPHT(*next))
4999 next++;
5000 }
5001 }
5002 memmove(prev, next, end - next);
5003 return (prev - next);
5004}
5005
Christopher Faulet0f226952018-10-22 09:29:56 +02005006
5007/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08005008 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02005009 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005010static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02005011{
5012 struct ist dst = ist2(str, 0);
5013
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005014 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005015 goto end;
5016 if (dst.len + 1 > len)
5017 goto end;
5018 dst.ptr[dst.len++] = ' ';
5019
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005020 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02005021 goto end;
5022 if (dst.len + 1 > len)
5023 goto end;
5024 dst.ptr[dst.len++] = ' ';
5025
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005026 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02005027 end:
5028 return dst.len;
5029}
5030
5031/*
5032 * Print a debug line with a start line.
5033 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005034static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005035{
5036 struct session *sess = strm_sess(s);
5037 int max;
5038
5039 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5040 dir,
5041 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5042 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5043
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005044 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005045 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005046 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005047 trash.area[trash.data++] = ' ';
5048
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005049 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005050 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005051 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005052 trash.area[trash.data++] = ' ';
5053
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005054 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005055 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005056 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005057 trash.area[trash.data++] = '\n';
5058
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005059 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005060}
5061
5062/*
5063 * Print a debug line with a header.
5064 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005065static 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 +02005066{
5067 struct session *sess = strm_sess(s);
5068 int max;
5069
5070 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5071 dir,
5072 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5073 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5074
5075 max = n.len;
5076 UBOUND(max, trash.size - trash.data - 3);
5077 chunk_memcat(&trash, n.ptr, max);
5078 trash.area[trash.data++] = ':';
5079 trash.area[trash.data++] = ' ';
5080
5081 max = v.len;
5082 UBOUND(max, trash.size - trash.data - 1);
5083 chunk_memcat(&trash, v.ptr, max);
5084 trash.area[trash.data++] = '\n';
5085
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005086 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005087}
5088
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005089/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5090 * In case of allocation failure, everything allocated is freed and NULL is
5091 * returned. Otherwise the new transaction is assigned to the stream and
5092 * returned.
5093 */
5094struct http_txn *http_alloc_txn(struct stream *s)
5095{
5096 struct http_txn *txn = s->txn;
5097
5098 if (txn)
5099 return txn;
5100
5101 txn = pool_alloc(pool_head_http_txn);
5102 if (!txn)
5103 return txn;
5104
5105 s->txn = txn;
5106 return txn;
5107}
5108
5109void http_txn_reset_req(struct http_txn *txn)
5110{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005111 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005112 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5113}
5114
5115void http_txn_reset_res(struct http_txn *txn)
5116{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005117 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005118 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5119}
5120
5121/*
5122 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5123 * the required fields are properly allocated and that we only need to (re)init
5124 * them. This should be used before processing any new request.
5125 */
5126void http_init_txn(struct stream *s)
5127{
5128 struct http_txn *txn = s->txn;
5129 struct conn_stream *cs = objt_cs(s->si[0].end);
5130
5131 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5132 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5133 : 0);
5134 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005135 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005136 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005137
5138 txn->cookie_first_date = 0;
5139 txn->cookie_last_date = 0;
5140
5141 txn->srv_cookie = NULL;
5142 txn->cli_cookie = NULL;
5143 txn->uri = NULL;
5144
5145 http_txn_reset_req(txn);
5146 http_txn_reset_res(txn);
5147
5148 txn->req.chn = &s->req;
5149 txn->rsp.chn = &s->res;
5150
5151 txn->auth.method = HTTP_AUTH_UNKNOWN;
5152
5153 vars_init(&s->vars_txn, SCOPE_TXN);
5154 vars_init(&s->vars_reqres, SCOPE_REQ);
5155}
5156
5157/* to be used at the end of a transaction */
5158void http_end_txn(struct stream *s)
5159{
5160 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005161
5162 /* these ones will have been dynamically allocated */
5163 pool_free(pool_head_requri, txn->uri);
5164 pool_free(pool_head_capture, txn->cli_cookie);
5165 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005166 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005167
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005168 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005169 txn->uri = NULL;
5170 txn->srv_cookie = NULL;
5171 txn->cli_cookie = NULL;
5172
Christopher Faulet59399252019-11-07 14:27:52 +01005173 if (!LIST_ISEMPTY(&s->vars_txn.head))
5174 vars_prune(&s->vars_txn, s->sess, s);
5175 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5176 vars_prune(&s->vars_reqres, s->sess, s);
5177}
5178
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005179
5180DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005181
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005182__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005183static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005184{
5185}
5186
5187
5188/*
5189 * Local variables:
5190 * c-indent-level: 8
5191 * c-basic-offset: 8
5192 * End:
5193 */