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