blob: c6e0bc7b5189c117afa566e41cbdd767f60b3f42 [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 Fauletfc9cfe42019-07-16 14:54:53 +020054static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s, int *deny_status);
55static 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;
475 int deny_status = HTTP_ERR_403;
476 struct connection *conn = objt_conn(sess->origin);
477
478 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
479 /* we need more data */
480 goto return_prx_yield;
481 }
482
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100483 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200484
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100485 htx = htxbuf(&req->buf);
Christopher Fauletff2759f2018-10-24 11:13:16 +0200486
Christopher Faulet1907ccc2019-04-29 13:12:02 +0200487 /* just in case we have some per-backend tracking. Only called the first
488 * execution of the analyser. */
489 if (!s->current_rule || s->current_rule_list != &px->http_req_rules)
490 stream_inc_be_http_req_ctr(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200491
492 /* evaluate http-request rules */
493 if (!LIST_ISEMPTY(&px->http_req_rules)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200494 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200495
496 switch (verdict) {
497 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
498 goto return_prx_yield;
499
500 case HTTP_RULE_RES_CONT:
501 case HTTP_RULE_RES_STOP: /* nothing to do */
502 break;
503
504 case HTTP_RULE_RES_DENY: /* deny or tarpit */
505 if (txn->flags & TX_CLTARPIT)
506 goto tarpit;
507 goto deny;
508
509 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
510 goto return_prx_cond;
511
512 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
513 goto done;
514
515 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
516 goto return_bad_req;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100517
518 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
519 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200520 }
521 }
522
523 if (conn && (conn->flags & CO_FL_EARLY_DATA) &&
524 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) {
Christopher Fauletff2759f2018-10-24 11:13:16 +0200525 struct http_hdr_ctx ctx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200526
Christopher Fauletff2759f2018-10-24 11:13:16 +0200527 ctx.blk = NULL;
528 if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) {
529 if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100530 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200531 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200532 }
533
534 /* OK at this stage, we know that the request was accepted according to
535 * the http-request rules, we can check for the stats. Note that the
536 * URI is detected *before* the req* rules in order not to be affected
537 * by a possible reqrep, while they are processed *after* so that a
538 * reqdeny can still block them. This clearly needs to change in 1.6!
539 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200540 if (!s->target && http_stats_check_uri(s, txn, px)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200541 s->target = &http_stats_applet.obj_type;
Willy Tarreau14bfe9a2018-12-19 15:19:27 +0100542 if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200543 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200544 if (!(s->flags & SF_ERR_MASK))
545 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100546 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200547 }
548
549 /* parse the whole stats request and extract the relevant information */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200550 http_handle_stats(s, req);
551 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200552 /* not all actions implemented: deny, allow, auth */
553
554 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
555 goto deny;
556
557 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
558 goto return_prx_cond;
Christopher Faulet3a26bee2019-12-16 12:47:40 +0100559
560 if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */
561 goto return_bad_req;
562
563 if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */
564 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200565 }
566
Christopher Faulet2571bc62019-03-01 11:44:26 +0100567 /* Proceed with the applets now. */
568 if (unlikely(objt_applet(s->target))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200569 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Olivier Houcharda798bf52019-03-08 18:52:00 +0100570 _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200571
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200572 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +0100573 goto return_int_err;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100574
Christopher Faulete0768eb2018-10-03 16:38:02 +0200575 if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is
576 s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy
577 if (!(s->flags & SF_FINST_MASK))
578 s->flags |= SF_FINST_R;
579
580 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
581 req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END);
582 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
583 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Christopher Fauletbcf242a2019-03-01 11:36:26 +0100584
585 req->flags |= CF_SEND_DONTWAIT;
586 s->flags |= SF_ASSIGNED;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200587 goto done;
588 }
589
590 /* check whether we have some ACLs set to redirect this request */
591 list_for_each_entry(rule, &px->redirect_rules, list) {
592 if (rule->cond) {
593 int ret;
594
595 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
596 ret = acl_pass(ret);
597 if (rule->cond->pol == ACL_COND_UNLESS)
598 ret = !ret;
599 if (!ret)
600 continue;
601 }
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200602 if (!http_apply_redirect_rule(rule, s, txn))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100603 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200604 goto done;
605 }
606
607 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
608 * If this happens, then the data will not come immediately, so we must
609 * send all what we have without waiting. Note that due to the small gain
610 * in waiting for the body of the request, it's easier to simply put the
611 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
612 * itself once used.
613 */
614 req->flags |= CF_SEND_DONTWAIT;
615
616 done: /* done with this analyser, continue with next ones that the calling
617 * points will have set, if any.
618 */
619 req->analyse_exp = TICK_ETERNITY;
620 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
621 req->analysers &= ~an_bit;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100622 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200623 return 1;
624
625 tarpit:
626 /* Allow cookie logging
627 */
628 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200629 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200630
631 /* When a connection is tarpitted, we use the tarpit timeout,
632 * which may be the same as the connect timeout if unspecified.
633 * If unset, then set it to zero because we really want it to
634 * eventually expire. We build the tarpit as an analyser.
635 */
Christopher Faulet202c6ce2019-01-07 14:57:35 +0100636 channel_htx_erase(&s->req, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200637
638 /* wipe the request out so that we can drop the connection early
639 * if the client closes first.
640 */
641 channel_dont_connect(req);
642
643 txn->status = http_err_codes[deny_status];
644
645 req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */
646 req->analysers |= AN_REQ_HTTP_TARPIT;
647 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
648 if (!req->analyse_exp)
649 req->analyse_exp = tick_add(now_ms, 0);
650 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100651 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100652 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100653 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200654 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100655 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200656 goto done_without_exp;
657
658 deny: /* this request was blocked (denied) */
659
660 /* Allow cookie logging
661 */
662 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200663 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200664
665 txn->flags |= TX_CLDENY;
666 txn->status = http_err_codes[deny_status];
667 s->logs.tv_request = now;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200668 stream_inc_http_err_ctr(s);
Olivier Houcharda798bf52019-03-08 18:52:00 +0100669 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100670 if (s->flags & SF_BE_ASSIGNED)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100671 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200672 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100673 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100674 goto return_prx_err;
675
676 return_int_err:
677 txn->status = 500;
678 if (!(s->flags & SF_ERR_MASK))
679 s->flags |= SF_ERR_INTERNAL;
680 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100681 if (s->flags & SF_BE_ASSIGNED)
682 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100683 if (sess->listener->counters)
684 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
685 goto return_prx_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200686
687 return_bad_req:
Christopher Faulete0768eb2018-10-03 16:38:02 +0200688 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100689 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200690 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100691 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100692 /* fall through */
693
694 return_prx_err:
695 http_reply_and_close(s, txn->status, http_error_message(s));
696 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200697
698 return_prx_cond:
699 if (!(s->flags & SF_ERR_MASK))
700 s->flags |= SF_ERR_PRXCOND;
701 if (!(s->flags & SF_FINST_MASK))
702 s->flags |= SF_FINST_R;
703
704 req->analysers &= AN_REQ_FLT_END;
705 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100706 DBG_TRACE_DEVEL("leaving on error",
707 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200708 return 0;
709
710 return_prx_yield:
711 channel_dont_connect(req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100712 DBG_TRACE_DEVEL("waiting for more data",
713 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200714 return 0;
715}
716
717/* This function performs all the processing enabled for the current request.
718 * It returns 1 if the processing can continue on next analysers, or zero if it
719 * needs more data, encounters an error, or wants to immediately abort the
720 * request. It relies on buffers flags, and updates s->req.analysers.
721 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200722int http_process_request(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200723{
724 struct session *sess = s->sess;
725 struct http_txn *txn = s->txn;
726 struct http_msg *msg = &txn->req;
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200727 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200728 struct connection *cli_conn = objt_conn(strm_sess(s)->origin);
729
730 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
731 /* we need more data */
732 channel_dont_connect(req);
733 return 0;
734 }
735
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100736 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200737
738 /*
739 * Right now, we know that we have processed the entire headers
740 * and that unwanted requests have been filtered out. We can do
741 * whatever we want with the remaining request. Also, now we
742 * may have separate values for ->fe, ->be.
743 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100744 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200745
746 /*
747 * If HTTP PROXY is set we simply get remote server address parsing
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200748 * incoming request.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200749 */
750 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100751 struct htx_sl *sl;
752 struct ist uri, path;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200753
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200754 if (!sockaddr_alloc(&s->target_addr)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200755 if (!(s->flags & SF_ERR_MASK))
756 s->flags |= SF_ERR_RESOURCE;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100757 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200758 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200759 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100760 uri = htx_sl_req_uri(sl);
761 path = http_get_path(uri);
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200762
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200763 if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200764 goto return_bad_req;
765
Willy Tarreau1c8d32b2019-07-18 15:47:45 +0200766 s->target = &s->be->obj_type;
767 s->flags |= SF_ADDR_SET | SF_ASSIGNED;
768
Christopher Faulete0768eb2018-10-03 16:38:02 +0200769 /* if the path was found, we have to remove everything between
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200770 * uri.ptr and path.ptr (excluded). If it was not found, we need
771 * to replace from all the uri by a single "/".
772 *
773 * Instead of rewritting the whole start line, we just update
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100774 * the star-line URI. Some space will be lost but it should be
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200775 * insignificant.
Christopher Faulete0768eb2018-10-03 16:38:02 +0200776 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100777 istcpy(&uri, (path.len ? path : ist("/")), uri.len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200778 }
779
780 /*
781 * 7: Now we can work with the cookies.
782 * Note that doing so might move headers in the request, but
783 * the fields will stay coherent and the URI will not move.
784 * This should only be performed in the backend.
785 */
786 if (s->be->cookie_name || sess->fe->capture_name)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200787 http_manage_client_side_cookies(s, req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200788
789 /* add unique-id if "header-unique-id" is specified */
790
791 if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) {
Christopher Fauletb8a53712019-12-16 11:29:38 +0100792 if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) {
793 if (!(s->flags & SF_ERR_MASK))
794 s->flags |= SF_ERR_RESOURCE;
795 goto return_int_err;
796 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200797 s->unique_id[0] = '\0';
798 build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id);
799 }
800
801 if (sess->fe->header_unique_id && s->unique_id) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200802 struct ist n = ist2(sess->fe->header_unique_id, strlen(sess->fe->header_unique_id));
803 struct ist v = ist2(s->unique_id, strlen(s->unique_id));
804
805 if (unlikely(!http_add_header(htx, n, v)))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100806 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200807 }
808
809 /*
810 * 9: add X-Forwarded-For if either the frontend or the backend
811 * asks for it.
812 */
813 if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200814 struct http_hdr_ctx ctx = { .blk = NULL };
815 struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name,
816 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len);
817
Christopher Faulete0768eb2018-10-03 16:38:02 +0200818 if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200819 http_find_header(htx, hdr, &ctx, 0)) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200820 /* The header is set to be added only if none is present
821 * and we found it, so don't do anything.
822 */
823 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200824 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200825 /* Add an X-Forwarded-For header unless the source IP is
826 * in the 'except' network range.
827 */
828 if ((!sess->fe->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200829 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200830 != sess->fe->except_net.s_addr) &&
831 (!s->be->except_mask.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200832 (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200833 != s->be->except_net.s_addr)) {
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200834 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200835
836 /* Note: we rely on the backend to get the header name to be used for
837 * x-forwarded-for, because the header is really meant for the backends.
838 * However, if the backend did not specify any option, we have to rely
839 * on the frontend's header name.
840 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200841 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
842 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100843 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200844 }
845 }
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200846 else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200847 /* FIXME: for the sake of completeness, we should also support
848 * 'except' here, although it is mostly useless in this case.
849 */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200850 char pn[INET6_ADDRSTRLEN];
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200851
Christopher Faulete0768eb2018-10-03 16:38:02 +0200852 inet_ntop(AF_INET6,
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200853 (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr,
Christopher Faulete0768eb2018-10-03 16:38:02 +0200854 pn, sizeof(pn));
855
856 /* Note: we rely on the backend to get the header name to be used for
857 * x-forwarded-for, because the header is really meant for the backends.
858 * However, if the backend did not specify any option, we have to rely
859 * on the frontend's header name.
860 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200861 chunk_printf(&trash, "%s", pn);
862 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100863 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200864 }
865 }
866
867 /*
868 * 10: add X-Original-To if either the frontend or the backend
869 * asks for it.
870 */
871 if ((sess->fe->options | s->be->options) & PR_O_ORGTO) {
872
873 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200874 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 +0200875 /* Add an X-Original-To header unless the destination IP is
876 * in the 'except' network range.
877 */
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200878 if (cli_conn->dst->ss_family == AF_INET &&
Christopher Faulete0768eb2018-10-03 16:38:02 +0200879 ((!sess->fe->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200880 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200881 != sess->fe->except_to.s_addr) &&
882 (!s->be->except_mask_to.s_addr ||
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200883 (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200884 != s->be->except_to.s_addr))) {
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200885 struct ist hdr;
Willy Tarreaua48f4b32019-07-17 15:11:59 +0200886 unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200887
888 /* Note: we rely on the backend to get the header name to be used for
889 * x-original-to, because the header is really meant for the backends.
890 * However, if the backend did not specify any option, we have to rely
891 * on the frontend's header name.
892 */
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200893 if (s->be->orgto_hdr_len)
894 hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len);
895 else
896 hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200897
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200898 chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
899 if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +0100900 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +0200901 }
902 }
Christopher Faulete0768eb2018-10-03 16:38:02 +0200903 }
904
Christopher Faulete0768eb2018-10-03 16:38:02 +0200905 /* If we have no server assigned yet and we're balancing on url_param
906 * with a POST request, we may be interested in checking the body for
907 * that parameter. This will be done in another analyser.
908 */
909 if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) &&
Willy Tarreau089eaa02019-01-14 15:17:46 +0100910 s->txn->meth == HTTP_METH_POST &&
911 (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) {
Christopher Faulete0768eb2018-10-03 16:38:02 +0200912 channel_dont_connect(req);
913 req->analysers |= AN_REQ_HTTP_BODY;
914 }
915
916 req->analysers &= ~AN_REQ_FLT_XFER_DATA;
917 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau1a18b542018-12-11 16:37:42 +0100918
Christopher Faulete0768eb2018-10-03 16:38:02 +0200919 /* We expect some data from the client. Unless we know for sure
920 * we already have a full request, we have to re-enable quick-ack
921 * in case we previously disabled it, otherwise we might cause
922 * the client to delay further data.
923 */
924 if ((sess->listener->options & LI_O_NOQUICKACK) &&
Christopher Fauletd7bdfb12018-10-24 11:14:34 +0200925 (htx_get_tail_type(htx) != HTX_BLK_EOM))
Willy Tarreau1a18b542018-12-11 16:37:42 +0100926 conn_set_quickack(cli_conn, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200927
928 /*************************************************************
929 * OK, that's finished for the headers. We have done what we *
930 * could. Let's switch to the DATA state. *
931 ************************************************************/
932 req->analyse_exp = TICK_ETERNITY;
933 req->analysers &= ~an_bit;
934
935 s->logs.tv_request = now;
936 /* OK let's go on with the BODY now */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100937 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200938 return 1;
939
Christopher Fauletb8a53712019-12-16 11:29:38 +0100940 return_int_err:
941 txn->status = 500;
942 if (!(s->flags & SF_ERR_MASK))
943 s->flags |= SF_ERR_INTERNAL;
944 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100945 if (s->flags & SF_BE_ASSIGNED)
946 _HA_ATOMIC_ADD(&sess->fe->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100947 if (sess->listener->counters)
948 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
949 goto return_prx_cond;
950
Christopher Faulete0768eb2018-10-03 16:38:02 +0200951 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +0200952 txn->status = 400;
Olivier Houcharda798bf52019-03-08 18:52:00 +0100953 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200954 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +0100955 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +0100956 /* fall through */
957
958 return_prx_cond:
959 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +0200960
961 if (!(s->flags & SF_ERR_MASK))
962 s->flags |= SF_ERR_PRXCOND;
963 if (!(s->flags & SF_FINST_MASK))
964 s->flags |= SF_FINST_R;
Christopher Fauletb8a53712019-12-16 11:29:38 +0100965
966 req->analysers &= AN_REQ_FLT_END;
967 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100968 DBG_TRACE_DEVEL("leaving on error",
969 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200970 return 0;
971}
972
973/* This function is an analyser which processes the HTTP tarpit. It always
974 * returns zero, at the beginning because it prevents any other processing
975 * from occurring, and at the end because it terminates the request.
976 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +0200977int http_process_tarpit(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +0200978{
979 struct http_txn *txn = s->txn;
980
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100981 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200982 /* This connection is being tarpitted. The CLIENT side has
983 * already set the connect expiration date to the right
984 * timeout. We just have to check that the client is still
985 * there and that the timeout has not expired.
986 */
987 channel_dont_connect(req);
988 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100989 !tick_is_expired(req->analyse_exp, now_ms)) {
990 DBG_TRACE_DEVEL("waiting for tarpit timeout expiry",
991 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +0200992 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100993 }
994
Christopher Faulete0768eb2018-10-03 16:38:02 +0200995
996 /* We will set the queue timer to the time spent, just for
997 * logging purposes. We fake a 500 server error, so that the
998 * attacker will not suspect his connection has been tarpitted.
999 * It will not cause trouble to the logs because we can exclude
1000 * the tarpitted connections by filtering on the 'PT' status flags.
1001 */
1002 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1003
1004 if (!(req->flags & CF_READ_ERROR))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001005 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001006
1007 req->analysers &= AN_REQ_FLT_END;
1008 req->analyse_exp = TICK_ETERNITY;
1009
1010 if (!(s->flags & SF_ERR_MASK))
1011 s->flags |= SF_ERR_PRXCOND;
1012 if (!(s->flags & SF_FINST_MASK))
1013 s->flags |= SF_FINST_T;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001014
1015 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001016 return 0;
1017}
1018
1019/* This function is an analyser which waits for the HTTP request body. It waits
1020 * for either the buffer to be full, or the full advertised contents to have
1021 * reached the buffer. It must only be called after the standard HTTP request
1022 * processing has occurred, because it expects the request to be parsed and will
1023 * look for the Expect header. It may send a 100-Continue interim response. It
1024 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
1025 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
1026 * needs to read more data, or 1 once it has completed its analysis.
1027 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001028int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001029{
1030 struct session *sess = s->sess;
1031 struct http_txn *txn = s->txn;
1032 struct http_msg *msg = &s->txn->req;
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001033 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001034
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001035 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001036
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001037 htx = htxbuf(&req->buf);
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001038
Willy Tarreau4236f032019-03-05 10:43:32 +01001039 if (htx->flags & HTX_FL_PARSING_ERROR)
1040 goto return_bad_req;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001041 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1042 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001043
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001044 if (msg->msg_state < HTTP_MSG_BODY)
1045 goto missing_data;
Christopher Faulet9768c262018-10-22 09:34:31 +02001046
Christopher Faulete0768eb2018-10-03 16:38:02 +02001047 /* We have to parse the HTTP request body to find any required data.
1048 * "balance url_param check_post" should have been the only way to get
1049 * into this. We were brought here after HTTP header analysis, so all
1050 * related structures are ready.
1051 */
1052
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001053 if (msg->msg_state < HTTP_MSG_DATA) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001054 if (http_handle_expect_hdr(s, htx, msg) == -1)
Christopher Fauletb8a53712019-12-16 11:29:38 +01001055 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001056 }
1057
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001058 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001059
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001060 /* Now we're in HTTP_MSG_DATA. We just need to know if all data have
1061 * been received or if the buffer is full.
Christopher Faulete0768eb2018-10-03 16:38:02 +02001062 */
Christopher Faulet54b5e212019-06-04 10:08:28 +02001063 if (htx_get_tail_type(htx) > HTX_BLK_DATA ||
Christopher Fauletdcd8c5e2019-01-21 11:24:38 +01001064 channel_htx_full(req, htx, global.tune.maxrewrite))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001065 goto http_end;
1066
Christopher Fauletf76ebe82018-10-24 11:16:22 +02001067 missing_data:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001068 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
1069 txn->status = 408;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001070 if (!(s->flags & SF_ERR_MASK))
1071 s->flags |= SF_ERR_CLITO;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001072 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1073 if (sess->listener->counters)
1074 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1075 goto return_prx_cond;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001076 }
1077
1078 /* we get here if we need to wait for more data */
1079 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
1080 /* Not enough data. We'll re-use the http-request
1081 * timeout here. Ideally, we should set the timeout
1082 * relative to the accept() date. We just set the
1083 * request timeout once at the beginning of the
1084 * request.
1085 */
1086 channel_dont_connect(req);
1087 if (!tick_isset(req->analyse_exp))
1088 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001089 DBG_TRACE_DEVEL("waiting for more data",
1090 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001091 return 0;
1092 }
1093
1094 http_end:
1095 /* The situation will not evolve, so let's give up on the analysis. */
1096 s->logs.tv_request = now; /* update the request timer to reflect full request */
1097 req->analysers &= ~an_bit;
1098 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001099 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001100 return 1;
1101
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001102 return_int_err:
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001103 txn->status = 500;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001104 if (!(s->flags & SF_ERR_MASK))
1105 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001106 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001107 if (s->flags & SF_BE_ASSIGNED)
1108 _HA_ATOMIC_ADD(&sess->fe->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001109 if (sess->listener->counters)
1110 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
1111 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001112
Christopher Faulete0768eb2018-10-03 16:38:02 +02001113 return_bad_req: /* let's centralize all bad requests */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001114 txn->status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001115 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
1116 if (sess->listener->counters)
1117 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
1118 /* fall through */
1119
1120 return_prx_cond:
1121 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001122
1123 if (!(s->flags & SF_ERR_MASK))
1124 s->flags |= SF_ERR_PRXCOND;
1125 if (!(s->flags & SF_FINST_MASK))
Christopher Fauletb8a53712019-12-16 11:29:38 +01001126 s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001127
Christopher Faulete0768eb2018-10-03 16:38:02 +02001128 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001129 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001130 DBG_TRACE_DEVEL("leaving on error",
1131 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001132 return 0;
1133}
1134
1135/* This function is an analyser which forwards request body (including chunk
1136 * sizes if any). It is called as soon as we must forward, even if we forward
1137 * zero byte. The only situation where it must not be called is when we're in
1138 * tunnel mode and we want to forward till the close. It's used both to forward
1139 * remaining data and to resync after end of body. It expects the msg_state to
1140 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
1141 * read more data, or 1 once we can go on with next request or end the stream.
1142 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
1143 * bytes of pending data + the headers if not already done.
1144 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001145int http_request_forward_body(struct stream *s, struct channel *req, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001146{
1147 struct session *sess = s->sess;
1148 struct http_txn *txn = s->txn;
Christopher Faulet9768c262018-10-22 09:34:31 +02001149 struct http_msg *msg = &txn->req;
1150 struct htx *htx;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001151 short status = 0;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001152 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001153
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001154 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001155
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001156 htx = htxbuf(&req->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001157
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001158 if (htx->flags & HTX_FL_PARSING_ERROR)
1159 goto return_bad_req;
1160 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1161 goto return_int_err;
1162
Christopher Faulete0768eb2018-10-03 16:38:02 +02001163 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
1164 ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) {
1165 /* Output closed while we were sending data. We must abort and
1166 * wake the other side up.
1167 */
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001168
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001169 /* Don't abort yet if we had L7 retries activated and it
1170 * was a write error, we may recover.
1171 */
1172 if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001173 (s->si[1].flags & SI_FL_L7_RETRY)) {
1174 DBG_TRACE_DEVEL("leaving on L7 retry",
1175 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Olivier Houchard29cac3c2019-07-12 15:48:58 +02001176 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001177 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001178 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001179 http_end_request(s);
1180 http_end_response(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001181 DBG_TRACE_DEVEL("leaving on error",
1182 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001183 return 1;
1184 }
1185
1186 /* Note that we don't have to send 100-continue back because we don't
1187 * need the data to complete our job, and it's up to the server to
1188 * decide whether to return 100, 417 or anything else in return of
1189 * an "Expect: 100-continue" header.
1190 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001191 if (msg->msg_state == HTTP_MSG_BODY)
1192 msg->msg_state = HTTP_MSG_DATA;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001193
Christopher Faulete0768eb2018-10-03 16:38:02 +02001194 /* in most states, we should abort in case of early close */
1195 channel_auto_close(req);
1196
1197 if (req->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01001198 if (req->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001199 if (req->flags & CF_EOI)
1200 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01001201 }
1202 else {
1203 /* We can't process the buffer's contents yet */
1204 req->flags |= CF_WAKE_WRITE;
1205 goto missing_data_or_waiting;
1206 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001207 }
1208
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001209 if (msg->msg_state >= HTTP_MSG_ENDING)
1210 goto ending;
1211
1212 if (txn->meth == HTTP_METH_CONNECT) {
1213 msg->msg_state = HTTP_MSG_ENDING;
1214 goto ending;
1215 }
1216
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001217 /* Forward input data. We get it by removing all outgoing data not
1218 * forwarded yet from HTX data size. If there are some data filters, we
1219 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02001220 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001221 if (HAS_REQ_DATA_FILTERS(s)) {
1222 ret = flt_http_payload(s, msg, htx->data);
1223 if (ret < 0)
1224 goto return_bad_req;
Christopher Faulet421e7692019-06-13 11:16:45 +02001225 c_adv(req, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001226 }
1227 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02001228 c_adv(req, htx->data - co_data(req));
Christopher Faulet66af0b22019-03-22 14:54:52 +01001229 if (msg->flags & HTTP_MSGF_XFER_LEN)
1230 channel_htx_forward_forever(req, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001231 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001232
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001233 if (htx->data != co_data(req))
1234 goto missing_data_or_waiting;
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001235
Christopher Faulet9768c262018-10-22 09:34:31 +02001236 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001237 * in HTTP_MSG_ENDING state. Then if all data was marked to be
1238 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02001239 */
1240 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
1241 goto missing_data_or_waiting;
1242
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001243 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02001244
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001245 ending:
1246 /* other states, ENDING...TUNNEL */
1247 if (msg->msg_state >= HTTP_MSG_DONE)
1248 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001249
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001250 if (HAS_REQ_DATA_FILTERS(s)) {
1251 ret = flt_http_end(s, msg);
1252 if (ret <= 0) {
1253 if (!ret)
1254 goto missing_data_or_waiting;
1255 goto return_bad_req;
1256 }
1257 }
1258
Christopher Faulet1a3e0272019-11-15 16:31:46 +01001259 if (txn->meth == HTTP_METH_CONNECT)
1260 msg->msg_state = HTTP_MSG_TUNNEL;
1261 else {
1262 msg->msg_state = HTTP_MSG_DONE;
1263 req->to_forward = 0;
1264 }
1265
1266 done:
1267 /* we don't want to forward closes on DONE except in tunnel mode. */
1268 if (!(txn->flags & TX_CON_WANT_TUN))
1269 channel_dont_close(req);
1270
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001271 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001272 if (!(req->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001273 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001274 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
1275 if (req->flags & CF_SHUTW) {
1276 /* request errors are most likely due to the
1277 * server aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01001278 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001279 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001280 goto return_bad_req;
1281 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001282 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001283 return 1;
1284 }
1285
1286 /* If "option abortonclose" is set on the backend, we want to monitor
1287 * the client's connection and forward any shutdown notification to the
1288 * server, which will decide whether to close or to go on processing the
1289 * request. We only do that in tunnel mode, and not in other modes since
1290 * it can be abused to exhaust source ports. */
Christopher Faulet769d0e92019-03-22 14:23:18 +01001291 if (s->be->options & PR_O_ABRT_CLOSE) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001292 channel_auto_read(req);
Christopher Fauletc41547b2019-07-16 14:32:23 +02001293 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN))
Christopher Faulete0768eb2018-10-03 16:38:02 +02001294 s->si[1].flags |= SI_FL_NOLINGER;
1295 channel_auto_close(req);
1296 }
1297 else if (s->txn->meth == HTTP_METH_POST) {
1298 /* POST requests may require to read extra CRLF sent by broken
1299 * browsers and which could cause an RST to be sent upon close
1300 * on some systems (eg: Linux). */
1301 channel_auto_read(req);
1302 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001303 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
1304 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001305 return 0;
1306
1307 missing_data_or_waiting:
1308 /* stop waiting for data if the input is closed before the end */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02001309 if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001310 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001311
1312 waiting:
1313 /* waiting for the last bits to leave the buffer */
1314 if (req->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01001315 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001316
1317 /* When TE: chunked is used, we need to get there again to parse remaining
1318 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
1319 * And when content-length is used, we never want to let the possible
1320 * shutdown be forwarded to the other side, as the state machine will
1321 * take care of it once the client responds. It's also important to
1322 * prevent TIME_WAITs from accumulating on the backend side, and for
1323 * HTTP/2 where the last frame comes with a shutdown.
1324 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001325 if (msg->flags & HTTP_MSGF_XFER_LEN)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001326 channel_dont_close(req);
1327
1328 /* We know that more data are expected, but we couldn't send more that
1329 * what we did. So we always set the CF_EXPECT_MORE flag so that the
1330 * system knows it must not set a PUSH on this first part. Interactive
1331 * modes are already handled by the stream sock layer. We must not do
1332 * this in content-length mode because it could present the MSG_MORE
1333 * flag with the last block of forwarded data, which would cause an
1334 * additional delay to be observed by the receiver.
1335 */
1336 if (msg->flags & HTTP_MSGF_TE_CHNK)
1337 req->flags |= CF_EXPECT_MORE;
1338
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001339 DBG_TRACE_DEVEL("waiting for more data to forward",
1340 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001341 return 0;
1342
Christopher Faulet93e02d82019-03-08 14:18:50 +01001343 return_cli_abort:
1344 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1345 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001346 if (sess->listener->counters)
1347 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001348 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001349 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001350 if (!(s->flags & SF_ERR_MASK))
1351 s->flags |= SF_ERR_CLICL;
1352 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001353 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001354
1355 return_srv_abort:
1356 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
1357 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001358 if (sess->listener->counters)
1359 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001360 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01001361 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001362 if (!(s->flags & SF_ERR_MASK))
1363 s->flags |= SF_ERR_SRVCL;
1364 status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001365 goto return_prx_cond;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001366
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001367 return_int_err:
1368 if (!(s->flags & SF_ERR_MASK))
1369 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001370 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001371 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001372 if (sess->listener->counters)
1373 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001374 if (objt_server(s->target))
1375 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001376 status = 500;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001377 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001378
Christopher Faulet93e02d82019-03-08 14:18:50 +01001379 return_bad_req:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001380 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001381 if (sess->listener->counters)
Olivier Houcharda798bf52019-03-08 18:52:00 +01001382 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01001383 status = 400;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001384 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001385
Christopher Fauletb8a53712019-12-16 11:29:38 +01001386 return_prx_cond:
Christopher Faulet9768c262018-10-22 09:34:31 +02001387 if (txn->status > 0) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001388 /* Note: we don't send any error if some data were already sent */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001389 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001390 } else {
Christopher Faulet93e02d82019-03-08 14:18:50 +01001391 txn->status = status;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001392 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001393 }
1394 req->analysers &= AN_REQ_FLT_END;
1395 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 +01001396 if (!(s->flags & SF_ERR_MASK))
1397 s->flags |= SF_ERR_PRXCOND;
Christopher Faulet93e02d82019-03-08 14:18:50 +01001398 if (!(s->flags & SF_FINST_MASK))
1399 s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001400 DBG_TRACE_DEVEL("leaving on error ",
1401 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001402 return 0;
1403}
1404
Olivier Houcharda254a372019-04-05 15:30:12 +02001405/* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */
1406/* Returns 0 if we can attempt to retry, -1 otherwise */
1407static __inline int do_l7_retry(struct stream *s, struct stream_interface *si)
1408{
1409 struct channel *req, *res;
1410 int co_data;
1411
1412 si->conn_retries--;
1413 if (si->conn_retries < 0)
1414 return -1;
1415
Willy Tarreau223995e2019-05-04 10:38:31 +02001416 if (objt_server(s->target))
1417 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1);
1418 _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
1419
Olivier Houcharda254a372019-04-05 15:30:12 +02001420 req = &s->req;
1421 res = &s->res;
1422 /* Remove any write error from the request, and read error from the response */
1423 req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW);
1424 res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW);
1425 res->analysers = 0;
1426 si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT);
Olivier Houchard4bd58672019-07-12 16:16:59 +02001427 stream_choose_redispatch(s);
Olivier Houcharda254a372019-04-05 15:30:12 +02001428 si->exp = TICK_ETERNITY;
1429 res->rex = TICK_ETERNITY;
1430 res->to_forward = 0;
1431 res->analyse_exp = TICK_ETERNITY;
1432 res->total = 0;
Olivier Houchard4bd58672019-07-12 16:16:59 +02001433 s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL);
Olivier Houcharda254a372019-04-05 15:30:12 +02001434 si_release_endpoint(&s->si[1]);
1435 b_free(&req->buf);
1436 /* Swap the L7 buffer with the channel buffer */
1437 /* We know we stored the co_data as b_data, so get it there */
1438 co_data = b_data(&si->l7_buffer);
1439 b_set_data(&si->l7_buffer, b_size(&si->l7_buffer));
1440 b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer));
1441
1442 co_set_data(req, co_data);
1443 b_reset(&res->buf);
1444 co_set_data(res, 0);
1445 return 0;
1446}
1447
Christopher Faulete0768eb2018-10-03 16:38:02 +02001448/* This stream analyser waits for a complete HTTP response. It returns 1 if the
1449 * processing can continue on next analysers, or zero if it either needs more
1450 * data or wants to immediately abort the response (eg: timeout, error, ...). It
1451 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers
1452 * when it has nothing left to do, and may remove any analyser when it wants to
1453 * abort.
1454 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001455int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001456{
Christopher Faulet9768c262018-10-22 09:34:31 +02001457 /*
1458 * We will analyze a complete HTTP response to check the its syntax.
1459 *
1460 * Once the start line and all headers are received, we may perform a
1461 * capture of the error (if any), and we will set a few fields. We also
1462 * logging and finally headers capture.
1463 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001464 struct session *sess = s->sess;
1465 struct http_txn *txn = s->txn;
1466 struct http_msg *msg = &txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02001467 struct htx *htx;
Olivier Houcharda254a372019-04-05 15:30:12 +02001468 struct stream_interface *si_b = &s->si[1];
Christopher Faulet61608322018-11-23 16:23:45 +01001469 struct connection *srv_conn;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001470 struct htx_sl *sl;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001471 int n;
1472
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001473 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001474
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001475 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001476
Willy Tarreau4236f032019-03-05 10:43:32 +01001477 /* Parsing errors are caught here */
1478 if (htx->flags & HTX_FL_PARSING_ERROR)
1479 goto return_bad_res;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001480 if (htx->flags & HTX_FL_PROCESSING_ERROR)
1481 goto return_int_err;
Willy Tarreau4236f032019-03-05 10:43:32 +01001482
Christopher Faulete0768eb2018-10-03 16:38:02 +02001483 /*
1484 * Now we quickly check if we have found a full valid response.
1485 * If not so, we check the FD and buffer states before leaving.
1486 * A full response is indicated by the fact that we have seen
1487 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
1488 * responses are checked first.
1489 *
1490 * Depending on whether the client is still there or not, we
1491 * may send an error response back or not. Note that normally
1492 * we should only check for HTTP status there, and check I/O
1493 * errors somewhere else.
1494 */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001495 next_one:
Christopher Faulet29f17582019-05-23 11:03:26 +02001496 if (unlikely(htx_is_empty(htx) || htx->first == -1)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001497 /* 1: have we encountered a read error ? */
1498 if (rep->flags & CF_READ_ERROR) {
Olivier Houchard865d8392019-05-03 22:46:27 +02001499 struct connection *conn = NULL;
1500
Olivier Houchard865d8392019-05-03 22:46:27 +02001501 if (objt_cs(s->si[1].end))
1502 conn = objt_cs(s->si[1].end)->conn;
1503
1504 if (si_b->flags & SI_FL_L7_RETRY &&
1505 (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001506 /* If we arrive here, then CF_READ_ERROR was
1507 * set by si_cs_recv() because we matched a
1508 * status, overwise it would have removed
1509 * the SI_FL_L7_RETRY flag, so it's ok not
1510 * to check s->be->retry_type.
1511 */
1512 if (co_data(rep) || do_l7_retry(s, si_b) == 0)
1513 return 0;
1514 }
1515
Olivier Houchard6db16992019-05-17 15:40:49 +02001516 if (txn->flags & TX_NOT_FIRST)
1517 goto abort_keep_alive;
1518
Olivier Houcharda798bf52019-03-08 18:52:00 +01001519 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001520 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001521 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001522 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001523 }
1524
Christopher Faulete0768eb2018-10-03 16:38:02 +02001525 rep->analysers &= AN_RES_FLT_END;
1526 txn->status = 502;
1527
1528 /* Check to see if the server refused the early data.
1529 * If so, just send a 425
1530 */
Olivier Houchard865d8392019-05-03 22:46:27 +02001531 if (conn->err_code == CO_ER_SSL_EARLY_FAILED) {
1532 if ((s->be->retry_type & PR_RE_EARLY_ERROR) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001533 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001534 do_l7_retry(s, si_b) == 0) {
1535 DBG_TRACE_DEVEL("leaving on L7 retry",
1536 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houchard865d8392019-05-03 22:46:27 +02001537 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001538 }
Olivier Houchard865d8392019-05-03 22:46:27 +02001539 txn->status = 425;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001540 }
1541
1542 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001543 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001544
1545 if (!(s->flags & SF_ERR_MASK))
1546 s->flags |= SF_ERR_SRVCL;
1547 if (!(s->flags & SF_FINST_MASK))
1548 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001549 DBG_TRACE_DEVEL("leaving on error",
1550 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001551 return 0;
1552 }
1553
Christopher Faulet9768c262018-10-22 09:34:31 +02001554 /* 2: read timeout : return a 504 to the client. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001555 else if (rep->flags & CF_READ_TIMEOUT) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001556 if ((si_b->flags & SI_FL_L7_RETRY) &&
1557 (s->be->retry_type & PR_RE_TIMEOUT)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001558 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1559 DBG_TRACE_DEVEL("leaving on L7 retry",
1560 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001561 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001562 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001563 }
Olivier Houcharda798bf52019-03-08 18:52:00 +01001564 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001565 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001566 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001567 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001568 }
1569
Christopher Faulete0768eb2018-10-03 16:38:02 +02001570 rep->analysers &= AN_RES_FLT_END;
1571 txn->status = 504;
1572 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001573 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001574
1575 if (!(s->flags & SF_ERR_MASK))
1576 s->flags |= SF_ERR_SRVTO;
1577 if (!(s->flags & SF_FINST_MASK))
1578 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001579 DBG_TRACE_DEVEL("leaving on error",
1580 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001581 return 0;
1582 }
1583
Christopher Faulet9768c262018-10-22 09:34:31 +02001584 /* 3: client abort with an abortonclose */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001585 else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001586 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
1587 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001588 if (sess->listener->counters)
1589 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001590 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001591 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001592
1593 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001594 txn->status = 400;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001595 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001596
1597 if (!(s->flags & SF_ERR_MASK))
1598 s->flags |= SF_ERR_CLICL;
1599 if (!(s->flags & SF_FINST_MASK))
1600 s->flags |= SF_FINST_H;
1601
1602 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001603 DBG_TRACE_DEVEL("leaving on error",
1604 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001605 return 0;
1606 }
1607
Christopher Faulet9768c262018-10-22 09:34:31 +02001608 /* 4: close from server, capture the response if the server has started to respond */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001609 else if (rep->flags & CF_SHUTR) {
Olivier Houcharda254a372019-04-05 15:30:12 +02001610 if ((si_b->flags & SI_FL_L7_RETRY) &&
1611 (s->be->retry_type & PR_RE_DISCONNECTED)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001612 if (co_data(rep) || do_l7_retry(s, si_b) == 0) {
1613 DBG_TRACE_DEVEL("leaving on L7 retry",
1614 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharda254a372019-04-05 15:30:12 +02001615 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001616 }
Olivier Houcharda254a372019-04-05 15:30:12 +02001617 }
1618
Olivier Houchard6db16992019-05-17 15:40:49 +02001619 if (txn->flags & TX_NOT_FIRST)
1620 goto abort_keep_alive;
1621
Olivier Houcharda798bf52019-03-08 18:52:00 +01001622 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001623 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001624 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001625 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001626 }
1627
Christopher Faulete0768eb2018-10-03 16:38:02 +02001628 rep->analysers &= AN_RES_FLT_END;
1629 txn->status = 502;
1630 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001631 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulete0768eb2018-10-03 16:38:02 +02001632
1633 if (!(s->flags & SF_ERR_MASK))
1634 s->flags |= SF_ERR_SRVCL;
1635 if (!(s->flags & SF_FINST_MASK))
1636 s->flags |= SF_FINST_H;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001637 DBG_TRACE_DEVEL("leaving on error",
1638 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001639 return 0;
1640 }
1641
Christopher Faulet9768c262018-10-22 09:34:31 +02001642 /* 5: write error to client (we don't send any message then) */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001643 else if (rep->flags & CF_WRITE_ERROR) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001644 if (txn->flags & TX_NOT_FIRST)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001645 goto abort_keep_alive;
1646
Olivier Houcharda798bf52019-03-08 18:52:00 +01001647 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001648 if (objt_server(s->target))
1649 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001650 rep->analysers &= AN_RES_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001651
1652 if (!(s->flags & SF_ERR_MASK))
1653 s->flags |= SF_ERR_CLICL;
1654 if (!(s->flags & SF_FINST_MASK))
1655 s->flags |= SF_FINST_H;
1656
1657 /* process_stream() will take care of the error */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001658 DBG_TRACE_DEVEL("leaving on error",
1659 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001660 return 0;
1661 }
1662
1663 channel_dont_close(rep);
1664 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001665 DBG_TRACE_DEVEL("waiting for more data",
1666 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001667 return 0;
1668 }
1669
1670 /* More interesting part now : we know that we have a complete
1671 * response which at least looks like HTTP. We have an indicator
1672 * of each header's length, so we can parse them quickly.
1673 */
Christopher Faulet9768c262018-10-22 09:34:31 +02001674 msg->msg_state = HTTP_MSG_BODY;
Christopher Faulet29f17582019-05-23 11:03:26 +02001675 BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL);
Christopher Faulet297fbb42019-05-13 14:41:27 +02001676 sl = http_get_stline(htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001677
Christopher Faulet9768c262018-10-22 09:34:31 +02001678 /* 0: we might have to print this header in debug mode */
1679 if (unlikely((global.mode & MODE_DEBUG) &&
1680 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
1681 int32_t pos;
1682
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001683 http_debug_stline("srvrep", s, sl);
Christopher Faulet9768c262018-10-22 09:34:31 +02001684
Christopher Fauleta3f15502019-05-13 15:27:23 +02001685 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet9768c262018-10-22 09:34:31 +02001686 struct htx_blk *blk = htx_get_blk(htx, pos);
1687 enum htx_blk_type type = htx_get_blk_type(blk);
1688
1689 if (type == HTX_BLK_EOH)
1690 break;
1691 if (type != HTX_BLK_HDR)
1692 continue;
1693
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001694 http_debug_hdr("srvhdr", s,
1695 htx_get_blk_name(htx, blk),
1696 htx_get_blk_value(htx, blk));
Christopher Faulet9768c262018-10-22 09:34:31 +02001697 }
1698 }
1699
Christopher Faulet03599112018-11-27 11:21:21 +01001700 /* 1: get the status code and the version. Also set HTTP flags */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001701 txn->status = sl->info.res.status;
Christopher Faulet03599112018-11-27 11:21:21 +01001702 if (sl->flags & HTX_SL_F_VER_11)
Christopher Faulet9768c262018-10-22 09:34:31 +02001703 msg->flags |= HTTP_MSGF_VER_11;
Christopher Faulet03599112018-11-27 11:21:21 +01001704 if (sl->flags & HTX_SL_F_XFER_LEN) {
1705 msg->flags |= HTTP_MSGF_XFER_LEN;
Christopher Faulet834eee72019-02-18 11:35:02 +01001706 msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK);
Christopher Fauletb2db4fa2018-11-27 16:51:09 +01001707 if (sl->flags & HTX_SL_F_BODYLESS)
1708 msg->flags |= HTTP_MSGF_BODYLESS;
Christopher Faulet03599112018-11-27 11:21:21 +01001709 }
Christopher Faulet9768c262018-10-22 09:34:31 +02001710
1711 n = txn->status / 100;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001712 if (n < 1 || n > 5)
1713 n = 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001714
Christopher Faulete0768eb2018-10-03 16:38:02 +02001715 /* when the client triggers a 4xx from the server, it's most often due
1716 * to a missing object or permission. These events should be tracked
1717 * because if they happen often, it may indicate a brute force or a
1718 * vulnerability scan.
1719 */
1720 if (n == 4)
1721 stream_inc_http_err_ctr(s);
1722
1723 if (objt_server(s->target))
Olivier Houcharda798bf52019-03-08 18:52:00 +01001724 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001725
Christopher Faulete0768eb2018-10-03 16:38:02 +02001726 /* Adjust server's health based on status code. Note: status codes 501
1727 * and 505 are triggered on demand by client request, so we must not
1728 * count them as server failures.
1729 */
1730 if (objt_server(s->target)) {
1731 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001732 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001733 else
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001734 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001735 }
1736
1737 /*
1738 * We may be facing a 100-continue response, or any other informational
1739 * 1xx response which is non-final, in which case this is not the right
1740 * response, and we're waiting for the next one. Let's allow this response
1741 * to go to the client and wait for the next one. There's an exception for
1742 * 101 which is used later in the code to switch protocols.
1743 */
1744 if (txn->status < 200 &&
1745 (txn->status == 100 || txn->status >= 102)) {
Christopher Fauletaed82cf2018-11-30 22:22:32 +01001746 FLT_STRM_CB(s, flt_http_reset(s, msg));
Christopher Faulet421e7692019-06-13 11:16:45 +02001747 htx->first = channel_htx_fwd_headers(rep, htx);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001748 msg->msg_state = HTTP_MSG_RPBEFORE;
Christopher Faulet3499f622019-09-03 15:23:54 +02001749 msg->flags = 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001750 txn->status = 0;
1751 s->logs.t_data = -1; /* was not a response yet */
Christopher Fauletb75b5ea2019-05-17 08:37:28 +02001752 goto next_one;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001753 }
1754
1755 /*
1756 * 2: check for cacheability.
1757 */
1758
1759 switch (txn->status) {
1760 case 200:
1761 case 203:
1762 case 204:
1763 case 206:
1764 case 300:
1765 case 301:
1766 case 404:
1767 case 405:
1768 case 410:
1769 case 414:
1770 case 501:
1771 break;
1772 default:
1773 /* RFC7231#6.1:
1774 * Responses with status codes that are defined as
1775 * cacheable by default (e.g., 200, 203, 204, 206,
1776 * 300, 301, 404, 405, 410, 414, and 501 in this
1777 * specification) can be reused by a cache with
1778 * heuristic expiration unless otherwise indicated
1779 * by the method definition or explicit cache
1780 * controls [RFC7234]; all other status codes are
1781 * not cacheable by default.
1782 */
1783 txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK);
1784 break;
1785 }
1786
1787 /*
1788 * 3: we may need to capture headers
1789 */
1790 s->logs.logwait &= ~LW_RESP;
1791 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001792 http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001793
Christopher Faulet9768c262018-10-22 09:34:31 +02001794 /* Skip parsing if no content length is possible. */
Christopher Faulete0768eb2018-10-03 16:38:02 +02001795 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
1796 txn->status == 101)) {
1797 /* Either we've established an explicit tunnel, or we're
1798 * switching the protocol. In both cases, we're very unlikely
1799 * to understand the next protocols. We have to switch to tunnel
1800 * mode, so that we transfer the request and responses then let
1801 * this protocol pass unmodified. When we later implement specific
1802 * parsers for such protocols, we'll want to check the Upgrade
1803 * header which contains information about that protocol for
1804 * responses with status 101 (eg: see RFC2817 about TLS).
1805 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02001806 txn->flags |= TX_CON_WANT_TUN;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001807 }
1808
Christopher Faulet61608322018-11-23 16:23:45 +01001809 /* check for NTML authentication headers in 401 (WWW-Authenticate) and
1810 * 407 (Proxy-Authenticate) responses and set the connection to private
1811 */
1812 srv_conn = cs_conn(objt_cs(s->si[1].end));
1813 if (srv_conn) {
1814 struct ist hdr;
1815 struct http_hdr_ctx ctx;
1816
1817 if (txn->status == 401)
1818 hdr = ist("WWW-Authenticate");
1819 else if (txn->status == 407)
1820 hdr = ist("Proxy-Authenticate");
1821 else
1822 goto end;
1823
1824 ctx.blk = NULL;
1825 while (http_find_header(htx, hdr, &ctx, 0)) {
1826 if ((ctx.value.len >= 9 && word_match(ctx.value.ptr, ctx.value.len, "Negotiate", 9)) ||
Olivier Houchard250031e2019-05-29 15:01:50 +02001827 (ctx.value.len >= 4 && word_match(ctx.value.ptr, ctx.value.len, "NTLM", 4))) {
1828 sess->flags |= SESS_FL_PREFER_LAST;
Christopher Faulet61608322018-11-23 16:23:45 +01001829 srv_conn->flags |= CO_FL_PRIVATE;
Olivier Houchard250031e2019-05-29 15:01:50 +02001830 }
Christopher Faulet61608322018-11-23 16:23:45 +01001831 }
1832 }
1833
1834 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02001835 /* we want to have the response time before we start processing it */
1836 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
1837
1838 /* end of job, return OK */
1839 rep->analysers &= ~an_bit;
1840 rep->analyse_exp = TICK_ETERNITY;
1841 channel_auto_close(rep);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001842 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001843 return 1;
1844
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001845 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01001846 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001847 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01001848 if (sess->listener->counters)
1849 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01001850 if (objt_server(s->target))
1851 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001852 txn->status = 500;
1853 if (!(s->flags & SF_ERR_MASK))
1854 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01001855 goto return_prx_cond;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001856
1857 return_bad_res:
Olivier Houcharda798bf52019-03-08 18:52:00 +01001858 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Christopher Faulet47365272018-10-31 17:40:50 +01001859 if (objt_server(s->target)) {
Olivier Houcharda798bf52019-03-08 18:52:00 +01001860 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Willy Tarreaub54c40a2018-12-02 19:28:41 +01001861 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Christopher Faulet47365272018-10-31 17:40:50 +01001862 }
Olivier Houcharde3249a92019-05-03 23:01:47 +02001863 if ((s->be->retry_type & PR_RE_JUNK_REQUEST) &&
Olivier Houchardad26d8d2019-05-10 17:48:28 +02001864 (si_b->flags & SI_FL_L7_RETRY) &&
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001865 do_l7_retry(s, si_b) == 0) {
1866 DBG_TRACE_DEVEL("leaving on L7 retry",
1867 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Olivier Houcharde3249a92019-05-03 23:01:47 +02001868 return 0;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001869 }
Christopher Faulet47365272018-10-31 17:40:50 +01001870 txn->status = 502;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001871 /* fall through */
1872
Christopher Fauletb8a53712019-12-16 11:29:38 +01001873 return_prx_cond:
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001874 http_reply_and_close(s, txn->status, http_error_message(s));
Christopher Faulet47365272018-10-31 17:40:50 +01001875
1876 if (!(s->flags & SF_ERR_MASK))
1877 s->flags |= SF_ERR_PRXCOND;
1878 if (!(s->flags & SF_FINST_MASK))
1879 s->flags |= SF_FINST_H;
Christopher Fauletb9a92f32019-09-09 10:15:21 +02001880
1881 s->si[1].flags |= SI_FL_NOLINGER;
1882 rep->analysers &= AN_RES_FLT_END;
1883 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001884 DBG_TRACE_DEVEL("leaving on error",
1885 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulet47365272018-10-31 17:40:50 +01001886 return 0;
1887
Christopher Faulete0768eb2018-10-03 16:38:02 +02001888 abort_keep_alive:
1889 /* A keep-alive request to the server failed on a network error.
1890 * The client is required to retry. We need to close without returning
1891 * any other information so that the client retries.
1892 */
1893 txn->status = 0;
1894 rep->analysers &= AN_RES_FLT_END;
1895 s->req.analysers &= AN_REQ_FLT_END;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001896 s->logs.logwait = 0;
1897 s->logs.level = 0;
1898 s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001899 http_reply_and_close(s, txn->status, NULL);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001900 DBG_TRACE_DEVEL("leaving by closing K/A connection",
1901 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001902 return 0;
1903}
1904
1905/* This function performs all the processing enabled for the current response.
1906 * It normally returns 1 unless it wants to break. It relies on buffers flags,
1907 * and updates s->res.analysers. It might make sense to explode it into several
1908 * other functions. It works like process_request (see indications above).
1909 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001910int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px)
Christopher Faulete0768eb2018-10-03 16:38:02 +02001911{
1912 struct session *sess = s->sess;
1913 struct http_txn *txn = s->txn;
1914 struct http_msg *msg = &txn->rsp;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001915 struct htx *htx;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001916 struct proxy *cur_proxy;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001917 enum rule_result ret = HTTP_RULE_RES_CONT;
1918
Christopher Fauletfec7bd12018-10-24 11:17:50 +02001919 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
1920 return 0;
Christopher Faulet9768c262018-10-22 09:34:31 +02001921
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001922 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001923
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01001924 htx = htxbuf(&rep->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001925
1926 /* The stats applet needs to adjust the Connection header but we don't
1927 * apply any filter there.
1928 */
1929 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
1930 rep->analysers &= ~an_bit;
1931 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauletf2824e62018-10-01 12:12:37 +02001932 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001933 }
1934
1935 /*
1936 * We will have to evaluate the filters.
1937 * As opposed to version 1.2, now they will be evaluated in the
1938 * filters order and not in the header order. This means that
1939 * each filter has to be validated among all headers.
1940 *
1941 * Filters are tried with ->be first, then with ->fe if it is
1942 * different from ->be.
1943 *
1944 * Maybe we are in resume condiion. In this case I choose the
1945 * "struct proxy" which contains the rule list matching the resume
1946 * pointer. If none of theses "struct proxy" match, I initialise
1947 * the process with the first one.
1948 *
1949 * In fact, I check only correspondance betwwen the current list
1950 * pointer and the ->fe rule list. If it doesn't match, I initialize
1951 * the loop with the ->be.
1952 */
1953 if (s->current_rule_list == &sess->fe->http_res_rules)
1954 cur_proxy = sess->fe;
1955 else
1956 cur_proxy = s->be;
1957 while (1) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02001958 /* evaluate http-response rules */
1959 if (ret == HTTP_RULE_RES_CONT) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001960 ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02001961
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001962 switch (ret) {
1963 case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */
1964 goto return_prx_yield;
1965
1966 case HTTP_RULE_RES_CONT:
1967 case HTTP_RULE_RES_STOP: /* nothing to do */
1968 break;
1969
1970 case HTTP_RULE_RES_DENY: /* deny or tarpit */
1971 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001972
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001973 case HTTP_RULE_RES_ABRT: /* abort request, response already sent */
1974 goto return_prx_cond;
1975
1976 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Christopher Fauletb8a53712019-12-16 11:29:38 +01001977 goto done;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001978
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001979 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
1980 goto return_bad_res;
Christopher Faulete0768eb2018-10-03 16:38:02 +02001981
Christopher Faulet3a26bee2019-12-16 12:47:40 +01001982 case HTTP_RULE_RES_ERROR: /* failed with a bad request */
1983 goto return_int_err;
1984 }
1985
1986 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02001987
Christopher Faulete0768eb2018-10-03 16:38:02 +02001988 /* check whether we're already working on the frontend */
1989 if (cur_proxy == sess->fe)
1990 break;
1991 cur_proxy = sess->fe;
1992 }
1993
1994 /* After this point, this anayzer can't return yield, so we can
1995 * remove the bit corresponding to this analyzer from the list.
1996 *
1997 * Note that the intermediate returns and goto found previously
1998 * reset the analyzers.
1999 */
2000 rep->analysers &= ~an_bit;
2001 rep->analyse_exp = TICK_ETERNITY;
2002
2003 /* OK that's all we can do for 1xx responses */
2004 if (unlikely(txn->status < 200 && txn->status != 101))
Christopher Fauletf2824e62018-10-01 12:12:37 +02002005 goto end;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002006
2007 /*
2008 * Now check for a server cookie.
2009 */
2010 if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002011 http_manage_server_side_cookies(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002012
2013 /*
2014 * Check for cache-control or pragma headers if required.
2015 */
2016 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002017 http_check_response_for_cacheability(s, rep);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002018
2019 /*
2020 * Add server cookie in the response if needed
2021 */
2022 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
2023 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
2024 (!(s->flags & SF_DIRECT) ||
2025 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
2026 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
2027 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
2028 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
2029 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
2030 !(s->flags & SF_IGNORE_PRST)) {
2031 /* the server is known, it's not the one the client requested, or the
2032 * cookie's last seen date needs to be refreshed. We have to
2033 * insert a set-cookie here, except if we want to insert only on POST
2034 * requests and this one isn't. Note that servers which don't have cookies
2035 * (eg: some backup servers) will return a full cookie removal request.
2036 */
2037 if (!objt_server(s->target)->cookie) {
2038 chunk_printf(&trash,
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002039 "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
Christopher Faulete0768eb2018-10-03 16:38:02 +02002040 s->be->cookie_name);
2041 }
2042 else {
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002043 chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002044
2045 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
2046 /* emit last_date, which is mandatory */
2047 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2048 s30tob64((date.tv_sec+3) >> 2,
2049 trash.area + trash.data);
2050 trash.data += 5;
2051
2052 if (s->be->cookie_maxlife) {
2053 /* emit first_date, which is either the original one or
2054 * the current date.
2055 */
2056 trash.area[trash.data++] = COOKIE_DELIM_DATE;
2057 s30tob64(txn->cookie_first_date ?
2058 txn->cookie_first_date >> 2 :
2059 (date.tv_sec+3) >> 2,
2060 trash.area + trash.data);
2061 trash.data += 5;
2062 }
2063 }
2064 chunk_appendf(&trash, "; path=/");
2065 }
2066
2067 if (s->be->cookie_domain)
2068 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
2069
2070 if (s->be->ck_opts & PR_CK_HTTPONLY)
2071 chunk_appendf(&trash, "; HttpOnly");
2072
2073 if (s->be->ck_opts & PR_CK_SECURE)
2074 chunk_appendf(&trash, "; Secure");
2075
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002076 if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002077 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002078
2079 txn->flags &= ~TX_SCK_MASK;
2080 if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT))
2081 /* the server did not change, only the date was updated */
2082 txn->flags |= TX_SCK_UPDATED;
2083 else
2084 txn->flags |= TX_SCK_INSERTED;
2085
2086 /* Here, we will tell an eventual cache on the client side that we don't
2087 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
2088 * Some caches understand the correct form: 'no-cache="set-cookie"', but
2089 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
2090 */
2091 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
2092
2093 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
2094
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002095 if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private"))))
Christopher Fauletb8a53712019-12-16 11:29:38 +01002096 goto return_int_err;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002097 }
2098 }
2099
2100 /*
2101 * Check if result will be cacheable with a cookie.
2102 * We'll block the response if security checks have caught
2103 * nasty things such as a cacheable cookie.
2104 */
2105 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
2106 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
2107 (s->be->options & PR_O_CHK_CACHE)) {
2108 /* we're in presence of a cacheable response containing
2109 * a set-cookie header. We'll block it as requested by
2110 * the 'checkcache' option, and send an alert.
2111 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002112 ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
2113 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
2114 send_log(s->be, LOG_ALERT,
2115 "Blocking cacheable cookie in response from instance %s, server %s.\n",
2116 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
Christopher Fauletb8a53712019-12-16 11:29:38 +01002117 goto deny;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002118 }
2119
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002120 end:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002121 /* Always enter in the body analyzer */
2122 rep->analysers &= ~AN_RES_FLT_XFER_DATA;
2123 rep->analysers |= AN_RES_HTTP_XFER_BODY;
2124
2125 /* if the user wants to log as soon as possible, without counting
2126 * bytes from the server, then this is the right moment. We have
2127 * to temporarily assign bytes_out to log what we currently have.
2128 */
2129 if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
2130 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002131 s->logs.bytes_out = htx->data;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002132 s->do_log(s);
2133 s->logs.bytes_out = 0;
2134 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002135 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002136 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002137
Christopher Fauletb8a53712019-12-16 11:29:38 +01002138 done:
2139 rep->analysers &= ~an_bit;
2140 rep->analyse_exp = TICK_ETERNITY;
2141 return 1;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002142
Christopher Fauletb8a53712019-12-16 11:29:38 +01002143 deny:
2144 txn->flags |= TX_CLDENY;
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002145 txn->status = 502;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002146 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002147 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002148 if (sess->listener->counters)
2149 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Christopher Fauleta08546b2019-12-16 16:07:34 +01002150 if (objt_server(s->target))
2151 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002152 goto return_prx_err;
2153
2154 return_int_err:
2155 txn->status = 500;
2156 if (!(s->flags & SF_ERR_MASK))
2157 s->flags |= SF_ERR_INTERNAL;
Christopher Fauletcff0f732019-12-16 16:13:44 +01002158 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002159 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
2160 if (objt_server(s->target))
2161 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002162 if (objt_server(s->target))
2163 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002164 goto return_prx_err;
2165
2166 return_bad_res:
2167 txn->status = 502;
2168 /* fall through */
2169
2170 return_prx_err:
2171 http_reply_and_close(s, txn->status, http_error_message(s));
2172 /* fall through */
2173
2174 return_prx_cond:
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002175 s->logs.t_data = -1; /* was not a valid response */
2176 s->si[1].flags |= SI_FL_NOLINGER;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002177
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002178 if (!(s->flags & SF_ERR_MASK))
2179 s->flags |= SF_ERR_PRXCOND;
2180 if (!(s->flags & SF_FINST_MASK))
2181 s->flags |= SF_FINST_H;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002182
2183 rep->analysers &= ~an_bit;
2184 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002185 DBG_TRACE_DEVEL("leaving on error",
2186 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Fauletfec7bd12018-10-24 11:17:50 +02002187 return 0;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002188
2189 return_prx_yield:
2190 channel_dont_close(rep);
2191 DBG_TRACE_DEVEL("waiting for more data",
2192 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
2193 return 0;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002194}
2195
2196/* This function is an analyser which forwards response body (including chunk
2197 * sizes if any). It is called as soon as we must forward, even if we forward
2198 * zero byte. The only situation where it must not be called is when we're in
2199 * tunnel mode and we want to forward till the close. It's used both to forward
2200 * remaining data and to resync after end of body. It expects the msg_state to
2201 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
2202 * read more data, or 1 once we can go on with next request or end the stream.
2203 *
2204 * It is capable of compressing response data both in content-length mode and
2205 * in chunked mode. The state machines follows different flows depending on
2206 * whether content-length and chunked modes are used, since there are no
2207 * trailers in content-length :
2208 *
2209 * chk-mode cl-mode
2210 * ,----- BODY -----.
2211 * / \
2212 * V size > 0 V chk-mode
2213 * .--> SIZE -------------> DATA -------------> CRLF
2214 * | | size == 0 | last byte |
2215 * | v final crlf v inspected |
2216 * | TRAILERS -----------> DONE |
2217 * | |
2218 * `----------------------------------------------'
2219 *
2220 * Compression only happens in the DATA state, and must be flushed in final
2221 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
2222 * is performed at once on final states for all bytes parsed, or when leaving
2223 * on missing data.
2224 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002225int http_response_forward_body(struct stream *s, struct channel *res, int an_bit)
Christopher Faulete0768eb2018-10-03 16:38:02 +02002226{
2227 struct session *sess = s->sess;
2228 struct http_txn *txn = s->txn;
2229 struct http_msg *msg = &s->txn->rsp;
Christopher Faulet9768c262018-10-22 09:34:31 +02002230 struct htx *htx;
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002231 int ret;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002232
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002233 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002234
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002235 htx = htxbuf(&res->buf);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002236
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002237 if (htx->flags & HTX_FL_PARSING_ERROR)
2238 goto return_bad_res;
2239 if (htx->flags & HTX_FL_PROCESSING_ERROR)
2240 goto return_int_err;
2241
Christopher Faulete0768eb2018-10-03 16:38:02 +02002242 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Christopher Fauletf2824e62018-10-01 12:12:37 +02002243 ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002244 /* Output closed while we were sending data. We must abort and
2245 * wake the other side up.
2246 */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002247 msg->msg_state = HTTP_MSG_ERROR;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002248 http_end_response(s);
2249 http_end_request(s);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002250 DBG_TRACE_DEVEL("leaving on error",
2251 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002252 return 1;
2253 }
2254
Christopher Faulet9768c262018-10-22 09:34:31 +02002255 if (msg->msg_state == HTTP_MSG_BODY)
2256 msg->msg_state = HTTP_MSG_DATA;
2257
Christopher Faulete0768eb2018-10-03 16:38:02 +02002258 /* in most states, we should abort in case of early close */
2259 channel_auto_close(res);
2260
Christopher Faulete0768eb2018-10-03 16:38:02 +02002261 if (res->to_forward) {
Christopher Faulet66af0b22019-03-22 14:54:52 +01002262 if (res->to_forward == CHN_INFINITE_FORWARD) {
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002263 if (res->flags & CF_EOI)
2264 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet66af0b22019-03-22 14:54:52 +01002265 }
2266 else {
2267 /* We can't process the buffer's contents yet */
2268 res->flags |= CF_WAKE_WRITE;
2269 goto missing_data_or_waiting;
2270 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002271 }
2272
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002273 if (msg->msg_state >= HTTP_MSG_ENDING)
2274 goto ending;
2275
2276 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2277 (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) {
2278 msg->msg_state = HTTP_MSG_ENDING;
2279 goto ending;
2280 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002281
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002282 /* Forward input data. We get it by removing all outgoing data not
2283 * forwarded yet from HTX data size. If there are some data filters, we
2284 * let them decide the amount of data to forward.
Christopher Faulet9768c262018-10-22 09:34:31 +02002285 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002286 if (HAS_RSP_DATA_FILTERS(s)) {
2287 ret = flt_http_payload(s, msg, htx->data);
2288 if (ret < 0)
2289 goto return_bad_res;
Christopher Faulet421e7692019-06-13 11:16:45 +02002290 c_adv(res, ret);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002291 }
2292 else {
Christopher Faulet421e7692019-06-13 11:16:45 +02002293 c_adv(res, htx->data - co_data(res));
Christopher Faulet66af0b22019-03-22 14:54:52 +01002294 if (msg->flags & HTTP_MSGF_XFER_LEN)
2295 channel_htx_forward_forever(res, htx);
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002296 }
Christopher Faulet9768c262018-10-22 09:34:31 +02002297
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002298 if (htx->data != co_data(res))
2299 goto missing_data_or_waiting;
2300
2301 if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) {
2302 msg->msg_state = HTTP_MSG_ENDING;
2303 goto ending;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002304 }
2305
Christopher Faulet9768c262018-10-22 09:34:31 +02002306 /* Check if the end-of-message is reached and if so, switch the message
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002307 * in HTTP_MSG_ENDING state. Then if all data was marked to be
2308 * forwarded, set the state to HTTP_MSG_DONE.
Christopher Faulet9768c262018-10-22 09:34:31 +02002309 */
2310 if (htx_get_tail_type(htx) != HTX_BLK_EOM)
2311 goto missing_data_or_waiting;
2312
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002313 msg->msg_state = HTTP_MSG_ENDING;
Christopher Faulet9768c262018-10-22 09:34:31 +02002314
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002315 ending:
2316 /* other states, ENDING...TUNNEL */
2317 if (msg->msg_state >= HTTP_MSG_DONE)
2318 goto done;
Christopher Faulet9768c262018-10-22 09:34:31 +02002319
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002320 if (HAS_RSP_DATA_FILTERS(s)) {
2321 ret = flt_http_end(s, msg);
2322 if (ret <= 0) {
2323 if (!ret)
2324 goto missing_data_or_waiting;
2325 goto return_bad_res;
2326 }
2327 }
2328
Christopher Faulet1a3e0272019-11-15 16:31:46 +01002329 if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 ||
2330 !(msg->flags & HTTP_MSGF_XFER_LEN)) {
2331 msg->msg_state = HTTP_MSG_TUNNEL;
2332 goto ending;
2333 }
2334 else {
2335 msg->msg_state = HTTP_MSG_DONE;
2336 res->to_forward = 0;
2337 }
2338
2339 done:
2340
2341 channel_dont_close(res);
2342
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002343 http_end_response(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002344 if (!(res->analysers & an_bit)) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002345 http_end_request(s);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002346 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
2347 if (res->flags & CF_SHUTW) {
2348 /* response errors are most likely due to the
2349 * client aborting the transfer. */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002350 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002351 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002352 goto return_bad_res;
2353 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002354 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002355 return 1;
2356 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002357 DBG_TRACE_DEVEL("waiting for the end of the HTTP txn",
2358 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002359 return 0;
2360
2361 missing_data_or_waiting:
2362 if (res->flags & CF_SHUTW)
Christopher Faulet93e02d82019-03-08 14:18:50 +01002363 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002364
2365 /* stop waiting for data if the input is closed before the end. If the
2366 * client side was already closed, it means that the client has aborted,
2367 * so we don't want to count this as a server abort. Otherwise it's a
2368 * server abort.
2369 */
Christopher Fauletd20fdb02019-06-13 16:43:22 +02002370 if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) {
Christopher Faulete0768eb2018-10-03 16:38:02 +02002371 if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002372 goto return_cli_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002373 /* If we have some pending data, we continue the processing */
Christopher Faulet93e02d82019-03-08 14:18:50 +01002374 if (htx_is_empty(htx))
2375 goto return_srv_abort;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002376 }
2377
Christopher Faulete0768eb2018-10-03 16:38:02 +02002378 /* When TE: chunked is used, we need to get there again to parse
2379 * remaining chunks even if the server has closed, so we don't want to
Christopher Faulet9768c262018-10-22 09:34:31 +02002380 * set CF_DONTCLOSE. Similarly when there is a content-leng or if there
2381 * are filters registered on the stream, we don't want to forward a
2382 * close
Christopher Faulete0768eb2018-10-03 16:38:02 +02002383 */
Christopher Fauletaed82cf2018-11-30 22:22:32 +01002384 if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s))
Christopher Faulete0768eb2018-10-03 16:38:02 +02002385 channel_dont_close(res);
2386
2387 /* We know that more data are expected, but we couldn't send more that
2388 * what we did. So we always set the CF_EXPECT_MORE flag so that the
2389 * system knows it must not set a PUSH on this first part. Interactive
2390 * modes are already handled by the stream sock layer. We must not do
2391 * this in content-length mode because it could present the MSG_MORE
2392 * flag with the last block of forwarded data, which would cause an
2393 * additional delay to be observed by the receiver.
2394 */
2395 if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING))
2396 res->flags |= CF_EXPECT_MORE;
2397
2398 /* the stream handler will take care of timeouts and errors */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002399 DBG_TRACE_DEVEL("waiting for more data to forward",
2400 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002401 return 0;
2402
Christopher Faulet93e02d82019-03-08 14:18:50 +01002403 return_srv_abort:
2404 _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
2405 _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002406 if (sess->listener->counters)
2407 _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002408 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002409 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002410 if (!(s->flags & SF_ERR_MASK))
2411 s->flags |= SF_ERR_SRVCL;
2412 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002413
Christopher Faulet93e02d82019-03-08 14:18:50 +01002414 return_cli_abort:
2415 _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
2416 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002417 if (sess->listener->counters)
2418 _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002419 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01002420 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002421 if (!(s->flags & SF_ERR_MASK))
2422 s->flags |= SF_ERR_CLICL;
2423 goto return_error;
Christopher Faulete0768eb2018-10-03 16:38:02 +02002424
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002425 return_int_err:
Christopher Fauletcff0f732019-12-16 16:13:44 +01002426 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002427 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01002428 if (sess->listener->counters)
2429 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
Christopher Fauletb8a53712019-12-16 11:29:38 +01002430 if (objt_server(s->target))
2431 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
Christopher Fauletb9a92f32019-09-09 10:15:21 +02002432 if (!(s->flags & SF_ERR_MASK))
2433 s->flags |= SF_ERR_INTERNAL;
2434 goto return_error;
2435
Christopher Faulet93e02d82019-03-08 14:18:50 +01002436 return_bad_res:
2437 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
2438 if (objt_server(s->target)) {
Christopher Fauletcff0f732019-12-16 16:13:44 +01002439 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
Christopher Faulet93e02d82019-03-08 14:18:50 +01002440 health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP);
2441 }
Christopher Faulete0768eb2018-10-03 16:38:02 +02002442 if (!(s->flags & SF_ERR_MASK))
Christopher Faulet93e02d82019-03-08 14:18:50 +01002443 s->flags |= SF_ERR_SRVCL;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002444 /* fall through */
Christopher Faulete0768eb2018-10-03 16:38:02 +02002445
Christopher Faulet93e02d82019-03-08 14:18:50 +01002446 return_error:
Christopher Faulete0768eb2018-10-03 16:38:02 +02002447 /* don't send any error message as we're in the body */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002448 http_reply_and_close(s, txn->status, NULL);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002449 res->analysers &= AN_RES_FLT_END;
2450 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 +02002451 if (!(s->flags & SF_FINST_MASK))
2452 s->flags |= SF_FINST_D;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01002453 DBG_TRACE_DEVEL("leaving on error",
2454 STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn);
Christopher Faulete0768eb2018-10-03 16:38:02 +02002455 return 0;
2456}
2457
Christopher Fauletf2824e62018-10-01 12:12:37 +02002458/* Perform an HTTP redirect based on the information in <rule>. The function
Christopher Faulet99daf282018-11-28 22:58:13 +01002459 * returns zero on success, or zero in case of a, irrecoverable error such
Christopher Fauletf2824e62018-10-01 12:12:37 +02002460 * as too large a request to build a valid response.
2461 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002462int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn)
Christopher Fauletf2824e62018-10-01 12:12:37 +02002463{
Christopher Faulet99daf282018-11-28 22:58:13 +01002464 struct channel *req = &s->req;
2465 struct channel *res = &s->res;
2466 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002467 struct htx_sl *sl;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002468 struct buffer *chunk;
Christopher Faulet99daf282018-11-28 22:58:13 +01002469 struct ist status, reason, location;
2470 unsigned int flags;
2471 size_t data;
Christopher Faulet08e66462019-05-23 16:44:59 +02002472 int close = 0; /* Try to keep the connection alive byt default */
Christopher Fauletf2824e62018-10-01 12:12:37 +02002473
2474 chunk = alloc_trash_chunk();
Christopher Fauletb8a53712019-12-16 11:29:38 +01002475 if (!chunk) {
2476 if (!(s->flags & SF_ERR_MASK))
2477 s->flags |= SF_ERR_RESOURCE;
Christopher Faulet99daf282018-11-28 22:58:13 +01002478 goto fail;
Christopher Fauletb8a53712019-12-16 11:29:38 +01002479 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002480
Christopher Faulet99daf282018-11-28 22:58:13 +01002481 /*
2482 * Create the location
2483 */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002484 htx = htxbuf(&req->buf);
Christopher Fauletf2824e62018-10-01 12:12:37 +02002485 switch(rule->type) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002486 case REDIRECT_TYPE_SCHEME: {
2487 struct http_hdr_ctx ctx;
2488 struct ist path, host;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002489
Christopher Faulet99daf282018-11-28 22:58:13 +01002490 host = ist("");
2491 ctx.blk = NULL;
2492 if (http_find_header(htx, ist("Host"), &ctx, 0))
2493 host = ctx.value;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002494
Christopher Faulet297fbb42019-05-13 14:41:27 +02002495 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002496 path = http_get_path(htx_sl_req_uri(sl));
2497 /* build message using path */
2498 if (path.ptr) {
2499 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2500 int qs = 0;
2501 while (qs < path.len) {
2502 if (*(path.ptr + qs) == '?') {
2503 path.len = qs;
2504 break;
2505 }
2506 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002507 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002508 }
2509 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002510 else
2511 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002512
Christopher Faulet99daf282018-11-28 22:58:13 +01002513 if (rule->rdr_str) { /* this is an old "redirect" rule */
2514 /* add scheme */
2515 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2516 goto fail;
2517 }
2518 else {
2519 /* add scheme with executing log format */
2520 chunk->data += build_logline(s, chunk->area + chunk->data,
2521 chunk->size - chunk->data,
2522 &rule->rdr_fmt);
2523 }
2524 /* add "://" + host + path */
2525 if (!chunk_memcat(chunk, "://", 3) ||
2526 !chunk_memcat(chunk, host.ptr, host.len) ||
2527 !chunk_memcat(chunk, path.ptr, path.len))
2528 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002529
Christopher Faulet99daf282018-11-28 22:58:13 +01002530 /* append a slash at the end of the location if needed and missing */
2531 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2532 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2533 if (chunk->data + 1 >= chunk->size)
2534 goto fail;
2535 chunk->area[chunk->data++] = '/';
2536 }
2537 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002538 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002539
Christopher Faulet99daf282018-11-28 22:58:13 +01002540 case REDIRECT_TYPE_PREFIX: {
2541 struct ist path;
2542
Christopher Faulet297fbb42019-05-13 14:41:27 +02002543 sl = http_get_stline(htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002544 path = http_get_path(htx_sl_req_uri(sl));
2545 /* build message using path */
2546 if (path.ptr) {
2547 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
2548 int qs = 0;
2549 while (qs < path.len) {
2550 if (*(path.ptr + qs) == '?') {
2551 path.len = qs;
2552 break;
2553 }
2554 qs++;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002555 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002556 }
2557 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002558 else
2559 path = ist("/");
Christopher Fauletf2824e62018-10-01 12:12:37 +02002560
Christopher Faulet99daf282018-11-28 22:58:13 +01002561 if (rule->rdr_str) { /* this is an old "redirect" rule */
2562 /* add prefix. Note that if prefix == "/", we don't want to
2563 * add anything, otherwise it makes it hard for the user to
2564 * configure a self-redirection.
2565 */
2566 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
2567 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2568 goto fail;
2569 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002570 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002571 else {
2572 /* add prefix with executing log format */
2573 chunk->data += build_logline(s, chunk->area + chunk->data,
2574 chunk->size - chunk->data,
2575 &rule->rdr_fmt);
2576 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02002577
Christopher Faulet99daf282018-11-28 22:58:13 +01002578 /* add path */
2579 if (!chunk_memcat(chunk, path.ptr, path.len))
2580 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002581
Christopher Faulet99daf282018-11-28 22:58:13 +01002582 /* append a slash at the end of the location if needed and missing */
2583 if (chunk->data && chunk->area[chunk->data - 1] != '/' &&
2584 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
2585 if (chunk->data + 1 >= chunk->size)
2586 goto fail;
2587 chunk->area[chunk->data++] = '/';
2588 }
2589 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002590 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002591 case REDIRECT_TYPE_LOCATION:
2592 default:
2593 if (rule->rdr_str) { /* this is an old "redirect" rule */
2594 /* add location */
2595 if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len))
2596 goto fail;
2597 }
2598 else {
2599 /* add location with executing log format */
2600 chunk->data += build_logline(s, chunk->area + chunk->data,
2601 chunk->size - chunk->data,
2602 &rule->rdr_fmt);
2603 }
2604 break;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002605 }
Christopher Faulet99daf282018-11-28 22:58:13 +01002606 location = ist2(chunk->area, chunk->data);
2607
2608 /*
2609 * Create the 30x response
2610 */
2611 switch (rule->code) {
2612 case 308:
2613 status = ist("308");
2614 reason = ist("Permanent Redirect");
2615 break;
2616 case 307:
2617 status = ist("307");
2618 reason = ist("Temporary Redirect");
2619 break;
2620 case 303:
2621 status = ist("303");
2622 reason = ist("See Other");
2623 break;
2624 case 301:
2625 status = ist("301");
2626 reason = ist("Moved Permanently");
2627 break;
2628 case 302:
2629 default:
2630 status = ist("302");
2631 reason = ist("Found");
2632 break;
2633 }
2634
Christopher Faulet08e66462019-05-23 16:44:59 +02002635 if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE)
2636 close = 1;
2637
Christopher Faulet99daf282018-11-28 22:58:13 +01002638 htx = htx_from_buf(&res->buf);
Kevin Zhu96b36392020-01-07 09:42:55 +01002639 /* Trim any possible response */
2640 channel_htx_truncate(&s->res, htx);
Christopher Faulet99daf282018-11-28 22:58:13 +01002641 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
2642 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason);
2643 if (!sl)
2644 goto fail;
2645 sl->info.res.status = rule->code;
2646 s->txn->status = rule->code;
2647
Christopher Faulet08e66462019-05-23 16:44:59 +02002648 if (close && !htx_add_header(htx, ist("Connection"), ist("close")))
2649 goto fail;
2650
2651 if (!htx_add_header(htx, ist("Content-length"), ist("0")) ||
Christopher Faulet99daf282018-11-28 22:58:13 +01002652 !htx_add_header(htx, ist("Location"), location))
2653 goto fail;
2654
2655 if (rule->code == 302 || rule->code == 303 || rule->code == 307) {
2656 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")))
2657 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002658 }
2659
2660 if (rule->cookie_len) {
Christopher Faulet99daf282018-11-28 22:58:13 +01002661 if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len)))
2662 goto fail;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002663 }
2664
Christopher Faulet99daf282018-11-28 22:58:13 +01002665 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
2666 goto fail;
2667
Kevin Zhu96b36392020-01-07 09:42:55 +01002668 htx_to_buf(htx, &res->buf);
2669
Christopher Fauletf2824e62018-10-01 12:12:37 +02002670 /* let's log the request time */
2671 s->logs.tv_request = now;
2672
Christopher Faulet06511812019-10-16 09:38:27 +02002673 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet99daf282018-11-28 22:58:13 +01002674 data = htx->data - co_data(res);
Christopher Faulet99daf282018-11-28 22:58:13 +01002675 c_adv(res, data);
2676 res->total += data;
2677
2678 channel_auto_read(req);
2679 channel_abort(req);
2680 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002681 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet99daf282018-11-28 22:58:13 +01002682
2683 res->wex = tick_add_ifset(now_ms, res->wto);
2684 channel_auto_read(res);
2685 channel_auto_close(res);
2686 channel_shutr_now(res);
2687
2688 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002689
2690 if (!(s->flags & SF_ERR_MASK))
2691 s->flags |= SF_ERR_LOCAL;
2692 if (!(s->flags & SF_FINST_MASK))
2693 s->flags |= SF_FINST_R;
2694
Christopher Faulet99daf282018-11-28 22:58:13 +01002695 free_trash_chunk(chunk);
2696 return 1;
2697
2698 fail:
2699 /* If an error occurred, remove the incomplete HTTP response from the
2700 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01002701 channel_htx_truncate(res, htxbuf(&res->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02002702 free_trash_chunk(chunk);
Christopher Faulet99daf282018-11-28 22:58:13 +01002703 return 0;
Christopher Fauletf2824e62018-10-01 12:12:37 +02002704}
2705
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002706/* Replace all headers matching the name <name>. The header value is replaced if
2707 * it matches the regex <re>. <str> is used for the replacement. If <full> is
2708 * set to 1, the full-line is matched and replaced. Otherwise, comma-separated
2709 * values are evaluated one by one. It returns 0 on success and -1 on error.
2710 */
2711int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name,
2712 const char *str, struct my_regex *re, int full)
Christopher Faulet72333522018-10-24 11:25:02 +02002713{
2714 struct http_hdr_ctx ctx;
2715 struct buffer *output = get_trash_chunk();
2716
Christopher Faulet72333522018-10-24 11:25:02 +02002717 ctx.blk = NULL;
Christopher Faulet92d34fe2019-12-17 09:20:34 +01002718 while (http_find_header(htx, name, &ctx, full)) {
Christopher Faulet72333522018-10-24 11:25:02 +02002719 if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0))
2720 continue;
2721
2722 output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch);
2723 if (output->data == -1)
2724 return -1;
2725 if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data)))
2726 return -1;
2727 }
2728 return 0;
2729}
2730
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002731/* This function executes one of the set-{method,path,query,uri} actions. It
2732 * takes the string from the variable 'replace' with length 'len', then modifies
2733 * the relevant part of the request line accordingly. Then it updates various
2734 * pointers to the next elements which were moved, and the total buffer length.
2735 * It finds the action to be performed in p[2], previously filled by function
2736 * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal
2737 * error, though this can be revisited when this code is finally exploited.
2738 *
2739 * 'action' can be '0' to replace method, '1' to replace path, '2' to replace
2740 * query string and 3 to replace uri.
2741 *
2742 * In query string case, the mark question '?' must be set at the start of the
2743 * string by the caller, event if the replacement query string is empty.
2744 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002745int http_req_replace_stline(int action, const char *replace, int len,
2746 struct proxy *px, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002747{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002748 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002749
2750 switch (action) {
2751 case 0: // method
2752 if (!http_replace_req_meth(htx, ist2(replace, len)))
2753 return -1;
2754 break;
2755
2756 case 1: // path
2757 if (!http_replace_req_path(htx, ist2(replace, len)))
2758 return -1;
2759 break;
2760
2761 case 2: // query
2762 if (!http_replace_req_query(htx, ist2(replace, len)))
2763 return -1;
2764 break;
2765
2766 case 3: // uri
2767 if (!http_replace_req_uri(htx, ist2(replace, len)))
2768 return -1;
2769 break;
2770
2771 default:
2772 return -1;
2773 }
2774 return 0;
2775}
2776
2777/* This function replace the HTTP status code and the associated message. The
Christopher Faulete00d06c2019-12-16 17:18:42 +01002778 * variable <status> contains the new status code. This function never fails. It
2779 * returns 0 in case of success, -1 in case of internal error.
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002780 */
Christopher Faulet96bff762019-12-17 13:46:18 +01002781int http_res_set_status(unsigned int status, struct ist reason, struct stream *s)
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002782{
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002783 struct htx *htx = htxbuf(&s->res.buf);
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002784 char *res;
2785
2786 chunk_reset(&trash);
2787 res = ultoa_o(status, trash.area, trash.size);
2788 trash.data = res - trash.area;
2789
2790 /* Do we have a custom reason format string? */
Christopher Faulet96bff762019-12-17 13:46:18 +01002791 if (reason.ptr == NULL) {
2792 const char *str = http_get_reason(status);
2793 reason = ist2(str, strlen(str));
2794 }
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002795
Christopher Faulete00d06c2019-12-16 17:18:42 +01002796 if (!http_replace_res_status(htx, ist2(trash.area, trash.data)))
2797 return -1;
Christopher Faulet96bff762019-12-17 13:46:18 +01002798 if (!http_replace_res_reason(htx, reason))
Christopher Faulete00d06c2019-12-16 17:18:42 +01002799 return -1;
2800 return 0;
Christopher Faulet8d8ac192018-10-24 11:27:39 +02002801}
2802
Christopher Faulet3e964192018-10-24 11:39:23 +02002803/* Executes the http-request rules <rules> for stream <s>, proxy <px> and
2804 * transaction <txn>. Returns the verdict of the first rule that prevents
2805 * further processing of the request (auth, deny, ...), and defaults to
2806 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
2807 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
2808 * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL
2809 * and a deny/tarpit rule is matched, it will be filled with this rule's deny
2810 * status.
2811 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002812static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules,
2813 struct stream *s, int *deny_status)
Christopher Faulet3e964192018-10-24 11:39:23 +02002814{
2815 struct session *sess = strm_sess(s);
2816 struct http_txn *txn = s->txn;
2817 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002818 struct act_rule *rule;
2819 struct http_hdr_ctx ctx;
2820 const char *auth_realm;
Christopher Faulet3e964192018-10-24 11:39:23 +02002821 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002822 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002823
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002824 htx = htxbuf(&s->req.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002825
2826 /* If "the current_rule_list" match the executed rule list, we are in
2827 * resume condition. If a resume is needed it is always in the action
2828 * and never in the ACL or converters. In this case, we initialise the
2829 * current rule, and go to the action execution point.
2830 */
2831 if (s->current_rule) {
2832 rule = s->current_rule;
2833 s->current_rule = NULL;
2834 if (s->current_rule_list == rules)
2835 goto resume_execution;
2836 }
2837 s->current_rule_list = rules;
2838
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002839 /* start the ruleset evaluation in strict mode */
2840 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002841
Christopher Faulet3e964192018-10-24 11:39:23 +02002842 list_for_each_entry(rule, rules, list) {
2843 /* check optional condition */
2844 if (rule->cond) {
2845 int ret;
2846
2847 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2848 ret = acl_pass(ret);
2849
2850 if (rule->cond->pol == ACL_COND_UNLESS)
2851 ret = !ret;
2852
2853 if (!ret) /* condition not matched */
2854 continue;
2855 }
2856
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002857 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02002858 resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002859 /* Always call the action function if defined */
2860 if (rule->action_ptr) {
2861 if ((s->req.flags & CF_READ_ERROR) ||
2862 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
2863 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002864 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002865
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002866 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002867 case ACT_RET_CONT:
2868 break;
2869 case ACT_RET_STOP:
2870 rule_ret = HTTP_RULE_RES_STOP;
2871 goto end;
2872 case ACT_RET_YIELD:
2873 s->current_rule = rule;
2874 rule_ret = HTTP_RULE_RES_YIELD;
2875 goto end;
2876 case ACT_RET_ERR:
2877 rule_ret = HTTP_RULE_RES_ERROR;
2878 goto end;
2879 case ACT_RET_DONE:
2880 rule_ret = HTTP_RULE_RES_DONE;
2881 goto end;
2882 case ACT_RET_DENY:
2883 rule_ret = HTTP_RULE_RES_DENY;
2884 goto end;
2885 case ACT_RET_ABRT:
2886 rule_ret = HTTP_RULE_RES_ABRT;
2887 goto end;
2888 case ACT_RET_INV:
2889 rule_ret = HTTP_RULE_RES_BADREQ;
2890 goto end;
2891 }
2892 continue; /* eval the next rule */
2893 }
2894
2895 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02002896 switch (rule->action) {
2897 case ACT_ACTION_ALLOW:
2898 rule_ret = HTTP_RULE_RES_STOP;
2899 goto end;
2900
2901 case ACT_ACTION_DENY:
2902 if (deny_status)
Christopher Faulet96bff762019-12-17 13:46:18 +01002903 *deny_status = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002904 rule_ret = HTTP_RULE_RES_DENY;
2905 goto end;
2906
2907 case ACT_HTTP_REQ_TARPIT:
2908 txn->flags |= TX_CLTARPIT;
2909 if (deny_status)
Christopher Faulet96bff762019-12-17 13:46:18 +01002910 *deny_status = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002911 rule_ret = HTTP_RULE_RES_DENY;
2912 goto end;
2913
2914 case ACT_HTTP_REQ_AUTH:
Christopher Faulet3e964192018-10-24 11:39:23 +02002915 /* Auth might be performed on regular http-req rules as well as on stats */
Christopher Faulet96bff762019-12-17 13:46:18 +01002916 auth_realm = rule->arg.http.str.ptr;
Christopher Faulet3e964192018-10-24 11:39:23 +02002917 if (!auth_realm) {
2918 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
2919 auth_realm = STATS_DEFAULT_REALM;
2920 else
2921 auth_realm = px->id;
2922 }
2923 /* send 401/407 depending on whether we use a proxy or not. We still
2924 * count one error, because normal browsing won't significantly
2925 * increase the counter but brute force attempts will.
2926 */
Christopher Faulet3e964192018-10-24 11:39:23 +02002927 rule_ret = HTTP_RULE_RES_ABRT;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002928 if (http_reply_40x_unauthorized(s, auth_realm) == -1)
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002929 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet12c51e22018-11-28 15:59:42 +01002930 stream_inc_http_err_ctr(s);
Christopher Faulet3e964192018-10-24 11:39:23 +02002931 goto end;
2932
2933 case ACT_HTTP_REDIR:
Christopher Faulet3e964192018-10-24 11:39:23 +02002934 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002935 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01002936 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02002937 goto end;
2938
2939 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01002940 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002941 break;
2942
2943 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01002944 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002945 break;
2946
2947 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01002948 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02002949 break;
2950
2951 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01002952 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02002953 break;
2954
Christopher Faulet3e964192018-10-24 11:39:23 +02002955 case ACT_HTTP_DEL_HDR:
2956 /* remove all occurrences of the header */
2957 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01002958 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02002959 http_remove_header(htx, &ctx);
2960 break;
2961
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01002962 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02002963 default:
2964 break;
2965 }
2966 }
2967
2968 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002969 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01002970 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01002971 txn->req.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01002972
Christopher Faulet3e964192018-10-24 11:39:23 +02002973 /* we reached the end of the rules, nothing to report */
2974 return rule_ret;
2975}
2976
2977/* Executes the http-response rules <rules> for stream <s> and proxy <px>. It
2978 * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP,
2979 * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT
2980 * is returned, the process can continue the evaluation of next rule list. If
2981 * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ
2982 * is returned, it means the operation could not be processed and a server error
2983 * must be returned. It may set the TX_SVDENY on txn->flags if it encounters a
2984 * deny rule. If *YIELD is returned, the caller must call again the function
2985 * with the same context.
2986 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02002987static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules,
2988 struct stream *s)
Christopher Faulet3e964192018-10-24 11:39:23 +02002989{
2990 struct session *sess = strm_sess(s);
2991 struct http_txn *txn = s->txn;
2992 struct htx *htx;
Christopher Faulet3e964192018-10-24 11:39:23 +02002993 struct act_rule *rule;
2994 struct http_hdr_ctx ctx;
2995 enum rule_result rule_ret = HTTP_RULE_RES_CONT;
Christopher Faulet105ba6c2019-12-18 14:41:51 +01002996 int act_opts = 0;
Christopher Faulet3e964192018-10-24 11:39:23 +02002997
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01002998 htx = htxbuf(&s->res.buf);
Christopher Faulet3e964192018-10-24 11:39:23 +02002999
3000 /* If "the current_rule_list" match the executed rule list, we are in
3001 * resume condition. If a resume is needed it is always in the action
3002 * and never in the ACL or converters. In this case, we initialise the
3003 * current rule, and go to the action execution point.
3004 */
3005 if (s->current_rule) {
3006 rule = s->current_rule;
3007 s->current_rule = NULL;
3008 if (s->current_rule_list == rules)
3009 goto resume_execution;
3010 }
3011 s->current_rule_list = rules;
3012
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003013 /* start the ruleset evaluation in strict mode */
3014 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003015
Christopher Faulet3e964192018-10-24 11:39:23 +02003016 list_for_each_entry(rule, rules, list) {
3017 /* check optional condition */
3018 if (rule->cond) {
3019 int ret;
3020
3021 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3022 ret = acl_pass(ret);
3023
3024 if (rule->cond->pol == ACL_COND_UNLESS)
3025 ret = !ret;
3026
3027 if (!ret) /* condition not matched */
3028 continue;
3029 }
3030
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003031 act_opts |= ACT_OPT_FIRST;
Christopher Faulet3e964192018-10-24 11:39:23 +02003032resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003033
3034 /* Always call the action function if defined */
3035 if (rule->action_ptr) {
3036 if ((s->req.flags & CF_READ_ERROR) ||
3037 ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) &&
3038 (px->options & PR_O_ABRT_CLOSE)))
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003039 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003040
Christopher Faulet105ba6c2019-12-18 14:41:51 +01003041 switch (rule->action_ptr(rule, px, sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003042 case ACT_RET_CONT:
3043 break;
3044 case ACT_RET_STOP:
3045 rule_ret = HTTP_RULE_RES_STOP;
3046 goto end;
3047 case ACT_RET_YIELD:
3048 s->current_rule = rule;
3049 rule_ret = HTTP_RULE_RES_YIELD;
3050 goto end;
3051 case ACT_RET_ERR:
3052 rule_ret = HTTP_RULE_RES_ERROR;
3053 goto end;
3054 case ACT_RET_DONE:
3055 rule_ret = HTTP_RULE_RES_DONE;
3056 goto end;
3057 case ACT_RET_DENY:
3058 rule_ret = HTTP_RULE_RES_DENY;
3059 goto end;
3060 case ACT_RET_ABRT:
3061 rule_ret = HTTP_RULE_RES_ABRT;
3062 goto end;
3063 case ACT_RET_INV:
3064 rule_ret = HTTP_RULE_RES_BADREQ;
3065 goto end;
3066 }
3067 continue; /* eval the next rule */
3068 }
3069
3070 /* If not action function defined, check for known actions */
Christopher Faulet3e964192018-10-24 11:39:23 +02003071 switch (rule->action) {
3072 case ACT_ACTION_ALLOW:
3073 rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */
3074 goto end;
3075
3076 case ACT_ACTION_DENY:
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003077 rule_ret = HTTP_RULE_RES_DENY;
Christopher Faulet3e964192018-10-24 11:39:23 +02003078 goto end;
3079
3080 case ACT_HTTP_SET_NICE:
Christopher Faulet96bff762019-12-17 13:46:18 +01003081 s->task->nice = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003082 break;
3083
3084 case ACT_HTTP_SET_TOS:
Christopher Faulet96bff762019-12-17 13:46:18 +01003085 conn_set_tos(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003086 break;
3087
3088 case ACT_HTTP_SET_MARK:
Christopher Faulet96bff762019-12-17 13:46:18 +01003089 conn_set_mark(objt_conn(sess->origin), rule->arg.http.i);
Christopher Faulet3e964192018-10-24 11:39:23 +02003090 break;
3091
3092 case ACT_HTTP_SET_LOGL:
Christopher Faulet96bff762019-12-17 13:46:18 +01003093 s->logs.level = rule->arg.http.i;
Christopher Faulet3e964192018-10-24 11:39:23 +02003094 break;
3095
Christopher Faulet3e964192018-10-24 11:39:23 +02003096 case ACT_HTTP_DEL_HDR:
3097 /* remove all occurrences of the header */
3098 ctx.blk = NULL;
Christopher Faulet96bff762019-12-17 13:46:18 +01003099 while (http_find_header(htx, rule->arg.http.str, &ctx, 1))
Christopher Faulet3e964192018-10-24 11:39:23 +02003100 http_remove_header(htx, &ctx);
3101 break;
3102
Christopher Faulet3e964192018-10-24 11:39:23 +02003103 case ACT_HTTP_REDIR:
3104 rule_ret = HTTP_RULE_RES_DONE;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003105 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
Christopher Faulet3a26bee2019-12-16 12:47:40 +01003106 rule_ret = HTTP_RULE_RES_ERROR;
Christopher Faulet3e964192018-10-24 11:39:23 +02003107 goto end;
3108
Christopher Fauletcd26e8a2019-12-18 11:13:39 +01003109 /* other flags exists, but normally, they never be matched. */
Christopher Faulet3e964192018-10-24 11:39:23 +02003110 default:
3111 break;
3112 }
3113 }
3114
3115 end:
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003116 /* if the ruleset evaluation is finished reset the strict mode */
Christopher Faulet46f95542019-12-20 10:07:22 +01003117 if (rule_ret != HTTP_RULE_RES_YIELD)
Christopher Faulet1aea50e2020-01-17 16:03:53 +01003118 txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW;
Christopher Faulet46f95542019-12-20 10:07:22 +01003119
Christopher Faulet3e964192018-10-24 11:39:23 +02003120 /* we reached the end of the rules, nothing to report */
3121 return rule_ret;
3122}
3123
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003124/*
3125 * Manage client-side cookie. It can impact performance by about 2% so it is
3126 * desirable to call it only when needed. This code is quite complex because
3127 * of the multiple very crappy and ambiguous syntaxes we have to support. it
3128 * highly recommended not to touch this part without a good reason !
3129 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003130static void http_manage_client_side_cookies(struct stream *s, struct channel *req)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003131{
3132 struct session *sess = s->sess;
3133 struct http_txn *txn = s->txn;
3134 struct htx *htx;
3135 struct http_hdr_ctx ctx;
3136 char *hdr_beg, *hdr_end, *del_from;
3137 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
3138 int preserve_hdr;
3139
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003140 htx = htxbuf(&req->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003141 ctx.blk = NULL;
3142 while (http_find_header(htx, ist("Cookie"), &ctx, 1)) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003143 int is_first = 1;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003144 del_from = NULL; /* nothing to be deleted */
3145 preserve_hdr = 0; /* assume we may kill the whole header */
3146
3147 /* Now look for cookies. Conforming to RFC2109, we have to support
3148 * attributes whose name begin with a '$', and associate them with
3149 * the right cookie, if we want to delete this cookie.
3150 * So there are 3 cases for each cookie read :
3151 * 1) it's a special attribute, beginning with a '$' : ignore it.
3152 * 2) it's a server id cookie that we *MAY* want to delete : save
3153 * some pointers on it (last semi-colon, beginning of cookie...)
3154 * 3) it's an application cookie : we *MAY* have to delete a previous
3155 * "special" cookie.
3156 * At the end of loop, if a "special" cookie remains, we may have to
3157 * remove it. If no application cookie persists in the header, we
3158 * *MUST* delete it.
3159 *
3160 * Note: RFC2965 is unclear about the processing of spaces around
3161 * the equal sign in the ATTR=VALUE form. A careful inspection of
3162 * the RFC explicitly allows spaces before it, and not within the
3163 * tokens (attrs or values). An inspection of RFC2109 allows that
3164 * too but section 10.1.3 lets one think that spaces may be allowed
3165 * after the equal sign too, resulting in some (rare) buggy
3166 * implementations trying to do that. So let's do what servers do.
3167 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
3168 * allowed quoted strings in values, with any possible character
3169 * after a backslash, including control chars and delimitors, which
3170 * causes parsing to become ambiguous. Browsers also allow spaces
3171 * within values even without quotes.
3172 *
3173 * We have to keep multiple pointers in order to support cookie
3174 * removal at the beginning, middle or end of header without
3175 * corrupting the header. All of these headers are valid :
3176 *
3177 * hdr_beg hdr_end
3178 * | |
3179 * v |
3180 * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 |
3181 * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v
3182 * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3
3183 * | | | | | | |
3184 * | | | | | | |
3185 * | | | | | | +--> next
3186 * | | | | | +----> val_end
3187 * | | | | +-----------> val_beg
3188 * | | | +--------------> equal
3189 * | | +----------------> att_end
3190 * | +---------------------> att_beg
3191 * +--------------------------> prev
3192 *
3193 */
3194 hdr_beg = ctx.value.ptr;
3195 hdr_end = hdr_beg + ctx.value.len;
3196 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3197 /* Iterate through all cookies on this line */
3198
3199 /* find att_beg */
3200 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003201 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003202 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003203 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003204
3205 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3206 att_beg++;
3207
3208 /* find att_end : this is the first character after the last non
3209 * space before the equal. It may be equal to hdr_end.
3210 */
3211 equal = att_end = att_beg;
3212 while (equal < hdr_end) {
3213 if (*equal == '=' || *equal == ',' || *equal == ';')
3214 break;
3215 if (HTTP_IS_SPHT(*equal++))
3216 continue;
3217 att_end = equal;
3218 }
3219
3220 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3221 * is between <att_beg> and <equal>, both may be identical.
3222 */
3223 /* look for end of cookie if there is an equal sign */
3224 if (equal < hdr_end && *equal == '=') {
3225 /* look for the beginning of the value */
3226 val_beg = equal + 1;
3227 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3228 val_beg++;
3229
3230 /* find the end of the value, respecting quotes */
3231 next = http_find_cookie_value_end(val_beg, hdr_end);
3232
3233 /* make val_end point to the first white space or delimitor after the value */
3234 val_end = next;
3235 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3236 val_end--;
3237 }
3238 else
3239 val_beg = val_end = next = equal;
3240
3241 /* We have nothing to do with attributes beginning with
3242 * '$'. However, they will automatically be removed if a
3243 * header before them is removed, since they're supposed
3244 * to be linked together.
3245 */
3246 if (*att_beg == '$')
3247 continue;
3248
3249 /* Ignore cookies with no equal sign */
3250 if (equal == next) {
3251 /* This is not our cookie, so we must preserve it. But if we already
3252 * scheduled another cookie for removal, we cannot remove the
3253 * complete header, but we can remove the previous block itself.
3254 */
3255 preserve_hdr = 1;
3256 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003257 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003258 val_end += delta;
3259 next += delta;
3260 hdr_end += delta;
3261 prev = del_from;
3262 del_from = NULL;
3263 }
3264 continue;
3265 }
3266
3267 /* if there are spaces around the equal sign, we need to
3268 * strip them otherwise we'll get trouble for cookie captures,
3269 * or even for rewrites. Since this happens extremely rarely,
3270 * it does not hurt performance.
3271 */
3272 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3273 int stripped_before = 0;
3274 int stripped_after = 0;
3275
3276 if (att_end != equal) {
3277 memmove(att_end, equal, hdr_end - equal);
3278 stripped_before = (att_end - equal);
3279 equal += stripped_before;
3280 val_beg += stripped_before;
3281 }
3282
3283 if (val_beg > equal + 1) {
3284 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3285 stripped_after = (equal + 1) - val_beg;
3286 val_beg += stripped_after;
3287 stripped_before += stripped_after;
3288 }
3289
3290 val_end += stripped_before;
3291 next += stripped_before;
3292 hdr_end += stripped_before;
3293 }
3294 /* now everything is as on the diagram above */
3295
3296 /* First, let's see if we want to capture this cookie. We check
3297 * that we don't already have a client side cookie, because we
3298 * can only capture one. Also as an optimisation, we ignore
3299 * cookies shorter than the declared name.
3300 */
3301 if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL &&
3302 (val_end - att_beg >= sess->fe->capture_namelen) &&
3303 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3304 int log_len = val_end - att_beg;
3305
3306 if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) {
3307 ha_alert("HTTP logging : out of memory.\n");
3308 } else {
3309 if (log_len > sess->fe->capture_len)
3310 log_len = sess->fe->capture_len;
3311 memcpy(txn->cli_cookie, att_beg, log_len);
3312 txn->cli_cookie[log_len] = 0;
3313 }
3314 }
3315
3316 /* Persistence cookies in passive, rewrite or insert mode have the
3317 * following form :
3318 *
3319 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
3320 *
3321 * For cookies in prefix mode, the form is :
3322 *
3323 * Cookie: NAME=SRV~VALUE
3324 */
3325 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3326 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3327 struct server *srv = s->be->srv;
3328 char *delim;
3329
3330 /* if we're in cookie prefix mode, we'll search the delimitor so that we
3331 * have the server ID between val_beg and delim, and the original cookie between
3332 * delim+1 and val_end. Otherwise, delim==val_end :
3333 *
3334 * hdr_beg
3335 * |
3336 * v
3337 * NAME=SRV; # in all but prefix modes
3338 * NAME=SRV~OPAQUE ; # in prefix mode
3339 * || || | |+-> next
3340 * || || | +--> val_end
3341 * || || +---------> delim
3342 * || |+------------> val_beg
3343 * || +-------------> att_end = equal
3344 * |+-----------------> att_beg
3345 * +------------------> prev
3346 *
3347 */
3348 if (s->be->ck_opts & PR_CK_PFX) {
3349 for (delim = val_beg; delim < val_end; delim++)
3350 if (*delim == COOKIE_DELIM)
3351 break;
3352 }
3353 else {
3354 char *vbar1;
3355 delim = val_end;
3356 /* Now check if the cookie contains a date field, which would
3357 * appear after a vertical bar ('|') just after the server name
3358 * and before the delimiter.
3359 */
3360 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
3361 if (vbar1) {
3362 /* OK, so left of the bar is the server's cookie and
3363 * right is the last seen date. It is a base64 encoded
3364 * 30-bit value representing the UNIX date since the
3365 * epoch in 4-second quantities.
3366 */
3367 int val;
3368 delim = vbar1++;
3369 if (val_end - vbar1 >= 5) {
3370 val = b64tos30(vbar1);
3371 if (val > 0)
3372 txn->cookie_last_date = val << 2;
3373 }
3374 /* look for a second vertical bar */
3375 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
3376 if (vbar1 && (val_end - vbar1 > 5)) {
3377 val = b64tos30(vbar1 + 1);
3378 if (val > 0)
3379 txn->cookie_first_date = val << 2;
3380 }
3381 }
3382 }
3383
3384 /* if the cookie has an expiration date and the proxy wants to check
3385 * it, then we do that now. We first check if the cookie is too old,
3386 * then only if it has expired. We detect strict overflow because the
3387 * time resolution here is not great (4 seconds). Cookies with dates
3388 * in the future are ignored if their offset is beyond one day. This
3389 * allows an admin to fix timezone issues without expiring everyone
3390 * and at the same time avoids keeping unwanted side effects for too
3391 * long.
3392 */
3393 if (txn->cookie_first_date && s->be->cookie_maxlife &&
3394 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
3395 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
3396 txn->flags &= ~TX_CK_MASK;
3397 txn->flags |= TX_CK_OLD;
3398 delim = val_beg; // let's pretend we have not found the cookie
3399 txn->cookie_first_date = 0;
3400 txn->cookie_last_date = 0;
3401 }
3402 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
3403 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
3404 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
3405 txn->flags &= ~TX_CK_MASK;
3406 txn->flags |= TX_CK_EXPIRED;
3407 delim = val_beg; // let's pretend we have not found the cookie
3408 txn->cookie_first_date = 0;
3409 txn->cookie_last_date = 0;
3410 }
3411
3412 /* Here, we'll look for the first running server which supports the cookie.
3413 * This allows to share a same cookie between several servers, for example
3414 * to dedicate backup servers to specific servers only.
3415 * However, to prevent clients from sticking to cookie-less backup server
3416 * when they have incidentely learned an empty cookie, we simply ignore
3417 * empty cookies and mark them as invalid.
3418 * The same behaviour is applied when persistence must be ignored.
3419 */
3420 if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3421 srv = NULL;
3422
3423 while (srv) {
3424 if (srv->cookie && (srv->cklen == delim - val_beg) &&
3425 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
3426 if ((srv->cur_state != SRV_ST_STOPPED) ||
3427 (s->be->options & PR_O_PERSIST) ||
3428 (s->flags & SF_FORCE_PRST)) {
3429 /* we found the server and we can use it */
3430 txn->flags &= ~TX_CK_MASK;
3431 txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
3432 s->flags |= SF_DIRECT | SF_ASSIGNED;
3433 s->target = &srv->obj_type;
3434 break;
3435 } else {
3436 /* we found a server, but it's down,
3437 * mark it as such and go on in case
3438 * another one is available.
3439 */
3440 txn->flags &= ~TX_CK_MASK;
3441 txn->flags |= TX_CK_DOWN;
3442 }
3443 }
3444 srv = srv->next;
3445 }
3446
3447 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
3448 /* no server matched this cookie or we deliberately skipped it */
3449 txn->flags &= ~TX_CK_MASK;
3450 if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED)))
3451 txn->flags |= TX_CK_UNUSED;
3452 else
3453 txn->flags |= TX_CK_INVALID;
3454 }
3455
3456 /* depending on the cookie mode, we may have to either :
3457 * - delete the complete cookie if we're in insert+indirect mode, so that
3458 * the server never sees it ;
3459 * - remove the server id from the cookie value, and tag the cookie as an
Joseph Herlante9d5c722018-11-25 11:00:25 -08003460 * application cookie so that it does not get accidentally removed later,
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003461 * if we're in cookie prefix mode
3462 */
3463 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
3464 int delta; /* negative */
3465
3466 memmove(val_beg, delim + 1, hdr_end - (delim + 1));
3467 delta = val_beg - (delim + 1);
3468 val_end += delta;
3469 next += delta;
3470 hdr_end += delta;
3471 del_from = NULL;
3472 preserve_hdr = 1; /* we want to keep this cookie */
3473 }
3474 else if (del_from == NULL &&
3475 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
3476 del_from = prev;
3477 }
3478 }
3479 else {
3480 /* This is not our cookie, so we must preserve it. But if we already
3481 * scheduled another cookie for removal, we cannot remove the
3482 * complete header, but we can remove the previous block itself.
3483 */
3484 preserve_hdr = 1;
3485
3486 if (del_from != NULL) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003487 int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003488 if (att_beg >= del_from)
3489 att_beg += delta;
3490 if (att_end >= del_from)
3491 att_end += delta;
3492 val_beg += delta;
3493 val_end += delta;
3494 next += delta;
3495 hdr_end += delta;
3496 prev = del_from;
3497 del_from = NULL;
3498 }
3499 }
3500
3501 /* continue with next cookie on this header line */
3502 att_beg = next;
3503 } /* for each cookie */
3504
3505
3506 /* There are no more cookies on this line.
3507 * We may still have one (or several) marked for deletion at the
3508 * end of the line. We must do this now in two ways :
3509 * - if some cookies must be preserved, we only delete from the
3510 * mark to the end of line ;
3511 * - if nothing needs to be preserved, simply delete the whole header
3512 */
3513 if (del_from) {
3514 hdr_end = (preserve_hdr ? del_from : hdr_beg);
3515 }
3516 if ((hdr_end - hdr_beg) != ctx.value.len) {
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003517 if (hdr_beg != hdr_end)
3518 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003519 else
3520 http_remove_header(htx, &ctx);
3521 }
3522 } /* for each "Cookie header */
3523}
3524
3525/*
3526 * Manage server-side cookies. It can impact performance by about 2% so it is
3527 * desirable to call it only when needed. This function is also used when we
3528 * just need to know if there is a cookie (eg: for check-cache).
3529 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003530static void http_manage_server_side_cookies(struct stream *s, struct channel *res)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003531{
3532 struct session *sess = s->sess;
3533 struct http_txn *txn = s->txn;
3534 struct htx *htx;
3535 struct http_hdr_ctx ctx;
3536 struct server *srv;
3537 char *hdr_beg, *hdr_end;
3538 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau6f7a02a2019-04-15 21:49:49 +02003539 int is_cookie2 = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003540
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003541 htx = htxbuf(&res->buf);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003542
3543 ctx.blk = NULL;
3544 while (1) {
Olivier Houchardf0f42382019-07-22 17:43:46 +02003545 int is_first = 1;
3546
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003547 if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) {
3548 if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1))
3549 break;
3550 is_cookie2 = 1;
3551 }
3552
3553 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
3554 * <prev> points to the colon.
3555 */
3556 txn->flags |= TX_SCK_PRESENT;
3557
3558 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
3559 * check-cache is enabled) and we are not interested in checking
3560 * them. Warning, the cookie capture is declared in the frontend.
3561 */
3562 if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL)
3563 break;
3564
3565 /* OK so now we know we have to process this response cookie.
3566 * The format of the Set-Cookie header is slightly different
3567 * from the format of the Cookie header in that it does not
3568 * support the comma as a cookie delimiter (thus the header
3569 * cannot be folded) because the Expires attribute described in
3570 * the original Netscape's spec may contain an unquoted date
3571 * with a comma inside. We have to live with this because
3572 * many browsers don't support Max-Age and some browsers don't
3573 * support quoted strings. However the Set-Cookie2 header is
3574 * clean.
3575 *
3576 * We have to keep multiple pointers in order to support cookie
3577 * removal at the beginning, middle or end of header without
3578 * corrupting the header (in case of set-cookie2). A special
3579 * pointer, <scav> points to the beginning of the set-cookie-av
3580 * fields after the first semi-colon. The <next> pointer points
3581 * either to the end of line (set-cookie) or next unquoted comma
3582 * (set-cookie2). All of these headers are valid :
3583 *
3584 * hdr_beg hdr_end
3585 * | |
3586 * v |
3587 * NAME1 = VALUE 1 ; Secure; Path="/" |
3588 * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v
3589 * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT
3590 * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard
3591 * | | | | | | | |
3592 * | | | | | | | +-> next
3593 * | | | | | | +------------> scav
3594 * | | | | | +--------------> val_end
3595 * | | | | +--------------------> val_beg
3596 * | | | +----------------------> equal
3597 * | | +------------------------> att_end
3598 * | +----------------------------> att_beg
3599 * +------------------------------> prev
3600 * -------------------------------> hdr_beg
3601 */
3602 hdr_beg = ctx.value.ptr;
3603 hdr_end = hdr_beg + ctx.value.len;
3604 for (prev = hdr_beg; prev < hdr_end; prev = next) {
3605
3606 /* Iterate through all cookies on this line */
3607
3608 /* find att_beg */
3609 att_beg = prev;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003610 if (!is_first)
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003611 att_beg++;
Olivier Houchardf0f42382019-07-22 17:43:46 +02003612 is_first = 0;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003613
3614 while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg))
3615 att_beg++;
3616
3617 /* find att_end : this is the first character after the last non
3618 * space before the equal. It may be equal to hdr_end.
3619 */
3620 equal = att_end = att_beg;
3621
3622 while (equal < hdr_end) {
3623 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
3624 break;
3625 if (HTTP_IS_SPHT(*equal++))
3626 continue;
3627 att_end = equal;
3628 }
3629
3630 /* here, <equal> points to '=', a delimitor or the end. <att_end>
3631 * is between <att_beg> and <equal>, both may be identical.
3632 */
3633
3634 /* look for end of cookie if there is an equal sign */
3635 if (equal < hdr_end && *equal == '=') {
3636 /* look for the beginning of the value */
3637 val_beg = equal + 1;
3638 while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg))
3639 val_beg++;
3640
3641 /* find the end of the value, respecting quotes */
3642 next = http_find_cookie_value_end(val_beg, hdr_end);
3643
3644 /* make val_end point to the first white space or delimitor after the value */
3645 val_end = next;
3646 while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1)))
3647 val_end--;
3648 }
3649 else {
3650 /* <equal> points to next comma, semi-colon or EOL */
3651 val_beg = val_end = next = equal;
3652 }
3653
3654 if (next < hdr_end) {
3655 /* Set-Cookie2 supports multiple cookies, and <next> points to
3656 * a colon or semi-colon before the end. So skip all attr-value
3657 * pairs and look for the next comma. For Set-Cookie, since
3658 * commas are permitted in values, skip to the end.
3659 */
3660 if (is_cookie2)
3661 next = http_find_hdr_value_end(next, hdr_end);
3662 else
3663 next = hdr_end;
3664 }
3665
3666 /* Now everything is as on the diagram above */
3667
3668 /* Ignore cookies with no equal sign */
3669 if (equal == val_end)
3670 continue;
3671
3672 /* If there are spaces around the equal sign, we need to
3673 * strip them otherwise we'll get trouble for cookie captures,
3674 * or even for rewrites. Since this happens extremely rarely,
3675 * it does not hurt performance.
3676 */
3677 if (unlikely(att_end != equal || val_beg > equal + 1)) {
3678 int stripped_before = 0;
3679 int stripped_after = 0;
3680
3681 if (att_end != equal) {
3682 memmove(att_end, equal, hdr_end - equal);
3683 stripped_before = (att_end - equal);
3684 equal += stripped_before;
3685 val_beg += stripped_before;
3686 }
3687
3688 if (val_beg > equal + 1) {
3689 memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg);
3690 stripped_after = (equal + 1) - val_beg;
3691 val_beg += stripped_after;
3692 stripped_before += stripped_after;
3693 }
3694
3695 val_end += stripped_before;
3696 next += stripped_before;
3697 hdr_end += stripped_before;
3698
Christopher Faulet3e2638e2019-06-18 09:49:16 +02003699 htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003700 ctx.value.len = hdr_end - hdr_beg;
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003701 }
3702
3703 /* First, let's see if we want to capture this cookie. We check
3704 * that we don't already have a server side cookie, because we
3705 * can only capture one. Also as an optimisation, we ignore
3706 * cookies shorter than the declared name.
3707 */
3708 if (sess->fe->capture_name != NULL &&
3709 txn->srv_cookie == NULL &&
3710 (val_end - att_beg >= sess->fe->capture_namelen) &&
3711 memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) {
3712 int log_len = val_end - att_beg;
3713 if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) {
3714 ha_alert("HTTP logging : out of memory.\n");
3715 }
3716 else {
3717 if (log_len > sess->fe->capture_len)
3718 log_len = sess->fe->capture_len;
3719 memcpy(txn->srv_cookie, att_beg, log_len);
3720 txn->srv_cookie[log_len] = 0;
3721 }
3722 }
3723
3724 srv = objt_server(s->target);
3725 /* now check if we need to process it for persistence */
3726 if (!(s->flags & SF_IGNORE_PRST) &&
3727 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
3728 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
3729 /* assume passive cookie by default */
3730 txn->flags &= ~TX_SCK_MASK;
3731 txn->flags |= TX_SCK_FOUND;
3732
3733 /* If the cookie is in insert mode on a known server, we'll delete
3734 * this occurrence because we'll insert another one later.
3735 * We'll delete it too if the "indirect" option is set and we're in
3736 * a direct access.
3737 */
3738 if (s->be->ck_opts & PR_CK_PSV) {
3739 /* The "preserve" flag was set, we don't want to touch the
3740 * server's cookie.
3741 */
3742 }
3743 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
3744 ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
3745 /* this cookie must be deleted */
3746 if (prev == hdr_beg && next == hdr_end) {
3747 /* whole header */
3748 http_remove_header(htx, &ctx);
3749 /* note: while both invalid now, <next> and <hdr_end>
3750 * are still equal, so the for() will stop as expected.
3751 */
3752 } else {
3753 /* just remove the value */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003754 int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next);
Christopher Fauletfcda7c62018-10-24 11:56:22 +02003755 next = prev;
3756 hdr_end += delta;
3757 }
3758 txn->flags &= ~TX_SCK_MASK;
3759 txn->flags |= TX_SCK_DELETED;
3760 /* and go on with next cookie */
3761 }
3762 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
3763 /* replace bytes val_beg->val_end with the cookie name associated
3764 * with this server since we know it.
3765 */
3766 int sliding, delta;
3767
3768 ctx.value = ist2(val_beg, val_end - val_beg);
3769 ctx.lws_before = ctx.lws_after = 0;
3770 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen));
3771 delta = srv->cklen - (val_end - val_beg);
3772 sliding = (ctx.value.ptr - val_beg);
3773 hdr_beg += sliding;
3774 val_beg += sliding;
3775 next += sliding + delta;
3776 hdr_end += sliding + delta;
3777
3778 txn->flags &= ~TX_SCK_MASK;
3779 txn->flags |= TX_SCK_REPLACED;
3780 }
3781 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
3782 /* insert the cookie name associated with this server
3783 * before existing cookie, and insert a delimiter between them..
3784 */
3785 int sliding, delta;
3786 ctx.value = ist2(val_beg, 0);
3787 ctx.lws_before = ctx.lws_after = 0;
3788 http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1));
3789 delta = srv->cklen + 1;
3790 sliding = (ctx.value.ptr - val_beg);
3791 hdr_beg += sliding;
3792 val_beg += sliding;
3793 next += sliding + delta;
3794 hdr_end += sliding + delta;
3795
3796 val_beg[srv->cklen] = COOKIE_DELIM;
3797 txn->flags &= ~TX_SCK_MASK;
3798 txn->flags |= TX_SCK_REPLACED;
3799 }
3800 }
3801 /* that's done for this cookie, check the next one on the same
3802 * line when next != hdr_end (only if is_cookie2).
3803 */
3804 }
3805 }
3806}
3807
Christopher Faulet25a02f62018-10-24 12:00:25 +02003808/*
3809 * Parses the Cache-Control and Pragma request header fields to determine if
3810 * the request may be served from the cache and/or if it is cacheable. Updates
3811 * s->txn->flags.
3812 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003813void http_check_request_for_cacheability(struct stream *s, struct channel *req)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003814{
3815 struct http_txn *txn = s->txn;
3816 struct htx *htx;
3817 int32_t pos;
3818 int pragma_found, cc_found, i;
3819
3820 if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE)
3821 return; /* nothing more to do here */
3822
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003823 htx = htxbuf(&req->buf);
Christopher Faulet25a02f62018-10-24 12:00:25 +02003824 pragma_found = cc_found = 0;
Christopher Fauleta3f15502019-05-13 15:27:23 +02003825 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003826 struct htx_blk *blk = htx_get_blk(htx, pos);
3827 enum htx_blk_type type = htx_get_blk_type(blk);
3828 struct ist n, v;
3829
3830 if (type == HTX_BLK_EOH)
3831 break;
3832 if (type != HTX_BLK_HDR)
3833 continue;
3834
3835 n = htx_get_blk_name(htx, blk);
3836 v = htx_get_blk_value(htx, blk);
3837
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003838 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003839 if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3840 pragma_found = 1;
3841 continue;
3842 }
3843 }
3844
3845 /* Don't use the cache and don't try to store if we found the
3846 * Authorization header */
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003847 if (isteq(n, ist("authorization"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003848 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3849 txn->flags |= TX_CACHE_IGNORE;
3850 continue;
3851 }
3852
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003853 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02003854 continue;
3855
3856 /* OK, right now we know we have a cache-control header */
3857 cc_found = 1;
3858 if (!v.len) /* no info */
3859 continue;
3860
3861 i = 0;
3862 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
3863 !isspace((unsigned char)*(v.ptr+i)))
3864 i++;
3865
3866 /* we have a complete value between v.ptr and (v.ptr+i). We don't check the
3867 * values after max-age, max-stale nor min-fresh, we simply don't
3868 * use the cache when they're specified.
3869 */
3870 if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) ||
3871 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
3872 ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) ||
3873 ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) {
3874 txn->flags |= TX_CACHE_IGNORE;
3875 continue;
3876 }
3877
3878 if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) {
3879 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3880 continue;
3881 }
3882 }
3883
3884 /* RFC7234#5.4:
3885 * When the Cache-Control header field is also present and
3886 * understood in a request, Pragma is ignored.
3887 * When the Cache-Control header field is not present in a
3888 * request, caches MUST consider the no-cache request
3889 * pragma-directive as having the same effect as if
3890 * "Cache-Control: no-cache" were present.
3891 */
3892 if (!cc_found && pragma_found)
3893 txn->flags |= TX_CACHE_IGNORE;
3894}
3895
3896/*
3897 * Check if response is cacheable or not. Updates s->txn->flags.
3898 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003899void http_check_response_for_cacheability(struct stream *s, struct channel *res)
Christopher Faulet25a02f62018-10-24 12:00:25 +02003900{
3901 struct http_txn *txn = s->txn;
3902 struct htx *htx;
3903 int32_t pos;
3904 int i;
3905
3906 if (txn->status < 200) {
3907 /* do not try to cache interim responses! */
3908 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3909 return;
3910 }
3911
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003912 htx = htxbuf(&res->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02003913 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003914 struct htx_blk *blk = htx_get_blk(htx, pos);
3915 enum htx_blk_type type = htx_get_blk_type(blk);
3916 struct ist n, v;
3917
3918 if (type == HTX_BLK_EOH)
3919 break;
3920 if (type != HTX_BLK_HDR)
3921 continue;
3922
3923 n = htx_get_blk_name(htx, blk);
3924 v = htx_get_blk_value(htx, blk);
3925
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003926 if (isteq(n, ist("pragma"))) {
Christopher Faulet25a02f62018-10-24 12:00:25 +02003927 if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) {
3928 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3929 return;
3930 }
3931 }
3932
Willy Tarreau2e754bf2018-12-07 11:38:03 +01003933 if (!isteq(n, ist("cache-control")))
Christopher Faulet25a02f62018-10-24 12:00:25 +02003934 continue;
3935
3936 /* OK, right now we know we have a cache-control header */
3937 if (!v.len) /* no info */
3938 continue;
3939
3940 i = 0;
3941 while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' &&
3942 !isspace((unsigned char)*(v.ptr+i)))
3943 i++;
3944
3945 /* we have a complete value between v.ptr and (v.ptr+i) */
3946 if (i < v.len && *(v.ptr + i) == '=') {
3947 if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) ||
3948 ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) {
3949 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3950 continue;
3951 }
3952
3953 /* we have something of the form no-cache="set-cookie" */
3954 if ((v.len >= 21) &&
3955 strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0
3956 && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ','))
3957 txn->flags &= ~TX_CACHE_COOK;
3958 continue;
3959 }
3960
3961 /* OK, so we know that either p2 points to the end of string or to a comma */
3962 if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) ||
3963 ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) ||
3964 ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) {
3965 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
3966 return;
3967 }
3968
3969 if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) {
3970 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
3971 continue;
3972 }
3973 }
3974}
3975
Christopher Faulet377c5a52018-10-24 21:21:30 +02003976/*
3977 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
3978 * for the current backend.
3979 *
3980 * It is assumed that the request is either a HEAD, GET, or POST and that the
3981 * uri_auth field is valid.
3982 *
3983 * Returns 1 if stats should be provided, otherwise 0.
3984 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02003985static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend)
Christopher Faulet377c5a52018-10-24 21:21:30 +02003986{
3987 struct uri_auth *uri_auth = backend->uri_auth;
3988 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01003989 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003990 struct ist uri;
Christopher Faulet377c5a52018-10-24 21:21:30 +02003991
3992 if (!uri_auth)
3993 return 0;
3994
3995 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
3996 return 0;
3997
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01003998 htx = htxbuf(&s->req.buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02003999 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004000 uri = htx_sl_req_uri(sl);
Willy Tarreau1eb3b482019-10-31 15:50:28 +01004001 if (*uri_auth->uri_prefix == '/')
4002 uri = http_get_path(uri);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004003
4004 /* check URI size */
4005 if (uri_auth->uri_len > uri.len)
4006 return 0;
4007
4008 if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
4009 return 0;
4010
4011 return 1;
4012}
4013
4014/* This function prepares an applet to handle the stats. It can deal with the
4015 * "100-continue" expectation, check that admin rules are met for POST requests,
4016 * and program a response message if something was unexpected. It cannot fail
4017 * and always relies on the stats applet to complete the job. It does not touch
4018 * analysers nor counters, which are left to the caller. It does not touch
4019 * s->target which is supposed to already point to the stats applet. The caller
4020 * is expected to have already assigned an appctx to the stream.
4021 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004022static int http_handle_stats(struct stream *s, struct channel *req)
Christopher Faulet377c5a52018-10-24 21:21:30 +02004023{
4024 struct stats_admin_rule *stats_admin_rule;
4025 struct stream_interface *si = &s->si[1];
4026 struct session *sess = s->sess;
4027 struct http_txn *txn = s->txn;
4028 struct http_msg *msg = &txn->req;
4029 struct uri_auth *uri_auth = s->be->uri_auth;
4030 const char *h, *lookup, *end;
4031 struct appctx *appctx;
4032 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004033 struct htx_sl *sl;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004034
4035 appctx = si_appctx(si);
4036 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
4037 appctx->st1 = appctx->st2 = 0;
4038 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau676c29e2019-10-09 10:50:01 +02004039 appctx->ctx.stats.flags |= uri_auth->flags;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004040 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
4041 if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD))
4042 appctx->ctx.stats.flags |= STAT_CHUNKED;
4043
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004044 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004045 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004046 lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len;
4047 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004048
4049 for (h = lookup; h <= end - 3; h++) {
4050 if (memcmp(h, ";up", 3) == 0) {
4051 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
4052 break;
4053 }
4054 }
4055
4056 if (uri_auth->refresh) {
4057 for (h = lookup; h <= end - 10; h++) {
4058 if (memcmp(h, ";norefresh", 10) == 0) {
4059 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
4060 break;
4061 }
4062 }
4063 }
4064
4065 for (h = lookup; h <= end - 4; h++) {
4066 if (memcmp(h, ";csv", 4) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004067 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004068 break;
4069 }
4070 }
4071
4072 for (h = lookup; h <= end - 6; h++) {
4073 if (memcmp(h, ";typed", 6) == 0) {
Christopher Faulet6338a082019-09-09 15:50:54 +02004074 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
Christopher Faulet377c5a52018-10-24 21:21:30 +02004075 appctx->ctx.stats.flags |= STAT_FMT_TYPED;
4076 break;
4077 }
4078 }
4079
Christopher Faulet6338a082019-09-09 15:50:54 +02004080 for (h = lookup; h <= end - 5; h++) {
4081 if (memcmp(h, ";json", 5) == 0) {
4082 appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM);
4083 appctx->ctx.stats.flags |= STAT_FMT_JSON;
4084 break;
4085 }
4086 }
4087
4088 for (h = lookup; h <= end - 12; h++) {
4089 if (memcmp(h, ";json-schema", 12) == 0) {
4090 appctx->ctx.stats.flags &= ~STAT_FMT_MASK;
4091 appctx->ctx.stats.flags |= STAT_JSON_SCHM;
4092 break;
4093 }
4094 }
4095
Christopher Faulet377c5a52018-10-24 21:21:30 +02004096 for (h = lookup; h <= end - 8; h++) {
4097 if (memcmp(h, ";st=", 4) == 0) {
4098 int i;
4099 h += 4;
4100 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
4101 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
4102 if (strncmp(stat_status_codes[i], h, 4) == 0) {
4103 appctx->ctx.stats.st_code = i;
4104 break;
4105 }
4106 }
4107 break;
4108 }
4109 }
4110
4111 appctx->ctx.stats.scope_str = 0;
4112 appctx->ctx.stats.scope_len = 0;
4113 for (h = lookup; h <= end - 8; h++) {
4114 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
4115 int itx = 0;
4116 const char *h2;
4117 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
4118 const char *err;
4119
4120 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
4121 h2 = h;
Christopher Fauleted7a0662019-01-14 11:07:34 +01004122 appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl);
4123 while (h < end) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004124 if (*h == ';' || *h == '&' || *h == ' ')
4125 break;
4126 itx++;
4127 h++;
4128 }
4129
4130 if (itx > STAT_SCOPE_TXT_MAXLEN)
4131 itx = STAT_SCOPE_TXT_MAXLEN;
4132 appctx->ctx.stats.scope_len = itx;
4133
4134 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
4135 memcpy(scope_txt, h2, itx);
4136 scope_txt[itx] = '\0';
4137 err = invalid_char(scope_txt);
4138 if (err) {
4139 /* bad char in search text => clear scope */
4140 appctx->ctx.stats.scope_str = 0;
4141 appctx->ctx.stats.scope_len = 0;
4142 }
4143 break;
4144 }
4145 }
4146
4147 /* now check whether we have some admin rules for this request */
4148 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
4149 int ret = 1;
4150
4151 if (stats_admin_rule->cond) {
4152 ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
4153 ret = acl_pass(ret);
4154 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
4155 ret = !ret;
4156 }
4157
4158 if (ret) {
4159 /* no rule, or the rule matches */
4160 appctx->ctx.stats.flags |= STAT_ADMIN;
4161 break;
4162 }
4163 }
4164
Christopher Faulet5d45e382019-02-27 15:15:23 +01004165 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
4166 appctx->st0 = STAT_HTTP_HEAD;
4167 else if (txn->meth == HTTP_METH_POST) {
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004168 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Christopher Faulet377c5a52018-10-24 21:21:30 +02004169 appctx->st0 = STAT_HTTP_POST;
Christopher Fauletbd9e8422019-08-15 22:26:48 +02004170 if (msg->msg_state < HTTP_MSG_DATA)
4171 req->analysers |= AN_REQ_HTTP_BODY;
4172 }
Christopher Faulet377c5a52018-10-24 21:21:30 +02004173 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004174 /* POST without admin level */
Christopher Faulet377c5a52018-10-24 21:21:30 +02004175 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4176 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
4177 appctx->st0 = STAT_HTTP_LAST;
4178 }
4179 }
4180 else {
Christopher Faulet5d45e382019-02-27 15:15:23 +01004181 /* Unsupported method */
4182 appctx->ctx.stats.flags &= ~STAT_CHUNKED;
4183 appctx->ctx.stats.st_code = STAT_STATUS_IVAL;
4184 appctx->st0 = STAT_HTTP_LAST;
Christopher Faulet377c5a52018-10-24 21:21:30 +02004185 }
4186
4187 s->task->nice = -32; /* small boost for HTTP statistics */
4188 return 1;
4189}
4190
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004191void http_perform_server_redirect(struct stream *s, struct stream_interface *si)
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004192{
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004193 struct channel *req = &s->req;
4194 struct channel *res = &s->res;
4195 struct server *srv;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004196 struct htx *htx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004197 struct htx_sl *sl;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004198 struct ist path, location;
4199 unsigned int flags;
4200 size_t data;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004201
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004202 /*
4203 * Create the location
4204 */
4205 chunk_reset(&trash);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004206
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004207 /* 1: add the server's prefix */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004208 /* special prefix "/" means don't change URL */
4209 srv = __objt_server(s->target);
4210 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
4211 if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len))
4212 return;
4213 }
4214
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004215 /* 2: add the request Path */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +01004216 htx = htxbuf(&req->buf);
Christopher Faulet297fbb42019-05-13 14:41:27 +02004217 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004218 path = http_get_path(htx_sl_req_uri(sl));
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004219 if (!path.ptr)
4220 return;
4221
4222 if (!chunk_memcat(&trash, path.ptr, path.len))
4223 return;
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004224 location = ist2(trash.area, trash.data);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004225
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004226 /*
4227 * Create the 302 respone
4228 */
4229 htx = htx_from_buf(&res->buf);
4230 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4231 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4232 ist("HTTP/1.1"), ist("302"), ist("Found"));
4233 if (!sl)
4234 goto fail;
4235 sl->info.res.status = 302;
4236 s->txn->status = 302;
4237
4238 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
4239 !htx_add_header(htx, ist("Connection"), ist("close")) ||
4240 !htx_add_header(htx, ist("Content-length"), ist("0")) ||
4241 !htx_add_header(htx, ist("Location"), location))
4242 goto fail;
4243
4244 if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM))
4245 goto fail;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004246
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004247 /*
4248 * Send the message
4249 */
4250 data = htx->data - co_data(res);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004251 c_adv(res, data);
4252 res->total += data;
4253
4254 /* return without error. */
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004255 si_shutr(si);
4256 si_shutw(si);
4257 si->err_type = SI_ET_NONE;
4258 si->state = SI_ST_CLO;
4259
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004260 channel_auto_read(req);
4261 channel_abort(req);
4262 channel_auto_close(req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004263 channel_htx_erase(req, htxbuf(&req->buf));
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004264 channel_auto_read(res);
4265 channel_auto_close(res);
4266
4267 if (!(s->flags & SF_ERR_MASK))
4268 s->flags |= SF_ERR_LOCAL;
4269 if (!(s->flags & SF_FINST_MASK))
4270 s->flags |= SF_FINST_C;
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004271
4272 /* FIXME: we should increase a counter of redirects per server and per backend. */
4273 srv_inc_sess_ctr(srv);
4274 srv_set_sess_last(srv);
Christopher Faulet0eaed6b2018-11-28 17:46:40 +01004275 return;
4276
4277 fail:
4278 /* If an error occurred, remove the incomplete HTTP response from the
4279 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004280 channel_htx_truncate(res, htx);
Christopher Fauletfefc73d2018-10-24 21:18:04 +02004281}
4282
Christopher Fauletf2824e62018-10-01 12:12:37 +02004283/* This function terminates the request because it was completly analyzed or
4284 * because an error was triggered during the body forwarding.
4285 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004286static void http_end_request(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004287{
4288 struct channel *chn = &s->req;
4289 struct http_txn *txn = s->txn;
4290
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004291 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004292
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004293 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4294 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004295 channel_abort(chn);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004296 channel_htx_truncate(chn, htxbuf(&chn->buf));
Christopher Fauletf2824e62018-10-01 12:12:37 +02004297 goto end;
4298 }
4299
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004300 if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) {
4301 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004302 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004303 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004304
4305 if (txn->req.msg_state == HTTP_MSG_DONE) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004306 /* No need to read anymore, the request was completely parsed.
4307 * We can shut the read side unless we want to abort_on_close,
4308 * or we have a POST request. The issue with POST requests is
4309 * that some browsers still send a CRLF after the request, and
4310 * this CRLF must be read so that it does not remain in the kernel
4311 * buffers, otherwise a close could cause an RST on some systems
4312 * (eg: Linux).
4313 */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004314 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004315 channel_dont_read(chn);
4316
4317 /* if the server closes the connection, we want to immediately react
4318 * and close the socket to save packets and syscalls.
4319 */
4320 s->si[1].flags |= SI_FL_NOHALF;
4321
4322 /* In any case we've finished parsing the request so we must
4323 * disable Nagle when sending data because 1) we're not going
4324 * to shut this side, and 2) the server is waiting for us to
4325 * send pending data.
4326 */
4327 chn->flags |= CF_NEVER_WAIT;
4328
Christopher Fauletd01ce402019-01-02 17:44:13 +01004329 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4330 /* The server has not finished to respond, so we
4331 * don't want to move in order not to upset it.
4332 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004333 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletd01ce402019-01-02 17:44:13 +01004334 return;
4335 }
4336
Christopher Fauletf2824e62018-10-01 12:12:37 +02004337 /* When we get here, it means that both the request and the
4338 * response have finished receiving. Depending on the connection
4339 * mode, we'll have to wait for the last bytes to leave in either
4340 * direction, and sometimes for a close to be effective.
4341 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004342 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004343 /* Tunnel mode will not have any analyser so it needs to
4344 * poll for reads.
4345 */
4346 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004347 if (b_data(&chn->buf)) {
4348 DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004349 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004350 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004351 txn->req.msg_state = HTTP_MSG_TUNNEL;
4352 }
4353 else {
4354 /* we're not expecting any new data to come for this
4355 * transaction, so we can close it.
Christopher Faulet9768c262018-10-22 09:34:31 +02004356 *
4357 * However, there is an exception if the response
4358 * length is undefined. In this case, we need to wait
4359 * the close from the server. The response will be
4360 * switched in TUNNEL mode until the end.
Christopher Fauletf2824e62018-10-01 12:12:37 +02004361 */
4362 if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) &&
4363 txn->rsp.msg_state != HTTP_MSG_CLOSED)
Christopher Faulet9768c262018-10-22 09:34:31 +02004364 goto check_channel_flags;
Christopher Fauletf2824e62018-10-01 12:12:37 +02004365
4366 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4367 channel_shutr_now(chn);
4368 channel_shutw_now(chn);
4369 }
4370 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004371 goto check_channel_flags;
4372 }
4373
4374 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4375 http_msg_closing:
4376 /* nothing else to forward, just waiting for the output buffer
4377 * to be empty and for the shutw_now to take effect.
4378 */
4379 if (channel_is_empty(chn)) {
4380 txn->req.msg_state = HTTP_MSG_CLOSED;
4381 goto http_msg_closed;
4382 }
4383 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004384 txn->req.msg_state = HTTP_MSG_ERROR;
4385 goto end;
4386 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004387 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004388 return;
4389 }
4390
4391 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4392 http_msg_closed:
Christopher Fauletf2824e62018-10-01 12:12:37 +02004393 /* if we don't know whether the server will close, we need to hard close */
4394 if (txn->rsp.flags & HTTP_MSGF_XFER_LEN)
4395 s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Christopher Fauletf2824e62018-10-01 12:12:37 +02004396 /* see above in MSG_DONE why we only do this in these states */
Christopher Faulet769d0e92019-03-22 14:23:18 +01004397 if (!(s->be->options & PR_O_ABRT_CLOSE))
Christopher Fauletf2824e62018-10-01 12:12:37 +02004398 channel_dont_read(chn);
4399 goto end;
4400 }
4401
4402 check_channel_flags:
4403 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4404 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4405 /* if we've just closed an output, let's switch */
4406 txn->req.msg_state = HTTP_MSG_CLOSING;
4407 goto http_msg_closing;
4408 }
4409
4410 end:
4411 chn->analysers &= AN_REQ_FLT_END;
4412 if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s))
4413 chn->analysers |= AN_REQ_FLT_XFER_DATA;
4414 channel_auto_close(chn);
4415 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004416 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004417}
4418
4419
4420/* This function terminates the response because it was completly analyzed or
4421 * because an error was triggered during the body forwarding.
4422 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004423static void http_end_response(struct stream *s)
Christopher Fauletf2824e62018-10-01 12:12:37 +02004424{
4425 struct channel *chn = &s->res;
4426 struct http_txn *txn = s->txn;
4427
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004428 DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004429
Christopher Fauletb42a8b62018-11-19 21:59:00 +01004430 if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR ||
4431 txn->rsp.msg_state == HTTP_MSG_ERROR)) {
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004432 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004433 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004434 goto end;
4435 }
4436
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004437 if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) {
4438 DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004439 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004440 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004441
4442 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4443 /* In theory, we don't need to read anymore, but we must
4444 * still monitor the server connection for a possible close
4445 * while the request is being uploaded, so we don't disable
4446 * reading.
4447 */
4448 /* channel_dont_read(chn); */
4449
4450 if (txn->req.msg_state < HTTP_MSG_DONE) {
4451 /* The client seems to still be sending data, probably
4452 * because we got an error response during an upload.
4453 * We have the choice of either breaking the connection
4454 * or letting it pass through. Let's do the later.
4455 */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004456 DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004457 return;
4458 }
4459
4460 /* When we get here, it means that both the request and the
4461 * response have finished receiving. Depending on the connection
4462 * mode, we'll have to wait for the last bytes to leave in either
4463 * direction, and sometimes for a close to be effective.
4464 */
Christopher Fauletc41547b2019-07-16 14:32:23 +02004465 if (txn->flags & TX_CON_WANT_TUN) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004466 channel_auto_read(chn);
4467 chn->flags |= CF_NEVER_WAIT;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004468 if (b_data(&chn->buf)) {
4469 DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn);
Christopher Faulet9768c262018-10-22 09:34:31 +02004470 return;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004471 }
Christopher Fauletf2824e62018-10-01 12:12:37 +02004472 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4473 }
4474 else {
4475 /* we're not expecting any new data to come for this
4476 * transaction, so we can close it.
4477 */
4478 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4479 channel_shutr_now(chn);
4480 channel_shutw_now(chn);
4481 }
4482 }
4483 goto check_channel_flags;
4484 }
4485
4486 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4487 http_msg_closing:
4488 /* nothing else to forward, just waiting for the output buffer
4489 * to be empty and for the shutw_now to take effect.
4490 */
4491 if (channel_is_empty(chn)) {
4492 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4493 goto http_msg_closed;
4494 }
4495 else if (chn->flags & CF_SHUTW) {
Christopher Fauletf2824e62018-10-01 12:12:37 +02004496 txn->rsp.msg_state = HTTP_MSG_ERROR;
Christopher Fauletcff0f732019-12-16 16:13:44 +01004497 _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1);
Olivier Houcharda798bf52019-03-08 18:52:00 +01004498 _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +01004499 if (strm_sess(s)->listener->counters)
4500 _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004501 if (objt_server(s->target))
Christopher Fauletcff0f732019-12-16 16:13:44 +01004502 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004503 goto end;
4504 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004505 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004506 return;
4507 }
4508
4509 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4510 http_msg_closed:
4511 /* drop any pending data */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004512 channel_htx_truncate(&s->req, htxbuf(&s->req.buf));
Christopher Faulet9768c262018-10-22 09:34:31 +02004513 channel_abort(&s->req);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004514 goto end;
4515 }
4516
4517 check_channel_flags:
4518 /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */
4519 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
4520 /* if we've just closed an output, let's switch */
4521 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4522 goto http_msg_closing;
4523 }
4524
4525 end:
4526 chn->analysers &= AN_RES_FLT_END;
4527 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s))
4528 chn->analysers |= AN_RES_FLT_XFER_DATA;
4529 channel_auto_close(chn);
4530 channel_auto_read(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01004531 DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn);
Christopher Fauletf2824e62018-10-01 12:12:37 +02004532}
4533
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004534void http_server_error(struct stream *s, struct stream_interface *si, int err,
4535 int finst, const struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004536{
4537 channel_auto_read(si_oc(si));
4538 channel_abort(si_oc(si));
4539 channel_auto_close(si_oc(si));
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004540 channel_htx_erase(si_oc(si), htxbuf(&(si_oc(si))->buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02004541 channel_auto_close(si_ic(si));
4542 channel_auto_read(si_ic(si));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004543
4544 /* <msg> is an HTX structure. So we copy it in the response's
4545 * channel */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004546 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004547 struct channel *chn = si_ic(si);
4548 struct htx *htx;
4549
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004550 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004551 chn->buf.data = msg->data;
4552 memcpy(chn->buf.area, msg->area, msg->data);
4553 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02004554 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02004555 c_adv(chn, htx->data);
4556 chn->total += htx->data;
4557 }
4558 if (!(s->flags & SF_ERR_MASK))
4559 s->flags |= err;
4560 if (!(s->flags & SF_FINST_MASK))
4561 s->flags |= finst;
4562}
4563
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004564void http_reply_and_close(struct stream *s, short status, struct buffer *msg)
Christopher Faulet0f226952018-10-22 09:29:56 +02004565{
4566 channel_auto_read(&s->req);
4567 channel_abort(&s->req);
4568 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004569 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
4570 channel_htx_truncate(&s->res, htxbuf(&s->res.buf));
Christopher Faulet0f226952018-10-22 09:29:56 +02004571
4572 s->txn->flags &= ~TX_WAIT_NEXT_RQ;
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004573
4574 /* <msg> is an HTX structure. So we copy it in the response's
4575 * channel */
4576 /* FIXME: It is a problem for now if there is some outgoing data */
Christopher Faulet9f5839c2019-07-22 16:41:43 +02004577 if (msg && !b_is_null(msg)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004578 struct channel *chn = &s->res;
4579 struct htx *htx;
4580
Christopher Fauletaed82cf2018-11-30 22:22:32 +01004581 FLT_STRM_CB(s, flt_http_reply(s, s->txn->status, msg));
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004582 chn->buf.data = msg->data;
4583 memcpy(chn->buf.area, msg->area, msg->data);
4584 htx = htx_from_buf(&chn->buf);
Christopher Faulet06511812019-10-16 09:38:27 +02004585 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet0f226952018-10-22 09:29:56 +02004586 c_adv(chn, htx->data);
4587 chn->total += htx->data;
4588 }
4589
4590 s->res.wex = tick_add_ifset(now_ms, s->res.wto);
4591 channel_auto_read(&s->res);
4592 channel_auto_close(&s->res);
4593 channel_shutr_now(&s->res);
4594}
4595
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004596struct buffer *http_error_message(struct stream *s)
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004597{
4598 const int msgnum = http_get_status_idx(s->txn->status);
4599
Christopher Faulet58857752020-01-15 15:19:50 +01004600 if (s->be->errmsg[msgnum])
4601 return s->be->errmsg[msgnum];
4602 else if (strm_fe(s)->errmsg[msgnum])
4603 return strm_fe(s)->errmsg[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004604 else
Christopher Fauletf7346382019-07-17 22:02:08 +02004605 return &http_err_chunks[msgnum];
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004606}
4607
Christopher Faulet304cc402019-07-15 15:46:28 +02004608/* Return the error message corresponding to si->err_type. It is assumed
4609 * that the server side is closed. Note that err_type is actually a
4610 * bitmask, where almost only aborts may be cumulated with other
4611 * values. We consider that aborted operations are more important
4612 * than timeouts or errors due to the fact that nobody else in the
4613 * logs might explain incomplete retries. All others should avoid
4614 * being cumulated. It should normally not be possible to have multiple
4615 * aborts at once, but just in case, the first one in sequence is reported.
4616 * Note that connection errors appearing on the second request of a keep-alive
4617 * connection are not reported since this allows the client to retry.
4618 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004619void http_return_srv_error(struct stream *s, struct stream_interface *si)
Christopher Faulet304cc402019-07-15 15:46:28 +02004620{
4621 int err_type = si->err_type;
4622
4623 /* set s->txn->status for http_error_message(s) */
4624 s->txn->status = 503;
4625
4626 if (err_type & SI_ET_QUEUE_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004627 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q,
4628 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004629 else if (err_type & SI_ET_CONN_ABRT)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004630 http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C,
4631 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4632 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004633 else if (err_type & SI_ET_QUEUE_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004634 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q,
4635 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004636 else if (err_type & SI_ET_QUEUE_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004637 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q,
4638 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004639 else if (err_type & SI_ET_CONN_TO)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004640 http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C,
4641 (s->txn->flags & TX_NOT_FIRST) ? NULL :
4642 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004643 else if (err_type & SI_ET_CONN_ERR)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004644 http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C,
4645 (s->flags & SF_SRV_REUSED) ? NULL :
4646 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004647 else if (err_type & SI_ET_CONN_RES)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004648 http_server_error(s, si, SF_ERR_RESOURCE, 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 { /* SI_ET_CONN_OTHER and others */
4652 s->txn->status = 500;
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004653 http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C,
4654 http_error_message(s));
Christopher Faulet304cc402019-07-15 15:46:28 +02004655 }
4656}
4657
Christopher Fauleta7b677c2018-11-29 16:48:49 +01004658
Christopher Faulet4a28a532019-03-01 11:19:40 +01004659/* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0
4660 * on success and -1 on error.
4661 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004662static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004663{
4664 /* If we have HTTP/1.1 message with a body and Expect: 100-continue,
4665 * then we must send an HTTP/1.1 100 Continue intermediate response.
4666 */
4667 if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) &&
4668 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
4669 struct ist hdr = { .ptr = "Expect", .len = 6 };
4670 struct http_hdr_ctx ctx;
4671
4672 ctx.blk = NULL;
4673 /* Expect is allowed in 1.1, look for it */
4674 if (http_find_header(htx, hdr, &ctx, 0) &&
4675 unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) {
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004676 if (http_reply_100_continue(s) == -1)
Christopher Faulet4a28a532019-03-01 11:19:40 +01004677 return -1;
4678 http_remove_header(htx, &ctx);
4679 }
4680 }
4681 return 0;
4682}
4683
Christopher Faulet23a3c792018-11-28 10:01:23 +01004684/* Send a 100-Continue response to the client. It returns 0 on success and -1
4685 * on error. The response channel is updated accordingly.
4686 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004687static int http_reply_100_continue(struct stream *s)
Christopher Faulet23a3c792018-11-28 10:01:23 +01004688{
4689 struct channel *res = &s->res;
4690 struct htx *htx = htx_from_buf(&res->buf);
4691 struct htx_sl *sl;
4692 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|
4693 HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS);
4694 size_t data;
4695
4696 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4697 ist("HTTP/1.1"), ist("100"), ist("Continue"));
4698 if (!sl)
4699 goto fail;
4700 sl->info.res.status = 100;
4701
Christopher Faulet1d5ec092019-06-26 14:23:54 +02004702 if (!htx_add_endof(htx, HTX_BLK_EOH))
Christopher Faulet23a3c792018-11-28 10:01:23 +01004703 goto fail;
4704
4705 data = htx->data - co_data(res);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004706 c_adv(res, data);
4707 res->total += data;
4708 return 0;
4709
4710 fail:
4711 /* If an error occurred, remove the incomplete HTTP response from the
4712 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004713 channel_htx_truncate(res, htx);
Christopher Faulet23a3c792018-11-28 10:01:23 +01004714 return -1;
4715}
4716
Christopher Faulet12c51e22018-11-28 15:59:42 +01004717
4718/* Send a 401-Unauthorized or 407-Unauthorized response to the client, depending
4719 * ont whether we use a proxy or not. It returns 0 on success and -1 on
4720 * error. The response channel is updated accordingly.
4721 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004722static int http_reply_40x_unauthorized(struct stream *s, const char *auth_realm)
Christopher Faulet12c51e22018-11-28 15:59:42 +01004723{
4724 struct channel *res = &s->res;
4725 struct htx *htx = htx_from_buf(&res->buf);
4726 struct htx_sl *sl;
4727 struct ist code, body;
4728 int status;
4729 unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4730 size_t data;
4731
4732 if (!(s->txn->flags & TX_USE_PX_CONN)) {
4733 status = 401;
4734 code = ist("401");
4735 body = ist("<html><body><h1>401 Unauthorized</h1>\n"
4736 "You need a valid user and password to access this content.\n"
4737 "</body></html>\n");
4738 }
4739 else {
4740 status = 407;
4741 code = ist("407");
4742 body = ist("<html><body><h1>407 Unauthorized</h1>\n"
4743 "You need a valid user and password to access this content.\n"
4744 "</body></html>\n");
4745 }
4746
4747 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags,
4748 ist("HTTP/1.1"), code, ist("Unauthorized"));
4749 if (!sl)
4750 goto fail;
4751 sl->info.res.status = status;
4752 s->txn->status = status;
4753
4754 if (chunk_printf(&trash, "Basic realm=\"%s\"", auth_realm) == -1)
4755 goto fail;
4756
Willy Tarreaub5ba2b02019-06-11 16:08:25 +02004757 if (!htx_add_header(htx, ist("Content-length"), ist("112")) ||
4758 !htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Faulet12c51e22018-11-28 15:59:42 +01004759 !htx_add_header(htx, ist("Connection"), ist("close")) ||
Jérôme Magnin86cef232018-12-28 14:49:08 +01004760 !htx_add_header(htx, ist("Content-Type"), ist("text/html")))
4761 goto fail;
4762 if (status == 401 && !htx_add_header(htx, ist("WWW-Authenticate"), ist2(trash.area, trash.data)))
4763 goto fail;
4764 if (status == 407 && !htx_add_header(htx, ist("Proxy-Authenticate"), ist2(trash.area, trash.data)))
Christopher Faulet12c51e22018-11-28 15:59:42 +01004765 goto fail;
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004766 if (!htx_add_endof(htx, HTX_BLK_EOH))
4767 goto fail;
4768
4769 while (body.len) {
4770 size_t sent = htx_add_data(htx, body);
4771 if (!sent)
4772 goto fail;
4773 body.ptr += sent;
4774 body.len -= sent;
4775 }
4776
4777 if (!htx_add_endof(htx, HTX_BLK_EOM))
Christopher Faulet12c51e22018-11-28 15:59:42 +01004778 goto fail;
4779
Christopher Faulet06511812019-10-16 09:38:27 +02004780 htx->flags |= HTX_FL_PROXY_RESP;
Christopher Faulet12c51e22018-11-28 15:59:42 +01004781 data = htx->data - co_data(res);
Christopher Faulet12c51e22018-11-28 15:59:42 +01004782 c_adv(res, data);
4783 res->total += data;
4784
4785 channel_auto_read(&s->req);
4786 channel_abort(&s->req);
4787 channel_auto_close(&s->req);
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004788 channel_htx_erase(&s->req, htxbuf(&s->req.buf));
Christopher Faulet12c51e22018-11-28 15:59:42 +01004789
4790 res->wex = tick_add_ifset(now_ms, res->wto);
4791 channel_auto_read(res);
4792 channel_auto_close(res);
4793 channel_shutr_now(res);
4794 return 0;
4795
4796 fail:
4797 /* If an error occurred, remove the incomplete HTTP response from the
4798 * buffer */
Christopher Faulet202c6ce2019-01-07 14:57:35 +01004799 channel_htx_truncate(res, htx);
Christopher Faulet12c51e22018-11-28 15:59:42 +01004800 return -1;
4801}
4802
Christopher Faulet0f226952018-10-22 09:29:56 +02004803/*
4804 * Capture headers from message <htx> according to header list <cap_hdr>, and
4805 * fill the <cap> pointers appropriately.
4806 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004807static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr)
Christopher Faulet0f226952018-10-22 09:29:56 +02004808{
4809 struct cap_hdr *h;
4810 int32_t pos;
4811
Christopher Fauleta3f15502019-05-13 15:27:23 +02004812 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet0f226952018-10-22 09:29:56 +02004813 struct htx_blk *blk = htx_get_blk(htx, pos);
4814 enum htx_blk_type type = htx_get_blk_type(blk);
4815 struct ist n, v;
4816
4817 if (type == HTX_BLK_EOH)
4818 break;
4819 if (type != HTX_BLK_HDR)
4820 continue;
4821
4822 n = htx_get_blk_name(htx, blk);
4823
4824 for (h = cap_hdr; h; h = h->next) {
4825 if (h->namelen && (h->namelen == n.len) &&
4826 (strncasecmp(n.ptr, h->name, h->namelen) == 0)) {
4827 if (cap[h->index] == NULL)
4828 cap[h->index] =
4829 pool_alloc(h->pool);
4830
4831 if (cap[h->index] == NULL) {
4832 ha_alert("HTTP capture : out of memory.\n");
4833 break;
4834 }
4835
4836 v = htx_get_blk_value(htx, blk);
4837 if (v.len > h->len)
4838 v.len = h->len;
4839
4840 memcpy(cap[h->index], v.ptr, v.len);
4841 cap[h->index][v.len]=0;
4842 }
4843 }
4844 }
4845}
4846
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004847/* Delete a value in a header between delimiters <from> and <next>. The header
4848 * itself is delimited by <start> and <end> pointers. The number of characters
4849 * displaced is returned, and the pointer to the first delimiter is updated if
4850 * required. The function tries as much as possible to respect the following
4851 * principles :
4852 * - replace <from> delimiter by the <next> one unless <from> points to <start>,
4853 * in which case <next> is simply removed
4854 * - set exactly one space character after the new first delimiter, unless there
4855 * are not enough characters in the block being moved to do so.
4856 * - remove unneeded spaces before the previous delimiter and after the new
4857 * one.
4858 *
4859 * It is the caller's responsibility to ensure that :
4860 * - <from> points to a valid delimiter or <start> ;
4861 * - <next> points to a valid delimiter or <end> ;
4862 * - there are non-space chars before <from>.
4863 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004864static int http_del_hdr_value(char *start, char *end, char **from, char *next)
Christopher Faulet0b6bdc52018-10-24 11:05:36 +02004865{
4866 char *prev = *from;
4867
4868 if (prev == start) {
4869 /* We're removing the first value. eat the semicolon, if <next>
4870 * is lower than <end> */
4871 if (next < end)
4872 next++;
4873
4874 while (next < end && HTTP_IS_SPHT(*next))
4875 next++;
4876 }
4877 else {
4878 /* Remove useless spaces before the old delimiter. */
4879 while (HTTP_IS_SPHT(*(prev-1)))
4880 prev--;
4881 *from = prev;
4882
4883 /* copy the delimiter and if possible a space if we're
4884 * not at the end of the line.
4885 */
4886 if (next < end) {
4887 *prev++ = *next++;
4888 if (prev + 1 < next)
4889 *prev++ = ' ';
4890 while (next < end && HTTP_IS_SPHT(*next))
4891 next++;
4892 }
4893 }
4894 memmove(prev, next, end - next);
4895 return (prev - next);
4896}
4897
Christopher Faulet0f226952018-10-22 09:29:56 +02004898
4899/* Formats the start line of the request (without CRLF) and puts it in <str> and
Joseph Herlantc42c0e92018-11-25 10:43:27 -08004900 * return the written length. The line can be truncated if it exceeds <len>.
Christopher Faulet0f226952018-10-22 09:29:56 +02004901 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004902static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len)
Christopher Faulet0f226952018-10-22 09:29:56 +02004903{
4904 struct ist dst = ist2(str, 0);
4905
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004906 if (istcat(&dst, htx_sl_req_meth(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004907 goto end;
4908 if (dst.len + 1 > len)
4909 goto end;
4910 dst.ptr[dst.len++] = ' ';
4911
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004912 if (istcat(&dst, htx_sl_req_uri(sl), len) == -1)
Christopher Faulet0f226952018-10-22 09:29:56 +02004913 goto end;
4914 if (dst.len + 1 > len)
4915 goto end;
4916 dst.ptr[dst.len++] = ' ';
4917
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004918 istcat(&dst, htx_sl_req_vsn(sl), len);
Christopher Faulet0f226952018-10-22 09:29:56 +02004919 end:
4920 return dst.len;
4921}
4922
4923/*
4924 * Print a debug line with a start line.
4925 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004926static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl)
Christopher Faulet0f226952018-10-22 09:29:56 +02004927{
4928 struct session *sess = strm_sess(s);
4929 int max;
4930
4931 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4932 dir,
4933 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4934 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4935
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004936 max = HTX_SL_P1_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004937 UBOUND(max, trash.size - trash.data - 3);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004938 chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004939 trash.area[trash.data++] = ' ';
4940
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004941 max = HTX_SL_P2_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004942 UBOUND(max, trash.size - trash.data - 2);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004943 chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004944 trash.area[trash.data++] = ' ';
4945
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004946 max = HTX_SL_P3_LEN(sl);
Christopher Faulet0f226952018-10-22 09:29:56 +02004947 UBOUND(max, trash.size - trash.data - 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01004948 chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max);
Christopher Faulet0f226952018-10-22 09:29:56 +02004949 trash.area[trash.data++] = '\n';
4950
4951 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
4952}
4953
4954/*
4955 * Print a debug line with a header.
4956 */
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02004957static 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 +02004958{
4959 struct session *sess = strm_sess(s);
4960 int max;
4961
4962 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
4963 dir,
4964 objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1,
4965 objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1);
4966
4967 max = n.len;
4968 UBOUND(max, trash.size - trash.data - 3);
4969 chunk_memcat(&trash, n.ptr, max);
4970 trash.area[trash.data++] = ':';
4971 trash.area[trash.data++] = ' ';
4972
4973 max = v.len;
4974 UBOUND(max, trash.size - trash.data - 1);
4975 chunk_memcat(&trash, v.ptr, max);
4976 trash.area[trash.data++] = '\n';
4977
4978 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
4979}
4980
Christopher Fauleta8a46e22019-07-16 14:53:09 +02004981/* Allocate a new HTTP transaction for stream <s> unless there is one already.
4982 * In case of allocation failure, everything allocated is freed and NULL is
4983 * returned. Otherwise the new transaction is assigned to the stream and
4984 * returned.
4985 */
4986struct http_txn *http_alloc_txn(struct stream *s)
4987{
4988 struct http_txn *txn = s->txn;
4989
4990 if (txn)
4991 return txn;
4992
4993 txn = pool_alloc(pool_head_http_txn);
4994 if (!txn)
4995 return txn;
4996
4997 s->txn = txn;
4998 return txn;
4999}
5000
5001void http_txn_reset_req(struct http_txn *txn)
5002{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005003 txn->req.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005004 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
5005}
5006
5007void http_txn_reset_res(struct http_txn *txn)
5008{
Christopher Faulet1aea50e2020-01-17 16:03:53 +01005009 txn->rsp.flags = 0;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005010 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
5011}
5012
5013/*
5014 * Initialize a new HTTP transaction for stream <s>. It is assumed that all
5015 * the required fields are properly allocated and that we only need to (re)init
5016 * them. This should be used before processing any new request.
5017 */
5018void http_init_txn(struct stream *s)
5019{
5020 struct http_txn *txn = s->txn;
5021 struct conn_stream *cs = objt_cs(s->si[0].end);
5022
5023 txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST)
5024 ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ)
5025 : 0);
5026 txn->status = -1;
5027 *(unsigned int *)txn->cache_hash = 0;
5028
5029 txn->cookie_first_date = 0;
5030 txn->cookie_last_date = 0;
5031
5032 txn->srv_cookie = NULL;
5033 txn->cli_cookie = NULL;
5034 txn->uri = NULL;
5035
5036 http_txn_reset_req(txn);
5037 http_txn_reset_res(txn);
5038
5039 txn->req.chn = &s->req;
5040 txn->rsp.chn = &s->res;
5041
5042 txn->auth.method = HTTP_AUTH_UNKNOWN;
5043
5044 vars_init(&s->vars_txn, SCOPE_TXN);
5045 vars_init(&s->vars_reqres, SCOPE_REQ);
5046}
5047
5048/* to be used at the end of a transaction */
5049void http_end_txn(struct stream *s)
5050{
5051 struct http_txn *txn = s->txn;
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005052
5053 /* these ones will have been dynamically allocated */
5054 pool_free(pool_head_requri, txn->uri);
5055 pool_free(pool_head_capture, txn->cli_cookie);
5056 pool_free(pool_head_capture, txn->srv_cookie);
5057 pool_free(pool_head_uniqueid, s->unique_id);
5058
5059 s->unique_id = NULL;
5060 txn->uri = NULL;
5061 txn->srv_cookie = NULL;
5062 txn->cli_cookie = NULL;
5063
Christopher Faulet59399252019-11-07 14:27:52 +01005064 if (!LIST_ISEMPTY(&s->vars_txn.head))
5065 vars_prune(&s->vars_txn, s->sess, s);
5066 if (!LIST_ISEMPTY(&s->vars_reqres.head))
5067 vars_prune(&s->vars_reqres, s->sess, s);
5068}
5069
Christopher Fauleta8a46e22019-07-16 14:53:09 +02005070
5071DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn));
5072DECLARE_POOL(pool_head_uniqueid, "uniqueid", UNIQUEID_LEN);
Christopher Faulet0f226952018-10-22 09:29:56 +02005073
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005074__attribute__((constructor))
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005075static void __http_protocol_init(void)
Christopher Fauletf4eb75d2018-10-11 15:55:07 +02005076{
5077}
5078
5079
5080/*
5081 * Local variables:
5082 * c-indent-level: 8
5083 * c-basic-offset: 8
5084 * End:
5085 */