Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
| 4 | * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Willy Tarreau | dcc048a | 2020-06-04 19:11:43 +0200 | [diff] [blame] | 13 | #include <haproxy/acl.h> |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 14 | #include <haproxy/action-t.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 15 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 16 | #include <haproxy/backend.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 17 | #include <haproxy/base64.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 18 | #include <haproxy/capture-t.h> |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 19 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 20 | #include <haproxy/channel.h> |
Willy Tarreau | 4aa573d | 2020-06-04 18:21:56 +0200 | [diff] [blame] | 21 | #include <haproxy/check.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 22 | #include <haproxy/connection.h> |
Christopher Faulet | 8da67aa | 2022-03-29 17:53:09 +0200 | [diff] [blame] | 23 | #include <haproxy/conn_stream.h> |
| 24 | #include <haproxy/cs_utils.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 25 | #include <haproxy/errors.h> |
Willy Tarreau | c7babd8 | 2020-06-04 21:29:29 +0200 | [diff] [blame] | 26 | #include <haproxy/filters.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 27 | #include <haproxy/http.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 28 | #include <haproxy/http_ana.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 29 | #include <haproxy/http_htx.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 30 | #include <haproxy/htx.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 31 | #include <haproxy/log.h> |
Willy Tarreau | 6131d6a | 2020-06-02 16:48:09 +0200 | [diff] [blame] | 32 | #include <haproxy/net_helper.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 33 | #include <haproxy/proxy.h> |
Willy Tarreau | 7cd8b6e | 2020-06-02 17:32:26 +0200 | [diff] [blame] | 34 | #include <haproxy/regex.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 35 | #include <haproxy/server-t.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 36 | #include <haproxy/stats.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 37 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 38 | #include <haproxy/stream_interface.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 39 | #include <haproxy/trace.h> |
Willy Tarreau | 8c42b8a | 2020-06-04 19:27:34 +0200 | [diff] [blame] | 40 | #include <haproxy/uri_auth-t.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 41 | #include <haproxy/vars.h> |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 42 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 43 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 44 | #define TRACE_SOURCE &trace_strm |
| 45 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 46 | extern const char *stat_status_codes[]; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 47 | |
Willy Tarreau | ff88270 | 2021-04-10 17:23:00 +0200 | [diff] [blame] | 48 | struct pool_head *pool_head_requri __read_mostly = NULL; |
| 49 | struct pool_head *pool_head_capture __read_mostly = NULL; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 50 | |
| 51 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 52 | static void http_end_request(struct stream *s); |
| 53 | static void http_end_response(struct stream *s); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 54 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 55 | static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr); |
| 56 | static int http_del_hdr_value(char *start, char *end, char **from, char *next); |
| 57 | static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len); |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 58 | static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl); |
| 59 | static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 60 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 61 | static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s); |
| 62 | static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 63 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 64 | static void http_manage_client_side_cookies(struct stream *s, struct channel *req); |
| 65 | static void http_manage_server_side_cookies(struct stream *s, struct channel *res); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 66 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 67 | static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend); |
| 68 | static int http_handle_stats(struct stream *s, struct channel *req); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 69 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 70 | static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg); |
| 71 | static int http_reply_100_continue(struct stream *s); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 72 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 73 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 74 | * processing can continue on next analysers, or zero if it either needs more |
| 75 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 76 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers |
| 77 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 78 | * abort. |
| 79 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 80 | int http_wait_for_request(struct stream *s, struct channel *req, int an_bit) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 81 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 82 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 83 | /* |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 84 | * We will analyze a complete HTTP request to check the its syntax. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 85 | * |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 86 | * Once the start line and all headers are received, we may perform a |
| 87 | * capture of the error (if any), and we will set a few fields. We also |
| 88 | * check for monitor-uri, logging and finally headers capture. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 89 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 90 | struct session *sess = s->sess; |
| 91 | struct http_txn *txn = s->txn; |
| 92 | struct http_msg *msg = &txn->req; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 93 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 94 | struct htx_sl *sl; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 95 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 96 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 97 | |
Christopher Faulet | da46a0d | 2021-01-21 17:32:58 +0100 | [diff] [blame] | 98 | if (unlikely(!IS_HTX_STRM(s))) { |
| 99 | /* It is only possible when a TCP stream is upgrade to HTTP. |
| 100 | * There is a transition period during which there is no |
| 101 | * data. The stream is still in raw mode and SF_IGNORE flag is |
| 102 | * still set. When this happens, the new mux is responsible to |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 103 | * handle all errors. Thus we may leave immediately. |
Christopher Faulet | da46a0d | 2021-01-21 17:32:58 +0100 | [diff] [blame] | 104 | */ |
| 105 | BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 106 | |
Christopher Faulet | 97b3a61 | 2021-03-15 17:10:12 +0100 | [diff] [blame] | 107 | /* Don't connect for now */ |
| 108 | channel_dont_connect(req); |
| 109 | |
| 110 | /* A SHUTR at this stage means we are performing a "destructive" |
| 111 | * HTTP upgrade (TCP>H2). In this case, we can leave. |
| 112 | */ |
| 113 | if (req->flags & CF_SHUTR) { |
| 114 | s->logs.logwait = 0; |
| 115 | s->logs.level = 0; |
| 116 | channel_abort(&s->req); |
| 117 | channel_abort(&s->res); |
| 118 | req->analysers &= AN_REQ_FLT_END; |
| 119 | req->analyse_exp = TICK_ETERNITY; |
| 120 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s); |
| 121 | return 1; |
| 122 | } |
Christopher Faulet | da46a0d | 2021-01-21 17:32:58 +0100 | [diff] [blame] | 123 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s); |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | htx = htxbuf(&req->buf); |
Christopher Faulet | 8bebd2f | 2020-10-06 17:54:56 +0200 | [diff] [blame] | 128 | |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 129 | /* Parsing errors are caught here */ |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 130 | if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) { |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 131 | stream_inc_http_req_ctr(s); |
Emeric Brun | 2897644 | 2020-10-07 08:50:09 +0200 | [diff] [blame] | 132 | proxy_inc_fe_req_ctr(sess->listener, sess->fe); |
Christopher Faulet | bf7175f | 2021-02-10 14:58:01 +0100 | [diff] [blame] | 133 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
| 134 | stream_inc_http_err_ctr(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 135 | goto return_bad_req; |
Christopher Faulet | bf7175f | 2021-02-10 14:58:01 +0100 | [diff] [blame] | 136 | } |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 137 | else |
| 138 | goto return_int_err; |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 141 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 142 | s->srv_error = http_return_srv_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 143 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 144 | msg->msg_state = HTTP_MSG_BODY; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 145 | stream_inc_http_req_ctr(s); |
Emeric Brun | 2897644 | 2020-10-07 08:50:09 +0200 | [diff] [blame] | 146 | proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 147 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 148 | /* kill the pending keep-alive timeout */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 149 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 150 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 151 | BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 152 | sl = http_get_stline(htx); |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 153 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 154 | /* 0: we might have to print this header in debug mode */ |
| 155 | if (unlikely((global.mode & MODE_DEBUG) && |
| 156 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 157 | int32_t pos; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 158 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 159 | http_debug_stline("clireq", s, sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 160 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 161 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 162 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 163 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 164 | |
| 165 | if (type == HTX_BLK_EOH) |
| 166 | break; |
| 167 | if (type != HTX_BLK_HDR) |
| 168 | continue; |
| 169 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 170 | http_debug_hdr("clihdr", s, |
| 171 | htx_get_blk_name(htx, blk), |
| 172 | htx_get_blk_value(htx, blk)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 173 | } |
| 174 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 175 | |
| 176 | /* |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 177 | * 1: identify the method and the version. Also set HTTP flags |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 178 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 179 | txn->meth = sl->info.req.meth; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 180 | if (sl->flags & HTX_SL_F_VER_11) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 181 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 182 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 183 | if (sl->flags & HTX_SL_F_CLEN) |
| 184 | msg->flags |= HTTP_MSGF_CNT_LEN; |
| 185 | else if (sl->flags & HTX_SL_F_CHNK) |
| 186 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Christopher Faulet | b2db4fa | 2018-11-27 16:51:09 +0100 | [diff] [blame] | 187 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 188 | msg->flags |= HTTP_MSGF_BODYLESS; |
Christopher Faulet | 576c358 | 2021-01-08 15:53:01 +0100 | [diff] [blame] | 189 | if (sl->flags & HTX_SL_F_CONN_UPG) |
| 190 | msg->flags |= HTTP_MSGF_CONN_UPG; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 191 | |
| 192 | /* we can make use of server redirect on GET and HEAD */ |
| 193 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 194 | s->flags |= SF_REDIRECTABLE; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 195 | else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 196 | /* PRI is reserved for the HTTP/2 preface */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 197 | goto return_bad_req; |
| 198 | } |
| 199 | |
| 200 | /* |
Christopher Faulet | 6072beb | 2020-02-18 15:34:58 +0100 | [diff] [blame] | 201 | * 2: check if the URI matches the monitor_uri. We have to do this for |
| 202 | * every request which gets in, because the monitor-uri is defined by |
| 203 | * the frontend. If the monitor-uri starts with a '/', the matching is |
| 204 | * done against the request's path. Otherwise, the request's uri is |
| 205 | * used. It is a workaround to let HTTP/2 health-checks work as |
| 206 | * expected. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 207 | */ |
Tim Duesterhus | 4b1fcaa | 2022-03-05 00:52:40 +0100 | [diff] [blame] | 208 | if (unlikely(isttest(sess->fe->monitor_uri))) { |
| 209 | const struct ist monitor_uri = sess->fe->monitor_uri; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 210 | struct http_uri_parser parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 211 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 212 | if ((istptr(monitor_uri)[0] == '/' && |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 213 | isteq(http_parse_path(&parser), monitor_uri)) || |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 214 | isteq(htx_sl_req_uri(sl), monitor_uri)) { |
| 215 | /* |
| 216 | * We have found the monitor URI |
| 217 | */ |
| 218 | struct acl_cond *cond; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 219 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 220 | s->flags |= SF_MONITOR; |
| 221 | _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 222 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 223 | /* Check if we want to fail this monitor request or not */ |
| 224 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
| 225 | int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 226 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 227 | ret = acl_pass(ret); |
| 228 | if (cond->pol == ACL_COND_UNLESS) |
| 229 | ret = !ret; |
| 230 | |
| 231 | if (ret) { |
| 232 | /* we fail this request, let's return 503 service unavail */ |
| 233 | txn->status = 503; |
| 234 | if (!(s->flags & SF_ERR_MASK)) |
| 235 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
| 236 | goto return_prx_cond; |
| 237 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 238 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 239 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 240 | /* nothing to fail, let's reply normally */ |
| 241 | txn->status = 200; |
| 242 | if (!(s->flags & SF_ERR_MASK)) |
| 243 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
| 244 | goto return_prx_cond; |
| 245 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /* |
| 249 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 250 | * Note: we cannot log anymore if the request has been |
| 251 | * classified as invalid. |
| 252 | */ |
| 253 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 254 | /* we have a complete HTTP request that we must log */ |
| 255 | if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 256 | size_t len; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 257 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 258 | len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 259 | txn->uri[len] = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 260 | |
| 261 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
| 262 | s->do_log(s); |
| 263 | } else { |
| 264 | ha_alert("HTTP logging : out of memory.\n"); |
| 265 | } |
| 266 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 267 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 268 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 269 | * we have what looks like a proxied connection instead of a connection, |
| 270 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 271 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 272 | * happen to do that despite being non-standard :-( |
| 273 | * We consider that a request not beginning with either '/' or '*' is |
| 274 | * a proxied connection, which covers both "scheme://location" and |
| 275 | * CONNECT ip:port. |
| 276 | */ |
| 277 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 278 | *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*') |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 279 | txn->flags |= TX_USE_PX_CONN; |
| 280 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 281 | /* 5: we may need to capture headers */ |
| 282 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 283 | http_capture_headers(htx, s->req_cap, sess->fe->req_cap); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 284 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 285 | /* we may have to wait for the request's body */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 286 | if (s->be->options & PR_O_WREQ_BODY) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 287 | req->analysers |= AN_REQ_HTTP_BODY; |
| 288 | |
| 289 | /* |
| 290 | * RFC7234#4: |
| 291 | * A cache MUST write through requests with methods |
| 292 | * that are unsafe (Section 4.2.1 of [RFC7231]) to |
| 293 | * the origin server; i.e., a cache is not allowed |
| 294 | * to generate a reply to such a request before |
| 295 | * having forwarded the request and having received |
| 296 | * a corresponding response. |
| 297 | * |
| 298 | * RFC7231#4.2.1: |
| 299 | * Of the request methods defined by this |
| 300 | * specification, the GET, HEAD, OPTIONS, and TRACE |
| 301 | * methods are defined to be safe. |
| 302 | */ |
| 303 | if (likely(txn->meth == HTTP_METH_GET || |
| 304 | txn->meth == HTTP_METH_HEAD || |
| 305 | txn->meth == HTTP_METH_OPTIONS || |
| 306 | txn->meth == HTTP_METH_TRACE)) |
| 307 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 308 | |
| 309 | /* end of job, return OK */ |
| 310 | req->analysers &= ~an_bit; |
| 311 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 312 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 313 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 314 | return 1; |
| 315 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 316 | return_int_err: |
| 317 | txn->status = 500; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 318 | if (!(s->flags & SF_ERR_MASK)) |
| 319 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 320 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 321 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 322 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 323 | goto return_prx_cond; |
| 324 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 325 | return_bad_req: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 326 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 327 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 328 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 329 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 330 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 331 | |
| 332 | return_prx_cond: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 333 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 334 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 335 | if (!(s->flags & SF_ERR_MASK)) |
| 336 | s->flags |= SF_ERR_PRXCOND; |
| 337 | if (!(s->flags & SF_FINST_MASK)) |
| 338 | s->flags |= SF_FINST_R; |
| 339 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 340 | DBG_TRACE_DEVEL("leaving on error", |
| 341 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | |
| 346 | /* This stream analyser runs all HTTP request processing which is common to |
| 347 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 348 | * reqadd, stats and redirects. This is performed for the designated proxy. |
| 349 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 350 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 351 | * error, ...). |
| 352 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 353 | int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 354 | { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 355 | struct list *def_rules, *rules; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 356 | struct session *sess = s->sess; |
| 357 | struct http_txn *txn = s->txn; |
| 358 | struct http_msg *msg = &txn->req; |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 359 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 360 | struct redirect_rule *rule; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 361 | enum rule_result verdict; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 362 | struct connection *conn = objt_conn(sess->origin); |
| 363 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 364 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 365 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 366 | htx = htxbuf(&req->buf); |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 367 | |
Christopher Faulet | 1907ccc | 2019-04-29 13:12:02 +0200 | [diff] [blame] | 368 | /* just in case we have some per-backend tracking. Only called the first |
| 369 | * execution of the analyser. */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 370 | if (!s->current_rule && !s->current_rule_list) |
Christopher Faulet | 1907ccc | 2019-04-29 13:12:02 +0200 | [diff] [blame] | 371 | stream_inc_be_http_req_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 372 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 373 | def_rules = ((px->defpx && (an_bit == AN_REQ_HTTP_PROCESS_FE || px != sess->fe)) ? &px->defpx->http_req_rules : NULL); |
| 374 | rules = &px->http_req_rules; |
| 375 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 376 | /* evaluate http-request rules */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 377 | if ((def_rules && !LIST_ISEMPTY(def_rules)) || !LIST_ISEMPTY(rules)) { |
| 378 | verdict = http_req_get_intercept_rule(px, def_rules, rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 379 | |
| 380 | switch (verdict) { |
| 381 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 382 | goto return_prx_yield; |
| 383 | |
| 384 | case HTTP_RULE_RES_CONT: |
| 385 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 386 | break; |
| 387 | |
| 388 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 389 | if (txn->flags & TX_CLTARPIT) |
| 390 | goto tarpit; |
| 391 | goto deny; |
| 392 | |
| 393 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 394 | goto return_prx_cond; |
| 395 | |
| 396 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
| 397 | goto done; |
| 398 | |
| 399 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 400 | goto return_bad_req; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 401 | |
| 402 | case HTTP_RULE_RES_ERROR: /* failed with a bad request */ |
| 403 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
| 407 | if (conn && (conn->flags & CO_FL_EARLY_DATA) && |
Olivier Houchard | 220a26c | 2020-01-23 14:57:36 +0100 | [diff] [blame] | 408 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) { |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 409 | struct http_hdr_ctx ctx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 410 | |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 411 | ctx.blk = NULL; |
| 412 | if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) { |
| 413 | if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1")))) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 414 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 415 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* OK at this stage, we know that the request was accepted according to |
| 419 | * the http-request rules, we can check for the stats. Note that the |
| 420 | * URI is detected *before* the req* rules in order not to be affected |
| 421 | * by a possible reqrep, while they are processed *after* so that a |
| 422 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 423 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 424 | if (!s->target && http_stats_check_uri(s, txn, px)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 425 | s->target = &http_stats_applet.obj_type; |
Christopher Faulet | 3704663 | 2022-04-01 11:36:58 +0200 | [diff] [blame] | 426 | if (unlikely(!cs_register_applet(s->csb, objt_applet(s->target)))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 427 | s->logs.tv_request = now; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 428 | if (!(s->flags & SF_ERR_MASK)) |
| 429 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 430 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /* parse the whole stats request and extract the relevant information */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 434 | http_handle_stats(s, req); |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 435 | verdict = http_req_get_intercept_rule(px, NULL, &px->uri_auth->http_req_rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 436 | /* not all actions implemented: deny, allow, auth */ |
| 437 | |
| 438 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 439 | goto deny; |
| 440 | |
| 441 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 442 | goto return_prx_cond; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 443 | |
| 444 | if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */ |
| 445 | goto return_bad_req; |
| 446 | |
| 447 | if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */ |
| 448 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 449 | } |
| 450 | |
Christopher Faulet | 2571bc6 | 2019-03-01 11:44:26 +0100 | [diff] [blame] | 451 | /* Proceed with the applets now. */ |
| 452 | if (unlikely(objt_applet(s->target))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 453 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 454 | _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 455 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 456 | if (http_handle_expect_hdr(s, htx, msg) == -1) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 457 | goto return_int_err; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 458 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 459 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 460 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 461 | if (!(s->flags & SF_FINST_MASK)) |
| 462 | s->flags |= SF_FINST_R; |
| 463 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 464 | if (HAS_FILTERS(s)) |
| 465 | req->analysers |= AN_REQ_FLT_HTTP_HDRS; |
| 466 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 467 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
| 468 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 469 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 470 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 471 | |
| 472 | req->flags |= CF_SEND_DONTWAIT; |
| 473 | s->flags |= SF_ASSIGNED; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 474 | goto done; |
| 475 | } |
| 476 | |
| 477 | /* check whether we have some ACLs set to redirect this request */ |
| 478 | list_for_each_entry(rule, &px->redirect_rules, list) { |
| 479 | if (rule->cond) { |
| 480 | int ret; |
| 481 | |
| 482 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 483 | ret = acl_pass(ret); |
| 484 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 485 | ret = !ret; |
| 486 | if (!ret) |
| 487 | continue; |
| 488 | } |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 489 | if (!http_apply_redirect_rule(rule, s, txn)) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 490 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 491 | goto done; |
| 492 | } |
| 493 | |
| 494 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 495 | * If this happens, then the data will not come immediately, so we must |
| 496 | * send all what we have without waiting. Note that due to the small gain |
| 497 | * in waiting for the body of the request, it's easier to simply put the |
| 498 | * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 499 | * itself once used. |
| 500 | */ |
| 501 | req->flags |= CF_SEND_DONTWAIT; |
| 502 | |
| 503 | done: /* done with this analyser, continue with next ones that the calling |
| 504 | * points will have set, if any. |
| 505 | */ |
| 506 | req->analyse_exp = TICK_ETERNITY; |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 507 | done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 508 | req->analysers &= ~an_bit; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 509 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 510 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 511 | return 1; |
| 512 | |
| 513 | tarpit: |
| 514 | /* Allow cookie logging |
| 515 | */ |
| 516 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 517 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 518 | |
| 519 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 520 | * which may be the same as the connect timeout if unspecified. |
| 521 | * If unset, then set it to zero because we really want it to |
| 522 | * eventually expire. We build the tarpit as an analyser. |
| 523 | */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 524 | channel_htx_erase(&s->req, htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 525 | |
| 526 | /* wipe the request out so that we can drop the connection early |
| 527 | * if the client closes first. |
| 528 | */ |
| 529 | channel_dont_connect(req); |
| 530 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 531 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
| 532 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 533 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 534 | if (!req->analyse_exp) |
| 535 | req->analyse_exp = tick_add(now_ms, 0); |
| 536 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 537 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 538 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 539 | _HA_ATOMIC_INC(&s->be->be_counters.denied_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 540 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 541 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 542 | goto done_without_exp; |
| 543 | |
| 544 | deny: /* this request was blocked (denied) */ |
| 545 | |
| 546 | /* Allow cookie logging |
| 547 | */ |
| 548 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 549 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 550 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 551 | s->logs.tv_request = now; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 552 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 553 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 554 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 555 | _HA_ATOMIC_INC(&s->be->be_counters.denied_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 556 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 557 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 558 | goto return_prx_err; |
| 559 | |
| 560 | return_int_err: |
| 561 | txn->status = 500; |
| 562 | if (!(s->flags & SF_ERR_MASK)) |
| 563 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 564 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 565 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 566 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 567 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 568 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 569 | goto return_prx_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 570 | |
| 571 | return_bad_req: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 572 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 573 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 574 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 575 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 576 | /* fall through */ |
| 577 | |
| 578 | return_prx_err: |
| 579 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 580 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 581 | |
| 582 | return_prx_cond: |
| 583 | if (!(s->flags & SF_ERR_MASK)) |
| 584 | s->flags |= SF_ERR_PRXCOND; |
| 585 | if (!(s->flags & SF_FINST_MASK)) |
| 586 | s->flags |= SF_FINST_R; |
| 587 | |
| 588 | req->analysers &= AN_REQ_FLT_END; |
| 589 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 590 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 591 | DBG_TRACE_DEVEL("leaving on error", |
| 592 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 593 | return 0; |
| 594 | |
| 595 | return_prx_yield: |
| 596 | channel_dont_connect(req); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 597 | DBG_TRACE_DEVEL("waiting for more data", |
| 598 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | /* This function performs all the processing enabled for the current request. |
| 603 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 604 | * needs more data, encounters an error, or wants to immediately abort the |
| 605 | * request. It relies on buffers flags, and updates s->req.analysers. |
| 606 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 607 | int http_process_request(struct stream *s, struct channel *req, int an_bit) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 608 | { |
| 609 | struct session *sess = s->sess; |
| 610 | struct http_txn *txn = s->txn; |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 611 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 612 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
| 613 | |
Christopher Faulet | 8bebd2f | 2020-10-06 17:54:56 +0200 | [diff] [blame] | 614 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 615 | |
| 616 | /* |
| 617 | * Right now, we know that we have processed the entire headers |
| 618 | * and that unwanted requests have been filtered out. We can do |
| 619 | * whatever we want with the remaining request. Also, now we |
| 620 | * may have separate values for ->fe, ->be. |
| 621 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 622 | htx = htxbuf(&req->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 623 | |
| 624 | /* |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 625 | * 7: Now we can work with the cookies. |
| 626 | * Note that doing so might move headers in the request, but |
| 627 | * the fields will stay coherent and the URI will not move. |
| 628 | * This should only be performed in the backend. |
| 629 | */ |
| 630 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 631 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 632 | |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 633 | /* 8: Generate unique ID if a "unique-id-format" is defined. |
| 634 | * |
| 635 | * A unique ID is generated even when it is not sent to ensure that the ID can make use of |
| 636 | * fetches only available in the HTTP request processing stage. |
| 637 | */ |
| 638 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 639 | struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 640 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 641 | if (!isttest(unique_id)) { |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 642 | if (!(s->flags & SF_ERR_MASK)) |
| 643 | s->flags |= SF_ERR_RESOURCE; |
| 644 | goto return_int_err; |
| 645 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 646 | |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 647 | /* send unique ID if a "unique-id-header" is defined */ |
Tim Duesterhus | 0643b0e | 2020-03-05 17:56:35 +0100 | [diff] [blame] | 648 | if (isttest(sess->fe->header_unique_id) && |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 649 | unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id))) |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 650 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | /* |
| 654 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 655 | * asks for it. |
| 656 | */ |
| 657 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Christopher Faulet | 8da67aa | 2022-03-29 17:53:09 +0200 | [diff] [blame] | 658 | const struct sockaddr_storage *src = cs_src(s->csf); |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 659 | struct http_hdr_ctx ctx = { .blk = NULL }; |
Tim Duesterhus | b50ab84 | 2022-03-05 00:52:41 +0100 | [diff] [blame] | 660 | struct ist hdr = isttest(s->be->fwdfor_hdr_name) ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name; |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 661 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 662 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 663 | http_find_header(htx, hdr, &ctx, 0)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 664 | /* The header is set to be added only if none is present |
| 665 | * and we found it, so don't do anything. |
| 666 | */ |
| 667 | } |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 668 | else if (src && src->ss_family == AF_INET) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 669 | /* Add an X-Forwarded-For header unless the source IP is |
| 670 | * in the 'except' network range. |
| 671 | */ |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 672 | if (ipcmp2net(src, &sess->fe->except_xff_net) && |
| 673 | ipcmp2net(src, &s->be->except_xff_net)) { |
| 674 | unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)src)->sin_addr; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 675 | |
| 676 | /* Note: we rely on the backend to get the header name to be used for |
| 677 | * x-forwarded-for, because the header is really meant for the backends. |
| 678 | * However, if the backend did not specify any option, we have to rely |
| 679 | * on the frontend's header name. |
| 680 | */ |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 681 | chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 682 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 683 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 684 | } |
| 685 | } |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 686 | else if (src && src->ss_family == AF_INET6) { |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 687 | /* Add an X-Forwarded-For header unless the source IP is |
| 688 | * in the 'except' network range. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 689 | */ |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 690 | if (ipcmp2net(src, &sess->fe->except_xff_net) && |
| 691 | ipcmp2net(src, &s->be->except_xff_net)) { |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 692 | char pn[INET6_ADDRSTRLEN]; |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 693 | |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 694 | inet_ntop(AF_INET6, |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 695 | (const void *)&((struct sockaddr_in6 *)(src))->sin6_addr, |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 696 | pn, sizeof(pn)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 697 | |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 698 | /* Note: we rely on the backend to get the header name to be used for |
| 699 | * x-forwarded-for, because the header is really meant for the backends. |
| 700 | * However, if the backend did not specify any option, we have to rely |
| 701 | * on the frontend's header name. |
| 702 | */ |
| 703 | chunk_printf(&trash, "%s", pn); |
| 704 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
| 705 | goto return_int_err; |
| 706 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
| 710 | /* |
| 711 | * 10: add X-Original-To if either the frontend or the backend |
| 712 | * asks for it. |
| 713 | */ |
| 714 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Christopher Faulet | 8da67aa | 2022-03-29 17:53:09 +0200 | [diff] [blame] | 715 | const struct sockaddr_storage *dst = cs_dst(s->csf); |
Tim Duesterhus | e502c3e | 2022-03-05 00:52:42 +0100 | [diff] [blame] | 716 | struct ist hdr = isttest(s->be->orgto_hdr_name) ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 717 | |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 718 | if (dst && dst->ss_family == AF_INET) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 719 | /* Add an X-Original-To header unless the destination IP is |
| 720 | * in the 'except' network range. |
| 721 | */ |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 722 | if (ipcmp2net(dst, &sess->fe->except_xot_net) && |
| 723 | ipcmp2net(dst, &s->be->except_xot_net)) { |
| 724 | unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)dst)->sin_addr; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 725 | |
| 726 | /* Note: we rely on the backend to get the header name to be used for |
| 727 | * x-original-to, because the header is really meant for the backends. |
| 728 | * However, if the backend did not specify any option, we have to rely |
| 729 | * on the frontend's header name. |
| 730 | */ |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 731 | chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 732 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 733 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 734 | } |
| 735 | } |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 736 | else if (dst && dst->ss_family == AF_INET6) { |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 737 | /* Add an X-Original-To header unless the source IP is |
| 738 | * in the 'except' network range. |
| 739 | */ |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 740 | if (ipcmp2net(dst, &sess->fe->except_xot_net) && |
| 741 | ipcmp2net(dst, &s->be->except_xot_net)) { |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 742 | char pn[INET6_ADDRSTRLEN]; |
| 743 | |
| 744 | inet_ntop(AF_INET6, |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 745 | (const void *)&((struct sockaddr_in6 *)dst)->sin6_addr, |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 746 | pn, sizeof(pn)); |
| 747 | |
| 748 | /* Note: we rely on the backend to get the header name to be used for |
| 749 | * x-forwarded-for, because the header is really meant for the backends. |
| 750 | * However, if the backend did not specify any option, we have to rely |
| 751 | * on the frontend's header name. |
| 752 | */ |
| 753 | chunk_printf(&trash, "%s", pn); |
| 754 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
| 755 | goto return_int_err; |
| 756 | } |
| 757 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 758 | } |
| 759 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 760 | /* Filter the request headers if there are filters attached to the |
| 761 | * stream. |
| 762 | */ |
| 763 | if (HAS_FILTERS(s)) |
| 764 | req->analysers |= AN_REQ_FLT_HTTP_HDRS; |
| 765 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 766 | /* If we have no server assigned yet and we're balancing on url_param |
| 767 | * with a POST request, we may be interested in checking the body for |
| 768 | * that parameter. This will be done in another analyser. |
| 769 | */ |
| 770 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | 089eaa0 | 2019-01-14 15:17:46 +0100 | [diff] [blame] | 771 | s->txn->meth == HTTP_METH_POST && |
| 772 | (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 773 | channel_dont_connect(req); |
| 774 | req->analysers |= AN_REQ_HTTP_BODY; |
| 775 | } |
| 776 | |
| 777 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 778 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 779 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 780 | /* We expect some data from the client. Unless we know for sure |
| 781 | * we already have a full request, we have to re-enable quick-ack |
| 782 | * in case we previously disabled it, otherwise we might cause |
| 783 | * the client to delay further data. |
| 784 | */ |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 785 | if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM)) |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 786 | conn_set_quickack(cli_conn, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 787 | |
| 788 | /************************************************************* |
| 789 | * OK, that's finished for the headers. We have done what we * |
| 790 | * could. Let's switch to the DATA state. * |
| 791 | ************************************************************/ |
| 792 | req->analyse_exp = TICK_ETERNITY; |
| 793 | req->analysers &= ~an_bit; |
| 794 | |
| 795 | s->logs.tv_request = now; |
| 796 | /* OK let's go on with the BODY now */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 797 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 798 | return 1; |
| 799 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 800 | return_int_err: |
| 801 | txn->status = 500; |
| 802 | if (!(s->flags & SF_ERR_MASK)) |
| 803 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 804 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 805 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 806 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 807 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 808 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 809 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 810 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 811 | |
| 812 | if (!(s->flags & SF_ERR_MASK)) |
| 813 | s->flags |= SF_ERR_PRXCOND; |
| 814 | if (!(s->flags & SF_FINST_MASK)) |
| 815 | s->flags |= SF_FINST_R; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 816 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 817 | DBG_TRACE_DEVEL("leaving on error", |
| 818 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 823 | * returns zero, at the beginning because it prevents any other processing |
| 824 | * from occurring, and at the end because it terminates the request. |
| 825 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 826 | int http_process_tarpit(struct stream *s, struct channel *req, int an_bit) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 827 | { |
| 828 | struct http_txn *txn = s->txn; |
| 829 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 830 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 831 | /* This connection is being tarpitted. The CLIENT side has |
| 832 | * already set the connect expiration date to the right |
| 833 | * timeout. We just have to check that the client is still |
| 834 | * there and that the timeout has not expired. |
| 835 | */ |
| 836 | channel_dont_connect(req); |
| 837 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 838 | !tick_is_expired(req->analyse_exp, now_ms)) { |
Christopher Faulet | b0c87f1 | 2021-10-29 14:37:07 +0200 | [diff] [blame] | 839 | /* Be sure to drain all data from the request channel */ |
| 840 | channel_htx_erase(req, htxbuf(&req->buf)); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 841 | DBG_TRACE_DEVEL("waiting for tarpit timeout expiry", |
| 842 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 843 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 844 | } |
| 845 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 846 | |
| 847 | /* We will set the queue timer to the time spent, just for |
| 848 | * logging purposes. We fake a 500 server error, so that the |
| 849 | * attacker will not suspect his connection has been tarpitted. |
| 850 | * It will not cause trouble to the logs because we can exclude |
| 851 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 852 | */ |
| 853 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 854 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 855 | http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL)); |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 856 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 857 | if (!(s->flags & SF_ERR_MASK)) |
| 858 | s->flags |= SF_ERR_PRXCOND; |
| 859 | if (!(s->flags & SF_FINST_MASK)) |
| 860 | s->flags |= SF_FINST_T; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 861 | |
| 862 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 867 | * for either the buffer to be full, or the full advertised contents to have |
| 868 | * reached the buffer. It must only be called after the standard HTTP request |
| 869 | * processing has occurred, because it expects the request to be parsed and will |
| 870 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 871 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 872 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 873 | * needs to read more data, or 1 once it has completed its analysis. |
| 874 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 875 | int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 876 | { |
| 877 | struct session *sess = s->sess; |
| 878 | struct http_txn *txn = s->txn; |
| 879 | struct http_msg *msg = &s->txn->req; |
| 880 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 881 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg); |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 882 | |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 883 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 884 | switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) { |
| 885 | case HTTP_RULE_RES_CONT: |
| 886 | goto http_end; |
| 887 | case HTTP_RULE_RES_YIELD: |
| 888 | goto missing_data_or_waiting; |
| 889 | case HTTP_RULE_RES_BADREQ: |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 890 | goto return_bad_req; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 891 | case HTTP_RULE_RES_ERROR: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 892 | goto return_int_err; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 893 | case HTTP_RULE_RES_ABRT: |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 894 | goto return_prx_cond; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 895 | default: |
| 896 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | http_end: |
| 900 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 901 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 902 | req->analysers &= ~an_bit; |
| 903 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 904 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 905 | return 1; |
| 906 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 907 | missing_data_or_waiting: |
| 908 | channel_dont_connect(req); |
| 909 | DBG_TRACE_DEVEL("waiting for more data", |
| 910 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 911 | return 0; |
| 912 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 913 | return_int_err: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 914 | txn->status = 500; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 915 | if (!(s->flags & SF_ERR_MASK)) |
| 916 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 917 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 918 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 919 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 920 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 921 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 922 | goto return_prx_err; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 923 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 924 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 925 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 926 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 927 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 928 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 929 | /* fall through */ |
| 930 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 931 | return_prx_err: |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 932 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 933 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 934 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 935 | return_prx_cond: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 936 | if (!(s->flags & SF_ERR_MASK)) |
| 937 | s->flags |= SF_ERR_PRXCOND; |
| 938 | if (!(s->flags & SF_FINST_MASK)) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 939 | s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 940 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 941 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 942 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 943 | DBG_TRACE_DEVEL("leaving on error", |
| 944 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 945 | return 0; |
| 946 | } |
| 947 | |
| 948 | /* This function is an analyser which forwards request body (including chunk |
| 949 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 950 | * zero byte. The only situation where it must not be called is when we're in |
| 951 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 952 | * remaining data and to resync after end of body. It expects the msg_state to |
| 953 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 954 | * read more data, or 1 once we can go on with next request or end the stream. |
| 955 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
| 956 | * bytes of pending data + the headers if not already done. |
| 957 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 958 | int http_request_forward_body(struct stream *s, struct channel *req, int an_bit) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 959 | { |
| 960 | struct session *sess = s->sess; |
| 961 | struct http_txn *txn = s->txn; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 962 | struct http_msg *msg = &txn->req; |
| 963 | struct htx *htx; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 964 | short status = 0; |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 965 | int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 966 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 967 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 968 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 969 | htx = htxbuf(&req->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 970 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 971 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 972 | goto return_bad_req; |
| 973 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 974 | goto return_int_err; |
| 975 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 976 | if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
| 977 | ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) { |
| 978 | /* Output closed while we were sending data. We must abort and |
| 979 | * wake the other side up. |
Christopher Faulet | f506d96 | 2021-04-27 10:56:28 +0200 | [diff] [blame] | 980 | * |
| 981 | * If we have finished to send the request and the response is |
| 982 | * still in progress, don't catch write error on the request |
| 983 | * side if it is in fact a read error on the server side. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 984 | */ |
Christopher Faulet | f506d96 | 2021-04-27 10:56:28 +0200 | [diff] [blame] | 985 | if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers) |
| 986 | return 0; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 987 | |
Olivier Houchard | 29cac3c | 2019-07-12 15:48:58 +0200 | [diff] [blame] | 988 | /* Don't abort yet if we had L7 retries activated and it |
| 989 | * was a write error, we may recover. |
| 990 | */ |
| 991 | if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) && |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 992 | (txn->flags & TX_L7_RETRY)) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 993 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 994 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Olivier Houchard | 29cac3c | 2019-07-12 15:48:58 +0200 | [diff] [blame] | 995 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 996 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 997 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 998 | http_end_request(s); |
| 999 | http_end_response(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1000 | DBG_TRACE_DEVEL("leaving on error", |
| 1001 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1002 | return 1; |
| 1003 | } |
| 1004 | |
| 1005 | /* Note that we don't have to send 100-continue back because we don't |
| 1006 | * need the data to complete our job, and it's up to the server to |
| 1007 | * decide whether to return 100, 417 or anything else in return of |
| 1008 | * an "Expect: 100-continue" header. |
| 1009 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1010 | if (msg->msg_state == HTTP_MSG_BODY) |
| 1011 | msg->msg_state = HTTP_MSG_DATA; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1012 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1013 | /* in most states, we should abort in case of early close */ |
| 1014 | channel_auto_close(req); |
| 1015 | |
| 1016 | if (req->to_forward) { |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 1017 | if (req->to_forward == CHN_INFINITE_FORWARD) { |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1018 | if (req->flags & CF_EOI) |
| 1019 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 1020 | } |
| 1021 | else { |
| 1022 | /* We can't process the buffer's contents yet */ |
| 1023 | req->flags |= CF_WAKE_WRITE; |
| 1024 | goto missing_data_or_waiting; |
| 1025 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1026 | } |
| 1027 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1028 | if (msg->msg_state >= HTTP_MSG_ENDING) |
| 1029 | goto ending; |
| 1030 | |
| 1031 | if (txn->meth == HTTP_METH_CONNECT) { |
| 1032 | msg->msg_state = HTTP_MSG_ENDING; |
| 1033 | goto ending; |
| 1034 | } |
| 1035 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1036 | /* Forward input data. We get it by removing all outgoing data not |
| 1037 | * forwarded yet from HTX data size. If there are some data filters, we |
| 1038 | * let them decide the amount of data to forward. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1039 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1040 | if (HAS_REQ_DATA_FILTERS(s)) { |
| 1041 | ret = flt_http_payload(s, msg, htx->data); |
| 1042 | if (ret < 0) |
| 1043 | goto return_bad_req; |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1044 | c_adv(req, ret); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1045 | } |
| 1046 | else { |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1047 | c_adv(req, htx->data - co_data(req)); |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 1048 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
| 1049 | channel_htx_forward_forever(req, htx); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1050 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1051 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1052 | if (htx->data != co_data(req)) |
| 1053 | goto missing_data_or_waiting; |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1054 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1055 | /* Check if the end-of-message is reached and if so, switch the message |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1056 | * in HTTP_MSG_ENDING state. Then if all data was marked to be |
| 1057 | * forwarded, set the state to HTTP_MSG_DONE. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1058 | */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 1059 | if (!(htx->flags & HTX_FL_EOM)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1060 | goto missing_data_or_waiting; |
| 1061 | |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1062 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1063 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1064 | ending: |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 1065 | req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */ |
| 1066 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1067 | /* other states, ENDING...TUNNEL */ |
| 1068 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 1069 | goto done; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1070 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1071 | if (HAS_REQ_DATA_FILTERS(s)) { |
| 1072 | ret = flt_http_end(s, msg); |
| 1073 | if (ret <= 0) { |
| 1074 | if (!ret) |
| 1075 | goto missing_data_or_waiting; |
| 1076 | goto return_bad_req; |
| 1077 | } |
| 1078 | } |
| 1079 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1080 | if (txn->meth == HTTP_METH_CONNECT) |
| 1081 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 1082 | else { |
| 1083 | msg->msg_state = HTTP_MSG_DONE; |
| 1084 | req->to_forward = 0; |
| 1085 | } |
| 1086 | |
| 1087 | done: |
| 1088 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 1089 | if (!(txn->flags & TX_CON_WANT_TUN)) |
| 1090 | channel_dont_close(req); |
| 1091 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1092 | http_end_request(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1093 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1094 | http_end_response(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1095 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 1096 | if (req->flags & CF_SHUTW) { |
| 1097 | /* request errors are most likely due to the |
| 1098 | * server aborting the transfer. */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1099 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1100 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1101 | goto return_bad_req; |
| 1102 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1103 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1104 | return 1; |
| 1105 | } |
| 1106 | |
| 1107 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 1108 | * the client's connection and forward any shutdown notification to the |
| 1109 | * server, which will decide whether to close or to go on processing the |
| 1110 | * request. We only do that in tunnel mode, and not in other modes since |
| 1111 | * it can be abused to exhaust source ports. */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 1112 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1113 | channel_auto_read(req); |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 1114 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN)) |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1115 | s->csb->flags |= CS_FL_NOLINGER; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1116 | channel_auto_close(req); |
| 1117 | } |
| 1118 | else if (s->txn->meth == HTTP_METH_POST) { |
| 1119 | /* POST requests may require to read extra CRLF sent by broken |
| 1120 | * browsers and which could cause an RST to be sent upon close |
| 1121 | * on some systems (eg: Linux). */ |
| 1122 | channel_auto_read(req); |
| 1123 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1124 | DBG_TRACE_DEVEL("waiting for the end of the HTTP txn", |
| 1125 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1126 | return 0; |
| 1127 | |
| 1128 | missing_data_or_waiting: |
| 1129 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1130 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1131 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1132 | |
| 1133 | waiting: |
| 1134 | /* waiting for the last bits to leave the buffer */ |
| 1135 | if (req->flags & CF_SHUTW) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1136 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1137 | |
| 1138 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 1139 | * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE. |
| 1140 | * And when content-length is used, we never want to let the possible |
| 1141 | * shutdown be forwarded to the other side, as the state machine will |
| 1142 | * take care of it once the client responds. It's also important to |
| 1143 | * prevent TIME_WAITs from accumulating on the backend side, and for |
| 1144 | * HTTP/2 where the last frame comes with a shutdown. |
| 1145 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1146 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1147 | channel_dont_close(req); |
| 1148 | |
| 1149 | /* We know that more data are expected, but we couldn't send more that |
| 1150 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
| 1151 | * system knows it must not set a PUSH on this first part. Interactive |
| 1152 | * modes are already handled by the stream sock layer. We must not do |
| 1153 | * this in content-length mode because it could present the MSG_MORE |
| 1154 | * flag with the last block of forwarded data, which would cause an |
| 1155 | * additional delay to be observed by the receiver. |
| 1156 | */ |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 1157 | if (HAS_REQ_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1158 | req->flags |= CF_EXPECT_MORE; |
| 1159 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1160 | DBG_TRACE_DEVEL("waiting for more data to forward", |
| 1161 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1162 | return 0; |
| 1163 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1164 | return_cli_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1165 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 1166 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1167 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1168 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1169 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1170 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1171 | if (!(s->flags & SF_ERR_MASK)) |
| 1172 | s->flags |= SF_ERR_CLICL; |
| 1173 | status = 400; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1174 | goto return_prx_cond; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1175 | |
| 1176 | return_srv_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1177 | _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts); |
| 1178 | _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1179 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1180 | _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1181 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1182 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1183 | if (!(s->flags & SF_ERR_MASK)) |
| 1184 | s->flags |= SF_ERR_SRVCL; |
| 1185 | status = 502; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1186 | goto return_prx_cond; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1187 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1188 | return_int_err: |
| 1189 | if (!(s->flags & SF_ERR_MASK)) |
| 1190 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1191 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 1192 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1193 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1194 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1195 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1196 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1197 | status = 500; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1198 | goto return_prx_cond; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1199 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1200 | return_bad_req: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1201 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1202 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1203 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1204 | status = 400; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1205 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1206 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1207 | return_prx_cond: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1208 | if (txn->status > 0) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1209 | /* Note: we don't send any error if some data were already sent */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1210 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1211 | } else { |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1212 | txn->status = status; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1213 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1214 | } |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1215 | if (!(s->flags & SF_ERR_MASK)) |
| 1216 | s->flags |= SF_ERR_PRXCOND; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1217 | if (!(s->flags & SF_FINST_MASK)) |
| 1218 | s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1219 | DBG_TRACE_DEVEL("leaving on error ", |
| 1220 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1221 | return 0; |
| 1222 | } |
| 1223 | |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1224 | /* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */ |
| 1225 | /* Returns 0 if we can attempt to retry, -1 otherwise */ |
| 1226 | static __inline int do_l7_retry(struct stream *s, struct stream_interface *si) |
| 1227 | { |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1228 | struct channel *req, *res; |
| 1229 | int co_data; |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1230 | |
Christopher Faulet | 731c8e6 | 2022-03-29 16:08:44 +0200 | [diff] [blame] | 1231 | s->conn_retries++; |
| 1232 | if (s->conn_retries >= s->be->conn_retries) |
Christopher Faulet | 552601d | 2021-05-26 10:31:06 +0200 | [diff] [blame] | 1233 | return -1; |
Christopher Faulet | 5b82cc5 | 2020-10-12 15:18:50 +0200 | [diff] [blame] | 1234 | |
Christopher Faulet | e763c8c | 2021-05-05 18:23:59 +0200 | [diff] [blame] | 1235 | if (objt_server(s->target)) { |
| 1236 | if (s->flags & SF_CURR_SESS) { |
| 1237 | s->flags &= ~SF_CURR_SESS; |
| 1238 | _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess); |
| 1239 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1240 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries); |
Christopher Faulet | e763c8c | 2021-05-05 18:23:59 +0200 | [diff] [blame] | 1241 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1242 | _HA_ATOMIC_INC(&s->be->be_counters.retries); |
Willy Tarreau | 223995e | 2019-05-04 10:38:31 +0200 | [diff] [blame] | 1243 | |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1244 | req = &s->req; |
| 1245 | res = &s->res; |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1246 | /* Remove any write error from the request, and read error from the response */ |
| 1247 | req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW); |
| 1248 | res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW); |
Christopher Faulet | 7bf46bb | 2022-01-04 10:56:03 +0100 | [diff] [blame] | 1249 | res->analysers &= AN_RES_FLT_END; |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 1250 | si->cs->endp->flags &= ~CS_EP_RXBLK_SHUT; |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 1251 | s->conn_err_type = STRM_ET_NONE; |
Christopher Faulet | ae024ce | 2022-03-29 19:02:31 +0200 | [diff] [blame] | 1252 | s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK); |
| 1253 | s->conn_exp = TICK_ETERNITY; |
Olivier Houchard | 4bd5867 | 2019-07-12 16:16:59 +0200 | [diff] [blame] | 1254 | stream_choose_redispatch(s); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1255 | res->rex = TICK_ETERNITY; |
| 1256 | res->to_forward = 0; |
| 1257 | res->analyse_exp = TICK_ETERNITY; |
| 1258 | res->total = 0; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 1259 | |
| 1260 | if (cs_reset_endp(s->csb) < 0) { |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 1261 | if (!(s->flags & SF_ERR_MASK)) |
| 1262 | s->flags |= SF_ERR_INTERNAL; |
| 1263 | return -1; |
| 1264 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1265 | |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1266 | b_free(&req->buf); |
| 1267 | /* Swap the L7 buffer with the channel buffer */ |
| 1268 | /* We know we stored the co_data as b_data, so get it there */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1269 | co_data = b_data(&s->txn->l7_buffer); |
| 1270 | b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer)); |
| 1271 | b_xfer(&req->buf, &s->txn->l7_buffer, b_data(&s->txn->l7_buffer)); |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1272 | co_set_data(req, co_data); |
Christopher Faulet | 5b82cc5 | 2020-10-12 15:18:50 +0200 | [diff] [blame] | 1273 | |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 1274 | DBG_TRACE_DEVEL("perform a L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, s->txn); |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1275 | |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1276 | b_reset(&res->buf); |
| 1277 | co_set_data(res, 0); |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1281 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 1282 | * processing can continue on next analysers, or zero if it either needs more |
| 1283 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 1284 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers |
| 1285 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1286 | * abort. |
| 1287 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1288 | int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1289 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1290 | /* |
| 1291 | * We will analyze a complete HTTP response to check the its syntax. |
| 1292 | * |
| 1293 | * Once the start line and all headers are received, we may perform a |
| 1294 | * capture of the error (if any), and we will set a few fields. We also |
| 1295 | * logging and finally headers capture. |
| 1296 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1297 | struct session *sess = s->sess; |
| 1298 | struct http_txn *txn = s->txn; |
| 1299 | struct http_msg *msg = &txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1300 | struct htx *htx; |
Christopher Faulet | a629447 | 2021-12-23 13:25:57 +0100 | [diff] [blame] | 1301 | struct stream_interface *si_b = cs_si(s->csb); |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1302 | struct connection *srv_conn; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1303 | struct htx_sl *sl; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1304 | int n; |
| 1305 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1306 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1307 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1308 | htx = htxbuf(&rep->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1309 | |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1310 | /* Parsing errors are caught here */ |
| 1311 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 1312 | goto return_bad_res; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1313 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 1314 | goto return_int_err; |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1315 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1316 | /* |
| 1317 | * Now we quickly check if we have found a full valid response. |
| 1318 | * If not so, we check the FD and buffer states before leaving. |
| 1319 | * A full response is indicated by the fact that we have seen |
| 1320 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
| 1321 | * responses are checked first. |
| 1322 | * |
| 1323 | * Depending on whether the client is still there or not, we |
| 1324 | * may send an error response back or not. Note that normally |
| 1325 | * we should only check for HTTP status there, and check I/O |
| 1326 | * errors somewhere else. |
| 1327 | */ |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1328 | next_one: |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 1329 | if (unlikely(htx_is_empty(htx) || htx->first == -1)) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1330 | /* 1: have we encountered a read error ? */ |
| 1331 | if (rep->flags & CF_READ_ERROR) { |
Christopher Faulet | 95a61e8 | 2021-12-22 14:22:03 +0100 | [diff] [blame] | 1332 | struct connection *conn = cs_conn(s->csb); |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1333 | |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1334 | /* Perform a L7 retry because server refuses the early data. */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1335 | if ((txn->flags & TX_L7_RETRY) && |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1336 | (s->be->retry_type & PR_RE_EARLY_ERROR) && |
| 1337 | conn && conn->err_code == CO_ER_SSL_EARLY_FAILED && |
| 1338 | do_l7_retry(s, si_b) == 0) { |
| 1339 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1340 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
Olivier Houchard | 6db1699 | 2019-05-17 15:40:49 +0200 | [diff] [blame] | 1344 | if (txn->flags & TX_NOT_FIRST) |
| 1345 | goto abort_keep_alive; |
| 1346 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1347 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1348 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1349 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1350 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1351 | } |
| 1352 | |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1353 | /* if the server refused the early data, just send a 425 */ |
| 1354 | if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1355 | txn->status = 425; |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1356 | else { |
| 1357 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1358 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1359 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1360 | |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1361 | s->csb->flags |= CS_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1362 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1363 | |
| 1364 | if (!(s->flags & SF_ERR_MASK)) |
| 1365 | s->flags |= SF_ERR_SRVCL; |
| 1366 | if (!(s->flags & SF_FINST_MASK)) |
| 1367 | s->flags |= SF_FINST_H; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1368 | DBG_TRACE_DEVEL("leaving on error", |
| 1369 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1370 | return 0; |
| 1371 | } |
| 1372 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1373 | /* 2: read timeout : return a 504 to the client. */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1374 | else if (rep->flags & CF_READ_TIMEOUT) { |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1375 | if ((txn->flags & TX_L7_RETRY) && |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1376 | (s->be->retry_type & PR_RE_TIMEOUT)) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1377 | if (co_data(rep) || do_l7_retry(s, si_b) == 0) { |
| 1378 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1379 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1380 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1381 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1382 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1383 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1384 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1385 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1386 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1387 | } |
| 1388 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1389 | txn->status = 504; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1390 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1391 | s->csb->flags |= CS_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1392 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1393 | |
| 1394 | if (!(s->flags & SF_ERR_MASK)) |
| 1395 | s->flags |= SF_ERR_SRVTO; |
| 1396 | if (!(s->flags & SF_FINST_MASK)) |
| 1397 | s->flags |= SF_FINST_H; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1398 | DBG_TRACE_DEVEL("leaving on error", |
| 1399 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1400 | return 0; |
| 1401 | } |
| 1402 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1403 | /* 3: client abort with an abortonclose */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1404 | else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1405 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 1406 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1407 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1408 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1409 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1410 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1411 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1412 | txn->status = 400; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1413 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1414 | |
| 1415 | if (!(s->flags & SF_ERR_MASK)) |
| 1416 | s->flags |= SF_ERR_CLICL; |
| 1417 | if (!(s->flags & SF_FINST_MASK)) |
| 1418 | s->flags |= SF_FINST_H; |
| 1419 | |
| 1420 | /* process_stream() will take care of the error */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1421 | DBG_TRACE_DEVEL("leaving on error", |
| 1422 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1423 | return 0; |
| 1424 | } |
| 1425 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1426 | /* 4: close from server, capture the response if the server has started to respond */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1427 | else if (rep->flags & CF_SHUTR) { |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1428 | if ((txn->flags & TX_L7_RETRY) && |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1429 | (s->be->retry_type & PR_RE_DISCONNECTED)) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1430 | if (co_data(rep) || do_l7_retry(s, si_b) == 0) { |
| 1431 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1432 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1433 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1434 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1435 | } |
| 1436 | |
Olivier Houchard | 6db1699 | 2019-05-17 15:40:49 +0200 | [diff] [blame] | 1437 | if (txn->flags & TX_NOT_FIRST) |
| 1438 | goto abort_keep_alive; |
| 1439 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1440 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1441 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1442 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1443 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1444 | } |
| 1445 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1446 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1447 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1448 | s->csb->flags |= CS_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1449 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1450 | |
| 1451 | if (!(s->flags & SF_ERR_MASK)) |
| 1452 | s->flags |= SF_ERR_SRVCL; |
| 1453 | if (!(s->flags & SF_FINST_MASK)) |
| 1454 | s->flags |= SF_FINST_H; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1455 | DBG_TRACE_DEVEL("leaving on error", |
| 1456 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1457 | return 0; |
| 1458 | } |
| 1459 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1460 | /* 5: write error to client (we don't send any message then) */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1461 | else if (rep->flags & CF_WRITE_ERROR) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1462 | if (txn->flags & TX_NOT_FIRST) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1463 | goto abort_keep_alive; |
| 1464 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1465 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1466 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1467 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1468 | rep->analysers &= AN_RES_FLT_END; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1469 | |
| 1470 | if (!(s->flags & SF_ERR_MASK)) |
| 1471 | s->flags |= SF_ERR_CLICL; |
| 1472 | if (!(s->flags & SF_FINST_MASK)) |
| 1473 | s->flags |= SF_FINST_H; |
| 1474 | |
| 1475 | /* process_stream() will take care of the error */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1476 | DBG_TRACE_DEVEL("leaving on error", |
| 1477 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1478 | return 0; |
| 1479 | } |
| 1480 | |
| 1481 | channel_dont_close(rep); |
| 1482 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1483 | DBG_TRACE_DEVEL("waiting for more data", |
| 1484 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1485 | return 0; |
| 1486 | } |
| 1487 | |
| 1488 | /* More interesting part now : we know that we have a complete |
| 1489 | * response which at least looks like HTTP. We have an indicator |
| 1490 | * of each header's length, so we can parse them quickly. |
| 1491 | */ |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 1492 | BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1493 | sl = http_get_stline(htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1494 | |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1495 | /* Perform a L7 retry because of the status code */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1496 | if ((txn->flags & TX_L7_RETRY) && |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1497 | l7_status_match(s->be, sl->info.res.status) && |
| 1498 | do_l7_retry(s, si_b) == 0) { |
| 1499 | DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 1500 | return 0; |
| 1501 | } |
| 1502 | |
| 1503 | /* Now, L7 buffer is useless, it can be released */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1504 | b_free(&txn->l7_buffer); |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1505 | |
| 1506 | msg->msg_state = HTTP_MSG_BODY; |
| 1507 | |
| 1508 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1509 | /* 0: we might have to print this header in debug mode */ |
| 1510 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1511 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 1512 | int32_t pos; |
| 1513 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1514 | http_debug_stline("srvrep", s, sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1515 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 1516 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1517 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 1518 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 1519 | |
| 1520 | if (type == HTX_BLK_EOH) |
| 1521 | break; |
| 1522 | if (type != HTX_BLK_HDR) |
| 1523 | continue; |
| 1524 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1525 | http_debug_hdr("srvhdr", s, |
| 1526 | htx_get_blk_name(htx, blk), |
| 1527 | htx_get_blk_value(htx, blk)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1528 | } |
| 1529 | } |
| 1530 | |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1531 | /* 1: get the status code and the version. Also set HTTP flags */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1532 | txn->status = sl->info.res.status; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1533 | if (sl->flags & HTX_SL_F_VER_11) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1534 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1535 | if (sl->flags & HTX_SL_F_XFER_LEN) { |
| 1536 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 1537 | if (sl->flags & HTX_SL_F_CLEN) |
| 1538 | msg->flags |= HTTP_MSGF_CNT_LEN; |
| 1539 | else if (sl->flags & HTX_SL_F_CHNK) |
| 1540 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1541 | } |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 1542 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 1543 | msg->flags |= HTTP_MSGF_BODYLESS; |
Christopher Faulet | 576c358 | 2021-01-08 15:53:01 +0100 | [diff] [blame] | 1544 | if (sl->flags & HTX_SL_F_CONN_UPG) |
| 1545 | msg->flags |= HTTP_MSGF_CONN_UPG; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1546 | |
| 1547 | n = txn->status / 100; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1548 | if (n < 1 || n > 5) |
| 1549 | n = 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1550 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1551 | /* when the client triggers a 4xx from the server, it's most often due |
| 1552 | * to a missing object or permission. These events should be tracked |
| 1553 | * because if they happen often, it may indicate a brute force or a |
| 1554 | * vulnerability scan. |
| 1555 | */ |
| 1556 | if (n == 4) |
| 1557 | stream_inc_http_err_ctr(s); |
| 1558 | |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1559 | if (n == 5 && txn->status != 501 && txn->status != 505) |
| 1560 | stream_inc_http_fail_ctr(s); |
| 1561 | |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 1562 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1563 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]); |
| 1564 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req); |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 1565 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1566 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1567 | /* Adjust server's health based on status code. Note: status codes 501 |
| 1568 | * and 505 are triggered on demand by client request, so we must not |
| 1569 | * count them as server failures. |
| 1570 | */ |
| 1571 | if (objt_server(s->target)) { |
| 1572 | if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505)) |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1573 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1574 | else |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1575 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | /* |
| 1579 | * We may be facing a 100-continue response, or any other informational |
| 1580 | * 1xx response which is non-final, in which case this is not the right |
| 1581 | * response, and we're waiting for the next one. Let's allow this response |
| 1582 | * to go to the client and wait for the next one. There's an exception for |
| 1583 | * 101 which is used later in the code to switch protocols. |
| 1584 | */ |
| 1585 | if (txn->status < 200 && |
| 1586 | (txn->status == 100 || txn->status >= 102)) { |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1587 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1588 | htx->first = channel_htx_fwd_headers(rep, htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1589 | msg->msg_state = HTTP_MSG_RPBEFORE; |
Christopher Faulet | 3499f62 | 2019-09-03 15:23:54 +0200 | [diff] [blame] | 1590 | msg->flags = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1591 | txn->status = 0; |
| 1592 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 7d51845 | 2020-08-31 11:07:07 +0200 | [diff] [blame] | 1593 | rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */ |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1594 | goto next_one; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1595 | } |
| 1596 | |
Christopher Faulet | 6e6c7b1 | 2021-01-08 16:02:05 +0100 | [diff] [blame] | 1597 | /* A 101-switching-protocols must contains a Connection header with the |
| 1598 | * "upgrade" option and the request too. It means both are agree to |
| 1599 | * upgrade. It is not so strict because there is no test on the Upgrade |
| 1600 | * header content. But it is probably stronger enough for now. |
| 1601 | */ |
| 1602 | if (txn->status == 101 && |
| 1603 | (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG))) |
| 1604 | goto return_bad_res; |
| 1605 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1606 | /* |
| 1607 | * 2: check for cacheability. |
| 1608 | */ |
| 1609 | |
| 1610 | switch (txn->status) { |
| 1611 | case 200: |
| 1612 | case 203: |
| 1613 | case 204: |
| 1614 | case 206: |
| 1615 | case 300: |
| 1616 | case 301: |
| 1617 | case 404: |
| 1618 | case 405: |
| 1619 | case 410: |
| 1620 | case 414: |
| 1621 | case 501: |
| 1622 | break; |
| 1623 | default: |
| 1624 | /* RFC7231#6.1: |
| 1625 | * Responses with status codes that are defined as |
| 1626 | * cacheable by default (e.g., 200, 203, 204, 206, |
| 1627 | * 300, 301, 404, 405, 410, 414, and 501 in this |
| 1628 | * specification) can be reused by a cache with |
| 1629 | * heuristic expiration unless otherwise indicated |
| 1630 | * by the method definition or explicit cache |
| 1631 | * controls [RFC7234]; all other status codes are |
| 1632 | * not cacheable by default. |
| 1633 | */ |
| 1634 | txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK); |
| 1635 | break; |
| 1636 | } |
| 1637 | |
| 1638 | /* |
| 1639 | * 3: we may need to capture headers |
| 1640 | */ |
| 1641 | s->logs.logwait &= ~LW_RESP; |
| 1642 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1643 | http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1644 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1645 | /* Skip parsing if no content length is possible. */ |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 1646 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1647 | txn->status == 101)) { |
| 1648 | /* Either we've established an explicit tunnel, or we're |
| 1649 | * switching the protocol. In both cases, we're very unlikely |
| 1650 | * to understand the next protocols. We have to switch to tunnel |
| 1651 | * mode, so that we transfer the request and responses then let |
| 1652 | * this protocol pass unmodified. When we later implement specific |
| 1653 | * parsers for such protocols, we'll want to check the Upgrade |
| 1654 | * header which contains information about that protocol for |
| 1655 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 1656 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 1657 | txn->flags |= TX_CON_WANT_TUN; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1658 | } |
| 1659 | |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1660 | /* check for NTML authentication headers in 401 (WWW-Authenticate) and |
| 1661 | * 407 (Proxy-Authenticate) responses and set the connection to private |
| 1662 | */ |
Christopher Faulet | 95a61e8 | 2021-12-22 14:22:03 +0100 | [diff] [blame] | 1663 | srv_conn = cs_conn(s->csb); |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1664 | if (srv_conn) { |
| 1665 | struct ist hdr; |
| 1666 | struct http_hdr_ctx ctx; |
| 1667 | |
| 1668 | if (txn->status == 401) |
| 1669 | hdr = ist("WWW-Authenticate"); |
| 1670 | else if (txn->status == 407) |
| 1671 | hdr = ist("Proxy-Authenticate"); |
| 1672 | else |
| 1673 | goto end; |
| 1674 | |
| 1675 | ctx.blk = NULL; |
| 1676 | while (http_find_header(htx, hdr, &ctx, 0)) { |
Willy Tarreau | f1dcced | 2020-05-07 19:27:02 +0200 | [diff] [blame] | 1677 | /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM", |
| 1678 | * possibly followed by blanks and a base64 string, the connection |
| 1679 | * is private. Since it's a mess to deal with, we only check for |
| 1680 | * values starting with "NTLM" or "Nego". Note that often multiple |
| 1681 | * headers are sent by the server there. |
| 1682 | */ |
| 1683 | if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) || |
Willy Tarreau | 49a1d28 | 2020-05-07 19:10:15 +0200 | [diff] [blame] | 1684 | (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) { |
Olivier Houchard | 250031e | 2019-05-29 15:01:50 +0200 | [diff] [blame] | 1685 | sess->flags |= SESS_FL_PREFER_LAST; |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 1686 | conn_set_owner(srv_conn, sess, NULL); |
Christopher Faulet | 21ddc74 | 2020-07-01 15:26:14 +0200 | [diff] [blame] | 1687 | conn_set_private(srv_conn); |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 1688 | /* If it fail now, the same will be done in mux->detach() callback */ |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 1689 | session_add_conn(srv_conn->owner, srv_conn, srv_conn->target); |
Willy Tarreau | f1dcced | 2020-05-07 19:27:02 +0200 | [diff] [blame] | 1690 | break; |
Olivier Houchard | 250031e | 2019-05-29 15:01:50 +0200 | [diff] [blame] | 1691 | } |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | end: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1696 | /* we want to have the response time before we start processing it */ |
| 1697 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 1698 | |
| 1699 | /* end of job, return OK */ |
| 1700 | rep->analysers &= ~an_bit; |
| 1701 | rep->analyse_exp = TICK_ETERNITY; |
| 1702 | channel_auto_close(rep); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1703 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1704 | return 1; |
| 1705 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1706 | return_int_err: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1707 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 1708 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1709 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1710 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1711 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1712 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1713 | txn->status = 500; |
| 1714 | if (!(s->flags & SF_ERR_MASK)) |
| 1715 | s->flags |= SF_ERR_INTERNAL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1716 | goto return_prx_cond; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1717 | |
| 1718 | return_bad_res: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1719 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1720 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1721 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1722 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1723 | } |
Olivier Houchard | e3249a9 | 2019-05-03 23:01:47 +0200 | [diff] [blame] | 1724 | if ((s->be->retry_type & PR_RE_JUNK_REQUEST) && |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1725 | (txn->flags & TX_L7_RETRY) && |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1726 | do_l7_retry(s, si_b) == 0) { |
| 1727 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1728 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | e3249a9 | 2019-05-03 23:01:47 +0200 | [diff] [blame] | 1729 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1730 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1731 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1732 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1733 | /* fall through */ |
| 1734 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1735 | return_prx_cond: |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1736 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1737 | |
| 1738 | if (!(s->flags & SF_ERR_MASK)) |
| 1739 | s->flags |= SF_ERR_PRXCOND; |
| 1740 | if (!(s->flags & SF_FINST_MASK)) |
| 1741 | s->flags |= SF_FINST_H; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1742 | |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1743 | s->csb->flags |= CS_FL_NOLINGER; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1744 | DBG_TRACE_DEVEL("leaving on error", |
| 1745 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1746 | return 0; |
| 1747 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1748 | abort_keep_alive: |
| 1749 | /* A keep-alive request to the server failed on a network error. |
| 1750 | * The client is required to retry. We need to close without returning |
| 1751 | * any other information so that the client retries. |
| 1752 | */ |
| 1753 | txn->status = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1754 | s->logs.logwait = 0; |
| 1755 | s->logs.level = 0; |
| 1756 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1757 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1758 | DBG_TRACE_DEVEL("leaving by closing K/A connection", |
| 1759 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1760 | return 0; |
| 1761 | } |
| 1762 | |
| 1763 | /* This function performs all the processing enabled for the current response. |
| 1764 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 1765 | * and updates s->res.analysers. It might make sense to explode it into several |
| 1766 | * other functions. It works like process_request (see indications above). |
| 1767 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1768 | int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1769 | { |
| 1770 | struct session *sess = s->sess; |
| 1771 | struct http_txn *txn = s->txn; |
| 1772 | struct http_msg *msg = &txn->rsp; |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1773 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1774 | struct proxy *cur_proxy; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1775 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 1776 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1777 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 1778 | return 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1779 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1780 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1781 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1782 | htx = htxbuf(&rep->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1783 | |
| 1784 | /* The stats applet needs to adjust the Connection header but we don't |
| 1785 | * apply any filter there. |
| 1786 | */ |
| 1787 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 1788 | rep->analysers &= ~an_bit; |
| 1789 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1790 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | /* |
| 1794 | * We will have to evaluate the filters. |
| 1795 | * As opposed to version 1.2, now they will be evaluated in the |
| 1796 | * filters order and not in the header order. This means that |
| 1797 | * each filter has to be validated among all headers. |
| 1798 | * |
| 1799 | * Filters are tried with ->be first, then with ->fe if it is |
| 1800 | * different from ->be. |
| 1801 | * |
| 1802 | * Maybe we are in resume condiion. In this case I choose the |
| 1803 | * "struct proxy" which contains the rule list matching the resume |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1804 | * pointer. If none of these "struct proxy" match, I initialise |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1805 | * the process with the first one. |
| 1806 | * |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1807 | * In fact, I check only correspondence between the current list |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1808 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 1809 | * the loop with the ->be. |
| 1810 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1811 | if (s->current_rule_list == &sess->fe->http_res_rules || |
| 1812 | (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1813 | cur_proxy = sess->fe; |
| 1814 | else |
| 1815 | cur_proxy = s->be; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1816 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1817 | while (1) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1818 | /* evaluate http-response rules */ |
Christopher Faulet | 46f46df | 2021-11-09 16:33:25 +0100 | [diff] [blame] | 1819 | if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1820 | struct list *def_rules, *rules; |
| 1821 | |
| 1822 | def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL); |
| 1823 | rules = &cur_proxy->http_res_rules; |
| 1824 | |
| 1825 | ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1826 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1827 | switch (ret) { |
| 1828 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 1829 | goto return_prx_yield; |
| 1830 | |
| 1831 | case HTTP_RULE_RES_CONT: |
| 1832 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 1833 | break; |
| 1834 | |
| 1835 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 1836 | goto deny; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1837 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1838 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent */ |
| 1839 | goto return_prx_cond; |
| 1840 | |
| 1841 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1842 | goto done; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1843 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1844 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 1845 | goto return_bad_res; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1846 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1847 | case HTTP_RULE_RES_ERROR: /* failed with a bad request */ |
| 1848 | goto return_int_err; |
| 1849 | } |
| 1850 | |
| 1851 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1852 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1853 | /* check whether we're already working on the frontend */ |
| 1854 | if (cur_proxy == sess->fe) |
| 1855 | break; |
| 1856 | cur_proxy = sess->fe; |
| 1857 | } |
| 1858 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1859 | /* OK that's all we can do for 1xx responses */ |
| 1860 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1861 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1862 | |
| 1863 | /* |
| 1864 | * Now check for a server cookie. |
| 1865 | */ |
| 1866 | if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1867 | http_manage_server_side_cookies(s, rep); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1868 | |
| 1869 | /* |
| 1870 | * Check for cache-control or pragma headers if required. |
| 1871 | */ |
| 1872 | if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1873 | http_check_response_for_cacheability(s, rep); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1874 | |
| 1875 | /* |
| 1876 | * Add server cookie in the response if needed |
| 1877 | */ |
| 1878 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 1879 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
| 1880 | (!(s->flags & SF_DIRECT) || |
| 1881 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 1882 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 1883 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 1884 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 1885 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 1886 | !(s->flags & SF_IGNORE_PRST)) { |
| 1887 | /* the server is known, it's not the one the client requested, or the |
| 1888 | * cookie's last seen date needs to be refreshed. We have to |
| 1889 | * insert a set-cookie here, except if we want to insert only on POST |
| 1890 | * requests and this one isn't. Note that servers which don't have cookies |
| 1891 | * (eg: some backup servers) will return a full cookie removal request. |
| 1892 | */ |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1893 | if (!__objt_server(s->target)->cookie) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1894 | chunk_printf(&trash, |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1895 | "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1896 | s->be->cookie_name); |
| 1897 | } |
| 1898 | else { |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1899 | chunk_printf(&trash, "%s=%s", s->be->cookie_name, __objt_server(s->target)->cookie); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1900 | |
| 1901 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 1902 | /* emit last_date, which is mandatory */ |
| 1903 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 1904 | s30tob64((date.tv_sec+3) >> 2, |
| 1905 | trash.area + trash.data); |
| 1906 | trash.data += 5; |
| 1907 | |
| 1908 | if (s->be->cookie_maxlife) { |
| 1909 | /* emit first_date, which is either the original one or |
| 1910 | * the current date. |
| 1911 | */ |
| 1912 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 1913 | s30tob64(txn->cookie_first_date ? |
| 1914 | txn->cookie_first_date >> 2 : |
| 1915 | (date.tv_sec+3) >> 2, |
| 1916 | trash.area + trash.data); |
| 1917 | trash.data += 5; |
| 1918 | } |
| 1919 | } |
| 1920 | chunk_appendf(&trash, "; path=/"); |
| 1921 | } |
| 1922 | |
| 1923 | if (s->be->cookie_domain) |
| 1924 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
| 1925 | |
| 1926 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 1927 | chunk_appendf(&trash, "; HttpOnly"); |
| 1928 | |
| 1929 | if (s->be->ck_opts & PR_CK_SECURE) |
| 1930 | chunk_appendf(&trash, "; Secure"); |
| 1931 | |
Christopher Faulet | 2f53390 | 2020-01-21 11:06:48 +0100 | [diff] [blame] | 1932 | if (s->be->cookie_attrs) |
| 1933 | chunk_appendf(&trash, "; %s", s->be->cookie_attrs); |
| 1934 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1935 | if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data)))) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1936 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1937 | |
| 1938 | txn->flags &= ~TX_SCK_MASK; |
| 1939 | if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
| 1940 | /* the server did not change, only the date was updated */ |
| 1941 | txn->flags |= TX_SCK_UPDATED; |
| 1942 | else |
| 1943 | txn->flags |= TX_SCK_INSERTED; |
| 1944 | |
| 1945 | /* Here, we will tell an eventual cache on the client side that we don't |
| 1946 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 1947 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 1948 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 1949 | */ |
| 1950 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
| 1951 | |
| 1952 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 1953 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1954 | if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private")))) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1955 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1956 | } |
| 1957 | } |
| 1958 | |
| 1959 | /* |
| 1960 | * Check if result will be cacheable with a cookie. |
| 1961 | * We'll block the response if security checks have caught |
| 1962 | * nasty things such as a cacheable cookie. |
| 1963 | */ |
| 1964 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 1965 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 1966 | (s->be->options & PR_O_CHK_CACHE)) { |
| 1967 | /* we're in presence of a cacheable response containing |
| 1968 | * a set-cookie header. We'll block it as requested by |
| 1969 | * the 'checkcache' option, and send an alert. |
| 1970 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1971 | ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1972 | s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>"); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1973 | send_log(s->be, LOG_ALERT, |
| 1974 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1975 | s->be->id, objt_server(s->target) ? __objt_server(s->target)->id : "<dispatch>"); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1976 | goto deny; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1977 | } |
| 1978 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1979 | end: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 1980 | /* |
| 1981 | * Evaluate after-response rules before forwarding the response. rules |
| 1982 | * from the backend are evaluated first, then one from the frontend if |
| 1983 | * it differs. |
| 1984 | */ |
| 1985 | if (!http_eval_after_res_rules(s)) |
| 1986 | goto return_int_err; |
| 1987 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 1988 | /* Filter the response headers if there are filters attached to the |
| 1989 | * stream. |
| 1990 | */ |
| 1991 | if (HAS_FILTERS(s)) |
| 1992 | rep->analysers |= AN_RES_FLT_HTTP_HDRS; |
| 1993 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1994 | /* Always enter in the body analyzer */ |
| 1995 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 1996 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
| 1997 | |
| 1998 | /* if the user wants to log as soon as possible, without counting |
| 1999 | * bytes from the server, then this is the right moment. We have |
| 2000 | * to temporarily assign bytes_out to log what we currently have. |
| 2001 | */ |
| 2002 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
| 2003 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2004 | s->logs.bytes_out = htx->data; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2005 | s->do_log(s); |
| 2006 | s->logs.bytes_out = 0; |
| 2007 | } |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2008 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2009 | done: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2010 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2011 | rep->analysers &= ~an_bit; |
| 2012 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2013 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2014 | return 1; |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2015 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2016 | deny: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2017 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp); |
| 2018 | _HA_ATOMIC_INC(&s->be->be_counters.denied_resp); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2019 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2020 | _HA_ATOMIC_INC(&sess->listener->counters->denied_resp); |
Christopher Faulet | a08546b | 2019-12-16 16:07:34 +0100 | [diff] [blame] | 2021 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2022 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2023 | goto return_prx_err; |
| 2024 | |
| 2025 | return_int_err: |
| 2026 | txn->status = 500; |
| 2027 | if (!(s->flags & SF_ERR_MASK)) |
| 2028 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2029 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 2030 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
Dragan Dosen | 9a006f9 | 2021-09-21 13:02:09 +0200 | [diff] [blame] | 2031 | if (sess->listener && sess->listener->counters) |
| 2032 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 2033 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2034 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2035 | goto return_prx_err; |
| 2036 | |
| 2037 | return_bad_res: |
| 2038 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2039 | stream_inc_http_fail_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2040 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | a20a653 | 2020-02-05 10:16:41 +0100 | [diff] [blame] | 2041 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2042 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | a20a653 | 2020-02-05 10:16:41 +0100 | [diff] [blame] | 2043 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 2044 | } |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2045 | /* fall through */ |
| 2046 | |
| 2047 | return_prx_err: |
| 2048 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 2049 | /* fall through */ |
| 2050 | |
| 2051 | return_prx_cond: |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2052 | s->logs.t_data = -1; /* was not a valid response */ |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 2053 | s->csb->flags |= CS_FL_NOLINGER; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2054 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2055 | if (!(s->flags & SF_ERR_MASK)) |
| 2056 | s->flags |= SF_ERR_PRXCOND; |
| 2057 | if (!(s->flags & SF_FINST_MASK)) |
| 2058 | s->flags |= SF_FINST_H; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2059 | |
Christopher Faulet | e58c000 | 2020-03-02 16:21:01 +0100 | [diff] [blame] | 2060 | rep->analysers &= AN_RES_FLT_END; |
| 2061 | s->req.analysers &= AN_REQ_FLT_END; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2062 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2063 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2064 | DBG_TRACE_DEVEL("leaving on error", |
| 2065 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2066 | return 0; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2067 | |
| 2068 | return_prx_yield: |
| 2069 | channel_dont_close(rep); |
| 2070 | DBG_TRACE_DEVEL("waiting for more data", |
| 2071 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 2072 | return 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | /* This function is an analyser which forwards response body (including chunk |
| 2076 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 2077 | * zero byte. The only situation where it must not be called is when we're in |
| 2078 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 2079 | * remaining data and to resync after end of body. It expects the msg_state to |
| 2080 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 2081 | * read more data, or 1 once we can go on with next request or end the stream. |
| 2082 | * |
| 2083 | * It is capable of compressing response data both in content-length mode and |
| 2084 | * in chunked mode. The state machines follows different flows depending on |
| 2085 | * whether content-length and chunked modes are used, since there are no |
| 2086 | * trailers in content-length : |
| 2087 | * |
| 2088 | * chk-mode cl-mode |
| 2089 | * ,----- BODY -----. |
| 2090 | * / \ |
| 2091 | * V size > 0 V chk-mode |
| 2092 | * .--> SIZE -------------> DATA -------------> CRLF |
| 2093 | * | | size == 0 | last byte | |
| 2094 | * | v final crlf v inspected | |
| 2095 | * | TRAILERS -----------> DONE | |
| 2096 | * | | |
| 2097 | * `----------------------------------------------' |
| 2098 | * |
| 2099 | * Compression only happens in the DATA state, and must be flushed in final |
| 2100 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 2101 | * is performed at once on final states for all bytes parsed, or when leaving |
| 2102 | * on missing data. |
| 2103 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2104 | int http_response_forward_body(struct stream *s, struct channel *res, int an_bit) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2105 | { |
| 2106 | struct session *sess = s->sess; |
| 2107 | struct http_txn *txn = s->txn; |
| 2108 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2109 | struct htx *htx; |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2110 | int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2111 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2112 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2113 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2114 | htx = htxbuf(&res->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2115 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2116 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 2117 | goto return_bad_res; |
| 2118 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 2119 | goto return_int_err; |
| 2120 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2121 | if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2122 | ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2123 | /* Output closed while we were sending data. We must abort and |
| 2124 | * wake the other side up. |
| 2125 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2126 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2127 | http_end_response(s); |
| 2128 | http_end_request(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2129 | DBG_TRACE_DEVEL("leaving on error", |
| 2130 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2131 | return 1; |
| 2132 | } |
| 2133 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2134 | if (msg->msg_state == HTTP_MSG_BODY) |
| 2135 | msg->msg_state = HTTP_MSG_DATA; |
| 2136 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2137 | /* in most states, we should abort in case of early close */ |
| 2138 | channel_auto_close(res); |
| 2139 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2140 | if (res->to_forward) { |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2141 | if (res->to_forward == CHN_INFINITE_FORWARD) { |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2142 | if (res->flags & CF_EOI) |
| 2143 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2144 | } |
| 2145 | else { |
| 2146 | /* We can't process the buffer's contents yet */ |
| 2147 | res->flags |= CF_WAKE_WRITE; |
| 2148 | goto missing_data_or_waiting; |
| 2149 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2150 | } |
| 2151 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2152 | if (msg->msg_state >= HTTP_MSG_ENDING) |
| 2153 | goto ending; |
| 2154 | |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 2155 | if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 || |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2156 | (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) { |
| 2157 | msg->msg_state = HTTP_MSG_ENDING; |
| 2158 | goto ending; |
| 2159 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2160 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2161 | /* Forward input data. We get it by removing all outgoing data not |
| 2162 | * forwarded yet from HTX data size. If there are some data filters, we |
| 2163 | * let them decide the amount of data to forward. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2164 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2165 | if (HAS_RSP_DATA_FILTERS(s)) { |
| 2166 | ret = flt_http_payload(s, msg, htx->data); |
| 2167 | if (ret < 0) |
| 2168 | goto return_bad_res; |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 2169 | c_adv(res, ret); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2170 | } |
| 2171 | else { |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 2172 | c_adv(res, htx->data - co_data(res)); |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2173 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
| 2174 | channel_htx_forward_forever(res, htx); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2175 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2176 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2177 | if (htx->data != co_data(res)) |
| 2178 | goto missing_data_or_waiting; |
| 2179 | |
| 2180 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) { |
| 2181 | msg->msg_state = HTTP_MSG_ENDING; |
| 2182 | goto ending; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2183 | } |
| 2184 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2185 | /* Check if the end-of-message is reached and if so, switch the message |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 2186 | * in HTTP_MSG_ENDING state. Then if all data was marked to be |
| 2187 | * forwarded, set the state to HTTP_MSG_DONE. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2188 | */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2189 | if (!(htx->flags & HTX_FL_EOM)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2190 | goto missing_data_or_waiting; |
| 2191 | |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 2192 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2193 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2194 | ending: |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 2195 | res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */ |
| 2196 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2197 | /* other states, ENDING...TUNNEL */ |
| 2198 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 2199 | goto done; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2200 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2201 | if (HAS_RSP_DATA_FILTERS(s)) { |
| 2202 | ret = flt_http_end(s, msg); |
| 2203 | if (ret <= 0) { |
| 2204 | if (!ret) |
| 2205 | goto missing_data_or_waiting; |
| 2206 | goto return_bad_res; |
| 2207 | } |
| 2208 | } |
| 2209 | |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 2210 | if ((txn->meth == HTTP_METH_CONNECT && txn->status >= 200 && txn->status < 300) || txn->status == 101 || |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2211 | !(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 2212 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 2213 | goto ending; |
| 2214 | } |
| 2215 | else { |
| 2216 | msg->msg_state = HTTP_MSG_DONE; |
| 2217 | res->to_forward = 0; |
| 2218 | } |
| 2219 | |
| 2220 | done: |
| 2221 | |
| 2222 | channel_dont_close(res); |
| 2223 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2224 | http_end_response(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2225 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2226 | http_end_request(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2227 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2228 | if (res->flags & CF_SHUTW) { |
| 2229 | /* response errors are most likely due to the |
| 2230 | * client aborting the transfer. */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2231 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2232 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2233 | goto return_bad_res; |
| 2234 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2235 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2236 | return 1; |
| 2237 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2238 | DBG_TRACE_DEVEL("waiting for the end of the HTTP txn", |
| 2239 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2240 | return 0; |
| 2241 | |
| 2242 | missing_data_or_waiting: |
| 2243 | if (res->flags & CF_SHUTW) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2244 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2245 | |
| 2246 | /* stop waiting for data if the input is closed before the end. If the |
| 2247 | * client side was already closed, it means that the client has aborted, |
| 2248 | * so we don't want to count this as a server abort. Otherwise it's a |
| 2249 | * server abort. |
| 2250 | */ |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 2251 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2252 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2253 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2254 | /* If we have some pending data, we continue the processing */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2255 | if (htx_is_empty(htx)) |
| 2256 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2257 | } |
| 2258 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2259 | /* When TE: chunked is used, we need to get there again to parse |
| 2260 | * remaining chunks even if the server has closed, so we don't want to |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2261 | * set CF_DONTCLOSE. Similarly when there is a content-leng or if there |
| 2262 | * are filters registered on the stream, we don't want to forward a |
| 2263 | * close |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2264 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2265 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2266 | channel_dont_close(res); |
| 2267 | |
| 2268 | /* We know that more data are expected, but we couldn't send more that |
| 2269 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
| 2270 | * system knows it must not set a PUSH on this first part. Interactive |
| 2271 | * modes are already handled by the stream sock layer. We must not do |
| 2272 | * this in content-length mode because it could present the MSG_MORE |
| 2273 | * flag with the last block of forwarded data, which would cause an |
| 2274 | * additional delay to be observed by the receiver. |
| 2275 | */ |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 2276 | if (HAS_RSP_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2277 | res->flags |= CF_EXPECT_MORE; |
| 2278 | |
| 2279 | /* the stream handler will take care of timeouts and errors */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2280 | DBG_TRACE_DEVEL("waiting for more data to forward", |
| 2281 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2282 | return 0; |
| 2283 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2284 | return_srv_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2285 | _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts); |
| 2286 | _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2287 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2288 | _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2289 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2290 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts); |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2291 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2292 | if (!(s->flags & SF_ERR_MASK)) |
| 2293 | s->flags |= SF_ERR_SRVCL; |
| 2294 | goto return_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2295 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2296 | return_cli_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2297 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 2298 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2299 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2300 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2301 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2302 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2303 | if (!(s->flags & SF_ERR_MASK)) |
| 2304 | s->flags |= SF_ERR_CLICL; |
| 2305 | goto return_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2306 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2307 | return_int_err: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2308 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 2309 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2310 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2311 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2312 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2313 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2314 | if (!(s->flags & SF_ERR_MASK)) |
| 2315 | s->flags |= SF_ERR_INTERNAL; |
| 2316 | goto return_error; |
| 2317 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2318 | return_bad_res: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2319 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2320 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2321 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2322 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 2323 | } |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2324 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2325 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2326 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2327 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2328 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2329 | return_error: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2330 | /* don't send any error message as we're in the body */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2331 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2332 | if (!(s->flags & SF_FINST_MASK)) |
| 2333 | s->flags |= SF_FINST_D; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2334 | DBG_TRACE_DEVEL("leaving on error", |
| 2335 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2336 | return 0; |
| 2337 | } |
| 2338 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2339 | /* Perform an HTTP redirect based on the information in <rule>. The function |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2340 | * returns zero in case of an irrecoverable error such as too large a request |
| 2341 | * to build a valid response, 1 in case of successful redirect (hence the rule |
| 2342 | * is final), or 2 if the rule has to be silently skipped. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2343 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2344 | int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2345 | { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2346 | struct channel *req = &s->req; |
| 2347 | struct channel *res = &s->res; |
| 2348 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2349 | struct htx_sl *sl; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2350 | struct buffer *chunk; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2351 | struct ist status, reason, location; |
| 2352 | unsigned int flags; |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2353 | int close = 0; /* Try to keep the connection alive byt default */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2354 | |
| 2355 | chunk = alloc_trash_chunk(); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2356 | if (!chunk) { |
| 2357 | if (!(s->flags & SF_ERR_MASK)) |
| 2358 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2359 | goto fail; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2360 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2361 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2362 | /* |
| 2363 | * Create the location |
| 2364 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2365 | htx = htxbuf(&req->buf); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2366 | switch(rule->type) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2367 | case REDIRECT_TYPE_SCHEME: { |
| 2368 | struct http_hdr_ctx ctx; |
| 2369 | struct ist path, host; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2370 | struct http_uri_parser parser; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2371 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2372 | host = ist(""); |
| 2373 | ctx.blk = NULL; |
| 2374 | if (http_find_header(htx, ist("Host"), &ctx, 0)) |
| 2375 | host = ctx.value; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2376 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2377 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2378 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 2379 | path = http_parse_path(&parser); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2380 | /* build message using path */ |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 2381 | if (isttest(path)) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2382 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2383 | int qs = 0; |
| 2384 | while (qs < path.len) { |
| 2385 | if (*(path.ptr + qs) == '?') { |
| 2386 | path.len = qs; |
| 2387 | break; |
| 2388 | } |
| 2389 | qs++; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2390 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2391 | } |
| 2392 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2393 | else |
| 2394 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2395 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2396 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2397 | /* add scheme */ |
| 2398 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2399 | goto fail; |
| 2400 | } |
| 2401 | else { |
| 2402 | /* add scheme with executing log format */ |
| 2403 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2404 | chunk->size - chunk->data, |
| 2405 | &rule->rdr_fmt); |
| 2406 | } |
| 2407 | /* add "://" + host + path */ |
| 2408 | if (!chunk_memcat(chunk, "://", 3) || |
| 2409 | !chunk_memcat(chunk, host.ptr, host.len) || |
| 2410 | !chunk_memcat(chunk, path.ptr, path.len)) |
| 2411 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2412 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2413 | /* append a slash at the end of the location if needed and missing */ |
| 2414 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2415 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2416 | if (chunk->data + 1 >= chunk->size) |
| 2417 | goto fail; |
| 2418 | chunk->area[chunk->data++] = '/'; |
| 2419 | } |
| 2420 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2421 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2422 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2423 | case REDIRECT_TYPE_PREFIX: { |
| 2424 | struct ist path; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2425 | struct http_uri_parser parser; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2426 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2427 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2428 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 2429 | path = http_parse_path(&parser); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2430 | /* build message using path */ |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 2431 | if (isttest(path)) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2432 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2433 | int qs = 0; |
| 2434 | while (qs < path.len) { |
| 2435 | if (*(path.ptr + qs) == '?') { |
| 2436 | path.len = qs; |
| 2437 | break; |
| 2438 | } |
| 2439 | qs++; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2440 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2441 | } |
| 2442 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2443 | else |
| 2444 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2445 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2446 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2447 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2448 | * add anything, otherwise it makes it hard for the user to |
| 2449 | * configure a self-redirection. |
| 2450 | */ |
| 2451 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 2452 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2453 | goto fail; |
| 2454 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2455 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2456 | else { |
| 2457 | /* add prefix with executing log format */ |
| 2458 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2459 | chunk->size - chunk->data, |
| 2460 | &rule->rdr_fmt); |
| 2461 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2462 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2463 | /* add path */ |
| 2464 | if (!chunk_memcat(chunk, path.ptr, path.len)) |
| 2465 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2466 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2467 | /* append a slash at the end of the location if needed and missing */ |
| 2468 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2469 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2470 | if (chunk->data + 1 >= chunk->size) |
| 2471 | goto fail; |
| 2472 | chunk->area[chunk->data++] = '/'; |
| 2473 | } |
| 2474 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2475 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2476 | case REDIRECT_TYPE_LOCATION: |
| 2477 | default: |
| 2478 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2479 | /* add location */ |
| 2480 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2481 | goto fail; |
| 2482 | } |
| 2483 | else { |
| 2484 | /* add location with executing log format */ |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2485 | int len = build_logline(s, chunk->area + chunk->data, |
| 2486 | chunk->size - chunk->data, |
| 2487 | &rule->rdr_fmt); |
| 2488 | if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) |
| 2489 | return 2; |
| 2490 | |
| 2491 | chunk->data += len; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2492 | } |
| 2493 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2494 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2495 | location = ist2(chunk->area, chunk->data); |
| 2496 | |
| 2497 | /* |
| 2498 | * Create the 30x response |
| 2499 | */ |
| 2500 | switch (rule->code) { |
| 2501 | case 308: |
| 2502 | status = ist("308"); |
| 2503 | reason = ist("Permanent Redirect"); |
| 2504 | break; |
| 2505 | case 307: |
| 2506 | status = ist("307"); |
| 2507 | reason = ist("Temporary Redirect"); |
| 2508 | break; |
| 2509 | case 303: |
| 2510 | status = ist("303"); |
| 2511 | reason = ist("See Other"); |
| 2512 | break; |
| 2513 | case 301: |
| 2514 | status = ist("301"); |
| 2515 | reason = ist("Moved Permanently"); |
| 2516 | break; |
| 2517 | case 302: |
| 2518 | default: |
| 2519 | status = ist("302"); |
| 2520 | reason = ist("Found"); |
| 2521 | break; |
| 2522 | } |
| 2523 | |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2524 | if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE) |
| 2525 | close = 1; |
| 2526 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2527 | htx = htx_from_buf(&res->buf); |
Kevin Zhu | 96b3639 | 2020-01-07 09:42:55 +0100 | [diff] [blame] | 2528 | /* Trim any possible response */ |
| 2529 | channel_htx_truncate(&s->res, htx); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2530 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 2531 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason); |
| 2532 | if (!sl) |
| 2533 | goto fail; |
| 2534 | sl->info.res.status = rule->code; |
| 2535 | s->txn->status = rule->code; |
| 2536 | |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2537 | if (close && !htx_add_header(htx, ist("Connection"), ist("close"))) |
| 2538 | goto fail; |
| 2539 | |
| 2540 | if (!htx_add_header(htx, ist("Content-length"), ist("0")) || |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2541 | !htx_add_header(htx, ist("Location"), location)) |
| 2542 | goto fail; |
| 2543 | |
| 2544 | if (rule->code == 302 || rule->code == 303 || rule->code == 307) { |
| 2545 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache"))) |
| 2546 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2547 | } |
| 2548 | |
| 2549 | if (rule->cookie_len) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2550 | if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len))) |
| 2551 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2552 | } |
| 2553 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2554 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2555 | goto fail; |
| 2556 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2557 | htx->flags |= HTX_FL_EOM; |
Kevin Zhu | 96b3639 | 2020-01-07 09:42:55 +0100 | [diff] [blame] | 2558 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 2559 | if (!http_forward_proxy_resp(s, 1)) |
| 2560 | goto fail; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2561 | |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2562 | if (rule->flags & REDIRECT_FLAG_FROM_REQ) { |
| 2563 | /* let's log the request time */ |
| 2564 | s->logs.tv_request = now; |
Christopher Faulet | d347588 | 2021-10-04 14:16:46 +0200 | [diff] [blame] | 2565 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2566 | |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2567 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2568 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2569 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2570 | |
| 2571 | if (!(s->flags & SF_ERR_MASK)) |
| 2572 | s->flags |= SF_ERR_LOCAL; |
| 2573 | if (!(s->flags & SF_FINST_MASK)) |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2574 | s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2575 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2576 | free_trash_chunk(chunk); |
| 2577 | return 1; |
| 2578 | |
| 2579 | fail: |
| 2580 | /* If an error occurred, remove the incomplete HTTP response from the |
| 2581 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 2582 | channel_htx_truncate(res, htxbuf(&res->buf)); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2583 | free_trash_chunk(chunk); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2584 | return 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2585 | } |
| 2586 | |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2587 | /* Replace all headers matching the name <name>. The header value is replaced if |
| 2588 | * it matches the regex <re>. <str> is used for the replacement. If <full> is |
| 2589 | * set to 1, the full-line is matched and replaced. Otherwise, comma-separated |
| 2590 | * values are evaluated one by one. It returns 0 on success and -1 on error. |
| 2591 | */ |
| 2592 | int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name, |
| 2593 | const char *str, struct my_regex *re, int full) |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2594 | { |
| 2595 | struct http_hdr_ctx ctx; |
| 2596 | struct buffer *output = get_trash_chunk(); |
| 2597 | |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2598 | ctx.blk = NULL; |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2599 | while (http_find_header(htx, name, &ctx, full)) { |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2600 | if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0)) |
| 2601 | continue; |
| 2602 | |
| 2603 | output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch); |
| 2604 | if (output->data == -1) |
| 2605 | return -1; |
| 2606 | if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data))) |
| 2607 | return -1; |
| 2608 | } |
| 2609 | return 0; |
| 2610 | } |
| 2611 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2612 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 2613 | * takes the string from the variable 'replace' with length 'len', then modifies |
| 2614 | * the relevant part of the request line accordingly. Then it updates various |
| 2615 | * pointers to the next elements which were moved, and the total buffer length. |
| 2616 | * It finds the action to be performed in p[2], previously filled by function |
| 2617 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 2618 | * error, though this can be revisited when this code is finally exploited. |
| 2619 | * |
| 2620 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
Christopher Faulet | 312294f | 2020-09-02 17:17:44 +0200 | [diff] [blame] | 2621 | * query string, 3 to replace uri or 4 to replace the path+query. |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2622 | * |
| 2623 | * In query string case, the mark question '?' must be set at the start of the |
| 2624 | * string by the caller, event if the replacement query string is empty. |
| 2625 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2626 | int http_req_replace_stline(int action, const char *replace, int len, |
| 2627 | struct proxy *px, struct stream *s) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2628 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2629 | struct htx *htx = htxbuf(&s->req.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2630 | |
| 2631 | switch (action) { |
| 2632 | case 0: // method |
| 2633 | if (!http_replace_req_meth(htx, ist2(replace, len))) |
| 2634 | return -1; |
| 2635 | break; |
| 2636 | |
| 2637 | case 1: // path |
Christopher Faulet | b8ce505 | 2020-08-31 16:11:57 +0200 | [diff] [blame] | 2638 | if (!http_replace_req_path(htx, ist2(replace, len), 0)) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2639 | return -1; |
| 2640 | break; |
| 2641 | |
| 2642 | case 2: // query |
| 2643 | if (!http_replace_req_query(htx, ist2(replace, len))) |
| 2644 | return -1; |
| 2645 | break; |
| 2646 | |
| 2647 | case 3: // uri |
| 2648 | if (!http_replace_req_uri(htx, ist2(replace, len))) |
| 2649 | return -1; |
| 2650 | break; |
| 2651 | |
Christopher Faulet | 312294f | 2020-09-02 17:17:44 +0200 | [diff] [blame] | 2652 | case 4: // path + query |
| 2653 | if (!http_replace_req_path(htx, ist2(replace, len), 1)) |
| 2654 | return -1; |
| 2655 | break; |
| 2656 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2657 | default: |
| 2658 | return -1; |
| 2659 | } |
| 2660 | return 0; |
| 2661 | } |
| 2662 | |
| 2663 | /* This function replace the HTTP status code and the associated message. The |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2664 | * variable <status> contains the new status code. This function never fails. It |
| 2665 | * returns 0 in case of success, -1 in case of internal error. |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2666 | */ |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2667 | int http_res_set_status(unsigned int status, struct ist reason, struct stream *s) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2668 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2669 | struct htx *htx = htxbuf(&s->res.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2670 | char *res; |
| 2671 | |
| 2672 | chunk_reset(&trash); |
| 2673 | res = ultoa_o(status, trash.area, trash.size); |
| 2674 | trash.data = res - trash.area; |
| 2675 | |
| 2676 | /* Do we have a custom reason format string? */ |
Tim Duesterhus | e296d3e | 2020-03-05 17:56:31 +0100 | [diff] [blame] | 2677 | if (!isttest(reason)) { |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2678 | const char *str = http_get_reason(status); |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 2679 | reason = ist(str); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2680 | } |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2681 | |
Christopher Faulet | bde2c4c | 2020-08-31 16:43:34 +0200 | [diff] [blame] | 2682 | if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason)) |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2683 | return -1; |
| 2684 | return 0; |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2685 | } |
| 2686 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2687 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
| 2688 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 2689 | * further processing of the request (auth, deny, ...), and defaults to |
| 2690 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 2691 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 2692 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 2693 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 2694 | * status. |
| 2695 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2696 | static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, |
| 2697 | struct list *rules, struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2698 | { |
| 2699 | struct session *sess = strm_sess(s); |
| 2700 | struct http_txn *txn = s->txn; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2701 | struct act_rule *rule; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2702 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2703 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2704 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2705 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2706 | * resume condition. If a resume is needed it is always in the action |
| 2707 | * and never in the ACL or converters. In this case, we initialise the |
| 2708 | * current rule, and go to the action execution point. |
| 2709 | */ |
| 2710 | if (s->current_rule) { |
| 2711 | rule = s->current_rule; |
| 2712 | s->current_rule = NULL; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2713 | if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules)) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2714 | goto resume_execution; |
| 2715 | } |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2716 | s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2717 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2718 | restart: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2719 | /* start the ruleset evaluation in strict mode */ |
| 2720 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2721 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2722 | list_for_each_entry(rule, s->current_rule_list, list) { |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2723 | /* check optional condition */ |
| 2724 | if (rule->cond) { |
| 2725 | int ret; |
| 2726 | |
| 2727 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 2728 | ret = acl_pass(ret); |
| 2729 | |
| 2730 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2731 | ret = !ret; |
| 2732 | |
| 2733 | if (!ret) /* condition not matched */ |
| 2734 | continue; |
| 2735 | } |
| 2736 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2737 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2738 | resume_execution: |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 2739 | if (rule->kw->flags & KWF_EXPERIMENTAL) |
| 2740 | mark_tainted(TAINTED_ACTION_EXP_EXECUTED); |
| 2741 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2742 | /* Always call the action function if defined */ |
| 2743 | if (rule->action_ptr) { |
| 2744 | if ((s->req.flags & CF_READ_ERROR) || |
| 2745 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 2746 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2747 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2748 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2749 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2750 | case ACT_RET_CONT: |
| 2751 | break; |
| 2752 | case ACT_RET_STOP: |
| 2753 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2754 | s->last_rule_file = rule->conf.file; |
| 2755 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2756 | goto end; |
| 2757 | case ACT_RET_YIELD: |
| 2758 | s->current_rule = rule; |
| 2759 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2760 | goto end; |
| 2761 | case ACT_RET_ERR: |
| 2762 | rule_ret = HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2763 | s->last_rule_file = rule->conf.file; |
| 2764 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2765 | goto end; |
| 2766 | case ACT_RET_DONE: |
| 2767 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2768 | s->last_rule_file = rule->conf.file; |
| 2769 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2770 | goto end; |
| 2771 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 2772 | if (txn->status == -1) |
| 2773 | txn->status = 403; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2774 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2775 | s->last_rule_file = rule->conf.file; |
| 2776 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2777 | goto end; |
| 2778 | case ACT_RET_ABRT: |
| 2779 | rule_ret = HTTP_RULE_RES_ABRT; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2780 | s->last_rule_file = rule->conf.file; |
| 2781 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2782 | goto end; |
| 2783 | case ACT_RET_INV: |
| 2784 | rule_ret = HTTP_RULE_RES_BADREQ; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2785 | s->last_rule_file = rule->conf.file; |
| 2786 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2787 | goto end; |
| 2788 | } |
| 2789 | continue; /* eval the next rule */ |
| 2790 | } |
| 2791 | |
| 2792 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2793 | switch (rule->action) { |
| 2794 | case ACT_ACTION_ALLOW: |
| 2795 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2796 | s->last_rule_file = rule->conf.file; |
| 2797 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2798 | goto end; |
| 2799 | |
| 2800 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2801 | txn->status = rule->arg.http_reply->status; |
| 2802 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2803 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2804 | s->last_rule_file = rule->conf.file; |
| 2805 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2806 | goto end; |
| 2807 | |
| 2808 | case ACT_HTTP_REQ_TARPIT: |
| 2809 | txn->flags |= TX_CLTARPIT; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2810 | txn->status = rule->arg.http_reply->status; |
| 2811 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2812 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2813 | s->last_rule_file = rule->conf.file; |
| 2814 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2815 | goto end; |
| 2816 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2817 | case ACT_HTTP_REDIR: { |
| 2818 | int ret = http_apply_redirect_rule(rule->arg.redir, s, txn); |
| 2819 | |
| 2820 | if (ret == 2) // 2 == skip |
| 2821 | break; |
| 2822 | |
| 2823 | rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2824 | s->last_rule_file = rule->conf.file; |
| 2825 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2826 | goto end; |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2827 | } |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2828 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2829 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2830 | default: |
| 2831 | break; |
| 2832 | } |
| 2833 | } |
| 2834 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2835 | if (def_rules && s->current_rule_list == def_rules) { |
| 2836 | s->current_rule_list = rules; |
| 2837 | goto restart; |
| 2838 | } |
| 2839 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2840 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2841 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2842 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2843 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2844 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2845 | /* we reached the end of the rules, nothing to report */ |
| 2846 | return rule_ret; |
| 2847 | } |
| 2848 | |
| 2849 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 2850 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 2851 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 2852 | * is returned, the process can continue the evaluation of next rule list. If |
| 2853 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 2854 | * is returned, it means the operation could not be processed and a server error |
Christopher Faulet | a53abad | 2020-05-13 08:12:22 +0200 | [diff] [blame] | 2855 | * must be returned. If *YIELD is returned, the caller must call again the |
| 2856 | * function with the same context. |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2857 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2858 | static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules, |
| 2859 | struct list *rules, struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2860 | { |
| 2861 | struct session *sess = strm_sess(s); |
| 2862 | struct http_txn *txn = s->txn; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2863 | struct act_rule *rule; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2864 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2865 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2866 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2867 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2868 | * resume condition. If a resume is needed it is always in the action |
| 2869 | * and never in the ACL or converters. In this case, we initialise the |
| 2870 | * current rule, and go to the action execution point. |
| 2871 | */ |
| 2872 | if (s->current_rule) { |
| 2873 | rule = s->current_rule; |
| 2874 | s->current_rule = NULL; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2875 | if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules)) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2876 | goto resume_execution; |
| 2877 | } |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2878 | s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules); |
| 2879 | |
| 2880 | restart: |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2881 | |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2882 | /* start the ruleset evaluation in strict mode */ |
| 2883 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2884 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2885 | list_for_each_entry(rule, s->current_rule_list, list) { |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2886 | /* check optional condition */ |
| 2887 | if (rule->cond) { |
| 2888 | int ret; |
| 2889 | |
| 2890 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 2891 | ret = acl_pass(ret); |
| 2892 | |
| 2893 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2894 | ret = !ret; |
| 2895 | |
| 2896 | if (!ret) /* condition not matched */ |
| 2897 | continue; |
| 2898 | } |
| 2899 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2900 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2901 | resume_execution: |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 2902 | if (rule->kw->flags & KWF_EXPERIMENTAL) |
| 2903 | mark_tainted(TAINTED_ACTION_EXP_EXECUTED); |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2904 | |
| 2905 | /* Always call the action function if defined */ |
| 2906 | if (rule->action_ptr) { |
| 2907 | if ((s->req.flags & CF_READ_ERROR) || |
| 2908 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 2909 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2910 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2911 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2912 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2913 | case ACT_RET_CONT: |
| 2914 | break; |
| 2915 | case ACT_RET_STOP: |
| 2916 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2917 | s->last_rule_file = rule->conf.file; |
| 2918 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2919 | goto end; |
| 2920 | case ACT_RET_YIELD: |
| 2921 | s->current_rule = rule; |
| 2922 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2923 | goto end; |
| 2924 | case ACT_RET_ERR: |
| 2925 | rule_ret = HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2926 | s->last_rule_file = rule->conf.file; |
| 2927 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2928 | goto end; |
| 2929 | case ACT_RET_DONE: |
| 2930 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2931 | s->last_rule_file = rule->conf.file; |
| 2932 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2933 | goto end; |
| 2934 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 2935 | if (txn->status == -1) |
| 2936 | txn->status = 502; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2937 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2938 | s->last_rule_file = rule->conf.file; |
| 2939 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2940 | goto end; |
| 2941 | case ACT_RET_ABRT: |
| 2942 | rule_ret = HTTP_RULE_RES_ABRT; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2943 | s->last_rule_file = rule->conf.file; |
| 2944 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2945 | goto end; |
| 2946 | case ACT_RET_INV: |
| 2947 | rule_ret = HTTP_RULE_RES_BADREQ; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2948 | s->last_rule_file = rule->conf.file; |
| 2949 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2950 | goto end; |
| 2951 | } |
| 2952 | continue; /* eval the next rule */ |
| 2953 | } |
| 2954 | |
| 2955 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2956 | switch (rule->action) { |
| 2957 | case ACT_ACTION_ALLOW: |
| 2958 | rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2959 | s->last_rule_file = rule->conf.file; |
| 2960 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2961 | goto end; |
| 2962 | |
| 2963 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2964 | txn->status = rule->arg.http_reply->status; |
| 2965 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 2966 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2967 | s->last_rule_file = rule->conf.file; |
| 2968 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2969 | goto end; |
| 2970 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2971 | case ACT_HTTP_REDIR: { |
| 2972 | int ret = http_apply_redirect_rule(rule->arg.redir, s, txn); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2973 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2974 | if (ret == 2) // 2 == skip |
| 2975 | break; |
| 2976 | |
| 2977 | rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2978 | s->last_rule_file = rule->conf.file; |
| 2979 | s->last_rule_line = rule->conf.line; |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2980 | goto end; |
| 2981 | } |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2982 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2983 | default: |
| 2984 | break; |
| 2985 | } |
| 2986 | } |
| 2987 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2988 | if (def_rules && s->current_rule_list == def_rules) { |
| 2989 | s->current_rule_list = rules; |
| 2990 | goto restart; |
| 2991 | } |
| 2992 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2993 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2994 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2995 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2996 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2997 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2998 | /* we reached the end of the rules, nothing to report */ |
| 2999 | return rule_ret; |
| 3000 | } |
| 3001 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3002 | /* Executes backend and frontend http-after-response rules for the stream <s>, |
| 3003 | * in that order. it return 1 on success and 0 on error. It is the caller |
| 3004 | * responsibility to catch error or ignore it. If it catches it, this function |
| 3005 | * may be called a second time, for the internal error. |
| 3006 | */ |
| 3007 | int http_eval_after_res_rules(struct stream *s) |
| 3008 | { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 3009 | struct list *def_rules, *rules; |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3010 | struct session *sess = s->sess; |
| 3011 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 3012 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 3013 | /* Eval after-response ruleset only if the reply is not const */ |
| 3014 | if (s->txn->flags & TX_CONST_REPLY) |
| 3015 | goto end; |
| 3016 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3017 | /* prune the request variables if not already done and swap to the response variables. */ |
| 3018 | if (s->vars_reqres.scope != SCOPE_RES) { |
| 3019 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 3020 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | b7bfcb3 | 2021-08-31 08:13:25 +0200 | [diff] [blame] | 3021 | vars_init_head(&s->vars_reqres, SCOPE_RES); |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3022 | } |
| 3023 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 3024 | def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL); |
| 3025 | rules = &s->be->http_after_res_rules; |
| 3026 | |
| 3027 | ret = http_res_get_intercept_rule(s->be, def_rules, rules, s); |
Christopher Faulet | 4c5a591 | 2021-11-09 17:48:39 +0100 | [diff] [blame] | 3028 | if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 3029 | def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL); |
| 3030 | rules = &sess->fe->http_after_res_rules; |
| 3031 | ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s); |
| 3032 | } |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3033 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 3034 | end: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3035 | /* All other codes than CONTINUE, STOP or DONE are forbidden */ |
| 3036 | return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE); |
| 3037 | } |
| 3038 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3039 | /* |
| 3040 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 3041 | * desirable to call it only when needed. This code is quite complex because |
| 3042 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 3043 | * highly recommended not to touch this part without a good reason ! |
| 3044 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3045 | static void http_manage_client_side_cookies(struct stream *s, struct channel *req) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3046 | { |
| 3047 | struct session *sess = s->sess; |
| 3048 | struct http_txn *txn = s->txn; |
| 3049 | struct htx *htx; |
| 3050 | struct http_hdr_ctx ctx; |
| 3051 | char *hdr_beg, *hdr_end, *del_from; |
| 3052 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
| 3053 | int preserve_hdr; |
| 3054 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3055 | htx = htxbuf(&req->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3056 | ctx.blk = NULL; |
| 3057 | while (http_find_header(htx, ist("Cookie"), &ctx, 1)) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3058 | int is_first = 1; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3059 | del_from = NULL; /* nothing to be deleted */ |
| 3060 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 3061 | |
| 3062 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3063 | * attributes whose name begin with a '$', and associate them with |
| 3064 | * the right cookie, if we want to delete this cookie. |
| 3065 | * So there are 3 cases for each cookie read : |
| 3066 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3067 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3068 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3069 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3070 | * "special" cookie. |
| 3071 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3072 | * remove it. If no application cookie persists in the header, we |
| 3073 | * *MUST* delete it. |
| 3074 | * |
| 3075 | * Note: RFC2965 is unclear about the processing of spaces around |
| 3076 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 3077 | * the RFC explicitly allows spaces before it, and not within the |
| 3078 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 3079 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 3080 | * after the equal sign too, resulting in some (rare) buggy |
| 3081 | * implementations trying to do that. So let's do what servers do. |
| 3082 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 3083 | * allowed quoted strings in values, with any possible character |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3084 | * after a backslash, including control chars and delimiters, which |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3085 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 3086 | * within values even without quotes. |
| 3087 | * |
| 3088 | * We have to keep multiple pointers in order to support cookie |
| 3089 | * removal at the beginning, middle or end of header without |
| 3090 | * corrupting the header. All of these headers are valid : |
| 3091 | * |
| 3092 | * hdr_beg hdr_end |
| 3093 | * | | |
| 3094 | * v | |
| 3095 | * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 | |
| 3096 | * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v |
| 3097 | * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3 |
| 3098 | * | | | | | | | |
| 3099 | * | | | | | | | |
| 3100 | * | | | | | | +--> next |
| 3101 | * | | | | | +----> val_end |
| 3102 | * | | | | +-----------> val_beg |
| 3103 | * | | | +--------------> equal |
| 3104 | * | | +----------------> att_end |
| 3105 | * | +---------------------> att_beg |
| 3106 | * +--------------------------> prev |
| 3107 | * |
| 3108 | */ |
| 3109 | hdr_beg = ctx.value.ptr; |
| 3110 | hdr_end = hdr_beg + ctx.value.len; |
| 3111 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3112 | /* Iterate through all cookies on this line */ |
| 3113 | |
| 3114 | /* find att_beg */ |
| 3115 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3116 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3117 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3118 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3119 | |
| 3120 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3121 | att_beg++; |
| 3122 | |
| 3123 | /* find att_end : this is the first character after the last non |
| 3124 | * space before the equal. It may be equal to hdr_end. |
| 3125 | */ |
| 3126 | equal = att_end = att_beg; |
| 3127 | while (equal < hdr_end) { |
| 3128 | if (*equal == '=' || *equal == ',' || *equal == ';') |
| 3129 | break; |
| 3130 | if (HTTP_IS_SPHT(*equal++)) |
| 3131 | continue; |
| 3132 | att_end = equal; |
| 3133 | } |
| 3134 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3135 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3136 | * is between <att_beg> and <equal>, both may be identical. |
| 3137 | */ |
| 3138 | /* look for end of cookie if there is an equal sign */ |
| 3139 | if (equal < hdr_end && *equal == '=') { |
| 3140 | /* look for the beginning of the value */ |
| 3141 | val_beg = equal + 1; |
| 3142 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3143 | val_beg++; |
| 3144 | |
| 3145 | /* find the end of the value, respecting quotes */ |
| 3146 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3147 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3148 | /* make val_end point to the first white space or delimiter after the value */ |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3149 | val_end = next; |
| 3150 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3151 | val_end--; |
| 3152 | } |
| 3153 | else |
| 3154 | val_beg = val_end = next = equal; |
| 3155 | |
| 3156 | /* We have nothing to do with attributes beginning with |
| 3157 | * '$'. However, they will automatically be removed if a |
| 3158 | * header before them is removed, since they're supposed |
| 3159 | * to be linked together. |
| 3160 | */ |
| 3161 | if (*att_beg == '$') |
| 3162 | continue; |
| 3163 | |
| 3164 | /* Ignore cookies with no equal sign */ |
| 3165 | if (equal == next) { |
| 3166 | /* This is not our cookie, so we must preserve it. But if we already |
| 3167 | * scheduled another cookie for removal, we cannot remove the |
| 3168 | * complete header, but we can remove the previous block itself. |
| 3169 | */ |
| 3170 | preserve_hdr = 1; |
| 3171 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3172 | int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3173 | val_end += delta; |
| 3174 | next += delta; |
| 3175 | hdr_end += delta; |
| 3176 | prev = del_from; |
| 3177 | del_from = NULL; |
| 3178 | } |
| 3179 | continue; |
| 3180 | } |
| 3181 | |
| 3182 | /* if there are spaces around the equal sign, we need to |
| 3183 | * strip them otherwise we'll get trouble for cookie captures, |
| 3184 | * or even for rewrites. Since this happens extremely rarely, |
| 3185 | * it does not hurt performance. |
| 3186 | */ |
| 3187 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3188 | int stripped_before = 0; |
| 3189 | int stripped_after = 0; |
| 3190 | |
| 3191 | if (att_end != equal) { |
| 3192 | memmove(att_end, equal, hdr_end - equal); |
| 3193 | stripped_before = (att_end - equal); |
| 3194 | equal += stripped_before; |
| 3195 | val_beg += stripped_before; |
| 3196 | } |
| 3197 | |
| 3198 | if (val_beg > equal + 1) { |
| 3199 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3200 | stripped_after = (equal + 1) - val_beg; |
| 3201 | val_beg += stripped_after; |
| 3202 | stripped_before += stripped_after; |
| 3203 | } |
| 3204 | |
| 3205 | val_end += stripped_before; |
| 3206 | next += stripped_before; |
| 3207 | hdr_end += stripped_before; |
| 3208 | } |
| 3209 | /* now everything is as on the diagram above */ |
| 3210 | |
| 3211 | /* First, let's see if we want to capture this cookie. We check |
| 3212 | * that we don't already have a client side cookie, because we |
| 3213 | * can only capture one. Also as an optimisation, we ignore |
| 3214 | * cookies shorter than the declared name. |
| 3215 | */ |
| 3216 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 3217 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3218 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3219 | int log_len = val_end - att_beg; |
| 3220 | |
| 3221 | if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3222 | ha_alert("HTTP logging : out of memory.\n"); |
| 3223 | } else { |
| 3224 | if (log_len > sess->fe->capture_len) |
| 3225 | log_len = sess->fe->capture_len; |
| 3226 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 3227 | txn->cli_cookie[log_len] = 0; |
| 3228 | } |
| 3229 | } |
| 3230 | |
| 3231 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 3232 | * following form : |
| 3233 | * |
| 3234 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
| 3235 | * |
| 3236 | * For cookies in prefix mode, the form is : |
| 3237 | * |
| 3238 | * Cookie: NAME=SRV~VALUE |
| 3239 | */ |
| 3240 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3241 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3242 | struct server *srv = s->be->srv; |
| 3243 | char *delim; |
| 3244 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3245 | /* if we're in cookie prefix mode, we'll search the delimiter so that we |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3246 | * have the server ID between val_beg and delim, and the original cookie between |
| 3247 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 3248 | * |
| 3249 | * hdr_beg |
| 3250 | * | |
| 3251 | * v |
| 3252 | * NAME=SRV; # in all but prefix modes |
| 3253 | * NAME=SRV~OPAQUE ; # in prefix mode |
| 3254 | * || || | |+-> next |
| 3255 | * || || | +--> val_end |
| 3256 | * || || +---------> delim |
| 3257 | * || |+------------> val_beg |
| 3258 | * || +-------------> att_end = equal |
| 3259 | * |+-----------------> att_beg |
| 3260 | * +------------------> prev |
| 3261 | * |
| 3262 | */ |
| 3263 | if (s->be->ck_opts & PR_CK_PFX) { |
| 3264 | for (delim = val_beg; delim < val_end; delim++) |
| 3265 | if (*delim == COOKIE_DELIM) |
| 3266 | break; |
| 3267 | } |
| 3268 | else { |
| 3269 | char *vbar1; |
| 3270 | delim = val_end; |
| 3271 | /* Now check if the cookie contains a date field, which would |
| 3272 | * appear after a vertical bar ('|') just after the server name |
| 3273 | * and before the delimiter. |
| 3274 | */ |
| 3275 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 3276 | if (vbar1) { |
| 3277 | /* OK, so left of the bar is the server's cookie and |
| 3278 | * right is the last seen date. It is a base64 encoded |
| 3279 | * 30-bit value representing the UNIX date since the |
| 3280 | * epoch in 4-second quantities. |
| 3281 | */ |
| 3282 | int val; |
| 3283 | delim = vbar1++; |
| 3284 | if (val_end - vbar1 >= 5) { |
| 3285 | val = b64tos30(vbar1); |
| 3286 | if (val > 0) |
| 3287 | txn->cookie_last_date = val << 2; |
| 3288 | } |
| 3289 | /* look for a second vertical bar */ |
| 3290 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 3291 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 3292 | val = b64tos30(vbar1 + 1); |
| 3293 | if (val > 0) |
| 3294 | txn->cookie_first_date = val << 2; |
| 3295 | } |
| 3296 | } |
| 3297 | } |
| 3298 | |
| 3299 | /* if the cookie has an expiration date and the proxy wants to check |
| 3300 | * it, then we do that now. We first check if the cookie is too old, |
| 3301 | * then only if it has expired. We detect strict overflow because the |
| 3302 | * time resolution here is not great (4 seconds). Cookies with dates |
| 3303 | * in the future are ignored if their offset is beyond one day. This |
| 3304 | * allows an admin to fix timezone issues without expiring everyone |
| 3305 | * and at the same time avoids keeping unwanted side effects for too |
| 3306 | * long. |
| 3307 | */ |
| 3308 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 3309 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) || |
| 3310 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
| 3311 | txn->flags &= ~TX_CK_MASK; |
| 3312 | txn->flags |= TX_CK_OLD; |
| 3313 | delim = val_beg; // let's pretend we have not found the cookie |
| 3314 | txn->cookie_first_date = 0; |
| 3315 | txn->cookie_last_date = 0; |
| 3316 | } |
| 3317 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 3318 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) || |
| 3319 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
| 3320 | txn->flags &= ~TX_CK_MASK; |
| 3321 | txn->flags |= TX_CK_EXPIRED; |
| 3322 | delim = val_beg; // let's pretend we have not found the cookie |
| 3323 | txn->cookie_first_date = 0; |
| 3324 | txn->cookie_last_date = 0; |
| 3325 | } |
| 3326 | |
| 3327 | /* Here, we'll look for the first running server which supports the cookie. |
| 3328 | * This allows to share a same cookie between several servers, for example |
| 3329 | * to dedicate backup servers to specific servers only. |
| 3330 | * However, to prevent clients from sticking to cookie-less backup server |
| 3331 | * when they have incidentely learned an empty cookie, we simply ignore |
| 3332 | * empty cookies and mark them as invalid. |
| 3333 | * The same behaviour is applied when persistence must be ignored. |
| 3334 | */ |
| 3335 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3336 | srv = NULL; |
| 3337 | |
| 3338 | while (srv) { |
| 3339 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 3340 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 3341 | if ((srv->cur_state != SRV_ST_STOPPED) || |
| 3342 | (s->be->options & PR_O_PERSIST) || |
| 3343 | (s->flags & SF_FORCE_PRST)) { |
| 3344 | /* we found the server and we can use it */ |
| 3345 | txn->flags &= ~TX_CK_MASK; |
| 3346 | txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN; |
| 3347 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
| 3348 | s->target = &srv->obj_type; |
| 3349 | break; |
| 3350 | } else { |
| 3351 | /* we found a server, but it's down, |
| 3352 | * mark it as such and go on in case |
| 3353 | * another one is available. |
| 3354 | */ |
| 3355 | txn->flags &= ~TX_CK_MASK; |
| 3356 | txn->flags |= TX_CK_DOWN; |
| 3357 | } |
| 3358 | } |
| 3359 | srv = srv->next; |
| 3360 | } |
| 3361 | |
| 3362 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
| 3363 | /* no server matched this cookie or we deliberately skipped it */ |
| 3364 | txn->flags &= ~TX_CK_MASK; |
| 3365 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3366 | txn->flags |= TX_CK_UNUSED; |
| 3367 | else |
| 3368 | txn->flags |= TX_CK_INVALID; |
| 3369 | } |
| 3370 | |
| 3371 | /* depending on the cookie mode, we may have to either : |
| 3372 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3373 | * the server never sees it ; |
| 3374 | * - remove the server id from the cookie value, and tag the cookie as an |
Joseph Herlant | e9d5c72 | 2018-11-25 11:00:25 -0800 | [diff] [blame] | 3375 | * application cookie so that it does not get accidentally removed later, |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3376 | * if we're in cookie prefix mode |
| 3377 | */ |
| 3378 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
| 3379 | int delta; /* negative */ |
| 3380 | |
| 3381 | memmove(val_beg, delim + 1, hdr_end - (delim + 1)); |
| 3382 | delta = val_beg - (delim + 1); |
| 3383 | val_end += delta; |
| 3384 | next += delta; |
| 3385 | hdr_end += delta; |
| 3386 | del_from = NULL; |
| 3387 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 3388 | } |
| 3389 | else if (del_from == NULL && |
| 3390 | (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) { |
| 3391 | del_from = prev; |
| 3392 | } |
| 3393 | } |
| 3394 | else { |
| 3395 | /* This is not our cookie, so we must preserve it. But if we already |
| 3396 | * scheduled another cookie for removal, we cannot remove the |
| 3397 | * complete header, but we can remove the previous block itself. |
| 3398 | */ |
| 3399 | preserve_hdr = 1; |
| 3400 | |
| 3401 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3402 | int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3403 | if (att_beg >= del_from) |
| 3404 | att_beg += delta; |
| 3405 | if (att_end >= del_from) |
| 3406 | att_end += delta; |
| 3407 | val_beg += delta; |
| 3408 | val_end += delta; |
| 3409 | next += delta; |
| 3410 | hdr_end += delta; |
| 3411 | prev = del_from; |
| 3412 | del_from = NULL; |
| 3413 | } |
| 3414 | } |
| 3415 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3416 | } /* for each cookie */ |
| 3417 | |
| 3418 | |
| 3419 | /* There are no more cookies on this line. |
| 3420 | * We may still have one (or several) marked for deletion at the |
| 3421 | * end of the line. We must do this now in two ways : |
| 3422 | * - if some cookies must be preserved, we only delete from the |
| 3423 | * mark to the end of line ; |
| 3424 | * - if nothing needs to be preserved, simply delete the whole header |
| 3425 | */ |
| 3426 | if (del_from) { |
| 3427 | hdr_end = (preserve_hdr ? del_from : hdr_beg); |
| 3428 | } |
| 3429 | if ((hdr_end - hdr_beg) != ctx.value.len) { |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3430 | if (hdr_beg != hdr_end) |
| 3431 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3432 | else |
| 3433 | http_remove_header(htx, &ctx); |
| 3434 | } |
| 3435 | } /* for each "Cookie header */ |
| 3436 | } |
| 3437 | |
| 3438 | /* |
| 3439 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 3440 | * desirable to call it only when needed. This function is also used when we |
| 3441 | * just need to know if there is a cookie (eg: for check-cache). |
| 3442 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3443 | static void http_manage_server_side_cookies(struct stream *s, struct channel *res) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3444 | { |
| 3445 | struct session *sess = s->sess; |
| 3446 | struct http_txn *txn = s->txn; |
| 3447 | struct htx *htx; |
| 3448 | struct http_hdr_ctx ctx; |
| 3449 | struct server *srv; |
| 3450 | char *hdr_beg, *hdr_end; |
| 3451 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 6f7a02a | 2019-04-15 21:49:49 +0200 | [diff] [blame] | 3452 | int is_cookie2 = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3453 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3454 | htx = htxbuf(&res->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3455 | |
| 3456 | ctx.blk = NULL; |
| 3457 | while (1) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3458 | int is_first = 1; |
| 3459 | |
Andrew McDermott | bfb15ab | 2022-02-11 18:26:49 +0000 | [diff] [blame] | 3460 | if (is_cookie2 || !http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) { |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3461 | if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1)) |
| 3462 | break; |
| 3463 | is_cookie2 = 1; |
| 3464 | } |
| 3465 | |
| 3466 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 3467 | * <prev> points to the colon. |
| 3468 | */ |
| 3469 | txn->flags |= TX_SCK_PRESENT; |
| 3470 | |
| 3471 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 3472 | * check-cache is enabled) and we are not interested in checking |
| 3473 | * them. Warning, the cookie capture is declared in the frontend. |
| 3474 | */ |
| 3475 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
| 3476 | break; |
| 3477 | |
| 3478 | /* OK so now we know we have to process this response cookie. |
| 3479 | * The format of the Set-Cookie header is slightly different |
| 3480 | * from the format of the Cookie header in that it does not |
| 3481 | * support the comma as a cookie delimiter (thus the header |
| 3482 | * cannot be folded) because the Expires attribute described in |
| 3483 | * the original Netscape's spec may contain an unquoted date |
| 3484 | * with a comma inside. We have to live with this because |
| 3485 | * many browsers don't support Max-Age and some browsers don't |
| 3486 | * support quoted strings. However the Set-Cookie2 header is |
| 3487 | * clean. |
| 3488 | * |
| 3489 | * We have to keep multiple pointers in order to support cookie |
| 3490 | * removal at the beginning, middle or end of header without |
| 3491 | * corrupting the header (in case of set-cookie2). A special |
| 3492 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 3493 | * fields after the first semi-colon. The <next> pointer points |
| 3494 | * either to the end of line (set-cookie) or next unquoted comma |
| 3495 | * (set-cookie2). All of these headers are valid : |
| 3496 | * |
| 3497 | * hdr_beg hdr_end |
| 3498 | * | | |
| 3499 | * v | |
| 3500 | * NAME1 = VALUE 1 ; Secure; Path="/" | |
| 3501 | * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v |
| 3502 | * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT |
| 3503 | * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard |
| 3504 | * | | | | | | | | |
| 3505 | * | | | | | | | +-> next |
| 3506 | * | | | | | | +------------> scav |
| 3507 | * | | | | | +--------------> val_end |
| 3508 | * | | | | +--------------------> val_beg |
| 3509 | * | | | +----------------------> equal |
| 3510 | * | | +------------------------> att_end |
| 3511 | * | +----------------------------> att_beg |
| 3512 | * +------------------------------> prev |
| 3513 | * -------------------------------> hdr_beg |
| 3514 | */ |
| 3515 | hdr_beg = ctx.value.ptr; |
| 3516 | hdr_end = hdr_beg + ctx.value.len; |
| 3517 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3518 | |
| 3519 | /* Iterate through all cookies on this line */ |
| 3520 | |
| 3521 | /* find att_beg */ |
| 3522 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3523 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3524 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3525 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3526 | |
| 3527 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3528 | att_beg++; |
| 3529 | |
| 3530 | /* find att_end : this is the first character after the last non |
| 3531 | * space before the equal. It may be equal to hdr_end. |
| 3532 | */ |
| 3533 | equal = att_end = att_beg; |
| 3534 | |
| 3535 | while (equal < hdr_end) { |
| 3536 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 3537 | break; |
| 3538 | if (HTTP_IS_SPHT(*equal++)) |
| 3539 | continue; |
| 3540 | att_end = equal; |
| 3541 | } |
| 3542 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3543 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3544 | * is between <att_beg> and <equal>, both may be identical. |
| 3545 | */ |
| 3546 | |
| 3547 | /* look for end of cookie if there is an equal sign */ |
| 3548 | if (equal < hdr_end && *equal == '=') { |
| 3549 | /* look for the beginning of the value */ |
| 3550 | val_beg = equal + 1; |
| 3551 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3552 | val_beg++; |
| 3553 | |
| 3554 | /* find the end of the value, respecting quotes */ |
| 3555 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3556 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3557 | /* make val_end point to the first white space or delimiter after the value */ |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3558 | val_end = next; |
| 3559 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3560 | val_end--; |
| 3561 | } |
| 3562 | else { |
| 3563 | /* <equal> points to next comma, semi-colon or EOL */ |
| 3564 | val_beg = val_end = next = equal; |
| 3565 | } |
| 3566 | |
| 3567 | if (next < hdr_end) { |
| 3568 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 3569 | * a colon or semi-colon before the end. So skip all attr-value |
| 3570 | * pairs and look for the next comma. For Set-Cookie, since |
| 3571 | * commas are permitted in values, skip to the end. |
| 3572 | */ |
| 3573 | if (is_cookie2) |
| 3574 | next = http_find_hdr_value_end(next, hdr_end); |
| 3575 | else |
| 3576 | next = hdr_end; |
| 3577 | } |
| 3578 | |
| 3579 | /* Now everything is as on the diagram above */ |
| 3580 | |
| 3581 | /* Ignore cookies with no equal sign */ |
| 3582 | if (equal == val_end) |
| 3583 | continue; |
| 3584 | |
| 3585 | /* If there are spaces around the equal sign, we need to |
| 3586 | * strip them otherwise we'll get trouble for cookie captures, |
| 3587 | * or even for rewrites. Since this happens extremely rarely, |
| 3588 | * it does not hurt performance. |
| 3589 | */ |
| 3590 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3591 | int stripped_before = 0; |
| 3592 | int stripped_after = 0; |
| 3593 | |
| 3594 | if (att_end != equal) { |
| 3595 | memmove(att_end, equal, hdr_end - equal); |
| 3596 | stripped_before = (att_end - equal); |
| 3597 | equal += stripped_before; |
| 3598 | val_beg += stripped_before; |
| 3599 | } |
| 3600 | |
| 3601 | if (val_beg > equal + 1) { |
| 3602 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3603 | stripped_after = (equal + 1) - val_beg; |
| 3604 | val_beg += stripped_after; |
| 3605 | stripped_before += stripped_after; |
| 3606 | } |
| 3607 | |
| 3608 | val_end += stripped_before; |
| 3609 | next += stripped_before; |
| 3610 | hdr_end += stripped_before; |
| 3611 | |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3612 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3613 | ctx.value.len = hdr_end - hdr_beg; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3614 | } |
| 3615 | |
| 3616 | /* First, let's see if we want to capture this cookie. We check |
| 3617 | * that we don't already have a server side cookie, because we |
| 3618 | * can only capture one. Also as an optimisation, we ignore |
| 3619 | * cookies shorter than the declared name. |
| 3620 | */ |
| 3621 | if (sess->fe->capture_name != NULL && |
| 3622 | txn->srv_cookie == NULL && |
| 3623 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3624 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3625 | int log_len = val_end - att_beg; |
| 3626 | if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3627 | ha_alert("HTTP logging : out of memory.\n"); |
| 3628 | } |
| 3629 | else { |
| 3630 | if (log_len > sess->fe->capture_len) |
| 3631 | log_len = sess->fe->capture_len; |
| 3632 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 3633 | txn->srv_cookie[log_len] = 0; |
| 3634 | } |
| 3635 | } |
| 3636 | |
| 3637 | srv = objt_server(s->target); |
| 3638 | /* now check if we need to process it for persistence */ |
| 3639 | if (!(s->flags & SF_IGNORE_PRST) && |
| 3640 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3641 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3642 | /* assume passive cookie by default */ |
| 3643 | txn->flags &= ~TX_SCK_MASK; |
| 3644 | txn->flags |= TX_SCK_FOUND; |
| 3645 | |
| 3646 | /* If the cookie is in insert mode on a known server, we'll delete |
| 3647 | * this occurrence because we'll insert another one later. |
| 3648 | * We'll delete it too if the "indirect" option is set and we're in |
| 3649 | * a direct access. |
| 3650 | */ |
| 3651 | if (s->be->ck_opts & PR_CK_PSV) { |
| 3652 | /* The "preserve" flag was set, we don't want to touch the |
| 3653 | * server's cookie. |
| 3654 | */ |
| 3655 | } |
| 3656 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
| 3657 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
| 3658 | /* this cookie must be deleted */ |
| 3659 | if (prev == hdr_beg && next == hdr_end) { |
| 3660 | /* whole header */ |
| 3661 | http_remove_header(htx, &ctx); |
| 3662 | /* note: while both invalid now, <next> and <hdr_end> |
| 3663 | * are still equal, so the for() will stop as expected. |
| 3664 | */ |
| 3665 | } else { |
| 3666 | /* just remove the value */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3667 | int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3668 | next = prev; |
| 3669 | hdr_end += delta; |
| 3670 | } |
| 3671 | txn->flags &= ~TX_SCK_MASK; |
| 3672 | txn->flags |= TX_SCK_DELETED; |
| 3673 | /* and go on with next cookie */ |
| 3674 | } |
| 3675 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
| 3676 | /* replace bytes val_beg->val_end with the cookie name associated |
| 3677 | * with this server since we know it. |
| 3678 | */ |
| 3679 | int sliding, delta; |
| 3680 | |
| 3681 | ctx.value = ist2(val_beg, val_end - val_beg); |
| 3682 | ctx.lws_before = ctx.lws_after = 0; |
| 3683 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen)); |
| 3684 | delta = srv->cklen - (val_end - val_beg); |
| 3685 | sliding = (ctx.value.ptr - val_beg); |
| 3686 | hdr_beg += sliding; |
| 3687 | val_beg += sliding; |
| 3688 | next += sliding + delta; |
| 3689 | hdr_end += sliding + delta; |
| 3690 | |
| 3691 | txn->flags &= ~TX_SCK_MASK; |
| 3692 | txn->flags |= TX_SCK_REPLACED; |
| 3693 | } |
| 3694 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
| 3695 | /* insert the cookie name associated with this server |
| 3696 | * before existing cookie, and insert a delimiter between them.. |
| 3697 | */ |
| 3698 | int sliding, delta; |
| 3699 | ctx.value = ist2(val_beg, 0); |
| 3700 | ctx.lws_before = ctx.lws_after = 0; |
| 3701 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1)); |
| 3702 | delta = srv->cklen + 1; |
| 3703 | sliding = (ctx.value.ptr - val_beg); |
| 3704 | hdr_beg += sliding; |
| 3705 | val_beg += sliding; |
| 3706 | next += sliding + delta; |
| 3707 | hdr_end += sliding + delta; |
| 3708 | |
| 3709 | val_beg[srv->cklen] = COOKIE_DELIM; |
| 3710 | txn->flags &= ~TX_SCK_MASK; |
| 3711 | txn->flags |= TX_SCK_REPLACED; |
| 3712 | } |
| 3713 | } |
| 3714 | /* that's done for this cookie, check the next one on the same |
| 3715 | * line when next != hdr_end (only if is_cookie2). |
| 3716 | */ |
| 3717 | } |
| 3718 | } |
| 3719 | } |
| 3720 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3721 | /* |
| 3722 | * Parses the Cache-Control and Pragma request header fields to determine if |
| 3723 | * the request may be served from the cache and/or if it is cacheable. Updates |
| 3724 | * s->txn->flags. |
| 3725 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3726 | void http_check_request_for_cacheability(struct stream *s, struct channel *req) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3727 | { |
| 3728 | struct http_txn *txn = s->txn; |
| 3729 | struct htx *htx; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3730 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 3731 | int pragma_found, cc_found; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3732 | |
| 3733 | if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE) |
| 3734 | return; /* nothing more to do here */ |
| 3735 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3736 | htx = htxbuf(&req->buf); |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3737 | pragma_found = cc_found = 0; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3738 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3739 | /* Check "pragma" header for HTTP/1.0 compatibility. */ |
| 3740 | if (http_find_header(htx, ist("pragma"), &ctx, 1)) { |
| 3741 | if (isteqi(ctx.value, ist("no-cache"))) { |
| 3742 | pragma_found = 1; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3743 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3744 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3745 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3746 | ctx.blk = NULL; |
| 3747 | /* Don't use the cache and don't try to store if we found the |
| 3748 | * Authorization header */ |
| 3749 | if (http_find_header(htx, ist("authorization"), &ctx, 1)) { |
| 3750 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3751 | txn->flags |= TX_CACHE_IGNORE; |
| 3752 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3753 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3754 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3755 | /* Look for "cache-control" header and iterate over all the values |
| 3756 | * until we find one that specifies that caching is possible or not. */ |
| 3757 | ctx.blk = NULL; |
| 3758 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3759 | cc_found = 1; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3760 | /* We don't check the values after max-age, max-stale nor min-fresh, |
| 3761 | * we simply don't use the cache when they're specified. */ |
| 3762 | if (istmatchi(ctx.value, ist("max-age")) || |
| 3763 | istmatchi(ctx.value, ist("no-cache")) || |
| 3764 | istmatchi(ctx.value, ist("max-stale")) || |
| 3765 | istmatchi(ctx.value, ist("min-fresh"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3766 | txn->flags |= TX_CACHE_IGNORE; |
| 3767 | continue; |
| 3768 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3769 | if (istmatchi(ctx.value, ist("no-store"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3770 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3771 | continue; |
| 3772 | } |
| 3773 | } |
| 3774 | |
| 3775 | /* RFC7234#5.4: |
| 3776 | * When the Cache-Control header field is also present and |
| 3777 | * understood in a request, Pragma is ignored. |
| 3778 | * When the Cache-Control header field is not present in a |
| 3779 | * request, caches MUST consider the no-cache request |
| 3780 | * pragma-directive as having the same effect as if |
| 3781 | * "Cache-Control: no-cache" were present. |
| 3782 | */ |
| 3783 | if (!cc_found && pragma_found) |
| 3784 | txn->flags |= TX_CACHE_IGNORE; |
| 3785 | } |
| 3786 | |
| 3787 | /* |
| 3788 | * Check if response is cacheable or not. Updates s->txn->flags. |
| 3789 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3790 | void http_check_response_for_cacheability(struct stream *s, struct channel *res) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3791 | { |
| 3792 | struct http_txn *txn = s->txn; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3793 | struct http_hdr_ctx ctx = { .blk = NULL }; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3794 | struct htx *htx; |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3795 | int has_freshness_info = 0; |
| 3796 | int has_validator = 0; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3797 | |
| 3798 | if (txn->status < 200) { |
| 3799 | /* do not try to cache interim responses! */ |
| 3800 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3801 | return; |
| 3802 | } |
| 3803 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3804 | htx = htxbuf(&res->buf); |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3805 | /* Check "pragma" header for HTTP/1.0 compatibility. */ |
| 3806 | if (http_find_header(htx, ist("pragma"), &ctx, 1)) { |
| 3807 | if (isteqi(ctx.value, ist("no-cache"))) { |
| 3808 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3809 | return; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3810 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3811 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3812 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3813 | /* Look for "cache-control" header and iterate over all the values |
| 3814 | * until we find one that specifies that caching is possible or not. */ |
| 3815 | ctx.blk = NULL; |
| 3816 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
| 3817 | if (isteqi(ctx.value, ist("public"))) { |
| 3818 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3819 | continue; |
| 3820 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3821 | if (isteqi(ctx.value, ist("private")) || |
| 3822 | isteqi(ctx.value, ist("no-cache")) || |
| 3823 | isteqi(ctx.value, ist("no-store")) || |
| 3824 | isteqi(ctx.value, ist("max-age=0")) || |
| 3825 | isteqi(ctx.value, ist("s-maxage=0"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3826 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3827 | continue; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3828 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3829 | /* We might have a no-cache="set-cookie" form. */ |
| 3830 | if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) { |
| 3831 | txn->flags &= ~TX_CACHE_COOK; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3832 | continue; |
| 3833 | } |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3834 | |
| 3835 | if (istmatchi(ctx.value, ist("s-maxage")) || |
| 3836 | istmatchi(ctx.value, ist("max-age"))) { |
| 3837 | has_freshness_info = 1; |
| 3838 | continue; |
| 3839 | } |
| 3840 | } |
| 3841 | |
| 3842 | /* If no freshness information could be found in Cache-Control values, |
| 3843 | * look for an Expires header. */ |
| 3844 | if (!has_freshness_info) { |
| 3845 | ctx.blk = NULL; |
| 3846 | has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0); |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3847 | } |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3848 | |
| 3849 | /* If no freshness information could be found in Cache-Control or Expires |
| 3850 | * values, look for an explicit validator. */ |
| 3851 | if (!has_freshness_info) { |
| 3852 | ctx.blk = NULL; |
| 3853 | has_validator = 1; |
| 3854 | if (!http_find_header(htx, ist("etag"), &ctx, 0)) { |
| 3855 | ctx.blk = NULL; |
| 3856 | if (!http_find_header(htx, ist("last-modified"), &ctx, 0)) |
| 3857 | has_validator = 0; |
| 3858 | } |
| 3859 | } |
| 3860 | |
| 3861 | /* We won't store an entry that has neither a cache validator nor an |
| 3862 | * explicit expiration time, as suggested in RFC 7234#3. */ |
| 3863 | if (!has_freshness_info && !has_validator) |
| 3864 | txn->flags |= TX_CACHE_IGNORE; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3865 | } |
| 3866 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3867 | /* |
| 3868 | * In a GET, HEAD or POST request, check if the requested URI matches the stats uri |
| 3869 | * for the current backend. |
| 3870 | * |
| 3871 | * It is assumed that the request is either a HEAD, GET, or POST and that the |
| 3872 | * uri_auth field is valid. |
| 3873 | * |
| 3874 | * Returns 1 if stats should be provided, otherwise 0. |
| 3875 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3876 | static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend) |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3877 | { |
| 3878 | struct uri_auth *uri_auth = backend->uri_auth; |
| 3879 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3880 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3881 | struct ist uri; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3882 | |
| 3883 | if (!uri_auth) |
| 3884 | return 0; |
| 3885 | |
| 3886 | if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST) |
| 3887 | return 0; |
| 3888 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3889 | htx = htxbuf(&s->req.buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 3890 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3891 | uri = htx_sl_req_uri(sl); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 3892 | if (*uri_auth->uri_prefix == '/') { |
| 3893 | struct http_uri_parser parser = http_uri_parser_init(uri); |
| 3894 | uri = http_parse_path(&parser); |
| 3895 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3896 | |
| 3897 | /* check URI size */ |
| 3898 | if (uri_auth->uri_len > uri.len) |
| 3899 | return 0; |
| 3900 | |
| 3901 | if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
| 3902 | return 0; |
| 3903 | |
| 3904 | return 1; |
| 3905 | } |
| 3906 | |
| 3907 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3908 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3909 | * and program a response message if something was unexpected. It cannot fail |
| 3910 | * and always relies on the stats applet to complete the job. It does not touch |
| 3911 | * analysers nor counters, which are left to the caller. It does not touch |
| 3912 | * s->target which is supposed to already point to the stats applet. The caller |
| 3913 | * is expected to have already assigned an appctx to the stream. |
| 3914 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3915 | static int http_handle_stats(struct stream *s, struct channel *req) |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3916 | { |
| 3917 | struct stats_admin_rule *stats_admin_rule; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3918 | struct session *sess = s->sess; |
| 3919 | struct http_txn *txn = s->txn; |
| 3920 | struct http_msg *msg = &txn->req; |
| 3921 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3922 | const char *h, *lookup, *end; |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 3923 | struct appctx *appctx = __cs_appctx(s->csb); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3924 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3925 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3926 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3927 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3928 | appctx->st1 = appctx->st2 = 0; |
| 3929 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
Willy Tarreau | 676c29e | 2019-10-09 10:50:01 +0200 | [diff] [blame] | 3930 | appctx->ctx.stats.flags |= uri_auth->flags; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3931 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
| 3932 | if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD)) |
| 3933 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
| 3934 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3935 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 3936 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3937 | lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len; |
| 3938 | end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3939 | |
| 3940 | for (h = lookup; h <= end - 3; h++) { |
| 3941 | if (memcmp(h, ";up", 3) == 0) { |
| 3942 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
| 3943 | break; |
| 3944 | } |
Amaury Denoyelle | 91e55ea | 2021-02-25 14:46:08 +0100 | [diff] [blame] | 3945 | } |
| 3946 | |
| 3947 | for (h = lookup; h <= end - 9; h++) { |
| 3948 | if (memcmp(h, ";no-maint", 9) == 0) { |
Willy Tarreau | 3e32036 | 2020-10-23 17:28:57 +0200 | [diff] [blame] | 3949 | appctx->ctx.stats.flags |= STAT_HIDE_MAINT; |
| 3950 | break; |
| 3951 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3952 | } |
| 3953 | |
| 3954 | if (uri_auth->refresh) { |
| 3955 | for (h = lookup; h <= end - 10; h++) { |
| 3956 | if (memcmp(h, ";norefresh", 10) == 0) { |
| 3957 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
| 3958 | break; |
| 3959 | } |
| 3960 | } |
| 3961 | } |
| 3962 | |
| 3963 | for (h = lookup; h <= end - 4; h++) { |
| 3964 | if (memcmp(h, ";csv", 4) == 0) { |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 3965 | appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3966 | break; |
| 3967 | } |
| 3968 | } |
| 3969 | |
| 3970 | for (h = lookup; h <= end - 6; h++) { |
| 3971 | if (memcmp(h, ";typed", 6) == 0) { |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 3972 | appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3973 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
| 3974 | break; |
| 3975 | } |
| 3976 | } |
| 3977 | |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 3978 | for (h = lookup; h <= end - 5; h++) { |
| 3979 | if (memcmp(h, ";json", 5) == 0) { |
| 3980 | appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
| 3981 | appctx->ctx.stats.flags |= STAT_FMT_JSON; |
| 3982 | break; |
| 3983 | } |
| 3984 | } |
| 3985 | |
| 3986 | for (h = lookup; h <= end - 12; h++) { |
| 3987 | if (memcmp(h, ";json-schema", 12) == 0) { |
| 3988 | appctx->ctx.stats.flags &= ~STAT_FMT_MASK; |
| 3989 | appctx->ctx.stats.flags |= STAT_JSON_SCHM; |
| 3990 | break; |
| 3991 | } |
| 3992 | } |
| 3993 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3994 | for (h = lookup; h <= end - 8; h++) { |
| 3995 | if (memcmp(h, ";st=", 4) == 0) { |
| 3996 | int i; |
| 3997 | h += 4; |
| 3998 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
| 3999 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 4000 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
| 4001 | appctx->ctx.stats.st_code = i; |
| 4002 | break; |
| 4003 | } |
| 4004 | } |
| 4005 | break; |
| 4006 | } |
| 4007 | } |
| 4008 | |
| 4009 | appctx->ctx.stats.scope_str = 0; |
| 4010 | appctx->ctx.stats.scope_len = 0; |
| 4011 | for (h = lookup; h <= end - 8; h++) { |
| 4012 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 4013 | int itx = 0; |
| 4014 | const char *h2; |
| 4015 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 4016 | const char *err; |
| 4017 | |
| 4018 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 4019 | h2 = h; |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 4020 | appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl); |
| 4021 | while (h < end) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4022 | if (*h == ';' || *h == '&' || *h == ' ') |
| 4023 | break; |
| 4024 | itx++; |
| 4025 | h++; |
| 4026 | } |
| 4027 | |
| 4028 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 4029 | itx = STAT_SCOPE_TXT_MAXLEN; |
| 4030 | appctx->ctx.stats.scope_len = itx; |
| 4031 | |
| 4032 | /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */ |
| 4033 | memcpy(scope_txt, h2, itx); |
| 4034 | scope_txt[itx] = '\0'; |
| 4035 | err = invalid_char(scope_txt); |
| 4036 | if (err) { |
| 4037 | /* bad char in search text => clear scope */ |
| 4038 | appctx->ctx.stats.scope_str = 0; |
| 4039 | appctx->ctx.stats.scope_len = 0; |
| 4040 | } |
| 4041 | break; |
| 4042 | } |
| 4043 | } |
| 4044 | |
| 4045 | /* now check whether we have some admin rules for this request */ |
| 4046 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
| 4047 | int ret = 1; |
| 4048 | |
| 4049 | if (stats_admin_rule->cond) { |
| 4050 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 4051 | ret = acl_pass(ret); |
| 4052 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 4053 | ret = !ret; |
| 4054 | } |
| 4055 | |
| 4056 | if (ret) { |
| 4057 | /* no rule, or the rule matches */ |
| 4058 | appctx->ctx.stats.flags |= STAT_ADMIN; |
| 4059 | break; |
| 4060 | } |
| 4061 | } |
| 4062 | |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4063 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 4064 | appctx->st0 = STAT_HTTP_HEAD; |
| 4065 | else if (txn->meth == HTTP_METH_POST) { |
Christopher Faulet | bd9e842 | 2019-08-15 22:26:48 +0200 | [diff] [blame] | 4066 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4067 | appctx->st0 = STAT_HTTP_POST; |
Christopher Faulet | bd9e842 | 2019-08-15 22:26:48 +0200 | [diff] [blame] | 4068 | if (msg->msg_state < HTTP_MSG_DATA) |
| 4069 | req->analysers |= AN_REQ_HTTP_BODY; |
| 4070 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4071 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4072 | /* POST without admin level */ |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4073 | appctx->ctx.stats.flags &= ~STAT_CHUNKED; |
| 4074 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 4075 | appctx->st0 = STAT_HTTP_LAST; |
| 4076 | } |
| 4077 | } |
| 4078 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4079 | /* Unsupported method */ |
| 4080 | appctx->ctx.stats.flags &= ~STAT_CHUNKED; |
| 4081 | appctx->ctx.stats.st_code = STAT_STATUS_IVAL; |
| 4082 | appctx->st0 = STAT_HTTP_LAST; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4083 | } |
| 4084 | |
| 4085 | s->task->nice = -32; /* small boost for HTTP statistics */ |
| 4086 | return 1; |
| 4087 | } |
| 4088 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4089 | /* This function waits for the message payload at most <time> milliseconds (may |
| 4090 | * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the |
| 4091 | * payload are received (0 means no limit). It returns HTTP_RULE_* depending on |
| 4092 | * the result: |
| 4093 | * |
| 4094 | * - HTTP_RULE_RES_CONT when conditions are met to stop waiting |
| 4095 | * - HTTP_RULE_RES_YIELD to wait for more data |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 4096 | * - HTTP_RULE_RES_ABRT when a timeout occurred. |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4097 | * - HTTP_RULE_RES_BADREQ if a parsing error is raised by lower level |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 4098 | * - HTTP_RULE_RES_ERROR if an internal error occurred |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4099 | * |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 4100 | * If a timeout occurred, this function is responsible to emit the right response |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4101 | * to the client, depending on the channel (408 on request side, 504 on response |
| 4102 | * side). All other errors must be handled by the caller. |
| 4103 | */ |
| 4104 | enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn, |
| 4105 | unsigned int time, unsigned int bytes) |
| 4106 | { |
| 4107 | struct session *sess = s->sess; |
| 4108 | struct http_txn *txn = s->txn; |
| 4109 | struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req); |
| 4110 | struct htx *htx; |
| 4111 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 4112 | |
| 4113 | htx = htxbuf(&chn->buf); |
| 4114 | |
| 4115 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
| 4116 | ret = HTTP_RULE_RES_BADREQ; |
| 4117 | goto end; |
| 4118 | } |
| 4119 | if (htx->flags & HTX_FL_PROCESSING_ERROR) { |
| 4120 | ret = HTTP_RULE_RES_ERROR; |
| 4121 | goto end; |
| 4122 | } |
| 4123 | |
| 4124 | /* Do nothing for bodyless and CONNECT requests */ |
| 4125 | if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS)) |
| 4126 | goto end; |
| 4127 | |
| 4128 | if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) { |
| 4129 | if (http_handle_expect_hdr(s, htx, msg) == -1) { |
| 4130 | ret = HTTP_RULE_RES_ERROR; |
| 4131 | goto end; |
| 4132 | } |
| 4133 | } |
| 4134 | |
| 4135 | msg->msg_state = HTTP_MSG_DATA; |
| 4136 | |
| 4137 | /* Now we're in HTTP_MSG_DATA. We just need to know if all data have |
| 4138 | * been received or if the buffer is full. |
| 4139 | */ |
Christopher Faulet | 7833596 | 2021-09-23 14:46:32 +0200 | [diff] [blame] | 4140 | if ((htx->flags & HTX_FL_EOM) || |
| 4141 | htx_get_tail_type(htx) > HTX_BLK_DATA || |
| 4142 | channel_htx_full(chn, htx, global.tune.maxrewrite) || |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4143 | cs_rx_blocked_room(chn_prod(chn))) |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4144 | goto end; |
| 4145 | |
| 4146 | if (bytes) { |
| 4147 | struct htx_blk *blk; |
| 4148 | unsigned int len = 0; |
| 4149 | |
| 4150 | for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 4151 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 4152 | continue; |
| 4153 | len += htx_get_blksz(blk); |
| 4154 | if (len >= bytes) |
| 4155 | goto end; |
| 4156 | } |
| 4157 | } |
| 4158 | |
| 4159 | if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) { |
| 4160 | if (!(chn->flags & CF_ISRESP)) |
| 4161 | goto abort_req; |
| 4162 | goto abort_res; |
| 4163 | } |
| 4164 | |
| 4165 | /* we get here if we need to wait for more data */ |
| 4166 | if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) { |
| 4167 | if (!tick_isset(chn->analyse_exp)) |
| 4168 | chn->analyse_exp = tick_add_ifset(now_ms, time); |
| 4169 | ret = HTTP_RULE_RES_YIELD; |
| 4170 | } |
| 4171 | |
| 4172 | end: |
| 4173 | return ret; |
| 4174 | |
| 4175 | abort_req: |
| 4176 | txn->status = 408; |
| 4177 | if (!(s->flags & SF_ERR_MASK)) |
| 4178 | s->flags |= SF_ERR_CLITO; |
| 4179 | if (!(s->flags & SF_FINST_MASK)) |
| 4180 | s->flags |= SF_FINST_D; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4181 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4182 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4183 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4184 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 4185 | ret = HTTP_RULE_RES_ABRT; |
| 4186 | goto end; |
| 4187 | |
| 4188 | abort_res: |
| 4189 | txn->status = 504; |
| 4190 | if (!(s->flags & SF_ERR_MASK)) |
| 4191 | s->flags |= SF_ERR_SRVTO; |
| 4192 | if (!(s->flags & SF_FINST_MASK)) |
| 4193 | s->flags |= SF_FINST_D; |
| 4194 | stream_inc_http_fail_ctr(s); |
| 4195 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 4196 | ret = HTTP_RULE_RES_ABRT; |
| 4197 | goto end; |
| 4198 | } |
| 4199 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4200 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4201 | { |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4202 | struct channel *req = &s->req; |
| 4203 | struct channel *res = &s->res; |
| 4204 | struct server *srv; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4205 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4206 | struct htx_sl *sl; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4207 | struct ist path, location; |
| 4208 | unsigned int flags; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4209 | struct http_uri_parser parser; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4210 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4211 | /* |
| 4212 | * Create the location |
| 4213 | */ |
| 4214 | chunk_reset(&trash); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4215 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4216 | /* 1: add the server's prefix */ |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4217 | /* special prefix "/" means don't change URL */ |
| 4218 | srv = __objt_server(s->target); |
| 4219 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 4220 | if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len)) |
| 4221 | return; |
| 4222 | } |
| 4223 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4224 | /* 2: add the request Path */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4225 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 4226 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4227 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4228 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4229 | if (!isttest(path)) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4230 | return; |
| 4231 | |
| 4232 | if (!chunk_memcat(&trash, path.ptr, path.len)) |
| 4233 | return; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4234 | location = ist2(trash.area, trash.data); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4235 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4236 | /* |
| 4237 | * Create the 302 respone |
| 4238 | */ |
| 4239 | htx = htx_from_buf(&res->buf); |
| 4240 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 4241 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4242 | ist("HTTP/1.1"), ist("302"), ist("Found")); |
| 4243 | if (!sl) |
| 4244 | goto fail; |
| 4245 | sl->info.res.status = 302; |
| 4246 | s->txn->status = 302; |
| 4247 | |
| 4248 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) || |
| 4249 | !htx_add_header(htx, ist("Connection"), ist("close")) || |
| 4250 | !htx_add_header(htx, ist("Content-length"), ist("0")) || |
| 4251 | !htx_add_header(htx, ist("Location"), location)) |
| 4252 | goto fail; |
| 4253 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4254 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4255 | goto fail; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4256 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4257 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | c20afb8 | 2020-01-24 19:16:26 +0100 | [diff] [blame] | 4258 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4259 | if (!http_forward_proxy_resp(s, 1)) |
| 4260 | goto fail; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4261 | |
| 4262 | /* return without error. */ |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 4263 | cs_shutr(si->cs); |
| 4264 | cs_shutw(si->cs); |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4265 | s->conn_err_type = STRM_ET_NONE; |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 4266 | si->cs->state = CS_ST_CLO; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4267 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4268 | if (!(s->flags & SF_ERR_MASK)) |
| 4269 | s->flags |= SF_ERR_LOCAL; |
| 4270 | if (!(s->flags & SF_FINST_MASK)) |
| 4271 | s->flags |= SF_FINST_C; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4272 | |
| 4273 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 4274 | srv_inc_sess_ctr(srv); |
| 4275 | srv_set_sess_last(srv); |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4276 | return; |
| 4277 | |
| 4278 | fail: |
| 4279 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4280 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4281 | channel_htx_truncate(res, htx); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4282 | } |
| 4283 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4284 | /* This function terminates the request because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4285 | * because an error was triggered during the body forwarding. |
| 4286 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4287 | static void http_end_request(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4288 | { |
| 4289 | struct channel *chn = &s->req; |
| 4290 | struct http_txn *txn = s->txn; |
| 4291 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4292 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4293 | |
Christopher Faulet | b42a8b6 | 2018-11-19 21:59:00 +0100 | [diff] [blame] | 4294 | if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR || |
| 4295 | txn->rsp.msg_state == HTTP_MSG_ERROR)) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4296 | channel_abort(chn); |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4297 | channel_htx_truncate(chn, htxbuf(&chn->buf)); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4298 | goto end; |
| 4299 | } |
| 4300 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4301 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) { |
| 4302 | DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4303 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4304 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4305 | |
| 4306 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4307 | /* No need to read anymore, the request was completely parsed. |
| 4308 | * We can shut the read side unless we want to abort_on_close, |
| 4309 | * or we have a POST request. The issue with POST requests is |
| 4310 | * that some browsers still send a CRLF after the request, and |
| 4311 | * this CRLF must be read so that it does not remain in the kernel |
| 4312 | * buffers, otherwise a close could cause an RST on some systems |
| 4313 | * (eg: Linux). |
| 4314 | */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 4315 | if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4316 | channel_dont_read(chn); |
| 4317 | |
| 4318 | /* if the server closes the connection, we want to immediately react |
| 4319 | * and close the socket to save packets and syscalls. |
| 4320 | */ |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 4321 | s->csb->flags |= CS_FL_NOHALF; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4322 | |
| 4323 | /* In any case we've finished parsing the request so we must |
| 4324 | * disable Nagle when sending data because 1) we're not going |
| 4325 | * to shut this side, and 2) the server is waiting for us to |
| 4326 | * send pending data. |
| 4327 | */ |
| 4328 | chn->flags |= CF_NEVER_WAIT; |
| 4329 | |
Christopher Faulet | d01ce40 | 2019-01-02 17:44:13 +0100 | [diff] [blame] | 4330 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4331 | /* The server has not finished to respond, so we |
| 4332 | * don't want to move in order not to upset it. |
| 4333 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4334 | DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | d01ce40 | 2019-01-02 17:44:13 +0100 | [diff] [blame] | 4335 | return; |
| 4336 | } |
| 4337 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4338 | /* When we get here, it means that both the request and the |
| 4339 | * response have finished receiving. Depending on the connection |
| 4340 | * mode, we'll have to wait for the last bytes to leave in either |
| 4341 | * direction, and sometimes for a close to be effective. |
| 4342 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4343 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4344 | /* Tunnel mode will not have any analyser so it needs to |
| 4345 | * poll for reads. |
| 4346 | */ |
| 4347 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4348 | if (b_data(&chn->buf)) { |
| 4349 | DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4350 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4351 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4352 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 4353 | } |
| 4354 | else { |
| 4355 | /* we're not expecting any new data to come for this |
| 4356 | * transaction, so we can close it. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4357 | * |
| 4358 | * However, there is an exception if the response |
| 4359 | * length is undefined. In this case, we need to wait |
| 4360 | * the close from the server. The response will be |
| 4361 | * switched in TUNNEL mode until the end. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4362 | */ |
| 4363 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 4364 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4365 | goto check_channel_flags; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4366 | |
| 4367 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4368 | channel_shutr_now(chn); |
| 4369 | channel_shutw_now(chn); |
| 4370 | } |
| 4371 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4372 | goto check_channel_flags; |
| 4373 | } |
| 4374 | |
| 4375 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4376 | http_msg_closing: |
| 4377 | /* nothing else to forward, just waiting for the output buffer |
| 4378 | * to be empty and for the shutw_now to take effect. |
| 4379 | */ |
| 4380 | if (channel_is_empty(chn)) { |
| 4381 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4382 | goto http_msg_closed; |
| 4383 | } |
| 4384 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4385 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4386 | goto end; |
| 4387 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4388 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4389 | return; |
| 4390 | } |
| 4391 | |
| 4392 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4393 | http_msg_closed: |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4394 | /* if we don't know whether the server will close, we need to hard close */ |
| 4395 | if (txn->rsp.flags & HTTP_MSGF_XFER_LEN) |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 4396 | s->csb->flags |= CS_FL_NOLINGER; /* we want to close ASAP */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4397 | /* see above in MSG_DONE why we only do this in these states */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 4398 | if (!(s->be->options & PR_O_ABRT_CLOSE)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4399 | channel_dont_read(chn); |
| 4400 | goto end; |
| 4401 | } |
| 4402 | |
| 4403 | check_channel_flags: |
| 4404 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4405 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4406 | /* if we've just closed an output, let's switch */ |
| 4407 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4408 | goto http_msg_closing; |
| 4409 | } |
| 4410 | |
| 4411 | end: |
| 4412 | chn->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4413 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4414 | chn->flags |= CF_NEVER_WAIT; |
| 4415 | if (HAS_REQ_DATA_FILTERS(s)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4416 | chn->analysers |= AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4417 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4418 | channel_auto_close(chn); |
| 4419 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4420 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4421 | } |
| 4422 | |
| 4423 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4424 | /* This function terminates the response because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4425 | * because an error was triggered during the body forwarding. |
| 4426 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4427 | static void http_end_response(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4428 | { |
| 4429 | struct channel *chn = &s->res; |
| 4430 | struct http_txn *txn = s->txn; |
| 4431 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4432 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4433 | |
Christopher Faulet | b42a8b6 | 2018-11-19 21:59:00 +0100 | [diff] [blame] | 4434 | if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR || |
| 4435 | txn->rsp.msg_state == HTTP_MSG_ERROR)) { |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4436 | channel_htx_truncate(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4437 | channel_abort(&s->req); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4438 | goto end; |
| 4439 | } |
| 4440 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4441 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) { |
| 4442 | DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4443 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4444 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4445 | |
| 4446 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4447 | /* In theory, we don't need to read anymore, but we must |
| 4448 | * still monitor the server connection for a possible close |
| 4449 | * while the request is being uploaded, so we don't disable |
| 4450 | * reading. |
| 4451 | */ |
| 4452 | /* channel_dont_read(chn); */ |
| 4453 | |
| 4454 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4455 | /* The client seems to still be sending data, probably |
| 4456 | * because we got an error response during an upload. |
| 4457 | * We have the choice of either breaking the connection |
| 4458 | * or letting it pass through. Let's do the later. |
| 4459 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4460 | DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4461 | return; |
| 4462 | } |
| 4463 | |
| 4464 | /* When we get here, it means that both the request and the |
| 4465 | * response have finished receiving. Depending on the connection |
| 4466 | * mode, we'll have to wait for the last bytes to leave in either |
| 4467 | * direction, and sometimes for a close to be effective. |
| 4468 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4469 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4470 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4471 | if (b_data(&chn->buf)) { |
| 4472 | DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4473 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4474 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4475 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4476 | } |
| 4477 | else { |
| 4478 | /* we're not expecting any new data to come for this |
| 4479 | * transaction, so we can close it. |
| 4480 | */ |
| 4481 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4482 | channel_shutr_now(chn); |
| 4483 | channel_shutw_now(chn); |
| 4484 | } |
| 4485 | } |
| 4486 | goto check_channel_flags; |
| 4487 | } |
| 4488 | |
| 4489 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4490 | http_msg_closing: |
| 4491 | /* nothing else to forward, just waiting for the output buffer |
| 4492 | * to be empty and for the shutw_now to take effect. |
| 4493 | */ |
| 4494 | if (channel_is_empty(chn)) { |
| 4495 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4496 | goto http_msg_closed; |
| 4497 | } |
| 4498 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4499 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4500 | _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts); |
| 4501 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 4502 | if (strm_sess(s)->listener && strm_sess(s)->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4503 | _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4504 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4505 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4506 | goto end; |
| 4507 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4508 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4509 | return; |
| 4510 | } |
| 4511 | |
| 4512 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4513 | http_msg_closed: |
| 4514 | /* drop any pending data */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4515 | channel_htx_truncate(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4516 | channel_abort(&s->req); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4517 | goto end; |
| 4518 | } |
| 4519 | |
| 4520 | check_channel_flags: |
| 4521 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4522 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4523 | /* if we've just closed an output, let's switch */ |
| 4524 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4525 | goto http_msg_closing; |
| 4526 | } |
| 4527 | |
| 4528 | end: |
| 4529 | chn->analysers &= AN_RES_FLT_END; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4530 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4531 | chn->flags |= CF_NEVER_WAIT; |
| 4532 | if (HAS_RSP_DATA_FILTERS(s)) |
| 4533 | chn->analysers |= AN_RES_FLT_XFER_DATA; |
| 4534 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4535 | channel_auto_close(chn); |
| 4536 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4537 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4538 | } |
| 4539 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4540 | /* Forward a response generated by HAProxy (error/redirect/return). This |
| 4541 | * function forwards all pending incoming data. If <final> is set to 0, nothing |
| 4542 | * more is performed. It is used for 1xx informational messages. Otherwise, the |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4543 | * transaction is terminated and the request is emptied. On success 1 is |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4544 | * returned. If an error occurred, 0 is returned. If it fails, this function |
| 4545 | * only exits. It is the caller responsibility to do the cleanup. |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4546 | */ |
| 4547 | int http_forward_proxy_resp(struct stream *s, int final) |
| 4548 | { |
| 4549 | struct channel *req = &s->req; |
| 4550 | struct channel *res = &s->res; |
| 4551 | struct htx *htx = htxbuf(&res->buf); |
| 4552 | size_t data; |
| 4553 | |
| 4554 | if (final) { |
| 4555 | htx->flags |= HTX_FL_PROXY_RESP; |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4556 | |
Christopher Faulet | aab1b67 | 2020-11-18 16:44:02 +0100 | [diff] [blame] | 4557 | if (!htx_is_empty(htx) && !http_eval_after_res_rules(s)) |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 4558 | return 0; |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4559 | |
Christopher Faulet | d6c4836 | 2020-10-19 18:01:38 +0200 | [diff] [blame] | 4560 | if (s->txn->meth == HTTP_METH_HEAD) |
| 4561 | htx_skip_msg_payload(htx); |
| 4562 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4563 | channel_auto_read(req); |
| 4564 | channel_abort(req); |
| 4565 | channel_auto_close(req); |
| 4566 | channel_htx_erase(req, htxbuf(&req->buf)); |
| 4567 | |
| 4568 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 4569 | channel_auto_read(res); |
| 4570 | channel_auto_close(res); |
| 4571 | channel_shutr_now(res); |
Christopher Faulet | 1a9db7c | 2020-06-25 15:36:45 +0200 | [diff] [blame] | 4572 | res->flags |= CF_EOI; /* The response is terminated, add EOI */ |
Christopher Faulet | 42432f3 | 2020-11-20 17:43:16 +0100 | [diff] [blame] | 4573 | htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */ |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4574 | } |
Christopher Faulet | cf6898c | 2020-06-25 15:55:11 +0200 | [diff] [blame] | 4575 | else { |
| 4576 | /* Send ASAP informational messages. Rely on CF_EOI for final |
| 4577 | * response. |
| 4578 | */ |
| 4579 | res->flags |= CF_SEND_DONTWAIT; |
| 4580 | } |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4581 | |
| 4582 | data = htx->data - co_data(res); |
| 4583 | c_adv(res, data); |
| 4584 | htx->first = -1; |
| 4585 | res->total += data; |
| 4586 | return 1; |
| 4587 | } |
| 4588 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4589 | void http_server_error(struct stream *s, struct stream_interface *si, int err, |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4590 | int finst, struct http_reply *msg) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4591 | { |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4592 | http_reply_and_close(s, s->txn->status, msg); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4593 | if (!(s->flags & SF_ERR_MASK)) |
| 4594 | s->flags |= err; |
| 4595 | if (!(s->flags & SF_FINST_MASK)) |
| 4596 | s->flags |= finst; |
| 4597 | } |
| 4598 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4599 | void http_reply_and_close(struct stream *s, short status, struct http_reply *msg) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4600 | { |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4601 | if (!msg) { |
| 4602 | channel_htx_truncate(&s->res, htxbuf(&s->res.buf)); |
| 4603 | goto end; |
| 4604 | } |
| 4605 | |
| 4606 | if (http_reply_message(s, msg) == -1) { |
| 4607 | /* On error, return a 500 error message, but don't rewrite it if |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4608 | * it is already an internal error. If it was already a "const" |
| 4609 | * 500 error, just fail. |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4610 | */ |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4611 | if (s->txn->status == 500) { |
| 4612 | if (s->txn->flags & TX_CONST_REPLY) |
| 4613 | goto end; |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4614 | s->txn->flags |= TX_CONST_REPLY; |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4615 | } |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4616 | s->txn->status = 500; |
| 4617 | s->txn->http_reply = NULL; |
| 4618 | return http_reply_and_close(s, s->txn->status, http_error_message(s)); |
| 4619 | } |
| 4620 | |
| 4621 | end: |
| 4622 | s->res.wex = tick_add_ifset(now_ms, s->res.wto); |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4623 | |
Christopher Faulet | 2d56500 | 2021-09-10 09:17:50 +0200 | [diff] [blame] | 4624 | /* At this staged, HTTP analysis is finished */ |
| 4625 | s->req.analysers &= AN_REQ_FLT_END; |
| 4626 | s->req.analyse_exp = TICK_ETERNITY; |
| 4627 | |
| 4628 | s->res.analysers &= AN_RES_FLT_END; |
| 4629 | s->res.analyse_exp = TICK_ETERNITY; |
| 4630 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4631 | channel_auto_read(&s->req); |
| 4632 | channel_abort(&s->req); |
| 4633 | channel_auto_close(&s->req); |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4634 | channel_htx_erase(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4635 | channel_auto_read(&s->res); |
| 4636 | channel_auto_close(&s->res); |
| 4637 | channel_shutr_now(&s->res); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4638 | } |
| 4639 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4640 | struct http_reply *http_error_message(struct stream *s) |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4641 | { |
| 4642 | const int msgnum = http_get_status_idx(s->txn->status); |
| 4643 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4644 | if (s->txn->http_reply) |
| 4645 | return s->txn->http_reply; |
| 4646 | else if (s->be->replies[msgnum]) |
| 4647 | return s->be->replies[msgnum]; |
| 4648 | else if (strm_fe(s)->replies[msgnum]) |
| 4649 | return strm_fe(s)->replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4650 | else |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4651 | return &http_err_replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4652 | } |
| 4653 | |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4654 | /* Produces an HTX message from an http reply. Depending on the http reply type, |
| 4655 | * a, errorfile, an raw file or a log-format string is used. On success, it |
| 4656 | * returns 0. If an error occurs -1 is returned. If it fails, this function only |
| 4657 | * exits. It is the caller responsibility to do the cleanup. |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4658 | */ |
Christopher Faulet | ae43b6c | 2020-05-27 15:24:22 +0200 | [diff] [blame] | 4659 | int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply) |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4660 | { |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4661 | struct buffer *errmsg; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4662 | struct htx_sl *sl; |
| 4663 | struct buffer *body = NULL; |
| 4664 | const char *status, *reason, *clen, *ctype; |
| 4665 | unsigned int slflags; |
| 4666 | int ret = 0; |
| 4667 | |
Christopher Faulet | e29a97e | 2020-05-14 14:49:25 +0200 | [diff] [blame] | 4668 | /* |
| 4669 | * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so |
| 4670 | * |
| 4671 | * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled |
| 4672 | * as no payload if NULL. the TXN status code is set with the status |
| 4673 | * of the original reply. |
| 4674 | */ |
| 4675 | |
| 4676 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4677 | if (reply->body.reply) |
| 4678 | reply = reply->body.reply; |
| 4679 | } |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4680 | if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) { |
| 4681 | /* get default error message */ |
| 4682 | if (reply == s->txn->http_reply) |
| 4683 | s->txn->http_reply = NULL; |
| 4684 | reply = http_error_message(s); |
| 4685 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4686 | if (reply->body.reply) |
| 4687 | reply = reply->body.reply; |
| 4688 | } |
| 4689 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4690 | |
| 4691 | if (reply->type == HTTP_REPLY_ERRMSG) { |
| 4692 | /* implicit or explicit error message*/ |
| 4693 | errmsg = reply->body.errmsg; |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4694 | if (errmsg && !b_is_null(errmsg)) { |
Christopher Faulet | 2056736 | 2020-05-15 14:52:49 +0200 | [diff] [blame] | 4695 | if (!htx_copy_msg(htx, errmsg)) |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4696 | goto fail; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4697 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4698 | } |
| 4699 | else { |
| 4700 | /* no payload, file or log-format string */ |
| 4701 | if (reply->type == HTTP_REPLY_RAW) { |
| 4702 | /* file */ |
| 4703 | body = &reply->body.obj; |
| 4704 | } |
| 4705 | else if (reply->type == HTTP_REPLY_LOGFMT) { |
| 4706 | /* log-format string */ |
| 4707 | body = alloc_trash_chunk(); |
| 4708 | if (!body) |
| 4709 | goto fail_alloc; |
| 4710 | body->data = build_logline(s, body->area, body->size, &reply->body.fmt); |
| 4711 | } |
| 4712 | /* else no payload */ |
| 4713 | |
| 4714 | status = ultoa(reply->status); |
| 4715 | reason = http_get_reason(reply->status); |
| 4716 | slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN); |
| 4717 | if (!body || !b_data(body)) |
| 4718 | slflags |= HTX_SL_F_BODYLESS; |
| 4719 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4720 | if (!sl) |
| 4721 | goto fail; |
| 4722 | sl->info.res.status = reply->status; |
| 4723 | |
| 4724 | clen = (body ? ultoa(b_data(body)) : "0"); |
| 4725 | ctype = reply->ctype; |
| 4726 | |
| 4727 | if (!LIST_ISEMPTY(&reply->hdrs)) { |
| 4728 | struct http_reply_hdr *hdr; |
| 4729 | struct buffer *value = alloc_trash_chunk(); |
| 4730 | |
| 4731 | if (!value) |
| 4732 | goto fail; |
| 4733 | |
| 4734 | list_for_each_entry(hdr, &reply->hdrs, list) { |
| 4735 | chunk_reset(value); |
| 4736 | value->data = build_logline(s, value->area, value->size, &hdr->value); |
| 4737 | if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) { |
| 4738 | free_trash_chunk(value); |
| 4739 | goto fail; |
| 4740 | } |
| 4741 | chunk_reset(value); |
| 4742 | } |
| 4743 | free_trash_chunk(value); |
| 4744 | } |
| 4745 | |
| 4746 | if (!htx_add_header(htx, ist("content-length"), ist(clen)) || |
| 4747 | (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) || |
| 4748 | !htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4749 | (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body))))) |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4750 | goto fail; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4751 | |
| 4752 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4753 | } |
| 4754 | |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4755 | leave: |
| 4756 | if (reply->type == HTTP_REPLY_LOGFMT) |
| 4757 | free_trash_chunk(body); |
| 4758 | return ret; |
| 4759 | |
| 4760 | fail_alloc: |
| 4761 | if (!(s->flags & SF_ERR_MASK)) |
| 4762 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4763 | /* fall through */ |
| 4764 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4765 | ret = -1; |
| 4766 | goto leave; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4767 | } |
| 4768 | |
| 4769 | /* Send an http reply to the client. On success, it returns 0. If an error |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4770 | * occurs -1 is returned and the response channel is truncated, removing this |
| 4771 | * way the faulty reply. This function may fail when the reply is formatted |
| 4772 | * (http_reply_to_htx) or when the reply is forwarded |
| 4773 | * (http_forward_proxy_resp). On the last case, it is because a |
| 4774 | * http-after-response rule fails. |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4775 | */ |
| 4776 | int http_reply_message(struct stream *s, struct http_reply *reply) |
| 4777 | { |
| 4778 | struct channel *res = &s->res; |
| 4779 | struct htx *htx = htx_from_buf(&res->buf); |
| 4780 | |
| 4781 | if (s->txn->status == -1) |
| 4782 | s->txn->status = reply->status; |
| 4783 | channel_htx_truncate(res, htx); |
| 4784 | |
| 4785 | if (http_reply_to_htx(s, htx, reply) == -1) |
| 4786 | goto fail; |
| 4787 | |
| 4788 | htx_to_buf(htx, &s->res.buf); |
| 4789 | if (!http_forward_proxy_resp(s, 1)) |
| 4790 | goto fail; |
| 4791 | return 0; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4792 | |
| 4793 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4794 | channel_htx_truncate(res, htx); |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4795 | if (!(s->flags & SF_ERR_MASK)) |
| 4796 | s->flags |= SF_ERR_PRXCOND; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4797 | return -1; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4798 | } |
| 4799 | |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4800 | /* Return the error message corresponding to s->conn_err_type. It is assumed |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4801 | * that the server side is closed. Note that err_type is actually a |
| 4802 | * bitmask, where almost only aborts may be cumulated with other |
| 4803 | * values. We consider that aborted operations are more important |
| 4804 | * than timeouts or errors due to the fact that nobody else in the |
| 4805 | * logs might explain incomplete retries. All others should avoid |
| 4806 | * being cumulated. It should normally not be possible to have multiple |
| 4807 | * aborts at once, but just in case, the first one in sequence is reported. |
| 4808 | * Note that connection errors appearing on the second request of a keep-alive |
| 4809 | * connection are not reported since this allows the client to retry. |
| 4810 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4811 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4812 | { |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4813 | int err_type = s->conn_err_type; |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4814 | |
| 4815 | /* set s->txn->status for http_error_message(s) */ |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4816 | if (err_type & STRM_ET_QUEUE_ABRT) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4817 | s->txn->status = -1; |
| 4818 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, NULL); |
| 4819 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4820 | else if (err_type & STRM_ET_CONN_ABRT) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4821 | s->txn->status = -1; |
| 4822 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, NULL); |
| 4823 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4824 | else if (err_type & STRM_ET_QUEUE_TO) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4825 | s->txn->status = 503; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4826 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
| 4827 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4828 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4829 | else if (err_type & STRM_ET_QUEUE_ERR) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4830 | s->txn->status = 503; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4831 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
| 4832 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4833 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4834 | else if (err_type & STRM_ET_CONN_TO) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4835 | s->txn->status = 503; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4836 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
| 4837 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4838 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4839 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4840 | else if (err_type & STRM_ET_CONN_ERR) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4841 | s->txn->status = 503; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4842 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
| 4843 | (s->flags & SF_SRV_REUSED) ? NULL : |
| 4844 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4845 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4846 | else if (err_type & STRM_ET_CONN_RES) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4847 | s->txn->status = 503; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4848 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
| 4849 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4850 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4851 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4852 | else { /* STRM_ET_CONN_OTHER and others */ |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4853 | s->txn->status = 500; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4854 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
| 4855 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4856 | } |
| 4857 | } |
| 4858 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4859 | |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4860 | /* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0 |
| 4861 | * on success and -1 on error. |
| 4862 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4863 | static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg) |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4864 | { |
| 4865 | /* If we have HTTP/1.1 message with a body and Expect: 100-continue, |
| 4866 | * then we must send an HTTP/1.1 100 Continue intermediate response. |
| 4867 | */ |
| 4868 | if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) && |
| 4869 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
| 4870 | struct ist hdr = { .ptr = "Expect", .len = 6 }; |
| 4871 | struct http_hdr_ctx ctx; |
| 4872 | |
| 4873 | ctx.blk = NULL; |
| 4874 | /* Expect is allowed in 1.1, look for it */ |
| 4875 | if (http_find_header(htx, hdr, &ctx, 0) && |
| 4876 | unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4877 | if (http_reply_100_continue(s) == -1) |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4878 | return -1; |
| 4879 | http_remove_header(htx, &ctx); |
| 4880 | } |
| 4881 | } |
| 4882 | return 0; |
| 4883 | } |
| 4884 | |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4885 | /* Send a 100-Continue response to the client. It returns 0 on success and -1 |
| 4886 | * on error. The response channel is updated accordingly. |
| 4887 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4888 | static int http_reply_100_continue(struct stream *s) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4889 | { |
| 4890 | struct channel *res = &s->res; |
| 4891 | struct htx *htx = htx_from_buf(&res->buf); |
| 4892 | struct htx_sl *sl; |
| 4893 | unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11| |
| 4894 | HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4895 | |
| 4896 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4897 | ist("HTTP/1.1"), ist("100"), ist("Continue")); |
| 4898 | if (!sl) |
| 4899 | goto fail; |
| 4900 | sl->info.res.status = 100; |
| 4901 | |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 4902 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4903 | goto fail; |
| 4904 | |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4905 | if (!http_forward_proxy_resp(s, 0)) |
| 4906 | goto fail; |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4907 | return 0; |
| 4908 | |
| 4909 | fail: |
| 4910 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4911 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4912 | channel_htx_truncate(res, htx); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4913 | return -1; |
| 4914 | } |
| 4915 | |
Christopher Faulet | 12c51e2 | 2018-11-28 15:59:42 +0100 | [diff] [blame] | 4916 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4917 | /* |
| 4918 | * Capture headers from message <htx> according to header list <cap_hdr>, and |
| 4919 | * fill the <cap> pointers appropriately. |
| 4920 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4921 | static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4922 | { |
| 4923 | struct cap_hdr *h; |
| 4924 | int32_t pos; |
| 4925 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4926 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4927 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4928 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4929 | struct ist n, v; |
| 4930 | |
| 4931 | if (type == HTX_BLK_EOH) |
| 4932 | break; |
| 4933 | if (type != HTX_BLK_HDR) |
| 4934 | continue; |
| 4935 | |
| 4936 | n = htx_get_blk_name(htx, blk); |
| 4937 | |
| 4938 | for (h = cap_hdr; h; h = h->next) { |
| 4939 | if (h->namelen && (h->namelen == n.len) && |
| 4940 | (strncasecmp(n.ptr, h->name, h->namelen) == 0)) { |
| 4941 | if (cap[h->index] == NULL) |
| 4942 | cap[h->index] = |
| 4943 | pool_alloc(h->pool); |
| 4944 | |
| 4945 | if (cap[h->index] == NULL) { |
| 4946 | ha_alert("HTTP capture : out of memory.\n"); |
| 4947 | break; |
| 4948 | } |
| 4949 | |
| 4950 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 4951 | v = isttrim(v, h->len); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4952 | |
| 4953 | memcpy(cap[h->index], v.ptr, v.len); |
| 4954 | cap[h->index][v.len]=0; |
| 4955 | } |
| 4956 | } |
| 4957 | } |
| 4958 | } |
| 4959 | |
Christopher Faulet | 0b6bdc5 | 2018-10-24 11:05:36 +0200 | [diff] [blame] | 4960 | /* Delete a value in a header between delimiters <from> and <next>. The header |
| 4961 | * itself is delimited by <start> and <end> pointers. The number of characters |
| 4962 | * displaced is returned, and the pointer to the first delimiter is updated if |
| 4963 | * required. The function tries as much as possible to respect the following |
| 4964 | * principles : |
| 4965 | * - replace <from> delimiter by the <next> one unless <from> points to <start>, |
| 4966 | * in which case <next> is simply removed |
| 4967 | * - set exactly one space character after the new first delimiter, unless there |
| 4968 | * are not enough characters in the block being moved to do so. |
| 4969 | * - remove unneeded spaces before the previous delimiter and after the new |
| 4970 | * one. |
| 4971 | * |
| 4972 | * It is the caller's responsibility to ensure that : |
| 4973 | * - <from> points to a valid delimiter or <start> ; |
| 4974 | * - <next> points to a valid delimiter or <end> ; |
| 4975 | * - there are non-space chars before <from>. |
| 4976 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4977 | static int http_del_hdr_value(char *start, char *end, char **from, char *next) |
Christopher Faulet | 0b6bdc5 | 2018-10-24 11:05:36 +0200 | [diff] [blame] | 4978 | { |
| 4979 | char *prev = *from; |
| 4980 | |
| 4981 | if (prev == start) { |
| 4982 | /* We're removing the first value. eat the semicolon, if <next> |
| 4983 | * is lower than <end> */ |
| 4984 | if (next < end) |
| 4985 | next++; |
| 4986 | |
| 4987 | while (next < end && HTTP_IS_SPHT(*next)) |
| 4988 | next++; |
| 4989 | } |
| 4990 | else { |
| 4991 | /* Remove useless spaces before the old delimiter. */ |
| 4992 | while (HTTP_IS_SPHT(*(prev-1))) |
| 4993 | prev--; |
| 4994 | *from = prev; |
| 4995 | |
| 4996 | /* copy the delimiter and if possible a space if we're |
| 4997 | * not at the end of the line. |
| 4998 | */ |
| 4999 | if (next < end) { |
| 5000 | *prev++ = *next++; |
| 5001 | if (prev + 1 < next) |
| 5002 | *prev++ = ' '; |
| 5003 | while (next < end && HTTP_IS_SPHT(*next)) |
| 5004 | next++; |
| 5005 | } |
| 5006 | } |
| 5007 | memmove(prev, next, end - next); |
| 5008 | return (prev - next); |
| 5009 | } |
| 5010 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5011 | |
| 5012 | /* Formats the start line of the request (without CRLF) and puts it in <str> and |
Joseph Herlant | c42c0e9 | 2018-11-25 10:43:27 -0800 | [diff] [blame] | 5013 | * return the written length. The line can be truncated if it exceeds <len>. |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5014 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5015 | static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5016 | { |
| 5017 | struct ist dst = ist2(str, 0); |
| 5018 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5019 | if (istcat(&dst, htx_sl_req_meth(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5020 | goto end; |
| 5021 | if (dst.len + 1 > len) |
| 5022 | goto end; |
| 5023 | dst.ptr[dst.len++] = ' '; |
| 5024 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5025 | if (istcat(&dst, htx_sl_req_uri(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5026 | goto end; |
| 5027 | if (dst.len + 1 > len) |
| 5028 | goto end; |
| 5029 | dst.ptr[dst.len++] = ' '; |
| 5030 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5031 | istcat(&dst, htx_sl_req_vsn(sl), len); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5032 | end: |
| 5033 | return dst.len; |
| 5034 | } |
| 5035 | |
| 5036 | /* |
| 5037 | * Print a debug line with a start line. |
| 5038 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5039 | static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5040 | { |
| 5041 | struct session *sess = strm_sess(s); |
| 5042 | int max; |
| 5043 | |
| 5044 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 5045 | dir, |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 5046 | objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1, |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 5047 | cs_conn(s->csb) ? (unsigned short)(__cs_conn(s->csb))->handle.fd : -1); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5048 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5049 | max = HTX_SL_P1_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5050 | UBOUND(max, trash.size - trash.data - 3); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5051 | chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5052 | trash.area[trash.data++] = ' '; |
| 5053 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5054 | max = HTX_SL_P2_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5055 | UBOUND(max, trash.size - trash.data - 2); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5056 | chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5057 | trash.area[trash.data++] = ' '; |
| 5058 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5059 | max = HTX_SL_P3_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5060 | UBOUND(max, trash.size - trash.data - 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5061 | chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5062 | trash.area[trash.data++] = '\n'; |
| 5063 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 5064 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5065 | } |
| 5066 | |
| 5067 | /* |
| 5068 | * Print a debug line with a header. |
| 5069 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5070 | static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5071 | { |
| 5072 | struct session *sess = strm_sess(s); |
| 5073 | int max; |
| 5074 | |
| 5075 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 5076 | dir, |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 5077 | objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1, |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 5078 | cs_conn(s->csb) ? (unsigned short)(__cs_conn(s->csb))->handle.fd : -1); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5079 | |
| 5080 | max = n.len; |
| 5081 | UBOUND(max, trash.size - trash.data - 3); |
| 5082 | chunk_memcat(&trash, n.ptr, max); |
| 5083 | trash.area[trash.data++] = ':'; |
| 5084 | trash.area[trash.data++] = ' '; |
| 5085 | |
| 5086 | max = v.len; |
| 5087 | UBOUND(max, trash.size - trash.data - 1); |
| 5088 | chunk_memcat(&trash, v.ptr, max); |
| 5089 | trash.area[trash.data++] = '\n'; |
| 5090 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 5091 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5092 | } |
| 5093 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5094 | void http_txn_reset_req(struct http_txn *txn) |
| 5095 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 5096 | txn->req.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5097 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 5098 | } |
| 5099 | |
| 5100 | void http_txn_reset_res(struct http_txn *txn) |
| 5101 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 5102 | txn->rsp.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5103 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 5104 | } |
| 5105 | |
| 5106 | /* |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5107 | * Create and initialize a new HTTP transaction for stream <s>. This should be |
| 5108 | * used before processing any new request. It returns the transaction or NLULL |
| 5109 | * on error. |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5110 | */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5111 | struct http_txn *http_create_txn(struct stream *s) |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5112 | { |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5113 | struct http_txn *txn; |
Christopher Faulet | 95a61e8 | 2021-12-22 14:22:03 +0100 | [diff] [blame] | 5114 | struct conn_stream *cs = s->csf; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5115 | |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5116 | txn = pool_alloc(pool_head_http_txn); |
| 5117 | if (!txn) |
| 5118 | return NULL; |
| 5119 | s->txn = txn; |
| 5120 | |
Christopher Faulet | e9e4820 | 2022-03-22 18:13:29 +0100 | [diff] [blame] | 5121 | txn->flags = ((cs && cs->endp->flags & CS_EP_NOT_FIRST) ? TX_NOT_FIRST : 0); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5122 | txn->status = -1; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 5123 | txn->http_reply = NULL; |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 5124 | txn->l7_buffer = BUF_NULL; |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 5125 | write_u32(txn->cache_hash, 0); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5126 | |
| 5127 | txn->cookie_first_date = 0; |
| 5128 | txn->cookie_last_date = 0; |
| 5129 | |
| 5130 | txn->srv_cookie = NULL; |
| 5131 | txn->cli_cookie = NULL; |
| 5132 | txn->uri = NULL; |
| 5133 | |
| 5134 | http_txn_reset_req(txn); |
| 5135 | http_txn_reset_res(txn); |
| 5136 | |
| 5137 | txn->req.chn = &s->req; |
| 5138 | txn->rsp.chn = &s->res; |
| 5139 | |
| 5140 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
| 5141 | |
Willy Tarreau | b7bfcb3 | 2021-08-31 08:13:25 +0200 | [diff] [blame] | 5142 | vars_init_head(&s->vars_txn, SCOPE_TXN); |
| 5143 | vars_init_head(&s->vars_reqres, SCOPE_REQ); |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5144 | |
| 5145 | return txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5146 | } |
| 5147 | |
| 5148 | /* to be used at the end of a transaction */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5149 | void http_destroy_txn(struct stream *s) |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5150 | { |
| 5151 | struct http_txn *txn = s->txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5152 | |
| 5153 | /* these ones will have been dynamically allocated */ |
| 5154 | pool_free(pool_head_requri, txn->uri); |
| 5155 | pool_free(pool_head_capture, txn->cli_cookie); |
| 5156 | pool_free(pool_head_capture, txn->srv_cookie); |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5157 | pool_free(pool_head_uniqueid, s->unique_id.ptr); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5158 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5159 | s->unique_id = IST_NULL; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5160 | txn->uri = NULL; |
| 5161 | txn->srv_cookie = NULL; |
| 5162 | txn->cli_cookie = NULL; |
| 5163 | |
Christopher Faulet | 5939925 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 5164 | if (!LIST_ISEMPTY(&s->vars_txn.head)) |
| 5165 | vars_prune(&s->vars_txn, s->sess, s); |
| 5166 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 5167 | vars_prune(&s->vars_reqres, s->sess, s); |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5168 | |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 5169 | b_free(&txn->l7_buffer); |
| 5170 | |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5171 | pool_free(pool_head_http_txn, txn); |
| 5172 | s->txn = NULL; |
Christopher Faulet | 5939925 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 5173 | } |
| 5174 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5175 | |
| 5176 | DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5177 | |
Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 5178 | __attribute__((constructor)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5179 | static void __http_protocol_init(void) |
Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 5180 | { |
| 5181 | } |
| 5182 | |
| 5183 | |
| 5184 | /* |
| 5185 | * Local variables: |
| 5186 | * c-indent-level: 8 |
| 5187 | * c-basic-offset: 8 |
| 5188 | * End: |
| 5189 | */ |