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 | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 16 | #include <haproxy/applet.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 17 | #include <haproxy/backend.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 18 | #include <haproxy/base64.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 19 | #include <haproxy/capture-t.h> |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 20 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 21 | #include <haproxy/channel.h> |
Willy Tarreau | 4aa573d | 2020-06-04 18:21:56 +0200 | [diff] [blame] | 22 | #include <haproxy/check.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 23 | #include <haproxy/connection.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 24 | #include <haproxy/errors.h> |
Willy Tarreau | c7babd8 | 2020-06-04 21:29:29 +0200 | [diff] [blame] | 25 | #include <haproxy/filters.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 26 | #include <haproxy/http.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 27 | #include <haproxy/http_ana.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 28 | #include <haproxy/http_htx.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 29 | #include <haproxy/htx.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 30 | #include <haproxy/log.h> |
Willy Tarreau | 6131d6a | 2020-06-02 16:48:09 +0200 | [diff] [blame] | 31 | #include <haproxy/net_helper.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 32 | #include <haproxy/proxy.h> |
Willy Tarreau | 7cd8b6e | 2020-06-02 17:32:26 +0200 | [diff] [blame] | 33 | #include <haproxy/regex.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 34 | #include <haproxy/sc_strm.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 | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 37 | #include <haproxy/stconn.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 38 | #include <haproxy/stream.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 | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 63 | static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 64 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 65 | static void http_manage_client_side_cookies(struct stream *s, struct channel *req); |
| 66 | 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] | 67 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 68 | static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend); |
| 69 | static int http_handle_stats(struct stream *s, struct channel *req); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 70 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 71 | static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg); |
| 72 | static int http_reply_100_continue(struct stream *s); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 73 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 74 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 75 | * processing can continue on next analysers, or zero if it either needs more |
| 76 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 77 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers |
| 78 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 79 | * abort. |
| 80 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 81 | 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] | 82 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 83 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 84 | /* |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 85 | * We will analyze a complete HTTP request to check the its syntax. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 86 | * |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 87 | * Once the start line and all headers are received, we may perform a |
| 88 | * capture of the error (if any), and we will set a few fields. We also |
| 89 | * check for monitor-uri, logging and finally headers capture. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 90 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 91 | struct session *sess = s->sess; |
| 92 | struct http_txn *txn = s->txn; |
| 93 | struct http_msg *msg = &txn->req; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 94 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 95 | struct htx_sl *sl; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 96 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 97 | 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] | 98 | |
Christopher Faulet | da46a0d | 2021-01-21 17:32:58 +0100 | [diff] [blame] | 99 | if (unlikely(!IS_HTX_STRM(s))) { |
| 100 | /* It is only possible when a TCP stream is upgrade to HTTP. |
| 101 | * There is a transition period during which there is no |
| 102 | * data. The stream is still in raw mode and SF_IGNORE flag is |
| 103 | * still set. When this happens, the new mux is responsible to |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 104 | * handle all errors. Thus we may leave immediately. |
Christopher Faulet | da46a0d | 2021-01-21 17:32:58 +0100 | [diff] [blame] | 105 | */ |
| 106 | BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 107 | |
Christopher Faulet | 97b3a61 | 2021-03-15 17:10:12 +0100 | [diff] [blame] | 108 | /* Don't connect for now */ |
| 109 | channel_dont_connect(req); |
| 110 | |
| 111 | /* A SHUTR at this stage means we are performing a "destructive" |
| 112 | * HTTP upgrade (TCP>H2). In this case, we can leave. |
| 113 | */ |
| 114 | if (req->flags & CF_SHUTR) { |
| 115 | s->logs.logwait = 0; |
| 116 | s->logs.level = 0; |
| 117 | channel_abort(&s->req); |
| 118 | channel_abort(&s->res); |
| 119 | req->analysers &= AN_REQ_FLT_END; |
| 120 | req->analyse_exp = TICK_ETERNITY; |
| 121 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s); |
| 122 | return 1; |
| 123 | } |
Christopher Faulet | da46a0d | 2021-01-21 17:32:58 +0100 | [diff] [blame] | 124 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | htx = htxbuf(&req->buf); |
Christopher Faulet | 8bebd2f | 2020-10-06 17:54:56 +0200 | [diff] [blame] | 129 | |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 130 | /* Parsing errors are caught here */ |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 131 | if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) { |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 132 | stream_inc_http_req_ctr(s); |
Emeric Brun | 2897644 | 2020-10-07 08:50:09 +0200 | [diff] [blame] | 133 | proxy_inc_fe_req_ctr(sess->listener, sess->fe); |
Christopher Faulet | bf7175f | 2021-02-10 14:58:01 +0100 | [diff] [blame] | 134 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
| 135 | stream_inc_http_err_ctr(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 136 | goto return_bad_req; |
Christopher Faulet | bf7175f | 2021-02-10 14:58:01 +0100 | [diff] [blame] | 137 | } |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 138 | else |
| 139 | goto return_int_err; |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 142 | /* 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] | 143 | s->srv_error = http_return_srv_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 144 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 145 | msg->msg_state = HTTP_MSG_BODY; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 146 | stream_inc_http_req_ctr(s); |
Emeric Brun | 2897644 | 2020-10-07 08:50:09 +0200 | [diff] [blame] | 147 | 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] | 148 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 149 | /* kill the pending keep-alive timeout */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 150 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 151 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 152 | BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 153 | sl = http_get_stline(htx); |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 154 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 155 | /* 0: we might have to print this header in debug mode */ |
| 156 | if (unlikely((global.mode & MODE_DEBUG) && |
| 157 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 158 | int32_t pos; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 159 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 160 | http_debug_stline("clireq", s, sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 161 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 162 | 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] | 163 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 164 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 165 | |
| 166 | if (type == HTX_BLK_EOH) |
| 167 | break; |
| 168 | if (type != HTX_BLK_HDR) |
| 169 | continue; |
| 170 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 171 | http_debug_hdr("clihdr", s, |
| 172 | htx_get_blk_name(htx, blk), |
| 173 | htx_get_blk_value(htx, blk)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 174 | } |
| 175 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 176 | |
| 177 | /* |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 178 | * 1: identify the method and the version. Also set HTTP flags |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 179 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 180 | txn->meth = sl->info.req.meth; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 181 | if (sl->flags & HTX_SL_F_VER_11) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 182 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 183 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 184 | if (sl->flags & HTX_SL_F_CLEN) |
| 185 | msg->flags |= HTTP_MSGF_CNT_LEN; |
| 186 | else if (sl->flags & HTX_SL_F_CHNK) |
| 187 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Christopher Faulet | b2db4fa | 2018-11-27 16:51:09 +0100 | [diff] [blame] | 188 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 189 | msg->flags |= HTTP_MSGF_BODYLESS; |
Christopher Faulet | 576c358 | 2021-01-08 15:53:01 +0100 | [diff] [blame] | 190 | if (sl->flags & HTX_SL_F_CONN_UPG) |
| 191 | msg->flags |= HTTP_MSGF_CONN_UPG; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 192 | |
| 193 | /* we can make use of server redirect on GET and HEAD */ |
| 194 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 195 | s->flags |= SF_REDIRECTABLE; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 196 | 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] | 197 | /* PRI is reserved for the HTTP/2 preface */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 198 | goto return_bad_req; |
| 199 | } |
| 200 | |
| 201 | /* |
Christopher Faulet | 6072beb | 2020-02-18 15:34:58 +0100 | [diff] [blame] | 202 | * 2: check if the URI matches the monitor_uri. We have to do this for |
| 203 | * every request which gets in, because the monitor-uri is defined by |
| 204 | * the frontend. If the monitor-uri starts with a '/', the matching is |
| 205 | * done against the request's path. Otherwise, the request's uri is |
| 206 | * used. It is a workaround to let HTTP/2 health-checks work as |
| 207 | * expected. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 208 | */ |
Tim Duesterhus | 4b1fcaa | 2022-03-05 00:52:40 +0100 | [diff] [blame] | 209 | if (unlikely(isttest(sess->fe->monitor_uri))) { |
| 210 | const struct ist monitor_uri = sess->fe->monitor_uri; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 211 | 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] | 212 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 213 | if ((istptr(monitor_uri)[0] == '/' && |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 214 | isteq(http_parse_path(&parser), monitor_uri)) || |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 215 | isteq(htx_sl_req_uri(sl), monitor_uri)) { |
| 216 | /* |
| 217 | * We have found the monitor URI |
| 218 | */ |
| 219 | struct acl_cond *cond; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 220 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 221 | s->flags |= SF_MONITOR; |
| 222 | _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 223 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 224 | /* Check if we want to fail this monitor request or not */ |
| 225 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
| 226 | 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] | 227 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 228 | ret = acl_pass(ret); |
| 229 | if (cond->pol == ACL_COND_UNLESS) |
| 230 | ret = !ret; |
| 231 | |
| 232 | if (ret) { |
| 233 | /* we fail this request, let's return 503 service unavail */ |
| 234 | txn->status = 503; |
| 235 | if (!(s->flags & SF_ERR_MASK)) |
| 236 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
| 237 | goto return_prx_cond; |
| 238 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 239 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 240 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 241 | /* nothing to fail, let's reply normally */ |
| 242 | txn->status = 200; |
| 243 | if (!(s->flags & SF_ERR_MASK)) |
| 244 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
| 245 | goto return_prx_cond; |
| 246 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* |
| 250 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 251 | * Note: we cannot log anymore if the request has been |
| 252 | * classified as invalid. |
| 253 | */ |
| 254 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 255 | /* we have a complete HTTP request that we must log */ |
| 256 | if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 257 | size_t len; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 258 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 259 | 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] | 260 | txn->uri[len] = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 261 | |
| 262 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
| 263 | s->do_log(s); |
| 264 | } else { |
| 265 | ha_alert("HTTP logging : out of memory.\n"); |
| 266 | } |
| 267 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 268 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 269 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 270 | * we have what looks like a proxied connection instead of a connection, |
| 271 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 272 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 273 | * happen to do that despite being non-standard :-( |
| 274 | * We consider that a request not beginning with either '/' or '*' is |
| 275 | * a proxied connection, which covers both "scheme://location" and |
| 276 | * CONNECT ip:port. |
| 277 | */ |
| 278 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 279 | *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*') |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 280 | txn->flags |= TX_USE_PX_CONN; |
| 281 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 282 | /* 5: we may need to capture headers */ |
| 283 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 284 | http_capture_headers(htx, s->req_cap, sess->fe->req_cap); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 285 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 286 | /* we may have to wait for the request's body */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 287 | if (s->be->options & PR_O_WREQ_BODY) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 288 | req->analysers |= AN_REQ_HTTP_BODY; |
| 289 | |
| 290 | /* |
| 291 | * RFC7234#4: |
| 292 | * A cache MUST write through requests with methods |
| 293 | * that are unsafe (Section 4.2.1 of [RFC7231]) to |
| 294 | * the origin server; i.e., a cache is not allowed |
| 295 | * to generate a reply to such a request before |
| 296 | * having forwarded the request and having received |
| 297 | * a corresponding response. |
| 298 | * |
| 299 | * RFC7231#4.2.1: |
| 300 | * Of the request methods defined by this |
| 301 | * specification, the GET, HEAD, OPTIONS, and TRACE |
| 302 | * methods are defined to be safe. |
| 303 | */ |
| 304 | if (likely(txn->meth == HTTP_METH_GET || |
| 305 | txn->meth == HTTP_METH_HEAD || |
| 306 | txn->meth == HTTP_METH_OPTIONS || |
| 307 | txn->meth == HTTP_METH_TRACE)) |
| 308 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 309 | |
| 310 | /* end of job, return OK */ |
| 311 | req->analysers &= ~an_bit; |
| 312 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 313 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 314 | 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] | 315 | return 1; |
| 316 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 317 | return_int_err: |
| 318 | txn->status = 500; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 319 | if (!(s->flags & SF_ERR_MASK)) |
| 320 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 321 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 322 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 323 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 324 | goto return_prx_cond; |
| 325 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 326 | return_bad_req: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 327 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 328 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 329 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 330 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 331 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 332 | |
| 333 | return_prx_cond: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 334 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 335 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 336 | if (!(s->flags & SF_ERR_MASK)) |
| 337 | s->flags |= SF_ERR_PRXCOND; |
| 338 | if (!(s->flags & SF_FINST_MASK)) |
| 339 | s->flags |= SF_FINST_R; |
| 340 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 341 | DBG_TRACE_DEVEL("leaving on error", |
| 342 | 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] | 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | |
| 347 | /* This stream analyser runs all HTTP request processing which is common to |
| 348 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 349 | * reqadd, stats and redirects. This is performed for the designated proxy. |
| 350 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 351 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 352 | * error, ...). |
| 353 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 354 | 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] | 355 | { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 356 | struct list *def_rules, *rules; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 357 | struct session *sess = s->sess; |
| 358 | struct http_txn *txn = s->txn; |
| 359 | struct http_msg *msg = &txn->req; |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 360 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 361 | struct redirect_rule *rule; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 362 | enum rule_result verdict; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 363 | struct connection *conn = objt_conn(sess->origin); |
| 364 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 365 | 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] | 366 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 367 | htx = htxbuf(&req->buf); |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 368 | |
Christopher Faulet | 1907ccc | 2019-04-29 13:12:02 +0200 | [diff] [blame] | 369 | /* just in case we have some per-backend tracking. Only called the first |
| 370 | * execution of the analyser. */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 371 | if (!s->current_rule && !s->current_rule_list) |
Christopher Faulet | 1907ccc | 2019-04-29 13:12:02 +0200 | [diff] [blame] | 372 | stream_inc_be_http_req_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 373 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 374 | def_rules = ((px->defpx && (an_bit == AN_REQ_HTTP_PROCESS_FE || px != sess->fe)) ? &px->defpx->http_req_rules : NULL); |
| 375 | rules = &px->http_req_rules; |
| 376 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 377 | /* evaluate http-request rules */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 378 | if ((def_rules && !LIST_ISEMPTY(def_rules)) || !LIST_ISEMPTY(rules)) { |
| 379 | verdict = http_req_get_intercept_rule(px, def_rules, rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 380 | |
| 381 | switch (verdict) { |
| 382 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 383 | goto return_prx_yield; |
| 384 | |
| 385 | case HTTP_RULE_RES_CONT: |
| 386 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 387 | break; |
| 388 | |
| 389 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 390 | if (txn->flags & TX_CLTARPIT) |
| 391 | goto tarpit; |
| 392 | goto deny; |
| 393 | |
| 394 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 395 | goto return_prx_cond; |
| 396 | |
| 397 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
| 398 | goto done; |
| 399 | |
| 400 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 401 | goto return_bad_req; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 402 | |
| 403 | case HTTP_RULE_RES_ERROR: /* failed with a bad request */ |
| 404 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 408 | if (px->options2 & (PR_O2_RSTRICT_REQ_HDR_NAMES_BLK|PR_O2_RSTRICT_REQ_HDR_NAMES_DEL)) { |
| 409 | verdict = http_req_restrict_header_names(s, htx, px); |
| 410 | if (verdict == HTTP_RULE_RES_DENY) |
| 411 | goto deny; |
| 412 | } |
| 413 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 414 | if (conn && (conn->flags & CO_FL_EARLY_DATA) && |
Olivier Houchard | 220a26c | 2020-01-23 14:57:36 +0100 | [diff] [blame] | 415 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) { |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 416 | struct http_hdr_ctx ctx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 417 | |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 418 | ctx.blk = NULL; |
| 419 | if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) { |
| 420 | if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1")))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 421 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 422 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /* OK at this stage, we know that the request was accepted according to |
| 426 | * the http-request rules, we can check for the stats. Note that the |
| 427 | * URI is detected *before* the req* rules in order not to be affected |
| 428 | * by a possible reqrep, while they are processed *after* so that a |
| 429 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 430 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 431 | if (!s->target && http_stats_check_uri(s, txn, px)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 432 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | a0b58b5 | 2022-05-27 08:33:53 +0200 | [diff] [blame] | 433 | if (unlikely(!sc_applet_create(s->scb, objt_applet(s->target)))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 434 | s->logs.tv_request = now; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 435 | if (!(s->flags & SF_ERR_MASK)) |
| 436 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 437 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /* parse the whole stats request and extract the relevant information */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 441 | http_handle_stats(s, req); |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 442 | 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] | 443 | /* not all actions implemented: deny, allow, auth */ |
| 444 | |
| 445 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 446 | goto deny; |
| 447 | |
| 448 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 449 | goto return_prx_cond; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 450 | |
| 451 | if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */ |
| 452 | goto return_bad_req; |
| 453 | |
| 454 | if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */ |
| 455 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 456 | } |
| 457 | |
Christopher Faulet | 2571bc6 | 2019-03-01 11:44:26 +0100 | [diff] [blame] | 458 | /* Proceed with the applets now. */ |
| 459 | if (unlikely(objt_applet(s->target))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 460 | 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] | 461 | _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 462 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 463 | if (http_handle_expect_hdr(s, htx, msg) == -1) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 464 | goto return_int_err; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 465 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 466 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 467 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 468 | if (!(s->flags & SF_FINST_MASK)) |
| 469 | s->flags |= SF_FINST_R; |
| 470 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 471 | if (HAS_FILTERS(s)) |
| 472 | req->analysers |= AN_REQ_FLT_HTTP_HDRS; |
| 473 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 474 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
| 475 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 476 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 477 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 478 | |
| 479 | req->flags |= CF_SEND_DONTWAIT; |
| 480 | s->flags |= SF_ASSIGNED; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 481 | goto done; |
| 482 | } |
| 483 | |
| 484 | /* check whether we have some ACLs set to redirect this request */ |
| 485 | list_for_each_entry(rule, &px->redirect_rules, list) { |
| 486 | if (rule->cond) { |
| 487 | int ret; |
| 488 | |
| 489 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 490 | ret = acl_pass(ret); |
| 491 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 492 | ret = !ret; |
| 493 | if (!ret) |
| 494 | continue; |
| 495 | } |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 496 | if (!http_apply_redirect_rule(rule, s, txn)) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 497 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 498 | goto done; |
| 499 | } |
| 500 | |
| 501 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 502 | * If this happens, then the data will not come immediately, so we must |
| 503 | * send all what we have without waiting. Note that due to the small gain |
| 504 | * in waiting for the body of the request, it's easier to simply put the |
| 505 | * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 506 | * itself once used. |
| 507 | */ |
| 508 | req->flags |= CF_SEND_DONTWAIT; |
| 509 | |
| 510 | done: /* done with this analyser, continue with next ones that the calling |
| 511 | * points will have set, if any. |
| 512 | */ |
| 513 | req->analyse_exp = TICK_ETERNITY; |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 514 | 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] | 515 | req->analysers &= ~an_bit; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 516 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 517 | 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] | 518 | return 1; |
| 519 | |
| 520 | tarpit: |
| 521 | /* Allow cookie logging |
| 522 | */ |
| 523 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 524 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 525 | |
| 526 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 527 | * which may be the same as the connect timeout if unspecified. |
| 528 | * If unset, then set it to zero because we really want it to |
| 529 | * eventually expire. We build the tarpit as an analyser. |
| 530 | */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 531 | channel_htx_erase(&s->req, htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 532 | |
| 533 | /* wipe the request out so that we can drop the connection early |
| 534 | * if the client closes first. |
| 535 | */ |
| 536 | channel_dont_connect(req); |
| 537 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 538 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
| 539 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 540 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 541 | if (!req->analyse_exp) |
| 542 | req->analyse_exp = tick_add(now_ms, 0); |
| 543 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 544 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 545 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 546 | _HA_ATOMIC_INC(&s->be->be_counters.denied_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 547 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 548 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 549 | goto done_without_exp; |
| 550 | |
| 551 | deny: /* this request was blocked (denied) */ |
| 552 | |
| 553 | /* Allow cookie logging |
| 554 | */ |
| 555 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 556 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 557 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 558 | s->logs.tv_request = now; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 559 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 560 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 561 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 562 | _HA_ATOMIC_INC(&s->be->be_counters.denied_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 563 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 564 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 565 | goto return_prx_err; |
| 566 | |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 567 | return_fail_rewrite: |
| 568 | if (!(s->flags & SF_ERR_MASK)) |
| 569 | s->flags |= SF_ERR_PRXCOND; |
| 570 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
| 571 | if (s->flags & SF_BE_ASSIGNED) |
| 572 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
| 573 | if (sess->listener && sess->listener->counters) |
| 574 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
| 575 | if (objt_server(s->target)) |
| 576 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
| 577 | /* fall through */ |
| 578 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 579 | return_int_err: |
| 580 | txn->status = 500; |
| 581 | if (!(s->flags & SF_ERR_MASK)) |
| 582 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 583 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 584 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 585 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 586 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 587 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 588 | goto return_prx_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 589 | |
| 590 | return_bad_req: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 591 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 592 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 593 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 594 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 595 | /* fall through */ |
| 596 | |
| 597 | return_prx_err: |
| 598 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 599 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 600 | |
| 601 | return_prx_cond: |
| 602 | if (!(s->flags & SF_ERR_MASK)) |
| 603 | s->flags |= SF_ERR_PRXCOND; |
| 604 | if (!(s->flags & SF_FINST_MASK)) |
| 605 | s->flags |= SF_FINST_R; |
| 606 | |
| 607 | req->analysers &= AN_REQ_FLT_END; |
| 608 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 609 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 610 | DBG_TRACE_DEVEL("leaving on error", |
| 611 | 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] | 612 | return 0; |
| 613 | |
| 614 | return_prx_yield: |
| 615 | channel_dont_connect(req); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 616 | DBG_TRACE_DEVEL("waiting for more data", |
| 617 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | /* This function performs all the processing enabled for the current request. |
| 622 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 623 | * needs more data, encounters an error, or wants to immediately abort the |
| 624 | * request. It relies on buffers flags, and updates s->req.analysers. |
| 625 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 626 | 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] | 627 | { |
| 628 | struct session *sess = s->sess; |
| 629 | struct http_txn *txn = s->txn; |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 630 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 631 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
| 632 | |
Christopher Faulet | 8bebd2f | 2020-10-06 17:54:56 +0200 | [diff] [blame] | 633 | 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] | 634 | |
| 635 | /* |
| 636 | * Right now, we know that we have processed the entire headers |
| 637 | * and that unwanted requests have been filtered out. We can do |
| 638 | * whatever we want with the remaining request. Also, now we |
| 639 | * may have separate values for ->fe, ->be. |
| 640 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 641 | htx = htxbuf(&req->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 642 | |
| 643 | /* |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 644 | * 7: Now we can work with the cookies. |
| 645 | * Note that doing so might move headers in the request, but |
| 646 | * the fields will stay coherent and the URI will not move. |
| 647 | * This should only be performed in the backend. |
| 648 | */ |
| 649 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 650 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 651 | |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 652 | /* 8: Generate unique ID if a "unique-id-format" is defined. |
| 653 | * |
| 654 | * A unique ID is generated even when it is not sent to ensure that the ID can make use of |
| 655 | * fetches only available in the HTTP request processing stage. |
| 656 | */ |
| 657 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 658 | 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] | 659 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 660 | if (!isttest(unique_id)) { |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 661 | if (!(s->flags & SF_ERR_MASK)) |
| 662 | s->flags |= SF_ERR_RESOURCE; |
| 663 | goto return_int_err; |
| 664 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 665 | |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 666 | /* send unique ID if a "unique-id-header" is defined */ |
Tim Duesterhus | 0643b0e | 2020-03-05 17:56:35 +0100 | [diff] [blame] | 667 | if (isttest(sess->fe->header_unique_id) && |
Tim Duesterhus | 7ad27d4 | 2022-05-18 00:22:15 +0200 | [diff] [blame] | 668 | unlikely(!http_add_header(htx, sess->fe->header_unique_id, unique_id))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 669 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | /* |
| 673 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 674 | * asks for it. |
| 675 | */ |
| 676 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | d68ff01 | 2022-05-27 08:57:21 +0200 | [diff] [blame] | 677 | const struct sockaddr_storage *src = sc_src(s->scf); |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 678 | struct http_hdr_ctx ctx = { .blk = NULL }; |
Tim Duesterhus | b50ab84 | 2022-03-05 00:52:41 +0100 | [diff] [blame] | 679 | 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] | 680 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 681 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 682 | http_find_header(htx, hdr, &ctx, 0)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 683 | /* The header is set to be added only if none is present |
| 684 | * and we found it, so don't do anything. |
| 685 | */ |
| 686 | } |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 687 | else if (src && src->ss_family == AF_INET) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 688 | /* Add an X-Forwarded-For header unless the source IP is |
| 689 | * in the 'except' network range. |
| 690 | */ |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 691 | if (ipcmp2net(src, &sess->fe->except_xff_net) && |
| 692 | ipcmp2net(src, &s->be->except_xff_net)) { |
| 693 | unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)src)->sin_addr; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 694 | |
| 695 | /* Note: we rely on the backend to get the header name to be used for |
| 696 | * x-forwarded-for, because the header is really meant for the backends. |
| 697 | * However, if the backend did not specify any option, we have to rely |
| 698 | * on the frontend's header name. |
| 699 | */ |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 700 | chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 701 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 702 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 703 | } |
| 704 | } |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 705 | else if (src && src->ss_family == AF_INET6) { |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 706 | /* Add an X-Forwarded-For header unless the source IP is |
| 707 | * in the 'except' network range. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 708 | */ |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 709 | if (ipcmp2net(src, &sess->fe->except_xff_net) && |
| 710 | ipcmp2net(src, &s->be->except_xff_net)) { |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 711 | char pn[INET6_ADDRSTRLEN]; |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 712 | |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 713 | inet_ntop(AF_INET6, |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 714 | (const void *)&((struct sockaddr_in6 *)(src))->sin6_addr, |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 715 | pn, sizeof(pn)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 716 | |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 717 | /* Note: we rely on the backend to get the header name to be used for |
| 718 | * x-forwarded-for, because the header is really meant for the backends. |
| 719 | * However, if the backend did not specify any option, we have to rely |
| 720 | * on the frontend's header name. |
| 721 | */ |
| 722 | chunk_printf(&trash, "%s", pn); |
| 723 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 724 | goto return_fail_rewrite; |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 725 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | * 10: add X-Original-To if either the frontend or the backend |
| 731 | * asks for it. |
| 732 | */ |
| 733 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Willy Tarreau | d68ff01 | 2022-05-27 08:57:21 +0200 | [diff] [blame] | 734 | const struct sockaddr_storage *dst = sc_dst(s->scf); |
Tim Duesterhus | e502c3e | 2022-03-05 00:52:42 +0100 | [diff] [blame] | 735 | 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] | 736 | |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 737 | if (dst && dst->ss_family == AF_INET) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 738 | /* Add an X-Original-To header unless the destination IP is |
| 739 | * in the 'except' network range. |
| 740 | */ |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 741 | if (ipcmp2net(dst, &sess->fe->except_xot_net) && |
| 742 | ipcmp2net(dst, &s->be->except_xot_net)) { |
| 743 | unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)dst)->sin_addr; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 744 | |
| 745 | /* Note: we rely on the backend to get the header name to be used for |
| 746 | * x-original-to, because the header is really meant for the backends. |
| 747 | * However, if the backend did not specify any option, we have to rely |
| 748 | * on the frontend's header name. |
| 749 | */ |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 750 | chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 751 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 752 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 753 | } |
| 754 | } |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 755 | else if (dst && dst->ss_family == AF_INET6) { |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 756 | /* Add an X-Original-To header unless the source IP is |
| 757 | * in the 'except' network range. |
| 758 | */ |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 759 | if (ipcmp2net(dst, &sess->fe->except_xot_net) && |
| 760 | ipcmp2net(dst, &s->be->except_xot_net)) { |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 761 | char pn[INET6_ADDRSTRLEN]; |
| 762 | |
| 763 | inet_ntop(AF_INET6, |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 764 | (const void *)&((struct sockaddr_in6 *)dst)->sin6_addr, |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 765 | pn, sizeof(pn)); |
| 766 | |
| 767 | /* Note: we rely on the backend to get the header name to be used for |
| 768 | * x-forwarded-for, because the header is really meant for the backends. |
| 769 | * However, if the backend did not specify any option, we have to rely |
| 770 | * on the frontend's header name. |
| 771 | */ |
| 772 | chunk_printf(&trash, "%s", pn); |
| 773 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 774 | goto return_fail_rewrite; |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 775 | } |
| 776 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 777 | } |
| 778 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 779 | /* Filter the request headers if there are filters attached to the |
| 780 | * stream. |
| 781 | */ |
| 782 | if (HAS_FILTERS(s)) |
| 783 | req->analysers |= AN_REQ_FLT_HTTP_HDRS; |
| 784 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 785 | /* If we have no server assigned yet and we're balancing on url_param |
| 786 | * with a POST request, we may be interested in checking the body for |
| 787 | * that parameter. This will be done in another analyser. |
| 788 | */ |
| 789 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | 089eaa0 | 2019-01-14 15:17:46 +0100 | [diff] [blame] | 790 | s->txn->meth == HTTP_METH_POST && |
| 791 | (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 792 | channel_dont_connect(req); |
| 793 | req->analysers |= AN_REQ_HTTP_BODY; |
| 794 | } |
| 795 | |
| 796 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 797 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 798 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 799 | /* We expect some data from the client. Unless we know for sure |
| 800 | * we already have a full request, we have to re-enable quick-ack |
| 801 | * in case we previously disabled it, otherwise we might cause |
| 802 | * the client to delay further data. |
| 803 | */ |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 804 | 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] | 805 | conn_set_quickack(cli_conn, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 806 | |
| 807 | /************************************************************* |
| 808 | * OK, that's finished for the headers. We have done what we * |
| 809 | * could. Let's switch to the DATA state. * |
| 810 | ************************************************************/ |
| 811 | req->analyse_exp = TICK_ETERNITY; |
| 812 | req->analysers &= ~an_bit; |
| 813 | |
| 814 | s->logs.tv_request = now; |
| 815 | /* OK let's go on with the BODY now */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 816 | 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] | 817 | return 1; |
| 818 | |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 819 | return_fail_rewrite: |
| 820 | if (!(s->flags & SF_ERR_MASK)) |
| 821 | s->flags |= SF_ERR_PRXCOND; |
| 822 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
| 823 | if (s->flags & SF_BE_ASSIGNED) |
| 824 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
| 825 | if (sess->listener && sess->listener->counters) |
| 826 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
| 827 | if (objt_server(s->target)) |
| 828 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
| 829 | /* fall through */ |
| 830 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 831 | return_int_err: |
| 832 | txn->status = 500; |
| 833 | if (!(s->flags & SF_ERR_MASK)) |
| 834 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 835 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 836 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 837 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 838 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 839 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 840 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 841 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 842 | |
| 843 | if (!(s->flags & SF_ERR_MASK)) |
| 844 | s->flags |= SF_ERR_PRXCOND; |
| 845 | if (!(s->flags & SF_FINST_MASK)) |
| 846 | s->flags |= SF_FINST_R; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 847 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 848 | DBG_TRACE_DEVEL("leaving on error", |
| 849 | 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] | 850 | return 0; |
| 851 | } |
| 852 | |
| 853 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 854 | * returns zero, at the beginning because it prevents any other processing |
| 855 | * from occurring, and at the end because it terminates the request. |
| 856 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 857 | 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] | 858 | { |
| 859 | struct http_txn *txn = s->txn; |
| 860 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 861 | 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] | 862 | /* This connection is being tarpitted. The CLIENT side has |
| 863 | * already set the connect expiration date to the right |
| 864 | * timeout. We just have to check that the client is still |
| 865 | * there and that the timeout has not expired. |
| 866 | */ |
| 867 | channel_dont_connect(req); |
| 868 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 869 | !tick_is_expired(req->analyse_exp, now_ms)) { |
Christopher Faulet | b0c87f1 | 2021-10-29 14:37:07 +0200 | [diff] [blame] | 870 | /* Be sure to drain all data from the request channel */ |
| 871 | channel_htx_erase(req, htxbuf(&req->buf)); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 872 | DBG_TRACE_DEVEL("waiting for tarpit timeout expiry", |
| 873 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 874 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 875 | } |
| 876 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 877 | |
| 878 | /* We will set the queue timer to the time spent, just for |
| 879 | * logging purposes. We fake a 500 server error, so that the |
| 880 | * attacker will not suspect his connection has been tarpitted. |
| 881 | * It will not cause trouble to the logs because we can exclude |
| 882 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 883 | */ |
| 884 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 885 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 886 | 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] | 887 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 888 | if (!(s->flags & SF_ERR_MASK)) |
| 889 | s->flags |= SF_ERR_PRXCOND; |
| 890 | if (!(s->flags & SF_FINST_MASK)) |
| 891 | s->flags |= SF_FINST_T; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 892 | |
| 893 | 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] | 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 898 | * for either the buffer to be full, or the full advertised contents to have |
| 899 | * reached the buffer. It must only be called after the standard HTTP request |
| 900 | * processing has occurred, because it expects the request to be parsed and will |
| 901 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 902 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 903 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 904 | * needs to read more data, or 1 once it has completed its analysis. |
| 905 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 906 | 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] | 907 | { |
| 908 | struct session *sess = s->sess; |
| 909 | struct http_txn *txn = s->txn; |
| 910 | struct http_msg *msg = &s->txn->req; |
| 911 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 912 | 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] | 913 | |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 914 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 915 | switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) { |
| 916 | case HTTP_RULE_RES_CONT: |
| 917 | goto http_end; |
| 918 | case HTTP_RULE_RES_YIELD: |
| 919 | goto missing_data_or_waiting; |
| 920 | case HTTP_RULE_RES_BADREQ: |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 921 | goto return_bad_req; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 922 | case HTTP_RULE_RES_ERROR: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 923 | goto return_int_err; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 924 | case HTTP_RULE_RES_ABRT: |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 925 | goto return_prx_cond; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 926 | default: |
| 927 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | http_end: |
| 931 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 932 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 933 | req->analysers &= ~an_bit; |
| 934 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 935 | 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] | 936 | return 1; |
| 937 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 938 | missing_data_or_waiting: |
| 939 | channel_dont_connect(req); |
| 940 | DBG_TRACE_DEVEL("waiting for more data", |
| 941 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 942 | return 0; |
| 943 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 944 | return_int_err: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 945 | txn->status = 500; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 946 | if (!(s->flags & SF_ERR_MASK)) |
| 947 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 948 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 949 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 950 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 951 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 952 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 953 | goto return_prx_err; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 954 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 955 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 956 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 957 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 958 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 959 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 960 | /* fall through */ |
| 961 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 962 | return_prx_err: |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 963 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 964 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 965 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 966 | return_prx_cond: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 967 | if (!(s->flags & SF_ERR_MASK)) |
| 968 | s->flags |= SF_ERR_PRXCOND; |
| 969 | if (!(s->flags & SF_FINST_MASK)) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 970 | 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] | 971 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 972 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 973 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 974 | DBG_TRACE_DEVEL("leaving on error", |
| 975 | 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] | 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | /* This function is an analyser which forwards request body (including chunk |
| 980 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 981 | * zero byte. The only situation where it must not be called is when we're in |
| 982 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 983 | * remaining data and to resync after end of body. It expects the msg_state to |
| 984 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 985 | * read more data, or 1 once we can go on with next request or end the stream. |
| 986 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
| 987 | * bytes of pending data + the headers if not already done. |
| 988 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 989 | 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] | 990 | { |
| 991 | struct session *sess = s->sess; |
| 992 | struct http_txn *txn = s->txn; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 993 | struct http_msg *msg = &txn->req; |
| 994 | struct htx *htx; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 995 | short status = 0; |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 996 | int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 997 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 998 | 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] | 999 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1000 | htx = htxbuf(&req->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1001 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1002 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 1003 | goto return_bad_req; |
| 1004 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 1005 | goto return_int_err; |
| 1006 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1007 | if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
| 1008 | ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) { |
| 1009 | /* Output closed while we were sending data. We must abort and |
| 1010 | * wake the other side up. |
Christopher Faulet | f506d96 | 2021-04-27 10:56:28 +0200 | [diff] [blame] | 1011 | * |
| 1012 | * If we have finished to send the request and the response is |
| 1013 | * still in progress, don't catch write error on the request |
| 1014 | * 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] | 1015 | */ |
Christopher Faulet | f506d96 | 2021-04-27 10:56:28 +0200 | [diff] [blame] | 1016 | if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers) |
| 1017 | return 0; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1018 | |
Olivier Houchard | 29cac3c | 2019-07-12 15:48:58 +0200 | [diff] [blame] | 1019 | /* Don't abort yet if we had L7 retries activated and it |
| 1020 | * was a write error, we may recover. |
| 1021 | */ |
| 1022 | if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) && |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1023 | (txn->flags & TX_L7_RETRY)) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1024 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1025 | 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] | 1026 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1027 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1028 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1029 | http_end_request(s); |
| 1030 | http_end_response(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1031 | DBG_TRACE_DEVEL("leaving on error", |
| 1032 | 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] | 1033 | return 1; |
| 1034 | } |
| 1035 | |
| 1036 | /* Note that we don't have to send 100-continue back because we don't |
| 1037 | * need the data to complete our job, and it's up to the server to |
| 1038 | * decide whether to return 100, 417 or anything else in return of |
| 1039 | * an "Expect: 100-continue" header. |
| 1040 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1041 | if (msg->msg_state == HTTP_MSG_BODY) |
| 1042 | msg->msg_state = HTTP_MSG_DATA; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1043 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1044 | /* in most states, we should abort in case of early close */ |
| 1045 | channel_auto_close(req); |
| 1046 | |
| 1047 | if (req->to_forward) { |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 1048 | if (req->to_forward == CHN_INFINITE_FORWARD) { |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1049 | if (req->flags & CF_EOI) |
| 1050 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 1051 | } |
| 1052 | else { |
| 1053 | /* We can't process the buffer's contents yet */ |
| 1054 | req->flags |= CF_WAKE_WRITE; |
| 1055 | goto missing_data_or_waiting; |
| 1056 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1057 | } |
| 1058 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1059 | if (msg->msg_state >= HTTP_MSG_ENDING) |
| 1060 | goto ending; |
| 1061 | |
| 1062 | if (txn->meth == HTTP_METH_CONNECT) { |
| 1063 | msg->msg_state = HTTP_MSG_ENDING; |
| 1064 | goto ending; |
| 1065 | } |
| 1066 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1067 | /* Forward input data. We get it by removing all outgoing data not |
| 1068 | * forwarded yet from HTX data size. If there are some data filters, we |
| 1069 | * let them decide the amount of data to forward. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +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_payload(s, msg, htx->data); |
| 1073 | if (ret < 0) |
| 1074 | goto return_bad_req; |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1075 | c_adv(req, ret); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1076 | } |
| 1077 | else { |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1078 | c_adv(req, htx->data - co_data(req)); |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 1079 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
| 1080 | channel_htx_forward_forever(req, htx); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1081 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1082 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1083 | if (htx->data != co_data(req)) |
| 1084 | goto missing_data_or_waiting; |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1085 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1086 | /* 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] | 1087 | * in HTTP_MSG_ENDING state. Then if all data was marked to be |
| 1088 | * forwarded, set the state to HTTP_MSG_DONE. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1089 | */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 1090 | if (!(htx->flags & HTX_FL_EOM)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1091 | goto missing_data_or_waiting; |
| 1092 | |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1093 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1094 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1095 | ending: |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 1096 | req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */ |
| 1097 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1098 | /* other states, ENDING...TUNNEL */ |
| 1099 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 1100 | goto done; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1101 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1102 | if (HAS_REQ_DATA_FILTERS(s)) { |
| 1103 | ret = flt_http_end(s, msg); |
| 1104 | if (ret <= 0) { |
| 1105 | if (!ret) |
| 1106 | goto missing_data_or_waiting; |
| 1107 | goto return_bad_req; |
| 1108 | } |
| 1109 | } |
| 1110 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1111 | if (txn->meth == HTTP_METH_CONNECT) |
| 1112 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 1113 | else { |
| 1114 | msg->msg_state = HTTP_MSG_DONE; |
| 1115 | req->to_forward = 0; |
| 1116 | } |
| 1117 | |
| 1118 | done: |
| 1119 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 1120 | if (!(txn->flags & TX_CON_WANT_TUN)) |
| 1121 | channel_dont_close(req); |
| 1122 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1123 | http_end_request(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1124 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1125 | http_end_response(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1126 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 1127 | if (req->flags & CF_SHUTW) { |
| 1128 | /* request errors are most likely due to the |
| 1129 | * server aborting the transfer. */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1130 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1131 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1132 | goto return_bad_req; |
| 1133 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1134 | 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] | 1135 | return 1; |
| 1136 | } |
| 1137 | |
| 1138 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 1139 | * the client's connection and forward any shutdown notification to the |
| 1140 | * server, which will decide whether to close or to go on processing the |
| 1141 | * request. We only do that in tunnel mode, and not in other modes since |
| 1142 | * it can be abused to exhaust source ports. */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 1143 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1144 | channel_auto_read(req); |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 1145 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN)) |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1146 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1147 | channel_auto_close(req); |
| 1148 | } |
| 1149 | else if (s->txn->meth == HTTP_METH_POST) { |
| 1150 | /* POST requests may require to read extra CRLF sent by broken |
| 1151 | * browsers and which could cause an RST to be sent upon close |
| 1152 | * on some systems (eg: Linux). */ |
| 1153 | channel_auto_read(req); |
| 1154 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1155 | DBG_TRACE_DEVEL("waiting for the end of the HTTP txn", |
| 1156 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1157 | return 0; |
| 1158 | |
| 1159 | missing_data_or_waiting: |
| 1160 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1161 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1162 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1163 | |
| 1164 | waiting: |
| 1165 | /* waiting for the last bits to leave the buffer */ |
| 1166 | if (req->flags & CF_SHUTW) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1167 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1168 | |
| 1169 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 1170 | * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE. |
| 1171 | * And when content-length is used, we never want to let the possible |
| 1172 | * shutdown be forwarded to the other side, as the state machine will |
| 1173 | * take care of it once the client responds. It's also important to |
| 1174 | * prevent TIME_WAITs from accumulating on the backend side, and for |
| 1175 | * HTTP/2 where the last frame comes with a shutdown. |
| 1176 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1177 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1178 | channel_dont_close(req); |
| 1179 | |
| 1180 | /* We know that more data are expected, but we couldn't send more that |
| 1181 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
| 1182 | * system knows it must not set a PUSH on this first part. Interactive |
| 1183 | * modes are already handled by the stream sock layer. We must not do |
| 1184 | * this in content-length mode because it could present the MSG_MORE |
| 1185 | * flag with the last block of forwarded data, which would cause an |
| 1186 | * additional delay to be observed by the receiver. |
| 1187 | */ |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 1188 | if (HAS_REQ_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1189 | req->flags |= CF_EXPECT_MORE; |
| 1190 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1191 | DBG_TRACE_DEVEL("waiting for more data to forward", |
| 1192 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1193 | return 0; |
| 1194 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1195 | return_cli_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1196 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 1197 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1198 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1199 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1200 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1201 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1202 | if (!(s->flags & SF_ERR_MASK)) |
| 1203 | s->flags |= SF_ERR_CLICL; |
| 1204 | status = 400; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1205 | goto return_prx_cond; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1206 | |
| 1207 | return_srv_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1208 | _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts); |
| 1209 | _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1210 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1211 | _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1212 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1213 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1214 | if (!(s->flags & SF_ERR_MASK)) |
| 1215 | s->flags |= SF_ERR_SRVCL; |
| 1216 | status = 502; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1217 | goto return_prx_cond; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1218 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1219 | return_int_err: |
| 1220 | if (!(s->flags & SF_ERR_MASK)) |
| 1221 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1222 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 1223 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1224 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1225 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1226 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1227 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1228 | status = 500; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1229 | goto return_prx_cond; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1230 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1231 | return_bad_req: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1232 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1233 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1234 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1235 | status = 400; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1236 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1237 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1238 | return_prx_cond: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1239 | if (txn->status > 0) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1240 | /* 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] | 1241 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1242 | } else { |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1243 | txn->status = status; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1244 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1245 | } |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1246 | if (!(s->flags & SF_ERR_MASK)) |
| 1247 | s->flags |= SF_ERR_PRXCOND; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1248 | if (!(s->flags & SF_FINST_MASK)) |
| 1249 | 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] | 1250 | DBG_TRACE_DEVEL("leaving on error ", |
| 1251 | 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] | 1252 | return 0; |
| 1253 | } |
| 1254 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1255 | /* Reset the stream and the backend stream connector to a situation suitable for attemption connection */ |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1256 | /* Returns 0 if we can attempt to retry, -1 otherwise */ |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 1257 | static __inline int do_l7_retry(struct stream *s, struct stconn *sc) |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1258 | { |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1259 | struct channel *req, *res; |
| 1260 | int co_data; |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1261 | |
Christopher Faulet | 731c8e6 | 2022-03-29 16:08:44 +0200 | [diff] [blame] | 1262 | s->conn_retries++; |
| 1263 | if (s->conn_retries >= s->be->conn_retries) |
Christopher Faulet | 552601d | 2021-05-26 10:31:06 +0200 | [diff] [blame] | 1264 | return -1; |
Christopher Faulet | 5b82cc5 | 2020-10-12 15:18:50 +0200 | [diff] [blame] | 1265 | |
Christopher Faulet | e763c8c | 2021-05-05 18:23:59 +0200 | [diff] [blame] | 1266 | if (objt_server(s->target)) { |
| 1267 | if (s->flags & SF_CURR_SESS) { |
| 1268 | s->flags &= ~SF_CURR_SESS; |
| 1269 | _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess); |
| 1270 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1271 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries); |
Christopher Faulet | e763c8c | 2021-05-05 18:23:59 +0200 | [diff] [blame] | 1272 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1273 | _HA_ATOMIC_INC(&s->be->be_counters.retries); |
Willy Tarreau | 223995e | 2019-05-04 10:38:31 +0200 | [diff] [blame] | 1274 | |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1275 | req = &s->req; |
| 1276 | res = &s->res; |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1277 | /* Remove any write error from the request, and read error from the response */ |
| 1278 | req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW); |
| 1279 | 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] | 1280 | res->analysers &= AN_RES_FLT_END; |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 1281 | s->conn_err_type = STRM_ET_NONE; |
Christopher Faulet | ae024ce | 2022-03-29 19:02:31 +0200 | [diff] [blame] | 1282 | s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK); |
| 1283 | s->conn_exp = TICK_ETERNITY; |
Olivier Houchard | 4bd5867 | 2019-07-12 16:16:59 +0200 | [diff] [blame] | 1284 | stream_choose_redispatch(s); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1285 | res->rex = TICK_ETERNITY; |
| 1286 | res->to_forward = 0; |
| 1287 | res->analyse_exp = TICK_ETERNITY; |
| 1288 | res->total = 0; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 1289 | |
Willy Tarreau | 19c65a9 | 2022-05-27 08:49:24 +0200 | [diff] [blame] | 1290 | if (sc_reset_endp(s->scb) < 0) { |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 1291 | if (!(s->flags & SF_ERR_MASK)) |
| 1292 | s->flags |= SF_ERR_INTERNAL; |
| 1293 | return -1; |
| 1294 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1295 | |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1296 | b_free(&req->buf); |
| 1297 | /* Swap the L7 buffer with the channel buffer */ |
| 1298 | /* 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] | 1299 | co_data = b_data(&s->txn->l7_buffer); |
| 1300 | b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer)); |
| 1301 | 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] | 1302 | co_set_data(req, co_data); |
Christopher Faulet | 5b82cc5 | 2020-10-12 15:18:50 +0200 | [diff] [blame] | 1303 | |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 1304 | 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] | 1305 | |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1306 | b_reset(&res->buf); |
| 1307 | co_set_data(res, 0); |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1311 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 1312 | * processing can continue on next analysers, or zero if it either needs more |
| 1313 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 1314 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers |
| 1315 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1316 | * abort. |
| 1317 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1318 | 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] | 1319 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1320 | /* |
| 1321 | * We will analyze a complete HTTP response to check the its syntax. |
| 1322 | * |
| 1323 | * Once the start line and all headers are received, we may perform a |
| 1324 | * capture of the error (if any), and we will set a few fields. We also |
| 1325 | * logging and finally headers capture. |
| 1326 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1327 | struct session *sess = s->sess; |
| 1328 | struct http_txn *txn = s->txn; |
| 1329 | struct http_msg *msg = &txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1330 | struct htx *htx; |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1331 | struct connection *srv_conn; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1332 | struct htx_sl *sl; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1333 | int n; |
| 1334 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1335 | 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] | 1336 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1337 | htx = htxbuf(&rep->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1338 | |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1339 | /* Parsing errors are caught here */ |
| 1340 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 1341 | goto return_bad_res; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1342 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 1343 | goto return_int_err; |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1344 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1345 | /* |
| 1346 | * Now we quickly check if we have found a full valid response. |
| 1347 | * If not so, we check the FD and buffer states before leaving. |
| 1348 | * A full response is indicated by the fact that we have seen |
| 1349 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
| 1350 | * responses are checked first. |
| 1351 | * |
| 1352 | * Depending on whether the client is still there or not, we |
| 1353 | * may send an error response back or not. Note that normally |
| 1354 | * we should only check for HTTP status there, and check I/O |
| 1355 | * errors somewhere else. |
| 1356 | */ |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1357 | next_one: |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 1358 | if (unlikely(htx_is_empty(htx) || htx->first == -1)) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1359 | /* 1: have we encountered a read error ? */ |
| 1360 | if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 1361 | struct connection *conn = sc_conn(s->scb); |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1362 | |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1363 | /* Perform a L7 retry because server refuses the early data. */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1364 | if ((txn->flags & TX_L7_RETRY) && |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1365 | (s->be->retry_type & PR_RE_EARLY_ERROR) && |
| 1366 | conn && conn->err_code == CO_ER_SSL_EARLY_FAILED && |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1367 | do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1368 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1369 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 1370 | return 0; |
| 1371 | } |
| 1372 | |
Olivier Houchard | 6db1699 | 2019-05-17 15:40:49 +0200 | [diff] [blame] | 1373 | if (txn->flags & TX_NOT_FIRST) |
| 1374 | goto abort_keep_alive; |
| 1375 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1376 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1377 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1378 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1379 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1380 | } |
| 1381 | |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1382 | /* if the server refused the early data, just send a 425 */ |
| 1383 | if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1384 | txn->status = 425; |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1385 | else { |
| 1386 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1387 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1388 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1389 | |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1390 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1391 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1392 | |
| 1393 | if (!(s->flags & SF_ERR_MASK)) |
| 1394 | s->flags |= SF_ERR_SRVCL; |
| 1395 | if (!(s->flags & SF_FINST_MASK)) |
| 1396 | s->flags |= SF_FINST_H; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1397 | DBG_TRACE_DEVEL("leaving on error", |
| 1398 | 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] | 1399 | return 0; |
| 1400 | } |
| 1401 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1402 | /* 2: read timeout : return a 504 to the client. */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1403 | else if (rep->flags & CF_READ_TIMEOUT) { |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1404 | if ((txn->flags & TX_L7_RETRY) && |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1405 | (s->be->retry_type & PR_RE_TIMEOUT)) { |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1406 | if (co_data(rep) || do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1407 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1408 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1409 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1410 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1411 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1412 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1413 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1414 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1415 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1416 | } |
| 1417 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1418 | txn->status = 504; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1419 | stream_inc_http_fail_ctr(s); |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1420 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1421 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1422 | |
| 1423 | if (!(s->flags & SF_ERR_MASK)) |
| 1424 | s->flags |= SF_ERR_SRVTO; |
| 1425 | if (!(s->flags & SF_FINST_MASK)) |
| 1426 | s->flags |= SF_FINST_H; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1427 | DBG_TRACE_DEVEL("leaving on error", |
| 1428 | 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] | 1429 | return 0; |
| 1430 | } |
| 1431 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1432 | /* 3: client abort with an abortonclose */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1433 | 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] | 1434 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 1435 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1436 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1437 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1438 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1439 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1440 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1441 | txn->status = 400; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1442 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1443 | |
| 1444 | if (!(s->flags & SF_ERR_MASK)) |
| 1445 | s->flags |= SF_ERR_CLICL; |
| 1446 | if (!(s->flags & SF_FINST_MASK)) |
| 1447 | s->flags |= SF_FINST_H; |
| 1448 | |
| 1449 | /* process_stream() will take care of the error */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1450 | DBG_TRACE_DEVEL("leaving on error", |
| 1451 | 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] | 1452 | return 0; |
| 1453 | } |
| 1454 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1455 | /* 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] | 1456 | else if (rep->flags & CF_SHUTR) { |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1457 | if ((txn->flags & TX_L7_RETRY) && |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1458 | (s->be->retry_type & PR_RE_DISCONNECTED)) { |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1459 | if (co_data(rep) || do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1460 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1461 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1462 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1463 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1464 | } |
| 1465 | |
Olivier Houchard | 6db1699 | 2019-05-17 15:40:49 +0200 | [diff] [blame] | 1466 | if (txn->flags & TX_NOT_FIRST) |
| 1467 | goto abort_keep_alive; |
| 1468 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1469 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1470 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1471 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1472 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1473 | } |
| 1474 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1475 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1476 | stream_inc_http_fail_ctr(s); |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1477 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1478 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1479 | |
| 1480 | if (!(s->flags & SF_ERR_MASK)) |
| 1481 | s->flags |= SF_ERR_SRVCL; |
| 1482 | if (!(s->flags & SF_FINST_MASK)) |
| 1483 | s->flags |= SF_FINST_H; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1484 | DBG_TRACE_DEVEL("leaving on error", |
| 1485 | 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] | 1486 | return 0; |
| 1487 | } |
| 1488 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1489 | /* 5: write error to client (we don't send any message then) */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1490 | else if (rep->flags & CF_WRITE_ERROR) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1491 | if (txn->flags & TX_NOT_FIRST) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1492 | goto abort_keep_alive; |
| 1493 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1494 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1495 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1496 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1497 | rep->analysers &= AN_RES_FLT_END; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1498 | |
| 1499 | if (!(s->flags & SF_ERR_MASK)) |
| 1500 | s->flags |= SF_ERR_CLICL; |
| 1501 | if (!(s->flags & SF_FINST_MASK)) |
| 1502 | s->flags |= SF_FINST_H; |
| 1503 | |
| 1504 | /* process_stream() will take care of the error */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1505 | DBG_TRACE_DEVEL("leaving on error", |
| 1506 | 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] | 1507 | return 0; |
| 1508 | } |
| 1509 | |
| 1510 | channel_dont_close(rep); |
| 1511 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1512 | DBG_TRACE_DEVEL("waiting for more data", |
| 1513 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1514 | return 0; |
| 1515 | } |
| 1516 | |
| 1517 | /* More interesting part now : we know that we have a complete |
| 1518 | * response which at least looks like HTTP. We have an indicator |
| 1519 | * of each header's length, so we can parse them quickly. |
| 1520 | */ |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 1521 | BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1522 | sl = http_get_stline(htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1523 | |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1524 | /* Perform a L7 retry because of the status code */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1525 | if ((txn->flags & TX_L7_RETRY) && |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1526 | l7_status_match(s->be, sl->info.res.status) && |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1527 | do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1528 | DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 1529 | return 0; |
| 1530 | } |
| 1531 | |
| 1532 | /* Now, L7 buffer is useless, it can be released */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1533 | b_free(&txn->l7_buffer); |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1534 | |
| 1535 | msg->msg_state = HTTP_MSG_BODY; |
| 1536 | |
| 1537 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1538 | /* 0: we might have to print this header in debug mode */ |
| 1539 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1540 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 1541 | int32_t pos; |
| 1542 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1543 | http_debug_stline("srvrep", s, sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1544 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 1545 | 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] | 1546 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 1547 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 1548 | |
| 1549 | if (type == HTX_BLK_EOH) |
| 1550 | break; |
| 1551 | if (type != HTX_BLK_HDR) |
| 1552 | continue; |
| 1553 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1554 | http_debug_hdr("srvhdr", s, |
| 1555 | htx_get_blk_name(htx, blk), |
| 1556 | htx_get_blk_value(htx, blk)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1557 | } |
| 1558 | } |
| 1559 | |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1560 | /* 1: get the status code and the version. Also set HTTP flags */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1561 | txn->status = sl->info.res.status; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1562 | if (sl->flags & HTX_SL_F_VER_11) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1563 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1564 | if (sl->flags & HTX_SL_F_XFER_LEN) { |
| 1565 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 1566 | if (sl->flags & HTX_SL_F_CLEN) |
| 1567 | msg->flags |= HTTP_MSGF_CNT_LEN; |
| 1568 | else if (sl->flags & HTX_SL_F_CHNK) |
| 1569 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1570 | } |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 1571 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 1572 | msg->flags |= HTTP_MSGF_BODYLESS; |
Christopher Faulet | 576c358 | 2021-01-08 15:53:01 +0100 | [diff] [blame] | 1573 | if (sl->flags & HTX_SL_F_CONN_UPG) |
| 1574 | msg->flags |= HTTP_MSGF_CONN_UPG; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1575 | |
| 1576 | n = txn->status / 100; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1577 | if (n < 1 || n > 5) |
| 1578 | n = 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1579 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1580 | /* when the client triggers a 4xx from the server, it's most often due |
| 1581 | * to a missing object or permission. These events should be tracked |
| 1582 | * because if they happen often, it may indicate a brute force or a |
| 1583 | * vulnerability scan. |
| 1584 | */ |
| 1585 | if (n == 4) |
| 1586 | stream_inc_http_err_ctr(s); |
| 1587 | |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1588 | if (n == 5 && txn->status != 501 && txn->status != 505) |
| 1589 | stream_inc_http_fail_ctr(s); |
| 1590 | |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 1591 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1592 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]); |
| 1593 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req); |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 1594 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1595 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1596 | /* Adjust server's health based on status code. Note: status codes 501 |
| 1597 | * and 505 are triggered on demand by client request, so we must not |
| 1598 | * count them as server failures. |
| 1599 | */ |
| 1600 | if (objt_server(s->target)) { |
| 1601 | 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] | 1602 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1603 | else |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1604 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1605 | } |
| 1606 | |
| 1607 | /* |
| 1608 | * We may be facing a 100-continue response, or any other informational |
| 1609 | * 1xx response which is non-final, in which case this is not the right |
| 1610 | * response, and we're waiting for the next one. Let's allow this response |
| 1611 | * to go to the client and wait for the next one. There's an exception for |
| 1612 | * 101 which is used later in the code to switch protocols. |
| 1613 | */ |
| 1614 | if (txn->status < 200 && |
| 1615 | (txn->status == 100 || txn->status >= 102)) { |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1616 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1617 | htx->first = channel_htx_fwd_headers(rep, htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1618 | msg->msg_state = HTTP_MSG_RPBEFORE; |
Christopher Faulet | 3499f62 | 2019-09-03 15:23:54 +0200 | [diff] [blame] | 1619 | msg->flags = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1620 | txn->status = 0; |
| 1621 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 7d51845 | 2020-08-31 11:07:07 +0200 | [diff] [blame] | 1622 | rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */ |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1623 | goto next_one; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1624 | } |
| 1625 | |
Christopher Faulet | 6e6c7b1 | 2021-01-08 16:02:05 +0100 | [diff] [blame] | 1626 | /* A 101-switching-protocols must contains a Connection header with the |
| 1627 | * "upgrade" option and the request too. It means both are agree to |
| 1628 | * upgrade. It is not so strict because there is no test on the Upgrade |
| 1629 | * header content. But it is probably stronger enough for now. |
| 1630 | */ |
| 1631 | if (txn->status == 101 && |
| 1632 | (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG))) |
| 1633 | goto return_bad_res; |
| 1634 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1635 | /* |
| 1636 | * 2: check for cacheability. |
| 1637 | */ |
| 1638 | |
| 1639 | switch (txn->status) { |
| 1640 | case 200: |
| 1641 | case 203: |
| 1642 | case 204: |
| 1643 | case 206: |
| 1644 | case 300: |
| 1645 | case 301: |
| 1646 | case 404: |
| 1647 | case 405: |
| 1648 | case 410: |
| 1649 | case 414: |
| 1650 | case 501: |
| 1651 | break; |
| 1652 | default: |
| 1653 | /* RFC7231#6.1: |
| 1654 | * Responses with status codes that are defined as |
| 1655 | * cacheable by default (e.g., 200, 203, 204, 206, |
| 1656 | * 300, 301, 404, 405, 410, 414, and 501 in this |
| 1657 | * specification) can be reused by a cache with |
| 1658 | * heuristic expiration unless otherwise indicated |
| 1659 | * by the method definition or explicit cache |
| 1660 | * controls [RFC7234]; all other status codes are |
| 1661 | * not cacheable by default. |
| 1662 | */ |
| 1663 | txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK); |
| 1664 | break; |
| 1665 | } |
| 1666 | |
| 1667 | /* |
| 1668 | * 3: we may need to capture headers |
| 1669 | */ |
| 1670 | s->logs.logwait &= ~LW_RESP; |
| 1671 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1672 | http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1673 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1674 | /* Skip parsing if no content length is possible. */ |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 1675 | 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] | 1676 | txn->status == 101)) { |
| 1677 | /* Either we've established an explicit tunnel, or we're |
| 1678 | * switching the protocol. In both cases, we're very unlikely |
| 1679 | * to understand the next protocols. We have to switch to tunnel |
| 1680 | * mode, so that we transfer the request and responses then let |
| 1681 | * this protocol pass unmodified. When we later implement specific |
| 1682 | * parsers for such protocols, we'll want to check the Upgrade |
| 1683 | * header which contains information about that protocol for |
| 1684 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 1685 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 1686 | txn->flags |= TX_CON_WANT_TUN; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1687 | } |
| 1688 | |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1689 | /* check for NTML authentication headers in 401 (WWW-Authenticate) and |
| 1690 | * 407 (Proxy-Authenticate) responses and set the connection to private |
| 1691 | */ |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 1692 | srv_conn = sc_conn(s->scb); |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1693 | if (srv_conn) { |
| 1694 | struct ist hdr; |
| 1695 | struct http_hdr_ctx ctx; |
| 1696 | |
| 1697 | if (txn->status == 401) |
| 1698 | hdr = ist("WWW-Authenticate"); |
| 1699 | else if (txn->status == 407) |
| 1700 | hdr = ist("Proxy-Authenticate"); |
| 1701 | else |
| 1702 | goto end; |
| 1703 | |
| 1704 | ctx.blk = NULL; |
| 1705 | while (http_find_header(htx, hdr, &ctx, 0)) { |
Willy Tarreau | f1dcced | 2020-05-07 19:27:02 +0200 | [diff] [blame] | 1706 | /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM", |
| 1707 | * possibly followed by blanks and a base64 string, the connection |
| 1708 | * is private. Since it's a mess to deal with, we only check for |
| 1709 | * values starting with "NTLM" or "Nego". Note that often multiple |
| 1710 | * headers are sent by the server there. |
| 1711 | */ |
| 1712 | if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) || |
Willy Tarreau | 49a1d28 | 2020-05-07 19:10:15 +0200 | [diff] [blame] | 1713 | (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) { |
Olivier Houchard | 250031e | 2019-05-29 15:01:50 +0200 | [diff] [blame] | 1714 | sess->flags |= SESS_FL_PREFER_LAST; |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 1715 | conn_set_owner(srv_conn, sess, NULL); |
Christopher Faulet | 21ddc74 | 2020-07-01 15:26:14 +0200 | [diff] [blame] | 1716 | conn_set_private(srv_conn); |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 1717 | /* 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] | 1718 | session_add_conn(srv_conn->owner, srv_conn, srv_conn->target); |
Willy Tarreau | f1dcced | 2020-05-07 19:27:02 +0200 | [diff] [blame] | 1719 | break; |
Olivier Houchard | 250031e | 2019-05-29 15:01:50 +0200 | [diff] [blame] | 1720 | } |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | end: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1725 | /* we want to have the response time before we start processing it */ |
| 1726 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 1727 | |
| 1728 | /* end of job, return OK */ |
| 1729 | rep->analysers &= ~an_bit; |
| 1730 | rep->analyse_exp = TICK_ETERNITY; |
| 1731 | channel_auto_close(rep); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1732 | 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] | 1733 | return 1; |
| 1734 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1735 | return_int_err: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1736 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 1737 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1738 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1739 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1740 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1741 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1742 | txn->status = 500; |
| 1743 | if (!(s->flags & SF_ERR_MASK)) |
| 1744 | s->flags |= SF_ERR_INTERNAL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1745 | goto return_prx_cond; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1746 | |
| 1747 | return_bad_res: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1748 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1749 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1750 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1751 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1752 | } |
Olivier Houchard | e3249a9 | 2019-05-03 23:01:47 +0200 | [diff] [blame] | 1753 | if ((s->be->retry_type & PR_RE_JUNK_REQUEST) && |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1754 | (txn->flags & TX_L7_RETRY) && |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1755 | do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1756 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1757 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | e3249a9 | 2019-05-03 23:01:47 +0200 | [diff] [blame] | 1758 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1759 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1760 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1761 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1762 | /* fall through */ |
| 1763 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1764 | return_prx_cond: |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1765 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1766 | |
| 1767 | if (!(s->flags & SF_ERR_MASK)) |
| 1768 | s->flags |= SF_ERR_PRXCOND; |
| 1769 | if (!(s->flags & SF_FINST_MASK)) |
| 1770 | s->flags |= SF_FINST_H; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1771 | |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1772 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1773 | DBG_TRACE_DEVEL("leaving on error", |
| 1774 | 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] | 1775 | return 0; |
| 1776 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1777 | abort_keep_alive: |
| 1778 | /* A keep-alive request to the server failed on a network error. |
| 1779 | * The client is required to retry. We need to close without returning |
| 1780 | * any other information so that the client retries. |
| 1781 | */ |
| 1782 | txn->status = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1783 | s->logs.logwait = 0; |
| 1784 | s->logs.level = 0; |
| 1785 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1786 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1787 | DBG_TRACE_DEVEL("leaving by closing K/A connection", |
| 1788 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1789 | return 0; |
| 1790 | } |
| 1791 | |
| 1792 | /* This function performs all the processing enabled for the current response. |
| 1793 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 1794 | * and updates s->res.analysers. It might make sense to explode it into several |
| 1795 | * other functions. It works like process_request (see indications above). |
| 1796 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1797 | 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] | 1798 | { |
| 1799 | struct session *sess = s->sess; |
| 1800 | struct http_txn *txn = s->txn; |
| 1801 | struct http_msg *msg = &txn->rsp; |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1802 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1803 | struct proxy *cur_proxy; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1804 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 1805 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1806 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 1807 | return 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1808 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1809 | 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] | 1810 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1811 | htx = htxbuf(&rep->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1812 | |
| 1813 | /* The stats applet needs to adjust the Connection header but we don't |
| 1814 | * apply any filter there. |
| 1815 | */ |
| 1816 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 1817 | rep->analysers &= ~an_bit; |
| 1818 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1819 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | /* |
| 1823 | * We will have to evaluate the filters. |
| 1824 | * As opposed to version 1.2, now they will be evaluated in the |
| 1825 | * filters order and not in the header order. This means that |
| 1826 | * each filter has to be validated among all headers. |
| 1827 | * |
| 1828 | * Filters are tried with ->be first, then with ->fe if it is |
| 1829 | * different from ->be. |
| 1830 | * |
| 1831 | * Maybe we are in resume condiion. In this case I choose the |
| 1832 | * "struct proxy" which contains the rule list matching the resume |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1833 | * pointer. If none of these "struct proxy" match, I initialise |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1834 | * the process with the first one. |
| 1835 | * |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1836 | * In fact, I check only correspondence between the current list |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1837 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 1838 | * the loop with the ->be. |
| 1839 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1840 | if (s->current_rule_list == &sess->fe->http_res_rules || |
| 1841 | (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] | 1842 | cur_proxy = sess->fe; |
| 1843 | else |
| 1844 | cur_proxy = s->be; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1845 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1846 | while (1) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1847 | /* evaluate http-response rules */ |
Christopher Faulet | 46f46df | 2021-11-09 16:33:25 +0100 | [diff] [blame] | 1848 | if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1849 | struct list *def_rules, *rules; |
| 1850 | |
| 1851 | def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL); |
| 1852 | rules = &cur_proxy->http_res_rules; |
| 1853 | |
| 1854 | ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1855 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1856 | switch (ret) { |
| 1857 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 1858 | goto return_prx_yield; |
| 1859 | |
| 1860 | case HTTP_RULE_RES_CONT: |
| 1861 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 1862 | break; |
| 1863 | |
| 1864 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 1865 | goto deny; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1866 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1867 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent */ |
| 1868 | goto return_prx_cond; |
| 1869 | |
| 1870 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1871 | goto done; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1872 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1873 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 1874 | goto return_bad_res; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1875 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1876 | case HTTP_RULE_RES_ERROR: /* failed with a bad request */ |
| 1877 | goto return_int_err; |
| 1878 | } |
| 1879 | |
| 1880 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1881 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1882 | /* check whether we're already working on the frontend */ |
| 1883 | if (cur_proxy == sess->fe) |
| 1884 | break; |
| 1885 | cur_proxy = sess->fe; |
| 1886 | } |
| 1887 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1888 | /* OK that's all we can do for 1xx responses */ |
| 1889 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1890 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1891 | |
| 1892 | /* |
| 1893 | * Now check for a server cookie. |
| 1894 | */ |
| 1895 | 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] | 1896 | http_manage_server_side_cookies(s, rep); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1897 | |
| 1898 | /* |
| 1899 | * Check for cache-control or pragma headers if required. |
| 1900 | */ |
| 1901 | 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] | 1902 | http_check_response_for_cacheability(s, rep); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1903 | |
| 1904 | /* |
| 1905 | * Add server cookie in the response if needed |
| 1906 | */ |
| 1907 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 1908 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
| 1909 | (!(s->flags & SF_DIRECT) || |
| 1910 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 1911 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 1912 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 1913 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 1914 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 1915 | !(s->flags & SF_IGNORE_PRST)) { |
| 1916 | /* the server is known, it's not the one the client requested, or the |
| 1917 | * cookie's last seen date needs to be refreshed. We have to |
| 1918 | * insert a set-cookie here, except if we want to insert only on POST |
| 1919 | * requests and this one isn't. Note that servers which don't have cookies |
| 1920 | * (eg: some backup servers) will return a full cookie removal request. |
| 1921 | */ |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1922 | if (!__objt_server(s->target)->cookie) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1923 | chunk_printf(&trash, |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1924 | "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1925 | s->be->cookie_name); |
| 1926 | } |
| 1927 | else { |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1928 | 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] | 1929 | |
| 1930 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 1931 | /* emit last_date, which is mandatory */ |
| 1932 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 1933 | s30tob64((date.tv_sec+3) >> 2, |
| 1934 | trash.area + trash.data); |
| 1935 | trash.data += 5; |
| 1936 | |
| 1937 | if (s->be->cookie_maxlife) { |
| 1938 | /* emit first_date, which is either the original one or |
| 1939 | * the current date. |
| 1940 | */ |
| 1941 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 1942 | s30tob64(txn->cookie_first_date ? |
| 1943 | txn->cookie_first_date >> 2 : |
| 1944 | (date.tv_sec+3) >> 2, |
| 1945 | trash.area + trash.data); |
| 1946 | trash.data += 5; |
| 1947 | } |
| 1948 | } |
| 1949 | chunk_appendf(&trash, "; path=/"); |
| 1950 | } |
| 1951 | |
| 1952 | if (s->be->cookie_domain) |
| 1953 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
| 1954 | |
| 1955 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 1956 | chunk_appendf(&trash, "; HttpOnly"); |
| 1957 | |
| 1958 | if (s->be->ck_opts & PR_CK_SECURE) |
| 1959 | chunk_appendf(&trash, "; Secure"); |
| 1960 | |
Christopher Faulet | 2f53390 | 2020-01-21 11:06:48 +0100 | [diff] [blame] | 1961 | if (s->be->cookie_attrs) |
| 1962 | chunk_appendf(&trash, "; %s", s->be->cookie_attrs); |
| 1963 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1964 | if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data)))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 1965 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1966 | |
| 1967 | txn->flags &= ~TX_SCK_MASK; |
| 1968 | if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
| 1969 | /* the server did not change, only the date was updated */ |
| 1970 | txn->flags |= TX_SCK_UPDATED; |
| 1971 | else |
| 1972 | txn->flags |= TX_SCK_INSERTED; |
| 1973 | |
| 1974 | /* Here, we will tell an eventual cache on the client side that we don't |
| 1975 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 1976 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 1977 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 1978 | */ |
| 1979 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
| 1980 | |
| 1981 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 1982 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1983 | if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private")))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 1984 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1985 | } |
| 1986 | } |
| 1987 | |
| 1988 | /* |
| 1989 | * Check if result will be cacheable with a cookie. |
| 1990 | * We'll block the response if security checks have caught |
| 1991 | * nasty things such as a cacheable cookie. |
| 1992 | */ |
| 1993 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 1994 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 1995 | (s->be->options & PR_O_CHK_CACHE)) { |
| 1996 | /* we're in presence of a cacheable response containing |
| 1997 | * a set-cookie header. We'll block it as requested by |
| 1998 | * the 'checkcache' option, and send an alert. |
| 1999 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2000 | 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] | 2001 | 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] | 2002 | send_log(s->be, LOG_ALERT, |
| 2003 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 2004 | 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] | 2005 | goto deny; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2006 | } |
| 2007 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2008 | end: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2009 | /* |
| 2010 | * Evaluate after-response rules before forwarding the response. rules |
| 2011 | * from the backend are evaluated first, then one from the frontend if |
| 2012 | * it differs. |
| 2013 | */ |
| 2014 | if (!http_eval_after_res_rules(s)) |
| 2015 | goto return_int_err; |
| 2016 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 2017 | /* Filter the response headers if there are filters attached to the |
| 2018 | * stream. |
| 2019 | */ |
| 2020 | if (HAS_FILTERS(s)) |
| 2021 | rep->analysers |= AN_RES_FLT_HTTP_HDRS; |
| 2022 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2023 | /* Always enter in the body analyzer */ |
| 2024 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 2025 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
| 2026 | |
| 2027 | /* if the user wants to log as soon as possible, without counting |
| 2028 | * bytes from the server, then this is the right moment. We have |
| 2029 | * to temporarily assign bytes_out to log what we currently have. |
| 2030 | */ |
| 2031 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
| 2032 | 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] | 2033 | s->logs.bytes_out = htx->data; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2034 | s->do_log(s); |
| 2035 | s->logs.bytes_out = 0; |
| 2036 | } |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2037 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2038 | done: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2039 | 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] | 2040 | rep->analysers &= ~an_bit; |
| 2041 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2042 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2043 | return 1; |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2044 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2045 | deny: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2046 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp); |
| 2047 | _HA_ATOMIC_INC(&s->be->be_counters.denied_resp); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2048 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2049 | _HA_ATOMIC_INC(&sess->listener->counters->denied_resp); |
Christopher Faulet | a08546b | 2019-12-16 16:07:34 +0100 | [diff] [blame] | 2050 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2051 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2052 | goto return_prx_err; |
| 2053 | |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 2054 | return_fail_rewrite: |
| 2055 | if (!(s->flags & SF_ERR_MASK)) |
| 2056 | s->flags |= SF_ERR_PRXCOND; |
| 2057 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
| 2058 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
| 2059 | if (sess->listener && sess->listener->counters) |
| 2060 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
| 2061 | if (objt_server(s->target)) |
| 2062 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
| 2063 | /* fall through */ |
| 2064 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2065 | return_int_err: |
| 2066 | txn->status = 500; |
| 2067 | if (!(s->flags & SF_ERR_MASK)) |
| 2068 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2069 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 2070 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
Dragan Dosen | 9a006f9 | 2021-09-21 13:02:09 +0200 | [diff] [blame] | 2071 | if (sess->listener && sess->listener->counters) |
| 2072 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 2073 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2074 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2075 | goto return_prx_err; |
| 2076 | |
| 2077 | return_bad_res: |
| 2078 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2079 | stream_inc_http_fail_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2080 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | a20a653 | 2020-02-05 10:16:41 +0100 | [diff] [blame] | 2081 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2082 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | a20a653 | 2020-02-05 10:16:41 +0100 | [diff] [blame] | 2083 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 2084 | } |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2085 | /* fall through */ |
| 2086 | |
| 2087 | return_prx_err: |
| 2088 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 2089 | /* fall through */ |
| 2090 | |
| 2091 | return_prx_cond: |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2092 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 2093 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2094 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2095 | if (!(s->flags & SF_ERR_MASK)) |
| 2096 | s->flags |= SF_ERR_PRXCOND; |
| 2097 | if (!(s->flags & SF_FINST_MASK)) |
| 2098 | s->flags |= SF_FINST_H; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2099 | |
Christopher Faulet | e58c000 | 2020-03-02 16:21:01 +0100 | [diff] [blame] | 2100 | rep->analysers &= AN_RES_FLT_END; |
| 2101 | s->req.analysers &= AN_REQ_FLT_END; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2102 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2103 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2104 | DBG_TRACE_DEVEL("leaving on error", |
| 2105 | 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] | 2106 | return 0; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2107 | |
| 2108 | return_prx_yield: |
| 2109 | channel_dont_close(rep); |
| 2110 | DBG_TRACE_DEVEL("waiting for more data", |
| 2111 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 2112 | return 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | /* This function is an analyser which forwards response body (including chunk |
| 2116 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 2117 | * zero byte. The only situation where it must not be called is when we're in |
| 2118 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 2119 | * remaining data and to resync after end of body. It expects the msg_state to |
| 2120 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 2121 | * read more data, or 1 once we can go on with next request or end the stream. |
| 2122 | * |
| 2123 | * It is capable of compressing response data both in content-length mode and |
| 2124 | * in chunked mode. The state machines follows different flows depending on |
| 2125 | * whether content-length and chunked modes are used, since there are no |
| 2126 | * trailers in content-length : |
| 2127 | * |
| 2128 | * chk-mode cl-mode |
| 2129 | * ,----- BODY -----. |
| 2130 | * / \ |
| 2131 | * V size > 0 V chk-mode |
| 2132 | * .--> SIZE -------------> DATA -------------> CRLF |
| 2133 | * | | size == 0 | last byte | |
| 2134 | * | v final crlf v inspected | |
| 2135 | * | TRAILERS -----------> DONE | |
| 2136 | * | | |
| 2137 | * `----------------------------------------------' |
| 2138 | * |
| 2139 | * Compression only happens in the DATA state, and must be flushed in final |
| 2140 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 2141 | * is performed at once on final states for all bytes parsed, or when leaving |
| 2142 | * on missing data. |
| 2143 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2144 | 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] | 2145 | { |
| 2146 | struct session *sess = s->sess; |
| 2147 | struct http_txn *txn = s->txn; |
| 2148 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2149 | struct htx *htx; |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2150 | int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2151 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2152 | 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] | 2153 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2154 | htx = htxbuf(&res->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2155 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2156 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 2157 | goto return_bad_res; |
| 2158 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 2159 | goto return_int_err; |
| 2160 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2161 | 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] | 2162 | ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2163 | /* Output closed while we were sending data. We must abort and |
| 2164 | * wake the other side up. |
| 2165 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2166 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2167 | http_end_response(s); |
| 2168 | http_end_request(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2169 | DBG_TRACE_DEVEL("leaving on error", |
| 2170 | 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] | 2171 | return 1; |
| 2172 | } |
| 2173 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2174 | if (msg->msg_state == HTTP_MSG_BODY) |
| 2175 | msg->msg_state = HTTP_MSG_DATA; |
| 2176 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2177 | /* in most states, we should abort in case of early close */ |
| 2178 | channel_auto_close(res); |
| 2179 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2180 | if (res->to_forward) { |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2181 | if (res->to_forward == CHN_INFINITE_FORWARD) { |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2182 | if (res->flags & CF_EOI) |
| 2183 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2184 | } |
| 2185 | else { |
| 2186 | /* We can't process the buffer's contents yet */ |
| 2187 | res->flags |= CF_WAKE_WRITE; |
| 2188 | goto missing_data_or_waiting; |
| 2189 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2190 | } |
| 2191 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2192 | if (msg->msg_state >= HTTP_MSG_ENDING) |
| 2193 | goto ending; |
| 2194 | |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 2195 | 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] | 2196 | (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) { |
| 2197 | msg->msg_state = HTTP_MSG_ENDING; |
| 2198 | goto ending; |
| 2199 | } |
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 | /* Forward input data. We get it by removing all outgoing data not |
| 2202 | * forwarded yet from HTX data size. If there are some data filters, we |
| 2203 | * let them decide the amount of data to forward. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2204 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2205 | if (HAS_RSP_DATA_FILTERS(s)) { |
| 2206 | ret = flt_http_payload(s, msg, htx->data); |
| 2207 | if (ret < 0) |
| 2208 | goto return_bad_res; |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 2209 | c_adv(res, ret); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2210 | } |
| 2211 | else { |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 2212 | c_adv(res, htx->data - co_data(res)); |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2213 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
| 2214 | channel_htx_forward_forever(res, htx); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2215 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2216 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2217 | if (htx->data != co_data(res)) |
| 2218 | goto missing_data_or_waiting; |
| 2219 | |
| 2220 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) { |
| 2221 | msg->msg_state = HTTP_MSG_ENDING; |
| 2222 | goto ending; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2223 | } |
| 2224 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2225 | /* 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] | 2226 | * in HTTP_MSG_ENDING state. Then if all data was marked to be |
| 2227 | * forwarded, set the state to HTTP_MSG_DONE. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2228 | */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2229 | if (!(htx->flags & HTX_FL_EOM)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2230 | goto missing_data_or_waiting; |
| 2231 | |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 2232 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2233 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2234 | ending: |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 2235 | res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */ |
| 2236 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2237 | /* other states, ENDING...TUNNEL */ |
| 2238 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 2239 | goto done; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2240 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2241 | if (HAS_RSP_DATA_FILTERS(s)) { |
| 2242 | ret = flt_http_end(s, msg); |
| 2243 | if (ret <= 0) { |
| 2244 | if (!ret) |
| 2245 | goto missing_data_or_waiting; |
| 2246 | goto return_bad_res; |
| 2247 | } |
| 2248 | } |
| 2249 | |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 2250 | 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] | 2251 | !(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 2252 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 2253 | goto ending; |
| 2254 | } |
| 2255 | else { |
| 2256 | msg->msg_state = HTTP_MSG_DONE; |
| 2257 | res->to_forward = 0; |
| 2258 | } |
| 2259 | |
| 2260 | done: |
| 2261 | |
| 2262 | channel_dont_close(res); |
| 2263 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2264 | http_end_response(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2265 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2266 | http_end_request(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2267 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2268 | if (res->flags & CF_SHUTW) { |
| 2269 | /* response errors are most likely due to the |
| 2270 | * client aborting the transfer. */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2271 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2272 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2273 | goto return_bad_res; |
| 2274 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2275 | 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] | 2276 | return 1; |
| 2277 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2278 | DBG_TRACE_DEVEL("waiting for the end of the HTTP txn", |
| 2279 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2280 | return 0; |
| 2281 | |
| 2282 | missing_data_or_waiting: |
| 2283 | if (res->flags & CF_SHUTW) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2284 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2285 | |
| 2286 | /* stop waiting for data if the input is closed before the end. If the |
| 2287 | * client side was already closed, it means that the client has aborted, |
| 2288 | * so we don't want to count this as a server abort. Otherwise it's a |
| 2289 | * server abort. |
| 2290 | */ |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 2291 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2292 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2293 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2294 | /* If we have some pending data, we continue the processing */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2295 | if (htx_is_empty(htx)) |
| 2296 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2297 | } |
| 2298 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2299 | /* When TE: chunked is used, we need to get there again to parse |
| 2300 | * 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] | 2301 | * set CF_DONTCLOSE. Similarly when there is a content-leng or if there |
| 2302 | * are filters registered on the stream, we don't want to forward a |
| 2303 | * close |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2304 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2305 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2306 | channel_dont_close(res); |
| 2307 | |
| 2308 | /* We know that more data are expected, but we couldn't send more that |
| 2309 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
| 2310 | * system knows it must not set a PUSH on this first part. Interactive |
| 2311 | * modes are already handled by the stream sock layer. We must not do |
| 2312 | * this in content-length mode because it could present the MSG_MORE |
| 2313 | * flag with the last block of forwarded data, which would cause an |
| 2314 | * additional delay to be observed by the receiver. |
| 2315 | */ |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 2316 | if (HAS_RSP_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2317 | res->flags |= CF_EXPECT_MORE; |
| 2318 | |
| 2319 | /* the stream handler will take care of timeouts and errors */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2320 | DBG_TRACE_DEVEL("waiting for more data to forward", |
| 2321 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2322 | return 0; |
| 2323 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2324 | return_srv_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2325 | _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts); |
| 2326 | _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2327 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2328 | _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2329 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2330 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts); |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2331 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2332 | if (!(s->flags & SF_ERR_MASK)) |
| 2333 | s->flags |= SF_ERR_SRVCL; |
| 2334 | goto return_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2335 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2336 | return_cli_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2337 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 2338 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2339 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2340 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2341 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2342 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2343 | if (!(s->flags & SF_ERR_MASK)) |
| 2344 | s->flags |= SF_ERR_CLICL; |
| 2345 | goto return_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2346 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2347 | return_int_err: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2348 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 2349 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2350 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2351 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2352 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2353 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2354 | if (!(s->flags & SF_ERR_MASK)) |
| 2355 | s->flags |= SF_ERR_INTERNAL; |
| 2356 | goto return_error; |
| 2357 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2358 | return_bad_res: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2359 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2360 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2361 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2362 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 2363 | } |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2364 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2365 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2366 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2367 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2368 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2369 | return_error: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2370 | /* don't send any error message as we're in the body */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2371 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2372 | if (!(s->flags & SF_FINST_MASK)) |
| 2373 | s->flags |= SF_FINST_D; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2374 | DBG_TRACE_DEVEL("leaving on error", |
| 2375 | 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] | 2376 | return 0; |
| 2377 | } |
| 2378 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2379 | /* Perform an HTTP redirect based on the information in <rule>. The function |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2380 | * returns zero in case of an irrecoverable error such as too large a request |
| 2381 | * to build a valid response, 1 in case of successful redirect (hence the rule |
| 2382 | * is final), or 2 if the rule has to be silently skipped. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2383 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2384 | 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] | 2385 | { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2386 | struct channel *req = &s->req; |
| 2387 | struct channel *res = &s->res; |
| 2388 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2389 | struct htx_sl *sl; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2390 | struct buffer *chunk; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2391 | struct ist status, reason, location; |
| 2392 | unsigned int flags; |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2393 | int ret = 1, close = 0; /* Try to keep the connection alive byt default */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2394 | |
| 2395 | chunk = alloc_trash_chunk(); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2396 | if (!chunk) { |
| 2397 | if (!(s->flags & SF_ERR_MASK)) |
| 2398 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2399 | goto fail; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2400 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2401 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2402 | /* |
| 2403 | * Create the location |
| 2404 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2405 | htx = htxbuf(&req->buf); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2406 | switch(rule->type) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2407 | case REDIRECT_TYPE_SCHEME: { |
| 2408 | struct http_hdr_ctx ctx; |
| 2409 | struct ist path, host; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2410 | struct http_uri_parser parser; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2411 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2412 | host = ist(""); |
| 2413 | ctx.blk = NULL; |
| 2414 | if (http_find_header(htx, ist("Host"), &ctx, 0)) |
| 2415 | host = ctx.value; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2416 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2417 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2418 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 2419 | path = http_parse_path(&parser); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2420 | /* build message using path */ |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 2421 | if (isttest(path)) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2422 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2423 | int qs = 0; |
| 2424 | while (qs < path.len) { |
| 2425 | if (*(path.ptr + qs) == '?') { |
| 2426 | path.len = qs; |
| 2427 | break; |
| 2428 | } |
| 2429 | qs++; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2430 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2431 | } |
| 2432 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2433 | else |
| 2434 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2435 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2436 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2437 | /* add scheme */ |
| 2438 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2439 | goto fail; |
| 2440 | } |
| 2441 | else { |
| 2442 | /* add scheme with executing log format */ |
| 2443 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2444 | chunk->size - chunk->data, |
| 2445 | &rule->rdr_fmt); |
| 2446 | } |
| 2447 | /* add "://" + host + path */ |
| 2448 | if (!chunk_memcat(chunk, "://", 3) || |
| 2449 | !chunk_memcat(chunk, host.ptr, host.len) || |
| 2450 | !chunk_memcat(chunk, path.ptr, path.len)) |
| 2451 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2452 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2453 | /* append a slash at the end of the location if needed and missing */ |
| 2454 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2455 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2456 | if (chunk->data + 1 >= chunk->size) |
| 2457 | goto fail; |
| 2458 | chunk->area[chunk->data++] = '/'; |
| 2459 | } |
| 2460 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 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 | case REDIRECT_TYPE_PREFIX: { |
| 2464 | struct ist path; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2465 | struct http_uri_parser parser; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2466 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2467 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2468 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 2469 | path = http_parse_path(&parser); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2470 | /* build message using path */ |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 2471 | if (isttest(path)) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2472 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2473 | int qs = 0; |
| 2474 | while (qs < path.len) { |
| 2475 | if (*(path.ptr + qs) == '?') { |
| 2476 | path.len = qs; |
| 2477 | break; |
| 2478 | } |
| 2479 | qs++; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2480 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2481 | } |
| 2482 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2483 | else |
| 2484 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2485 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2486 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2487 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2488 | * add anything, otherwise it makes it hard for the user to |
| 2489 | * configure a self-redirection. |
| 2490 | */ |
| 2491 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 2492 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2493 | goto fail; |
| 2494 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2495 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2496 | else { |
| 2497 | /* add prefix with executing log format */ |
| 2498 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2499 | chunk->size - chunk->data, |
| 2500 | &rule->rdr_fmt); |
| 2501 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2502 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2503 | /* add path */ |
| 2504 | if (!chunk_memcat(chunk, path.ptr, path.len)) |
| 2505 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2506 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2507 | /* append a slash at the end of the location if needed and missing */ |
| 2508 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2509 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2510 | if (chunk->data + 1 >= chunk->size) |
| 2511 | goto fail; |
| 2512 | chunk->area[chunk->data++] = '/'; |
| 2513 | } |
| 2514 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2515 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2516 | case REDIRECT_TYPE_LOCATION: |
| 2517 | default: |
| 2518 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2519 | /* add location */ |
| 2520 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2521 | goto fail; |
| 2522 | } |
| 2523 | else { |
| 2524 | /* add location with executing log format */ |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2525 | int len = build_logline(s, chunk->area + chunk->data, |
| 2526 | chunk->size - chunk->data, |
| 2527 | &rule->rdr_fmt); |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2528 | if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) { |
| 2529 | ret = 2; |
| 2530 | goto out; |
| 2531 | } |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2532 | |
| 2533 | chunk->data += len; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2534 | } |
| 2535 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2536 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2537 | location = ist2(chunk->area, chunk->data); |
| 2538 | |
| 2539 | /* |
| 2540 | * Create the 30x response |
| 2541 | */ |
| 2542 | switch (rule->code) { |
| 2543 | case 308: |
| 2544 | status = ist("308"); |
| 2545 | reason = ist("Permanent Redirect"); |
| 2546 | break; |
| 2547 | case 307: |
| 2548 | status = ist("307"); |
| 2549 | reason = ist("Temporary Redirect"); |
| 2550 | break; |
| 2551 | case 303: |
| 2552 | status = ist("303"); |
| 2553 | reason = ist("See Other"); |
| 2554 | break; |
| 2555 | case 301: |
| 2556 | status = ist("301"); |
| 2557 | reason = ist("Moved Permanently"); |
| 2558 | break; |
| 2559 | case 302: |
| 2560 | default: |
| 2561 | status = ist("302"); |
| 2562 | reason = ist("Found"); |
| 2563 | break; |
| 2564 | } |
| 2565 | |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2566 | if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE) |
| 2567 | close = 1; |
| 2568 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2569 | htx = htx_from_buf(&res->buf); |
Kevin Zhu | 96b3639 | 2020-01-07 09:42:55 +0100 | [diff] [blame] | 2570 | /* Trim any possible response */ |
| 2571 | channel_htx_truncate(&s->res, htx); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2572 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 2573 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason); |
| 2574 | if (!sl) |
| 2575 | goto fail; |
| 2576 | sl->info.res.status = rule->code; |
| 2577 | s->txn->status = rule->code; |
| 2578 | |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2579 | if (close && !htx_add_header(htx, ist("Connection"), ist("close"))) |
| 2580 | goto fail; |
| 2581 | |
| 2582 | if (!htx_add_header(htx, ist("Content-length"), ist("0")) || |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2583 | !htx_add_header(htx, ist("Location"), location)) |
| 2584 | goto fail; |
| 2585 | |
| 2586 | if (rule->code == 302 || rule->code == 303 || rule->code == 307) { |
| 2587 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache"))) |
| 2588 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | if (rule->cookie_len) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2592 | if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len))) |
| 2593 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2594 | } |
| 2595 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2596 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2597 | goto fail; |
| 2598 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2599 | htx->flags |= HTX_FL_EOM; |
Kevin Zhu | 96b3639 | 2020-01-07 09:42:55 +0100 | [diff] [blame] | 2600 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 2601 | if (!http_forward_proxy_resp(s, 1)) |
| 2602 | goto fail; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2603 | |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2604 | if (rule->flags & REDIRECT_FLAG_FROM_REQ) { |
| 2605 | /* let's log the request time */ |
| 2606 | s->logs.tv_request = now; |
Christopher Faulet | d347588 | 2021-10-04 14:16:46 +0200 | [diff] [blame] | 2607 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2608 | |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2609 | 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] | 2610 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2611 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2612 | |
| 2613 | if (!(s->flags & SF_ERR_MASK)) |
| 2614 | s->flags |= SF_ERR_LOCAL; |
| 2615 | if (!(s->flags & SF_FINST_MASK)) |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2616 | 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] | 2617 | |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2618 | out: |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2619 | free_trash_chunk(chunk); |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2620 | return ret; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2621 | |
| 2622 | fail: |
| 2623 | /* If an error occurred, remove the incomplete HTTP response from the |
| 2624 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 2625 | channel_htx_truncate(res, htxbuf(&res->buf)); |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2626 | ret = 0; |
| 2627 | goto out; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2628 | } |
| 2629 | |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2630 | /* This function filters the request header names to only allow [0-9a-zA-Z-] |
| 2631 | * characters. Depending on the proxy configuration, headers with a name not |
| 2632 | * matching this charset are removed or the request is rejected with a |
| 2633 | * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT |
| 2634 | * to continue the request processing or HTTP_RULE_RES_DENY if the request is |
| 2635 | * rejected. |
| 2636 | */ |
| 2637 | static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px) |
| 2638 | { |
| 2639 | struct htx_blk *blk; |
| 2640 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
| 2641 | |
| 2642 | blk = htx_get_first_blk(htx); |
| 2643 | while (blk) { |
| 2644 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 2645 | |
| 2646 | if (type == HTX_BLK_HDR) { |
| 2647 | struct ist n = htx_get_blk_name(htx, blk); |
| 2648 | int i; |
| 2649 | |
| 2650 | for (i = 0; i < istlen(n); i++) { |
| 2651 | if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') { |
| 2652 | /* Block the request or remove the header */ |
| 2653 | if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK) |
| 2654 | goto block; |
| 2655 | blk = htx_remove_blk(htx, blk); |
| 2656 | continue; |
| 2657 | } |
| 2658 | } |
| 2659 | } |
| 2660 | if (type == HTX_BLK_EOH) |
| 2661 | break; |
| 2662 | |
| 2663 | blk = htx_get_next_blk(htx, blk); |
| 2664 | } |
| 2665 | out: |
| 2666 | return rule_ret; |
| 2667 | block: |
| 2668 | /* Block the request returning a 403-Forbidden response */ |
| 2669 | s->txn->status = 403; |
| 2670 | rule_ret = HTTP_RULE_RES_DENY; |
| 2671 | goto out; |
| 2672 | } |
| 2673 | |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2674 | /* Replace all headers matching the name <name>. The header value is replaced if |
| 2675 | * it matches the regex <re>. <str> is used for the replacement. If <full> is |
| 2676 | * set to 1, the full-line is matched and replaced. Otherwise, comma-separated |
| 2677 | * values are evaluated one by one. It returns 0 on success and -1 on error. |
| 2678 | */ |
| 2679 | int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name, |
| 2680 | const char *str, struct my_regex *re, int full) |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2681 | { |
| 2682 | struct http_hdr_ctx ctx; |
| 2683 | struct buffer *output = get_trash_chunk(); |
| 2684 | |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2685 | ctx.blk = NULL; |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2686 | while (http_find_header(htx, name, &ctx, full)) { |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2687 | if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0)) |
| 2688 | continue; |
| 2689 | |
| 2690 | output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch); |
| 2691 | if (output->data == -1) |
| 2692 | return -1; |
| 2693 | if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data))) |
| 2694 | return -1; |
| 2695 | } |
| 2696 | return 0; |
| 2697 | } |
| 2698 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2699 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 2700 | * takes the string from the variable 'replace' with length 'len', then modifies |
| 2701 | * the relevant part of the request line accordingly. Then it updates various |
| 2702 | * pointers to the next elements which were moved, and the total buffer length. |
| 2703 | * It finds the action to be performed in p[2], previously filled by function |
| 2704 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 2705 | * error, though this can be revisited when this code is finally exploited. |
| 2706 | * |
| 2707 | * '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] | 2708 | * 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] | 2709 | * |
| 2710 | * In query string case, the mark question '?' must be set at the start of the |
| 2711 | * string by the caller, event if the replacement query string is empty. |
| 2712 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2713 | int http_req_replace_stline(int action, const char *replace, int len, |
| 2714 | struct proxy *px, struct stream *s) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2715 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2716 | struct htx *htx = htxbuf(&s->req.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2717 | |
| 2718 | switch (action) { |
| 2719 | case 0: // method |
| 2720 | if (!http_replace_req_meth(htx, ist2(replace, len))) |
| 2721 | return -1; |
| 2722 | break; |
| 2723 | |
| 2724 | case 1: // path |
Christopher Faulet | b8ce505 | 2020-08-31 16:11:57 +0200 | [diff] [blame] | 2725 | if (!http_replace_req_path(htx, ist2(replace, len), 0)) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2726 | return -1; |
| 2727 | break; |
| 2728 | |
| 2729 | case 2: // query |
| 2730 | if (!http_replace_req_query(htx, ist2(replace, len))) |
| 2731 | return -1; |
| 2732 | break; |
| 2733 | |
| 2734 | case 3: // uri |
| 2735 | if (!http_replace_req_uri(htx, ist2(replace, len))) |
| 2736 | return -1; |
| 2737 | break; |
| 2738 | |
Christopher Faulet | 312294f | 2020-09-02 17:17:44 +0200 | [diff] [blame] | 2739 | case 4: // path + query |
| 2740 | if (!http_replace_req_path(htx, ist2(replace, len), 1)) |
| 2741 | return -1; |
| 2742 | break; |
| 2743 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2744 | default: |
| 2745 | return -1; |
| 2746 | } |
| 2747 | return 0; |
| 2748 | } |
| 2749 | |
| 2750 | /* This function replace the HTTP status code and the associated message. The |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2751 | * variable <status> contains the new status code. This function never fails. It |
| 2752 | * returns 0 in case of success, -1 in case of internal error. |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2753 | */ |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2754 | 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] | 2755 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2756 | struct htx *htx = htxbuf(&s->res.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2757 | char *res; |
| 2758 | |
| 2759 | chunk_reset(&trash); |
| 2760 | res = ultoa_o(status, trash.area, trash.size); |
| 2761 | trash.data = res - trash.area; |
| 2762 | |
| 2763 | /* Do we have a custom reason format string? */ |
Tim Duesterhus | e296d3e | 2020-03-05 17:56:31 +0100 | [diff] [blame] | 2764 | if (!isttest(reason)) { |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2765 | const char *str = http_get_reason(status); |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 2766 | reason = ist(str); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2767 | } |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2768 | |
Christopher Faulet | bde2c4c | 2020-08-31 16:43:34 +0200 | [diff] [blame] | 2769 | if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason)) |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2770 | return -1; |
| 2771 | return 0; |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2772 | } |
| 2773 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2774 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
| 2775 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 2776 | * further processing of the request (auth, deny, ...), and defaults to |
| 2777 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 2778 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 2779 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 2780 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 2781 | * status. |
| 2782 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2783 | static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, |
| 2784 | struct list *rules, struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2785 | { |
| 2786 | struct session *sess = strm_sess(s); |
| 2787 | struct http_txn *txn = s->txn; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2788 | struct act_rule *rule; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2789 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2790 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2791 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2792 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2793 | * resume condition. If a resume is needed it is always in the action |
| 2794 | * and never in the ACL or converters. In this case, we initialise the |
| 2795 | * current rule, and go to the action execution point. |
| 2796 | */ |
| 2797 | if (s->current_rule) { |
| 2798 | rule = s->current_rule; |
| 2799 | s->current_rule = NULL; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2800 | 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] | 2801 | goto resume_execution; |
| 2802 | } |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2803 | 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] | 2804 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2805 | restart: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2806 | /* start the ruleset evaluation in strict mode */ |
| 2807 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2808 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2809 | list_for_each_entry(rule, s->current_rule_list, list) { |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2810 | /* check optional condition */ |
| 2811 | if (rule->cond) { |
| 2812 | int ret; |
| 2813 | |
| 2814 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 2815 | ret = acl_pass(ret); |
| 2816 | |
| 2817 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2818 | ret = !ret; |
| 2819 | |
| 2820 | if (!ret) /* condition not matched */ |
| 2821 | continue; |
| 2822 | } |
| 2823 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2824 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2825 | resume_execution: |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 2826 | if (rule->kw->flags & KWF_EXPERIMENTAL) |
| 2827 | mark_tainted(TAINTED_ACTION_EXP_EXECUTED); |
| 2828 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2829 | /* Always call the action function if defined */ |
| 2830 | if (rule->action_ptr) { |
| 2831 | if ((s->req.flags & CF_READ_ERROR) || |
| 2832 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 2833 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2834 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2835 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2836 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2837 | case ACT_RET_CONT: |
| 2838 | break; |
| 2839 | case ACT_RET_STOP: |
| 2840 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2841 | s->last_rule_file = rule->conf.file; |
| 2842 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2843 | goto end; |
| 2844 | case ACT_RET_YIELD: |
| 2845 | s->current_rule = rule; |
| 2846 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2847 | goto end; |
| 2848 | case ACT_RET_ERR: |
| 2849 | rule_ret = HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2850 | s->last_rule_file = rule->conf.file; |
| 2851 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2852 | goto end; |
| 2853 | case ACT_RET_DONE: |
| 2854 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2855 | s->last_rule_file = rule->conf.file; |
| 2856 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2857 | goto end; |
| 2858 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 2859 | if (txn->status == -1) |
| 2860 | txn->status = 403; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2861 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2862 | s->last_rule_file = rule->conf.file; |
| 2863 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2864 | goto end; |
| 2865 | case ACT_RET_ABRT: |
| 2866 | rule_ret = HTTP_RULE_RES_ABRT; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2867 | s->last_rule_file = rule->conf.file; |
| 2868 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2869 | goto end; |
| 2870 | case ACT_RET_INV: |
| 2871 | rule_ret = HTTP_RULE_RES_BADREQ; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2872 | s->last_rule_file = rule->conf.file; |
| 2873 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2874 | goto end; |
| 2875 | } |
| 2876 | continue; /* eval the next rule */ |
| 2877 | } |
| 2878 | |
| 2879 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2880 | switch (rule->action) { |
| 2881 | case ACT_ACTION_ALLOW: |
| 2882 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2883 | s->last_rule_file = rule->conf.file; |
| 2884 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2885 | goto end; |
| 2886 | |
| 2887 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2888 | txn->status = rule->arg.http_reply->status; |
| 2889 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2890 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2891 | s->last_rule_file = rule->conf.file; |
| 2892 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2893 | goto end; |
| 2894 | |
| 2895 | case ACT_HTTP_REQ_TARPIT: |
| 2896 | txn->flags |= TX_CLTARPIT; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2897 | txn->status = rule->arg.http_reply->status; |
| 2898 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2899 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2900 | s->last_rule_file = rule->conf.file; |
| 2901 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2902 | goto end; |
| 2903 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2904 | case ACT_HTTP_REDIR: { |
| 2905 | int ret = http_apply_redirect_rule(rule->arg.redir, s, txn); |
| 2906 | |
| 2907 | if (ret == 2) // 2 == skip |
| 2908 | break; |
| 2909 | |
| 2910 | rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2911 | s->last_rule_file = rule->conf.file; |
| 2912 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2913 | goto end; |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2914 | } |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2915 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2916 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2917 | default: |
| 2918 | break; |
| 2919 | } |
| 2920 | } |
| 2921 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2922 | if (def_rules && s->current_rule_list == def_rules) { |
| 2923 | s->current_rule_list = rules; |
| 2924 | goto restart; |
| 2925 | } |
| 2926 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2927 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2928 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2929 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2930 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2931 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2932 | /* we reached the end of the rules, nothing to report */ |
| 2933 | return rule_ret; |
| 2934 | } |
| 2935 | |
| 2936 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 2937 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 2938 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 2939 | * is returned, the process can continue the evaluation of next rule list. If |
| 2940 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 2941 | * 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] | 2942 | * must be returned. If *YIELD is returned, the caller must call again the |
| 2943 | * function with the same context. |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2944 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2945 | static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules, |
| 2946 | struct list *rules, struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2947 | { |
| 2948 | struct session *sess = strm_sess(s); |
| 2949 | struct http_txn *txn = s->txn; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2950 | struct act_rule *rule; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2951 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2952 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2953 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2954 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2955 | * resume condition. If a resume is needed it is always in the action |
| 2956 | * and never in the ACL or converters. In this case, we initialise the |
| 2957 | * current rule, and go to the action execution point. |
| 2958 | */ |
| 2959 | if (s->current_rule) { |
| 2960 | rule = s->current_rule; |
| 2961 | s->current_rule = NULL; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2962 | 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] | 2963 | goto resume_execution; |
| 2964 | } |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2965 | s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules); |
| 2966 | |
| 2967 | restart: |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2968 | |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2969 | /* start the ruleset evaluation in strict mode */ |
| 2970 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2971 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2972 | list_for_each_entry(rule, s->current_rule_list, list) { |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2973 | /* check optional condition */ |
| 2974 | if (rule->cond) { |
| 2975 | int ret; |
| 2976 | |
| 2977 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 2978 | ret = acl_pass(ret); |
| 2979 | |
| 2980 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2981 | ret = !ret; |
| 2982 | |
| 2983 | if (!ret) /* condition not matched */ |
| 2984 | continue; |
| 2985 | } |
| 2986 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2987 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2988 | resume_execution: |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 2989 | if (rule->kw->flags & KWF_EXPERIMENTAL) |
| 2990 | mark_tainted(TAINTED_ACTION_EXP_EXECUTED); |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2991 | |
| 2992 | /* Always call the action function if defined */ |
| 2993 | if (rule->action_ptr) { |
| 2994 | if ((s->req.flags & CF_READ_ERROR) || |
| 2995 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 2996 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2997 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2998 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2999 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3000 | case ACT_RET_CONT: |
| 3001 | break; |
| 3002 | case ACT_RET_STOP: |
| 3003 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 3004 | s->last_rule_file = rule->conf.file; |
| 3005 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3006 | goto end; |
| 3007 | case ACT_RET_YIELD: |
| 3008 | s->current_rule = rule; |
| 3009 | rule_ret = HTTP_RULE_RES_YIELD; |
| 3010 | goto end; |
| 3011 | case ACT_RET_ERR: |
| 3012 | rule_ret = HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 3013 | s->last_rule_file = rule->conf.file; |
| 3014 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3015 | goto end; |
| 3016 | case ACT_RET_DONE: |
| 3017 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 3018 | s->last_rule_file = rule->conf.file; |
| 3019 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3020 | goto end; |
| 3021 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 3022 | if (txn->status == -1) |
| 3023 | txn->status = 502; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3024 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 3025 | s->last_rule_file = rule->conf.file; |
| 3026 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3027 | goto end; |
| 3028 | case ACT_RET_ABRT: |
| 3029 | rule_ret = HTTP_RULE_RES_ABRT; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 3030 | s->last_rule_file = rule->conf.file; |
| 3031 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3032 | goto end; |
| 3033 | case ACT_RET_INV: |
| 3034 | rule_ret = HTTP_RULE_RES_BADREQ; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 3035 | s->last_rule_file = rule->conf.file; |
| 3036 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3037 | goto end; |
| 3038 | } |
| 3039 | continue; /* eval the next rule */ |
| 3040 | } |
| 3041 | |
| 3042 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3043 | switch (rule->action) { |
| 3044 | case ACT_ACTION_ALLOW: |
| 3045 | rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 3046 | s->last_rule_file = rule->conf.file; |
| 3047 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3048 | goto end; |
| 3049 | |
| 3050 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 3051 | txn->status = rule->arg.http_reply->status; |
| 3052 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 3053 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 3054 | s->last_rule_file = rule->conf.file; |
| 3055 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3056 | goto end; |
| 3057 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 3058 | case ACT_HTTP_REDIR: { |
| 3059 | int ret = http_apply_redirect_rule(rule->arg.redir, s, txn); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3060 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 3061 | if (ret == 2) // 2 == skip |
| 3062 | break; |
| 3063 | |
| 3064 | rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 3065 | s->last_rule_file = rule->conf.file; |
| 3066 | s->last_rule_line = rule->conf.line; |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 3067 | goto end; |
| 3068 | } |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3069 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3070 | default: |
| 3071 | break; |
| 3072 | } |
| 3073 | } |
| 3074 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 3075 | if (def_rules && s->current_rule_list == def_rules) { |
| 3076 | s->current_rule_list = rules; |
| 3077 | goto restart; |
| 3078 | } |
| 3079 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3080 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 3081 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 3082 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 3083 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 3084 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3085 | /* we reached the end of the rules, nothing to report */ |
| 3086 | return rule_ret; |
| 3087 | } |
| 3088 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3089 | /* Executes backend and frontend http-after-response rules for the stream <s>, |
| 3090 | * in that order. it return 1 on success and 0 on error. It is the caller |
| 3091 | * responsibility to catch error or ignore it. If it catches it, this function |
| 3092 | * may be called a second time, for the internal error. |
| 3093 | */ |
| 3094 | int http_eval_after_res_rules(struct stream *s) |
| 3095 | { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 3096 | struct list *def_rules, *rules; |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3097 | struct session *sess = s->sess; |
| 3098 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 3099 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 3100 | /* Eval after-response ruleset only if the reply is not const */ |
| 3101 | if (s->txn->flags & TX_CONST_REPLY) |
| 3102 | goto end; |
| 3103 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3104 | /* prune the request variables if not already done and swap to the response variables. */ |
| 3105 | if (s->vars_reqres.scope != SCOPE_RES) { |
| 3106 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 3107 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | b7bfcb3 | 2021-08-31 08:13:25 +0200 | [diff] [blame] | 3108 | vars_init_head(&s->vars_reqres, SCOPE_RES); |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3109 | } |
| 3110 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 3111 | def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL); |
| 3112 | rules = &s->be->http_after_res_rules; |
| 3113 | |
| 3114 | ret = http_res_get_intercept_rule(s->be, def_rules, rules, s); |
Christopher Faulet | 4c5a591 | 2021-11-09 17:48:39 +0100 | [diff] [blame] | 3115 | 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] | 3116 | def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL); |
| 3117 | rules = &sess->fe->http_after_res_rules; |
| 3118 | ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s); |
| 3119 | } |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3120 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 3121 | end: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3122 | /* All other codes than CONTINUE, STOP or DONE are forbidden */ |
| 3123 | return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE); |
| 3124 | } |
| 3125 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3126 | /* |
| 3127 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 3128 | * desirable to call it only when needed. This code is quite complex because |
| 3129 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 3130 | * highly recommended not to touch this part without a good reason ! |
| 3131 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3132 | 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] | 3133 | { |
| 3134 | struct session *sess = s->sess; |
| 3135 | struct http_txn *txn = s->txn; |
| 3136 | struct htx *htx; |
| 3137 | struct http_hdr_ctx ctx; |
| 3138 | char *hdr_beg, *hdr_end, *del_from; |
| 3139 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
| 3140 | int preserve_hdr; |
| 3141 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3142 | htx = htxbuf(&req->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3143 | ctx.blk = NULL; |
| 3144 | while (http_find_header(htx, ist("Cookie"), &ctx, 1)) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3145 | int is_first = 1; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3146 | del_from = NULL; /* nothing to be deleted */ |
| 3147 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 3148 | |
| 3149 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3150 | * attributes whose name begin with a '$', and associate them with |
| 3151 | * the right cookie, if we want to delete this cookie. |
| 3152 | * So there are 3 cases for each cookie read : |
| 3153 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3154 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3155 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3156 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3157 | * "special" cookie. |
| 3158 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3159 | * remove it. If no application cookie persists in the header, we |
| 3160 | * *MUST* delete it. |
| 3161 | * |
| 3162 | * Note: RFC2965 is unclear about the processing of spaces around |
| 3163 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 3164 | * the RFC explicitly allows spaces before it, and not within the |
| 3165 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 3166 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 3167 | * after the equal sign too, resulting in some (rare) buggy |
| 3168 | * implementations trying to do that. So let's do what servers do. |
| 3169 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 3170 | * allowed quoted strings in values, with any possible character |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3171 | * after a backslash, including control chars and delimiters, which |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3172 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 3173 | * within values even without quotes. |
| 3174 | * |
| 3175 | * We have to keep multiple pointers in order to support cookie |
| 3176 | * removal at the beginning, middle or end of header without |
| 3177 | * corrupting the header. All of these headers are valid : |
| 3178 | * |
| 3179 | * hdr_beg hdr_end |
| 3180 | * | | |
| 3181 | * v | |
| 3182 | * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 | |
| 3183 | * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v |
| 3184 | * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3 |
| 3185 | * | | | | | | | |
| 3186 | * | | | | | | | |
| 3187 | * | | | | | | +--> next |
| 3188 | * | | | | | +----> val_end |
| 3189 | * | | | | +-----------> val_beg |
| 3190 | * | | | +--------------> equal |
| 3191 | * | | +----------------> att_end |
| 3192 | * | +---------------------> att_beg |
| 3193 | * +--------------------------> prev |
| 3194 | * |
| 3195 | */ |
| 3196 | hdr_beg = ctx.value.ptr; |
| 3197 | hdr_end = hdr_beg + ctx.value.len; |
| 3198 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3199 | /* Iterate through all cookies on this line */ |
| 3200 | |
| 3201 | /* find att_beg */ |
| 3202 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3203 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3204 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3205 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3206 | |
| 3207 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3208 | att_beg++; |
| 3209 | |
| 3210 | /* find att_end : this is the first character after the last non |
| 3211 | * space before the equal. It may be equal to hdr_end. |
| 3212 | */ |
| 3213 | equal = att_end = att_beg; |
| 3214 | while (equal < hdr_end) { |
| 3215 | if (*equal == '=' || *equal == ',' || *equal == ';') |
| 3216 | break; |
| 3217 | if (HTTP_IS_SPHT(*equal++)) |
| 3218 | continue; |
| 3219 | att_end = equal; |
| 3220 | } |
| 3221 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3222 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3223 | * is between <att_beg> and <equal>, both may be identical. |
| 3224 | */ |
| 3225 | /* look for end of cookie if there is an equal sign */ |
| 3226 | if (equal < hdr_end && *equal == '=') { |
| 3227 | /* look for the beginning of the value */ |
| 3228 | val_beg = equal + 1; |
| 3229 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3230 | val_beg++; |
| 3231 | |
| 3232 | /* find the end of the value, respecting quotes */ |
| 3233 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3234 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3235 | /* 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] | 3236 | val_end = next; |
| 3237 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3238 | val_end--; |
| 3239 | } |
| 3240 | else |
| 3241 | val_beg = val_end = next = equal; |
| 3242 | |
| 3243 | /* We have nothing to do with attributes beginning with |
| 3244 | * '$'. However, they will automatically be removed if a |
| 3245 | * header before them is removed, since they're supposed |
| 3246 | * to be linked together. |
| 3247 | */ |
| 3248 | if (*att_beg == '$') |
| 3249 | continue; |
| 3250 | |
| 3251 | /* Ignore cookies with no equal sign */ |
| 3252 | if (equal == next) { |
| 3253 | /* This is not our cookie, so we must preserve it. But if we already |
| 3254 | * scheduled another cookie for removal, we cannot remove the |
| 3255 | * complete header, but we can remove the previous block itself. |
| 3256 | */ |
| 3257 | preserve_hdr = 1; |
| 3258 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3259 | 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] | 3260 | val_end += delta; |
| 3261 | next += delta; |
| 3262 | hdr_end += delta; |
| 3263 | prev = del_from; |
| 3264 | del_from = NULL; |
| 3265 | } |
| 3266 | continue; |
| 3267 | } |
| 3268 | |
| 3269 | /* if there are spaces around the equal sign, we need to |
| 3270 | * strip them otherwise we'll get trouble for cookie captures, |
| 3271 | * or even for rewrites. Since this happens extremely rarely, |
| 3272 | * it does not hurt performance. |
| 3273 | */ |
| 3274 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3275 | int stripped_before = 0; |
| 3276 | int stripped_after = 0; |
| 3277 | |
| 3278 | if (att_end != equal) { |
| 3279 | memmove(att_end, equal, hdr_end - equal); |
| 3280 | stripped_before = (att_end - equal); |
| 3281 | equal += stripped_before; |
| 3282 | val_beg += stripped_before; |
| 3283 | } |
| 3284 | |
| 3285 | if (val_beg > equal + 1) { |
| 3286 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3287 | stripped_after = (equal + 1) - val_beg; |
| 3288 | val_beg += stripped_after; |
| 3289 | stripped_before += stripped_after; |
| 3290 | } |
| 3291 | |
| 3292 | val_end += stripped_before; |
| 3293 | next += stripped_before; |
| 3294 | hdr_end += stripped_before; |
| 3295 | } |
| 3296 | /* now everything is as on the diagram above */ |
| 3297 | |
| 3298 | /* First, let's see if we want to capture this cookie. We check |
| 3299 | * that we don't already have a client side cookie, because we |
| 3300 | * can only capture one. Also as an optimisation, we ignore |
| 3301 | * cookies shorter than the declared name. |
| 3302 | */ |
| 3303 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 3304 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3305 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3306 | int log_len = val_end - att_beg; |
| 3307 | |
| 3308 | if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3309 | ha_alert("HTTP logging : out of memory.\n"); |
| 3310 | } else { |
| 3311 | if (log_len > sess->fe->capture_len) |
| 3312 | log_len = sess->fe->capture_len; |
| 3313 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 3314 | txn->cli_cookie[log_len] = 0; |
| 3315 | } |
| 3316 | } |
| 3317 | |
| 3318 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 3319 | * following form : |
| 3320 | * |
| 3321 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
| 3322 | * |
| 3323 | * For cookies in prefix mode, the form is : |
| 3324 | * |
| 3325 | * Cookie: NAME=SRV~VALUE |
| 3326 | */ |
| 3327 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3328 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3329 | struct server *srv = s->be->srv; |
| 3330 | char *delim; |
| 3331 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3332 | /* 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] | 3333 | * have the server ID between val_beg and delim, and the original cookie between |
| 3334 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 3335 | * |
| 3336 | * hdr_beg |
| 3337 | * | |
| 3338 | * v |
| 3339 | * NAME=SRV; # in all but prefix modes |
| 3340 | * NAME=SRV~OPAQUE ; # in prefix mode |
| 3341 | * || || | |+-> next |
| 3342 | * || || | +--> val_end |
| 3343 | * || || +---------> delim |
| 3344 | * || |+------------> val_beg |
| 3345 | * || +-------------> att_end = equal |
| 3346 | * |+-----------------> att_beg |
| 3347 | * +------------------> prev |
| 3348 | * |
| 3349 | */ |
| 3350 | if (s->be->ck_opts & PR_CK_PFX) { |
| 3351 | for (delim = val_beg; delim < val_end; delim++) |
| 3352 | if (*delim == COOKIE_DELIM) |
| 3353 | break; |
| 3354 | } |
| 3355 | else { |
| 3356 | char *vbar1; |
| 3357 | delim = val_end; |
| 3358 | /* Now check if the cookie contains a date field, which would |
| 3359 | * appear after a vertical bar ('|') just after the server name |
| 3360 | * and before the delimiter. |
| 3361 | */ |
| 3362 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 3363 | if (vbar1) { |
| 3364 | /* OK, so left of the bar is the server's cookie and |
| 3365 | * right is the last seen date. It is a base64 encoded |
| 3366 | * 30-bit value representing the UNIX date since the |
| 3367 | * epoch in 4-second quantities. |
| 3368 | */ |
| 3369 | int val; |
| 3370 | delim = vbar1++; |
| 3371 | if (val_end - vbar1 >= 5) { |
| 3372 | val = b64tos30(vbar1); |
| 3373 | if (val > 0) |
| 3374 | txn->cookie_last_date = val << 2; |
| 3375 | } |
| 3376 | /* look for a second vertical bar */ |
| 3377 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 3378 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 3379 | val = b64tos30(vbar1 + 1); |
| 3380 | if (val > 0) |
| 3381 | txn->cookie_first_date = val << 2; |
| 3382 | } |
| 3383 | } |
| 3384 | } |
| 3385 | |
| 3386 | /* if the cookie has an expiration date and the proxy wants to check |
| 3387 | * it, then we do that now. We first check if the cookie is too old, |
| 3388 | * then only if it has expired. We detect strict overflow because the |
| 3389 | * time resolution here is not great (4 seconds). Cookies with dates |
| 3390 | * in the future are ignored if their offset is beyond one day. This |
| 3391 | * allows an admin to fix timezone issues without expiring everyone |
| 3392 | * and at the same time avoids keeping unwanted side effects for too |
| 3393 | * long. |
| 3394 | */ |
| 3395 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 3396 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) || |
| 3397 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
| 3398 | txn->flags &= ~TX_CK_MASK; |
| 3399 | txn->flags |= TX_CK_OLD; |
| 3400 | delim = val_beg; // let's pretend we have not found the cookie |
| 3401 | txn->cookie_first_date = 0; |
| 3402 | txn->cookie_last_date = 0; |
| 3403 | } |
| 3404 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 3405 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) || |
| 3406 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
| 3407 | txn->flags &= ~TX_CK_MASK; |
| 3408 | txn->flags |= TX_CK_EXPIRED; |
| 3409 | delim = val_beg; // let's pretend we have not found the cookie |
| 3410 | txn->cookie_first_date = 0; |
| 3411 | txn->cookie_last_date = 0; |
| 3412 | } |
| 3413 | |
| 3414 | /* Here, we'll look for the first running server which supports the cookie. |
| 3415 | * This allows to share a same cookie between several servers, for example |
| 3416 | * to dedicate backup servers to specific servers only. |
| 3417 | * However, to prevent clients from sticking to cookie-less backup server |
| 3418 | * when they have incidentely learned an empty cookie, we simply ignore |
| 3419 | * empty cookies and mark them as invalid. |
| 3420 | * The same behaviour is applied when persistence must be ignored. |
| 3421 | */ |
| 3422 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3423 | srv = NULL; |
| 3424 | |
| 3425 | while (srv) { |
| 3426 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 3427 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 3428 | if ((srv->cur_state != SRV_ST_STOPPED) || |
| 3429 | (s->be->options & PR_O_PERSIST) || |
| 3430 | (s->flags & SF_FORCE_PRST)) { |
| 3431 | /* we found the server and we can use it */ |
| 3432 | txn->flags &= ~TX_CK_MASK; |
| 3433 | txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN; |
| 3434 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
| 3435 | s->target = &srv->obj_type; |
| 3436 | break; |
| 3437 | } else { |
| 3438 | /* we found a server, but it's down, |
| 3439 | * mark it as such and go on in case |
| 3440 | * another one is available. |
| 3441 | */ |
| 3442 | txn->flags &= ~TX_CK_MASK; |
| 3443 | txn->flags |= TX_CK_DOWN; |
| 3444 | } |
| 3445 | } |
| 3446 | srv = srv->next; |
| 3447 | } |
| 3448 | |
| 3449 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
| 3450 | /* no server matched this cookie or we deliberately skipped it */ |
| 3451 | txn->flags &= ~TX_CK_MASK; |
| 3452 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3453 | txn->flags |= TX_CK_UNUSED; |
| 3454 | else |
| 3455 | txn->flags |= TX_CK_INVALID; |
| 3456 | } |
| 3457 | |
| 3458 | /* depending on the cookie mode, we may have to either : |
| 3459 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3460 | * the server never sees it ; |
| 3461 | * - 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] | 3462 | * application cookie so that it does not get accidentally removed later, |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3463 | * if we're in cookie prefix mode |
| 3464 | */ |
| 3465 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
| 3466 | int delta; /* negative */ |
| 3467 | |
| 3468 | memmove(val_beg, delim + 1, hdr_end - (delim + 1)); |
| 3469 | delta = val_beg - (delim + 1); |
| 3470 | val_end += delta; |
| 3471 | next += delta; |
| 3472 | hdr_end += delta; |
| 3473 | del_from = NULL; |
| 3474 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 3475 | } |
| 3476 | else if (del_from == NULL && |
| 3477 | (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) { |
| 3478 | del_from = prev; |
| 3479 | } |
| 3480 | } |
| 3481 | else { |
| 3482 | /* This is not our cookie, so we must preserve it. But if we already |
| 3483 | * scheduled another cookie for removal, we cannot remove the |
| 3484 | * complete header, but we can remove the previous block itself. |
| 3485 | */ |
| 3486 | preserve_hdr = 1; |
| 3487 | |
| 3488 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3489 | 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] | 3490 | if (att_beg >= del_from) |
| 3491 | att_beg += delta; |
| 3492 | if (att_end >= del_from) |
| 3493 | att_end += delta; |
| 3494 | val_beg += delta; |
| 3495 | val_end += delta; |
| 3496 | next += delta; |
| 3497 | hdr_end += delta; |
| 3498 | prev = del_from; |
| 3499 | del_from = NULL; |
| 3500 | } |
| 3501 | } |
| 3502 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3503 | } /* for each cookie */ |
| 3504 | |
| 3505 | |
| 3506 | /* There are no more cookies on this line. |
| 3507 | * We may still have one (or several) marked for deletion at the |
| 3508 | * end of the line. We must do this now in two ways : |
| 3509 | * - if some cookies must be preserved, we only delete from the |
| 3510 | * mark to the end of line ; |
| 3511 | * - if nothing needs to be preserved, simply delete the whole header |
| 3512 | */ |
| 3513 | if (del_from) { |
| 3514 | hdr_end = (preserve_hdr ? del_from : hdr_beg); |
| 3515 | } |
| 3516 | if ((hdr_end - hdr_beg) != ctx.value.len) { |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3517 | if (hdr_beg != hdr_end) |
| 3518 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3519 | else |
| 3520 | http_remove_header(htx, &ctx); |
| 3521 | } |
| 3522 | } /* for each "Cookie header */ |
| 3523 | } |
| 3524 | |
| 3525 | /* |
| 3526 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 3527 | * desirable to call it only when needed. This function is also used when we |
| 3528 | * just need to know if there is a cookie (eg: for check-cache). |
| 3529 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3530 | 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] | 3531 | { |
| 3532 | struct session *sess = s->sess; |
| 3533 | struct http_txn *txn = s->txn; |
| 3534 | struct htx *htx; |
| 3535 | struct http_hdr_ctx ctx; |
| 3536 | struct server *srv; |
| 3537 | char *hdr_beg, *hdr_end; |
| 3538 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 6f7a02a | 2019-04-15 21:49:49 +0200 | [diff] [blame] | 3539 | int is_cookie2 = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3540 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3541 | htx = htxbuf(&res->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3542 | |
| 3543 | ctx.blk = NULL; |
| 3544 | while (1) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3545 | int is_first = 1; |
| 3546 | |
Andrew McDermott | bfb15ab | 2022-02-11 18:26:49 +0000 | [diff] [blame] | 3547 | if (is_cookie2 || !http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) { |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3548 | if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1)) |
| 3549 | break; |
| 3550 | is_cookie2 = 1; |
| 3551 | } |
| 3552 | |
| 3553 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 3554 | * <prev> points to the colon. |
| 3555 | */ |
| 3556 | txn->flags |= TX_SCK_PRESENT; |
| 3557 | |
| 3558 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 3559 | * check-cache is enabled) and we are not interested in checking |
| 3560 | * them. Warning, the cookie capture is declared in the frontend. |
| 3561 | */ |
| 3562 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
| 3563 | break; |
| 3564 | |
| 3565 | /* OK so now we know we have to process this response cookie. |
| 3566 | * The format of the Set-Cookie header is slightly different |
| 3567 | * from the format of the Cookie header in that it does not |
| 3568 | * support the comma as a cookie delimiter (thus the header |
| 3569 | * cannot be folded) because the Expires attribute described in |
| 3570 | * the original Netscape's spec may contain an unquoted date |
| 3571 | * with a comma inside. We have to live with this because |
| 3572 | * many browsers don't support Max-Age and some browsers don't |
| 3573 | * support quoted strings. However the Set-Cookie2 header is |
| 3574 | * clean. |
| 3575 | * |
| 3576 | * We have to keep multiple pointers in order to support cookie |
| 3577 | * removal at the beginning, middle or end of header without |
| 3578 | * corrupting the header (in case of set-cookie2). A special |
| 3579 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 3580 | * fields after the first semi-colon. The <next> pointer points |
| 3581 | * either to the end of line (set-cookie) or next unquoted comma |
| 3582 | * (set-cookie2). All of these headers are valid : |
| 3583 | * |
| 3584 | * hdr_beg hdr_end |
| 3585 | * | | |
| 3586 | * v | |
| 3587 | * NAME1 = VALUE 1 ; Secure; Path="/" | |
| 3588 | * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v |
| 3589 | * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT |
| 3590 | * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard |
| 3591 | * | | | | | | | | |
| 3592 | * | | | | | | | +-> next |
| 3593 | * | | | | | | +------------> scav |
| 3594 | * | | | | | +--------------> val_end |
| 3595 | * | | | | +--------------------> val_beg |
| 3596 | * | | | +----------------------> equal |
| 3597 | * | | +------------------------> att_end |
| 3598 | * | +----------------------------> att_beg |
| 3599 | * +------------------------------> prev |
| 3600 | * -------------------------------> hdr_beg |
| 3601 | */ |
| 3602 | hdr_beg = ctx.value.ptr; |
| 3603 | hdr_end = hdr_beg + ctx.value.len; |
| 3604 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3605 | |
| 3606 | /* Iterate through all cookies on this line */ |
| 3607 | |
| 3608 | /* find att_beg */ |
| 3609 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3610 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3611 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3612 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3613 | |
| 3614 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3615 | att_beg++; |
| 3616 | |
| 3617 | /* find att_end : this is the first character after the last non |
| 3618 | * space before the equal. It may be equal to hdr_end. |
| 3619 | */ |
| 3620 | equal = att_end = att_beg; |
| 3621 | |
| 3622 | while (equal < hdr_end) { |
| 3623 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 3624 | break; |
| 3625 | if (HTTP_IS_SPHT(*equal++)) |
| 3626 | continue; |
| 3627 | att_end = equal; |
| 3628 | } |
| 3629 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3630 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3631 | * is between <att_beg> and <equal>, both may be identical. |
| 3632 | */ |
| 3633 | |
| 3634 | /* look for end of cookie if there is an equal sign */ |
| 3635 | if (equal < hdr_end && *equal == '=') { |
| 3636 | /* look for the beginning of the value */ |
| 3637 | val_beg = equal + 1; |
| 3638 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3639 | val_beg++; |
| 3640 | |
| 3641 | /* find the end of the value, respecting quotes */ |
| 3642 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3643 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3644 | /* 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] | 3645 | val_end = next; |
| 3646 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3647 | val_end--; |
| 3648 | } |
| 3649 | else { |
| 3650 | /* <equal> points to next comma, semi-colon or EOL */ |
| 3651 | val_beg = val_end = next = equal; |
| 3652 | } |
| 3653 | |
| 3654 | if (next < hdr_end) { |
| 3655 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 3656 | * a colon or semi-colon before the end. So skip all attr-value |
| 3657 | * pairs and look for the next comma. For Set-Cookie, since |
| 3658 | * commas are permitted in values, skip to the end. |
| 3659 | */ |
| 3660 | if (is_cookie2) |
| 3661 | next = http_find_hdr_value_end(next, hdr_end); |
| 3662 | else |
| 3663 | next = hdr_end; |
| 3664 | } |
| 3665 | |
| 3666 | /* Now everything is as on the diagram above */ |
| 3667 | |
| 3668 | /* Ignore cookies with no equal sign */ |
| 3669 | if (equal == val_end) |
| 3670 | continue; |
| 3671 | |
| 3672 | /* If there are spaces around the equal sign, we need to |
| 3673 | * strip them otherwise we'll get trouble for cookie captures, |
| 3674 | * or even for rewrites. Since this happens extremely rarely, |
| 3675 | * it does not hurt performance. |
| 3676 | */ |
| 3677 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3678 | int stripped_before = 0; |
| 3679 | int stripped_after = 0; |
| 3680 | |
| 3681 | if (att_end != equal) { |
| 3682 | memmove(att_end, equal, hdr_end - equal); |
| 3683 | stripped_before = (att_end - equal); |
| 3684 | equal += stripped_before; |
| 3685 | val_beg += stripped_before; |
| 3686 | } |
| 3687 | |
| 3688 | if (val_beg > equal + 1) { |
| 3689 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3690 | stripped_after = (equal + 1) - val_beg; |
| 3691 | val_beg += stripped_after; |
| 3692 | stripped_before += stripped_after; |
| 3693 | } |
| 3694 | |
| 3695 | val_end += stripped_before; |
| 3696 | next += stripped_before; |
| 3697 | hdr_end += stripped_before; |
| 3698 | |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3699 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3700 | ctx.value.len = hdr_end - hdr_beg; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3701 | } |
| 3702 | |
| 3703 | /* First, let's see if we want to capture this cookie. We check |
| 3704 | * that we don't already have a server side cookie, because we |
| 3705 | * can only capture one. Also as an optimisation, we ignore |
| 3706 | * cookies shorter than the declared name. |
| 3707 | */ |
| 3708 | if (sess->fe->capture_name != NULL && |
| 3709 | txn->srv_cookie == NULL && |
| 3710 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3711 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3712 | int log_len = val_end - att_beg; |
| 3713 | if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3714 | ha_alert("HTTP logging : out of memory.\n"); |
| 3715 | } |
| 3716 | else { |
| 3717 | if (log_len > sess->fe->capture_len) |
| 3718 | log_len = sess->fe->capture_len; |
| 3719 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 3720 | txn->srv_cookie[log_len] = 0; |
| 3721 | } |
| 3722 | } |
| 3723 | |
| 3724 | srv = objt_server(s->target); |
| 3725 | /* now check if we need to process it for persistence */ |
| 3726 | if (!(s->flags & SF_IGNORE_PRST) && |
| 3727 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3728 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3729 | /* assume passive cookie by default */ |
| 3730 | txn->flags &= ~TX_SCK_MASK; |
| 3731 | txn->flags |= TX_SCK_FOUND; |
| 3732 | |
| 3733 | /* If the cookie is in insert mode on a known server, we'll delete |
| 3734 | * this occurrence because we'll insert another one later. |
| 3735 | * We'll delete it too if the "indirect" option is set and we're in |
| 3736 | * a direct access. |
| 3737 | */ |
| 3738 | if (s->be->ck_opts & PR_CK_PSV) { |
| 3739 | /* The "preserve" flag was set, we don't want to touch the |
| 3740 | * server's cookie. |
| 3741 | */ |
| 3742 | } |
| 3743 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
| 3744 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
| 3745 | /* this cookie must be deleted */ |
| 3746 | if (prev == hdr_beg && next == hdr_end) { |
| 3747 | /* whole header */ |
| 3748 | http_remove_header(htx, &ctx); |
| 3749 | /* note: while both invalid now, <next> and <hdr_end> |
| 3750 | * are still equal, so the for() will stop as expected. |
| 3751 | */ |
| 3752 | } else { |
| 3753 | /* just remove the value */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3754 | int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3755 | next = prev; |
| 3756 | hdr_end += delta; |
| 3757 | } |
| 3758 | txn->flags &= ~TX_SCK_MASK; |
| 3759 | txn->flags |= TX_SCK_DELETED; |
| 3760 | /* and go on with next cookie */ |
| 3761 | } |
| 3762 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
| 3763 | /* replace bytes val_beg->val_end with the cookie name associated |
| 3764 | * with this server since we know it. |
| 3765 | */ |
| 3766 | int sliding, delta; |
| 3767 | |
| 3768 | ctx.value = ist2(val_beg, val_end - val_beg); |
| 3769 | ctx.lws_before = ctx.lws_after = 0; |
| 3770 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen)); |
| 3771 | delta = srv->cklen - (val_end - val_beg); |
| 3772 | sliding = (ctx.value.ptr - val_beg); |
| 3773 | hdr_beg += sliding; |
| 3774 | val_beg += sliding; |
| 3775 | next += sliding + delta; |
| 3776 | hdr_end += sliding + delta; |
| 3777 | |
| 3778 | txn->flags &= ~TX_SCK_MASK; |
| 3779 | txn->flags |= TX_SCK_REPLACED; |
| 3780 | } |
| 3781 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
| 3782 | /* insert the cookie name associated with this server |
| 3783 | * before existing cookie, and insert a delimiter between them.. |
| 3784 | */ |
| 3785 | int sliding, delta; |
| 3786 | ctx.value = ist2(val_beg, 0); |
| 3787 | ctx.lws_before = ctx.lws_after = 0; |
| 3788 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1)); |
| 3789 | delta = srv->cklen + 1; |
| 3790 | sliding = (ctx.value.ptr - val_beg); |
| 3791 | hdr_beg += sliding; |
| 3792 | val_beg += sliding; |
| 3793 | next += sliding + delta; |
| 3794 | hdr_end += sliding + delta; |
| 3795 | |
| 3796 | val_beg[srv->cklen] = COOKIE_DELIM; |
| 3797 | txn->flags &= ~TX_SCK_MASK; |
| 3798 | txn->flags |= TX_SCK_REPLACED; |
| 3799 | } |
| 3800 | } |
| 3801 | /* that's done for this cookie, check the next one on the same |
| 3802 | * line when next != hdr_end (only if is_cookie2). |
| 3803 | */ |
| 3804 | } |
| 3805 | } |
| 3806 | } |
| 3807 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3808 | /* |
| 3809 | * Parses the Cache-Control and Pragma request header fields to determine if |
| 3810 | * the request may be served from the cache and/or if it is cacheable. Updates |
| 3811 | * s->txn->flags. |
| 3812 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3813 | void http_check_request_for_cacheability(struct stream *s, struct channel *req) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3814 | { |
| 3815 | struct http_txn *txn = s->txn; |
| 3816 | struct htx *htx; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3817 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 3818 | int pragma_found, cc_found; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3819 | |
| 3820 | if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE) |
| 3821 | return; /* nothing more to do here */ |
| 3822 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3823 | htx = htxbuf(&req->buf); |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3824 | pragma_found = cc_found = 0; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3825 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3826 | /* Check "pragma" header for HTTP/1.0 compatibility. */ |
| 3827 | if (http_find_header(htx, ist("pragma"), &ctx, 1)) { |
| 3828 | if (isteqi(ctx.value, ist("no-cache"))) { |
| 3829 | pragma_found = 1; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3830 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3831 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3832 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3833 | ctx.blk = NULL; |
| 3834 | /* Don't use the cache and don't try to store if we found the |
| 3835 | * Authorization header */ |
| 3836 | if (http_find_header(htx, ist("authorization"), &ctx, 1)) { |
| 3837 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3838 | txn->flags |= TX_CACHE_IGNORE; |
| 3839 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3840 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3841 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3842 | /* Look for "cache-control" header and iterate over all the values |
| 3843 | * until we find one that specifies that caching is possible or not. */ |
| 3844 | ctx.blk = NULL; |
| 3845 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3846 | cc_found = 1; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3847 | /* We don't check the values after max-age, max-stale nor min-fresh, |
| 3848 | * we simply don't use the cache when they're specified. */ |
| 3849 | if (istmatchi(ctx.value, ist("max-age")) || |
| 3850 | istmatchi(ctx.value, ist("no-cache")) || |
| 3851 | istmatchi(ctx.value, ist("max-stale")) || |
| 3852 | istmatchi(ctx.value, ist("min-fresh"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3853 | txn->flags |= TX_CACHE_IGNORE; |
| 3854 | continue; |
| 3855 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3856 | if (istmatchi(ctx.value, ist("no-store"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3857 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3858 | continue; |
| 3859 | } |
| 3860 | } |
| 3861 | |
| 3862 | /* RFC7234#5.4: |
| 3863 | * When the Cache-Control header field is also present and |
| 3864 | * understood in a request, Pragma is ignored. |
| 3865 | * When the Cache-Control header field is not present in a |
| 3866 | * request, caches MUST consider the no-cache request |
| 3867 | * pragma-directive as having the same effect as if |
| 3868 | * "Cache-Control: no-cache" were present. |
| 3869 | */ |
| 3870 | if (!cc_found && pragma_found) |
| 3871 | txn->flags |= TX_CACHE_IGNORE; |
| 3872 | } |
| 3873 | |
| 3874 | /* |
| 3875 | * Check if response is cacheable or not. Updates s->txn->flags. |
| 3876 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3877 | void http_check_response_for_cacheability(struct stream *s, struct channel *res) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3878 | { |
| 3879 | struct http_txn *txn = s->txn; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3880 | struct http_hdr_ctx ctx = { .blk = NULL }; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3881 | struct htx *htx; |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3882 | int has_freshness_info = 0; |
| 3883 | int has_validator = 0; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3884 | |
| 3885 | if (txn->status < 200) { |
| 3886 | /* do not try to cache interim responses! */ |
| 3887 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3888 | return; |
| 3889 | } |
| 3890 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3891 | htx = htxbuf(&res->buf); |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3892 | /* Check "pragma" header for HTTP/1.0 compatibility. */ |
| 3893 | if (http_find_header(htx, ist("pragma"), &ctx, 1)) { |
| 3894 | if (isteqi(ctx.value, ist("no-cache"))) { |
| 3895 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3896 | return; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3897 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3898 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3899 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3900 | /* Look for "cache-control" header and iterate over all the values |
| 3901 | * until we find one that specifies that caching is possible or not. */ |
| 3902 | ctx.blk = NULL; |
| 3903 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
| 3904 | if (isteqi(ctx.value, ist("public"))) { |
| 3905 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3906 | continue; |
| 3907 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3908 | if (isteqi(ctx.value, ist("private")) || |
| 3909 | isteqi(ctx.value, ist("no-cache")) || |
| 3910 | isteqi(ctx.value, ist("no-store")) || |
| 3911 | isteqi(ctx.value, ist("max-age=0")) || |
| 3912 | isteqi(ctx.value, ist("s-maxage=0"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3913 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3914 | continue; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3915 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3916 | /* We might have a no-cache="set-cookie" form. */ |
| 3917 | if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) { |
| 3918 | txn->flags &= ~TX_CACHE_COOK; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3919 | continue; |
| 3920 | } |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3921 | |
| 3922 | if (istmatchi(ctx.value, ist("s-maxage")) || |
| 3923 | istmatchi(ctx.value, ist("max-age"))) { |
| 3924 | has_freshness_info = 1; |
| 3925 | continue; |
| 3926 | } |
| 3927 | } |
| 3928 | |
| 3929 | /* If no freshness information could be found in Cache-Control values, |
| 3930 | * look for an Expires header. */ |
| 3931 | if (!has_freshness_info) { |
| 3932 | ctx.blk = NULL; |
| 3933 | has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0); |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3934 | } |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3935 | |
| 3936 | /* If no freshness information could be found in Cache-Control or Expires |
| 3937 | * values, look for an explicit validator. */ |
| 3938 | if (!has_freshness_info) { |
| 3939 | ctx.blk = NULL; |
| 3940 | has_validator = 1; |
| 3941 | if (!http_find_header(htx, ist("etag"), &ctx, 0)) { |
| 3942 | ctx.blk = NULL; |
| 3943 | if (!http_find_header(htx, ist("last-modified"), &ctx, 0)) |
| 3944 | has_validator = 0; |
| 3945 | } |
| 3946 | } |
| 3947 | |
| 3948 | /* We won't store an entry that has neither a cache validator nor an |
| 3949 | * explicit expiration time, as suggested in RFC 7234#3. */ |
| 3950 | if (!has_freshness_info && !has_validator) |
| 3951 | txn->flags |= TX_CACHE_IGNORE; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3952 | } |
| 3953 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3954 | /* |
| 3955 | * In a GET, HEAD or POST request, check if the requested URI matches the stats uri |
| 3956 | * for the current backend. |
| 3957 | * |
| 3958 | * It is assumed that the request is either a HEAD, GET, or POST and that the |
| 3959 | * uri_auth field is valid. |
| 3960 | * |
| 3961 | * Returns 1 if stats should be provided, otherwise 0. |
| 3962 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3963 | 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] | 3964 | { |
| 3965 | struct uri_auth *uri_auth = backend->uri_auth; |
| 3966 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3967 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3968 | struct ist uri; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3969 | |
| 3970 | if (!uri_auth) |
| 3971 | return 0; |
| 3972 | |
| 3973 | if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST) |
| 3974 | return 0; |
| 3975 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3976 | htx = htxbuf(&s->req.buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 3977 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3978 | uri = htx_sl_req_uri(sl); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 3979 | if (*uri_auth->uri_prefix == '/') { |
| 3980 | struct http_uri_parser parser = http_uri_parser_init(uri); |
| 3981 | uri = http_parse_path(&parser); |
| 3982 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3983 | |
| 3984 | /* check URI size */ |
| 3985 | if (uri_auth->uri_len > uri.len) |
| 3986 | return 0; |
| 3987 | |
| 3988 | if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
| 3989 | return 0; |
| 3990 | |
| 3991 | return 1; |
| 3992 | } |
| 3993 | |
| 3994 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3995 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3996 | * and program a response message if something was unexpected. It cannot fail |
| 3997 | * and always relies on the stats applet to complete the job. It does not touch |
| 3998 | * analysers nor counters, which are left to the caller. It does not touch |
| 3999 | * s->target which is supposed to already point to the stats applet. The caller |
| 4000 | * is expected to have already assigned an appctx to the stream. |
| 4001 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4002 | static int http_handle_stats(struct stream *s, struct channel *req) |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4003 | { |
| 4004 | struct stats_admin_rule *stats_admin_rule; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4005 | struct session *sess = s->sess; |
| 4006 | struct http_txn *txn = s->txn; |
| 4007 | struct http_msg *msg = &txn->req; |
| 4008 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 4009 | const char *h, *lookup, *end; |
Willy Tarreau | 8e7c6e6 | 2022-05-18 17:58:02 +0200 | [diff] [blame] | 4010 | struct appctx *appctx = __sc_appctx(s->scb); |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4011 | struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4012 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4013 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4014 | |
Willy Tarreau | 41f8852 | 2022-05-03 18:39:27 +0200 | [diff] [blame] | 4015 | appctx->st1 = 0; |
Willy Tarreau | 6ef1648 | 2022-05-06 18:07:53 +0200 | [diff] [blame] | 4016 | ctx->state = STAT_STATE_INIT; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4017 | ctx->st_code = STAT_STATUS_INIT; |
| 4018 | ctx->flags |= uri_auth->flags; |
| 4019 | ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4020 | if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD)) |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4021 | ctx->flags |= STAT_CHUNKED; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4022 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4023 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 4024 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4025 | lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len; |
| 4026 | end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4027 | |
| 4028 | for (h = lookup; h <= end - 3; h++) { |
| 4029 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4030 | ctx->flags |= STAT_HIDE_DOWN; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4031 | break; |
| 4032 | } |
Amaury Denoyelle | 91e55ea | 2021-02-25 14:46:08 +0100 | [diff] [blame] | 4033 | } |
| 4034 | |
| 4035 | for (h = lookup; h <= end - 9; h++) { |
| 4036 | if (memcmp(h, ";no-maint", 9) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4037 | ctx->flags |= STAT_HIDE_MAINT; |
Willy Tarreau | 3e32036 | 2020-10-23 17:28:57 +0200 | [diff] [blame] | 4038 | break; |
| 4039 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4040 | } |
| 4041 | |
| 4042 | if (uri_auth->refresh) { |
| 4043 | for (h = lookup; h <= end - 10; h++) { |
| 4044 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4045 | ctx->flags |= STAT_NO_REFRESH; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4046 | break; |
| 4047 | } |
| 4048 | } |
| 4049 | } |
| 4050 | |
| 4051 | for (h = lookup; h <= end - 4; h++) { |
| 4052 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4053 | ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4054 | break; |
| 4055 | } |
| 4056 | } |
| 4057 | |
| 4058 | for (h = lookup; h <= end - 6; h++) { |
| 4059 | if (memcmp(h, ";typed", 6) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4060 | ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
| 4061 | ctx->flags |= STAT_FMT_TYPED; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4062 | break; |
| 4063 | } |
| 4064 | } |
| 4065 | |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 4066 | for (h = lookup; h <= end - 5; h++) { |
| 4067 | if (memcmp(h, ";json", 5) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4068 | ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
| 4069 | ctx->flags |= STAT_FMT_JSON; |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 4070 | break; |
| 4071 | } |
| 4072 | } |
| 4073 | |
| 4074 | for (h = lookup; h <= end - 12; h++) { |
| 4075 | if (memcmp(h, ";json-schema", 12) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4076 | ctx->flags &= ~STAT_FMT_MASK; |
| 4077 | ctx->flags |= STAT_JSON_SCHM; |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 4078 | break; |
| 4079 | } |
| 4080 | } |
| 4081 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4082 | for (h = lookup; h <= end - 8; h++) { |
| 4083 | if (memcmp(h, ";st=", 4) == 0) { |
| 4084 | int i; |
| 4085 | h += 4; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4086 | ctx->st_code = STAT_STATUS_UNKN; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4087 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 4088 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4089 | ctx->st_code = i; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4090 | break; |
| 4091 | } |
| 4092 | } |
| 4093 | break; |
| 4094 | } |
| 4095 | } |
| 4096 | |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4097 | ctx->scope_str = 0; |
| 4098 | ctx->scope_len = 0; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4099 | for (h = lookup; h <= end - 8; h++) { |
| 4100 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 4101 | int itx = 0; |
| 4102 | const char *h2; |
| 4103 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 4104 | const char *err; |
| 4105 | |
| 4106 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 4107 | h2 = h; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4108 | ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl); |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 4109 | while (h < end) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4110 | if (*h == ';' || *h == '&' || *h == ' ') |
| 4111 | break; |
| 4112 | itx++; |
| 4113 | h++; |
| 4114 | } |
| 4115 | |
| 4116 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 4117 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4118 | ctx->scope_len = itx; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4119 | |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4120 | /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */ |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4121 | memcpy(scope_txt, h2, itx); |
| 4122 | scope_txt[itx] = '\0'; |
| 4123 | err = invalid_char(scope_txt); |
| 4124 | if (err) { |
| 4125 | /* bad char in search text => clear scope */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4126 | ctx->scope_str = 0; |
| 4127 | ctx->scope_len = 0; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4128 | } |
| 4129 | break; |
| 4130 | } |
| 4131 | } |
| 4132 | |
| 4133 | /* now check whether we have some admin rules for this request */ |
| 4134 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
| 4135 | int ret = 1; |
| 4136 | |
| 4137 | if (stats_admin_rule->cond) { |
| 4138 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 4139 | ret = acl_pass(ret); |
| 4140 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 4141 | ret = !ret; |
| 4142 | } |
| 4143 | |
| 4144 | if (ret) { |
| 4145 | /* no rule, or the rule matches */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4146 | ctx->flags |= STAT_ADMIN; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4147 | break; |
| 4148 | } |
| 4149 | } |
| 4150 | |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4151 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 4152 | appctx->st0 = STAT_HTTP_HEAD; |
| 4153 | else if (txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4154 | if (ctx->flags & STAT_ADMIN) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4155 | appctx->st0 = STAT_HTTP_POST; |
Christopher Faulet | bd9e842 | 2019-08-15 22:26:48 +0200 | [diff] [blame] | 4156 | if (msg->msg_state < HTTP_MSG_DATA) |
| 4157 | req->analysers |= AN_REQ_HTTP_BODY; |
| 4158 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4159 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4160 | /* POST without admin level */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4161 | ctx->flags &= ~STAT_CHUNKED; |
| 4162 | ctx->st_code = STAT_STATUS_DENY; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4163 | appctx->st0 = STAT_HTTP_LAST; |
| 4164 | } |
| 4165 | } |
| 4166 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4167 | /* Unsupported method */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4168 | ctx->flags &= ~STAT_CHUNKED; |
| 4169 | ctx->st_code = STAT_STATUS_IVAL; |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4170 | appctx->st0 = STAT_HTTP_LAST; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4171 | } |
| 4172 | |
| 4173 | s->task->nice = -32; /* small boost for HTTP statistics */ |
| 4174 | return 1; |
| 4175 | } |
| 4176 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4177 | /* This function waits for the message payload at most <time> milliseconds (may |
| 4178 | * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the |
| 4179 | * payload are received (0 means no limit). It returns HTTP_RULE_* depending on |
| 4180 | * the result: |
| 4181 | * |
| 4182 | * - HTTP_RULE_RES_CONT when conditions are met to stop waiting |
| 4183 | * - HTTP_RULE_RES_YIELD to wait for more data |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 4184 | * - HTTP_RULE_RES_ABRT when a timeout occurred. |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4185 | * - 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] | 4186 | * - HTTP_RULE_RES_ERROR if an internal error occurred |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4187 | * |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 4188 | * 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] | 4189 | * to the client, depending on the channel (408 on request side, 504 on response |
| 4190 | * side). All other errors must be handled by the caller. |
| 4191 | */ |
| 4192 | enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn, |
| 4193 | unsigned int time, unsigned int bytes) |
| 4194 | { |
| 4195 | struct session *sess = s->sess; |
| 4196 | struct http_txn *txn = s->txn; |
| 4197 | struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req); |
| 4198 | struct htx *htx; |
| 4199 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 4200 | |
| 4201 | htx = htxbuf(&chn->buf); |
| 4202 | |
| 4203 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
| 4204 | ret = HTTP_RULE_RES_BADREQ; |
| 4205 | goto end; |
| 4206 | } |
| 4207 | if (htx->flags & HTX_FL_PROCESSING_ERROR) { |
| 4208 | ret = HTTP_RULE_RES_ERROR; |
| 4209 | goto end; |
| 4210 | } |
| 4211 | |
| 4212 | /* Do nothing for bodyless and CONNECT requests */ |
| 4213 | if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS)) |
| 4214 | goto end; |
| 4215 | |
| 4216 | if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) { |
| 4217 | if (http_handle_expect_hdr(s, htx, msg) == -1) { |
| 4218 | ret = HTTP_RULE_RES_ERROR; |
| 4219 | goto end; |
| 4220 | } |
| 4221 | } |
| 4222 | |
| 4223 | msg->msg_state = HTTP_MSG_DATA; |
| 4224 | |
| 4225 | /* Now we're in HTTP_MSG_DATA. We just need to know if all data have |
| 4226 | * been received or if the buffer is full. |
| 4227 | */ |
Christopher Faulet | 7833596 | 2021-09-23 14:46:32 +0200 | [diff] [blame] | 4228 | if ((htx->flags & HTX_FL_EOM) || |
| 4229 | htx_get_tail_type(htx) > HTX_BLK_DATA || |
| 4230 | channel_htx_full(chn, htx, global.tune.maxrewrite) || |
Willy Tarreau | 99615ed | 2022-05-25 07:29:36 +0200 | [diff] [blame] | 4231 | sc_waiting_room(chn_prod(chn))) |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4232 | goto end; |
| 4233 | |
| 4234 | if (bytes) { |
| 4235 | struct htx_blk *blk; |
| 4236 | unsigned int len = 0; |
| 4237 | |
| 4238 | for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 4239 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 4240 | continue; |
| 4241 | len += htx_get_blksz(blk); |
| 4242 | if (len >= bytes) |
| 4243 | goto end; |
| 4244 | } |
| 4245 | } |
| 4246 | |
| 4247 | if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) { |
| 4248 | if (!(chn->flags & CF_ISRESP)) |
| 4249 | goto abort_req; |
| 4250 | goto abort_res; |
| 4251 | } |
| 4252 | |
| 4253 | /* we get here if we need to wait for more data */ |
| 4254 | if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) { |
| 4255 | if (!tick_isset(chn->analyse_exp)) |
| 4256 | chn->analyse_exp = tick_add_ifset(now_ms, time); |
| 4257 | ret = HTTP_RULE_RES_YIELD; |
| 4258 | } |
| 4259 | |
| 4260 | end: |
| 4261 | return ret; |
| 4262 | |
| 4263 | abort_req: |
| 4264 | txn->status = 408; |
| 4265 | if (!(s->flags & SF_ERR_MASK)) |
| 4266 | s->flags |= SF_ERR_CLITO; |
| 4267 | if (!(s->flags & SF_FINST_MASK)) |
| 4268 | s->flags |= SF_FINST_D; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4269 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4270 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4271 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4272 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 4273 | ret = HTTP_RULE_RES_ABRT; |
| 4274 | goto end; |
| 4275 | |
| 4276 | abort_res: |
| 4277 | txn->status = 504; |
| 4278 | if (!(s->flags & SF_ERR_MASK)) |
| 4279 | s->flags |= SF_ERR_SRVTO; |
| 4280 | if (!(s->flags & SF_FINST_MASK)) |
| 4281 | s->flags |= SF_FINST_D; |
| 4282 | stream_inc_http_fail_ctr(s); |
| 4283 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 4284 | ret = HTTP_RULE_RES_ABRT; |
| 4285 | goto end; |
| 4286 | } |
| 4287 | |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4288 | void http_perform_server_redirect(struct stream *s, struct stconn *sc) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4289 | { |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4290 | struct channel *req = &s->req; |
| 4291 | struct channel *res = &s->res; |
| 4292 | struct server *srv; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4293 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4294 | struct htx_sl *sl; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4295 | struct ist path, location; |
| 4296 | unsigned int flags; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4297 | struct http_uri_parser parser; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4298 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4299 | /* |
| 4300 | * Create the location |
| 4301 | */ |
| 4302 | chunk_reset(&trash); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4303 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4304 | /* 1: add the server's prefix */ |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4305 | /* special prefix "/" means don't change URL */ |
| 4306 | srv = __objt_server(s->target); |
| 4307 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 4308 | if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len)) |
| 4309 | return; |
| 4310 | } |
| 4311 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4312 | /* 2: add the request Path */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4313 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 4314 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4315 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4316 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4317 | if (!isttest(path)) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4318 | return; |
| 4319 | |
| 4320 | if (!chunk_memcat(&trash, path.ptr, path.len)) |
| 4321 | return; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4322 | location = ist2(trash.area, trash.data); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4323 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4324 | /* |
| 4325 | * Create the 302 respone |
| 4326 | */ |
| 4327 | htx = htx_from_buf(&res->buf); |
| 4328 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 4329 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4330 | ist("HTTP/1.1"), ist("302"), ist("Found")); |
| 4331 | if (!sl) |
| 4332 | goto fail; |
| 4333 | sl->info.res.status = 302; |
| 4334 | s->txn->status = 302; |
| 4335 | |
| 4336 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) || |
| 4337 | !htx_add_header(htx, ist("Connection"), ist("close")) || |
| 4338 | !htx_add_header(htx, ist("Content-length"), ist("0")) || |
| 4339 | !htx_add_header(htx, ist("Location"), location)) |
| 4340 | goto fail; |
| 4341 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4342 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4343 | goto fail; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4344 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4345 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | c20afb8 | 2020-01-24 19:16:26 +0100 | [diff] [blame] | 4346 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4347 | if (!http_forward_proxy_resp(s, 1)) |
| 4348 | goto fail; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4349 | |
| 4350 | /* return without error. */ |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4351 | sc_shutr(sc); |
| 4352 | sc_shutw(sc); |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4353 | s->conn_err_type = STRM_ET_NONE; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4354 | sc->state = SC_ST_CLO; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4355 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4356 | if (!(s->flags & SF_ERR_MASK)) |
| 4357 | s->flags |= SF_ERR_LOCAL; |
| 4358 | if (!(s->flags & SF_FINST_MASK)) |
| 4359 | s->flags |= SF_FINST_C; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4360 | |
| 4361 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 4362 | srv_inc_sess_ctr(srv); |
| 4363 | srv_set_sess_last(srv); |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4364 | return; |
| 4365 | |
| 4366 | fail: |
| 4367 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4368 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4369 | channel_htx_truncate(res, htx); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4370 | } |
| 4371 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4372 | /* This function terminates the request because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4373 | * because an error was triggered during the body forwarding. |
| 4374 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4375 | static void http_end_request(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4376 | { |
| 4377 | struct channel *chn = &s->req; |
| 4378 | struct http_txn *txn = s->txn; |
| 4379 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4380 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4381 | |
Christopher Faulet | b42a8b6 | 2018-11-19 21:59:00 +0100 | [diff] [blame] | 4382 | if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR || |
| 4383 | txn->rsp.msg_state == HTTP_MSG_ERROR)) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4384 | channel_abort(chn); |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4385 | channel_htx_truncate(chn, htxbuf(&chn->buf)); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4386 | goto end; |
| 4387 | } |
| 4388 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4389 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) { |
| 4390 | 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] | 4391 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4392 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4393 | |
| 4394 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4395 | /* No need to read anymore, the request was completely parsed. |
| 4396 | * We can shut the read side unless we want to abort_on_close, |
| 4397 | * or we have a POST request. The issue with POST requests is |
| 4398 | * that some browsers still send a CRLF after the request, and |
| 4399 | * this CRLF must be read so that it does not remain in the kernel |
| 4400 | * buffers, otherwise a close could cause an RST on some systems |
| 4401 | * (eg: Linux). |
| 4402 | */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 4403 | 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] | 4404 | channel_dont_read(chn); |
| 4405 | |
| 4406 | /* if the server closes the connection, we want to immediately react |
| 4407 | * and close the socket to save packets and syscalls. |
| 4408 | */ |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 4409 | s->scb->flags |= SC_FL_NOHALF; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4410 | |
| 4411 | /* In any case we've finished parsing the request so we must |
| 4412 | * disable Nagle when sending data because 1) we're not going |
| 4413 | * to shut this side, and 2) the server is waiting for us to |
| 4414 | * send pending data. |
| 4415 | */ |
| 4416 | chn->flags |= CF_NEVER_WAIT; |
| 4417 | |
Christopher Faulet | d01ce40 | 2019-01-02 17:44:13 +0100 | [diff] [blame] | 4418 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4419 | /* The server has not finished to respond, so we |
| 4420 | * don't want to move in order not to upset it. |
| 4421 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4422 | 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] | 4423 | return; |
| 4424 | } |
| 4425 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4426 | /* When we get here, it means that both the request and the |
| 4427 | * response have finished receiving. Depending on the connection |
| 4428 | * mode, we'll have to wait for the last bytes to leave in either |
| 4429 | * direction, and sometimes for a close to be effective. |
| 4430 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4431 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4432 | /* Tunnel mode will not have any analyser so it needs to |
| 4433 | * poll for reads. |
| 4434 | */ |
| 4435 | channel_auto_read(chn); |
| 4436 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 4437 | } |
| 4438 | else { |
| 4439 | /* we're not expecting any new data to come for this |
| 4440 | * transaction, so we can close it. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4441 | * |
| 4442 | * However, there is an exception if the response |
| 4443 | * length is undefined. In this case, we need to wait |
| 4444 | * the close from the server. The response will be |
| 4445 | * switched in TUNNEL mode until the end. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4446 | */ |
| 4447 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 4448 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4449 | goto check_channel_flags; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4450 | |
| 4451 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4452 | channel_shutr_now(chn); |
| 4453 | channel_shutw_now(chn); |
| 4454 | } |
| 4455 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4456 | goto check_channel_flags; |
| 4457 | } |
| 4458 | |
| 4459 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4460 | http_msg_closing: |
| 4461 | /* nothing else to forward, just waiting for the output buffer |
| 4462 | * to be empty and for the shutw_now to take effect. |
| 4463 | */ |
| 4464 | if (channel_is_empty(chn)) { |
| 4465 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4466 | goto http_msg_closed; |
| 4467 | } |
| 4468 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4469 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4470 | goto end; |
| 4471 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4472 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4473 | return; |
| 4474 | } |
| 4475 | |
| 4476 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4477 | http_msg_closed: |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4478 | /* if we don't know whether the server will close, we need to hard close */ |
| 4479 | if (txn->rsp.flags & HTTP_MSGF_XFER_LEN) |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 4480 | s->scb->flags |= SC_FL_NOLINGER; /* we want to close ASAP */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4481 | /* 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] | 4482 | if (!(s->be->options & PR_O_ABRT_CLOSE)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4483 | channel_dont_read(chn); |
| 4484 | goto end; |
| 4485 | } |
| 4486 | |
| 4487 | check_channel_flags: |
| 4488 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4489 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4490 | /* if we've just closed an output, let's switch */ |
| 4491 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4492 | goto http_msg_closing; |
| 4493 | } |
| 4494 | |
| 4495 | end: |
| 4496 | chn->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4497 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4498 | chn->flags |= CF_NEVER_WAIT; |
| 4499 | if (HAS_REQ_DATA_FILTERS(s)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4500 | chn->analysers |= AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4501 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4502 | channel_auto_close(chn); |
| 4503 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4504 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4505 | } |
| 4506 | |
| 4507 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4508 | /* This function terminates the response because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4509 | * because an error was triggered during the body forwarding. |
| 4510 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4511 | static void http_end_response(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4512 | { |
| 4513 | struct channel *chn = &s->res; |
| 4514 | struct http_txn *txn = s->txn; |
| 4515 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4516 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4517 | |
Christopher Faulet | b42a8b6 | 2018-11-19 21:59:00 +0100 | [diff] [blame] | 4518 | if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR || |
| 4519 | txn->rsp.msg_state == HTTP_MSG_ERROR)) { |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4520 | channel_htx_truncate(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4521 | channel_abort(&s->req); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4522 | goto end; |
| 4523 | } |
| 4524 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4525 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) { |
| 4526 | 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] | 4527 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4528 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4529 | |
| 4530 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4531 | /* In theory, we don't need to read anymore, but we must |
| 4532 | * still monitor the server connection for a possible close |
| 4533 | * while the request is being uploaded, so we don't disable |
| 4534 | * reading. |
| 4535 | */ |
| 4536 | /* channel_dont_read(chn); */ |
| 4537 | |
| 4538 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4539 | /* The client seems to still be sending data, probably |
| 4540 | * because we got an error response during an upload. |
| 4541 | * We have the choice of either breaking the connection |
| 4542 | * or letting it pass through. Let's do the later. |
| 4543 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4544 | 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] | 4545 | return; |
| 4546 | } |
| 4547 | |
| 4548 | /* When we get here, it means that both the request and the |
| 4549 | * response have finished receiving. Depending on the connection |
| 4550 | * mode, we'll have to wait for the last bytes to leave in either |
| 4551 | * direction, and sometimes for a close to be effective. |
| 4552 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4553 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4554 | channel_auto_read(chn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4555 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4556 | } |
| 4557 | else { |
| 4558 | /* we're not expecting any new data to come for this |
| 4559 | * transaction, so we can close it. |
| 4560 | */ |
| 4561 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4562 | channel_shutr_now(chn); |
| 4563 | channel_shutw_now(chn); |
| 4564 | } |
| 4565 | } |
| 4566 | goto check_channel_flags; |
| 4567 | } |
| 4568 | |
| 4569 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4570 | http_msg_closing: |
| 4571 | /* nothing else to forward, just waiting for the output buffer |
| 4572 | * to be empty and for the shutw_now to take effect. |
| 4573 | */ |
| 4574 | if (channel_is_empty(chn)) { |
| 4575 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4576 | goto http_msg_closed; |
| 4577 | } |
| 4578 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4579 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4580 | _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts); |
| 4581 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 4582 | if (strm_sess(s)->listener && strm_sess(s)->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4583 | _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4584 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4585 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4586 | goto end; |
| 4587 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4588 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4589 | return; |
| 4590 | } |
| 4591 | |
| 4592 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4593 | http_msg_closed: |
| 4594 | /* drop any pending data */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4595 | channel_htx_truncate(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4596 | channel_abort(&s->req); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4597 | goto end; |
| 4598 | } |
| 4599 | |
| 4600 | check_channel_flags: |
| 4601 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4602 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4603 | /* if we've just closed an output, let's switch */ |
| 4604 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4605 | goto http_msg_closing; |
| 4606 | } |
| 4607 | |
| 4608 | end: |
| 4609 | chn->analysers &= AN_RES_FLT_END; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4610 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4611 | chn->flags |= CF_NEVER_WAIT; |
| 4612 | if (HAS_RSP_DATA_FILTERS(s)) |
| 4613 | chn->analysers |= AN_RES_FLT_XFER_DATA; |
| 4614 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4615 | channel_auto_close(chn); |
| 4616 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4617 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4618 | } |
| 4619 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4620 | /* Forward a response generated by HAProxy (error/redirect/return). This |
| 4621 | * function forwards all pending incoming data. If <final> is set to 0, nothing |
| 4622 | * more is performed. It is used for 1xx informational messages. Otherwise, the |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4623 | * transaction is terminated and the request is emptied. On success 1 is |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4624 | * returned. If an error occurred, 0 is returned. If it fails, this function |
| 4625 | * only exits. It is the caller responsibility to do the cleanup. |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4626 | */ |
| 4627 | int http_forward_proxy_resp(struct stream *s, int final) |
| 4628 | { |
| 4629 | struct channel *req = &s->req; |
| 4630 | struct channel *res = &s->res; |
| 4631 | struct htx *htx = htxbuf(&res->buf); |
| 4632 | size_t data; |
| 4633 | |
| 4634 | if (final) { |
| 4635 | htx->flags |= HTX_FL_PROXY_RESP; |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4636 | |
Christopher Faulet | aab1b67 | 2020-11-18 16:44:02 +0100 | [diff] [blame] | 4637 | if (!htx_is_empty(htx) && !http_eval_after_res_rules(s)) |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 4638 | return 0; |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4639 | |
Christopher Faulet | d6c4836 | 2020-10-19 18:01:38 +0200 | [diff] [blame] | 4640 | if (s->txn->meth == HTTP_METH_HEAD) |
| 4641 | htx_skip_msg_payload(htx); |
| 4642 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4643 | channel_auto_read(req); |
| 4644 | channel_abort(req); |
| 4645 | channel_auto_close(req); |
| 4646 | channel_htx_erase(req, htxbuf(&req->buf)); |
| 4647 | |
| 4648 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 4649 | channel_auto_read(res); |
| 4650 | channel_auto_close(res); |
| 4651 | channel_shutr_now(res); |
Christopher Faulet | 1a9db7c | 2020-06-25 15:36:45 +0200 | [diff] [blame] | 4652 | res->flags |= CF_EOI; /* The response is terminated, add EOI */ |
Christopher Faulet | 42432f3 | 2020-11-20 17:43:16 +0100 | [diff] [blame] | 4653 | htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */ |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4654 | } |
Christopher Faulet | cf6898c | 2020-06-25 15:55:11 +0200 | [diff] [blame] | 4655 | else { |
| 4656 | /* Send ASAP informational messages. Rely on CF_EOI for final |
| 4657 | * response. |
| 4658 | */ |
| 4659 | res->flags |= CF_SEND_DONTWAIT; |
| 4660 | } |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4661 | |
| 4662 | data = htx->data - co_data(res); |
| 4663 | c_adv(res, data); |
| 4664 | htx->first = -1; |
| 4665 | res->total += data; |
| 4666 | return 1; |
| 4667 | } |
| 4668 | |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4669 | void http_server_error(struct stream *s, struct stconn *sc, int err, |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4670 | int finst, struct http_reply *msg) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4671 | { |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4672 | http_reply_and_close(s, s->txn->status, msg); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4673 | if (!(s->flags & SF_ERR_MASK)) |
| 4674 | s->flags |= err; |
| 4675 | if (!(s->flags & SF_FINST_MASK)) |
| 4676 | s->flags |= finst; |
| 4677 | } |
| 4678 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4679 | 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] | 4680 | { |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4681 | if (!msg) { |
| 4682 | channel_htx_truncate(&s->res, htxbuf(&s->res.buf)); |
| 4683 | goto end; |
| 4684 | } |
| 4685 | |
| 4686 | if (http_reply_message(s, msg) == -1) { |
| 4687 | /* 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] | 4688 | * it is already an internal error. If it was already a "const" |
| 4689 | * 500 error, just fail. |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4690 | */ |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4691 | if (s->txn->status == 500) { |
| 4692 | if (s->txn->flags & TX_CONST_REPLY) |
| 4693 | goto end; |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4694 | s->txn->flags |= TX_CONST_REPLY; |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4695 | } |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4696 | s->txn->status = 500; |
| 4697 | s->txn->http_reply = NULL; |
| 4698 | return http_reply_and_close(s, s->txn->status, http_error_message(s)); |
| 4699 | } |
| 4700 | |
| 4701 | end: |
| 4702 | s->res.wex = tick_add_ifset(now_ms, s->res.wto); |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4703 | |
Christopher Faulet | 2d56500 | 2021-09-10 09:17:50 +0200 | [diff] [blame] | 4704 | /* At this staged, HTTP analysis is finished */ |
| 4705 | s->req.analysers &= AN_REQ_FLT_END; |
| 4706 | s->req.analyse_exp = TICK_ETERNITY; |
| 4707 | |
| 4708 | s->res.analysers &= AN_RES_FLT_END; |
| 4709 | s->res.analyse_exp = TICK_ETERNITY; |
| 4710 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4711 | channel_auto_read(&s->req); |
| 4712 | channel_abort(&s->req); |
| 4713 | channel_auto_close(&s->req); |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4714 | channel_htx_erase(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4715 | channel_auto_read(&s->res); |
| 4716 | channel_auto_close(&s->res); |
| 4717 | channel_shutr_now(&s->res); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4718 | } |
| 4719 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4720 | struct http_reply *http_error_message(struct stream *s) |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4721 | { |
| 4722 | const int msgnum = http_get_status_idx(s->txn->status); |
| 4723 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4724 | if (s->txn->http_reply) |
| 4725 | return s->txn->http_reply; |
| 4726 | else if (s->be->replies[msgnum]) |
| 4727 | return s->be->replies[msgnum]; |
| 4728 | else if (strm_fe(s)->replies[msgnum]) |
| 4729 | return strm_fe(s)->replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4730 | else |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4731 | return &http_err_replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4732 | } |
| 4733 | |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4734 | /* Produces an HTX message from an http reply. Depending on the http reply type, |
| 4735 | * a, errorfile, an raw file or a log-format string is used. On success, it |
| 4736 | * returns 0. If an error occurs -1 is returned. If it fails, this function only |
| 4737 | * exits. It is the caller responsibility to do the cleanup. |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4738 | */ |
Christopher Faulet | ae43b6c | 2020-05-27 15:24:22 +0200 | [diff] [blame] | 4739 | 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] | 4740 | { |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4741 | struct buffer *errmsg; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4742 | struct htx_sl *sl; |
| 4743 | struct buffer *body = NULL; |
| 4744 | const char *status, *reason, *clen, *ctype; |
| 4745 | unsigned int slflags; |
| 4746 | int ret = 0; |
| 4747 | |
Christopher Faulet | e29a97e | 2020-05-14 14:49:25 +0200 | [diff] [blame] | 4748 | /* |
| 4749 | * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so |
| 4750 | * |
| 4751 | * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled |
| 4752 | * as no payload if NULL. the TXN status code is set with the status |
| 4753 | * of the original reply. |
| 4754 | */ |
| 4755 | |
| 4756 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4757 | if (reply->body.reply) |
| 4758 | reply = reply->body.reply; |
| 4759 | } |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4760 | if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) { |
| 4761 | /* get default error message */ |
| 4762 | if (reply == s->txn->http_reply) |
| 4763 | s->txn->http_reply = NULL; |
| 4764 | reply = http_error_message(s); |
| 4765 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4766 | if (reply->body.reply) |
| 4767 | reply = reply->body.reply; |
| 4768 | } |
| 4769 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4770 | |
| 4771 | if (reply->type == HTTP_REPLY_ERRMSG) { |
| 4772 | /* implicit or explicit error message*/ |
| 4773 | errmsg = reply->body.errmsg; |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4774 | if (errmsg && !b_is_null(errmsg)) { |
Christopher Faulet | 2056736 | 2020-05-15 14:52:49 +0200 | [diff] [blame] | 4775 | if (!htx_copy_msg(htx, errmsg)) |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4776 | goto fail; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4777 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4778 | } |
| 4779 | else { |
| 4780 | /* no payload, file or log-format string */ |
| 4781 | if (reply->type == HTTP_REPLY_RAW) { |
| 4782 | /* file */ |
| 4783 | body = &reply->body.obj; |
| 4784 | } |
| 4785 | else if (reply->type == HTTP_REPLY_LOGFMT) { |
| 4786 | /* log-format string */ |
| 4787 | body = alloc_trash_chunk(); |
| 4788 | if (!body) |
| 4789 | goto fail_alloc; |
| 4790 | body->data = build_logline(s, body->area, body->size, &reply->body.fmt); |
| 4791 | } |
| 4792 | /* else no payload */ |
| 4793 | |
| 4794 | status = ultoa(reply->status); |
| 4795 | reason = http_get_reason(reply->status); |
| 4796 | slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN); |
| 4797 | if (!body || !b_data(body)) |
| 4798 | slflags |= HTX_SL_F_BODYLESS; |
| 4799 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4800 | if (!sl) |
| 4801 | goto fail; |
| 4802 | sl->info.res.status = reply->status; |
| 4803 | |
| 4804 | clen = (body ? ultoa(b_data(body)) : "0"); |
| 4805 | ctype = reply->ctype; |
| 4806 | |
| 4807 | if (!LIST_ISEMPTY(&reply->hdrs)) { |
| 4808 | struct http_reply_hdr *hdr; |
| 4809 | struct buffer *value = alloc_trash_chunk(); |
| 4810 | |
| 4811 | if (!value) |
| 4812 | goto fail; |
| 4813 | |
| 4814 | list_for_each_entry(hdr, &reply->hdrs, list) { |
| 4815 | chunk_reset(value); |
| 4816 | value->data = build_logline(s, value->area, value->size, &hdr->value); |
| 4817 | if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) { |
| 4818 | free_trash_chunk(value); |
| 4819 | goto fail; |
| 4820 | } |
| 4821 | chunk_reset(value); |
| 4822 | } |
| 4823 | free_trash_chunk(value); |
| 4824 | } |
| 4825 | |
| 4826 | if (!htx_add_header(htx, ist("content-length"), ist(clen)) || |
| 4827 | (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) || |
| 4828 | !htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4829 | (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] | 4830 | goto fail; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4831 | |
| 4832 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4833 | } |
| 4834 | |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4835 | leave: |
| 4836 | if (reply->type == HTTP_REPLY_LOGFMT) |
| 4837 | free_trash_chunk(body); |
| 4838 | return ret; |
| 4839 | |
| 4840 | fail_alloc: |
| 4841 | if (!(s->flags & SF_ERR_MASK)) |
| 4842 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4843 | /* fall through */ |
| 4844 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4845 | ret = -1; |
| 4846 | goto leave; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4847 | } |
| 4848 | |
| 4849 | /* 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] | 4850 | * occurs -1 is returned and the response channel is truncated, removing this |
| 4851 | * way the faulty reply. This function may fail when the reply is formatted |
| 4852 | * (http_reply_to_htx) or when the reply is forwarded |
| 4853 | * (http_forward_proxy_resp). On the last case, it is because a |
| 4854 | * http-after-response rule fails. |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4855 | */ |
| 4856 | int http_reply_message(struct stream *s, struct http_reply *reply) |
| 4857 | { |
| 4858 | struct channel *res = &s->res; |
| 4859 | struct htx *htx = htx_from_buf(&res->buf); |
| 4860 | |
| 4861 | if (s->txn->status == -1) |
| 4862 | s->txn->status = reply->status; |
| 4863 | channel_htx_truncate(res, htx); |
| 4864 | |
| 4865 | if (http_reply_to_htx(s, htx, reply) == -1) |
| 4866 | goto fail; |
| 4867 | |
| 4868 | htx_to_buf(htx, &s->res.buf); |
| 4869 | if (!http_forward_proxy_resp(s, 1)) |
| 4870 | goto fail; |
| 4871 | return 0; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4872 | |
| 4873 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4874 | channel_htx_truncate(res, htx); |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4875 | if (!(s->flags & SF_ERR_MASK)) |
| 4876 | s->flags |= SF_ERR_PRXCOND; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4877 | return -1; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4878 | } |
| 4879 | |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4880 | /* 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] | 4881 | * that the server side is closed. Note that err_type is actually a |
| 4882 | * bitmask, where almost only aborts may be cumulated with other |
| 4883 | * values. We consider that aborted operations are more important |
| 4884 | * than timeouts or errors due to the fact that nobody else in the |
| 4885 | * logs might explain incomplete retries. All others should avoid |
| 4886 | * being cumulated. It should normally not be possible to have multiple |
| 4887 | * aborts at once, but just in case, the first one in sequence is reported. |
| 4888 | * Note that connection errors appearing on the second request of a keep-alive |
| 4889 | * connection are not reported since this allows the client to retry. |
| 4890 | */ |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4891 | void http_return_srv_error(struct stream *s, struct stconn *sc) |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4892 | { |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4893 | int err_type = s->conn_err_type; |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4894 | |
| 4895 | /* set s->txn->status for http_error_message(s) */ |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4896 | if (err_type & STRM_ET_QUEUE_ABRT) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4897 | s->txn->status = -1; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4898 | http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_Q, NULL); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4899 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4900 | else if (err_type & STRM_ET_CONN_ABRT) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4901 | s->txn->status = -1; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4902 | http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_C, NULL); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4903 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4904 | else if (err_type & STRM_ET_QUEUE_TO) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4905 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4906 | http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4907 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4908 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4909 | else if (err_type & STRM_ET_QUEUE_ERR) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4910 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4911 | http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4912 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4913 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4914 | else if (err_type & STRM_ET_CONN_TO) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4915 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4916 | http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4917 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4918 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4919 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4920 | else if (err_type & STRM_ET_CONN_ERR) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4921 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4922 | http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4923 | (s->flags & SF_SRV_REUSED) ? NULL : |
| 4924 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4925 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4926 | else if (err_type & STRM_ET_CONN_RES) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4927 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4928 | http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4929 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4930 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4931 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4932 | else { /* STRM_ET_CONN_OTHER and others */ |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4933 | s->txn->status = 500; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4934 | http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4935 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4936 | } |
| 4937 | } |
| 4938 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4939 | |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4940 | /* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0 |
| 4941 | * on success and -1 on error. |
| 4942 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4943 | 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] | 4944 | { |
| 4945 | /* If we have HTTP/1.1 message with a body and Expect: 100-continue, |
| 4946 | * then we must send an HTTP/1.1 100 Continue intermediate response. |
| 4947 | */ |
| 4948 | if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) && |
| 4949 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
| 4950 | struct ist hdr = { .ptr = "Expect", .len = 6 }; |
| 4951 | struct http_hdr_ctx ctx; |
| 4952 | |
| 4953 | ctx.blk = NULL; |
| 4954 | /* Expect is allowed in 1.1, look for it */ |
| 4955 | if (http_find_header(htx, hdr, &ctx, 0) && |
| 4956 | unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4957 | if (http_reply_100_continue(s) == -1) |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4958 | return -1; |
| 4959 | http_remove_header(htx, &ctx); |
| 4960 | } |
| 4961 | } |
| 4962 | return 0; |
| 4963 | } |
| 4964 | |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4965 | /* Send a 100-Continue response to the client. It returns 0 on success and -1 |
| 4966 | * on error. The response channel is updated accordingly. |
| 4967 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4968 | static int http_reply_100_continue(struct stream *s) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4969 | { |
| 4970 | struct channel *res = &s->res; |
| 4971 | struct htx *htx = htx_from_buf(&res->buf); |
| 4972 | struct htx_sl *sl; |
| 4973 | unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11| |
| 4974 | HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4975 | |
| 4976 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4977 | ist("HTTP/1.1"), ist("100"), ist("Continue")); |
| 4978 | if (!sl) |
| 4979 | goto fail; |
| 4980 | sl->info.res.status = 100; |
| 4981 | |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 4982 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4983 | goto fail; |
| 4984 | |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4985 | if (!http_forward_proxy_resp(s, 0)) |
| 4986 | goto fail; |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4987 | return 0; |
| 4988 | |
| 4989 | fail: |
| 4990 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4991 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4992 | channel_htx_truncate(res, htx); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4993 | return -1; |
| 4994 | } |
| 4995 | |
Christopher Faulet | 12c51e2 | 2018-11-28 15:59:42 +0100 | [diff] [blame] | 4996 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4997 | /* |
| 4998 | * Capture headers from message <htx> according to header list <cap_hdr>, and |
| 4999 | * fill the <cap> pointers appropriately. |
| 5000 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5001 | 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] | 5002 | { |
| 5003 | struct cap_hdr *h; |
| 5004 | int32_t pos; |
| 5005 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5006 | 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] | 5007 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5008 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5009 | struct ist n, v; |
| 5010 | |
| 5011 | if (type == HTX_BLK_EOH) |
| 5012 | break; |
| 5013 | if (type != HTX_BLK_HDR) |
| 5014 | continue; |
| 5015 | |
| 5016 | n = htx_get_blk_name(htx, blk); |
| 5017 | |
| 5018 | for (h = cap_hdr; h; h = h->next) { |
| 5019 | if (h->namelen && (h->namelen == n.len) && |
| 5020 | (strncasecmp(n.ptr, h->name, h->namelen) == 0)) { |
| 5021 | if (cap[h->index] == NULL) |
| 5022 | cap[h->index] = |
| 5023 | pool_alloc(h->pool); |
| 5024 | |
| 5025 | if (cap[h->index] == NULL) { |
| 5026 | ha_alert("HTTP capture : out of memory.\n"); |
| 5027 | break; |
| 5028 | } |
| 5029 | |
| 5030 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 5031 | v = isttrim(v, h->len); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5032 | |
| 5033 | memcpy(cap[h->index], v.ptr, v.len); |
| 5034 | cap[h->index][v.len]=0; |
| 5035 | } |
| 5036 | } |
| 5037 | } |
| 5038 | } |
| 5039 | |
Christopher Faulet | 0b6bdc5 | 2018-10-24 11:05:36 +0200 | [diff] [blame] | 5040 | /* Delete a value in a header between delimiters <from> and <next>. The header |
| 5041 | * itself is delimited by <start> and <end> pointers. The number of characters |
| 5042 | * displaced is returned, and the pointer to the first delimiter is updated if |
| 5043 | * required. The function tries as much as possible to respect the following |
| 5044 | * principles : |
| 5045 | * - replace <from> delimiter by the <next> one unless <from> points to <start>, |
| 5046 | * in which case <next> is simply removed |
| 5047 | * - set exactly one space character after the new first delimiter, unless there |
| 5048 | * are not enough characters in the block being moved to do so. |
| 5049 | * - remove unneeded spaces before the previous delimiter and after the new |
| 5050 | * one. |
| 5051 | * |
| 5052 | * It is the caller's responsibility to ensure that : |
| 5053 | * - <from> points to a valid delimiter or <start> ; |
| 5054 | * - <next> points to a valid delimiter or <end> ; |
| 5055 | * - there are non-space chars before <from>. |
| 5056 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5057 | 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] | 5058 | { |
| 5059 | char *prev = *from; |
| 5060 | |
| 5061 | if (prev == start) { |
| 5062 | /* We're removing the first value. eat the semicolon, if <next> |
| 5063 | * is lower than <end> */ |
| 5064 | if (next < end) |
| 5065 | next++; |
| 5066 | |
| 5067 | while (next < end && HTTP_IS_SPHT(*next)) |
| 5068 | next++; |
| 5069 | } |
| 5070 | else { |
| 5071 | /* Remove useless spaces before the old delimiter. */ |
| 5072 | while (HTTP_IS_SPHT(*(prev-1))) |
| 5073 | prev--; |
| 5074 | *from = prev; |
| 5075 | |
| 5076 | /* copy the delimiter and if possible a space if we're |
| 5077 | * not at the end of the line. |
| 5078 | */ |
| 5079 | if (next < end) { |
| 5080 | *prev++ = *next++; |
| 5081 | if (prev + 1 < next) |
| 5082 | *prev++ = ' '; |
| 5083 | while (next < end && HTTP_IS_SPHT(*next)) |
| 5084 | next++; |
| 5085 | } |
| 5086 | } |
| 5087 | memmove(prev, next, end - next); |
| 5088 | return (prev - next); |
| 5089 | } |
| 5090 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5091 | |
| 5092 | /* 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] | 5093 | * 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] | 5094 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5095 | 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] | 5096 | { |
| 5097 | struct ist dst = ist2(str, 0); |
| 5098 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5099 | if (istcat(&dst, htx_sl_req_meth(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5100 | goto end; |
| 5101 | if (dst.len + 1 > len) |
| 5102 | goto end; |
| 5103 | dst.ptr[dst.len++] = ' '; |
| 5104 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5105 | if (istcat(&dst, htx_sl_req_uri(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5106 | goto end; |
| 5107 | if (dst.len + 1 > len) |
| 5108 | goto end; |
| 5109 | dst.ptr[dst.len++] = ' '; |
| 5110 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5111 | istcat(&dst, htx_sl_req_vsn(sl), len); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5112 | end: |
| 5113 | return dst.len; |
| 5114 | } |
| 5115 | |
| 5116 | /* |
| 5117 | * Print a debug line with a start line. |
| 5118 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5119 | 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] | 5120 | { |
| 5121 | struct session *sess = strm_sess(s); |
| 5122 | int max; |
| 5123 | |
| 5124 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 5125 | dir, |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 5126 | objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1, |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 5127 | sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5128 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5129 | max = HTX_SL_P1_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5130 | UBOUND(max, trash.size - trash.data - 3); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5131 | chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5132 | trash.area[trash.data++] = ' '; |
| 5133 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5134 | max = HTX_SL_P2_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5135 | UBOUND(max, trash.size - trash.data - 2); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5136 | chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5137 | trash.area[trash.data++] = ' '; |
| 5138 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5139 | max = HTX_SL_P3_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5140 | UBOUND(max, trash.size - trash.data - 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5141 | chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5142 | trash.area[trash.data++] = '\n'; |
| 5143 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 5144 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5145 | } |
| 5146 | |
| 5147 | /* |
| 5148 | * Print a debug line with a header. |
| 5149 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5150 | 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] | 5151 | { |
| 5152 | struct session *sess = strm_sess(s); |
| 5153 | int max; |
| 5154 | |
| 5155 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 5156 | dir, |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 5157 | objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1, |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 5158 | sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5159 | |
| 5160 | max = n.len; |
| 5161 | UBOUND(max, trash.size - trash.data - 3); |
| 5162 | chunk_memcat(&trash, n.ptr, max); |
| 5163 | trash.area[trash.data++] = ':'; |
| 5164 | trash.area[trash.data++] = ' '; |
| 5165 | |
| 5166 | max = v.len; |
| 5167 | UBOUND(max, trash.size - trash.data - 1); |
| 5168 | chunk_memcat(&trash, v.ptr, max); |
| 5169 | trash.area[trash.data++] = '\n'; |
| 5170 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 5171 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5172 | } |
| 5173 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5174 | void http_txn_reset_req(struct http_txn *txn) |
| 5175 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 5176 | txn->req.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5177 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 5178 | } |
| 5179 | |
| 5180 | void http_txn_reset_res(struct http_txn *txn) |
| 5181 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 5182 | txn->rsp.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5183 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 5184 | } |
| 5185 | |
| 5186 | /* |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5187 | * Create and initialize a new HTTP transaction for stream <s>. This should be |
| 5188 | * used before processing any new request. It returns the transaction or NLULL |
| 5189 | * on error. |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5190 | */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5191 | struct http_txn *http_create_txn(struct stream *s) |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5192 | { |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5193 | struct http_txn *txn; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 5194 | struct stconn *sc = s->scf; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5195 | |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5196 | txn = pool_alloc(pool_head_http_txn); |
| 5197 | if (!txn) |
| 5198 | return NULL; |
| 5199 | s->txn = txn; |
| 5200 | |
Christopher Faulet | 5eb67f5 | 2022-06-22 17:12:05 +0200 | [diff] [blame] | 5201 | txn->meth = HTTP_METH_OTHER; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 5202 | txn->flags = ((sc && sc_ep_test(sc, SE_FL_NOT_FIRST)) ? TX_NOT_FIRST : 0); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5203 | txn->status = -1; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 5204 | txn->http_reply = NULL; |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 5205 | txn->l7_buffer = BUF_NULL; |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 5206 | write_u32(txn->cache_hash, 0); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5207 | |
| 5208 | txn->cookie_first_date = 0; |
| 5209 | txn->cookie_last_date = 0; |
| 5210 | |
| 5211 | txn->srv_cookie = NULL; |
| 5212 | txn->cli_cookie = NULL; |
| 5213 | txn->uri = NULL; |
| 5214 | |
| 5215 | http_txn_reset_req(txn); |
| 5216 | http_txn_reset_res(txn); |
| 5217 | |
| 5218 | txn->req.chn = &s->req; |
| 5219 | txn->rsp.chn = &s->res; |
| 5220 | |
| 5221 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
| 5222 | |
Willy Tarreau | b7bfcb3 | 2021-08-31 08:13:25 +0200 | [diff] [blame] | 5223 | vars_init_head(&s->vars_txn, SCOPE_TXN); |
| 5224 | vars_init_head(&s->vars_reqres, SCOPE_REQ); |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5225 | |
| 5226 | return txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5227 | } |
| 5228 | |
| 5229 | /* to be used at the end of a transaction */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5230 | void http_destroy_txn(struct stream *s) |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5231 | { |
| 5232 | struct http_txn *txn = s->txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5233 | |
| 5234 | /* these ones will have been dynamically allocated */ |
| 5235 | pool_free(pool_head_requri, txn->uri); |
| 5236 | pool_free(pool_head_capture, txn->cli_cookie); |
| 5237 | pool_free(pool_head_capture, txn->srv_cookie); |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5238 | pool_free(pool_head_uniqueid, s->unique_id.ptr); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5239 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5240 | s->unique_id = IST_NULL; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5241 | txn->uri = NULL; |
| 5242 | txn->srv_cookie = NULL; |
| 5243 | txn->cli_cookie = NULL; |
| 5244 | |
Christopher Faulet | 5939925 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 5245 | if (!LIST_ISEMPTY(&s->vars_txn.head)) |
| 5246 | vars_prune(&s->vars_txn, s->sess, s); |
| 5247 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 5248 | vars_prune(&s->vars_reqres, s->sess, s); |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5249 | |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 5250 | b_free(&txn->l7_buffer); |
| 5251 | |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5252 | pool_free(pool_head_http_txn, txn); |
| 5253 | s->txn = NULL; |
Christopher Faulet | 5939925 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 5254 | } |
| 5255 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5256 | |
| 5257 | DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5258 | |
Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 5259 | /* |
| 5260 | * Local variables: |
| 5261 | * c-indent-level: 8 |
| 5262 | * c-basic-offset: 8 |
| 5263 | * End: |
| 5264 | */ |