blob: 82a9e14a26ad93a6b935b7179e361ea7d3ca94f2 [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
Christopher Faulete0768eb2018-10-03 16:38:02 +020013#include <common/base64.h>
14#include <common/config.h>
15#include <common/debug.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010016#include <common/htx.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020017#include <common/uri_auth.h>
18
Christopher Faulet0f226952018-10-22 09:29:56 +020019#include <types/capture.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020020
21#include <proto/acl.h>
Christopher Faulet3e964192018-10-24 11:39:23 +020022#include <proto/action.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020023#include <proto/channel.h>
24#include <proto/checks.h>
25#include <proto/connection.h>
26#include <proto/filters.h>
Christopher Faulet0f226952018-10-22 09:29:56 +020027#include <proto/http_htx.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020028#include <proto/log.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020029#include <proto/http_ana.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020030#include <proto/proxy.h>
Christopher Fauletfefc73d2018-10-24 21:18:04 +020031#include <proto/server.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020032#include <proto/stream.h>
33#include <proto/stream_interface.h>
34#include <proto/stats.h>
Christopher Fauleta8a46e22019-07-16 14:53:09 +020035#include <proto/vars.h>
Christopher Faulete0768eb2018-10-03 16:38:02 +020036
Christopher Fauleteea8fc72019-11-05 16:18:10 +010037#define TRACE_SOURCE &trace_strm
38
Christopher Faulet377c5a52018-10-24 21:21:30 +020039extern const char *stat_status_codes[];
Christopher Fauletf2824e62018-10-01 12:12:37 +020040
Christopher Fauleta8a46e22019-07-16 14:53:09 +020041struct pool_head *pool_head_requri = NULL;
42struct pool_head *pool_head_capture = NULL;
43
44
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020045static void http_end_request(struct stream *s);
46static void http_end_response(struct stream *s);
Christopher Fauletf2824e62018-10-01 12:12:37 +020047
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020048static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr);
49static int http_del_hdr_value(char *start, char *end, char **from, char *next);
50static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020051static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl);
52static 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 +020053
Christopher Fauletb58f62b2020-01-13 16:40:13 +010054static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020055static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s);
Christopher Faulet3e964192018-10-24 11:39:23 +020056
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020057static void http_manage_client_side_cookies(struct stream *s, struct channel *req);
58static void http_manage_server_side_cookies(struct stream *s, struct channel *res);
Christopher Fauletfcda7c62018-10-24 11:56:22 +020059
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020060static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend);
61static int http_handle_stats(struct stream *s, struct channel *req);
Christopher Faulet377c5a52018-10-24 21:21:30 +020062
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020063static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg);
64static int http_reply_100_continue(struct stream *s);
65static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm);
Christopher Faulet23a3c792018-11-28 10:01:23 +010066
Christopher Faulete0768eb2018-10-03 16:38:02 +020067/* This stream analyser waits for a complete HTTP request. It returns 1 if the
68 * processing can continue on next analysers, or zero if it either needs more
69 * data or wants to immediately abort the request (eg: timeout, error, ...). It
70 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers
71 * when it has nothing left to do, and may remove any analyser when it wants to
72 * abort.
73 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020074int http_wait_for_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +020075{
Christopher Faulet9768c262018-10-22 09:34:31 +020076
Christopher Faulete0768eb2018-10-03 16:38:02 +020077 /*
Christopher Faulet9768c262018-10-22 09:34:31 +020078 * We will analyze a complete HTTP request to check the its syntax.
Christopher Faulete0768eb2018-10-03 16:38:02 +020079 *
Christopher Faulet9768c262018-10-22 09:34:31 +020080 * Once the start line and all headers are received, we may perform a
81 * capture of the error (if any), and we will set a few fields. We also
82 * check for monitor-uri, logging and finally headers capture.
Christopher Faulete0768eb2018-10-03 16:38:02 +020083 */
Christopher Faulete0768eb2018-10-03 16:38:02 +020084 struct session *sess = s->sess;
85 struct http_txn *txn = s->txn;
86 struct http_msg *msg = &txn->req;
Christopher Faulet9768c262018-10-22 09:34:31 +020087 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +010088 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +020089
Christopher Fauleteea8fc72019-11-05 16:18:10 +010090 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +020091
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010092 htx = htxbuf(&req->buf);
Christopher Faulet9768c262018-10-22 09:34:31 +020093
Willy Tarreau4236f032019-03-05 10:43:32 +010094 /* Parsing errors are caught here */
Christopher Fauletb9a92f32019-09-09 10:15:21 +020095 if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) {
Willy Tarreau4236f032019-03-05 10:43:32 +010096 stream_inc_http_req_ctr(s);
97 stream_inc_http_err_ctr(s);
98 proxy_inc_fe_req_ctr(sess->fe);
Christopher Fauletb9a92f32019-09-09 10:15:21 +020099 if (htx->flags & HTX_FL_PARSING_ERROR)
100 goto return_bad_req;
101 else
102 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +0100103 }
104
Christopher Faulete0768eb2018-10-03 16:38:02 +0200105 /* we're speaking HTTP here, so let's speak HTTP to the client */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200106 s->srv_error = http_return_srv_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200107
108 /* If there is data available for analysis, log the end of the idle time. */
Christopher Faulet870aad92018-11-29 15:23:46 +0100109 if (c_data(req) && s->logs.t_idle == -1) {
110 const struct cs_info *csinfo = si_get_cs_info(objt_cs(s->si[0].end));
111
112 s->logs.t_idle = ((csinfo)
113 ? csinfo->t_idle
114 : tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake);
115 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200116
Christopher Faulete0768eb2018-10-03 16:38:02 +0200117 /*
118 * Now we quickly check if we have found a full valid request.
119 * If not so, we check the FD and buffer states before leaving.
120 * A full request is indicated by the fact that we have seen
121 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
122 * requests are checked first. When waiting for a second request
123 * on a keep-alive stream, if we encounter and error, close, t/o,
124 * we note the error in the stream flags but don't set any state.
125 * Since the error will be noted there, it will not be counted by
126 * process_stream() as a frontend error.
127 * Last, we may increase some tracked counters' http request errors on
128 * the cases that are deliberately the client's fault. For instance,
129 * a timeout or connection reset is not counted as an error. However
130 * a bad request is.
131 */
Christopher Faulet29f17582019-05-23 11:03:26 +0200132 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet0ef372a2019-04-08 10:57:20 +0200133 if (htx->flags & HTX_FL_UPGRADE)
134 goto failed_keep_alive;
135
Christopher Faulet9768c262018-10-22 09:34:31 +0200136 /* 1: have we encountered a read error ? */
137 if (req->flags & CF_READ_ERROR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200138 if (!(s->flags & SF_ERR_MASK))
139 s->flags |= SF_ERR_CLICL;
140
141 if (txn->flags & TX_WAIT_NEXT_RQ)
142 goto failed_keep_alive;
143
144 if (sess->fe->options & PR_O_IGNORE_PRB)
145 goto failed_keep_alive;
146
Christopher Faulet9768c262018-10-22 09:34:31 +0200147 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200148 stream_inc_http_req_ctr(s);
149 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100150 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200151 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100152 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200153
Christopher Faulet9768c262018-10-22 09:34:31 +0200154 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200155 http_reply_and_close(s, txn->status, NULL);
Christopher Faulet9768c262018-10-22 09:34:31 +0200156 req->analysers &= AN_REQ_FLT_END;
157
Christopher Faulete0768eb2018-10-03 16:38:02 +0200158 if (!(s->flags & SF_FINST_MASK))
159 s->flags |= SF_FINST_R;
160 return 0;
161 }
162
Christopher Faulet9768c262018-10-22 09:34:31 +0200163 /* 2: has the read timeout expired ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200164 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
165 if (!(s->flags & SF_ERR_MASK))
166 s->flags |= SF_ERR_CLITO;
167
168 if (txn->flags & TX_WAIT_NEXT_RQ)
169 goto failed_keep_alive;
170
171 if (sess->fe->options & PR_O_IGNORE_PRB)
172 goto failed_keep_alive;
173
Christopher Faulet9768c262018-10-22 09:34:31 +0200174 stream_inc_http_err_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200175 stream_inc_http_req_ctr(s);
176 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100177 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200178 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100179 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200180
Christopher Faulet9768c262018-10-22 09:34:31 +0200181 txn->status = 408;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200182 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200183 req->analysers &= AN_REQ_FLT_END;
184
Christopher Faulete0768eb2018-10-03 16:38:02 +0200185 if (!(s->flags & SF_FINST_MASK))
186 s->flags |= SF_FINST_R;
187 return 0;
188 }
189
Christopher Faulet9768c262018-10-22 09:34:31 +0200190 /* 3: have we encountered a close ? */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200191 else if (req->flags & CF_SHUTR) {
192 if (!(s->flags & SF_ERR_MASK))
193 s->flags |= SF_ERR_CLICL;
194
195 if (txn->flags & TX_WAIT_NEXT_RQ)
196 goto failed_keep_alive;
197
198 if (sess->fe->options & PR_O_IGNORE_PRB)
199 goto failed_keep_alive;
200
Christopher Faulete0768eb2018-10-03 16:38:02 +0200201 stream_inc_http_err_ctr(s);
202 stream_inc_http_req_ctr(s);
203 proxy_inc_fe_req_ctr(sess->fe);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100204 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200205 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100206 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200207
Christopher Faulet9768c262018-10-22 09:34:31 +0200208 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200209 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet9768c262018-10-22 09:34:31 +0200210 req->analysers &= AN_REQ_FLT_END;
211
Christopher Faulete0768eb2018-10-03 16:38:02 +0200212 if (!(s->flags & SF_FINST_MASK))
213 s->flags |= SF_FINST_R;
214 return 0;
215 }
216
217 channel_dont_connect(req);
218 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
219 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100220
Christopher Faulet9768c262018-10-22 09:34:31 +0200221 if (sess->listener->options & LI_O_NOQUICKACK && htx_is_not_empty(htx) &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200222 objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) {
223 /* We need more data, we have to re-enable quick-ack in case we
224 * previously disabled it, otherwise we might cause the client
225 * to delay next data.
226 */
Willy Tarreau1a18b542018-12-11 16:37:42 +0100227 conn_set_quickack(objt_conn(sess->origin), 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200228 }
Willy Tarreau1a18b542018-12-11 16:37:42 +0100229
Christopher Faulet47365272018-10-31 17:40:50 +0100230 if ((req->flags & CF_READ_PARTIAL) && (txn->flags & TX_WAIT_NEXT_RQ)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200231 /* If the client starts to talk, let's fall back to
232 * request timeout processing.
233 */
234 txn->flags &= ~TX_WAIT_NEXT_RQ;
235 req->analyse_exp = TICK_ETERNITY;
236 }
237
238 /* just set the request timeout once at the beginning of the request */
239 if (!tick_isset(req->analyse_exp)) {
Christopher Faulet47365272018-10-31 17:40:50 +0100240 if ((txn->flags & TX_WAIT_NEXT_RQ) && tick_isset(s->be->timeout.httpka))
Christopher Faulete0768eb2018-10-03 16:38:02 +0200241 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
242 else
243 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
244 }
245
246 /* we're not ready yet */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100247 DBG_TRACE_DEVEL("waiting for the request",
248 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200249 return 0;
250
251 failed_keep_alive:
252 /* Here we process low-level errors for keep-alive requests. In
253 * short, if the request is not the first one and it experiences
254 * a timeout, read error or shutdown, we just silently close so
255 * that the client can try again.
256 */
257 txn->status = 0;
258 msg->msg_state = HTTP_MSG_RQBEFORE;
259 req->analysers &= AN_REQ_FLT_END;
260 s->logs.logwait = 0;
261 s->logs.level = 0;
262 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200263 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100264 DBG_TRACE_DEVEL("leaving by closing K/A connection",
265 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200266 return 0;
267 }
268
Christopher Faulet9768c262018-10-22 09:34:31 +0200269 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200270 stream_inc_http_req_ctr(s);
271 proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */
272
Christopher Faulet9768c262018-10-22 09:34:31 +0200273 /* kill the pending keep-alive timeout */
274 txn->flags &= ~TX_WAIT_NEXT_RQ;
275 req->analyse_exp = TICK_ETERNITY;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200276
Christopher Faulet29f17582019-05-23 11:03:26 +0200277 BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +0200278 sl = http_get_stline(htx);
Christopher Faulet03599112018-11-27 11:21:21 +0100279
Christopher Faulet9768c262018-10-22 09:34:31 +0200280 /* 0: we might have to print this header in debug mode */
281 if (unlikely((global.mode & MODE_DEBUG) &&
282 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
283 int32_t pos;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200284
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200285 http_debug_stline("clireq", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +0200286
Christopher Fauleta3f15502019-05-13 15:27:23 +0200287 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200288 struct htx_blk *blk = htx_get_blk(htx, pos);
289 enum htx_blk_type type = htx_get_blk_type(blk);
290
291 if (type == HTX_BLK_EOH)
292 break;
293 if (type != HTX_BLK_HDR)
294 continue;
295
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200296 http_debug_hdr("clihdr", s,
297 htx_get_blk_name(htx, blk),
298 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +0200299 }
300 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200301
302 /*
Christopher Faulet03599112018-11-27 11:21:21 +0100303 * 1: identify the method and the version. Also set HTTP flags
Christopher Faulete0768eb2018-10-03 16:38:02 +0200304 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100305 txn->meth = sl->info.req.meth;
Christopher Faulet03599112018-11-27 11:21:21 +0100306 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +0200307 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +0100308 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +0100309 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +0100310 if (sl->flags & HTX_SL_F_BODYLESS)
311 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200312
313 /* we can make use of server redirect on GET and HEAD */
314 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
315 s->flags |= SF_REDIRECTABLE;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100316 else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200317 /* PRI is reserved for the HTTP/2 preface */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200318 goto return_bad_req;
319 }
320
321 /*
322 * 2: check if the URI matches the monitor_uri.
323 * We have to do this for every request which gets in, because
324 * the monitor-uri is defined by the frontend.
325 */
326 if (unlikely((sess->fe->monitor_uri_len != 0) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100327 isteqi(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200328 /*
329 * We have found the monitor URI
330 */
331 struct acl_cond *cond;
332
333 s->flags |= SF_MONITOR;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100334 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200335
336 /* Check if we want to fail this monitor request or not */
337 list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) {
338 int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
339
340 ret = acl_pass(ret);
341 if (cond->pol == ACL_COND_UNLESS)
342 ret = !ret;
343
344 if (ret) {
345 /* we fail this request, let's return 503 service unavail */
346 txn->status = 503;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200347 if (!(s->flags & SF_ERR_MASK))
348 s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */
349 goto return_prx_cond;
350 }
351 }
352
Joseph Herlantc42c0e92018-11-25 10:43:27 -0800353 /* nothing to fail, let's reply normally */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200354 txn->status = 200;
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 /*
361 * 3: Maybe we have to copy the original REQURI for the logs ?
362 * Note: we cannot log anymore if the request has been
363 * classified as invalid.
364 */
365 if (unlikely(s->logs.logwait & LW_REQ)) {
366 /* we have a complete HTTP request that we must log */
367 if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) {
Christopher Faulet9768c262018-10-22 09:34:31 +0200368 size_t len;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200369
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200370 len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1);
Christopher Faulet9768c262018-10-22 09:34:31 +0200371 txn->uri[len] = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200372
373 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
374 s->do_log(s);
375 } else {
376 ha_alert("HTTP logging : out of memory.\n");
377 }
378 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200379
Christopher Faulete0768eb2018-10-03 16:38:02 +0200380 /* if the frontend has "option http-use-proxy-header", we'll check if
381 * we have what looks like a proxied connection instead of a connection,
382 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
383 * Note that this is *not* RFC-compliant, however browsers and proxies
384 * happen to do that despite being non-standard :-(
385 * We consider that a request not beginning with either '/' or '*' is
386 * a proxied connection, which covers both "scheme://location" and
387 * CONNECT ip:port.
388 */
389 if ((sess->fe->options2 & PR_O2_USE_PXHDR) &&
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100390 *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*')
Christopher Faulete0768eb2018-10-03 16:38:02 +0200391 txn->flags |= TX_USE_PX_CONN;
392
Christopher Faulete0768eb2018-10-03 16:38:02 +0200393 /* 5: we may need to capture headers */
394 if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200395 http_capture_headers(htx, s->req_cap, sess->fe->req_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200396
Christopher Faulete0768eb2018-10-03 16:38:02 +0200397 /* we may have to wait for the request's body */
Christopher Faulet9768c262018-10-22 09:34:31 +0200398 if (s->be->options & PR_O_WREQ_BODY)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200399 req->analysers |= AN_REQ_HTTP_BODY;
400
401 /*
402 * RFC7234#4:
403 * A cache MUST write through requests with methods
404 * that are unsafe (Section 4.2.1 of [RFC7231]) to
405 * the origin server; i.e., a cache is not allowed
406 * to generate a reply to such a request before
407 * having forwarded the request and having received
408 * a corresponding response.
409 *
410 * RFC7231#4.2.1:
411 * Of the request methods defined by this
412 * specification, the GET, HEAD, OPTIONS, and TRACE
413 * methods are defined to be safe.
414 */
415 if (likely(txn->meth == HTTP_METH_GET ||
416 txn->meth == HTTP_METH_HEAD ||
417 txn->meth == HTTP_METH_OPTIONS ||
418 txn->meth == HTTP_METH_TRACE))
419 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
420
421 /* end of job, return OK */
422 req->analysers &= ~an_bit;
423 req->analyse_exp = TICK_ETERNITY;
Christopher Faulet9768c262018-10-22 09:34:31 +0200424
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100425 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200426 return 1;
427
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200428 return_int_err:
429 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200430 if (!(s->flags & SF_ERR_MASK))
431 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100432 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200433 if (sess->listener->counters)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100434 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200435 goto return_prx_cond;
436
Christopher Faulete0768eb2018-10-03 16:38:02 +0200437 return_bad_req:
Christopher Faulet9768c262018-10-22 09:34:31 +0200438 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100439 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200440 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100441 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200442 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200443
444 return_prx_cond:
Christopher Fauletb9a92f32019-09-09 10:15:21 +0200445 http_reply_and_close(s, txn->status, http_error_message(s));
446
Christopher Faulete0768eb2018-10-03 16:38:02 +0200447 if (!(s->flags & SF_ERR_MASK))
448 s->flags |= SF_ERR_PRXCOND;
449 if (!(s->flags & SF_FINST_MASK))
450 s->flags |= SF_FINST_R;
451
452 req->analysers &= AN_REQ_FLT_END;
453 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100454 DBG_TRACE_DEVEL("leaving on error",
455 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200456 return 0;
457}
458
459
460/* This stream analyser runs all HTTP request processing which is common to
461 * frontends and backends, which means blocking ACLs, filters, connection-close,
462 * reqadd, stats and redirects. This is performed for the designated proxy.
463 * It returns 1 if the processing can continue on next analysers, or zero if it
464 * either needs more data or wants to immediately abort the request (eg: deny,
465 * error, ...).
466 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200467int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200468{
469 struct session *sess = s->sess;
470 struct http_txn *txn = s->txn;
471 struct http_msg *msg = &txn->req;
Christopher Fauletff2759f2018-10-24 11:13:16 +0200472 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200473 struct redirect_rule *rule;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200474 enum rule_result verdict;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200475 struct connection *conn = objt_conn(sess->origin);
476
477 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
478 /* we need more data */
479 goto return_prx_yield;
480 }
481
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100482 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200483
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100484 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200485
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200486 /* just in case we have some per-backend tracking. Only called the first
487 * execution of the analyser. */
488 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
489 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200490
491 /* evaluate http-request rules */
492 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100493 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200494
495 switch (verdict) {
496 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
497 goto return_prx_yield;
498
499 case HTTP_RULE_RES_CONT:
500 case HTTP_RULE_RES_STOP: /* nothing to do */
501 break;
502
503 case HTTP_RULE_RES_DENY: /* deny or tarpit */
504 if (txn->flags & TX_CLTARPIT)
505 goto tarpit;
506 goto deny;
507
508 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
509 goto return_prx_cond;
510
511 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
512 goto done;
513
514 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
515 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100516
517 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
518 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200519 }
520 }
521
522 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
523 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200524 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200525
Christopher Fauletff2759f2018-10-24 11:13:16 +0200526 ctx.blk = NULL;
527 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
528 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100529 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200530 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200531 }
532
533 /* OK at this stage, we know that the request was accepted according to
534 * the http-request rules, we can check for the stats. Note that the
535 * URI is detected *before* the req* rules in order not to be affected
536 * by a possible reqrep, while they are processed *after* so that a
537 * reqdeny can still block them. This clearly needs to change in 1.6!
538 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200539 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200540 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100541 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200542 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200543 if (!(s->flags & SF_ERR_MASK))
544 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100545 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200546 }
547
548 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200549 http_handle_stats(s, req);
Christopher Fauletb58f62b2020-01-13 16:40:13 +0100550 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200551 /* not all actions implemented: deny, allow, auth */
552
553 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
554 goto deny;
555
556 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
557 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100558
559 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
560 goto return_bad_req;
561
562 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
563 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200564 }
565
Christopher Faulet2571bc62019-03-01 11:44:26 +0100566 /* Proceed with the applets now. */
567 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200568 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100569 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200570
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200571 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100572 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100573
Christopher Faulete0768eb2018-10-03 16:38:02 +0200574 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
575 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
576 if (!(s->flags & SF_FINST_MASK))
577 s->flags |= SF_FINST_R;
578
579 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
580 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
581 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
582 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100583
584 req->flags |= CF_SEND_DONTWAIT;
585 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200586 goto done;
587 }
588
589 /* check whether we have some ACLs set to redirect this request */
590 list_for_each_entry(rule, &px->redirect_rules, list) {
591 if (rule->cond) {
592 int ret;
593
594 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
595 ret = acl_pass(ret);
596 if (rule->cond->pol == ACL_COND_UNLESS)
597 ret = !ret;
598 if (!ret)
599 continue;
600 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200601 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100602 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200603 goto done;
604 }
605
606 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
607 * If this happens, then the data will not come immediately, so we must
608 * send all what we have without waiting. Note that due to the small gain
609 * in waiting for the body of the request, it's easier to simply put the
610 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
611 * itself once used.
612 */
613 req->flags |= CF_SEND_DONTWAIT;
614
615 done: /* done with this analyser, continue with next ones that the calling
616 * points will have set, if any.
617 */
618 req->analyse_exp = TICK_ETERNITY;
619 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
620 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100621 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200622 return 1;
623
624 tarpit:
625 /* Allow cookie logging
626 */
627 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200628 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200629
630 /* When a connection is tarpitted, we use the tarpit timeout,
631 * which may be the same as the connect timeout if unspecified.
632 * If unset, then set it to zero because we really want it to
633 * eventually expire. We build the tarpit as an analyser.
634 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100635 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200636
637 /* wipe the request out so that we can drop the connection early
638 * if the client closes first.
639 */
640 channel_dont_connect(req);
641
Christopher Faulete0768eb2018-10-03 16:38:02 +0200642 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
643 req->analysers |= AN_REQ_HTTP_TARPIT;
644 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
645 if (!req->analyse_exp)
646 req->analyse_exp = tick_add(now_ms, 0);
647 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100648 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100649 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100650 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200651 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100652 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200653 goto done_without_exp;
654
655 deny: /* this request was blocked (denied) */
656
657 /* Allow cookie logging
658 */
659 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200660 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200661
Christopher Faulete0768eb2018-10-03 16:38:02 +0200662 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200663 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100664 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100665 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100666 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200667 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100668 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100669 goto return_prx_err;
670
671 return_int_err:
672 txn->status = 500;
673 if (!(s->flags & SF_ERR_MASK))
674 s->flags |= SF_ERR_INTERNAL;
675 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100676 if (s->flags & SF_BE_ASSIGNED)
677 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100678 if (sess->listener->counters)
679 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
680 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200681
682 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200683 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100684 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200685 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100686 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100687 /* fall through */
688
689 return_prx_err:
690 http_reply_and_close(s, txn->status, http_error_message(s));
691 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200692
693 return_prx_cond:
694 if (!(s->flags & SF_ERR_MASK))
695 s->flags |= SF_ERR_PRXCOND;
696 if (!(s->flags & SF_FINST_MASK))
697 s->flags |= SF_FINST_R;
698
699 req->analysers &= AN_REQ_FLT_END;
700 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100701 DBG_TRACE_DEVEL("leaving on error",
702 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200703 return 0;
704
705 return_prx_yield:
706 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100707 DBG_TRACE_DEVEL("waiting for more data",
708 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200709 return 0;
710}
711
712/* This function performs all the processing enabled for the current request.
713 * It returns 1 if the processing can continue on next analysers, or zero if it
714 * needs more data, encounters an error, or wants to immediately abort the
715 * request. It relies on buffers flags, and updates s->req.analysers.
716 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200717int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200718{
719 struct session *sess = s->sess;
720 struct http_txn *txn = s->txn;
721 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200722 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200723 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
724
725 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
726 /* we need more data */
727 channel_dont_connect(req);
728 return 0;
729 }
730
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100731 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200732
733 /*
734 * Right now, we know that we have processed the entire headers
735 * and that unwanted requests have been filtered out. We can do
736 * whatever we want with the remaining request. Also, now we
737 * may have separate values for ->fe, ->be.
738 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100739 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200740
741 /*
742 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200743 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200744 */
745 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100746 struct htx_sl *sl;
747 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200748
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200749 if (!sockaddr_alloc(&s->target_addr)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200750 if (!(s->flags & SF_ERR_MASK))
751 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100752 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200753 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200754 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100755 uri = htx_sl_req_uri(sl);
756 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200757
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200758 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200759 goto return_bad_req;
760
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200761 s->target = &s->be->obj_type;
762 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
763
Christopher Faulete0768eb2018-10-03 16:38:02 +0200764 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200765 * uri.ptr and path.ptr (excluded). If it was not found, we need
766 * to replace from all the uri by a single "/".
767 *
768 * Instead of rewritting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100769 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200770 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200771 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100772 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200773 }
774
775 /*
776 * 7: Now we can work with the cookies.
777 * Note that doing so might move headers in the request, but
778 * the fields will stay coherent and the URI will not move.
779 * This should only be performed in the backend.
780 */
781 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200782 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200783
784 /* add unique-id if "header-unique-id" is specified */
785
786 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100787 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) {
788 if (!(s->flags & SF_ERR_MASK))
789 s->flags |= SF_ERR_RESOURCE;
790 goto return_int_err;
791 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200792 s->unique_id[0] = '\0';
793 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
794 }
795
796 if (sess->fe->header_unique_id && s->unique_id) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200797 struct ist n = ist2(sess->fe->header_unique_id, strlen(sess->fe->header_unique_id));
798 struct ist v = ist2(s->unique_id, strlen(s->unique_id));
799
800 if (unlikely(!http_add_header(htx, n, v)))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100801 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200802 }
803
804 /*
805 * 9: add X-Forwarded-For if either the frontend or the backend
806 * asks for it.
807 */
808 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200809 struct http_hdr_ctx ctx = { .blk = NULL };
810 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
811 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
812
Christopher Faulete0768eb2018-10-03 16:38:02 +0200813 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200814 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200815 /* The header is set to be added only if none is present
816 * and we found it, so don't do anything.
817 */
818 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200819 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200820 /* Add an X-Forwarded-For header unless the source IP is
821 * in the 'except' network range.
822 */
823 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200824 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200825 != sess->fe->except_net.s_addr) &&
826 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200827 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200828 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200829 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200830
831 /* Note: we rely on the backend to get the header name to be used for
832 * x-forwarded-for, because the header is really meant for the backends.
833 * However, if the backend did not specify any option, we have to rely
834 * on the frontend's header name.
835 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200836 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
837 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100838 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200839 }
840 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200841 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200842 /* FIXME: for the sake of completeness, we should also support
843 * 'except' here, although it is mostly useless in this case.
844 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200845 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200846
Christopher Faulete0768eb2018-10-03 16:38:02 +0200847 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200848 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200849 pn, sizeof(pn));
850
851 /* Note: we rely on the backend to get the header name to be used for
852 * x-forwarded-for, because the header is really meant for the backends.
853 * However, if the backend did not specify any option, we have to rely
854 * on the frontend's header name.
855 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200856 chunk_printf(&trash, "%s", pn);
857 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100858 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200859 }
860 }
861
862 /*
863 * 10: add X-Original-To if either the frontend or the backend
864 * asks for it.
865 */
866 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
867
868 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200869 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 +0200870 /* Add an X-Original-To header unless the destination IP is
871 * in the 'except' network range.
872 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200873 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200874 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200875 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200876 != sess->fe->except_to.s_addr) &&
877 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200878 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200879 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200880 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200881 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200882
883 /* Note: we rely on the backend to get the header name to be used for
884 * x-original-to, because the header is really meant for the backends.
885 * However, if the backend did not specify any option, we have to rely
886 * on the frontend's header name.
887 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200888 if (s->be->orgto_hdr_len)
889 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
890 else
891 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200892
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200893 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
894 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100895 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200896 }
897 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200898 }
899
Christopher Faulete0768eb2018-10-03 16:38:02 +0200900 /* If we have no server assigned yet and we're balancing on url_param
901 * with a POST request, we may be interested in checking the body for
902 * that parameter. This will be done in another analyser.
903 */
904 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100905 s->txn->meth == HTTP_METH_POST &&
906 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200907 channel_dont_connect(req);
908 req->analysers |= AN_REQ_HTTP_BODY;
909 }
910
911 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
912 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100913
Christopher Faulete0768eb2018-10-03 16:38:02 +0200914 /* We expect some data from the client. Unless we know for sure
915 * we already have a full request, we have to re-enable quick-ack
916 * in case we previously disabled it, otherwise we might cause
917 * the client to delay further data.
918 */
919 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200920 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100921 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200922
923 /*************************************************************
924 * OK, that's finished for the headers. We have done what we *
925 * could. Let's switch to the DATA state. *
926 ************************************************************/
927 req->analyse_exp = TICK_ETERNITY;
928 req->analysers &= ~an_bit;
929
930 s->logs.tv_request = now;
931 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100932 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200933 return 1;
934
Christopher Fauletb8a53712019-12-16 11:29:38 +0100935 return_int_err:
936 txn->status = 500;
937 if (!(s->flags & SF_ERR_MASK))
938 s->flags |= SF_ERR_INTERNAL;
939 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100940 if (s->flags & SF_BE_ASSIGNED)
941 _HA_ATOMIC_ADD(&sess->fe->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100942 if (sess->listener->counters)
943 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
944 goto return_prx_cond;
945
Christopher Faulete0768eb2018-10-03 16:38:02 +0200946 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200947 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100948 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200949 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100950 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100951 /* fall through */
952
953 return_prx_cond:
954 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200955
956 if (!(s->flags & SF_ERR_MASK))
957 s->flags |= SF_ERR_PRXCOND;
958 if (!(s->flags & SF_FINST_MASK))
959 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100960
961 req->analysers &= AN_REQ_FLT_END;
962 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100963 DBG_TRACE_DEVEL("leaving on error",
964 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200965 return 0;
966}
967
968/* This function is an analyser which processes the HTTP tarpit. It always
969 * returns zero, at the beginning because it prevents any other processing
970 * from occurring, and at the end because it terminates the request.
971 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200972int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200973{
974 struct http_txn *txn = s->txn;
975
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100976 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200977 /* This connection is being tarpitted. The CLIENT side has
978 * already set the connect expiration date to the right
979 * timeout. We just have to check that the client is still
980 * there and that the timeout has not expired.
981 */
982 channel_dont_connect(req);
983 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100984 !tick_is_expired(req->analyse_exp, now_ms)) {
985 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
986 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200987 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100988 }
989
Christopher Faulete0768eb2018-10-03 16:38:02 +0200990
991 /* We will set the queue timer to the time spent, just for
992 * logging purposes. We fake a 500 server error, so that the
993 * attacker will not suspect his connection has been tarpitted.
994 * It will not cause trouble to the logs because we can exclude
995 * the tarpitted connections by filtering on the 'PT' status flags.
996 */
997 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
998
999 if (!(req->flags & CF_READ_ERROR))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001000 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001001
1002 req->analysers &= AN_REQ_FLT_END;
1003 req->analyse_exp = TICK_ETERNITY;
1004
1005 if (!(s->flags & SF_ERR_MASK))
1006 s->flags |= SF_ERR_PRXCOND;
1007 if (!(s->flags & SF_FINST_MASK))
1008 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001009
1010 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001011 return 0;
1012}
1013
1014/* This function is an analyser which waits for the HTTP request body. It waits
1015 * for either the buffer to be full, or the full advertised contents to have
1016 * reached the buffer. It must only be called after the standard HTTP request
1017 * processing has occurred, because it expects the request to be parsed and will
1018 * look for the Expect header. It may send a 100-Continue interim response. It
1019 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
1020 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
1021 * needs to read more data, or 1 once it has completed its analysis.
1022 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001023int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001024{
1025 struct session *sess = s->sess;
1026 struct http_txn *txn = s->txn;
1027 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001028 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001029
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001030 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001031
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001032 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001033
Willy Tarreau4236f032019-03-05 10:43:32 +01001034 if (htx->flags & HTX_FL_PARSING_ERROR)
1035 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001036 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1037 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001038
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001039 if (msg->msg_state < HTTP_MSG_BODY)
1040 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001041
Christopher Faulete0768eb2018-10-03 16:38:02 +02001042 /* We have to parse the HTTP request body to find any required data.
1043 * "balance url_param check_post" should have been the only way to get
1044 * into this. We were brought here after HTTP header analysis, so all
1045 * related structures are ready.
1046 */
1047
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001048 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001049 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001050 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001051 }
1052
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001053 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001054
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001055 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1056 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001057 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001058 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001059 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001060 goto http_end;
1061
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001062 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001063 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1064 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001065 if (!(s->flags & SF_ERR_MASK))
1066 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001067 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1068 if (sess->listener->counters)
1069 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1070 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001071 }
1072
1073 /* we get here if we need to wait for more data */
1074 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1075 /* Not enough data. We'll re-use the http-request
1076 * timeout here. Ideally, we should set the timeout
1077 * relative to the accept() date. We just set the
1078 * request timeout once at the beginning of the
1079 * request.
1080 */
1081 channel_dont_connect(req);
1082 if (!tick_isset(req->analyse_exp))
1083 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001084 DBG_TRACE_DEVEL("waiting for more data",
1085 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001086 return 0;
1087 }
1088
1089 http_end:
1090 /* The situation will not evolve, so let's give up on the analysis. */
1091 s->logs.tv_request = now; /* update the request timer to reflect full request */
1092 req->analysers &= ~an_bit;
1093 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001094 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001095 return 1;
1096
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001097 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001098 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001099 if (!(s->flags & SF_ERR_MASK))
1100 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001101 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001102 if (s->flags & SF_BE_ASSIGNED)
1103 _HA_ATOMIC_ADD(&sess->fe->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001104 if (sess->listener->counters)
1105 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
1106 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001107
Christopher Faulete0768eb2018-10-03 16:38:02 +02001108 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001109 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001110 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1111 if (sess->listener->counters)
1112 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1113 /* fall through */
1114
1115 return_prx_cond:
1116 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001117
1118 if (!(s->flags & SF_ERR_MASK))
1119 s->flags |= SF_ERR_PRXCOND;
1120 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001121 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001122
Christopher Faulete0768eb2018-10-03 16:38:02 +02001123 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001124 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001125 DBG_TRACE_DEVEL("leaving on error",
1126 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001127 return 0;
1128}
1129
1130/* This function is an analyser which forwards request body (including chunk
1131 * sizes if any). It is called as soon as we must forward, even if we forward
1132 * zero byte. The only situation where it must not be called is when we're in
1133 * tunnel mode and we want to forward till the close. It's used both to forward
1134 * remaining data and to resync after end of body. It expects the msg_state to
1135 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1136 * read more data, or 1 once we can go on with next request or end the stream.
1137 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1138 * bytes of pending data + the headers if not already done.
1139 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001140int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001141{
1142 struct session *sess = s->sess;
1143 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001144 struct http_msg *msg = &txn->req;
1145 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001146 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001147 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001148
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001149 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001150
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001151 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001152
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001153 if (htx->flags & HTX_FL_PARSING_ERROR)
1154 goto return_bad_req;
1155 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1156 goto return_int_err;
1157
Christopher Faulete0768eb2018-10-03 16:38:02 +02001158 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1159 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1160 /* Output closed while we were sending data. We must abort and
1161 * wake the other side up.
1162 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001163
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001164 /* Don't abort yet if we had L7 retries activated and it
1165 * was a write error, we may recover.
1166 */
1167 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001168 (s->si[1].flags & SI_FL_L7_RETRY)) {
1169 DBG_TRACE_DEVEL("leaving on L7 retry",
1170 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001171 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001172 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001173 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001174 http_end_request(s);
1175 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001176 DBG_TRACE_DEVEL("leaving on error",
1177 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001178 return 1;
1179 }
1180
1181 /* Note that we don't have to send 100-continue back because we don't
1182 * need the data to complete our job, and it's up to the server to
1183 * decide whether to return 100, 417 or anything else in return of
1184 * an "Expect: 100-continue" header.
1185 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001186 if (msg->msg_state == HTTP_MSG_BODY)
1187 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001188
Christopher Faulete0768eb2018-10-03 16:38:02 +02001189 /* in most states, we should abort in case of early close */
1190 channel_auto_close(req);
1191
1192 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001193 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001194 if (req->flags & CF_EOI)
1195 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001196 }
1197 else {
1198 /* We can't process the buffer's contents yet */
1199 req->flags |= CF_WAKE_WRITE;
1200 goto missing_data_or_waiting;
1201 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001202 }
1203
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001204 if (msg->msg_state >= HTTP_MSG_ENDING)
1205 goto ending;
1206
1207 if (txn->meth == HTTP_METH_CONNECT) {
1208 msg->msg_state = HTTP_MSG_ENDING;
1209 goto ending;
1210 }
1211
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001212 /* Forward input data. We get it by removing all outgoing data not
1213 * forwarded yet from HTX data size. If there are some data filters, we
1214 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001215 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001216 if (HAS_REQ_DATA_FILTERS(s)) {
1217 ret = flt_http_payload(s, msg, htx->data);
1218 if (ret < 0)
1219 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001220 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001221 }
1222 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001223 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001224 if (msg->flags & HTTP_MSGF_XFER_LEN)
1225 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001226 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001227
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001228 if (htx->data != co_data(req))
1229 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001230
Christopher Faulet9768c262018-10-22 09:34:31 +02001231 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001232 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1233 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001234 */
1235 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1236 goto missing_data_or_waiting;
1237
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001238 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001239
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001240 ending:
1241 /* other states, ENDING...TUNNEL */
1242 if (msg->msg_state >= HTTP_MSG_DONE)
1243 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001244
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001245 if (HAS_REQ_DATA_FILTERS(s)) {
1246 ret = flt_http_end(s, msg);
1247 if (ret <= 0) {
1248 if (!ret)
1249 goto missing_data_or_waiting;
1250 goto return_bad_req;
1251 }
1252 }
1253
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001254 if (txn->meth == HTTP_METH_CONNECT)
1255 msg->msg_state = HTTP_MSG_TUNNEL;
1256 else {
1257 msg->msg_state = HTTP_MSG_DONE;
1258 req->to_forward = 0;
1259 }
1260
1261 done:
1262 /* we don't want to forward closes on DONE except in tunnel mode. */
1263 if (!(txn->flags & TX_CON_WANT_TUN))
1264 channel_dont_close(req);
1265
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001266 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001267 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001268 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001269 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1270 if (req->flags & CF_SHUTW) {
1271 /* request errors are most likely due to the
1272 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001273 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001274 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001275 goto return_bad_req;
1276 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001277 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001278 return 1;
1279 }
1280
1281 /* If "option abortonclose" is set on the backend, we want to monitor
1282 * the client's connection and forward any shutdown notification to the
1283 * server, which will decide whether to close or to go on processing the
1284 * request. We only do that in tunnel mode, and not in other modes since
1285 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001286 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001287 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001288 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001289 s->si[1].flags |= SI_FL_NOLINGER;
1290 channel_auto_close(req);
1291 }
1292 else if (s->txn->meth == HTTP_METH_POST) {
1293 /* POST requests may require to read extra CRLF sent by broken
1294 * browsers and which could cause an RST to be sent upon close
1295 * on some systems (eg: Linux). */
1296 channel_auto_read(req);
1297 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001298 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1299 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001300 return 0;
1301
1302 missing_data_or_waiting:
1303 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001304 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001305 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001306
1307 waiting:
1308 /* waiting for the last bits to leave the buffer */
1309 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001310 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001311
1312 /* When TE: chunked is used, we need to get there again to parse remaining
1313 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1314 * And when content-length is used, we never want to let the possible
1315 * shutdown be forwarded to the other side, as the state machine will
1316 * take care of it once the client responds. It's also important to
1317 * prevent TIME_WAITs from accumulating on the backend side, and for
1318 * HTTP/2 where the last frame comes with a shutdown.
1319 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001320 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001321 channel_dont_close(req);
1322
1323 /* We know that more data are expected, but we couldn't send more that
1324 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1325 * system knows it must not set a PUSH on this first part. Interactive
1326 * modes are already handled by the stream sock layer. We must not do
1327 * this in content-length mode because it could present the MSG_MORE
1328 * flag with the last block of forwarded data, which would cause an
1329 * additional delay to be observed by the receiver.
1330 */
1331 if (msg->flags & HTTP_MSGF_TE_CHNK)
1332 req->flags |= CF_EXPECT_MORE;
1333
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001334 DBG_TRACE_DEVEL("waiting for more data to forward",
1335 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001336 return 0;
1337
Christopher Faulet93e02d82019-03-08 14:18:50 +01001338 return_cli_abort:
1339 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1340 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001341 if (sess->listener->counters)
1342 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001343 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001344 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001345 if (!(s->flags & SF_ERR_MASK))
1346 s->flags |= SF_ERR_CLICL;
1347 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001348 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001349
1350 return_srv_abort:
1351 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1352 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001353 if (sess->listener->counters)
1354 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001355 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001356 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001357 if (!(s->flags & SF_ERR_MASK))
1358 s->flags |= SF_ERR_SRVCL;
1359 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001360 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001361
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001362 return_int_err:
1363 if (!(s->flags & SF_ERR_MASK))
1364 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001365 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001366 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001367 if (sess->listener->counters)
1368 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001369 if (objt_server(s->target))
1370 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001371 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001372 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001373
Christopher Faulet93e02d82019-03-08 14:18:50 +01001374 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001375 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001376 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001377 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001378 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001379 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001380
Christopher Fauletb8a53712019-12-16 11:29:38 +01001381 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001382 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001383 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001384 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001385 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001386 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001387 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001388 }
1389 req->analysers &= AN_REQ_FLT_END;
1390 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 +01001391 if (!(s->flags & SF_ERR_MASK))
1392 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001393 if (!(s->flags & SF_FINST_MASK))
1394 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001395 DBG_TRACE_DEVEL("leaving on error ",
1396 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001397 return 0;
1398}
1399
Olivier Houcharda254a372019-04-05 15:30:12 +02001400/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1401/* Returns 0 if we can attempt to retry, -1 otherwise */
1402static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1403{
1404 struct channel *req, *res;
1405 int co_data;
1406
1407 si->conn_retries--;
1408 if (si->conn_retries < 0)
1409 return -1;
1410
Willy Tarreau223995e2019-05-04 10:38:31 +02001411 if (objt_server(s->target))
1412 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1413 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1414
Olivier Houcharda254a372019-04-05 15:30:12 +02001415 req = &s->req;
1416 res = &s->res;
1417 /* Remove any write error from the request, and read error from the response */
1418 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1419 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1420 res->analysers = 0;
1421 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001422 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001423 si->exp = TICK_ETERNITY;
1424 res->rex = TICK_ETERNITY;
1425 res->to_forward = 0;
1426 res->analyse_exp = TICK_ETERNITY;
1427 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001428 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001429 si_release_endpoint(&s->si[1]);
1430 b_free(&req->buf);
1431 /* Swap the L7 buffer with the channel buffer */
1432 /* We know we stored the co_data as b_data, so get it there */
1433 co_data = b_data(&si->l7_buffer);
1434 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1435 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1436
1437 co_set_data(req, co_data);
1438 b_reset(&res->buf);
1439 co_set_data(res, 0);
1440 return 0;
1441}
1442
Christopher Faulete0768eb2018-10-03 16:38:02 +02001443/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1444 * processing can continue on next analysers, or zero if it either needs more
1445 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1446 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1447 * when it has nothing left to do, and may remove any analyser when it wants to
1448 * abort.
1449 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001450int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001451{
Christopher Faulet9768c262018-10-22 09:34:31 +02001452 /*
1453 * We will analyze a complete HTTP response to check the its syntax.
1454 *
1455 * Once the start line and all headers are received, we may perform a
1456 * capture of the error (if any), and we will set a few fields. We also
1457 * logging and finally headers capture.
1458 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001459 struct session *sess = s->sess;
1460 struct http_txn *txn = s->txn;
1461 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001462 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001463 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001464 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001465 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001466 int n;
1467
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001468 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001469
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001470 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001471
Willy Tarreau4236f032019-03-05 10:43:32 +01001472 /* Parsing errors are caught here */
1473 if (htx->flags & HTX_FL_PARSING_ERROR)
1474 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001475 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1476 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001477
Christopher Faulete0768eb2018-10-03 16:38:02 +02001478 /*
1479 * Now we quickly check if we have found a full valid response.
1480 * If not so, we check the FD and buffer states before leaving.
1481 * A full response is indicated by the fact that we have seen
1482 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1483 * responses are checked first.
1484 *
1485 * Depending on whether the client is still there or not, we
1486 * may send an error response back or not. Note that normally
1487 * we should only check for HTTP status there, and check I/O
1488 * errors somewhere else.
1489 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001490 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001491 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001492 /* 1: have we encountered a read error ? */
1493 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001494 struct connection *conn = NULL;
1495
Olivier Houchard865d8392019-05-03 22:46:27 +02001496 if (objt_cs(s->si[1].end))
1497 conn = objt_cs(s->si[1].end)->conn;
1498
1499 if (si_b->flags & SI_FL_L7_RETRY &&
1500 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001501 /* If we arrive here, then CF_READ_ERROR was
1502 * set by si_cs_recv() because we matched a
1503 * status, overwise it would have removed
1504 * the SI_FL_L7_RETRY flag, so it's ok not
1505 * to check s->be->retry_type.
1506 */
1507 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1508 return 0;
1509 }
1510
Olivier Houchard6db16992019-05-17 15:40:49 +02001511 if (txn->flags & TX_NOT_FIRST)
1512 goto abort_keep_alive;
1513
Olivier Houcharda798bf52019-03-08 18:52:00 +01001514 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001515 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001516 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001517 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001518 }
1519
Christopher Faulete0768eb2018-10-03 16:38:02 +02001520 rep->analysers &= AN_RES_FLT_END;
1521 txn->status = 502;
1522
1523 /* Check to see if the server refused the early data.
1524 * If so, just send a 425
1525 */
Olivier Houchard865d8392019-05-03 22:46:27 +02001526 if (conn->err_code == CO_ER_SSL_EARLY_FAILED) {
1527 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001528 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001529 do_l7_retry(s, si_b) == 0) {
1530 DBG_TRACE_DEVEL("leaving on L7 retry",
1531 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001532 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001533 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001534 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001535 }
1536
1537 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001538 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001539
1540 if (!(s->flags & SF_ERR_MASK))
1541 s->flags |= SF_ERR_SRVCL;
1542 if (!(s->flags & SF_FINST_MASK))
1543 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001544 DBG_TRACE_DEVEL("leaving on error",
1545 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001546 return 0;
1547 }
1548
Christopher Faulet9768c262018-10-22 09:34:31 +02001549 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001550 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001551 if ((si_b->flags & SI_FL_L7_RETRY) &&
1552 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001553 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1554 DBG_TRACE_DEVEL("leaving on L7 retry",
1555 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001556 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001557 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001558 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001559 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001560 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001561 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001562 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001563 }
1564
Christopher Faulete0768eb2018-10-03 16:38:02 +02001565 rep->analysers &= AN_RES_FLT_END;
1566 txn->status = 504;
1567 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001568 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001569
1570 if (!(s->flags & SF_ERR_MASK))
1571 s->flags |= SF_ERR_SRVTO;
1572 if (!(s->flags & SF_FINST_MASK))
1573 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001574 DBG_TRACE_DEVEL("leaving on error",
1575 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001576 return 0;
1577 }
1578
Christopher Faulet9768c262018-10-22 09:34:31 +02001579 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001580 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001581 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1582 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001583 if (sess->listener->counters)
1584 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001585 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001586 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001587
1588 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001589 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001590 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001591
1592 if (!(s->flags & SF_ERR_MASK))
1593 s->flags |= SF_ERR_CLICL;
1594 if (!(s->flags & SF_FINST_MASK))
1595 s->flags |= SF_FINST_H;
1596
1597 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001598 DBG_TRACE_DEVEL("leaving on error",
1599 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001600 return 0;
1601 }
1602
Christopher Faulet9768c262018-10-22 09:34:31 +02001603 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001604 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001605 if ((si_b->flags & SI_FL_L7_RETRY) &&
1606 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001607 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1608 DBG_TRACE_DEVEL("leaving on L7 retry",
1609 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001610 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001611 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001612 }
1613
Olivier Houchard6db16992019-05-17 15:40:49 +02001614 if (txn->flags & TX_NOT_FIRST)
1615 goto abort_keep_alive;
1616
Olivier Houcharda798bf52019-03-08 18:52:00 +01001617 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001618 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001619 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001620 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001621 }
1622
Christopher Faulete0768eb2018-10-03 16:38:02 +02001623 rep->analysers &= AN_RES_FLT_END;
1624 txn->status = 502;
1625 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001626 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001627
1628 if (!(s->flags & SF_ERR_MASK))
1629 s->flags |= SF_ERR_SRVCL;
1630 if (!(s->flags & SF_FINST_MASK))
1631 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001632 DBG_TRACE_DEVEL("leaving on error",
1633 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001634 return 0;
1635 }
1636
Christopher Faulet9768c262018-10-22 09:34:31 +02001637 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001638 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001639 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001640 goto abort_keep_alive;
1641
Olivier Houcharda798bf52019-03-08 18:52:00 +01001642 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001643 if (objt_server(s->target))
1644 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001645 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001646
1647 if (!(s->flags & SF_ERR_MASK))
1648 s->flags |= SF_ERR_CLICL;
1649 if (!(s->flags & SF_FINST_MASK))
1650 s->flags |= SF_FINST_H;
1651
1652 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001653 DBG_TRACE_DEVEL("leaving on error",
1654 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001655 return 0;
1656 }
1657
1658 channel_dont_close(rep);
1659 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001660 DBG_TRACE_DEVEL("waiting for more data",
1661 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001662 return 0;
1663 }
1664
1665 /* More interesting part now : we know that we have a complete
1666 * response which at least looks like HTTP. We have an indicator
1667 * of each header's length, so we can parse them quickly.
1668 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001669 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001670 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001671 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001672
Christopher Faulet9768c262018-10-22 09:34:31 +02001673 /* 0: we might have to print this header in debug mode */
1674 if (unlikely((global.mode & MODE_DEBUG) &&
1675 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1676 int32_t pos;
1677
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001678 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001679
Christopher Fauleta3f15502019-05-13 15:27:23 +02001680 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001681 struct htx_blk *blk = htx_get_blk(htx, pos);
1682 enum htx_blk_type type = htx_get_blk_type(blk);
1683
1684 if (type == HTX_BLK_EOH)
1685 break;
1686 if (type != HTX_BLK_HDR)
1687 continue;
1688
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001689 http_debug_hdr("srvhdr", s,
1690 htx_get_blk_name(htx, blk),
1691 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001692 }
1693 }
1694
Christopher Faulet03599112018-11-27 11:21:21 +01001695 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001696 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001697 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001698 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001699 if (sl->flags & HTX_SL_F_XFER_LEN) {
1700 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001701 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001702 if (sl->flags & HTX_SL_F_BODYLESS)
1703 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001704 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001705
1706 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001707 if (n < 1 || n > 5)
1708 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001709
Christopher Faulete0768eb2018-10-03 16:38:02 +02001710 /* when the client triggers a 4xx from the server, it's most often due
1711 * to a missing object or permission. These events should be tracked
1712 * because if they happen often, it may indicate a brute force or a
1713 * vulnerability scan.
1714 */
1715 if (n == 4)
1716 stream_inc_http_err_ctr(s);
1717
1718 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001719 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001720
Christopher Faulete0768eb2018-10-03 16:38:02 +02001721 /* Adjust server's health based on status code. Note: status codes 501
1722 * and 505 are triggered on demand by client request, so we must not
1723 * count them as server failures.
1724 */
1725 if (objt_server(s->target)) {
1726 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001727 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001728 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001729 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001730 }
1731
1732 /*
1733 * We may be facing a 100-continue response, or any other informational
1734 * 1xx response which is non-final, in which case this is not the right
1735 * response, and we're waiting for the next one. Let's allow this response
1736 * to go to the client and wait for the next one. There's an exception for
1737 * 101 which is used later in the code to switch protocols.
1738 */
1739 if (txn->status < 200 &&
1740 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001741 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001742 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001743 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001744 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001745 txn->status = 0;
1746 s->logs.t_data = -1; /* was not a response yet */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001747 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001748 }
1749
1750 /*
1751 * 2: check for cacheability.
1752 */
1753
1754 switch (txn->status) {
1755 case 200:
1756 case 203:
1757 case 204:
1758 case 206:
1759 case 300:
1760 case 301:
1761 case 404:
1762 case 405:
1763 case 410:
1764 case 414:
1765 case 501:
1766 break;
1767 default:
1768 /* RFC7231#6.1:
1769 * Responses with status codes that are defined as
1770 * cacheable by default (e.g., 200, 203, 204, 206,
1771 * 300, 301, 404, 405, 410, 414, and 501 in this
1772 * specification) can be reused by a cache with
1773 * heuristic expiration unless otherwise indicated
1774 * by the method definition or explicit cache
1775 * controls [RFC7234]; all other status codes are
1776 * not cacheable by default.
1777 */
1778 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1779 break;
1780 }
1781
1782 /*
1783 * 3: we may need to capture headers
1784 */
1785 s->logs.logwait &= ~LW_RESP;
1786 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001787 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001788
Christopher Faulet9768c262018-10-22 09:34:31 +02001789 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001790 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1791 txn->status == 101)) {
1792 /* Either we've established an explicit tunnel, or we're
1793 * switching the protocol. In both cases, we're very unlikely
1794 * to understand the next protocols. We have to switch to tunnel
1795 * mode, so that we transfer the request and responses then let
1796 * this protocol pass unmodified. When we later implement specific
1797 * parsers for such protocols, we'll want to check the Upgrade
1798 * header which contains information about that protocol for
1799 * responses with status 101 (eg: see RFC2817 about TLS).
1800 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001801 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001802 }
1803
Christopher Faulet61608322018-11-23 16:23:45 +01001804 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1805 * 407 (Proxy-Authenticate) responses and set the connection to private
1806 */
1807 srv_conn = cs_conn(objt_cs(s->si[1].end));
1808 if (srv_conn) {
1809 struct ist hdr;
1810 struct http_hdr_ctx ctx;
1811
1812 if (txn->status == 401)
1813 hdr = ist("WWW-Authenticate");
1814 else if (txn->status == 407)
1815 hdr = ist("Proxy-Authenticate");
1816 else
1817 goto end;
1818
1819 ctx.blk = NULL;
1820 while (http_find_header(htx, hdr, &ctx, 0)) {
1821 if ((ctx.value.len >= 9 && word_match(ctx.value.ptr, ctx.value.len, "Negotiate", 9)) ||
Olivier Houchard250031e2019-05-29 15:01:50 +02001822 (ctx.value.len >= 4 && word_match(ctx.value.ptr, ctx.value.len, "NTLM", 4))) {
1823 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet61608322018-11-23 16:23:45 +01001824 srv_conn->flags |= CO_FL_PRIVATE;
Olivier Houchard250031e2019-05-29 15:01:50 +02001825 }
Christopher Faulet61608322018-11-23 16:23:45 +01001826 }
1827 }
1828
1829 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001830 /* we want to have the response time before we start processing it */
1831 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1832
1833 /* end of job, return OK */
1834 rep->analysers &= ~an_bit;
1835 rep->analyse_exp = TICK_ETERNITY;
1836 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001837 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001838 return 1;
1839
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001840 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001841 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001842 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001843 if (sess->listener->counters)
1844 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001845 if (objt_server(s->target))
1846 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001847 txn->status = 500;
1848 if (!(s->flags & SF_ERR_MASK))
1849 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001850 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001851
1852 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001853 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001854 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001855 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001856 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001857 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001858 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001859 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001860 do_l7_retry(s, si_b) == 0) {
1861 DBG_TRACE_DEVEL("leaving on L7 retry",
1862 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001863 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001864 }
Christopher Faulet47365272018-10-31 17:40:50 +01001865 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001866 /* fall through */
1867
Christopher Fauletb8a53712019-12-16 11:29:38 +01001868 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001869 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001870
1871 if (!(s->flags & SF_ERR_MASK))
1872 s->flags |= SF_ERR_PRXCOND;
1873 if (!(s->flags & SF_FINST_MASK))
1874 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001875
1876 s->si[1].flags |= SI_FL_NOLINGER;
1877 rep->analysers &= AN_RES_FLT_END;
1878 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001879 DBG_TRACE_DEVEL("leaving on error",
1880 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001881 return 0;
1882
Christopher Faulete0768eb2018-10-03 16:38:02 +02001883 abort_keep_alive:
1884 /* A keep-alive request to the server failed on a network error.
1885 * The client is required to retry. We need to close without returning
1886 * any other information so that the client retries.
1887 */
1888 txn->status = 0;
1889 rep->analysers &= AN_RES_FLT_END;
1890 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001891 s->logs.logwait = 0;
1892 s->logs.level = 0;
1893 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001894 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001895 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1896 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001897 return 0;
1898}
1899
1900/* This function performs all the processing enabled for the current response.
1901 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1902 * and updates s->res.analysers. It might make sense to explode it into several
1903 * other functions. It works like process_request (see indications above).
1904 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001905int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001906{
1907 struct session *sess = s->sess;
1908 struct http_txn *txn = s->txn;
1909 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001910 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001911 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001912 enum rule_result ret = HTTP_RULE_RES_CONT;
1913
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001914 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1915 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001916
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001917 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001918
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001919 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001920
1921 /* The stats applet needs to adjust the Connection header but we don't
1922 * apply any filter there.
1923 */
1924 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1925 rep->analysers &= ~an_bit;
1926 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001927 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001928 }
1929
1930 /*
1931 * We will have to evaluate the filters.
1932 * As opposed to version 1.2, now they will be evaluated in the
1933 * filters order and not in the header order. This means that
1934 * each filter has to be validated among all headers.
1935 *
1936 * Filters are tried with ->be first, then with ->fe if it is
1937 * different from ->be.
1938 *
1939 * Maybe we are in resume condiion. In this case I choose the
1940 * "struct proxy" which contains the rule list matching the resume
1941 * pointer. If none of theses "struct proxy" match, I initialise
1942 * the process with the first one.
1943 *
1944 * In fact, I check only correspondance betwwen the current list
1945 * pointer and the ->fe rule list. If it doesn't match, I initialize
1946 * the loop with the ->be.
1947 */
1948 if (s->current_rule_list == &sess->fe->http_res_rules)
1949 cur_proxy = sess->fe;
1950 else
1951 cur_proxy = s->be;
1952 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001953 /* evaluate http-response rules */
1954 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001955 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001956
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001957 switch (ret) {
1958 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1959 goto return_prx_yield;
1960
1961 case HTTP_RULE_RES_CONT:
1962 case HTTP_RULE_RES_STOP: /* nothing to do */
1963 break;
1964
1965 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1966 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001967
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001968 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1969 goto return_prx_cond;
1970
1971 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001972 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001973
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001974 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1975 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001976
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001977 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1978 goto return_int_err;
1979 }
1980
1981 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001982
Christopher Faulete0768eb2018-10-03 16:38:02 +02001983 /* check whether we're already working on the frontend */
1984 if (cur_proxy == sess->fe)
1985 break;
1986 cur_proxy = sess->fe;
1987 }
1988
1989 /* After this point, this anayzer can't return yield, so we can
1990 * remove the bit corresponding to this analyzer from the list.
1991 *
1992 * Note that the intermediate returns and goto found previously
1993 * reset the analyzers.
1994 */
1995 rep->analysers &= ~an_bit;
1996 rep->analyse_exp = TICK_ETERNITY;
1997
1998 /* OK that's all we can do for 1xx responses */
1999 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002000 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002001
2002 /*
2003 * Now check for a server cookie.
2004 */
2005 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002006 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002007
2008 /*
2009 * Check for cache-control or pragma headers if required.
2010 */
2011 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002012 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002013
2014 /*
2015 * Add server cookie in the response if needed
2016 */
2017 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
2018 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
2019 (!(s->flags & SF_DIRECT) ||
2020 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
2021 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
2022 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
2023 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
2024 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
2025 !(s->flags & SF_IGNORE_PRST)) {
2026 /* the server is known, it's not the one the client requested, or the
2027 * cookie's last seen date needs to be refreshed. We have to
2028 * insert a set-cookie here, except if we want to insert only on POST
2029 * requests and this one isn't. Note that servers which don't have cookies
2030 * (eg: some backup servers) will return a full cookie removal request.
2031 */
2032 if (!objt_server(s->target)->cookie) {
2033 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002034 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02002035 s->be->cookie_name);
2036 }
2037 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002038 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002039
2040 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2041 /* emit last_date, which is mandatory */
2042 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2043 s30tob64((date.tv_sec+3) >> 2,
2044 trash.area + trash.data);
2045 trash.data += 5;
2046
2047 if (s->be->cookie_maxlife) {
2048 /* emit first_date, which is either the original one or
2049 * the current date.
2050 */
2051 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2052 s30tob64(txn->cookie_first_date ?
2053 txn->cookie_first_date >> 2 :
2054 (date.tv_sec+3) >> 2,
2055 trash.area + trash.data);
2056 trash.data += 5;
2057 }
2058 }
2059 chunk_appendf(&trash, "; path=/");
2060 }
2061
2062 if (s->be->cookie_domain)
2063 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2064
2065 if (s->be->ck_opts & PR_CK_HTTPONLY)
2066 chunk_appendf(&trash, "; HttpOnly");
2067
2068 if (s->be->ck_opts & PR_CK_SECURE)
2069 chunk_appendf(&trash, "; Secure");
2070
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002071 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002072 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002073
2074 txn->flags &= ~TX_SCK_MASK;
2075 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2076 /* the server did not change, only the date was updated */
2077 txn->flags |= TX_SCK_UPDATED;
2078 else
2079 txn->flags |= TX_SCK_INSERTED;
2080
2081 /* Here, we will tell an eventual cache on the client side that we don't
2082 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2083 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2084 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2085 */
2086 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2087
2088 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2089
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002090 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002091 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002092 }
2093 }
2094
2095 /*
2096 * Check if result will be cacheable with a cookie.
2097 * We'll block the response if security checks have caught
2098 * nasty things such as a cacheable cookie.
2099 */
2100 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2101 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2102 (s->be->options & PR_O_CHK_CACHE)) {
2103 /* we're in presence of a cacheable response containing
2104 * a set-cookie header. We'll block it as requested by
2105 * the 'checkcache' option, and send an alert.
2106 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002107 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2108 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2109 send_log(s->be, LOG_ALERT,
2110 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2111 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002112 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002113 }
2114
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002115 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002116 /* Always enter in the body analyzer */
2117 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2118 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2119
2120 /* if the user wants to log as soon as possible, without counting
2121 * bytes from the server, then this is the right moment. We have
2122 * to temporarily assign bytes_out to log what we currently have.
2123 */
2124 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2125 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002126 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002127 s->do_log(s);
2128 s->logs.bytes_out = 0;
2129 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002130 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002131 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002132
Christopher Fauletb8a53712019-12-16 11:29:38 +01002133 done:
2134 rep->analysers &= ~an_bit;
2135 rep->analyse_exp = TICK_ETERNITY;
2136 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002137
Christopher Fauletb8a53712019-12-16 11:29:38 +01002138 deny:
Christopher Fauletb8a53712019-12-16 11:29:38 +01002139 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002140 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002141 if (sess->listener->counters)
2142 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002143 if (objt_server(s->target))
2144 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002145 goto return_prx_err;
2146
2147 return_int_err:
2148 txn->status = 500;
2149 if (!(s->flags & SF_ERR_MASK))
2150 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002151 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002152 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2153 if (objt_server(s->target))
2154 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002155 if (objt_server(s->target))
2156 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002157 goto return_prx_err;
2158
2159 return_bad_res:
2160 txn->status = 502;
2161 /* fall through */
2162
2163 return_prx_err:
2164 http_reply_and_close(s, txn->status, http_error_message(s));
2165 /* fall through */
2166
2167 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002168 s->logs.t_data = -1; /* was not a valid response */
2169 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002170
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002171 if (!(s->flags & SF_ERR_MASK))
2172 s->flags |= SF_ERR_PRXCOND;
2173 if (!(s->flags & SF_FINST_MASK))
2174 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002175
2176 rep->analysers &= ~an_bit;
2177 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002178 DBG_TRACE_DEVEL("leaving on error",
2179 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002180 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002181
2182 return_prx_yield:
2183 channel_dont_close(rep);
2184 DBG_TRACE_DEVEL("waiting for more data",
2185 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2186 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002187}
2188
2189/* This function is an analyser which forwards response body (including chunk
2190 * sizes if any). It is called as soon as we must forward, even if we forward
2191 * zero byte. The only situation where it must not be called is when we're in
2192 * tunnel mode and we want to forward till the close. It's used both to forward
2193 * remaining data and to resync after end of body. It expects the msg_state to
2194 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2195 * read more data, or 1 once we can go on with next request or end the stream.
2196 *
2197 * It is capable of compressing response data both in content-length mode and
2198 * in chunked mode. The state machines follows different flows depending on
2199 * whether content-length and chunked modes are used, since there are no
2200 * trailers in content-length :
2201 *
2202 * chk-mode cl-mode
2203 * ,----- BODY -----.
2204 * / \
2205 * V size > 0 V chk-mode
2206 * .--> SIZE -------------> DATA -------------> CRLF
2207 * | | size == 0 | last byte |
2208 * | v final crlf v inspected |
2209 * | TRAILERS -----------> DONE |
2210 * | |
2211 * `----------------------------------------------'
2212 *
2213 * Compression only happens in the DATA state, and must be flushed in final
2214 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2215 * is performed at once on final states for all bytes parsed, or when leaving
2216 * on missing data.
2217 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002218int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002219{
2220 struct session *sess = s->sess;
2221 struct http_txn *txn = s->txn;
2222 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002223 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002224 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002225
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002226 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002227
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002228 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002229
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002230 if (htx->flags & HTX_FL_PARSING_ERROR)
2231 goto return_bad_res;
2232 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2233 goto return_int_err;
2234
Christopher Faulete0768eb2018-10-03 16:38:02 +02002235 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002236 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002237 /* Output closed while we were sending data. We must abort and
2238 * wake the other side up.
2239 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002240 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002241 http_end_response(s);
2242 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002243 DBG_TRACE_DEVEL("leaving on error",
2244 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002245 return 1;
2246 }
2247
Christopher Faulet9768c262018-10-22 09:34:31 +02002248 if (msg->msg_state == HTTP_MSG_BODY)
2249 msg->msg_state = HTTP_MSG_DATA;
2250
Christopher Faulete0768eb2018-10-03 16:38:02 +02002251 /* in most states, we should abort in case of early close */
2252 channel_auto_close(res);
2253
Christopher Faulete0768eb2018-10-03 16:38:02 +02002254 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002255 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002256 if (res->flags & CF_EOI)
2257 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002258 }
2259 else {
2260 /* We can't process the buffer's contents yet */
2261 res->flags |= CF_WAKE_WRITE;
2262 goto missing_data_or_waiting;
2263 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002264 }
2265
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002266 if (msg->msg_state >= HTTP_MSG_ENDING)
2267 goto ending;
2268
2269 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2270 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2271 msg->msg_state = HTTP_MSG_ENDING;
2272 goto ending;
2273 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002274
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002275 /* Forward input data. We get it by removing all outgoing data not
2276 * forwarded yet from HTX data size. If there are some data filters, we
2277 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002278 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002279 if (HAS_RSP_DATA_FILTERS(s)) {
2280 ret = flt_http_payload(s, msg, htx->data);
2281 if (ret < 0)
2282 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002283 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002284 }
2285 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002286 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002287 if (msg->flags & HTTP_MSGF_XFER_LEN)
2288 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002289 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002290
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002291 if (htx->data != co_data(res))
2292 goto missing_data_or_waiting;
2293
2294 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2295 msg->msg_state = HTTP_MSG_ENDING;
2296 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002297 }
2298
Christopher Faulet9768c262018-10-22 09:34:31 +02002299 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002300 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2301 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002302 */
2303 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2304 goto missing_data_or_waiting;
2305
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002306 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002307
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002308 ending:
2309 /* other states, ENDING...TUNNEL */
2310 if (msg->msg_state >= HTTP_MSG_DONE)
2311 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002312
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002313 if (HAS_RSP_DATA_FILTERS(s)) {
2314 ret = flt_http_end(s, msg);
2315 if (ret <= 0) {
2316 if (!ret)
2317 goto missing_data_or_waiting;
2318 goto return_bad_res;
2319 }
2320 }
2321
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002322 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2323 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2324 msg->msg_state = HTTP_MSG_TUNNEL;
2325 goto ending;
2326 }
2327 else {
2328 msg->msg_state = HTTP_MSG_DONE;
2329 res->to_forward = 0;
2330 }
2331
2332 done:
2333
2334 channel_dont_close(res);
2335
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002336 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002337 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002338 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002339 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2340 if (res->flags & CF_SHUTW) {
2341 /* response errors are most likely due to the
2342 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002343 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002344 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002345 goto return_bad_res;
2346 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002347 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002348 return 1;
2349 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002350 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2351 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002352 return 0;
2353
2354 missing_data_or_waiting:
2355 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002356 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002357
2358 /* stop waiting for data if the input is closed before the end. If the
2359 * client side was already closed, it means that the client has aborted,
2360 * so we don't want to count this as a server abort. Otherwise it's a
2361 * server abort.
2362 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002363 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002364 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002365 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002366 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002367 if (htx_is_empty(htx))
2368 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002369 }
2370
Christopher Faulete0768eb2018-10-03 16:38:02 +02002371 /* When TE: chunked is used, we need to get there again to parse
2372 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002373 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2374 * are filters registered on the stream, we don't want to forward a
2375 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002376 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002377 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002378 channel_dont_close(res);
2379
2380 /* We know that more data are expected, but we couldn't send more that
2381 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2382 * system knows it must not set a PUSH on this first part. Interactive
2383 * modes are already handled by the stream sock layer. We must not do
2384 * this in content-length mode because it could present the MSG_MORE
2385 * flag with the last block of forwarded data, which would cause an
2386 * additional delay to be observed by the receiver.
2387 */
2388 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2389 res->flags |= CF_EXPECT_MORE;
2390
2391 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002392 DBG_TRACE_DEVEL("waiting for more data to forward",
2393 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002394 return 0;
2395
Christopher Faulet93e02d82019-03-08 14:18:50 +01002396 return_srv_abort:
2397 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2398 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002399 if (sess->listener->counters)
2400 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002401 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002402 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002403 if (!(s->flags & SF_ERR_MASK))
2404 s->flags |= SF_ERR_SRVCL;
2405 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002406
Christopher Faulet93e02d82019-03-08 14:18:50 +01002407 return_cli_abort:
2408 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2409 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002410 if (sess->listener->counters)
2411 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002412 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002413 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002414 if (!(s->flags & SF_ERR_MASK))
2415 s->flags |= SF_ERR_CLICL;
2416 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002417
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002418 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002419 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002420 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002421 if (sess->listener->counters)
2422 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002423 if (objt_server(s->target))
2424 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002425 if (!(s->flags & SF_ERR_MASK))
2426 s->flags |= SF_ERR_INTERNAL;
2427 goto return_error;
2428
Christopher Faulet93e02d82019-03-08 14:18:50 +01002429 return_bad_res:
2430 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2431 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002432 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002433 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2434 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002435 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002436 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002437 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002438
Christopher Faulet93e02d82019-03-08 14:18:50 +01002439 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002440 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002441 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002442 res->analysers &= AN_RES_FLT_END;
2443 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 +02002444 if (!(s->flags & SF_FINST_MASK))
2445 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002446 DBG_TRACE_DEVEL("leaving on error",
2447 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002448 return 0;
2449}
2450
Christopher Fauletf2824e62018-10-01 12:12:37 +02002451/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002452 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002453 * as too large a request to build a valid response.
2454 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002455int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002456{
Christopher Faulet99daf282018-11-28 22:58:13 +01002457 struct channel *req = &s->req;
2458 struct channel *res = &s->res;
2459 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002460 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002461 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002462 struct ist status, reason, location;
2463 unsigned int flags;
2464 size_t data;
Christopher Faulet08e66462019-05-23 16:44:59 +02002465 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002466
2467 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002468 if (!chunk) {
2469 if (!(s->flags & SF_ERR_MASK))
2470 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002471 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002472 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002473
Christopher Faulet99daf282018-11-28 22:58:13 +01002474 /*
2475 * Create the location
2476 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002477 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002478 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002479 case REDIRECT_TYPE_SCHEME: {
2480 struct http_hdr_ctx ctx;
2481 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002482
Christopher Faulet99daf282018-11-28 22:58:13 +01002483 host = ist("");
2484 ctx.blk = NULL;
2485 if (http_find_header(htx, ist("Host"), &ctx, 0))
2486 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002487
Christopher Faulet297fbb42019-05-13 14:41:27 +02002488 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002489 path = http_get_path(htx_sl_req_uri(sl));
2490 /* build message using path */
2491 if (path.ptr) {
2492 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2493 int qs = 0;
2494 while (qs < path.len) {
2495 if (*(path.ptr + qs) == '?') {
2496 path.len = qs;
2497 break;
2498 }
2499 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002500 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002501 }
2502 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002503 else
2504 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002505
Christopher Faulet99daf282018-11-28 22:58:13 +01002506 if (rule->rdr_str) { /* this is an old "redirect" rule */
2507 /* add scheme */
2508 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2509 goto fail;
2510 }
2511 else {
2512 /* add scheme with executing log format */
2513 chunk->data += build_logline(s, chunk->area + chunk->data,
2514 chunk->size - chunk->data,
2515 &rule->rdr_fmt);
2516 }
2517 /* add "://" + host + path */
2518 if (!chunk_memcat(chunk, "://", 3) ||
2519 !chunk_memcat(chunk, host.ptr, host.len) ||
2520 !chunk_memcat(chunk, path.ptr, path.len))
2521 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002522
Christopher Faulet99daf282018-11-28 22:58:13 +01002523 /* append a slash at the end of the location if needed and missing */
2524 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2525 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2526 if (chunk->data + 1 >= chunk->size)
2527 goto fail;
2528 chunk->area[chunk->data++] = '/';
2529 }
2530 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002531 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002532
Christopher Faulet99daf282018-11-28 22:58:13 +01002533 case REDIRECT_TYPE_PREFIX: {
2534 struct ist path;
2535
Christopher Faulet297fbb42019-05-13 14:41:27 +02002536 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002537 path = http_get_path(htx_sl_req_uri(sl));
2538 /* build message using path */
2539 if (path.ptr) {
2540 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2541 int qs = 0;
2542 while (qs < path.len) {
2543 if (*(path.ptr + qs) == '?') {
2544 path.len = qs;
2545 break;
2546 }
2547 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002548 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002549 }
2550 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002551 else
2552 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002553
Christopher Faulet99daf282018-11-28 22:58:13 +01002554 if (rule->rdr_str) { /* this is an old "redirect" rule */
2555 /* add prefix. Note that if prefix == "/", we don't want to
2556 * add anything, otherwise it makes it hard for the user to
2557 * configure a self-redirection.
2558 */
2559 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2560 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2561 goto fail;
2562 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002563 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002564 else {
2565 /* add prefix with executing log format */
2566 chunk->data += build_logline(s, chunk->area + chunk->data,
2567 chunk->size - chunk->data,
2568 &rule->rdr_fmt);
2569 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002570
Christopher Faulet99daf282018-11-28 22:58:13 +01002571 /* add path */
2572 if (!chunk_memcat(chunk, path.ptr, path.len))
2573 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002574
Christopher Faulet99daf282018-11-28 22:58:13 +01002575 /* append a slash at the end of the location if needed and missing */
2576 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2577 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2578 if (chunk->data + 1 >= chunk->size)
2579 goto fail;
2580 chunk->area[chunk->data++] = '/';
2581 }
2582 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002583 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002584 case REDIRECT_TYPE_LOCATION:
2585 default:
2586 if (rule->rdr_str) { /* this is an old "redirect" rule */
2587 /* add location */
2588 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2589 goto fail;
2590 }
2591 else {
2592 /* add location with executing log format */
2593 chunk->data += build_logline(s, chunk->area + chunk->data,
2594 chunk->size - chunk->data,
2595 &rule->rdr_fmt);
2596 }
2597 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002598 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002599 location = ist2(chunk->area, chunk->data);
2600
2601 /*
2602 * Create the 30x response
2603 */
2604 switch (rule->code) {
2605 case 308:
2606 status = ist("308");
2607 reason = ist("Permanent Redirect");
2608 break;
2609 case 307:
2610 status = ist("307");
2611 reason = ist("Temporary Redirect");
2612 break;
2613 case 303:
2614 status = ist("303");
2615 reason = ist("See Other");
2616 break;
2617 case 301:
2618 status = ist("301");
2619 reason = ist("Moved Permanently");
2620 break;
2621 case 302:
2622 default:
2623 status = ist("302");
2624 reason = ist("Found");
2625 break;
2626 }
2627
Christopher Faulet08e66462019-05-23 16:44:59 +02002628 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2629 close = 1;
2630
Christopher Faulet99daf282018-11-28 22:58:13 +01002631 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002632 /* Trim any possible response */
2633 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002634 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2635 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2636 if (!sl)
2637 goto fail;
2638 sl->info.res.status = rule->code;
2639 s->txn->status = rule->code;
2640
Christopher Faulet08e66462019-05-23 16:44:59 +02002641 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2642 goto fail;
2643
2644 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002645 !htx_add_header(htx, ist("Location"), location))
2646 goto fail;
2647
2648 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2649 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2650 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002651 }
2652
2653 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002654 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2655 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002656 }
2657
Christopher Faulet99daf282018-11-28 22:58:13 +01002658 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2659 goto fail;
2660
Kevin Zhu96b36392020-01-07 09:42:55 +01002661 htx_to_buf(htx, &res->buf);
2662
Christopher Fauletf2824e62018-10-01 12:12:37 +02002663 /* let's log the request time */
2664 s->logs.tv_request = now;
2665
Christopher Faulet06511812019-10-16 09:38:27 +02002666 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet99daf282018-11-28 22:58:13 +01002667 data = htx->data - co_data(res);
Christopher Faulet99daf282018-11-28 22:58:13 +01002668 c_adv(res, data);
2669 res->total += data;
2670
2671 channel_auto_read(req);
2672 channel_abort(req);
2673 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002674 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet99daf282018-11-28 22:58:13 +01002675
2676 res->wex = tick_add_ifset(now_ms, res->wto);
2677 channel_auto_read(res);
2678 channel_auto_close(res);
2679 channel_shutr_now(res);
2680
2681 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002682
2683 if (!(s->flags & SF_ERR_MASK))
2684 s->flags |= SF_ERR_LOCAL;
2685 if (!(s->flags & SF_FINST_MASK))
2686 s->flags |= SF_FINST_R;
2687
Christopher Faulet99daf282018-11-28 22:58:13 +01002688 free_trash_chunk(chunk);
2689 return 1;
2690
2691 fail:
2692 /* If an error occurred, remove the incomplete HTTP response from the
2693 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002694 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002695 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002696 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002697}
2698
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002699/* Replace all headers matching the name <name>. The header value is replaced if
2700 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2701 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2702 * values are evaluated one by one. It returns 0 on success and -1 on error.
2703 */
2704int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2705 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002706{
2707 struct http_hdr_ctx ctx;
2708 struct buffer *output = get_trash_chunk();
2709
Christopher Faulet72333522018-10-24 11:25:02 +02002710 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002711 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002712 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2713 continue;
2714
2715 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2716 if (output->data == -1)
2717 return -1;
2718 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2719 return -1;
2720 }
2721 return 0;
2722}
2723
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002724/* This function executes one of the set-{method,path,query,uri} actions. It
2725 * takes the string from the variable 'replace' with length 'len', then modifies
2726 * the relevant part of the request line accordingly. Then it updates various
2727 * pointers to the next elements which were moved, and the total buffer length.
2728 * It finds the action to be performed in p[2], previously filled by function
2729 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2730 * error, though this can be revisited when this code is finally exploited.
2731 *
2732 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2733 * query string and 3 to replace uri.
2734 *
2735 * In query string case, the mark question '?' must be set at the start of the
2736 * string by the caller, event if the replacement query string is empty.
2737 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002738int http_req_replace_stline(int action, const char *replace, int len,
2739 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002740{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002741 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002742
2743 switch (action) {
2744 case 0: // method
2745 if (!http_replace_req_meth(htx, ist2(replace, len)))
2746 return -1;
2747 break;
2748
2749 case 1: // path
2750 if (!http_replace_req_path(htx, ist2(replace, len)))
2751 return -1;
2752 break;
2753
2754 case 2: // query
2755 if (!http_replace_req_query(htx, ist2(replace, len)))
2756 return -1;
2757 break;
2758
2759 case 3: // uri
2760 if (!http_replace_req_uri(htx, ist2(replace, len)))
2761 return -1;
2762 break;
2763
2764 default:
2765 return -1;
2766 }
2767 return 0;
2768}
2769
2770/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002771 * variable <status> contains the new status code. This function never fails. It
2772 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002773 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002774int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002775{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002776 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002777 char *res;
2778
2779 chunk_reset(&trash);
2780 res = ultoa_o(status, trash.area, trash.size);
2781 trash.data = res - trash.area;
2782
2783 /* Do we have a custom reason format string? */
Christopher Faulet96bff762019-12-17 13:46:18 +01002784 if (reason.ptr == NULL) {
2785 const char *str = http_get_reason(status);
2786 reason = ist2(str, strlen(str));
2787 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002788
Christopher Faulete00d06c2019-12-16 17:18:42 +01002789 if (!http_replace_res_status(htx, ist2(trash.area, trash.data)))
2790 return -1;
Christopher Faulet96bff762019-12-17 13:46:18 +01002791 if (!http_replace_res_reason(htx, reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002792 return -1;
2793 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002794}
2795
Christopher Faulet3e964192018-10-24 11:39:23 +02002796/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2797 * transaction <txn>. Returns the verdict of the first rule that prevents
2798 * further processing of the request (auth, deny, ...), and defaults to
2799 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2800 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2801 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2802 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2803 * status.
2804 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002805static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002806 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002807{
2808 struct session *sess = strm_sess(s);
2809 struct http_txn *txn = s->txn;
2810 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002811 struct act_rule *rule;
2812 struct http_hdr_ctx ctx;
2813 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002814 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002815 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002816
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002817 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002818
2819 /* If "the current_rule_list" match the executed rule list, we are in
2820 * resume condition. If a resume is needed it is always in the action
2821 * and never in the ACL or converters. In this case, we initialise the
2822 * current rule, and go to the action execution point.
2823 */
2824 if (s->current_rule) {
2825 rule = s->current_rule;
2826 s->current_rule = NULL;
2827 if (s->current_rule_list == rules)
2828 goto resume_execution;
2829 }
2830 s->current_rule_list = rules;
2831
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002832 /* start the ruleset evaluation in strict mode */
2833 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002834
Christopher Faulet3e964192018-10-24 11:39:23 +02002835 list_for_each_entry(rule, rules, list) {
2836 /* check optional condition */
2837 if (rule->cond) {
2838 int ret;
2839
2840 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2841 ret = acl_pass(ret);
2842
2843 if (rule->cond->pol == ACL_COND_UNLESS)
2844 ret = !ret;
2845
2846 if (!ret) /* condition not matched */
2847 continue;
2848 }
2849
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002850 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002851 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002852 /* Always call the action function if defined */
2853 if (rule->action_ptr) {
2854 if ((s->req.flags & CF_READ_ERROR) ||
2855 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2856 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002857 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002858
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002859 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002860 case ACT_RET_CONT:
2861 break;
2862 case ACT_RET_STOP:
2863 rule_ret = HTTP_RULE_RES_STOP;
2864 goto end;
2865 case ACT_RET_YIELD:
2866 s->current_rule = rule;
2867 rule_ret = HTTP_RULE_RES_YIELD;
2868 goto end;
2869 case ACT_RET_ERR:
2870 rule_ret = HTTP_RULE_RES_ERROR;
2871 goto end;
2872 case ACT_RET_DONE:
2873 rule_ret = HTTP_RULE_RES_DONE;
2874 goto end;
2875 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002876 txn->flags |= TX_CLDENY;
2877 if (txn->status == -1)
2878 txn->status = 403;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002879 rule_ret = HTTP_RULE_RES_DENY;
2880 goto end;
2881 case ACT_RET_ABRT:
2882 rule_ret = HTTP_RULE_RES_ABRT;
2883 goto end;
2884 case ACT_RET_INV:
2885 rule_ret = HTTP_RULE_RES_BADREQ;
2886 goto end;
2887 }
2888 continue; /* eval the next rule */
2889 }
2890
2891 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002892 switch (rule->action) {
2893 case ACT_ACTION_ALLOW:
2894 rule_ret = HTTP_RULE_RES_STOP;
2895 goto end;
2896
2897 case ACT_ACTION_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01002898 txn->flags |= TX_CLDENY;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01002899 txn->status = rule->arg.http_deny.status;
2900 if (rule->arg.http_deny.errmsg)
2901 txn->errmsg = rule->arg.http_deny.errmsg;
Christopher Faulet3e964192018-10-24 11:39:23 +02002902 rule_ret = HTTP_RULE_RES_DENY;
2903 goto end;
2904
2905 case ACT_HTTP_REQ_TARPIT:
2906 txn->flags |= TX_CLTARPIT;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01002907 txn->status = rule->arg.http_deny.status;
2908 if (rule->arg.http_deny.errmsg)
2909 txn->errmsg = rule->arg.http_deny.errmsg;
Christopher Faulet3e964192018-10-24 11:39:23 +02002910 rule_ret = HTTP_RULE_RES_DENY;
2911 goto end;
2912
2913 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002914 /* Auth might be performed on regular http-req rules as well as on stats */
Christopher Faulet96bff762019-12-17 13:46:18 +01002915 auth_realm = rule->arg.http.str.ptr;
Christopher Faulet3e964192018-10-24 11:39:23 +02002916 if (!auth_realm) {
2917 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2918 auth_realm = STATS_DEFAULT_REALM;
2919 else
2920 auth_realm = px->id;
2921 }
2922 /* send 401/407 depending on whether we use a proxy or not. We still
2923 * count one error, because normal browsing won't significantly
2924 * increase the counter but brute force attempts will.
2925 */
Christopher Faulet3e964192018-10-24 11:39:23 +02002926 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002927 if (http_reply_40x_unauthorized(s, auth_realm) == -1)
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002928 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet12c51e22018-11-28 15:59:42 +01002929 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02002930 goto end;
2931
2932 case ACT_HTTP_REDIR:
Christopher Faulet3e964192018-10-24 11:39:23 +02002933 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002934 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002935 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002936 goto end;
2937
2938 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002939 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002940 break;
2941
2942 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002943 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002944 break;
2945
2946 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002947 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002948 break;
2949
2950 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002951 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002952 break;
2953
Christopher Faulet3e964192018-10-24 11:39:23 +02002954 case ACT_HTTP_DEL_HDR:
2955 /* remove all occurrences of the header */
2956 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01002957 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02002958 http_remove_header(htx, &ctx);
2959 break;
2960
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002961 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002962 default:
2963 break;
2964 }
2965 }
2966
2967 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002968 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002969 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002970 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002971
Christopher Faulet3e964192018-10-24 11:39:23 +02002972 /* we reached the end of the rules, nothing to report */
2973 return rule_ret;
2974}
2975
2976/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2977 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2978 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2979 * is returned, the process can continue the evaluation of next rule list. If
2980 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2981 * is returned, it means the operation could not be processed and a server error
2982 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2983 * deny rule. If *YIELD is returned, the caller must call again the function
2984 * with the same context.
2985 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002986static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2987 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002988{
2989 struct session *sess = strm_sess(s);
2990 struct http_txn *txn = s->txn;
2991 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002992 struct act_rule *rule;
2993 struct http_hdr_ctx ctx;
2994 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002995 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002996
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002997 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002998
2999 /* If "the current_rule_list" match the executed rule list, we are in
3000 * resume condition. If a resume is needed it is always in the action
3001 * and never in the ACL or converters. In this case, we initialise the
3002 * current rule, and go to the action execution point.
3003 */
3004 if (s->current_rule) {
3005 rule = s->current_rule;
3006 s->current_rule = NULL;
3007 if (s->current_rule_list == rules)
3008 goto resume_execution;
3009 }
3010 s->current_rule_list = rules;
3011
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003012 /* start the ruleset evaluation in strict mode */
3013 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003014
Christopher Faulet3e964192018-10-24 11:39:23 +02003015 list_for_each_entry(rule, rules, list) {
3016 /* check optional condition */
3017 if (rule->cond) {
3018 int ret;
3019
3020 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3021 ret = acl_pass(ret);
3022
3023 if (rule->cond->pol == ACL_COND_UNLESS)
3024 ret = !ret;
3025
3026 if (!ret) /* condition not matched */
3027 continue;
3028 }
3029
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003030 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02003031resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003032
3033 /* Always call the action function if defined */
3034 if (rule->action_ptr) {
3035 if ((s->req.flags & CF_READ_ERROR) ||
3036 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3037 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003038 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003039
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003040 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003041 case ACT_RET_CONT:
3042 break;
3043 case ACT_RET_STOP:
3044 rule_ret = HTTP_RULE_RES_STOP;
3045 goto end;
3046 case ACT_RET_YIELD:
3047 s->current_rule = rule;
3048 rule_ret = HTTP_RULE_RES_YIELD;
3049 goto end;
3050 case ACT_RET_ERR:
3051 rule_ret = HTTP_RULE_RES_ERROR;
3052 goto end;
3053 case ACT_RET_DONE:
3054 rule_ret = HTTP_RULE_RES_DONE;
3055 goto end;
3056 case ACT_RET_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01003057 txn->flags |= TX_CLDENY;
3058 if (txn->status == -1)
3059 txn->status = 502;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003060 rule_ret = HTTP_RULE_RES_DENY;
3061 goto end;
3062 case ACT_RET_ABRT:
3063 rule_ret = HTTP_RULE_RES_ABRT;
3064 goto end;
3065 case ACT_RET_INV:
3066 rule_ret = HTTP_RULE_RES_BADREQ;
3067 goto end;
3068 }
3069 continue; /* eval the next rule */
3070 }
3071
3072 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003073 switch (rule->action) {
3074 case ACT_ACTION_ALLOW:
3075 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3076 goto end;
3077
3078 case ACT_ACTION_DENY:
Christopher Fauletb58f62b2020-01-13 16:40:13 +01003079 txn->flags |= TX_CLDENY;
Christopher Faulet554c0eb2020-01-14 12:00:28 +01003080 txn->status = rule->arg.http_deny.status;
3081 if (rule->arg.http_deny.errmsg)
3082 txn->errmsg = rule->arg.http_deny.errmsg;
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003083 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003084 goto end;
3085
3086 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003087 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003088 break;
3089
3090 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003091 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003092 break;
3093
3094 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003095 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003096 break;
3097
3098 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003099 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003100 break;
3101
Christopher Faulet3e964192018-10-24 11:39:23 +02003102 case ACT_HTTP_DEL_HDR:
3103 /* remove all occurrences of the header */
3104 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01003105 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02003106 http_remove_header(htx, &ctx);
3107 break;
3108
Christopher Faulet3e964192018-10-24 11:39:23 +02003109 case ACT_HTTP_REDIR:
3110 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003111 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003112 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003113 goto end;
3114
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003115 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003116 default:
3117 break;
3118 }
3119 }
3120
3121 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003122 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003123 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003124 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003125
Christopher Faulet3e964192018-10-24 11:39:23 +02003126 /* we reached the end of the rules, nothing to report */
3127 return rule_ret;
3128}
3129
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003130/*
3131 * Manage client-side cookie. It can impact performance by about 2% so it is
3132 * desirable to call it only when needed. This code is quite complex because
3133 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3134 * highly recommended not to touch this part without a good reason !
3135 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003136static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003137{
3138 struct session *sess = s->sess;
3139 struct http_txn *txn = s->txn;
3140 struct htx *htx;
3141 struct http_hdr_ctx ctx;
3142 char *hdr_beg, *hdr_end, *del_from;
3143 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3144 int preserve_hdr;
3145
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003146 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003147 ctx.blk = NULL;
3148 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003149 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003150 del_from = NULL; /* nothing to be deleted */
3151 preserve_hdr = 0; /* assume we may kill the whole header */
3152
3153 /* Now look for cookies. Conforming to RFC2109, we have to support
3154 * attributes whose name begin with a '$', and associate them with
3155 * the right cookie, if we want to delete this cookie.
3156 * So there are 3 cases for each cookie read :
3157 * 1) it's a special attribute, beginning with a '$' : ignore it.
3158 * 2) it's a server id cookie that we *MAY* want to delete : save
3159 * some pointers on it (last semi-colon, beginning of cookie...)
3160 * 3) it's an application cookie : we *MAY* have to delete a previous
3161 * "special" cookie.
3162 * At the end of loop, if a "special" cookie remains, we may have to
3163 * remove it. If no application cookie persists in the header, we
3164 * *MUST* delete it.
3165 *
3166 * Note: RFC2965 is unclear about the processing of spaces around
3167 * the equal sign in the ATTR=VALUE form. A careful inspection of
3168 * the RFC explicitly allows spaces before it, and not within the
3169 * tokens (attrs or values). An inspection of RFC2109 allows that
3170 * too but section 10.1.3 lets one think that spaces may be allowed
3171 * after the equal sign too, resulting in some (rare) buggy
3172 * implementations trying to do that. So let's do what servers do.
3173 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3174 * allowed quoted strings in values, with any possible character
3175 * after a backslash, including control chars and delimitors, which
3176 * causes parsing to become ambiguous. Browsers also allow spaces
3177 * within values even without quotes.
3178 *
3179 * We have to keep multiple pointers in order to support cookie
3180 * removal at the beginning, middle or end of header without
3181 * corrupting the header. All of these headers are valid :
3182 *
3183 * hdr_beg hdr_end
3184 * | |
3185 * v |
3186 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3187 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3188 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3189 * | | | | | | |
3190 * | | | | | | |
3191 * | | | | | | +--> next
3192 * | | | | | +----> val_end
3193 * | | | | +-----------> val_beg
3194 * | | | +--------------> equal
3195 * | | +----------------> att_end
3196 * | +---------------------> att_beg
3197 * +--------------------------> prev
3198 *
3199 */
3200 hdr_beg = ctx.value.ptr;
3201 hdr_end = hdr_beg + ctx.value.len;
3202 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3203 /* Iterate through all cookies on this line */
3204
3205 /* find att_beg */
3206 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003207 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003208 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003209 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003210
3211 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3212 att_beg++;
3213
3214 /* find att_end : this is the first character after the last non
3215 * space before the equal. It may be equal to hdr_end.
3216 */
3217 equal = att_end = att_beg;
3218 while (equal < hdr_end) {
3219 if (*equal == '=' || *equal == ',' || *equal == ';')
3220 break;
3221 if (HTTP_IS_SPHT(*equal++))
3222 continue;
3223 att_end = equal;
3224 }
3225
3226 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3227 * is between <att_beg> and <equal>, both may be identical.
3228 */
3229 /* look for end of cookie if there is an equal sign */
3230 if (equal < hdr_end && *equal == '=') {
3231 /* look for the beginning of the value */
3232 val_beg = equal + 1;
3233 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3234 val_beg++;
3235
3236 /* find the end of the value, respecting quotes */
3237 next = http_find_cookie_value_end(val_beg, hdr_end);
3238
3239 /* make val_end point to the first white space or delimitor after the value */
3240 val_end = next;
3241 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3242 val_end--;
3243 }
3244 else
3245 val_beg = val_end = next = equal;
3246
3247 /* We have nothing to do with attributes beginning with
3248 * '$'. However, they will automatically be removed if a
3249 * header before them is removed, since they're supposed
3250 * to be linked together.
3251 */
3252 if (*att_beg == '$')
3253 continue;
3254
3255 /* Ignore cookies with no equal sign */
3256 if (equal == next) {
3257 /* This is not our cookie, so we must preserve it. But if we already
3258 * scheduled another cookie for removal, we cannot remove the
3259 * complete header, but we can remove the previous block itself.
3260 */
3261 preserve_hdr = 1;
3262 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003263 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003264 val_end += delta;
3265 next += delta;
3266 hdr_end += delta;
3267 prev = del_from;
3268 del_from = NULL;
3269 }
3270 continue;
3271 }
3272
3273 /* if there are spaces around the equal sign, we need to
3274 * strip them otherwise we'll get trouble for cookie captures,
3275 * or even for rewrites. Since this happens extremely rarely,
3276 * it does not hurt performance.
3277 */
3278 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3279 int stripped_before = 0;
3280 int stripped_after = 0;
3281
3282 if (att_end != equal) {
3283 memmove(att_end, equal, hdr_end - equal);
3284 stripped_before = (att_end - equal);
3285 equal += stripped_before;
3286 val_beg += stripped_before;
3287 }
3288
3289 if (val_beg > equal + 1) {
3290 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3291 stripped_after = (equal + 1) - val_beg;
3292 val_beg += stripped_after;
3293 stripped_before += stripped_after;
3294 }
3295
3296 val_end += stripped_before;
3297 next += stripped_before;
3298 hdr_end += stripped_before;
3299 }
3300 /* now everything is as on the diagram above */
3301
3302 /* First, let's see if we want to capture this cookie. We check
3303 * that we don't already have a client side cookie, because we
3304 * can only capture one. Also as an optimisation, we ignore
3305 * cookies shorter than the declared name.
3306 */
3307 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3308 (val_end - att_beg >= sess->fe->capture_namelen) &&
3309 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3310 int log_len = val_end - att_beg;
3311
3312 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3313 ha_alert("HTTP logging : out of memory.\n");
3314 } else {
3315 if (log_len > sess->fe->capture_len)
3316 log_len = sess->fe->capture_len;
3317 memcpy(txn->cli_cookie, att_beg, log_len);
3318 txn->cli_cookie[log_len] = 0;
3319 }
3320 }
3321
3322 /* Persistence cookies in passive, rewrite or insert mode have the
3323 * following form :
3324 *
3325 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3326 *
3327 * For cookies in prefix mode, the form is :
3328 *
3329 * Cookie: NAME=SRV~VALUE
3330 */
3331 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3332 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3333 struct server *srv = s->be->srv;
3334 char *delim;
3335
3336 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3337 * have the server ID between val_beg and delim, and the original cookie between
3338 * delim+1 and val_end. Otherwise, delim==val_end :
3339 *
3340 * hdr_beg
3341 * |
3342 * v
3343 * NAME=SRV; # in all but prefix modes
3344 * NAME=SRV~OPAQUE ; # in prefix mode
3345 * || || | |+-> next
3346 * || || | +--> val_end
3347 * || || +---------> delim
3348 * || |+------------> val_beg
3349 * || +-------------> att_end = equal
3350 * |+-----------------> att_beg
3351 * +------------------> prev
3352 *
3353 */
3354 if (s->be->ck_opts & PR_CK_PFX) {
3355 for (delim = val_beg; delim < val_end; delim++)
3356 if (*delim == COOKIE_DELIM)
3357 break;
3358 }
3359 else {
3360 char *vbar1;
3361 delim = val_end;
3362 /* Now check if the cookie contains a date field, which would
3363 * appear after a vertical bar ('|') just after the server name
3364 * and before the delimiter.
3365 */
3366 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3367 if (vbar1) {
3368 /* OK, so left of the bar is the server's cookie and
3369 * right is the last seen date. It is a base64 encoded
3370 * 30-bit value representing the UNIX date since the
3371 * epoch in 4-second quantities.
3372 */
3373 int val;
3374 delim = vbar1++;
3375 if (val_end - vbar1 >= 5) {
3376 val = b64tos30(vbar1);
3377 if (val > 0)
3378 txn->cookie_last_date = val << 2;
3379 }
3380 /* look for a second vertical bar */
3381 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3382 if (vbar1 && (val_end - vbar1 > 5)) {
3383 val = b64tos30(vbar1 + 1);
3384 if (val > 0)
3385 txn->cookie_first_date = val << 2;
3386 }
3387 }
3388 }
3389
3390 /* if the cookie has an expiration date and the proxy wants to check
3391 * it, then we do that now. We first check if the cookie is too old,
3392 * then only if it has expired. We detect strict overflow because the
3393 * time resolution here is not great (4 seconds). Cookies with dates
3394 * in the future are ignored if their offset is beyond one day. This
3395 * allows an admin to fix timezone issues without expiring everyone
3396 * and at the same time avoids keeping unwanted side effects for too
3397 * long.
3398 */
3399 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3400 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3401 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3402 txn->flags &= ~TX_CK_MASK;
3403 txn->flags |= TX_CK_OLD;
3404 delim = val_beg; // let's pretend we have not found the cookie
3405 txn->cookie_first_date = 0;
3406 txn->cookie_last_date = 0;
3407 }
3408 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3409 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3410 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3411 txn->flags &= ~TX_CK_MASK;
3412 txn->flags |= TX_CK_EXPIRED;
3413 delim = val_beg; // let's pretend we have not found the cookie
3414 txn->cookie_first_date = 0;
3415 txn->cookie_last_date = 0;
3416 }
3417
3418 /* Here, we'll look for the first running server which supports the cookie.
3419 * This allows to share a same cookie between several servers, for example
3420 * to dedicate backup servers to specific servers only.
3421 * However, to prevent clients from sticking to cookie-less backup server
3422 * when they have incidentely learned an empty cookie, we simply ignore
3423 * empty cookies and mark them as invalid.
3424 * The same behaviour is applied when persistence must be ignored.
3425 */
3426 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3427 srv = NULL;
3428
3429 while (srv) {
3430 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3431 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3432 if ((srv->cur_state != SRV_ST_STOPPED) ||
3433 (s->be->options & PR_O_PERSIST) ||
3434 (s->flags & SF_FORCE_PRST)) {
3435 /* we found the server and we can use it */
3436 txn->flags &= ~TX_CK_MASK;
3437 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3438 s->flags |= SF_DIRECT | SF_ASSIGNED;
3439 s->target = &srv->obj_type;
3440 break;
3441 } else {
3442 /* we found a server, but it's down,
3443 * mark it as such and go on in case
3444 * another one is available.
3445 */
3446 txn->flags &= ~TX_CK_MASK;
3447 txn->flags |= TX_CK_DOWN;
3448 }
3449 }
3450 srv = srv->next;
3451 }
3452
3453 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3454 /* no server matched this cookie or we deliberately skipped it */
3455 txn->flags &= ~TX_CK_MASK;
3456 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3457 txn->flags |= TX_CK_UNUSED;
3458 else
3459 txn->flags |= TX_CK_INVALID;
3460 }
3461
3462 /* depending on the cookie mode, we may have to either :
3463 * - delete the complete cookie if we're in insert+indirect mode, so that
3464 * the server never sees it ;
3465 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003466 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003467 * if we're in cookie prefix mode
3468 */
3469 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3470 int delta; /* negative */
3471
3472 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3473 delta = val_beg - (delim + 1);
3474 val_end += delta;
3475 next += delta;
3476 hdr_end += delta;
3477 del_from = NULL;
3478 preserve_hdr = 1; /* we want to keep this cookie */
3479 }
3480 else if (del_from == NULL &&
3481 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3482 del_from = prev;
3483 }
3484 }
3485 else {
3486 /* This is not our cookie, so we must preserve it. But if we already
3487 * scheduled another cookie for removal, we cannot remove the
3488 * complete header, but we can remove the previous block itself.
3489 */
3490 preserve_hdr = 1;
3491
3492 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003493 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003494 if (att_beg >= del_from)
3495 att_beg += delta;
3496 if (att_end >= del_from)
3497 att_end += delta;
3498 val_beg += delta;
3499 val_end += delta;
3500 next += delta;
3501 hdr_end += delta;
3502 prev = del_from;
3503 del_from = NULL;
3504 }
3505 }
3506
3507 /* continue with next cookie on this header line */
3508 att_beg = next;
3509 } /* for each cookie */
3510
3511
3512 /* There are no more cookies on this line.
3513 * We may still have one (or several) marked for deletion at the
3514 * end of the line. We must do this now in two ways :
3515 * - if some cookies must be preserved, we only delete from the
3516 * mark to the end of line ;
3517 * - if nothing needs to be preserved, simply delete the whole header
3518 */
3519 if (del_from) {
3520 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3521 }
3522 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003523 if (hdr_beg != hdr_end)
3524 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003525 else
3526 http_remove_header(htx, &ctx);
3527 }
3528 } /* for each "Cookie header */
3529}
3530
3531/*
3532 * Manage server-side cookies. It can impact performance by about 2% so it is
3533 * desirable to call it only when needed. This function is also used when we
3534 * just need to know if there is a cookie (eg: for check-cache).
3535 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003536static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003537{
3538 struct session *sess = s->sess;
3539 struct http_txn *txn = s->txn;
3540 struct htx *htx;
3541 struct http_hdr_ctx ctx;
3542 struct server *srv;
3543 char *hdr_beg, *hdr_end;
3544 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003545 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003546
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003547 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003548
3549 ctx.blk = NULL;
3550 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003551 int is_first = 1;
3552
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003553 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3554 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3555 break;
3556 is_cookie2 = 1;
3557 }
3558
3559 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3560 * <prev> points to the colon.
3561 */
3562 txn->flags |= TX_SCK_PRESENT;
3563
3564 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3565 * check-cache is enabled) and we are not interested in checking
3566 * them. Warning, the cookie capture is declared in the frontend.
3567 */
3568 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3569 break;
3570
3571 /* OK so now we know we have to process this response cookie.
3572 * The format of the Set-Cookie header is slightly different
3573 * from the format of the Cookie header in that it does not
3574 * support the comma as a cookie delimiter (thus the header
3575 * cannot be folded) because the Expires attribute described in
3576 * the original Netscape's spec may contain an unquoted date
3577 * with a comma inside. We have to live with this because
3578 * many browsers don't support Max-Age and some browsers don't
3579 * support quoted strings. However the Set-Cookie2 header is
3580 * clean.
3581 *
3582 * We have to keep multiple pointers in order to support cookie
3583 * removal at the beginning, middle or end of header without
3584 * corrupting the header (in case of set-cookie2). A special
3585 * pointer, <scav> points to the beginning of the set-cookie-av
3586 * fields after the first semi-colon. The <next> pointer points
3587 * either to the end of line (set-cookie) or next unquoted comma
3588 * (set-cookie2). All of these headers are valid :
3589 *
3590 * hdr_beg hdr_end
3591 * | |
3592 * v |
3593 * NAME1 = VALUE 1 ; Secure; Path="/" |
3594 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3595 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3596 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3597 * | | | | | | | |
3598 * | | | | | | | +-> next
3599 * | | | | | | +------------> scav
3600 * | | | | | +--------------> val_end
3601 * | | | | +--------------------> val_beg
3602 * | | | +----------------------> equal
3603 * | | +------------------------> att_end
3604 * | +----------------------------> att_beg
3605 * +------------------------------> prev
3606 * -------------------------------> hdr_beg
3607 */
3608 hdr_beg = ctx.value.ptr;
3609 hdr_end = hdr_beg + ctx.value.len;
3610 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3611
3612 /* Iterate through all cookies on this line */
3613
3614 /* find att_beg */
3615 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003616 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003617 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003618 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003619
3620 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3621 att_beg++;
3622
3623 /* find att_end : this is the first character after the last non
3624 * space before the equal. It may be equal to hdr_end.
3625 */
3626 equal = att_end = att_beg;
3627
3628 while (equal < hdr_end) {
3629 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3630 break;
3631 if (HTTP_IS_SPHT(*equal++))
3632 continue;
3633 att_end = equal;
3634 }
3635
3636 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3637 * is between <att_beg> and <equal>, both may be identical.
3638 */
3639
3640 /* look for end of cookie if there is an equal sign */
3641 if (equal < hdr_end && *equal == '=') {
3642 /* look for the beginning of the value */
3643 val_beg = equal + 1;
3644 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3645 val_beg++;
3646
3647 /* find the end of the value, respecting quotes */
3648 next = http_find_cookie_value_end(val_beg, hdr_end);
3649
3650 /* make val_end point to the first white space or delimitor after the value */
3651 val_end = next;
3652 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3653 val_end--;
3654 }
3655 else {
3656 /* <equal> points to next comma, semi-colon or EOL */
3657 val_beg = val_end = next = equal;
3658 }
3659
3660 if (next < hdr_end) {
3661 /* Set-Cookie2 supports multiple cookies, and <next> points to
3662 * a colon or semi-colon before the end. So skip all attr-value
3663 * pairs and look for the next comma. For Set-Cookie, since
3664 * commas are permitted in values, skip to the end.
3665 */
3666 if (is_cookie2)
3667 next = http_find_hdr_value_end(next, hdr_end);
3668 else
3669 next = hdr_end;
3670 }
3671
3672 /* Now everything is as on the diagram above */
3673
3674 /* Ignore cookies with no equal sign */
3675 if (equal == val_end)
3676 continue;
3677
3678 /* If there are spaces around the equal sign, we need to
3679 * strip them otherwise we'll get trouble for cookie captures,
3680 * or even for rewrites. Since this happens extremely rarely,
3681 * it does not hurt performance.
3682 */
3683 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3684 int stripped_before = 0;
3685 int stripped_after = 0;
3686
3687 if (att_end != equal) {
3688 memmove(att_end, equal, hdr_end - equal);
3689 stripped_before = (att_end - equal);
3690 equal += stripped_before;
3691 val_beg += stripped_before;
3692 }
3693
3694 if (val_beg > equal + 1) {
3695 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3696 stripped_after = (equal + 1) - val_beg;
3697 val_beg += stripped_after;
3698 stripped_before += stripped_after;
3699 }
3700
3701 val_end += stripped_before;
3702 next += stripped_before;
3703 hdr_end += stripped_before;
3704
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003705 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003706 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003707 }
3708
3709 /* First, let's see if we want to capture this cookie. We check
3710 * that we don't already have a server side cookie, because we
3711 * can only capture one. Also as an optimisation, we ignore
3712 * cookies shorter than the declared name.
3713 */
3714 if (sess->fe->capture_name != NULL &&
3715 txn->srv_cookie == NULL &&
3716 (val_end - att_beg >= sess->fe->capture_namelen) &&
3717 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3718 int log_len = val_end - att_beg;
3719 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3720 ha_alert("HTTP logging : out of memory.\n");
3721 }
3722 else {
3723 if (log_len > sess->fe->capture_len)
3724 log_len = sess->fe->capture_len;
3725 memcpy(txn->srv_cookie, att_beg, log_len);
3726 txn->srv_cookie[log_len] = 0;
3727 }
3728 }
3729
3730 srv = objt_server(s->target);
3731 /* now check if we need to process it for persistence */
3732 if (!(s->flags & SF_IGNORE_PRST) &&
3733 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3734 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3735 /* assume passive cookie by default */
3736 txn->flags &= ~TX_SCK_MASK;
3737 txn->flags |= TX_SCK_FOUND;
3738
3739 /* If the cookie is in insert mode on a known server, we'll delete
3740 * this occurrence because we'll insert another one later.
3741 * We'll delete it too if the "indirect" option is set and we're in
3742 * a direct access.
3743 */
3744 if (s->be->ck_opts & PR_CK_PSV) {
3745 /* The "preserve" flag was set, we don't want to touch the
3746 * server's cookie.
3747 */
3748 }
3749 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3750 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3751 /* this cookie must be deleted */
3752 if (prev == hdr_beg && next == hdr_end) {
3753 /* whole header */
3754 http_remove_header(htx, &ctx);
3755 /* note: while both invalid now, <next> and <hdr_end>
3756 * are still equal, so the for() will stop as expected.
3757 */
3758 } else {
3759 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003760 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003761 next = prev;
3762 hdr_end += delta;
3763 }
3764 txn->flags &= ~TX_SCK_MASK;
3765 txn->flags |= TX_SCK_DELETED;
3766 /* and go on with next cookie */
3767 }
3768 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3769 /* replace bytes val_beg->val_end with the cookie name associated
3770 * with this server since we know it.
3771 */
3772 int sliding, delta;
3773
3774 ctx.value = ist2(val_beg, val_end - val_beg);
3775 ctx.lws_before = ctx.lws_after = 0;
3776 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3777 delta = srv->cklen - (val_end - val_beg);
3778 sliding = (ctx.value.ptr - val_beg);
3779 hdr_beg += sliding;
3780 val_beg += sliding;
3781 next += sliding + delta;
3782 hdr_end += sliding + delta;
3783
3784 txn->flags &= ~TX_SCK_MASK;
3785 txn->flags |= TX_SCK_REPLACED;
3786 }
3787 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3788 /* insert the cookie name associated with this server
3789 * before existing cookie, and insert a delimiter between them..
3790 */
3791 int sliding, delta;
3792 ctx.value = ist2(val_beg, 0);
3793 ctx.lws_before = ctx.lws_after = 0;
3794 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3795 delta = srv->cklen + 1;
3796 sliding = (ctx.value.ptr - val_beg);
3797 hdr_beg += sliding;
3798 val_beg += sliding;
3799 next += sliding + delta;
3800 hdr_end += sliding + delta;
3801
3802 val_beg[srv->cklen] = COOKIE_DELIM;
3803 txn->flags &= ~TX_SCK_MASK;
3804 txn->flags |= TX_SCK_REPLACED;
3805 }
3806 }
3807 /* that's done for this cookie, check the next one on the same
3808 * line when next != hdr_end (only if is_cookie2).
3809 */
3810 }
3811 }
3812}
3813
Christopher Faulet25a02f62018-10-24 12:00:25 +02003814/*
3815 * Parses the Cache-Control and Pragma request header fields to determine if
3816 * the request may be served from the cache and/or if it is cacheable. Updates
3817 * s->txn->flags.
3818 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003819void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003820{
3821 struct http_txn *txn = s->txn;
3822 struct htx *htx;
3823 int32_t pos;
3824 int pragma_found, cc_found, i;
3825
3826 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3827 return; /* nothing more to do here */
3828
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003829 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003830 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02003831 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003832 struct htx_blk *blk = htx_get_blk(htx, pos);
3833 enum htx_blk_type type = htx_get_blk_type(blk);
3834 struct ist n, v;
3835
3836 if (type == HTX_BLK_EOH)
3837 break;
3838 if (type != HTX_BLK_HDR)
3839 continue;
3840
3841 n = htx_get_blk_name(htx, blk);
3842 v = htx_get_blk_value(htx, blk);
3843
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003844 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003845 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3846 pragma_found = 1;
3847 continue;
3848 }
3849 }
3850
3851 /* Don't use the cache and don't try to store if we found the
3852 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003853 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003854 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3855 txn->flags |= TX_CACHE_IGNORE;
3856 continue;
3857 }
3858
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003859 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02003860 continue;
3861
3862 /* OK, right now we know we have a cache-control header */
3863 cc_found = 1;
3864 if (!v.len) /* no info */
3865 continue;
3866
3867 i = 0;
3868 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
3869 !isspace((unsigned char)*(v.ptr+i)))
3870 i++;
3871
3872 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
3873 * values after max-age, max-stale nor min-fresh, we simply don't
3874 * use the cache when they're specified.
3875 */
3876 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
3877 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
3878 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
3879 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
3880 txn->flags |= TX_CACHE_IGNORE;
3881 continue;
3882 }
3883
3884 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
3885 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3886 continue;
3887 }
3888 }
3889
3890 /* RFC7234#5.4:
3891 * When the Cache-Control header field is also present and
3892 * understood in a request, Pragma is ignored.
3893 * When the Cache-Control header field is not present in a
3894 * request, caches MUST consider the no-cache request
3895 * pragma-directive as having the same effect as if
3896 * "Cache-Control: no-cache" were present.
3897 */
3898 if (!cc_found && pragma_found)
3899 txn->flags |= TX_CACHE_IGNORE;
3900}
3901
3902/*
3903 * Check if response is cacheable or not. Updates s->txn->flags.
3904 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003905void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003906{
3907 struct http_txn *txn = s->txn;
3908 struct htx *htx;
3909 int32_t pos;
3910 int i;
3911
3912 if (txn->status < 200) {
3913 /* do not try to cache interim responses! */
3914 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3915 return;
3916 }
3917
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003918 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02003919 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003920 struct htx_blk *blk = htx_get_blk(htx, pos);
3921 enum htx_blk_type type = htx_get_blk_type(blk);
3922 struct ist n, v;
3923
3924 if (type == HTX_BLK_EOH)
3925 break;
3926 if (type != HTX_BLK_HDR)
3927 continue;
3928
3929 n = htx_get_blk_name(htx, blk);
3930 v = htx_get_blk_value(htx, blk);
3931
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003932 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003933 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3934 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3935 return;
3936 }
3937 }
3938
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003939 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02003940 continue;
3941
3942 /* OK, right now we know we have a cache-control header */
3943 if (!v.len) /* no info */
3944 continue;
3945
3946 i = 0;
3947 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
3948 !isspace((unsigned char)*(v.ptr+i)))
3949 i++;
3950
3951 /* we have a complete value between v.ptr and (v.ptr+i) */
3952 if (i < v.len && *(v.ptr + i) == '=') {
3953 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
3954 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
3955 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3956 continue;
3957 }
3958
3959 /* we have something of the form no-cache="set-cookie" */
3960 if ((v.len >= 21) &&
3961 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
3962 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
3963 txn->flags &= ~TX_CACHE_COOK;
3964 continue;
3965 }
3966
3967 /* OK, so we know that either p2 points to the end of string or to a comma */
3968 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
3969 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
3970 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
3971 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3972 return;
3973 }
3974
3975 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
3976 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
3977 continue;
3978 }
3979 }
3980}
3981
Christopher Faulet377c5a52018-10-24 21:21:30 +02003982/*
3983 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3984 * for the current backend.
3985 *
3986 * It is assumed that the request is either a HEAD, GET, or POST and that the
3987 * uri_auth field is valid.
3988 *
3989 * Returns 1 if stats should be provided, otherwise 0.
3990 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003991static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003992{
3993 struct uri_auth *uri_auth = backend->uri_auth;
3994 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003995 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003996 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003997
3998 if (!uri_auth)
3999 return 0;
4000
4001 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
4002 return 0;
4003
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004004 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004005 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004006 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01004007 if (*uri_auth->uri_prefix == '/')
4008 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004009
4010 /* check URI size */
4011 if (uri_auth->uri_len > uri.len)
4012 return 0;
4013
4014 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4015 return 0;
4016
4017 return 1;
4018}
4019
4020/* This function prepares an applet to handle the stats. It can deal with the
4021 * "100-continue" expectation, check that admin rules are met for POST requests,
4022 * and program a response message if something was unexpected. It cannot fail
4023 * and always relies on the stats applet to complete the job. It does not touch
4024 * analysers nor counters, which are left to the caller. It does not touch
4025 * s->target which is supposed to already point to the stats applet. The caller
4026 * is expected to have already assigned an appctx to the stream.
4027 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004028static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004029{
4030 struct stats_admin_rule *stats_admin_rule;
4031 struct stream_interface *si = &s->si[1];
4032 struct session *sess = s->sess;
4033 struct http_txn *txn = s->txn;
4034 struct http_msg *msg = &txn->req;
4035 struct uri_auth *uri_auth = s->be->uri_auth;
4036 const char *h, *lookup, *end;
4037 struct appctx *appctx;
4038 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004039 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004040
4041 appctx = si_appctx(si);
4042 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4043 appctx->st1 = appctx->st2 = 0;
4044 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004045 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004046 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4047 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4048 appctx->ctx.stats.flags |= STAT_CHUNKED;
4049
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004050 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004051 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004052 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4053 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004054
4055 for (h = lookup; h <= end - 3; h++) {
4056 if (memcmp(h, ";up", 3) == 0) {
4057 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4058 break;
4059 }
4060 }
4061
4062 if (uri_auth->refresh) {
4063 for (h = lookup; h <= end - 10; h++) {
4064 if (memcmp(h, ";norefresh", 10) == 0) {
4065 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4066 break;
4067 }
4068 }
4069 }
4070
4071 for (h = lookup; h <= end - 4; h++) {
4072 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004073 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004074 break;
4075 }
4076 }
4077
4078 for (h = lookup; h <= end - 6; h++) {
4079 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004080 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004081 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4082 break;
4083 }
4084 }
4085
Christopher Faulet6338a082019-09-09 15:50:54 +02004086 for (h = lookup; h <= end - 5; h++) {
4087 if (memcmp(h, ";json", 5) == 0) {
4088 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4089 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4090 break;
4091 }
4092 }
4093
4094 for (h = lookup; h <= end - 12; h++) {
4095 if (memcmp(h, ";json-schema", 12) == 0) {
4096 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4097 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4098 break;
4099 }
4100 }
4101
Christopher Faulet377c5a52018-10-24 21:21:30 +02004102 for (h = lookup; h <= end - 8; h++) {
4103 if (memcmp(h, ";st=", 4) == 0) {
4104 int i;
4105 h += 4;
4106 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4107 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4108 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4109 appctx->ctx.stats.st_code = i;
4110 break;
4111 }
4112 }
4113 break;
4114 }
4115 }
4116
4117 appctx->ctx.stats.scope_str = 0;
4118 appctx->ctx.stats.scope_len = 0;
4119 for (h = lookup; h <= end - 8; h++) {
4120 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4121 int itx = 0;
4122 const char *h2;
4123 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4124 const char *err;
4125
4126 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4127 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004128 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4129 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004130 if (*h == ';' || *h == '&' || *h == ' ')
4131 break;
4132 itx++;
4133 h++;
4134 }
4135
4136 if (itx > STAT_SCOPE_TXT_MAXLEN)
4137 itx = STAT_SCOPE_TXT_MAXLEN;
4138 appctx->ctx.stats.scope_len = itx;
4139
4140 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4141 memcpy(scope_txt, h2, itx);
4142 scope_txt[itx] = '\0';
4143 err = invalid_char(scope_txt);
4144 if (err) {
4145 /* bad char in search text => clear scope */
4146 appctx->ctx.stats.scope_str = 0;
4147 appctx->ctx.stats.scope_len = 0;
4148 }
4149 break;
4150 }
4151 }
4152
4153 /* now check whether we have some admin rules for this request */
4154 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4155 int ret = 1;
4156
4157 if (stats_admin_rule->cond) {
4158 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4159 ret = acl_pass(ret);
4160 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4161 ret = !ret;
4162 }
4163
4164 if (ret) {
4165 /* no rule, or the rule matches */
4166 appctx->ctx.stats.flags |= STAT_ADMIN;
4167 break;
4168 }
4169 }
4170
Christopher Faulet5d45e382019-02-27 15:15:23 +01004171 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4172 appctx->st0 = STAT_HTTP_HEAD;
4173 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004174 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004175 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004176 if (msg->msg_state < HTTP_MSG_DATA)
4177 req->analysers |= AN_REQ_HTTP_BODY;
4178 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004179 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004180 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004181 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4182 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4183 appctx->st0 = STAT_HTTP_LAST;
4184 }
4185 }
4186 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004187 /* Unsupported method */
4188 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4189 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4190 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004191 }
4192
4193 s->task->nice = -32; /* small boost for HTTP statistics */
4194 return 1;
4195}
4196
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004197void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004198{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004199 struct channel *req = &s->req;
4200 struct channel *res = &s->res;
4201 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004202 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004203 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004204 struct ist path, location;
4205 unsigned int flags;
4206 size_t data;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004207
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004208 /*
4209 * Create the location
4210 */
4211 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004212
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004213 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004214 /* special prefix "/" means don't change URL */
4215 srv = __objt_server(s->target);
4216 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4217 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4218 return;
4219 }
4220
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004221 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004222 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004223 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004224 path = http_get_path(htx_sl_req_uri(sl));
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004225 if (!path.ptr)
4226 return;
4227
4228 if (!chunk_memcat(&trash, path.ptr, path.len))
4229 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004230 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004231
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004232 /*
4233 * Create the 302 respone
4234 */
4235 htx = htx_from_buf(&res->buf);
4236 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4237 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4238 ist("HTTP/1.1"), ist("302"), ist("Found"));
4239 if (!sl)
4240 goto fail;
4241 sl->info.res.status = 302;
4242 s->txn->status = 302;
4243
4244 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4245 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4246 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4247 !htx_add_header(htx, ist("Location"), location))
4248 goto fail;
4249
4250 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4251 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004252
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004253 /*
4254 * Send the message
4255 */
4256 data = htx->data - co_data(res);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004257 c_adv(res, data);
4258 res->total += data;
4259
4260 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004261 si_shutr(si);
4262 si_shutw(si);
4263 si->err_type = SI_ET_NONE;
4264 si->state = SI_ST_CLO;
4265
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004266 channel_auto_read(req);
4267 channel_abort(req);
4268 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004269 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004270 channel_auto_read(res);
4271 channel_auto_close(res);
4272
4273 if (!(s->flags & SF_ERR_MASK))
4274 s->flags |= SF_ERR_LOCAL;
4275 if (!(s->flags & SF_FINST_MASK))
4276 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004277
4278 /* FIXME: we should increase a counter of redirects per server and per backend. */
4279 srv_inc_sess_ctr(srv);
4280 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004281 return;
4282
4283 fail:
4284 /* If an error occurred, remove the incomplete HTTP response from the
4285 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004286 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004287}
4288
Christopher Fauletf2824e62018-10-01 12:12:37 +02004289/* This function terminates the request because it was completly analyzed or
4290 * because an error was triggered during the body forwarding.
4291 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004292static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004293{
4294 struct channel *chn = &s->req;
4295 struct http_txn *txn = s->txn;
4296
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004297 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004298
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004299 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4300 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004301 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004302 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004303 goto end;
4304 }
4305
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004306 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4307 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004308 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004309 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004310
4311 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004312 /* No need to read anymore, the request was completely parsed.
4313 * We can shut the read side unless we want to abort_on_close,
4314 * or we have a POST request. The issue with POST requests is
4315 * that some browsers still send a CRLF after the request, and
4316 * this CRLF must be read so that it does not remain in the kernel
4317 * buffers, otherwise a close could cause an RST on some systems
4318 * (eg: Linux).
4319 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004320 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004321 channel_dont_read(chn);
4322
4323 /* if the server closes the connection, we want to immediately react
4324 * and close the socket to save packets and syscalls.
4325 */
4326 s->si[1].flags |= SI_FL_NOHALF;
4327
4328 /* In any case we've finished parsing the request so we must
4329 * disable Nagle when sending data because 1) we're not going
4330 * to shut this side, and 2) the server is waiting for us to
4331 * send pending data.
4332 */
4333 chn->flags |= CF_NEVER_WAIT;
4334
Christopher Fauletd01ce402019-01-02 17:44:13 +01004335 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4336 /* The server has not finished to respond, so we
4337 * don't want to move in order not to upset it.
4338 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004339 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004340 return;
4341 }
4342
Christopher Fauletf2824e62018-10-01 12:12:37 +02004343 /* When we get here, it means that both the request and the
4344 * response have finished receiving. Depending on the connection
4345 * mode, we'll have to wait for the last bytes to leave in either
4346 * direction, and sometimes for a close to be effective.
4347 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004348 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004349 /* Tunnel mode will not have any analyser so it needs to
4350 * poll for reads.
4351 */
4352 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004353 if (b_data(&chn->buf)) {
4354 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004355 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004356 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004357 txn->req.msg_state = HTTP_MSG_TUNNEL;
4358 }
4359 else {
4360 /* we're not expecting any new data to come for this
4361 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004362 *
4363 * However, there is an exception if the response
4364 * length is undefined. In this case, we need to wait
4365 * the close from the server. The response will be
4366 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004367 */
4368 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4369 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004370 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004371
4372 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4373 channel_shutr_now(chn);
4374 channel_shutw_now(chn);
4375 }
4376 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004377 goto check_channel_flags;
4378 }
4379
4380 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4381 http_msg_closing:
4382 /* nothing else to forward, just waiting for the output buffer
4383 * to be empty and for the shutw_now to take effect.
4384 */
4385 if (channel_is_empty(chn)) {
4386 txn->req.msg_state = HTTP_MSG_CLOSED;
4387 goto http_msg_closed;
4388 }
4389 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004390 txn->req.msg_state = HTTP_MSG_ERROR;
4391 goto end;
4392 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004393 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004394 return;
4395 }
4396
4397 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4398 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004399 /* if we don't know whether the server will close, we need to hard close */
4400 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4401 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004402 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004403 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004404 channel_dont_read(chn);
4405 goto end;
4406 }
4407
4408 check_channel_flags:
4409 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4410 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4411 /* if we've just closed an output, let's switch */
4412 txn->req.msg_state = HTTP_MSG_CLOSING;
4413 goto http_msg_closing;
4414 }
4415
4416 end:
4417 chn->analysers &= AN_REQ_FLT_END;
4418 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4419 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4420 channel_auto_close(chn);
4421 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004422 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004423}
4424
4425
4426/* This function terminates the response because it was completly analyzed or
4427 * because an error was triggered during the body forwarding.
4428 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004429static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004430{
4431 struct channel *chn = &s->res;
4432 struct http_txn *txn = s->txn;
4433
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004434 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004435
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004436 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4437 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004438 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004439 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004440 goto end;
4441 }
4442
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004443 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4444 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004445 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004446 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004447
4448 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4449 /* In theory, we don't need to read anymore, but we must
4450 * still monitor the server connection for a possible close
4451 * while the request is being uploaded, so we don't disable
4452 * reading.
4453 */
4454 /* channel_dont_read(chn); */
4455
4456 if (txn->req.msg_state < HTTP_MSG_DONE) {
4457 /* The client seems to still be sending data, probably
4458 * because we got an error response during an upload.
4459 * We have the choice of either breaking the connection
4460 * or letting it pass through. Let's do the later.
4461 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004462 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004463 return;
4464 }
4465
4466 /* When we get here, it means that both the request and the
4467 * response have finished receiving. Depending on the connection
4468 * mode, we'll have to wait for the last bytes to leave in either
4469 * direction, and sometimes for a close to be effective.
4470 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004471 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004472 channel_auto_read(chn);
4473 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004474 if (b_data(&chn->buf)) {
4475 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004476 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004477 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004478 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4479 }
4480 else {
4481 /* we're not expecting any new data to come for this
4482 * transaction, so we can close it.
4483 */
4484 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4485 channel_shutr_now(chn);
4486 channel_shutw_now(chn);
4487 }
4488 }
4489 goto check_channel_flags;
4490 }
4491
4492 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4493 http_msg_closing:
4494 /* nothing else to forward, just waiting for the output buffer
4495 * to be empty and for the shutw_now to take effect.
4496 */
4497 if (channel_is_empty(chn)) {
4498 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4499 goto http_msg_closed;
4500 }
4501 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004502 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004503 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004504 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004505 if (strm_sess(s)->listener->counters)
4506 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004507 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004508 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004509 goto end;
4510 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004511 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004512 return;
4513 }
4514
4515 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4516 http_msg_closed:
4517 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004518 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004519 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004520 goto end;
4521 }
4522
4523 check_channel_flags:
4524 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4525 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4526 /* if we've just closed an output, let's switch */
4527 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4528 goto http_msg_closing;
4529 }
4530
4531 end:
4532 chn->analysers &= AN_RES_FLT_END;
4533 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4534 chn->analysers |= AN_RES_FLT_XFER_DATA;
4535 channel_auto_close(chn);
4536 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004537 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004538}
4539
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004540void http_server_error(struct stream *s, struct stream_interface *si, int err,
4541 int finst, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004542{
4543 channel_auto_read(si_oc(si));
4544 channel_abort(si_oc(si));
4545 channel_auto_close(si_oc(si));
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004546 channel_htx_erase(si_oc(si), htxbuf(&(si_oc(si))->buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02004547 channel_auto_close(si_ic(si));
4548 channel_auto_read(si_ic(si));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004549
4550 /* <msg> is an HTX structure. So we copy it in the response's
4551 * channel */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004552 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004553 struct channel *chn = si_ic(si);
4554 struct htx *htx;
4555
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004556 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004557 chn->buf.data = msg->data;
4558 memcpy(chn->buf.area, msg->area, msg->data);
4559 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02004560 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02004561 c_adv(chn, htx->data);
4562 chn->total += htx->data;
4563 }
4564 if (!(s->flags & SF_ERR_MASK))
4565 s->flags |= err;
4566 if (!(s->flags & SF_FINST_MASK))
4567 s->flags |= finst;
4568}
4569
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004570void http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004571{
4572 channel_auto_read(&s->req);
4573 channel_abort(&s->req);
4574 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004575 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
4576 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02004577
4578 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004579
4580 /* <msg> is an HTX structure. So we copy it in the response's
4581 * channel */
4582 /* FIXME: It is a problem for now if there is some outgoing data */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004583 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004584 struct channel *chn = &s->res;
4585 struct htx *htx;
4586
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004587 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004588 chn->buf.data = msg->data;
4589 memcpy(chn->buf.area, msg->area, msg->data);
4590 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02004591 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02004592 c_adv(chn, htx->data);
4593 chn->total += htx->data;
4594 }
4595
4596 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
4597 channel_auto_read(&s->res);
4598 channel_auto_close(&s->res);
4599 channel_shutr_now(&s->res);
4600}
4601
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004602struct buffer *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004603{
4604 const int msgnum = http_get_status_idx(s->txn->status);
4605
Christopher Faulet473e8802020-01-14 11:12:37 +01004606 if (s->txn && s->txn->errmsg)
4607 return s->txn->errmsg;
4608 else if (s->be->errmsg[msgnum])
Christopher Faulet58857752020-01-15 15:19:50 +01004609 return s->be->errmsg[msgnum];
4610 else if (strm_fe(s)->errmsg[msgnum])
4611 return strm_fe(s)->errmsg[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004612 else
Christopher Fauletf7346382019-07-17 22:02:08 +02004613 return &http_err_chunks[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004614}
4615
Christopher Faulet304cc402019-07-15 15:46:28 +02004616/* Return the error message corresponding to si->err_type. It is assumed
4617 * that the server side is closed. Note that err_type is actually a
4618 * bitmask, where almost only aborts may be cumulated with other
4619 * values. We consider that aborted operations are more important
4620 * than timeouts or errors due to the fact that nobody else in the
4621 * logs might explain incomplete retries. All others should avoid
4622 * being cumulated. It should normally not be possible to have multiple
4623 * aborts at once, but just in case, the first one in sequence is reported.
4624 * Note that connection errors appearing on the second request of a keep-alive
4625 * connection are not reported since this allows the client to retry.
4626 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004627void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004628{
4629 int err_type = si->err_type;
4630
4631 /* set s->txn->status for http_error_message(s) */
4632 s->txn->status = 503;
4633
4634 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004635 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4636 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004637 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004638 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4639 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4640 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004641 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004642 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4643 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004644 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004645 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4646 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004647 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004648 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4649 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4650 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004651 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004652 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4653 (s->flags & SF_SRV_REUSED) ? NULL :
4654 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004655 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004656 http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C,
4657 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4658 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004659 else { /* SI_ET_CONN_OTHER and others */
4660 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004661 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4662 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004663 }
4664}
4665
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004666
Christopher Faulet4a28a532019-03-01 11:19:40 +01004667/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4668 * on success and -1 on error.
4669 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004670static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004671{
4672 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4673 * then we must send an HTTP/1.1 100 Continue intermediate response.
4674 */
4675 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4676 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4677 struct ist hdr = { .ptr = "Expect", .len = 6 };
4678 struct http_hdr_ctx ctx;
4679
4680 ctx.blk = NULL;
4681 /* Expect is allowed in 1.1, look for it */
4682 if (http_find_header(htx, hdr, &ctx, 0) &&
4683 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004684 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004685 return -1;
4686 http_remove_header(htx, &ctx);
4687 }
4688 }
4689 return 0;
4690}
4691
Christopher Faulet23a3c792018-11-28 10:01:23 +01004692/* Send a 100-Continue response to the client. It returns 0 on success and -1
4693 * on error. The response channel is updated accordingly.
4694 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004695static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004696{
4697 struct channel *res = &s->res;
4698 struct htx *htx = htx_from_buf(&res->buf);
4699 struct htx_sl *sl;
4700 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4701 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4702 size_t data;
4703
4704 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4705 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4706 if (!sl)
4707 goto fail;
4708 sl->info.res.status = 100;
4709
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004710 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004711 goto fail;
4712
4713 data = htx->data - co_data(res);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004714 c_adv(res, data);
4715 res->total += data;
4716 return 0;
4717
4718 fail:
4719 /* If an error occurred, remove the incomplete HTTP response from the
4720 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004721 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004722 return -1;
4723}
4724
Christopher Faulet12c51e22018-11-28 15:59:42 +01004725
4726/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
4727 * ont whether we use a proxy or not. It returns 0 on success and -1 on
4728 * error. The response channel is updated accordingly.
4729 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004730static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
Christopher Faulet12c51e22018-11-28 15:59:42 +01004731{
4732 struct channel *res = &s->res;
4733 struct htx *htx = htx_from_buf(&res->buf);
4734 struct htx_sl *sl;
4735 struct ist code, body;
4736 int status;
4737 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4738 size_t data;
4739
4740 if (!(s->txn->flags & TX_USE_PX_CONN)) {
4741 status = 401;
4742 code = ist("401");
4743 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
4744 "You need a valid user and password to access this content.\n"
4745 "</body></html>\n");
4746 }
4747 else {
4748 status = 407;
4749 code = ist("407");
4750 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
4751 "You need a valid user and password to access this content.\n"
4752 "</body></html>\n");
4753 }
4754
4755 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4756 ist("HTTP/1.1"), code, ist("Unauthorized"));
4757 if (!sl)
4758 goto fail;
4759 sl->info.res.status = status;
4760 s->txn->status = status;
4761
4762 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
4763 goto fail;
4764
Willy Tarreaub5ba2b02019-06-11 16:08:25 +02004765 if (!htx_add_header(htx, ist("Content-length"), ist("112")) ||
4766 !htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Faulet12c51e22018-11-28 15:59:42 +01004767 !htx_add_header(htx, ist("Connection"), ist("close")) ||
Jérôme Magnin86cef232018-12-28 14:49:08 +01004768 !htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4769 goto fail;
4770 if (status == 401 && !htx_add_header(htx, ist("WWW-Authenticate"), ist2(trash.area, trash.data)))
4771 goto fail;
4772 if (status == 407 && !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
Christopher Faulet12c51e22018-11-28 15:59:42 +01004773 goto fail;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004774 if (!htx_add_endof(htx, HTX_BLK_EOH))
4775 goto fail;
4776
4777 while (body.len) {
4778 size_t sent = htx_add_data(htx, body);
4779 if (!sent)
4780 goto fail;
4781 body.ptr += sent;
4782 body.len -= sent;
4783 }
4784
4785 if (!htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet12c51e22018-11-28 15:59:42 +01004786 goto fail;
4787
Christopher Faulet06511812019-10-16 09:38:27 +02004788 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet12c51e22018-11-28 15:59:42 +01004789 data = htx->data - co_data(res);
Christopher Faulet12c51e22018-11-28 15:59:42 +01004790 c_adv(res, data);
4791 res->total += data;
4792
4793 channel_auto_read(&s->req);
4794 channel_abort(&s->req);
4795 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004796 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet12c51e22018-11-28 15:59:42 +01004797
4798 res->wex = tick_add_ifset(now_ms, res->wto);
4799 channel_auto_read(res);
4800 channel_auto_close(res);
4801 channel_shutr_now(res);
4802 return 0;
4803
4804 fail:
4805 /* If an error occurred, remove the incomplete HTTP response from the
4806 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004807 channel_htx_truncate(res, htx);
Christopher Faulet12c51e22018-11-28 15:59:42 +01004808 return -1;
4809}
4810
Christopher Faulet0f226952018-10-22 09:29:56 +02004811/*
4812 * Capture headers from message <htx> according to header list <cap_hdr>, and
4813 * fill the <cap> pointers appropriately.
4814 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004815static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004816{
4817 struct cap_hdr *h;
4818 int32_t pos;
4819
Christopher Fauleta3f15502019-05-13 15:27:23 +02004820 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004821 struct htx_blk *blk = htx_get_blk(htx, pos);
4822 enum htx_blk_type type = htx_get_blk_type(blk);
4823 struct ist n, v;
4824
4825 if (type == HTX_BLK_EOH)
4826 break;
4827 if (type != HTX_BLK_HDR)
4828 continue;
4829
4830 n = htx_get_blk_name(htx, blk);
4831
4832 for (h = cap_hdr; h; h = h->next) {
4833 if (h->namelen && (h->namelen == n.len) &&
4834 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4835 if (cap[h->index] == NULL)
4836 cap[h->index] =
4837 pool_alloc(h->pool);
4838
4839 if (cap[h->index] == NULL) {
4840 ha_alert("HTTP capture : out of memory.\n");
4841 break;
4842 }
4843
4844 v = htx_get_blk_value(htx, blk);
4845 if (v.len > h->len)
4846 v.len = h->len;
4847
4848 memcpy(cap[h->index], v.ptr, v.len);
4849 cap[h->index][v.len]=0;
4850 }
4851 }
4852 }
4853}
4854
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004855/* Delete a value in a header between delimiters <from> and <next>. The header
4856 * itself is delimited by <start> and <end> pointers. The number of characters
4857 * displaced is returned, and the pointer to the first delimiter is updated if
4858 * required. The function tries as much as possible to respect the following
4859 * principles :
4860 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4861 * in which case <next> is simply removed
4862 * - set exactly one space character after the new first delimiter, unless there
4863 * are not enough characters in the block being moved to do so.
4864 * - remove unneeded spaces before the previous delimiter and after the new
4865 * one.
4866 *
4867 * It is the caller's responsibility to ensure that :
4868 * - <from> points to a valid delimiter or <start> ;
4869 * - <next> points to a valid delimiter or <end> ;
4870 * - there are non-space chars before <from>.
4871 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004872static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004873{
4874 char *prev = *from;
4875
4876 if (prev == start) {
4877 /* We're removing the first value. eat the semicolon, if <next>
4878 * is lower than <end> */
4879 if (next < end)
4880 next++;
4881
4882 while (next < end && HTTP_IS_SPHT(*next))
4883 next++;
4884 }
4885 else {
4886 /* Remove useless spaces before the old delimiter. */
4887 while (HTTP_IS_SPHT(*(prev-1)))
4888 prev--;
4889 *from = prev;
4890
4891 /* copy the delimiter and if possible a space if we're
4892 * not at the end of the line.
4893 */
4894 if (next < end) {
4895 *prev++ = *next++;
4896 if (prev + 1 < next)
4897 *prev++ = ' ';
4898 while (next < end && HTTP_IS_SPHT(*next))
4899 next++;
4900 }
4901 }
4902 memmove(prev, next, end - next);
4903 return (prev - next);
4904}
4905
Christopher Faulet0f226952018-10-22 09:29:56 +02004906
4907/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004908 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004909 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004910static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004911{
4912 struct ist dst = ist2(str, 0);
4913
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004914 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004915 goto end;
4916 if (dst.len + 1 > len)
4917 goto end;
4918 dst.ptr[dst.len++] = ' ';
4919
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004920 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004921 goto end;
4922 if (dst.len + 1 > len)
4923 goto end;
4924 dst.ptr[dst.len++] = ' ';
4925
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004926 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004927 end:
4928 return dst.len;
4929}
4930
4931/*
4932 * Print a debug line with a start line.
4933 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004934static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004935{
4936 struct session *sess = strm_sess(s);
4937 int max;
4938
4939 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4940 dir,
4941 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4942 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4943
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004944 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004945 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004946 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004947 trash.area[trash.data++] = ' ';
4948
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004949 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004950 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004951 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004952 trash.area[trash.data++] = ' ';
4953
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004954 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004955 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004956 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004957 trash.area[trash.data++] = '\n';
4958
4959 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
4960}
4961
4962/*
4963 * Print a debug line with a header.
4964 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004965static 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 +02004966{
4967 struct session *sess = strm_sess(s);
4968 int max;
4969
4970 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4971 dir,
4972 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4973 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4974
4975 max = n.len;
4976 UBOUND(max, trash.size - trash.data - 3);
4977 chunk_memcat(&trash, n.ptr, max);
4978 trash.area[trash.data++] = ':';
4979 trash.area[trash.data++] = ' ';
4980
4981 max = v.len;
4982 UBOUND(max, trash.size - trash.data - 1);
4983 chunk_memcat(&trash, v.ptr, max);
4984 trash.area[trash.data++] = '\n';
4985
4986 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
4987}
4988
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004989/* Allocate a new HTTP transaction for stream <s> unless there is one already.
4990 * In case of allocation failure, everything allocated is freed and NULL is
4991 * returned. Otherwise the new transaction is assigned to the stream and
4992 * returned.
4993 */
4994struct http_txn *http_alloc_txn(struct stream *s)
4995{
4996 struct http_txn *txn = s->txn;
4997
4998 if (txn)
4999 return txn;
5000
5001 txn = pool_alloc(pool_head_http_txn);
5002 if (!txn)
5003 return txn;
5004
5005 s->txn = txn;
5006 return txn;
5007}
5008
5009void http_txn_reset_req(struct http_txn *txn)
5010{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005011 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005012 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5013}
5014
5015void http_txn_reset_res(struct http_txn *txn)
5016{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005017 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005018 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5019}
5020
5021/*
5022 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5023 * the required fields are properly allocated and that we only need to (re)init
5024 * them. This should be used before processing any new request.
5025 */
5026void http_init_txn(struct stream *s)
5027{
5028 struct http_txn *txn = s->txn;
5029 struct conn_stream *cs = objt_cs(s->si[0].end);
5030
5031 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5032 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5033 : 0);
5034 txn->status = -1;
Christopher Faulet473e8802020-01-14 11:12:37 +01005035 txn->errmsg = NULL;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005036 *(unsigned int *)txn->cache_hash = 0;
5037
5038 txn->cookie_first_date = 0;
5039 txn->cookie_last_date = 0;
5040
5041 txn->srv_cookie = NULL;
5042 txn->cli_cookie = NULL;
5043 txn->uri = NULL;
5044
5045 http_txn_reset_req(txn);
5046 http_txn_reset_res(txn);
5047
5048 txn->req.chn = &s->req;
5049 txn->rsp.chn = &s->res;
5050
5051 txn->auth.method = HTTP_AUTH_UNKNOWN;
5052
5053 vars_init(&s->vars_txn, SCOPE_TXN);
5054 vars_init(&s->vars_reqres, SCOPE_REQ);
5055}
5056
5057/* to be used at the end of a transaction */
5058void http_end_txn(struct stream *s)
5059{
5060 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005061
5062 /* these ones will have been dynamically allocated */
5063 pool_free(pool_head_requri, txn->uri);
5064 pool_free(pool_head_capture, txn->cli_cookie);
5065 pool_free(pool_head_capture, txn->srv_cookie);
5066 pool_free(pool_head_uniqueid, s->unique_id);
5067
5068 s->unique_id = NULL;
5069 txn->uri = NULL;
5070 txn->srv_cookie = NULL;
5071 txn->cli_cookie = NULL;
5072
Christopher Faulet59399252019-11-07 14:27:52 +01005073 if (!LIST_ISEMPTY(&s->vars_txn.head))
5074 vars_prune(&s->vars_txn, s->sess, s);
5075 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5076 vars_prune(&s->vars_reqres, s->sess, s);
5077}
5078
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005079
5080DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
5081DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
Christopher Faulet0f226952018-10-22 09:29:56 +02005082
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005083__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005084static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005085{
5086}
5087
5088
5089/*
5090 * Local variables:
5091 * c-indent-level: 8
5092 * c-basic-offset: 8
5093 * End:
5094 */