blob: d89b7d5d7ca83f445b3b85a44bb281b89c103cb4 [file] [log] [blame]
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02001/*
2 * HTTP protocol analyzer
3 *
4 * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreaudcc048a2020-06-04 19:11:43 +020013#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020014#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020016#include <haproxy/backend.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020017#include <haproxy/base64.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020018#include <haproxy/capture-t.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020019#include <haproxy/channel.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020020#include <haproxy/check.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020021#include <haproxy/connection.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020022#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020023#include <haproxy/filters.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020024#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020025#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020027#include <haproxy/htx.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020028#include <haproxy/log.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020029#include <haproxy/net_helper.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020030#include <haproxy/proxy.h>
Willy Tarreau7cd8b6e2020-06-02 17:32:26 +020031#include <haproxy/regex.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020032#include <haproxy/server-t.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020033#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020034#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020035#include <haproxy/stream_interface.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020036#include <haproxy/trace.h>
Willy Tarreau8c42b8a2020-06-04 19:27:34 +020037#include <haproxy/uri_auth-t.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020038#include <haproxy/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020039
Christopher Faulete0768eb2018-10-03 16:38:02 +020040
Christopher Fauleteea8fc72019-11-05 16:18:10 +010041#define TRACE_SOURCE &trace_strm
42
Christopher Faulet377c5a52018-10-24 21:21:30 +020043extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020044
Christopher Fauleta8a46e22019-07-16 14:53:09 +020045struct pool_head *pool_head_requri = NULL;
46struct pool_head *pool_head_capture = NULL;
47
48
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020049static void http_end_request(struct stream *s);
50static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020051
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020052static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
53static int http_del_hdr_value(char *start, char *end, char **from, char *next);
54static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020055static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
56static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v);
Christopher Faulet0f226952018-10-22 09:29:56 +020057
Christopher Fauletb58f62b2020-01-13 16:40:13 +010058static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020059static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020060
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020061static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
62static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020063
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020064static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
65static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020066
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020067static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
68static int http_reply_100_continue(struct stream *s);
Christopher Faulet23a3c792018-11-28 10:01:23 +010069
Christopher Faulete0768eb2018-10-03 16:38:02 +020070/* This stream analyser waits for a complete HTTP request. It returns 1 if the
71 * processing can continue on next analysers, or zero if it either needs more
72 * data or wants to immediately abort the request (eg: timeout, error, ...). It
73 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
74 * when it has nothing left to do, and may remove any analyser when it wants to
75 * abort.
76 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020077int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020078{
Christopher Faulet9768c262018-10-22 09:34:31 +020079
Christopher Faulete0768eb2018-10-03 16:38:02 +020080 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020081 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020082 *
Christopher Faulet9768c262018-10-22 09:34:31 +020083 * Once the start line and all headers are received, we may perform a
84 * capture of the error (if any), and we will set a few fields. We also
85 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020086 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020087 struct session *sess = s->sess;
88 struct http_txn *txn = s->txn;
89 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020090 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010091 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020092
Christopher Fauleteea8fc72019-11-05 16:18:10 +010093 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020094
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010095 htx = htxbuf(&req->buf);
Christopher Faulet9768c262018-10-22 09:34:31 +020096
Willy Tarreau4236f032019-03-05 10:43:32 +010097 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +020098 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +010099 stream_inc_http_req_ctr(s);
100 stream_inc_http_err_ctr(s);
Emeric Brun28976442020-10-07 08:50:09 +0200101 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200102 if (htx->flags & HTX_FL_PARSING_ERROR)
103 goto return_bad_req;
104 else
105 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100106 }
107
Christopher Faulete0768eb2018-10-03 16:38:02 +0200108 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200109 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200110
111 /* If there is data available for analysis, log the end of the idle time. */
Christopher 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);
Emeric Brun28976442020-10-07 08:50:09 +0200152 proxy_inc_fe_req_ctr(sess->listener, sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100153 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher 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);
Emeric Brun28976442020-10-07 08:50:09 +0200179 proxy_inc_fe_req_ctr(sess->listener, 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);
Emeric Brun28976442020-10-07 08:50:09 +0200206 proxy_inc_fe_req_ctr(sess->listener, 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);
Emeric Brun28976442020-10-07 08:50:09 +0200274 proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200275
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 Tarreau9b7587a2020-10-15 07:32:10 +0200757 if (!sockaddr_alloc(&s->target_addr, NULL, 0)) {
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 Faulet63c69a92020-11-16 16:03:35 +01001047 /* CONNECT requests have no body */
1048 if (txn->meth == HTTP_METH_CONNECT)
1049 goto http_end;
1050
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001051 if (msg->msg_state < HTTP_MSG_BODY)
1052 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001053
Christopher Faulete0768eb2018-10-03 16:38:02 +02001054 /* We have to parse the HTTP request body to find any required data.
1055 * "balance url_param check_post" should have been the only way to get
1056 * into this. We were brought here after HTTP header analysis, so all
1057 * related structures are ready.
1058 */
1059
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001060 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001061 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001062 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001063 }
1064
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001065 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001066
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001067 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1068 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001069 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001070 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001071 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001072 goto http_end;
1073
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001074 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001075 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1076 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001077 if (!(s->flags & SF_ERR_MASK))
1078 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001079 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1080 if (sess->listener->counters)
1081 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1082 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001083 }
1084
1085 /* we get here if we need to wait for more data */
1086 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1087 /* Not enough data. We'll re-use the http-request
1088 * timeout here. Ideally, we should set the timeout
1089 * relative to the accept() date. We just set the
1090 * request timeout once at the beginning of the
1091 * request.
1092 */
1093 channel_dont_connect(req);
1094 if (!tick_isset(req->analyse_exp))
1095 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001096 DBG_TRACE_DEVEL("waiting for more data",
1097 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001098 return 0;
1099 }
1100
1101 http_end:
1102 /* The situation will not evolve, so let's give up on the analysis. */
1103 s->logs.tv_request = now; /* update the request timer to reflect full request */
1104 req->analysers &= ~an_bit;
1105 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001106 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001107 return 1;
1108
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001109 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001110 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001111 if (!(s->flags & SF_ERR_MASK))
1112 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001113 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001114 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletbe20cf32020-01-24 11:41:38 +01001115 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001116 if (sess->listener->counters)
1117 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
1118 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001119
Christopher Faulete0768eb2018-10-03 16:38:02 +02001120 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001121 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001122 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1123 if (sess->listener->counters)
1124 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1125 /* fall through */
1126
1127 return_prx_cond:
1128 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001129
1130 if (!(s->flags & SF_ERR_MASK))
1131 s->flags |= SF_ERR_PRXCOND;
1132 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001133 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001134
Christopher Faulete0768eb2018-10-03 16:38:02 +02001135 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001136 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001137 DBG_TRACE_DEVEL("leaving on error",
1138 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001139 return 0;
1140}
1141
1142/* This function is an analyser which forwards request body (including chunk
1143 * sizes if any). It is called as soon as we must forward, even if we forward
1144 * zero byte. The only situation where it must not be called is when we're in
1145 * tunnel mode and we want to forward till the close. It's used both to forward
1146 * remaining data and to resync after end of body. It expects the msg_state to
1147 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1148 * read more data, or 1 once we can go on with next request or end the stream.
1149 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1150 * bytes of pending data + the headers if not already done.
1151 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001152int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001153{
1154 struct session *sess = s->sess;
1155 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001156 struct http_msg *msg = &txn->req;
1157 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001158 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001159 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001160
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001161 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001162
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001163 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001164
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001165 if (htx->flags & HTX_FL_PARSING_ERROR)
1166 goto return_bad_req;
1167 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1168 goto return_int_err;
1169
Christopher Faulete0768eb2018-10-03 16:38:02 +02001170 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1171 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1172 /* Output closed while we were sending data. We must abort and
1173 * wake the other side up.
1174 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001175
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001176 /* Don't abort yet if we had L7 retries activated and it
1177 * was a write error, we may recover.
1178 */
1179 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001180 (s->si[1].flags & SI_FL_L7_RETRY)) {
1181 DBG_TRACE_DEVEL("leaving on L7 retry",
1182 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001183 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001184 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001185 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001186 http_end_request(s);
1187 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001188 DBG_TRACE_DEVEL("leaving on error",
1189 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001190 return 1;
1191 }
1192
1193 /* Note that we don't have to send 100-continue back because we don't
1194 * need the data to complete our job, and it's up to the server to
1195 * decide whether to return 100, 417 or anything else in return of
1196 * an "Expect: 100-continue" header.
1197 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001198 if (msg->msg_state == HTTP_MSG_BODY)
1199 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001200
Christopher Faulete0768eb2018-10-03 16:38:02 +02001201 /* in most states, we should abort in case of early close */
1202 channel_auto_close(req);
1203
1204 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001205 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001206 if (req->flags & CF_EOI)
1207 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001208 }
1209 else {
1210 /* We can't process the buffer's contents yet */
1211 req->flags |= CF_WAKE_WRITE;
1212 goto missing_data_or_waiting;
1213 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001214 }
1215
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001216 if (msg->msg_state >= HTTP_MSG_ENDING)
1217 goto ending;
1218
1219 if (txn->meth == HTTP_METH_CONNECT) {
1220 msg->msg_state = HTTP_MSG_ENDING;
1221 goto ending;
1222 }
1223
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001224 /* Forward input data. We get it by removing all outgoing data not
1225 * forwarded yet from HTX data size. If there are some data filters, we
1226 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001227 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001228 if (HAS_REQ_DATA_FILTERS(s)) {
1229 ret = flt_http_payload(s, msg, htx->data);
1230 if (ret < 0)
1231 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001232 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001233 }
1234 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001235 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001236 if (msg->flags & HTTP_MSGF_XFER_LEN)
1237 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001238 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001239
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001240 if (htx->data != co_data(req))
1241 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001242
Christopher Faulet9768c262018-10-22 09:34:31 +02001243 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001244 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1245 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001246 */
1247 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1248 goto missing_data_or_waiting;
1249
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001250 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001251
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001252 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001253 req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
1254
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001255 /* other states, ENDING...TUNNEL */
1256 if (msg->msg_state >= HTTP_MSG_DONE)
1257 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001258
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001259 if (HAS_REQ_DATA_FILTERS(s)) {
1260 ret = flt_http_end(s, msg);
1261 if (ret <= 0) {
1262 if (!ret)
1263 goto missing_data_or_waiting;
1264 goto return_bad_req;
1265 }
1266 }
1267
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001268 if (txn->meth == HTTP_METH_CONNECT)
1269 msg->msg_state = HTTP_MSG_TUNNEL;
1270 else {
1271 msg->msg_state = HTTP_MSG_DONE;
1272 req->to_forward = 0;
1273 }
1274
1275 done:
1276 /* we don't want to forward closes on DONE except in tunnel mode. */
1277 if (!(txn->flags & TX_CON_WANT_TUN))
1278 channel_dont_close(req);
1279
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001280 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001281 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001282 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001283 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1284 if (req->flags & CF_SHUTW) {
1285 /* request errors are most likely due to the
1286 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001287 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001288 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001289 goto return_bad_req;
1290 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001291 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001292 return 1;
1293 }
1294
1295 /* If "option abortonclose" is set on the backend, we want to monitor
1296 * the client's connection and forward any shutdown notification to the
1297 * server, which will decide whether to close or to go on processing the
1298 * request. We only do that in tunnel mode, and not in other modes since
1299 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001300 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001301 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001302 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001303 s->si[1].flags |= SI_FL_NOLINGER;
1304 channel_auto_close(req);
1305 }
1306 else if (s->txn->meth == HTTP_METH_POST) {
1307 /* POST requests may require to read extra CRLF sent by broken
1308 * browsers and which could cause an RST to be sent upon close
1309 * on some systems (eg: Linux). */
1310 channel_auto_read(req);
1311 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001312 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1313 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001314 return 0;
1315
1316 missing_data_or_waiting:
1317 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001318 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001319 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001320
1321 waiting:
1322 /* waiting for the last bits to leave the buffer */
1323 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001324 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001325
1326 /* When TE: chunked is used, we need to get there again to parse remaining
1327 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1328 * And when content-length is used, we never want to let the possible
1329 * shutdown be forwarded to the other side, as the state machine will
1330 * take care of it once the client responds. It's also important to
1331 * prevent TIME_WAITs from accumulating on the backend side, and for
1332 * HTTP/2 where the last frame comes with a shutdown.
1333 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001334 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001335 channel_dont_close(req);
1336
1337 /* We know that more data are expected, but we couldn't send more that
1338 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1339 * system knows it must not set a PUSH on this first part. Interactive
1340 * modes are already handled by the stream sock layer. We must not do
1341 * this in content-length mode because it could present the MSG_MORE
1342 * flag with the last block of forwarded data, which would cause an
1343 * additional delay to be observed by the receiver.
1344 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02001345 if (HAS_REQ_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001346 req->flags |= CF_EXPECT_MORE;
1347
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001348 DBG_TRACE_DEVEL("waiting for more data to forward",
1349 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001350 return 0;
1351
Christopher Faulet93e02d82019-03-08 14:18:50 +01001352 return_cli_abort:
1353 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1354 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001355 if (sess->listener->counters)
1356 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001357 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001358 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001359 if (!(s->flags & SF_ERR_MASK))
1360 s->flags |= SF_ERR_CLICL;
1361 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001362 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001363
1364 return_srv_abort:
1365 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1366 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001367 if (sess->listener->counters)
1368 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001369 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001370 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001371 if (!(s->flags & SF_ERR_MASK))
1372 s->flags |= SF_ERR_SRVCL;
1373 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001374 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001375
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001376 return_int_err:
1377 if (!(s->flags & SF_ERR_MASK))
1378 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001379 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001380 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001381 if (sess->listener->counters)
1382 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001383 if (objt_server(s->target))
1384 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001385 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001386 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001387
Christopher Faulet93e02d82019-03-08 14:18:50 +01001388 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001389 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001390 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001391 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001392 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001393 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001394
Christopher Fauletb8a53712019-12-16 11:29:38 +01001395 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001396 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001397 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001398 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001399 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001400 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001401 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001402 }
1403 req->analysers &= AN_REQ_FLT_END;
1404 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 +01001405 if (!(s->flags & SF_ERR_MASK))
1406 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001407 if (!(s->flags & SF_FINST_MASK))
1408 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001409 DBG_TRACE_DEVEL("leaving on error ",
1410 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001411 return 0;
1412}
1413
Olivier Houcharda254a372019-04-05 15:30:12 +02001414/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1415/* Returns 0 if we can attempt to retry, -1 otherwise */
1416static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1417{
1418 struct channel *req, *res;
1419 int co_data;
1420
1421 si->conn_retries--;
1422 if (si->conn_retries < 0)
1423 return -1;
1424
Willy Tarreau223995e2019-05-04 10:38:31 +02001425 if (objt_server(s->target))
1426 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1427 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1428
Olivier Houcharda254a372019-04-05 15:30:12 +02001429 req = &s->req;
1430 res = &s->res;
1431 /* Remove any write error from the request, and read error from the response */
1432 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1433 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1434 res->analysers = 0;
1435 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard8cabc972020-05-12 22:18:14 +02001436 s->flags &= ~SF_ADDR_SET;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001437 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001438 si->exp = TICK_ETERNITY;
1439 res->rex = TICK_ETERNITY;
1440 res->to_forward = 0;
1441 res->analyse_exp = TICK_ETERNITY;
1442 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001443 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001444 si_release_endpoint(&s->si[1]);
1445 b_free(&req->buf);
1446 /* Swap the L7 buffer with the channel buffer */
1447 /* We know we stored the co_data as b_data, so get it there */
1448 co_data = b_data(&si->l7_buffer);
1449 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1450 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1451
1452 co_set_data(req, co_data);
1453 b_reset(&res->buf);
1454 co_set_data(res, 0);
1455 return 0;
1456}
1457
Christopher Faulete0768eb2018-10-03 16:38:02 +02001458/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1459 * processing can continue on next analysers, or zero if it either needs more
1460 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1461 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1462 * when it has nothing left to do, and may remove any analyser when it wants to
1463 * abort.
1464 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001465int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001466{
Christopher Faulet9768c262018-10-22 09:34:31 +02001467 /*
1468 * We will analyze a complete HTTP response to check the its syntax.
1469 *
1470 * Once the start line and all headers are received, we may perform a
1471 * capture of the error (if any), and we will set a few fields. We also
1472 * logging and finally headers capture.
1473 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001474 struct session *sess = s->sess;
1475 struct http_txn *txn = s->txn;
1476 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001477 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001478 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001479 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001480 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001481 int n;
1482
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001483 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001484
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001485 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001486
Willy Tarreau4236f032019-03-05 10:43:32 +01001487 /* Parsing errors are caught here */
1488 if (htx->flags & HTX_FL_PARSING_ERROR)
1489 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001490 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1491 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001492
Christopher Faulete0768eb2018-10-03 16:38:02 +02001493 /*
1494 * Now we quickly check if we have found a full valid response.
1495 * If not so, we check the FD and buffer states before leaving.
1496 * A full response is indicated by the fact that we have seen
1497 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1498 * responses are checked first.
1499 *
1500 * Depending on whether the client is still there or not, we
1501 * may send an error response back or not. Note that normally
1502 * we should only check for HTTP status there, and check I/O
1503 * errors somewhere else.
1504 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001505 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001506 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001507 /* 1: have we encountered a read error ? */
1508 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001509 struct connection *conn = NULL;
1510
Olivier Houchard865d8392019-05-03 22:46:27 +02001511 if (objt_cs(s->si[1].end))
1512 conn = objt_cs(s->si[1].end)->conn;
1513
1514 if (si_b->flags & SI_FL_L7_RETRY &&
1515 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001516 /* If we arrive here, then CF_READ_ERROR was
1517 * set by si_cs_recv() because we matched a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001518 * status, otherwise it would have removed
Olivier Houcharda254a372019-04-05 15:30:12 +02001519 * the SI_FL_L7_RETRY flag, so it's ok not
1520 * to check s->be->retry_type.
1521 */
1522 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1523 return 0;
1524 }
1525
Olivier Houchard6db16992019-05-17 15:40:49 +02001526 if (txn->flags & TX_NOT_FIRST)
1527 goto abort_keep_alive;
1528
Olivier Houcharda798bf52019-03-08 18:52:00 +01001529 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001530 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001531 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001532 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001533 }
1534
Christopher Faulete0768eb2018-10-03 16:38:02 +02001535 rep->analysers &= AN_RES_FLT_END;
1536 txn->status = 502;
1537
1538 /* Check to see if the server refused the early data.
1539 * If so, just send a 425
1540 */
Willy Tarreauee99aaf2020-06-23 05:58:20 +02001541 if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001542 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001543 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001544 do_l7_retry(s, si_b) == 0) {
1545 DBG_TRACE_DEVEL("leaving on L7 retry",
1546 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001547 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001548 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001549 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001550 }
1551
1552 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001553 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001554
1555 if (!(s->flags & SF_ERR_MASK))
1556 s->flags |= SF_ERR_SRVCL;
1557 if (!(s->flags & SF_FINST_MASK))
1558 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001559 DBG_TRACE_DEVEL("leaving on error",
1560 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001561 return 0;
1562 }
1563
Christopher Faulet9768c262018-10-22 09:34:31 +02001564 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001565 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001566 if ((si_b->flags & SI_FL_L7_RETRY) &&
1567 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001568 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1569 DBG_TRACE_DEVEL("leaving on L7 retry",
1570 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001571 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001572 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001573 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001574 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001575 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001576 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001577 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001578 }
1579
Christopher Faulete0768eb2018-10-03 16:38:02 +02001580 rep->analysers &= AN_RES_FLT_END;
1581 txn->status = 504;
1582 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001583 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001584
1585 if (!(s->flags & SF_ERR_MASK))
1586 s->flags |= SF_ERR_SRVTO;
1587 if (!(s->flags & SF_FINST_MASK))
1588 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001589 DBG_TRACE_DEVEL("leaving on error",
1590 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001591 return 0;
1592 }
1593
Christopher Faulet9768c262018-10-22 09:34:31 +02001594 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001595 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001596 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1597 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001598 if (sess->listener->counters)
1599 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001600 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001601 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001602
1603 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001604 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001605 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001606
1607 if (!(s->flags & SF_ERR_MASK))
1608 s->flags |= SF_ERR_CLICL;
1609 if (!(s->flags & SF_FINST_MASK))
1610 s->flags |= SF_FINST_H;
1611
1612 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001613 DBG_TRACE_DEVEL("leaving on error",
1614 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001615 return 0;
1616 }
1617
Christopher Faulet9768c262018-10-22 09:34:31 +02001618 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001619 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001620 if ((si_b->flags & SI_FL_L7_RETRY) &&
1621 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001622 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1623 DBG_TRACE_DEVEL("leaving on L7 retry",
1624 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001625 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001626 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001627 }
1628
Olivier Houchard6db16992019-05-17 15:40:49 +02001629 if (txn->flags & TX_NOT_FIRST)
1630 goto abort_keep_alive;
1631
Olivier Houcharda798bf52019-03-08 18:52:00 +01001632 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001633 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001634 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001635 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001636 }
1637
Christopher Faulete0768eb2018-10-03 16:38:02 +02001638 rep->analysers &= AN_RES_FLT_END;
1639 txn->status = 502;
1640 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001641 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001642
1643 if (!(s->flags & SF_ERR_MASK))
1644 s->flags |= SF_ERR_SRVCL;
1645 if (!(s->flags & SF_FINST_MASK))
1646 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001647 DBG_TRACE_DEVEL("leaving on error",
1648 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001649 return 0;
1650 }
1651
Christopher Faulet9768c262018-10-22 09:34:31 +02001652 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001653 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001654 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001655 goto abort_keep_alive;
1656
Olivier Houcharda798bf52019-03-08 18:52:00 +01001657 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001658 if (objt_server(s->target))
1659 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001660 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001661
1662 if (!(s->flags & SF_ERR_MASK))
1663 s->flags |= SF_ERR_CLICL;
1664 if (!(s->flags & SF_FINST_MASK))
1665 s->flags |= SF_FINST_H;
1666
1667 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001668 DBG_TRACE_DEVEL("leaving on error",
1669 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001670 return 0;
1671 }
1672
1673 channel_dont_close(rep);
1674 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001675 DBG_TRACE_DEVEL("waiting for more data",
1676 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001677 return 0;
1678 }
1679
1680 /* More interesting part now : we know that we have a complete
1681 * response which at least looks like HTTP. We have an indicator
1682 * of each header's length, so we can parse them quickly.
1683 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001684 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001685 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001686 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001687
Christopher Faulet9768c262018-10-22 09:34:31 +02001688 /* 0: we might have to print this header in debug mode */
1689 if (unlikely((global.mode & MODE_DEBUG) &&
1690 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1691 int32_t pos;
1692
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001693 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001694
Christopher Fauleta3f15502019-05-13 15:27:23 +02001695 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001696 struct htx_blk *blk = htx_get_blk(htx, pos);
1697 enum htx_blk_type type = htx_get_blk_type(blk);
1698
1699 if (type == HTX_BLK_EOH)
1700 break;
1701 if (type != HTX_BLK_HDR)
1702 continue;
1703
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001704 http_debug_hdr("srvhdr", s,
1705 htx_get_blk_name(htx, blk),
1706 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001707 }
1708 }
1709
Christopher Faulet03599112018-11-27 11:21:21 +01001710 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001711 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001712 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001713 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001714 if (sl->flags & HTX_SL_F_XFER_LEN) {
1715 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001716 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001717 if (sl->flags & HTX_SL_F_BODYLESS)
1718 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001719 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001720
1721 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001722 if (n < 1 || n > 5)
1723 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001724
Christopher Faulete0768eb2018-10-03 16:38:02 +02001725 /* when the client triggers a 4xx from the server, it's most often due
1726 * to a missing object or permission. These events should be tracked
1727 * because if they happen often, it may indicate a brute force or a
1728 * vulnerability scan.
1729 */
1730 if (n == 4)
1731 stream_inc_http_err_ctr(s);
1732
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001733 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001734 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001735 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1);
1736 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001737
Christopher Faulete0768eb2018-10-03 16:38:02 +02001738 /* Adjust server's health based on status code. Note: status codes 501
1739 * and 505 are triggered on demand by client request, so we must not
1740 * count them as server failures.
1741 */
1742 if (objt_server(s->target)) {
1743 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001744 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001745 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001746 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001747 }
1748
1749 /*
1750 * We may be facing a 100-continue response, or any other informational
1751 * 1xx response which is non-final, in which case this is not the right
1752 * response, and we're waiting for the next one. Let's allow this response
1753 * to go to the client and wait for the next one. There's an exception for
1754 * 101 which is used later in the code to switch protocols.
1755 */
1756 if (txn->status < 200 &&
1757 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001758 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001759 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001760 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001761 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001762 txn->status = 0;
1763 s->logs.t_data = -1; /* was not a response yet */
Christopher Faulet7d518452020-08-31 11:07:07 +02001764 rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001765 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001766 }
1767
1768 /*
1769 * 2: check for cacheability.
1770 */
1771
1772 switch (txn->status) {
1773 case 200:
1774 case 203:
1775 case 204:
1776 case 206:
1777 case 300:
1778 case 301:
1779 case 404:
1780 case 405:
1781 case 410:
1782 case 414:
1783 case 501:
1784 break;
1785 default:
1786 /* RFC7231#6.1:
1787 * Responses with status codes that are defined as
1788 * cacheable by default (e.g., 200, 203, 204, 206,
1789 * 300, 301, 404, 405, 410, 414, and 501 in this
1790 * specification) can be reused by a cache with
1791 * heuristic expiration unless otherwise indicated
1792 * by the method definition or explicit cache
1793 * controls [RFC7234]; all other status codes are
1794 * not cacheable by default.
1795 */
1796 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1797 break;
1798 }
1799
1800 /*
1801 * 3: we may need to capture headers
1802 */
1803 s->logs.logwait &= ~LW_RESP;
1804 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001805 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001806
Christopher Faulet9768c262018-10-22 09:34:31 +02001807 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001808 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1809 txn->status == 101)) {
1810 /* Either we've established an explicit tunnel, or we're
1811 * switching the protocol. In both cases, we're very unlikely
1812 * to understand the next protocols. We have to switch to tunnel
1813 * mode, so that we transfer the request and responses then let
1814 * this protocol pass unmodified. When we later implement specific
1815 * parsers for such protocols, we'll want to check the Upgrade
1816 * header which contains information about that protocol for
1817 * responses with status 101 (eg: see RFC2817 about TLS).
1818 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001819 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001820 }
1821
Christopher Faulet61608322018-11-23 16:23:45 +01001822 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1823 * 407 (Proxy-Authenticate) responses and set the connection to private
1824 */
1825 srv_conn = cs_conn(objt_cs(s->si[1].end));
1826 if (srv_conn) {
1827 struct ist hdr;
1828 struct http_hdr_ctx ctx;
1829
1830 if (txn->status == 401)
1831 hdr = ist("WWW-Authenticate");
1832 else if (txn->status == 407)
1833 hdr = ist("Proxy-Authenticate");
1834 else
1835 goto end;
1836
1837 ctx.blk = NULL;
1838 while (http_find_header(htx, hdr, &ctx, 0)) {
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001839 /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM",
1840 * possibly followed by blanks and a base64 string, the connection
1841 * is private. Since it's a mess to deal with, we only check for
1842 * values starting with "NTLM" or "Nego". Note that often multiple
1843 * headers are sent by the server there.
1844 */
1845 if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) ||
Willy Tarreau49a1d282020-05-07 19:10:15 +02001846 (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) {
Olivier Houchard250031e2019-05-29 15:01:50 +02001847 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet08016ab2020-07-01 16:10:06 +02001848 conn_set_owner(srv_conn, sess, NULL);
Christopher Faulet21ddc742020-07-01 15:26:14 +02001849 conn_set_private(srv_conn);
Ilya Shipitsin6b79f382020-07-23 00:32:55 +05001850 /* If it fail now, the same will be done in mux->detach() callback */
Christopher Faulet08016ab2020-07-01 16:10:06 +02001851 session_add_conn(srv_conn->owner, srv_conn, srv_conn->target);
Willy Tarreauf1dcced2020-05-07 19:27:02 +02001852 break;
Olivier Houchard250031e2019-05-29 15:01:50 +02001853 }
Christopher Faulet61608322018-11-23 16:23:45 +01001854 }
1855 }
1856
1857 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001858 /* we want to have the response time before we start processing it */
1859 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1860
1861 /* end of job, return OK */
1862 rep->analysers &= ~an_bit;
1863 rep->analyse_exp = TICK_ETERNITY;
1864 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001865 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001866 return 1;
1867
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001868 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001869 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001870 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001871 if (sess->listener->counters)
1872 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001873 if (objt_server(s->target))
1874 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001875 txn->status = 500;
1876 if (!(s->flags & SF_ERR_MASK))
1877 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001878 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001879
1880 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001881 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001882 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001883 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001884 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001885 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001886 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001887 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001888 do_l7_retry(s, si_b) == 0) {
1889 DBG_TRACE_DEVEL("leaving on L7 retry",
1890 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001891 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001892 }
Christopher Faulet47365272018-10-31 17:40:50 +01001893 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001894 /* fall through */
1895
Christopher Fauletb8a53712019-12-16 11:29:38 +01001896 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001897 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001898
1899 if (!(s->flags & SF_ERR_MASK))
1900 s->flags |= SF_ERR_PRXCOND;
1901 if (!(s->flags & SF_FINST_MASK))
1902 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001903
1904 s->si[1].flags |= SI_FL_NOLINGER;
1905 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete58c0002020-03-02 16:21:01 +01001906 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001907 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001908 DBG_TRACE_DEVEL("leaving on error",
1909 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001910 return 0;
1911
Christopher Faulete0768eb2018-10-03 16:38:02 +02001912 abort_keep_alive:
1913 /* A keep-alive request to the server failed on a network error.
1914 * The client is required to retry. We need to close without returning
1915 * any other information so that the client retries.
1916 */
1917 txn->status = 0;
1918 rep->analysers &= AN_RES_FLT_END;
1919 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001920 s->logs.logwait = 0;
1921 s->logs.level = 0;
1922 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001923 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001924 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1925 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001926 return 0;
1927}
1928
1929/* This function performs all the processing enabled for the current response.
1930 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1931 * and updates s->res.analysers. It might make sense to explode it into several
1932 * other functions. It works like process_request (see indications above).
1933 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001934int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001935{
1936 struct session *sess = s->sess;
1937 struct http_txn *txn = s->txn;
1938 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001939 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001940 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001941 enum rule_result ret = HTTP_RULE_RES_CONT;
1942
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001943 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1944 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001945
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001946 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001947
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001948 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001949
1950 /* The stats applet needs to adjust the Connection header but we don't
1951 * apply any filter there.
1952 */
1953 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1954 rep->analysers &= ~an_bit;
1955 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001956 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001957 }
1958
1959 /*
1960 * We will have to evaluate the filters.
1961 * As opposed to version 1.2, now they will be evaluated in the
1962 * filters order and not in the header order. This means that
1963 * each filter has to be validated among all headers.
1964 *
1965 * Filters are tried with ->be first, then with ->fe if it is
1966 * different from ->be.
1967 *
1968 * Maybe we are in resume condiion. In this case I choose the
1969 * "struct proxy" which contains the rule list matching the resume
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001970 * pointer. If none of these "struct proxy" match, I initialise
Christopher Faulete0768eb2018-10-03 16:38:02 +02001971 * the process with the first one.
1972 *
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001973 * In fact, I check only correspondence between the current list
Christopher Faulete0768eb2018-10-03 16:38:02 +02001974 * pointer and the ->fe rule list. If it doesn't match, I initialize
1975 * the loop with the ->be.
1976 */
1977 if (s->current_rule_list == &sess->fe->http_res_rules)
1978 cur_proxy = sess->fe;
1979 else
1980 cur_proxy = s->be;
1981 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001982 /* evaluate http-response rules */
1983 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001984 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001985
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001986 switch (ret) {
1987 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1988 goto return_prx_yield;
1989
1990 case HTTP_RULE_RES_CONT:
1991 case HTTP_RULE_RES_STOP: /* nothing to do */
1992 break;
1993
1994 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1995 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001996
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001997 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1998 goto return_prx_cond;
1999
2000 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01002001 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002002
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002003 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
2004 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002005
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002006 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
2007 goto return_int_err;
2008 }
2009
2010 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002011
Christopher Faulete0768eb2018-10-03 16:38:02 +02002012 /* check whether we're already working on the frontend */
2013 if (cur_proxy == sess->fe)
2014 break;
2015 cur_proxy = sess->fe;
2016 }
2017
Christopher Faulete0768eb2018-10-03 16:38:02 +02002018 /* OK that's all we can do for 1xx responses */
2019 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002020 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002021
2022 /*
2023 * Now check for a server cookie.
2024 */
2025 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002026 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002027
2028 /*
2029 * Check for cache-control or pragma headers if required.
2030 */
2031 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002032 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002033
2034 /*
2035 * Add server cookie in the response if needed
2036 */
2037 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
2038 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
2039 (!(s->flags & SF_DIRECT) ||
2040 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
2041 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
2042 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
2043 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
2044 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
2045 !(s->flags & SF_IGNORE_PRST)) {
2046 /* the server is known, it's not the one the client requested, or the
2047 * cookie's last seen date needs to be refreshed. We have to
2048 * insert a set-cookie here, except if we want to insert only on POST
2049 * requests and this one isn't. Note that servers which don't have cookies
2050 * (eg: some backup servers) will return a full cookie removal request.
2051 */
2052 if (!objt_server(s->target)->cookie) {
2053 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002054 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02002055 s->be->cookie_name);
2056 }
2057 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002058 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002059
2060 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2061 /* emit last_date, which is mandatory */
2062 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2063 s30tob64((date.tv_sec+3) >> 2,
2064 trash.area + trash.data);
2065 trash.data += 5;
2066
2067 if (s->be->cookie_maxlife) {
2068 /* emit first_date, which is either the original one or
2069 * the current date.
2070 */
2071 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2072 s30tob64(txn->cookie_first_date ?
2073 txn->cookie_first_date >> 2 :
2074 (date.tv_sec+3) >> 2,
2075 trash.area + trash.data);
2076 trash.data += 5;
2077 }
2078 }
2079 chunk_appendf(&trash, "; path=/");
2080 }
2081
2082 if (s->be->cookie_domain)
2083 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2084
2085 if (s->be->ck_opts & PR_CK_HTTPONLY)
2086 chunk_appendf(&trash, "; HttpOnly");
2087
2088 if (s->be->ck_opts & PR_CK_SECURE)
2089 chunk_appendf(&trash, "; Secure");
2090
Christopher Faulet2f533902020-01-21 11:06:48 +01002091 if (s->be->cookie_attrs)
2092 chunk_appendf(&trash, "; %s", s->be->cookie_attrs);
2093
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002094 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002095 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002096
2097 txn->flags &= ~TX_SCK_MASK;
2098 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2099 /* the server did not change, only the date was updated */
2100 txn->flags |= TX_SCK_UPDATED;
2101 else
2102 txn->flags |= TX_SCK_INSERTED;
2103
2104 /* Here, we will tell an eventual cache on the client side that we don't
2105 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2106 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2107 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2108 */
2109 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2110
2111 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2112
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002113 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002114 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002115 }
2116 }
2117
2118 /*
2119 * Check if result will be cacheable with a cookie.
2120 * We'll block the response if security checks have caught
2121 * nasty things such as a cacheable cookie.
2122 */
2123 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2124 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2125 (s->be->options & PR_O_CHK_CACHE)) {
2126 /* we're in presence of a cacheable response containing
2127 * a set-cookie header. We'll block it as requested by
2128 * the 'checkcache' option, and send an alert.
2129 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002130 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2131 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2132 send_log(s->be, LOG_ALERT,
2133 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2134 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002135 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002136 }
2137
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002138 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002139 /*
2140 * Evaluate after-response rules before forwarding the response. rules
2141 * from the backend are evaluated first, then one from the frontend if
2142 * it differs.
2143 */
2144 if (!http_eval_after_res_rules(s))
2145 goto return_int_err;
2146
Christopher Faulete0768eb2018-10-03 16:38:02 +02002147 /* Always enter in the body analyzer */
2148 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2149 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2150
2151 /* if the user wants to log as soon as possible, without counting
2152 * bytes from the server, then this is the right moment. We have
2153 * to temporarily assign bytes_out to log what we currently have.
2154 */
2155 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2156 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002157 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002158 s->do_log(s);
2159 s->logs.bytes_out = 0;
2160 }
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002161
Christopher Fauletb8a53712019-12-16 11:29:38 +01002162 done:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01002163 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002164 rep->analysers &= ~an_bit;
2165 rep->analyse_exp = TICK_ETERNITY;
2166 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002167
Christopher Fauletb8a53712019-12-16 11:29:38 +01002168 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002169 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002170 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002171 if (sess->listener->counters)
2172 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002173 if (objt_server(s->target))
2174 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002175 goto return_prx_err;
2176
2177 return_int_err:
2178 txn->status = 500;
2179 if (!(s->flags & SF_ERR_MASK))
2180 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002181 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002182 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2183 if (objt_server(s->target))
2184 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002185 if (objt_server(s->target))
2186 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002187 goto return_prx_err;
2188
2189 return_bad_res:
2190 txn->status = 502;
Christopher Fauleta20a6532020-02-05 10:16:41 +01002191 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2192 if (objt_server(s->target)) {
2193 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
2194 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2195 }
Christopher Fauletb8a53712019-12-16 11:29:38 +01002196 /* fall through */
2197
2198 return_prx_err:
2199 http_reply_and_close(s, txn->status, http_error_message(s));
2200 /* fall through */
2201
2202 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002203 s->logs.t_data = -1; /* was not a valid response */
2204 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002205
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002206 if (!(s->flags & SF_ERR_MASK))
2207 s->flags |= SF_ERR_PRXCOND;
2208 if (!(s->flags & SF_FINST_MASK))
2209 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002210
Christopher Faulete58c0002020-03-02 16:21:01 +01002211 rep->analysers &= AN_RES_FLT_END;
2212 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002213 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002214 DBG_TRACE_DEVEL("leaving on error",
2215 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002216 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002217
2218 return_prx_yield:
2219 channel_dont_close(rep);
2220 DBG_TRACE_DEVEL("waiting for more data",
2221 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2222 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002223}
2224
2225/* This function is an analyser which forwards response body (including chunk
2226 * sizes if any). It is called as soon as we must forward, even if we forward
2227 * zero byte. The only situation where it must not be called is when we're in
2228 * tunnel mode and we want to forward till the close. It's used both to forward
2229 * remaining data and to resync after end of body. It expects the msg_state to
2230 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2231 * read more data, or 1 once we can go on with next request or end the stream.
2232 *
2233 * It is capable of compressing response data both in content-length mode and
2234 * in chunked mode. The state machines follows different flows depending on
2235 * whether content-length and chunked modes are used, since there are no
2236 * trailers in content-length :
2237 *
2238 * chk-mode cl-mode
2239 * ,----- BODY -----.
2240 * / \
2241 * V size > 0 V chk-mode
2242 * .--> SIZE -------------> DATA -------------> CRLF
2243 * | | size == 0 | last byte |
2244 * | v final crlf v inspected |
2245 * | TRAILERS -----------> DONE |
2246 * | |
2247 * `----------------------------------------------'
2248 *
2249 * Compression only happens in the DATA state, and must be flushed in final
2250 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2251 * is performed at once on final states for all bytes parsed, or when leaving
2252 * on missing data.
2253 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002254int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002255{
2256 struct session *sess = s->sess;
2257 struct http_txn *txn = s->txn;
2258 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002259 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002260 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002261
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002262 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002263
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002264 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002265
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002266 if (htx->flags & HTX_FL_PARSING_ERROR)
2267 goto return_bad_res;
2268 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2269 goto return_int_err;
2270
Christopher Faulete0768eb2018-10-03 16:38:02 +02002271 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002272 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002273 /* Output closed while we were sending data. We must abort and
2274 * wake the other side up.
2275 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002276 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002277 http_end_response(s);
2278 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002279 DBG_TRACE_DEVEL("leaving on error",
2280 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002281 return 1;
2282 }
2283
Christopher Faulet9768c262018-10-22 09:34:31 +02002284 if (msg->msg_state == HTTP_MSG_BODY)
2285 msg->msg_state = HTTP_MSG_DATA;
2286
Christopher Faulete0768eb2018-10-03 16:38:02 +02002287 /* in most states, we should abort in case of early close */
2288 channel_auto_close(res);
2289
Christopher Faulete0768eb2018-10-03 16:38:02 +02002290 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002291 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002292 if (res->flags & CF_EOI)
2293 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002294 }
2295 else {
2296 /* We can't process the buffer's contents yet */
2297 res->flags |= CF_WAKE_WRITE;
2298 goto missing_data_or_waiting;
2299 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002300 }
2301
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002302 if (msg->msg_state >= HTTP_MSG_ENDING)
2303 goto ending;
2304
2305 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2306 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2307 msg->msg_state = HTTP_MSG_ENDING;
2308 goto ending;
2309 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002310
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002311 /* Forward input data. We get it by removing all outgoing data not
2312 * forwarded yet from HTX data size. If there are some data filters, we
2313 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002314 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002315 if (HAS_RSP_DATA_FILTERS(s)) {
2316 ret = flt_http_payload(s, msg, htx->data);
2317 if (ret < 0)
2318 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002319 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002320 }
2321 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002322 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002323 if (msg->flags & HTTP_MSGF_XFER_LEN)
2324 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002325 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002326
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002327 if (htx->data != co_data(res))
2328 goto missing_data_or_waiting;
2329
2330 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2331 msg->msg_state = HTTP_MSG_ENDING;
2332 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002333 }
2334
Christopher Faulet9768c262018-10-22 09:34:31 +02002335 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002336 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2337 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002338 */
2339 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2340 goto missing_data_or_waiting;
2341
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002342 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002343
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002344 ending:
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002345 res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */
2346
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002347 /* other states, ENDING...TUNNEL */
2348 if (msg->msg_state >= HTTP_MSG_DONE)
2349 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002350
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002351 if (HAS_RSP_DATA_FILTERS(s)) {
2352 ret = flt_http_end(s, msg);
2353 if (ret <= 0) {
2354 if (!ret)
2355 goto missing_data_or_waiting;
2356 goto return_bad_res;
2357 }
2358 }
2359
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002360 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2361 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2362 msg->msg_state = HTTP_MSG_TUNNEL;
2363 goto ending;
2364 }
2365 else {
2366 msg->msg_state = HTTP_MSG_DONE;
2367 res->to_forward = 0;
2368 }
2369
2370 done:
2371
2372 channel_dont_close(res);
2373
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002374 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002375 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002376 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002377 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2378 if (res->flags & CF_SHUTW) {
2379 /* response errors are most likely due to the
2380 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002381 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002382 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002383 goto return_bad_res;
2384 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002385 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002386 return 1;
2387 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002388 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2389 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002390 return 0;
2391
2392 missing_data_or_waiting:
2393 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002394 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002395
2396 /* stop waiting for data if the input is closed before the end. If the
2397 * client side was already closed, it means that the client has aborted,
2398 * so we don't want to count this as a server abort. Otherwise it's a
2399 * server abort.
2400 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002401 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002402 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002403 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002404 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002405 if (htx_is_empty(htx))
2406 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002407 }
2408
Christopher Faulete0768eb2018-10-03 16:38:02 +02002409 /* When TE: chunked is used, we need to get there again to parse
2410 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002411 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2412 * are filters registered on the stream, we don't want to forward a
2413 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002414 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002415 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002416 channel_dont_close(res);
2417
2418 /* We know that more data are expected, but we couldn't send more that
2419 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2420 * system knows it must not set a PUSH on this first part. Interactive
2421 * modes are already handled by the stream sock layer. We must not do
2422 * this in content-length mode because it could present the MSG_MORE
2423 * flag with the last block of forwarded data, which would cause an
2424 * additional delay to be observed by the receiver.
2425 */
Christopher Faulet2151cdd2020-07-22 16:34:59 +02002426 if (HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002427 res->flags |= CF_EXPECT_MORE;
2428
2429 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002430 DBG_TRACE_DEVEL("waiting for more data to forward",
2431 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002432 return 0;
2433
Christopher Faulet93e02d82019-03-08 14:18:50 +01002434 return_srv_abort:
2435 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2436 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002437 if (sess->listener->counters)
2438 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002439 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002440 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002441 if (!(s->flags & SF_ERR_MASK))
2442 s->flags |= SF_ERR_SRVCL;
2443 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002444
Christopher Faulet93e02d82019-03-08 14:18:50 +01002445 return_cli_abort:
2446 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2447 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002448 if (sess->listener->counters)
2449 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002450 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002451 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002452 if (!(s->flags & SF_ERR_MASK))
2453 s->flags |= SF_ERR_CLICL;
2454 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002455
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002456 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002457 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002458 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002459 if (sess->listener->counters)
2460 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002461 if (objt_server(s->target))
2462 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002463 if (!(s->flags & SF_ERR_MASK))
2464 s->flags |= SF_ERR_INTERNAL;
2465 goto return_error;
2466
Christopher Faulet93e02d82019-03-08 14:18:50 +01002467 return_bad_res:
2468 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2469 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002470 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002471 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2472 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002473 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002474 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002475 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002476
Christopher Faulet93e02d82019-03-08 14:18:50 +01002477 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002478 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002479 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002480 res->analysers &= AN_RES_FLT_END;
2481 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 +02002482 if (!(s->flags & SF_FINST_MASK))
2483 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002484 DBG_TRACE_DEVEL("leaving on error",
2485 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002486 return 0;
2487}
2488
Christopher Fauletf2824e62018-10-01 12:12:37 +02002489/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002490 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002491 * as too large a request to build a valid response.
2492 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002493int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002494{
Christopher Faulet99daf282018-11-28 22:58:13 +01002495 struct channel *req = &s->req;
2496 struct channel *res = &s->res;
2497 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002498 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002499 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002500 struct ist status, reason, location;
2501 unsigned int flags;
Christopher Faulet08e66462019-05-23 16:44:59 +02002502 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002503
2504 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002505 if (!chunk) {
2506 if (!(s->flags & SF_ERR_MASK))
2507 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002508 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002509 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002510
Christopher Faulet99daf282018-11-28 22:58:13 +01002511 /*
2512 * Create the location
2513 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002514 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002515 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002516 case REDIRECT_TYPE_SCHEME: {
2517 struct http_hdr_ctx ctx;
2518 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002519
Christopher Faulet99daf282018-11-28 22:58:13 +01002520 host = ist("");
2521 ctx.blk = NULL;
2522 if (http_find_header(htx, ist("Host"), &ctx, 0))
2523 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002524
Christopher Faulet297fbb42019-05-13 14:41:27 +02002525 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002526 path = http_get_path(htx_sl_req_uri(sl));
2527 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002528 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002529 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2530 int qs = 0;
2531 while (qs < path.len) {
2532 if (*(path.ptr + qs) == '?') {
2533 path.len = qs;
2534 break;
2535 }
2536 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002537 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002538 }
2539 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002540 else
2541 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002542
Christopher Faulet99daf282018-11-28 22:58:13 +01002543 if (rule->rdr_str) { /* this is an old "redirect" rule */
2544 /* add scheme */
2545 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2546 goto fail;
2547 }
2548 else {
2549 /* add scheme with executing log format */
2550 chunk->data += build_logline(s, chunk->area + chunk->data,
2551 chunk->size - chunk->data,
2552 &rule->rdr_fmt);
2553 }
2554 /* add "://" + host + path */
2555 if (!chunk_memcat(chunk, "://", 3) ||
2556 !chunk_memcat(chunk, host.ptr, host.len) ||
2557 !chunk_memcat(chunk, path.ptr, path.len))
2558 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002559
Christopher Faulet99daf282018-11-28 22:58:13 +01002560 /* append a slash at the end of the location if needed and missing */
2561 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2562 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2563 if (chunk->data + 1 >= chunk->size)
2564 goto fail;
2565 chunk->area[chunk->data++] = '/';
2566 }
2567 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002568 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002569
Christopher Faulet99daf282018-11-28 22:58:13 +01002570 case REDIRECT_TYPE_PREFIX: {
2571 struct ist path;
2572
Christopher Faulet297fbb42019-05-13 14:41:27 +02002573 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002574 path = http_get_path(htx_sl_req_uri(sl));
2575 /* build message using path */
Tim Duesterhused526372020-03-05 17:56:33 +01002576 if (isttest(path)) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002577 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2578 int qs = 0;
2579 while (qs < path.len) {
2580 if (*(path.ptr + qs) == '?') {
2581 path.len = qs;
2582 break;
2583 }
2584 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002585 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002586 }
2587 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002588 else
2589 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002590
Christopher Faulet99daf282018-11-28 22:58:13 +01002591 if (rule->rdr_str) { /* this is an old "redirect" rule */
2592 /* add prefix. Note that if prefix == "/", we don't want to
2593 * add anything, otherwise it makes it hard for the user to
2594 * configure a self-redirection.
2595 */
2596 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2597 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2598 goto fail;
2599 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002600 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002601 else {
2602 /* add prefix with executing log format */
2603 chunk->data += build_logline(s, chunk->area + chunk->data,
2604 chunk->size - chunk->data,
2605 &rule->rdr_fmt);
2606 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002607
Christopher Faulet99daf282018-11-28 22:58:13 +01002608 /* add path */
2609 if (!chunk_memcat(chunk, path.ptr, path.len))
2610 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002611
Christopher Faulet99daf282018-11-28 22:58:13 +01002612 /* append a slash at the end of the location if needed and missing */
2613 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2614 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2615 if (chunk->data + 1 >= chunk->size)
2616 goto fail;
2617 chunk->area[chunk->data++] = '/';
2618 }
2619 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002620 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002621 case REDIRECT_TYPE_LOCATION:
2622 default:
2623 if (rule->rdr_str) { /* this is an old "redirect" rule */
2624 /* add location */
2625 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2626 goto fail;
2627 }
2628 else {
2629 /* add location with executing log format */
2630 chunk->data += build_logline(s, chunk->area + chunk->data,
2631 chunk->size - chunk->data,
2632 &rule->rdr_fmt);
2633 }
2634 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002635 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002636 location = ist2(chunk->area, chunk->data);
2637
2638 /*
2639 * Create the 30x response
2640 */
2641 switch (rule->code) {
2642 case 308:
2643 status = ist("308");
2644 reason = ist("Permanent Redirect");
2645 break;
2646 case 307:
2647 status = ist("307");
2648 reason = ist("Temporary Redirect");
2649 break;
2650 case 303:
2651 status = ist("303");
2652 reason = ist("See Other");
2653 break;
2654 case 301:
2655 status = ist("301");
2656 reason = ist("Moved Permanently");
2657 break;
2658 case 302:
2659 default:
2660 status = ist("302");
2661 reason = ist("Found");
2662 break;
2663 }
2664
Christopher Faulet08e66462019-05-23 16:44:59 +02002665 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2666 close = 1;
2667
Christopher Faulet99daf282018-11-28 22:58:13 +01002668 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002669 /* Trim any possible response */
2670 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002671 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2672 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2673 if (!sl)
2674 goto fail;
2675 sl->info.res.status = rule->code;
2676 s->txn->status = rule->code;
2677
Christopher Faulet08e66462019-05-23 16:44:59 +02002678 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2679 goto fail;
2680
2681 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002682 !htx_add_header(htx, ist("Location"), location))
2683 goto fail;
2684
2685 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2686 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2687 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002688 }
2689
2690 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002691 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2692 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002693 }
2694
Christopher Faulet99daf282018-11-28 22:58:13 +01002695 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2696 goto fail;
2697
Kevin Zhu96b36392020-01-07 09:42:55 +01002698 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01002699 if (!http_forward_proxy_resp(s, 1))
2700 goto fail;
Christopher Faulet99daf282018-11-28 22:58:13 +01002701
Christopher Faulet60b33a52020-01-28 09:18:10 +01002702 if (rule->flags & REDIRECT_FLAG_FROM_REQ) {
2703 /* let's log the request time */
2704 s->logs.tv_request = now;
2705 req->analysers &= AN_REQ_FLT_END;
Christopher Faulet99daf282018-11-28 22:58:13 +01002706
Christopher Faulet60b33a52020-01-28 09:18:10 +01002707 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
2708 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
2709 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002710
2711 if (!(s->flags & SF_ERR_MASK))
2712 s->flags |= SF_ERR_LOCAL;
2713 if (!(s->flags & SF_FINST_MASK))
Christopher Faulet60b33a52020-01-28 09:18:10 +01002714 s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002715
Christopher Faulet99daf282018-11-28 22:58:13 +01002716 free_trash_chunk(chunk);
2717 return 1;
2718
2719 fail:
2720 /* If an error occurred, remove the incomplete HTTP response from the
2721 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002722 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002723 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002724 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002725}
2726
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002727/* Replace all headers matching the name <name>. The header value is replaced if
2728 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2729 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2730 * values are evaluated one by one. It returns 0 on success and -1 on error.
2731 */
2732int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2733 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002734{
2735 struct http_hdr_ctx ctx;
2736 struct buffer *output = get_trash_chunk();
2737
Christopher Faulet72333522018-10-24 11:25:02 +02002738 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002739 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002740 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2741 continue;
2742
2743 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2744 if (output->data == -1)
2745 return -1;
2746 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2747 return -1;
2748 }
2749 return 0;
2750}
2751
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002752/* This function executes one of the set-{method,path,query,uri} actions. It
2753 * takes the string from the variable 'replace' with length 'len', then modifies
2754 * the relevant part of the request line accordingly. Then it updates various
2755 * pointers to the next elements which were moved, and the total buffer length.
2756 * It finds the action to be performed in p[2], previously filled by function
2757 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2758 * error, though this can be revisited when this code is finally exploited.
2759 *
2760 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
Christopher Faulet312294f2020-09-02 17:17:44 +02002761 * query string, 3 to replace uri or 4 to replace the path+query.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002762 *
2763 * In query string case, the mark question '?' must be set at the start of the
2764 * string by the caller, event if the replacement query string is empty.
2765 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002766int http_req_replace_stline(int action, const char *replace, int len,
2767 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002768{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002769 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002770
2771 switch (action) {
2772 case 0: // method
2773 if (!http_replace_req_meth(htx, ist2(replace, len)))
2774 return -1;
2775 break;
2776
2777 case 1: // path
Christopher Fauletb8ce5052020-08-31 16:11:57 +02002778 if (!http_replace_req_path(htx, ist2(replace, len), 0))
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002779 return -1;
2780 break;
2781
2782 case 2: // query
2783 if (!http_replace_req_query(htx, ist2(replace, len)))
2784 return -1;
2785 break;
2786
2787 case 3: // uri
2788 if (!http_replace_req_uri(htx, ist2(replace, len)))
2789 return -1;
2790 break;
2791
Christopher Faulet312294f2020-09-02 17:17:44 +02002792 case 4: // path + query
2793 if (!http_replace_req_path(htx, ist2(replace, len), 1))
2794 return -1;
2795 break;
2796
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002797 default:
2798 return -1;
2799 }
2800 return 0;
2801}
2802
2803/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002804 * variable <status> contains the new status code. This function never fails. It
2805 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002806 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002807int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002808{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002809 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002810 char *res;
2811
2812 chunk_reset(&trash);
2813 res = ultoa_o(status, trash.area, trash.size);
2814 trash.data = res - trash.area;
2815
2816 /* Do we have a custom reason format string? */
Tim Duesterhuse296d3e2020-03-05 17:56:31 +01002817 if (!isttest(reason)) {
Christopher Faulet96bff762019-12-17 13:46:18 +01002818 const char *str = http_get_reason(status);
2819 reason = ist2(str, strlen(str));
2820 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002821
Christopher Fauletbde2c4c2020-08-31 16:43:34 +02002822 if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002823 return -1;
2824 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002825}
2826
Christopher Faulet3e964192018-10-24 11:39:23 +02002827/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2828 * transaction <txn>. Returns the verdict of the first rule that prevents
2829 * further processing of the request (auth, deny, ...), and defaults to
2830 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2831 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2832 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2833 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2834 * status.
2835 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002836static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002837 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002838{
2839 struct session *sess = strm_sess(s);
2840 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002841 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002842 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002843 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002844
Christopher Faulet3e964192018-10-24 11:39:23 +02002845 /* If "the current_rule_list" match the executed rule list, we are in
2846 * resume condition. If a resume is needed it is always in the action
2847 * and never in the ACL or converters. In this case, we initialise the
2848 * current rule, and go to the action execution point.
2849 */
2850 if (s->current_rule) {
2851 rule = s->current_rule;
2852 s->current_rule = NULL;
2853 if (s->current_rule_list == rules)
2854 goto resume_execution;
2855 }
2856 s->current_rule_list = rules;
2857
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002858 /* start the ruleset evaluation in strict mode */
2859 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002860
Christopher Faulet3e964192018-10-24 11:39:23 +02002861 list_for_each_entry(rule, rules, list) {
2862 /* check optional condition */
2863 if (rule->cond) {
2864 int ret;
2865
2866 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2867 ret = acl_pass(ret);
2868
2869 if (rule->cond->pol == ACL_COND_UNLESS)
2870 ret = !ret;
2871
2872 if (!ret) /* condition not matched */
2873 continue;
2874 }
2875
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002876 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002877 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002878 /* Always call the action function if defined */
2879 if (rule->action_ptr) {
2880 if ((s->req.flags & CF_READ_ERROR) ||
2881 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2882 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002883 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002884
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002885 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002886 case ACT_RET_CONT:
2887 break;
2888 case ACT_RET_STOP:
2889 rule_ret = HTTP_RULE_RES_STOP;
2890 goto end;
2891 case ACT_RET_YIELD:
2892 s->current_rule = rule;
2893 rule_ret = HTTP_RULE_RES_YIELD;
2894 goto end;
2895 case ACT_RET_ERR:
2896 rule_ret = HTTP_RULE_RES_ERROR;
2897 goto end;
2898 case ACT_RET_DONE:
2899 rule_ret = HTTP_RULE_RES_DONE;
2900 goto end;
2901 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002902 if (txn->status == -1)
2903 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002904 rule_ret = HTTP_RULE_RES_DENY;
2905 goto end;
2906 case ACT_RET_ABRT:
2907 rule_ret = HTTP_RULE_RES_ABRT;
2908 goto end;
2909 case ACT_RET_INV:
2910 rule_ret = HTTP_RULE_RES_BADREQ;
2911 goto end;
2912 }
2913 continue; /* eval the next rule */
2914 }
2915
2916 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002917 switch (rule->action) {
2918 case ACT_ACTION_ALLOW:
2919 rule_ret = HTTP_RULE_RES_STOP;
2920 goto end;
2921
2922 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002923 txn->status = rule->arg.http_reply->status;
2924 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002925 rule_ret = HTTP_RULE_RES_DENY;
2926 goto end;
2927
2928 case ACT_HTTP_REQ_TARPIT:
2929 txn->flags |= TX_CLTARPIT;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02002930 txn->status = rule->arg.http_reply->status;
2931 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3e964192018-10-24 11:39:23 +02002932 rule_ret = HTTP_RULE_RES_DENY;
2933 goto end;
2934
Christopher Faulet3e964192018-10-24 11:39:23 +02002935 case ACT_HTTP_REDIR:
Christopher Faulet90d22a82020-03-06 11:18:39 +01002936 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002937 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002938 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002939 goto end;
2940
2941 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002942 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002943 break;
2944
2945 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002946 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002947 break;
2948
2949 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002950 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002951 break;
2952
2953 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002954 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002955 break;
2956
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002957 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002958 default:
2959 break;
2960 }
2961 }
2962
2963 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002964 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002965 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002966 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002967
Christopher Faulet3e964192018-10-24 11:39:23 +02002968 /* we reached the end of the rules, nothing to report */
2969 return rule_ret;
2970}
2971
2972/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2973 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2974 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2975 * is returned, the process can continue the evaluation of next rule list. If
2976 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2977 * is returned, it means the operation could not be processed and a server error
Christopher Fauleta53abad2020-05-13 08:12:22 +02002978 * must be returned. If *YIELD is returned, the caller must call again the
2979 * function with the same context.
Christopher Faulet3e964192018-10-24 11:39:23 +02002980 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002981static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2982 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002983{
2984 struct session *sess = strm_sess(s);
2985 struct http_txn *txn = s->txn;
Christopher Faulet3e964192018-10-24 11:39:23 +02002986 struct act_rule *rule;
Christopher Faulet3e964192018-10-24 11:39:23 +02002987 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002988 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002989
Christopher Faulet3e964192018-10-24 11:39:23 +02002990 /* If "the current_rule_list" match the executed rule list, we are in
2991 * resume condition. If a resume is needed it is always in the action
2992 * and never in the ACL or converters. In this case, we initialise the
2993 * current rule, and go to the action execution point.
2994 */
2995 if (s->current_rule) {
2996 rule = s->current_rule;
2997 s->current_rule = NULL;
2998 if (s->current_rule_list == rules)
2999 goto resume_execution;
3000 }
3001 s->current_rule_list = rules;
3002
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003003 /* start the ruleset evaluation in strict mode */
3004 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003005
Christopher Faulet3e964192018-10-24 11:39:23 +02003006 list_for_each_entry(rule, rules, list) {
3007 /* check optional condition */
3008 if (rule->cond) {
3009 int ret;
3010
3011 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3012 ret = acl_pass(ret);
3013
3014 if (rule->cond->pol == ACL_COND_UNLESS)
3015 ret = !ret;
3016
3017 if (!ret) /* condition not matched */
3018 continue;
3019 }
3020
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003021 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02003022resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003023
3024 /* Always call the action function if defined */
3025 if (rule->action_ptr) {
3026 if ((s->req.flags & CF_READ_ERROR) ||
3027 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3028 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003029 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003030
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003031 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003032 case ACT_RET_CONT:
3033 break;
3034 case ACT_RET_STOP:
3035 rule_ret = HTTP_RULE_RES_STOP;
3036 goto end;
3037 case ACT_RET_YIELD:
3038 s->current_rule = rule;
3039 rule_ret = HTTP_RULE_RES_YIELD;
3040 goto end;
3041 case ACT_RET_ERR:
3042 rule_ret = HTTP_RULE_RES_ERROR;
3043 goto end;
3044 case ACT_RET_DONE:
3045 rule_ret = HTTP_RULE_RES_DONE;
3046 goto end;
3047 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01003048 if (txn->status == -1)
3049 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003050 rule_ret = HTTP_RULE_RES_DENY;
3051 goto end;
3052 case ACT_RET_ABRT:
3053 rule_ret = HTTP_RULE_RES_ABRT;
3054 goto end;
3055 case ACT_RET_INV:
3056 rule_ret = HTTP_RULE_RES_BADREQ;
3057 goto end;
3058 }
3059 continue; /* eval the next rule */
3060 }
3061
3062 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003063 switch (rule->action) {
3064 case ACT_ACTION_ALLOW:
3065 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3066 goto end;
3067
3068 case ACT_ACTION_DENY:
Christopher Faulet5cb513a2020-05-13 17:56:56 +02003069 txn->status = rule->arg.http_reply->status;
3070 txn->http_reply = rule->arg.http_reply;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003071 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003072 goto end;
3073
3074 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003075 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003076 break;
3077
3078 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003079 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003080 break;
3081
3082 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003083 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003084 break;
3085
3086 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003087 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003088 break;
3089
Christopher Faulet3e964192018-10-24 11:39:23 +02003090 case ACT_HTTP_REDIR:
Christopher Faulet49c2a702020-03-06 15:44:37 +01003091 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003092 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003093 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003094 goto end;
3095
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003096 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003097 default:
3098 break;
3099 }
3100 }
3101
3102 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003103 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003104 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003105 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003106
Christopher Faulet3e964192018-10-24 11:39:23 +02003107 /* we reached the end of the rules, nothing to report */
3108 return rule_ret;
3109}
3110
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003111/* Executes backend and frontend http-after-response rules for the stream <s>,
3112 * in that order. it return 1 on success and 0 on error. It is the caller
3113 * responsibility to catch error or ignore it. If it catches it, this function
3114 * may be called a second time, for the internal error.
3115 */
3116int http_eval_after_res_rules(struct stream *s)
3117{
3118 struct session *sess = s->sess;
3119 enum rule_result ret = HTTP_RULE_RES_CONT;
3120
Christopher Faulet507479b2020-05-15 12:29:46 +02003121 /* Eval after-response ruleset only if the reply is not const */
3122 if (s->txn->flags & TX_CONST_REPLY)
3123 goto end;
3124
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003125 /* prune the request variables if not already done and swap to the response variables. */
3126 if (s->vars_reqres.scope != SCOPE_RES) {
3127 if (!LIST_ISEMPTY(&s->vars_reqres.head))
3128 vars_prune(&s->vars_reqres, s->sess, s);
3129 vars_init(&s->vars_reqres, SCOPE_RES);
3130 }
3131
3132 ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s);
3133 if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be)
3134 ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s);
3135
Christopher Faulet507479b2020-05-15 12:29:46 +02003136 end:
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01003137 /* All other codes than CONTINUE, STOP or DONE are forbidden */
3138 return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE);
3139}
3140
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003141/*
3142 * Manage client-side cookie. It can impact performance by about 2% so it is
3143 * desirable to call it only when needed. This code is quite complex because
3144 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3145 * highly recommended not to touch this part without a good reason !
3146 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003147static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003148{
3149 struct session *sess = s->sess;
3150 struct http_txn *txn = s->txn;
3151 struct htx *htx;
3152 struct http_hdr_ctx ctx;
3153 char *hdr_beg, *hdr_end, *del_from;
3154 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3155 int preserve_hdr;
3156
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003157 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003158 ctx.blk = NULL;
3159 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003160 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003161 del_from = NULL; /* nothing to be deleted */
3162 preserve_hdr = 0; /* assume we may kill the whole header */
3163
3164 /* Now look for cookies. Conforming to RFC2109, we have to support
3165 * attributes whose name begin with a '$', and associate them with
3166 * the right cookie, if we want to delete this cookie.
3167 * So there are 3 cases for each cookie read :
3168 * 1) it's a special attribute, beginning with a '$' : ignore it.
3169 * 2) it's a server id cookie that we *MAY* want to delete : save
3170 * some pointers on it (last semi-colon, beginning of cookie...)
3171 * 3) it's an application cookie : we *MAY* have to delete a previous
3172 * "special" cookie.
3173 * At the end of loop, if a "special" cookie remains, we may have to
3174 * remove it. If no application cookie persists in the header, we
3175 * *MUST* delete it.
3176 *
3177 * Note: RFC2965 is unclear about the processing of spaces around
3178 * the equal sign in the ATTR=VALUE form. A careful inspection of
3179 * the RFC explicitly allows spaces before it, and not within the
3180 * tokens (attrs or values). An inspection of RFC2109 allows that
3181 * too but section 10.1.3 lets one think that spaces may be allowed
3182 * after the equal sign too, resulting in some (rare) buggy
3183 * implementations trying to do that. So let's do what servers do.
3184 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3185 * allowed quoted strings in values, with any possible character
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003186 * after a backslash, including control chars and delimiters, which
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003187 * causes parsing to become ambiguous. Browsers also allow spaces
3188 * within values even without quotes.
3189 *
3190 * We have to keep multiple pointers in order to support cookie
3191 * removal at the beginning, middle or end of header without
3192 * corrupting the header. All of these headers are valid :
3193 *
3194 * hdr_beg hdr_end
3195 * | |
3196 * v |
3197 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3198 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3199 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3200 * | | | | | | |
3201 * | | | | | | |
3202 * | | | | | | +--> next
3203 * | | | | | +----> val_end
3204 * | | | | +-----------> val_beg
3205 * | | | +--------------> equal
3206 * | | +----------------> att_end
3207 * | +---------------------> att_beg
3208 * +--------------------------> prev
3209 *
3210 */
3211 hdr_beg = ctx.value.ptr;
3212 hdr_end = hdr_beg + ctx.value.len;
3213 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3214 /* Iterate through all cookies on this line */
3215
3216 /* find att_beg */
3217 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003218 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003219 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003220 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003221
3222 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3223 att_beg++;
3224
3225 /* find att_end : this is the first character after the last non
3226 * space before the equal. It may be equal to hdr_end.
3227 */
3228 equal = att_end = att_beg;
3229 while (equal < hdr_end) {
3230 if (*equal == '=' || *equal == ',' || *equal == ';')
3231 break;
3232 if (HTTP_IS_SPHT(*equal++))
3233 continue;
3234 att_end = equal;
3235 }
3236
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003237 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003238 * is between <att_beg> and <equal>, both may be identical.
3239 */
3240 /* look for end of cookie if there is an equal sign */
3241 if (equal < hdr_end && *equal == '=') {
3242 /* look for the beginning of the value */
3243 val_beg = equal + 1;
3244 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3245 val_beg++;
3246
3247 /* find the end of the value, respecting quotes */
3248 next = http_find_cookie_value_end(val_beg, hdr_end);
3249
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003250 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003251 val_end = next;
3252 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3253 val_end--;
3254 }
3255 else
3256 val_beg = val_end = next = equal;
3257
3258 /* We have nothing to do with attributes beginning with
3259 * '$'. However, they will automatically be removed if a
3260 * header before them is removed, since they're supposed
3261 * to be linked together.
3262 */
3263 if (*att_beg == '$')
3264 continue;
3265
3266 /* Ignore cookies with no equal sign */
3267 if (equal == next) {
3268 /* This is not our cookie, so we must preserve it. But if we already
3269 * scheduled another cookie for removal, we cannot remove the
3270 * complete header, but we can remove the previous block itself.
3271 */
3272 preserve_hdr = 1;
3273 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003274 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003275 val_end += delta;
3276 next += delta;
3277 hdr_end += delta;
3278 prev = del_from;
3279 del_from = NULL;
3280 }
3281 continue;
3282 }
3283
3284 /* if there are spaces around the equal sign, we need to
3285 * strip them otherwise we'll get trouble for cookie captures,
3286 * or even for rewrites. Since this happens extremely rarely,
3287 * it does not hurt performance.
3288 */
3289 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3290 int stripped_before = 0;
3291 int stripped_after = 0;
3292
3293 if (att_end != equal) {
3294 memmove(att_end, equal, hdr_end - equal);
3295 stripped_before = (att_end - equal);
3296 equal += stripped_before;
3297 val_beg += stripped_before;
3298 }
3299
3300 if (val_beg > equal + 1) {
3301 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3302 stripped_after = (equal + 1) - val_beg;
3303 val_beg += stripped_after;
3304 stripped_before += stripped_after;
3305 }
3306
3307 val_end += stripped_before;
3308 next += stripped_before;
3309 hdr_end += stripped_before;
3310 }
3311 /* now everything is as on the diagram above */
3312
3313 /* First, let's see if we want to capture this cookie. We check
3314 * that we don't already have a client side cookie, because we
3315 * can only capture one. Also as an optimisation, we ignore
3316 * cookies shorter than the declared name.
3317 */
3318 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3319 (val_end - att_beg >= sess->fe->capture_namelen) &&
3320 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3321 int log_len = val_end - att_beg;
3322
3323 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3324 ha_alert("HTTP logging : out of memory.\n");
3325 } else {
3326 if (log_len > sess->fe->capture_len)
3327 log_len = sess->fe->capture_len;
3328 memcpy(txn->cli_cookie, att_beg, log_len);
3329 txn->cli_cookie[log_len] = 0;
3330 }
3331 }
3332
3333 /* Persistence cookies in passive, rewrite or insert mode have the
3334 * following form :
3335 *
3336 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3337 *
3338 * For cookies in prefix mode, the form is :
3339 *
3340 * Cookie: NAME=SRV~VALUE
3341 */
3342 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3343 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3344 struct server *srv = s->be->srv;
3345 char *delim;
3346
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003347 /* if we're in cookie prefix mode, we'll search the delimiter so that we
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003348 * have the server ID between val_beg and delim, and the original cookie between
3349 * delim+1 and val_end. Otherwise, delim==val_end :
3350 *
3351 * hdr_beg
3352 * |
3353 * v
3354 * NAME=SRV; # in all but prefix modes
3355 * NAME=SRV~OPAQUE ; # in prefix mode
3356 * || || | |+-> next
3357 * || || | +--> val_end
3358 * || || +---------> delim
3359 * || |+------------> val_beg
3360 * || +-------------> att_end = equal
3361 * |+-----------------> att_beg
3362 * +------------------> prev
3363 *
3364 */
3365 if (s->be->ck_opts & PR_CK_PFX) {
3366 for (delim = val_beg; delim < val_end; delim++)
3367 if (*delim == COOKIE_DELIM)
3368 break;
3369 }
3370 else {
3371 char *vbar1;
3372 delim = val_end;
3373 /* Now check if the cookie contains a date field, which would
3374 * appear after a vertical bar ('|') just after the server name
3375 * and before the delimiter.
3376 */
3377 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3378 if (vbar1) {
3379 /* OK, so left of the bar is the server's cookie and
3380 * right is the last seen date. It is a base64 encoded
3381 * 30-bit value representing the UNIX date since the
3382 * epoch in 4-second quantities.
3383 */
3384 int val;
3385 delim = vbar1++;
3386 if (val_end - vbar1 >= 5) {
3387 val = b64tos30(vbar1);
3388 if (val > 0)
3389 txn->cookie_last_date = val << 2;
3390 }
3391 /* look for a second vertical bar */
3392 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3393 if (vbar1 && (val_end - vbar1 > 5)) {
3394 val = b64tos30(vbar1 + 1);
3395 if (val > 0)
3396 txn->cookie_first_date = val << 2;
3397 }
3398 }
3399 }
3400
3401 /* if the cookie has an expiration date and the proxy wants to check
3402 * it, then we do that now. We first check if the cookie is too old,
3403 * then only if it has expired. We detect strict overflow because the
3404 * time resolution here is not great (4 seconds). Cookies with dates
3405 * in the future are ignored if their offset is beyond one day. This
3406 * allows an admin to fix timezone issues without expiring everyone
3407 * and at the same time avoids keeping unwanted side effects for too
3408 * long.
3409 */
3410 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3411 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3412 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3413 txn->flags &= ~TX_CK_MASK;
3414 txn->flags |= TX_CK_OLD;
3415 delim = val_beg; // let's pretend we have not found the cookie
3416 txn->cookie_first_date = 0;
3417 txn->cookie_last_date = 0;
3418 }
3419 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3420 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3421 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3422 txn->flags &= ~TX_CK_MASK;
3423 txn->flags |= TX_CK_EXPIRED;
3424 delim = val_beg; // let's pretend we have not found the cookie
3425 txn->cookie_first_date = 0;
3426 txn->cookie_last_date = 0;
3427 }
3428
3429 /* Here, we'll look for the first running server which supports the cookie.
3430 * This allows to share a same cookie between several servers, for example
3431 * to dedicate backup servers to specific servers only.
3432 * However, to prevent clients from sticking to cookie-less backup server
3433 * when they have incidentely learned an empty cookie, we simply ignore
3434 * empty cookies and mark them as invalid.
3435 * The same behaviour is applied when persistence must be ignored.
3436 */
3437 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3438 srv = NULL;
3439
3440 while (srv) {
3441 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3442 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3443 if ((srv->cur_state != SRV_ST_STOPPED) ||
3444 (s->be->options & PR_O_PERSIST) ||
3445 (s->flags & SF_FORCE_PRST)) {
3446 /* we found the server and we can use it */
3447 txn->flags &= ~TX_CK_MASK;
3448 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3449 s->flags |= SF_DIRECT | SF_ASSIGNED;
3450 s->target = &srv->obj_type;
3451 break;
3452 } else {
3453 /* we found a server, but it's down,
3454 * mark it as such and go on in case
3455 * another one is available.
3456 */
3457 txn->flags &= ~TX_CK_MASK;
3458 txn->flags |= TX_CK_DOWN;
3459 }
3460 }
3461 srv = srv->next;
3462 }
3463
3464 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3465 /* no server matched this cookie or we deliberately skipped it */
3466 txn->flags &= ~TX_CK_MASK;
3467 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3468 txn->flags |= TX_CK_UNUSED;
3469 else
3470 txn->flags |= TX_CK_INVALID;
3471 }
3472
3473 /* depending on the cookie mode, we may have to either :
3474 * - delete the complete cookie if we're in insert+indirect mode, so that
3475 * the server never sees it ;
3476 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003477 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003478 * if we're in cookie prefix mode
3479 */
3480 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3481 int delta; /* negative */
3482
3483 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3484 delta = val_beg - (delim + 1);
3485 val_end += delta;
3486 next += delta;
3487 hdr_end += delta;
3488 del_from = NULL;
3489 preserve_hdr = 1; /* we want to keep this cookie */
3490 }
3491 else if (del_from == NULL &&
3492 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3493 del_from = prev;
3494 }
3495 }
3496 else {
3497 /* This is not our cookie, so we must preserve it. But if we already
3498 * scheduled another cookie for removal, we cannot remove the
3499 * complete header, but we can remove the previous block itself.
3500 */
3501 preserve_hdr = 1;
3502
3503 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003504 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003505 if (att_beg >= del_from)
3506 att_beg += delta;
3507 if (att_end >= del_from)
3508 att_end += delta;
3509 val_beg += delta;
3510 val_end += delta;
3511 next += delta;
3512 hdr_end += delta;
3513 prev = del_from;
3514 del_from = NULL;
3515 }
3516 }
3517
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003518 } /* for each cookie */
3519
3520
3521 /* There are no more cookies on this line.
3522 * We may still have one (or several) marked for deletion at the
3523 * end of the line. We must do this now in two ways :
3524 * - if some cookies must be preserved, we only delete from the
3525 * mark to the end of line ;
3526 * - if nothing needs to be preserved, simply delete the whole header
3527 */
3528 if (del_from) {
3529 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3530 }
3531 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003532 if (hdr_beg != hdr_end)
3533 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003534 else
3535 http_remove_header(htx, &ctx);
3536 }
3537 } /* for each "Cookie header */
3538}
3539
3540/*
3541 * Manage server-side cookies. It can impact performance by about 2% so it is
3542 * desirable to call it only when needed. This function is also used when we
3543 * just need to know if there is a cookie (eg: for check-cache).
3544 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003545static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003546{
3547 struct session *sess = s->sess;
3548 struct http_txn *txn = s->txn;
3549 struct htx *htx;
3550 struct http_hdr_ctx ctx;
3551 struct server *srv;
3552 char *hdr_beg, *hdr_end;
3553 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003554 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003555
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003556 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003557
3558 ctx.blk = NULL;
3559 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003560 int is_first = 1;
3561
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003562 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3563 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3564 break;
3565 is_cookie2 = 1;
3566 }
3567
3568 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3569 * <prev> points to the colon.
3570 */
3571 txn->flags |= TX_SCK_PRESENT;
3572
3573 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3574 * check-cache is enabled) and we are not interested in checking
3575 * them. Warning, the cookie capture is declared in the frontend.
3576 */
3577 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3578 break;
3579
3580 /* OK so now we know we have to process this response cookie.
3581 * The format of the Set-Cookie header is slightly different
3582 * from the format of the Cookie header in that it does not
3583 * support the comma as a cookie delimiter (thus the header
3584 * cannot be folded) because the Expires attribute described in
3585 * the original Netscape's spec may contain an unquoted date
3586 * with a comma inside. We have to live with this because
3587 * many browsers don't support Max-Age and some browsers don't
3588 * support quoted strings. However the Set-Cookie2 header is
3589 * clean.
3590 *
3591 * We have to keep multiple pointers in order to support cookie
3592 * removal at the beginning, middle or end of header without
3593 * corrupting the header (in case of set-cookie2). A special
3594 * pointer, <scav> points to the beginning of the set-cookie-av
3595 * fields after the first semi-colon. The <next> pointer points
3596 * either to the end of line (set-cookie) or next unquoted comma
3597 * (set-cookie2). All of these headers are valid :
3598 *
3599 * hdr_beg hdr_end
3600 * | |
3601 * v |
3602 * NAME1 = VALUE 1 ; Secure; Path="/" |
3603 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3604 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3605 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3606 * | | | | | | | |
3607 * | | | | | | | +-> next
3608 * | | | | | | +------------> scav
3609 * | | | | | +--------------> val_end
3610 * | | | | +--------------------> val_beg
3611 * | | | +----------------------> equal
3612 * | | +------------------------> att_end
3613 * | +----------------------------> att_beg
3614 * +------------------------------> prev
3615 * -------------------------------> hdr_beg
3616 */
3617 hdr_beg = ctx.value.ptr;
3618 hdr_end = hdr_beg + ctx.value.len;
3619 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3620
3621 /* Iterate through all cookies on this line */
3622
3623 /* find att_beg */
3624 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003625 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003626 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003627 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003628
3629 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3630 att_beg++;
3631
3632 /* find att_end : this is the first character after the last non
3633 * space before the equal. It may be equal to hdr_end.
3634 */
3635 equal = att_end = att_beg;
3636
3637 while (equal < hdr_end) {
3638 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3639 break;
3640 if (HTTP_IS_SPHT(*equal++))
3641 continue;
3642 att_end = equal;
3643 }
3644
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003645 /* here, <equal> points to '=', a delimiter or the end. <att_end>
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003646 * is between <att_beg> and <equal>, both may be identical.
3647 */
3648
3649 /* look for end of cookie if there is an equal sign */
3650 if (equal < hdr_end && *equal == '=') {
3651 /* look for the beginning of the value */
3652 val_beg = equal + 1;
3653 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3654 val_beg++;
3655
3656 /* find the end of the value, respecting quotes */
3657 next = http_find_cookie_value_end(val_beg, hdr_end);
3658
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003659 /* make val_end point to the first white space or delimiter after the value */
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003660 val_end = next;
3661 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3662 val_end--;
3663 }
3664 else {
3665 /* <equal> points to next comma, semi-colon or EOL */
3666 val_beg = val_end = next = equal;
3667 }
3668
3669 if (next < hdr_end) {
3670 /* Set-Cookie2 supports multiple cookies, and <next> points to
3671 * a colon or semi-colon before the end. So skip all attr-value
3672 * pairs and look for the next comma. For Set-Cookie, since
3673 * commas are permitted in values, skip to the end.
3674 */
3675 if (is_cookie2)
3676 next = http_find_hdr_value_end(next, hdr_end);
3677 else
3678 next = hdr_end;
3679 }
3680
3681 /* Now everything is as on the diagram above */
3682
3683 /* Ignore cookies with no equal sign */
3684 if (equal == val_end)
3685 continue;
3686
3687 /* If there are spaces around the equal sign, we need to
3688 * strip them otherwise we'll get trouble for cookie captures,
3689 * or even for rewrites. Since this happens extremely rarely,
3690 * it does not hurt performance.
3691 */
3692 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3693 int stripped_before = 0;
3694 int stripped_after = 0;
3695
3696 if (att_end != equal) {
3697 memmove(att_end, equal, hdr_end - equal);
3698 stripped_before = (att_end - equal);
3699 equal += stripped_before;
3700 val_beg += stripped_before;
3701 }
3702
3703 if (val_beg > equal + 1) {
3704 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3705 stripped_after = (equal + 1) - val_beg;
3706 val_beg += stripped_after;
3707 stripped_before += stripped_after;
3708 }
3709
3710 val_end += stripped_before;
3711 next += stripped_before;
3712 hdr_end += stripped_before;
3713
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003714 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003715 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003716 }
3717
3718 /* First, let's see if we want to capture this cookie. We check
3719 * that we don't already have a server side cookie, because we
3720 * can only capture one. Also as an optimisation, we ignore
3721 * cookies shorter than the declared name.
3722 */
3723 if (sess->fe->capture_name != NULL &&
3724 txn->srv_cookie == NULL &&
3725 (val_end - att_beg >= sess->fe->capture_namelen) &&
3726 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3727 int log_len = val_end - att_beg;
3728 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3729 ha_alert("HTTP logging : out of memory.\n");
3730 }
3731 else {
3732 if (log_len > sess->fe->capture_len)
3733 log_len = sess->fe->capture_len;
3734 memcpy(txn->srv_cookie, att_beg, log_len);
3735 txn->srv_cookie[log_len] = 0;
3736 }
3737 }
3738
3739 srv = objt_server(s->target);
3740 /* now check if we need to process it for persistence */
3741 if (!(s->flags & SF_IGNORE_PRST) &&
3742 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3743 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3744 /* assume passive cookie by default */
3745 txn->flags &= ~TX_SCK_MASK;
3746 txn->flags |= TX_SCK_FOUND;
3747
3748 /* If the cookie is in insert mode on a known server, we'll delete
3749 * this occurrence because we'll insert another one later.
3750 * We'll delete it too if the "indirect" option is set and we're in
3751 * a direct access.
3752 */
3753 if (s->be->ck_opts & PR_CK_PSV) {
3754 /* The "preserve" flag was set, we don't want to touch the
3755 * server's cookie.
3756 */
3757 }
3758 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3759 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3760 /* this cookie must be deleted */
3761 if (prev == hdr_beg && next == hdr_end) {
3762 /* whole header */
3763 http_remove_header(htx, &ctx);
3764 /* note: while both invalid now, <next> and <hdr_end>
3765 * are still equal, so the for() will stop as expected.
3766 */
3767 } else {
3768 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003769 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003770 next = prev;
3771 hdr_end += delta;
3772 }
3773 txn->flags &= ~TX_SCK_MASK;
3774 txn->flags |= TX_SCK_DELETED;
3775 /* and go on with next cookie */
3776 }
3777 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3778 /* replace bytes val_beg->val_end with the cookie name associated
3779 * with this server since we know it.
3780 */
3781 int sliding, delta;
3782
3783 ctx.value = ist2(val_beg, val_end - val_beg);
3784 ctx.lws_before = ctx.lws_after = 0;
3785 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3786 delta = srv->cklen - (val_end - val_beg);
3787 sliding = (ctx.value.ptr - val_beg);
3788 hdr_beg += sliding;
3789 val_beg += sliding;
3790 next += sliding + delta;
3791 hdr_end += sliding + delta;
3792
3793 txn->flags &= ~TX_SCK_MASK;
3794 txn->flags |= TX_SCK_REPLACED;
3795 }
3796 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3797 /* insert the cookie name associated with this server
3798 * before existing cookie, and insert a delimiter between them..
3799 */
3800 int sliding, delta;
3801 ctx.value = ist2(val_beg, 0);
3802 ctx.lws_before = ctx.lws_after = 0;
3803 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3804 delta = srv->cklen + 1;
3805 sliding = (ctx.value.ptr - val_beg);
3806 hdr_beg += sliding;
3807 val_beg += sliding;
3808 next += sliding + delta;
3809 hdr_end += sliding + delta;
3810
3811 val_beg[srv->cklen] = COOKIE_DELIM;
3812 txn->flags &= ~TX_SCK_MASK;
3813 txn->flags |= TX_SCK_REPLACED;
3814 }
3815 }
3816 /* that's done for this cookie, check the next one on the same
3817 * line when next != hdr_end (only if is_cookie2).
3818 */
3819 }
3820 }
3821}
3822
Christopher Faulet25a02f62018-10-24 12:00:25 +02003823/*
3824 * Parses the Cache-Control and Pragma request header fields to determine if
3825 * the request may be served from the cache and/or if it is cacheable. Updates
3826 * s->txn->flags.
3827 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003828void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003829{
3830 struct http_txn *txn = s->txn;
3831 struct htx *htx;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003832 struct http_hdr_ctx ctx = { .blk = NULL };
3833 int pragma_found, cc_found;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003834
3835 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3836 return; /* nothing more to do here */
3837
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003838 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003839 pragma_found = cc_found = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003840
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003841 /* Check "pragma" header for HTTP/1.0 compatibility. */
3842 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3843 if (isteqi(ctx.value, ist("no-cache"))) {
3844 pragma_found = 1;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003845 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003846 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003847
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003848 ctx.blk = NULL;
3849 /* Don't use the cache and don't try to store if we found the
3850 * Authorization header */
3851 if (http_find_header(htx, ist("authorization"), &ctx, 1)) {
3852 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3853 txn->flags |= TX_CACHE_IGNORE;
3854 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003855
Christopher Faulet25a02f62018-10-24 12:00:25 +02003856
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003857 /* Look for "cache-control" header and iterate over all the values
3858 * until we find one that specifies that caching is possible or not. */
3859 ctx.blk = NULL;
3860 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003861 cc_found = 1;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003862 /* We don't check the values after max-age, max-stale nor min-fresh,
3863 * we simply don't use the cache when they're specified. */
3864 if (istmatchi(ctx.value, ist("max-age")) ||
3865 istmatchi(ctx.value, ist("no-cache")) ||
3866 istmatchi(ctx.value, ist("max-stale")) ||
3867 istmatchi(ctx.value, ist("min-fresh"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003868 txn->flags |= TX_CACHE_IGNORE;
3869 continue;
3870 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003871 if (istmatchi(ctx.value, ist("no-store"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003872 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3873 continue;
3874 }
3875 }
3876
3877 /* RFC7234#5.4:
3878 * When the Cache-Control header field is also present and
3879 * understood in a request, Pragma is ignored.
3880 * When the Cache-Control header field is not present in a
3881 * request, caches MUST consider the no-cache request
3882 * pragma-directive as having the same effect as if
3883 * "Cache-Control: no-cache" were present.
3884 */
3885 if (!cc_found && pragma_found)
3886 txn->flags |= TX_CACHE_IGNORE;
3887}
3888
3889/*
3890 * Check if response is cacheable or not. Updates s->txn->flags.
3891 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003892void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003893{
3894 struct http_txn *txn = s->txn;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003895 struct http_hdr_ctx ctx = { .blk = NULL };
Christopher Faulet25a02f62018-10-24 12:00:25 +02003896 struct htx *htx;
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003897 int has_freshness_info = 0;
3898 int has_validator = 0;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003899
3900 if (txn->status < 200) {
3901 /* do not try to cache interim responses! */
3902 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3903 return;
3904 }
3905
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003906 htx = htxbuf(&res->buf);
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003907 /* Check "pragma" header for HTTP/1.0 compatibility. */
3908 if (http_find_header(htx, ist("pragma"), &ctx, 1)) {
3909 if (isteqi(ctx.value, ist("no-cache"))) {
3910 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3911 return;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003912 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003913 }
Christopher Faulet25a02f62018-10-24 12:00:25 +02003914
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003915 /* Look for "cache-control" header and iterate over all the values
3916 * until we find one that specifies that caching is possible or not. */
3917 ctx.blk = NULL;
3918 while (http_find_header(htx, ist("cache-control"), &ctx, 0)) {
3919 if (isteqi(ctx.value, ist("public"))) {
3920 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003921 continue;
3922 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003923 if (isteqi(ctx.value, ist("private")) ||
3924 isteqi(ctx.value, ist("no-cache")) ||
3925 isteqi(ctx.value, ist("no-store")) ||
3926 isteqi(ctx.value, ist("max-age=0")) ||
3927 isteqi(ctx.value, ist("s-maxage=0"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003928 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003929 continue;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003930 }
Remi Tricot-Le Breton40ed97b2020-10-28 11:35:15 +01003931 /* We might have a no-cache="set-cookie" form. */
3932 if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) {
3933 txn->flags &= ~TX_CACHE_COOK;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003934 continue;
3935 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003936
3937 if (istmatchi(ctx.value, ist("s-maxage")) ||
3938 istmatchi(ctx.value, ist("max-age"))) {
3939 has_freshness_info = 1;
3940 continue;
3941 }
3942 }
3943
3944 /* If no freshness information could be found in Cache-Control values,
3945 * look for an Expires header. */
3946 if (!has_freshness_info) {
3947 ctx.blk = NULL;
3948 has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003949 }
Remi Tricot-Le Bretoncc9bf2e2020-11-12 11:14:41 +01003950
3951 /* If no freshness information could be found in Cache-Control or Expires
3952 * values, look for an explicit validator. */
3953 if (!has_freshness_info) {
3954 ctx.blk = NULL;
3955 has_validator = 1;
3956 if (!http_find_header(htx, ist("etag"), &ctx, 0)) {
3957 ctx.blk = NULL;
3958 if (!http_find_header(htx, ist("last-modified"), &ctx, 0))
3959 has_validator = 0;
3960 }
3961 }
3962
3963 /* We won't store an entry that has neither a cache validator nor an
3964 * explicit expiration time, as suggested in RFC 7234#3. */
3965 if (!has_freshness_info && !has_validator)
3966 txn->flags |= TX_CACHE_IGNORE;
Christopher Faulet25a02f62018-10-24 12:00:25 +02003967}
3968
Christopher Faulet377c5a52018-10-24 21:21:30 +02003969/*
3970 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3971 * for the current backend.
3972 *
3973 * It is assumed that the request is either a HEAD, GET, or POST and that the
3974 * uri_auth field is valid.
3975 *
3976 * Returns 1 if stats should be provided, otherwise 0.
3977 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003978static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003979{
3980 struct uri_auth *uri_auth = backend->uri_auth;
3981 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003982 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003983 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003984
3985 if (!uri_auth)
3986 return 0;
3987
3988 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3989 return 0;
3990
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003991 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003992 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003993 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01003994 if (*uri_auth->uri_prefix == '/')
3995 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02003996
3997 /* check URI size */
3998 if (uri_auth->uri_len > uri.len)
3999 return 0;
4000
4001 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4002 return 0;
4003
4004 return 1;
4005}
4006
4007/* This function prepares an applet to handle the stats. It can deal with the
4008 * "100-continue" expectation, check that admin rules are met for POST requests,
4009 * and program a response message if something was unexpected. It cannot fail
4010 * and always relies on the stats applet to complete the job. It does not touch
4011 * analysers nor counters, which are left to the caller. It does not touch
4012 * s->target which is supposed to already point to the stats applet. The caller
4013 * is expected to have already assigned an appctx to the stream.
4014 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004015static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004016{
4017 struct stats_admin_rule *stats_admin_rule;
4018 struct stream_interface *si = &s->si[1];
4019 struct session *sess = s->sess;
4020 struct http_txn *txn = s->txn;
4021 struct http_msg *msg = &txn->req;
4022 struct uri_auth *uri_auth = s->be->uri_auth;
4023 const char *h, *lookup, *end;
4024 struct appctx *appctx;
4025 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004026 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004027
4028 appctx = si_appctx(si);
4029 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4030 appctx->st1 = appctx->st2 = 0;
4031 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004032 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004033 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4034 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4035 appctx->ctx.stats.flags |= STAT_CHUNKED;
4036
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004037 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004038 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004039 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4040 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004041
4042 for (h = lookup; h <= end - 3; h++) {
4043 if (memcmp(h, ";up", 3) == 0) {
4044 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4045 break;
4046 }
Willy Tarreau3e320362020-10-23 17:28:57 +02004047 if (memcmp(h, ";no-maint", 3) == 0) {
4048 appctx->ctx.stats.flags |= STAT_HIDE_MAINT;
4049 break;
4050 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004051 }
4052
4053 if (uri_auth->refresh) {
4054 for (h = lookup; h <= end - 10; h++) {
4055 if (memcmp(h, ";norefresh", 10) == 0) {
4056 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4057 break;
4058 }
4059 }
4060 }
4061
4062 for (h = lookup; h <= end - 4; h++) {
4063 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004064 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004065 break;
4066 }
4067 }
4068
4069 for (h = lookup; h <= end - 6; h++) {
4070 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004071 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004072 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4073 break;
4074 }
4075 }
4076
Christopher Faulet6338a082019-09-09 15:50:54 +02004077 for (h = lookup; h <= end - 5; h++) {
4078 if (memcmp(h, ";json", 5) == 0) {
4079 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4080 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4081 break;
4082 }
4083 }
4084
4085 for (h = lookup; h <= end - 12; h++) {
4086 if (memcmp(h, ";json-schema", 12) == 0) {
4087 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4088 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4089 break;
4090 }
4091 }
4092
Christopher Faulet377c5a52018-10-24 21:21:30 +02004093 for (h = lookup; h <= end - 8; h++) {
4094 if (memcmp(h, ";st=", 4) == 0) {
4095 int i;
4096 h += 4;
4097 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4098 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4099 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4100 appctx->ctx.stats.st_code = i;
4101 break;
4102 }
4103 }
4104 break;
4105 }
4106 }
4107
4108 appctx->ctx.stats.scope_str = 0;
4109 appctx->ctx.stats.scope_len = 0;
4110 for (h = lookup; h <= end - 8; h++) {
4111 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4112 int itx = 0;
4113 const char *h2;
4114 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4115 const char *err;
4116
4117 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4118 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004119 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4120 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004121 if (*h == ';' || *h == '&' || *h == ' ')
4122 break;
4123 itx++;
4124 h++;
4125 }
4126
4127 if (itx > STAT_SCOPE_TXT_MAXLEN)
4128 itx = STAT_SCOPE_TXT_MAXLEN;
4129 appctx->ctx.stats.scope_len = itx;
4130
4131 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4132 memcpy(scope_txt, h2, itx);
4133 scope_txt[itx] = '\0';
4134 err = invalid_char(scope_txt);
4135 if (err) {
4136 /* bad char in search text => clear scope */
4137 appctx->ctx.stats.scope_str = 0;
4138 appctx->ctx.stats.scope_len = 0;
4139 }
4140 break;
4141 }
4142 }
4143
4144 /* now check whether we have some admin rules for this request */
4145 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4146 int ret = 1;
4147
4148 if (stats_admin_rule->cond) {
4149 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4150 ret = acl_pass(ret);
4151 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4152 ret = !ret;
4153 }
4154
4155 if (ret) {
4156 /* no rule, or the rule matches */
4157 appctx->ctx.stats.flags |= STAT_ADMIN;
4158 break;
4159 }
4160 }
4161
Christopher Faulet5d45e382019-02-27 15:15:23 +01004162 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4163 appctx->st0 = STAT_HTTP_HEAD;
4164 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004165 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004166 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004167 if (msg->msg_state < HTTP_MSG_DATA)
4168 req->analysers |= AN_REQ_HTTP_BODY;
4169 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004170 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004171 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004172 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4173 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4174 appctx->st0 = STAT_HTTP_LAST;
4175 }
4176 }
4177 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004178 /* Unsupported method */
4179 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4180 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4181 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004182 }
4183
4184 s->task->nice = -32; /* small boost for HTTP statistics */
4185 return 1;
4186}
4187
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004188void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004189{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004190 struct channel *req = &s->req;
4191 struct channel *res = &s->res;
4192 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004193 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004194 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004195 struct ist path, location;
4196 unsigned int flags;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004197
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004198 /*
4199 * Create the location
4200 */
4201 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004202
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004203 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004204 /* special prefix "/" means don't change URL */
4205 srv = __objt_server(s->target);
4206 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4207 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4208 return;
4209 }
4210
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004211 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004212 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004213 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004214 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004215 if (!isttest(path))
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004216 return;
4217
4218 if (!chunk_memcat(&trash, path.ptr, path.len))
4219 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004220 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004221
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004222 /*
4223 * Create the 302 respone
4224 */
4225 htx = htx_from_buf(&res->buf);
4226 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4227 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4228 ist("HTTP/1.1"), ist("302"), ist("Found"));
4229 if (!sl)
4230 goto fail;
4231 sl->info.res.status = 302;
4232 s->txn->status = 302;
4233
4234 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4235 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4236 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4237 !htx_add_header(htx, ist("Location"), location))
4238 goto fail;
4239
4240 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4241 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004242
Christopher Fauletc20afb82020-01-24 19:16:26 +01004243 htx_to_buf(htx, &res->buf);
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004244 if (!http_forward_proxy_resp(s, 1))
4245 goto fail;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004246
4247 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004248 si_shutr(si);
4249 si_shutw(si);
4250 si->err_type = SI_ET_NONE;
4251 si->state = SI_ST_CLO;
4252
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004253 if (!(s->flags & SF_ERR_MASK))
4254 s->flags |= SF_ERR_LOCAL;
4255 if (!(s->flags & SF_FINST_MASK))
4256 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004257
4258 /* FIXME: we should increase a counter of redirects per server and per backend. */
4259 srv_inc_sess_ctr(srv);
4260 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004261 return;
4262
4263 fail:
4264 /* If an error occurred, remove the incomplete HTTP response from the
4265 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004266 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004267}
4268
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004269/* This function terminates the request because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004270 * because an error was triggered during the body forwarding.
4271 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004272static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004273{
4274 struct channel *chn = &s->req;
4275 struct http_txn *txn = s->txn;
4276
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004277 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004278
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004279 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4280 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004281 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004282 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004283 goto end;
4284 }
4285
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004286 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4287 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004288 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004289 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004290
4291 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004292 /* No need to read anymore, the request was completely parsed.
4293 * We can shut the read side unless we want to abort_on_close,
4294 * or we have a POST request. The issue with POST requests is
4295 * that some browsers still send a CRLF after the request, and
4296 * this CRLF must be read so that it does not remain in the kernel
4297 * buffers, otherwise a close could cause an RST on some systems
4298 * (eg: Linux).
4299 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004300 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004301 channel_dont_read(chn);
4302
4303 /* if the server closes the connection, we want to immediately react
4304 * and close the socket to save packets and syscalls.
4305 */
4306 s->si[1].flags |= SI_FL_NOHALF;
4307
4308 /* In any case we've finished parsing the request so we must
4309 * disable Nagle when sending data because 1) we're not going
4310 * to shut this side, and 2) the server is waiting for us to
4311 * send pending data.
4312 */
4313 chn->flags |= CF_NEVER_WAIT;
4314
Christopher Fauletd01ce402019-01-02 17:44:13 +01004315 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4316 /* The server has not finished to respond, so we
4317 * don't want to move in order not to upset it.
4318 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004319 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004320 return;
4321 }
4322
Christopher Fauletf2824e62018-10-01 12:12:37 +02004323 /* When we get here, it means that both the request and the
4324 * response have finished receiving. Depending on the connection
4325 * mode, we'll have to wait for the last bytes to leave in either
4326 * direction, and sometimes for a close to be effective.
4327 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004328 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004329 /* Tunnel mode will not have any analyser so it needs to
4330 * poll for reads.
4331 */
4332 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004333 if (b_data(&chn->buf)) {
4334 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004335 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004336 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004337 txn->req.msg_state = HTTP_MSG_TUNNEL;
4338 }
4339 else {
4340 /* we're not expecting any new data to come for this
4341 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004342 *
4343 * However, there is an exception if the response
4344 * length is undefined. In this case, we need to wait
4345 * the close from the server. The response will be
4346 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004347 */
4348 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4349 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004350 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004351
4352 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4353 channel_shutr_now(chn);
4354 channel_shutw_now(chn);
4355 }
4356 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004357 goto check_channel_flags;
4358 }
4359
4360 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4361 http_msg_closing:
4362 /* nothing else to forward, just waiting for the output buffer
4363 * to be empty and for the shutw_now to take effect.
4364 */
4365 if (channel_is_empty(chn)) {
4366 txn->req.msg_state = HTTP_MSG_CLOSED;
4367 goto http_msg_closed;
4368 }
4369 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004370 txn->req.msg_state = HTTP_MSG_ERROR;
4371 goto end;
4372 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004373 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004374 return;
4375 }
4376
4377 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4378 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004379 /* if we don't know whether the server will close, we need to hard close */
4380 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4381 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004382 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004383 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004384 channel_dont_read(chn);
4385 goto end;
4386 }
4387
4388 check_channel_flags:
4389 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4390 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4391 /* if we've just closed an output, let's switch */
4392 txn->req.msg_state = HTTP_MSG_CLOSING;
4393 goto http_msg_closing;
4394 }
4395
4396 end:
4397 chn->analysers &= AN_REQ_FLT_END;
4398 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4399 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4400 channel_auto_close(chn);
4401 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004402 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004403}
4404
4405
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05004406/* This function terminates the response because it was completely analyzed or
Christopher Fauletf2824e62018-10-01 12:12:37 +02004407 * because an error was triggered during the body forwarding.
4408 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004409static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004410{
4411 struct channel *chn = &s->res;
4412 struct http_txn *txn = s->txn;
4413
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004414 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004415
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004416 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4417 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004418 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004419 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004420 goto end;
4421 }
4422
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004423 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4424 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004425 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004426 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004427
4428 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4429 /* In theory, we don't need to read anymore, but we must
4430 * still monitor the server connection for a possible close
4431 * while the request is being uploaded, so we don't disable
4432 * reading.
4433 */
4434 /* channel_dont_read(chn); */
4435
4436 if (txn->req.msg_state < HTTP_MSG_DONE) {
4437 /* The client seems to still be sending data, probably
4438 * because we got an error response during an upload.
4439 * We have the choice of either breaking the connection
4440 * or letting it pass through. Let's do the later.
4441 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004442 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004443 return;
4444 }
4445
4446 /* When we get here, it means that both the request and the
4447 * response have finished receiving. Depending on the connection
4448 * mode, we'll have to wait for the last bytes to leave in either
4449 * direction, and sometimes for a close to be effective.
4450 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004451 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004452 channel_auto_read(chn);
4453 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004454 if (b_data(&chn->buf)) {
4455 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004456 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004457 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004458 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4459 }
4460 else {
4461 /* we're not expecting any new data to come for this
4462 * transaction, so we can close it.
4463 */
4464 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4465 channel_shutr_now(chn);
4466 channel_shutw_now(chn);
4467 }
4468 }
4469 goto check_channel_flags;
4470 }
4471
4472 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4473 http_msg_closing:
4474 /* nothing else to forward, just waiting for the output buffer
4475 * to be empty and for the shutw_now to take effect.
4476 */
4477 if (channel_is_empty(chn)) {
4478 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4479 goto http_msg_closed;
4480 }
4481 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004482 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004483 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004484 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004485 if (strm_sess(s)->listener->counters)
4486 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004487 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004488 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004489 goto end;
4490 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004491 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004492 return;
4493 }
4494
4495 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4496 http_msg_closed:
4497 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004498 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004499 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004500 goto end;
4501 }
4502
4503 check_channel_flags:
4504 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4505 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4506 /* if we've just closed an output, let's switch */
4507 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4508 goto http_msg_closing;
4509 }
4510
4511 end:
4512 chn->analysers &= AN_RES_FLT_END;
4513 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4514 chn->analysers |= AN_RES_FLT_XFER_DATA;
4515 channel_auto_close(chn);
4516 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004517 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004518}
4519
Christopher Fauletef70e252020-01-28 09:26:19 +01004520/* Forward a response generated by HAProxy (error/redirect/return). This
4521 * function forwards all pending incoming data. If <final> is set to 0, nothing
4522 * more is performed. It is used for 1xx informational messages. Otherwise, the
Christopher Faulet507479b2020-05-15 12:29:46 +02004523 * transaction is terminated and the request is emptied. On success 1 is
Christopher Faulet40e6b552020-06-25 16:04:50 +02004524 * returned. If an error occurred, 0 is returned. If it fails, this function
4525 * only exits. It is the caller responsibility to do the cleanup.
Christopher Fauletef70e252020-01-28 09:26:19 +01004526 */
4527int http_forward_proxy_resp(struct stream *s, int final)
4528{
4529 struct channel *req = &s->req;
4530 struct channel *res = &s->res;
4531 struct htx *htx = htxbuf(&res->buf);
4532 size_t data;
4533
4534 if (final) {
4535 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet507479b2020-05-15 12:29:46 +02004536
Christopher Fauletaab1b672020-11-18 16:44:02 +01004537 if (!htx_is_empty(htx) && !http_eval_after_res_rules(s))
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01004538 return 0;
Christopher Fauletef70e252020-01-28 09:26:19 +01004539
Christopher Fauletd6c48362020-10-19 18:01:38 +02004540 if (s->txn->meth == HTTP_METH_HEAD)
4541 htx_skip_msg_payload(htx);
4542
Christopher Fauletef70e252020-01-28 09:26:19 +01004543 channel_auto_read(req);
4544 channel_abort(req);
4545 channel_auto_close(req);
4546 channel_htx_erase(req, htxbuf(&req->buf));
4547
4548 res->wex = tick_add_ifset(now_ms, res->wto);
4549 channel_auto_read(res);
4550 channel_auto_close(res);
4551 channel_shutr_now(res);
Christopher Faulet1a9db7c2020-06-25 15:36:45 +02004552 res->flags |= CF_EOI; /* The response is terminated, add EOI */
Christopher Faulet810df062020-07-22 16:20:34 +02004553 htxbuf(&res->buf)->flags |= HTX_FL_EOI; /* no more data are expected */
Christopher Fauletef70e252020-01-28 09:26:19 +01004554 }
Christopher Fauletcf6898c2020-06-25 15:55:11 +02004555 else {
4556 /* Send ASAP informational messages. Rely on CF_EOI for final
4557 * response.
4558 */
4559 res->flags |= CF_SEND_DONTWAIT;
4560 }
Christopher Fauletef70e252020-01-28 09:26:19 +01004561
4562 data = htx->data - co_data(res);
4563 c_adv(res, data);
4564 htx->first = -1;
4565 res->total += data;
4566 return 1;
4567}
4568
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004569void http_server_error(struct stream *s, struct stream_interface *si, int err,
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004570 int finst, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004571{
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004572 http_reply_and_close(s, s->txn->status, msg);
Christopher Faulet0f226952018-10-22 09:29:56 +02004573 if (!(s->flags & SF_ERR_MASK))
4574 s->flags |= err;
4575 if (!(s->flags & SF_FINST_MASK))
4576 s->flags |= finst;
4577}
4578
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004579void http_reply_and_close(struct stream *s, short status, struct http_reply *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004580{
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004581 if (!msg) {
4582 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
4583 goto end;
4584 }
4585
4586 if (http_reply_message(s, msg) == -1) {
4587 /* On error, return a 500 error message, but don't rewrite it if
Christopher Faulet40e6b552020-06-25 16:04:50 +02004588 * it is already an internal error. If it was already a "const"
4589 * 500 error, just fail.
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004590 */
Christopher Faulet40e6b552020-06-25 16:04:50 +02004591 if (s->txn->status == 500) {
4592 if (s->txn->flags & TX_CONST_REPLY)
4593 goto end;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004594 s->txn->flags |= TX_CONST_REPLY;
Christopher Faulet40e6b552020-06-25 16:04:50 +02004595 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004596 s->txn->status = 500;
4597 s->txn->http_reply = NULL;
4598 return http_reply_and_close(s, s->txn->status, http_error_message(s));
4599 }
4600
4601end:
4602 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
4603 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
4604
Christopher Faulet0f226952018-10-22 09:29:56 +02004605 channel_auto_read(&s->req);
4606 channel_abort(&s->req);
4607 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004608 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet72c7d8d2020-01-27 15:32:25 +01004609 channel_auto_read(&s->res);
4610 channel_auto_close(&s->res);
4611 channel_shutr_now(&s->res);
Christopher Faulet0f226952018-10-22 09:29:56 +02004612}
4613
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004614struct http_reply *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004615{
4616 const int msgnum = http_get_status_idx(s->txn->status);
4617
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004618 if (s->txn->http_reply)
4619 return s->txn->http_reply;
4620 else if (s->be->replies[msgnum])
4621 return s->be->replies[msgnum];
4622 else if (strm_fe(s)->replies[msgnum])
4623 return strm_fe(s)->replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004624 else
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004625 return &http_err_replies[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004626}
4627
Christopher Faulet40e6b552020-06-25 16:04:50 +02004628/* Produces an HTX message from an http reply. Depending on the http reply type,
4629 * a, errorfile, an raw file or a log-format string is used. On success, it
4630 * returns 0. If an error occurs -1 is returned. If it fails, this function only
4631 * exits. It is the caller responsibility to do the cleanup.
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004632 */
Christopher Fauletae43b6c2020-05-27 15:24:22 +02004633int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004634{
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004635 struct buffer *errmsg;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004636 struct htx_sl *sl;
4637 struct buffer *body = NULL;
4638 const char *status, *reason, *clen, *ctype;
4639 unsigned int slflags;
4640 int ret = 0;
4641
Christopher Faulete29a97e2020-05-14 14:49:25 +02004642 /*
4643 * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
4644 *
4645 * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled
4646 * as no payload if NULL. the TXN status code is set with the status
4647 * of the original reply.
4648 */
4649
4650 if (reply->type == HTTP_REPLY_INDIRECT) {
4651 if (reply->body.reply)
4652 reply = reply->body.reply;
4653 }
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004654 if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) {
4655 /* get default error message */
4656 if (reply == s->txn->http_reply)
4657 s->txn->http_reply = NULL;
4658 reply = http_error_message(s);
4659 if (reply->type == HTTP_REPLY_INDIRECT) {
4660 if (reply->body.reply)
4661 reply = reply->body.reply;
4662 }
4663 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004664
4665 if (reply->type == HTTP_REPLY_ERRMSG) {
4666 /* implicit or explicit error message*/
4667 errmsg = reply->body.errmsg;
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004668 if (errmsg && !b_is_null(errmsg)) {
Christopher Faulet20567362020-05-15 14:52:49 +02004669 if (!htx_copy_msg(htx, errmsg))
Christopher Faulet8dfeccf2020-05-15 14:16:29 +02004670 goto fail;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004671 }
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004672 }
4673 else {
4674 /* no payload, file or log-format string */
4675 if (reply->type == HTTP_REPLY_RAW) {
4676 /* file */
4677 body = &reply->body.obj;
4678 }
4679 else if (reply->type == HTTP_REPLY_LOGFMT) {
4680 /* log-format string */
4681 body = alloc_trash_chunk();
4682 if (!body)
4683 goto fail_alloc;
4684 body->data = build_logline(s, body->area, body->size, &reply->body.fmt);
4685 }
4686 /* else no payload */
4687
4688 status = ultoa(reply->status);
4689 reason = http_get_reason(reply->status);
4690 slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN);
4691 if (!body || !b_data(body))
4692 slflags |= HTX_SL_F_BODYLESS;
4693 sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason));
4694 if (!sl)
4695 goto fail;
4696 sl->info.res.status = reply->status;
4697
4698 clen = (body ? ultoa(b_data(body)) : "0");
4699 ctype = reply->ctype;
4700
4701 if (!LIST_ISEMPTY(&reply->hdrs)) {
4702 struct http_reply_hdr *hdr;
4703 struct buffer *value = alloc_trash_chunk();
4704
4705 if (!value)
4706 goto fail;
4707
4708 list_for_each_entry(hdr, &reply->hdrs, list) {
4709 chunk_reset(value);
4710 value->data = build_logline(s, value->area, value->size, &hdr->value);
4711 if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) {
4712 free_trash_chunk(value);
4713 goto fail;
4714 }
4715 chunk_reset(value);
4716 }
4717 free_trash_chunk(value);
4718 }
4719
4720 if (!htx_add_header(htx, ist("content-length"), ist(clen)) ||
4721 (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) ||
4722 !htx_add_endof(htx, HTX_BLK_EOH) ||
4723 (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))) ||
4724 !htx_add_endof(htx, HTX_BLK_EOM))
4725 goto fail;
4726 }
4727
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004728 leave:
4729 if (reply->type == HTTP_REPLY_LOGFMT)
4730 free_trash_chunk(body);
4731 return ret;
4732
4733 fail_alloc:
4734 if (!(s->flags & SF_ERR_MASK))
4735 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004736 /* fall through */
4737 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004738 ret = -1;
4739 goto leave;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004740}
4741
4742/* Send an http reply to the client. On success, it returns 0. If an error
Christopher Faulet40e6b552020-06-25 16:04:50 +02004743 * occurs -1 is returned and the response channel is truncated, removing this
4744 * way the faulty reply. This function may fail when the reply is formatted
4745 * (http_reply_to_htx) or when the reply is forwarded
4746 * (http_forward_proxy_resp). On the last case, it is because a
4747 * http-after-response rule fails.
Christopher Faulet97e466c2020-05-15 15:12:47 +02004748 */
4749int http_reply_message(struct stream *s, struct http_reply *reply)
4750{
4751 struct channel *res = &s->res;
4752 struct htx *htx = htx_from_buf(&res->buf);
4753
4754 if (s->txn->status == -1)
4755 s->txn->status = reply->status;
4756 channel_htx_truncate(res, htx);
4757
4758 if (http_reply_to_htx(s, htx, reply) == -1)
4759 goto fail;
4760
4761 htx_to_buf(htx, &s->res.buf);
4762 if (!http_forward_proxy_resp(s, 1))
4763 goto fail;
4764 return 0;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004765
4766 fail:
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004767 channel_htx_truncate(res, htx);
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004768 if (!(s->flags & SF_ERR_MASK))
4769 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet97e466c2020-05-15 15:12:47 +02004770 return -1;
Christopher Faulet0e2ad612020-05-13 16:38:37 +02004771}
4772
Christopher Faulet304cc402019-07-15 15:46:28 +02004773/* Return the error message corresponding to si->err_type. It is assumed
4774 * that the server side is closed. Note that err_type is actually a
4775 * bitmask, where almost only aborts may be cumulated with other
4776 * values. We consider that aborted operations are more important
4777 * than timeouts or errors due to the fact that nobody else in the
4778 * logs might explain incomplete retries. All others should avoid
4779 * being cumulated. It should normally not be possible to have multiple
4780 * aborts at once, but just in case, the first one in sequence is reported.
4781 * Note that connection errors appearing on the second request of a keep-alive
4782 * connection are not reported since this allows the client to retry.
4783 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004784void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004785{
4786 int err_type = si->err_type;
4787
4788 /* set s->txn->status for http_error_message(s) */
4789 s->txn->status = 503;
4790
4791 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004792 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4793 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004794 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004795 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4796 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4797 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004798 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004799 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4800 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004801 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004802 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4803 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004804 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004805 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4806 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4807 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004808 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004809 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4810 (s->flags & SF_SRV_REUSED) ? NULL :
4811 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004812 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004813 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4814 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4815 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004816 else { /* SI_ET_CONN_OTHER and others */
4817 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004818 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4819 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004820 }
4821}
4822
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004823
Christopher Faulet4a28a532019-03-01 11:19:40 +01004824/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4825 * on success and -1 on error.
4826 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004827static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004828{
4829 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4830 * then we must send an HTTP/1.1 100 Continue intermediate response.
4831 */
4832 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4833 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4834 struct ist hdr = { .ptr = "Expect", .len = 6 };
4835 struct http_hdr_ctx ctx;
4836
4837 ctx.blk = NULL;
4838 /* Expect is allowed in 1.1, look for it */
4839 if (http_find_header(htx, hdr, &ctx, 0) &&
4840 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004841 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004842 return -1;
4843 http_remove_header(htx, &ctx);
4844 }
4845 }
4846 return 0;
4847}
4848
Christopher Faulet23a3c792018-11-28 10:01:23 +01004849/* Send a 100-Continue response to the client. It returns 0 on success and -1
4850 * on error. The response channel is updated accordingly.
4851 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004852static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004853{
4854 struct channel *res = &s->res;
4855 struct htx *htx = htx_from_buf(&res->buf);
4856 struct htx_sl *sl;
4857 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4858 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004859
4860 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4861 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4862 if (!sl)
4863 goto fail;
4864 sl->info.res.status = 100;
4865
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004866 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004867 goto fail;
4868
Christopher Fauleta72a7e42020-01-28 09:28:11 +01004869 if (!http_forward_proxy_resp(s, 0))
4870 goto fail;
Christopher Faulet23a3c792018-11-28 10:01:23 +01004871 return 0;
4872
4873 fail:
4874 /* If an error occurred, remove the incomplete HTTP response from the
4875 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004876 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004877 return -1;
4878}
4879
Christopher Faulet12c51e22018-11-28 15:59:42 +01004880
Christopher Faulet0f226952018-10-22 09:29:56 +02004881/*
4882 * Capture headers from message <htx> according to header list <cap_hdr>, and
4883 * fill the <cap> pointers appropriately.
4884 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004885static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004886{
4887 struct cap_hdr *h;
4888 int32_t pos;
4889
Christopher Fauleta3f15502019-05-13 15:27:23 +02004890 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004891 struct htx_blk *blk = htx_get_blk(htx, pos);
4892 enum htx_blk_type type = htx_get_blk_type(blk);
4893 struct ist n, v;
4894
4895 if (type == HTX_BLK_EOH)
4896 break;
4897 if (type != HTX_BLK_HDR)
4898 continue;
4899
4900 n = htx_get_blk_name(htx, blk);
4901
4902 for (h = cap_hdr; h; h = h->next) {
4903 if (h->namelen && (h->namelen == n.len) &&
4904 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4905 if (cap[h->index] == NULL)
4906 cap[h->index] =
4907 pool_alloc(h->pool);
4908
4909 if (cap[h->index] == NULL) {
4910 ha_alert("HTTP capture : out of memory.\n");
4911 break;
4912 }
4913
4914 v = htx_get_blk_value(htx, blk);
4915 if (v.len > h->len)
4916 v.len = h->len;
4917
4918 memcpy(cap[h->index], v.ptr, v.len);
4919 cap[h->index][v.len]=0;
4920 }
4921 }
4922 }
4923}
4924
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004925/* Delete a value in a header between delimiters <from> and <next>. The header
4926 * itself is delimited by <start> and <end> pointers. The number of characters
4927 * displaced is returned, and the pointer to the first delimiter is updated if
4928 * required. The function tries as much as possible to respect the following
4929 * principles :
4930 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4931 * in which case <next> is simply removed
4932 * - set exactly one space character after the new first delimiter, unless there
4933 * are not enough characters in the block being moved to do so.
4934 * - remove unneeded spaces before the previous delimiter and after the new
4935 * one.
4936 *
4937 * It is the caller's responsibility to ensure that :
4938 * - <from> points to a valid delimiter or <start> ;
4939 * - <next> points to a valid delimiter or <end> ;
4940 * - there are non-space chars before <from>.
4941 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004942static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004943{
4944 char *prev = *from;
4945
4946 if (prev == start) {
4947 /* We're removing the first value. eat the semicolon, if <next>
4948 * is lower than <end> */
4949 if (next < end)
4950 next++;
4951
4952 while (next < end && HTTP_IS_SPHT(*next))
4953 next++;
4954 }
4955 else {
4956 /* Remove useless spaces before the old delimiter. */
4957 while (HTTP_IS_SPHT(*(prev-1)))
4958 prev--;
4959 *from = prev;
4960
4961 /* copy the delimiter and if possible a space if we're
4962 * not at the end of the line.
4963 */
4964 if (next < end) {
4965 *prev++ = *next++;
4966 if (prev + 1 < next)
4967 *prev++ = ' ';
4968 while (next < end && HTTP_IS_SPHT(*next))
4969 next++;
4970 }
4971 }
4972 memmove(prev, next, end - next);
4973 return (prev - next);
4974}
4975
Christopher Faulet0f226952018-10-22 09:29:56 +02004976
4977/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004978 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004979 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004980static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004981{
4982 struct ist dst = ist2(str, 0);
4983
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004984 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004985 goto end;
4986 if (dst.len + 1 > len)
4987 goto end;
4988 dst.ptr[dst.len++] = ' ';
4989
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004990 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004991 goto end;
4992 if (dst.len + 1 > len)
4993 goto end;
4994 dst.ptr[dst.len++] = ' ';
4995
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004996 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004997 end:
4998 return dst.len;
4999}
5000
5001/*
5002 * Print a debug line with a start line.
5003 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005004static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02005005{
5006 struct session *sess = strm_sess(s);
5007 int max;
5008
5009 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5010 dir,
5011 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5012 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5013
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005014 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005015 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005016 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005017 trash.area[trash.data++] = ' ';
5018
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005019 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005020 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005021 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005022 trash.area[trash.data++] = ' ';
5023
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005024 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02005025 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01005026 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02005027 trash.area[trash.data++] = '\n';
5028
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005029 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005030}
5031
5032/*
5033 * Print a debug line with a header.
5034 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005035static 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 +02005036{
5037 struct session *sess = strm_sess(s);
5038 int max;
5039
5040 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
5041 dir,
5042 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
5043 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
5044
5045 max = n.len;
5046 UBOUND(max, trash.size - trash.data - 3);
5047 chunk_memcat(&trash, n.ptr, max);
5048 trash.area[trash.data++] = ':';
5049 trash.area[trash.data++] = ' ';
5050
5051 max = v.len;
5052 UBOUND(max, trash.size - trash.data - 1);
5053 chunk_memcat(&trash, v.ptr, max);
5054 trash.area[trash.data++] = '\n';
5055
Willy Tarreau2e8ab6b2020-03-14 11:03:20 +01005056 DISGUISE(write(1, trash.area, trash.data));
Christopher Faulet0f226952018-10-22 09:29:56 +02005057}
5058
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005059/* Allocate a new HTTP transaction for stream <s> unless there is one already.
5060 * In case of allocation failure, everything allocated is freed and NULL is
5061 * returned. Otherwise the new transaction is assigned to the stream and
5062 * returned.
5063 */
5064struct http_txn *http_alloc_txn(struct stream *s)
5065{
5066 struct http_txn *txn = s->txn;
5067
5068 if (txn)
5069 return txn;
5070
5071 txn = pool_alloc(pool_head_http_txn);
5072 if (!txn)
5073 return txn;
5074
5075 s->txn = txn;
5076 return txn;
5077}
5078
5079void http_txn_reset_req(struct http_txn *txn)
5080{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005081 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005082 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5083}
5084
5085void http_txn_reset_res(struct http_txn *txn)
5086{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005087 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005088 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5089}
5090
5091/*
5092 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5093 * the required fields are properly allocated and that we only need to (re)init
5094 * them. This should be used before processing any new request.
5095 */
5096void http_init_txn(struct stream *s)
5097{
5098 struct http_txn *txn = s->txn;
5099 struct conn_stream *cs = objt_cs(s->si[0].end);
5100
5101 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5102 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5103 : 0);
5104 txn->status = -1;
Christopher Faulet5cb513a2020-05-13 17:56:56 +02005105 txn->http_reply = NULL;
Willy Tarreau8b507582020-02-25 09:35:07 +01005106 write_u32(txn->cache_hash, 0);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005107
5108 txn->cookie_first_date = 0;
5109 txn->cookie_last_date = 0;
5110
5111 txn->srv_cookie = NULL;
5112 txn->cli_cookie = NULL;
5113 txn->uri = NULL;
5114
5115 http_txn_reset_req(txn);
5116 http_txn_reset_res(txn);
5117
5118 txn->req.chn = &s->req;
5119 txn->rsp.chn = &s->res;
5120
5121 txn->auth.method = HTTP_AUTH_UNKNOWN;
5122
5123 vars_init(&s->vars_txn, SCOPE_TXN);
5124 vars_init(&s->vars_reqres, SCOPE_REQ);
5125}
5126
5127/* to be used at the end of a transaction */
5128void http_end_txn(struct stream *s)
5129{
5130 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005131
5132 /* these ones will have been dynamically allocated */
5133 pool_free(pool_head_requri, txn->uri);
5134 pool_free(pool_head_capture, txn->cli_cookie);
5135 pool_free(pool_head_capture, txn->srv_cookie);
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005136 pool_free(pool_head_uniqueid, s->unique_id.ptr);
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005137
Tim Duesterhusa17e6622020-03-05 20:19:02 +01005138 s->unique_id = IST_NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005139 txn->uri = NULL;
5140 txn->srv_cookie = NULL;
5141 txn->cli_cookie = NULL;
5142
Christopher Faulet59399252019-11-07 14:27:52 +01005143 if (!LIST_ISEMPTY(&s->vars_txn.head))
5144 vars_prune(&s->vars_txn, s->sess, s);
5145 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5146 vars_prune(&s->vars_reqres, s->sess, s);
5147}
5148
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005149
5150DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
Christopher Faulet0f226952018-10-22 09:29:56 +02005151
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005152__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005153static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005154{
5155}
5156
5157
5158/*
5159 * Local variables:
5160 * c-indent-level: 8
5161 * c-basic-offset: 8
5162 * End:
5163 */