Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
| 4 | * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Willy Tarreau | dcc048a | 2020-06-04 19:11:43 +0200 | [diff] [blame] | 13 | #include <haproxy/acl.h> |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 14 | #include <haproxy/action-t.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 15 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 16 | #include <haproxy/backend.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 17 | #include <haproxy/base64.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 18 | #include <haproxy/capture-t.h> |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 19 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 20 | #include <haproxy/channel.h> |
Willy Tarreau | 4aa573d | 2020-06-04 18:21:56 +0200 | [diff] [blame] | 21 | #include <haproxy/check.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 22 | #include <haproxy/connection.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 23 | #include <haproxy/errors.h> |
Willy Tarreau | c7babd8 | 2020-06-04 21:29:29 +0200 | [diff] [blame] | 24 | #include <haproxy/filters.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 25 | #include <haproxy/http.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 26 | #include <haproxy/http_ana.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 27 | #include <haproxy/http_htx.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 28 | #include <haproxy/htx.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 29 | #include <haproxy/log.h> |
Willy Tarreau | 6131d6a | 2020-06-02 16:48:09 +0200 | [diff] [blame] | 30 | #include <haproxy/net_helper.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 31 | #include <haproxy/proxy.h> |
Willy Tarreau | 7cd8b6e | 2020-06-02 17:32:26 +0200 | [diff] [blame] | 32 | #include <haproxy/regex.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 33 | #include <haproxy/server-t.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 34 | #include <haproxy/stats.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 35 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 36 | #include <haproxy/stream_interface.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 37 | #include <haproxy/trace.h> |
Willy Tarreau | 8c42b8a | 2020-06-04 19:27:34 +0200 | [diff] [blame] | 38 | #include <haproxy/uri_auth-t.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 39 | #include <haproxy/vars.h> |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 40 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 41 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 42 | #define TRACE_SOURCE &trace_strm |
| 43 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 44 | extern const char *stat_status_codes[]; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 45 | |
Willy Tarreau | ff88270 | 2021-04-10 17:23:00 +0200 | [diff] [blame] | 46 | struct pool_head *pool_head_requri __read_mostly = NULL; |
| 47 | struct pool_head *pool_head_capture __read_mostly = NULL; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 48 | |
| 49 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 50 | static void http_end_request(struct stream *s); |
| 51 | static void http_end_response(struct stream *s); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 52 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 53 | static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr); |
| 54 | static int http_del_hdr_value(char *start, char *end, char **from, char *next); |
| 55 | 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] | 56 | static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl); |
| 57 | 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] | 58 | |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 59 | static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s); |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 60 | static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s); |
Christopher Faulet | 7950715 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 61 | 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] | 62 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 63 | static void http_manage_client_side_cookies(struct stream *s, struct channel *req); |
| 64 | 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] | 65 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 66 | static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend); |
| 67 | static int http_handle_stats(struct stream *s, struct channel *req); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 68 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 69 | static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg); |
| 70 | static int http_reply_100_continue(struct stream *s); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 71 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 72 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 73 | * processing can continue on next analysers, or zero if it either needs more |
| 74 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 75 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers |
| 76 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 77 | * abort. |
| 78 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 79 | 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] | 80 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 81 | |
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 | * We will analyze a complete HTTP request to check the its syntax. |
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 | * Once the start line and all headers are received, we may perform a |
| 86 | * capture of the error (if any), and we will set a few fields. We also |
| 87 | * check for monitor-uri, logging and finally headers capture. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 88 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 89 | struct session *sess = s->sess; |
| 90 | struct http_txn *txn = s->txn; |
| 91 | struct http_msg *msg = &txn->req; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 92 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 93 | struct htx_sl *sl; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 94 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 95 | 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] | 96 | |
Christopher Faulet | da46a0d | 2021-01-21 17:32:58 +0100 | [diff] [blame] | 97 | if (unlikely(!IS_HTX_STRM(s))) { |
| 98 | /* It is only possible when a TCP stream is upgrade to HTTP. |
| 99 | * There is a transition period during which there is no |
| 100 | * data. The stream is still in raw mode and SF_IGNORE flag is |
| 101 | * still set. When this happens, the new mux is responsible to |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 102 | * handle all errors. Thus we may leave immediately. |
Christopher Faulet | da46a0d | 2021-01-21 17:32:58 +0100 | [diff] [blame] | 103 | */ |
| 104 | BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 105 | |
Christopher Faulet | 97b3a61 | 2021-03-15 17:10:12 +0100 | [diff] [blame] | 106 | /* Don't connect for now */ |
| 107 | channel_dont_connect(req); |
| 108 | |
| 109 | /* A SHUTR at this stage means we are performing a "destructive" |
| 110 | * HTTP upgrade (TCP>H2). In this case, we can leave. |
| 111 | */ |
| 112 | if (req->flags & CF_SHUTR) { |
| 113 | s->logs.logwait = 0; |
| 114 | s->logs.level = 0; |
| 115 | channel_abort(&s->req); |
| 116 | channel_abort(&s->res); |
| 117 | req->analysers &= AN_REQ_FLT_END; |
| 118 | req->analyse_exp = TICK_ETERNITY; |
| 119 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s); |
| 120 | return 1; |
| 121 | } |
Christopher Faulet | da46a0d | 2021-01-21 17:32:58 +0100 | [diff] [blame] | 122 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s); |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | htx = htxbuf(&req->buf); |
Christopher Faulet | 8bebd2f | 2020-10-06 17:54:56 +0200 | [diff] [blame] | 127 | |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 128 | /* Parsing errors are caught here */ |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 129 | if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) { |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 130 | stream_inc_http_req_ctr(s); |
Emeric Brun | 2897644 | 2020-10-07 08:50:09 +0200 | [diff] [blame] | 131 | proxy_inc_fe_req_ctr(sess->listener, sess->fe); |
Christopher Faulet | bf7175f | 2021-02-10 14:58:01 +0100 | [diff] [blame] | 132 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
| 133 | stream_inc_http_err_ctr(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 134 | goto return_bad_req; |
Christopher Faulet | bf7175f | 2021-02-10 14:58:01 +0100 | [diff] [blame] | 135 | } |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 136 | else |
| 137 | goto return_int_err; |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 140 | /* 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] | 141 | s->srv_error = http_return_srv_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 142 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 143 | msg->msg_state = HTTP_MSG_BODY; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 144 | stream_inc_http_req_ctr(s); |
Emeric Brun | 2897644 | 2020-10-07 08:50:09 +0200 | [diff] [blame] | 145 | 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] | 146 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 147 | /* kill the pending keep-alive timeout */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 148 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 149 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 150 | BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 151 | sl = http_get_stline(htx); |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 152 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 153 | /* 0: we might have to print this header in debug mode */ |
| 154 | if (unlikely((global.mode & MODE_DEBUG) && |
| 155 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 156 | int32_t pos; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 157 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 158 | http_debug_stline("clireq", s, sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 159 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 160 | 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] | 161 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 162 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 163 | |
| 164 | if (type == HTX_BLK_EOH) |
| 165 | break; |
| 166 | if (type != HTX_BLK_HDR) |
| 167 | continue; |
| 168 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 169 | http_debug_hdr("clihdr", s, |
| 170 | htx_get_blk_name(htx, blk), |
| 171 | htx_get_blk_value(htx, blk)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 172 | } |
| 173 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 174 | |
| 175 | /* |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 176 | * 1: identify the method and the version. Also set HTTP flags |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 177 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 178 | txn->meth = sl->info.req.meth; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 179 | if (sl->flags & HTX_SL_F_VER_11) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 180 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 181 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 182 | if (sl->flags & HTX_SL_F_CLEN) |
| 183 | msg->flags |= HTTP_MSGF_CNT_LEN; |
| 184 | else if (sl->flags & HTX_SL_F_CHNK) |
| 185 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Christopher Faulet | b2db4fa | 2018-11-27 16:51:09 +0100 | [diff] [blame] | 186 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 187 | msg->flags |= HTTP_MSGF_BODYLESS; |
Christopher Faulet | 576c358 | 2021-01-08 15:53:01 +0100 | [diff] [blame] | 188 | if (sl->flags & HTX_SL_F_CONN_UPG) |
| 189 | msg->flags |= HTTP_MSGF_CONN_UPG; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 190 | |
| 191 | /* we can make use of server redirect on GET and HEAD */ |
| 192 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 193 | s->flags |= SF_REDIRECTABLE; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 194 | 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] | 195 | /* PRI is reserved for the HTTP/2 preface */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 196 | goto return_bad_req; |
| 197 | } |
| 198 | |
| 199 | /* |
Christopher Faulet | 6072beb | 2020-02-18 15:34:58 +0100 | [diff] [blame] | 200 | * 2: check if the URI matches the monitor_uri. We have to do this for |
| 201 | * every request which gets in, because the monitor-uri is defined by |
| 202 | * the frontend. If the monitor-uri starts with a '/', the matching is |
| 203 | * done against the request's path. Otherwise, the request's uri is |
| 204 | * used. It is a workaround to let HTTP/2 health-checks work as |
| 205 | * expected. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 206 | */ |
| 207 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
Christopher Faulet | 6072beb | 2020-02-18 15:34:58 +0100 | [diff] [blame] | 208 | ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)), |
| 209 | ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) || |
| 210 | isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 211 | /* |
| 212 | * We have found the monitor URI |
| 213 | */ |
| 214 | struct acl_cond *cond; |
| 215 | |
| 216 | s->flags |= SF_MONITOR; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 217 | _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 218 | |
| 219 | /* Check if we want to fail this monitor request or not */ |
| 220 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
| 221 | int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 222 | |
| 223 | ret = acl_pass(ret); |
| 224 | if (cond->pol == ACL_COND_UNLESS) |
| 225 | ret = !ret; |
| 226 | |
| 227 | if (ret) { |
| 228 | /* we fail this request, let's return 503 service unavail */ |
| 229 | txn->status = 503; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 230 | if (!(s->flags & SF_ERR_MASK)) |
| 231 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
| 232 | goto return_prx_cond; |
| 233 | } |
| 234 | } |
| 235 | |
Joseph Herlant | c42c0e9 | 2018-11-25 10:43:27 -0800 | [diff] [blame] | 236 | /* nothing to fail, let's reply normally */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 237 | txn->status = 200; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 238 | if (!(s->flags & SF_ERR_MASK)) |
| 239 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
| 240 | goto return_prx_cond; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 245 | * Note: we cannot log anymore if the request has been |
| 246 | * classified as invalid. |
| 247 | */ |
| 248 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 249 | /* we have a complete HTTP request that we must log */ |
| 250 | if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 251 | size_t len; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 252 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 253 | 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] | 254 | txn->uri[len] = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 255 | |
| 256 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
| 257 | s->do_log(s); |
| 258 | } else { |
| 259 | ha_alert("HTTP logging : out of memory.\n"); |
| 260 | } |
| 261 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 262 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 263 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 264 | * we have what looks like a proxied connection instead of a connection, |
| 265 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 266 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 267 | * happen to do that despite being non-standard :-( |
| 268 | * We consider that a request not beginning with either '/' or '*' is |
| 269 | * a proxied connection, which covers both "scheme://location" and |
| 270 | * CONNECT ip:port. |
| 271 | */ |
| 272 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 273 | *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*') |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 274 | txn->flags |= TX_USE_PX_CONN; |
| 275 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 276 | /* 5: we may need to capture headers */ |
| 277 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 278 | http_capture_headers(htx, s->req_cap, sess->fe->req_cap); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 279 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 280 | /* we may have to wait for the request's body */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 281 | if (s->be->options & PR_O_WREQ_BODY) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 282 | req->analysers |= AN_REQ_HTTP_BODY; |
| 283 | |
| 284 | /* |
| 285 | * RFC7234#4: |
| 286 | * A cache MUST write through requests with methods |
| 287 | * that are unsafe (Section 4.2.1 of [RFC7231]) to |
| 288 | * the origin server; i.e., a cache is not allowed |
| 289 | * to generate a reply to such a request before |
| 290 | * having forwarded the request and having received |
| 291 | * a corresponding response. |
| 292 | * |
| 293 | * RFC7231#4.2.1: |
| 294 | * Of the request methods defined by this |
| 295 | * specification, the GET, HEAD, OPTIONS, and TRACE |
| 296 | * methods are defined to be safe. |
| 297 | */ |
| 298 | if (likely(txn->meth == HTTP_METH_GET || |
| 299 | txn->meth == HTTP_METH_HEAD || |
| 300 | txn->meth == HTTP_METH_OPTIONS || |
| 301 | txn->meth == HTTP_METH_TRACE)) |
| 302 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 303 | |
| 304 | /* end of job, return OK */ |
| 305 | req->analysers &= ~an_bit; |
| 306 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 307 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 308 | 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] | 309 | return 1; |
| 310 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 311 | return_int_err: |
| 312 | txn->status = 500; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 313 | if (!(s->flags & SF_ERR_MASK)) |
| 314 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 315 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 316 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 317 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 318 | goto return_prx_cond; |
| 319 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 320 | return_bad_req: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 321 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 322 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 323 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 324 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 325 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 326 | |
| 327 | return_prx_cond: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 328 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 329 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 330 | if (!(s->flags & SF_ERR_MASK)) |
| 331 | s->flags |= SF_ERR_PRXCOND; |
| 332 | if (!(s->flags & SF_FINST_MASK)) |
| 333 | s->flags |= SF_FINST_R; |
| 334 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 335 | DBG_TRACE_DEVEL("leaving on error", |
| 336 | 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] | 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | |
| 341 | /* This stream analyser runs all HTTP request processing which is common to |
| 342 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 343 | * reqadd, stats and redirects. This is performed for the designated proxy. |
| 344 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 345 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 346 | * error, ...). |
| 347 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 348 | 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] | 349 | { |
| 350 | struct session *sess = s->sess; |
| 351 | struct http_txn *txn = s->txn; |
| 352 | struct http_msg *msg = &txn->req; |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 353 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 354 | struct redirect_rule *rule; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 355 | enum rule_result verdict; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 356 | struct connection *conn = objt_conn(sess->origin); |
| 357 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 358 | 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] | 359 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 360 | htx = htxbuf(&req->buf); |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 361 | |
Christopher Faulet | 1907ccc | 2019-04-29 13:12:02 +0200 | [diff] [blame] | 362 | /* just in case we have some per-backend tracking. Only called the first |
| 363 | * execution of the analyser. */ |
| 364 | if (!s->current_rule || s->current_rule_list != &px->http_req_rules) |
| 365 | stream_inc_be_http_req_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 366 | |
| 367 | /* evaluate http-request rules */ |
| 368 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 369 | verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 370 | |
| 371 | switch (verdict) { |
| 372 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 373 | goto return_prx_yield; |
| 374 | |
| 375 | case HTTP_RULE_RES_CONT: |
| 376 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 377 | break; |
| 378 | |
| 379 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 380 | if (txn->flags & TX_CLTARPIT) |
| 381 | goto tarpit; |
| 382 | goto deny; |
| 383 | |
| 384 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 385 | goto return_prx_cond; |
| 386 | |
| 387 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
| 388 | goto done; |
| 389 | |
| 390 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 391 | goto return_bad_req; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 392 | |
| 393 | case HTTP_RULE_RES_ERROR: /* failed with a bad request */ |
| 394 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
Christopher Faulet | 7950715 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 398 | if (px->options2 & (PR_O2_RSTRICT_REQ_HDR_NAMES_BLK|PR_O2_RSTRICT_REQ_HDR_NAMES_DEL)) { |
| 399 | verdict = http_req_restrict_header_names(s, htx, px); |
| 400 | if (verdict == HTTP_RULE_RES_DENY) |
| 401 | goto deny; |
| 402 | } |
| 403 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 404 | if (conn && (conn->flags & CO_FL_EARLY_DATA) && |
Olivier Houchard | 220a26c | 2020-01-23 14:57:36 +0100 | [diff] [blame] | 405 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) { |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 406 | struct http_hdr_ctx ctx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 407 | |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 408 | ctx.blk = NULL; |
| 409 | if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) { |
| 410 | if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1")))) |
Christopher Faulet | 028c164 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 411 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 412 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /* OK at this stage, we know that the request was accepted according to |
| 416 | * the http-request rules, we can check for the stats. Note that the |
| 417 | * URI is detected *before* the req* rules in order not to be affected |
| 418 | * by a possible reqrep, while they are processed *after* so that a |
| 419 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 420 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 421 | if (!s->target && http_stats_check_uri(s, txn, px)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 422 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 423 | if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 424 | s->logs.tv_request = now; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 425 | if (!(s->flags & SF_ERR_MASK)) |
| 426 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 427 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /* parse the whole stats request and extract the relevant information */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 431 | http_handle_stats(s, req); |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 432 | verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 433 | /* not all actions implemented: deny, allow, auth */ |
| 434 | |
| 435 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 436 | goto deny; |
| 437 | |
| 438 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 439 | goto return_prx_cond; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 440 | |
| 441 | if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */ |
| 442 | goto return_bad_req; |
| 443 | |
| 444 | if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */ |
| 445 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Christopher Faulet | 2571bc6 | 2019-03-01 11:44:26 +0100 | [diff] [blame] | 448 | /* Proceed with the applets now. */ |
| 449 | if (unlikely(objt_applet(s->target))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 450 | 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] | 451 | _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 452 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 453 | if (http_handle_expect_hdr(s, htx, msg) == -1) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 454 | goto return_int_err; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 455 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 456 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 457 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 458 | if (!(s->flags & SF_FINST_MASK)) |
| 459 | s->flags |= SF_FINST_R; |
| 460 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 461 | if (HAS_FILTERS(s)) |
| 462 | req->analysers |= AN_REQ_FLT_HTTP_HDRS; |
| 463 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 464 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
| 465 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 466 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 467 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 468 | |
| 469 | req->flags |= CF_SEND_DONTWAIT; |
| 470 | s->flags |= SF_ASSIGNED; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 471 | goto done; |
| 472 | } |
| 473 | |
| 474 | /* check whether we have some ACLs set to redirect this request */ |
| 475 | list_for_each_entry(rule, &px->redirect_rules, list) { |
| 476 | if (rule->cond) { |
| 477 | int ret; |
| 478 | |
| 479 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 480 | ret = acl_pass(ret); |
| 481 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 482 | ret = !ret; |
| 483 | if (!ret) |
| 484 | continue; |
| 485 | } |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 486 | if (!http_apply_redirect_rule(rule, s, txn)) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 487 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 488 | goto done; |
| 489 | } |
| 490 | |
| 491 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 492 | * If this happens, then the data will not come immediately, so we must |
| 493 | * send all what we have without waiting. Note that due to the small gain |
| 494 | * in waiting for the body of the request, it's easier to simply put the |
| 495 | * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 496 | * itself once used. |
| 497 | */ |
| 498 | req->flags |= CF_SEND_DONTWAIT; |
| 499 | |
| 500 | done: /* done with this analyser, continue with next ones that the calling |
| 501 | * points will have set, if any. |
| 502 | */ |
| 503 | req->analyse_exp = TICK_ETERNITY; |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 504 | 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] | 505 | req->analysers &= ~an_bit; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 506 | 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] | 507 | return 1; |
| 508 | |
| 509 | tarpit: |
| 510 | /* Allow cookie logging |
| 511 | */ |
| 512 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 513 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 514 | |
| 515 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 516 | * which may be the same as the connect timeout if unspecified. |
| 517 | * If unset, then set it to zero because we really want it to |
| 518 | * eventually expire. We build the tarpit as an analyser. |
| 519 | */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 520 | channel_htx_erase(&s->req, htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 521 | |
| 522 | /* wipe the request out so that we can drop the connection early |
| 523 | * if the client closes first. |
| 524 | */ |
| 525 | channel_dont_connect(req); |
| 526 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 527 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
| 528 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 529 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 530 | if (!req->analyse_exp) |
| 531 | req->analyse_exp = tick_add(now_ms, 0); |
| 532 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 533 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 534 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 535 | _HA_ATOMIC_INC(&s->be->be_counters.denied_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 536 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 537 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 538 | goto done_without_exp; |
| 539 | |
| 540 | deny: /* this request was blocked (denied) */ |
| 541 | |
| 542 | /* Allow cookie logging |
| 543 | */ |
| 544 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 545 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 546 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 547 | s->logs.tv_request = now; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 548 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 549 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 550 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 551 | _HA_ATOMIC_INC(&s->be->be_counters.denied_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 552 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 553 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 554 | goto return_prx_err; |
| 555 | |
Christopher Faulet | 028c164 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 556 | return_fail_rewrite: |
| 557 | if (!(s->flags & SF_ERR_MASK)) |
| 558 | s->flags |= SF_ERR_PRXCOND; |
| 559 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
| 560 | if (s->flags & SF_BE_ASSIGNED) |
| 561 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
| 562 | if (sess->listener && sess->listener->counters) |
| 563 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
| 564 | if (objt_server(s->target)) |
| 565 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
| 566 | /* fall through */ |
| 567 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 568 | return_int_err: |
| 569 | txn->status = 500; |
| 570 | if (!(s->flags & SF_ERR_MASK)) |
| 571 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 572 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 573 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 574 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 575 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 576 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 577 | goto return_prx_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 578 | |
| 579 | return_bad_req: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 580 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 581 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 582 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 583 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 584 | /* fall through */ |
| 585 | |
| 586 | return_prx_err: |
| 587 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 588 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 589 | |
| 590 | return_prx_cond: |
| 591 | if (!(s->flags & SF_ERR_MASK)) |
| 592 | s->flags |= SF_ERR_PRXCOND; |
| 593 | if (!(s->flags & SF_FINST_MASK)) |
| 594 | s->flags |= SF_FINST_R; |
| 595 | |
| 596 | req->analysers &= AN_REQ_FLT_END; |
| 597 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 598 | DBG_TRACE_DEVEL("leaving on error", |
| 599 | 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] | 600 | return 0; |
| 601 | |
| 602 | return_prx_yield: |
| 603 | channel_dont_connect(req); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 604 | DBG_TRACE_DEVEL("waiting for more data", |
| 605 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | /* This function performs all the processing enabled for the current request. |
| 610 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 611 | * needs more data, encounters an error, or wants to immediately abort the |
| 612 | * request. It relies on buffers flags, and updates s->req.analysers. |
| 613 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 614 | 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] | 615 | { |
| 616 | struct session *sess = s->sess; |
| 617 | struct http_txn *txn = s->txn; |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 618 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 619 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
| 620 | |
Christopher Faulet | 8bebd2f | 2020-10-06 17:54:56 +0200 | [diff] [blame] | 621 | 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] | 622 | |
| 623 | /* |
| 624 | * Right now, we know that we have processed the entire headers |
| 625 | * and that unwanted requests have been filtered out. We can do |
| 626 | * whatever we want with the remaining request. Also, now we |
| 627 | * may have separate values for ->fe, ->be. |
| 628 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 629 | htx = htxbuf(&req->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 630 | |
| 631 | /* |
| 632 | * If HTTP PROXY is set we simply get remote server address parsing |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 633 | * incoming request. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 634 | */ |
| 635 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 636 | struct htx_sl *sl; |
| 637 | struct ist uri, path; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 638 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 639 | if (!sockaddr_alloc(&s->target_addr, NULL, 0)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 640 | if (!(s->flags & SF_ERR_MASK)) |
| 641 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 642 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 643 | } |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 644 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 645 | uri = htx_sl_req_uri(sl); |
| 646 | path = http_get_path(uri); |
Willy Tarreau | a48f4b3 | 2019-07-17 15:11:59 +0200 | [diff] [blame] | 647 | |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 648 | if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 649 | goto return_bad_req; |
| 650 | |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 651 | s->target = &s->be->obj_type; |
| 652 | s->flags |= SF_ADDR_SET | SF_ASSIGNED; |
| 653 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 654 | /* if the path was found, we have to remove everything between |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 655 | * uri.ptr and path.ptr (excluded). If it was not found, we need |
| 656 | * to replace from all the uri by a single "/". |
| 657 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 658 | * Instead of rewriting the whole start line, we just update |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 659 | * the star-line URI. Some space will be lost but it should be |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 660 | * insignificant. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 661 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 662 | istcpy(&uri, (path.len ? path : ist("/")), uri.len); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /* |
| 666 | * 7: Now we can work with the cookies. |
| 667 | * Note that doing so might move headers in the request, but |
| 668 | * the fields will stay coherent and the URI will not move. |
| 669 | * This should only be performed in the backend. |
| 670 | */ |
| 671 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 672 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 673 | |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 674 | /* 8: Generate unique ID if a "unique-id-format" is defined. |
| 675 | * |
| 676 | * A unique ID is generated even when it is not sent to ensure that the ID can make use of |
| 677 | * fetches only available in the HTTP request processing stage. |
| 678 | */ |
| 679 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 680 | 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] | 681 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 682 | if (!isttest(unique_id)) { |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 683 | if (!(s->flags & SF_ERR_MASK)) |
| 684 | s->flags |= SF_ERR_RESOURCE; |
| 685 | goto return_int_err; |
| 686 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 687 | |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 688 | /* send unique ID if a "unique-id-header" is defined */ |
Tim Duesterhus | 0643b0e | 2020-03-05 17:56:35 +0100 | [diff] [blame] | 689 | if (isttest(sess->fe->header_unique_id) && |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 690 | unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id))) |
Christopher Faulet | 028c164 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 691 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /* |
| 695 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 696 | * asks for it. |
| 697 | */ |
| 698 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 699 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 700 | struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 701 | s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len); |
| 702 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 703 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 704 | http_find_header(htx, hdr, &ctx, 0)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 705 | /* The header is set to be added only if none is present |
| 706 | * and we found it, so don't do anything. |
| 707 | */ |
| 708 | } |
Willy Tarreau | a48f4b3 | 2019-07-17 15:11:59 +0200 | [diff] [blame] | 709 | else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 710 | /* Add an X-Forwarded-For header unless the source IP is |
| 711 | * in the 'except' network range. |
| 712 | */ |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 713 | if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) && |
| 714 | ipcmp2net(cli_conn->src, &s->be->except_xff_net)) { |
Willy Tarreau | a48f4b3 | 2019-07-17 15:11:59 +0200 | [diff] [blame] | 715 | unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 716 | |
| 717 | /* Note: we rely on the backend to get the header name to be used for |
| 718 | * x-forwarded-for, because the header is really meant for the backends. |
| 719 | * However, if the backend did not specify any option, we have to rely |
| 720 | * on the frontend's header name. |
| 721 | */ |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 722 | chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 723 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
Christopher Faulet | 028c164 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 724 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 725 | } |
| 726 | } |
Willy Tarreau | a48f4b3 | 2019-07-17 15:11:59 +0200 | [diff] [blame] | 727 | else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) { |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 728 | /* Add an X-Forwarded-For header unless the source IP is |
| 729 | * in the 'except' network range. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 730 | */ |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 731 | if (ipcmp2net(cli_conn->src, &sess->fe->except_xff_net) && |
| 732 | ipcmp2net(cli_conn->src, &s->be->except_xff_net)) { |
| 733 | char pn[INET6_ADDRSTRLEN]; |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 734 | |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 735 | inet_ntop(AF_INET6, |
| 736 | (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr, |
| 737 | pn, sizeof(pn)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 738 | |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 739 | /* Note: we rely on the backend to get the header name to be used for |
| 740 | * x-forwarded-for, because the header is really meant for the backends. |
| 741 | * However, if the backend did not specify any option, we have to rely |
| 742 | * on the frontend's header name. |
| 743 | */ |
| 744 | chunk_printf(&trash, "%s", pn); |
| 745 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
Christopher Faulet | 028c164 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 746 | goto return_fail_rewrite; |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 747 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
| 751 | /* |
| 752 | * 10: add X-Original-To if either the frontend or the backend |
| 753 | * asks for it. |
| 754 | */ |
| 755 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 756 | struct ist hdr = ist2(s->be->orgto_hdr_len ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name, |
| 757 | s->be->orgto_hdr_len ? s->be->orgto_hdr_len : sess->fe->orgto_hdr_len); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 758 | |
Christopher Faulet | cccded9 | 2021-02-26 12:45:56 +0100 | [diff] [blame] | 759 | if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 760 | /* Add an X-Original-To header unless the destination IP is |
| 761 | * in the 'except' network range. |
| 762 | */ |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 763 | if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) && |
| 764 | ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) { |
Willy Tarreau | a48f4b3 | 2019-07-17 15:11:59 +0200 | [diff] [blame] | 765 | unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 766 | |
| 767 | /* Note: we rely on the backend to get the header name to be used for |
| 768 | * x-original-to, because the header is really meant for the backends. |
| 769 | * However, if the backend did not specify any option, we have to rely |
| 770 | * on the frontend's header name. |
| 771 | */ |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 772 | chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 773 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
Christopher Faulet | 028c164 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 774 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 775 | } |
| 776 | } |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 777 | else if (cli_conn && conn_get_dst(cli_conn) && cli_conn->dst->ss_family == AF_INET6) { |
| 778 | /* Add an X-Original-To header unless the source IP is |
| 779 | * in the 'except' network range. |
| 780 | */ |
| 781 | if (ipcmp2net(cli_conn->dst, &sess->fe->except_xot_net) && |
| 782 | ipcmp2net(cli_conn->dst, &s->be->except_xot_net)) { |
| 783 | char pn[INET6_ADDRSTRLEN]; |
| 784 | |
| 785 | inet_ntop(AF_INET6, |
| 786 | (const void *)&((struct sockaddr_in6 *)(cli_conn->dst))->sin6_addr, |
| 787 | pn, sizeof(pn)); |
| 788 | |
| 789 | /* Note: we rely on the backend to get the header name to be used for |
| 790 | * x-forwarded-for, because the header is really meant for the backends. |
| 791 | * However, if the backend did not specify any option, we have to rely |
| 792 | * on the frontend's header name. |
| 793 | */ |
| 794 | chunk_printf(&trash, "%s", pn); |
| 795 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
Christopher Faulet | 028c164 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 796 | goto return_fail_rewrite; |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 797 | } |
| 798 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 799 | } |
| 800 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 801 | /* Filter the request headers if there are filters attached to the |
| 802 | * stream. |
| 803 | */ |
| 804 | if (HAS_FILTERS(s)) |
| 805 | req->analysers |= AN_REQ_FLT_HTTP_HDRS; |
| 806 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 807 | /* If we have no server assigned yet and we're balancing on url_param |
| 808 | * with a POST request, we may be interested in checking the body for |
| 809 | * that parameter. This will be done in another analyser. |
| 810 | */ |
| 811 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | 089eaa0 | 2019-01-14 15:17:46 +0100 | [diff] [blame] | 812 | s->txn->meth == HTTP_METH_POST && |
| 813 | (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 814 | channel_dont_connect(req); |
| 815 | req->analysers |= AN_REQ_HTTP_BODY; |
| 816 | } |
| 817 | |
| 818 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 819 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 820 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 821 | /* We expect some data from the client. Unless we know for sure |
| 822 | * we already have a full request, we have to re-enable quick-ack |
| 823 | * in case we previously disabled it, otherwise we might cause |
| 824 | * the client to delay further data. |
| 825 | */ |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 826 | 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] | 827 | conn_set_quickack(cli_conn, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 828 | |
| 829 | /************************************************************* |
| 830 | * OK, that's finished for the headers. We have done what we * |
| 831 | * could. Let's switch to the DATA state. * |
| 832 | ************************************************************/ |
| 833 | req->analyse_exp = TICK_ETERNITY; |
| 834 | req->analysers &= ~an_bit; |
| 835 | |
| 836 | s->logs.tv_request = now; |
| 837 | /* OK let's go on with the BODY now */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 838 | 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] | 839 | return 1; |
| 840 | |
Christopher Faulet | 028c164 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 841 | return_fail_rewrite: |
| 842 | if (!(s->flags & SF_ERR_MASK)) |
| 843 | s->flags |= SF_ERR_PRXCOND; |
| 844 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
| 845 | if (s->flags & SF_BE_ASSIGNED) |
| 846 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
| 847 | if (sess->listener && sess->listener->counters) |
| 848 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
| 849 | if (objt_server(s->target)) |
| 850 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
| 851 | /* fall through */ |
| 852 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 853 | return_int_err: |
| 854 | txn->status = 500; |
| 855 | if (!(s->flags & SF_ERR_MASK)) |
| 856 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 857 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 858 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 859 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 860 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 861 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 862 | goto return_prx_cond; |
| 863 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 864 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 865 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 866 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 867 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 868 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 869 | /* fall through */ |
| 870 | |
| 871 | return_prx_cond: |
| 872 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 873 | |
| 874 | if (!(s->flags & SF_ERR_MASK)) |
| 875 | s->flags |= SF_ERR_PRXCOND; |
| 876 | if (!(s->flags & SF_FINST_MASK)) |
| 877 | s->flags |= SF_FINST_R; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 878 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 879 | DBG_TRACE_DEVEL("leaving on error", |
| 880 | 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] | 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 885 | * returns zero, at the beginning because it prevents any other processing |
| 886 | * from occurring, and at the end because it terminates the request. |
| 887 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 888 | 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] | 889 | { |
| 890 | struct http_txn *txn = s->txn; |
| 891 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 892 | 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] | 893 | /* This connection is being tarpitted. The CLIENT side has |
| 894 | * already set the connect expiration date to the right |
| 895 | * timeout. We just have to check that the client is still |
| 896 | * there and that the timeout has not expired. |
| 897 | */ |
| 898 | channel_dont_connect(req); |
| 899 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 900 | !tick_is_expired(req->analyse_exp, now_ms)) { |
Christopher Faulet | b02d5f0 | 2021-10-29 14:37:07 +0200 | [diff] [blame] | 901 | /* Be sure to drain all data from the request channel */ |
| 902 | channel_htx_erase(req, htxbuf(&req->buf)); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 903 | DBG_TRACE_DEVEL("waiting for tarpit timeout expiry", |
| 904 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 905 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 906 | } |
| 907 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 908 | |
| 909 | /* We will set the queue timer to the time spent, just for |
| 910 | * logging purposes. We fake a 500 server error, so that the |
| 911 | * attacker will not suspect his connection has been tarpitted. |
| 912 | * It will not cause trouble to the logs because we can exclude |
| 913 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 914 | */ |
| 915 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 916 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 917 | http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL)); |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 918 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 919 | if (!(s->flags & SF_ERR_MASK)) |
| 920 | s->flags |= SF_ERR_PRXCOND; |
| 921 | if (!(s->flags & SF_FINST_MASK)) |
| 922 | s->flags |= SF_FINST_T; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 923 | |
| 924 | 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] | 925 | return 0; |
| 926 | } |
| 927 | |
| 928 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 929 | * for either the buffer to be full, or the full advertised contents to have |
| 930 | * reached the buffer. It must only be called after the standard HTTP request |
| 931 | * processing has occurred, because it expects the request to be parsed and will |
| 932 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 933 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 934 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 935 | * needs to read more data, or 1 once it has completed its analysis. |
| 936 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 937 | 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] | 938 | { |
| 939 | struct session *sess = s->sess; |
| 940 | struct http_txn *txn = s->txn; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 941 | |
Christopher Faulet | 1a023fa | 2023-01-13 10:49:31 +0100 | [diff] [blame] | 942 | 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] | 943 | |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 944 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 945 | switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) { |
| 946 | case HTTP_RULE_RES_CONT: |
| 947 | goto http_end; |
| 948 | case HTTP_RULE_RES_YIELD: |
| 949 | goto missing_data_or_waiting; |
| 950 | case HTTP_RULE_RES_BADREQ: |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 951 | goto return_bad_req; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 952 | case HTTP_RULE_RES_ERROR: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 953 | goto return_int_err; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 954 | case HTTP_RULE_RES_ABRT: |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 955 | goto return_prx_cond; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 956 | default: |
| 957 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | http_end: |
| 961 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 962 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 963 | req->analysers &= ~an_bit; |
| 964 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 965 | 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] | 966 | return 1; |
| 967 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 968 | missing_data_or_waiting: |
| 969 | channel_dont_connect(req); |
| 970 | DBG_TRACE_DEVEL("waiting for more data", |
| 971 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 972 | return 0; |
| 973 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 974 | return_int_err: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 975 | txn->status = 500; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 976 | if (!(s->flags & SF_ERR_MASK)) |
| 977 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 978 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 979 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 980 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 981 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 982 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 983 | goto return_prx_err; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 984 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 985 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 986 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 987 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 988 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 989 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 990 | /* fall through */ |
| 991 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 992 | return_prx_err: |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 993 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 994 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 995 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 996 | return_prx_cond: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 997 | if (!(s->flags & SF_ERR_MASK)) |
| 998 | s->flags |= SF_ERR_PRXCOND; |
| 999 | if (!(s->flags & SF_FINST_MASK)) |
Christopher Faulet | 1a023fa | 2023-01-13 10:49:31 +0100 | [diff] [blame] | 1000 | s->flags |= SF_FINST_R; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1001 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1002 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1003 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1004 | DBG_TRACE_DEVEL("leaving on error", |
| 1005 | 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] | 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | /* This function is an analyser which forwards request body (including chunk |
| 1010 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 1011 | * zero byte. The only situation where it must not be called is when we're in |
| 1012 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 1013 | * remaining data and to resync after end of body. It expects the msg_state to |
| 1014 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 1015 | * read more data, or 1 once we can go on with next request or end the stream. |
| 1016 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
| 1017 | * bytes of pending data + the headers if not already done. |
| 1018 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1019 | 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] | 1020 | { |
| 1021 | struct session *sess = s->sess; |
| 1022 | struct http_txn *txn = s->txn; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1023 | struct http_msg *msg = &txn->req; |
| 1024 | struct htx *htx; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1025 | short status = 0; |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1026 | int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1027 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1028 | 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] | 1029 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1030 | htx = htxbuf(&req->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1031 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1032 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 1033 | goto return_bad_req; |
| 1034 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 1035 | goto return_int_err; |
| 1036 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1037 | if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
| 1038 | ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) { |
| 1039 | /* Output closed while we were sending data. We must abort and |
| 1040 | * wake the other side up. |
Christopher Faulet | f506d96 | 2021-04-27 10:56:28 +0200 | [diff] [blame] | 1041 | * |
| 1042 | * If we have finished to send the request and the response is |
| 1043 | * still in progress, don't catch write error on the request |
| 1044 | * side if it is in fact a read error on the server side. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1045 | */ |
Christopher Faulet | f506d96 | 2021-04-27 10:56:28 +0200 | [diff] [blame] | 1046 | if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers) |
| 1047 | return 0; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1048 | |
Olivier Houchard | 29cac3c | 2019-07-12 15:48:58 +0200 | [diff] [blame] | 1049 | /* Don't abort yet if we had L7 retries activated and it |
| 1050 | * was a write error, we may recover. |
| 1051 | */ |
| 1052 | if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) && |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1053 | (s->si[1].flags & SI_FL_L7_RETRY)) { |
| 1054 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1055 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Olivier Houchard | 29cac3c | 2019-07-12 15:48:58 +0200 | [diff] [blame] | 1056 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1057 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1058 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1059 | http_end_request(s); |
| 1060 | http_end_response(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1061 | DBG_TRACE_DEVEL("leaving on error", |
| 1062 | 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] | 1063 | return 1; |
| 1064 | } |
| 1065 | |
| 1066 | /* Note that we don't have to send 100-continue back because we don't |
| 1067 | * need the data to complete our job, and it's up to the server to |
| 1068 | * decide whether to return 100, 417 or anything else in return of |
| 1069 | * an "Expect: 100-continue" header. |
| 1070 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1071 | if (msg->msg_state == HTTP_MSG_BODY) |
| 1072 | msg->msg_state = HTTP_MSG_DATA; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1073 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1074 | /* in most states, we should abort in case of early close */ |
| 1075 | channel_auto_close(req); |
| 1076 | |
| 1077 | if (req->to_forward) { |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 1078 | if (req->to_forward == CHN_INFINITE_FORWARD) { |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1079 | if (req->flags & CF_EOI) |
| 1080 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 1081 | } |
| 1082 | else { |
| 1083 | /* We can't process the buffer's contents yet */ |
| 1084 | req->flags |= CF_WAKE_WRITE; |
| 1085 | goto missing_data_or_waiting; |
| 1086 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1087 | } |
| 1088 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1089 | if (msg->msg_state >= HTTP_MSG_ENDING) |
| 1090 | goto ending; |
| 1091 | |
| 1092 | if (txn->meth == HTTP_METH_CONNECT) { |
| 1093 | msg->msg_state = HTTP_MSG_ENDING; |
| 1094 | goto ending; |
| 1095 | } |
| 1096 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1097 | /* Forward input data. We get it by removing all outgoing data not |
| 1098 | * forwarded yet from HTX data size. If there are some data filters, we |
| 1099 | * let them decide the amount of data to forward. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1100 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1101 | if (HAS_REQ_DATA_FILTERS(s)) { |
| 1102 | ret = flt_http_payload(s, msg, htx->data); |
| 1103 | if (ret < 0) |
| 1104 | goto return_bad_req; |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1105 | c_adv(req, ret); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1106 | } |
| 1107 | else { |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1108 | c_adv(req, htx->data - co_data(req)); |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 1109 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
| 1110 | channel_htx_forward_forever(req, htx); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1111 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1112 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1113 | if (htx->data != co_data(req)) |
| 1114 | goto missing_data_or_waiting; |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1115 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1116 | /* 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] | 1117 | * in HTTP_MSG_ENDING state. Then if all data was marked to be |
| 1118 | * forwarded, set the state to HTTP_MSG_DONE. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1119 | */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 1120 | if (!(htx->flags & HTX_FL_EOM)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1121 | goto missing_data_or_waiting; |
| 1122 | |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1123 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1124 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1125 | ending: |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 1126 | req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */ |
| 1127 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1128 | /* other states, ENDING...TUNNEL */ |
| 1129 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 1130 | goto done; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1131 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1132 | if (HAS_REQ_DATA_FILTERS(s)) { |
| 1133 | ret = flt_http_end(s, msg); |
| 1134 | if (ret <= 0) { |
| 1135 | if (!ret) |
| 1136 | goto missing_data_or_waiting; |
| 1137 | goto return_bad_req; |
| 1138 | } |
| 1139 | } |
| 1140 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1141 | if (txn->meth == HTTP_METH_CONNECT) |
| 1142 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 1143 | else { |
| 1144 | msg->msg_state = HTTP_MSG_DONE; |
| 1145 | req->to_forward = 0; |
| 1146 | } |
| 1147 | |
| 1148 | done: |
| 1149 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 1150 | if (!(txn->flags & TX_CON_WANT_TUN)) |
| 1151 | channel_dont_close(req); |
| 1152 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1153 | http_end_request(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1154 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1155 | http_end_response(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1156 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 1157 | if (req->flags & CF_SHUTW) { |
| 1158 | /* request errors are most likely due to the |
| 1159 | * server aborting the transfer. */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1160 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1161 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1162 | goto return_bad_req; |
| 1163 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1164 | 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] | 1165 | return 1; |
| 1166 | } |
| 1167 | |
| 1168 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 1169 | * the client's connection and forward any shutdown notification to the |
| 1170 | * server, which will decide whether to close or to go on processing the |
| 1171 | * request. We only do that in tunnel mode, and not in other modes since |
| 1172 | * it can be abused to exhaust source ports. */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 1173 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1174 | channel_auto_read(req); |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 1175 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1176 | s->si[1].flags |= SI_FL_NOLINGER; |
| 1177 | channel_auto_close(req); |
| 1178 | } |
| 1179 | else if (s->txn->meth == HTTP_METH_POST) { |
| 1180 | /* POST requests may require to read extra CRLF sent by broken |
| 1181 | * browsers and which could cause an RST to be sent upon close |
| 1182 | * on some systems (eg: Linux). */ |
| 1183 | channel_auto_read(req); |
| 1184 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1185 | DBG_TRACE_DEVEL("waiting for the end of the HTTP txn", |
| 1186 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1187 | return 0; |
| 1188 | |
| 1189 | missing_data_or_waiting: |
| 1190 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1191 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1192 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1193 | |
| 1194 | waiting: |
| 1195 | /* waiting for the last bits to leave the buffer */ |
| 1196 | if (req->flags & CF_SHUTW) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1197 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1198 | |
| 1199 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 1200 | * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE. |
| 1201 | * And when content-length is used, we never want to let the possible |
| 1202 | * shutdown be forwarded to the other side, as the state machine will |
| 1203 | * take care of it once the client responds. It's also important to |
| 1204 | * prevent TIME_WAITs from accumulating on the backend side, and for |
| 1205 | * HTTP/2 where the last frame comes with a shutdown. |
| 1206 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1207 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1208 | channel_dont_close(req); |
| 1209 | |
| 1210 | /* We know that more data are expected, but we couldn't send more that |
| 1211 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
| 1212 | * system knows it must not set a PUSH on this first part. Interactive |
| 1213 | * modes are already handled by the stream sock layer. We must not do |
| 1214 | * this in content-length mode because it could present the MSG_MORE |
| 1215 | * flag with the last block of forwarded data, which would cause an |
| 1216 | * additional delay to be observed by the receiver. |
| 1217 | */ |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 1218 | if (HAS_REQ_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1219 | req->flags |= CF_EXPECT_MORE; |
| 1220 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1221 | DBG_TRACE_DEVEL("waiting for more data to forward", |
| 1222 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1223 | return 0; |
| 1224 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1225 | return_cli_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1226 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 1227 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1228 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1229 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1230 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1231 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1232 | if (!(s->flags & SF_ERR_MASK)) |
| 1233 | s->flags |= SF_ERR_CLICL; |
| 1234 | status = 400; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1235 | goto return_prx_cond; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1236 | |
| 1237 | return_srv_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1238 | _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts); |
| 1239 | _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1240 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1241 | _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1242 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1243 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1244 | if (!(s->flags & SF_ERR_MASK)) |
| 1245 | s->flags |= SF_ERR_SRVCL; |
| 1246 | status = 502; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1247 | goto return_prx_cond; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1248 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1249 | return_int_err: |
| 1250 | if (!(s->flags & SF_ERR_MASK)) |
| 1251 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1252 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 1253 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1254 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1255 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1256 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1257 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1258 | status = 500; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1259 | goto return_prx_cond; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1260 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1261 | return_bad_req: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1262 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1263 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1264 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1265 | status = 400; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1266 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1267 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1268 | return_prx_cond: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1269 | if (txn->status > 0) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1270 | /* 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] | 1271 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1272 | } else { |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1273 | txn->status = status; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1274 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1275 | } |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1276 | if (!(s->flags & SF_ERR_MASK)) |
| 1277 | s->flags |= SF_ERR_PRXCOND; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1278 | if (!(s->flags & SF_FINST_MASK)) |
| 1279 | s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1280 | DBG_TRACE_DEVEL("leaving on error ", |
| 1281 | 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] | 1282 | return 0; |
| 1283 | } |
| 1284 | |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1285 | /* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */ |
| 1286 | /* Returns 0 if we can attempt to retry, -1 otherwise */ |
| 1287 | static __inline int do_l7_retry(struct stream *s, struct stream_interface *si) |
| 1288 | { |
Christopher Faulet | 5bf8585 | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1289 | struct channel *req, *res; |
| 1290 | int co_data; |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1291 | |
| 1292 | si->conn_retries--; |
| 1293 | if (si->conn_retries < 0) |
Christopher Faulet | 043cdb2 | 2021-05-26 10:31:06 +0200 | [diff] [blame] | 1294 | return -1; |
Christopher Faulet | 5b82cc5 | 2020-10-12 15:18:50 +0200 | [diff] [blame] | 1295 | |
Christopher Faulet | e763c8c | 2021-05-05 18:23:59 +0200 | [diff] [blame] | 1296 | if (objt_server(s->target)) { |
| 1297 | if (s->flags & SF_CURR_SESS) { |
| 1298 | s->flags &= ~SF_CURR_SESS; |
| 1299 | _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess); |
| 1300 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1301 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries); |
Christopher Faulet | e763c8c | 2021-05-05 18:23:59 +0200 | [diff] [blame] | 1302 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1303 | _HA_ATOMIC_INC(&s->be->be_counters.retries); |
Willy Tarreau | 223995e | 2019-05-04 10:38:31 +0200 | [diff] [blame] | 1304 | |
Christopher Faulet | 5bf8585 | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1305 | req = &s->req; |
| 1306 | res = &s->res; |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1307 | /* Remove any write error from the request, and read error from the response */ |
| 1308 | req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW); |
| 1309 | res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW); |
Christopher Faulet | 42d4ee1 | 2022-01-04 10:56:03 +0100 | [diff] [blame] | 1310 | res->analysers &= AN_RES_FLT_END; |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1311 | si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT); |
Christopher Faulet | 30aa0da | 2021-05-05 21:05:09 +0200 | [diff] [blame] | 1312 | si->err_type = SI_ET_NONE; |
| 1313 | s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK); |
Olivier Houchard | 4bd5867 | 2019-07-12 16:16:59 +0200 | [diff] [blame] | 1314 | stream_choose_redispatch(s); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1315 | si->exp = TICK_ETERNITY; |
| 1316 | res->rex = TICK_ETERNITY; |
| 1317 | res->to_forward = 0; |
| 1318 | res->analyse_exp = TICK_ETERNITY; |
| 1319 | res->total = 0; |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1320 | si_release_endpoint(&s->si[1]); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1321 | |
Christopher Faulet | 5bf8585 | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1322 | b_free(&req->buf); |
| 1323 | /* Swap the L7 buffer with the channel buffer */ |
| 1324 | /* We know we stored the co_data as b_data, so get it there */ |
| 1325 | co_data = b_data(&si->l7_buffer); |
| 1326 | b_set_data(&si->l7_buffer, b_size(&si->l7_buffer)); |
| 1327 | b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer)); |
| 1328 | co_set_data(req, co_data); |
Christopher Faulet | 5b82cc5 | 2020-10-12 15:18:50 +0200 | [diff] [blame] | 1329 | |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 1330 | DBG_TRACE_DEVEL("perform a L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, s->txn); |
Christopher Faulet | 5bf8585 | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1331 | |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1332 | b_reset(&res->buf); |
| 1333 | co_set_data(res, 0); |
| 1334 | return 0; |
| 1335 | } |
| 1336 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1337 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 1338 | * processing can continue on next analysers, or zero if it either needs more |
| 1339 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 1340 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers |
| 1341 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1342 | * abort. |
| 1343 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1344 | 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] | 1345 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1346 | /* |
| 1347 | * We will analyze a complete HTTP response to check the its syntax. |
| 1348 | * |
| 1349 | * Once the start line and all headers are received, we may perform a |
| 1350 | * capture of the error (if any), and we will set a few fields. We also |
| 1351 | * logging and finally headers capture. |
| 1352 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1353 | struct session *sess = s->sess; |
| 1354 | struct http_txn *txn = s->txn; |
| 1355 | struct http_msg *msg = &txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1356 | struct htx *htx; |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1357 | struct stream_interface *si_b = &s->si[1]; |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1358 | struct connection *srv_conn; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1359 | struct htx_sl *sl; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1360 | int n; |
| 1361 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1362 | 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] | 1363 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1364 | htx = htxbuf(&rep->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1365 | |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1366 | /* Parsing errors are caught here */ |
| 1367 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 1368 | goto return_bad_res; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1369 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 1370 | goto return_int_err; |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1371 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1372 | /* |
| 1373 | * Now we quickly check if we have found a full valid response. |
| 1374 | * If not so, we check the FD and buffer states before leaving. |
| 1375 | * A full response is indicated by the fact that we have seen |
| 1376 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
| 1377 | * responses are checked first. |
| 1378 | * |
| 1379 | * Depending on whether the client is still there or not, we |
| 1380 | * may send an error response back or not. Note that normally |
| 1381 | * we should only check for HTTP status there, and check I/O |
| 1382 | * errors somewhere else. |
| 1383 | */ |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1384 | next_one: |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 1385 | if (unlikely(htx_is_empty(htx) || htx->first == -1)) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1386 | /* 1: have we encountered a read error ? */ |
| 1387 | if (rep->flags & CF_READ_ERROR) { |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1388 | struct connection *conn = NULL; |
| 1389 | |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1390 | if (objt_cs(s->si[1].end)) |
Willy Tarreau | 6618259 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1391 | conn = __objt_cs(s->si[1].end)->conn; |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1392 | |
Christopher Faulet | b5265fe | 2023-03-01 15:47:18 +0100 | [diff] [blame] | 1393 | if ((si_b->flags & SI_FL_L7_RETRY) && |
| 1394 | (s->be->retry_type & PR_RE_DISCONNECTED) && |
| 1395 | (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) { |
| 1396 | if (co_data(rep) || do_l7_retry(s, si_b) == 0) |
| 1397 | return 0; |
| 1398 | } |
| 1399 | |
Christopher Faulet | b187534 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1400 | /* Perform a L7 retry because server refuses the early data. */ |
| 1401 | if ((si_b->flags & SI_FL_L7_RETRY) && |
| 1402 | (s->be->retry_type & PR_RE_EARLY_ERROR) && |
| 1403 | conn && conn->err_code == CO_ER_SSL_EARLY_FAILED && |
| 1404 | do_l7_retry(s, si_b) == 0) { |
| 1405 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1406 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
Christopher Faulet | 6f8118e | 2024-04-09 08:19:01 +0200 | [diff] [blame^] | 1410 | if (s->flags & SF_SRV_REUSED) |
Olivier Houchard | 6db1699 | 2019-05-17 15:40:49 +0200 | [diff] [blame] | 1411 | goto abort_keep_alive; |
| 1412 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1413 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1414 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1415 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1416 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1417 | } |
| 1418 | |
Christopher Faulet | b187534 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1419 | /* if the server refused the early data, just send a 425 */ |
| 1420 | if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1421 | txn->status = 425; |
Christopher Faulet | b187534 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1422 | else { |
| 1423 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1424 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | b187534 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1425 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1426 | |
| 1427 | s->si[1].flags |= SI_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1428 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1429 | |
| 1430 | if (!(s->flags & SF_ERR_MASK)) |
| 1431 | s->flags |= SF_ERR_SRVCL; |
| 1432 | if (!(s->flags & SF_FINST_MASK)) |
| 1433 | s->flags |= SF_FINST_H; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1434 | DBG_TRACE_DEVEL("leaving on error", |
| 1435 | 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] | 1436 | return 0; |
| 1437 | } |
| 1438 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1439 | /* 2: read timeout : return a 504 to the client. */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1440 | else if (rep->flags & CF_READ_TIMEOUT) { |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1441 | if ((si_b->flags & SI_FL_L7_RETRY) && |
| 1442 | (s->be->retry_type & PR_RE_TIMEOUT)) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1443 | if (co_data(rep) || do_l7_retry(s, si_b) == 0) { |
| 1444 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1445 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1446 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1447 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1448 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1449 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1450 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1451 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1452 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1453 | } |
| 1454 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1455 | txn->status = 504; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1456 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1457 | s->si[1].flags |= SI_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1458 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1459 | |
| 1460 | if (!(s->flags & SF_ERR_MASK)) |
| 1461 | s->flags |= SF_ERR_SRVTO; |
| 1462 | if (!(s->flags & SF_FINST_MASK)) |
| 1463 | s->flags |= SF_FINST_H; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1464 | DBG_TRACE_DEVEL("leaving on error", |
| 1465 | 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] | 1466 | return 0; |
| 1467 | } |
| 1468 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1469 | /* 3: client abort with an abortonclose */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1470 | 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] | 1471 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 1472 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1473 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1474 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1475 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1476 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1477 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1478 | txn->status = 400; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1479 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1480 | |
| 1481 | if (!(s->flags & SF_ERR_MASK)) |
| 1482 | s->flags |= SF_ERR_CLICL; |
| 1483 | if (!(s->flags & SF_FINST_MASK)) |
| 1484 | s->flags |= SF_FINST_H; |
| 1485 | |
| 1486 | /* process_stream() will take care of the error */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1487 | DBG_TRACE_DEVEL("leaving on error", |
| 1488 | 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] | 1489 | return 0; |
| 1490 | } |
| 1491 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1492 | /* 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] | 1493 | else if (rep->flags & CF_SHUTR) { |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1494 | if ((si_b->flags & SI_FL_L7_RETRY) && |
| 1495 | (s->be->retry_type & PR_RE_DISCONNECTED)) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1496 | if (co_data(rep) || do_l7_retry(s, si_b) == 0) { |
| 1497 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1498 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1499 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1500 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1501 | } |
| 1502 | |
Christopher Faulet | 6f8118e | 2024-04-09 08:19:01 +0200 | [diff] [blame^] | 1503 | if (s->flags & SF_SRV_REUSED) |
Olivier Houchard | 6db1699 | 2019-05-17 15:40:49 +0200 | [diff] [blame] | 1504 | goto abort_keep_alive; |
| 1505 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1506 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1507 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1508 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1509 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1510 | } |
| 1511 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1512 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1513 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1514 | s->si[1].flags |= SI_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1515 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1516 | |
| 1517 | if (!(s->flags & SF_ERR_MASK)) |
| 1518 | s->flags |= SF_ERR_SRVCL; |
| 1519 | if (!(s->flags & SF_FINST_MASK)) |
| 1520 | s->flags |= SF_FINST_H; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1521 | DBG_TRACE_DEVEL("leaving on error", |
| 1522 | 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] | 1523 | return 0; |
| 1524 | } |
| 1525 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1526 | /* 5: write error to client (we don't send any message then) */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1527 | else if (rep->flags & CF_WRITE_ERROR) { |
Christopher Faulet | 6f8118e | 2024-04-09 08:19:01 +0200 | [diff] [blame^] | 1528 | if (s->flags & SF_SRV_REUSED) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1529 | goto abort_keep_alive; |
| 1530 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1531 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1532 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1533 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1534 | rep->analysers &= AN_RES_FLT_END; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1535 | |
| 1536 | if (!(s->flags & SF_ERR_MASK)) |
| 1537 | s->flags |= SF_ERR_CLICL; |
| 1538 | if (!(s->flags & SF_FINST_MASK)) |
| 1539 | s->flags |= SF_FINST_H; |
| 1540 | |
| 1541 | /* process_stream() will take care of the error */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1542 | DBG_TRACE_DEVEL("leaving on error", |
| 1543 | 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] | 1544 | return 0; |
| 1545 | } |
| 1546 | |
| 1547 | channel_dont_close(rep); |
| 1548 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1549 | DBG_TRACE_DEVEL("waiting for more data", |
| 1550 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1551 | return 0; |
| 1552 | } |
| 1553 | |
| 1554 | /* More interesting part now : we know that we have a complete |
| 1555 | * response which at least looks like HTTP. We have an indicator |
| 1556 | * of each header's length, so we can parse them quickly. |
| 1557 | */ |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 1558 | BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1559 | sl = http_get_stline(htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1560 | |
Christopher Faulet | 0f66d55 | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1561 | /* Perform a L7 retry because of the status code */ |
| 1562 | if ((si_b->flags & SI_FL_L7_RETRY) && |
| 1563 | l7_status_match(s->be, sl->info.res.status) && |
| 1564 | do_l7_retry(s, si_b) == 0) { |
| 1565 | DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 1566 | return 0; |
| 1567 | } |
| 1568 | |
| 1569 | /* Now, L7 buffer is useless, it can be released */ |
| 1570 | b_free(&s->si[1].l7_buffer); |
| 1571 | |
| 1572 | msg->msg_state = HTTP_MSG_BODY; |
| 1573 | |
| 1574 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1575 | /* 0: we might have to print this header in debug mode */ |
| 1576 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1577 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 1578 | int32_t pos; |
| 1579 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1580 | http_debug_stline("srvrep", s, sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1581 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 1582 | 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] | 1583 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 1584 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 1585 | |
| 1586 | if (type == HTX_BLK_EOH) |
| 1587 | break; |
| 1588 | if (type != HTX_BLK_HDR) |
| 1589 | continue; |
| 1590 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1591 | http_debug_hdr("srvhdr", s, |
| 1592 | htx_get_blk_name(htx, blk), |
| 1593 | htx_get_blk_value(htx, blk)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1594 | } |
| 1595 | } |
| 1596 | |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1597 | /* 1: get the status code and the version. Also set HTTP flags */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1598 | txn->status = sl->info.res.status; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1599 | if (sl->flags & HTX_SL_F_VER_11) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1600 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1601 | if (sl->flags & HTX_SL_F_XFER_LEN) { |
| 1602 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 1603 | if (sl->flags & HTX_SL_F_CLEN) |
| 1604 | msg->flags |= HTTP_MSGF_CNT_LEN; |
| 1605 | else if (sl->flags & HTX_SL_F_CHNK) |
| 1606 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1607 | } |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 1608 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 1609 | msg->flags |= HTTP_MSGF_BODYLESS; |
Christopher Faulet | 576c358 | 2021-01-08 15:53:01 +0100 | [diff] [blame] | 1610 | if (sl->flags & HTX_SL_F_CONN_UPG) |
| 1611 | msg->flags |= HTTP_MSGF_CONN_UPG; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1612 | |
| 1613 | n = txn->status / 100; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1614 | if (n < 1 || n > 5) |
| 1615 | n = 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1616 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1617 | /* when the client triggers a 4xx from the server, it's most often due |
| 1618 | * to a missing object or permission. These events should be tracked |
| 1619 | * because if they happen often, it may indicate a brute force or a |
| 1620 | * vulnerability scan. |
| 1621 | */ |
| 1622 | if (n == 4) |
| 1623 | stream_inc_http_err_ctr(s); |
| 1624 | |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1625 | if (n == 5 && txn->status != 501 && txn->status != 505) |
| 1626 | stream_inc_http_fail_ctr(s); |
| 1627 | |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 1628 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1629 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]); |
| 1630 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req); |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 1631 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1632 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1633 | /* Adjust server's health based on status code. Note: status codes 501 |
| 1634 | * and 505 are triggered on demand by client request, so we must not |
| 1635 | * count them as server failures. |
| 1636 | */ |
| 1637 | if (objt_server(s->target)) { |
| 1638 | 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] | 1639 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1640 | else |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1641 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | /* |
| 1645 | * We may be facing a 100-continue response, or any other informational |
| 1646 | * 1xx response which is non-final, in which case this is not the right |
| 1647 | * response, and we're waiting for the next one. Let's allow this response |
| 1648 | * to go to the client and wait for the next one. There's an exception for |
| 1649 | * 101 which is used later in the code to switch protocols. |
| 1650 | */ |
| 1651 | if (txn->status < 200 && |
| 1652 | (txn->status == 100 || txn->status >= 102)) { |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1653 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1654 | htx->first = channel_htx_fwd_headers(rep, htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1655 | msg->msg_state = HTTP_MSG_RPBEFORE; |
Christopher Faulet | 3499f62 | 2019-09-03 15:23:54 +0200 | [diff] [blame] | 1656 | msg->flags = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1657 | txn->status = 0; |
| 1658 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 7d51845 | 2020-08-31 11:07:07 +0200 | [diff] [blame] | 1659 | rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */ |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1660 | goto next_one; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1661 | } |
| 1662 | |
Christopher Faulet | 6e6c7b1 | 2021-01-08 16:02:05 +0100 | [diff] [blame] | 1663 | /* A 101-switching-protocols must contains a Connection header with the |
| 1664 | * "upgrade" option and the request too. It means both are agree to |
| 1665 | * upgrade. It is not so strict because there is no test on the Upgrade |
| 1666 | * header content. But it is probably stronger enough for now. |
| 1667 | */ |
| 1668 | if (txn->status == 101 && |
| 1669 | (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG))) |
| 1670 | goto return_bad_res; |
| 1671 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1672 | /* |
| 1673 | * 2: check for cacheability. |
| 1674 | */ |
| 1675 | |
| 1676 | switch (txn->status) { |
| 1677 | case 200: |
| 1678 | case 203: |
| 1679 | case 204: |
| 1680 | case 206: |
| 1681 | case 300: |
| 1682 | case 301: |
| 1683 | case 404: |
| 1684 | case 405: |
| 1685 | case 410: |
| 1686 | case 414: |
| 1687 | case 501: |
| 1688 | break; |
| 1689 | default: |
| 1690 | /* RFC7231#6.1: |
| 1691 | * Responses with status codes that are defined as |
| 1692 | * cacheable by default (e.g., 200, 203, 204, 206, |
| 1693 | * 300, 301, 404, 405, 410, 414, and 501 in this |
| 1694 | * specification) can be reused by a cache with |
| 1695 | * heuristic expiration unless otherwise indicated |
| 1696 | * by the method definition or explicit cache |
| 1697 | * controls [RFC7234]; all other status codes are |
| 1698 | * not cacheable by default. |
| 1699 | */ |
| 1700 | txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK); |
| 1701 | break; |
| 1702 | } |
| 1703 | |
| 1704 | /* |
| 1705 | * 3: we may need to capture headers |
| 1706 | */ |
| 1707 | s->logs.logwait &= ~LW_RESP; |
| 1708 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1709 | http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1710 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1711 | /* Skip parsing if no content length is possible. */ |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 1712 | 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] | 1713 | txn->status == 101)) { |
| 1714 | /* Either we've established an explicit tunnel, or we're |
| 1715 | * switching the protocol. In both cases, we're very unlikely |
| 1716 | * to understand the next protocols. We have to switch to tunnel |
| 1717 | * mode, so that we transfer the request and responses then let |
| 1718 | * this protocol pass unmodified. When we later implement specific |
| 1719 | * parsers for such protocols, we'll want to check the Upgrade |
| 1720 | * header which contains information about that protocol for |
| 1721 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 1722 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 1723 | txn->flags |= TX_CON_WANT_TUN; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1724 | } |
| 1725 | |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1726 | /* check for NTML authentication headers in 401 (WWW-Authenticate) and |
| 1727 | * 407 (Proxy-Authenticate) responses and set the connection to private |
| 1728 | */ |
| 1729 | srv_conn = cs_conn(objt_cs(s->si[1].end)); |
| 1730 | if (srv_conn) { |
| 1731 | struct ist hdr; |
| 1732 | struct http_hdr_ctx ctx; |
| 1733 | |
| 1734 | if (txn->status == 401) |
| 1735 | hdr = ist("WWW-Authenticate"); |
| 1736 | else if (txn->status == 407) |
| 1737 | hdr = ist("Proxy-Authenticate"); |
| 1738 | else |
| 1739 | goto end; |
| 1740 | |
| 1741 | ctx.blk = NULL; |
| 1742 | while (http_find_header(htx, hdr, &ctx, 0)) { |
Willy Tarreau | f1dcced | 2020-05-07 19:27:02 +0200 | [diff] [blame] | 1743 | /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM", |
| 1744 | * possibly followed by blanks and a base64 string, the connection |
| 1745 | * is private. Since it's a mess to deal with, we only check for |
| 1746 | * values starting with "NTLM" or "Nego". Note that often multiple |
| 1747 | * headers are sent by the server there. |
| 1748 | */ |
| 1749 | if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) || |
Willy Tarreau | 49a1d28 | 2020-05-07 19:10:15 +0200 | [diff] [blame] | 1750 | (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) { |
Olivier Houchard | 250031e | 2019-05-29 15:01:50 +0200 | [diff] [blame] | 1751 | sess->flags |= SESS_FL_PREFER_LAST; |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 1752 | conn_set_owner(srv_conn, sess, NULL); |
Christopher Faulet | 21ddc74 | 2020-07-01 15:26:14 +0200 | [diff] [blame] | 1753 | conn_set_private(srv_conn); |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 1754 | /* 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] | 1755 | session_add_conn(srv_conn->owner, srv_conn, srv_conn->target); |
Willy Tarreau | f1dcced | 2020-05-07 19:27:02 +0200 | [diff] [blame] | 1756 | break; |
Olivier Houchard | 250031e | 2019-05-29 15:01:50 +0200 | [diff] [blame] | 1757 | } |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | end: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1762 | /* we want to have the response time before we start processing it */ |
| 1763 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 1764 | |
| 1765 | /* end of job, return OK */ |
| 1766 | rep->analysers &= ~an_bit; |
| 1767 | rep->analyse_exp = TICK_ETERNITY; |
| 1768 | channel_auto_close(rep); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1769 | 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] | 1770 | return 1; |
| 1771 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1772 | return_int_err: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1773 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 1774 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1775 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1776 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1777 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1778 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1779 | txn->status = 500; |
| 1780 | if (!(s->flags & SF_ERR_MASK)) |
| 1781 | s->flags |= SF_ERR_INTERNAL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1782 | goto return_prx_cond; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1783 | |
| 1784 | return_bad_res: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1785 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1786 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1787 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1788 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1789 | } |
Olivier Houchard | e3249a9 | 2019-05-03 23:01:47 +0200 | [diff] [blame] | 1790 | if ((s->be->retry_type & PR_RE_JUNK_REQUEST) && |
Olivier Houchard | ad26d8d | 2019-05-10 17:48:28 +0200 | [diff] [blame] | 1791 | (si_b->flags & SI_FL_L7_RETRY) && |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1792 | do_l7_retry(s, si_b) == 0) { |
| 1793 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1794 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | e3249a9 | 2019-05-03 23:01:47 +0200 | [diff] [blame] | 1795 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1796 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1797 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1798 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1799 | /* fall through */ |
| 1800 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1801 | return_prx_cond: |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1802 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1803 | |
| 1804 | if (!(s->flags & SF_ERR_MASK)) |
| 1805 | s->flags |= SF_ERR_PRXCOND; |
| 1806 | if (!(s->flags & SF_FINST_MASK)) |
| 1807 | s->flags |= SF_FINST_H; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1808 | |
| 1809 | s->si[1].flags |= SI_FL_NOLINGER; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1810 | DBG_TRACE_DEVEL("leaving on error", |
| 1811 | 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] | 1812 | return 0; |
| 1813 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1814 | abort_keep_alive: |
| 1815 | /* A keep-alive request to the server failed on a network error. |
| 1816 | * The client is required to retry. We need to close without returning |
| 1817 | * any other information so that the client retries. |
| 1818 | */ |
| 1819 | txn->status = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1820 | s->logs.logwait = 0; |
| 1821 | s->logs.level = 0; |
| 1822 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1823 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1824 | DBG_TRACE_DEVEL("leaving by closing K/A connection", |
| 1825 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1826 | return 0; |
| 1827 | } |
| 1828 | |
| 1829 | /* This function performs all the processing enabled for the current response. |
| 1830 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 1831 | * and updates s->res.analysers. It might make sense to explode it into several |
| 1832 | * other functions. It works like process_request (see indications above). |
| 1833 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1834 | 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] | 1835 | { |
| 1836 | struct session *sess = s->sess; |
| 1837 | struct http_txn *txn = s->txn; |
| 1838 | struct http_msg *msg = &txn->rsp; |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1839 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1840 | struct proxy *cur_proxy; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1841 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 1842 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1843 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 1844 | return 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1845 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1846 | 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] | 1847 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1848 | htx = htxbuf(&rep->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1849 | |
| 1850 | /* The stats applet needs to adjust the Connection header but we don't |
| 1851 | * apply any filter there. |
| 1852 | */ |
| 1853 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 1854 | rep->analysers &= ~an_bit; |
| 1855 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1856 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | /* |
| 1860 | * We will have to evaluate the filters. |
| 1861 | * As opposed to version 1.2, now they will be evaluated in the |
| 1862 | * filters order and not in the header order. This means that |
| 1863 | * each filter has to be validated among all headers. |
| 1864 | * |
| 1865 | * Filters are tried with ->be first, then with ->fe if it is |
| 1866 | * different from ->be. |
| 1867 | * |
| 1868 | * Maybe we are in resume condiion. In this case I choose the |
| 1869 | * "struct proxy" which contains the rule list matching the resume |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1870 | * pointer. If none of these "struct proxy" match, I initialise |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1871 | * the process with the first one. |
| 1872 | * |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1873 | * In fact, I check only correspondence between the current list |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1874 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 1875 | * the loop with the ->be. |
| 1876 | */ |
| 1877 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 1878 | cur_proxy = sess->fe; |
| 1879 | else |
| 1880 | cur_proxy = s->be; |
| 1881 | while (1) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1882 | /* evaluate http-response rules */ |
Christopher Faulet | b4c4a97 | 2021-11-09 16:33:25 +0100 | [diff] [blame] | 1883 | if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1884 | ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1885 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1886 | switch (ret) { |
| 1887 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 1888 | goto return_prx_yield; |
| 1889 | |
| 1890 | case HTTP_RULE_RES_CONT: |
| 1891 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 1892 | break; |
| 1893 | |
| 1894 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 1895 | goto deny; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1896 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1897 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent */ |
| 1898 | goto return_prx_cond; |
| 1899 | |
| 1900 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1901 | goto done; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1902 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1903 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 1904 | goto return_bad_res; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1905 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1906 | case HTTP_RULE_RES_ERROR: /* failed with a bad request */ |
| 1907 | goto return_int_err; |
| 1908 | } |
| 1909 | |
| 1910 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1911 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1912 | /* check whether we're already working on the frontend */ |
| 1913 | if (cur_proxy == sess->fe) |
| 1914 | break; |
| 1915 | cur_proxy = sess->fe; |
| 1916 | } |
| 1917 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1918 | /* OK that's all we can do for 1xx responses */ |
| 1919 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1920 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1921 | |
| 1922 | /* |
| 1923 | * Now check for a server cookie. |
| 1924 | */ |
| 1925 | 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] | 1926 | http_manage_server_side_cookies(s, rep); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1927 | |
| 1928 | /* |
| 1929 | * Check for cache-control or pragma headers if required. |
| 1930 | */ |
| 1931 | 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] | 1932 | http_check_response_for_cacheability(s, rep); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1933 | |
| 1934 | /* |
| 1935 | * Add server cookie in the response if needed |
| 1936 | */ |
| 1937 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 1938 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
| 1939 | (!(s->flags & SF_DIRECT) || |
| 1940 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 1941 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 1942 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 1943 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 1944 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 1945 | !(s->flags & SF_IGNORE_PRST)) { |
| 1946 | /* the server is known, it's not the one the client requested, or the |
| 1947 | * cookie's last seen date needs to be refreshed. We have to |
| 1948 | * insert a set-cookie here, except if we want to insert only on POST |
| 1949 | * requests and this one isn't. Note that servers which don't have cookies |
| 1950 | * (eg: some backup servers) will return a full cookie removal request. |
| 1951 | */ |
Willy Tarreau | 6618259 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1952 | if (!__objt_server(s->target)->cookie) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1953 | chunk_printf(&trash, |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1954 | "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1955 | s->be->cookie_name); |
| 1956 | } |
| 1957 | else { |
Willy Tarreau | 6618259 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1958 | 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] | 1959 | |
| 1960 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 1961 | /* emit last_date, which is mandatory */ |
| 1962 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 1963 | s30tob64((date.tv_sec+3) >> 2, |
| 1964 | trash.area + trash.data); |
| 1965 | trash.data += 5; |
| 1966 | |
| 1967 | if (s->be->cookie_maxlife) { |
| 1968 | /* emit first_date, which is either the original one or |
| 1969 | * the current date. |
| 1970 | */ |
| 1971 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 1972 | s30tob64(txn->cookie_first_date ? |
| 1973 | txn->cookie_first_date >> 2 : |
| 1974 | (date.tv_sec+3) >> 2, |
| 1975 | trash.area + trash.data); |
| 1976 | trash.data += 5; |
| 1977 | } |
| 1978 | } |
| 1979 | chunk_appendf(&trash, "; path=/"); |
| 1980 | } |
| 1981 | |
| 1982 | if (s->be->cookie_domain) |
| 1983 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
| 1984 | |
| 1985 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 1986 | chunk_appendf(&trash, "; HttpOnly"); |
| 1987 | |
| 1988 | if (s->be->ck_opts & PR_CK_SECURE) |
| 1989 | chunk_appendf(&trash, "; Secure"); |
| 1990 | |
Christopher Faulet | 2f53390 | 2020-01-21 11:06:48 +0100 | [diff] [blame] | 1991 | if (s->be->cookie_attrs) |
| 1992 | chunk_appendf(&trash, "; %s", s->be->cookie_attrs); |
| 1993 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1994 | if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data)))) |
Christopher Faulet | 028c164 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 1995 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1996 | |
| 1997 | txn->flags &= ~TX_SCK_MASK; |
| 1998 | if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
| 1999 | /* the server did not change, only the date was updated */ |
| 2000 | txn->flags |= TX_SCK_UPDATED; |
| 2001 | else |
| 2002 | txn->flags |= TX_SCK_INSERTED; |
| 2003 | |
| 2004 | /* Here, we will tell an eventual cache on the client side that we don't |
| 2005 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 2006 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 2007 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 2008 | */ |
| 2009 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
| 2010 | |
| 2011 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 2012 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2013 | if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private")))) |
Christopher Faulet | 028c164 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 2014 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | /* |
| 2019 | * Check if result will be cacheable with a cookie. |
| 2020 | * We'll block the response if security checks have caught |
| 2021 | * nasty things such as a cacheable cookie. |
| 2022 | */ |
| 2023 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 2024 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 2025 | (s->be->options & PR_O_CHK_CACHE)) { |
| 2026 | /* we're in presence of a cacheable response containing |
| 2027 | * a set-cookie header. We'll block it as requested by |
| 2028 | * the 'checkcache' option, and send an alert. |
| 2029 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2030 | ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 6618259 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 2031 | 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] | 2032 | send_log(s->be, LOG_ALERT, |
| 2033 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 6618259 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 2034 | 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] | 2035 | goto deny; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2036 | } |
| 2037 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2038 | end: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2039 | /* |
| 2040 | * Evaluate after-response rules before forwarding the response. rules |
| 2041 | * from the backend are evaluated first, then one from the frontend if |
| 2042 | * it differs. |
| 2043 | */ |
| 2044 | if (!http_eval_after_res_rules(s)) |
| 2045 | goto return_int_err; |
| 2046 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 2047 | /* Filter the response headers if there are filters attached to the |
| 2048 | * stream. |
| 2049 | */ |
| 2050 | if (HAS_FILTERS(s)) |
| 2051 | rep->analysers |= AN_RES_FLT_HTTP_HDRS; |
| 2052 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2053 | /* Always enter in the body analyzer */ |
| 2054 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 2055 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
| 2056 | |
| 2057 | /* if the user wants to log as soon as possible, without counting |
| 2058 | * bytes from the server, then this is the right moment. We have |
| 2059 | * to temporarily assign bytes_out to log what we currently have. |
| 2060 | */ |
| 2061 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
| 2062 | 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] | 2063 | s->logs.bytes_out = htx->data; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2064 | s->do_log(s); |
| 2065 | s->logs.bytes_out = 0; |
| 2066 | } |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2067 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2068 | done: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2069 | 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] | 2070 | rep->analysers &= ~an_bit; |
| 2071 | rep->analyse_exp = TICK_ETERNITY; |
| 2072 | return 1; |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2073 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2074 | deny: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2075 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp); |
| 2076 | _HA_ATOMIC_INC(&s->be->be_counters.denied_resp); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2077 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2078 | _HA_ATOMIC_INC(&sess->listener->counters->denied_resp); |
Christopher Faulet | a08546b | 2019-12-16 16:07:34 +0100 | [diff] [blame] | 2079 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2080 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2081 | goto return_prx_err; |
| 2082 | |
Christopher Faulet | 028c164 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 2083 | return_fail_rewrite: |
| 2084 | if (!(s->flags & SF_ERR_MASK)) |
| 2085 | s->flags |= SF_ERR_PRXCOND; |
| 2086 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
| 2087 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
| 2088 | if (sess->listener && sess->listener->counters) |
| 2089 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
| 2090 | if (objt_server(s->target)) |
| 2091 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
| 2092 | /* fall through */ |
| 2093 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2094 | return_int_err: |
| 2095 | txn->status = 500; |
| 2096 | if (!(s->flags & SF_ERR_MASK)) |
| 2097 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2098 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 2099 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
Dragan Dosen | 84426cd | 2021-09-21 13:02:09 +0200 | [diff] [blame] | 2100 | if (sess->listener && sess->listener->counters) |
| 2101 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 2102 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2103 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2104 | goto return_prx_err; |
| 2105 | |
| 2106 | return_bad_res: |
| 2107 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2108 | stream_inc_http_fail_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2109 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | a20a653 | 2020-02-05 10:16:41 +0100 | [diff] [blame] | 2110 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2111 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | a20a653 | 2020-02-05 10:16:41 +0100 | [diff] [blame] | 2112 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 2113 | } |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2114 | /* fall through */ |
| 2115 | |
| 2116 | return_prx_err: |
| 2117 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 2118 | /* fall through */ |
| 2119 | |
| 2120 | return_prx_cond: |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2121 | s->logs.t_data = -1; /* was not a valid response */ |
| 2122 | s->si[1].flags |= SI_FL_NOLINGER; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2123 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2124 | if (!(s->flags & SF_ERR_MASK)) |
| 2125 | s->flags |= SF_ERR_PRXCOND; |
| 2126 | if (!(s->flags & SF_FINST_MASK)) |
| 2127 | s->flags |= SF_FINST_H; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2128 | |
Christopher Faulet | e58c000 | 2020-03-02 16:21:01 +0100 | [diff] [blame] | 2129 | rep->analysers &= AN_RES_FLT_END; |
| 2130 | s->req.analysers &= AN_REQ_FLT_END; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2131 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2132 | DBG_TRACE_DEVEL("leaving on error", |
| 2133 | 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] | 2134 | return 0; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2135 | |
| 2136 | return_prx_yield: |
| 2137 | channel_dont_close(rep); |
| 2138 | DBG_TRACE_DEVEL("waiting for more data", |
| 2139 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 2140 | return 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2141 | } |
| 2142 | |
| 2143 | /* This function is an analyser which forwards response body (including chunk |
| 2144 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 2145 | * zero byte. The only situation where it must not be called is when we're in |
| 2146 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 2147 | * remaining data and to resync after end of body. It expects the msg_state to |
| 2148 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 2149 | * read more data, or 1 once we can go on with next request or end the stream. |
| 2150 | * |
| 2151 | * It is capable of compressing response data both in content-length mode and |
| 2152 | * in chunked mode. The state machines follows different flows depending on |
| 2153 | * whether content-length and chunked modes are used, since there are no |
| 2154 | * trailers in content-length : |
| 2155 | * |
| 2156 | * chk-mode cl-mode |
| 2157 | * ,----- BODY -----. |
| 2158 | * / \ |
| 2159 | * V size > 0 V chk-mode |
| 2160 | * .--> SIZE -------------> DATA -------------> CRLF |
| 2161 | * | | size == 0 | last byte | |
| 2162 | * | v final crlf v inspected | |
| 2163 | * | TRAILERS -----------> DONE | |
| 2164 | * | | |
| 2165 | * `----------------------------------------------' |
| 2166 | * |
| 2167 | * Compression only happens in the DATA state, and must be flushed in final |
| 2168 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 2169 | * is performed at once on final states for all bytes parsed, or when leaving |
| 2170 | * on missing data. |
| 2171 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2172 | 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] | 2173 | { |
| 2174 | struct session *sess = s->sess; |
| 2175 | struct http_txn *txn = s->txn; |
| 2176 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2177 | struct htx *htx; |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2178 | int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2179 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2180 | 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] | 2181 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2182 | htx = htxbuf(&res->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2183 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2184 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 2185 | goto return_bad_res; |
| 2186 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 2187 | goto return_int_err; |
| 2188 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2189 | if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2190 | ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2191 | /* Output closed while we were sending data. We must abort and |
| 2192 | * wake the other side up. |
| 2193 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2194 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2195 | http_end_response(s); |
| 2196 | http_end_request(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2197 | DBG_TRACE_DEVEL("leaving on error", |
| 2198 | 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] | 2199 | return 1; |
| 2200 | } |
| 2201 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2202 | if (msg->msg_state == HTTP_MSG_BODY) |
| 2203 | msg->msg_state = HTTP_MSG_DATA; |
| 2204 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2205 | /* in most states, we should abort in case of early close */ |
| 2206 | channel_auto_close(res); |
| 2207 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2208 | if (res->to_forward) { |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2209 | if (res->to_forward == CHN_INFINITE_FORWARD) { |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2210 | if (res->flags & CF_EOI) |
| 2211 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2212 | } |
| 2213 | else { |
| 2214 | /* We can't process the buffer's contents yet */ |
| 2215 | res->flags |= CF_WAKE_WRITE; |
| 2216 | goto missing_data_or_waiting; |
| 2217 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2218 | } |
| 2219 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2220 | if (msg->msg_state >= HTTP_MSG_ENDING) |
| 2221 | goto ending; |
| 2222 | |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 2223 | 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] | 2224 | (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) { |
| 2225 | msg->msg_state = HTTP_MSG_ENDING; |
| 2226 | goto ending; |
| 2227 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2228 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2229 | /* Forward input data. We get it by removing all outgoing data not |
| 2230 | * forwarded yet from HTX data size. If there are some data filters, we |
| 2231 | * let them decide the amount of data to forward. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2232 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2233 | if (HAS_RSP_DATA_FILTERS(s)) { |
| 2234 | ret = flt_http_payload(s, msg, htx->data); |
| 2235 | if (ret < 0) |
| 2236 | goto return_bad_res; |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 2237 | c_adv(res, ret); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2238 | } |
| 2239 | else { |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 2240 | c_adv(res, htx->data - co_data(res)); |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2241 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
| 2242 | channel_htx_forward_forever(res, htx); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2243 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2244 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2245 | if (htx->data != co_data(res)) |
| 2246 | goto missing_data_or_waiting; |
| 2247 | |
| 2248 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) { |
| 2249 | msg->msg_state = HTTP_MSG_ENDING; |
| 2250 | goto ending; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2251 | } |
| 2252 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2253 | /* 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] | 2254 | * in HTTP_MSG_ENDING state. Then if all data was marked to be |
| 2255 | * forwarded, set the state to HTTP_MSG_DONE. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2256 | */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2257 | if (!(htx->flags & HTX_FL_EOM)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2258 | goto missing_data_or_waiting; |
| 2259 | |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 2260 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2261 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2262 | ending: |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 2263 | res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */ |
| 2264 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2265 | /* other states, ENDING...TUNNEL */ |
| 2266 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 2267 | goto done; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2268 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2269 | if (HAS_RSP_DATA_FILTERS(s)) { |
| 2270 | ret = flt_http_end(s, msg); |
| 2271 | if (ret <= 0) { |
| 2272 | if (!ret) |
| 2273 | goto missing_data_or_waiting; |
| 2274 | goto return_bad_res; |
| 2275 | } |
| 2276 | } |
| 2277 | |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 2278 | 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] | 2279 | !(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 2280 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 2281 | goto ending; |
| 2282 | } |
| 2283 | else { |
| 2284 | msg->msg_state = HTTP_MSG_DONE; |
| 2285 | res->to_forward = 0; |
| 2286 | } |
| 2287 | |
| 2288 | done: |
| 2289 | |
| 2290 | channel_dont_close(res); |
| 2291 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2292 | http_end_response(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2293 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2294 | http_end_request(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2295 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2296 | if (res->flags & CF_SHUTW) { |
| 2297 | /* response errors are most likely due to the |
| 2298 | * client aborting the transfer. */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2299 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2300 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2301 | goto return_bad_res; |
| 2302 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2303 | 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] | 2304 | return 1; |
| 2305 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2306 | DBG_TRACE_DEVEL("waiting for the end of the HTTP txn", |
| 2307 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2308 | return 0; |
| 2309 | |
| 2310 | missing_data_or_waiting: |
| 2311 | if (res->flags & CF_SHUTW) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2312 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2313 | |
| 2314 | /* stop waiting for data if the input is closed before the end. If the |
| 2315 | * client side was already closed, it means that the client has aborted, |
| 2316 | * so we don't want to count this as a server abort. Otherwise it's a |
| 2317 | * server abort. |
| 2318 | */ |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 2319 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2320 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2321 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2322 | /* If we have some pending data, we continue the processing */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2323 | if (htx_is_empty(htx)) |
| 2324 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2325 | } |
| 2326 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2327 | /* When TE: chunked is used, we need to get there again to parse |
| 2328 | * 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] | 2329 | * set CF_DONTCLOSE. Similarly when there is a content-leng or if there |
| 2330 | * are filters registered on the stream, we don't want to forward a |
| 2331 | * close |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2332 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2333 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2334 | channel_dont_close(res); |
| 2335 | |
| 2336 | /* We know that more data are expected, but we couldn't send more that |
| 2337 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
| 2338 | * system knows it must not set a PUSH on this first part. Interactive |
| 2339 | * modes are already handled by the stream sock layer. We must not do |
| 2340 | * this in content-length mode because it could present the MSG_MORE |
| 2341 | * flag with the last block of forwarded data, which would cause an |
| 2342 | * additional delay to be observed by the receiver. |
| 2343 | */ |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 2344 | if (HAS_RSP_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2345 | res->flags |= CF_EXPECT_MORE; |
| 2346 | |
| 2347 | /* the stream handler will take care of timeouts and errors */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2348 | DBG_TRACE_DEVEL("waiting for more data to forward", |
| 2349 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2350 | return 0; |
| 2351 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2352 | return_srv_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2353 | _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts); |
| 2354 | _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2355 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2356 | _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2357 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2358 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts); |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2359 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2360 | if (!(s->flags & SF_ERR_MASK)) |
| 2361 | s->flags |= SF_ERR_SRVCL; |
| 2362 | goto return_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2363 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2364 | return_cli_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2365 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 2366 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2367 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2368 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2369 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2370 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2371 | if (!(s->flags & SF_ERR_MASK)) |
| 2372 | s->flags |= SF_ERR_CLICL; |
| 2373 | goto return_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2374 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2375 | return_int_err: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2376 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 2377 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2378 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2379 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2380 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2381 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2382 | if (!(s->flags & SF_ERR_MASK)) |
| 2383 | s->flags |= SF_ERR_INTERNAL; |
| 2384 | goto return_error; |
| 2385 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2386 | return_bad_res: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2387 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2388 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2389 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2390 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 2391 | } |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2392 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2393 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2394 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2395 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2396 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2397 | return_error: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2398 | /* don't send any error message as we're in the body */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2399 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2400 | if (!(s->flags & SF_FINST_MASK)) |
| 2401 | s->flags |= SF_FINST_D; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2402 | DBG_TRACE_DEVEL("leaving on error", |
| 2403 | 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] | 2404 | return 0; |
| 2405 | } |
| 2406 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2407 | /* Perform an HTTP redirect based on the information in <rule>. The function |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2408 | * returns zero on success, or zero in case of a, irrecoverable error such |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2409 | * as too large a request to build a valid response. |
| 2410 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2411 | 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] | 2412 | { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2413 | struct channel *req = &s->req; |
| 2414 | struct channel *res = &s->res; |
| 2415 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2416 | struct htx_sl *sl; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2417 | struct buffer *chunk; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2418 | struct ist status, reason, location; |
| 2419 | unsigned int flags; |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2420 | int close = 0; /* Try to keep the connection alive byt default */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2421 | |
| 2422 | chunk = alloc_trash_chunk(); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2423 | if (!chunk) { |
| 2424 | if (!(s->flags & SF_ERR_MASK)) |
| 2425 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2426 | goto fail; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2427 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2428 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2429 | /* |
| 2430 | * Create the location |
| 2431 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2432 | htx = htxbuf(&req->buf); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2433 | switch(rule->type) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2434 | case REDIRECT_TYPE_SCHEME: { |
| 2435 | struct http_hdr_ctx ctx; |
| 2436 | struct ist path, host; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2437 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2438 | host = ist(""); |
| 2439 | ctx.blk = NULL; |
| 2440 | if (http_find_header(htx, ist("Host"), &ctx, 0)) |
| 2441 | host = ctx.value; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2442 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2443 | sl = http_get_stline(htx); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2444 | path = http_get_path(htx_sl_req_uri(sl)); |
| 2445 | /* build message using path */ |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 2446 | if (isttest(path)) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2447 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2448 | int qs = 0; |
| 2449 | while (qs < path.len) { |
| 2450 | if (*(path.ptr + qs) == '?') { |
| 2451 | path.len = qs; |
| 2452 | break; |
| 2453 | } |
| 2454 | qs++; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2455 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2456 | } |
| 2457 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2458 | else |
| 2459 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2460 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2461 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2462 | /* add scheme */ |
| 2463 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2464 | goto fail; |
| 2465 | } |
| 2466 | else { |
| 2467 | /* add scheme with executing log format */ |
| 2468 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2469 | chunk->size - chunk->data, |
| 2470 | &rule->rdr_fmt); |
| 2471 | } |
| 2472 | /* add "://" + host + path */ |
| 2473 | if (!chunk_memcat(chunk, "://", 3) || |
| 2474 | !chunk_memcat(chunk, host.ptr, host.len) || |
| 2475 | !chunk_memcat(chunk, path.ptr, path.len)) |
| 2476 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2477 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2478 | /* append a slash at the end of the location if needed and missing */ |
| 2479 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2480 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2481 | if (chunk->data + 1 >= chunk->size) |
| 2482 | goto fail; |
| 2483 | chunk->area[chunk->data++] = '/'; |
| 2484 | } |
| 2485 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2486 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2487 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2488 | case REDIRECT_TYPE_PREFIX: { |
| 2489 | struct ist path; |
| 2490 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2491 | sl = http_get_stline(htx); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2492 | path = http_get_path(htx_sl_req_uri(sl)); |
| 2493 | /* build message using path */ |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 2494 | if (isttest(path)) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2495 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2496 | int qs = 0; |
| 2497 | while (qs < path.len) { |
| 2498 | if (*(path.ptr + qs) == '?') { |
| 2499 | path.len = qs; |
| 2500 | break; |
| 2501 | } |
| 2502 | qs++; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2503 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2504 | } |
| 2505 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2506 | else |
| 2507 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2508 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2509 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2510 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2511 | * add anything, otherwise it makes it hard for the user to |
| 2512 | * configure a self-redirection. |
| 2513 | */ |
| 2514 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 2515 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2516 | goto fail; |
| 2517 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2518 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2519 | else { |
| 2520 | /* add prefix with executing log format */ |
| 2521 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2522 | chunk->size - chunk->data, |
| 2523 | &rule->rdr_fmt); |
| 2524 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2525 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2526 | /* add path */ |
| 2527 | if (!chunk_memcat(chunk, path.ptr, path.len)) |
| 2528 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2529 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2530 | /* append a slash at the end of the location if needed and missing */ |
| 2531 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2532 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2533 | if (chunk->data + 1 >= chunk->size) |
| 2534 | goto fail; |
| 2535 | chunk->area[chunk->data++] = '/'; |
| 2536 | } |
| 2537 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2538 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2539 | case REDIRECT_TYPE_LOCATION: |
| 2540 | default: |
| 2541 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2542 | /* add location */ |
| 2543 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2544 | goto fail; |
| 2545 | } |
| 2546 | else { |
| 2547 | /* add location with executing log format */ |
| 2548 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2549 | chunk->size - chunk->data, |
| 2550 | &rule->rdr_fmt); |
| 2551 | } |
| 2552 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2553 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2554 | location = ist2(chunk->area, chunk->data); |
| 2555 | |
| 2556 | /* |
| 2557 | * Create the 30x response |
| 2558 | */ |
| 2559 | switch (rule->code) { |
| 2560 | case 308: |
| 2561 | status = ist("308"); |
| 2562 | reason = ist("Permanent Redirect"); |
| 2563 | break; |
| 2564 | case 307: |
| 2565 | status = ist("307"); |
| 2566 | reason = ist("Temporary Redirect"); |
| 2567 | break; |
| 2568 | case 303: |
| 2569 | status = ist("303"); |
| 2570 | reason = ist("See Other"); |
| 2571 | break; |
| 2572 | case 301: |
| 2573 | status = ist("301"); |
| 2574 | reason = ist("Moved Permanently"); |
| 2575 | break; |
| 2576 | case 302: |
| 2577 | default: |
| 2578 | status = ist("302"); |
| 2579 | reason = ist("Found"); |
| 2580 | break; |
| 2581 | } |
| 2582 | |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2583 | if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE) |
| 2584 | close = 1; |
| 2585 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2586 | htx = htx_from_buf(&res->buf); |
Kevin Zhu | 96b3639 | 2020-01-07 09:42:55 +0100 | [diff] [blame] | 2587 | /* Trim any possible response */ |
| 2588 | channel_htx_truncate(&s->res, htx); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2589 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 2590 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason); |
| 2591 | if (!sl) |
| 2592 | goto fail; |
| 2593 | sl->info.res.status = rule->code; |
| 2594 | s->txn->status = rule->code; |
| 2595 | |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2596 | if (close && !htx_add_header(htx, ist("Connection"), ist("close"))) |
| 2597 | goto fail; |
| 2598 | |
| 2599 | if (!htx_add_header(htx, ist("Content-length"), ist("0")) || |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2600 | !htx_add_header(htx, ist("Location"), location)) |
| 2601 | goto fail; |
| 2602 | |
| 2603 | if (rule->code == 302 || rule->code == 303 || rule->code == 307) { |
| 2604 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache"))) |
| 2605 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2606 | } |
| 2607 | |
| 2608 | if (rule->cookie_len) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2609 | if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len))) |
| 2610 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2611 | } |
| 2612 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2613 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2614 | goto fail; |
| 2615 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2616 | htx->flags |= HTX_FL_EOM; |
Kevin Zhu | 96b3639 | 2020-01-07 09:42:55 +0100 | [diff] [blame] | 2617 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 2618 | if (!http_forward_proxy_resp(s, 1)) |
| 2619 | goto fail; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2620 | |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2621 | if (rule->flags & REDIRECT_FLAG_FROM_REQ) { |
| 2622 | /* let's log the request time */ |
| 2623 | s->logs.tv_request = now; |
Christopher Faulet | 9132227 | 2021-10-04 14:16:46 +0200 | [diff] [blame] | 2624 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2625 | |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2626 | 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] | 2627 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2628 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2629 | |
| 2630 | if (!(s->flags & SF_ERR_MASK)) |
| 2631 | s->flags |= SF_ERR_LOCAL; |
| 2632 | if (!(s->flags & SF_FINST_MASK)) |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2633 | s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2634 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2635 | free_trash_chunk(chunk); |
| 2636 | return 1; |
| 2637 | |
| 2638 | fail: |
| 2639 | /* If an error occurred, remove the incomplete HTTP response from the |
| 2640 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 2641 | channel_htx_truncate(res, htxbuf(&res->buf)); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2642 | free_trash_chunk(chunk); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2643 | return 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2644 | } |
| 2645 | |
Christopher Faulet | 7950715 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2646 | /* This function filters the request header names to only allow [0-9a-zA-Z-] |
| 2647 | * characters. Depending on the proxy configuration, headers with a name not |
| 2648 | * matching this charset are removed or the request is rejected with a |
| 2649 | * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT |
| 2650 | * to continue the request processing or HTTP_RULE_RES_DENY if the request is |
| 2651 | * rejected. |
| 2652 | */ |
| 2653 | static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px) |
| 2654 | { |
| 2655 | struct htx_blk *blk; |
| 2656 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
| 2657 | |
| 2658 | blk = htx_get_first_blk(htx); |
| 2659 | while (blk) { |
| 2660 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 2661 | |
| 2662 | if (type == HTX_BLK_HDR) { |
| 2663 | struct ist n = htx_get_blk_name(htx, blk); |
Mateusz Malek | 4360b9a | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2664 | int i, end = istlen(n); |
Christopher Faulet | 7950715 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2665 | |
Mateusz Malek | 4360b9a | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2666 | for (i = 0; i < end; i++) { |
Christopher Faulet | 7950715 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2667 | if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') { |
Mateusz Malek | 4360b9a | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2668 | break; |
Christopher Faulet | 7950715 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2669 | } |
| 2670 | } |
Mateusz Malek | 4360b9a | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2671 | |
| 2672 | if (i < end) { |
| 2673 | /* Disallowed character found - block the request or remove the header */ |
| 2674 | if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK) |
| 2675 | goto block; |
| 2676 | blk = htx_remove_blk(htx, blk); |
| 2677 | continue; |
| 2678 | } |
Christopher Faulet | 7950715 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2679 | } |
| 2680 | if (type == HTX_BLK_EOH) |
| 2681 | break; |
| 2682 | |
| 2683 | blk = htx_get_next_blk(htx, blk); |
| 2684 | } |
| 2685 | out: |
| 2686 | return rule_ret; |
| 2687 | block: |
| 2688 | /* Block the request returning a 403-Forbidden response */ |
| 2689 | s->txn->status = 403; |
| 2690 | rule_ret = HTTP_RULE_RES_DENY; |
| 2691 | goto out; |
| 2692 | } |
| 2693 | |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2694 | /* Replace all headers matching the name <name>. The header value is replaced if |
| 2695 | * it matches the regex <re>. <str> is used for the replacement. If <full> is |
| 2696 | * set to 1, the full-line is matched and replaced. Otherwise, comma-separated |
| 2697 | * values are evaluated one by one. It returns 0 on success and -1 on error. |
| 2698 | */ |
| 2699 | int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name, |
| 2700 | const char *str, struct my_regex *re, int full) |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2701 | { |
| 2702 | struct http_hdr_ctx ctx; |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2703 | |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2704 | ctx.blk = NULL; |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2705 | while (http_find_header(htx, name, &ctx, full)) { |
Christopher Faulet | 90f7847 | 2023-08-04 16:51:11 +0200 | [diff] [blame] | 2706 | struct buffer *output = get_trash_chunk(); |
| 2707 | |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2708 | if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0)) |
| 2709 | continue; |
| 2710 | |
| 2711 | output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch); |
| 2712 | if (output->data == -1) |
| 2713 | return -1; |
| 2714 | if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data))) |
| 2715 | return -1; |
| 2716 | } |
| 2717 | return 0; |
| 2718 | } |
| 2719 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2720 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 2721 | * takes the string from the variable 'replace' with length 'len', then modifies |
| 2722 | * the relevant part of the request line accordingly. Then it updates various |
| 2723 | * pointers to the next elements which were moved, and the total buffer length. |
| 2724 | * It finds the action to be performed in p[2], previously filled by function |
| 2725 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 2726 | * error, though this can be revisited when this code is finally exploited. |
| 2727 | * |
| 2728 | * '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] | 2729 | * 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] | 2730 | * |
| 2731 | * In query string case, the mark question '?' must be set at the start of the |
| 2732 | * string by the caller, event if the replacement query string is empty. |
| 2733 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2734 | int http_req_replace_stline(int action, const char *replace, int len, |
| 2735 | struct proxy *px, struct stream *s) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2736 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2737 | struct htx *htx = htxbuf(&s->req.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2738 | |
| 2739 | switch (action) { |
| 2740 | case 0: // method |
| 2741 | if (!http_replace_req_meth(htx, ist2(replace, len))) |
| 2742 | return -1; |
| 2743 | break; |
| 2744 | |
| 2745 | case 1: // path |
Christopher Faulet | b8ce505 | 2020-08-31 16:11:57 +0200 | [diff] [blame] | 2746 | if (!http_replace_req_path(htx, ist2(replace, len), 0)) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2747 | return -1; |
| 2748 | break; |
| 2749 | |
| 2750 | case 2: // query |
| 2751 | if (!http_replace_req_query(htx, ist2(replace, len))) |
| 2752 | return -1; |
| 2753 | break; |
| 2754 | |
| 2755 | case 3: // uri |
| 2756 | if (!http_replace_req_uri(htx, ist2(replace, len))) |
| 2757 | return -1; |
| 2758 | break; |
| 2759 | |
Christopher Faulet | 312294f | 2020-09-02 17:17:44 +0200 | [diff] [blame] | 2760 | case 4: // path + query |
| 2761 | if (!http_replace_req_path(htx, ist2(replace, len), 1)) |
| 2762 | return -1; |
| 2763 | break; |
| 2764 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2765 | default: |
| 2766 | return -1; |
| 2767 | } |
| 2768 | return 0; |
| 2769 | } |
| 2770 | |
| 2771 | /* This function replace the HTTP status code and the associated message. The |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2772 | * variable <status> contains the new status code. This function never fails. It |
| 2773 | * returns 0 in case of success, -1 in case of internal error. |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2774 | */ |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2775 | 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] | 2776 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2777 | struct htx *htx = htxbuf(&s->res.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2778 | char *res; |
| 2779 | |
| 2780 | chunk_reset(&trash); |
| 2781 | res = ultoa_o(status, trash.area, trash.size); |
| 2782 | trash.data = res - trash.area; |
| 2783 | |
| 2784 | /* Do we have a custom reason format string? */ |
Tim Duesterhus | e296d3e | 2020-03-05 17:56:31 +0100 | [diff] [blame] | 2785 | if (!isttest(reason)) { |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2786 | const char *str = http_get_reason(status); |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 2787 | reason = ist(str); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2788 | } |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2789 | |
Christopher Faulet | bde2c4c | 2020-08-31 16:43:34 +0200 | [diff] [blame] | 2790 | if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason)) |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2791 | return -1; |
Willy Tarreau | bed299a | 2023-01-10 14:50:44 +0100 | [diff] [blame] | 2792 | s->txn->status = status; |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2793 | return 0; |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2794 | } |
| 2795 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2796 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
| 2797 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 2798 | * further processing of the request (auth, deny, ...), and defaults to |
| 2799 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 2800 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 2801 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 2802 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 2803 | * status. |
| 2804 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2805 | static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 2806 | struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2807 | { |
| 2808 | struct session *sess = strm_sess(s); |
| 2809 | struct http_txn *txn = s->txn; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2810 | struct act_rule *rule; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2811 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2812 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2813 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2814 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2815 | * resume condition. If a resume is needed it is always in the action |
| 2816 | * and never in the ACL or converters. In this case, we initialise the |
| 2817 | * current rule, and go to the action execution point. |
| 2818 | */ |
| 2819 | if (s->current_rule) { |
| 2820 | rule = s->current_rule; |
| 2821 | s->current_rule = NULL; |
| 2822 | if (s->current_rule_list == rules) |
| 2823 | goto resume_execution; |
| 2824 | } |
| 2825 | s->current_rule_list = rules; |
| 2826 | |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2827 | /* start the ruleset evaluation in strict mode */ |
| 2828 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2829 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2830 | list_for_each_entry(rule, rules, list) { |
| 2831 | /* check optional condition */ |
| 2832 | if (rule->cond) { |
| 2833 | int ret; |
| 2834 | |
| 2835 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 2836 | ret = acl_pass(ret); |
| 2837 | |
| 2838 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2839 | ret = !ret; |
| 2840 | |
| 2841 | if (!ret) /* condition not matched */ |
| 2842 | continue; |
| 2843 | } |
| 2844 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2845 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2846 | resume_execution: |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 2847 | if (rule->kw->flags & KWF_EXPERIMENTAL) |
| 2848 | mark_tainted(TAINTED_ACTION_EXP_EXECUTED); |
| 2849 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2850 | /* Always call the action function if defined */ |
| 2851 | if (rule->action_ptr) { |
| 2852 | if ((s->req.flags & CF_READ_ERROR) || |
| 2853 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 2854 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2855 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2856 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2857 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2858 | case ACT_RET_CONT: |
| 2859 | break; |
| 2860 | case ACT_RET_STOP: |
| 2861 | rule_ret = HTTP_RULE_RES_STOP; |
| 2862 | goto end; |
| 2863 | case ACT_RET_YIELD: |
| 2864 | s->current_rule = rule; |
| 2865 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2866 | goto end; |
| 2867 | case ACT_RET_ERR: |
| 2868 | rule_ret = HTTP_RULE_RES_ERROR; |
| 2869 | goto end; |
| 2870 | case ACT_RET_DONE: |
| 2871 | rule_ret = HTTP_RULE_RES_DONE; |
| 2872 | goto end; |
| 2873 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 2874 | if (txn->status == -1) |
| 2875 | txn->status = 403; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2876 | rule_ret = HTTP_RULE_RES_DENY; |
| 2877 | goto end; |
| 2878 | case ACT_RET_ABRT: |
| 2879 | rule_ret = HTTP_RULE_RES_ABRT; |
| 2880 | goto end; |
| 2881 | case ACT_RET_INV: |
| 2882 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2883 | goto end; |
| 2884 | } |
| 2885 | continue; /* eval the next rule */ |
| 2886 | } |
| 2887 | |
| 2888 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2889 | switch (rule->action) { |
| 2890 | case ACT_ACTION_ALLOW: |
| 2891 | rule_ret = HTTP_RULE_RES_STOP; |
| 2892 | goto end; |
| 2893 | |
| 2894 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2895 | txn->status = rule->arg.http_reply->status; |
| 2896 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2897 | rule_ret = HTTP_RULE_RES_DENY; |
| 2898 | goto end; |
| 2899 | |
| 2900 | case ACT_HTTP_REQ_TARPIT: |
| 2901 | txn->flags |= TX_CLTARPIT; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2902 | txn->status = rule->arg.http_reply->status; |
| 2903 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2904 | rule_ret = HTTP_RULE_RES_DENY; |
| 2905 | goto end; |
| 2906 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2907 | case ACT_HTTP_REDIR: |
Christopher Faulet | 90d22a8 | 2020-03-06 11:18:39 +0100 | [diff] [blame] | 2908 | rule_ret = HTTP_RULE_RES_ABRT; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2909 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 2910 | rule_ret = HTTP_RULE_RES_ERROR; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2911 | goto end; |
| 2912 | |
| 2913 | case ACT_HTTP_SET_NICE: |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2914 | s->task->nice = rule->arg.http.i; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2915 | break; |
| 2916 | |
| 2917 | case ACT_HTTP_SET_TOS: |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2918 | conn_set_tos(objt_conn(sess->origin), rule->arg.http.i); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2919 | break; |
| 2920 | |
| 2921 | case ACT_HTTP_SET_MARK: |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2922 | conn_set_mark(objt_conn(sess->origin), rule->arg.http.i); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2923 | break; |
| 2924 | |
| 2925 | case ACT_HTTP_SET_LOGL: |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2926 | s->logs.level = rule->arg.http.i; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2927 | break; |
| 2928 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2929 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2930 | default: |
| 2931 | break; |
| 2932 | } |
| 2933 | } |
| 2934 | |
| 2935 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2936 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2937 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2938 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2939 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2940 | /* we reached the end of the rules, nothing to report */ |
| 2941 | return rule_ret; |
| 2942 | } |
| 2943 | |
| 2944 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 2945 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 2946 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 2947 | * is returned, the process can continue the evaluation of next rule list. If |
| 2948 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 2949 | * 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] | 2950 | * must be returned. If *YIELD is returned, the caller must call again the |
| 2951 | * function with the same context. |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2952 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2953 | static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, |
| 2954 | struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2955 | { |
| 2956 | struct session *sess = strm_sess(s); |
| 2957 | struct http_txn *txn = s->txn; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2958 | struct act_rule *rule; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2959 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2960 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2961 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2962 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2963 | * resume condition. If a resume is needed it is always in the action |
| 2964 | * and never in the ACL or converters. In this case, we initialise the |
| 2965 | * current rule, and go to the action execution point. |
| 2966 | */ |
| 2967 | if (s->current_rule) { |
| 2968 | rule = s->current_rule; |
| 2969 | s->current_rule = NULL; |
| 2970 | if (s->current_rule_list == rules) |
| 2971 | goto resume_execution; |
| 2972 | } |
| 2973 | s->current_rule_list = rules; |
| 2974 | |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2975 | /* start the ruleset evaluation in strict mode */ |
| 2976 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2977 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2978 | list_for_each_entry(rule, rules, list) { |
| 2979 | /* check optional condition */ |
| 2980 | if (rule->cond) { |
| 2981 | int ret; |
| 2982 | |
| 2983 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 2984 | ret = acl_pass(ret); |
| 2985 | |
| 2986 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2987 | ret = !ret; |
| 2988 | |
| 2989 | if (!ret) /* condition not matched */ |
| 2990 | continue; |
| 2991 | } |
| 2992 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2993 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2994 | resume_execution: |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 2995 | if (rule->kw->flags & KWF_EXPERIMENTAL) |
| 2996 | mark_tainted(TAINTED_ACTION_EXP_EXECUTED); |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2997 | |
| 2998 | /* Always call the action function if defined */ |
| 2999 | if (rule->action_ptr) { |
| 3000 | if ((s->req.flags & CF_READ_ERROR) || |
| 3001 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 3002 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 3003 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3004 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 3005 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3006 | case ACT_RET_CONT: |
| 3007 | break; |
| 3008 | case ACT_RET_STOP: |
| 3009 | rule_ret = HTTP_RULE_RES_STOP; |
| 3010 | goto end; |
| 3011 | case ACT_RET_YIELD: |
| 3012 | s->current_rule = rule; |
| 3013 | rule_ret = HTTP_RULE_RES_YIELD; |
| 3014 | goto end; |
| 3015 | case ACT_RET_ERR: |
| 3016 | rule_ret = HTTP_RULE_RES_ERROR; |
| 3017 | goto end; |
| 3018 | case ACT_RET_DONE: |
| 3019 | rule_ret = HTTP_RULE_RES_DONE; |
| 3020 | goto end; |
| 3021 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 3022 | if (txn->status == -1) |
| 3023 | txn->status = 502; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3024 | rule_ret = HTTP_RULE_RES_DENY; |
| 3025 | goto end; |
| 3026 | case ACT_RET_ABRT: |
| 3027 | rule_ret = HTTP_RULE_RES_ABRT; |
| 3028 | goto end; |
| 3029 | case ACT_RET_INV: |
| 3030 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 3031 | goto end; |
| 3032 | } |
| 3033 | continue; /* eval the next rule */ |
| 3034 | } |
| 3035 | |
| 3036 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3037 | switch (rule->action) { |
| 3038 | case ACT_ACTION_ALLOW: |
| 3039 | rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
| 3040 | goto end; |
| 3041 | |
| 3042 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 3043 | txn->status = rule->arg.http_reply->status; |
| 3044 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 3045 | rule_ret = HTTP_RULE_RES_DENY; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3046 | goto end; |
| 3047 | |
| 3048 | case ACT_HTTP_SET_NICE: |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 3049 | s->task->nice = rule->arg.http.i; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3050 | break; |
| 3051 | |
| 3052 | case ACT_HTTP_SET_TOS: |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 3053 | conn_set_tos(objt_conn(sess->origin), rule->arg.http.i); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3054 | break; |
| 3055 | |
| 3056 | case ACT_HTTP_SET_MARK: |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 3057 | conn_set_mark(objt_conn(sess->origin), rule->arg.http.i); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3058 | break; |
| 3059 | |
| 3060 | case ACT_HTTP_SET_LOGL: |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 3061 | s->logs.level = rule->arg.http.i; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3062 | break; |
| 3063 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3064 | case ACT_HTTP_REDIR: |
Christopher Faulet | 49c2a70 | 2020-03-06 15:44:37 +0100 | [diff] [blame] | 3065 | rule_ret = HTTP_RULE_RES_ABRT; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3066 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 3067 | rule_ret = HTTP_RULE_RES_ERROR; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3068 | goto end; |
| 3069 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3070 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3071 | default: |
| 3072 | break; |
| 3073 | } |
| 3074 | } |
| 3075 | |
| 3076 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 3077 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 3078 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 3079 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 3080 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3081 | /* we reached the end of the rules, nothing to report */ |
| 3082 | return rule_ret; |
| 3083 | } |
| 3084 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3085 | /* Executes backend and frontend http-after-response rules for the stream <s>, |
| 3086 | * in that order. it return 1 on success and 0 on error. It is the caller |
| 3087 | * responsibility to catch error or ignore it. If it catches it, this function |
| 3088 | * may be called a second time, for the internal error. |
| 3089 | */ |
| 3090 | int http_eval_after_res_rules(struct stream *s) |
| 3091 | { |
| 3092 | struct session *sess = s->sess; |
| 3093 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 3094 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 3095 | /* Eval after-response ruleset only if the reply is not const */ |
| 3096 | if (s->txn->flags & TX_CONST_REPLY) |
| 3097 | goto end; |
| 3098 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3099 | /* prune the request variables if not already done and swap to the response variables. */ |
| 3100 | if (s->vars_reqres.scope != SCOPE_RES) { |
| 3101 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 3102 | vars_prune(&s->vars_reqres, s->sess, s); |
| 3103 | vars_init(&s->vars_reqres, SCOPE_RES); |
| 3104 | } |
| 3105 | |
| 3106 | ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s); |
Christopher Faulet | f5db0fc | 2021-11-09 17:48:39 +0100 | [diff] [blame] | 3107 | if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3108 | ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s); |
| 3109 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 3110 | end: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3111 | /* All other codes than CONTINUE, STOP or DONE are forbidden */ |
| 3112 | return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE); |
| 3113 | } |
| 3114 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3115 | /* |
| 3116 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 3117 | * desirable to call it only when needed. This code is quite complex because |
| 3118 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 3119 | * highly recommended not to touch this part without a good reason ! |
| 3120 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3121 | 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] | 3122 | { |
| 3123 | struct session *sess = s->sess; |
| 3124 | struct http_txn *txn = s->txn; |
| 3125 | struct htx *htx; |
| 3126 | struct http_hdr_ctx ctx; |
| 3127 | char *hdr_beg, *hdr_end, *del_from; |
| 3128 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
| 3129 | int preserve_hdr; |
| 3130 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3131 | htx = htxbuf(&req->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3132 | ctx.blk = NULL; |
| 3133 | while (http_find_header(htx, ist("Cookie"), &ctx, 1)) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3134 | int is_first = 1; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3135 | del_from = NULL; /* nothing to be deleted */ |
| 3136 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 3137 | |
| 3138 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3139 | * attributes whose name begin with a '$', and associate them with |
| 3140 | * the right cookie, if we want to delete this cookie. |
| 3141 | * So there are 3 cases for each cookie read : |
| 3142 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3143 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3144 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3145 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3146 | * "special" cookie. |
| 3147 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3148 | * remove it. If no application cookie persists in the header, we |
| 3149 | * *MUST* delete it. |
| 3150 | * |
| 3151 | * Note: RFC2965 is unclear about the processing of spaces around |
| 3152 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 3153 | * the RFC explicitly allows spaces before it, and not within the |
| 3154 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 3155 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 3156 | * after the equal sign too, resulting in some (rare) buggy |
| 3157 | * implementations trying to do that. So let's do what servers do. |
| 3158 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 3159 | * allowed quoted strings in values, with any possible character |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3160 | * after a backslash, including control chars and delimiters, which |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3161 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 3162 | * within values even without quotes. |
| 3163 | * |
| 3164 | * We have to keep multiple pointers in order to support cookie |
| 3165 | * removal at the beginning, middle or end of header without |
| 3166 | * corrupting the header. All of these headers are valid : |
| 3167 | * |
| 3168 | * hdr_beg hdr_end |
| 3169 | * | | |
| 3170 | * v | |
| 3171 | * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 | |
| 3172 | * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v |
| 3173 | * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3 |
| 3174 | * | | | | | | | |
| 3175 | * | | | | | | | |
| 3176 | * | | | | | | +--> next |
| 3177 | * | | | | | +----> val_end |
| 3178 | * | | | | +-----------> val_beg |
| 3179 | * | | | +--------------> equal |
| 3180 | * | | +----------------> att_end |
| 3181 | * | +---------------------> att_beg |
| 3182 | * +--------------------------> prev |
| 3183 | * |
| 3184 | */ |
| 3185 | hdr_beg = ctx.value.ptr; |
| 3186 | hdr_end = hdr_beg + ctx.value.len; |
| 3187 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3188 | /* Iterate through all cookies on this line */ |
| 3189 | |
| 3190 | /* find att_beg */ |
| 3191 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3192 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3193 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3194 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3195 | |
| 3196 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3197 | att_beg++; |
| 3198 | |
| 3199 | /* find att_end : this is the first character after the last non |
| 3200 | * space before the equal. It may be equal to hdr_end. |
| 3201 | */ |
| 3202 | equal = att_end = att_beg; |
| 3203 | while (equal < hdr_end) { |
| 3204 | if (*equal == '=' || *equal == ',' || *equal == ';') |
| 3205 | break; |
| 3206 | if (HTTP_IS_SPHT(*equal++)) |
| 3207 | continue; |
| 3208 | att_end = equal; |
| 3209 | } |
| 3210 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3211 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3212 | * is between <att_beg> and <equal>, both may be identical. |
| 3213 | */ |
| 3214 | /* look for end of cookie if there is an equal sign */ |
| 3215 | if (equal < hdr_end && *equal == '=') { |
| 3216 | /* look for the beginning of the value */ |
| 3217 | val_beg = equal + 1; |
| 3218 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3219 | val_beg++; |
| 3220 | |
| 3221 | /* find the end of the value, respecting quotes */ |
| 3222 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3223 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3224 | /* 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] | 3225 | val_end = next; |
| 3226 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3227 | val_end--; |
| 3228 | } |
| 3229 | else |
| 3230 | val_beg = val_end = next = equal; |
| 3231 | |
| 3232 | /* We have nothing to do with attributes beginning with |
| 3233 | * '$'. However, they will automatically be removed if a |
| 3234 | * header before them is removed, since they're supposed |
| 3235 | * to be linked together. |
| 3236 | */ |
| 3237 | if (*att_beg == '$') |
| 3238 | continue; |
| 3239 | |
| 3240 | /* Ignore cookies with no equal sign */ |
| 3241 | if (equal == next) { |
| 3242 | /* This is not our cookie, so we must preserve it. But if we already |
| 3243 | * scheduled another cookie for removal, we cannot remove the |
| 3244 | * complete header, but we can remove the previous block itself. |
| 3245 | */ |
| 3246 | preserve_hdr = 1; |
| 3247 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3248 | 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] | 3249 | val_end += delta; |
| 3250 | next += delta; |
| 3251 | hdr_end += delta; |
| 3252 | prev = del_from; |
| 3253 | del_from = NULL; |
| 3254 | } |
| 3255 | continue; |
| 3256 | } |
| 3257 | |
| 3258 | /* if there are spaces around the equal sign, we need to |
| 3259 | * strip them otherwise we'll get trouble for cookie captures, |
| 3260 | * or even for rewrites. Since this happens extremely rarely, |
| 3261 | * it does not hurt performance. |
| 3262 | */ |
| 3263 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3264 | int stripped_before = 0; |
| 3265 | int stripped_after = 0; |
| 3266 | |
| 3267 | if (att_end != equal) { |
| 3268 | memmove(att_end, equal, hdr_end - equal); |
| 3269 | stripped_before = (att_end - equal); |
| 3270 | equal += stripped_before; |
| 3271 | val_beg += stripped_before; |
| 3272 | } |
| 3273 | |
| 3274 | if (val_beg > equal + 1) { |
| 3275 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3276 | stripped_after = (equal + 1) - val_beg; |
| 3277 | val_beg += stripped_after; |
| 3278 | stripped_before += stripped_after; |
| 3279 | } |
| 3280 | |
| 3281 | val_end += stripped_before; |
| 3282 | next += stripped_before; |
| 3283 | hdr_end += stripped_before; |
| 3284 | } |
| 3285 | /* now everything is as on the diagram above */ |
| 3286 | |
| 3287 | /* First, let's see if we want to capture this cookie. We check |
| 3288 | * that we don't already have a client side cookie, because we |
| 3289 | * can only capture one. Also as an optimisation, we ignore |
| 3290 | * cookies shorter than the declared name. |
| 3291 | */ |
| 3292 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 3293 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3294 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3295 | int log_len = val_end - att_beg; |
| 3296 | |
| 3297 | if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3298 | ha_alert("HTTP logging : out of memory.\n"); |
| 3299 | } else { |
| 3300 | if (log_len > sess->fe->capture_len) |
| 3301 | log_len = sess->fe->capture_len; |
| 3302 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 3303 | txn->cli_cookie[log_len] = 0; |
| 3304 | } |
| 3305 | } |
| 3306 | |
| 3307 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 3308 | * following form : |
| 3309 | * |
| 3310 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
| 3311 | * |
| 3312 | * For cookies in prefix mode, the form is : |
| 3313 | * |
| 3314 | * Cookie: NAME=SRV~VALUE |
| 3315 | */ |
| 3316 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3317 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3318 | struct server *srv = s->be->srv; |
| 3319 | char *delim; |
| 3320 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3321 | /* 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] | 3322 | * have the server ID between val_beg and delim, and the original cookie between |
| 3323 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 3324 | * |
| 3325 | * hdr_beg |
| 3326 | * | |
| 3327 | * v |
| 3328 | * NAME=SRV; # in all but prefix modes |
| 3329 | * NAME=SRV~OPAQUE ; # in prefix mode |
| 3330 | * || || | |+-> next |
| 3331 | * || || | +--> val_end |
| 3332 | * || || +---------> delim |
| 3333 | * || |+------------> val_beg |
| 3334 | * || +-------------> att_end = equal |
| 3335 | * |+-----------------> att_beg |
| 3336 | * +------------------> prev |
| 3337 | * |
| 3338 | */ |
| 3339 | if (s->be->ck_opts & PR_CK_PFX) { |
| 3340 | for (delim = val_beg; delim < val_end; delim++) |
| 3341 | if (*delim == COOKIE_DELIM) |
| 3342 | break; |
| 3343 | } |
| 3344 | else { |
| 3345 | char *vbar1; |
| 3346 | delim = val_end; |
| 3347 | /* Now check if the cookie contains a date field, which would |
| 3348 | * appear after a vertical bar ('|') just after the server name |
| 3349 | * and before the delimiter. |
| 3350 | */ |
| 3351 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 3352 | if (vbar1) { |
| 3353 | /* OK, so left of the bar is the server's cookie and |
| 3354 | * right is the last seen date. It is a base64 encoded |
| 3355 | * 30-bit value representing the UNIX date since the |
| 3356 | * epoch in 4-second quantities. |
| 3357 | */ |
| 3358 | int val; |
| 3359 | delim = vbar1++; |
| 3360 | if (val_end - vbar1 >= 5) { |
| 3361 | val = b64tos30(vbar1); |
| 3362 | if (val > 0) |
| 3363 | txn->cookie_last_date = val << 2; |
| 3364 | } |
| 3365 | /* look for a second vertical bar */ |
| 3366 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 3367 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 3368 | val = b64tos30(vbar1 + 1); |
| 3369 | if (val > 0) |
| 3370 | txn->cookie_first_date = val << 2; |
| 3371 | } |
| 3372 | } |
| 3373 | } |
| 3374 | |
| 3375 | /* if the cookie has an expiration date and the proxy wants to check |
| 3376 | * it, then we do that now. We first check if the cookie is too old, |
| 3377 | * then only if it has expired. We detect strict overflow because the |
| 3378 | * time resolution here is not great (4 seconds). Cookies with dates |
| 3379 | * in the future are ignored if their offset is beyond one day. This |
| 3380 | * allows an admin to fix timezone issues without expiring everyone |
| 3381 | * and at the same time avoids keeping unwanted side effects for too |
| 3382 | * long. |
| 3383 | */ |
| 3384 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 3385 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) || |
| 3386 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
| 3387 | txn->flags &= ~TX_CK_MASK; |
| 3388 | txn->flags |= TX_CK_OLD; |
| 3389 | delim = val_beg; // let's pretend we have not found the cookie |
| 3390 | txn->cookie_first_date = 0; |
| 3391 | txn->cookie_last_date = 0; |
| 3392 | } |
| 3393 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 3394 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) || |
| 3395 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
| 3396 | txn->flags &= ~TX_CK_MASK; |
| 3397 | txn->flags |= TX_CK_EXPIRED; |
| 3398 | delim = val_beg; // let's pretend we have not found the cookie |
| 3399 | txn->cookie_first_date = 0; |
| 3400 | txn->cookie_last_date = 0; |
| 3401 | } |
| 3402 | |
| 3403 | /* Here, we'll look for the first running server which supports the cookie. |
| 3404 | * This allows to share a same cookie between several servers, for example |
| 3405 | * to dedicate backup servers to specific servers only. |
| 3406 | * However, to prevent clients from sticking to cookie-less backup server |
| 3407 | * when they have incidentely learned an empty cookie, we simply ignore |
| 3408 | * empty cookies and mark them as invalid. |
| 3409 | * The same behaviour is applied when persistence must be ignored. |
| 3410 | */ |
| 3411 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3412 | srv = NULL; |
| 3413 | |
| 3414 | while (srv) { |
| 3415 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 3416 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 3417 | if ((srv->cur_state != SRV_ST_STOPPED) || |
| 3418 | (s->be->options & PR_O_PERSIST) || |
| 3419 | (s->flags & SF_FORCE_PRST)) { |
| 3420 | /* we found the server and we can use it */ |
| 3421 | txn->flags &= ~TX_CK_MASK; |
| 3422 | txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN; |
| 3423 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
| 3424 | s->target = &srv->obj_type; |
| 3425 | break; |
| 3426 | } else { |
| 3427 | /* we found a server, but it's down, |
| 3428 | * mark it as such and go on in case |
| 3429 | * another one is available. |
| 3430 | */ |
| 3431 | txn->flags &= ~TX_CK_MASK; |
| 3432 | txn->flags |= TX_CK_DOWN; |
| 3433 | } |
| 3434 | } |
| 3435 | srv = srv->next; |
| 3436 | } |
| 3437 | |
| 3438 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
| 3439 | /* no server matched this cookie or we deliberately skipped it */ |
| 3440 | txn->flags &= ~TX_CK_MASK; |
| 3441 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3442 | txn->flags |= TX_CK_UNUSED; |
| 3443 | else |
| 3444 | txn->flags |= TX_CK_INVALID; |
| 3445 | } |
| 3446 | |
| 3447 | /* depending on the cookie mode, we may have to either : |
| 3448 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3449 | * the server never sees it ; |
| 3450 | * - 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] | 3451 | * application cookie so that it does not get accidentally removed later, |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3452 | * if we're in cookie prefix mode |
| 3453 | */ |
| 3454 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
| 3455 | int delta; /* negative */ |
| 3456 | |
| 3457 | memmove(val_beg, delim + 1, hdr_end - (delim + 1)); |
| 3458 | delta = val_beg - (delim + 1); |
| 3459 | val_end += delta; |
| 3460 | next += delta; |
| 3461 | hdr_end += delta; |
| 3462 | del_from = NULL; |
| 3463 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 3464 | } |
| 3465 | else if (del_from == NULL && |
| 3466 | (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) { |
| 3467 | del_from = prev; |
| 3468 | } |
| 3469 | } |
| 3470 | else { |
| 3471 | /* This is not our cookie, so we must preserve it. But if we already |
| 3472 | * scheduled another cookie for removal, we cannot remove the |
| 3473 | * complete header, but we can remove the previous block itself. |
| 3474 | */ |
| 3475 | preserve_hdr = 1; |
| 3476 | |
| 3477 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3478 | 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] | 3479 | if (att_beg >= del_from) |
| 3480 | att_beg += delta; |
| 3481 | if (att_end >= del_from) |
| 3482 | att_end += delta; |
| 3483 | val_beg += delta; |
| 3484 | val_end += delta; |
| 3485 | next += delta; |
| 3486 | hdr_end += delta; |
| 3487 | prev = del_from; |
| 3488 | del_from = NULL; |
| 3489 | } |
| 3490 | } |
| 3491 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3492 | } /* for each cookie */ |
| 3493 | |
| 3494 | |
| 3495 | /* There are no more cookies on this line. |
| 3496 | * We may still have one (or several) marked for deletion at the |
| 3497 | * end of the line. We must do this now in two ways : |
| 3498 | * - if some cookies must be preserved, we only delete from the |
| 3499 | * mark to the end of line ; |
| 3500 | * - if nothing needs to be preserved, simply delete the whole header |
| 3501 | */ |
| 3502 | if (del_from) { |
| 3503 | hdr_end = (preserve_hdr ? del_from : hdr_beg); |
| 3504 | } |
| 3505 | if ((hdr_end - hdr_beg) != ctx.value.len) { |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3506 | if (hdr_beg != hdr_end) |
| 3507 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3508 | else |
| 3509 | http_remove_header(htx, &ctx); |
| 3510 | } |
| 3511 | } /* for each "Cookie header */ |
| 3512 | } |
| 3513 | |
| 3514 | /* |
| 3515 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 3516 | * desirable to call it only when needed. This function is also used when we |
| 3517 | * just need to know if there is a cookie (eg: for check-cache). |
| 3518 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3519 | 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] | 3520 | { |
| 3521 | struct session *sess = s->sess; |
| 3522 | struct http_txn *txn = s->txn; |
| 3523 | struct htx *htx; |
| 3524 | struct http_hdr_ctx ctx; |
| 3525 | struct server *srv; |
| 3526 | char *hdr_beg, *hdr_end; |
| 3527 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 6f7a02a | 2019-04-15 21:49:49 +0200 | [diff] [blame] | 3528 | int is_cookie2 = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3529 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3530 | htx = htxbuf(&res->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3531 | |
| 3532 | ctx.blk = NULL; |
| 3533 | while (1) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3534 | int is_first = 1; |
| 3535 | |
Andrew McDermott | 86032c3 | 2022-02-11 18:26:49 +0000 | [diff] [blame] | 3536 | if (is_cookie2 || !http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) { |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3537 | if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1)) |
| 3538 | break; |
| 3539 | is_cookie2 = 1; |
| 3540 | } |
| 3541 | |
| 3542 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 3543 | * <prev> points to the colon. |
| 3544 | */ |
| 3545 | txn->flags |= TX_SCK_PRESENT; |
| 3546 | |
| 3547 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 3548 | * check-cache is enabled) and we are not interested in checking |
| 3549 | * them. Warning, the cookie capture is declared in the frontend. |
| 3550 | */ |
| 3551 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
| 3552 | break; |
| 3553 | |
| 3554 | /* OK so now we know we have to process this response cookie. |
| 3555 | * The format of the Set-Cookie header is slightly different |
| 3556 | * from the format of the Cookie header in that it does not |
| 3557 | * support the comma as a cookie delimiter (thus the header |
| 3558 | * cannot be folded) because the Expires attribute described in |
| 3559 | * the original Netscape's spec may contain an unquoted date |
| 3560 | * with a comma inside. We have to live with this because |
| 3561 | * many browsers don't support Max-Age and some browsers don't |
| 3562 | * support quoted strings. However the Set-Cookie2 header is |
| 3563 | * clean. |
| 3564 | * |
| 3565 | * We have to keep multiple pointers in order to support cookie |
| 3566 | * removal at the beginning, middle or end of header without |
| 3567 | * corrupting the header (in case of set-cookie2). A special |
| 3568 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 3569 | * fields after the first semi-colon. The <next> pointer points |
| 3570 | * either to the end of line (set-cookie) or next unquoted comma |
| 3571 | * (set-cookie2). All of these headers are valid : |
| 3572 | * |
| 3573 | * hdr_beg hdr_end |
| 3574 | * | | |
| 3575 | * v | |
| 3576 | * NAME1 = VALUE 1 ; Secure; Path="/" | |
| 3577 | * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v |
| 3578 | * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT |
| 3579 | * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard |
| 3580 | * | | | | | | | | |
| 3581 | * | | | | | | | +-> next |
| 3582 | * | | | | | | +------------> scav |
| 3583 | * | | | | | +--------------> val_end |
| 3584 | * | | | | +--------------------> val_beg |
| 3585 | * | | | +----------------------> equal |
| 3586 | * | | +------------------------> att_end |
| 3587 | * | +----------------------------> att_beg |
| 3588 | * +------------------------------> prev |
| 3589 | * -------------------------------> hdr_beg |
| 3590 | */ |
| 3591 | hdr_beg = ctx.value.ptr; |
| 3592 | hdr_end = hdr_beg + ctx.value.len; |
| 3593 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3594 | |
| 3595 | /* Iterate through all cookies on this line */ |
| 3596 | |
| 3597 | /* find att_beg */ |
| 3598 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3599 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3600 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3601 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3602 | |
| 3603 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3604 | att_beg++; |
| 3605 | |
| 3606 | /* find att_end : this is the first character after the last non |
| 3607 | * space before the equal. It may be equal to hdr_end. |
| 3608 | */ |
| 3609 | equal = att_end = att_beg; |
| 3610 | |
| 3611 | while (equal < hdr_end) { |
| 3612 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 3613 | break; |
| 3614 | if (HTTP_IS_SPHT(*equal++)) |
| 3615 | continue; |
| 3616 | att_end = equal; |
| 3617 | } |
| 3618 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3619 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3620 | * is between <att_beg> and <equal>, both may be identical. |
| 3621 | */ |
| 3622 | |
| 3623 | /* look for end of cookie if there is an equal sign */ |
| 3624 | if (equal < hdr_end && *equal == '=') { |
| 3625 | /* look for the beginning of the value */ |
| 3626 | val_beg = equal + 1; |
| 3627 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3628 | val_beg++; |
| 3629 | |
| 3630 | /* find the end of the value, respecting quotes */ |
| 3631 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3632 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3633 | /* 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] | 3634 | val_end = next; |
| 3635 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3636 | val_end--; |
| 3637 | } |
| 3638 | else { |
| 3639 | /* <equal> points to next comma, semi-colon or EOL */ |
| 3640 | val_beg = val_end = next = equal; |
| 3641 | } |
| 3642 | |
| 3643 | if (next < hdr_end) { |
| 3644 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 3645 | * a colon or semi-colon before the end. So skip all attr-value |
| 3646 | * pairs and look for the next comma. For Set-Cookie, since |
| 3647 | * commas are permitted in values, skip to the end. |
| 3648 | */ |
| 3649 | if (is_cookie2) |
| 3650 | next = http_find_hdr_value_end(next, hdr_end); |
| 3651 | else |
| 3652 | next = hdr_end; |
| 3653 | } |
| 3654 | |
| 3655 | /* Now everything is as on the diagram above */ |
| 3656 | |
| 3657 | /* Ignore cookies with no equal sign */ |
| 3658 | if (equal == val_end) |
| 3659 | continue; |
| 3660 | |
| 3661 | /* If there are spaces around the equal sign, we need to |
| 3662 | * strip them otherwise we'll get trouble for cookie captures, |
| 3663 | * or even for rewrites. Since this happens extremely rarely, |
| 3664 | * it does not hurt performance. |
| 3665 | */ |
| 3666 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3667 | int stripped_before = 0; |
| 3668 | int stripped_after = 0; |
| 3669 | |
| 3670 | if (att_end != equal) { |
| 3671 | memmove(att_end, equal, hdr_end - equal); |
| 3672 | stripped_before = (att_end - equal); |
| 3673 | equal += stripped_before; |
| 3674 | val_beg += stripped_before; |
| 3675 | } |
| 3676 | |
| 3677 | if (val_beg > equal + 1) { |
| 3678 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3679 | stripped_after = (equal + 1) - val_beg; |
| 3680 | val_beg += stripped_after; |
| 3681 | stripped_before += stripped_after; |
| 3682 | } |
| 3683 | |
| 3684 | val_end += stripped_before; |
| 3685 | next += stripped_before; |
| 3686 | hdr_end += stripped_before; |
| 3687 | |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3688 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3689 | ctx.value.len = hdr_end - hdr_beg; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | /* First, let's see if we want to capture this cookie. We check |
| 3693 | * that we don't already have a server side cookie, because we |
| 3694 | * can only capture one. Also as an optimisation, we ignore |
| 3695 | * cookies shorter than the declared name. |
| 3696 | */ |
| 3697 | if (sess->fe->capture_name != NULL && |
| 3698 | txn->srv_cookie == NULL && |
| 3699 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3700 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3701 | int log_len = val_end - att_beg; |
| 3702 | if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3703 | ha_alert("HTTP logging : out of memory.\n"); |
| 3704 | } |
| 3705 | else { |
| 3706 | if (log_len > sess->fe->capture_len) |
| 3707 | log_len = sess->fe->capture_len; |
| 3708 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 3709 | txn->srv_cookie[log_len] = 0; |
| 3710 | } |
| 3711 | } |
| 3712 | |
| 3713 | srv = objt_server(s->target); |
| 3714 | /* now check if we need to process it for persistence */ |
| 3715 | if (!(s->flags & SF_IGNORE_PRST) && |
| 3716 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3717 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3718 | /* assume passive cookie by default */ |
| 3719 | txn->flags &= ~TX_SCK_MASK; |
| 3720 | txn->flags |= TX_SCK_FOUND; |
| 3721 | |
| 3722 | /* If the cookie is in insert mode on a known server, we'll delete |
| 3723 | * this occurrence because we'll insert another one later. |
| 3724 | * We'll delete it too if the "indirect" option is set and we're in |
| 3725 | * a direct access. |
| 3726 | */ |
| 3727 | if (s->be->ck_opts & PR_CK_PSV) { |
| 3728 | /* The "preserve" flag was set, we don't want to touch the |
| 3729 | * server's cookie. |
| 3730 | */ |
| 3731 | } |
| 3732 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
| 3733 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
| 3734 | /* this cookie must be deleted */ |
| 3735 | if (prev == hdr_beg && next == hdr_end) { |
| 3736 | /* whole header */ |
| 3737 | http_remove_header(htx, &ctx); |
| 3738 | /* note: while both invalid now, <next> and <hdr_end> |
| 3739 | * are still equal, so the for() will stop as expected. |
| 3740 | */ |
| 3741 | } else { |
| 3742 | /* just remove the value */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3743 | int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3744 | next = prev; |
| 3745 | hdr_end += delta; |
| 3746 | } |
| 3747 | txn->flags &= ~TX_SCK_MASK; |
| 3748 | txn->flags |= TX_SCK_DELETED; |
| 3749 | /* and go on with next cookie */ |
| 3750 | } |
| 3751 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
| 3752 | /* replace bytes val_beg->val_end with the cookie name associated |
| 3753 | * with this server since we know it. |
| 3754 | */ |
| 3755 | int sliding, delta; |
| 3756 | |
| 3757 | ctx.value = ist2(val_beg, val_end - val_beg); |
| 3758 | ctx.lws_before = ctx.lws_after = 0; |
| 3759 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen)); |
| 3760 | delta = srv->cklen - (val_end - val_beg); |
| 3761 | sliding = (ctx.value.ptr - val_beg); |
| 3762 | hdr_beg += sliding; |
| 3763 | val_beg += sliding; |
| 3764 | next += sliding + delta; |
| 3765 | hdr_end += sliding + delta; |
| 3766 | |
| 3767 | txn->flags &= ~TX_SCK_MASK; |
| 3768 | txn->flags |= TX_SCK_REPLACED; |
| 3769 | } |
| 3770 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
| 3771 | /* insert the cookie name associated with this server |
| 3772 | * before existing cookie, and insert a delimiter between them.. |
| 3773 | */ |
| 3774 | int sliding, delta; |
| 3775 | ctx.value = ist2(val_beg, 0); |
| 3776 | ctx.lws_before = ctx.lws_after = 0; |
| 3777 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1)); |
| 3778 | delta = srv->cklen + 1; |
| 3779 | sliding = (ctx.value.ptr - val_beg); |
| 3780 | hdr_beg += sliding; |
| 3781 | val_beg += sliding; |
| 3782 | next += sliding + delta; |
| 3783 | hdr_end += sliding + delta; |
| 3784 | |
| 3785 | val_beg[srv->cklen] = COOKIE_DELIM; |
| 3786 | txn->flags &= ~TX_SCK_MASK; |
| 3787 | txn->flags |= TX_SCK_REPLACED; |
| 3788 | } |
| 3789 | } |
| 3790 | /* that's done for this cookie, check the next one on the same |
| 3791 | * line when next != hdr_end (only if is_cookie2). |
| 3792 | */ |
| 3793 | } |
| 3794 | } |
| 3795 | } |
| 3796 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3797 | /* |
| 3798 | * Parses the Cache-Control and Pragma request header fields to determine if |
| 3799 | * the request may be served from the cache and/or if it is cacheable. Updates |
| 3800 | * s->txn->flags. |
| 3801 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3802 | void http_check_request_for_cacheability(struct stream *s, struct channel *req) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3803 | { |
| 3804 | struct http_txn *txn = s->txn; |
| 3805 | struct htx *htx; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3806 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 3807 | int pragma_found, cc_found; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3808 | |
| 3809 | if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE) |
| 3810 | return; /* nothing more to do here */ |
| 3811 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3812 | htx = htxbuf(&req->buf); |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3813 | pragma_found = cc_found = 0; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3814 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3815 | /* Check "pragma" header for HTTP/1.0 compatibility. */ |
| 3816 | if (http_find_header(htx, ist("pragma"), &ctx, 1)) { |
| 3817 | if (isteqi(ctx.value, ist("no-cache"))) { |
| 3818 | pragma_found = 1; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3819 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3820 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3821 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3822 | ctx.blk = NULL; |
| 3823 | /* Don't use the cache and don't try to store if we found the |
| 3824 | * Authorization header */ |
| 3825 | if (http_find_header(htx, ist("authorization"), &ctx, 1)) { |
| 3826 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3827 | txn->flags |= TX_CACHE_IGNORE; |
| 3828 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3829 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3830 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3831 | /* Look for "cache-control" header and iterate over all the values |
| 3832 | * until we find one that specifies that caching is possible or not. */ |
| 3833 | ctx.blk = NULL; |
| 3834 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3835 | cc_found = 1; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3836 | /* We don't check the values after max-age, max-stale nor min-fresh, |
| 3837 | * we simply don't use the cache when they're specified. */ |
| 3838 | if (istmatchi(ctx.value, ist("max-age")) || |
| 3839 | istmatchi(ctx.value, ist("no-cache")) || |
| 3840 | istmatchi(ctx.value, ist("max-stale")) || |
| 3841 | istmatchi(ctx.value, ist("min-fresh"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3842 | txn->flags |= TX_CACHE_IGNORE; |
| 3843 | continue; |
| 3844 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3845 | if (istmatchi(ctx.value, ist("no-store"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3846 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3847 | continue; |
| 3848 | } |
| 3849 | } |
| 3850 | |
| 3851 | /* RFC7234#5.4: |
| 3852 | * When the Cache-Control header field is also present and |
| 3853 | * understood in a request, Pragma is ignored. |
| 3854 | * When the Cache-Control header field is not present in a |
| 3855 | * request, caches MUST consider the no-cache request |
| 3856 | * pragma-directive as having the same effect as if |
| 3857 | * "Cache-Control: no-cache" were present. |
| 3858 | */ |
| 3859 | if (!cc_found && pragma_found) |
| 3860 | txn->flags |= TX_CACHE_IGNORE; |
| 3861 | } |
| 3862 | |
| 3863 | /* |
| 3864 | * Check if response is cacheable or not. Updates s->txn->flags. |
| 3865 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3866 | void http_check_response_for_cacheability(struct stream *s, struct channel *res) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3867 | { |
| 3868 | struct http_txn *txn = s->txn; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3869 | struct http_hdr_ctx ctx = { .blk = NULL }; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3870 | struct htx *htx; |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3871 | int has_freshness_info = 0; |
| 3872 | int has_validator = 0; |
Remi Tricot-Le Breton | dd270e0 | 2023-07-04 17:13:28 +0200 | [diff] [blame] | 3873 | int has_null_maxage = 0; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3874 | |
| 3875 | if (txn->status < 200) { |
| 3876 | /* do not try to cache interim responses! */ |
| 3877 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3878 | return; |
| 3879 | } |
| 3880 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3881 | htx = htxbuf(&res->buf); |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3882 | /* Check "pragma" header for HTTP/1.0 compatibility. */ |
| 3883 | if (http_find_header(htx, ist("pragma"), &ctx, 1)) { |
| 3884 | if (isteqi(ctx.value, ist("no-cache"))) { |
| 3885 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3886 | return; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3887 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3888 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3889 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3890 | /* Look for "cache-control" header and iterate over all the values |
| 3891 | * until we find one that specifies that caching is possible or not. */ |
| 3892 | ctx.blk = NULL; |
| 3893 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
| 3894 | if (isteqi(ctx.value, ist("public"))) { |
| 3895 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3896 | continue; |
| 3897 | } |
Remi Tricot-Le Breton | dd270e0 | 2023-07-04 17:13:28 +0200 | [diff] [blame] | 3898 | /* This max-age might be overridden by a s-maxage directive, do |
| 3899 | * not unset the TX_CACHEABLE yet. */ |
| 3900 | if (isteqi(ctx.value, ist("max-age=0"))) { |
| 3901 | has_null_maxage = 1; |
| 3902 | continue; |
| 3903 | } |
| 3904 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3905 | if (isteqi(ctx.value, ist("private")) || |
| 3906 | isteqi(ctx.value, ist("no-cache")) || |
| 3907 | isteqi(ctx.value, ist("no-store")) || |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3908 | isteqi(ctx.value, ist("s-maxage=0"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3909 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3910 | continue; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3911 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3912 | /* We might have a no-cache="set-cookie" form. */ |
| 3913 | if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) { |
| 3914 | txn->flags &= ~TX_CACHE_COOK; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3915 | continue; |
| 3916 | } |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3917 | |
Remi Tricot-Le Breton | dd270e0 | 2023-07-04 17:13:28 +0200 | [diff] [blame] | 3918 | if (istmatchi(ctx.value, ist("s-maxage"))) { |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3919 | has_freshness_info = 1; |
Remi Tricot-Le Breton | dd270e0 | 2023-07-04 17:13:28 +0200 | [diff] [blame] | 3920 | has_null_maxage = 0; /* The null max-age is overridden, ignore it */ |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3921 | continue; |
| 3922 | } |
Remi Tricot-Le Breton | dd270e0 | 2023-07-04 17:13:28 +0200 | [diff] [blame] | 3923 | if (istmatchi(ctx.value, ist("max-age"))) { |
| 3924 | has_freshness_info = 1; |
| 3925 | continue; |
| 3926 | } |
| 3927 | } |
| 3928 | |
| 3929 | /* We had a 'max-age=0' directive but no extra s-maxage, do not cache |
| 3930 | * the response. */ |
| 3931 | if (has_null_maxage) { |
| 3932 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3933 | } |
| 3934 | |
| 3935 | /* If no freshness information could be found in Cache-Control values, |
| 3936 | * look for an Expires header. */ |
| 3937 | if (!has_freshness_info) { |
| 3938 | ctx.blk = NULL; |
| 3939 | has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0); |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3940 | } |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3941 | |
| 3942 | /* If no freshness information could be found in Cache-Control or Expires |
| 3943 | * values, look for an explicit validator. */ |
| 3944 | if (!has_freshness_info) { |
| 3945 | ctx.blk = NULL; |
| 3946 | has_validator = 1; |
| 3947 | if (!http_find_header(htx, ist("etag"), &ctx, 0)) { |
| 3948 | ctx.blk = NULL; |
| 3949 | if (!http_find_header(htx, ist("last-modified"), &ctx, 0)) |
| 3950 | has_validator = 0; |
| 3951 | } |
| 3952 | } |
| 3953 | |
| 3954 | /* We won't store an entry that has neither a cache validator nor an |
| 3955 | * explicit expiration time, as suggested in RFC 7234#3. */ |
| 3956 | if (!has_freshness_info && !has_validator) |
Remi Tricot-Le Breton | a59ce4f | 2023-02-21 11:47:17 +0100 | [diff] [blame] | 3957 | txn->flags &= ~TX_CACHEABLE; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3958 | } |
| 3959 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3960 | /* |
| 3961 | * In a GET, HEAD or POST request, check if the requested URI matches the stats uri |
| 3962 | * for the current backend. |
| 3963 | * |
| 3964 | * It is assumed that the request is either a HEAD, GET, or POST and that the |
| 3965 | * uri_auth field is valid. |
| 3966 | * |
| 3967 | * Returns 1 if stats should be provided, otherwise 0. |
| 3968 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3969 | 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] | 3970 | { |
| 3971 | struct uri_auth *uri_auth = backend->uri_auth; |
| 3972 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3973 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3974 | struct ist uri; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3975 | |
| 3976 | if (!uri_auth) |
| 3977 | return 0; |
| 3978 | |
| 3979 | if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST) |
| 3980 | return 0; |
| 3981 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3982 | htx = htxbuf(&s->req.buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 3983 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3984 | uri = htx_sl_req_uri(sl); |
Willy Tarreau | 1eb3b48 | 2019-10-31 15:50:28 +0100 | [diff] [blame] | 3985 | if (*uri_auth->uri_prefix == '/') |
| 3986 | uri = http_get_path(uri); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3987 | |
| 3988 | /* check URI size */ |
| 3989 | if (uri_auth->uri_len > uri.len) |
| 3990 | return 0; |
| 3991 | |
| 3992 | if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
| 3993 | return 0; |
| 3994 | |
| 3995 | return 1; |
| 3996 | } |
| 3997 | |
| 3998 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3999 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 4000 | * and program a response message if something was unexpected. It cannot fail |
| 4001 | * and always relies on the stats applet to complete the job. It does not touch |
| 4002 | * analysers nor counters, which are left to the caller. It does not touch |
| 4003 | * s->target which is supposed to already point to the stats applet. The caller |
| 4004 | * is expected to have already assigned an appctx to the stream. |
| 4005 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4006 | static int http_handle_stats(struct stream *s, struct channel *req) |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4007 | { |
| 4008 | struct stats_admin_rule *stats_admin_rule; |
| 4009 | struct stream_interface *si = &s->si[1]; |
| 4010 | struct session *sess = s->sess; |
| 4011 | struct http_txn *txn = s->txn; |
| 4012 | struct http_msg *msg = &txn->req; |
| 4013 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 4014 | const char *h, *lookup, *end; |
| 4015 | struct appctx *appctx; |
| 4016 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4017 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4018 | |
| 4019 | appctx = si_appctx(si); |
| 4020 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 4021 | appctx->st1 = appctx->st2 = 0; |
| 4022 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
Willy Tarreau | 676c29e | 2019-10-09 10:50:01 +0200 | [diff] [blame] | 4023 | appctx->ctx.stats.flags |= uri_auth->flags; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4024 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
| 4025 | if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD)) |
| 4026 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
| 4027 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4028 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 4029 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4030 | lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len; |
| 4031 | end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4032 | |
| 4033 | for (h = lookup; h <= end - 3; h++) { |
| 4034 | if (memcmp(h, ";up", 3) == 0) { |
| 4035 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
| 4036 | break; |
| 4037 | } |
Amaury Denoyelle | 91e55ea | 2021-02-25 14:46:08 +0100 | [diff] [blame] | 4038 | } |
| 4039 | |
| 4040 | for (h = lookup; h <= end - 9; h++) { |
| 4041 | if (memcmp(h, ";no-maint", 9) == 0) { |
Willy Tarreau | 3e32036 | 2020-10-23 17:28:57 +0200 | [diff] [blame] | 4042 | appctx->ctx.stats.flags |= STAT_HIDE_MAINT; |
| 4043 | break; |
| 4044 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4045 | } |
| 4046 | |
| 4047 | if (uri_auth->refresh) { |
| 4048 | for (h = lookup; h <= end - 10; h++) { |
| 4049 | if (memcmp(h, ";norefresh", 10) == 0) { |
| 4050 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
| 4051 | break; |
| 4052 | } |
| 4053 | } |
| 4054 | } |
| 4055 | |
| 4056 | for (h = lookup; h <= end - 4; h++) { |
| 4057 | if (memcmp(h, ";csv", 4) == 0) { |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 4058 | appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4059 | break; |
| 4060 | } |
| 4061 | } |
| 4062 | |
| 4063 | for (h = lookup; h <= end - 6; h++) { |
| 4064 | if (memcmp(h, ";typed", 6) == 0) { |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 4065 | appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4066 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
| 4067 | break; |
| 4068 | } |
| 4069 | } |
| 4070 | |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 4071 | for (h = lookup; h <= end - 5; h++) { |
| 4072 | if (memcmp(h, ";json", 5) == 0) { |
| 4073 | appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
| 4074 | appctx->ctx.stats.flags |= STAT_FMT_JSON; |
| 4075 | break; |
| 4076 | } |
| 4077 | } |
| 4078 | |
| 4079 | for (h = lookup; h <= end - 12; h++) { |
| 4080 | if (memcmp(h, ";json-schema", 12) == 0) { |
| 4081 | appctx->ctx.stats.flags &= ~STAT_FMT_MASK; |
| 4082 | appctx->ctx.stats.flags |= STAT_JSON_SCHM; |
| 4083 | break; |
| 4084 | } |
| 4085 | } |
| 4086 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4087 | for (h = lookup; h <= end - 8; h++) { |
| 4088 | if (memcmp(h, ";st=", 4) == 0) { |
| 4089 | int i; |
| 4090 | h += 4; |
| 4091 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
| 4092 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 4093 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
| 4094 | appctx->ctx.stats.st_code = i; |
| 4095 | break; |
| 4096 | } |
| 4097 | } |
| 4098 | break; |
| 4099 | } |
| 4100 | } |
| 4101 | |
| 4102 | appctx->ctx.stats.scope_str = 0; |
| 4103 | appctx->ctx.stats.scope_len = 0; |
| 4104 | for (h = lookup; h <= end - 8; h++) { |
| 4105 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 4106 | int itx = 0; |
| 4107 | const char *h2; |
| 4108 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 4109 | const char *err; |
| 4110 | |
| 4111 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 4112 | h2 = h; |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 4113 | appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl); |
| 4114 | while (h < end) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4115 | if (*h == ';' || *h == '&' || *h == ' ') |
| 4116 | break; |
| 4117 | itx++; |
| 4118 | h++; |
| 4119 | } |
| 4120 | |
| 4121 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 4122 | itx = STAT_SCOPE_TXT_MAXLEN; |
| 4123 | appctx->ctx.stats.scope_len = itx; |
| 4124 | |
| 4125 | /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */ |
| 4126 | memcpy(scope_txt, h2, itx); |
| 4127 | scope_txt[itx] = '\0'; |
| 4128 | err = invalid_char(scope_txt); |
| 4129 | if (err) { |
| 4130 | /* bad char in search text => clear scope */ |
| 4131 | appctx->ctx.stats.scope_str = 0; |
| 4132 | appctx->ctx.stats.scope_len = 0; |
| 4133 | } |
| 4134 | break; |
| 4135 | } |
| 4136 | } |
| 4137 | |
| 4138 | /* now check whether we have some admin rules for this request */ |
| 4139 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
| 4140 | int ret = 1; |
| 4141 | |
| 4142 | if (stats_admin_rule->cond) { |
| 4143 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 4144 | ret = acl_pass(ret); |
| 4145 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 4146 | ret = !ret; |
| 4147 | } |
| 4148 | |
| 4149 | if (ret) { |
| 4150 | /* no rule, or the rule matches */ |
| 4151 | appctx->ctx.stats.flags |= STAT_ADMIN; |
| 4152 | break; |
| 4153 | } |
| 4154 | } |
| 4155 | |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4156 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 4157 | appctx->st0 = STAT_HTTP_HEAD; |
| 4158 | else if (txn->meth == HTTP_METH_POST) { |
Christopher Faulet | bd9e842 | 2019-08-15 22:26:48 +0200 | [diff] [blame] | 4159 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4160 | appctx->st0 = STAT_HTTP_POST; |
Christopher Faulet | bd9e842 | 2019-08-15 22:26:48 +0200 | [diff] [blame] | 4161 | if (msg->msg_state < HTTP_MSG_DATA) |
| 4162 | req->analysers |= AN_REQ_HTTP_BODY; |
| 4163 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4164 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4165 | /* POST without admin level */ |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4166 | appctx->ctx.stats.flags &= ~STAT_CHUNKED; |
| 4167 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 4168 | appctx->st0 = STAT_HTTP_LAST; |
| 4169 | } |
| 4170 | } |
| 4171 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4172 | /* Unsupported method */ |
| 4173 | appctx->ctx.stats.flags &= ~STAT_CHUNKED; |
| 4174 | appctx->ctx.stats.st_code = STAT_STATUS_IVAL; |
| 4175 | appctx->st0 = STAT_HTTP_LAST; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4176 | } |
| 4177 | |
| 4178 | s->task->nice = -32; /* small boost for HTTP statistics */ |
| 4179 | return 1; |
| 4180 | } |
| 4181 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4182 | /* This function waits for the message payload at most <time> milliseconds (may |
| 4183 | * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the |
| 4184 | * payload are received (0 means no limit). It returns HTTP_RULE_* depending on |
| 4185 | * the result: |
| 4186 | * |
| 4187 | * - HTTP_RULE_RES_CONT when conditions are met to stop waiting |
| 4188 | * - HTTP_RULE_RES_YIELD to wait for more data |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 4189 | * - HTTP_RULE_RES_ABRT when a timeout occurred. |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4190 | * - 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] | 4191 | * - HTTP_RULE_RES_ERROR if an internal error occurred |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4192 | * |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 4193 | * 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] | 4194 | * to the client, depending on the channel (408 on request side, 504 on response |
| 4195 | * side). All other errors must be handled by the caller. |
| 4196 | */ |
| 4197 | enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn, |
| 4198 | unsigned int time, unsigned int bytes) |
| 4199 | { |
| 4200 | struct session *sess = s->sess; |
| 4201 | struct http_txn *txn = s->txn; |
| 4202 | struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req); |
| 4203 | struct htx *htx; |
| 4204 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 4205 | |
| 4206 | htx = htxbuf(&chn->buf); |
| 4207 | |
| 4208 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
| 4209 | ret = HTTP_RULE_RES_BADREQ; |
| 4210 | goto end; |
| 4211 | } |
| 4212 | if (htx->flags & HTX_FL_PROCESSING_ERROR) { |
| 4213 | ret = HTTP_RULE_RES_ERROR; |
| 4214 | goto end; |
| 4215 | } |
| 4216 | |
| 4217 | /* Do nothing for bodyless and CONNECT requests */ |
| 4218 | if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS)) |
| 4219 | goto end; |
| 4220 | |
| 4221 | if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) { |
| 4222 | if (http_handle_expect_hdr(s, htx, msg) == -1) { |
| 4223 | ret = HTTP_RULE_RES_ERROR; |
| 4224 | goto end; |
| 4225 | } |
| 4226 | } |
| 4227 | |
| 4228 | msg->msg_state = HTTP_MSG_DATA; |
| 4229 | |
| 4230 | /* Now we're in HTTP_MSG_DATA. We just need to know if all data have |
| 4231 | * been received or if the buffer is full. |
| 4232 | */ |
Christopher Faulet | 59df812 | 2021-09-23 14:46:32 +0200 | [diff] [blame] | 4233 | if ((htx->flags & HTX_FL_EOM) || |
| 4234 | htx_get_tail_type(htx) > HTX_BLK_DATA || |
| 4235 | channel_htx_full(chn, htx, global.tune.maxrewrite) || |
| 4236 | si_rx_blocked_room(chn_prod(chn))) |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4237 | goto end; |
| 4238 | |
| 4239 | if (bytes) { |
| 4240 | struct htx_blk *blk; |
| 4241 | unsigned int len = 0; |
| 4242 | |
| 4243 | for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 4244 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 4245 | continue; |
| 4246 | len += htx_get_blksz(blk); |
| 4247 | if (len >= bytes) |
| 4248 | goto end; |
| 4249 | } |
| 4250 | } |
| 4251 | |
| 4252 | if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) { |
| 4253 | if (!(chn->flags & CF_ISRESP)) |
| 4254 | goto abort_req; |
| 4255 | goto abort_res; |
| 4256 | } |
| 4257 | |
| 4258 | /* we get here if we need to wait for more data */ |
| 4259 | if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) { |
| 4260 | if (!tick_isset(chn->analyse_exp)) |
| 4261 | chn->analyse_exp = tick_add_ifset(now_ms, time); |
| 4262 | ret = HTTP_RULE_RES_YIELD; |
| 4263 | } |
| 4264 | |
| 4265 | end: |
| 4266 | return ret; |
| 4267 | |
| 4268 | abort_req: |
| 4269 | txn->status = 408; |
| 4270 | if (!(s->flags & SF_ERR_MASK)) |
| 4271 | s->flags |= SF_ERR_CLITO; |
| 4272 | if (!(s->flags & SF_FINST_MASK)) |
Christopher Faulet | 1a023fa | 2023-01-13 10:49:31 +0100 | [diff] [blame] | 4273 | s->flags |= SF_FINST_R; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4274 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4275 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4276 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4277 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 4278 | ret = HTTP_RULE_RES_ABRT; |
| 4279 | goto end; |
| 4280 | |
| 4281 | abort_res: |
| 4282 | txn->status = 504; |
| 4283 | if (!(s->flags & SF_ERR_MASK)) |
| 4284 | s->flags |= SF_ERR_SRVTO; |
| 4285 | if (!(s->flags & SF_FINST_MASK)) |
Christopher Faulet | 1a023fa | 2023-01-13 10:49:31 +0100 | [diff] [blame] | 4286 | s->flags |= SF_FINST_R; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4287 | stream_inc_http_fail_ctr(s); |
| 4288 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 4289 | ret = HTTP_RULE_RES_ABRT; |
| 4290 | goto end; |
| 4291 | } |
| 4292 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4293 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4294 | { |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4295 | struct channel *req = &s->req; |
| 4296 | struct channel *res = &s->res; |
| 4297 | struct server *srv; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4298 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4299 | struct htx_sl *sl; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4300 | struct ist path, location; |
| 4301 | unsigned int flags; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4302 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4303 | /* |
| 4304 | * Create the location |
| 4305 | */ |
| 4306 | chunk_reset(&trash); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4307 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4308 | /* 1: add the server's prefix */ |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4309 | /* special prefix "/" means don't change URL */ |
| 4310 | srv = __objt_server(s->target); |
| 4311 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 4312 | if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len)) |
| 4313 | return; |
| 4314 | } |
| 4315 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4316 | /* 2: add the request Path */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4317 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 4318 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4319 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4320 | if (!isttest(path)) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4321 | return; |
| 4322 | |
| 4323 | if (!chunk_memcat(&trash, path.ptr, path.len)) |
| 4324 | return; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4325 | location = ist2(trash.area, trash.data); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4326 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4327 | /* |
| 4328 | * Create the 302 respone |
| 4329 | */ |
| 4330 | htx = htx_from_buf(&res->buf); |
| 4331 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 4332 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4333 | ist("HTTP/1.1"), ist("302"), ist("Found")); |
| 4334 | if (!sl) |
| 4335 | goto fail; |
| 4336 | sl->info.res.status = 302; |
| 4337 | s->txn->status = 302; |
| 4338 | |
| 4339 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) || |
| 4340 | !htx_add_header(htx, ist("Connection"), ist("close")) || |
| 4341 | !htx_add_header(htx, ist("Content-length"), ist("0")) || |
| 4342 | !htx_add_header(htx, ist("Location"), location)) |
| 4343 | goto fail; |
| 4344 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4345 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4346 | goto fail; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4347 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4348 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | c20afb8 | 2020-01-24 19:16:26 +0100 | [diff] [blame] | 4349 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4350 | if (!http_forward_proxy_resp(s, 1)) |
| 4351 | goto fail; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4352 | |
| 4353 | /* return without error. */ |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4354 | si_shutr(si); |
| 4355 | si_shutw(si); |
| 4356 | si->err_type = SI_ET_NONE; |
| 4357 | si->state = SI_ST_CLO; |
| 4358 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4359 | if (!(s->flags & SF_ERR_MASK)) |
| 4360 | s->flags |= SF_ERR_LOCAL; |
| 4361 | if (!(s->flags & SF_FINST_MASK)) |
| 4362 | s->flags |= SF_FINST_C; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4363 | |
| 4364 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 4365 | srv_inc_sess_ctr(srv); |
| 4366 | srv_set_sess_last(srv); |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4367 | return; |
| 4368 | |
| 4369 | fail: |
| 4370 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4371 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4372 | channel_htx_truncate(res, htx); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4373 | } |
| 4374 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4375 | /* This function terminates the request because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4376 | * because an error was triggered during the body forwarding. |
| 4377 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4378 | static void http_end_request(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4379 | { |
| 4380 | struct channel *chn = &s->req; |
| 4381 | struct http_txn *txn = s->txn; |
| 4382 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4383 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4384 | |
Christopher Faulet | b42a8b6 | 2018-11-19 21:59:00 +0100 | [diff] [blame] | 4385 | if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR || |
| 4386 | txn->rsp.msg_state == HTTP_MSG_ERROR)) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4387 | channel_abort(chn); |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4388 | channel_htx_truncate(chn, htxbuf(&chn->buf)); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4389 | goto end; |
| 4390 | } |
| 4391 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4392 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) { |
| 4393 | 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] | 4394 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4395 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4396 | |
| 4397 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4398 | /* No need to read anymore, the request was completely parsed. |
| 4399 | * We can shut the read side unless we want to abort_on_close, |
| 4400 | * or we have a POST request. The issue with POST requests is |
| 4401 | * that some browsers still send a CRLF after the request, and |
| 4402 | * this CRLF must be read so that it does not remain in the kernel |
| 4403 | * buffers, otherwise a close could cause an RST on some systems |
| 4404 | * (eg: Linux). |
| 4405 | */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 4406 | 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] | 4407 | channel_dont_read(chn); |
| 4408 | |
| 4409 | /* if the server closes the connection, we want to immediately react |
| 4410 | * and close the socket to save packets and syscalls. |
| 4411 | */ |
| 4412 | s->si[1].flags |= SI_FL_NOHALF; |
| 4413 | |
| 4414 | /* In any case we've finished parsing the request so we must |
| 4415 | * disable Nagle when sending data because 1) we're not going |
| 4416 | * to shut this side, and 2) the server is waiting for us to |
| 4417 | * send pending data. |
| 4418 | */ |
| 4419 | chn->flags |= CF_NEVER_WAIT; |
| 4420 | |
Christopher Faulet | d01ce40 | 2019-01-02 17:44:13 +0100 | [diff] [blame] | 4421 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4422 | /* The server has not finished to respond, so we |
| 4423 | * don't want to move in order not to upset it. |
| 4424 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4425 | 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] | 4426 | return; |
| 4427 | } |
| 4428 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4429 | /* When we get here, it means that both the request and the |
| 4430 | * response have finished receiving. Depending on the connection |
| 4431 | * mode, we'll have to wait for the last bytes to leave in either |
| 4432 | * direction, and sometimes for a close to be effective. |
| 4433 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4434 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4435 | /* Tunnel mode will not have any analyser so it needs to |
| 4436 | * poll for reads. |
| 4437 | */ |
| 4438 | channel_auto_read(chn); |
| 4439 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 4440 | } |
| 4441 | else { |
| 4442 | /* we're not expecting any new data to come for this |
| 4443 | * transaction, so we can close it. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4444 | * |
| 4445 | * However, there is an exception if the response |
| 4446 | * length is undefined. In this case, we need to wait |
| 4447 | * the close from the server. The response will be |
| 4448 | * switched in TUNNEL mode until the end. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4449 | */ |
| 4450 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 4451 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4452 | goto check_channel_flags; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4453 | |
| 4454 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4455 | channel_shutr_now(chn); |
| 4456 | channel_shutw_now(chn); |
| 4457 | } |
| 4458 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4459 | goto check_channel_flags; |
| 4460 | } |
| 4461 | |
| 4462 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4463 | http_msg_closing: |
| 4464 | /* nothing else to forward, just waiting for the output buffer |
| 4465 | * to be empty and for the shutw_now to take effect. |
| 4466 | */ |
| 4467 | if (channel_is_empty(chn)) { |
| 4468 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4469 | goto http_msg_closed; |
| 4470 | } |
| 4471 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4472 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4473 | goto end; |
| 4474 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4475 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4476 | return; |
| 4477 | } |
| 4478 | |
| 4479 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4480 | http_msg_closed: |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4481 | /* if we don't know whether the server will close, we need to hard close */ |
| 4482 | if (txn->rsp.flags & HTTP_MSGF_XFER_LEN) |
| 4483 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4484 | /* 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] | 4485 | if (!(s->be->options & PR_O_ABRT_CLOSE)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4486 | channel_dont_read(chn); |
| 4487 | goto end; |
| 4488 | } |
| 4489 | |
| 4490 | check_channel_flags: |
| 4491 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4492 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4493 | /* if we've just closed an output, let's switch */ |
| 4494 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4495 | goto http_msg_closing; |
| 4496 | } |
| 4497 | |
| 4498 | end: |
| 4499 | chn->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4500 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4501 | chn->flags |= CF_NEVER_WAIT; |
| 4502 | if (HAS_REQ_DATA_FILTERS(s)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4503 | chn->analysers |= AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4504 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4505 | channel_auto_close(chn); |
| 4506 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4507 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4511 | /* This function terminates the response because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4512 | * because an error was triggered during the body forwarding. |
| 4513 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4514 | static void http_end_response(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4515 | { |
| 4516 | struct channel *chn = &s->res; |
| 4517 | struct http_txn *txn = s->txn; |
| 4518 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4519 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4520 | |
Christopher Faulet | b42a8b6 | 2018-11-19 21:59:00 +0100 | [diff] [blame] | 4521 | if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR || |
| 4522 | txn->rsp.msg_state == HTTP_MSG_ERROR)) { |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4523 | channel_htx_truncate(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4524 | channel_abort(&s->req); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4525 | goto end; |
| 4526 | } |
| 4527 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4528 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) { |
| 4529 | 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] | 4530 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4531 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4532 | |
| 4533 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4534 | /* In theory, we don't need to read anymore, but we must |
| 4535 | * still monitor the server connection for a possible close |
| 4536 | * while the request is being uploaded, so we don't disable |
| 4537 | * reading. |
| 4538 | */ |
| 4539 | /* channel_dont_read(chn); */ |
| 4540 | |
| 4541 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4542 | /* The client seems to still be sending data, probably |
| 4543 | * because we got an error response during an upload. |
| 4544 | * We have the choice of either breaking the connection |
| 4545 | * or letting it pass through. Let's do the later. |
| 4546 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4547 | 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] | 4548 | return; |
| 4549 | } |
| 4550 | |
| 4551 | /* When we get here, it means that both the request and the |
| 4552 | * response have finished receiving. Depending on the connection |
| 4553 | * mode, we'll have to wait for the last bytes to leave in either |
| 4554 | * direction, and sometimes for a close to be effective. |
| 4555 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4556 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4557 | channel_auto_read(chn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4558 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4559 | } |
| 4560 | else { |
| 4561 | /* we're not expecting any new data to come for this |
| 4562 | * transaction, so we can close it. |
| 4563 | */ |
| 4564 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4565 | channel_shutr_now(chn); |
| 4566 | channel_shutw_now(chn); |
| 4567 | } |
| 4568 | } |
| 4569 | goto check_channel_flags; |
| 4570 | } |
| 4571 | |
| 4572 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4573 | http_msg_closing: |
| 4574 | /* nothing else to forward, just waiting for the output buffer |
| 4575 | * to be empty and for the shutw_now to take effect. |
| 4576 | */ |
| 4577 | if (channel_is_empty(chn)) { |
| 4578 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4579 | goto http_msg_closed; |
| 4580 | } |
| 4581 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4582 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4583 | _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts); |
| 4584 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 4585 | if (strm_sess(s)->listener && strm_sess(s)->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4586 | _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4587 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4588 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4589 | goto end; |
| 4590 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4591 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4592 | return; |
| 4593 | } |
| 4594 | |
| 4595 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4596 | http_msg_closed: |
| 4597 | /* drop any pending data */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4598 | channel_htx_truncate(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4599 | channel_abort(&s->req); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4600 | goto end; |
| 4601 | } |
| 4602 | |
| 4603 | check_channel_flags: |
| 4604 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4605 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4606 | /* if we've just closed an output, let's switch */ |
| 4607 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4608 | goto http_msg_closing; |
| 4609 | } |
| 4610 | |
| 4611 | end: |
| 4612 | chn->analysers &= AN_RES_FLT_END; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4613 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4614 | chn->flags |= CF_NEVER_WAIT; |
| 4615 | if (HAS_RSP_DATA_FILTERS(s)) |
| 4616 | chn->analysers |= AN_RES_FLT_XFER_DATA; |
| 4617 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4618 | channel_auto_close(chn); |
| 4619 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4620 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4621 | } |
| 4622 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4623 | /* Forward a response generated by HAProxy (error/redirect/return). This |
| 4624 | * function forwards all pending incoming data. If <final> is set to 0, nothing |
| 4625 | * more is performed. It is used for 1xx informational messages. Otherwise, the |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4626 | * transaction is terminated and the request is emptied. On success 1 is |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4627 | * returned. If an error occurred, 0 is returned. If it fails, this function |
| 4628 | * only exits. It is the caller responsibility to do the cleanup. |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4629 | */ |
| 4630 | int http_forward_proxy_resp(struct stream *s, int final) |
| 4631 | { |
| 4632 | struct channel *req = &s->req; |
| 4633 | struct channel *res = &s->res; |
| 4634 | struct htx *htx = htxbuf(&res->buf); |
| 4635 | size_t data; |
| 4636 | |
| 4637 | if (final) { |
| 4638 | htx->flags |= HTX_FL_PROXY_RESP; |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4639 | |
Christopher Faulet | aab1b67 | 2020-11-18 16:44:02 +0100 | [diff] [blame] | 4640 | if (!htx_is_empty(htx) && !http_eval_after_res_rules(s)) |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 4641 | return 0; |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4642 | |
Christopher Faulet | d6c4836 | 2020-10-19 18:01:38 +0200 | [diff] [blame] | 4643 | if (s->txn->meth == HTTP_METH_HEAD) |
| 4644 | htx_skip_msg_payload(htx); |
| 4645 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4646 | channel_auto_read(req); |
| 4647 | channel_abort(req); |
| 4648 | channel_auto_close(req); |
| 4649 | channel_htx_erase(req, htxbuf(&req->buf)); |
| 4650 | |
| 4651 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 4652 | channel_auto_read(res); |
| 4653 | channel_auto_close(res); |
| 4654 | channel_shutr_now(res); |
Christopher Faulet | 1a9db7c | 2020-06-25 15:36:45 +0200 | [diff] [blame] | 4655 | res->flags |= CF_EOI; /* The response is terminated, add EOI */ |
Christopher Faulet | 42432f3 | 2020-11-20 17:43:16 +0100 | [diff] [blame] | 4656 | htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */ |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4657 | } |
Christopher Faulet | cf6898c | 2020-06-25 15:55:11 +0200 | [diff] [blame] | 4658 | else { |
| 4659 | /* Send ASAP informational messages. Rely on CF_EOI for final |
| 4660 | * response. |
| 4661 | */ |
| 4662 | res->flags |= CF_SEND_DONTWAIT; |
| 4663 | } |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4664 | |
| 4665 | data = htx->data - co_data(res); |
| 4666 | c_adv(res, data); |
| 4667 | htx->first = -1; |
| 4668 | res->total += data; |
| 4669 | return 1; |
| 4670 | } |
| 4671 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4672 | void http_server_error(struct stream *s, struct stream_interface *si, int err, |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4673 | int finst, struct http_reply *msg) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4674 | { |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4675 | http_reply_and_close(s, s->txn->status, msg); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4676 | if (!(s->flags & SF_ERR_MASK)) |
| 4677 | s->flags |= err; |
| 4678 | if (!(s->flags & SF_FINST_MASK)) |
| 4679 | s->flags |= finst; |
| 4680 | } |
| 4681 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4682 | 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] | 4683 | { |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4684 | if (!msg) { |
| 4685 | channel_htx_truncate(&s->res, htxbuf(&s->res.buf)); |
| 4686 | goto end; |
| 4687 | } |
| 4688 | |
| 4689 | if (http_reply_message(s, msg) == -1) { |
| 4690 | /* 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] | 4691 | * it is already an internal error. If it was already a "const" |
| 4692 | * 500 error, just fail. |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4693 | */ |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4694 | if (s->txn->status == 500) { |
| 4695 | if (s->txn->flags & TX_CONST_REPLY) |
| 4696 | goto end; |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4697 | s->txn->flags |= TX_CONST_REPLY; |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4698 | } |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4699 | s->txn->status = 500; |
| 4700 | s->txn->http_reply = NULL; |
| 4701 | return http_reply_and_close(s, s->txn->status, http_error_message(s)); |
| 4702 | } |
| 4703 | |
| 4704 | end: |
| 4705 | s->res.wex = tick_add_ifset(now_ms, s->res.wto); |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4706 | |
Christopher Faulet | db8ba10 | 2021-09-10 09:17:50 +0200 | [diff] [blame] | 4707 | /* At this staged, HTTP analysis is finished */ |
| 4708 | s->req.analysers &= AN_REQ_FLT_END; |
| 4709 | s->req.analyse_exp = TICK_ETERNITY; |
| 4710 | |
| 4711 | s->res.analysers &= AN_RES_FLT_END; |
| 4712 | s->res.analyse_exp = TICK_ETERNITY; |
| 4713 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4714 | channel_auto_read(&s->req); |
| 4715 | channel_abort(&s->req); |
| 4716 | channel_auto_close(&s->req); |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4717 | channel_htx_erase(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4718 | channel_auto_read(&s->res); |
| 4719 | channel_auto_close(&s->res); |
| 4720 | channel_shutr_now(&s->res); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4721 | } |
| 4722 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4723 | struct http_reply *http_error_message(struct stream *s) |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4724 | { |
| 4725 | const int msgnum = http_get_status_idx(s->txn->status); |
| 4726 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4727 | if (s->txn->http_reply) |
| 4728 | return s->txn->http_reply; |
| 4729 | else if (s->be->replies[msgnum]) |
| 4730 | return s->be->replies[msgnum]; |
| 4731 | else if (strm_fe(s)->replies[msgnum]) |
| 4732 | return strm_fe(s)->replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4733 | else |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4734 | return &http_err_replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4735 | } |
| 4736 | |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4737 | /* Produces an HTX message from an http reply. Depending on the http reply type, |
| 4738 | * a, errorfile, an raw file or a log-format string is used. On success, it |
| 4739 | * returns 0. If an error occurs -1 is returned. If it fails, this function only |
| 4740 | * exits. It is the caller responsibility to do the cleanup. |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4741 | */ |
Christopher Faulet | ae43b6c | 2020-05-27 15:24:22 +0200 | [diff] [blame] | 4742 | 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] | 4743 | { |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4744 | struct buffer *errmsg; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4745 | struct htx_sl *sl; |
| 4746 | struct buffer *body = NULL; |
| 4747 | const char *status, *reason, *clen, *ctype; |
| 4748 | unsigned int slflags; |
| 4749 | int ret = 0; |
| 4750 | |
Christopher Faulet | e29a97e | 2020-05-14 14:49:25 +0200 | [diff] [blame] | 4751 | /* |
| 4752 | * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so |
| 4753 | * |
| 4754 | * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled |
| 4755 | * as no payload if NULL. the TXN status code is set with the status |
| 4756 | * of the original reply. |
| 4757 | */ |
| 4758 | |
| 4759 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4760 | if (reply->body.reply) |
| 4761 | reply = reply->body.reply; |
| 4762 | } |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4763 | if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) { |
| 4764 | /* get default error message */ |
| 4765 | if (reply == s->txn->http_reply) |
| 4766 | s->txn->http_reply = NULL; |
| 4767 | reply = http_error_message(s); |
| 4768 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4769 | if (reply->body.reply) |
| 4770 | reply = reply->body.reply; |
| 4771 | } |
| 4772 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4773 | |
| 4774 | if (reply->type == HTTP_REPLY_ERRMSG) { |
| 4775 | /* implicit or explicit error message*/ |
| 4776 | errmsg = reply->body.errmsg; |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4777 | if (errmsg && !b_is_null(errmsg)) { |
Christopher Faulet | 2056736 | 2020-05-15 14:52:49 +0200 | [diff] [blame] | 4778 | if (!htx_copy_msg(htx, errmsg)) |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4779 | goto fail; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4780 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4781 | } |
| 4782 | else { |
| 4783 | /* no payload, file or log-format string */ |
| 4784 | if (reply->type == HTTP_REPLY_RAW) { |
| 4785 | /* file */ |
| 4786 | body = &reply->body.obj; |
| 4787 | } |
| 4788 | else if (reply->type == HTTP_REPLY_LOGFMT) { |
| 4789 | /* log-format string */ |
| 4790 | body = alloc_trash_chunk(); |
| 4791 | if (!body) |
| 4792 | goto fail_alloc; |
| 4793 | body->data = build_logline(s, body->area, body->size, &reply->body.fmt); |
| 4794 | } |
| 4795 | /* else no payload */ |
| 4796 | |
| 4797 | status = ultoa(reply->status); |
| 4798 | reason = http_get_reason(reply->status); |
| 4799 | slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN); |
| 4800 | if (!body || !b_data(body)) |
| 4801 | slflags |= HTX_SL_F_BODYLESS; |
| 4802 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4803 | if (!sl) |
| 4804 | goto fail; |
| 4805 | sl->info.res.status = reply->status; |
| 4806 | |
| 4807 | clen = (body ? ultoa(b_data(body)) : "0"); |
| 4808 | ctype = reply->ctype; |
| 4809 | |
| 4810 | if (!LIST_ISEMPTY(&reply->hdrs)) { |
| 4811 | struct http_reply_hdr *hdr; |
| 4812 | struct buffer *value = alloc_trash_chunk(); |
| 4813 | |
| 4814 | if (!value) |
| 4815 | goto fail; |
| 4816 | |
| 4817 | list_for_each_entry(hdr, &reply->hdrs, list) { |
| 4818 | chunk_reset(value); |
| 4819 | value->data = build_logline(s, value->area, value->size, &hdr->value); |
| 4820 | if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) { |
| 4821 | free_trash_chunk(value); |
| 4822 | goto fail; |
| 4823 | } |
| 4824 | chunk_reset(value); |
| 4825 | } |
| 4826 | free_trash_chunk(value); |
| 4827 | } |
| 4828 | |
| 4829 | if (!htx_add_header(htx, ist("content-length"), ist(clen)) || |
| 4830 | (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) || |
| 4831 | !htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4832 | (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] | 4833 | goto fail; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4834 | |
| 4835 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4836 | } |
| 4837 | |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4838 | leave: |
| 4839 | if (reply->type == HTTP_REPLY_LOGFMT) |
| 4840 | free_trash_chunk(body); |
| 4841 | return ret; |
| 4842 | |
| 4843 | fail_alloc: |
| 4844 | if (!(s->flags & SF_ERR_MASK)) |
| 4845 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4846 | /* fall through */ |
| 4847 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4848 | ret = -1; |
| 4849 | goto leave; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4850 | } |
| 4851 | |
| 4852 | /* 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] | 4853 | * occurs -1 is returned and the response channel is truncated, removing this |
| 4854 | * way the faulty reply. This function may fail when the reply is formatted |
| 4855 | * (http_reply_to_htx) or when the reply is forwarded |
| 4856 | * (http_forward_proxy_resp). On the last case, it is because a |
| 4857 | * http-after-response rule fails. |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4858 | */ |
| 4859 | int http_reply_message(struct stream *s, struct http_reply *reply) |
| 4860 | { |
| 4861 | struct channel *res = &s->res; |
| 4862 | struct htx *htx = htx_from_buf(&res->buf); |
| 4863 | |
| 4864 | if (s->txn->status == -1) |
| 4865 | s->txn->status = reply->status; |
| 4866 | channel_htx_truncate(res, htx); |
| 4867 | |
| 4868 | if (http_reply_to_htx(s, htx, reply) == -1) |
| 4869 | goto fail; |
| 4870 | |
| 4871 | htx_to_buf(htx, &s->res.buf); |
| 4872 | if (!http_forward_proxy_resp(s, 1)) |
| 4873 | goto fail; |
| 4874 | return 0; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4875 | |
| 4876 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4877 | channel_htx_truncate(res, htx); |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4878 | if (!(s->flags & SF_ERR_MASK)) |
| 4879 | s->flags |= SF_ERR_PRXCOND; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4880 | return -1; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4881 | } |
| 4882 | |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4883 | /* Return the error message corresponding to si->err_type. It is assumed |
| 4884 | * that the server side is closed. Note that err_type is actually a |
| 4885 | * bitmask, where almost only aborts may be cumulated with other |
| 4886 | * values. We consider that aborted operations are more important |
| 4887 | * than timeouts or errors due to the fact that nobody else in the |
| 4888 | * logs might explain incomplete retries. All others should avoid |
| 4889 | * being cumulated. It should normally not be possible to have multiple |
| 4890 | * aborts at once, but just in case, the first one in sequence is reported. |
| 4891 | * Note that connection errors appearing on the second request of a keep-alive |
| 4892 | * connection are not reported since this allows the client to retry. |
| 4893 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4894 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4895 | { |
| 4896 | int err_type = si->err_type; |
| 4897 | |
| 4898 | /* set s->txn->status for http_error_message(s) */ |
| 4899 | s->txn->status = 503; |
| 4900 | |
| 4901 | if (err_type & SI_ET_QUEUE_ABRT) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4902 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
| 4903 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4904 | else if (err_type & SI_ET_CONN_ABRT) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4905 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
| 4906 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4907 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4908 | else if (err_type & SI_ET_QUEUE_TO) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4909 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
| 4910 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4911 | else if (err_type & SI_ET_QUEUE_ERR) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4912 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
| 4913 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4914 | else if (err_type & SI_ET_CONN_TO) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4915 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
| 4916 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4917 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4918 | else if (err_type & SI_ET_CONN_ERR) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4919 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
| 4920 | (s->flags & SF_SRV_REUSED) ? NULL : |
| 4921 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4922 | else if (err_type & SI_ET_CONN_RES) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4923 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
| 4924 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4925 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4926 | else { /* SI_ET_CONN_OTHER and others */ |
| 4927 | s->txn->status = 500; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4928 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
| 4929 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4930 | } |
| 4931 | } |
| 4932 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4933 | |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4934 | /* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0 |
| 4935 | * on success and -1 on error. |
| 4936 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4937 | 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] | 4938 | { |
| 4939 | /* If we have HTTP/1.1 message with a body and Expect: 100-continue, |
| 4940 | * then we must send an HTTP/1.1 100 Continue intermediate response. |
| 4941 | */ |
| 4942 | if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) && |
| 4943 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
| 4944 | struct ist hdr = { .ptr = "Expect", .len = 6 }; |
| 4945 | struct http_hdr_ctx ctx; |
| 4946 | |
| 4947 | ctx.blk = NULL; |
| 4948 | /* Expect is allowed in 1.1, look for it */ |
| 4949 | if (http_find_header(htx, hdr, &ctx, 0) && |
| 4950 | unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4951 | if (http_reply_100_continue(s) == -1) |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4952 | return -1; |
| 4953 | http_remove_header(htx, &ctx); |
| 4954 | } |
| 4955 | } |
| 4956 | return 0; |
| 4957 | } |
| 4958 | |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4959 | /* Send a 100-Continue response to the client. It returns 0 on success and -1 |
| 4960 | * on error. The response channel is updated accordingly. |
| 4961 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4962 | static int http_reply_100_continue(struct stream *s) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4963 | { |
| 4964 | struct channel *res = &s->res; |
| 4965 | struct htx *htx = htx_from_buf(&res->buf); |
| 4966 | struct htx_sl *sl; |
| 4967 | unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11| |
| 4968 | HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4969 | |
| 4970 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4971 | ist("HTTP/1.1"), ist("100"), ist("Continue")); |
| 4972 | if (!sl) |
| 4973 | goto fail; |
| 4974 | sl->info.res.status = 100; |
| 4975 | |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 4976 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4977 | goto fail; |
| 4978 | |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4979 | if (!http_forward_proxy_resp(s, 0)) |
| 4980 | goto fail; |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4981 | return 0; |
| 4982 | |
| 4983 | fail: |
| 4984 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4985 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4986 | channel_htx_truncate(res, htx); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4987 | return -1; |
| 4988 | } |
| 4989 | |
Christopher Faulet | 12c51e2 | 2018-11-28 15:59:42 +0100 | [diff] [blame] | 4990 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4991 | /* |
| 4992 | * Capture headers from message <htx> according to header list <cap_hdr>, and |
| 4993 | * fill the <cap> pointers appropriately. |
| 4994 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4995 | 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] | 4996 | { |
| 4997 | struct cap_hdr *h; |
| 4998 | int32_t pos; |
| 4999 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5000 | 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] | 5001 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5002 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5003 | struct ist n, v; |
| 5004 | |
| 5005 | if (type == HTX_BLK_EOH) |
| 5006 | break; |
| 5007 | if (type != HTX_BLK_HDR) |
| 5008 | continue; |
| 5009 | |
| 5010 | n = htx_get_blk_name(htx, blk); |
| 5011 | |
| 5012 | for (h = cap_hdr; h; h = h->next) { |
| 5013 | if (h->namelen && (h->namelen == n.len) && |
| 5014 | (strncasecmp(n.ptr, h->name, h->namelen) == 0)) { |
| 5015 | if (cap[h->index] == NULL) |
| 5016 | cap[h->index] = |
| 5017 | pool_alloc(h->pool); |
| 5018 | |
| 5019 | if (cap[h->index] == NULL) { |
| 5020 | ha_alert("HTTP capture : out of memory.\n"); |
| 5021 | break; |
| 5022 | } |
| 5023 | |
| 5024 | v = htx_get_blk_value(htx, blk); |
| 5025 | if (v.len > h->len) |
| 5026 | v.len = h->len; |
| 5027 | |
| 5028 | memcpy(cap[h->index], v.ptr, v.len); |
| 5029 | cap[h->index][v.len]=0; |
| 5030 | } |
| 5031 | } |
| 5032 | } |
| 5033 | } |
| 5034 | |
Christopher Faulet | 0b6bdc5 | 2018-10-24 11:05:36 +0200 | [diff] [blame] | 5035 | /* Delete a value in a header between delimiters <from> and <next>. The header |
| 5036 | * itself is delimited by <start> and <end> pointers. The number of characters |
| 5037 | * displaced is returned, and the pointer to the first delimiter is updated if |
| 5038 | * required. The function tries as much as possible to respect the following |
| 5039 | * principles : |
| 5040 | * - replace <from> delimiter by the <next> one unless <from> points to <start>, |
| 5041 | * in which case <next> is simply removed |
| 5042 | * - set exactly one space character after the new first delimiter, unless there |
| 5043 | * are not enough characters in the block being moved to do so. |
| 5044 | * - remove unneeded spaces before the previous delimiter and after the new |
| 5045 | * one. |
| 5046 | * |
| 5047 | * It is the caller's responsibility to ensure that : |
| 5048 | * - <from> points to a valid delimiter or <start> ; |
| 5049 | * - <next> points to a valid delimiter or <end> ; |
| 5050 | * - there are non-space chars before <from>. |
| 5051 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5052 | 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] | 5053 | { |
| 5054 | char *prev = *from; |
| 5055 | |
| 5056 | if (prev == start) { |
| 5057 | /* We're removing the first value. eat the semicolon, if <next> |
| 5058 | * is lower than <end> */ |
| 5059 | if (next < end) |
| 5060 | next++; |
| 5061 | |
| 5062 | while (next < end && HTTP_IS_SPHT(*next)) |
| 5063 | next++; |
| 5064 | } |
| 5065 | else { |
| 5066 | /* Remove useless spaces before the old delimiter. */ |
| 5067 | while (HTTP_IS_SPHT(*(prev-1))) |
| 5068 | prev--; |
| 5069 | *from = prev; |
| 5070 | |
| 5071 | /* copy the delimiter and if possible a space if we're |
| 5072 | * not at the end of the line. |
| 5073 | */ |
| 5074 | if (next < end) { |
| 5075 | *prev++ = *next++; |
| 5076 | if (prev + 1 < next) |
| 5077 | *prev++ = ' '; |
| 5078 | while (next < end && HTTP_IS_SPHT(*next)) |
| 5079 | next++; |
| 5080 | } |
| 5081 | } |
| 5082 | memmove(prev, next, end - next); |
| 5083 | return (prev - next); |
| 5084 | } |
| 5085 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5086 | |
| 5087 | /* 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] | 5088 | * 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] | 5089 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5090 | 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] | 5091 | { |
| 5092 | struct ist dst = ist2(str, 0); |
| 5093 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5094 | if (istcat(&dst, htx_sl_req_meth(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5095 | goto end; |
| 5096 | if (dst.len + 1 > len) |
| 5097 | goto end; |
| 5098 | dst.ptr[dst.len++] = ' '; |
| 5099 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5100 | if (istcat(&dst, htx_sl_req_uri(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5101 | goto end; |
| 5102 | if (dst.len + 1 > len) |
| 5103 | goto end; |
| 5104 | dst.ptr[dst.len++] = ' '; |
| 5105 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5106 | istcat(&dst, htx_sl_req_vsn(sl), len); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5107 | end: |
| 5108 | return dst.len; |
| 5109 | } |
| 5110 | |
| 5111 | /* |
| 5112 | * Print a debug line with a start line. |
| 5113 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5114 | 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] | 5115 | { |
| 5116 | struct session *sess = strm_sess(s); |
| 5117 | int max; |
| 5118 | |
| 5119 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 5120 | dir, |
Willy Tarreau | 6618259 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 5121 | objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1, |
| 5122 | objt_cs(s->si[1].end) ? (unsigned short)__objt_cs(s->si[1].end)->conn->handle.fd : -1); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5123 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5124 | max = HTX_SL_P1_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5125 | UBOUND(max, trash.size - trash.data - 3); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5126 | chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5127 | trash.area[trash.data++] = ' '; |
| 5128 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5129 | max = HTX_SL_P2_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5130 | UBOUND(max, trash.size - trash.data - 2); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5131 | chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5132 | trash.area[trash.data++] = ' '; |
| 5133 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5134 | max = HTX_SL_P3_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5135 | UBOUND(max, trash.size - trash.data - 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5136 | chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5137 | trash.area[trash.data++] = '\n'; |
| 5138 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 5139 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5140 | } |
| 5141 | |
| 5142 | /* |
| 5143 | * Print a debug line with a header. |
| 5144 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5145 | 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] | 5146 | { |
| 5147 | struct session *sess = strm_sess(s); |
| 5148 | int max; |
| 5149 | |
| 5150 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 5151 | dir, |
Willy Tarreau | 6618259 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 5152 | objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1, |
| 5153 | objt_cs(s->si[1].end) ? (unsigned short)__objt_cs(s->si[1].end)->conn->handle.fd : -1); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5154 | |
| 5155 | max = n.len; |
| 5156 | UBOUND(max, trash.size - trash.data - 3); |
| 5157 | chunk_memcat(&trash, n.ptr, max); |
| 5158 | trash.area[trash.data++] = ':'; |
| 5159 | trash.area[trash.data++] = ' '; |
| 5160 | |
| 5161 | max = v.len; |
| 5162 | UBOUND(max, trash.size - trash.data - 1); |
| 5163 | chunk_memcat(&trash, v.ptr, max); |
| 5164 | trash.area[trash.data++] = '\n'; |
| 5165 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 5166 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5167 | } |
| 5168 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5169 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 5170 | * In case of allocation failure, everything allocated is freed and NULL is |
| 5171 | * returned. Otherwise the new transaction is assigned to the stream and |
| 5172 | * returned. |
| 5173 | */ |
| 5174 | struct http_txn *http_alloc_txn(struct stream *s) |
| 5175 | { |
| 5176 | struct http_txn *txn = s->txn; |
| 5177 | |
| 5178 | if (txn) |
| 5179 | return txn; |
| 5180 | |
| 5181 | txn = pool_alloc(pool_head_http_txn); |
| 5182 | if (!txn) |
| 5183 | return txn; |
| 5184 | |
| 5185 | s->txn = txn; |
| 5186 | return txn; |
| 5187 | } |
| 5188 | |
| 5189 | void http_txn_reset_req(struct http_txn *txn) |
| 5190 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 5191 | txn->req.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5192 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 5193 | } |
| 5194 | |
| 5195 | void http_txn_reset_res(struct http_txn *txn) |
| 5196 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 5197 | txn->rsp.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5198 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 5199 | } |
| 5200 | |
| 5201 | /* |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5202 | * Create and initialize a new HTTP transaction for stream <s>. This should be |
| 5203 | * used before processing any new request. It returns the transaction or NLULL |
| 5204 | * on error. |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5205 | */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5206 | struct http_txn *http_create_txn(struct stream *s) |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5207 | { |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5208 | struct http_txn *txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5209 | struct conn_stream *cs = objt_cs(s->si[0].end); |
| 5210 | |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5211 | txn = pool_alloc(pool_head_http_txn); |
| 5212 | if (!txn) |
| 5213 | return NULL; |
| 5214 | s->txn = txn; |
| 5215 | |
Christopher Faulet | db7ced0 | 2022-06-22 17:12:05 +0200 | [diff] [blame] | 5216 | txn->meth = HTTP_METH_OTHER; |
Christopher Faulet | da831fa | 2020-10-06 17:58:43 +0200 | [diff] [blame] | 5217 | txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) ? TX_NOT_FIRST : 0); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5218 | txn->status = -1; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 5219 | txn->http_reply = NULL; |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 5220 | write_u32(txn->cache_hash, 0); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5221 | |
| 5222 | txn->cookie_first_date = 0; |
| 5223 | txn->cookie_last_date = 0; |
| 5224 | |
| 5225 | txn->srv_cookie = NULL; |
| 5226 | txn->cli_cookie = NULL; |
| 5227 | txn->uri = NULL; |
| 5228 | |
| 5229 | http_txn_reset_req(txn); |
| 5230 | http_txn_reset_res(txn); |
| 5231 | |
| 5232 | txn->req.chn = &s->req; |
| 5233 | txn->rsp.chn = &s->res; |
| 5234 | |
| 5235 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
| 5236 | |
Aurelien DARRAGON | 32e8e9b | 2022-11-18 09:17:29 +0100 | [diff] [blame] | 5237 | /* here we don't want to re-initialize s->vars_txn and s->vars_reqres |
| 5238 | * variable lists, because they were already initialized upon stream |
| 5239 | * creation in stream_new(), and thus may already contain some variables |
| 5240 | */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5241 | |
| 5242 | return txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5243 | } |
| 5244 | |
| 5245 | /* to be used at the end of a transaction */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5246 | void http_destroy_txn(struct stream *s) |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5247 | { |
| 5248 | struct http_txn *txn = s->txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5249 | |
| 5250 | /* these ones will have been dynamically allocated */ |
| 5251 | pool_free(pool_head_requri, txn->uri); |
| 5252 | pool_free(pool_head_capture, txn->cli_cookie); |
| 5253 | pool_free(pool_head_capture, txn->srv_cookie); |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5254 | pool_free(pool_head_uniqueid, s->unique_id.ptr); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5255 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5256 | s->unique_id = IST_NULL; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5257 | txn->uri = NULL; |
| 5258 | txn->srv_cookie = NULL; |
| 5259 | txn->cli_cookie = NULL; |
| 5260 | |
Christopher Faulet | 5939925 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 5261 | if (!LIST_ISEMPTY(&s->vars_txn.head)) |
| 5262 | vars_prune(&s->vars_txn, s->sess, s); |
| 5263 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 5264 | vars_prune(&s->vars_reqres, s->sess, s); |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5265 | |
| 5266 | pool_free(pool_head_http_txn, txn); |
| 5267 | s->txn = NULL; |
Christopher Faulet | 5939925 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 5268 | } |
| 5269 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5270 | |
| 5271 | DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5272 | |
Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 5273 | __attribute__((constructor)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5274 | static void __http_protocol_init(void) |
Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 5275 | { |
| 5276 | } |
| 5277 | |
| 5278 | |
| 5279 | /* |
| 5280 | * Local variables: |
| 5281 | * c-indent-level: 8 |
| 5282 | * c-basic-offset: 8 |
| 5283 | * End: |
| 5284 | */ |