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)); |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 335 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 336 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 337 | DBG_TRACE_DEVEL("leaving on error", |
| 338 | 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] | 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | /* This stream analyser runs all HTTP request processing which is common to |
| 344 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 345 | * reqadd, stats and redirects. This is performed for the designated proxy. |
| 346 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 347 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 348 | * error, ...). |
| 349 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 350 | 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] | 351 | { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 352 | struct list *def_rules, *rules; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 353 | struct session *sess = s->sess; |
| 354 | struct http_txn *txn = s->txn; |
| 355 | struct http_msg *msg = &txn->req; |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 356 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 357 | struct redirect_rule *rule; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 358 | enum rule_result verdict; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 359 | struct connection *conn = objt_conn(sess->origin); |
| 360 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 361 | 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] | 362 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 363 | htx = htxbuf(&req->buf); |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 364 | |
Christopher Faulet | 1907ccc | 2019-04-29 13:12:02 +0200 | [diff] [blame] | 365 | /* just in case we have some per-backend tracking. Only called the first |
| 366 | * execution of the analyser. */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 367 | if (!s->current_rule && !s->current_rule_list) |
Christopher Faulet | 1907ccc | 2019-04-29 13:12:02 +0200 | [diff] [blame] | 368 | stream_inc_be_http_req_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 369 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 370 | def_rules = ((px->defpx && (an_bit == AN_REQ_HTTP_PROCESS_FE || px != sess->fe)) ? &px->defpx->http_req_rules : NULL); |
| 371 | rules = &px->http_req_rules; |
| 372 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 373 | /* evaluate http-request rules */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 374 | if ((def_rules && !LIST_ISEMPTY(def_rules)) || !LIST_ISEMPTY(rules)) { |
| 375 | verdict = http_req_get_intercept_rule(px, def_rules, rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 376 | |
| 377 | switch (verdict) { |
| 378 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 379 | goto return_prx_yield; |
| 380 | |
| 381 | case HTTP_RULE_RES_CONT: |
| 382 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 383 | break; |
| 384 | |
| 385 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 386 | if (txn->flags & TX_CLTARPIT) |
| 387 | goto tarpit; |
| 388 | goto deny; |
| 389 | |
| 390 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 391 | goto return_prx_cond; |
| 392 | |
| 393 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
| 394 | goto done; |
| 395 | |
| 396 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 397 | goto return_bad_req; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 398 | |
| 399 | case HTTP_RULE_RES_ERROR: /* failed with a bad request */ |
| 400 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 404 | if (px->options2 & (PR_O2_RSTRICT_REQ_HDR_NAMES_BLK|PR_O2_RSTRICT_REQ_HDR_NAMES_DEL)) { |
| 405 | verdict = http_req_restrict_header_names(s, htx, px); |
| 406 | if (verdict == HTTP_RULE_RES_DENY) |
| 407 | goto deny; |
| 408 | } |
| 409 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 410 | if (conn && (conn->flags & CO_FL_EARLY_DATA) && |
Olivier Houchard | 220a26c | 2020-01-23 14:57:36 +0100 | [diff] [blame] | 411 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) { |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 412 | struct http_hdr_ctx ctx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 413 | |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 414 | ctx.blk = NULL; |
| 415 | if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) { |
| 416 | if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1")))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 417 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 418 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /* OK at this stage, we know that the request was accepted according to |
| 422 | * the http-request rules, we can check for the stats. Note that the |
| 423 | * URI is detected *before* the req* rules in order not to be affected |
| 424 | * by a possible reqrep, while they are processed *after* so that a |
| 425 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 426 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 427 | if (!s->target && http_stats_check_uri(s, txn, px)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 428 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | a0b58b5 | 2022-05-27 08:33:53 +0200 | [diff] [blame] | 429 | if (unlikely(!sc_applet_create(s->scb, objt_applet(s->target)))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 430 | s->logs.tv_request = now; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 431 | if (!(s->flags & SF_ERR_MASK)) |
| 432 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 433 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* parse the whole stats request and extract the relevant information */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 437 | http_handle_stats(s, req); |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 438 | 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] | 439 | /* not all actions implemented: deny, allow, auth */ |
| 440 | |
| 441 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 442 | goto deny; |
| 443 | |
| 444 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 445 | goto return_prx_cond; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 446 | |
| 447 | if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */ |
| 448 | goto return_bad_req; |
| 449 | |
| 450 | if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */ |
| 451 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Christopher Faulet | 2571bc6 | 2019-03-01 11:44:26 +0100 | [diff] [blame] | 454 | /* Proceed with the applets now. */ |
| 455 | if (unlikely(objt_applet(s->target))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 456 | 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] | 457 | _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 458 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 459 | if (http_handle_expect_hdr(s, htx, msg) == -1) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 460 | goto return_int_err; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 461 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 462 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 463 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 464 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 465 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 466 | if (HAS_FILTERS(s)) |
| 467 | req->analysers |= AN_REQ_FLT_HTTP_HDRS; |
| 468 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 469 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
| 470 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 471 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 472 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 473 | |
| 474 | req->flags |= CF_SEND_DONTWAIT; |
| 475 | s->flags |= SF_ASSIGNED; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 476 | goto done; |
| 477 | } |
| 478 | |
| 479 | /* check whether we have some ACLs set to redirect this request */ |
| 480 | list_for_each_entry(rule, &px->redirect_rules, list) { |
| 481 | if (rule->cond) { |
| 482 | int ret; |
| 483 | |
| 484 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 485 | ret = acl_pass(ret); |
| 486 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 487 | ret = !ret; |
| 488 | if (!ret) |
| 489 | continue; |
| 490 | } |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 491 | if (!http_apply_redirect_rule(rule, s, txn)) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 492 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 493 | goto done; |
| 494 | } |
| 495 | |
| 496 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 497 | * If this happens, then the data will not come immediately, so we must |
| 498 | * send all what we have without waiting. Note that due to the small gain |
| 499 | * in waiting for the body of the request, it's easier to simply put the |
| 500 | * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 501 | * itself once used. |
| 502 | */ |
| 503 | req->flags |= CF_SEND_DONTWAIT; |
| 504 | |
| 505 | done: /* done with this analyser, continue with next ones that the calling |
| 506 | * points will have set, if any. |
| 507 | */ |
| 508 | req->analyse_exp = TICK_ETERNITY; |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 509 | 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] | 510 | req->analysers &= ~an_bit; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 511 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 512 | 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] | 513 | return 1; |
| 514 | |
| 515 | tarpit: |
| 516 | /* Allow cookie logging |
| 517 | */ |
| 518 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 519 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 520 | |
| 521 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 522 | * which may be the same as the connect timeout if unspecified. |
| 523 | * If unset, then set it to zero because we really want it to |
| 524 | * eventually expire. We build the tarpit as an analyser. |
| 525 | */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 526 | channel_htx_erase(&s->req, htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 527 | |
| 528 | /* wipe the request out so that we can drop the connection early |
| 529 | * if the client closes first. |
| 530 | */ |
| 531 | channel_dont_connect(req); |
| 532 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 533 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
| 534 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 535 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 536 | if (!req->analyse_exp) |
| 537 | req->analyse_exp = tick_add(now_ms, 0); |
| 538 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 539 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 540 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 541 | _HA_ATOMIC_INC(&s->be->be_counters.denied_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 542 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 543 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 544 | goto done_without_exp; |
| 545 | |
| 546 | deny: /* this request was blocked (denied) */ |
| 547 | |
| 548 | /* Allow cookie logging |
| 549 | */ |
| 550 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 551 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 552 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 553 | s->logs.tv_request = now; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 554 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 555 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 556 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 557 | _HA_ATOMIC_INC(&s->be->be_counters.denied_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 558 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 559 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 560 | goto return_prx_err; |
| 561 | |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 562 | return_fail_rewrite: |
| 563 | if (!(s->flags & SF_ERR_MASK)) |
| 564 | s->flags |= SF_ERR_PRXCOND; |
| 565 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
| 566 | if (s->flags & SF_BE_ASSIGNED) |
| 567 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
| 568 | if (sess->listener && sess->listener->counters) |
| 569 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
| 570 | if (objt_server(s->target)) |
| 571 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
| 572 | /* fall through */ |
| 573 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 574 | return_int_err: |
| 575 | txn->status = 500; |
| 576 | if (!(s->flags & SF_ERR_MASK)) |
| 577 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 578 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 579 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 580 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 581 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 582 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 583 | goto return_prx_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 584 | |
| 585 | return_bad_req: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 586 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 587 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 588 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 589 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 590 | /* fall through */ |
| 591 | |
| 592 | return_prx_err: |
| 593 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 594 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 595 | |
| 596 | return_prx_cond: |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 597 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 598 | |
| 599 | req->analysers &= AN_REQ_FLT_END; |
| 600 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 601 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 602 | DBG_TRACE_DEVEL("leaving on error", |
| 603 | 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] | 604 | return 0; |
| 605 | |
| 606 | return_prx_yield: |
| 607 | channel_dont_connect(req); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 608 | DBG_TRACE_DEVEL("waiting for more data", |
| 609 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | /* This function performs all the processing enabled for the current request. |
| 614 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 615 | * needs more data, encounters an error, or wants to immediately abort the |
| 616 | * request. It relies on buffers flags, and updates s->req.analysers. |
| 617 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 618 | 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] | 619 | { |
| 620 | struct session *sess = s->sess; |
| 621 | struct http_txn *txn = s->txn; |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 622 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 623 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
| 624 | |
Christopher Faulet | 8bebd2f | 2020-10-06 17:54:56 +0200 | [diff] [blame] | 625 | 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] | 626 | |
| 627 | /* |
| 628 | * Right now, we know that we have processed the entire headers |
| 629 | * and that unwanted requests have been filtered out. We can do |
| 630 | * whatever we want with the remaining request. Also, now we |
| 631 | * may have separate values for ->fe, ->be. |
| 632 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 633 | htx = htxbuf(&req->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 634 | |
| 635 | /* |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 636 | * 7: Now we can work with the cookies. |
| 637 | * Note that doing so might move headers in the request, but |
| 638 | * the fields will stay coherent and the URI will not move. |
| 639 | * This should only be performed in the backend. |
| 640 | */ |
| 641 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 642 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 643 | |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 644 | /* 8: Generate unique ID if a "unique-id-format" is defined. |
| 645 | * |
| 646 | * A unique ID is generated even when it is not sent to ensure that the ID can make use of |
| 647 | * fetches only available in the HTTP request processing stage. |
| 648 | */ |
| 649 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 650 | 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] | 651 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 652 | if (!isttest(unique_id)) { |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 653 | if (!(s->flags & SF_ERR_MASK)) |
| 654 | s->flags |= SF_ERR_RESOURCE; |
| 655 | goto return_int_err; |
| 656 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 657 | |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 658 | /* send unique ID if a "unique-id-header" is defined */ |
Tim Duesterhus | 0643b0e | 2020-03-05 17:56:35 +0100 | [diff] [blame] | 659 | if (isttest(sess->fe->header_unique_id) && |
Tim Duesterhus | 7ad27d4 | 2022-05-18 00:22:15 +0200 | [diff] [blame] | 660 | unlikely(!http_add_header(htx, sess->fe->header_unique_id, unique_id))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 661 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* |
| 665 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 666 | * asks for it. |
| 667 | */ |
| 668 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | d68ff01 | 2022-05-27 08:57:21 +0200 | [diff] [blame] | 669 | const struct sockaddr_storage *src = sc_src(s->scf); |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 670 | struct http_hdr_ctx ctx = { .blk = NULL }; |
Tim Duesterhus | b50ab84 | 2022-03-05 00:52:41 +0100 | [diff] [blame] | 671 | 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] | 672 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 673 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 674 | http_find_header(htx, hdr, &ctx, 0)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 675 | /* The header is set to be added only if none is present |
| 676 | * and we found it, so don't do anything. |
| 677 | */ |
| 678 | } |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 679 | else if (src && src->ss_family == AF_INET) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 680 | /* Add an X-Forwarded-For header unless the source IP is |
| 681 | * in the 'except' network range. |
| 682 | */ |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 683 | if (ipcmp2net(src, &sess->fe->except_xff_net) && |
| 684 | ipcmp2net(src, &s->be->except_xff_net)) { |
| 685 | unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)src)->sin_addr; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 686 | |
| 687 | /* Note: we rely on the backend to get the header name to be used for |
| 688 | * x-forwarded-for, because the header is really meant for the backends. |
| 689 | * However, if the backend did not specify any option, we have to rely |
| 690 | * on the frontend's header name. |
| 691 | */ |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 692 | chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 693 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 694 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 695 | } |
| 696 | } |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 697 | else if (src && src->ss_family == AF_INET6) { |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 698 | /* Add an X-Forwarded-For header unless the source IP is |
| 699 | * in the 'except' network range. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 700 | */ |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 701 | if (ipcmp2net(src, &sess->fe->except_xff_net) && |
| 702 | ipcmp2net(src, &s->be->except_xff_net)) { |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 703 | char pn[INET6_ADDRSTRLEN]; |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 704 | |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 705 | inet_ntop(AF_INET6, |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 706 | (const void *)&((struct sockaddr_in6 *)(src))->sin6_addr, |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 707 | pn, sizeof(pn)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 708 | |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 709 | /* Note: we rely on the backend to get the header name to be used for |
| 710 | * x-forwarded-for, because the header is really meant for the backends. |
| 711 | * However, if the backend did not specify any option, we have to rely |
| 712 | * on the frontend's header name. |
| 713 | */ |
| 714 | chunk_printf(&trash, "%s", pn); |
| 715 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 716 | goto return_fail_rewrite; |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 717 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | * 10: add X-Original-To if either the frontend or the backend |
| 723 | * asks for it. |
| 724 | */ |
| 725 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Willy Tarreau | d68ff01 | 2022-05-27 08:57:21 +0200 | [diff] [blame] | 726 | const struct sockaddr_storage *dst = sc_dst(s->scf); |
Tim Duesterhus | e502c3e | 2022-03-05 00:52:42 +0100 | [diff] [blame] | 727 | 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] | 728 | |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 729 | if (dst && dst->ss_family == AF_INET) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 730 | /* Add an X-Original-To header unless the destination IP is |
| 731 | * in the 'except' network range. |
| 732 | */ |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 733 | if (ipcmp2net(dst, &sess->fe->except_xot_net) && |
| 734 | ipcmp2net(dst, &s->be->except_xot_net)) { |
| 735 | unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)dst)->sin_addr; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 736 | |
| 737 | /* Note: we rely on the backend to get the header name to be used for |
| 738 | * x-original-to, because the header is really meant for the backends. |
| 739 | * However, if the backend did not specify any option, we have to rely |
| 740 | * on the frontend's header name. |
| 741 | */ |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 742 | chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 743 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 744 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 745 | } |
| 746 | } |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 747 | else if (dst && dst->ss_family == AF_INET6) { |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 748 | /* Add an X-Original-To header unless the source IP is |
| 749 | * in the 'except' network range. |
| 750 | */ |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 751 | if (ipcmp2net(dst, &sess->fe->except_xot_net) && |
| 752 | ipcmp2net(dst, &s->be->except_xot_net)) { |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 753 | char pn[INET6_ADDRSTRLEN]; |
| 754 | |
| 755 | inet_ntop(AF_INET6, |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 756 | (const void *)&((struct sockaddr_in6 *)dst)->sin6_addr, |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 757 | pn, sizeof(pn)); |
| 758 | |
| 759 | /* Note: we rely on the backend to get the header name to be used for |
| 760 | * x-forwarded-for, because the header is really meant for the backends. |
| 761 | * However, if the backend did not specify any option, we have to rely |
| 762 | * on the frontend's header name. |
| 763 | */ |
| 764 | chunk_printf(&trash, "%s", pn); |
| 765 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 766 | goto return_fail_rewrite; |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 767 | } |
| 768 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 769 | } |
| 770 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 771 | /* Filter the request headers if there are filters attached to the |
| 772 | * stream. |
| 773 | */ |
| 774 | if (HAS_FILTERS(s)) |
| 775 | req->analysers |= AN_REQ_FLT_HTTP_HDRS; |
| 776 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 777 | /* If we have no server assigned yet and we're balancing on url_param |
| 778 | * with a POST request, we may be interested in checking the body for |
| 779 | * that parameter. This will be done in another analyser. |
| 780 | */ |
| 781 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | 089eaa0 | 2019-01-14 15:17:46 +0100 | [diff] [blame] | 782 | s->txn->meth == HTTP_METH_POST && |
| 783 | (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 784 | channel_dont_connect(req); |
| 785 | req->analysers |= AN_REQ_HTTP_BODY; |
| 786 | } |
| 787 | |
| 788 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 789 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 790 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 791 | /* We expect some data from the client. Unless we know for sure |
| 792 | * we already have a full request, we have to re-enable quick-ack |
| 793 | * in case we previously disabled it, otherwise we might cause |
| 794 | * the client to delay further data. |
| 795 | */ |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 796 | 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] | 797 | conn_set_quickack(cli_conn, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 798 | |
| 799 | /************************************************************* |
| 800 | * OK, that's finished for the headers. We have done what we * |
| 801 | * could. Let's switch to the DATA state. * |
| 802 | ************************************************************/ |
| 803 | req->analyse_exp = TICK_ETERNITY; |
| 804 | req->analysers &= ~an_bit; |
| 805 | |
| 806 | s->logs.tv_request = now; |
| 807 | /* OK let's go on with the BODY now */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 808 | 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] | 809 | return 1; |
| 810 | |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 811 | return_fail_rewrite: |
| 812 | if (!(s->flags & SF_ERR_MASK)) |
| 813 | s->flags |= SF_ERR_PRXCOND; |
| 814 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
| 815 | if (s->flags & SF_BE_ASSIGNED) |
| 816 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
| 817 | if (sess->listener && sess->listener->counters) |
| 818 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
| 819 | if (objt_server(s->target)) |
| 820 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
| 821 | /* fall through */ |
| 822 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 823 | return_int_err: |
| 824 | txn->status = 500; |
| 825 | if (!(s->flags & SF_ERR_MASK)) |
| 826 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 827 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 828 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 829 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 830 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 831 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 832 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 833 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 834 | http_set_term_flags(s); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 835 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 836 | DBG_TRACE_DEVEL("leaving on error", |
| 837 | 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] | 838 | return 0; |
| 839 | } |
| 840 | |
| 841 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 842 | * returns zero, at the beginning because it prevents any other processing |
| 843 | * from occurring, and at the end because it terminates the request. |
| 844 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 845 | 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] | 846 | { |
| 847 | struct http_txn *txn = s->txn; |
| 848 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 849 | 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] | 850 | /* This connection is being tarpitted. The CLIENT side has |
| 851 | * already set the connect expiration date to the right |
| 852 | * timeout. We just have to check that the client is still |
| 853 | * there and that the timeout has not expired. |
| 854 | */ |
| 855 | channel_dont_connect(req); |
Christopher Faulet | 4b490b7 | 2023-01-04 11:55:15 +0100 | [diff] [blame] | 856 | if (!(req->flags & CF_SHUTR) && |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 857 | !tick_is_expired(req->analyse_exp, now_ms)) { |
Christopher Faulet | b0c87f1 | 2021-10-29 14:37:07 +0200 | [diff] [blame] | 858 | /* Be sure to drain all data from the request channel */ |
| 859 | channel_htx_erase(req, htxbuf(&req->buf)); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 860 | DBG_TRACE_DEVEL("waiting for tarpit timeout expiry", |
| 861 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 862 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 863 | } |
| 864 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 865 | |
| 866 | /* We will set the queue timer to the time spent, just for |
| 867 | * logging purposes. We fake a 500 server error, so that the |
| 868 | * attacker will not suspect his connection has been tarpitted. |
| 869 | * It will not cause trouble to the logs because we can exclude |
| 870 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 871 | */ |
| 872 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 873 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 874 | http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL)); |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 875 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 876 | |
| 877 | 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] | 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 882 | * for either the buffer to be full, or the full advertised contents to have |
| 883 | * reached the buffer. It must only be called after the standard HTTP request |
| 884 | * processing has occurred, because it expects the request to be parsed and will |
| 885 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 886 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 887 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 888 | * needs to read more data, or 1 once it has completed its analysis. |
| 889 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 890 | 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] | 891 | { |
| 892 | struct session *sess = s->sess; |
| 893 | struct http_txn *txn = s->txn; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 894 | |
Christopher Faulet | f4569bb | 2023-01-13 10:49:31 +0100 | [diff] [blame] | 895 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &s->txn->req); |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 896 | |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 897 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 898 | switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) { |
| 899 | case HTTP_RULE_RES_CONT: |
| 900 | goto http_end; |
| 901 | case HTTP_RULE_RES_YIELD: |
| 902 | goto missing_data_or_waiting; |
| 903 | case HTTP_RULE_RES_BADREQ: |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 904 | goto return_bad_req; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 905 | case HTTP_RULE_RES_ERROR: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 906 | goto return_int_err; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 907 | case HTTP_RULE_RES_ABRT: |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 908 | goto return_prx_cond; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 909 | default: |
| 910 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | http_end: |
| 914 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 915 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 916 | req->analysers &= ~an_bit; |
| 917 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 918 | 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] | 919 | return 1; |
| 920 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 921 | missing_data_or_waiting: |
| 922 | channel_dont_connect(req); |
| 923 | DBG_TRACE_DEVEL("waiting for more data", |
| 924 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 925 | return 0; |
| 926 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 927 | return_int_err: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 928 | txn->status = 500; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 929 | if (!(s->flags & SF_ERR_MASK)) |
| 930 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 931 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 932 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 933 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 934 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 935 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 936 | goto return_prx_err; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 937 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 938 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 939 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 940 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 941 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 942 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 943 | /* fall through */ |
| 944 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 945 | return_prx_err: |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 946 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 947 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 948 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 949 | return_prx_cond: |
Christopher Faulet | f0d80df | 2023-01-13 10:20:20 +0100 | [diff] [blame] | 950 | http_set_term_flags(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 951 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 952 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 953 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 954 | DBG_TRACE_DEVEL("leaving on error", |
| 955 | 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] | 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | /* This function is an analyser which forwards request body (including chunk |
| 960 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 961 | * zero byte. The only situation where it must not be called is when we're in |
| 962 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 963 | * remaining data and to resync after end of body. It expects the msg_state to |
| 964 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 965 | * read more data, or 1 once we can go on with next request or end the stream. |
| 966 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
| 967 | * bytes of pending data + the headers if not already done. |
| 968 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 969 | 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] | 970 | { |
| 971 | struct session *sess = s->sess; |
| 972 | struct http_txn *txn = s->txn; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 973 | struct http_msg *msg = &txn->req; |
| 974 | struct htx *htx; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 975 | short status = 0; |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 976 | int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 977 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 978 | 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] | 979 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 980 | htx = htxbuf(&req->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 981 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 982 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 983 | goto return_bad_req; |
| 984 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 985 | goto return_int_err; |
| 986 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 987 | /* Note that we don't have to send 100-continue back because we don't |
| 988 | * need the data to complete our job, and it's up to the server to |
| 989 | * decide whether to return 100, 417 or anything else in return of |
| 990 | * an "Expect: 100-continue" header. |
| 991 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 992 | if (msg->msg_state == HTTP_MSG_BODY) |
| 993 | msg->msg_state = HTTP_MSG_DATA; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 994 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 995 | /* in most states, we should abort in case of early close */ |
| 996 | channel_auto_close(req); |
| 997 | |
| 998 | if (req->to_forward) { |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 999 | if (req->to_forward == CHN_INFINITE_FORWARD) { |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1000 | if (req->flags & CF_EOI) |
| 1001 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 1002 | } |
| 1003 | else { |
| 1004 | /* We can't process the buffer's contents yet */ |
| 1005 | req->flags |= CF_WAKE_WRITE; |
| 1006 | goto missing_data_or_waiting; |
| 1007 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1008 | } |
| 1009 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1010 | if (msg->msg_state >= HTTP_MSG_ENDING) |
| 1011 | goto ending; |
| 1012 | |
| 1013 | if (txn->meth == HTTP_METH_CONNECT) { |
| 1014 | msg->msg_state = HTTP_MSG_ENDING; |
| 1015 | goto ending; |
| 1016 | } |
| 1017 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1018 | /* Forward input data. We get it by removing all outgoing data not |
| 1019 | * forwarded yet from HTX data size. If there are some data filters, we |
| 1020 | * let them decide the amount of data to forward. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1021 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1022 | if (HAS_REQ_DATA_FILTERS(s)) { |
| 1023 | ret = flt_http_payload(s, msg, htx->data); |
| 1024 | if (ret < 0) |
| 1025 | goto return_bad_req; |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1026 | c_adv(req, ret); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1027 | } |
| 1028 | else { |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1029 | c_adv(req, htx->data - co_data(req)); |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 1030 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
| 1031 | channel_htx_forward_forever(req, htx); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1032 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1033 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1034 | if (htx->data != co_data(req)) |
| 1035 | goto missing_data_or_waiting; |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1036 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1037 | /* 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] | 1038 | * in HTTP_MSG_ENDING state. Then if all data was marked to be |
| 1039 | * forwarded, set the state to HTTP_MSG_DONE. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1040 | */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 1041 | if (!(htx->flags & HTX_FL_EOM)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1042 | goto missing_data_or_waiting; |
| 1043 | |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1044 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1045 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1046 | ending: |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 1047 | req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */ |
| 1048 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1049 | /* other states, ENDING...TUNNEL */ |
| 1050 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 1051 | goto done; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1052 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1053 | if (HAS_REQ_DATA_FILTERS(s)) { |
| 1054 | ret = flt_http_end(s, msg); |
| 1055 | if (ret <= 0) { |
| 1056 | if (!ret) |
| 1057 | goto missing_data_or_waiting; |
| 1058 | goto return_bad_req; |
| 1059 | } |
| 1060 | } |
| 1061 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1062 | if (txn->meth == HTTP_METH_CONNECT) |
| 1063 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 1064 | else { |
| 1065 | msg->msg_state = HTTP_MSG_DONE; |
| 1066 | req->to_forward = 0; |
| 1067 | } |
| 1068 | |
| 1069 | done: |
| 1070 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 1071 | if (!(txn->flags & TX_CON_WANT_TUN)) |
| 1072 | channel_dont_close(req); |
| 1073 | |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 1074 | if ((req->flags & CF_SHUTW) && co_data(req)) { |
| 1075 | /* request errors are most likely due to the server aborting the |
| 1076 | * transfer. */ |
| 1077 | goto return_srv_abort; |
| 1078 | } |
| 1079 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1080 | http_end_request(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1081 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1082 | 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] | 1083 | return 1; |
| 1084 | } |
| 1085 | |
| 1086 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 1087 | * the client's connection and forward any shutdown notification to the |
| 1088 | * server, which will decide whether to close or to go on processing the |
| 1089 | * request. We only do that in tunnel mode, and not in other modes since |
| 1090 | * it can be abused to exhaust source ports. */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 1091 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1092 | channel_auto_read(req); |
Christopher Faulet | 446d803 | 2022-12-12 07:53:18 +0100 | [diff] [blame] | 1093 | if ((req->flags & CF_SHUTR) && !(txn->flags & TX_CON_WANT_TUN)) |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1094 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1095 | channel_auto_close(req); |
| 1096 | } |
| 1097 | else if (s->txn->meth == HTTP_METH_POST) { |
| 1098 | /* POST requests may require to read extra CRLF sent by broken |
| 1099 | * browsers and which could cause an RST to be sent upon close |
| 1100 | * on some systems (eg: Linux). */ |
| 1101 | channel_auto_read(req); |
| 1102 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1103 | DBG_TRACE_DEVEL("waiting for the end of the HTTP txn", |
| 1104 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1105 | return 0; |
| 1106 | |
| 1107 | missing_data_or_waiting: |
| 1108 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1109 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1110 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1111 | |
| 1112 | waiting: |
| 1113 | /* waiting for the last bits to leave the buffer */ |
| 1114 | if (req->flags & CF_SHUTW) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1115 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1116 | |
| 1117 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 1118 | * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE. |
| 1119 | * And when content-length is used, we never want to let the possible |
| 1120 | * shutdown be forwarded to the other side, as the state machine will |
| 1121 | * take care of it once the client responds. It's also important to |
| 1122 | * prevent TIME_WAITs from accumulating on the backend side, and for |
| 1123 | * HTTP/2 where the last frame comes with a shutdown. |
| 1124 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1125 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1126 | channel_dont_close(req); |
| 1127 | |
| 1128 | /* We know that more data are expected, but we couldn't send more that |
| 1129 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
| 1130 | * system knows it must not set a PUSH on this first part. Interactive |
| 1131 | * modes are already handled by the stream sock layer. We must not do |
| 1132 | * this in content-length mode because it could present the MSG_MORE |
| 1133 | * flag with the last block of forwarded data, which would cause an |
| 1134 | * additional delay to be observed by the receiver. |
| 1135 | */ |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 1136 | if (HAS_REQ_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1137 | req->flags |= CF_EXPECT_MORE; |
| 1138 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1139 | DBG_TRACE_DEVEL("waiting for more data to forward", |
| 1140 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1141 | return 0; |
| 1142 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1143 | return_cli_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1144 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 1145 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1146 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1147 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1148 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1149 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1150 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 1151 | s->flags |= ((req->flags & CF_READ_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1152 | status = 400; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1153 | goto return_prx_cond; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1154 | |
| 1155 | return_srv_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1156 | _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts); |
| 1157 | _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1158 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1159 | _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1160 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1161 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1162 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 1163 | s->flags |= ((req->flags & CF_WRITE_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1164 | status = 502; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1165 | goto return_prx_cond; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1166 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1167 | return_int_err: |
| 1168 | if (!(s->flags & SF_ERR_MASK)) |
| 1169 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1170 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 1171 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1172 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1173 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1174 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1175 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1176 | status = 500; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1177 | goto return_prx_cond; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1178 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1179 | return_bad_req: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1180 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1181 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1182 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1183 | status = 400; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1184 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1185 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1186 | return_prx_cond: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1187 | if (txn->status > 0) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1188 | /* 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] | 1189 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1190 | } else { |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1191 | txn->status = status; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1192 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1193 | } |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 1194 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1195 | DBG_TRACE_DEVEL("leaving on error ", |
| 1196 | 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] | 1197 | return 0; |
| 1198 | } |
| 1199 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1200 | /* 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] | 1201 | /* Returns 0 if we can attempt to retry, -1 otherwise */ |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 1202 | static __inline int do_l7_retry(struct stream *s, struct stconn *sc) |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1203 | { |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1204 | struct channel *req, *res; |
| 1205 | int co_data; |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1206 | |
Christopher Faulet | 731c8e6 | 2022-03-29 16:08:44 +0200 | [diff] [blame] | 1207 | s->conn_retries++; |
| 1208 | if (s->conn_retries >= s->be->conn_retries) |
Christopher Faulet | 552601d | 2021-05-26 10:31:06 +0200 | [diff] [blame] | 1209 | return -1; |
Christopher Faulet | 5b82cc5 | 2020-10-12 15:18:50 +0200 | [diff] [blame] | 1210 | |
Christopher Faulet | e763c8c | 2021-05-05 18:23:59 +0200 | [diff] [blame] | 1211 | if (objt_server(s->target)) { |
| 1212 | if (s->flags & SF_CURR_SESS) { |
| 1213 | s->flags &= ~SF_CURR_SESS; |
| 1214 | _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess); |
| 1215 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1216 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries); |
Christopher Faulet | e763c8c | 2021-05-05 18:23:59 +0200 | [diff] [blame] | 1217 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1218 | _HA_ATOMIC_INC(&s->be->be_counters.retries); |
Willy Tarreau | 223995e | 2019-05-04 10:38:31 +0200 | [diff] [blame] | 1219 | |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1220 | req = &s->req; |
| 1221 | res = &s->res; |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1222 | /* Remove any write error from the request, and read error from the response */ |
| 1223 | req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW); |
Christopher Faulet | 6e1bbc4 | 2022-12-12 08:08:15 +0100 | [diff] [blame] | 1224 | res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_EVENT | CF_SHUTR_NOW); |
Christopher Faulet | 7bf46bb | 2022-01-04 10:56:03 +0100 | [diff] [blame] | 1225 | res->analysers &= AN_RES_FLT_END; |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 1226 | s->conn_err_type = STRM_ET_NONE; |
Christopher Faulet | ae024ce | 2022-03-29 19:02:31 +0200 | [diff] [blame] | 1227 | s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK); |
| 1228 | s->conn_exp = TICK_ETERNITY; |
Olivier Houchard | 4bd5867 | 2019-07-12 16:16:59 +0200 | [diff] [blame] | 1229 | stream_choose_redispatch(s); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1230 | res->rex = TICK_ETERNITY; |
| 1231 | res->to_forward = 0; |
| 1232 | res->analyse_exp = TICK_ETERNITY; |
| 1233 | res->total = 0; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 1234 | |
Willy Tarreau | 19c65a9 | 2022-05-27 08:49:24 +0200 | [diff] [blame] | 1235 | if (sc_reset_endp(s->scb) < 0) { |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 1236 | if (!(s->flags & SF_ERR_MASK)) |
| 1237 | s->flags |= SF_ERR_INTERNAL; |
| 1238 | return -1; |
| 1239 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1240 | |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1241 | b_free(&req->buf); |
| 1242 | /* Swap the L7 buffer with the channel buffer */ |
| 1243 | /* 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] | 1244 | co_data = b_data(&s->txn->l7_buffer); |
| 1245 | b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer)); |
| 1246 | 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] | 1247 | co_set_data(req, co_data); |
Christopher Faulet | 5b82cc5 | 2020-10-12 15:18:50 +0200 | [diff] [blame] | 1248 | |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 1249 | 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] | 1250 | |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1251 | b_reset(&res->buf); |
| 1252 | co_set_data(res, 0); |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1256 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 1257 | * processing can continue on next analysers, or zero if it either needs more |
| 1258 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 1259 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers |
| 1260 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1261 | * abort. |
| 1262 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1263 | 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] | 1264 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1265 | /* |
| 1266 | * We will analyze a complete HTTP response to check the its syntax. |
| 1267 | * |
| 1268 | * Once the start line and all headers are received, we may perform a |
| 1269 | * capture of the error (if any), and we will set a few fields. We also |
| 1270 | * logging and finally headers capture. |
| 1271 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1272 | struct session *sess = s->sess; |
| 1273 | struct http_txn *txn = s->txn; |
| 1274 | struct http_msg *msg = &txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1275 | struct htx *htx; |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1276 | struct connection *srv_conn; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1277 | struct htx_sl *sl; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1278 | int n; |
| 1279 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1280 | 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] | 1281 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1282 | htx = htxbuf(&rep->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1283 | |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1284 | /* Parsing errors are caught here */ |
| 1285 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 1286 | goto return_bad_res; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1287 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 1288 | goto return_int_err; |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1289 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1290 | /* |
| 1291 | * Now we quickly check if we have found a full valid response. |
| 1292 | * If not so, we check the FD and buffer states before leaving. |
| 1293 | * A full response is indicated by the fact that we have seen |
| 1294 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
| 1295 | * responses are checked first. |
| 1296 | * |
| 1297 | * Depending on whether the client is still there or not, we |
| 1298 | * may send an error response back or not. Note that normally |
| 1299 | * we should only check for HTTP status there, and check I/O |
| 1300 | * errors somewhere else. |
| 1301 | */ |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1302 | next_one: |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 1303 | if (unlikely(htx_is_empty(htx) || htx->first == -1)) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1304 | /* 1: have we encountered a read error ? */ |
| 1305 | if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 1306 | struct connection *conn = sc_conn(s->scb); |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1307 | |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1308 | /* Perform a L7 retry because server refuses the early data. */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1309 | if ((txn->flags & TX_L7_RETRY) && |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1310 | (s->be->retry_type & PR_RE_EARLY_ERROR) && |
| 1311 | conn && conn->err_code == CO_ER_SSL_EARLY_FAILED && |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1312 | do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1313 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1314 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
Olivier Houchard | 6db1699 | 2019-05-17 15:40:49 +0200 | [diff] [blame] | 1318 | if (txn->flags & TX_NOT_FIRST) |
| 1319 | goto abort_keep_alive; |
| 1320 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1321 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1322 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1323 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1324 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1325 | } |
| 1326 | |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1327 | /* if the server refused the early data, just send a 425 */ |
| 1328 | if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1329 | txn->status = 425; |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1330 | else { |
| 1331 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1332 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1333 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1334 | |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1335 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1336 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1337 | |
| 1338 | if (!(s->flags & SF_ERR_MASK)) |
| 1339 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1340 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1341 | DBG_TRACE_DEVEL("leaving on error", |
| 1342 | 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] | 1343 | return 0; |
| 1344 | } |
| 1345 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1346 | /* 2: read timeout : return a 504 to the client. */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1347 | else if (rep->flags & CF_READ_TIMEOUT) { |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1348 | if ((txn->flags & TX_L7_RETRY) && |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1349 | (s->be->retry_type & PR_RE_TIMEOUT)) { |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1350 | if (co_data(rep) || do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1351 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1352 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1353 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1354 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1355 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1356 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1357 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1358 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1359 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1360 | } |
| 1361 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1362 | txn->status = 504; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1363 | stream_inc_http_fail_ctr(s); |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1364 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1365 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1366 | |
| 1367 | if (!(s->flags & SF_ERR_MASK)) |
| 1368 | s->flags |= SF_ERR_SRVTO; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1369 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1370 | DBG_TRACE_DEVEL("leaving on error", |
| 1371 | 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] | 1372 | return 0; |
| 1373 | } |
| 1374 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1375 | /* 3: client abort with an abortonclose */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1376 | 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] | 1377 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 1378 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1379 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1380 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1381 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1382 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1383 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1384 | txn->status = 400; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1385 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1386 | |
| 1387 | if (!(s->flags & SF_ERR_MASK)) |
| 1388 | s->flags |= SF_ERR_CLICL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1389 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1390 | |
| 1391 | /* process_stream() will take care of the error */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1392 | DBG_TRACE_DEVEL("leaving on error", |
| 1393 | 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] | 1394 | return 0; |
| 1395 | } |
| 1396 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1397 | /* 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] | 1398 | else if (rep->flags & CF_SHUTR) { |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1399 | if ((txn->flags & TX_L7_RETRY) && |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1400 | (s->be->retry_type & PR_RE_DISCONNECTED)) { |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1401 | if (co_data(rep) || do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1402 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1403 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1404 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1405 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1406 | } |
| 1407 | |
Olivier Houchard | 6db1699 | 2019-05-17 15:40:49 +0200 | [diff] [blame] | 1408 | if (txn->flags & TX_NOT_FIRST) |
| 1409 | goto abort_keep_alive; |
| 1410 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1411 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1412 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1413 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1414 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1415 | } |
| 1416 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1417 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1418 | stream_inc_http_fail_ctr(s); |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1419 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1420 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1421 | |
| 1422 | if (!(s->flags & SF_ERR_MASK)) |
| 1423 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1424 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1425 | DBG_TRACE_DEVEL("leaving on error", |
| 1426 | 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] | 1427 | return 0; |
| 1428 | } |
| 1429 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1430 | /* 5: write error to client (we don't send any message then) */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1431 | else if (rep->flags & CF_WRITE_ERROR) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1432 | if (txn->flags & TX_NOT_FIRST) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1433 | goto abort_keep_alive; |
| 1434 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1435 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1436 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1437 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1438 | rep->analysers &= AN_RES_FLT_END; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1439 | |
| 1440 | if (!(s->flags & SF_ERR_MASK)) |
| 1441 | s->flags |= SF_ERR_CLICL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1442 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1443 | |
| 1444 | /* process_stream() will take care of the error */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1445 | DBG_TRACE_DEVEL("leaving on error", |
| 1446 | 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] | 1447 | return 0; |
| 1448 | } |
| 1449 | |
| 1450 | channel_dont_close(rep); |
| 1451 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1452 | DBG_TRACE_DEVEL("waiting for more data", |
| 1453 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1454 | return 0; |
| 1455 | } |
| 1456 | |
| 1457 | /* More interesting part now : we know that we have a complete |
| 1458 | * response which at least looks like HTTP. We have an indicator |
| 1459 | * of each header's length, so we can parse them quickly. |
| 1460 | */ |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 1461 | BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1462 | sl = http_get_stline(htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1463 | |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1464 | /* Perform a L7 retry because of the status code */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1465 | if ((txn->flags & TX_L7_RETRY) && |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1466 | l7_status_match(s->be, sl->info.res.status) && |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1467 | do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1468 | DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 1469 | return 0; |
| 1470 | } |
| 1471 | |
| 1472 | /* Now, L7 buffer is useless, it can be released */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1473 | b_free(&txn->l7_buffer); |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1474 | |
| 1475 | msg->msg_state = HTTP_MSG_BODY; |
| 1476 | |
| 1477 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1478 | /* 0: we might have to print this header in debug mode */ |
| 1479 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1480 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 1481 | int32_t pos; |
| 1482 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1483 | http_debug_stline("srvrep", s, sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1484 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 1485 | 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] | 1486 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 1487 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 1488 | |
| 1489 | if (type == HTX_BLK_EOH) |
| 1490 | break; |
| 1491 | if (type != HTX_BLK_HDR) |
| 1492 | continue; |
| 1493 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1494 | http_debug_hdr("srvhdr", s, |
| 1495 | htx_get_blk_name(htx, blk), |
| 1496 | htx_get_blk_value(htx, blk)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1497 | } |
| 1498 | } |
| 1499 | |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1500 | /* 1: get the status code and the version. Also set HTTP flags */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1501 | txn->status = sl->info.res.status; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1502 | if (sl->flags & HTX_SL_F_VER_11) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1503 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1504 | if (sl->flags & HTX_SL_F_XFER_LEN) { |
| 1505 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 1506 | if (sl->flags & HTX_SL_F_CLEN) |
| 1507 | msg->flags |= HTTP_MSGF_CNT_LEN; |
| 1508 | else if (sl->flags & HTX_SL_F_CHNK) |
| 1509 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1510 | } |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 1511 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 1512 | msg->flags |= HTTP_MSGF_BODYLESS; |
Christopher Faulet | 576c358 | 2021-01-08 15:53:01 +0100 | [diff] [blame] | 1513 | if (sl->flags & HTX_SL_F_CONN_UPG) |
| 1514 | msg->flags |= HTTP_MSGF_CONN_UPG; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1515 | |
| 1516 | n = txn->status / 100; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1517 | if (n < 1 || n > 5) |
| 1518 | n = 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1519 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1520 | /* when the client triggers a 4xx from the server, it's most often due |
| 1521 | * to a missing object or permission. These events should be tracked |
| 1522 | * because if they happen often, it may indicate a brute force or a |
| 1523 | * vulnerability scan. |
| 1524 | */ |
| 1525 | if (n == 4) |
| 1526 | stream_inc_http_err_ctr(s); |
| 1527 | |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1528 | if (n == 5 && txn->status != 501 && txn->status != 505) |
| 1529 | stream_inc_http_fail_ctr(s); |
| 1530 | |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 1531 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1532 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]); |
| 1533 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req); |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 1534 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1535 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1536 | /* Adjust server's health based on status code. Note: status codes 501 |
| 1537 | * and 505 are triggered on demand by client request, so we must not |
| 1538 | * count them as server failures. |
| 1539 | */ |
| 1540 | if (objt_server(s->target)) { |
| 1541 | 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] | 1542 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1543 | else |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1544 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | /* |
| 1548 | * We may be facing a 100-continue response, or any other informational |
| 1549 | * 1xx response which is non-final, in which case this is not the right |
| 1550 | * response, and we're waiting for the next one. Let's allow this response |
| 1551 | * to go to the client and wait for the next one. There's an exception for |
| 1552 | * 101 which is used later in the code to switch protocols. |
| 1553 | */ |
| 1554 | if (txn->status < 200 && |
| 1555 | (txn->status == 100 || txn->status >= 102)) { |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1556 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1557 | htx->first = channel_htx_fwd_headers(rep, htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1558 | msg->msg_state = HTTP_MSG_RPBEFORE; |
Christopher Faulet | 3499f62 | 2019-09-03 15:23:54 +0200 | [diff] [blame] | 1559 | msg->flags = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1560 | txn->status = 0; |
| 1561 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 7d51845 | 2020-08-31 11:07:07 +0200 | [diff] [blame] | 1562 | rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */ |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1563 | goto next_one; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1564 | } |
| 1565 | |
Christopher Faulet | 6e6c7b1 | 2021-01-08 16:02:05 +0100 | [diff] [blame] | 1566 | /* A 101-switching-protocols must contains a Connection header with the |
| 1567 | * "upgrade" option and the request too. It means both are agree to |
| 1568 | * upgrade. It is not so strict because there is no test on the Upgrade |
| 1569 | * header content. But it is probably stronger enough for now. |
| 1570 | */ |
| 1571 | if (txn->status == 101 && |
| 1572 | (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG))) |
| 1573 | goto return_bad_res; |
| 1574 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1575 | /* |
| 1576 | * 2: check for cacheability. |
| 1577 | */ |
| 1578 | |
| 1579 | switch (txn->status) { |
| 1580 | case 200: |
| 1581 | case 203: |
| 1582 | case 204: |
| 1583 | case 206: |
| 1584 | case 300: |
| 1585 | case 301: |
| 1586 | case 404: |
| 1587 | case 405: |
| 1588 | case 410: |
| 1589 | case 414: |
| 1590 | case 501: |
| 1591 | break; |
| 1592 | default: |
| 1593 | /* RFC7231#6.1: |
| 1594 | * Responses with status codes that are defined as |
| 1595 | * cacheable by default (e.g., 200, 203, 204, 206, |
| 1596 | * 300, 301, 404, 405, 410, 414, and 501 in this |
| 1597 | * specification) can be reused by a cache with |
| 1598 | * heuristic expiration unless otherwise indicated |
| 1599 | * by the method definition or explicit cache |
| 1600 | * controls [RFC7234]; all other status codes are |
| 1601 | * not cacheable by default. |
| 1602 | */ |
| 1603 | txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK); |
| 1604 | break; |
| 1605 | } |
| 1606 | |
| 1607 | /* |
| 1608 | * 3: we may need to capture headers |
| 1609 | */ |
| 1610 | s->logs.logwait &= ~LW_RESP; |
| 1611 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1612 | http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1613 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1614 | /* Skip parsing if no content length is possible. */ |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 1615 | 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] | 1616 | txn->status == 101)) { |
| 1617 | /* Either we've established an explicit tunnel, or we're |
| 1618 | * switching the protocol. In both cases, we're very unlikely |
| 1619 | * to understand the next protocols. We have to switch to tunnel |
| 1620 | * mode, so that we transfer the request and responses then let |
| 1621 | * this protocol pass unmodified. When we later implement specific |
| 1622 | * parsers for such protocols, we'll want to check the Upgrade |
| 1623 | * header which contains information about that protocol for |
| 1624 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 1625 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 1626 | txn->flags |= TX_CON_WANT_TUN; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1627 | } |
| 1628 | |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1629 | /* check for NTML authentication headers in 401 (WWW-Authenticate) and |
| 1630 | * 407 (Proxy-Authenticate) responses and set the connection to private |
| 1631 | */ |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 1632 | srv_conn = sc_conn(s->scb); |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1633 | if (srv_conn) { |
| 1634 | struct ist hdr; |
| 1635 | struct http_hdr_ctx ctx; |
| 1636 | |
| 1637 | if (txn->status == 401) |
| 1638 | hdr = ist("WWW-Authenticate"); |
| 1639 | else if (txn->status == 407) |
| 1640 | hdr = ist("Proxy-Authenticate"); |
| 1641 | else |
| 1642 | goto end; |
| 1643 | |
| 1644 | ctx.blk = NULL; |
| 1645 | while (http_find_header(htx, hdr, &ctx, 0)) { |
Willy Tarreau | f1dcced | 2020-05-07 19:27:02 +0200 | [diff] [blame] | 1646 | /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM", |
| 1647 | * possibly followed by blanks and a base64 string, the connection |
| 1648 | * is private. Since it's a mess to deal with, we only check for |
| 1649 | * values starting with "NTLM" or "Nego". Note that often multiple |
| 1650 | * headers are sent by the server there. |
| 1651 | */ |
| 1652 | if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) || |
Willy Tarreau | 49a1d28 | 2020-05-07 19:10:15 +0200 | [diff] [blame] | 1653 | (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) { |
Olivier Houchard | 250031e | 2019-05-29 15:01:50 +0200 | [diff] [blame] | 1654 | sess->flags |= SESS_FL_PREFER_LAST; |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 1655 | conn_set_owner(srv_conn, sess, NULL); |
Christopher Faulet | 21ddc74 | 2020-07-01 15:26:14 +0200 | [diff] [blame] | 1656 | conn_set_private(srv_conn); |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 1657 | /* 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] | 1658 | session_add_conn(srv_conn->owner, srv_conn, srv_conn->target); |
Willy Tarreau | f1dcced | 2020-05-07 19:27:02 +0200 | [diff] [blame] | 1659 | break; |
Olivier Houchard | 250031e | 2019-05-29 15:01:50 +0200 | [diff] [blame] | 1660 | } |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | end: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1665 | /* we want to have the response time before we start processing it */ |
| 1666 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 1667 | |
| 1668 | /* end of job, return OK */ |
| 1669 | rep->analysers &= ~an_bit; |
| 1670 | rep->analyse_exp = TICK_ETERNITY; |
| 1671 | channel_auto_close(rep); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1672 | 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] | 1673 | return 1; |
| 1674 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1675 | return_int_err: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1676 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 1677 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1678 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1679 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1680 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1681 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1682 | txn->status = 500; |
| 1683 | if (!(s->flags & SF_ERR_MASK)) |
| 1684 | s->flags |= SF_ERR_INTERNAL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1685 | goto return_prx_cond; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1686 | |
| 1687 | return_bad_res: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1688 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1689 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1690 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1691 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1692 | } |
Olivier Houchard | e3249a9 | 2019-05-03 23:01:47 +0200 | [diff] [blame] | 1693 | if ((s->be->retry_type & PR_RE_JUNK_REQUEST) && |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1694 | (txn->flags & TX_L7_RETRY) && |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1695 | do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1696 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1697 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | e3249a9 | 2019-05-03 23:01:47 +0200 | [diff] [blame] | 1698 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1699 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1700 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1701 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1702 | /* fall through */ |
| 1703 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1704 | return_prx_cond: |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1705 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1706 | http_set_term_flags(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1707 | |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1708 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1709 | DBG_TRACE_DEVEL("leaving on error", |
| 1710 | 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] | 1711 | return 0; |
| 1712 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1713 | abort_keep_alive: |
| 1714 | /* A keep-alive request to the server failed on a network error. |
| 1715 | * The client is required to retry. We need to close without returning |
| 1716 | * any other information so that the client retries. |
| 1717 | */ |
| 1718 | txn->status = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1719 | s->logs.logwait = 0; |
| 1720 | s->logs.level = 0; |
| 1721 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1722 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1723 | DBG_TRACE_DEVEL("leaving by closing K/A connection", |
| 1724 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1725 | return 0; |
| 1726 | } |
| 1727 | |
| 1728 | /* This function performs all the processing enabled for the current response. |
| 1729 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 1730 | * and updates s->res.analysers. It might make sense to explode it into several |
| 1731 | * other functions. It works like process_request (see indications above). |
| 1732 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1733 | 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] | 1734 | { |
| 1735 | struct session *sess = s->sess; |
| 1736 | struct http_txn *txn = s->txn; |
| 1737 | struct http_msg *msg = &txn->rsp; |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1738 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1739 | struct proxy *cur_proxy; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1740 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 1741 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1742 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 1743 | return 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1744 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1745 | 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] | 1746 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1747 | htx = htxbuf(&rep->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1748 | |
| 1749 | /* The stats applet needs to adjust the Connection header but we don't |
| 1750 | * apply any filter there. |
| 1751 | */ |
| 1752 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 1753 | rep->analysers &= ~an_bit; |
| 1754 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1755 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | /* |
| 1759 | * We will have to evaluate the filters. |
| 1760 | * As opposed to version 1.2, now they will be evaluated in the |
| 1761 | * filters order and not in the header order. This means that |
| 1762 | * each filter has to be validated among all headers. |
| 1763 | * |
| 1764 | * Filters are tried with ->be first, then with ->fe if it is |
| 1765 | * different from ->be. |
| 1766 | * |
| 1767 | * Maybe we are in resume condiion. In this case I choose the |
| 1768 | * "struct proxy" which contains the rule list matching the resume |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1769 | * pointer. If none of these "struct proxy" match, I initialise |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1770 | * the process with the first one. |
| 1771 | * |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1772 | * In fact, I check only correspondence between the current list |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1773 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 1774 | * the loop with the ->be. |
| 1775 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1776 | if (s->current_rule_list == &sess->fe->http_res_rules || |
| 1777 | (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] | 1778 | cur_proxy = sess->fe; |
| 1779 | else |
| 1780 | cur_proxy = s->be; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1781 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1782 | while (1) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1783 | /* evaluate http-response rules */ |
Christopher Faulet | 46f46df | 2021-11-09 16:33:25 +0100 | [diff] [blame] | 1784 | if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1785 | struct list *def_rules, *rules; |
| 1786 | |
| 1787 | def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL); |
| 1788 | rules = &cur_proxy->http_res_rules; |
| 1789 | |
| 1790 | ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1791 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1792 | switch (ret) { |
| 1793 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 1794 | goto return_prx_yield; |
| 1795 | |
| 1796 | case HTTP_RULE_RES_CONT: |
| 1797 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 1798 | break; |
| 1799 | |
| 1800 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 1801 | goto deny; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1802 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1803 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent */ |
| 1804 | goto return_prx_cond; |
| 1805 | |
| 1806 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1807 | goto done; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1808 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1809 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 1810 | goto return_bad_res; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1811 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1812 | case HTTP_RULE_RES_ERROR: /* failed with a bad request */ |
| 1813 | goto return_int_err; |
| 1814 | } |
| 1815 | |
| 1816 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1817 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1818 | /* check whether we're already working on the frontend */ |
| 1819 | if (cur_proxy == sess->fe) |
| 1820 | break; |
| 1821 | cur_proxy = sess->fe; |
| 1822 | } |
| 1823 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1824 | /* OK that's all we can do for 1xx responses */ |
| 1825 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1826 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1827 | |
| 1828 | /* |
| 1829 | * Now check for a server cookie. |
| 1830 | */ |
| 1831 | 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] | 1832 | http_manage_server_side_cookies(s, rep); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1833 | |
| 1834 | /* |
| 1835 | * Check for cache-control or pragma headers if required. |
| 1836 | */ |
| 1837 | 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] | 1838 | http_check_response_for_cacheability(s, rep); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1839 | |
| 1840 | /* |
| 1841 | * Add server cookie in the response if needed |
| 1842 | */ |
| 1843 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 1844 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
| 1845 | (!(s->flags & SF_DIRECT) || |
| 1846 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 1847 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 1848 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 1849 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 1850 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 1851 | !(s->flags & SF_IGNORE_PRST)) { |
| 1852 | /* the server is known, it's not the one the client requested, or the |
| 1853 | * cookie's last seen date needs to be refreshed. We have to |
| 1854 | * insert a set-cookie here, except if we want to insert only on POST |
| 1855 | * requests and this one isn't. Note that servers which don't have cookies |
| 1856 | * (eg: some backup servers) will return a full cookie removal request. |
| 1857 | */ |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1858 | if (!__objt_server(s->target)->cookie) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1859 | chunk_printf(&trash, |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1860 | "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1861 | s->be->cookie_name); |
| 1862 | } |
| 1863 | else { |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1864 | 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] | 1865 | |
| 1866 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 1867 | /* emit last_date, which is mandatory */ |
| 1868 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 1869 | s30tob64((date.tv_sec+3) >> 2, |
| 1870 | trash.area + trash.data); |
| 1871 | trash.data += 5; |
| 1872 | |
| 1873 | if (s->be->cookie_maxlife) { |
| 1874 | /* emit first_date, which is either the original one or |
| 1875 | * the current date. |
| 1876 | */ |
| 1877 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 1878 | s30tob64(txn->cookie_first_date ? |
| 1879 | txn->cookie_first_date >> 2 : |
| 1880 | (date.tv_sec+3) >> 2, |
| 1881 | trash.area + trash.data); |
| 1882 | trash.data += 5; |
| 1883 | } |
| 1884 | } |
| 1885 | chunk_appendf(&trash, "; path=/"); |
| 1886 | } |
| 1887 | |
| 1888 | if (s->be->cookie_domain) |
| 1889 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
| 1890 | |
| 1891 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 1892 | chunk_appendf(&trash, "; HttpOnly"); |
| 1893 | |
| 1894 | if (s->be->ck_opts & PR_CK_SECURE) |
| 1895 | chunk_appendf(&trash, "; Secure"); |
| 1896 | |
Christopher Faulet | 2f53390 | 2020-01-21 11:06:48 +0100 | [diff] [blame] | 1897 | if (s->be->cookie_attrs) |
| 1898 | chunk_appendf(&trash, "; %s", s->be->cookie_attrs); |
| 1899 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1900 | 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] | 1901 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1902 | |
| 1903 | txn->flags &= ~TX_SCK_MASK; |
| 1904 | if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
| 1905 | /* the server did not change, only the date was updated */ |
| 1906 | txn->flags |= TX_SCK_UPDATED; |
| 1907 | else |
| 1908 | txn->flags |= TX_SCK_INSERTED; |
| 1909 | |
| 1910 | /* Here, we will tell an eventual cache on the client side that we don't |
| 1911 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 1912 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 1913 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 1914 | */ |
| 1915 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
| 1916 | |
| 1917 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 1918 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1919 | if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private")))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 1920 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | /* |
| 1925 | * Check if result will be cacheable with a cookie. |
| 1926 | * We'll block the response if security checks have caught |
| 1927 | * nasty things such as a cacheable cookie. |
| 1928 | */ |
| 1929 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 1930 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 1931 | (s->be->options & PR_O_CHK_CACHE)) { |
| 1932 | /* we're in presence of a cacheable response containing |
| 1933 | * a set-cookie header. We'll block it as requested by |
| 1934 | * the 'checkcache' option, and send an alert. |
| 1935 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1936 | 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] | 1937 | 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] | 1938 | send_log(s->be, LOG_ALERT, |
| 1939 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1940 | 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] | 1941 | goto deny; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1942 | } |
| 1943 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1944 | end: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 1945 | /* |
| 1946 | * Evaluate after-response rules before forwarding the response. rules |
| 1947 | * from the backend are evaluated first, then one from the frontend if |
| 1948 | * it differs. |
| 1949 | */ |
| 1950 | if (!http_eval_after_res_rules(s)) |
| 1951 | goto return_int_err; |
| 1952 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 1953 | /* Filter the response headers if there are filters attached to the |
| 1954 | * stream. |
| 1955 | */ |
| 1956 | if (HAS_FILTERS(s)) |
| 1957 | rep->analysers |= AN_RES_FLT_HTTP_HDRS; |
| 1958 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1959 | /* Always enter in the body analyzer */ |
| 1960 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 1961 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
| 1962 | |
| 1963 | /* if the user wants to log as soon as possible, without counting |
| 1964 | * bytes from the server, then this is the right moment. We have |
| 1965 | * to temporarily assign bytes_out to log what we currently have. |
| 1966 | */ |
| 1967 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
| 1968 | 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] | 1969 | s->logs.bytes_out = htx->data; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1970 | s->do_log(s); |
| 1971 | s->logs.bytes_out = 0; |
| 1972 | } |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1973 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1974 | done: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 1975 | 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] | 1976 | rep->analysers &= ~an_bit; |
| 1977 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1978 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1979 | return 1; |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1980 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1981 | deny: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1982 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp); |
| 1983 | _HA_ATOMIC_INC(&s->be->be_counters.denied_resp); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1984 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1985 | _HA_ATOMIC_INC(&sess->listener->counters->denied_resp); |
Christopher Faulet | a08546b | 2019-12-16 16:07:34 +0100 | [diff] [blame] | 1986 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1987 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1988 | goto return_prx_err; |
| 1989 | |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 1990 | return_fail_rewrite: |
| 1991 | if (!(s->flags & SF_ERR_MASK)) |
| 1992 | s->flags |= SF_ERR_PRXCOND; |
| 1993 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
| 1994 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
| 1995 | if (sess->listener && sess->listener->counters) |
| 1996 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
| 1997 | if (objt_server(s->target)) |
| 1998 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
| 1999 | /* fall through */ |
| 2000 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2001 | return_int_err: |
| 2002 | txn->status = 500; |
| 2003 | if (!(s->flags & SF_ERR_MASK)) |
| 2004 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2005 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 2006 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
Dragan Dosen | 9a006f9 | 2021-09-21 13:02:09 +0200 | [diff] [blame] | 2007 | if (sess->listener && sess->listener->counters) |
| 2008 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 2009 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2010 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2011 | goto return_prx_err; |
| 2012 | |
| 2013 | return_bad_res: |
| 2014 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2015 | stream_inc_http_fail_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2016 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | a20a653 | 2020-02-05 10:16:41 +0100 | [diff] [blame] | 2017 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2018 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | a20a653 | 2020-02-05 10:16:41 +0100 | [diff] [blame] | 2019 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 2020 | } |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2021 | /* fall through */ |
| 2022 | |
| 2023 | return_prx_err: |
| 2024 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 2025 | /* fall through */ |
| 2026 | |
| 2027 | return_prx_cond: |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2028 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 2029 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2030 | |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 2031 | http_set_term_flags(s); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2032 | |
Christopher Faulet | e58c000 | 2020-03-02 16:21:01 +0100 | [diff] [blame] | 2033 | rep->analysers &= AN_RES_FLT_END; |
| 2034 | s->req.analysers &= AN_REQ_FLT_END; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2035 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2036 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2037 | DBG_TRACE_DEVEL("leaving on error", |
| 2038 | 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] | 2039 | return 0; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2040 | |
| 2041 | return_prx_yield: |
| 2042 | channel_dont_close(rep); |
| 2043 | DBG_TRACE_DEVEL("waiting for more data", |
| 2044 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 2045 | return 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2046 | } |
| 2047 | |
| 2048 | /* This function is an analyser which forwards response body (including chunk |
| 2049 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 2050 | * zero byte. The only situation where it must not be called is when we're in |
| 2051 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 2052 | * remaining data and to resync after end of body. It expects the msg_state to |
| 2053 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 2054 | * read more data, or 1 once we can go on with next request or end the stream. |
| 2055 | * |
| 2056 | * It is capable of compressing response data both in content-length mode and |
| 2057 | * in chunked mode. The state machines follows different flows depending on |
| 2058 | * whether content-length and chunked modes are used, since there are no |
| 2059 | * trailers in content-length : |
| 2060 | * |
| 2061 | * chk-mode cl-mode |
| 2062 | * ,----- BODY -----. |
| 2063 | * / \ |
| 2064 | * V size > 0 V chk-mode |
| 2065 | * .--> SIZE -------------> DATA -------------> CRLF |
| 2066 | * | | size == 0 | last byte | |
| 2067 | * | v final crlf v inspected | |
| 2068 | * | TRAILERS -----------> DONE | |
| 2069 | * | | |
| 2070 | * `----------------------------------------------' |
| 2071 | * |
| 2072 | * Compression only happens in the DATA state, and must be flushed in final |
| 2073 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 2074 | * is performed at once on final states for all bytes parsed, or when leaving |
| 2075 | * on missing data. |
| 2076 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2077 | 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] | 2078 | { |
| 2079 | struct session *sess = s->sess; |
| 2080 | struct http_txn *txn = s->txn; |
| 2081 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2082 | struct htx *htx; |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2083 | int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2084 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2085 | 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] | 2086 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2087 | htx = htxbuf(&res->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2088 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2089 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 2090 | goto return_bad_res; |
| 2091 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 2092 | goto return_int_err; |
| 2093 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2094 | if (msg->msg_state == HTTP_MSG_BODY) |
| 2095 | msg->msg_state = HTTP_MSG_DATA; |
| 2096 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2097 | /* in most states, we should abort in case of early close */ |
| 2098 | channel_auto_close(res); |
| 2099 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2100 | if (res->to_forward) { |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2101 | if (res->to_forward == CHN_INFINITE_FORWARD) { |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2102 | if (res->flags & CF_EOI) |
| 2103 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2104 | } |
| 2105 | else { |
| 2106 | /* We can't process the buffer's contents yet */ |
| 2107 | res->flags |= CF_WAKE_WRITE; |
| 2108 | goto missing_data_or_waiting; |
| 2109 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2110 | } |
| 2111 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2112 | if (msg->msg_state >= HTTP_MSG_ENDING) |
| 2113 | goto ending; |
| 2114 | |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 2115 | 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] | 2116 | (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) { |
| 2117 | msg->msg_state = HTTP_MSG_ENDING; |
| 2118 | goto ending; |
| 2119 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2120 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2121 | /* Forward input data. We get it by removing all outgoing data not |
| 2122 | * forwarded yet from HTX data size. If there are some data filters, we |
| 2123 | * let them decide the amount of data to forward. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2124 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2125 | if (HAS_RSP_DATA_FILTERS(s)) { |
| 2126 | ret = flt_http_payload(s, msg, htx->data); |
| 2127 | if (ret < 0) |
| 2128 | goto return_bad_res; |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 2129 | c_adv(res, ret); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2130 | } |
| 2131 | else { |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 2132 | c_adv(res, htx->data - co_data(res)); |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2133 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
| 2134 | channel_htx_forward_forever(res, htx); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2135 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2136 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2137 | if (htx->data != co_data(res)) |
| 2138 | goto missing_data_or_waiting; |
| 2139 | |
| 2140 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) { |
| 2141 | msg->msg_state = HTTP_MSG_ENDING; |
| 2142 | goto ending; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2143 | } |
| 2144 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2145 | /* 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] | 2146 | * in HTTP_MSG_ENDING state. Then if all data was marked to be |
| 2147 | * forwarded, set the state to HTTP_MSG_DONE. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2148 | */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2149 | if (!(htx->flags & HTX_FL_EOM)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2150 | goto missing_data_or_waiting; |
| 2151 | |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 2152 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2153 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2154 | ending: |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 2155 | res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */ |
| 2156 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2157 | /* other states, ENDING...TUNNEL */ |
| 2158 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 2159 | goto done; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2160 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2161 | if (HAS_RSP_DATA_FILTERS(s)) { |
| 2162 | ret = flt_http_end(s, msg); |
| 2163 | if (ret <= 0) { |
| 2164 | if (!ret) |
| 2165 | goto missing_data_or_waiting; |
| 2166 | goto return_bad_res; |
| 2167 | } |
| 2168 | } |
| 2169 | |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 2170 | 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] | 2171 | !(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 2172 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 2173 | goto ending; |
| 2174 | } |
| 2175 | else { |
| 2176 | msg->msg_state = HTTP_MSG_DONE; |
| 2177 | res->to_forward = 0; |
| 2178 | } |
| 2179 | |
| 2180 | done: |
| 2181 | |
| 2182 | channel_dont_close(res); |
| 2183 | |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 2184 | if ((res->flags & CF_SHUTW) && co_data(res)) { |
| 2185 | /* response errors are most likely due to the client aborting |
| 2186 | * the transfer. */ |
| 2187 | goto return_cli_abort; |
| 2188 | } |
| 2189 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2190 | http_end_response(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2191 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2192 | 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] | 2193 | return 1; |
| 2194 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2195 | DBG_TRACE_DEVEL("waiting for the end of the HTTP txn", |
| 2196 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2197 | return 0; |
| 2198 | |
| 2199 | missing_data_or_waiting: |
| 2200 | if (res->flags & CF_SHUTW) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2201 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2202 | |
| 2203 | /* stop waiting for data if the input is closed before the end. If the |
| 2204 | * client side was already closed, it means that the client has aborted, |
| 2205 | * so we don't want to count this as a server abort. Otherwise it's a |
| 2206 | * server abort. |
| 2207 | */ |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 2208 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2209 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2210 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2211 | /* If we have some pending data, we continue the processing */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2212 | if (htx_is_empty(htx)) |
| 2213 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2214 | } |
| 2215 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2216 | /* When TE: chunked is used, we need to get there again to parse |
| 2217 | * 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] | 2218 | * set CF_DONTCLOSE. Similarly when there is a content-leng or if there |
| 2219 | * are filters registered on the stream, we don't want to forward a |
| 2220 | * close |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2221 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2222 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2223 | channel_dont_close(res); |
| 2224 | |
| 2225 | /* We know that more data are expected, but we couldn't send more that |
| 2226 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
| 2227 | * system knows it must not set a PUSH on this first part. Interactive |
| 2228 | * modes are already handled by the stream sock layer. We must not do |
| 2229 | * this in content-length mode because it could present the MSG_MORE |
| 2230 | * flag with the last block of forwarded data, which would cause an |
| 2231 | * additional delay to be observed by the receiver. |
| 2232 | */ |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 2233 | if (HAS_RSP_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2234 | res->flags |= CF_EXPECT_MORE; |
| 2235 | |
| 2236 | /* the stream handler will take care of timeouts and errors */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2237 | DBG_TRACE_DEVEL("waiting for more data to forward", |
| 2238 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2239 | return 0; |
| 2240 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2241 | return_srv_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2242 | _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts); |
| 2243 | _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2244 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2245 | _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2246 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2247 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts); |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2248 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2249 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 2250 | s->flags |= ((res->flags & CF_READ_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2251 | goto return_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2252 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2253 | return_cli_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2254 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 2255 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2256 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2257 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2258 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2259 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2260 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 2261 | s->flags |= ((res->flags & CF_WRITE_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2262 | goto return_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2263 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2264 | return_int_err: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2265 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 2266 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2267 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2268 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2269 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2270 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2271 | if (!(s->flags & SF_ERR_MASK)) |
| 2272 | s->flags |= SF_ERR_INTERNAL; |
| 2273 | goto return_error; |
| 2274 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2275 | return_bad_res: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2276 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2277 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2278 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2279 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 2280 | } |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2281 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2282 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2283 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2284 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2285 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2286 | return_error: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2287 | /* don't send any error message as we're in the body */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2288 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 2289 | http_set_term_flags(s); |
| 2290 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2291 | DBG_TRACE_DEVEL("leaving on error", |
| 2292 | 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] | 2293 | return 0; |
| 2294 | } |
| 2295 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2296 | /* Perform an HTTP redirect based on the information in <rule>. The function |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2297 | * returns zero in case of an irrecoverable error such as too large a request |
| 2298 | * to build a valid response, 1 in case of successful redirect (hence the rule |
| 2299 | * is final), or 2 if the rule has to be silently skipped. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2300 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2301 | 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] | 2302 | { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2303 | struct channel *req = &s->req; |
| 2304 | struct channel *res = &s->res; |
| 2305 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2306 | struct htx_sl *sl; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2307 | struct buffer *chunk; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2308 | struct ist status, reason, location; |
| 2309 | unsigned int flags; |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2310 | 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] | 2311 | |
| 2312 | chunk = alloc_trash_chunk(); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2313 | if (!chunk) { |
| 2314 | if (!(s->flags & SF_ERR_MASK)) |
| 2315 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2316 | goto fail; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2317 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2318 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2319 | /* |
| 2320 | * Create the location |
| 2321 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2322 | htx = htxbuf(&req->buf); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2323 | switch(rule->type) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2324 | case REDIRECT_TYPE_SCHEME: { |
| 2325 | struct http_hdr_ctx ctx; |
| 2326 | struct ist path, host; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2327 | struct http_uri_parser parser; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2328 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2329 | host = ist(""); |
| 2330 | ctx.blk = NULL; |
| 2331 | if (http_find_header(htx, ist("Host"), &ctx, 0)) |
| 2332 | host = ctx.value; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2333 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2334 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2335 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 2336 | path = http_parse_path(&parser); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2337 | /* build message using path */ |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 2338 | if (isttest(path)) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2339 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2340 | int qs = 0; |
| 2341 | while (qs < path.len) { |
| 2342 | if (*(path.ptr + qs) == '?') { |
| 2343 | path.len = qs; |
| 2344 | break; |
| 2345 | } |
| 2346 | qs++; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2347 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2348 | } |
| 2349 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2350 | else |
| 2351 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2352 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2353 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2354 | /* add scheme */ |
| 2355 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2356 | goto fail; |
| 2357 | } |
| 2358 | else { |
| 2359 | /* add scheme with executing log format */ |
| 2360 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2361 | chunk->size - chunk->data, |
| 2362 | &rule->rdr_fmt); |
| 2363 | } |
| 2364 | /* add "://" + host + path */ |
| 2365 | if (!chunk_memcat(chunk, "://", 3) || |
| 2366 | !chunk_memcat(chunk, host.ptr, host.len) || |
| 2367 | !chunk_memcat(chunk, path.ptr, path.len)) |
| 2368 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2369 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2370 | /* append a slash at the end of the location if needed and missing */ |
| 2371 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2372 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2373 | if (chunk->data + 1 >= chunk->size) |
| 2374 | goto fail; |
| 2375 | chunk->area[chunk->data++] = '/'; |
| 2376 | } |
| 2377 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2378 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2379 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2380 | case REDIRECT_TYPE_PREFIX: { |
| 2381 | struct ist path; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2382 | struct http_uri_parser parser; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2383 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2384 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2385 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 2386 | path = http_parse_path(&parser); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2387 | /* build message using path */ |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 2388 | if (isttest(path)) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2389 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2390 | int qs = 0; |
| 2391 | while (qs < path.len) { |
| 2392 | if (*(path.ptr + qs) == '?') { |
| 2393 | path.len = qs; |
| 2394 | break; |
| 2395 | } |
| 2396 | qs++; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2397 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2398 | } |
| 2399 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2400 | else |
| 2401 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2402 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2403 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2404 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2405 | * add anything, otherwise it makes it hard for the user to |
| 2406 | * configure a self-redirection. |
| 2407 | */ |
| 2408 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 2409 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2410 | goto fail; |
| 2411 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2412 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2413 | else { |
| 2414 | /* add prefix with executing log format */ |
| 2415 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2416 | chunk->size - chunk->data, |
| 2417 | &rule->rdr_fmt); |
| 2418 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2419 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2420 | /* add path */ |
| 2421 | if (!chunk_memcat(chunk, path.ptr, path.len)) |
| 2422 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2423 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2424 | /* append a slash at the end of the location if needed and missing */ |
| 2425 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2426 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2427 | if (chunk->data + 1 >= chunk->size) |
| 2428 | goto fail; |
| 2429 | chunk->area[chunk->data++] = '/'; |
| 2430 | } |
| 2431 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2432 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2433 | case REDIRECT_TYPE_LOCATION: |
| 2434 | default: |
| 2435 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2436 | /* add location */ |
| 2437 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2438 | goto fail; |
| 2439 | } |
| 2440 | else { |
| 2441 | /* add location with executing log format */ |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2442 | int len = build_logline(s, chunk->area + chunk->data, |
| 2443 | chunk->size - chunk->data, |
| 2444 | &rule->rdr_fmt); |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2445 | if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) { |
| 2446 | ret = 2; |
| 2447 | goto out; |
| 2448 | } |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2449 | |
| 2450 | chunk->data += len; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2451 | } |
| 2452 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2453 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2454 | location = ist2(chunk->area, chunk->data); |
| 2455 | |
| 2456 | /* |
| 2457 | * Create the 30x response |
| 2458 | */ |
| 2459 | switch (rule->code) { |
| 2460 | case 308: |
| 2461 | status = ist("308"); |
| 2462 | reason = ist("Permanent Redirect"); |
| 2463 | break; |
| 2464 | case 307: |
| 2465 | status = ist("307"); |
| 2466 | reason = ist("Temporary Redirect"); |
| 2467 | break; |
| 2468 | case 303: |
| 2469 | status = ist("303"); |
| 2470 | reason = ist("See Other"); |
| 2471 | break; |
| 2472 | case 301: |
| 2473 | status = ist("301"); |
| 2474 | reason = ist("Moved Permanently"); |
| 2475 | break; |
| 2476 | case 302: |
| 2477 | default: |
| 2478 | status = ist("302"); |
| 2479 | reason = ist("Found"); |
| 2480 | break; |
| 2481 | } |
| 2482 | |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2483 | if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE) |
| 2484 | close = 1; |
| 2485 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2486 | htx = htx_from_buf(&res->buf); |
Kevin Zhu | 96b3639 | 2020-01-07 09:42:55 +0100 | [diff] [blame] | 2487 | /* Trim any possible response */ |
| 2488 | channel_htx_truncate(&s->res, htx); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2489 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 2490 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason); |
| 2491 | if (!sl) |
| 2492 | goto fail; |
| 2493 | sl->info.res.status = rule->code; |
| 2494 | s->txn->status = rule->code; |
| 2495 | |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2496 | if (close && !htx_add_header(htx, ist("Connection"), ist("close"))) |
| 2497 | goto fail; |
| 2498 | |
| 2499 | if (!htx_add_header(htx, ist("Content-length"), ist("0")) || |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2500 | !htx_add_header(htx, ist("Location"), location)) |
| 2501 | goto fail; |
| 2502 | |
| 2503 | if (rule->code == 302 || rule->code == 303 || rule->code == 307) { |
| 2504 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache"))) |
| 2505 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | if (rule->cookie_len) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2509 | if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len))) |
| 2510 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2511 | } |
| 2512 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2513 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2514 | goto fail; |
| 2515 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2516 | htx->flags |= HTX_FL_EOM; |
Kevin Zhu | 96b3639 | 2020-01-07 09:42:55 +0100 | [diff] [blame] | 2517 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 2518 | if (!http_forward_proxy_resp(s, 1)) |
| 2519 | goto fail; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2520 | |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2521 | if (rule->flags & REDIRECT_FLAG_FROM_REQ) { |
| 2522 | /* let's log the request time */ |
| 2523 | s->logs.tv_request = now; |
Christopher Faulet | d347588 | 2021-10-04 14:16:46 +0200 | [diff] [blame] | 2524 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2525 | |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2526 | 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] | 2527 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2528 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2529 | |
| 2530 | if (!(s->flags & SF_ERR_MASK)) |
| 2531 | s->flags |= SF_ERR_LOCAL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 2532 | http_set_term_flags(s); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2533 | |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2534 | out: |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2535 | free_trash_chunk(chunk); |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2536 | return ret; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2537 | |
| 2538 | fail: |
| 2539 | /* If an error occurred, remove the incomplete HTTP response from the |
| 2540 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 2541 | channel_htx_truncate(res, htxbuf(&res->buf)); |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2542 | ret = 0; |
| 2543 | goto out; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2544 | } |
| 2545 | |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2546 | /* This function filters the request header names to only allow [0-9a-zA-Z-] |
| 2547 | * characters. Depending on the proxy configuration, headers with a name not |
| 2548 | * matching this charset are removed or the request is rejected with a |
| 2549 | * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT |
| 2550 | * to continue the request processing or HTTP_RULE_RES_DENY if the request is |
| 2551 | * rejected. |
| 2552 | */ |
| 2553 | static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px) |
| 2554 | { |
| 2555 | struct htx_blk *blk; |
| 2556 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
| 2557 | |
| 2558 | blk = htx_get_first_blk(htx); |
| 2559 | while (blk) { |
| 2560 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 2561 | |
| 2562 | if (type == HTX_BLK_HDR) { |
| 2563 | struct ist n = htx_get_blk_name(htx, blk); |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2564 | int i, end = istlen(n); |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2565 | |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2566 | for (i = 0; i < end; i++) { |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2567 | if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') { |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2568 | break; |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2569 | } |
| 2570 | } |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2571 | |
| 2572 | if (i < end) { |
| 2573 | /* Disallowed character found - block the request or remove the header */ |
| 2574 | if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK) |
| 2575 | goto block; |
| 2576 | blk = htx_remove_blk(htx, blk); |
| 2577 | continue; |
| 2578 | } |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2579 | } |
| 2580 | if (type == HTX_BLK_EOH) |
| 2581 | break; |
| 2582 | |
| 2583 | blk = htx_get_next_blk(htx, blk); |
| 2584 | } |
| 2585 | out: |
| 2586 | return rule_ret; |
| 2587 | block: |
| 2588 | /* Block the request returning a 403-Forbidden response */ |
| 2589 | s->txn->status = 403; |
| 2590 | rule_ret = HTTP_RULE_RES_DENY; |
| 2591 | goto out; |
| 2592 | } |
| 2593 | |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2594 | /* Replace all headers matching the name <name>. The header value is replaced if |
| 2595 | * it matches the regex <re>. <str> is used for the replacement. If <full> is |
| 2596 | * set to 1, the full-line is matched and replaced. Otherwise, comma-separated |
| 2597 | * values are evaluated one by one. It returns 0 on success and -1 on error. |
| 2598 | */ |
| 2599 | int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name, |
| 2600 | const char *str, struct my_regex *re, int full) |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2601 | { |
| 2602 | struct http_hdr_ctx ctx; |
| 2603 | struct buffer *output = get_trash_chunk(); |
| 2604 | |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2605 | ctx.blk = NULL; |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2606 | while (http_find_header(htx, name, &ctx, full)) { |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2607 | if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0)) |
| 2608 | continue; |
| 2609 | |
| 2610 | output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch); |
| 2611 | if (output->data == -1) |
| 2612 | return -1; |
| 2613 | if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data))) |
| 2614 | return -1; |
| 2615 | } |
| 2616 | return 0; |
| 2617 | } |
| 2618 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2619 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 2620 | * takes the string from the variable 'replace' with length 'len', then modifies |
| 2621 | * the relevant part of the request line accordingly. Then it updates various |
| 2622 | * pointers to the next elements which were moved, and the total buffer length. |
| 2623 | * It finds the action to be performed in p[2], previously filled by function |
| 2624 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 2625 | * error, though this can be revisited when this code is finally exploited. |
| 2626 | * |
| 2627 | * '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] | 2628 | * 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] | 2629 | * |
| 2630 | * In query string case, the mark question '?' must be set at the start of the |
| 2631 | * string by the caller, event if the replacement query string is empty. |
| 2632 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2633 | int http_req_replace_stline(int action, const char *replace, int len, |
| 2634 | struct proxy *px, struct stream *s) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2635 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2636 | struct htx *htx = htxbuf(&s->req.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2637 | |
| 2638 | switch (action) { |
| 2639 | case 0: // method |
| 2640 | if (!http_replace_req_meth(htx, ist2(replace, len))) |
| 2641 | return -1; |
| 2642 | break; |
| 2643 | |
| 2644 | case 1: // path |
Christopher Faulet | b8ce505 | 2020-08-31 16:11:57 +0200 | [diff] [blame] | 2645 | if (!http_replace_req_path(htx, ist2(replace, len), 0)) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2646 | return -1; |
| 2647 | break; |
| 2648 | |
| 2649 | case 2: // query |
| 2650 | if (!http_replace_req_query(htx, ist2(replace, len))) |
| 2651 | return -1; |
| 2652 | break; |
| 2653 | |
| 2654 | case 3: // uri |
| 2655 | if (!http_replace_req_uri(htx, ist2(replace, len))) |
| 2656 | return -1; |
| 2657 | break; |
| 2658 | |
Christopher Faulet | 312294f | 2020-09-02 17:17:44 +0200 | [diff] [blame] | 2659 | case 4: // path + query |
| 2660 | if (!http_replace_req_path(htx, ist2(replace, len), 1)) |
| 2661 | return -1; |
| 2662 | break; |
| 2663 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2664 | default: |
| 2665 | return -1; |
| 2666 | } |
| 2667 | return 0; |
| 2668 | } |
| 2669 | |
| 2670 | /* This function replace the HTTP status code and the associated message. The |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2671 | * variable <status> contains the new status code. This function never fails. It |
| 2672 | * returns 0 in case of success, -1 in case of internal error. |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2673 | */ |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2674 | 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] | 2675 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2676 | struct htx *htx = htxbuf(&s->res.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2677 | char *res; |
| 2678 | |
| 2679 | chunk_reset(&trash); |
| 2680 | res = ultoa_o(status, trash.area, trash.size); |
| 2681 | trash.data = res - trash.area; |
| 2682 | |
| 2683 | /* Do we have a custom reason format string? */ |
Tim Duesterhus | e296d3e | 2020-03-05 17:56:31 +0100 | [diff] [blame] | 2684 | if (!isttest(reason)) { |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2685 | const char *str = http_get_reason(status); |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 2686 | reason = ist(str); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2687 | } |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2688 | |
Christopher Faulet | bde2c4c | 2020-08-31 16:43:34 +0200 | [diff] [blame] | 2689 | if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason)) |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2690 | return -1; |
Willy Tarreau | 640e253 | 2023-01-10 14:50:44 +0100 | [diff] [blame^] | 2691 | s->txn->status = status; |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2692 | return 0; |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2693 | } |
| 2694 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2695 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
| 2696 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 2697 | * further processing of the request (auth, deny, ...), and defaults to |
| 2698 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 2699 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 2700 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 2701 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 2702 | * status. |
| 2703 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2704 | static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, |
| 2705 | struct list *rules, struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2706 | { |
| 2707 | struct session *sess = strm_sess(s); |
| 2708 | struct http_txn *txn = s->txn; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2709 | struct act_rule *rule; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2710 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2711 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2712 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2713 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2714 | * resume condition. If a resume is needed it is always in the action |
| 2715 | * and never in the ACL or converters. In this case, we initialise the |
| 2716 | * current rule, and go to the action execution point. |
| 2717 | */ |
| 2718 | if (s->current_rule) { |
| 2719 | rule = s->current_rule; |
| 2720 | s->current_rule = NULL; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2721 | 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] | 2722 | goto resume_execution; |
| 2723 | } |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2724 | 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] | 2725 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2726 | restart: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2727 | /* start the ruleset evaluation in strict mode */ |
| 2728 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2729 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2730 | list_for_each_entry(rule, s->current_rule_list, list) { |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2731 | /* check optional condition */ |
| 2732 | if (rule->cond) { |
| 2733 | int ret; |
| 2734 | |
| 2735 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 2736 | ret = acl_pass(ret); |
| 2737 | |
| 2738 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2739 | ret = !ret; |
| 2740 | |
| 2741 | if (!ret) /* condition not matched */ |
| 2742 | continue; |
| 2743 | } |
| 2744 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2745 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2746 | resume_execution: |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 2747 | if (rule->kw->flags & KWF_EXPERIMENTAL) |
| 2748 | mark_tainted(TAINTED_ACTION_EXP_EXECUTED); |
| 2749 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2750 | /* Always call the action function if defined */ |
| 2751 | if (rule->action_ptr) { |
| 2752 | if ((s->req.flags & CF_READ_ERROR) || |
Christopher Faulet | 446d803 | 2022-12-12 07:53:18 +0100 | [diff] [blame] | 2753 | ((s->req.flags & CF_SHUTR) && |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2754 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2755 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2756 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2757 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2758 | case ACT_RET_CONT: |
| 2759 | break; |
| 2760 | case ACT_RET_STOP: |
| 2761 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2762 | s->last_rule_file = rule->conf.file; |
| 2763 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2764 | goto end; |
| 2765 | case ACT_RET_YIELD: |
| 2766 | s->current_rule = rule; |
| 2767 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2768 | goto end; |
| 2769 | case ACT_RET_ERR: |
| 2770 | rule_ret = HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2771 | s->last_rule_file = rule->conf.file; |
| 2772 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2773 | goto end; |
| 2774 | case ACT_RET_DONE: |
| 2775 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2776 | s->last_rule_file = rule->conf.file; |
| 2777 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2778 | goto end; |
| 2779 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 2780 | if (txn->status == -1) |
| 2781 | txn->status = 403; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2782 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2783 | s->last_rule_file = rule->conf.file; |
| 2784 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2785 | goto end; |
| 2786 | case ACT_RET_ABRT: |
| 2787 | rule_ret = HTTP_RULE_RES_ABRT; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2788 | s->last_rule_file = rule->conf.file; |
| 2789 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2790 | goto end; |
| 2791 | case ACT_RET_INV: |
| 2792 | rule_ret = HTTP_RULE_RES_BADREQ; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2793 | s->last_rule_file = rule->conf.file; |
| 2794 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2795 | goto end; |
| 2796 | } |
| 2797 | continue; /* eval the next rule */ |
| 2798 | } |
| 2799 | |
| 2800 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2801 | switch (rule->action) { |
| 2802 | case ACT_ACTION_ALLOW: |
| 2803 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2804 | s->last_rule_file = rule->conf.file; |
| 2805 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2806 | goto end; |
| 2807 | |
| 2808 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2809 | txn->status = rule->arg.http_reply->status; |
| 2810 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2811 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2812 | s->last_rule_file = rule->conf.file; |
| 2813 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2814 | goto end; |
| 2815 | |
| 2816 | case ACT_HTTP_REQ_TARPIT: |
| 2817 | txn->flags |= TX_CLTARPIT; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2818 | txn->status = rule->arg.http_reply->status; |
| 2819 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2820 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2821 | s->last_rule_file = rule->conf.file; |
| 2822 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2823 | goto end; |
| 2824 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2825 | case ACT_HTTP_REDIR: { |
| 2826 | int ret = http_apply_redirect_rule(rule->arg.redir, s, txn); |
| 2827 | |
| 2828 | if (ret == 2) // 2 == skip |
| 2829 | break; |
| 2830 | |
| 2831 | rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2832 | s->last_rule_file = rule->conf.file; |
| 2833 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2834 | goto end; |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2835 | } |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2836 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2837 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2838 | default: |
| 2839 | break; |
| 2840 | } |
| 2841 | } |
| 2842 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2843 | if (def_rules && s->current_rule_list == def_rules) { |
| 2844 | s->current_rule_list = rules; |
| 2845 | goto restart; |
| 2846 | } |
| 2847 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2848 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2849 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2850 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2851 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2852 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2853 | /* we reached the end of the rules, nothing to report */ |
| 2854 | return rule_ret; |
| 2855 | } |
| 2856 | |
| 2857 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 2858 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 2859 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 2860 | * is returned, the process can continue the evaluation of next rule list. If |
| 2861 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 2862 | * 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] | 2863 | * must be returned. If *YIELD is returned, the caller must call again the |
| 2864 | * function with the same context. |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2865 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2866 | static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules, |
| 2867 | struct list *rules, struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2868 | { |
| 2869 | struct session *sess = strm_sess(s); |
| 2870 | struct http_txn *txn = s->txn; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2871 | struct act_rule *rule; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2872 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2873 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2874 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2875 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2876 | * resume condition. If a resume is needed it is always in the action |
| 2877 | * and never in the ACL or converters. In this case, we initialise the |
| 2878 | * current rule, and go to the action execution point. |
| 2879 | */ |
| 2880 | if (s->current_rule) { |
| 2881 | rule = s->current_rule; |
| 2882 | s->current_rule = NULL; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2883 | 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] | 2884 | goto resume_execution; |
| 2885 | } |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2886 | s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules); |
| 2887 | |
| 2888 | restart: |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2889 | |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2890 | /* start the ruleset evaluation in strict mode */ |
| 2891 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2892 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2893 | list_for_each_entry(rule, s->current_rule_list, list) { |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2894 | /* check optional condition */ |
| 2895 | if (rule->cond) { |
| 2896 | int ret; |
| 2897 | |
| 2898 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 2899 | ret = acl_pass(ret); |
| 2900 | |
| 2901 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2902 | ret = !ret; |
| 2903 | |
| 2904 | if (!ret) /* condition not matched */ |
| 2905 | continue; |
| 2906 | } |
| 2907 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2908 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2909 | resume_execution: |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 2910 | if (rule->kw->flags & KWF_EXPERIMENTAL) |
| 2911 | mark_tainted(TAINTED_ACTION_EXP_EXECUTED); |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2912 | |
| 2913 | /* Always call the action function if defined */ |
| 2914 | if (rule->action_ptr) { |
| 2915 | if ((s->req.flags & CF_READ_ERROR) || |
Christopher Faulet | 446d803 | 2022-12-12 07:53:18 +0100 | [diff] [blame] | 2916 | ((s->req.flags & CF_SHUTR) && |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2917 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2918 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2919 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2920 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2921 | case ACT_RET_CONT: |
| 2922 | break; |
| 2923 | case ACT_RET_STOP: |
| 2924 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2925 | s->last_rule_file = rule->conf.file; |
| 2926 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2927 | goto end; |
| 2928 | case ACT_RET_YIELD: |
| 2929 | s->current_rule = rule; |
| 2930 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2931 | goto end; |
| 2932 | case ACT_RET_ERR: |
| 2933 | rule_ret = HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2934 | s->last_rule_file = rule->conf.file; |
| 2935 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2936 | goto end; |
| 2937 | case ACT_RET_DONE: |
| 2938 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2939 | s->last_rule_file = rule->conf.file; |
| 2940 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2941 | goto end; |
| 2942 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 2943 | if (txn->status == -1) |
| 2944 | txn->status = 502; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2945 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2946 | s->last_rule_file = rule->conf.file; |
| 2947 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2948 | goto end; |
| 2949 | case ACT_RET_ABRT: |
| 2950 | rule_ret = HTTP_RULE_RES_ABRT; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2951 | s->last_rule_file = rule->conf.file; |
| 2952 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2953 | goto end; |
| 2954 | case ACT_RET_INV: |
| 2955 | rule_ret = HTTP_RULE_RES_BADREQ; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2956 | s->last_rule_file = rule->conf.file; |
| 2957 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2958 | goto end; |
| 2959 | } |
| 2960 | continue; /* eval the next rule */ |
| 2961 | } |
| 2962 | |
| 2963 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2964 | switch (rule->action) { |
| 2965 | case ACT_ACTION_ALLOW: |
| 2966 | rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2967 | s->last_rule_file = rule->conf.file; |
| 2968 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2969 | goto end; |
| 2970 | |
| 2971 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2972 | txn->status = rule->arg.http_reply->status; |
| 2973 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 2974 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2975 | s->last_rule_file = rule->conf.file; |
| 2976 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2977 | goto end; |
| 2978 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2979 | case ACT_HTTP_REDIR: { |
| 2980 | int ret = http_apply_redirect_rule(rule->arg.redir, s, txn); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2981 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2982 | if (ret == 2) // 2 == skip |
| 2983 | break; |
| 2984 | |
| 2985 | rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2986 | s->last_rule_file = rule->conf.file; |
| 2987 | s->last_rule_line = rule->conf.line; |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2988 | goto end; |
| 2989 | } |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2990 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2991 | default: |
| 2992 | break; |
| 2993 | } |
| 2994 | } |
| 2995 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2996 | if (def_rules && s->current_rule_list == def_rules) { |
| 2997 | s->current_rule_list = rules; |
| 2998 | goto restart; |
| 2999 | } |
| 3000 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3001 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 3002 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 3003 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 3004 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 3005 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3006 | /* we reached the end of the rules, nothing to report */ |
| 3007 | return rule_ret; |
| 3008 | } |
| 3009 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3010 | /* Executes backend and frontend http-after-response rules for the stream <s>, |
| 3011 | * in that order. it return 1 on success and 0 on error. It is the caller |
| 3012 | * responsibility to catch error or ignore it. If it catches it, this function |
| 3013 | * may be called a second time, for the internal error. |
| 3014 | */ |
| 3015 | int http_eval_after_res_rules(struct stream *s) |
| 3016 | { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 3017 | struct list *def_rules, *rules; |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3018 | struct session *sess = s->sess; |
| 3019 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 3020 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 3021 | /* Eval after-response ruleset only if the reply is not const */ |
| 3022 | if (s->txn->flags & TX_CONST_REPLY) |
| 3023 | goto end; |
| 3024 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3025 | /* prune the request variables if not already done and swap to the response variables. */ |
| 3026 | if (s->vars_reqres.scope != SCOPE_RES) { |
| 3027 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 3028 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | b7bfcb3 | 2021-08-31 08:13:25 +0200 | [diff] [blame] | 3029 | vars_init_head(&s->vars_reqres, SCOPE_RES); |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3030 | } |
| 3031 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 3032 | def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL); |
| 3033 | rules = &s->be->http_after_res_rules; |
| 3034 | |
| 3035 | ret = http_res_get_intercept_rule(s->be, def_rules, rules, s); |
Christopher Faulet | 4c5a591 | 2021-11-09 17:48:39 +0100 | [diff] [blame] | 3036 | 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] | 3037 | def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL); |
| 3038 | rules = &sess->fe->http_after_res_rules; |
| 3039 | ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s); |
| 3040 | } |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3041 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 3042 | end: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3043 | /* All other codes than CONTINUE, STOP or DONE are forbidden */ |
| 3044 | return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE); |
| 3045 | } |
| 3046 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3047 | /* |
| 3048 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 3049 | * desirable to call it only when needed. This code is quite complex because |
| 3050 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 3051 | * highly recommended not to touch this part without a good reason ! |
| 3052 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3053 | 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] | 3054 | { |
| 3055 | struct session *sess = s->sess; |
| 3056 | struct http_txn *txn = s->txn; |
| 3057 | struct htx *htx; |
| 3058 | struct http_hdr_ctx ctx; |
| 3059 | char *hdr_beg, *hdr_end, *del_from; |
| 3060 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
| 3061 | int preserve_hdr; |
| 3062 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3063 | htx = htxbuf(&req->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3064 | ctx.blk = NULL; |
| 3065 | while (http_find_header(htx, ist("Cookie"), &ctx, 1)) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3066 | int is_first = 1; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3067 | del_from = NULL; /* nothing to be deleted */ |
| 3068 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 3069 | |
| 3070 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3071 | * attributes whose name begin with a '$', and associate them with |
| 3072 | * the right cookie, if we want to delete this cookie. |
| 3073 | * So there are 3 cases for each cookie read : |
| 3074 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3075 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3076 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3077 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3078 | * "special" cookie. |
| 3079 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3080 | * remove it. If no application cookie persists in the header, we |
| 3081 | * *MUST* delete it. |
| 3082 | * |
| 3083 | * Note: RFC2965 is unclear about the processing of spaces around |
| 3084 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 3085 | * the RFC explicitly allows spaces before it, and not within the |
| 3086 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 3087 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 3088 | * after the equal sign too, resulting in some (rare) buggy |
| 3089 | * implementations trying to do that. So let's do what servers do. |
| 3090 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 3091 | * allowed quoted strings in values, with any possible character |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3092 | * after a backslash, including control chars and delimiters, which |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3093 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 3094 | * within values even without quotes. |
| 3095 | * |
| 3096 | * We have to keep multiple pointers in order to support cookie |
| 3097 | * removal at the beginning, middle or end of header without |
| 3098 | * corrupting the header. All of these headers are valid : |
| 3099 | * |
| 3100 | * hdr_beg hdr_end |
| 3101 | * | | |
| 3102 | * v | |
| 3103 | * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 | |
| 3104 | * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v |
| 3105 | * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3 |
| 3106 | * | | | | | | | |
| 3107 | * | | | | | | | |
| 3108 | * | | | | | | +--> next |
| 3109 | * | | | | | +----> val_end |
| 3110 | * | | | | +-----------> val_beg |
| 3111 | * | | | +--------------> equal |
| 3112 | * | | +----------------> att_end |
| 3113 | * | +---------------------> att_beg |
| 3114 | * +--------------------------> prev |
| 3115 | * |
| 3116 | */ |
| 3117 | hdr_beg = ctx.value.ptr; |
| 3118 | hdr_end = hdr_beg + ctx.value.len; |
| 3119 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3120 | /* Iterate through all cookies on this line */ |
| 3121 | |
| 3122 | /* find att_beg */ |
| 3123 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3124 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3125 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3126 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3127 | |
| 3128 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3129 | att_beg++; |
| 3130 | |
| 3131 | /* find att_end : this is the first character after the last non |
| 3132 | * space before the equal. It may be equal to hdr_end. |
| 3133 | */ |
| 3134 | equal = att_end = att_beg; |
| 3135 | while (equal < hdr_end) { |
| 3136 | if (*equal == '=' || *equal == ',' || *equal == ';') |
| 3137 | break; |
| 3138 | if (HTTP_IS_SPHT(*equal++)) |
| 3139 | continue; |
| 3140 | att_end = equal; |
| 3141 | } |
| 3142 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3143 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3144 | * is between <att_beg> and <equal>, both may be identical. |
| 3145 | */ |
| 3146 | /* look for end of cookie if there is an equal sign */ |
| 3147 | if (equal < hdr_end && *equal == '=') { |
| 3148 | /* look for the beginning of the value */ |
| 3149 | val_beg = equal + 1; |
| 3150 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3151 | val_beg++; |
| 3152 | |
| 3153 | /* find the end of the value, respecting quotes */ |
| 3154 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3155 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3156 | /* 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] | 3157 | val_end = next; |
| 3158 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3159 | val_end--; |
| 3160 | } |
| 3161 | else |
| 3162 | val_beg = val_end = next = equal; |
| 3163 | |
| 3164 | /* We have nothing to do with attributes beginning with |
| 3165 | * '$'. However, they will automatically be removed if a |
| 3166 | * header before them is removed, since they're supposed |
| 3167 | * to be linked together. |
| 3168 | */ |
| 3169 | if (*att_beg == '$') |
| 3170 | continue; |
| 3171 | |
| 3172 | /* Ignore cookies with no equal sign */ |
| 3173 | if (equal == next) { |
| 3174 | /* This is not our cookie, so we must preserve it. But if we already |
| 3175 | * scheduled another cookie for removal, we cannot remove the |
| 3176 | * complete header, but we can remove the previous block itself. |
| 3177 | */ |
| 3178 | preserve_hdr = 1; |
| 3179 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3180 | 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] | 3181 | val_end += delta; |
| 3182 | next += delta; |
| 3183 | hdr_end += delta; |
| 3184 | prev = del_from; |
| 3185 | del_from = NULL; |
| 3186 | } |
| 3187 | continue; |
| 3188 | } |
| 3189 | |
| 3190 | /* if there are spaces around the equal sign, we need to |
| 3191 | * strip them otherwise we'll get trouble for cookie captures, |
| 3192 | * or even for rewrites. Since this happens extremely rarely, |
| 3193 | * it does not hurt performance. |
| 3194 | */ |
| 3195 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3196 | int stripped_before = 0; |
| 3197 | int stripped_after = 0; |
| 3198 | |
| 3199 | if (att_end != equal) { |
| 3200 | memmove(att_end, equal, hdr_end - equal); |
| 3201 | stripped_before = (att_end - equal); |
| 3202 | equal += stripped_before; |
| 3203 | val_beg += stripped_before; |
| 3204 | } |
| 3205 | |
| 3206 | if (val_beg > equal + 1) { |
| 3207 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3208 | stripped_after = (equal + 1) - val_beg; |
| 3209 | val_beg += stripped_after; |
| 3210 | stripped_before += stripped_after; |
| 3211 | } |
| 3212 | |
| 3213 | val_end += stripped_before; |
| 3214 | next += stripped_before; |
| 3215 | hdr_end += stripped_before; |
| 3216 | } |
| 3217 | /* now everything is as on the diagram above */ |
| 3218 | |
| 3219 | /* First, let's see if we want to capture this cookie. We check |
| 3220 | * that we don't already have a client side cookie, because we |
| 3221 | * can only capture one. Also as an optimisation, we ignore |
| 3222 | * cookies shorter than the declared name. |
| 3223 | */ |
| 3224 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 3225 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3226 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3227 | int log_len = val_end - att_beg; |
| 3228 | |
| 3229 | if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3230 | ha_alert("HTTP logging : out of memory.\n"); |
| 3231 | } else { |
| 3232 | if (log_len > sess->fe->capture_len) |
| 3233 | log_len = sess->fe->capture_len; |
| 3234 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 3235 | txn->cli_cookie[log_len] = 0; |
| 3236 | } |
| 3237 | } |
| 3238 | |
| 3239 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 3240 | * following form : |
| 3241 | * |
| 3242 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
| 3243 | * |
| 3244 | * For cookies in prefix mode, the form is : |
| 3245 | * |
| 3246 | * Cookie: NAME=SRV~VALUE |
| 3247 | */ |
| 3248 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3249 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3250 | struct server *srv = s->be->srv; |
| 3251 | char *delim; |
| 3252 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3253 | /* 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] | 3254 | * have the server ID between val_beg and delim, and the original cookie between |
| 3255 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 3256 | * |
| 3257 | * hdr_beg |
| 3258 | * | |
| 3259 | * v |
| 3260 | * NAME=SRV; # in all but prefix modes |
| 3261 | * NAME=SRV~OPAQUE ; # in prefix mode |
| 3262 | * || || | |+-> next |
| 3263 | * || || | +--> val_end |
| 3264 | * || || +---------> delim |
| 3265 | * || |+------------> val_beg |
| 3266 | * || +-------------> att_end = equal |
| 3267 | * |+-----------------> att_beg |
| 3268 | * +------------------> prev |
| 3269 | * |
| 3270 | */ |
| 3271 | if (s->be->ck_opts & PR_CK_PFX) { |
| 3272 | for (delim = val_beg; delim < val_end; delim++) |
| 3273 | if (*delim == COOKIE_DELIM) |
| 3274 | break; |
| 3275 | } |
| 3276 | else { |
| 3277 | char *vbar1; |
| 3278 | delim = val_end; |
| 3279 | /* Now check if the cookie contains a date field, which would |
| 3280 | * appear after a vertical bar ('|') just after the server name |
| 3281 | * and before the delimiter. |
| 3282 | */ |
| 3283 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 3284 | if (vbar1) { |
| 3285 | /* OK, so left of the bar is the server's cookie and |
| 3286 | * right is the last seen date. It is a base64 encoded |
| 3287 | * 30-bit value representing the UNIX date since the |
| 3288 | * epoch in 4-second quantities. |
| 3289 | */ |
| 3290 | int val; |
| 3291 | delim = vbar1++; |
| 3292 | if (val_end - vbar1 >= 5) { |
| 3293 | val = b64tos30(vbar1); |
| 3294 | if (val > 0) |
| 3295 | txn->cookie_last_date = val << 2; |
| 3296 | } |
| 3297 | /* look for a second vertical bar */ |
| 3298 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 3299 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 3300 | val = b64tos30(vbar1 + 1); |
| 3301 | if (val > 0) |
| 3302 | txn->cookie_first_date = val << 2; |
| 3303 | } |
| 3304 | } |
| 3305 | } |
| 3306 | |
| 3307 | /* if the cookie has an expiration date and the proxy wants to check |
| 3308 | * it, then we do that now. We first check if the cookie is too old, |
| 3309 | * then only if it has expired. We detect strict overflow because the |
| 3310 | * time resolution here is not great (4 seconds). Cookies with dates |
| 3311 | * in the future are ignored if their offset is beyond one day. This |
| 3312 | * allows an admin to fix timezone issues without expiring everyone |
| 3313 | * and at the same time avoids keeping unwanted side effects for too |
| 3314 | * long. |
| 3315 | */ |
| 3316 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 3317 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) || |
| 3318 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
| 3319 | txn->flags &= ~TX_CK_MASK; |
| 3320 | txn->flags |= TX_CK_OLD; |
| 3321 | delim = val_beg; // let's pretend we have not found the cookie |
| 3322 | txn->cookie_first_date = 0; |
| 3323 | txn->cookie_last_date = 0; |
| 3324 | } |
| 3325 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 3326 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) || |
| 3327 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
| 3328 | txn->flags &= ~TX_CK_MASK; |
| 3329 | txn->flags |= TX_CK_EXPIRED; |
| 3330 | delim = val_beg; // let's pretend we have not found the cookie |
| 3331 | txn->cookie_first_date = 0; |
| 3332 | txn->cookie_last_date = 0; |
| 3333 | } |
| 3334 | |
| 3335 | /* Here, we'll look for the first running server which supports the cookie. |
| 3336 | * This allows to share a same cookie between several servers, for example |
| 3337 | * to dedicate backup servers to specific servers only. |
| 3338 | * However, to prevent clients from sticking to cookie-less backup server |
| 3339 | * when they have incidentely learned an empty cookie, we simply ignore |
| 3340 | * empty cookies and mark them as invalid. |
| 3341 | * The same behaviour is applied when persistence must be ignored. |
| 3342 | */ |
| 3343 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3344 | srv = NULL; |
| 3345 | |
| 3346 | while (srv) { |
| 3347 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 3348 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 3349 | if ((srv->cur_state != SRV_ST_STOPPED) || |
| 3350 | (s->be->options & PR_O_PERSIST) || |
| 3351 | (s->flags & SF_FORCE_PRST)) { |
| 3352 | /* we found the server and we can use it */ |
| 3353 | txn->flags &= ~TX_CK_MASK; |
| 3354 | txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN; |
| 3355 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
| 3356 | s->target = &srv->obj_type; |
| 3357 | break; |
| 3358 | } else { |
| 3359 | /* we found a server, but it's down, |
| 3360 | * mark it as such and go on in case |
| 3361 | * another one is available. |
| 3362 | */ |
| 3363 | txn->flags &= ~TX_CK_MASK; |
| 3364 | txn->flags |= TX_CK_DOWN; |
| 3365 | } |
| 3366 | } |
| 3367 | srv = srv->next; |
| 3368 | } |
| 3369 | |
| 3370 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
| 3371 | /* no server matched this cookie or we deliberately skipped it */ |
| 3372 | txn->flags &= ~TX_CK_MASK; |
| 3373 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3374 | txn->flags |= TX_CK_UNUSED; |
| 3375 | else |
| 3376 | txn->flags |= TX_CK_INVALID; |
| 3377 | } |
| 3378 | |
| 3379 | /* depending on the cookie mode, we may have to either : |
| 3380 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3381 | * the server never sees it ; |
| 3382 | * - 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] | 3383 | * application cookie so that it does not get accidentally removed later, |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3384 | * if we're in cookie prefix mode |
| 3385 | */ |
| 3386 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
| 3387 | int delta; /* negative */ |
| 3388 | |
| 3389 | memmove(val_beg, delim + 1, hdr_end - (delim + 1)); |
| 3390 | delta = val_beg - (delim + 1); |
| 3391 | val_end += delta; |
| 3392 | next += delta; |
| 3393 | hdr_end += delta; |
| 3394 | del_from = NULL; |
| 3395 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 3396 | } |
| 3397 | else if (del_from == NULL && |
| 3398 | (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) { |
| 3399 | del_from = prev; |
| 3400 | } |
| 3401 | } |
| 3402 | else { |
| 3403 | /* This is not our cookie, so we must preserve it. But if we already |
| 3404 | * scheduled another cookie for removal, we cannot remove the |
| 3405 | * complete header, but we can remove the previous block itself. |
| 3406 | */ |
| 3407 | preserve_hdr = 1; |
| 3408 | |
| 3409 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3410 | 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] | 3411 | if (att_beg >= del_from) |
| 3412 | att_beg += delta; |
| 3413 | if (att_end >= del_from) |
| 3414 | att_end += delta; |
| 3415 | val_beg += delta; |
| 3416 | val_end += delta; |
| 3417 | next += delta; |
| 3418 | hdr_end += delta; |
| 3419 | prev = del_from; |
| 3420 | del_from = NULL; |
| 3421 | } |
| 3422 | } |
| 3423 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3424 | } /* for each cookie */ |
| 3425 | |
| 3426 | |
| 3427 | /* There are no more cookies on this line. |
| 3428 | * We may still have one (or several) marked for deletion at the |
| 3429 | * end of the line. We must do this now in two ways : |
| 3430 | * - if some cookies must be preserved, we only delete from the |
| 3431 | * mark to the end of line ; |
| 3432 | * - if nothing needs to be preserved, simply delete the whole header |
| 3433 | */ |
| 3434 | if (del_from) { |
| 3435 | hdr_end = (preserve_hdr ? del_from : hdr_beg); |
| 3436 | } |
| 3437 | if ((hdr_end - hdr_beg) != ctx.value.len) { |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3438 | if (hdr_beg != hdr_end) |
| 3439 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3440 | else |
| 3441 | http_remove_header(htx, &ctx); |
| 3442 | } |
| 3443 | } /* for each "Cookie header */ |
| 3444 | } |
| 3445 | |
| 3446 | /* |
| 3447 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 3448 | * desirable to call it only when needed. This function is also used when we |
| 3449 | * just need to know if there is a cookie (eg: for check-cache). |
| 3450 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3451 | 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] | 3452 | { |
| 3453 | struct session *sess = s->sess; |
| 3454 | struct http_txn *txn = s->txn; |
| 3455 | struct htx *htx; |
| 3456 | struct http_hdr_ctx ctx; |
| 3457 | struct server *srv; |
| 3458 | char *hdr_beg, *hdr_end; |
| 3459 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3460 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3461 | htx = htxbuf(&res->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3462 | |
| 3463 | ctx.blk = NULL; |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3464 | while (http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3465 | int is_first = 1; |
| 3466 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3467 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 3468 | * <prev> points to the colon. |
| 3469 | */ |
| 3470 | txn->flags |= TX_SCK_PRESENT; |
| 3471 | |
| 3472 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 3473 | * check-cache is enabled) and we are not interested in checking |
| 3474 | * them. Warning, the cookie capture is declared in the frontend. |
| 3475 | */ |
| 3476 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
| 3477 | break; |
| 3478 | |
| 3479 | /* OK so now we know we have to process this response cookie. |
| 3480 | * The format of the Set-Cookie header is slightly different |
| 3481 | * from the format of the Cookie header in that it does not |
| 3482 | * support the comma as a cookie delimiter (thus the header |
| 3483 | * cannot be folded) because the Expires attribute described in |
| 3484 | * the original Netscape's spec may contain an unquoted date |
| 3485 | * with a comma inside. We have to live with this because |
| 3486 | * many browsers don't support Max-Age and some browsers don't |
| 3487 | * support quoted strings. However the Set-Cookie2 header is |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3488 | * clean but basically nobody supports it. |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3489 | * |
| 3490 | * We have to keep multiple pointers in order to support cookie |
| 3491 | * removal at the beginning, middle or end of header without |
| 3492 | * corrupting the header (in case of set-cookie2). A special |
| 3493 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 3494 | * fields after the first semi-colon. The <next> pointer points |
| 3495 | * either to the end of line (set-cookie) or next unquoted comma |
| 3496 | * (set-cookie2). All of these headers are valid : |
| 3497 | * |
| 3498 | * hdr_beg hdr_end |
| 3499 | * | | |
| 3500 | * v | |
| 3501 | * NAME1 = VALUE 1 ; Secure; Path="/" | |
| 3502 | * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v |
| 3503 | * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT |
| 3504 | * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard |
| 3505 | * | | | | | | | | |
| 3506 | * | | | | | | | +-> next |
| 3507 | * | | | | | | +------------> scav |
| 3508 | * | | | | | +--------------> val_end |
| 3509 | * | | | | +--------------------> val_beg |
| 3510 | * | | | +----------------------> equal |
| 3511 | * | | +------------------------> att_end |
| 3512 | * | +----------------------------> att_beg |
| 3513 | * +------------------------------> prev |
| 3514 | * -------------------------------> hdr_beg |
| 3515 | */ |
| 3516 | hdr_beg = ctx.value.ptr; |
| 3517 | hdr_end = hdr_beg + ctx.value.len; |
| 3518 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3519 | |
| 3520 | /* Iterate through all cookies on this line */ |
| 3521 | |
| 3522 | /* find att_beg */ |
| 3523 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3524 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3525 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3526 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3527 | |
| 3528 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3529 | att_beg++; |
| 3530 | |
| 3531 | /* find att_end : this is the first character after the last non |
| 3532 | * space before the equal. It may be equal to hdr_end. |
| 3533 | */ |
| 3534 | equal = att_end = att_beg; |
| 3535 | |
| 3536 | while (equal < hdr_end) { |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3537 | if (*equal == '=' || *equal == ';') |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3538 | break; |
| 3539 | if (HTTP_IS_SPHT(*equal++)) |
| 3540 | continue; |
| 3541 | att_end = equal; |
| 3542 | } |
| 3543 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3544 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3545 | * is between <att_beg> and <equal>, both may be identical. |
| 3546 | */ |
| 3547 | |
| 3548 | /* look for end of cookie if there is an equal sign */ |
| 3549 | if (equal < hdr_end && *equal == '=') { |
| 3550 | /* look for the beginning of the value */ |
| 3551 | val_beg = equal + 1; |
| 3552 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3553 | val_beg++; |
| 3554 | |
| 3555 | /* find the end of the value, respecting quotes */ |
| 3556 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3557 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3558 | /* 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] | 3559 | val_end = next; |
| 3560 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3561 | val_end--; |
| 3562 | } |
| 3563 | else { |
| 3564 | /* <equal> points to next comma, semi-colon or EOL */ |
| 3565 | val_beg = val_end = next = equal; |
| 3566 | } |
| 3567 | |
| 3568 | if (next < hdr_end) { |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3569 | /* For Set-Cookie, since commas are permitted |
| 3570 | * in values, skip to the end. |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3571 | */ |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3572 | next = hdr_end; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3573 | } |
| 3574 | |
| 3575 | /* Now everything is as on the diagram above */ |
| 3576 | |
| 3577 | /* Ignore cookies with no equal sign */ |
| 3578 | if (equal == val_end) |
| 3579 | continue; |
| 3580 | |
| 3581 | /* If there are spaces around the equal sign, we need to |
| 3582 | * strip them otherwise we'll get trouble for cookie captures, |
| 3583 | * or even for rewrites. Since this happens extremely rarely, |
| 3584 | * it does not hurt performance. |
| 3585 | */ |
| 3586 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3587 | int stripped_before = 0; |
| 3588 | int stripped_after = 0; |
| 3589 | |
| 3590 | if (att_end != equal) { |
| 3591 | memmove(att_end, equal, hdr_end - equal); |
| 3592 | stripped_before = (att_end - equal); |
| 3593 | equal += stripped_before; |
| 3594 | val_beg += stripped_before; |
| 3595 | } |
| 3596 | |
| 3597 | if (val_beg > equal + 1) { |
| 3598 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3599 | stripped_after = (equal + 1) - val_beg; |
| 3600 | val_beg += stripped_after; |
| 3601 | stripped_before += stripped_after; |
| 3602 | } |
| 3603 | |
| 3604 | val_end += stripped_before; |
| 3605 | next += stripped_before; |
| 3606 | hdr_end += stripped_before; |
| 3607 | |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3608 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3609 | ctx.value.len = hdr_end - hdr_beg; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3610 | } |
| 3611 | |
| 3612 | /* First, let's see if we want to capture this cookie. We check |
| 3613 | * that we don't already have a server side cookie, because we |
| 3614 | * can only capture one. Also as an optimisation, we ignore |
| 3615 | * cookies shorter than the declared name. |
| 3616 | */ |
| 3617 | if (sess->fe->capture_name != NULL && |
| 3618 | txn->srv_cookie == NULL && |
| 3619 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3620 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3621 | int log_len = val_end - att_beg; |
| 3622 | if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3623 | ha_alert("HTTP logging : out of memory.\n"); |
| 3624 | } |
| 3625 | else { |
| 3626 | if (log_len > sess->fe->capture_len) |
| 3627 | log_len = sess->fe->capture_len; |
| 3628 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 3629 | txn->srv_cookie[log_len] = 0; |
| 3630 | } |
| 3631 | } |
| 3632 | |
| 3633 | srv = objt_server(s->target); |
| 3634 | /* now check if we need to process it for persistence */ |
| 3635 | if (!(s->flags & SF_IGNORE_PRST) && |
| 3636 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3637 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3638 | /* assume passive cookie by default */ |
| 3639 | txn->flags &= ~TX_SCK_MASK; |
| 3640 | txn->flags |= TX_SCK_FOUND; |
| 3641 | |
| 3642 | /* If the cookie is in insert mode on a known server, we'll delete |
| 3643 | * this occurrence because we'll insert another one later. |
| 3644 | * We'll delete it too if the "indirect" option is set and we're in |
| 3645 | * a direct access. |
| 3646 | */ |
| 3647 | if (s->be->ck_opts & PR_CK_PSV) { |
| 3648 | /* The "preserve" flag was set, we don't want to touch the |
| 3649 | * server's cookie. |
| 3650 | */ |
| 3651 | } |
| 3652 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
| 3653 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
| 3654 | /* this cookie must be deleted */ |
| 3655 | if (prev == hdr_beg && next == hdr_end) { |
| 3656 | /* whole header */ |
| 3657 | http_remove_header(htx, &ctx); |
| 3658 | /* note: while both invalid now, <next> and <hdr_end> |
| 3659 | * are still equal, so the for() will stop as expected. |
| 3660 | */ |
| 3661 | } else { |
| 3662 | /* just remove the value */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3663 | int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3664 | next = prev; |
| 3665 | hdr_end += delta; |
| 3666 | } |
| 3667 | txn->flags &= ~TX_SCK_MASK; |
| 3668 | txn->flags |= TX_SCK_DELETED; |
| 3669 | /* and go on with next cookie */ |
| 3670 | } |
| 3671 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
| 3672 | /* replace bytes val_beg->val_end with the cookie name associated |
| 3673 | * with this server since we know it. |
| 3674 | */ |
| 3675 | int sliding, delta; |
| 3676 | |
| 3677 | ctx.value = ist2(val_beg, val_end - val_beg); |
| 3678 | ctx.lws_before = ctx.lws_after = 0; |
| 3679 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen)); |
| 3680 | delta = srv->cklen - (val_end - val_beg); |
| 3681 | sliding = (ctx.value.ptr - val_beg); |
| 3682 | hdr_beg += sliding; |
| 3683 | val_beg += sliding; |
| 3684 | next += sliding + delta; |
| 3685 | hdr_end += sliding + delta; |
| 3686 | |
| 3687 | txn->flags &= ~TX_SCK_MASK; |
| 3688 | txn->flags |= TX_SCK_REPLACED; |
| 3689 | } |
| 3690 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
| 3691 | /* insert the cookie name associated with this server |
| 3692 | * before existing cookie, and insert a delimiter between them.. |
| 3693 | */ |
| 3694 | int sliding, delta; |
| 3695 | ctx.value = ist2(val_beg, 0); |
| 3696 | ctx.lws_before = ctx.lws_after = 0; |
| 3697 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1)); |
| 3698 | delta = srv->cklen + 1; |
| 3699 | sliding = (ctx.value.ptr - val_beg); |
| 3700 | hdr_beg += sliding; |
| 3701 | val_beg += sliding; |
| 3702 | next += sliding + delta; |
| 3703 | hdr_end += sliding + delta; |
| 3704 | |
| 3705 | val_beg[srv->cklen] = COOKIE_DELIM; |
| 3706 | txn->flags &= ~TX_SCK_MASK; |
| 3707 | txn->flags |= TX_SCK_REPLACED; |
| 3708 | } |
| 3709 | } |
| 3710 | /* that's done for this cookie, check the next one on the same |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3711 | * line when next != hdr_end (which should normally not happen |
| 3712 | * with set-cookie2 support removed). |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3713 | */ |
| 3714 | } |
| 3715 | } |
| 3716 | } |
| 3717 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3718 | /* |
| 3719 | * Parses the Cache-Control and Pragma request header fields to determine if |
| 3720 | * the request may be served from the cache and/or if it is cacheable. Updates |
| 3721 | * s->txn->flags. |
| 3722 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3723 | void http_check_request_for_cacheability(struct stream *s, struct channel *req) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3724 | { |
| 3725 | struct http_txn *txn = s->txn; |
| 3726 | struct htx *htx; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3727 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 3728 | int pragma_found, cc_found; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3729 | |
| 3730 | if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE) |
| 3731 | return; /* nothing more to do here */ |
| 3732 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3733 | htx = htxbuf(&req->buf); |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3734 | pragma_found = cc_found = 0; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3735 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3736 | /* Check "pragma" header for HTTP/1.0 compatibility. */ |
| 3737 | if (http_find_header(htx, ist("pragma"), &ctx, 1)) { |
| 3738 | if (isteqi(ctx.value, ist("no-cache"))) { |
| 3739 | pragma_found = 1; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3740 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3741 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3742 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3743 | ctx.blk = NULL; |
| 3744 | /* Don't use the cache and don't try to store if we found the |
| 3745 | * Authorization header */ |
| 3746 | if (http_find_header(htx, ist("authorization"), &ctx, 1)) { |
| 3747 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3748 | txn->flags |= TX_CACHE_IGNORE; |
| 3749 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3750 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3751 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3752 | /* Look for "cache-control" header and iterate over all the values |
| 3753 | * until we find one that specifies that caching is possible or not. */ |
| 3754 | ctx.blk = NULL; |
| 3755 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3756 | cc_found = 1; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3757 | /* We don't check the values after max-age, max-stale nor min-fresh, |
| 3758 | * we simply don't use the cache when they're specified. */ |
| 3759 | if (istmatchi(ctx.value, ist("max-age")) || |
| 3760 | istmatchi(ctx.value, ist("no-cache")) || |
| 3761 | istmatchi(ctx.value, ist("max-stale")) || |
| 3762 | istmatchi(ctx.value, ist("min-fresh"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3763 | txn->flags |= TX_CACHE_IGNORE; |
| 3764 | continue; |
| 3765 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3766 | if (istmatchi(ctx.value, ist("no-store"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3767 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3768 | continue; |
| 3769 | } |
| 3770 | } |
| 3771 | |
| 3772 | /* RFC7234#5.4: |
| 3773 | * When the Cache-Control header field is also present and |
| 3774 | * understood in a request, Pragma is ignored. |
| 3775 | * When the Cache-Control header field is not present in a |
| 3776 | * request, caches MUST consider the no-cache request |
| 3777 | * pragma-directive as having the same effect as if |
| 3778 | * "Cache-Control: no-cache" were present. |
| 3779 | */ |
| 3780 | if (!cc_found && pragma_found) |
| 3781 | txn->flags |= TX_CACHE_IGNORE; |
| 3782 | } |
| 3783 | |
| 3784 | /* |
| 3785 | * Check if response is cacheable or not. Updates s->txn->flags. |
| 3786 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3787 | void http_check_response_for_cacheability(struct stream *s, struct channel *res) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3788 | { |
| 3789 | struct http_txn *txn = s->txn; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3790 | struct http_hdr_ctx ctx = { .blk = NULL }; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3791 | struct htx *htx; |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3792 | int has_freshness_info = 0; |
| 3793 | int has_validator = 0; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3794 | |
| 3795 | if (txn->status < 200) { |
| 3796 | /* do not try to cache interim responses! */ |
| 3797 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3798 | return; |
| 3799 | } |
| 3800 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3801 | htx = htxbuf(&res->buf); |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3802 | /* Check "pragma" header for HTTP/1.0 compatibility. */ |
| 3803 | if (http_find_header(htx, ist("pragma"), &ctx, 1)) { |
| 3804 | if (isteqi(ctx.value, ist("no-cache"))) { |
| 3805 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3806 | return; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3807 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3808 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3809 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3810 | /* Look for "cache-control" header and iterate over all the values |
| 3811 | * until we find one that specifies that caching is possible or not. */ |
| 3812 | ctx.blk = NULL; |
| 3813 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
| 3814 | if (isteqi(ctx.value, ist("public"))) { |
| 3815 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3816 | continue; |
| 3817 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3818 | if (isteqi(ctx.value, ist("private")) || |
| 3819 | isteqi(ctx.value, ist("no-cache")) || |
| 3820 | isteqi(ctx.value, ist("no-store")) || |
| 3821 | isteqi(ctx.value, ist("max-age=0")) || |
| 3822 | isteqi(ctx.value, ist("s-maxage=0"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3823 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3824 | continue; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3825 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3826 | /* We might have a no-cache="set-cookie" form. */ |
| 3827 | if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) { |
| 3828 | txn->flags &= ~TX_CACHE_COOK; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3829 | continue; |
| 3830 | } |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3831 | |
| 3832 | if (istmatchi(ctx.value, ist("s-maxage")) || |
| 3833 | istmatchi(ctx.value, ist("max-age"))) { |
| 3834 | has_freshness_info = 1; |
| 3835 | continue; |
| 3836 | } |
| 3837 | } |
| 3838 | |
| 3839 | /* If no freshness information could be found in Cache-Control values, |
| 3840 | * look for an Expires header. */ |
| 3841 | if (!has_freshness_info) { |
| 3842 | ctx.blk = NULL; |
| 3843 | has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0); |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3844 | } |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3845 | |
| 3846 | /* If no freshness information could be found in Cache-Control or Expires |
| 3847 | * values, look for an explicit validator. */ |
| 3848 | if (!has_freshness_info) { |
| 3849 | ctx.blk = NULL; |
| 3850 | has_validator = 1; |
| 3851 | if (!http_find_header(htx, ist("etag"), &ctx, 0)) { |
| 3852 | ctx.blk = NULL; |
| 3853 | if (!http_find_header(htx, ist("last-modified"), &ctx, 0)) |
| 3854 | has_validator = 0; |
| 3855 | } |
| 3856 | } |
| 3857 | |
| 3858 | /* We won't store an entry that has neither a cache validator nor an |
| 3859 | * explicit expiration time, as suggested in RFC 7234#3. */ |
| 3860 | if (!has_freshness_info && !has_validator) |
| 3861 | txn->flags |= TX_CACHE_IGNORE; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3862 | } |
| 3863 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3864 | /* |
| 3865 | * In a GET, HEAD or POST request, check if the requested URI matches the stats uri |
| 3866 | * for the current backend. |
| 3867 | * |
| 3868 | * It is assumed that the request is either a HEAD, GET, or POST and that the |
| 3869 | * uri_auth field is valid. |
| 3870 | * |
| 3871 | * Returns 1 if stats should be provided, otherwise 0. |
| 3872 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3873 | 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] | 3874 | { |
| 3875 | struct uri_auth *uri_auth = backend->uri_auth; |
| 3876 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3877 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3878 | struct ist uri; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3879 | |
| 3880 | if (!uri_auth) |
| 3881 | return 0; |
| 3882 | |
| 3883 | if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST) |
| 3884 | return 0; |
| 3885 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3886 | htx = htxbuf(&s->req.buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 3887 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3888 | uri = htx_sl_req_uri(sl); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 3889 | if (*uri_auth->uri_prefix == '/') { |
| 3890 | struct http_uri_parser parser = http_uri_parser_init(uri); |
| 3891 | uri = http_parse_path(&parser); |
| 3892 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3893 | |
| 3894 | /* check URI size */ |
| 3895 | if (uri_auth->uri_len > uri.len) |
| 3896 | return 0; |
| 3897 | |
| 3898 | if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
| 3899 | return 0; |
| 3900 | |
| 3901 | return 1; |
| 3902 | } |
| 3903 | |
| 3904 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3905 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3906 | * and program a response message if something was unexpected. It cannot fail |
| 3907 | * and always relies on the stats applet to complete the job. It does not touch |
| 3908 | * analysers nor counters, which are left to the caller. It does not touch |
| 3909 | * s->target which is supposed to already point to the stats applet. The caller |
| 3910 | * is expected to have already assigned an appctx to the stream. |
| 3911 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3912 | static int http_handle_stats(struct stream *s, struct channel *req) |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3913 | { |
| 3914 | struct stats_admin_rule *stats_admin_rule; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3915 | struct session *sess = s->sess; |
| 3916 | struct http_txn *txn = s->txn; |
| 3917 | struct http_msg *msg = &txn->req; |
| 3918 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3919 | const char *h, *lookup, *end; |
Willy Tarreau | 8e7c6e6 | 2022-05-18 17:58:02 +0200 | [diff] [blame] | 3920 | struct appctx *appctx = __sc_appctx(s->scb); |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3921 | struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3922 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3923 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3924 | |
Willy Tarreau | 41f8852 | 2022-05-03 18:39:27 +0200 | [diff] [blame] | 3925 | appctx->st1 = 0; |
Willy Tarreau | 6ef1648 | 2022-05-06 18:07:53 +0200 | [diff] [blame] | 3926 | ctx->state = STAT_STATE_INIT; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3927 | ctx->st_code = STAT_STATUS_INIT; |
| 3928 | ctx->flags |= uri_auth->flags; |
| 3929 | ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3930 | if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD)) |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3931 | ctx->flags |= STAT_CHUNKED; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3932 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3933 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 3934 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3935 | lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len; |
| 3936 | end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3937 | |
| 3938 | for (h = lookup; h <= end - 3; h++) { |
| 3939 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3940 | ctx->flags |= STAT_HIDE_DOWN; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3941 | break; |
| 3942 | } |
Amaury Denoyelle | 91e55ea | 2021-02-25 14:46:08 +0100 | [diff] [blame] | 3943 | } |
| 3944 | |
| 3945 | for (h = lookup; h <= end - 9; h++) { |
| 3946 | if (memcmp(h, ";no-maint", 9) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3947 | ctx->flags |= STAT_HIDE_MAINT; |
Willy Tarreau | 3e32036 | 2020-10-23 17:28:57 +0200 | [diff] [blame] | 3948 | break; |
| 3949 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3950 | } |
| 3951 | |
| 3952 | if (uri_auth->refresh) { |
| 3953 | for (h = lookup; h <= end - 10; h++) { |
| 3954 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3955 | ctx->flags |= STAT_NO_REFRESH; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3956 | break; |
| 3957 | } |
| 3958 | } |
| 3959 | } |
| 3960 | |
| 3961 | for (h = lookup; h <= end - 4; h++) { |
| 3962 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3963 | ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3964 | break; |
| 3965 | } |
| 3966 | } |
| 3967 | |
| 3968 | for (h = lookup; h <= end - 6; h++) { |
| 3969 | if (memcmp(h, ";typed", 6) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3970 | ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
| 3971 | ctx->flags |= STAT_FMT_TYPED; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3972 | break; |
| 3973 | } |
| 3974 | } |
| 3975 | |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 3976 | for (h = lookup; h <= end - 5; h++) { |
| 3977 | if (memcmp(h, ";json", 5) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3978 | ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
| 3979 | ctx->flags |= STAT_FMT_JSON; |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 3980 | break; |
| 3981 | } |
| 3982 | } |
| 3983 | |
| 3984 | for (h = lookup; h <= end - 12; h++) { |
| 3985 | if (memcmp(h, ";json-schema", 12) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3986 | ctx->flags &= ~STAT_FMT_MASK; |
| 3987 | ctx->flags |= STAT_JSON_SCHM; |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 3988 | break; |
| 3989 | } |
| 3990 | } |
| 3991 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3992 | for (h = lookup; h <= end - 8; h++) { |
| 3993 | if (memcmp(h, ";st=", 4) == 0) { |
| 3994 | int i; |
| 3995 | h += 4; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3996 | ctx->st_code = STAT_STATUS_UNKN; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3997 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3998 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3999 | ctx->st_code = i; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4000 | break; |
| 4001 | } |
| 4002 | } |
| 4003 | break; |
| 4004 | } |
| 4005 | } |
| 4006 | |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4007 | ctx->scope_str = 0; |
| 4008 | ctx->scope_len = 0; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4009 | for (h = lookup; h <= end - 8; h++) { |
| 4010 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 4011 | int itx = 0; |
| 4012 | const char *h2; |
| 4013 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 4014 | const char *err; |
| 4015 | |
| 4016 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 4017 | h2 = h; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4018 | ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl); |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 4019 | while (h < end) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4020 | if (*h == ';' || *h == '&' || *h == ' ') |
| 4021 | break; |
| 4022 | itx++; |
| 4023 | h++; |
| 4024 | } |
| 4025 | |
| 4026 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 4027 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4028 | ctx->scope_len = itx; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4029 | |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4030 | /* 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] | 4031 | memcpy(scope_txt, h2, itx); |
| 4032 | scope_txt[itx] = '\0'; |
| 4033 | err = invalid_char(scope_txt); |
| 4034 | if (err) { |
| 4035 | /* bad char in search text => clear scope */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4036 | ctx->scope_str = 0; |
| 4037 | ctx->scope_len = 0; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4038 | } |
| 4039 | break; |
| 4040 | } |
| 4041 | } |
| 4042 | |
| 4043 | /* now check whether we have some admin rules for this request */ |
| 4044 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
| 4045 | int ret = 1; |
| 4046 | |
| 4047 | if (stats_admin_rule->cond) { |
| 4048 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 4049 | ret = acl_pass(ret); |
| 4050 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 4051 | ret = !ret; |
| 4052 | } |
| 4053 | |
| 4054 | if (ret) { |
| 4055 | /* no rule, or the rule matches */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4056 | ctx->flags |= STAT_ADMIN; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4057 | break; |
| 4058 | } |
| 4059 | } |
| 4060 | |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4061 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 4062 | appctx->st0 = STAT_HTTP_HEAD; |
| 4063 | else if (txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4064 | if (ctx->flags & STAT_ADMIN) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4065 | appctx->st0 = STAT_HTTP_POST; |
Christopher Faulet | bd9e842 | 2019-08-15 22:26:48 +0200 | [diff] [blame] | 4066 | if (msg->msg_state < HTTP_MSG_DATA) |
| 4067 | req->analysers |= AN_REQ_HTTP_BODY; |
| 4068 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4069 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4070 | /* POST without admin level */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4071 | ctx->flags &= ~STAT_CHUNKED; |
| 4072 | ctx->st_code = STAT_STATUS_DENY; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4073 | appctx->st0 = STAT_HTTP_LAST; |
| 4074 | } |
| 4075 | } |
| 4076 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4077 | /* Unsupported method */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4078 | ctx->flags &= ~STAT_CHUNKED; |
| 4079 | ctx->st_code = STAT_STATUS_IVAL; |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4080 | appctx->st0 = STAT_HTTP_LAST; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | s->task->nice = -32; /* small boost for HTTP statistics */ |
| 4084 | return 1; |
| 4085 | } |
| 4086 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4087 | /* This function waits for the message payload at most <time> milliseconds (may |
| 4088 | * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the |
| 4089 | * payload are received (0 means no limit). It returns HTTP_RULE_* depending on |
| 4090 | * the result: |
| 4091 | * |
| 4092 | * - HTTP_RULE_RES_CONT when conditions are met to stop waiting |
| 4093 | * - HTTP_RULE_RES_YIELD to wait for more data |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 4094 | * - HTTP_RULE_RES_ABRT when a timeout occurred. |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4095 | * - 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] | 4096 | * - HTTP_RULE_RES_ERROR if an internal error occurred |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4097 | * |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 4098 | * 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] | 4099 | * to the client, depending on the channel (408 on request side, 504 on response |
| 4100 | * side). All other errors must be handled by the caller. |
| 4101 | */ |
| 4102 | enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn, |
| 4103 | unsigned int time, unsigned int bytes) |
| 4104 | { |
| 4105 | struct session *sess = s->sess; |
| 4106 | struct http_txn *txn = s->txn; |
| 4107 | struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req); |
| 4108 | struct htx *htx; |
| 4109 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 4110 | |
| 4111 | htx = htxbuf(&chn->buf); |
| 4112 | |
| 4113 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
| 4114 | ret = HTTP_RULE_RES_BADREQ; |
| 4115 | goto end; |
| 4116 | } |
| 4117 | if (htx->flags & HTX_FL_PROCESSING_ERROR) { |
| 4118 | ret = HTTP_RULE_RES_ERROR; |
| 4119 | goto end; |
| 4120 | } |
| 4121 | |
| 4122 | /* Do nothing for bodyless and CONNECT requests */ |
| 4123 | if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS)) |
| 4124 | goto end; |
| 4125 | |
| 4126 | if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) { |
| 4127 | if (http_handle_expect_hdr(s, htx, msg) == -1) { |
| 4128 | ret = HTTP_RULE_RES_ERROR; |
| 4129 | goto end; |
| 4130 | } |
| 4131 | } |
| 4132 | |
| 4133 | msg->msg_state = HTTP_MSG_DATA; |
| 4134 | |
| 4135 | /* Now we're in HTTP_MSG_DATA. We just need to know if all data have |
| 4136 | * been received or if the buffer is full. |
| 4137 | */ |
Christopher Faulet | 7833596 | 2021-09-23 14:46:32 +0200 | [diff] [blame] | 4138 | if ((htx->flags & HTX_FL_EOM) || |
| 4139 | htx_get_tail_type(htx) > HTX_BLK_DATA || |
| 4140 | channel_htx_full(chn, htx, global.tune.maxrewrite) || |
Willy Tarreau | 99615ed | 2022-05-25 07:29:36 +0200 | [diff] [blame] | 4141 | sc_waiting_room(chn_prod(chn))) |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4142 | goto end; |
| 4143 | |
| 4144 | if (bytes) { |
| 4145 | struct htx_blk *blk; |
| 4146 | unsigned int len = 0; |
| 4147 | |
| 4148 | for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 4149 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 4150 | continue; |
| 4151 | len += htx_get_blksz(blk); |
| 4152 | if (len >= bytes) |
| 4153 | goto end; |
| 4154 | } |
| 4155 | } |
| 4156 | |
| 4157 | if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) { |
| 4158 | if (!(chn->flags & CF_ISRESP)) |
| 4159 | goto abort_req; |
| 4160 | goto abort_res; |
| 4161 | } |
| 4162 | |
| 4163 | /* we get here if we need to wait for more data */ |
Christopher Faulet | 4b490b7 | 2023-01-04 11:55:15 +0100 | [diff] [blame] | 4164 | if (!(chn->flags & CF_SHUTR)) { |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4165 | if (!tick_isset(chn->analyse_exp)) |
| 4166 | chn->analyse_exp = tick_add_ifset(now_ms, time); |
| 4167 | ret = HTTP_RULE_RES_YIELD; |
| 4168 | } |
| 4169 | |
| 4170 | end: |
| 4171 | return ret; |
| 4172 | |
Christopher Faulet | f0d80df | 2023-01-13 10:20:20 +0100 | [diff] [blame] | 4173 | abort: |
| 4174 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 4175 | ret = HTTP_RULE_RES_ABRT; |
| 4176 | goto end; |
| 4177 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4178 | abort_req: |
| 4179 | txn->status = 408; |
| 4180 | if (!(s->flags & SF_ERR_MASK)) |
| 4181 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4182 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4183 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4184 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | f0d80df | 2023-01-13 10:20:20 +0100 | [diff] [blame] | 4185 | goto abort; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4186 | |
| 4187 | abort_res: |
| 4188 | txn->status = 504; |
| 4189 | if (!(s->flags & SF_ERR_MASK)) |
| 4190 | s->flags |= SF_ERR_SRVTO; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4191 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | f0d80df | 2023-01-13 10:20:20 +0100 | [diff] [blame] | 4192 | goto abort; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4193 | } |
| 4194 | |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4195 | void http_perform_server_redirect(struct stream *s, struct stconn *sc) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4196 | { |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4197 | struct channel *req = &s->req; |
| 4198 | struct channel *res = &s->res; |
| 4199 | struct server *srv; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4200 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4201 | struct htx_sl *sl; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4202 | struct ist path, location; |
| 4203 | unsigned int flags; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4204 | struct http_uri_parser parser; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4205 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4206 | /* |
| 4207 | * Create the location |
| 4208 | */ |
| 4209 | chunk_reset(&trash); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4210 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4211 | /* 1: add the server's prefix */ |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4212 | /* special prefix "/" means don't change URL */ |
| 4213 | srv = __objt_server(s->target); |
| 4214 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 4215 | if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len)) |
| 4216 | return; |
| 4217 | } |
| 4218 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4219 | /* 2: add the request Path */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4220 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 4221 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4222 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4223 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4224 | if (!isttest(path)) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4225 | return; |
| 4226 | |
| 4227 | if (!chunk_memcat(&trash, path.ptr, path.len)) |
| 4228 | return; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4229 | location = ist2(trash.area, trash.data); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4230 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4231 | /* |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 4232 | * Create the 302 response |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4233 | */ |
| 4234 | htx = htx_from_buf(&res->buf); |
| 4235 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 4236 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4237 | ist("HTTP/1.1"), ist("302"), ist("Found")); |
| 4238 | if (!sl) |
| 4239 | goto fail; |
| 4240 | sl->info.res.status = 302; |
| 4241 | s->txn->status = 302; |
| 4242 | |
| 4243 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) || |
| 4244 | !htx_add_header(htx, ist("Connection"), ist("close")) || |
| 4245 | !htx_add_header(htx, ist("Content-length"), ist("0")) || |
| 4246 | !htx_add_header(htx, ist("Location"), location)) |
| 4247 | goto fail; |
| 4248 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4249 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4250 | goto fail; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4251 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4252 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | c20afb8 | 2020-01-24 19:16:26 +0100 | [diff] [blame] | 4253 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4254 | if (!http_forward_proxy_resp(s, 1)) |
| 4255 | goto fail; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4256 | |
| 4257 | /* return without error. */ |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4258 | sc_shutr(sc); |
| 4259 | sc_shutw(sc); |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4260 | s->conn_err_type = STRM_ET_NONE; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4261 | sc->state = SC_ST_CLO; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4262 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4263 | if (!(s->flags & SF_ERR_MASK)) |
| 4264 | s->flags |= SF_ERR_LOCAL; |
| 4265 | if (!(s->flags & SF_FINST_MASK)) |
| 4266 | s->flags |= SF_FINST_C; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4267 | |
| 4268 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 4269 | srv_inc_sess_ctr(srv); |
| 4270 | srv_set_sess_last(srv); |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4271 | return; |
| 4272 | |
| 4273 | fail: |
| 4274 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4275 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4276 | channel_htx_truncate(res, htx); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4277 | } |
| 4278 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4279 | /* This function terminates the request because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4280 | * because an error was triggered during the body forwarding. |
| 4281 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4282 | static void http_end_request(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4283 | { |
| 4284 | struct channel *chn = &s->req; |
| 4285 | struct http_txn *txn = s->txn; |
| 4286 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4287 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4288 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4289 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) { |
| 4290 | 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] | 4291 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4292 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4293 | |
| 4294 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4295 | /* No need to read anymore, the request was completely parsed. |
| 4296 | * We can shut the read side unless we want to abort_on_close, |
| 4297 | * or we have a POST request. The issue with POST requests is |
| 4298 | * that some browsers still send a CRLF after the request, and |
| 4299 | * this CRLF must be read so that it does not remain in the kernel |
| 4300 | * buffers, otherwise a close could cause an RST on some systems |
| 4301 | * (eg: Linux). |
| 4302 | */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 4303 | 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] | 4304 | channel_dont_read(chn); |
| 4305 | |
| 4306 | /* if the server closes the connection, we want to immediately react |
| 4307 | * and close the socket to save packets and syscalls. |
| 4308 | */ |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 4309 | s->scb->flags |= SC_FL_NOHALF; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4310 | |
| 4311 | /* In any case we've finished parsing the request so we must |
| 4312 | * disable Nagle when sending data because 1) we're not going |
| 4313 | * to shut this side, and 2) the server is waiting for us to |
| 4314 | * send pending data. |
| 4315 | */ |
| 4316 | chn->flags |= CF_NEVER_WAIT; |
| 4317 | |
Christopher Faulet | d01ce40 | 2019-01-02 17:44:13 +0100 | [diff] [blame] | 4318 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4319 | /* The server has not finished to respond, so we |
| 4320 | * don't want to move in order not to upset it. |
| 4321 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4322 | 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] | 4323 | return; |
| 4324 | } |
| 4325 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4326 | /* When we get here, it means that both the request and the |
| 4327 | * response have finished receiving. Depending on the connection |
| 4328 | * mode, we'll have to wait for the last bytes to leave in either |
| 4329 | * direction, and sometimes for a close to be effective. |
| 4330 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4331 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4332 | /* Tunnel mode will not have any analyser so it needs to |
| 4333 | * poll for reads. |
| 4334 | */ |
| 4335 | channel_auto_read(chn); |
| 4336 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 4337 | } |
| 4338 | else { |
| 4339 | /* we're not expecting any new data to come for this |
| 4340 | * transaction, so we can close it. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4341 | * |
| 4342 | * However, there is an exception if the response |
| 4343 | * length is undefined. In this case, we need to wait |
| 4344 | * the close from the server. The response will be |
| 4345 | * switched in TUNNEL mode until the end. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4346 | */ |
| 4347 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 4348 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4349 | goto check_channel_flags; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4350 | |
| 4351 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4352 | channel_shutr_now(chn); |
| 4353 | channel_shutw_now(chn); |
| 4354 | } |
| 4355 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4356 | goto check_channel_flags; |
| 4357 | } |
| 4358 | |
| 4359 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4360 | http_msg_closing: |
| 4361 | /* nothing else to forward, just waiting for the output buffer |
| 4362 | * to be empty and for the shutw_now to take effect. |
| 4363 | */ |
| 4364 | if (channel_is_empty(chn)) { |
| 4365 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4366 | goto http_msg_closed; |
| 4367 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4368 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4369 | return; |
| 4370 | } |
| 4371 | |
| 4372 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4373 | http_msg_closed: |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4374 | /* if we don't know whether the server will close, we need to hard close */ |
| 4375 | if (txn->rsp.flags & HTTP_MSGF_XFER_LEN) |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 4376 | s->scb->flags |= SC_FL_NOLINGER; /* we want to close ASAP */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4377 | /* 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] | 4378 | if (!(s->be->options & PR_O_ABRT_CLOSE)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4379 | channel_dont_read(chn); |
| 4380 | goto end; |
| 4381 | } |
| 4382 | |
| 4383 | check_channel_flags: |
| 4384 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4385 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4386 | /* if we've just closed an output, let's switch */ |
| 4387 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4388 | goto http_msg_closing; |
| 4389 | } |
| 4390 | |
| 4391 | end: |
| 4392 | chn->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4393 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4394 | chn->flags |= CF_NEVER_WAIT; |
| 4395 | if (HAS_REQ_DATA_FILTERS(s)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4396 | chn->analysers |= AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4397 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4398 | channel_auto_close(chn); |
| 4399 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4400 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4401 | } |
| 4402 | |
| 4403 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4404 | /* This function terminates the response because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4405 | * because an error was triggered during the body forwarding. |
| 4406 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4407 | static void http_end_response(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4408 | { |
| 4409 | struct channel *chn = &s->res; |
| 4410 | struct http_txn *txn = s->txn; |
| 4411 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4412 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4413 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4414 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) { |
| 4415 | 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] | 4416 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4417 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4418 | |
| 4419 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4420 | /* In theory, we don't need to read anymore, but we must |
| 4421 | * still monitor the server connection for a possible close |
| 4422 | * while the request is being uploaded, so we don't disable |
| 4423 | * reading. |
| 4424 | */ |
| 4425 | /* channel_dont_read(chn); */ |
| 4426 | |
| 4427 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4428 | /* The client seems to still be sending data, probably |
| 4429 | * because we got an error response during an upload. |
| 4430 | * We have the choice of either breaking the connection |
| 4431 | * or letting it pass through. Let's do the later. |
| 4432 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4433 | 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] | 4434 | return; |
| 4435 | } |
| 4436 | |
| 4437 | /* When we get here, it means that both the request and the |
| 4438 | * response have finished receiving. Depending on the connection |
| 4439 | * mode, we'll have to wait for the last bytes to leave in either |
| 4440 | * direction, and sometimes for a close to be effective. |
| 4441 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4442 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4443 | channel_auto_read(chn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4444 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4445 | } |
| 4446 | else { |
| 4447 | /* we're not expecting any new data to come for this |
| 4448 | * transaction, so we can close it. |
| 4449 | */ |
| 4450 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4451 | channel_shutr_now(chn); |
| 4452 | channel_shutw_now(chn); |
| 4453 | } |
| 4454 | } |
| 4455 | goto check_channel_flags; |
| 4456 | } |
| 4457 | |
| 4458 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4459 | http_msg_closing: |
| 4460 | /* nothing else to forward, just waiting for the output buffer |
| 4461 | * to be empty and for the shutw_now to take effect. |
| 4462 | */ |
| 4463 | if (channel_is_empty(chn)) { |
| 4464 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4465 | goto http_msg_closed; |
| 4466 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4467 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4468 | return; |
| 4469 | } |
| 4470 | |
| 4471 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4472 | http_msg_closed: |
| 4473 | /* drop any pending data */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4474 | channel_htx_truncate(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4475 | channel_abort(&s->req); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4476 | goto end; |
| 4477 | } |
| 4478 | |
| 4479 | check_channel_flags: |
| 4480 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4481 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4482 | /* if we've just closed an output, let's switch */ |
| 4483 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4484 | goto http_msg_closing; |
| 4485 | } |
| 4486 | |
| 4487 | end: |
| 4488 | chn->analysers &= AN_RES_FLT_END; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4489 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4490 | chn->flags |= CF_NEVER_WAIT; |
| 4491 | if (HAS_RSP_DATA_FILTERS(s)) |
| 4492 | chn->analysers |= AN_RES_FLT_XFER_DATA; |
| 4493 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4494 | channel_auto_close(chn); |
| 4495 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4496 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4497 | } |
| 4498 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4499 | /* Forward a response generated by HAProxy (error/redirect/return). This |
| 4500 | * function forwards all pending incoming data. If <final> is set to 0, nothing |
| 4501 | * more is performed. It is used for 1xx informational messages. Otherwise, the |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4502 | * transaction is terminated and the request is emptied. On success 1 is |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4503 | * returned. If an error occurred, 0 is returned. If it fails, this function |
| 4504 | * only exits. It is the caller responsibility to do the cleanup. |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4505 | */ |
| 4506 | int http_forward_proxy_resp(struct stream *s, int final) |
| 4507 | { |
| 4508 | struct channel *req = &s->req; |
| 4509 | struct channel *res = &s->res; |
| 4510 | struct htx *htx = htxbuf(&res->buf); |
| 4511 | size_t data; |
| 4512 | |
| 4513 | if (final) { |
| 4514 | htx->flags |= HTX_FL_PROXY_RESP; |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4515 | |
Christopher Faulet | aab1b67 | 2020-11-18 16:44:02 +0100 | [diff] [blame] | 4516 | if (!htx_is_empty(htx) && !http_eval_after_res_rules(s)) |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 4517 | return 0; |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4518 | |
Christopher Faulet | d6c4836 | 2020-10-19 18:01:38 +0200 | [diff] [blame] | 4519 | if (s->txn->meth == HTTP_METH_HEAD) |
| 4520 | htx_skip_msg_payload(htx); |
| 4521 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4522 | channel_auto_read(req); |
| 4523 | channel_abort(req); |
| 4524 | channel_auto_close(req); |
| 4525 | channel_htx_erase(req, htxbuf(&req->buf)); |
| 4526 | |
| 4527 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 4528 | channel_auto_read(res); |
| 4529 | channel_auto_close(res); |
| 4530 | channel_shutr_now(res); |
Christopher Faulet | 1a9db7c | 2020-06-25 15:36:45 +0200 | [diff] [blame] | 4531 | res->flags |= CF_EOI; /* The response is terminated, add EOI */ |
Christopher Faulet | 42432f3 | 2020-11-20 17:43:16 +0100 | [diff] [blame] | 4532 | htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */ |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4533 | } |
Christopher Faulet | cf6898c | 2020-06-25 15:55:11 +0200 | [diff] [blame] | 4534 | else { |
| 4535 | /* Send ASAP informational messages. Rely on CF_EOI for final |
| 4536 | * response. |
| 4537 | */ |
| 4538 | res->flags |= CF_SEND_DONTWAIT; |
| 4539 | } |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4540 | |
| 4541 | data = htx->data - co_data(res); |
| 4542 | c_adv(res, data); |
| 4543 | htx->first = -1; |
| 4544 | res->total += data; |
| 4545 | return 1; |
| 4546 | } |
| 4547 | |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4548 | void http_server_error(struct stream *s, struct stconn *sc, int err, |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4549 | int finst, struct http_reply *msg) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4550 | { |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4551 | http_reply_and_close(s, s->txn->status, msg); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4552 | if (!(s->flags & SF_ERR_MASK)) |
| 4553 | s->flags |= err; |
| 4554 | if (!(s->flags & SF_FINST_MASK)) |
| 4555 | s->flags |= finst; |
| 4556 | } |
| 4557 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4558 | 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] | 4559 | { |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4560 | if (!msg) { |
| 4561 | channel_htx_truncate(&s->res, htxbuf(&s->res.buf)); |
| 4562 | goto end; |
| 4563 | } |
| 4564 | |
| 4565 | if (http_reply_message(s, msg) == -1) { |
| 4566 | /* 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] | 4567 | * it is already an internal error. If it was already a "const" |
| 4568 | * 500 error, just fail. |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4569 | */ |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4570 | if (s->txn->status == 500) { |
| 4571 | if (s->txn->flags & TX_CONST_REPLY) |
| 4572 | goto end; |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4573 | s->txn->flags |= TX_CONST_REPLY; |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4574 | } |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4575 | s->txn->status = 500; |
| 4576 | s->txn->http_reply = NULL; |
| 4577 | return http_reply_and_close(s, s->txn->status, http_error_message(s)); |
| 4578 | } |
| 4579 | |
| 4580 | end: |
| 4581 | s->res.wex = tick_add_ifset(now_ms, s->res.wto); |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4582 | |
Christopher Faulet | 2d56500 | 2021-09-10 09:17:50 +0200 | [diff] [blame] | 4583 | /* At this staged, HTTP analysis is finished */ |
| 4584 | s->req.analysers &= AN_REQ_FLT_END; |
| 4585 | s->req.analyse_exp = TICK_ETERNITY; |
| 4586 | |
| 4587 | s->res.analysers &= AN_RES_FLT_END; |
| 4588 | s->res.analyse_exp = TICK_ETERNITY; |
| 4589 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4590 | channel_auto_read(&s->req); |
| 4591 | channel_abort(&s->req); |
| 4592 | channel_auto_close(&s->req); |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4593 | channel_htx_erase(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4594 | channel_auto_read(&s->res); |
| 4595 | channel_auto_close(&s->res); |
| 4596 | channel_shutr_now(&s->res); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4597 | } |
| 4598 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4599 | struct http_reply *http_error_message(struct stream *s) |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4600 | { |
| 4601 | const int msgnum = http_get_status_idx(s->txn->status); |
| 4602 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4603 | if (s->txn->http_reply) |
| 4604 | return s->txn->http_reply; |
| 4605 | else if (s->be->replies[msgnum]) |
| 4606 | return s->be->replies[msgnum]; |
| 4607 | else if (strm_fe(s)->replies[msgnum]) |
| 4608 | return strm_fe(s)->replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4609 | else |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4610 | return &http_err_replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4611 | } |
| 4612 | |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4613 | /* Produces an HTX message from an http reply. Depending on the http reply type, |
| 4614 | * a, errorfile, an raw file or a log-format string is used. On success, it |
| 4615 | * returns 0. If an error occurs -1 is returned. If it fails, this function only |
| 4616 | * exits. It is the caller responsibility to do the cleanup. |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4617 | */ |
Christopher Faulet | ae43b6c | 2020-05-27 15:24:22 +0200 | [diff] [blame] | 4618 | 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] | 4619 | { |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4620 | struct buffer *errmsg; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4621 | struct htx_sl *sl; |
| 4622 | struct buffer *body = NULL; |
| 4623 | const char *status, *reason, *clen, *ctype; |
| 4624 | unsigned int slflags; |
| 4625 | int ret = 0; |
| 4626 | |
Christopher Faulet | e29a97e | 2020-05-14 14:49:25 +0200 | [diff] [blame] | 4627 | /* |
| 4628 | * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so |
| 4629 | * |
| 4630 | * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled |
| 4631 | * as no payload if NULL. the TXN status code is set with the status |
| 4632 | * of the original reply. |
| 4633 | */ |
| 4634 | |
| 4635 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4636 | if (reply->body.reply) |
| 4637 | reply = reply->body.reply; |
| 4638 | } |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4639 | if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) { |
| 4640 | /* get default error message */ |
| 4641 | if (reply == s->txn->http_reply) |
| 4642 | s->txn->http_reply = NULL; |
| 4643 | reply = http_error_message(s); |
| 4644 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4645 | if (reply->body.reply) |
| 4646 | reply = reply->body.reply; |
| 4647 | } |
| 4648 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4649 | |
| 4650 | if (reply->type == HTTP_REPLY_ERRMSG) { |
| 4651 | /* implicit or explicit error message*/ |
| 4652 | errmsg = reply->body.errmsg; |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4653 | if (errmsg && !b_is_null(errmsg)) { |
Christopher Faulet | 2056736 | 2020-05-15 14:52:49 +0200 | [diff] [blame] | 4654 | if (!htx_copy_msg(htx, errmsg)) |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4655 | goto fail; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4656 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4657 | } |
| 4658 | else { |
| 4659 | /* no payload, file or log-format string */ |
| 4660 | if (reply->type == HTTP_REPLY_RAW) { |
| 4661 | /* file */ |
| 4662 | body = &reply->body.obj; |
| 4663 | } |
| 4664 | else if (reply->type == HTTP_REPLY_LOGFMT) { |
| 4665 | /* log-format string */ |
| 4666 | body = alloc_trash_chunk(); |
| 4667 | if (!body) |
| 4668 | goto fail_alloc; |
| 4669 | body->data = build_logline(s, body->area, body->size, &reply->body.fmt); |
| 4670 | } |
| 4671 | /* else no payload */ |
| 4672 | |
| 4673 | status = ultoa(reply->status); |
| 4674 | reason = http_get_reason(reply->status); |
| 4675 | slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN); |
| 4676 | if (!body || !b_data(body)) |
| 4677 | slflags |= HTX_SL_F_BODYLESS; |
| 4678 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4679 | if (!sl) |
| 4680 | goto fail; |
| 4681 | sl->info.res.status = reply->status; |
| 4682 | |
| 4683 | clen = (body ? ultoa(b_data(body)) : "0"); |
| 4684 | ctype = reply->ctype; |
| 4685 | |
| 4686 | if (!LIST_ISEMPTY(&reply->hdrs)) { |
| 4687 | struct http_reply_hdr *hdr; |
| 4688 | struct buffer *value = alloc_trash_chunk(); |
| 4689 | |
| 4690 | if (!value) |
| 4691 | goto fail; |
| 4692 | |
| 4693 | list_for_each_entry(hdr, &reply->hdrs, list) { |
| 4694 | chunk_reset(value); |
| 4695 | value->data = build_logline(s, value->area, value->size, &hdr->value); |
| 4696 | if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) { |
| 4697 | free_trash_chunk(value); |
| 4698 | goto fail; |
| 4699 | } |
| 4700 | chunk_reset(value); |
| 4701 | } |
| 4702 | free_trash_chunk(value); |
| 4703 | } |
| 4704 | |
| 4705 | if (!htx_add_header(htx, ist("content-length"), ist(clen)) || |
| 4706 | (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) || |
| 4707 | !htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4708 | (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] | 4709 | goto fail; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4710 | |
| 4711 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4712 | } |
| 4713 | |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4714 | leave: |
| 4715 | if (reply->type == HTTP_REPLY_LOGFMT) |
| 4716 | free_trash_chunk(body); |
| 4717 | return ret; |
| 4718 | |
| 4719 | fail_alloc: |
| 4720 | if (!(s->flags & SF_ERR_MASK)) |
| 4721 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4722 | /* fall through */ |
| 4723 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4724 | ret = -1; |
| 4725 | goto leave; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4726 | } |
| 4727 | |
| 4728 | /* 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] | 4729 | * occurs -1 is returned and the response channel is truncated, removing this |
| 4730 | * way the faulty reply. This function may fail when the reply is formatted |
| 4731 | * (http_reply_to_htx) or when the reply is forwarded |
| 4732 | * (http_forward_proxy_resp). On the last case, it is because a |
| 4733 | * http-after-response rule fails. |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4734 | */ |
| 4735 | int http_reply_message(struct stream *s, struct http_reply *reply) |
| 4736 | { |
| 4737 | struct channel *res = &s->res; |
| 4738 | struct htx *htx = htx_from_buf(&res->buf); |
| 4739 | |
| 4740 | if (s->txn->status == -1) |
| 4741 | s->txn->status = reply->status; |
| 4742 | channel_htx_truncate(res, htx); |
| 4743 | |
| 4744 | if (http_reply_to_htx(s, htx, reply) == -1) |
| 4745 | goto fail; |
| 4746 | |
| 4747 | htx_to_buf(htx, &s->res.buf); |
| 4748 | if (!http_forward_proxy_resp(s, 1)) |
| 4749 | goto fail; |
| 4750 | return 0; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4751 | |
| 4752 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4753 | channel_htx_truncate(res, htx); |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4754 | if (!(s->flags & SF_ERR_MASK)) |
| 4755 | s->flags |= SF_ERR_PRXCOND; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4756 | return -1; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4757 | } |
| 4758 | |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4759 | /* 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] | 4760 | * that the server side is closed. Note that err_type is actually a |
| 4761 | * bitmask, where almost only aborts may be cumulated with other |
| 4762 | * values. We consider that aborted operations are more important |
| 4763 | * than timeouts or errors due to the fact that nobody else in the |
| 4764 | * logs might explain incomplete retries. All others should avoid |
| 4765 | * being cumulated. It should normally not be possible to have multiple |
| 4766 | * aborts at once, but just in case, the first one in sequence is reported. |
| 4767 | * Note that connection errors appearing on the second request of a keep-alive |
| 4768 | * connection are not reported since this allows the client to retry. |
| 4769 | */ |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4770 | void http_return_srv_error(struct stream *s, struct stconn *sc) |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4771 | { |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4772 | int err_type = s->conn_err_type; |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4773 | |
| 4774 | /* set s->txn->status for http_error_message(s) */ |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4775 | if (err_type & STRM_ET_QUEUE_ABRT) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4776 | s->txn->status = -1; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4777 | http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_Q, NULL); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4778 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4779 | else if (err_type & STRM_ET_CONN_ABRT) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4780 | s->txn->status = -1; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4781 | http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_C, NULL); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4782 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4783 | else if (err_type & STRM_ET_QUEUE_TO) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4784 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4785 | http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4786 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4787 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4788 | else if (err_type & STRM_ET_QUEUE_ERR) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4789 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4790 | http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4791 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4792 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4793 | else if (err_type & STRM_ET_CONN_TO) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4794 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4795 | http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4796 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4797 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4798 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4799 | else if (err_type & STRM_ET_CONN_ERR) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4800 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4801 | http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4802 | (s->flags & SF_SRV_REUSED) ? NULL : |
| 4803 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4804 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4805 | else if (err_type & STRM_ET_CONN_RES) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4806 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4807 | http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4808 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4809 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4810 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4811 | else { /* STRM_ET_CONN_OTHER and others */ |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4812 | s->txn->status = 500; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4813 | http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4814 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4815 | } |
| 4816 | } |
| 4817 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4818 | |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4819 | /* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0 |
| 4820 | * on success and -1 on error. |
| 4821 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4822 | 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] | 4823 | { |
| 4824 | /* If we have HTTP/1.1 message with a body and Expect: 100-continue, |
| 4825 | * then we must send an HTTP/1.1 100 Continue intermediate response. |
| 4826 | */ |
| 4827 | if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) && |
| 4828 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
| 4829 | struct ist hdr = { .ptr = "Expect", .len = 6 }; |
| 4830 | struct http_hdr_ctx ctx; |
| 4831 | |
| 4832 | ctx.blk = NULL; |
| 4833 | /* Expect is allowed in 1.1, look for it */ |
| 4834 | if (http_find_header(htx, hdr, &ctx, 0) && |
| 4835 | unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4836 | if (http_reply_100_continue(s) == -1) |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4837 | return -1; |
| 4838 | http_remove_header(htx, &ctx); |
| 4839 | } |
| 4840 | } |
| 4841 | return 0; |
| 4842 | } |
| 4843 | |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4844 | /* Send a 100-Continue response to the client. It returns 0 on success and -1 |
| 4845 | * on error. The response channel is updated accordingly. |
| 4846 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4847 | static int http_reply_100_continue(struct stream *s) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4848 | { |
| 4849 | struct channel *res = &s->res; |
| 4850 | struct htx *htx = htx_from_buf(&res->buf); |
| 4851 | struct htx_sl *sl; |
| 4852 | unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11| |
| 4853 | HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4854 | |
| 4855 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4856 | ist("HTTP/1.1"), ist("100"), ist("Continue")); |
| 4857 | if (!sl) |
| 4858 | goto fail; |
| 4859 | sl->info.res.status = 100; |
| 4860 | |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 4861 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4862 | goto fail; |
| 4863 | |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4864 | if (!http_forward_proxy_resp(s, 0)) |
| 4865 | goto fail; |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4866 | return 0; |
| 4867 | |
| 4868 | fail: |
| 4869 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4870 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4871 | channel_htx_truncate(res, htx); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4872 | return -1; |
| 4873 | } |
| 4874 | |
Christopher Faulet | 12c51e2 | 2018-11-28 15:59:42 +0100 | [diff] [blame] | 4875 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4876 | /* |
| 4877 | * Capture headers from message <htx> according to header list <cap_hdr>, and |
| 4878 | * fill the <cap> pointers appropriately. |
| 4879 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4880 | 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] | 4881 | { |
| 4882 | struct cap_hdr *h; |
| 4883 | int32_t pos; |
| 4884 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4885 | 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] | 4886 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4887 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4888 | struct ist n, v; |
| 4889 | |
| 4890 | if (type == HTX_BLK_EOH) |
| 4891 | break; |
| 4892 | if (type != HTX_BLK_HDR) |
| 4893 | continue; |
| 4894 | |
| 4895 | n = htx_get_blk_name(htx, blk); |
| 4896 | |
| 4897 | for (h = cap_hdr; h; h = h->next) { |
| 4898 | if (h->namelen && (h->namelen == n.len) && |
| 4899 | (strncasecmp(n.ptr, h->name, h->namelen) == 0)) { |
| 4900 | if (cap[h->index] == NULL) |
| 4901 | cap[h->index] = |
| 4902 | pool_alloc(h->pool); |
| 4903 | |
| 4904 | if (cap[h->index] == NULL) { |
| 4905 | ha_alert("HTTP capture : out of memory.\n"); |
| 4906 | break; |
| 4907 | } |
| 4908 | |
| 4909 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 4910 | v = isttrim(v, h->len); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4911 | |
| 4912 | memcpy(cap[h->index], v.ptr, v.len); |
| 4913 | cap[h->index][v.len]=0; |
| 4914 | } |
| 4915 | } |
| 4916 | } |
| 4917 | } |
| 4918 | |
Christopher Faulet | 0b6bdc5 | 2018-10-24 11:05:36 +0200 | [diff] [blame] | 4919 | /* Delete a value in a header between delimiters <from> and <next>. The header |
| 4920 | * itself is delimited by <start> and <end> pointers. The number of characters |
| 4921 | * displaced is returned, and the pointer to the first delimiter is updated if |
| 4922 | * required. The function tries as much as possible to respect the following |
| 4923 | * principles : |
| 4924 | * - replace <from> delimiter by the <next> one unless <from> points to <start>, |
| 4925 | * in which case <next> is simply removed |
| 4926 | * - set exactly one space character after the new first delimiter, unless there |
| 4927 | * are not enough characters in the block being moved to do so. |
| 4928 | * - remove unneeded spaces before the previous delimiter and after the new |
| 4929 | * one. |
| 4930 | * |
| 4931 | * It is the caller's responsibility to ensure that : |
| 4932 | * - <from> points to a valid delimiter or <start> ; |
| 4933 | * - <next> points to a valid delimiter or <end> ; |
| 4934 | * - there are non-space chars before <from>. |
| 4935 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4936 | 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] | 4937 | { |
| 4938 | char *prev = *from; |
| 4939 | |
| 4940 | if (prev == start) { |
| 4941 | /* We're removing the first value. eat the semicolon, if <next> |
| 4942 | * is lower than <end> */ |
| 4943 | if (next < end) |
| 4944 | next++; |
| 4945 | |
| 4946 | while (next < end && HTTP_IS_SPHT(*next)) |
| 4947 | next++; |
| 4948 | } |
| 4949 | else { |
| 4950 | /* Remove useless spaces before the old delimiter. */ |
| 4951 | while (HTTP_IS_SPHT(*(prev-1))) |
| 4952 | prev--; |
| 4953 | *from = prev; |
| 4954 | |
| 4955 | /* copy the delimiter and if possible a space if we're |
| 4956 | * not at the end of the line. |
| 4957 | */ |
| 4958 | if (next < end) { |
| 4959 | *prev++ = *next++; |
| 4960 | if (prev + 1 < next) |
| 4961 | *prev++ = ' '; |
| 4962 | while (next < end && HTTP_IS_SPHT(*next)) |
| 4963 | next++; |
| 4964 | } |
| 4965 | } |
| 4966 | memmove(prev, next, end - next); |
| 4967 | return (prev - next); |
| 4968 | } |
| 4969 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4970 | |
| 4971 | /* 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] | 4972 | * 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] | 4973 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4974 | 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] | 4975 | { |
| 4976 | struct ist dst = ist2(str, 0); |
| 4977 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4978 | if (istcat(&dst, htx_sl_req_meth(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4979 | goto end; |
| 4980 | if (dst.len + 1 > len) |
| 4981 | goto end; |
| 4982 | dst.ptr[dst.len++] = ' '; |
| 4983 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4984 | if (istcat(&dst, htx_sl_req_uri(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4985 | goto end; |
| 4986 | if (dst.len + 1 > len) |
| 4987 | goto end; |
| 4988 | dst.ptr[dst.len++] = ' '; |
| 4989 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4990 | istcat(&dst, htx_sl_req_vsn(sl), len); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4991 | end: |
| 4992 | return dst.len; |
| 4993 | } |
| 4994 | |
| 4995 | /* |
| 4996 | * Print a debug line with a start line. |
| 4997 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4998 | 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] | 4999 | { |
| 5000 | struct session *sess = strm_sess(s); |
| 5001 | int max; |
| 5002 | |
| 5003 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 5004 | dir, |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 5005 | 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] | 5006 | 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] | 5007 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5008 | max = HTX_SL_P1_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5009 | UBOUND(max, trash.size - trash.data - 3); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5010 | chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5011 | trash.area[trash.data++] = ' '; |
| 5012 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5013 | max = HTX_SL_P2_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5014 | UBOUND(max, trash.size - trash.data - 2); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5015 | chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5016 | trash.area[trash.data++] = ' '; |
| 5017 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5018 | max = HTX_SL_P3_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5019 | UBOUND(max, trash.size - trash.data - 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5020 | chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5021 | trash.area[trash.data++] = '\n'; |
| 5022 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 5023 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5024 | } |
| 5025 | |
| 5026 | /* |
| 5027 | * Print a debug line with a header. |
| 5028 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5029 | 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] | 5030 | { |
| 5031 | struct session *sess = strm_sess(s); |
| 5032 | int max; |
| 5033 | |
| 5034 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 5035 | dir, |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 5036 | 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] | 5037 | 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] | 5038 | |
| 5039 | max = n.len; |
| 5040 | UBOUND(max, trash.size - trash.data - 3); |
| 5041 | chunk_memcat(&trash, n.ptr, max); |
| 5042 | trash.area[trash.data++] = ':'; |
| 5043 | trash.area[trash.data++] = ' '; |
| 5044 | |
| 5045 | max = v.len; |
| 5046 | UBOUND(max, trash.size - trash.data - 1); |
| 5047 | chunk_memcat(&trash, v.ptr, max); |
| 5048 | trash.area[trash.data++] = '\n'; |
| 5049 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 5050 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5051 | } |
| 5052 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5053 | void http_txn_reset_req(struct http_txn *txn) |
| 5054 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 5055 | txn->req.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5056 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 5057 | } |
| 5058 | |
| 5059 | void http_txn_reset_res(struct http_txn *txn) |
| 5060 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 5061 | txn->rsp.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5062 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 5063 | } |
| 5064 | |
| 5065 | /* |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5066 | * Create and initialize a new HTTP transaction for stream <s>. This should be |
| 5067 | * used before processing any new request. It returns the transaction or NLULL |
| 5068 | * on error. |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5069 | */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5070 | struct http_txn *http_create_txn(struct stream *s) |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5071 | { |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5072 | struct http_txn *txn; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 5073 | struct stconn *sc = s->scf; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5074 | |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5075 | txn = pool_alloc(pool_head_http_txn); |
| 5076 | if (!txn) |
| 5077 | return NULL; |
| 5078 | s->txn = txn; |
| 5079 | |
Christopher Faulet | 5eb67f5 | 2022-06-22 17:12:05 +0200 | [diff] [blame] | 5080 | txn->meth = HTTP_METH_OTHER; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 5081 | 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] | 5082 | txn->status = -1; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 5083 | txn->http_reply = NULL; |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 5084 | txn->l7_buffer = BUF_NULL; |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 5085 | write_u32(txn->cache_hash, 0); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5086 | |
| 5087 | txn->cookie_first_date = 0; |
| 5088 | txn->cookie_last_date = 0; |
| 5089 | |
| 5090 | txn->srv_cookie = NULL; |
| 5091 | txn->cli_cookie = NULL; |
| 5092 | txn->uri = NULL; |
| 5093 | |
| 5094 | http_txn_reset_req(txn); |
| 5095 | http_txn_reset_res(txn); |
| 5096 | |
| 5097 | txn->req.chn = &s->req; |
| 5098 | txn->rsp.chn = &s->res; |
| 5099 | |
| 5100 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
| 5101 | |
Aurelien DARRAGON | 5ad2b64 | 2022-11-18 09:17:29 +0100 | [diff] [blame] | 5102 | /* here we don't want to re-initialize s->vars_txn and s->vars_reqres |
| 5103 | * variable lists, because they were already initialized upon stream |
| 5104 | * creation in stream_new(), and thus may already contain some variables |
| 5105 | */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5106 | |
| 5107 | return txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5108 | } |
| 5109 | |
| 5110 | /* to be used at the end of a transaction */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5111 | void http_destroy_txn(struct stream *s) |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5112 | { |
| 5113 | struct http_txn *txn = s->txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5114 | |
| 5115 | /* these ones will have been dynamically allocated */ |
| 5116 | pool_free(pool_head_requri, txn->uri); |
| 5117 | pool_free(pool_head_capture, txn->cli_cookie); |
| 5118 | pool_free(pool_head_capture, txn->srv_cookie); |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5119 | pool_free(pool_head_uniqueid, s->unique_id.ptr); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5120 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5121 | s->unique_id = IST_NULL; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5122 | txn->uri = NULL; |
| 5123 | txn->srv_cookie = NULL; |
| 5124 | txn->cli_cookie = NULL; |
| 5125 | |
Christopher Faulet | 5939925 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 5126 | if (!LIST_ISEMPTY(&s->vars_txn.head)) |
| 5127 | vars_prune(&s->vars_txn, s->sess, s); |
| 5128 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 5129 | vars_prune(&s->vars_reqres, s->sess, s); |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5130 | |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 5131 | b_free(&txn->l7_buffer); |
| 5132 | |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5133 | pool_free(pool_head_http_txn, txn); |
| 5134 | s->txn = NULL; |
Christopher Faulet | 5939925 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 5135 | } |
| 5136 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5137 | |
Christopher Faulet | 71236de | 2023-01-13 09:06:38 +0100 | [diff] [blame] | 5138 | void http_set_term_flags(struct stream *s) |
| 5139 | { |
| 5140 | if (!(s->flags & SF_ERR_MASK)) |
| 5141 | s->flags |= SF_ERR_PRXCOND; |
| 5142 | |
| 5143 | if (!(s->flags & SF_FINST_MASK)) { |
| 5144 | if (s->scb->state == SC_ST_INI) { |
| 5145 | /* Before any connection attempt on the server side, we |
| 5146 | * are still in the request analysis. Just take case to |
| 5147 | * detect tarpit error |
| 5148 | */ |
| 5149 | if (s->req.analysers & AN_REQ_HTTP_TARPIT) |
| 5150 | s->flags |= SF_FINST_T; |
| 5151 | else |
| 5152 | s->flags |= SF_FINST_R; |
| 5153 | } |
| 5154 | else if (s->scb->state == SC_ST_QUE) |
| 5155 | s->flags |= SF_FINST_Q; |
| 5156 | else if (sc_state_in(s->scb->state, SC_SB_REQ|SC_SB_TAR|SC_SB_ASS|SC_SB_CON|SC_SB_CER|SC_SB_RDY)) { |
| 5157 | if (unlikely(objt_applet(s->target))) { |
| 5158 | s->flags |= SF_FINST_R; |
| 5159 | } |
| 5160 | else |
| 5161 | s->flags |= SF_FINST_C; |
| 5162 | } |
| 5163 | else { |
| 5164 | if (s->txn->rsp.msg_state < HTTP_MSG_DATA) { |
| 5165 | /* We are still processing the response headers */ |
| 5166 | s->flags |= SF_FINST_H; |
| 5167 | } |
| 5168 | // (res >= done) & (res->flags & shutw) |
| 5169 | else if (s->txn->rsp.msg_state >= HTTP_MSG_DONE && |
| 5170 | (s->flags & (SF_ERR_CLITO|SF_ERR_CLICL))) { |
| 5171 | /* A client error was reported and we are |
| 5172 | * transmitting the last block of data |
| 5173 | */ |
| 5174 | s->flags |= SF_FINST_L; |
| 5175 | } |
| 5176 | else { |
| 5177 | /* Otherwise we are in DATA phase on both sides */ |
| 5178 | s->flags |= SF_FINST_D; |
| 5179 | } |
| 5180 | } |
| 5181 | } |
| 5182 | } |
| 5183 | |
| 5184 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5185 | DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5186 | |
Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 5187 | /* |
| 5188 | * Local variables: |
| 5189 | * c-indent-level: 8 |
| 5190 | * c-basic-offset: 8 |
| 5191 | * End: |
| 5192 | */ |