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); |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2648 | int i, end = istlen(n); |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2649 | |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2650 | for (i = 0; i < end; i++) { |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2651 | if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') { |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2652 | break; |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2653 | } |
| 2654 | } |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2655 | |
| 2656 | if (i < end) { |
| 2657 | /* Disallowed character found - block the request or remove the header */ |
| 2658 | if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK) |
| 2659 | goto block; |
| 2660 | blk = htx_remove_blk(htx, blk); |
| 2661 | continue; |
| 2662 | } |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2663 | } |
| 2664 | if (type == HTX_BLK_EOH) |
| 2665 | break; |
| 2666 | |
| 2667 | blk = htx_get_next_blk(htx, blk); |
| 2668 | } |
| 2669 | out: |
| 2670 | return rule_ret; |
| 2671 | block: |
| 2672 | /* Block the request returning a 403-Forbidden response */ |
| 2673 | s->txn->status = 403; |
| 2674 | rule_ret = HTTP_RULE_RES_DENY; |
| 2675 | goto out; |
| 2676 | } |
| 2677 | |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2678 | /* Replace all headers matching the name <name>. The header value is replaced if |
| 2679 | * it matches the regex <re>. <str> is used for the replacement. If <full> is |
| 2680 | * set to 1, the full-line is matched and replaced. Otherwise, comma-separated |
| 2681 | * values are evaluated one by one. It returns 0 on success and -1 on error. |
| 2682 | */ |
| 2683 | int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name, |
| 2684 | const char *str, struct my_regex *re, int full) |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2685 | { |
| 2686 | struct http_hdr_ctx ctx; |
| 2687 | struct buffer *output = get_trash_chunk(); |
| 2688 | |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2689 | ctx.blk = NULL; |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2690 | while (http_find_header(htx, name, &ctx, full)) { |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2691 | if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0)) |
| 2692 | continue; |
| 2693 | |
| 2694 | output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch); |
| 2695 | if (output->data == -1) |
| 2696 | return -1; |
| 2697 | if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data))) |
| 2698 | return -1; |
| 2699 | } |
| 2700 | return 0; |
| 2701 | } |
| 2702 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2703 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 2704 | * takes the string from the variable 'replace' with length 'len', then modifies |
| 2705 | * the relevant part of the request line accordingly. Then it updates various |
| 2706 | * pointers to the next elements which were moved, and the total buffer length. |
| 2707 | * It finds the action to be performed in p[2], previously filled by function |
| 2708 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 2709 | * error, though this can be revisited when this code is finally exploited. |
| 2710 | * |
| 2711 | * '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] | 2712 | * 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] | 2713 | * |
| 2714 | * In query string case, the mark question '?' must be set at the start of the |
| 2715 | * string by the caller, event if the replacement query string is empty. |
| 2716 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2717 | int http_req_replace_stline(int action, const char *replace, int len, |
| 2718 | struct proxy *px, struct stream *s) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2719 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2720 | struct htx *htx = htxbuf(&s->req.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2721 | |
| 2722 | switch (action) { |
| 2723 | case 0: // method |
| 2724 | if (!http_replace_req_meth(htx, ist2(replace, len))) |
| 2725 | return -1; |
| 2726 | break; |
| 2727 | |
| 2728 | case 1: // path |
Christopher Faulet | b8ce505 | 2020-08-31 16:11:57 +0200 | [diff] [blame] | 2729 | if (!http_replace_req_path(htx, ist2(replace, len), 0)) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2730 | return -1; |
| 2731 | break; |
| 2732 | |
| 2733 | case 2: // query |
| 2734 | if (!http_replace_req_query(htx, ist2(replace, len))) |
| 2735 | return -1; |
| 2736 | break; |
| 2737 | |
| 2738 | case 3: // uri |
| 2739 | if (!http_replace_req_uri(htx, ist2(replace, len))) |
| 2740 | return -1; |
| 2741 | break; |
| 2742 | |
Christopher Faulet | 312294f | 2020-09-02 17:17:44 +0200 | [diff] [blame] | 2743 | case 4: // path + query |
| 2744 | if (!http_replace_req_path(htx, ist2(replace, len), 1)) |
| 2745 | return -1; |
| 2746 | break; |
| 2747 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2748 | default: |
| 2749 | return -1; |
| 2750 | } |
| 2751 | return 0; |
| 2752 | } |
| 2753 | |
| 2754 | /* This function replace the HTTP status code and the associated message. The |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2755 | * variable <status> contains the new status code. This function never fails. It |
| 2756 | * returns 0 in case of success, -1 in case of internal error. |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2757 | */ |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2758 | 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] | 2759 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2760 | struct htx *htx = htxbuf(&s->res.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2761 | char *res; |
| 2762 | |
| 2763 | chunk_reset(&trash); |
| 2764 | res = ultoa_o(status, trash.area, trash.size); |
| 2765 | trash.data = res - trash.area; |
| 2766 | |
| 2767 | /* Do we have a custom reason format string? */ |
Tim Duesterhus | e296d3e | 2020-03-05 17:56:31 +0100 | [diff] [blame] | 2768 | if (!isttest(reason)) { |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2769 | const char *str = http_get_reason(status); |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 2770 | reason = ist(str); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2771 | } |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2772 | |
Christopher Faulet | bde2c4c | 2020-08-31 16:43:34 +0200 | [diff] [blame] | 2773 | if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason)) |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2774 | return -1; |
| 2775 | return 0; |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2776 | } |
| 2777 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2778 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
| 2779 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 2780 | * further processing of the request (auth, deny, ...), and defaults to |
| 2781 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 2782 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 2783 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 2784 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 2785 | * status. |
| 2786 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2787 | static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, |
| 2788 | struct list *rules, struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2789 | { |
| 2790 | struct session *sess = strm_sess(s); |
| 2791 | struct http_txn *txn = s->txn; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2792 | struct act_rule *rule; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2793 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2794 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2795 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2796 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2797 | * resume condition. If a resume is needed it is always in the action |
| 2798 | * and never in the ACL or converters. In this case, we initialise the |
| 2799 | * current rule, and go to the action execution point. |
| 2800 | */ |
| 2801 | if (s->current_rule) { |
| 2802 | rule = s->current_rule; |
| 2803 | s->current_rule = NULL; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2804 | 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] | 2805 | goto resume_execution; |
| 2806 | } |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2807 | 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] | 2808 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2809 | restart: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2810 | /* start the ruleset evaluation in strict mode */ |
| 2811 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2812 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2813 | list_for_each_entry(rule, s->current_rule_list, list) { |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2814 | /* check optional condition */ |
| 2815 | if (rule->cond) { |
| 2816 | int ret; |
| 2817 | |
| 2818 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 2819 | ret = acl_pass(ret); |
| 2820 | |
| 2821 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2822 | ret = !ret; |
| 2823 | |
| 2824 | if (!ret) /* condition not matched */ |
| 2825 | continue; |
| 2826 | } |
| 2827 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2828 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2829 | resume_execution: |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 2830 | if (rule->kw->flags & KWF_EXPERIMENTAL) |
| 2831 | mark_tainted(TAINTED_ACTION_EXP_EXECUTED); |
| 2832 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2833 | /* Always call the action function if defined */ |
| 2834 | if (rule->action_ptr) { |
| 2835 | if ((s->req.flags & CF_READ_ERROR) || |
| 2836 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 2837 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2838 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2839 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2840 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2841 | case ACT_RET_CONT: |
| 2842 | break; |
| 2843 | case ACT_RET_STOP: |
| 2844 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2845 | s->last_rule_file = rule->conf.file; |
| 2846 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2847 | goto end; |
| 2848 | case ACT_RET_YIELD: |
| 2849 | s->current_rule = rule; |
| 2850 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2851 | goto end; |
| 2852 | case ACT_RET_ERR: |
| 2853 | rule_ret = HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2854 | s->last_rule_file = rule->conf.file; |
| 2855 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2856 | goto end; |
| 2857 | case ACT_RET_DONE: |
| 2858 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2859 | s->last_rule_file = rule->conf.file; |
| 2860 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2861 | goto end; |
| 2862 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 2863 | if (txn->status == -1) |
| 2864 | txn->status = 403; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2865 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2866 | s->last_rule_file = rule->conf.file; |
| 2867 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2868 | goto end; |
| 2869 | case ACT_RET_ABRT: |
| 2870 | rule_ret = HTTP_RULE_RES_ABRT; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2871 | s->last_rule_file = rule->conf.file; |
| 2872 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2873 | goto end; |
| 2874 | case ACT_RET_INV: |
| 2875 | rule_ret = HTTP_RULE_RES_BADREQ; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2876 | s->last_rule_file = rule->conf.file; |
| 2877 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2878 | goto end; |
| 2879 | } |
| 2880 | continue; /* eval the next rule */ |
| 2881 | } |
| 2882 | |
| 2883 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2884 | switch (rule->action) { |
| 2885 | case ACT_ACTION_ALLOW: |
| 2886 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2887 | s->last_rule_file = rule->conf.file; |
| 2888 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2889 | goto end; |
| 2890 | |
| 2891 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2892 | txn->status = rule->arg.http_reply->status; |
| 2893 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2894 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2895 | s->last_rule_file = rule->conf.file; |
| 2896 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2897 | goto end; |
| 2898 | |
| 2899 | case ACT_HTTP_REQ_TARPIT: |
| 2900 | txn->flags |= TX_CLTARPIT; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2901 | txn->status = rule->arg.http_reply->status; |
| 2902 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2903 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2904 | s->last_rule_file = rule->conf.file; |
| 2905 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2906 | goto end; |
| 2907 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2908 | case ACT_HTTP_REDIR: { |
| 2909 | int ret = http_apply_redirect_rule(rule->arg.redir, s, txn); |
| 2910 | |
| 2911 | if (ret == 2) // 2 == skip |
| 2912 | break; |
| 2913 | |
| 2914 | rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2915 | s->last_rule_file = rule->conf.file; |
| 2916 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2917 | goto end; |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2918 | } |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2919 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2920 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2921 | default: |
| 2922 | break; |
| 2923 | } |
| 2924 | } |
| 2925 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2926 | if (def_rules && s->current_rule_list == def_rules) { |
| 2927 | s->current_rule_list = rules; |
| 2928 | goto restart; |
| 2929 | } |
| 2930 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2931 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2932 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2933 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2934 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2935 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2936 | /* we reached the end of the rules, nothing to report */ |
| 2937 | return rule_ret; |
| 2938 | } |
| 2939 | |
| 2940 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 2941 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 2942 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 2943 | * is returned, the process can continue the evaluation of next rule list. If |
| 2944 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 2945 | * 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] | 2946 | * must be returned. If *YIELD is returned, the caller must call again the |
| 2947 | * function with the same context. |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2948 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2949 | static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules, |
| 2950 | struct list *rules, struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2951 | { |
| 2952 | struct session *sess = strm_sess(s); |
| 2953 | struct http_txn *txn = s->txn; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2954 | struct act_rule *rule; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2955 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2956 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2957 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2958 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2959 | * resume condition. If a resume is needed it is always in the action |
| 2960 | * and never in the ACL or converters. In this case, we initialise the |
| 2961 | * current rule, and go to the action execution point. |
| 2962 | */ |
| 2963 | if (s->current_rule) { |
| 2964 | rule = s->current_rule; |
| 2965 | s->current_rule = NULL; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2966 | 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] | 2967 | goto resume_execution; |
| 2968 | } |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2969 | s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules); |
| 2970 | |
| 2971 | restart: |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2972 | |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2973 | /* start the ruleset evaluation in strict mode */ |
| 2974 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2975 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2976 | list_for_each_entry(rule, s->current_rule_list, list) { |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2977 | /* check optional condition */ |
| 2978 | if (rule->cond) { |
| 2979 | int ret; |
| 2980 | |
| 2981 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 2982 | ret = acl_pass(ret); |
| 2983 | |
| 2984 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2985 | ret = !ret; |
| 2986 | |
| 2987 | if (!ret) /* condition not matched */ |
| 2988 | continue; |
| 2989 | } |
| 2990 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2991 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2992 | resume_execution: |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 2993 | if (rule->kw->flags & KWF_EXPERIMENTAL) |
| 2994 | mark_tainted(TAINTED_ACTION_EXP_EXECUTED); |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2995 | |
| 2996 | /* Always call the action function if defined */ |
| 2997 | if (rule->action_ptr) { |
| 2998 | if ((s->req.flags & CF_READ_ERROR) || |
| 2999 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 3000 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 3001 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3002 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 3003 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3004 | case ACT_RET_CONT: |
| 3005 | break; |
| 3006 | case ACT_RET_STOP: |
| 3007 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 3008 | s->last_rule_file = rule->conf.file; |
| 3009 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3010 | goto end; |
| 3011 | case ACT_RET_YIELD: |
| 3012 | s->current_rule = rule; |
| 3013 | rule_ret = HTTP_RULE_RES_YIELD; |
| 3014 | goto end; |
| 3015 | case ACT_RET_ERR: |
| 3016 | rule_ret = HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 3017 | s->last_rule_file = rule->conf.file; |
| 3018 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3019 | goto end; |
| 3020 | case ACT_RET_DONE: |
| 3021 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 3022 | s->last_rule_file = rule->conf.file; |
| 3023 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3024 | goto end; |
| 3025 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 3026 | if (txn->status == -1) |
| 3027 | txn->status = 502; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3028 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 3029 | s->last_rule_file = rule->conf.file; |
| 3030 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3031 | goto end; |
| 3032 | case ACT_RET_ABRT: |
| 3033 | rule_ret = HTTP_RULE_RES_ABRT; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 3034 | s->last_rule_file = rule->conf.file; |
| 3035 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3036 | goto end; |
| 3037 | case ACT_RET_INV: |
| 3038 | rule_ret = HTTP_RULE_RES_BADREQ; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 3039 | s->last_rule_file = rule->conf.file; |
| 3040 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3041 | goto end; |
| 3042 | } |
| 3043 | continue; /* eval the next rule */ |
| 3044 | } |
| 3045 | |
| 3046 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3047 | switch (rule->action) { |
| 3048 | case ACT_ACTION_ALLOW: |
| 3049 | rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 3050 | s->last_rule_file = rule->conf.file; |
| 3051 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3052 | goto end; |
| 3053 | |
| 3054 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 3055 | txn->status = rule->arg.http_reply->status; |
| 3056 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 3057 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 3058 | s->last_rule_file = rule->conf.file; |
| 3059 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3060 | goto end; |
| 3061 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 3062 | case ACT_HTTP_REDIR: { |
| 3063 | int ret = http_apply_redirect_rule(rule->arg.redir, s, txn); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3064 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 3065 | if (ret == 2) // 2 == skip |
| 3066 | break; |
| 3067 | |
| 3068 | rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 3069 | s->last_rule_file = rule->conf.file; |
| 3070 | s->last_rule_line = rule->conf.line; |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 3071 | goto end; |
| 3072 | } |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3073 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3074 | default: |
| 3075 | break; |
| 3076 | } |
| 3077 | } |
| 3078 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 3079 | if (def_rules && s->current_rule_list == def_rules) { |
| 3080 | s->current_rule_list = rules; |
| 3081 | goto restart; |
| 3082 | } |
| 3083 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3084 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 3085 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 3086 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 3087 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 3088 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3089 | /* we reached the end of the rules, nothing to report */ |
| 3090 | return rule_ret; |
| 3091 | } |
| 3092 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3093 | /* Executes backend and frontend http-after-response rules for the stream <s>, |
| 3094 | * in that order. it return 1 on success and 0 on error. It is the caller |
| 3095 | * responsibility to catch error or ignore it. If it catches it, this function |
| 3096 | * may be called a second time, for the internal error. |
| 3097 | */ |
| 3098 | int http_eval_after_res_rules(struct stream *s) |
| 3099 | { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 3100 | struct list *def_rules, *rules; |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3101 | struct session *sess = s->sess; |
| 3102 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 3103 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 3104 | /* Eval after-response ruleset only if the reply is not const */ |
| 3105 | if (s->txn->flags & TX_CONST_REPLY) |
| 3106 | goto end; |
| 3107 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3108 | /* prune the request variables if not already done and swap to the response variables. */ |
| 3109 | if (s->vars_reqres.scope != SCOPE_RES) { |
| 3110 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 3111 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | b7bfcb3 | 2021-08-31 08:13:25 +0200 | [diff] [blame] | 3112 | vars_init_head(&s->vars_reqres, SCOPE_RES); |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3113 | } |
| 3114 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 3115 | def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL); |
| 3116 | rules = &s->be->http_after_res_rules; |
| 3117 | |
| 3118 | ret = http_res_get_intercept_rule(s->be, def_rules, rules, s); |
Christopher Faulet | 4c5a591 | 2021-11-09 17:48:39 +0100 | [diff] [blame] | 3119 | 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] | 3120 | def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL); |
| 3121 | rules = &sess->fe->http_after_res_rules; |
| 3122 | ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s); |
| 3123 | } |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3124 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 3125 | end: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3126 | /* All other codes than CONTINUE, STOP or DONE are forbidden */ |
| 3127 | return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE); |
| 3128 | } |
| 3129 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3130 | /* |
| 3131 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 3132 | * desirable to call it only when needed. This code is quite complex because |
| 3133 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 3134 | * highly recommended not to touch this part without a good reason ! |
| 3135 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3136 | 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] | 3137 | { |
| 3138 | struct session *sess = s->sess; |
| 3139 | struct http_txn *txn = s->txn; |
| 3140 | struct htx *htx; |
| 3141 | struct http_hdr_ctx ctx; |
| 3142 | char *hdr_beg, *hdr_end, *del_from; |
| 3143 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
| 3144 | int preserve_hdr; |
| 3145 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3146 | htx = htxbuf(&req->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3147 | ctx.blk = NULL; |
| 3148 | while (http_find_header(htx, ist("Cookie"), &ctx, 1)) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3149 | int is_first = 1; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3150 | del_from = NULL; /* nothing to be deleted */ |
| 3151 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 3152 | |
| 3153 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3154 | * attributes whose name begin with a '$', and associate them with |
| 3155 | * the right cookie, if we want to delete this cookie. |
| 3156 | * So there are 3 cases for each cookie read : |
| 3157 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3158 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3159 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3160 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3161 | * "special" cookie. |
| 3162 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3163 | * remove it. If no application cookie persists in the header, we |
| 3164 | * *MUST* delete it. |
| 3165 | * |
| 3166 | * Note: RFC2965 is unclear about the processing of spaces around |
| 3167 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 3168 | * the RFC explicitly allows spaces before it, and not within the |
| 3169 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 3170 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 3171 | * after the equal sign too, resulting in some (rare) buggy |
| 3172 | * implementations trying to do that. So let's do what servers do. |
| 3173 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 3174 | * allowed quoted strings in values, with any possible character |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3175 | * after a backslash, including control chars and delimiters, which |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3176 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 3177 | * within values even without quotes. |
| 3178 | * |
| 3179 | * We have to keep multiple pointers in order to support cookie |
| 3180 | * removal at the beginning, middle or end of header without |
| 3181 | * corrupting the header. All of these headers are valid : |
| 3182 | * |
| 3183 | * hdr_beg hdr_end |
| 3184 | * | | |
| 3185 | * v | |
| 3186 | * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 | |
| 3187 | * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v |
| 3188 | * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3 |
| 3189 | * | | | | | | | |
| 3190 | * | | | | | | | |
| 3191 | * | | | | | | +--> next |
| 3192 | * | | | | | +----> val_end |
| 3193 | * | | | | +-----------> val_beg |
| 3194 | * | | | +--------------> equal |
| 3195 | * | | +----------------> att_end |
| 3196 | * | +---------------------> att_beg |
| 3197 | * +--------------------------> prev |
| 3198 | * |
| 3199 | */ |
| 3200 | hdr_beg = ctx.value.ptr; |
| 3201 | hdr_end = hdr_beg + ctx.value.len; |
| 3202 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3203 | /* Iterate through all cookies on this line */ |
| 3204 | |
| 3205 | /* find att_beg */ |
| 3206 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3207 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3208 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3209 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3210 | |
| 3211 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3212 | att_beg++; |
| 3213 | |
| 3214 | /* find att_end : this is the first character after the last non |
| 3215 | * space before the equal. It may be equal to hdr_end. |
| 3216 | */ |
| 3217 | equal = att_end = att_beg; |
| 3218 | while (equal < hdr_end) { |
| 3219 | if (*equal == '=' || *equal == ',' || *equal == ';') |
| 3220 | break; |
| 3221 | if (HTTP_IS_SPHT(*equal++)) |
| 3222 | continue; |
| 3223 | att_end = equal; |
| 3224 | } |
| 3225 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3226 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3227 | * is between <att_beg> and <equal>, both may be identical. |
| 3228 | */ |
| 3229 | /* look for end of cookie if there is an equal sign */ |
| 3230 | if (equal < hdr_end && *equal == '=') { |
| 3231 | /* look for the beginning of the value */ |
| 3232 | val_beg = equal + 1; |
| 3233 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3234 | val_beg++; |
| 3235 | |
| 3236 | /* find the end of the value, respecting quotes */ |
| 3237 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3238 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3239 | /* 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] | 3240 | val_end = next; |
| 3241 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3242 | val_end--; |
| 3243 | } |
| 3244 | else |
| 3245 | val_beg = val_end = next = equal; |
| 3246 | |
| 3247 | /* We have nothing to do with attributes beginning with |
| 3248 | * '$'. However, they will automatically be removed if a |
| 3249 | * header before them is removed, since they're supposed |
| 3250 | * to be linked together. |
| 3251 | */ |
| 3252 | if (*att_beg == '$') |
| 3253 | continue; |
| 3254 | |
| 3255 | /* Ignore cookies with no equal sign */ |
| 3256 | if (equal == next) { |
| 3257 | /* This is not our cookie, so we must preserve it. But if we already |
| 3258 | * scheduled another cookie for removal, we cannot remove the |
| 3259 | * complete header, but we can remove the previous block itself. |
| 3260 | */ |
| 3261 | preserve_hdr = 1; |
| 3262 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3263 | 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] | 3264 | val_end += delta; |
| 3265 | next += delta; |
| 3266 | hdr_end += delta; |
| 3267 | prev = del_from; |
| 3268 | del_from = NULL; |
| 3269 | } |
| 3270 | continue; |
| 3271 | } |
| 3272 | |
| 3273 | /* if there are spaces around the equal sign, we need to |
| 3274 | * strip them otherwise we'll get trouble for cookie captures, |
| 3275 | * or even for rewrites. Since this happens extremely rarely, |
| 3276 | * it does not hurt performance. |
| 3277 | */ |
| 3278 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3279 | int stripped_before = 0; |
| 3280 | int stripped_after = 0; |
| 3281 | |
| 3282 | if (att_end != equal) { |
| 3283 | memmove(att_end, equal, hdr_end - equal); |
| 3284 | stripped_before = (att_end - equal); |
| 3285 | equal += stripped_before; |
| 3286 | val_beg += stripped_before; |
| 3287 | } |
| 3288 | |
| 3289 | if (val_beg > equal + 1) { |
| 3290 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3291 | stripped_after = (equal + 1) - val_beg; |
| 3292 | val_beg += stripped_after; |
| 3293 | stripped_before += stripped_after; |
| 3294 | } |
| 3295 | |
| 3296 | val_end += stripped_before; |
| 3297 | next += stripped_before; |
| 3298 | hdr_end += stripped_before; |
| 3299 | } |
| 3300 | /* now everything is as on the diagram above */ |
| 3301 | |
| 3302 | /* First, let's see if we want to capture this cookie. We check |
| 3303 | * that we don't already have a client side cookie, because we |
| 3304 | * can only capture one. Also as an optimisation, we ignore |
| 3305 | * cookies shorter than the declared name. |
| 3306 | */ |
| 3307 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 3308 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3309 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3310 | int log_len = val_end - att_beg; |
| 3311 | |
| 3312 | if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3313 | ha_alert("HTTP logging : out of memory.\n"); |
| 3314 | } else { |
| 3315 | if (log_len > sess->fe->capture_len) |
| 3316 | log_len = sess->fe->capture_len; |
| 3317 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 3318 | txn->cli_cookie[log_len] = 0; |
| 3319 | } |
| 3320 | } |
| 3321 | |
| 3322 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 3323 | * following form : |
| 3324 | * |
| 3325 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
| 3326 | * |
| 3327 | * For cookies in prefix mode, the form is : |
| 3328 | * |
| 3329 | * Cookie: NAME=SRV~VALUE |
| 3330 | */ |
| 3331 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3332 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3333 | struct server *srv = s->be->srv; |
| 3334 | char *delim; |
| 3335 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3336 | /* 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] | 3337 | * have the server ID between val_beg and delim, and the original cookie between |
| 3338 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 3339 | * |
| 3340 | * hdr_beg |
| 3341 | * | |
| 3342 | * v |
| 3343 | * NAME=SRV; # in all but prefix modes |
| 3344 | * NAME=SRV~OPAQUE ; # in prefix mode |
| 3345 | * || || | |+-> next |
| 3346 | * || || | +--> val_end |
| 3347 | * || || +---------> delim |
| 3348 | * || |+------------> val_beg |
| 3349 | * || +-------------> att_end = equal |
| 3350 | * |+-----------------> att_beg |
| 3351 | * +------------------> prev |
| 3352 | * |
| 3353 | */ |
| 3354 | if (s->be->ck_opts & PR_CK_PFX) { |
| 3355 | for (delim = val_beg; delim < val_end; delim++) |
| 3356 | if (*delim == COOKIE_DELIM) |
| 3357 | break; |
| 3358 | } |
| 3359 | else { |
| 3360 | char *vbar1; |
| 3361 | delim = val_end; |
| 3362 | /* Now check if the cookie contains a date field, which would |
| 3363 | * appear after a vertical bar ('|') just after the server name |
| 3364 | * and before the delimiter. |
| 3365 | */ |
| 3366 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 3367 | if (vbar1) { |
| 3368 | /* OK, so left of the bar is the server's cookie and |
| 3369 | * right is the last seen date. It is a base64 encoded |
| 3370 | * 30-bit value representing the UNIX date since the |
| 3371 | * epoch in 4-second quantities. |
| 3372 | */ |
| 3373 | int val; |
| 3374 | delim = vbar1++; |
| 3375 | if (val_end - vbar1 >= 5) { |
| 3376 | val = b64tos30(vbar1); |
| 3377 | if (val > 0) |
| 3378 | txn->cookie_last_date = val << 2; |
| 3379 | } |
| 3380 | /* look for a second vertical bar */ |
| 3381 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 3382 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 3383 | val = b64tos30(vbar1 + 1); |
| 3384 | if (val > 0) |
| 3385 | txn->cookie_first_date = val << 2; |
| 3386 | } |
| 3387 | } |
| 3388 | } |
| 3389 | |
| 3390 | /* if the cookie has an expiration date and the proxy wants to check |
| 3391 | * it, then we do that now. We first check if the cookie is too old, |
| 3392 | * then only if it has expired. We detect strict overflow because the |
| 3393 | * time resolution here is not great (4 seconds). Cookies with dates |
| 3394 | * in the future are ignored if their offset is beyond one day. This |
| 3395 | * allows an admin to fix timezone issues without expiring everyone |
| 3396 | * and at the same time avoids keeping unwanted side effects for too |
| 3397 | * long. |
| 3398 | */ |
| 3399 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 3400 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) || |
| 3401 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
| 3402 | txn->flags &= ~TX_CK_MASK; |
| 3403 | txn->flags |= TX_CK_OLD; |
| 3404 | delim = val_beg; // let's pretend we have not found the cookie |
| 3405 | txn->cookie_first_date = 0; |
| 3406 | txn->cookie_last_date = 0; |
| 3407 | } |
| 3408 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 3409 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) || |
| 3410 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
| 3411 | txn->flags &= ~TX_CK_MASK; |
| 3412 | txn->flags |= TX_CK_EXPIRED; |
| 3413 | delim = val_beg; // let's pretend we have not found the cookie |
| 3414 | txn->cookie_first_date = 0; |
| 3415 | txn->cookie_last_date = 0; |
| 3416 | } |
| 3417 | |
| 3418 | /* Here, we'll look for the first running server which supports the cookie. |
| 3419 | * This allows to share a same cookie between several servers, for example |
| 3420 | * to dedicate backup servers to specific servers only. |
| 3421 | * However, to prevent clients from sticking to cookie-less backup server |
| 3422 | * when they have incidentely learned an empty cookie, we simply ignore |
| 3423 | * empty cookies and mark them as invalid. |
| 3424 | * The same behaviour is applied when persistence must be ignored. |
| 3425 | */ |
| 3426 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3427 | srv = NULL; |
| 3428 | |
| 3429 | while (srv) { |
| 3430 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 3431 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 3432 | if ((srv->cur_state != SRV_ST_STOPPED) || |
| 3433 | (s->be->options & PR_O_PERSIST) || |
| 3434 | (s->flags & SF_FORCE_PRST)) { |
| 3435 | /* we found the server and we can use it */ |
| 3436 | txn->flags &= ~TX_CK_MASK; |
| 3437 | txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN; |
| 3438 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
| 3439 | s->target = &srv->obj_type; |
| 3440 | break; |
| 3441 | } else { |
| 3442 | /* we found a server, but it's down, |
| 3443 | * mark it as such and go on in case |
| 3444 | * another one is available. |
| 3445 | */ |
| 3446 | txn->flags &= ~TX_CK_MASK; |
| 3447 | txn->flags |= TX_CK_DOWN; |
| 3448 | } |
| 3449 | } |
| 3450 | srv = srv->next; |
| 3451 | } |
| 3452 | |
| 3453 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
| 3454 | /* no server matched this cookie or we deliberately skipped it */ |
| 3455 | txn->flags &= ~TX_CK_MASK; |
| 3456 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3457 | txn->flags |= TX_CK_UNUSED; |
| 3458 | else |
| 3459 | txn->flags |= TX_CK_INVALID; |
| 3460 | } |
| 3461 | |
| 3462 | /* depending on the cookie mode, we may have to either : |
| 3463 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3464 | * the server never sees it ; |
| 3465 | * - remove the server id from the cookie value, and tag the cookie as an |
Joseph Herlant | e9d5c72 | 2018-11-25 11:00:25 -0800 | [diff] [blame] | 3466 | * application cookie so that it does not get accidentally removed later, |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3467 | * if we're in cookie prefix mode |
| 3468 | */ |
| 3469 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
| 3470 | int delta; /* negative */ |
| 3471 | |
| 3472 | memmove(val_beg, delim + 1, hdr_end - (delim + 1)); |
| 3473 | delta = val_beg - (delim + 1); |
| 3474 | val_end += delta; |
| 3475 | next += delta; |
| 3476 | hdr_end += delta; |
| 3477 | del_from = NULL; |
| 3478 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 3479 | } |
| 3480 | else if (del_from == NULL && |
| 3481 | (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) { |
| 3482 | del_from = prev; |
| 3483 | } |
| 3484 | } |
| 3485 | else { |
| 3486 | /* This is not our cookie, so we must preserve it. But if we already |
| 3487 | * scheduled another cookie for removal, we cannot remove the |
| 3488 | * complete header, but we can remove the previous block itself. |
| 3489 | */ |
| 3490 | preserve_hdr = 1; |
| 3491 | |
| 3492 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3493 | 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] | 3494 | if (att_beg >= del_from) |
| 3495 | att_beg += delta; |
| 3496 | if (att_end >= del_from) |
| 3497 | att_end += delta; |
| 3498 | val_beg += delta; |
| 3499 | val_end += delta; |
| 3500 | next += delta; |
| 3501 | hdr_end += delta; |
| 3502 | prev = del_from; |
| 3503 | del_from = NULL; |
| 3504 | } |
| 3505 | } |
| 3506 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3507 | } /* for each cookie */ |
| 3508 | |
| 3509 | |
| 3510 | /* There are no more cookies on this line. |
| 3511 | * We may still have one (or several) marked for deletion at the |
| 3512 | * end of the line. We must do this now in two ways : |
| 3513 | * - if some cookies must be preserved, we only delete from the |
| 3514 | * mark to the end of line ; |
| 3515 | * - if nothing needs to be preserved, simply delete the whole header |
| 3516 | */ |
| 3517 | if (del_from) { |
| 3518 | hdr_end = (preserve_hdr ? del_from : hdr_beg); |
| 3519 | } |
| 3520 | if ((hdr_end - hdr_beg) != ctx.value.len) { |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3521 | if (hdr_beg != hdr_end) |
| 3522 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3523 | else |
| 3524 | http_remove_header(htx, &ctx); |
| 3525 | } |
| 3526 | } /* for each "Cookie header */ |
| 3527 | } |
| 3528 | |
| 3529 | /* |
| 3530 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 3531 | * desirable to call it only when needed. This function is also used when we |
| 3532 | * just need to know if there is a cookie (eg: for check-cache). |
| 3533 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3534 | 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] | 3535 | { |
| 3536 | struct session *sess = s->sess; |
| 3537 | struct http_txn *txn = s->txn; |
| 3538 | struct htx *htx; |
| 3539 | struct http_hdr_ctx ctx; |
| 3540 | struct server *srv; |
| 3541 | char *hdr_beg, *hdr_end; |
| 3542 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 6f7a02a | 2019-04-15 21:49:49 +0200 | [diff] [blame] | 3543 | int is_cookie2 = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3544 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3545 | htx = htxbuf(&res->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3546 | |
| 3547 | ctx.blk = NULL; |
| 3548 | while (1) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3549 | int is_first = 1; |
| 3550 | |
Andrew McDermott | bfb15ab | 2022-02-11 18:26:49 +0000 | [diff] [blame] | 3551 | if (is_cookie2 || !http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) { |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3552 | if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1)) |
| 3553 | break; |
| 3554 | is_cookie2 = 1; |
| 3555 | } |
| 3556 | |
| 3557 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 3558 | * <prev> points to the colon. |
| 3559 | */ |
| 3560 | txn->flags |= TX_SCK_PRESENT; |
| 3561 | |
| 3562 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 3563 | * check-cache is enabled) and we are not interested in checking |
| 3564 | * them. Warning, the cookie capture is declared in the frontend. |
| 3565 | */ |
| 3566 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
| 3567 | break; |
| 3568 | |
| 3569 | /* OK so now we know we have to process this response cookie. |
| 3570 | * The format of the Set-Cookie header is slightly different |
| 3571 | * from the format of the Cookie header in that it does not |
| 3572 | * support the comma as a cookie delimiter (thus the header |
| 3573 | * cannot be folded) because the Expires attribute described in |
| 3574 | * the original Netscape's spec may contain an unquoted date |
| 3575 | * with a comma inside. We have to live with this because |
| 3576 | * many browsers don't support Max-Age and some browsers don't |
| 3577 | * support quoted strings. However the Set-Cookie2 header is |
| 3578 | * clean. |
| 3579 | * |
| 3580 | * We have to keep multiple pointers in order to support cookie |
| 3581 | * removal at the beginning, middle or end of header without |
| 3582 | * corrupting the header (in case of set-cookie2). A special |
| 3583 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 3584 | * fields after the first semi-colon. The <next> pointer points |
| 3585 | * either to the end of line (set-cookie) or next unquoted comma |
| 3586 | * (set-cookie2). All of these headers are valid : |
| 3587 | * |
| 3588 | * hdr_beg hdr_end |
| 3589 | * | | |
| 3590 | * v | |
| 3591 | * NAME1 = VALUE 1 ; Secure; Path="/" | |
| 3592 | * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v |
| 3593 | * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT |
| 3594 | * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard |
| 3595 | * | | | | | | | | |
| 3596 | * | | | | | | | +-> next |
| 3597 | * | | | | | | +------------> scav |
| 3598 | * | | | | | +--------------> val_end |
| 3599 | * | | | | +--------------------> val_beg |
| 3600 | * | | | +----------------------> equal |
| 3601 | * | | +------------------------> att_end |
| 3602 | * | +----------------------------> att_beg |
| 3603 | * +------------------------------> prev |
| 3604 | * -------------------------------> hdr_beg |
| 3605 | */ |
| 3606 | hdr_beg = ctx.value.ptr; |
| 3607 | hdr_end = hdr_beg + ctx.value.len; |
| 3608 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3609 | |
| 3610 | /* Iterate through all cookies on this line */ |
| 3611 | |
| 3612 | /* find att_beg */ |
| 3613 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3614 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3615 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3616 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3617 | |
| 3618 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3619 | att_beg++; |
| 3620 | |
| 3621 | /* find att_end : this is the first character after the last non |
| 3622 | * space before the equal. It may be equal to hdr_end. |
| 3623 | */ |
| 3624 | equal = att_end = att_beg; |
| 3625 | |
| 3626 | while (equal < hdr_end) { |
| 3627 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 3628 | break; |
| 3629 | if (HTTP_IS_SPHT(*equal++)) |
| 3630 | continue; |
| 3631 | att_end = equal; |
| 3632 | } |
| 3633 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3634 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3635 | * is between <att_beg> and <equal>, both may be identical. |
| 3636 | */ |
| 3637 | |
| 3638 | /* look for end of cookie if there is an equal sign */ |
| 3639 | if (equal < hdr_end && *equal == '=') { |
| 3640 | /* look for the beginning of the value */ |
| 3641 | val_beg = equal + 1; |
| 3642 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3643 | val_beg++; |
| 3644 | |
| 3645 | /* find the end of the value, respecting quotes */ |
| 3646 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3647 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3648 | /* 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] | 3649 | val_end = next; |
| 3650 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3651 | val_end--; |
| 3652 | } |
| 3653 | else { |
| 3654 | /* <equal> points to next comma, semi-colon or EOL */ |
| 3655 | val_beg = val_end = next = equal; |
| 3656 | } |
| 3657 | |
| 3658 | if (next < hdr_end) { |
| 3659 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 3660 | * a colon or semi-colon before the end. So skip all attr-value |
| 3661 | * pairs and look for the next comma. For Set-Cookie, since |
| 3662 | * commas are permitted in values, skip to the end. |
| 3663 | */ |
| 3664 | if (is_cookie2) |
| 3665 | next = http_find_hdr_value_end(next, hdr_end); |
| 3666 | else |
| 3667 | next = hdr_end; |
| 3668 | } |
| 3669 | |
| 3670 | /* Now everything is as on the diagram above */ |
| 3671 | |
| 3672 | /* Ignore cookies with no equal sign */ |
| 3673 | if (equal == val_end) |
| 3674 | continue; |
| 3675 | |
| 3676 | /* If there are spaces around the equal sign, we need to |
| 3677 | * strip them otherwise we'll get trouble for cookie captures, |
| 3678 | * or even for rewrites. Since this happens extremely rarely, |
| 3679 | * it does not hurt performance. |
| 3680 | */ |
| 3681 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3682 | int stripped_before = 0; |
| 3683 | int stripped_after = 0; |
| 3684 | |
| 3685 | if (att_end != equal) { |
| 3686 | memmove(att_end, equal, hdr_end - equal); |
| 3687 | stripped_before = (att_end - equal); |
| 3688 | equal += stripped_before; |
| 3689 | val_beg += stripped_before; |
| 3690 | } |
| 3691 | |
| 3692 | if (val_beg > equal + 1) { |
| 3693 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3694 | stripped_after = (equal + 1) - val_beg; |
| 3695 | val_beg += stripped_after; |
| 3696 | stripped_before += stripped_after; |
| 3697 | } |
| 3698 | |
| 3699 | val_end += stripped_before; |
| 3700 | next += stripped_before; |
| 3701 | hdr_end += stripped_before; |
| 3702 | |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3703 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3704 | ctx.value.len = hdr_end - hdr_beg; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | /* First, let's see if we want to capture this cookie. We check |
| 3708 | * that we don't already have a server side cookie, because we |
| 3709 | * can only capture one. Also as an optimisation, we ignore |
| 3710 | * cookies shorter than the declared name. |
| 3711 | */ |
| 3712 | if (sess->fe->capture_name != NULL && |
| 3713 | txn->srv_cookie == NULL && |
| 3714 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3715 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3716 | int log_len = val_end - att_beg; |
| 3717 | if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3718 | ha_alert("HTTP logging : out of memory.\n"); |
| 3719 | } |
| 3720 | else { |
| 3721 | if (log_len > sess->fe->capture_len) |
| 3722 | log_len = sess->fe->capture_len; |
| 3723 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 3724 | txn->srv_cookie[log_len] = 0; |
| 3725 | } |
| 3726 | } |
| 3727 | |
| 3728 | srv = objt_server(s->target); |
| 3729 | /* now check if we need to process it for persistence */ |
| 3730 | if (!(s->flags & SF_IGNORE_PRST) && |
| 3731 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3732 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3733 | /* assume passive cookie by default */ |
| 3734 | txn->flags &= ~TX_SCK_MASK; |
| 3735 | txn->flags |= TX_SCK_FOUND; |
| 3736 | |
| 3737 | /* If the cookie is in insert mode on a known server, we'll delete |
| 3738 | * this occurrence because we'll insert another one later. |
| 3739 | * We'll delete it too if the "indirect" option is set and we're in |
| 3740 | * a direct access. |
| 3741 | */ |
| 3742 | if (s->be->ck_opts & PR_CK_PSV) { |
| 3743 | /* The "preserve" flag was set, we don't want to touch the |
| 3744 | * server's cookie. |
| 3745 | */ |
| 3746 | } |
| 3747 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
| 3748 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
| 3749 | /* this cookie must be deleted */ |
| 3750 | if (prev == hdr_beg && next == hdr_end) { |
| 3751 | /* whole header */ |
| 3752 | http_remove_header(htx, &ctx); |
| 3753 | /* note: while both invalid now, <next> and <hdr_end> |
| 3754 | * are still equal, so the for() will stop as expected. |
| 3755 | */ |
| 3756 | } else { |
| 3757 | /* just remove the value */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3758 | int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3759 | next = prev; |
| 3760 | hdr_end += delta; |
| 3761 | } |
| 3762 | txn->flags &= ~TX_SCK_MASK; |
| 3763 | txn->flags |= TX_SCK_DELETED; |
| 3764 | /* and go on with next cookie */ |
| 3765 | } |
| 3766 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
| 3767 | /* replace bytes val_beg->val_end with the cookie name associated |
| 3768 | * with this server since we know it. |
| 3769 | */ |
| 3770 | int sliding, delta; |
| 3771 | |
| 3772 | ctx.value = ist2(val_beg, val_end - val_beg); |
| 3773 | ctx.lws_before = ctx.lws_after = 0; |
| 3774 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen)); |
| 3775 | delta = srv->cklen - (val_end - val_beg); |
| 3776 | sliding = (ctx.value.ptr - val_beg); |
| 3777 | hdr_beg += sliding; |
| 3778 | val_beg += sliding; |
| 3779 | next += sliding + delta; |
| 3780 | hdr_end += sliding + delta; |
| 3781 | |
| 3782 | txn->flags &= ~TX_SCK_MASK; |
| 3783 | txn->flags |= TX_SCK_REPLACED; |
| 3784 | } |
| 3785 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
| 3786 | /* insert the cookie name associated with this server |
| 3787 | * before existing cookie, and insert a delimiter between them.. |
| 3788 | */ |
| 3789 | int sliding, delta; |
| 3790 | ctx.value = ist2(val_beg, 0); |
| 3791 | ctx.lws_before = ctx.lws_after = 0; |
| 3792 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1)); |
| 3793 | delta = srv->cklen + 1; |
| 3794 | sliding = (ctx.value.ptr - val_beg); |
| 3795 | hdr_beg += sliding; |
| 3796 | val_beg += sliding; |
| 3797 | next += sliding + delta; |
| 3798 | hdr_end += sliding + delta; |
| 3799 | |
| 3800 | val_beg[srv->cklen] = COOKIE_DELIM; |
| 3801 | txn->flags &= ~TX_SCK_MASK; |
| 3802 | txn->flags |= TX_SCK_REPLACED; |
| 3803 | } |
| 3804 | } |
| 3805 | /* that's done for this cookie, check the next one on the same |
| 3806 | * line when next != hdr_end (only if is_cookie2). |
| 3807 | */ |
| 3808 | } |
| 3809 | } |
| 3810 | } |
| 3811 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3812 | /* |
| 3813 | * Parses the Cache-Control and Pragma request header fields to determine if |
| 3814 | * the request may be served from the cache and/or if it is cacheable. Updates |
| 3815 | * s->txn->flags. |
| 3816 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3817 | void http_check_request_for_cacheability(struct stream *s, struct channel *req) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3818 | { |
| 3819 | struct http_txn *txn = s->txn; |
| 3820 | struct htx *htx; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3821 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 3822 | int pragma_found, cc_found; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3823 | |
| 3824 | if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE) |
| 3825 | return; /* nothing more to do here */ |
| 3826 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3827 | htx = htxbuf(&req->buf); |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3828 | pragma_found = cc_found = 0; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3829 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3830 | /* Check "pragma" header for HTTP/1.0 compatibility. */ |
| 3831 | if (http_find_header(htx, ist("pragma"), &ctx, 1)) { |
| 3832 | if (isteqi(ctx.value, ist("no-cache"))) { |
| 3833 | pragma_found = 1; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3834 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3835 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3836 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3837 | ctx.blk = NULL; |
| 3838 | /* Don't use the cache and don't try to store if we found the |
| 3839 | * Authorization header */ |
| 3840 | if (http_find_header(htx, ist("authorization"), &ctx, 1)) { |
| 3841 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3842 | txn->flags |= TX_CACHE_IGNORE; |
| 3843 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3844 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3845 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3846 | /* Look for "cache-control" header and iterate over all the values |
| 3847 | * until we find one that specifies that caching is possible or not. */ |
| 3848 | ctx.blk = NULL; |
| 3849 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3850 | cc_found = 1; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3851 | /* We don't check the values after max-age, max-stale nor min-fresh, |
| 3852 | * we simply don't use the cache when they're specified. */ |
| 3853 | if (istmatchi(ctx.value, ist("max-age")) || |
| 3854 | istmatchi(ctx.value, ist("no-cache")) || |
| 3855 | istmatchi(ctx.value, ist("max-stale")) || |
| 3856 | istmatchi(ctx.value, ist("min-fresh"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3857 | txn->flags |= TX_CACHE_IGNORE; |
| 3858 | continue; |
| 3859 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3860 | if (istmatchi(ctx.value, ist("no-store"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3861 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3862 | continue; |
| 3863 | } |
| 3864 | } |
| 3865 | |
| 3866 | /* RFC7234#5.4: |
| 3867 | * When the Cache-Control header field is also present and |
| 3868 | * understood in a request, Pragma is ignored. |
| 3869 | * When the Cache-Control header field is not present in a |
| 3870 | * request, caches MUST consider the no-cache request |
| 3871 | * pragma-directive as having the same effect as if |
| 3872 | * "Cache-Control: no-cache" were present. |
| 3873 | */ |
| 3874 | if (!cc_found && pragma_found) |
| 3875 | txn->flags |= TX_CACHE_IGNORE; |
| 3876 | } |
| 3877 | |
| 3878 | /* |
| 3879 | * Check if response is cacheable or not. Updates s->txn->flags. |
| 3880 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3881 | void http_check_response_for_cacheability(struct stream *s, struct channel *res) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3882 | { |
| 3883 | struct http_txn *txn = s->txn; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3884 | struct http_hdr_ctx ctx = { .blk = NULL }; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3885 | struct htx *htx; |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3886 | int has_freshness_info = 0; |
| 3887 | int has_validator = 0; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3888 | |
| 3889 | if (txn->status < 200) { |
| 3890 | /* do not try to cache interim responses! */ |
| 3891 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3892 | return; |
| 3893 | } |
| 3894 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3895 | htx = htxbuf(&res->buf); |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3896 | /* Check "pragma" header for HTTP/1.0 compatibility. */ |
| 3897 | if (http_find_header(htx, ist("pragma"), &ctx, 1)) { |
| 3898 | if (isteqi(ctx.value, ist("no-cache"))) { |
| 3899 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3900 | return; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3901 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3902 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3903 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3904 | /* Look for "cache-control" header and iterate over all the values |
| 3905 | * until we find one that specifies that caching is possible or not. */ |
| 3906 | ctx.blk = NULL; |
| 3907 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
| 3908 | if (isteqi(ctx.value, ist("public"))) { |
| 3909 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3910 | continue; |
| 3911 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3912 | if (isteqi(ctx.value, ist("private")) || |
| 3913 | isteqi(ctx.value, ist("no-cache")) || |
| 3914 | isteqi(ctx.value, ist("no-store")) || |
| 3915 | isteqi(ctx.value, ist("max-age=0")) || |
| 3916 | isteqi(ctx.value, ist("s-maxage=0"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3917 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3918 | continue; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3919 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3920 | /* We might have a no-cache="set-cookie" form. */ |
| 3921 | if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) { |
| 3922 | txn->flags &= ~TX_CACHE_COOK; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3923 | continue; |
| 3924 | } |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3925 | |
| 3926 | if (istmatchi(ctx.value, ist("s-maxage")) || |
| 3927 | istmatchi(ctx.value, ist("max-age"))) { |
| 3928 | has_freshness_info = 1; |
| 3929 | continue; |
| 3930 | } |
| 3931 | } |
| 3932 | |
| 3933 | /* If no freshness information could be found in Cache-Control values, |
| 3934 | * look for an Expires header. */ |
| 3935 | if (!has_freshness_info) { |
| 3936 | ctx.blk = NULL; |
| 3937 | has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0); |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3938 | } |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3939 | |
| 3940 | /* If no freshness information could be found in Cache-Control or Expires |
| 3941 | * values, look for an explicit validator. */ |
| 3942 | if (!has_freshness_info) { |
| 3943 | ctx.blk = NULL; |
| 3944 | has_validator = 1; |
| 3945 | if (!http_find_header(htx, ist("etag"), &ctx, 0)) { |
| 3946 | ctx.blk = NULL; |
| 3947 | if (!http_find_header(htx, ist("last-modified"), &ctx, 0)) |
| 3948 | has_validator = 0; |
| 3949 | } |
| 3950 | } |
| 3951 | |
| 3952 | /* We won't store an entry that has neither a cache validator nor an |
| 3953 | * explicit expiration time, as suggested in RFC 7234#3. */ |
| 3954 | if (!has_freshness_info && !has_validator) |
| 3955 | txn->flags |= TX_CACHE_IGNORE; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3956 | } |
| 3957 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3958 | /* |
| 3959 | * In a GET, HEAD or POST request, check if the requested URI matches the stats uri |
| 3960 | * for the current backend. |
| 3961 | * |
| 3962 | * It is assumed that the request is either a HEAD, GET, or POST and that the |
| 3963 | * uri_auth field is valid. |
| 3964 | * |
| 3965 | * Returns 1 if stats should be provided, otherwise 0. |
| 3966 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3967 | 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] | 3968 | { |
| 3969 | struct uri_auth *uri_auth = backend->uri_auth; |
| 3970 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3971 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3972 | struct ist uri; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3973 | |
| 3974 | if (!uri_auth) |
| 3975 | return 0; |
| 3976 | |
| 3977 | if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST) |
| 3978 | return 0; |
| 3979 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3980 | htx = htxbuf(&s->req.buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 3981 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3982 | uri = htx_sl_req_uri(sl); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 3983 | if (*uri_auth->uri_prefix == '/') { |
| 3984 | struct http_uri_parser parser = http_uri_parser_init(uri); |
| 3985 | uri = http_parse_path(&parser); |
| 3986 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3987 | |
| 3988 | /* check URI size */ |
| 3989 | if (uri_auth->uri_len > uri.len) |
| 3990 | return 0; |
| 3991 | |
| 3992 | if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
| 3993 | return 0; |
| 3994 | |
| 3995 | return 1; |
| 3996 | } |
| 3997 | |
| 3998 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3999 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 4000 | * and program a response message if something was unexpected. It cannot fail |
| 4001 | * and always relies on the stats applet to complete the job. It does not touch |
| 4002 | * analysers nor counters, which are left to the caller. It does not touch |
| 4003 | * s->target which is supposed to already point to the stats applet. The caller |
| 4004 | * is expected to have already assigned an appctx to the stream. |
| 4005 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4006 | static int http_handle_stats(struct stream *s, struct channel *req) |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4007 | { |
| 4008 | struct stats_admin_rule *stats_admin_rule; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4009 | struct session *sess = s->sess; |
| 4010 | struct http_txn *txn = s->txn; |
| 4011 | struct http_msg *msg = &txn->req; |
| 4012 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 4013 | const char *h, *lookup, *end; |
Willy Tarreau | 8e7c6e6 | 2022-05-18 17:58:02 +0200 | [diff] [blame] | 4014 | struct appctx *appctx = __sc_appctx(s->scb); |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4015 | struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4016 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4017 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4018 | |
Willy Tarreau | 41f8852 | 2022-05-03 18:39:27 +0200 | [diff] [blame] | 4019 | appctx->st1 = 0; |
Willy Tarreau | 6ef1648 | 2022-05-06 18:07:53 +0200 | [diff] [blame] | 4020 | ctx->state = STAT_STATE_INIT; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4021 | ctx->st_code = STAT_STATUS_INIT; |
| 4022 | ctx->flags |= uri_auth->flags; |
| 4023 | ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4024 | if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD)) |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4025 | ctx->flags |= STAT_CHUNKED; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4026 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4027 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 4028 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4029 | lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len; |
| 4030 | end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4031 | |
| 4032 | for (h = lookup; h <= end - 3; h++) { |
| 4033 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4034 | ctx->flags |= STAT_HIDE_DOWN; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4035 | break; |
| 4036 | } |
Amaury Denoyelle | 91e55ea | 2021-02-25 14:46:08 +0100 | [diff] [blame] | 4037 | } |
| 4038 | |
| 4039 | for (h = lookup; h <= end - 9; h++) { |
| 4040 | if (memcmp(h, ";no-maint", 9) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4041 | ctx->flags |= STAT_HIDE_MAINT; |
Willy Tarreau | 3e32036 | 2020-10-23 17:28:57 +0200 | [diff] [blame] | 4042 | break; |
| 4043 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4044 | } |
| 4045 | |
| 4046 | if (uri_auth->refresh) { |
| 4047 | for (h = lookup; h <= end - 10; h++) { |
| 4048 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4049 | ctx->flags |= STAT_NO_REFRESH; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4050 | break; |
| 4051 | } |
| 4052 | } |
| 4053 | } |
| 4054 | |
| 4055 | for (h = lookup; h <= end - 4; h++) { |
| 4056 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4057 | ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4058 | break; |
| 4059 | } |
| 4060 | } |
| 4061 | |
| 4062 | for (h = lookup; h <= end - 6; h++) { |
| 4063 | if (memcmp(h, ";typed", 6) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4064 | ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
| 4065 | ctx->flags |= STAT_FMT_TYPED; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4066 | break; |
| 4067 | } |
| 4068 | } |
| 4069 | |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 4070 | for (h = lookup; h <= end - 5; h++) { |
| 4071 | if (memcmp(h, ";json", 5) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4072 | ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
| 4073 | ctx->flags |= STAT_FMT_JSON; |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 4074 | break; |
| 4075 | } |
| 4076 | } |
| 4077 | |
| 4078 | for (h = lookup; h <= end - 12; h++) { |
| 4079 | if (memcmp(h, ";json-schema", 12) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4080 | ctx->flags &= ~STAT_FMT_MASK; |
| 4081 | ctx->flags |= STAT_JSON_SCHM; |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 4082 | break; |
| 4083 | } |
| 4084 | } |
| 4085 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4086 | for (h = lookup; h <= end - 8; h++) { |
| 4087 | if (memcmp(h, ";st=", 4) == 0) { |
| 4088 | int i; |
| 4089 | h += 4; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4090 | ctx->st_code = STAT_STATUS_UNKN; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4091 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 4092 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4093 | ctx->st_code = i; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4094 | break; |
| 4095 | } |
| 4096 | } |
| 4097 | break; |
| 4098 | } |
| 4099 | } |
| 4100 | |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4101 | ctx->scope_str = 0; |
| 4102 | ctx->scope_len = 0; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4103 | for (h = lookup; h <= end - 8; h++) { |
| 4104 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 4105 | int itx = 0; |
| 4106 | const char *h2; |
| 4107 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 4108 | const char *err; |
| 4109 | |
| 4110 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 4111 | h2 = h; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4112 | ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl); |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 4113 | while (h < end) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4114 | if (*h == ';' || *h == '&' || *h == ' ') |
| 4115 | break; |
| 4116 | itx++; |
| 4117 | h++; |
| 4118 | } |
| 4119 | |
| 4120 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 4121 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4122 | ctx->scope_len = itx; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4123 | |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4124 | /* 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] | 4125 | memcpy(scope_txt, h2, itx); |
| 4126 | scope_txt[itx] = '\0'; |
| 4127 | err = invalid_char(scope_txt); |
| 4128 | if (err) { |
| 4129 | /* bad char in search text => clear scope */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4130 | ctx->scope_str = 0; |
| 4131 | ctx->scope_len = 0; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4132 | } |
| 4133 | break; |
| 4134 | } |
| 4135 | } |
| 4136 | |
| 4137 | /* now check whether we have some admin rules for this request */ |
| 4138 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
| 4139 | int ret = 1; |
| 4140 | |
| 4141 | if (stats_admin_rule->cond) { |
| 4142 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 4143 | ret = acl_pass(ret); |
| 4144 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 4145 | ret = !ret; |
| 4146 | } |
| 4147 | |
| 4148 | if (ret) { |
| 4149 | /* no rule, or the rule matches */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4150 | ctx->flags |= STAT_ADMIN; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4151 | break; |
| 4152 | } |
| 4153 | } |
| 4154 | |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4155 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 4156 | appctx->st0 = STAT_HTTP_HEAD; |
| 4157 | else if (txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4158 | if (ctx->flags & STAT_ADMIN) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4159 | appctx->st0 = STAT_HTTP_POST; |
Christopher Faulet | bd9e842 | 2019-08-15 22:26:48 +0200 | [diff] [blame] | 4160 | if (msg->msg_state < HTTP_MSG_DATA) |
| 4161 | req->analysers |= AN_REQ_HTTP_BODY; |
| 4162 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4163 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4164 | /* POST without admin level */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4165 | ctx->flags &= ~STAT_CHUNKED; |
| 4166 | ctx->st_code = STAT_STATUS_DENY; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4167 | appctx->st0 = STAT_HTTP_LAST; |
| 4168 | } |
| 4169 | } |
| 4170 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4171 | /* Unsupported method */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4172 | ctx->flags &= ~STAT_CHUNKED; |
| 4173 | ctx->st_code = STAT_STATUS_IVAL; |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4174 | appctx->st0 = STAT_HTTP_LAST; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4175 | } |
| 4176 | |
| 4177 | s->task->nice = -32; /* small boost for HTTP statistics */ |
| 4178 | return 1; |
| 4179 | } |
| 4180 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4181 | /* This function waits for the message payload at most <time> milliseconds (may |
| 4182 | * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the |
| 4183 | * payload are received (0 means no limit). It returns HTTP_RULE_* depending on |
| 4184 | * the result: |
| 4185 | * |
| 4186 | * - HTTP_RULE_RES_CONT when conditions are met to stop waiting |
| 4187 | * - HTTP_RULE_RES_YIELD to wait for more data |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 4188 | * - HTTP_RULE_RES_ABRT when a timeout occurred. |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4189 | * - 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] | 4190 | * - HTTP_RULE_RES_ERROR if an internal error occurred |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4191 | * |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 4192 | * 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] | 4193 | * to the client, depending on the channel (408 on request side, 504 on response |
| 4194 | * side). All other errors must be handled by the caller. |
| 4195 | */ |
| 4196 | enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn, |
| 4197 | unsigned int time, unsigned int bytes) |
| 4198 | { |
| 4199 | struct session *sess = s->sess; |
| 4200 | struct http_txn *txn = s->txn; |
| 4201 | struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req); |
| 4202 | struct htx *htx; |
| 4203 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 4204 | |
| 4205 | htx = htxbuf(&chn->buf); |
| 4206 | |
| 4207 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
| 4208 | ret = HTTP_RULE_RES_BADREQ; |
| 4209 | goto end; |
| 4210 | } |
| 4211 | if (htx->flags & HTX_FL_PROCESSING_ERROR) { |
| 4212 | ret = HTTP_RULE_RES_ERROR; |
| 4213 | goto end; |
| 4214 | } |
| 4215 | |
| 4216 | /* Do nothing for bodyless and CONNECT requests */ |
| 4217 | if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS)) |
| 4218 | goto end; |
| 4219 | |
| 4220 | if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) { |
| 4221 | if (http_handle_expect_hdr(s, htx, msg) == -1) { |
| 4222 | ret = HTTP_RULE_RES_ERROR; |
| 4223 | goto end; |
| 4224 | } |
| 4225 | } |
| 4226 | |
| 4227 | msg->msg_state = HTTP_MSG_DATA; |
| 4228 | |
| 4229 | /* Now we're in HTTP_MSG_DATA. We just need to know if all data have |
| 4230 | * been received or if the buffer is full. |
| 4231 | */ |
Christopher Faulet | 7833596 | 2021-09-23 14:46:32 +0200 | [diff] [blame] | 4232 | if ((htx->flags & HTX_FL_EOM) || |
| 4233 | htx_get_tail_type(htx) > HTX_BLK_DATA || |
| 4234 | channel_htx_full(chn, htx, global.tune.maxrewrite) || |
Willy Tarreau | 99615ed | 2022-05-25 07:29:36 +0200 | [diff] [blame] | 4235 | sc_waiting_room(chn_prod(chn))) |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4236 | goto end; |
| 4237 | |
| 4238 | if (bytes) { |
| 4239 | struct htx_blk *blk; |
| 4240 | unsigned int len = 0; |
| 4241 | |
| 4242 | for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 4243 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 4244 | continue; |
| 4245 | len += htx_get_blksz(blk); |
| 4246 | if (len >= bytes) |
| 4247 | goto end; |
| 4248 | } |
| 4249 | } |
| 4250 | |
| 4251 | if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) { |
| 4252 | if (!(chn->flags & CF_ISRESP)) |
| 4253 | goto abort_req; |
| 4254 | goto abort_res; |
| 4255 | } |
| 4256 | |
| 4257 | /* we get here if we need to wait for more data */ |
| 4258 | if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) { |
| 4259 | if (!tick_isset(chn->analyse_exp)) |
| 4260 | chn->analyse_exp = tick_add_ifset(now_ms, time); |
| 4261 | ret = HTTP_RULE_RES_YIELD; |
| 4262 | } |
| 4263 | |
| 4264 | end: |
| 4265 | return ret; |
| 4266 | |
| 4267 | abort_req: |
| 4268 | txn->status = 408; |
| 4269 | if (!(s->flags & SF_ERR_MASK)) |
| 4270 | s->flags |= SF_ERR_CLITO; |
| 4271 | if (!(s->flags & SF_FINST_MASK)) |
| 4272 | s->flags |= SF_FINST_D; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4273 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4274 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4275 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4276 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 4277 | ret = HTTP_RULE_RES_ABRT; |
| 4278 | goto end; |
| 4279 | |
| 4280 | abort_res: |
| 4281 | txn->status = 504; |
| 4282 | if (!(s->flags & SF_ERR_MASK)) |
| 4283 | s->flags |= SF_ERR_SRVTO; |
| 4284 | if (!(s->flags & SF_FINST_MASK)) |
| 4285 | s->flags |= SF_FINST_D; |
| 4286 | stream_inc_http_fail_ctr(s); |
| 4287 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 4288 | ret = HTTP_RULE_RES_ABRT; |
| 4289 | goto end; |
| 4290 | } |
| 4291 | |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4292 | void http_perform_server_redirect(struct stream *s, struct stconn *sc) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4293 | { |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4294 | struct channel *req = &s->req; |
| 4295 | struct channel *res = &s->res; |
| 4296 | struct server *srv; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4297 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4298 | struct htx_sl *sl; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4299 | struct ist path, location; |
| 4300 | unsigned int flags; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4301 | struct http_uri_parser parser; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4302 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4303 | /* |
| 4304 | * Create the location |
| 4305 | */ |
| 4306 | chunk_reset(&trash); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4307 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4308 | /* 1: add the server's prefix */ |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4309 | /* special prefix "/" means don't change URL */ |
| 4310 | srv = __objt_server(s->target); |
| 4311 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 4312 | if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len)) |
| 4313 | return; |
| 4314 | } |
| 4315 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4316 | /* 2: add the request Path */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4317 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 4318 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4319 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4320 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4321 | if (!isttest(path)) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4322 | return; |
| 4323 | |
| 4324 | if (!chunk_memcat(&trash, path.ptr, path.len)) |
| 4325 | return; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4326 | location = ist2(trash.area, trash.data); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4327 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4328 | /* |
| 4329 | * Create the 302 respone |
| 4330 | */ |
| 4331 | htx = htx_from_buf(&res->buf); |
| 4332 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 4333 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4334 | ist("HTTP/1.1"), ist("302"), ist("Found")); |
| 4335 | if (!sl) |
| 4336 | goto fail; |
| 4337 | sl->info.res.status = 302; |
| 4338 | s->txn->status = 302; |
| 4339 | |
| 4340 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) || |
| 4341 | !htx_add_header(htx, ist("Connection"), ist("close")) || |
| 4342 | !htx_add_header(htx, ist("Content-length"), ist("0")) || |
| 4343 | !htx_add_header(htx, ist("Location"), location)) |
| 4344 | goto fail; |
| 4345 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4346 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4347 | goto fail; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4348 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4349 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | c20afb8 | 2020-01-24 19:16:26 +0100 | [diff] [blame] | 4350 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4351 | if (!http_forward_proxy_resp(s, 1)) |
| 4352 | goto fail; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4353 | |
| 4354 | /* return without error. */ |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4355 | sc_shutr(sc); |
| 4356 | sc_shutw(sc); |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4357 | s->conn_err_type = STRM_ET_NONE; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4358 | sc->state = SC_ST_CLO; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4359 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4360 | if (!(s->flags & SF_ERR_MASK)) |
| 4361 | s->flags |= SF_ERR_LOCAL; |
| 4362 | if (!(s->flags & SF_FINST_MASK)) |
| 4363 | s->flags |= SF_FINST_C; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4364 | |
| 4365 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 4366 | srv_inc_sess_ctr(srv); |
| 4367 | srv_set_sess_last(srv); |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4368 | return; |
| 4369 | |
| 4370 | fail: |
| 4371 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4372 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4373 | channel_htx_truncate(res, htx); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4374 | } |
| 4375 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4376 | /* This function terminates the request because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4377 | * because an error was triggered during the body forwarding. |
| 4378 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4379 | static void http_end_request(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4380 | { |
| 4381 | struct channel *chn = &s->req; |
| 4382 | struct http_txn *txn = s->txn; |
| 4383 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4384 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4385 | |
Christopher Faulet | b42a8b6 | 2018-11-19 21:59:00 +0100 | [diff] [blame] | 4386 | if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR || |
| 4387 | txn->rsp.msg_state == HTTP_MSG_ERROR)) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4388 | channel_abort(chn); |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4389 | channel_htx_truncate(chn, htxbuf(&chn->buf)); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4390 | goto end; |
| 4391 | } |
| 4392 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4393 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) { |
| 4394 | 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] | 4395 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4396 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4397 | |
| 4398 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4399 | /* No need to read anymore, the request was completely parsed. |
| 4400 | * We can shut the read side unless we want to abort_on_close, |
| 4401 | * or we have a POST request. The issue with POST requests is |
| 4402 | * that some browsers still send a CRLF after the request, and |
| 4403 | * this CRLF must be read so that it does not remain in the kernel |
| 4404 | * buffers, otherwise a close could cause an RST on some systems |
| 4405 | * (eg: Linux). |
| 4406 | */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 4407 | 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] | 4408 | channel_dont_read(chn); |
| 4409 | |
| 4410 | /* if the server closes the connection, we want to immediately react |
| 4411 | * and close the socket to save packets and syscalls. |
| 4412 | */ |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 4413 | s->scb->flags |= SC_FL_NOHALF; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4414 | |
| 4415 | /* In any case we've finished parsing the request so we must |
| 4416 | * disable Nagle when sending data because 1) we're not going |
| 4417 | * to shut this side, and 2) the server is waiting for us to |
| 4418 | * send pending data. |
| 4419 | */ |
| 4420 | chn->flags |= CF_NEVER_WAIT; |
| 4421 | |
Christopher Faulet | d01ce40 | 2019-01-02 17:44:13 +0100 | [diff] [blame] | 4422 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4423 | /* The server has not finished to respond, so we |
| 4424 | * don't want to move in order not to upset it. |
| 4425 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4426 | 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] | 4427 | return; |
| 4428 | } |
| 4429 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4430 | /* When we get here, it means that both the request and the |
| 4431 | * response have finished receiving. Depending on the connection |
| 4432 | * mode, we'll have to wait for the last bytes to leave in either |
| 4433 | * direction, and sometimes for a close to be effective. |
| 4434 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4435 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4436 | /* Tunnel mode will not have any analyser so it needs to |
| 4437 | * poll for reads. |
| 4438 | */ |
| 4439 | channel_auto_read(chn); |
| 4440 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 4441 | } |
| 4442 | else { |
| 4443 | /* we're not expecting any new data to come for this |
| 4444 | * transaction, so we can close it. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4445 | * |
| 4446 | * However, there is an exception if the response |
| 4447 | * length is undefined. In this case, we need to wait |
| 4448 | * the close from the server. The response will be |
| 4449 | * switched in TUNNEL mode until the end. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4450 | */ |
| 4451 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 4452 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4453 | goto check_channel_flags; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4454 | |
| 4455 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4456 | channel_shutr_now(chn); |
| 4457 | channel_shutw_now(chn); |
| 4458 | } |
| 4459 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4460 | goto check_channel_flags; |
| 4461 | } |
| 4462 | |
| 4463 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4464 | http_msg_closing: |
| 4465 | /* nothing else to forward, just waiting for the output buffer |
| 4466 | * to be empty and for the shutw_now to take effect. |
| 4467 | */ |
| 4468 | if (channel_is_empty(chn)) { |
| 4469 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4470 | goto http_msg_closed; |
| 4471 | } |
| 4472 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4473 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4474 | goto end; |
| 4475 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4476 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4477 | return; |
| 4478 | } |
| 4479 | |
| 4480 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4481 | http_msg_closed: |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4482 | /* if we don't know whether the server will close, we need to hard close */ |
| 4483 | if (txn->rsp.flags & HTTP_MSGF_XFER_LEN) |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 4484 | s->scb->flags |= SC_FL_NOLINGER; /* we want to close ASAP */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4485 | /* 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] | 4486 | if (!(s->be->options & PR_O_ABRT_CLOSE)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4487 | channel_dont_read(chn); |
| 4488 | goto end; |
| 4489 | } |
| 4490 | |
| 4491 | check_channel_flags: |
| 4492 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4493 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4494 | /* if we've just closed an output, let's switch */ |
| 4495 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4496 | goto http_msg_closing; |
| 4497 | } |
| 4498 | |
| 4499 | end: |
| 4500 | chn->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4501 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4502 | chn->flags |= CF_NEVER_WAIT; |
| 4503 | if (HAS_REQ_DATA_FILTERS(s)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4504 | chn->analysers |= AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4505 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4506 | channel_auto_close(chn); |
| 4507 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4508 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4509 | } |
| 4510 | |
| 4511 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4512 | /* This function terminates the response because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4513 | * because an error was triggered during the body forwarding. |
| 4514 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4515 | static void http_end_response(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4516 | { |
| 4517 | struct channel *chn = &s->res; |
| 4518 | struct http_txn *txn = s->txn; |
| 4519 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4520 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4521 | |
Christopher Faulet | b42a8b6 | 2018-11-19 21:59:00 +0100 | [diff] [blame] | 4522 | if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR || |
| 4523 | txn->rsp.msg_state == HTTP_MSG_ERROR)) { |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4524 | channel_htx_truncate(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4525 | channel_abort(&s->req); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4526 | goto end; |
| 4527 | } |
| 4528 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4529 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) { |
| 4530 | 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] | 4531 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4532 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4533 | |
| 4534 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4535 | /* In theory, we don't need to read anymore, but we must |
| 4536 | * still monitor the server connection for a possible close |
| 4537 | * while the request is being uploaded, so we don't disable |
| 4538 | * reading. |
| 4539 | */ |
| 4540 | /* channel_dont_read(chn); */ |
| 4541 | |
| 4542 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4543 | /* The client seems to still be sending data, probably |
| 4544 | * because we got an error response during an upload. |
| 4545 | * We have the choice of either breaking the connection |
| 4546 | * or letting it pass through. Let's do the later. |
| 4547 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4548 | 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] | 4549 | return; |
| 4550 | } |
| 4551 | |
| 4552 | /* When we get here, it means that both the request and the |
| 4553 | * response have finished receiving. Depending on the connection |
| 4554 | * mode, we'll have to wait for the last bytes to leave in either |
| 4555 | * direction, and sometimes for a close to be effective. |
| 4556 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4557 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4558 | channel_auto_read(chn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4559 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4560 | } |
| 4561 | else { |
| 4562 | /* we're not expecting any new data to come for this |
| 4563 | * transaction, so we can close it. |
| 4564 | */ |
| 4565 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4566 | channel_shutr_now(chn); |
| 4567 | channel_shutw_now(chn); |
| 4568 | } |
| 4569 | } |
| 4570 | goto check_channel_flags; |
| 4571 | } |
| 4572 | |
| 4573 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4574 | http_msg_closing: |
| 4575 | /* nothing else to forward, just waiting for the output buffer |
| 4576 | * to be empty and for the shutw_now to take effect. |
| 4577 | */ |
| 4578 | if (channel_is_empty(chn)) { |
| 4579 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4580 | goto http_msg_closed; |
| 4581 | } |
| 4582 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4583 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4584 | _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts); |
| 4585 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 4586 | if (strm_sess(s)->listener && strm_sess(s)->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4587 | _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4588 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4589 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4590 | goto end; |
| 4591 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4592 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4593 | return; |
| 4594 | } |
| 4595 | |
| 4596 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4597 | http_msg_closed: |
| 4598 | /* drop any pending data */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4599 | channel_htx_truncate(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4600 | channel_abort(&s->req); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4601 | goto end; |
| 4602 | } |
| 4603 | |
| 4604 | check_channel_flags: |
| 4605 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4606 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4607 | /* if we've just closed an output, let's switch */ |
| 4608 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4609 | goto http_msg_closing; |
| 4610 | } |
| 4611 | |
| 4612 | end: |
| 4613 | chn->analysers &= AN_RES_FLT_END; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4614 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4615 | chn->flags |= CF_NEVER_WAIT; |
| 4616 | if (HAS_RSP_DATA_FILTERS(s)) |
| 4617 | chn->analysers |= AN_RES_FLT_XFER_DATA; |
| 4618 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4619 | channel_auto_close(chn); |
| 4620 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4621 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4622 | } |
| 4623 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4624 | /* Forward a response generated by HAProxy (error/redirect/return). This |
| 4625 | * function forwards all pending incoming data. If <final> is set to 0, nothing |
| 4626 | * more is performed. It is used for 1xx informational messages. Otherwise, the |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4627 | * transaction is terminated and the request is emptied. On success 1 is |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4628 | * returned. If an error occurred, 0 is returned. If it fails, this function |
| 4629 | * only exits. It is the caller responsibility to do the cleanup. |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4630 | */ |
| 4631 | int http_forward_proxy_resp(struct stream *s, int final) |
| 4632 | { |
| 4633 | struct channel *req = &s->req; |
| 4634 | struct channel *res = &s->res; |
| 4635 | struct htx *htx = htxbuf(&res->buf); |
| 4636 | size_t data; |
| 4637 | |
| 4638 | if (final) { |
| 4639 | htx->flags |= HTX_FL_PROXY_RESP; |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4640 | |
Christopher Faulet | aab1b67 | 2020-11-18 16:44:02 +0100 | [diff] [blame] | 4641 | if (!htx_is_empty(htx) && !http_eval_after_res_rules(s)) |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 4642 | return 0; |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4643 | |
Christopher Faulet | d6c4836 | 2020-10-19 18:01:38 +0200 | [diff] [blame] | 4644 | if (s->txn->meth == HTTP_METH_HEAD) |
| 4645 | htx_skip_msg_payload(htx); |
| 4646 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4647 | channel_auto_read(req); |
| 4648 | channel_abort(req); |
| 4649 | channel_auto_close(req); |
| 4650 | channel_htx_erase(req, htxbuf(&req->buf)); |
| 4651 | |
| 4652 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 4653 | channel_auto_read(res); |
| 4654 | channel_auto_close(res); |
| 4655 | channel_shutr_now(res); |
Christopher Faulet | 1a9db7c | 2020-06-25 15:36:45 +0200 | [diff] [blame] | 4656 | res->flags |= CF_EOI; /* The response is terminated, add EOI */ |
Christopher Faulet | 42432f3 | 2020-11-20 17:43:16 +0100 | [diff] [blame] | 4657 | htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */ |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4658 | } |
Christopher Faulet | cf6898c | 2020-06-25 15:55:11 +0200 | [diff] [blame] | 4659 | else { |
| 4660 | /* Send ASAP informational messages. Rely on CF_EOI for final |
| 4661 | * response. |
| 4662 | */ |
| 4663 | res->flags |= CF_SEND_DONTWAIT; |
| 4664 | } |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4665 | |
| 4666 | data = htx->data - co_data(res); |
| 4667 | c_adv(res, data); |
| 4668 | htx->first = -1; |
| 4669 | res->total += data; |
| 4670 | return 1; |
| 4671 | } |
| 4672 | |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4673 | void http_server_error(struct stream *s, struct stconn *sc, int err, |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4674 | int finst, struct http_reply *msg) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4675 | { |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4676 | http_reply_and_close(s, s->txn->status, msg); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4677 | if (!(s->flags & SF_ERR_MASK)) |
| 4678 | s->flags |= err; |
| 4679 | if (!(s->flags & SF_FINST_MASK)) |
| 4680 | s->flags |= finst; |
| 4681 | } |
| 4682 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4683 | 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] | 4684 | { |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4685 | if (!msg) { |
| 4686 | channel_htx_truncate(&s->res, htxbuf(&s->res.buf)); |
| 4687 | goto end; |
| 4688 | } |
| 4689 | |
| 4690 | if (http_reply_message(s, msg) == -1) { |
| 4691 | /* 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] | 4692 | * it is already an internal error. If it was already a "const" |
| 4693 | * 500 error, just fail. |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4694 | */ |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4695 | if (s->txn->status == 500) { |
| 4696 | if (s->txn->flags & TX_CONST_REPLY) |
| 4697 | goto end; |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4698 | s->txn->flags |= TX_CONST_REPLY; |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4699 | } |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4700 | s->txn->status = 500; |
| 4701 | s->txn->http_reply = NULL; |
| 4702 | return http_reply_and_close(s, s->txn->status, http_error_message(s)); |
| 4703 | } |
| 4704 | |
| 4705 | end: |
| 4706 | s->res.wex = tick_add_ifset(now_ms, s->res.wto); |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4707 | |
Christopher Faulet | 2d56500 | 2021-09-10 09:17:50 +0200 | [diff] [blame] | 4708 | /* At this staged, HTTP analysis is finished */ |
| 4709 | s->req.analysers &= AN_REQ_FLT_END; |
| 4710 | s->req.analyse_exp = TICK_ETERNITY; |
| 4711 | |
| 4712 | s->res.analysers &= AN_RES_FLT_END; |
| 4713 | s->res.analyse_exp = TICK_ETERNITY; |
| 4714 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4715 | channel_auto_read(&s->req); |
| 4716 | channel_abort(&s->req); |
| 4717 | channel_auto_close(&s->req); |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4718 | channel_htx_erase(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4719 | channel_auto_read(&s->res); |
| 4720 | channel_auto_close(&s->res); |
| 4721 | channel_shutr_now(&s->res); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4722 | } |
| 4723 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4724 | struct http_reply *http_error_message(struct stream *s) |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4725 | { |
| 4726 | const int msgnum = http_get_status_idx(s->txn->status); |
| 4727 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4728 | if (s->txn->http_reply) |
| 4729 | return s->txn->http_reply; |
| 4730 | else if (s->be->replies[msgnum]) |
| 4731 | return s->be->replies[msgnum]; |
| 4732 | else if (strm_fe(s)->replies[msgnum]) |
| 4733 | return strm_fe(s)->replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4734 | else |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4735 | return &http_err_replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4736 | } |
| 4737 | |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4738 | /* Produces an HTX message from an http reply. Depending on the http reply type, |
| 4739 | * a, errorfile, an raw file or a log-format string is used. On success, it |
| 4740 | * returns 0. If an error occurs -1 is returned. If it fails, this function only |
| 4741 | * exits. It is the caller responsibility to do the cleanup. |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4742 | */ |
Christopher Faulet | ae43b6c | 2020-05-27 15:24:22 +0200 | [diff] [blame] | 4743 | 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] | 4744 | { |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4745 | struct buffer *errmsg; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4746 | struct htx_sl *sl; |
| 4747 | struct buffer *body = NULL; |
| 4748 | const char *status, *reason, *clen, *ctype; |
| 4749 | unsigned int slflags; |
| 4750 | int ret = 0; |
| 4751 | |
Christopher Faulet | e29a97e | 2020-05-14 14:49:25 +0200 | [diff] [blame] | 4752 | /* |
| 4753 | * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so |
| 4754 | * |
| 4755 | * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled |
| 4756 | * as no payload if NULL. the TXN status code is set with the status |
| 4757 | * of the original reply. |
| 4758 | */ |
| 4759 | |
| 4760 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4761 | if (reply->body.reply) |
| 4762 | reply = reply->body.reply; |
| 4763 | } |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4764 | if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) { |
| 4765 | /* get default error message */ |
| 4766 | if (reply == s->txn->http_reply) |
| 4767 | s->txn->http_reply = NULL; |
| 4768 | reply = http_error_message(s); |
| 4769 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4770 | if (reply->body.reply) |
| 4771 | reply = reply->body.reply; |
| 4772 | } |
| 4773 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4774 | |
| 4775 | if (reply->type == HTTP_REPLY_ERRMSG) { |
| 4776 | /* implicit or explicit error message*/ |
| 4777 | errmsg = reply->body.errmsg; |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4778 | if (errmsg && !b_is_null(errmsg)) { |
Christopher Faulet | 2056736 | 2020-05-15 14:52:49 +0200 | [diff] [blame] | 4779 | if (!htx_copy_msg(htx, errmsg)) |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4780 | goto fail; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4781 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4782 | } |
| 4783 | else { |
| 4784 | /* no payload, file or log-format string */ |
| 4785 | if (reply->type == HTTP_REPLY_RAW) { |
| 4786 | /* file */ |
| 4787 | body = &reply->body.obj; |
| 4788 | } |
| 4789 | else if (reply->type == HTTP_REPLY_LOGFMT) { |
| 4790 | /* log-format string */ |
| 4791 | body = alloc_trash_chunk(); |
| 4792 | if (!body) |
| 4793 | goto fail_alloc; |
| 4794 | body->data = build_logline(s, body->area, body->size, &reply->body.fmt); |
| 4795 | } |
| 4796 | /* else no payload */ |
| 4797 | |
| 4798 | status = ultoa(reply->status); |
| 4799 | reason = http_get_reason(reply->status); |
| 4800 | slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN); |
| 4801 | if (!body || !b_data(body)) |
| 4802 | slflags |= HTX_SL_F_BODYLESS; |
| 4803 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4804 | if (!sl) |
| 4805 | goto fail; |
| 4806 | sl->info.res.status = reply->status; |
| 4807 | |
| 4808 | clen = (body ? ultoa(b_data(body)) : "0"); |
| 4809 | ctype = reply->ctype; |
| 4810 | |
| 4811 | if (!LIST_ISEMPTY(&reply->hdrs)) { |
| 4812 | struct http_reply_hdr *hdr; |
| 4813 | struct buffer *value = alloc_trash_chunk(); |
| 4814 | |
| 4815 | if (!value) |
| 4816 | goto fail; |
| 4817 | |
| 4818 | list_for_each_entry(hdr, &reply->hdrs, list) { |
| 4819 | chunk_reset(value); |
| 4820 | value->data = build_logline(s, value->area, value->size, &hdr->value); |
| 4821 | if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) { |
| 4822 | free_trash_chunk(value); |
| 4823 | goto fail; |
| 4824 | } |
| 4825 | chunk_reset(value); |
| 4826 | } |
| 4827 | free_trash_chunk(value); |
| 4828 | } |
| 4829 | |
| 4830 | if (!htx_add_header(htx, ist("content-length"), ist(clen)) || |
| 4831 | (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) || |
| 4832 | !htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4833 | (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] | 4834 | goto fail; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4835 | |
| 4836 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4837 | } |
| 4838 | |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4839 | leave: |
| 4840 | if (reply->type == HTTP_REPLY_LOGFMT) |
| 4841 | free_trash_chunk(body); |
| 4842 | return ret; |
| 4843 | |
| 4844 | fail_alloc: |
| 4845 | if (!(s->flags & SF_ERR_MASK)) |
| 4846 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4847 | /* fall through */ |
| 4848 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4849 | ret = -1; |
| 4850 | goto leave; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4851 | } |
| 4852 | |
| 4853 | /* 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] | 4854 | * occurs -1 is returned and the response channel is truncated, removing this |
| 4855 | * way the faulty reply. This function may fail when the reply is formatted |
| 4856 | * (http_reply_to_htx) or when the reply is forwarded |
| 4857 | * (http_forward_proxy_resp). On the last case, it is because a |
| 4858 | * http-after-response rule fails. |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4859 | */ |
| 4860 | int http_reply_message(struct stream *s, struct http_reply *reply) |
| 4861 | { |
| 4862 | struct channel *res = &s->res; |
| 4863 | struct htx *htx = htx_from_buf(&res->buf); |
| 4864 | |
| 4865 | if (s->txn->status == -1) |
| 4866 | s->txn->status = reply->status; |
| 4867 | channel_htx_truncate(res, htx); |
| 4868 | |
| 4869 | if (http_reply_to_htx(s, htx, reply) == -1) |
| 4870 | goto fail; |
| 4871 | |
| 4872 | htx_to_buf(htx, &s->res.buf); |
| 4873 | if (!http_forward_proxy_resp(s, 1)) |
| 4874 | goto fail; |
| 4875 | return 0; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4876 | |
| 4877 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4878 | channel_htx_truncate(res, htx); |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4879 | if (!(s->flags & SF_ERR_MASK)) |
| 4880 | s->flags |= SF_ERR_PRXCOND; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4881 | return -1; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4882 | } |
| 4883 | |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4884 | /* 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] | 4885 | * that the server side is closed. Note that err_type is actually a |
| 4886 | * bitmask, where almost only aborts may be cumulated with other |
| 4887 | * values. We consider that aborted operations are more important |
| 4888 | * than timeouts or errors due to the fact that nobody else in the |
| 4889 | * logs might explain incomplete retries. All others should avoid |
| 4890 | * being cumulated. It should normally not be possible to have multiple |
| 4891 | * aborts at once, but just in case, the first one in sequence is reported. |
| 4892 | * Note that connection errors appearing on the second request of a keep-alive |
| 4893 | * connection are not reported since this allows the client to retry. |
| 4894 | */ |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4895 | void http_return_srv_error(struct stream *s, struct stconn *sc) |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4896 | { |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4897 | int err_type = s->conn_err_type; |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4898 | |
| 4899 | /* set s->txn->status for http_error_message(s) */ |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4900 | if (err_type & STRM_ET_QUEUE_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_Q, 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_CONN_ABRT) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4905 | s->txn->status = -1; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4906 | http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_C, NULL); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4907 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4908 | else if (err_type & STRM_ET_QUEUE_TO) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4909 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4910 | http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4911 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4912 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4913 | else if (err_type & STRM_ET_QUEUE_ERR) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4914 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4915 | http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4916 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4917 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4918 | else if (err_type & STRM_ET_CONN_TO) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4919 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4920 | http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4921 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4922 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4923 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4924 | else if (err_type & STRM_ET_CONN_ERR) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4925 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4926 | http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4927 | (s->flags & SF_SRV_REUSED) ? NULL : |
| 4928 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4929 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4930 | else if (err_type & STRM_ET_CONN_RES) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4931 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4932 | http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4933 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4934 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4935 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4936 | else { /* STRM_ET_CONN_OTHER and others */ |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4937 | s->txn->status = 500; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4938 | http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4939 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4940 | } |
| 4941 | } |
| 4942 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4943 | |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4944 | /* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0 |
| 4945 | * on success and -1 on error. |
| 4946 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4947 | 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] | 4948 | { |
| 4949 | /* If we have HTTP/1.1 message with a body and Expect: 100-continue, |
| 4950 | * then we must send an HTTP/1.1 100 Continue intermediate response. |
| 4951 | */ |
| 4952 | if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) && |
| 4953 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
| 4954 | struct ist hdr = { .ptr = "Expect", .len = 6 }; |
| 4955 | struct http_hdr_ctx ctx; |
| 4956 | |
| 4957 | ctx.blk = NULL; |
| 4958 | /* Expect is allowed in 1.1, look for it */ |
| 4959 | if (http_find_header(htx, hdr, &ctx, 0) && |
| 4960 | unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4961 | if (http_reply_100_continue(s) == -1) |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4962 | return -1; |
| 4963 | http_remove_header(htx, &ctx); |
| 4964 | } |
| 4965 | } |
| 4966 | return 0; |
| 4967 | } |
| 4968 | |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4969 | /* Send a 100-Continue response to the client. It returns 0 on success and -1 |
| 4970 | * on error. The response channel is updated accordingly. |
| 4971 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4972 | static int http_reply_100_continue(struct stream *s) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4973 | { |
| 4974 | struct channel *res = &s->res; |
| 4975 | struct htx *htx = htx_from_buf(&res->buf); |
| 4976 | struct htx_sl *sl; |
| 4977 | unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11| |
| 4978 | HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4979 | |
| 4980 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4981 | ist("HTTP/1.1"), ist("100"), ist("Continue")); |
| 4982 | if (!sl) |
| 4983 | goto fail; |
| 4984 | sl->info.res.status = 100; |
| 4985 | |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 4986 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4987 | goto fail; |
| 4988 | |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4989 | if (!http_forward_proxy_resp(s, 0)) |
| 4990 | goto fail; |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4991 | return 0; |
| 4992 | |
| 4993 | fail: |
| 4994 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4995 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4996 | channel_htx_truncate(res, htx); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4997 | return -1; |
| 4998 | } |
| 4999 | |
Christopher Faulet | 12c51e2 | 2018-11-28 15:59:42 +0100 | [diff] [blame] | 5000 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5001 | /* |
| 5002 | * Capture headers from message <htx> according to header list <cap_hdr>, and |
| 5003 | * fill the <cap> pointers appropriately. |
| 5004 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5005 | 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] | 5006 | { |
| 5007 | struct cap_hdr *h; |
| 5008 | int32_t pos; |
| 5009 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5010 | 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] | 5011 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5012 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5013 | struct ist n, v; |
| 5014 | |
| 5015 | if (type == HTX_BLK_EOH) |
| 5016 | break; |
| 5017 | if (type != HTX_BLK_HDR) |
| 5018 | continue; |
| 5019 | |
| 5020 | n = htx_get_blk_name(htx, blk); |
| 5021 | |
| 5022 | for (h = cap_hdr; h; h = h->next) { |
| 5023 | if (h->namelen && (h->namelen == n.len) && |
| 5024 | (strncasecmp(n.ptr, h->name, h->namelen) == 0)) { |
| 5025 | if (cap[h->index] == NULL) |
| 5026 | cap[h->index] = |
| 5027 | pool_alloc(h->pool); |
| 5028 | |
| 5029 | if (cap[h->index] == NULL) { |
| 5030 | ha_alert("HTTP capture : out of memory.\n"); |
| 5031 | break; |
| 5032 | } |
| 5033 | |
| 5034 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 5035 | v = isttrim(v, h->len); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5036 | |
| 5037 | memcpy(cap[h->index], v.ptr, v.len); |
| 5038 | cap[h->index][v.len]=0; |
| 5039 | } |
| 5040 | } |
| 5041 | } |
| 5042 | } |
| 5043 | |
Christopher Faulet | 0b6bdc5 | 2018-10-24 11:05:36 +0200 | [diff] [blame] | 5044 | /* Delete a value in a header between delimiters <from> and <next>. The header |
| 5045 | * itself is delimited by <start> and <end> pointers. The number of characters |
| 5046 | * displaced is returned, and the pointer to the first delimiter is updated if |
| 5047 | * required. The function tries as much as possible to respect the following |
| 5048 | * principles : |
| 5049 | * - replace <from> delimiter by the <next> one unless <from> points to <start>, |
| 5050 | * in which case <next> is simply removed |
| 5051 | * - set exactly one space character after the new first delimiter, unless there |
| 5052 | * are not enough characters in the block being moved to do so. |
| 5053 | * - remove unneeded spaces before the previous delimiter and after the new |
| 5054 | * one. |
| 5055 | * |
| 5056 | * It is the caller's responsibility to ensure that : |
| 5057 | * - <from> points to a valid delimiter or <start> ; |
| 5058 | * - <next> points to a valid delimiter or <end> ; |
| 5059 | * - there are non-space chars before <from>. |
| 5060 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5061 | 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] | 5062 | { |
| 5063 | char *prev = *from; |
| 5064 | |
| 5065 | if (prev == start) { |
| 5066 | /* We're removing the first value. eat the semicolon, if <next> |
| 5067 | * is lower than <end> */ |
| 5068 | if (next < end) |
| 5069 | next++; |
| 5070 | |
| 5071 | while (next < end && HTTP_IS_SPHT(*next)) |
| 5072 | next++; |
| 5073 | } |
| 5074 | else { |
| 5075 | /* Remove useless spaces before the old delimiter. */ |
| 5076 | while (HTTP_IS_SPHT(*(prev-1))) |
| 5077 | prev--; |
| 5078 | *from = prev; |
| 5079 | |
| 5080 | /* copy the delimiter and if possible a space if we're |
| 5081 | * not at the end of the line. |
| 5082 | */ |
| 5083 | if (next < end) { |
| 5084 | *prev++ = *next++; |
| 5085 | if (prev + 1 < next) |
| 5086 | *prev++ = ' '; |
| 5087 | while (next < end && HTTP_IS_SPHT(*next)) |
| 5088 | next++; |
| 5089 | } |
| 5090 | } |
| 5091 | memmove(prev, next, end - next); |
| 5092 | return (prev - next); |
| 5093 | } |
| 5094 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5095 | |
| 5096 | /* 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] | 5097 | * 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] | 5098 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5099 | 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] | 5100 | { |
| 5101 | struct ist dst = ist2(str, 0); |
| 5102 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5103 | if (istcat(&dst, htx_sl_req_meth(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5104 | goto end; |
| 5105 | if (dst.len + 1 > len) |
| 5106 | goto end; |
| 5107 | dst.ptr[dst.len++] = ' '; |
| 5108 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5109 | if (istcat(&dst, htx_sl_req_uri(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5110 | goto end; |
| 5111 | if (dst.len + 1 > len) |
| 5112 | goto end; |
| 5113 | dst.ptr[dst.len++] = ' '; |
| 5114 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5115 | istcat(&dst, htx_sl_req_vsn(sl), len); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5116 | end: |
| 5117 | return dst.len; |
| 5118 | } |
| 5119 | |
| 5120 | /* |
| 5121 | * Print a debug line with a start line. |
| 5122 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5123 | 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] | 5124 | { |
| 5125 | struct session *sess = strm_sess(s); |
| 5126 | int max; |
| 5127 | |
| 5128 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 5129 | dir, |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 5130 | 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] | 5131 | 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] | 5132 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5133 | max = HTX_SL_P1_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5134 | UBOUND(max, trash.size - trash.data - 3); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5135 | chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5136 | trash.area[trash.data++] = ' '; |
| 5137 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5138 | max = HTX_SL_P2_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5139 | UBOUND(max, trash.size - trash.data - 2); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5140 | chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5141 | trash.area[trash.data++] = ' '; |
| 5142 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5143 | max = HTX_SL_P3_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5144 | UBOUND(max, trash.size - trash.data - 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5145 | chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5146 | trash.area[trash.data++] = '\n'; |
| 5147 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 5148 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5149 | } |
| 5150 | |
| 5151 | /* |
| 5152 | * Print a debug line with a header. |
| 5153 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5154 | 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] | 5155 | { |
| 5156 | struct session *sess = strm_sess(s); |
| 5157 | int max; |
| 5158 | |
| 5159 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 5160 | dir, |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 5161 | 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] | 5162 | 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] | 5163 | |
| 5164 | max = n.len; |
| 5165 | UBOUND(max, trash.size - trash.data - 3); |
| 5166 | chunk_memcat(&trash, n.ptr, max); |
| 5167 | trash.area[trash.data++] = ':'; |
| 5168 | trash.area[trash.data++] = ' '; |
| 5169 | |
| 5170 | max = v.len; |
| 5171 | UBOUND(max, trash.size - trash.data - 1); |
| 5172 | chunk_memcat(&trash, v.ptr, max); |
| 5173 | trash.area[trash.data++] = '\n'; |
| 5174 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 5175 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5176 | } |
| 5177 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5178 | void http_txn_reset_req(struct http_txn *txn) |
| 5179 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 5180 | txn->req.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5181 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 5182 | } |
| 5183 | |
| 5184 | void http_txn_reset_res(struct http_txn *txn) |
| 5185 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 5186 | txn->rsp.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5187 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 5188 | } |
| 5189 | |
| 5190 | /* |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5191 | * Create and initialize a new HTTP transaction for stream <s>. This should be |
| 5192 | * used before processing any new request. It returns the transaction or NLULL |
| 5193 | * on error. |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5194 | */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5195 | struct http_txn *http_create_txn(struct stream *s) |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5196 | { |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5197 | struct http_txn *txn; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 5198 | struct stconn *sc = s->scf; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5199 | |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5200 | txn = pool_alloc(pool_head_http_txn); |
| 5201 | if (!txn) |
| 5202 | return NULL; |
| 5203 | s->txn = txn; |
| 5204 | |
Christopher Faulet | 5eb67f5 | 2022-06-22 17:12:05 +0200 | [diff] [blame] | 5205 | txn->meth = HTTP_METH_OTHER; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 5206 | 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] | 5207 | txn->status = -1; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 5208 | txn->http_reply = NULL; |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 5209 | txn->l7_buffer = BUF_NULL; |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 5210 | write_u32(txn->cache_hash, 0); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5211 | |
| 5212 | txn->cookie_first_date = 0; |
| 5213 | txn->cookie_last_date = 0; |
| 5214 | |
| 5215 | txn->srv_cookie = NULL; |
| 5216 | txn->cli_cookie = NULL; |
| 5217 | txn->uri = NULL; |
| 5218 | |
| 5219 | http_txn_reset_req(txn); |
| 5220 | http_txn_reset_res(txn); |
| 5221 | |
| 5222 | txn->req.chn = &s->req; |
| 5223 | txn->rsp.chn = &s->res; |
| 5224 | |
| 5225 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
| 5226 | |
Willy Tarreau | b7bfcb3 | 2021-08-31 08:13:25 +0200 | [diff] [blame] | 5227 | vars_init_head(&s->vars_txn, SCOPE_TXN); |
| 5228 | vars_init_head(&s->vars_reqres, SCOPE_REQ); |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5229 | |
| 5230 | return txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5231 | } |
| 5232 | |
| 5233 | /* to be used at the end of a transaction */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5234 | void http_destroy_txn(struct stream *s) |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5235 | { |
| 5236 | struct http_txn *txn = s->txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5237 | |
| 5238 | /* these ones will have been dynamically allocated */ |
| 5239 | pool_free(pool_head_requri, txn->uri); |
| 5240 | pool_free(pool_head_capture, txn->cli_cookie); |
| 5241 | pool_free(pool_head_capture, txn->srv_cookie); |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5242 | pool_free(pool_head_uniqueid, s->unique_id.ptr); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5243 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5244 | s->unique_id = IST_NULL; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5245 | txn->uri = NULL; |
| 5246 | txn->srv_cookie = NULL; |
| 5247 | txn->cli_cookie = NULL; |
| 5248 | |
Christopher Faulet | 5939925 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 5249 | if (!LIST_ISEMPTY(&s->vars_txn.head)) |
| 5250 | vars_prune(&s->vars_txn, s->sess, s); |
| 5251 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 5252 | vars_prune(&s->vars_reqres, s->sess, s); |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5253 | |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 5254 | b_free(&txn->l7_buffer); |
| 5255 | |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5256 | pool_free(pool_head_http_txn, txn); |
| 5257 | s->txn = NULL; |
Christopher Faulet | 5939925 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 5258 | } |
| 5259 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5260 | |
| 5261 | DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5262 | |
Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 5263 | /* |
| 5264 | * Local variables: |
| 5265 | * c-indent-level: 8 |
| 5266 | * c-basic-offset: 8 |
| 5267 | * End: |
| 5268 | */ |