Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
| 4 | * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Willy Tarreau | dcc048a | 2020-06-04 19:11:43 +0200 | [diff] [blame] | 13 | #include <haproxy/acl.h> |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 14 | #include <haproxy/action-t.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 15 | #include <haproxy/api.h> |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 16 | #include <haproxy/applet.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 17 | #include <haproxy/backend.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 18 | #include <haproxy/base64.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 19 | #include <haproxy/capture-t.h> |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 20 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 21 | #include <haproxy/channel.h> |
Willy Tarreau | 4aa573d | 2020-06-04 18:21:56 +0200 | [diff] [blame] | 22 | #include <haproxy/check.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 23 | #include <haproxy/connection.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 24 | #include <haproxy/errors.h> |
Willy Tarreau | c7babd8 | 2020-06-04 21:29:29 +0200 | [diff] [blame] | 25 | #include <haproxy/filters.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 26 | #include <haproxy/http.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 27 | #include <haproxy/http_ana.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 28 | #include <haproxy/http_htx.h> |
Aurelien DARRAGON | b2bb925 | 2022-12-28 15:37:57 +0100 | [diff] [blame] | 29 | #include <haproxy/http_ext.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 30 | #include <haproxy/htx.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 31 | #include <haproxy/log.h> |
Willy Tarreau | 6131d6a | 2020-06-02 16:48:09 +0200 | [diff] [blame] | 32 | #include <haproxy/net_helper.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 33 | #include <haproxy/proxy.h> |
Willy Tarreau | 7cd8b6e | 2020-06-02 17:32:26 +0200 | [diff] [blame] | 34 | #include <haproxy/regex.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 35 | #include <haproxy/sc_strm.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 36 | #include <haproxy/server-t.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 37 | #include <haproxy/stats.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 38 | #include <haproxy/stconn.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 39 | #include <haproxy/stream.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 40 | #include <haproxy/trace.h> |
Willy Tarreau | 8c42b8a | 2020-06-04 19:27:34 +0200 | [diff] [blame] | 41 | #include <haproxy/uri_auth-t.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 42 | #include <haproxy/vars.h> |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 43 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 44 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 45 | #define TRACE_SOURCE &trace_strm |
| 46 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 47 | extern const char *stat_status_codes[]; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 48 | |
Willy Tarreau | ff88270 | 2021-04-10 17:23:00 +0200 | [diff] [blame] | 49 | struct pool_head *pool_head_requri __read_mostly = NULL; |
| 50 | struct pool_head *pool_head_capture __read_mostly = NULL; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 51 | |
| 52 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 53 | static void http_end_request(struct stream *s); |
| 54 | static void http_end_response(struct stream *s); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 55 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 56 | static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr); |
| 57 | static int http_del_hdr_value(char *start, char *end, char **from, char *next); |
| 58 | 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] | 59 | static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl); |
| 60 | 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] | 61 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 62 | static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s); |
| 63 | static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s); |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 64 | 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] | 65 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 66 | static void http_manage_client_side_cookies(struct stream *s, struct channel *req); |
| 67 | 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] | 68 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 69 | static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend); |
| 70 | static int http_handle_stats(struct stream *s, struct channel *req); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 71 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 72 | static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg); |
| 73 | static int http_reply_100_continue(struct stream *s); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 74 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 75 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 76 | * processing can continue on next analysers, or zero if it either needs more |
| 77 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 78 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers |
| 79 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 80 | * abort. |
| 81 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 82 | 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] | 83 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 84 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 85 | /* |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 86 | * We will analyze a complete HTTP request to check the its syntax. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 87 | * |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 88 | * Once the start line and all headers are received, we may perform a |
| 89 | * capture of the error (if any), and we will set a few fields. We also |
| 90 | * check for monitor-uri, logging and finally headers capture. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 91 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 92 | struct session *sess = s->sess; |
| 93 | struct http_txn *txn = s->txn; |
| 94 | struct http_msg *msg = &txn->req; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 95 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 96 | struct htx_sl *sl; |
Frédéric Lécaille | 9969adb | 2023-01-18 11:52:21 +0100 | [diff] [blame] | 97 | char http_ver; |
| 98 | int len; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 99 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 100 | 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] | 101 | |
Christopher Faulet | da46a0d | 2021-01-21 17:32:58 +0100 | [diff] [blame] | 102 | if (unlikely(!IS_HTX_STRM(s))) { |
| 103 | /* It is only possible when a TCP stream is upgrade to HTTP. |
| 104 | * There is a transition period during which there is no |
| 105 | * data. The stream is still in raw mode and SF_IGNORE flag is |
| 106 | * still set. When this happens, the new mux is responsible to |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 107 | * handle all errors. Thus we may leave immediately. |
Christopher Faulet | da46a0d | 2021-01-21 17:32:58 +0100 | [diff] [blame] | 108 | */ |
| 109 | BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 110 | |
Christopher Faulet | 97b3a61 | 2021-03-15 17:10:12 +0100 | [diff] [blame] | 111 | /* Don't connect for now */ |
| 112 | channel_dont_connect(req); |
| 113 | |
| 114 | /* A SHUTR at this stage means we are performing a "destructive" |
| 115 | * HTTP upgrade (TCP>H2). In this case, we can leave. |
| 116 | */ |
| 117 | if (req->flags & CF_SHUTR) { |
| 118 | s->logs.logwait = 0; |
| 119 | s->logs.level = 0; |
| 120 | channel_abort(&s->req); |
| 121 | channel_abort(&s->res); |
| 122 | req->analysers &= AN_REQ_FLT_END; |
| 123 | req->analyse_exp = TICK_ETERNITY; |
| 124 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s); |
| 125 | return 1; |
| 126 | } |
Christopher Faulet | da46a0d | 2021-01-21 17:32:58 +0100 | [diff] [blame] | 127 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | htx = htxbuf(&req->buf); |
Frédéric Lécaille | 9969adb | 2023-01-18 11:52:21 +0100 | [diff] [blame] | 132 | sl = http_get_stline(htx); |
| 133 | len = HTX_SL_REQ_VLEN(sl); |
| 134 | if (len < 6) { |
| 135 | http_ver = 0; |
| 136 | } |
| 137 | else { |
| 138 | char *ptr; |
| 139 | |
| 140 | ptr = HTX_SL_REQ_VPTR(sl); |
| 141 | http_ver = ptr[5] - '0'; |
| 142 | } |
Christopher Faulet | 8bebd2f | 2020-10-06 17:54:56 +0200 | [diff] [blame] | 143 | |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 144 | /* Parsing errors are caught here */ |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 145 | if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) { |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 146 | stream_inc_http_req_ctr(s); |
Frédéric Lécaille | 9969adb | 2023-01-18 11:52:21 +0100 | [diff] [blame] | 147 | proxy_inc_fe_req_ctr(sess->listener, sess->fe, http_ver); |
Christopher Faulet | bf7175f | 2021-02-10 14:58:01 +0100 | [diff] [blame] | 148 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
| 149 | stream_inc_http_err_ctr(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 150 | goto return_bad_req; |
Christopher Faulet | bf7175f | 2021-02-10 14:58:01 +0100 | [diff] [blame] | 151 | } |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 152 | else |
| 153 | goto return_int_err; |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 156 | /* 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] | 157 | s->srv_error = http_return_srv_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 158 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 159 | msg->msg_state = HTTP_MSG_BODY; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 160 | stream_inc_http_req_ctr(s); |
Frédéric Lécaille | 9969adb | 2023-01-18 11:52:21 +0100 | [diff] [blame] | 161 | proxy_inc_fe_req_ctr(sess->listener, sess->fe, http_ver); /* one more valid request for this FE */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 162 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 163 | /* kill the pending keep-alive timeout */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 164 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 165 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 166 | BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL); |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 167 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 168 | /* 0: we might have to print this header in debug mode */ |
| 169 | if (unlikely((global.mode & MODE_DEBUG) && |
| 170 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 171 | int32_t pos; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 172 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 173 | http_debug_stline("clireq", s, sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 174 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 175 | 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] | 176 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 177 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 178 | |
| 179 | if (type == HTX_BLK_EOH) |
| 180 | break; |
| 181 | if (type != HTX_BLK_HDR) |
| 182 | continue; |
| 183 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 184 | http_debug_hdr("clihdr", s, |
| 185 | htx_get_blk_name(htx, blk), |
| 186 | htx_get_blk_value(htx, blk)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 187 | } |
| 188 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 189 | |
| 190 | /* |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 191 | * 1: identify the method and the version. Also set HTTP flags |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 192 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 193 | txn->meth = sl->info.req.meth; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 194 | if (sl->flags & HTX_SL_F_VER_11) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 195 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 196 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 197 | if (sl->flags & HTX_SL_F_CLEN) |
| 198 | msg->flags |= HTTP_MSGF_CNT_LEN; |
| 199 | else if (sl->flags & HTX_SL_F_CHNK) |
| 200 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Christopher Faulet | b2db4fa | 2018-11-27 16:51:09 +0100 | [diff] [blame] | 201 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 202 | msg->flags |= HTTP_MSGF_BODYLESS; |
Christopher Faulet | 576c358 | 2021-01-08 15:53:01 +0100 | [diff] [blame] | 203 | if (sl->flags & HTX_SL_F_CONN_UPG) |
| 204 | msg->flags |= HTTP_MSGF_CONN_UPG; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 205 | |
| 206 | /* we can make use of server redirect on GET and HEAD */ |
| 207 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 208 | s->flags |= SF_REDIRECTABLE; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 209 | 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] | 210 | /* PRI is reserved for the HTTP/2 preface */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 211 | goto return_bad_req; |
| 212 | } |
| 213 | |
| 214 | /* |
Christopher Faulet | 6072beb | 2020-02-18 15:34:58 +0100 | [diff] [blame] | 215 | * 2: check if the URI matches the monitor_uri. We have to do this for |
| 216 | * every request which gets in, because the monitor-uri is defined by |
| 217 | * the frontend. If the monitor-uri starts with a '/', the matching is |
| 218 | * done against the request's path. Otherwise, the request's uri is |
| 219 | * used. It is a workaround to let HTTP/2 health-checks work as |
| 220 | * expected. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 221 | */ |
Tim Duesterhus | 4b1fcaa | 2022-03-05 00:52:40 +0100 | [diff] [blame] | 222 | if (unlikely(isttest(sess->fe->monitor_uri))) { |
| 223 | const struct ist monitor_uri = sess->fe->monitor_uri; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 224 | struct http_uri_parser parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 225 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 226 | if ((istptr(monitor_uri)[0] == '/' && |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 227 | isteq(http_parse_path(&parser), monitor_uri)) || |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 228 | isteq(htx_sl_req_uri(sl), monitor_uri)) { |
| 229 | /* |
| 230 | * We have found the monitor URI |
| 231 | */ |
| 232 | struct acl_cond *cond; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 233 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 234 | s->flags |= SF_MONITOR; |
| 235 | _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 236 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 237 | /* Check if we want to fail this monitor request or not */ |
| 238 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
| 239 | int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 240 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 241 | ret = acl_pass(ret); |
| 242 | if (cond->pol == ACL_COND_UNLESS) |
| 243 | ret = !ret; |
| 244 | |
| 245 | if (ret) { |
| 246 | /* we fail this request, let's return 503 service unavail */ |
| 247 | txn->status = 503; |
| 248 | if (!(s->flags & SF_ERR_MASK)) |
| 249 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
| 250 | goto return_prx_cond; |
| 251 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 252 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 253 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 254 | /* nothing to fail, let's reply normally */ |
| 255 | txn->status = 200; |
| 256 | if (!(s->flags & SF_ERR_MASK)) |
| 257 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
| 258 | goto return_prx_cond; |
| 259 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /* |
| 263 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 264 | * Note: we cannot log anymore if the request has been |
| 265 | * classified as invalid. |
| 266 | */ |
| 267 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 268 | /* we have a complete HTTP request that we must log */ |
| 269 | if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 270 | size_t len; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 271 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 272 | 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] | 273 | txn->uri[len] = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 274 | |
| 275 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
| 276 | s->do_log(s); |
| 277 | } else { |
| 278 | ha_alert("HTTP logging : out of memory.\n"); |
| 279 | } |
| 280 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 281 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 282 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 283 | * we have what looks like a proxied connection instead of a connection, |
| 284 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 285 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 286 | * happen to do that despite being non-standard :-( |
| 287 | * We consider that a request not beginning with either '/' or '*' is |
| 288 | * a proxied connection, which covers both "scheme://location" and |
| 289 | * CONNECT ip:port. |
| 290 | */ |
| 291 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 292 | *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*') |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 293 | txn->flags |= TX_USE_PX_CONN; |
| 294 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 295 | /* 5: we may need to capture headers */ |
| 296 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 297 | http_capture_headers(htx, s->req_cap, sess->fe->req_cap); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 298 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 299 | /* we may have to wait for the request's body */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 300 | if (s->be->options & PR_O_WREQ_BODY) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 301 | req->analysers |= AN_REQ_HTTP_BODY; |
| 302 | |
| 303 | /* |
| 304 | * RFC7234#4: |
| 305 | * A cache MUST write through requests with methods |
| 306 | * that are unsafe (Section 4.2.1 of [RFC7231]) to |
| 307 | * the origin server; i.e., a cache is not allowed |
| 308 | * to generate a reply to such a request before |
| 309 | * having forwarded the request and having received |
| 310 | * a corresponding response. |
| 311 | * |
| 312 | * RFC7231#4.2.1: |
| 313 | * Of the request methods defined by this |
| 314 | * specification, the GET, HEAD, OPTIONS, and TRACE |
| 315 | * methods are defined to be safe. |
| 316 | */ |
| 317 | if (likely(txn->meth == HTTP_METH_GET || |
| 318 | txn->meth == HTTP_METH_HEAD || |
| 319 | txn->meth == HTTP_METH_OPTIONS || |
| 320 | txn->meth == HTTP_METH_TRACE)) |
| 321 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 322 | |
| 323 | /* end of job, return OK */ |
| 324 | req->analysers &= ~an_bit; |
| 325 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 326 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 327 | 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] | 328 | return 1; |
| 329 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 330 | return_int_err: |
| 331 | txn->status = 500; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 332 | if (!(s->flags & SF_ERR_MASK)) |
| 333 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 334 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 335 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 336 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 337 | goto return_prx_cond; |
| 338 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 339 | return_bad_req: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 340 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 341 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 342 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 343 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 344 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 345 | |
| 346 | return_prx_cond: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 347 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 348 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 349 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 350 | DBG_TRACE_DEVEL("leaving on error", |
| 351 | 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] | 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | |
| 356 | /* This stream analyser runs all HTTP request processing which is common to |
| 357 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 358 | * reqadd, stats and redirects. This is performed for the designated proxy. |
| 359 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 360 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 361 | * error, ...). |
| 362 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 363 | 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] | 364 | { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 365 | struct list *def_rules, *rules; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 366 | struct session *sess = s->sess; |
| 367 | struct http_txn *txn = s->txn; |
| 368 | struct http_msg *msg = &txn->req; |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 369 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 370 | struct redirect_rule *rule; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 371 | enum rule_result verdict; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 372 | struct connection *conn = objt_conn(sess->origin); |
| 373 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 374 | 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] | 375 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 376 | htx = htxbuf(&req->buf); |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 377 | |
Christopher Faulet | 1907ccc | 2019-04-29 13:12:02 +0200 | [diff] [blame] | 378 | /* just in case we have some per-backend tracking. Only called the first |
| 379 | * execution of the analyser. */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 380 | if (!s->current_rule && !s->current_rule_list) |
Christopher Faulet | 1907ccc | 2019-04-29 13:12:02 +0200 | [diff] [blame] | 381 | stream_inc_be_http_req_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 382 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 383 | def_rules = ((px->defpx && (an_bit == AN_REQ_HTTP_PROCESS_FE || px != sess->fe)) ? &px->defpx->http_req_rules : NULL); |
| 384 | rules = &px->http_req_rules; |
| 385 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 386 | /* evaluate http-request rules */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 387 | if ((def_rules && !LIST_ISEMPTY(def_rules)) || !LIST_ISEMPTY(rules)) { |
| 388 | verdict = http_req_get_intercept_rule(px, def_rules, rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 389 | |
| 390 | switch (verdict) { |
| 391 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 392 | goto return_prx_yield; |
| 393 | |
| 394 | case HTTP_RULE_RES_CONT: |
| 395 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 396 | break; |
| 397 | |
| 398 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 399 | if (txn->flags & TX_CLTARPIT) |
| 400 | goto tarpit; |
| 401 | goto deny; |
| 402 | |
| 403 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 404 | goto return_prx_cond; |
| 405 | |
| 406 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
| 407 | goto done; |
| 408 | |
| 409 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 410 | goto return_bad_req; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 411 | |
| 412 | case HTTP_RULE_RES_ERROR: /* failed with a bad request */ |
| 413 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 417 | if (px->options2 & (PR_O2_RSTRICT_REQ_HDR_NAMES_BLK|PR_O2_RSTRICT_REQ_HDR_NAMES_DEL)) { |
| 418 | verdict = http_req_restrict_header_names(s, htx, px); |
| 419 | if (verdict == HTTP_RULE_RES_DENY) |
| 420 | goto deny; |
| 421 | } |
| 422 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 423 | if (conn && (conn->flags & CO_FL_EARLY_DATA) && |
Olivier Houchard | 220a26c | 2020-01-23 14:57:36 +0100 | [diff] [blame] | 424 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) { |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 425 | struct http_hdr_ctx ctx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 426 | |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 427 | ctx.blk = NULL; |
| 428 | if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) { |
| 429 | if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1")))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 430 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 431 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* OK at this stage, we know that the request was accepted according to |
| 435 | * the http-request rules, we can check for the stats. Note that the |
| 436 | * URI is detected *before* the req* rules in order not to be affected |
| 437 | * by a possible reqrep, while they are processed *after* so that a |
| 438 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 439 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 440 | if (!s->target && http_stats_check_uri(s, txn, px)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 441 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | a0b58b5 | 2022-05-27 08:33:53 +0200 | [diff] [blame] | 442 | if (unlikely(!sc_applet_create(s->scb, objt_applet(s->target)))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 443 | s->logs.tv_request = now; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 444 | if (!(s->flags & SF_ERR_MASK)) |
| 445 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 446 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /* parse the whole stats request and extract the relevant information */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 450 | http_handle_stats(s, req); |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 451 | verdict = http_req_get_intercept_rule(px, NULL, &px->uri_auth->http_req_rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 452 | /* not all actions implemented: deny, allow, auth */ |
| 453 | |
| 454 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 455 | goto deny; |
| 456 | |
| 457 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 458 | goto return_prx_cond; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 459 | |
| 460 | if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */ |
| 461 | goto return_bad_req; |
| 462 | |
| 463 | if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */ |
| 464 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 465 | } |
| 466 | |
Christopher Faulet | 2571bc6 | 2019-03-01 11:44:26 +0100 | [diff] [blame] | 467 | /* Proceed with the applets now. */ |
| 468 | if (unlikely(objt_applet(s->target))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 469 | 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] | 470 | _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 471 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 472 | if (http_handle_expect_hdr(s, htx, msg) == -1) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 473 | goto return_int_err; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 474 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 475 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 476 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 477 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 478 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 479 | if (HAS_FILTERS(s)) |
| 480 | req->analysers |= AN_REQ_FLT_HTTP_HDRS; |
| 481 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 482 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
| 483 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 484 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 485 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 486 | |
| 487 | req->flags |= CF_SEND_DONTWAIT; |
| 488 | s->flags |= SF_ASSIGNED; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 489 | goto done; |
| 490 | } |
| 491 | |
| 492 | /* check whether we have some ACLs set to redirect this request */ |
| 493 | list_for_each_entry(rule, &px->redirect_rules, list) { |
| 494 | if (rule->cond) { |
| 495 | int ret; |
| 496 | |
| 497 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 498 | ret = acl_pass(ret); |
| 499 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 500 | ret = !ret; |
| 501 | if (!ret) |
| 502 | continue; |
| 503 | } |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 504 | if (!http_apply_redirect_rule(rule, s, txn)) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 505 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 506 | goto done; |
| 507 | } |
| 508 | |
| 509 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 510 | * If this happens, then the data will not come immediately, so we must |
| 511 | * send all what we have without waiting. Note that due to the small gain |
| 512 | * in waiting for the body of the request, it's easier to simply put the |
| 513 | * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 514 | * itself once used. |
| 515 | */ |
| 516 | req->flags |= CF_SEND_DONTWAIT; |
| 517 | |
| 518 | done: /* done with this analyser, continue with next ones that the calling |
| 519 | * points will have set, if any. |
| 520 | */ |
| 521 | req->analyse_exp = TICK_ETERNITY; |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 522 | 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] | 523 | req->analysers &= ~an_bit; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 524 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 525 | 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] | 526 | return 1; |
| 527 | |
| 528 | tarpit: |
| 529 | /* Allow cookie logging |
| 530 | */ |
| 531 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 532 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 533 | |
| 534 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 535 | * which may be the same as the connect timeout if unspecified. |
| 536 | * If unset, then set it to zero because we really want it to |
| 537 | * eventually expire. We build the tarpit as an analyser. |
| 538 | */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 539 | channel_htx_erase(&s->req, htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 540 | |
| 541 | /* wipe the request out so that we can drop the connection early |
| 542 | * if the client closes first. |
| 543 | */ |
| 544 | channel_dont_connect(req); |
| 545 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 546 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
| 547 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 548 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 549 | if (!req->analyse_exp) |
| 550 | req->analyse_exp = tick_add(now_ms, 0); |
| 551 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 552 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 553 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 554 | _HA_ATOMIC_INC(&s->be->be_counters.denied_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 555 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 556 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 557 | goto done_without_exp; |
| 558 | |
| 559 | deny: /* this request was blocked (denied) */ |
| 560 | |
| 561 | /* Allow cookie logging |
| 562 | */ |
| 563 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 564 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 565 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 566 | s->logs.tv_request = now; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 567 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 568 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 569 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 570 | _HA_ATOMIC_INC(&s->be->be_counters.denied_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 571 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 572 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 573 | goto return_prx_err; |
| 574 | |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 575 | return_fail_rewrite: |
| 576 | if (!(s->flags & SF_ERR_MASK)) |
| 577 | s->flags |= SF_ERR_PRXCOND; |
| 578 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
| 579 | if (s->flags & SF_BE_ASSIGNED) |
| 580 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
| 581 | if (sess->listener && sess->listener->counters) |
| 582 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
| 583 | if (objt_server(s->target)) |
| 584 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
| 585 | /* fall through */ |
| 586 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 587 | return_int_err: |
| 588 | txn->status = 500; |
| 589 | if (!(s->flags & SF_ERR_MASK)) |
| 590 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 591 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 592 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 593 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 594 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 595 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 596 | goto return_prx_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 597 | |
| 598 | return_bad_req: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 599 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 600 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 601 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 602 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 603 | /* fall through */ |
| 604 | |
| 605 | return_prx_err: |
| 606 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 607 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 608 | |
| 609 | return_prx_cond: |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 610 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 611 | |
| 612 | req->analysers &= AN_REQ_FLT_END; |
| 613 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 614 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 615 | DBG_TRACE_DEVEL("leaving on error", |
| 616 | 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] | 617 | return 0; |
| 618 | |
| 619 | return_prx_yield: |
| 620 | channel_dont_connect(req); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 621 | DBG_TRACE_DEVEL("waiting for more data", |
| 622 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | /* This function performs all the processing enabled for the current request. |
| 627 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 628 | * needs more data, encounters an error, or wants to immediately abort the |
| 629 | * request. It relies on buffers flags, and updates s->req.analysers. |
| 630 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 631 | 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] | 632 | { |
| 633 | struct session *sess = s->sess; |
| 634 | struct http_txn *txn = s->txn; |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 635 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 636 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
| 637 | |
Christopher Faulet | 8bebd2f | 2020-10-06 17:54:56 +0200 | [diff] [blame] | 638 | 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] | 639 | |
| 640 | /* |
| 641 | * Right now, we know that we have processed the entire headers |
| 642 | * and that unwanted requests have been filtered out. We can do |
| 643 | * whatever we want with the remaining request. Also, now we |
| 644 | * may have separate values for ->fe, ->be. |
| 645 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 646 | htx = htxbuf(&req->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 647 | |
| 648 | /* |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 649 | * 7: Now we can work with the cookies. |
| 650 | * Note that doing so might move headers in the request, but |
| 651 | * the fields will stay coherent and the URI will not move. |
| 652 | * This should only be performed in the backend. |
| 653 | */ |
| 654 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 655 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 656 | |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 657 | /* 8: Generate unique ID if a "unique-id-format" is defined. |
| 658 | * |
| 659 | * A unique ID is generated even when it is not sent to ensure that the ID can make use of |
| 660 | * fetches only available in the HTTP request processing stage. |
| 661 | */ |
| 662 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 663 | 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] | 664 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 665 | if (!isttest(unique_id)) { |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 666 | if (!(s->flags & SF_ERR_MASK)) |
| 667 | s->flags |= SF_ERR_RESOURCE; |
| 668 | goto return_int_err; |
| 669 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 670 | |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 671 | /* send unique ID if a "unique-id-header" is defined */ |
Tim Duesterhus | 0643b0e | 2020-03-05 17:56:35 +0100 | [diff] [blame] | 672 | if (isttest(sess->fe->header_unique_id) && |
Tim Duesterhus | 7ad27d4 | 2022-05-18 00:22:15 +0200 | [diff] [blame] | 673 | unlikely(!http_add_header(htx, sess->fe->header_unique_id, unique_id))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 674 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 675 | } |
| 676 | |
Aurelien DARRAGON | b2e2ec5 | 2023-01-09 11:09:03 +0100 | [diff] [blame] | 677 | /* handle http extensions (if configured) */ |
| 678 | if (unlikely(!http_handle_7239_header(s, req))) |
| 679 | goto return_fail_rewrite; |
| 680 | if (unlikely(!http_handle_xff_header(s, req))) |
| 681 | goto return_fail_rewrite; |
| 682 | if (unlikely(!http_handle_xot_header(s, req))) |
| 683 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 684 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 685 | /* Filter the request headers if there are filters attached to the |
| 686 | * stream. |
| 687 | */ |
| 688 | if (HAS_FILTERS(s)) |
| 689 | req->analysers |= AN_REQ_FLT_HTTP_HDRS; |
| 690 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 691 | /* If we have no server assigned yet and we're balancing on url_param |
| 692 | * with a POST request, we may be interested in checking the body for |
| 693 | * that parameter. This will be done in another analyser. |
| 694 | */ |
| 695 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | 089eaa0 | 2019-01-14 15:17:46 +0100 | [diff] [blame] | 696 | s->txn->meth == HTTP_METH_POST && |
| 697 | (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 698 | channel_dont_connect(req); |
| 699 | req->analysers |= AN_REQ_HTTP_BODY; |
| 700 | } |
| 701 | |
| 702 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 703 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 704 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 705 | /* We expect some data from the client. Unless we know for sure |
| 706 | * we already have a full request, we have to re-enable quick-ack |
| 707 | * in case we previously disabled it, otherwise we might cause |
| 708 | * the client to delay further data. |
| 709 | */ |
Willy Tarreau | 9bdcf42 | 2023-01-12 19:40:42 +0100 | [diff] [blame] | 710 | if ((sess->listener && (sess->listener->bind_conf->options & BC_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM)) |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 711 | conn_set_quickack(cli_conn, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 712 | |
| 713 | /************************************************************* |
| 714 | * OK, that's finished for the headers. We have done what we * |
| 715 | * could. Let's switch to the DATA state. * |
| 716 | ************************************************************/ |
| 717 | req->analyse_exp = TICK_ETERNITY; |
| 718 | req->analysers &= ~an_bit; |
| 719 | |
| 720 | s->logs.tv_request = now; |
| 721 | /* OK let's go on with the BODY now */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 722 | 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] | 723 | return 1; |
| 724 | |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 725 | return_fail_rewrite: |
| 726 | if (!(s->flags & SF_ERR_MASK)) |
| 727 | s->flags |= SF_ERR_PRXCOND; |
| 728 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
| 729 | if (s->flags & SF_BE_ASSIGNED) |
| 730 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
| 731 | if (sess->listener && sess->listener->counters) |
| 732 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
| 733 | if (objt_server(s->target)) |
| 734 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
| 735 | /* fall through */ |
| 736 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 737 | return_int_err: |
| 738 | txn->status = 500; |
| 739 | if (!(s->flags & SF_ERR_MASK)) |
| 740 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 741 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 742 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 743 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 744 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 745 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 746 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 747 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 748 | http_set_term_flags(s); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 749 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 750 | DBG_TRACE_DEVEL("leaving on error", |
| 751 | 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] | 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 756 | * returns zero, at the beginning because it prevents any other processing |
| 757 | * from occurring, and at the end because it terminates the request. |
| 758 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 759 | 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] | 760 | { |
| 761 | struct http_txn *txn = s->txn; |
| 762 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 763 | 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] | 764 | /* This connection is being tarpitted. The CLIENT side has |
| 765 | * already set the connect expiration date to the right |
| 766 | * timeout. We just have to check that the client is still |
| 767 | * there and that the timeout has not expired. |
| 768 | */ |
| 769 | channel_dont_connect(req); |
Christopher Faulet | 4b490b7 | 2023-01-04 11:55:15 +0100 | [diff] [blame] | 770 | if (!(req->flags & CF_SHUTR) && |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 771 | !tick_is_expired(req->analyse_exp, now_ms)) { |
Christopher Faulet | b0c87f1 | 2021-10-29 14:37:07 +0200 | [diff] [blame] | 772 | /* Be sure to drain all data from the request channel */ |
| 773 | channel_htx_erase(req, htxbuf(&req->buf)); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 774 | DBG_TRACE_DEVEL("waiting for tarpit timeout expiry", |
| 775 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 776 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 777 | } |
| 778 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 779 | |
| 780 | /* We will set the queue timer to the time spent, just for |
| 781 | * logging purposes. We fake a 500 server error, so that the |
| 782 | * attacker will not suspect his connection has been tarpitted. |
| 783 | * It will not cause trouble to the logs because we can exclude |
| 784 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 785 | */ |
| 786 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 787 | |
Christopher Faulet | 2e56a73 | 2023-01-26 16:18:09 +0100 | [diff] [blame^] | 788 | http_reply_and_close(s, txn->status, (!sc_ep_test(s->scf, SE_FL_ERROR) ? http_error_message(s) : NULL)); |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 789 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 790 | |
| 791 | 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] | 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 796 | * for either the buffer to be full, or the full advertised contents to have |
| 797 | * reached the buffer. It must only be called after the standard HTTP request |
| 798 | * processing has occurred, because it expects the request to be parsed and will |
| 799 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 800 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 801 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 802 | * needs to read more data, or 1 once it has completed its analysis. |
| 803 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 804 | 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] | 805 | { |
| 806 | struct session *sess = s->sess; |
| 807 | struct http_txn *txn = s->txn; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 808 | |
Christopher Faulet | f4569bb | 2023-01-13 10:49:31 +0100 | [diff] [blame] | 809 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &s->txn->req); |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 810 | |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 811 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 812 | switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) { |
| 813 | case HTTP_RULE_RES_CONT: |
| 814 | goto http_end; |
| 815 | case HTTP_RULE_RES_YIELD: |
| 816 | goto missing_data_or_waiting; |
| 817 | case HTTP_RULE_RES_BADREQ: |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 818 | goto return_bad_req; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 819 | case HTTP_RULE_RES_ERROR: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 820 | goto return_int_err; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 821 | case HTTP_RULE_RES_ABRT: |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 822 | goto return_prx_cond; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 823 | default: |
| 824 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | http_end: |
| 828 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 829 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 830 | req->analysers &= ~an_bit; |
| 831 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 832 | 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] | 833 | return 1; |
| 834 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 835 | missing_data_or_waiting: |
| 836 | channel_dont_connect(req); |
| 837 | DBG_TRACE_DEVEL("waiting for more data", |
| 838 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 839 | return 0; |
| 840 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 841 | return_int_err: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 842 | txn->status = 500; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 843 | if (!(s->flags & SF_ERR_MASK)) |
| 844 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 845 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 846 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 847 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 848 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 849 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 850 | goto return_prx_err; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 851 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 852 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 853 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 854 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 855 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 856 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 857 | /* fall through */ |
| 858 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 859 | return_prx_err: |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 860 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 861 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 862 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 863 | return_prx_cond: |
Christopher Faulet | f0d80df | 2023-01-13 10:20:20 +0100 | [diff] [blame] | 864 | http_set_term_flags(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 865 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 866 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 867 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 868 | DBG_TRACE_DEVEL("leaving on error", |
| 869 | 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] | 870 | return 0; |
| 871 | } |
| 872 | |
| 873 | /* This function is an analyser which forwards request body (including chunk |
| 874 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 875 | * zero byte. The only situation where it must not be called is when we're in |
| 876 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 877 | * remaining data and to resync after end of body. It expects the msg_state to |
| 878 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 879 | * read more data, or 1 once we can go on with next request or end the stream. |
| 880 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
| 881 | * bytes of pending data + the headers if not already done. |
| 882 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 883 | 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] | 884 | { |
| 885 | struct session *sess = s->sess; |
| 886 | struct http_txn *txn = s->txn; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 887 | struct http_msg *msg = &txn->req; |
| 888 | struct htx *htx; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 889 | short status = 0; |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 890 | int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 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, msg); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 893 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 894 | htx = htxbuf(&req->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 895 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 896 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 897 | goto return_bad_req; |
| 898 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 899 | goto return_int_err; |
| 900 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 901 | /* Note that we don't have to send 100-continue back because we don't |
| 902 | * need the data to complete our job, and it's up to the server to |
| 903 | * decide whether to return 100, 417 or anything else in return of |
| 904 | * an "Expect: 100-continue" header. |
| 905 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 906 | if (msg->msg_state == HTTP_MSG_BODY) |
| 907 | msg->msg_state = HTTP_MSG_DATA; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 908 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 909 | /* in most states, we should abort in case of early close */ |
| 910 | channel_auto_close(req); |
| 911 | |
| 912 | if (req->to_forward) { |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 913 | if (req->to_forward == CHN_INFINITE_FORWARD) { |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 914 | if (req->flags & CF_EOI) |
| 915 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 916 | } |
| 917 | else { |
| 918 | /* We can't process the buffer's contents yet */ |
| 919 | req->flags |= CF_WAKE_WRITE; |
| 920 | goto missing_data_or_waiting; |
| 921 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 922 | } |
| 923 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 924 | if (msg->msg_state >= HTTP_MSG_ENDING) |
| 925 | goto ending; |
| 926 | |
| 927 | if (txn->meth == HTTP_METH_CONNECT) { |
| 928 | msg->msg_state = HTTP_MSG_ENDING; |
| 929 | goto ending; |
| 930 | } |
| 931 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 932 | /* Forward input data. We get it by removing all outgoing data not |
| 933 | * forwarded yet from HTX data size. If there are some data filters, we |
| 934 | * let them decide the amount of data to forward. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 935 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 936 | if (HAS_REQ_DATA_FILTERS(s)) { |
| 937 | ret = flt_http_payload(s, msg, htx->data); |
| 938 | if (ret < 0) |
| 939 | goto return_bad_req; |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 940 | c_adv(req, ret); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 941 | } |
| 942 | else { |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 943 | c_adv(req, htx->data - co_data(req)); |
Christopher Faulet | 2f7c82b | 2023-02-20 14:06:52 +0100 | [diff] [blame] | 944 | if ((global.tune.options & GTUNE_USE_FAST_FWD) && (msg->flags & HTTP_MSGF_XFER_LEN)) |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 945 | channel_htx_forward_forever(req, htx); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 946 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 947 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 948 | if (htx->data != co_data(req)) |
| 949 | goto missing_data_or_waiting; |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 950 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 951 | /* 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] | 952 | * in HTTP_MSG_ENDING state. Then if all data was marked to be |
| 953 | * forwarded, set the state to HTTP_MSG_DONE. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 954 | */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 955 | if (!(htx->flags & HTX_FL_EOM)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 956 | goto missing_data_or_waiting; |
| 957 | |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 958 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 959 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 960 | ending: |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 961 | req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */ |
| 962 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 963 | /* other states, ENDING...TUNNEL */ |
| 964 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 965 | goto done; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 966 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 967 | if (HAS_REQ_DATA_FILTERS(s)) { |
| 968 | ret = flt_http_end(s, msg); |
| 969 | if (ret <= 0) { |
| 970 | if (!ret) |
| 971 | goto missing_data_or_waiting; |
| 972 | goto return_bad_req; |
| 973 | } |
| 974 | } |
| 975 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 976 | if (txn->meth == HTTP_METH_CONNECT) |
| 977 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 978 | else { |
| 979 | msg->msg_state = HTTP_MSG_DONE; |
| 980 | req->to_forward = 0; |
| 981 | } |
| 982 | |
| 983 | done: |
| 984 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 985 | if (!(txn->flags & TX_CON_WANT_TUN)) |
| 986 | channel_dont_close(req); |
| 987 | |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 988 | if ((req->flags & CF_SHUTW) && co_data(req)) { |
| 989 | /* request errors are most likely due to the server aborting the |
| 990 | * transfer. */ |
| 991 | goto return_srv_abort; |
| 992 | } |
| 993 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 994 | http_end_request(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 995 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 996 | 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] | 997 | return 1; |
| 998 | } |
| 999 | |
| 1000 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 1001 | * the client's connection and forward any shutdown notification to the |
| 1002 | * server, which will decide whether to close or to go on processing the |
| 1003 | * request. We only do that in tunnel mode, and not in other modes since |
| 1004 | * it can be abused to exhaust source ports. */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 1005 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1006 | channel_auto_read(req); |
Christopher Faulet | 446d803 | 2022-12-12 07:53:18 +0100 | [diff] [blame] | 1007 | if ((req->flags & CF_SHUTR) && !(txn->flags & TX_CON_WANT_TUN)) |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1008 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1009 | channel_auto_close(req); |
| 1010 | } |
| 1011 | else if (s->txn->meth == HTTP_METH_POST) { |
| 1012 | /* POST requests may require to read extra CRLF sent by broken |
| 1013 | * browsers and which could cause an RST to be sent upon close |
| 1014 | * on some systems (eg: Linux). */ |
| 1015 | channel_auto_read(req); |
| 1016 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1017 | DBG_TRACE_DEVEL("waiting for the end of the HTTP txn", |
| 1018 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1019 | return 0; |
| 1020 | |
| 1021 | missing_data_or_waiting: |
| 1022 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1023 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1024 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1025 | |
| 1026 | waiting: |
| 1027 | /* waiting for the last bits to leave the buffer */ |
| 1028 | if (req->flags & CF_SHUTW) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1029 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1030 | |
| 1031 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 1032 | * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE. |
| 1033 | * And when content-length is used, we never want to let the possible |
| 1034 | * shutdown be forwarded to the other side, as the state machine will |
| 1035 | * take care of it once the client responds. It's also important to |
| 1036 | * prevent TIME_WAITs from accumulating on the backend side, and for |
| 1037 | * HTTP/2 where the last frame comes with a shutdown. |
| 1038 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1039 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1040 | channel_dont_close(req); |
| 1041 | |
| 1042 | /* We know that more data are expected, but we couldn't send more that |
| 1043 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
| 1044 | * system knows it must not set a PUSH on this first part. Interactive |
| 1045 | * modes are already handled by the stream sock layer. We must not do |
| 1046 | * this in content-length mode because it could present the MSG_MORE |
| 1047 | * flag with the last block of forwarded data, which would cause an |
| 1048 | * additional delay to be observed by the receiver. |
| 1049 | */ |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 1050 | if (HAS_REQ_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1051 | req->flags |= CF_EXPECT_MORE; |
| 1052 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1053 | DBG_TRACE_DEVEL("waiting for more data to forward", |
| 1054 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1055 | return 0; |
| 1056 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1057 | return_cli_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1058 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 1059 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1060 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1061 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1062 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1063 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1064 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 1065 | s->flags |= ((req->flags & CF_READ_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1066 | status = 400; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1067 | goto return_prx_cond; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1068 | |
| 1069 | return_srv_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1070 | _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts); |
| 1071 | _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1072 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1073 | _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1074 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1075 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1076 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 1077 | s->flags |= ((req->flags & CF_WRITE_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1078 | status = 502; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1079 | goto return_prx_cond; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1080 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1081 | return_int_err: |
| 1082 | if (!(s->flags & SF_ERR_MASK)) |
| 1083 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1084 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 1085 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1086 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1087 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1088 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1089 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1090 | status = 500; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1091 | goto return_prx_cond; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1092 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1093 | return_bad_req: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1094 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1095 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1096 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1097 | status = 400; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1098 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1099 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1100 | return_prx_cond: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1101 | if (txn->status > 0) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1102 | /* 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] | 1103 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1104 | } else { |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1105 | txn->status = status; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1106 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1107 | } |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 1108 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1109 | DBG_TRACE_DEVEL("leaving on error ", |
| 1110 | 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] | 1111 | return 0; |
| 1112 | } |
| 1113 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1114 | /* Reset the stream and the backend stream connector to a situation suitable for attemption connection */ |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1115 | /* Returns 0 if we can attempt to retry, -1 otherwise */ |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 1116 | static __inline int do_l7_retry(struct stream *s, struct stconn *sc) |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1117 | { |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1118 | struct channel *req, *res; |
| 1119 | int co_data; |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1120 | |
Christopher Faulet | 731c8e6 | 2022-03-29 16:08:44 +0200 | [diff] [blame] | 1121 | s->conn_retries++; |
| 1122 | if (s->conn_retries >= s->be->conn_retries) |
Christopher Faulet | 552601d | 2021-05-26 10:31:06 +0200 | [diff] [blame] | 1123 | return -1; |
Christopher Faulet | 5b82cc5 | 2020-10-12 15:18:50 +0200 | [diff] [blame] | 1124 | |
Christopher Faulet | e763c8c | 2021-05-05 18:23:59 +0200 | [diff] [blame] | 1125 | if (objt_server(s->target)) { |
| 1126 | if (s->flags & SF_CURR_SESS) { |
| 1127 | s->flags &= ~SF_CURR_SESS; |
| 1128 | _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess); |
| 1129 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1130 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries); |
Christopher Faulet | e763c8c | 2021-05-05 18:23:59 +0200 | [diff] [blame] | 1131 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1132 | _HA_ATOMIC_INC(&s->be->be_counters.retries); |
Willy Tarreau | 223995e | 2019-05-04 10:38:31 +0200 | [diff] [blame] | 1133 | |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1134 | req = &s->req; |
| 1135 | res = &s->res; |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1136 | /* Remove any write error from the request, and read error from the response */ |
Christopher Faulet | 2e56a73 | 2023-01-26 16:18:09 +0100 | [diff] [blame^] | 1137 | req->flags &= ~(CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW); |
| 1138 | res->flags &= ~(CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_EVENT | CF_SHUTR_NOW); |
Christopher Faulet | 7bf46bb | 2022-01-04 10:56:03 +0100 | [diff] [blame] | 1139 | res->analysers &= AN_RES_FLT_END; |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 1140 | s->conn_err_type = STRM_ET_NONE; |
Christopher Faulet | ae024ce | 2022-03-29 19:02:31 +0200 | [diff] [blame] | 1141 | s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK); |
| 1142 | s->conn_exp = TICK_ETERNITY; |
Olivier Houchard | 4bd5867 | 2019-07-12 16:16:59 +0200 | [diff] [blame] | 1143 | stream_choose_redispatch(s); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1144 | res->rex = TICK_ETERNITY; |
| 1145 | res->to_forward = 0; |
| 1146 | res->analyse_exp = TICK_ETERNITY; |
| 1147 | res->total = 0; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 1148 | |
Willy Tarreau | 19c65a9 | 2022-05-27 08:49:24 +0200 | [diff] [blame] | 1149 | if (sc_reset_endp(s->scb) < 0) { |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 1150 | if (!(s->flags & SF_ERR_MASK)) |
| 1151 | s->flags |= SF_ERR_INTERNAL; |
| 1152 | return -1; |
| 1153 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1154 | |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1155 | b_free(&req->buf); |
| 1156 | /* Swap the L7 buffer with the channel buffer */ |
| 1157 | /* We know we stored the co_data as b_data, so get it there */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1158 | co_data = b_data(&s->txn->l7_buffer); |
| 1159 | b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer)); |
| 1160 | b_xfer(&req->buf, &s->txn->l7_buffer, b_data(&s->txn->l7_buffer)); |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1161 | co_set_data(req, co_data); |
Christopher Faulet | 5b82cc5 | 2020-10-12 15:18:50 +0200 | [diff] [blame] | 1162 | |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 1163 | DBG_TRACE_DEVEL("perform a L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, s->txn); |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1164 | |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1165 | b_reset(&res->buf); |
| 1166 | co_set_data(res, 0); |
| 1167 | return 0; |
| 1168 | } |
| 1169 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1170 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 1171 | * processing can continue on next analysers, or zero if it either needs more |
| 1172 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 1173 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers |
| 1174 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1175 | * abort. |
| 1176 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1177 | 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] | 1178 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1179 | /* |
| 1180 | * We will analyze a complete HTTP response to check the its syntax. |
| 1181 | * |
| 1182 | * Once the start line and all headers are received, we may perform a |
| 1183 | * capture of the error (if any), and we will set a few fields. We also |
| 1184 | * logging and finally headers capture. |
| 1185 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1186 | struct session *sess = s->sess; |
| 1187 | struct http_txn *txn = s->txn; |
| 1188 | struct http_msg *msg = &txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1189 | struct htx *htx; |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1190 | struct connection *srv_conn; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1191 | struct htx_sl *sl; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1192 | int n; |
| 1193 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1194 | 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] | 1195 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1196 | htx = htxbuf(&rep->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1197 | |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1198 | /* Parsing errors are caught here */ |
| 1199 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 1200 | goto return_bad_res; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1201 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 1202 | goto return_int_err; |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1203 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1204 | /* |
| 1205 | * Now we quickly check if we have found a full valid response. |
| 1206 | * If not so, we check the FD and buffer states before leaving. |
| 1207 | * A full response is indicated by the fact that we have seen |
| 1208 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
| 1209 | * responses are checked first. |
| 1210 | * |
| 1211 | * Depending on whether the client is still there or not, we |
| 1212 | * may send an error response back or not. Note that normally |
| 1213 | * we should only check for HTTP status there, and check I/O |
| 1214 | * errors somewhere else. |
| 1215 | */ |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1216 | next_one: |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 1217 | if (unlikely(htx_is_empty(htx) || htx->first == -1)) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1218 | /* 1: have we encountered a read error ? */ |
Christopher Faulet | 2e56a73 | 2023-01-26 16:18:09 +0100 | [diff] [blame^] | 1219 | if (sc_ep_test(s->scb, SE_FL_ERROR)) { |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 1220 | struct connection *conn = sc_conn(s->scb); |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1221 | |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1222 | /* Perform a L7 retry because server refuses the early data. */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1223 | if ((txn->flags & TX_L7_RETRY) && |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1224 | (s->be->retry_type & PR_RE_EARLY_ERROR) && |
| 1225 | conn && conn->err_code == CO_ER_SSL_EARLY_FAILED && |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1226 | do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1227 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1228 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
Olivier Houchard | 6db1699 | 2019-05-17 15:40:49 +0200 | [diff] [blame] | 1232 | if (txn->flags & TX_NOT_FIRST) |
| 1233 | goto abort_keep_alive; |
| 1234 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1235 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1236 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1237 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1238 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1239 | } |
| 1240 | |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1241 | /* if the server refused the early data, just send a 425 */ |
| 1242 | if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1243 | txn->status = 425; |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1244 | else { |
| 1245 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1246 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1247 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1248 | |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1249 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1250 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1251 | |
| 1252 | if (!(s->flags & SF_ERR_MASK)) |
| 1253 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1254 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1255 | DBG_TRACE_DEVEL("leaving on error", |
| 1256 | 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] | 1257 | return 0; |
| 1258 | } |
| 1259 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1260 | /* 2: read timeout : return a 504 to the client. */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1261 | else if (rep->flags & CF_READ_TIMEOUT) { |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1262 | if ((txn->flags & TX_L7_RETRY) && |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1263 | (s->be->retry_type & PR_RE_TIMEOUT)) { |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1264 | if (co_data(rep) || do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1265 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1266 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1267 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1268 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1269 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1270 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1271 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1272 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1273 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1274 | } |
| 1275 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1276 | txn->status = 504; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1277 | stream_inc_http_fail_ctr(s); |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1278 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1279 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1280 | |
| 1281 | if (!(s->flags & SF_ERR_MASK)) |
| 1282 | s->flags |= SF_ERR_SRVTO; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1283 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1284 | DBG_TRACE_DEVEL("leaving on error", |
| 1285 | 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] | 1286 | return 0; |
| 1287 | } |
| 1288 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1289 | /* 3: client abort with an abortonclose */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1290 | 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] | 1291 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 1292 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1293 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1294 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1295 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1296 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1297 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1298 | txn->status = 400; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1299 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1300 | |
| 1301 | if (!(s->flags & SF_ERR_MASK)) |
| 1302 | s->flags |= SF_ERR_CLICL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1303 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1304 | |
| 1305 | /* process_stream() will take care of the error */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1306 | DBG_TRACE_DEVEL("leaving on error", |
| 1307 | 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] | 1308 | return 0; |
| 1309 | } |
| 1310 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1311 | /* 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] | 1312 | else if (rep->flags & CF_SHUTR) { |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1313 | if ((txn->flags & TX_L7_RETRY) && |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1314 | (s->be->retry_type & PR_RE_DISCONNECTED)) { |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1315 | if (co_data(rep) || do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1316 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1317 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1318 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1319 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1320 | } |
| 1321 | |
Olivier Houchard | 6db1699 | 2019-05-17 15:40:49 +0200 | [diff] [blame] | 1322 | if (txn->flags & TX_NOT_FIRST) |
| 1323 | goto abort_keep_alive; |
| 1324 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1325 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1326 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1327 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1328 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1329 | } |
| 1330 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1331 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1332 | stream_inc_http_fail_ctr(s); |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1333 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1334 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1335 | |
| 1336 | if (!(s->flags & SF_ERR_MASK)) |
| 1337 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1338 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1339 | DBG_TRACE_DEVEL("leaving on error", |
| 1340 | 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] | 1341 | return 0; |
| 1342 | } |
| 1343 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1344 | /* 5: write error to client (we don't send any message then) */ |
Christopher Faulet | 2e56a73 | 2023-01-26 16:18:09 +0100 | [diff] [blame^] | 1345 | else if (sc_ep_test(s->scf, SE_FL_ERR_PENDING)) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1346 | if (txn->flags & TX_NOT_FIRST) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1347 | goto abort_keep_alive; |
| 1348 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1349 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1350 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1351 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1352 | rep->analysers &= AN_RES_FLT_END; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1353 | |
| 1354 | if (!(s->flags & SF_ERR_MASK)) |
| 1355 | s->flags |= SF_ERR_CLICL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1356 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1357 | |
| 1358 | /* process_stream() will take care of the error */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1359 | DBG_TRACE_DEVEL("leaving on error", |
| 1360 | 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] | 1361 | return 0; |
| 1362 | } |
| 1363 | |
| 1364 | channel_dont_close(rep); |
| 1365 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1366 | DBG_TRACE_DEVEL("waiting for more data", |
| 1367 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1368 | return 0; |
| 1369 | } |
| 1370 | |
| 1371 | /* More interesting part now : we know that we have a complete |
| 1372 | * response which at least looks like HTTP. We have an indicator |
| 1373 | * of each header's length, so we can parse them quickly. |
| 1374 | */ |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 1375 | BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1376 | sl = http_get_stline(htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1377 | |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1378 | /* Perform a L7 retry because of the status code */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1379 | if ((txn->flags & TX_L7_RETRY) && |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1380 | l7_status_match(s->be, sl->info.res.status) && |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1381 | do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1382 | DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
| 1386 | /* Now, L7 buffer is useless, it can be released */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1387 | b_free(&txn->l7_buffer); |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1388 | |
| 1389 | msg->msg_state = HTTP_MSG_BODY; |
| 1390 | |
| 1391 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1392 | /* 0: we might have to print this header in debug mode */ |
| 1393 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1394 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 1395 | int32_t pos; |
| 1396 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1397 | http_debug_stline("srvrep", s, sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1398 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 1399 | 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] | 1400 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 1401 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 1402 | |
| 1403 | if (type == HTX_BLK_EOH) |
| 1404 | break; |
| 1405 | if (type != HTX_BLK_HDR) |
| 1406 | continue; |
| 1407 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1408 | http_debug_hdr("srvhdr", s, |
| 1409 | htx_get_blk_name(htx, blk), |
| 1410 | htx_get_blk_value(htx, blk)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1411 | } |
| 1412 | } |
| 1413 | |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1414 | /* 1: get the status code and the version. Also set HTTP flags */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1415 | txn->status = sl->info.res.status; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1416 | if (sl->flags & HTX_SL_F_VER_11) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1417 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1418 | if (sl->flags & HTX_SL_F_XFER_LEN) { |
| 1419 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 1420 | if (sl->flags & HTX_SL_F_CLEN) |
| 1421 | msg->flags |= HTTP_MSGF_CNT_LEN; |
| 1422 | else if (sl->flags & HTX_SL_F_CHNK) |
| 1423 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1424 | } |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 1425 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 1426 | msg->flags |= HTTP_MSGF_BODYLESS; |
Christopher Faulet | 576c358 | 2021-01-08 15:53:01 +0100 | [diff] [blame] | 1427 | if (sl->flags & HTX_SL_F_CONN_UPG) |
| 1428 | msg->flags |= HTTP_MSGF_CONN_UPG; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1429 | |
| 1430 | n = txn->status / 100; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1431 | if (n < 1 || n > 5) |
| 1432 | n = 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1433 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1434 | /* when the client triggers a 4xx from the server, it's most often due |
| 1435 | * to a missing object or permission. These events should be tracked |
| 1436 | * because if they happen often, it may indicate a brute force or a |
| 1437 | * vulnerability scan. |
| 1438 | */ |
| 1439 | if (n == 4) |
| 1440 | stream_inc_http_err_ctr(s); |
| 1441 | |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1442 | if (n == 5 && txn->status != 501 && txn->status != 505) |
| 1443 | stream_inc_http_fail_ctr(s); |
| 1444 | |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 1445 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1446 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]); |
| 1447 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req); |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 1448 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1449 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1450 | /* Adjust server's health based on status code. Note: status codes 501 |
| 1451 | * and 505 are triggered on demand by client request, so we must not |
| 1452 | * count them as server failures. |
| 1453 | */ |
| 1454 | if (objt_server(s->target)) { |
| 1455 | 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] | 1456 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1457 | else |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1458 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | /* |
| 1462 | * We may be facing a 100-continue response, or any other informational |
| 1463 | * 1xx response which is non-final, in which case this is not the right |
| 1464 | * response, and we're waiting for the next one. Let's allow this response |
| 1465 | * to go to the client and wait for the next one. There's an exception for |
| 1466 | * 101 which is used later in the code to switch protocols. |
| 1467 | */ |
| 1468 | if (txn->status < 200 && |
| 1469 | (txn->status == 100 || txn->status >= 102)) { |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1470 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1471 | htx->first = channel_htx_fwd_headers(rep, htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1472 | msg->msg_state = HTTP_MSG_RPBEFORE; |
Christopher Faulet | 3499f62 | 2019-09-03 15:23:54 +0200 | [diff] [blame] | 1473 | msg->flags = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1474 | txn->status = 0; |
| 1475 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 7d51845 | 2020-08-31 11:07:07 +0200 | [diff] [blame] | 1476 | rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */ |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1477 | goto next_one; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1478 | } |
| 1479 | |
Christopher Faulet | 6e6c7b1 | 2021-01-08 16:02:05 +0100 | [diff] [blame] | 1480 | /* A 101-switching-protocols must contains a Connection header with the |
| 1481 | * "upgrade" option and the request too. It means both are agree to |
| 1482 | * upgrade. It is not so strict because there is no test on the Upgrade |
| 1483 | * header content. But it is probably stronger enough for now. |
| 1484 | */ |
| 1485 | if (txn->status == 101 && |
| 1486 | (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG))) |
| 1487 | goto return_bad_res; |
| 1488 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1489 | /* |
| 1490 | * 2: check for cacheability. |
| 1491 | */ |
| 1492 | |
| 1493 | switch (txn->status) { |
| 1494 | case 200: |
| 1495 | case 203: |
| 1496 | case 204: |
| 1497 | case 206: |
| 1498 | case 300: |
| 1499 | case 301: |
| 1500 | case 404: |
| 1501 | case 405: |
| 1502 | case 410: |
| 1503 | case 414: |
| 1504 | case 501: |
| 1505 | break; |
| 1506 | default: |
| 1507 | /* RFC7231#6.1: |
| 1508 | * Responses with status codes that are defined as |
| 1509 | * cacheable by default (e.g., 200, 203, 204, 206, |
| 1510 | * 300, 301, 404, 405, 410, 414, and 501 in this |
| 1511 | * specification) can be reused by a cache with |
| 1512 | * heuristic expiration unless otherwise indicated |
| 1513 | * by the method definition or explicit cache |
| 1514 | * controls [RFC7234]; all other status codes are |
| 1515 | * not cacheable by default. |
| 1516 | */ |
| 1517 | txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK); |
| 1518 | break; |
| 1519 | } |
| 1520 | |
| 1521 | /* |
| 1522 | * 3: we may need to capture headers |
| 1523 | */ |
| 1524 | s->logs.logwait &= ~LW_RESP; |
| 1525 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1526 | http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1527 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1528 | /* Skip parsing if no content length is possible. */ |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 1529 | 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] | 1530 | txn->status == 101)) { |
| 1531 | /* Either we've established an explicit tunnel, or we're |
| 1532 | * switching the protocol. In both cases, we're very unlikely |
| 1533 | * to understand the next protocols. We have to switch to tunnel |
| 1534 | * mode, so that we transfer the request and responses then let |
| 1535 | * this protocol pass unmodified. When we later implement specific |
| 1536 | * parsers for such protocols, we'll want to check the Upgrade |
| 1537 | * header which contains information about that protocol for |
| 1538 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 1539 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 1540 | txn->flags |= TX_CON_WANT_TUN; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1541 | } |
| 1542 | |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1543 | /* check for NTML authentication headers in 401 (WWW-Authenticate) and |
| 1544 | * 407 (Proxy-Authenticate) responses and set the connection to private |
| 1545 | */ |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 1546 | srv_conn = sc_conn(s->scb); |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1547 | if (srv_conn) { |
| 1548 | struct ist hdr; |
| 1549 | struct http_hdr_ctx ctx; |
| 1550 | |
| 1551 | if (txn->status == 401) |
| 1552 | hdr = ist("WWW-Authenticate"); |
| 1553 | else if (txn->status == 407) |
| 1554 | hdr = ist("Proxy-Authenticate"); |
| 1555 | else |
| 1556 | goto end; |
| 1557 | |
| 1558 | ctx.blk = NULL; |
| 1559 | while (http_find_header(htx, hdr, &ctx, 0)) { |
Willy Tarreau | f1dcced | 2020-05-07 19:27:02 +0200 | [diff] [blame] | 1560 | /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM", |
| 1561 | * possibly followed by blanks and a base64 string, the connection |
| 1562 | * is private. Since it's a mess to deal with, we only check for |
| 1563 | * values starting with "NTLM" or "Nego". Note that often multiple |
| 1564 | * headers are sent by the server there. |
| 1565 | */ |
| 1566 | if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) || |
Willy Tarreau | 49a1d28 | 2020-05-07 19:10:15 +0200 | [diff] [blame] | 1567 | (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) { |
Olivier Houchard | 250031e | 2019-05-29 15:01:50 +0200 | [diff] [blame] | 1568 | sess->flags |= SESS_FL_PREFER_LAST; |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 1569 | conn_set_owner(srv_conn, sess, NULL); |
Christopher Faulet | 21ddc74 | 2020-07-01 15:26:14 +0200 | [diff] [blame] | 1570 | conn_set_private(srv_conn); |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 1571 | /* 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] | 1572 | session_add_conn(srv_conn->owner, srv_conn, srv_conn->target); |
Willy Tarreau | f1dcced | 2020-05-07 19:27:02 +0200 | [diff] [blame] | 1573 | break; |
Olivier Houchard | 250031e | 2019-05-29 15:01:50 +0200 | [diff] [blame] | 1574 | } |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | end: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1579 | /* we want to have the response time before we start processing it */ |
| 1580 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 1581 | |
| 1582 | /* end of job, return OK */ |
| 1583 | rep->analysers &= ~an_bit; |
| 1584 | rep->analyse_exp = TICK_ETERNITY; |
| 1585 | channel_auto_close(rep); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1586 | 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] | 1587 | return 1; |
| 1588 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1589 | return_int_err: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1590 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 1591 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1592 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1593 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1594 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1595 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1596 | txn->status = 500; |
| 1597 | if (!(s->flags & SF_ERR_MASK)) |
| 1598 | s->flags |= SF_ERR_INTERNAL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1599 | goto return_prx_cond; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1600 | |
| 1601 | return_bad_res: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1602 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1603 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1604 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1605 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1606 | } |
Olivier Houchard | e3249a9 | 2019-05-03 23:01:47 +0200 | [diff] [blame] | 1607 | if ((s->be->retry_type & PR_RE_JUNK_REQUEST) && |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1608 | (txn->flags & TX_L7_RETRY) && |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1609 | do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1610 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1611 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | e3249a9 | 2019-05-03 23:01:47 +0200 | [diff] [blame] | 1612 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1613 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1614 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1615 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1616 | /* fall through */ |
| 1617 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1618 | return_prx_cond: |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1619 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1620 | http_set_term_flags(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1621 | |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1622 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1623 | DBG_TRACE_DEVEL("leaving on error", |
| 1624 | 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] | 1625 | return 0; |
| 1626 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1627 | abort_keep_alive: |
| 1628 | /* A keep-alive request to the server failed on a network error. |
| 1629 | * The client is required to retry. We need to close without returning |
| 1630 | * any other information so that the client retries. |
| 1631 | */ |
| 1632 | txn->status = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1633 | s->logs.logwait = 0; |
| 1634 | s->logs.level = 0; |
| 1635 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1636 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1637 | DBG_TRACE_DEVEL("leaving by closing K/A connection", |
| 1638 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1639 | return 0; |
| 1640 | } |
| 1641 | |
| 1642 | /* This function performs all the processing enabled for the current response. |
| 1643 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 1644 | * and updates s->res.analysers. It might make sense to explode it into several |
| 1645 | * other functions. It works like process_request (see indications above). |
| 1646 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1647 | 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] | 1648 | { |
| 1649 | struct session *sess = s->sess; |
| 1650 | struct http_txn *txn = s->txn; |
| 1651 | struct http_msg *msg = &txn->rsp; |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1652 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1653 | struct proxy *cur_proxy; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1654 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 1655 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1656 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 1657 | return 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1658 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1659 | 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] | 1660 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1661 | htx = htxbuf(&rep->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1662 | |
| 1663 | /* The stats applet needs to adjust the Connection header but we don't |
| 1664 | * apply any filter there. |
| 1665 | */ |
| 1666 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 1667 | rep->analysers &= ~an_bit; |
| 1668 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1669 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | /* |
| 1673 | * We will have to evaluate the filters. |
| 1674 | * As opposed to version 1.2, now they will be evaluated in the |
| 1675 | * filters order and not in the header order. This means that |
| 1676 | * each filter has to be validated among all headers. |
| 1677 | * |
| 1678 | * Filters are tried with ->be first, then with ->fe if it is |
| 1679 | * different from ->be. |
| 1680 | * |
| 1681 | * Maybe we are in resume condiion. In this case I choose the |
| 1682 | * "struct proxy" which contains the rule list matching the resume |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1683 | * pointer. If none of these "struct proxy" match, I initialise |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1684 | * the process with the first one. |
| 1685 | * |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1686 | * In fact, I check only correspondence between the current list |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1687 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 1688 | * the loop with the ->be. |
| 1689 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1690 | if (s->current_rule_list == &sess->fe->http_res_rules || |
| 1691 | (sess->fe->defpx && s->current_rule_list == &sess->fe->defpx->http_res_rules)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1692 | cur_proxy = sess->fe; |
| 1693 | else |
| 1694 | cur_proxy = s->be; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1695 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1696 | while (1) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1697 | /* evaluate http-response rules */ |
Christopher Faulet | 46f46df | 2021-11-09 16:33:25 +0100 | [diff] [blame] | 1698 | if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1699 | struct list *def_rules, *rules; |
| 1700 | |
| 1701 | def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL); |
| 1702 | rules = &cur_proxy->http_res_rules; |
| 1703 | |
| 1704 | ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1705 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1706 | switch (ret) { |
| 1707 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 1708 | goto return_prx_yield; |
| 1709 | |
| 1710 | case HTTP_RULE_RES_CONT: |
| 1711 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 1712 | break; |
| 1713 | |
| 1714 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 1715 | goto deny; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1716 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1717 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent */ |
| 1718 | goto return_prx_cond; |
| 1719 | |
| 1720 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1721 | goto done; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1722 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1723 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 1724 | goto return_bad_res; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1725 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1726 | case HTTP_RULE_RES_ERROR: /* failed with a bad request */ |
| 1727 | goto return_int_err; |
| 1728 | } |
| 1729 | |
| 1730 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1731 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1732 | /* check whether we're already working on the frontend */ |
| 1733 | if (cur_proxy == sess->fe) |
| 1734 | break; |
| 1735 | cur_proxy = sess->fe; |
| 1736 | } |
| 1737 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1738 | /* OK that's all we can do for 1xx responses */ |
| 1739 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1740 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1741 | |
| 1742 | /* |
| 1743 | * Now check for a server cookie. |
| 1744 | */ |
| 1745 | 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] | 1746 | http_manage_server_side_cookies(s, rep); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1747 | |
| 1748 | /* |
| 1749 | * Check for cache-control or pragma headers if required. |
| 1750 | */ |
| 1751 | 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] | 1752 | http_check_response_for_cacheability(s, rep); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1753 | |
| 1754 | /* |
| 1755 | * Add server cookie in the response if needed |
| 1756 | */ |
| 1757 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 1758 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
| 1759 | (!(s->flags & SF_DIRECT) || |
| 1760 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 1761 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 1762 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 1763 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 1764 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 1765 | !(s->flags & SF_IGNORE_PRST)) { |
| 1766 | /* the server is known, it's not the one the client requested, or the |
| 1767 | * cookie's last seen date needs to be refreshed. We have to |
| 1768 | * insert a set-cookie here, except if we want to insert only on POST |
| 1769 | * requests and this one isn't. Note that servers which don't have cookies |
| 1770 | * (eg: some backup servers) will return a full cookie removal request. |
| 1771 | */ |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1772 | if (!__objt_server(s->target)->cookie) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1773 | chunk_printf(&trash, |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1774 | "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1775 | s->be->cookie_name); |
| 1776 | } |
| 1777 | else { |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1778 | 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] | 1779 | |
| 1780 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 1781 | /* emit last_date, which is mandatory */ |
| 1782 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 1783 | s30tob64((date.tv_sec+3) >> 2, |
| 1784 | trash.area + trash.data); |
| 1785 | trash.data += 5; |
| 1786 | |
| 1787 | if (s->be->cookie_maxlife) { |
| 1788 | /* emit first_date, which is either the original one or |
| 1789 | * the current date. |
| 1790 | */ |
| 1791 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 1792 | s30tob64(txn->cookie_first_date ? |
| 1793 | txn->cookie_first_date >> 2 : |
| 1794 | (date.tv_sec+3) >> 2, |
| 1795 | trash.area + trash.data); |
| 1796 | trash.data += 5; |
| 1797 | } |
| 1798 | } |
| 1799 | chunk_appendf(&trash, "; path=/"); |
| 1800 | } |
| 1801 | |
| 1802 | if (s->be->cookie_domain) |
| 1803 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
| 1804 | |
| 1805 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 1806 | chunk_appendf(&trash, "; HttpOnly"); |
| 1807 | |
| 1808 | if (s->be->ck_opts & PR_CK_SECURE) |
| 1809 | chunk_appendf(&trash, "; Secure"); |
| 1810 | |
Christopher Faulet | 2f53390 | 2020-01-21 11:06:48 +0100 | [diff] [blame] | 1811 | if (s->be->cookie_attrs) |
| 1812 | chunk_appendf(&trash, "; %s", s->be->cookie_attrs); |
| 1813 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1814 | if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data)))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 1815 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1816 | |
| 1817 | txn->flags &= ~TX_SCK_MASK; |
| 1818 | if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
| 1819 | /* the server did not change, only the date was updated */ |
| 1820 | txn->flags |= TX_SCK_UPDATED; |
| 1821 | else |
| 1822 | txn->flags |= TX_SCK_INSERTED; |
| 1823 | |
| 1824 | /* Here, we will tell an eventual cache on the client side that we don't |
| 1825 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 1826 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 1827 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 1828 | */ |
| 1829 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
| 1830 | |
| 1831 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 1832 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1833 | if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private")))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 1834 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | /* |
| 1839 | * Check if result will be cacheable with a cookie. |
| 1840 | * We'll block the response if security checks have caught |
| 1841 | * nasty things such as a cacheable cookie. |
| 1842 | */ |
| 1843 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 1844 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 1845 | (s->be->options & PR_O_CHK_CACHE)) { |
| 1846 | /* we're in presence of a cacheable response containing |
| 1847 | * a set-cookie header. We'll block it as requested by |
| 1848 | * the 'checkcache' option, and send an alert. |
| 1849 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1850 | ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1851 | 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] | 1852 | send_log(s->be, LOG_ALERT, |
| 1853 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1854 | 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] | 1855 | goto deny; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1856 | } |
| 1857 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1858 | end: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 1859 | /* |
| 1860 | * Evaluate after-response rules before forwarding the response. rules |
| 1861 | * from the backend are evaluated first, then one from the frontend if |
| 1862 | * it differs. |
| 1863 | */ |
| 1864 | if (!http_eval_after_res_rules(s)) |
| 1865 | goto return_int_err; |
| 1866 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 1867 | /* Filter the response headers if there are filters attached to the |
| 1868 | * stream. |
| 1869 | */ |
| 1870 | if (HAS_FILTERS(s)) |
| 1871 | rep->analysers |= AN_RES_FLT_HTTP_HDRS; |
| 1872 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1873 | /* Always enter in the body analyzer */ |
| 1874 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 1875 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
| 1876 | |
| 1877 | /* if the user wants to log as soon as possible, without counting |
| 1878 | * bytes from the server, then this is the right moment. We have |
| 1879 | * to temporarily assign bytes_out to log what we currently have. |
| 1880 | */ |
| 1881 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
| 1882 | 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] | 1883 | s->logs.bytes_out = htx->data; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1884 | s->do_log(s); |
| 1885 | s->logs.bytes_out = 0; |
| 1886 | } |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1887 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1888 | done: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 1889 | 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] | 1890 | rep->analysers &= ~an_bit; |
| 1891 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1892 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1893 | return 1; |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1894 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1895 | deny: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1896 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp); |
| 1897 | _HA_ATOMIC_INC(&s->be->be_counters.denied_resp); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1898 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1899 | _HA_ATOMIC_INC(&sess->listener->counters->denied_resp); |
Christopher Faulet | a08546b | 2019-12-16 16:07:34 +0100 | [diff] [blame] | 1900 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1901 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1902 | goto return_prx_err; |
| 1903 | |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 1904 | return_fail_rewrite: |
| 1905 | if (!(s->flags & SF_ERR_MASK)) |
| 1906 | s->flags |= SF_ERR_PRXCOND; |
| 1907 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
| 1908 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
| 1909 | if (sess->listener && sess->listener->counters) |
| 1910 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
| 1911 | if (objt_server(s->target)) |
| 1912 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
| 1913 | /* fall through */ |
| 1914 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1915 | return_int_err: |
| 1916 | txn->status = 500; |
| 1917 | if (!(s->flags & SF_ERR_MASK)) |
| 1918 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1919 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 1920 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
Dragan Dosen | 9a006f9 | 2021-09-21 13:02:09 +0200 | [diff] [blame] | 1921 | if (sess->listener && sess->listener->counters) |
| 1922 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1923 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1924 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1925 | goto return_prx_err; |
| 1926 | |
| 1927 | return_bad_res: |
| 1928 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1929 | stream_inc_http_fail_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1930 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | a20a653 | 2020-02-05 10:16:41 +0100 | [diff] [blame] | 1931 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1932 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | a20a653 | 2020-02-05 10:16:41 +0100 | [diff] [blame] | 1933 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 1934 | } |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1935 | /* fall through */ |
| 1936 | |
| 1937 | return_prx_err: |
| 1938 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 1939 | /* fall through */ |
| 1940 | |
| 1941 | return_prx_cond: |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1942 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1943 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1944 | |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1945 | http_set_term_flags(s); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1946 | |
Christopher Faulet | e58c000 | 2020-03-02 16:21:01 +0100 | [diff] [blame] | 1947 | rep->analysers &= AN_RES_FLT_END; |
| 1948 | s->req.analysers &= AN_REQ_FLT_END; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1949 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1950 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1951 | DBG_TRACE_DEVEL("leaving on error", |
| 1952 | 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] | 1953 | return 0; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1954 | |
| 1955 | return_prx_yield: |
| 1956 | channel_dont_close(rep); |
| 1957 | DBG_TRACE_DEVEL("waiting for more data", |
| 1958 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 1959 | return 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | /* This function is an analyser which forwards response body (including chunk |
| 1963 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 1964 | * zero byte. The only situation where it must not be called is when we're in |
| 1965 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 1966 | * remaining data and to resync after end of body. It expects the msg_state to |
| 1967 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 1968 | * read more data, or 1 once we can go on with next request or end the stream. |
| 1969 | * |
| 1970 | * It is capable of compressing response data both in content-length mode and |
| 1971 | * in chunked mode. The state machines follows different flows depending on |
| 1972 | * whether content-length and chunked modes are used, since there are no |
| 1973 | * trailers in content-length : |
| 1974 | * |
| 1975 | * chk-mode cl-mode |
| 1976 | * ,----- BODY -----. |
| 1977 | * / \ |
| 1978 | * V size > 0 V chk-mode |
| 1979 | * .--> SIZE -------------> DATA -------------> CRLF |
| 1980 | * | | size == 0 | last byte | |
| 1981 | * | v final crlf v inspected | |
| 1982 | * | TRAILERS -----------> DONE | |
| 1983 | * | | |
| 1984 | * `----------------------------------------------' |
| 1985 | * |
| 1986 | * Compression only happens in the DATA state, and must be flushed in final |
| 1987 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 1988 | * is performed at once on final states for all bytes parsed, or when leaving |
| 1989 | * on missing data. |
| 1990 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1991 | 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] | 1992 | { |
| 1993 | struct session *sess = s->sess; |
| 1994 | struct http_txn *txn = s->txn; |
| 1995 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1996 | struct htx *htx; |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1997 | int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1998 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1999 | 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] | 2000 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2001 | htx = htxbuf(&res->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2002 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2003 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 2004 | goto return_bad_res; |
| 2005 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 2006 | goto return_int_err; |
| 2007 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2008 | if (msg->msg_state == HTTP_MSG_BODY) |
| 2009 | msg->msg_state = HTTP_MSG_DATA; |
| 2010 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2011 | /* in most states, we should abort in case of early close */ |
| 2012 | channel_auto_close(res); |
| 2013 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2014 | if (res->to_forward) { |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2015 | if (res->to_forward == CHN_INFINITE_FORWARD) { |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2016 | if (res->flags & CF_EOI) |
| 2017 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2018 | } |
| 2019 | else { |
| 2020 | /* We can't process the buffer's contents yet */ |
| 2021 | res->flags |= CF_WAKE_WRITE; |
| 2022 | goto missing_data_or_waiting; |
| 2023 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2024 | } |
| 2025 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2026 | if (msg->msg_state >= HTTP_MSG_ENDING) |
| 2027 | goto ending; |
| 2028 | |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 2029 | 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] | 2030 | (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) { |
| 2031 | msg->msg_state = HTTP_MSG_ENDING; |
| 2032 | goto ending; |
| 2033 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2034 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2035 | /* Forward input data. We get it by removing all outgoing data not |
| 2036 | * forwarded yet from HTX data size. If there are some data filters, we |
| 2037 | * let them decide the amount of data to forward. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2038 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2039 | if (HAS_RSP_DATA_FILTERS(s)) { |
| 2040 | ret = flt_http_payload(s, msg, htx->data); |
| 2041 | if (ret < 0) |
| 2042 | goto return_bad_res; |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 2043 | c_adv(res, ret); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2044 | } |
| 2045 | else { |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 2046 | c_adv(res, htx->data - co_data(res)); |
Christopher Faulet | 2f7c82b | 2023-02-20 14:06:52 +0100 | [diff] [blame] | 2047 | if ((global.tune.options & GTUNE_USE_FAST_FWD) && (msg->flags & HTTP_MSGF_XFER_LEN)) |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2048 | channel_htx_forward_forever(res, htx); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2049 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2050 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2051 | if (htx->data != co_data(res)) |
| 2052 | goto missing_data_or_waiting; |
| 2053 | |
| 2054 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) { |
| 2055 | msg->msg_state = HTTP_MSG_ENDING; |
| 2056 | goto ending; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2057 | } |
| 2058 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2059 | /* 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] | 2060 | * in HTTP_MSG_ENDING state. Then if all data was marked to be |
| 2061 | * forwarded, set the state to HTTP_MSG_DONE. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2062 | */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2063 | if (!(htx->flags & HTX_FL_EOM)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2064 | goto missing_data_or_waiting; |
| 2065 | |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 2066 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2067 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2068 | ending: |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 2069 | res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */ |
| 2070 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2071 | /* other states, ENDING...TUNNEL */ |
| 2072 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 2073 | goto done; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2074 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2075 | if (HAS_RSP_DATA_FILTERS(s)) { |
| 2076 | ret = flt_http_end(s, msg); |
| 2077 | if (ret <= 0) { |
| 2078 | if (!ret) |
| 2079 | goto missing_data_or_waiting; |
| 2080 | goto return_bad_res; |
| 2081 | } |
| 2082 | } |
| 2083 | |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 2084 | 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] | 2085 | !(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 2086 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 2087 | goto ending; |
| 2088 | } |
| 2089 | else { |
| 2090 | msg->msg_state = HTTP_MSG_DONE; |
| 2091 | res->to_forward = 0; |
| 2092 | } |
| 2093 | |
| 2094 | done: |
| 2095 | |
| 2096 | channel_dont_close(res); |
| 2097 | |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 2098 | if ((res->flags & CF_SHUTW) && co_data(res)) { |
| 2099 | /* response errors are most likely due to the client aborting |
| 2100 | * the transfer. */ |
| 2101 | goto return_cli_abort; |
| 2102 | } |
| 2103 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2104 | http_end_response(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2105 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2106 | 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] | 2107 | return 1; |
| 2108 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2109 | DBG_TRACE_DEVEL("waiting for the end of the HTTP txn", |
| 2110 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2111 | return 0; |
| 2112 | |
| 2113 | missing_data_or_waiting: |
| 2114 | if (res->flags & CF_SHUTW) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2115 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2116 | |
| 2117 | /* stop waiting for data if the input is closed before the end. If the |
| 2118 | * client side was already closed, it means that the client has aborted, |
| 2119 | * so we don't want to count this as a server abort. Otherwise it's a |
| 2120 | * server abort. |
| 2121 | */ |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 2122 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2123 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2124 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2125 | /* If we have some pending data, we continue the processing */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2126 | if (htx_is_empty(htx)) |
| 2127 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2128 | } |
| 2129 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2130 | /* When TE: chunked is used, we need to get there again to parse |
| 2131 | * 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] | 2132 | * set CF_DONTCLOSE. Similarly when there is a content-leng or if there |
| 2133 | * are filters registered on the stream, we don't want to forward a |
| 2134 | * close |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2135 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2136 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2137 | channel_dont_close(res); |
| 2138 | |
| 2139 | /* We know that more data are expected, but we couldn't send more that |
| 2140 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
| 2141 | * system knows it must not set a PUSH on this first part. Interactive |
| 2142 | * modes are already handled by the stream sock layer. We must not do |
| 2143 | * this in content-length mode because it could present the MSG_MORE |
| 2144 | * flag with the last block of forwarded data, which would cause an |
| 2145 | * additional delay to be observed by the receiver. |
| 2146 | */ |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 2147 | if (HAS_RSP_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2148 | res->flags |= CF_EXPECT_MORE; |
| 2149 | |
| 2150 | /* the stream handler will take care of timeouts and errors */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2151 | DBG_TRACE_DEVEL("waiting for more data to forward", |
| 2152 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2153 | return 0; |
| 2154 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2155 | return_srv_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2156 | _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts); |
| 2157 | _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2158 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2159 | _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2160 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2161 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts); |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2162 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2163 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 2164 | s->flags |= ((res->flags & CF_READ_TIMEOUT) ? SF_ERR_SRVTO : SF_ERR_SRVCL); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2165 | goto return_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2166 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2167 | return_cli_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2168 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 2169 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2170 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2171 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2172 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2173 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2174 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 2175 | s->flags |= ((res->flags & CF_WRITE_TIMEOUT) ? SF_ERR_CLITO : SF_ERR_CLICL); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2176 | goto return_error; |
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 | return_int_err: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2179 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 2180 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2181 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2182 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2183 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2184 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2185 | if (!(s->flags & SF_ERR_MASK)) |
| 2186 | s->flags |= SF_ERR_INTERNAL; |
| 2187 | goto return_error; |
| 2188 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2189 | return_bad_res: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2190 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2191 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2192 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2193 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 2194 | } |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2195 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2196 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2197 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2198 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2199 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2200 | return_error: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2201 | /* don't send any error message as we're in the body */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2202 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 2203 | http_set_term_flags(s); |
| 2204 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2205 | DBG_TRACE_DEVEL("leaving on error", |
| 2206 | 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] | 2207 | return 0; |
| 2208 | } |
| 2209 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2210 | /* Perform an HTTP redirect based on the information in <rule>. The function |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2211 | * returns zero in case of an irrecoverable error such as too large a request |
| 2212 | * to build a valid response, 1 in case of successful redirect (hence the rule |
| 2213 | * is final), or 2 if the rule has to be silently skipped. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2214 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2215 | 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] | 2216 | { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2217 | struct channel *req = &s->req; |
| 2218 | struct channel *res = &s->res; |
| 2219 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2220 | struct htx_sl *sl; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2221 | struct buffer *chunk; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2222 | struct ist status, reason, location; |
| 2223 | unsigned int flags; |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2224 | int ret = 1, close = 0; /* Try to keep the connection alive byt default */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2225 | |
| 2226 | chunk = alloc_trash_chunk(); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2227 | if (!chunk) { |
| 2228 | if (!(s->flags & SF_ERR_MASK)) |
| 2229 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2230 | goto fail; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2231 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2232 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2233 | /* |
| 2234 | * Create the location |
| 2235 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2236 | htx = htxbuf(&req->buf); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2237 | switch(rule->type) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2238 | case REDIRECT_TYPE_SCHEME: { |
| 2239 | struct http_hdr_ctx ctx; |
| 2240 | struct ist path, host; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2241 | struct http_uri_parser parser; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2242 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2243 | host = ist(""); |
| 2244 | ctx.blk = NULL; |
| 2245 | if (http_find_header(htx, ist("Host"), &ctx, 0)) |
| 2246 | host = ctx.value; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2247 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2248 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2249 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 2250 | path = http_parse_path(&parser); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2251 | /* build message using path */ |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 2252 | if (isttest(path)) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2253 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2254 | int qs = 0; |
| 2255 | while (qs < path.len) { |
| 2256 | if (*(path.ptr + qs) == '?') { |
| 2257 | path.len = qs; |
| 2258 | break; |
| 2259 | } |
| 2260 | qs++; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2261 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2262 | } |
| 2263 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2264 | else |
| 2265 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2266 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2267 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2268 | /* add scheme */ |
| 2269 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2270 | goto fail; |
| 2271 | } |
| 2272 | else { |
| 2273 | /* add scheme with executing log format */ |
| 2274 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2275 | chunk->size - chunk->data, |
| 2276 | &rule->rdr_fmt); |
| 2277 | } |
| 2278 | /* add "://" + host + path */ |
| 2279 | if (!chunk_memcat(chunk, "://", 3) || |
| 2280 | !chunk_memcat(chunk, host.ptr, host.len) || |
| 2281 | !chunk_memcat(chunk, path.ptr, path.len)) |
| 2282 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2283 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2284 | /* append a slash at the end of the location if needed and missing */ |
| 2285 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2286 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2287 | if (chunk->data + 1 >= chunk->size) |
| 2288 | goto fail; |
| 2289 | chunk->area[chunk->data++] = '/'; |
| 2290 | } |
| 2291 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2292 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2293 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2294 | case REDIRECT_TYPE_PREFIX: { |
| 2295 | struct ist path; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2296 | struct http_uri_parser parser; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2297 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2298 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2299 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 2300 | path = http_parse_path(&parser); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2301 | /* build message using path */ |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 2302 | if (isttest(path)) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2303 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2304 | int qs = 0; |
| 2305 | while (qs < path.len) { |
| 2306 | if (*(path.ptr + qs) == '?') { |
| 2307 | path.len = qs; |
| 2308 | break; |
| 2309 | } |
| 2310 | qs++; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2311 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2312 | } |
| 2313 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2314 | else |
| 2315 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2316 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2317 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2318 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2319 | * add anything, otherwise it makes it hard for the user to |
| 2320 | * configure a self-redirection. |
| 2321 | */ |
| 2322 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 2323 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2324 | goto fail; |
| 2325 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2326 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2327 | else { |
| 2328 | /* add prefix with executing log format */ |
| 2329 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2330 | chunk->size - chunk->data, |
| 2331 | &rule->rdr_fmt); |
| 2332 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2333 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2334 | /* add path */ |
| 2335 | if (!chunk_memcat(chunk, path.ptr, path.len)) |
| 2336 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2337 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2338 | /* append a slash at the end of the location if needed and missing */ |
| 2339 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2340 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2341 | if (chunk->data + 1 >= chunk->size) |
| 2342 | goto fail; |
| 2343 | chunk->area[chunk->data++] = '/'; |
| 2344 | } |
| 2345 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2346 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2347 | case REDIRECT_TYPE_LOCATION: |
| 2348 | default: |
| 2349 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2350 | /* add location */ |
| 2351 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2352 | goto fail; |
| 2353 | } |
| 2354 | else { |
| 2355 | /* add location with executing log format */ |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2356 | int len = build_logline(s, chunk->area + chunk->data, |
| 2357 | chunk->size - chunk->data, |
| 2358 | &rule->rdr_fmt); |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2359 | if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) { |
| 2360 | ret = 2; |
| 2361 | goto out; |
| 2362 | } |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2363 | |
| 2364 | chunk->data += len; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2365 | } |
| 2366 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2367 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2368 | location = ist2(chunk->area, chunk->data); |
| 2369 | |
| 2370 | /* |
| 2371 | * Create the 30x response |
| 2372 | */ |
| 2373 | switch (rule->code) { |
| 2374 | case 308: |
| 2375 | status = ist("308"); |
| 2376 | reason = ist("Permanent Redirect"); |
| 2377 | break; |
| 2378 | case 307: |
| 2379 | status = ist("307"); |
| 2380 | reason = ist("Temporary Redirect"); |
| 2381 | break; |
| 2382 | case 303: |
| 2383 | status = ist("303"); |
| 2384 | reason = ist("See Other"); |
| 2385 | break; |
| 2386 | case 301: |
| 2387 | status = ist("301"); |
| 2388 | reason = ist("Moved Permanently"); |
| 2389 | break; |
| 2390 | case 302: |
| 2391 | default: |
| 2392 | status = ist("302"); |
| 2393 | reason = ist("Found"); |
| 2394 | break; |
| 2395 | } |
| 2396 | |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2397 | if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE) |
| 2398 | close = 1; |
| 2399 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2400 | htx = htx_from_buf(&res->buf); |
Kevin Zhu | 96b3639 | 2020-01-07 09:42:55 +0100 | [diff] [blame] | 2401 | /* Trim any possible response */ |
| 2402 | channel_htx_truncate(&s->res, htx); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2403 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 2404 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason); |
| 2405 | if (!sl) |
| 2406 | goto fail; |
| 2407 | sl->info.res.status = rule->code; |
| 2408 | s->txn->status = rule->code; |
| 2409 | |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2410 | if (close && !htx_add_header(htx, ist("Connection"), ist("close"))) |
| 2411 | goto fail; |
| 2412 | |
| 2413 | if (!htx_add_header(htx, ist("Content-length"), ist("0")) || |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2414 | !htx_add_header(htx, ist("Location"), location)) |
| 2415 | goto fail; |
| 2416 | |
| 2417 | if (rule->code == 302 || rule->code == 303 || rule->code == 307) { |
| 2418 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache"))) |
| 2419 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | if (rule->cookie_len) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2423 | if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len))) |
| 2424 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2425 | } |
| 2426 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2427 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2428 | goto fail; |
| 2429 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2430 | htx->flags |= HTX_FL_EOM; |
Kevin Zhu | 96b3639 | 2020-01-07 09:42:55 +0100 | [diff] [blame] | 2431 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 2432 | if (!http_forward_proxy_resp(s, 1)) |
| 2433 | goto fail; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2434 | |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2435 | if (rule->flags & REDIRECT_FLAG_FROM_REQ) { |
| 2436 | /* let's log the request time */ |
| 2437 | s->logs.tv_request = now; |
Christopher Faulet | d347588 | 2021-10-04 14:16:46 +0200 | [diff] [blame] | 2438 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2439 | |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2440 | 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] | 2441 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2442 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2443 | |
| 2444 | if (!(s->flags & SF_ERR_MASK)) |
| 2445 | s->flags |= SF_ERR_LOCAL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 2446 | http_set_term_flags(s); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2447 | |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2448 | out: |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2449 | free_trash_chunk(chunk); |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2450 | return ret; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2451 | |
| 2452 | fail: |
| 2453 | /* If an error occurred, remove the incomplete HTTP response from the |
| 2454 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 2455 | channel_htx_truncate(res, htxbuf(&res->buf)); |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2456 | ret = 0; |
| 2457 | goto out; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2458 | } |
| 2459 | |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2460 | /* This function filters the request header names to only allow [0-9a-zA-Z-] |
| 2461 | * characters. Depending on the proxy configuration, headers with a name not |
| 2462 | * matching this charset are removed or the request is rejected with a |
| 2463 | * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT |
| 2464 | * to continue the request processing or HTTP_RULE_RES_DENY if the request is |
| 2465 | * rejected. |
| 2466 | */ |
| 2467 | static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px) |
| 2468 | { |
| 2469 | struct htx_blk *blk; |
| 2470 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
| 2471 | |
| 2472 | blk = htx_get_first_blk(htx); |
| 2473 | while (blk) { |
| 2474 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 2475 | |
| 2476 | if (type == HTX_BLK_HDR) { |
| 2477 | struct ist n = htx_get_blk_name(htx, blk); |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2478 | int i, end = istlen(n); |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2479 | |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2480 | for (i = 0; i < end; i++) { |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2481 | if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') { |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2482 | break; |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2483 | } |
| 2484 | } |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2485 | |
| 2486 | if (i < end) { |
| 2487 | /* Disallowed character found - block the request or remove the header */ |
| 2488 | if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK) |
| 2489 | goto block; |
| 2490 | blk = htx_remove_blk(htx, blk); |
| 2491 | continue; |
| 2492 | } |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2493 | } |
| 2494 | if (type == HTX_BLK_EOH) |
| 2495 | break; |
| 2496 | |
| 2497 | blk = htx_get_next_blk(htx, blk); |
| 2498 | } |
| 2499 | out: |
| 2500 | return rule_ret; |
| 2501 | block: |
| 2502 | /* Block the request returning a 403-Forbidden response */ |
| 2503 | s->txn->status = 403; |
| 2504 | rule_ret = HTTP_RULE_RES_DENY; |
| 2505 | goto out; |
| 2506 | } |
| 2507 | |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2508 | /* Replace all headers matching the name <name>. The header value is replaced if |
| 2509 | * it matches the regex <re>. <str> is used for the replacement. If <full> is |
| 2510 | * set to 1, the full-line is matched and replaced. Otherwise, comma-separated |
| 2511 | * values are evaluated one by one. It returns 0 on success and -1 on error. |
| 2512 | */ |
| 2513 | int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name, |
| 2514 | const char *str, struct my_regex *re, int full) |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2515 | { |
| 2516 | struct http_hdr_ctx ctx; |
| 2517 | struct buffer *output = get_trash_chunk(); |
| 2518 | |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2519 | ctx.blk = NULL; |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2520 | while (http_find_header(htx, name, &ctx, full)) { |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2521 | if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0)) |
| 2522 | continue; |
| 2523 | |
| 2524 | output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch); |
| 2525 | if (output->data == -1) |
| 2526 | return -1; |
| 2527 | if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data))) |
| 2528 | return -1; |
| 2529 | } |
| 2530 | return 0; |
| 2531 | } |
| 2532 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2533 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 2534 | * takes the string from the variable 'replace' with length 'len', then modifies |
| 2535 | * the relevant part of the request line accordingly. Then it updates various |
| 2536 | * pointers to the next elements which were moved, and the total buffer length. |
| 2537 | * It finds the action to be performed in p[2], previously filled by function |
| 2538 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 2539 | * error, though this can be revisited when this code is finally exploited. |
| 2540 | * |
| 2541 | * '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] | 2542 | * 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] | 2543 | * |
| 2544 | * In query string case, the mark question '?' must be set at the start of the |
| 2545 | * string by the caller, event if the replacement query string is empty. |
| 2546 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2547 | int http_req_replace_stline(int action, const char *replace, int len, |
| 2548 | struct proxy *px, struct stream *s) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2549 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2550 | struct htx *htx = htxbuf(&s->req.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2551 | |
| 2552 | switch (action) { |
| 2553 | case 0: // method |
| 2554 | if (!http_replace_req_meth(htx, ist2(replace, len))) |
| 2555 | return -1; |
| 2556 | break; |
| 2557 | |
| 2558 | case 1: // path |
Christopher Faulet | b8ce505 | 2020-08-31 16:11:57 +0200 | [diff] [blame] | 2559 | if (!http_replace_req_path(htx, ist2(replace, len), 0)) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2560 | return -1; |
| 2561 | break; |
| 2562 | |
| 2563 | case 2: // query |
| 2564 | if (!http_replace_req_query(htx, ist2(replace, len))) |
| 2565 | return -1; |
| 2566 | break; |
| 2567 | |
| 2568 | case 3: // uri |
| 2569 | if (!http_replace_req_uri(htx, ist2(replace, len))) |
| 2570 | return -1; |
| 2571 | break; |
| 2572 | |
Christopher Faulet | 312294f | 2020-09-02 17:17:44 +0200 | [diff] [blame] | 2573 | case 4: // path + query |
| 2574 | if (!http_replace_req_path(htx, ist2(replace, len), 1)) |
| 2575 | return -1; |
| 2576 | break; |
| 2577 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2578 | default: |
| 2579 | return -1; |
| 2580 | } |
| 2581 | return 0; |
| 2582 | } |
| 2583 | |
| 2584 | /* This function replace the HTTP status code and the associated message. The |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2585 | * variable <status> contains the new status code. This function never fails. It |
| 2586 | * returns 0 in case of success, -1 in case of internal error. |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2587 | */ |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2588 | 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] | 2589 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2590 | struct htx *htx = htxbuf(&s->res.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2591 | char *res; |
| 2592 | |
| 2593 | chunk_reset(&trash); |
| 2594 | res = ultoa_o(status, trash.area, trash.size); |
| 2595 | trash.data = res - trash.area; |
| 2596 | |
| 2597 | /* Do we have a custom reason format string? */ |
Tim Duesterhus | e296d3e | 2020-03-05 17:56:31 +0100 | [diff] [blame] | 2598 | if (!isttest(reason)) { |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2599 | const char *str = http_get_reason(status); |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 2600 | reason = ist(str); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2601 | } |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2602 | |
Christopher Faulet | bde2c4c | 2020-08-31 16:43:34 +0200 | [diff] [blame] | 2603 | if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason)) |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2604 | return -1; |
Willy Tarreau | 640e253 | 2023-01-10 14:50:44 +0100 | [diff] [blame] | 2605 | s->txn->status = status; |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2606 | return 0; |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2607 | } |
| 2608 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2609 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
| 2610 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 2611 | * further processing of the request (auth, deny, ...), and defaults to |
| 2612 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 2613 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 2614 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 2615 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 2616 | * status. |
| 2617 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2618 | static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, |
| 2619 | struct list *rules, struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2620 | { |
| 2621 | struct session *sess = strm_sess(s); |
| 2622 | struct http_txn *txn = s->txn; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2623 | struct act_rule *rule; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2624 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2625 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2626 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2627 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2628 | * resume condition. If a resume is needed it is always in the action |
| 2629 | * and never in the ACL or converters. In this case, we initialise the |
| 2630 | * current rule, and go to the action execution point. |
| 2631 | */ |
| 2632 | if (s->current_rule) { |
| 2633 | rule = s->current_rule; |
| 2634 | s->current_rule = NULL; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2635 | if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules)) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2636 | goto resume_execution; |
| 2637 | } |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2638 | s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2639 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2640 | restart: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2641 | /* start the ruleset evaluation in strict mode */ |
| 2642 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2643 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2644 | list_for_each_entry(rule, s->current_rule_list, list) { |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2645 | /* check optional condition */ |
| 2646 | if (rule->cond) { |
| 2647 | int ret; |
| 2648 | |
| 2649 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 2650 | ret = acl_pass(ret); |
| 2651 | |
| 2652 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2653 | ret = !ret; |
| 2654 | |
| 2655 | if (!ret) /* condition not matched */ |
| 2656 | continue; |
| 2657 | } |
| 2658 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2659 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2660 | resume_execution: |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 2661 | if (rule->kw->flags & KWF_EXPERIMENTAL) |
| 2662 | mark_tainted(TAINTED_ACTION_EXP_EXECUTED); |
| 2663 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2664 | /* Always call the action function if defined */ |
| 2665 | if (rule->action_ptr) { |
Christopher Faulet | 2e56a73 | 2023-01-26 16:18:09 +0100 | [diff] [blame^] | 2666 | if (sc_ep_test(s->scf, SE_FL_ERROR) || |
Christopher Faulet | 446d803 | 2022-12-12 07:53:18 +0100 | [diff] [blame] | 2667 | ((s->req.flags & CF_SHUTR) && |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2668 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2669 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2670 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2671 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2672 | case ACT_RET_CONT: |
| 2673 | break; |
| 2674 | case ACT_RET_STOP: |
| 2675 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2676 | s->last_rule_file = rule->conf.file; |
| 2677 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2678 | goto end; |
| 2679 | case ACT_RET_YIELD: |
| 2680 | s->current_rule = rule; |
| 2681 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2682 | goto end; |
| 2683 | case ACT_RET_ERR: |
| 2684 | rule_ret = HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2685 | s->last_rule_file = rule->conf.file; |
| 2686 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2687 | goto end; |
| 2688 | case ACT_RET_DONE: |
| 2689 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2690 | s->last_rule_file = rule->conf.file; |
| 2691 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2692 | goto end; |
| 2693 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 2694 | if (txn->status == -1) |
| 2695 | txn->status = 403; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2696 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2697 | s->last_rule_file = rule->conf.file; |
| 2698 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2699 | goto end; |
| 2700 | case ACT_RET_ABRT: |
| 2701 | rule_ret = HTTP_RULE_RES_ABRT; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2702 | s->last_rule_file = rule->conf.file; |
| 2703 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2704 | goto end; |
| 2705 | case ACT_RET_INV: |
| 2706 | rule_ret = HTTP_RULE_RES_BADREQ; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2707 | s->last_rule_file = rule->conf.file; |
| 2708 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2709 | goto end; |
| 2710 | } |
| 2711 | continue; /* eval the next rule */ |
| 2712 | } |
| 2713 | |
| 2714 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2715 | switch (rule->action) { |
| 2716 | case ACT_ACTION_ALLOW: |
| 2717 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2718 | s->last_rule_file = rule->conf.file; |
| 2719 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2720 | goto end; |
| 2721 | |
| 2722 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2723 | txn->status = rule->arg.http_reply->status; |
| 2724 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2725 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2726 | s->last_rule_file = rule->conf.file; |
| 2727 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2728 | goto end; |
| 2729 | |
| 2730 | case ACT_HTTP_REQ_TARPIT: |
| 2731 | txn->flags |= TX_CLTARPIT; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2732 | txn->status = rule->arg.http_reply->status; |
| 2733 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2734 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2735 | s->last_rule_file = rule->conf.file; |
| 2736 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2737 | goto end; |
| 2738 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2739 | case ACT_HTTP_REDIR: { |
| 2740 | int ret = http_apply_redirect_rule(rule->arg.redir, s, txn); |
| 2741 | |
| 2742 | if (ret == 2) // 2 == skip |
| 2743 | break; |
| 2744 | |
| 2745 | rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2746 | s->last_rule_file = rule->conf.file; |
| 2747 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2748 | goto end; |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2749 | } |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2750 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2751 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2752 | default: |
| 2753 | break; |
| 2754 | } |
| 2755 | } |
| 2756 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2757 | if (def_rules && s->current_rule_list == def_rules) { |
| 2758 | s->current_rule_list = rules; |
| 2759 | goto restart; |
| 2760 | } |
| 2761 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2762 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2763 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2764 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2765 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2766 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2767 | /* we reached the end of the rules, nothing to report */ |
| 2768 | return rule_ret; |
| 2769 | } |
| 2770 | |
| 2771 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 2772 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 2773 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 2774 | * is returned, the process can continue the evaluation of next rule list. If |
| 2775 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 2776 | * 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] | 2777 | * must be returned. If *YIELD is returned, the caller must call again the |
| 2778 | * function with the same context. |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2779 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2780 | static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules, |
| 2781 | struct list *rules, struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2782 | { |
| 2783 | struct session *sess = strm_sess(s); |
| 2784 | struct http_txn *txn = s->txn; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2785 | struct act_rule *rule; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2786 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2787 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2788 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2789 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2790 | * resume condition. If a resume is needed it is always in the action |
| 2791 | * and never in the ACL or converters. In this case, we initialise the |
| 2792 | * current rule, and go to the action execution point. |
| 2793 | */ |
| 2794 | if (s->current_rule) { |
| 2795 | rule = s->current_rule; |
| 2796 | s->current_rule = NULL; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2797 | if (s->current_rule_list == rules || (def_rules && s->current_rule_list == def_rules)) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2798 | goto resume_execution; |
| 2799 | } |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2800 | s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules); |
| 2801 | |
| 2802 | restart: |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2803 | |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2804 | /* start the ruleset evaluation in strict mode */ |
| 2805 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2806 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2807 | list_for_each_entry(rule, s->current_rule_list, list) { |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2808 | /* check optional condition */ |
| 2809 | if (rule->cond) { |
| 2810 | int ret; |
| 2811 | |
| 2812 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 2813 | ret = acl_pass(ret); |
| 2814 | |
| 2815 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2816 | ret = !ret; |
| 2817 | |
| 2818 | if (!ret) /* condition not matched */ |
| 2819 | continue; |
| 2820 | } |
| 2821 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2822 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2823 | resume_execution: |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 2824 | if (rule->kw->flags & KWF_EXPERIMENTAL) |
| 2825 | mark_tainted(TAINTED_ACTION_EXP_EXECUTED); |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2826 | |
| 2827 | /* Always call the action function if defined */ |
| 2828 | if (rule->action_ptr) { |
Christopher Faulet | 2e56a73 | 2023-01-26 16:18:09 +0100 | [diff] [blame^] | 2829 | if (sc_ep_test(s->scf, SE_FL_ERROR) || |
Christopher Faulet | 446d803 | 2022-12-12 07:53:18 +0100 | [diff] [blame] | 2830 | ((s->req.flags & CF_SHUTR) && |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2831 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2832 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2833 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2834 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2835 | case ACT_RET_CONT: |
| 2836 | break; |
| 2837 | case ACT_RET_STOP: |
| 2838 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2839 | s->last_rule_file = rule->conf.file; |
| 2840 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2841 | goto end; |
| 2842 | case ACT_RET_YIELD: |
| 2843 | s->current_rule = rule; |
| 2844 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2845 | goto end; |
| 2846 | case ACT_RET_ERR: |
| 2847 | rule_ret = HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2848 | s->last_rule_file = rule->conf.file; |
| 2849 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2850 | goto end; |
| 2851 | case ACT_RET_DONE: |
| 2852 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2853 | s->last_rule_file = rule->conf.file; |
| 2854 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2855 | goto end; |
| 2856 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 2857 | if (txn->status == -1) |
| 2858 | txn->status = 502; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2859 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2860 | s->last_rule_file = rule->conf.file; |
| 2861 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2862 | goto end; |
| 2863 | case ACT_RET_ABRT: |
| 2864 | rule_ret = HTTP_RULE_RES_ABRT; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2865 | s->last_rule_file = rule->conf.file; |
| 2866 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2867 | goto end; |
| 2868 | case ACT_RET_INV: |
| 2869 | rule_ret = HTTP_RULE_RES_BADREQ; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2870 | s->last_rule_file = rule->conf.file; |
| 2871 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2872 | goto end; |
| 2873 | } |
| 2874 | continue; /* eval the next rule */ |
| 2875 | } |
| 2876 | |
| 2877 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2878 | switch (rule->action) { |
| 2879 | case ACT_ACTION_ALLOW: |
| 2880 | rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2881 | s->last_rule_file = rule->conf.file; |
| 2882 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2883 | goto end; |
| 2884 | |
| 2885 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2886 | txn->status = rule->arg.http_reply->status; |
| 2887 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 2888 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2889 | s->last_rule_file = rule->conf.file; |
| 2890 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2891 | goto end; |
| 2892 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2893 | case ACT_HTTP_REDIR: { |
| 2894 | int ret = http_apply_redirect_rule(rule->arg.redir, s, txn); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2895 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2896 | if (ret == 2) // 2 == skip |
| 2897 | break; |
| 2898 | |
| 2899 | rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2900 | s->last_rule_file = rule->conf.file; |
| 2901 | s->last_rule_line = rule->conf.line; |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2902 | goto end; |
| 2903 | } |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2904 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2905 | default: |
| 2906 | break; |
| 2907 | } |
| 2908 | } |
| 2909 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2910 | if (def_rules && s->current_rule_list == def_rules) { |
| 2911 | s->current_rule_list = rules; |
| 2912 | goto restart; |
| 2913 | } |
| 2914 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2915 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2916 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2917 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2918 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2919 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2920 | /* we reached the end of the rules, nothing to report */ |
| 2921 | return rule_ret; |
| 2922 | } |
| 2923 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2924 | /* Executes backend and frontend http-after-response rules for the stream <s>, |
| 2925 | * in that order. it return 1 on success and 0 on error. It is the caller |
| 2926 | * responsibility to catch error or ignore it. If it catches it, this function |
| 2927 | * may be called a second time, for the internal error. |
| 2928 | */ |
| 2929 | int http_eval_after_res_rules(struct stream *s) |
| 2930 | { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2931 | struct list *def_rules, *rules; |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2932 | struct session *sess = s->sess; |
| 2933 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 2934 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 2935 | /* Eval after-response ruleset only if the reply is not const */ |
| 2936 | if (s->txn->flags & TX_CONST_REPLY) |
| 2937 | goto end; |
| 2938 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2939 | /* prune the request variables if not already done and swap to the response variables. */ |
| 2940 | if (s->vars_reqres.scope != SCOPE_RES) { |
| 2941 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 2942 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | b7bfcb3 | 2021-08-31 08:13:25 +0200 | [diff] [blame] | 2943 | vars_init_head(&s->vars_reqres, SCOPE_RES); |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2944 | } |
| 2945 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2946 | def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL); |
| 2947 | rules = &s->be->http_after_res_rules; |
| 2948 | |
| 2949 | ret = http_res_get_intercept_rule(s->be, def_rules, rules, s); |
Christopher Faulet | 4c5a591 | 2021-11-09 17:48:39 +0100 | [diff] [blame] | 2950 | if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2951 | def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL); |
| 2952 | rules = &sess->fe->http_after_res_rules; |
| 2953 | ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s); |
| 2954 | } |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2955 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 2956 | end: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2957 | /* All other codes than CONTINUE, STOP or DONE are forbidden */ |
| 2958 | return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE); |
| 2959 | } |
| 2960 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 2961 | /* |
| 2962 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 2963 | * desirable to call it only when needed. This code is quite complex because |
| 2964 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 2965 | * highly recommended not to touch this part without a good reason ! |
| 2966 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2967 | 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] | 2968 | { |
| 2969 | struct session *sess = s->sess; |
| 2970 | struct http_txn *txn = s->txn; |
| 2971 | struct htx *htx; |
| 2972 | struct http_hdr_ctx ctx; |
| 2973 | char *hdr_beg, *hdr_end, *del_from; |
| 2974 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
| 2975 | int preserve_hdr; |
| 2976 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2977 | htx = htxbuf(&req->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 2978 | ctx.blk = NULL; |
| 2979 | while (http_find_header(htx, ist("Cookie"), &ctx, 1)) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 2980 | int is_first = 1; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 2981 | del_from = NULL; /* nothing to be deleted */ |
| 2982 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 2983 | |
| 2984 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 2985 | * attributes whose name begin with a '$', and associate them with |
| 2986 | * the right cookie, if we want to delete this cookie. |
| 2987 | * So there are 3 cases for each cookie read : |
| 2988 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 2989 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 2990 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 2991 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 2992 | * "special" cookie. |
| 2993 | * At the end of loop, if a "special" cookie remains, we may have to |
| 2994 | * remove it. If no application cookie persists in the header, we |
| 2995 | * *MUST* delete it. |
| 2996 | * |
| 2997 | * Note: RFC2965 is unclear about the processing of spaces around |
| 2998 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 2999 | * the RFC explicitly allows spaces before it, and not within the |
| 3000 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 3001 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 3002 | * after the equal sign too, resulting in some (rare) buggy |
| 3003 | * implementations trying to do that. So let's do what servers do. |
| 3004 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 3005 | * allowed quoted strings in values, with any possible character |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3006 | * after a backslash, including control chars and delimiters, which |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3007 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 3008 | * within values even without quotes. |
| 3009 | * |
| 3010 | * We have to keep multiple pointers in order to support cookie |
| 3011 | * removal at the beginning, middle or end of header without |
| 3012 | * corrupting the header. All of these headers are valid : |
| 3013 | * |
| 3014 | * hdr_beg hdr_end |
| 3015 | * | | |
| 3016 | * v | |
| 3017 | * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 | |
| 3018 | * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v |
| 3019 | * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3 |
| 3020 | * | | | | | | | |
| 3021 | * | | | | | | | |
| 3022 | * | | | | | | +--> next |
| 3023 | * | | | | | +----> val_end |
| 3024 | * | | | | +-----------> val_beg |
| 3025 | * | | | +--------------> equal |
| 3026 | * | | +----------------> att_end |
| 3027 | * | +---------------------> att_beg |
| 3028 | * +--------------------------> prev |
| 3029 | * |
| 3030 | */ |
| 3031 | hdr_beg = ctx.value.ptr; |
| 3032 | hdr_end = hdr_beg + ctx.value.len; |
| 3033 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3034 | /* Iterate through all cookies on this line */ |
| 3035 | |
| 3036 | /* find att_beg */ |
| 3037 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3038 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3039 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3040 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3041 | |
| 3042 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3043 | att_beg++; |
| 3044 | |
| 3045 | /* find att_end : this is the first character after the last non |
| 3046 | * space before the equal. It may be equal to hdr_end. |
| 3047 | */ |
| 3048 | equal = att_end = att_beg; |
| 3049 | while (equal < hdr_end) { |
| 3050 | if (*equal == '=' || *equal == ',' || *equal == ';') |
| 3051 | break; |
| 3052 | if (HTTP_IS_SPHT(*equal++)) |
| 3053 | continue; |
| 3054 | att_end = equal; |
| 3055 | } |
| 3056 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3057 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3058 | * is between <att_beg> and <equal>, both may be identical. |
| 3059 | */ |
| 3060 | /* look for end of cookie if there is an equal sign */ |
| 3061 | if (equal < hdr_end && *equal == '=') { |
| 3062 | /* look for the beginning of the value */ |
| 3063 | val_beg = equal + 1; |
| 3064 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3065 | val_beg++; |
| 3066 | |
| 3067 | /* find the end of the value, respecting quotes */ |
| 3068 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3069 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3070 | /* 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] | 3071 | val_end = next; |
| 3072 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3073 | val_end--; |
| 3074 | } |
| 3075 | else |
| 3076 | val_beg = val_end = next = equal; |
| 3077 | |
| 3078 | /* We have nothing to do with attributes beginning with |
| 3079 | * '$'. However, they will automatically be removed if a |
| 3080 | * header before them is removed, since they're supposed |
| 3081 | * to be linked together. |
| 3082 | */ |
| 3083 | if (*att_beg == '$') |
| 3084 | continue; |
| 3085 | |
| 3086 | /* Ignore cookies with no equal sign */ |
| 3087 | if (equal == next) { |
| 3088 | /* This is not our cookie, so we must preserve it. But if we already |
| 3089 | * scheduled another cookie for removal, we cannot remove the |
| 3090 | * complete header, but we can remove the previous block itself. |
| 3091 | */ |
| 3092 | preserve_hdr = 1; |
| 3093 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3094 | 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] | 3095 | val_end += delta; |
| 3096 | next += delta; |
| 3097 | hdr_end += delta; |
| 3098 | prev = del_from; |
| 3099 | del_from = NULL; |
| 3100 | } |
| 3101 | continue; |
| 3102 | } |
| 3103 | |
| 3104 | /* if there are spaces around the equal sign, we need to |
| 3105 | * strip them otherwise we'll get trouble for cookie captures, |
| 3106 | * or even for rewrites. Since this happens extremely rarely, |
| 3107 | * it does not hurt performance. |
| 3108 | */ |
| 3109 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3110 | int stripped_before = 0; |
| 3111 | int stripped_after = 0; |
| 3112 | |
| 3113 | if (att_end != equal) { |
| 3114 | memmove(att_end, equal, hdr_end - equal); |
| 3115 | stripped_before = (att_end - equal); |
| 3116 | equal += stripped_before; |
| 3117 | val_beg += stripped_before; |
| 3118 | } |
| 3119 | |
| 3120 | if (val_beg > equal + 1) { |
| 3121 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3122 | stripped_after = (equal + 1) - val_beg; |
| 3123 | val_beg += stripped_after; |
| 3124 | stripped_before += stripped_after; |
| 3125 | } |
| 3126 | |
| 3127 | val_end += stripped_before; |
| 3128 | next += stripped_before; |
| 3129 | hdr_end += stripped_before; |
| 3130 | } |
| 3131 | /* now everything is as on the diagram above */ |
| 3132 | |
| 3133 | /* First, let's see if we want to capture this cookie. We check |
| 3134 | * that we don't already have a client side cookie, because we |
| 3135 | * can only capture one. Also as an optimisation, we ignore |
| 3136 | * cookies shorter than the declared name. |
| 3137 | */ |
| 3138 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 3139 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3140 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3141 | int log_len = val_end - att_beg; |
| 3142 | |
| 3143 | if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3144 | ha_alert("HTTP logging : out of memory.\n"); |
| 3145 | } else { |
| 3146 | if (log_len > sess->fe->capture_len) |
| 3147 | log_len = sess->fe->capture_len; |
| 3148 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 3149 | txn->cli_cookie[log_len] = 0; |
| 3150 | } |
| 3151 | } |
| 3152 | |
| 3153 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 3154 | * following form : |
| 3155 | * |
| 3156 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
| 3157 | * |
| 3158 | * For cookies in prefix mode, the form is : |
| 3159 | * |
| 3160 | * Cookie: NAME=SRV~VALUE |
| 3161 | */ |
| 3162 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3163 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3164 | struct server *srv = s->be->srv; |
| 3165 | char *delim; |
| 3166 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3167 | /* 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] | 3168 | * have the server ID between val_beg and delim, and the original cookie between |
| 3169 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 3170 | * |
| 3171 | * hdr_beg |
| 3172 | * | |
| 3173 | * v |
| 3174 | * NAME=SRV; # in all but prefix modes |
| 3175 | * NAME=SRV~OPAQUE ; # in prefix mode |
| 3176 | * || || | |+-> next |
| 3177 | * || || | +--> val_end |
| 3178 | * || || +---------> delim |
| 3179 | * || |+------------> val_beg |
| 3180 | * || +-------------> att_end = equal |
| 3181 | * |+-----------------> att_beg |
| 3182 | * +------------------> prev |
| 3183 | * |
| 3184 | */ |
| 3185 | if (s->be->ck_opts & PR_CK_PFX) { |
| 3186 | for (delim = val_beg; delim < val_end; delim++) |
| 3187 | if (*delim == COOKIE_DELIM) |
| 3188 | break; |
| 3189 | } |
| 3190 | else { |
| 3191 | char *vbar1; |
| 3192 | delim = val_end; |
| 3193 | /* Now check if the cookie contains a date field, which would |
| 3194 | * appear after a vertical bar ('|') just after the server name |
| 3195 | * and before the delimiter. |
| 3196 | */ |
| 3197 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 3198 | if (vbar1) { |
| 3199 | /* OK, so left of the bar is the server's cookie and |
| 3200 | * right is the last seen date. It is a base64 encoded |
| 3201 | * 30-bit value representing the UNIX date since the |
| 3202 | * epoch in 4-second quantities. |
| 3203 | */ |
| 3204 | int val; |
| 3205 | delim = vbar1++; |
| 3206 | if (val_end - vbar1 >= 5) { |
| 3207 | val = b64tos30(vbar1); |
| 3208 | if (val > 0) |
| 3209 | txn->cookie_last_date = val << 2; |
| 3210 | } |
| 3211 | /* look for a second vertical bar */ |
| 3212 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 3213 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 3214 | val = b64tos30(vbar1 + 1); |
| 3215 | if (val > 0) |
| 3216 | txn->cookie_first_date = val << 2; |
| 3217 | } |
| 3218 | } |
| 3219 | } |
| 3220 | |
| 3221 | /* if the cookie has an expiration date and the proxy wants to check |
| 3222 | * it, then we do that now. We first check if the cookie is too old, |
| 3223 | * then only if it has expired. We detect strict overflow because the |
| 3224 | * time resolution here is not great (4 seconds). Cookies with dates |
| 3225 | * in the future are ignored if their offset is beyond one day. This |
| 3226 | * allows an admin to fix timezone issues without expiring everyone |
| 3227 | * and at the same time avoids keeping unwanted side effects for too |
| 3228 | * long. |
| 3229 | */ |
| 3230 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 3231 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) || |
| 3232 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
| 3233 | txn->flags &= ~TX_CK_MASK; |
| 3234 | txn->flags |= TX_CK_OLD; |
| 3235 | delim = val_beg; // let's pretend we have not found the cookie |
| 3236 | txn->cookie_first_date = 0; |
| 3237 | txn->cookie_last_date = 0; |
| 3238 | } |
| 3239 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 3240 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) || |
| 3241 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
| 3242 | txn->flags &= ~TX_CK_MASK; |
| 3243 | txn->flags |= TX_CK_EXPIRED; |
| 3244 | delim = val_beg; // let's pretend we have not found the cookie |
| 3245 | txn->cookie_first_date = 0; |
| 3246 | txn->cookie_last_date = 0; |
| 3247 | } |
| 3248 | |
| 3249 | /* Here, we'll look for the first running server which supports the cookie. |
| 3250 | * This allows to share a same cookie between several servers, for example |
| 3251 | * to dedicate backup servers to specific servers only. |
| 3252 | * However, to prevent clients from sticking to cookie-less backup server |
| 3253 | * when they have incidentely learned an empty cookie, we simply ignore |
| 3254 | * empty cookies and mark them as invalid. |
| 3255 | * The same behaviour is applied when persistence must be ignored. |
| 3256 | */ |
| 3257 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3258 | srv = NULL; |
| 3259 | |
| 3260 | while (srv) { |
| 3261 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 3262 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 3263 | if ((srv->cur_state != SRV_ST_STOPPED) || |
| 3264 | (s->be->options & PR_O_PERSIST) || |
| 3265 | (s->flags & SF_FORCE_PRST)) { |
| 3266 | /* we found the server and we can use it */ |
| 3267 | txn->flags &= ~TX_CK_MASK; |
| 3268 | txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN; |
| 3269 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
| 3270 | s->target = &srv->obj_type; |
| 3271 | break; |
| 3272 | } else { |
| 3273 | /* we found a server, but it's down, |
| 3274 | * mark it as such and go on in case |
| 3275 | * another one is available. |
| 3276 | */ |
| 3277 | txn->flags &= ~TX_CK_MASK; |
| 3278 | txn->flags |= TX_CK_DOWN; |
| 3279 | } |
| 3280 | } |
| 3281 | srv = srv->next; |
| 3282 | } |
| 3283 | |
| 3284 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
| 3285 | /* no server matched this cookie or we deliberately skipped it */ |
| 3286 | txn->flags &= ~TX_CK_MASK; |
| 3287 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3288 | txn->flags |= TX_CK_UNUSED; |
| 3289 | else |
| 3290 | txn->flags |= TX_CK_INVALID; |
| 3291 | } |
| 3292 | |
| 3293 | /* depending on the cookie mode, we may have to either : |
| 3294 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3295 | * the server never sees it ; |
| 3296 | * - 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] | 3297 | * application cookie so that it does not get accidentally removed later, |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3298 | * if we're in cookie prefix mode |
| 3299 | */ |
| 3300 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
| 3301 | int delta; /* negative */ |
| 3302 | |
| 3303 | memmove(val_beg, delim + 1, hdr_end - (delim + 1)); |
| 3304 | delta = val_beg - (delim + 1); |
| 3305 | val_end += delta; |
| 3306 | next += delta; |
| 3307 | hdr_end += delta; |
| 3308 | del_from = NULL; |
| 3309 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 3310 | } |
| 3311 | else if (del_from == NULL && |
| 3312 | (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) { |
| 3313 | del_from = prev; |
| 3314 | } |
| 3315 | } |
| 3316 | else { |
| 3317 | /* This is not our cookie, so we must preserve it. But if we already |
| 3318 | * scheduled another cookie for removal, we cannot remove the |
| 3319 | * complete header, but we can remove the previous block itself. |
| 3320 | */ |
| 3321 | preserve_hdr = 1; |
| 3322 | |
| 3323 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3324 | 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] | 3325 | if (att_beg >= del_from) |
| 3326 | att_beg += delta; |
| 3327 | if (att_end >= del_from) |
| 3328 | att_end += delta; |
| 3329 | val_beg += delta; |
| 3330 | val_end += delta; |
| 3331 | next += delta; |
| 3332 | hdr_end += delta; |
| 3333 | prev = del_from; |
| 3334 | del_from = NULL; |
| 3335 | } |
| 3336 | } |
| 3337 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3338 | } /* for each cookie */ |
| 3339 | |
| 3340 | |
| 3341 | /* There are no more cookies on this line. |
| 3342 | * We may still have one (or several) marked for deletion at the |
| 3343 | * end of the line. We must do this now in two ways : |
| 3344 | * - if some cookies must be preserved, we only delete from the |
| 3345 | * mark to the end of line ; |
| 3346 | * - if nothing needs to be preserved, simply delete the whole header |
| 3347 | */ |
| 3348 | if (del_from) { |
| 3349 | hdr_end = (preserve_hdr ? del_from : hdr_beg); |
| 3350 | } |
| 3351 | if ((hdr_end - hdr_beg) != ctx.value.len) { |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3352 | if (hdr_beg != hdr_end) |
| 3353 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3354 | else |
| 3355 | http_remove_header(htx, &ctx); |
| 3356 | } |
| 3357 | } /* for each "Cookie header */ |
| 3358 | } |
| 3359 | |
| 3360 | /* |
| 3361 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 3362 | * desirable to call it only when needed. This function is also used when we |
| 3363 | * just need to know if there is a cookie (eg: for check-cache). |
| 3364 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3365 | 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] | 3366 | { |
| 3367 | struct session *sess = s->sess; |
| 3368 | struct http_txn *txn = s->txn; |
| 3369 | struct htx *htx; |
| 3370 | struct http_hdr_ctx ctx; |
| 3371 | struct server *srv; |
| 3372 | char *hdr_beg, *hdr_end; |
| 3373 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3374 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3375 | htx = htxbuf(&res->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3376 | |
| 3377 | ctx.blk = NULL; |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3378 | while (http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3379 | int is_first = 1; |
| 3380 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3381 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 3382 | * <prev> points to the colon. |
| 3383 | */ |
| 3384 | txn->flags |= TX_SCK_PRESENT; |
| 3385 | |
| 3386 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 3387 | * check-cache is enabled) and we are not interested in checking |
| 3388 | * them. Warning, the cookie capture is declared in the frontend. |
| 3389 | */ |
| 3390 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
| 3391 | break; |
| 3392 | |
| 3393 | /* OK so now we know we have to process this response cookie. |
| 3394 | * The format of the Set-Cookie header is slightly different |
| 3395 | * from the format of the Cookie header in that it does not |
| 3396 | * support the comma as a cookie delimiter (thus the header |
| 3397 | * cannot be folded) because the Expires attribute described in |
| 3398 | * the original Netscape's spec may contain an unquoted date |
| 3399 | * with a comma inside. We have to live with this because |
| 3400 | * many browsers don't support Max-Age and some browsers don't |
| 3401 | * support quoted strings. However the Set-Cookie2 header is |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3402 | * clean but basically nobody supports it. |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3403 | * |
| 3404 | * We have to keep multiple pointers in order to support cookie |
| 3405 | * removal at the beginning, middle or end of header without |
| 3406 | * corrupting the header (in case of set-cookie2). A special |
| 3407 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 3408 | * fields after the first semi-colon. The <next> pointer points |
| 3409 | * either to the end of line (set-cookie) or next unquoted comma |
| 3410 | * (set-cookie2). All of these headers are valid : |
| 3411 | * |
| 3412 | * hdr_beg hdr_end |
| 3413 | * | | |
| 3414 | * v | |
| 3415 | * NAME1 = VALUE 1 ; Secure; Path="/" | |
| 3416 | * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v |
| 3417 | * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT |
| 3418 | * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard |
| 3419 | * | | | | | | | | |
| 3420 | * | | | | | | | +-> next |
| 3421 | * | | | | | | +------------> scav |
| 3422 | * | | | | | +--------------> val_end |
| 3423 | * | | | | +--------------------> val_beg |
| 3424 | * | | | +----------------------> equal |
| 3425 | * | | +------------------------> att_end |
| 3426 | * | +----------------------------> att_beg |
| 3427 | * +------------------------------> prev |
| 3428 | * -------------------------------> hdr_beg |
| 3429 | */ |
| 3430 | hdr_beg = ctx.value.ptr; |
| 3431 | hdr_end = hdr_beg + ctx.value.len; |
| 3432 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3433 | |
| 3434 | /* Iterate through all cookies on this line */ |
| 3435 | |
| 3436 | /* find att_beg */ |
| 3437 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3438 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3439 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3440 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3441 | |
| 3442 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3443 | att_beg++; |
| 3444 | |
| 3445 | /* find att_end : this is the first character after the last non |
| 3446 | * space before the equal. It may be equal to hdr_end. |
| 3447 | */ |
| 3448 | equal = att_end = att_beg; |
| 3449 | |
| 3450 | while (equal < hdr_end) { |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3451 | if (*equal == '=' || *equal == ';') |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3452 | break; |
| 3453 | if (HTTP_IS_SPHT(*equal++)) |
| 3454 | continue; |
| 3455 | att_end = equal; |
| 3456 | } |
| 3457 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3458 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3459 | * is between <att_beg> and <equal>, both may be identical. |
| 3460 | */ |
| 3461 | |
| 3462 | /* look for end of cookie if there is an equal sign */ |
| 3463 | if (equal < hdr_end && *equal == '=') { |
| 3464 | /* look for the beginning of the value */ |
| 3465 | val_beg = equal + 1; |
| 3466 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3467 | val_beg++; |
| 3468 | |
| 3469 | /* find the end of the value, respecting quotes */ |
| 3470 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3471 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3472 | /* 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] | 3473 | val_end = next; |
| 3474 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3475 | val_end--; |
| 3476 | } |
| 3477 | else { |
| 3478 | /* <equal> points to next comma, semi-colon or EOL */ |
| 3479 | val_beg = val_end = next = equal; |
| 3480 | } |
| 3481 | |
| 3482 | if (next < hdr_end) { |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3483 | /* For Set-Cookie, since commas are permitted |
| 3484 | * in values, skip to the end. |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3485 | */ |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3486 | next = hdr_end; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3487 | } |
| 3488 | |
| 3489 | /* Now everything is as on the diagram above */ |
| 3490 | |
| 3491 | /* Ignore cookies with no equal sign */ |
| 3492 | if (equal == val_end) |
| 3493 | continue; |
| 3494 | |
| 3495 | /* If there are spaces around the equal sign, we need to |
| 3496 | * strip them otherwise we'll get trouble for cookie captures, |
| 3497 | * or even for rewrites. Since this happens extremely rarely, |
| 3498 | * it does not hurt performance. |
| 3499 | */ |
| 3500 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3501 | int stripped_before = 0; |
| 3502 | int stripped_after = 0; |
| 3503 | |
| 3504 | if (att_end != equal) { |
| 3505 | memmove(att_end, equal, hdr_end - equal); |
| 3506 | stripped_before = (att_end - equal); |
| 3507 | equal += stripped_before; |
| 3508 | val_beg += stripped_before; |
| 3509 | } |
| 3510 | |
| 3511 | if (val_beg > equal + 1) { |
| 3512 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3513 | stripped_after = (equal + 1) - val_beg; |
| 3514 | val_beg += stripped_after; |
| 3515 | stripped_before += stripped_after; |
| 3516 | } |
| 3517 | |
| 3518 | val_end += stripped_before; |
| 3519 | next += stripped_before; |
| 3520 | hdr_end += stripped_before; |
| 3521 | |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3522 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3523 | ctx.value.len = hdr_end - hdr_beg; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3524 | } |
| 3525 | |
| 3526 | /* First, let's see if we want to capture this cookie. We check |
| 3527 | * that we don't already have a server side cookie, because we |
| 3528 | * can only capture one. Also as an optimisation, we ignore |
| 3529 | * cookies shorter than the declared name. |
| 3530 | */ |
| 3531 | if (sess->fe->capture_name != NULL && |
| 3532 | txn->srv_cookie == NULL && |
| 3533 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3534 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3535 | int log_len = val_end - att_beg; |
| 3536 | if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3537 | ha_alert("HTTP logging : out of memory.\n"); |
| 3538 | } |
| 3539 | else { |
| 3540 | if (log_len > sess->fe->capture_len) |
| 3541 | log_len = sess->fe->capture_len; |
| 3542 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 3543 | txn->srv_cookie[log_len] = 0; |
| 3544 | } |
| 3545 | } |
| 3546 | |
| 3547 | srv = objt_server(s->target); |
| 3548 | /* now check if we need to process it for persistence */ |
| 3549 | if (!(s->flags & SF_IGNORE_PRST) && |
| 3550 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3551 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3552 | /* assume passive cookie by default */ |
| 3553 | txn->flags &= ~TX_SCK_MASK; |
| 3554 | txn->flags |= TX_SCK_FOUND; |
| 3555 | |
| 3556 | /* If the cookie is in insert mode on a known server, we'll delete |
| 3557 | * this occurrence because we'll insert another one later. |
| 3558 | * We'll delete it too if the "indirect" option is set and we're in |
| 3559 | * a direct access. |
| 3560 | */ |
| 3561 | if (s->be->ck_opts & PR_CK_PSV) { |
| 3562 | /* The "preserve" flag was set, we don't want to touch the |
| 3563 | * server's cookie. |
| 3564 | */ |
| 3565 | } |
| 3566 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
| 3567 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
| 3568 | /* this cookie must be deleted */ |
| 3569 | if (prev == hdr_beg && next == hdr_end) { |
| 3570 | /* whole header */ |
| 3571 | http_remove_header(htx, &ctx); |
| 3572 | /* note: while both invalid now, <next> and <hdr_end> |
| 3573 | * are still equal, so the for() will stop as expected. |
| 3574 | */ |
| 3575 | } else { |
| 3576 | /* just remove the value */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3577 | int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3578 | next = prev; |
| 3579 | hdr_end += delta; |
| 3580 | } |
| 3581 | txn->flags &= ~TX_SCK_MASK; |
| 3582 | txn->flags |= TX_SCK_DELETED; |
| 3583 | /* and go on with next cookie */ |
| 3584 | } |
| 3585 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
| 3586 | /* replace bytes val_beg->val_end with the cookie name associated |
| 3587 | * with this server since we know it. |
| 3588 | */ |
| 3589 | int sliding, delta; |
| 3590 | |
| 3591 | ctx.value = ist2(val_beg, val_end - val_beg); |
| 3592 | ctx.lws_before = ctx.lws_after = 0; |
| 3593 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen)); |
| 3594 | delta = srv->cklen - (val_end - val_beg); |
| 3595 | sliding = (ctx.value.ptr - val_beg); |
| 3596 | hdr_beg += sliding; |
| 3597 | val_beg += sliding; |
| 3598 | next += sliding + delta; |
| 3599 | hdr_end += sliding + delta; |
| 3600 | |
| 3601 | txn->flags &= ~TX_SCK_MASK; |
| 3602 | txn->flags |= TX_SCK_REPLACED; |
| 3603 | } |
| 3604 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
| 3605 | /* insert the cookie name associated with this server |
| 3606 | * before existing cookie, and insert a delimiter between them.. |
| 3607 | */ |
| 3608 | int sliding, delta; |
| 3609 | ctx.value = ist2(val_beg, 0); |
| 3610 | ctx.lws_before = ctx.lws_after = 0; |
| 3611 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1)); |
| 3612 | delta = srv->cklen + 1; |
| 3613 | sliding = (ctx.value.ptr - val_beg); |
| 3614 | hdr_beg += sliding; |
| 3615 | val_beg += sliding; |
| 3616 | next += sliding + delta; |
| 3617 | hdr_end += sliding + delta; |
| 3618 | |
| 3619 | val_beg[srv->cklen] = COOKIE_DELIM; |
| 3620 | txn->flags &= ~TX_SCK_MASK; |
| 3621 | txn->flags |= TX_SCK_REPLACED; |
| 3622 | } |
| 3623 | } |
| 3624 | /* that's done for this cookie, check the next one on the same |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3625 | * line when next != hdr_end (which should normally not happen |
| 3626 | * with set-cookie2 support removed). |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3627 | */ |
| 3628 | } |
| 3629 | } |
| 3630 | } |
| 3631 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3632 | /* |
| 3633 | * Parses the Cache-Control and Pragma request header fields to determine if |
| 3634 | * the request may be served from the cache and/or if it is cacheable. Updates |
| 3635 | * s->txn->flags. |
| 3636 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3637 | void http_check_request_for_cacheability(struct stream *s, struct channel *req) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3638 | { |
| 3639 | struct http_txn *txn = s->txn; |
| 3640 | struct htx *htx; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3641 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 3642 | int pragma_found, cc_found; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3643 | |
| 3644 | if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE) |
| 3645 | return; /* nothing more to do here */ |
| 3646 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3647 | htx = htxbuf(&req->buf); |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3648 | pragma_found = cc_found = 0; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3649 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3650 | /* Check "pragma" header for HTTP/1.0 compatibility. */ |
| 3651 | if (http_find_header(htx, ist("pragma"), &ctx, 1)) { |
| 3652 | if (isteqi(ctx.value, ist("no-cache"))) { |
| 3653 | pragma_found = 1; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3654 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3655 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3656 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3657 | ctx.blk = NULL; |
| 3658 | /* Don't use the cache and don't try to store if we found the |
| 3659 | * Authorization header */ |
| 3660 | if (http_find_header(htx, ist("authorization"), &ctx, 1)) { |
| 3661 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3662 | txn->flags |= TX_CACHE_IGNORE; |
| 3663 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3664 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3665 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3666 | /* Look for "cache-control" header and iterate over all the values |
| 3667 | * until we find one that specifies that caching is possible or not. */ |
| 3668 | ctx.blk = NULL; |
| 3669 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3670 | cc_found = 1; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3671 | /* We don't check the values after max-age, max-stale nor min-fresh, |
| 3672 | * we simply don't use the cache when they're specified. */ |
| 3673 | if (istmatchi(ctx.value, ist("max-age")) || |
| 3674 | istmatchi(ctx.value, ist("no-cache")) || |
| 3675 | istmatchi(ctx.value, ist("max-stale")) || |
| 3676 | istmatchi(ctx.value, ist("min-fresh"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3677 | txn->flags |= TX_CACHE_IGNORE; |
| 3678 | continue; |
| 3679 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3680 | if (istmatchi(ctx.value, ist("no-store"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3681 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3682 | continue; |
| 3683 | } |
| 3684 | } |
| 3685 | |
| 3686 | /* RFC7234#5.4: |
| 3687 | * When the Cache-Control header field is also present and |
| 3688 | * understood in a request, Pragma is ignored. |
| 3689 | * When the Cache-Control header field is not present in a |
| 3690 | * request, caches MUST consider the no-cache request |
| 3691 | * pragma-directive as having the same effect as if |
| 3692 | * "Cache-Control: no-cache" were present. |
| 3693 | */ |
| 3694 | if (!cc_found && pragma_found) |
| 3695 | txn->flags |= TX_CACHE_IGNORE; |
| 3696 | } |
| 3697 | |
| 3698 | /* |
| 3699 | * Check if response is cacheable or not. Updates s->txn->flags. |
| 3700 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3701 | void http_check_response_for_cacheability(struct stream *s, struct channel *res) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3702 | { |
| 3703 | struct http_txn *txn = s->txn; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3704 | struct http_hdr_ctx ctx = { .blk = NULL }; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3705 | struct htx *htx; |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3706 | int has_freshness_info = 0; |
| 3707 | int has_validator = 0; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3708 | |
| 3709 | if (txn->status < 200) { |
| 3710 | /* do not try to cache interim responses! */ |
| 3711 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3712 | return; |
| 3713 | } |
| 3714 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3715 | htx = htxbuf(&res->buf); |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3716 | /* Check "pragma" header for HTTP/1.0 compatibility. */ |
| 3717 | if (http_find_header(htx, ist("pragma"), &ctx, 1)) { |
| 3718 | if (isteqi(ctx.value, ist("no-cache"))) { |
| 3719 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3720 | return; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3721 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3722 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3723 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3724 | /* Look for "cache-control" header and iterate over all the values |
| 3725 | * until we find one that specifies that caching is possible or not. */ |
| 3726 | ctx.blk = NULL; |
| 3727 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
| 3728 | if (isteqi(ctx.value, ist("public"))) { |
| 3729 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3730 | continue; |
| 3731 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3732 | if (isteqi(ctx.value, ist("private")) || |
| 3733 | isteqi(ctx.value, ist("no-cache")) || |
| 3734 | isteqi(ctx.value, ist("no-store")) || |
| 3735 | isteqi(ctx.value, ist("max-age=0")) || |
| 3736 | isteqi(ctx.value, ist("s-maxage=0"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3737 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3738 | continue; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3739 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3740 | /* We might have a no-cache="set-cookie" form. */ |
| 3741 | if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) { |
| 3742 | txn->flags &= ~TX_CACHE_COOK; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3743 | continue; |
| 3744 | } |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3745 | |
| 3746 | if (istmatchi(ctx.value, ist("s-maxage")) || |
| 3747 | istmatchi(ctx.value, ist("max-age"))) { |
| 3748 | has_freshness_info = 1; |
| 3749 | continue; |
| 3750 | } |
| 3751 | } |
| 3752 | |
| 3753 | /* If no freshness information could be found in Cache-Control values, |
| 3754 | * look for an Expires header. */ |
| 3755 | if (!has_freshness_info) { |
| 3756 | ctx.blk = NULL; |
| 3757 | has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0); |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3758 | } |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3759 | |
| 3760 | /* If no freshness information could be found in Cache-Control or Expires |
| 3761 | * values, look for an explicit validator. */ |
| 3762 | if (!has_freshness_info) { |
| 3763 | ctx.blk = NULL; |
| 3764 | has_validator = 1; |
| 3765 | if (!http_find_header(htx, ist("etag"), &ctx, 0)) { |
| 3766 | ctx.blk = NULL; |
| 3767 | if (!http_find_header(htx, ist("last-modified"), &ctx, 0)) |
| 3768 | has_validator = 0; |
| 3769 | } |
| 3770 | } |
| 3771 | |
| 3772 | /* We won't store an entry that has neither a cache validator nor an |
| 3773 | * explicit expiration time, as suggested in RFC 7234#3. */ |
| 3774 | if (!has_freshness_info && !has_validator) |
Remi Tricot-Le Breton | 879debe | 2023-02-21 11:47:17 +0100 | [diff] [blame] | 3775 | txn->flags &= ~TX_CACHEABLE; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3776 | } |
| 3777 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3778 | /* |
| 3779 | * In a GET, HEAD or POST request, check if the requested URI matches the stats uri |
| 3780 | * for the current backend. |
| 3781 | * |
| 3782 | * It is assumed that the request is either a HEAD, GET, or POST and that the |
| 3783 | * uri_auth field is valid. |
| 3784 | * |
| 3785 | * Returns 1 if stats should be provided, otherwise 0. |
| 3786 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3787 | 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] | 3788 | { |
| 3789 | struct uri_auth *uri_auth = backend->uri_auth; |
| 3790 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3791 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3792 | struct ist uri; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3793 | |
| 3794 | if (!uri_auth) |
| 3795 | return 0; |
| 3796 | |
| 3797 | if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST) |
| 3798 | return 0; |
| 3799 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3800 | htx = htxbuf(&s->req.buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 3801 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3802 | uri = htx_sl_req_uri(sl); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 3803 | if (*uri_auth->uri_prefix == '/') { |
| 3804 | struct http_uri_parser parser = http_uri_parser_init(uri); |
| 3805 | uri = http_parse_path(&parser); |
| 3806 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3807 | |
| 3808 | /* check URI size */ |
| 3809 | if (uri_auth->uri_len > uri.len) |
| 3810 | return 0; |
| 3811 | |
| 3812 | if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
| 3813 | return 0; |
| 3814 | |
| 3815 | return 1; |
| 3816 | } |
| 3817 | |
| 3818 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3819 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3820 | * and program a response message if something was unexpected. It cannot fail |
| 3821 | * and always relies on the stats applet to complete the job. It does not touch |
| 3822 | * analysers nor counters, which are left to the caller. It does not touch |
| 3823 | * s->target which is supposed to already point to the stats applet. The caller |
| 3824 | * is expected to have already assigned an appctx to the stream. |
| 3825 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3826 | static int http_handle_stats(struct stream *s, struct channel *req) |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3827 | { |
| 3828 | struct stats_admin_rule *stats_admin_rule; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3829 | struct session *sess = s->sess; |
| 3830 | struct http_txn *txn = s->txn; |
| 3831 | struct http_msg *msg = &txn->req; |
| 3832 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3833 | const char *h, *lookup, *end; |
Willy Tarreau | 8e7c6e6 | 2022-05-18 17:58:02 +0200 | [diff] [blame] | 3834 | struct appctx *appctx = __sc_appctx(s->scb); |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3835 | struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3836 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3837 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3838 | |
Willy Tarreau | 41f8852 | 2022-05-03 18:39:27 +0200 | [diff] [blame] | 3839 | appctx->st1 = 0; |
Willy Tarreau | 6ef1648 | 2022-05-06 18:07:53 +0200 | [diff] [blame] | 3840 | ctx->state = STAT_STATE_INIT; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3841 | ctx->st_code = STAT_STATUS_INIT; |
| 3842 | ctx->flags |= uri_auth->flags; |
| 3843 | ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3844 | if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD)) |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3845 | ctx->flags |= STAT_CHUNKED; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3846 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3847 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 3848 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3849 | lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len; |
| 3850 | end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3851 | |
| 3852 | for (h = lookup; h <= end - 3; h++) { |
| 3853 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3854 | ctx->flags |= STAT_HIDE_DOWN; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3855 | break; |
| 3856 | } |
Amaury Denoyelle | 91e55ea | 2021-02-25 14:46:08 +0100 | [diff] [blame] | 3857 | } |
| 3858 | |
| 3859 | for (h = lookup; h <= end - 9; h++) { |
| 3860 | if (memcmp(h, ";no-maint", 9) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3861 | ctx->flags |= STAT_HIDE_MAINT; |
Willy Tarreau | 3e32036 | 2020-10-23 17:28:57 +0200 | [diff] [blame] | 3862 | break; |
| 3863 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3864 | } |
| 3865 | |
| 3866 | if (uri_auth->refresh) { |
| 3867 | for (h = lookup; h <= end - 10; h++) { |
| 3868 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3869 | ctx->flags |= STAT_NO_REFRESH; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3870 | break; |
| 3871 | } |
| 3872 | } |
| 3873 | } |
| 3874 | |
| 3875 | for (h = lookup; h <= end - 4; h++) { |
| 3876 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3877 | ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3878 | break; |
| 3879 | } |
| 3880 | } |
| 3881 | |
| 3882 | for (h = lookup; h <= end - 6; h++) { |
| 3883 | if (memcmp(h, ";typed", 6) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3884 | ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
| 3885 | ctx->flags |= STAT_FMT_TYPED; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3886 | break; |
| 3887 | } |
| 3888 | } |
| 3889 | |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 3890 | for (h = lookup; h <= end - 5; h++) { |
| 3891 | if (memcmp(h, ";json", 5) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3892 | ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
| 3893 | ctx->flags |= STAT_FMT_JSON; |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 3894 | break; |
| 3895 | } |
| 3896 | } |
| 3897 | |
| 3898 | for (h = lookup; h <= end - 12; h++) { |
| 3899 | if (memcmp(h, ";json-schema", 12) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3900 | ctx->flags &= ~STAT_FMT_MASK; |
| 3901 | ctx->flags |= STAT_JSON_SCHM; |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 3902 | break; |
| 3903 | } |
| 3904 | } |
| 3905 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3906 | for (h = lookup; h <= end - 8; h++) { |
| 3907 | if (memcmp(h, ";st=", 4) == 0) { |
| 3908 | int i; |
| 3909 | h += 4; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3910 | ctx->st_code = STAT_STATUS_UNKN; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3911 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3912 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3913 | ctx->st_code = i; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3914 | break; |
| 3915 | } |
| 3916 | } |
| 3917 | break; |
| 3918 | } |
| 3919 | } |
| 3920 | |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3921 | ctx->scope_str = 0; |
| 3922 | ctx->scope_len = 0; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3923 | for (h = lookup; h <= end - 8; h++) { |
| 3924 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3925 | int itx = 0; |
| 3926 | const char *h2; |
| 3927 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3928 | const char *err; |
| 3929 | |
| 3930 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3931 | h2 = h; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3932 | ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl); |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 3933 | while (h < end) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3934 | if (*h == ';' || *h == '&' || *h == ' ') |
| 3935 | break; |
| 3936 | itx++; |
| 3937 | h++; |
| 3938 | } |
| 3939 | |
| 3940 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3941 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3942 | ctx->scope_len = itx; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3943 | |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3944 | /* scope_txt = search query, ctx->scope_len is always <= STAT_SCOPE_TXT_MAXLEN */ |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3945 | memcpy(scope_txt, h2, itx); |
| 3946 | scope_txt[itx] = '\0'; |
| 3947 | err = invalid_char(scope_txt); |
| 3948 | if (err) { |
| 3949 | /* bad char in search text => clear scope */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3950 | ctx->scope_str = 0; |
| 3951 | ctx->scope_len = 0; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3952 | } |
| 3953 | break; |
| 3954 | } |
| 3955 | } |
| 3956 | |
| 3957 | /* now check whether we have some admin rules for this request */ |
| 3958 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
| 3959 | int ret = 1; |
| 3960 | |
| 3961 | if (stats_admin_rule->cond) { |
| 3962 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 3963 | ret = acl_pass(ret); |
| 3964 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3965 | ret = !ret; |
| 3966 | } |
| 3967 | |
| 3968 | if (ret) { |
| 3969 | /* no rule, or the rule matches */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3970 | ctx->flags |= STAT_ADMIN; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3971 | break; |
| 3972 | } |
| 3973 | } |
| 3974 | |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 3975 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 3976 | appctx->st0 = STAT_HTTP_HEAD; |
| 3977 | else if (txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3978 | if (ctx->flags & STAT_ADMIN) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3979 | appctx->st0 = STAT_HTTP_POST; |
Christopher Faulet | bd9e842 | 2019-08-15 22:26:48 +0200 | [diff] [blame] | 3980 | if (msg->msg_state < HTTP_MSG_DATA) |
| 3981 | req->analysers |= AN_REQ_HTTP_BODY; |
| 3982 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3983 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 3984 | /* POST without admin level */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3985 | ctx->flags &= ~STAT_CHUNKED; |
| 3986 | ctx->st_code = STAT_STATUS_DENY; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3987 | appctx->st0 = STAT_HTTP_LAST; |
| 3988 | } |
| 3989 | } |
| 3990 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 3991 | /* Unsupported method */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3992 | ctx->flags &= ~STAT_CHUNKED; |
| 3993 | ctx->st_code = STAT_STATUS_IVAL; |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 3994 | appctx->st0 = STAT_HTTP_LAST; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3995 | } |
| 3996 | |
| 3997 | s->task->nice = -32; /* small boost for HTTP statistics */ |
| 3998 | return 1; |
| 3999 | } |
| 4000 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4001 | /* This function waits for the message payload at most <time> milliseconds (may |
| 4002 | * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the |
| 4003 | * payload are received (0 means no limit). It returns HTTP_RULE_* depending on |
| 4004 | * the result: |
| 4005 | * |
| 4006 | * - HTTP_RULE_RES_CONT when conditions are met to stop waiting |
| 4007 | * - HTTP_RULE_RES_YIELD to wait for more data |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 4008 | * - HTTP_RULE_RES_ABRT when a timeout occurred. |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4009 | * - 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] | 4010 | * - HTTP_RULE_RES_ERROR if an internal error occurred |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4011 | * |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 4012 | * 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] | 4013 | * to the client, depending on the channel (408 on request side, 504 on response |
| 4014 | * side). All other errors must be handled by the caller. |
| 4015 | */ |
| 4016 | enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn, |
| 4017 | unsigned int time, unsigned int bytes) |
| 4018 | { |
| 4019 | struct session *sess = s->sess; |
| 4020 | struct http_txn *txn = s->txn; |
| 4021 | struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req); |
| 4022 | struct htx *htx; |
| 4023 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 4024 | |
| 4025 | htx = htxbuf(&chn->buf); |
| 4026 | |
| 4027 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
| 4028 | ret = HTTP_RULE_RES_BADREQ; |
| 4029 | goto end; |
| 4030 | } |
| 4031 | if (htx->flags & HTX_FL_PROCESSING_ERROR) { |
| 4032 | ret = HTTP_RULE_RES_ERROR; |
| 4033 | goto end; |
| 4034 | } |
| 4035 | |
| 4036 | /* Do nothing for bodyless and CONNECT requests */ |
| 4037 | if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS)) |
| 4038 | goto end; |
| 4039 | |
| 4040 | if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) { |
| 4041 | if (http_handle_expect_hdr(s, htx, msg) == -1) { |
| 4042 | ret = HTTP_RULE_RES_ERROR; |
| 4043 | goto end; |
| 4044 | } |
| 4045 | } |
| 4046 | |
| 4047 | msg->msg_state = HTTP_MSG_DATA; |
| 4048 | |
| 4049 | /* Now we're in HTTP_MSG_DATA. We just need to know if all data have |
| 4050 | * been received or if the buffer is full. |
| 4051 | */ |
Christopher Faulet | 7833596 | 2021-09-23 14:46:32 +0200 | [diff] [blame] | 4052 | if ((htx->flags & HTX_FL_EOM) || |
| 4053 | htx_get_tail_type(htx) > HTX_BLK_DATA || |
| 4054 | channel_htx_full(chn, htx, global.tune.maxrewrite) || |
Willy Tarreau | 99615ed | 2022-05-25 07:29:36 +0200 | [diff] [blame] | 4055 | sc_waiting_room(chn_prod(chn))) |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4056 | goto end; |
| 4057 | |
| 4058 | if (bytes) { |
| 4059 | struct htx_blk *blk; |
| 4060 | unsigned int len = 0; |
| 4061 | |
| 4062 | for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 4063 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 4064 | continue; |
| 4065 | len += htx_get_blksz(blk); |
| 4066 | if (len >= bytes) |
| 4067 | goto end; |
| 4068 | } |
| 4069 | } |
| 4070 | |
| 4071 | if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) { |
| 4072 | if (!(chn->flags & CF_ISRESP)) |
| 4073 | goto abort_req; |
| 4074 | goto abort_res; |
| 4075 | } |
| 4076 | |
| 4077 | /* we get here if we need to wait for more data */ |
Christopher Faulet | 4b490b7 | 2023-01-04 11:55:15 +0100 | [diff] [blame] | 4078 | if (!(chn->flags & CF_SHUTR)) { |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4079 | if (!tick_isset(chn->analyse_exp)) |
| 4080 | chn->analyse_exp = tick_add_ifset(now_ms, time); |
| 4081 | ret = HTTP_RULE_RES_YIELD; |
| 4082 | } |
| 4083 | |
| 4084 | end: |
| 4085 | return ret; |
| 4086 | |
Christopher Faulet | f0d80df | 2023-01-13 10:20:20 +0100 | [diff] [blame] | 4087 | abort: |
| 4088 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 4089 | ret = HTTP_RULE_RES_ABRT; |
| 4090 | goto end; |
| 4091 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4092 | abort_req: |
| 4093 | txn->status = 408; |
| 4094 | if (!(s->flags & SF_ERR_MASK)) |
| 4095 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4096 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4097 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4098 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | f0d80df | 2023-01-13 10:20:20 +0100 | [diff] [blame] | 4099 | goto abort; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4100 | |
| 4101 | abort_res: |
| 4102 | txn->status = 504; |
| 4103 | if (!(s->flags & SF_ERR_MASK)) |
| 4104 | s->flags |= SF_ERR_SRVTO; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4105 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | f0d80df | 2023-01-13 10:20:20 +0100 | [diff] [blame] | 4106 | goto abort; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4107 | } |
| 4108 | |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4109 | void http_perform_server_redirect(struct stream *s, struct stconn *sc) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4110 | { |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4111 | struct channel *req = &s->req; |
| 4112 | struct channel *res = &s->res; |
| 4113 | struct server *srv; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4114 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4115 | struct htx_sl *sl; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4116 | struct ist path, location; |
| 4117 | unsigned int flags; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4118 | struct http_uri_parser parser; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4119 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4120 | /* |
| 4121 | * Create the location |
| 4122 | */ |
| 4123 | chunk_reset(&trash); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4124 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4125 | /* 1: add the server's prefix */ |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4126 | /* special prefix "/" means don't change URL */ |
| 4127 | srv = __objt_server(s->target); |
| 4128 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 4129 | if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len)) |
| 4130 | return; |
| 4131 | } |
| 4132 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4133 | /* 2: add the request Path */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4134 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 4135 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4136 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4137 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4138 | if (!isttest(path)) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4139 | return; |
| 4140 | |
| 4141 | if (!chunk_memcat(&trash, path.ptr, path.len)) |
| 4142 | return; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4143 | location = ist2(trash.area, trash.data); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4144 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4145 | /* |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 4146 | * Create the 302 response |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4147 | */ |
| 4148 | htx = htx_from_buf(&res->buf); |
| 4149 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 4150 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4151 | ist("HTTP/1.1"), ist("302"), ist("Found")); |
| 4152 | if (!sl) |
| 4153 | goto fail; |
| 4154 | sl->info.res.status = 302; |
| 4155 | s->txn->status = 302; |
| 4156 | |
| 4157 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) || |
| 4158 | !htx_add_header(htx, ist("Connection"), ist("close")) || |
| 4159 | !htx_add_header(htx, ist("Content-length"), ist("0")) || |
| 4160 | !htx_add_header(htx, ist("Location"), location)) |
| 4161 | goto fail; |
| 4162 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4163 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4164 | goto fail; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4165 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4166 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | c20afb8 | 2020-01-24 19:16:26 +0100 | [diff] [blame] | 4167 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4168 | if (!http_forward_proxy_resp(s, 1)) |
| 4169 | goto fail; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4170 | |
| 4171 | /* return without error. */ |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4172 | sc_shutr(sc); |
| 4173 | sc_shutw(sc); |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4174 | s->conn_err_type = STRM_ET_NONE; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4175 | sc->state = SC_ST_CLO; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4176 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4177 | if (!(s->flags & SF_ERR_MASK)) |
| 4178 | s->flags |= SF_ERR_LOCAL; |
| 4179 | if (!(s->flags & SF_FINST_MASK)) |
| 4180 | s->flags |= SF_FINST_C; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4181 | |
| 4182 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 4183 | srv_inc_sess_ctr(srv); |
| 4184 | srv_set_sess_last(srv); |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4185 | return; |
| 4186 | |
| 4187 | fail: |
| 4188 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4189 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4190 | channel_htx_truncate(res, htx); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4191 | } |
| 4192 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4193 | /* This function terminates the request because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4194 | * because an error was triggered during the body forwarding. |
| 4195 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4196 | static void http_end_request(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4197 | { |
| 4198 | struct channel *chn = &s->req; |
| 4199 | struct http_txn *txn = s->txn; |
| 4200 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4201 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4202 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4203 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) { |
| 4204 | 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] | 4205 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4206 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4207 | |
| 4208 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4209 | /* No need to read anymore, the request was completely parsed. |
| 4210 | * We can shut the read side unless we want to abort_on_close, |
| 4211 | * or we have a POST request. The issue with POST requests is |
| 4212 | * that some browsers still send a CRLF after the request, and |
| 4213 | * this CRLF must be read so that it does not remain in the kernel |
| 4214 | * buffers, otherwise a close could cause an RST on some systems |
| 4215 | * (eg: Linux). |
| 4216 | */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 4217 | 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] | 4218 | channel_dont_read(chn); |
| 4219 | |
| 4220 | /* if the server closes the connection, we want to immediately react |
| 4221 | * and close the socket to save packets and syscalls. |
| 4222 | */ |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 4223 | s->scb->flags |= SC_FL_NOHALF; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4224 | |
| 4225 | /* In any case we've finished parsing the request so we must |
| 4226 | * disable Nagle when sending data because 1) we're not going |
| 4227 | * to shut this side, and 2) the server is waiting for us to |
| 4228 | * send pending data. |
| 4229 | */ |
| 4230 | chn->flags |= CF_NEVER_WAIT; |
| 4231 | |
Christopher Faulet | af12436 | 2023-02-14 10:48:02 +0100 | [diff] [blame] | 4232 | if (txn->rsp.msg_state < HTTP_MSG_DONE && s->scb->state != SC_ST_CLO) { |
| 4233 | /* The server has not finished to respond and the |
| 4234 | * backend SC is not closed, so we don't want to move in |
| 4235 | * order not to upset it. |
Christopher Faulet | d01ce40 | 2019-01-02 17:44:13 +0100 | [diff] [blame] | 4236 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4237 | 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] | 4238 | return; |
| 4239 | } |
| 4240 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4241 | /* When we get here, it means that both the request and the |
| 4242 | * response have finished receiving. Depending on the connection |
| 4243 | * mode, we'll have to wait for the last bytes to leave in either |
| 4244 | * direction, and sometimes for a close to be effective. |
| 4245 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4246 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4247 | /* Tunnel mode will not have any analyser so it needs to |
| 4248 | * poll for reads. |
| 4249 | */ |
| 4250 | channel_auto_read(chn); |
| 4251 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 4252 | } |
| 4253 | else { |
| 4254 | /* we're not expecting any new data to come for this |
| 4255 | * transaction, so we can close it. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4256 | * |
| 4257 | * However, there is an exception if the response |
| 4258 | * length is undefined. In this case, we need to wait |
| 4259 | * the close from the server. The response will be |
| 4260 | * switched in TUNNEL mode until the end. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4261 | */ |
| 4262 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 4263 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4264 | goto check_channel_flags; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4265 | |
| 4266 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4267 | channel_shutr_now(chn); |
| 4268 | channel_shutw_now(chn); |
| 4269 | } |
| 4270 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4271 | goto check_channel_flags; |
| 4272 | } |
| 4273 | |
| 4274 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4275 | http_msg_closing: |
| 4276 | /* nothing else to forward, just waiting for the output buffer |
| 4277 | * to be empty and for the shutw_now to take effect. |
| 4278 | */ |
| 4279 | if (channel_is_empty(chn)) { |
| 4280 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4281 | goto http_msg_closed; |
| 4282 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4283 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4284 | return; |
| 4285 | } |
| 4286 | |
| 4287 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4288 | http_msg_closed: |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4289 | /* if we don't know whether the server will close, we need to hard close */ |
| 4290 | if (txn->rsp.flags & HTTP_MSGF_XFER_LEN) |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 4291 | s->scb->flags |= SC_FL_NOLINGER; /* we want to close ASAP */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4292 | /* 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] | 4293 | if (!(s->be->options & PR_O_ABRT_CLOSE)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4294 | channel_dont_read(chn); |
| 4295 | goto end; |
| 4296 | } |
| 4297 | |
| 4298 | check_channel_flags: |
| 4299 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4300 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4301 | /* if we've just closed an output, let's switch */ |
| 4302 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4303 | goto http_msg_closing; |
| 4304 | } |
| 4305 | |
| 4306 | end: |
| 4307 | chn->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4308 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4309 | chn->flags |= CF_NEVER_WAIT; |
| 4310 | if (HAS_REQ_DATA_FILTERS(s)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4311 | chn->analysers |= AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4312 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4313 | channel_auto_close(chn); |
| 4314 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4315 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4316 | } |
| 4317 | |
| 4318 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4319 | /* This function terminates the response because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4320 | * because an error was triggered during the body forwarding. |
| 4321 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4322 | static void http_end_response(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4323 | { |
| 4324 | struct channel *chn = &s->res; |
| 4325 | struct http_txn *txn = s->txn; |
| 4326 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4327 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4328 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4329 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) { |
| 4330 | 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] | 4331 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4332 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4333 | |
| 4334 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4335 | /* In theory, we don't need to read anymore, but we must |
| 4336 | * still monitor the server connection for a possible close |
| 4337 | * while the request is being uploaded, so we don't disable |
| 4338 | * reading. |
| 4339 | */ |
| 4340 | /* channel_dont_read(chn); */ |
| 4341 | |
Christopher Faulet | af12436 | 2023-02-14 10:48:02 +0100 | [diff] [blame] | 4342 | if (txn->req.msg_state < HTTP_MSG_DONE && s->scf->state != SC_ST_CLO) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4343 | /* The client seems to still be sending data, probably |
| 4344 | * because we got an error response during an upload. |
| 4345 | * We have the choice of either breaking the connection |
| 4346 | * or letting it pass through. Let's do the later. |
| 4347 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4348 | 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] | 4349 | return; |
| 4350 | } |
| 4351 | |
| 4352 | /* When we get here, it means that both the request and the |
| 4353 | * response have finished receiving. Depending on the connection |
| 4354 | * mode, we'll have to wait for the last bytes to leave in either |
| 4355 | * direction, and sometimes for a close to be effective. |
| 4356 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4357 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4358 | channel_auto_read(chn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4359 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4360 | } |
| 4361 | else { |
| 4362 | /* we're not expecting any new data to come for this |
| 4363 | * transaction, so we can close it. |
| 4364 | */ |
| 4365 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4366 | channel_shutr_now(chn); |
| 4367 | channel_shutw_now(chn); |
| 4368 | } |
| 4369 | } |
| 4370 | goto check_channel_flags; |
| 4371 | } |
| 4372 | |
| 4373 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4374 | http_msg_closing: |
| 4375 | /* nothing else to forward, just waiting for the output buffer |
| 4376 | * to be empty and for the shutw_now to take effect. |
| 4377 | */ |
| 4378 | if (channel_is_empty(chn)) { |
| 4379 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4380 | goto http_msg_closed; |
| 4381 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4382 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4383 | return; |
| 4384 | } |
| 4385 | |
| 4386 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4387 | http_msg_closed: |
| 4388 | /* drop any pending data */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4389 | channel_htx_truncate(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4390 | channel_abort(&s->req); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4391 | goto end; |
| 4392 | } |
| 4393 | |
| 4394 | check_channel_flags: |
| 4395 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4396 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4397 | /* if we've just closed an output, let's switch */ |
| 4398 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4399 | goto http_msg_closing; |
| 4400 | } |
| 4401 | |
| 4402 | end: |
| 4403 | chn->analysers &= AN_RES_FLT_END; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4404 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4405 | chn->flags |= CF_NEVER_WAIT; |
| 4406 | if (HAS_RSP_DATA_FILTERS(s)) |
| 4407 | chn->analysers |= AN_RES_FLT_XFER_DATA; |
| 4408 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4409 | channel_auto_close(chn); |
| 4410 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4411 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4412 | } |
| 4413 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4414 | /* Forward a response generated by HAProxy (error/redirect/return). This |
| 4415 | * function forwards all pending incoming data. If <final> is set to 0, nothing |
| 4416 | * more is performed. It is used for 1xx informational messages. Otherwise, the |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4417 | * transaction is terminated and the request is emptied. On success 1 is |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4418 | * returned. If an error occurred, 0 is returned. If it fails, this function |
| 4419 | * only exits. It is the caller responsibility to do the cleanup. |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4420 | */ |
| 4421 | int http_forward_proxy_resp(struct stream *s, int final) |
| 4422 | { |
| 4423 | struct channel *req = &s->req; |
| 4424 | struct channel *res = &s->res; |
| 4425 | struct htx *htx = htxbuf(&res->buf); |
| 4426 | size_t data; |
| 4427 | |
| 4428 | if (final) { |
| 4429 | htx->flags |= HTX_FL_PROXY_RESP; |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4430 | |
Christopher Faulet | aab1b67 | 2020-11-18 16:44:02 +0100 | [diff] [blame] | 4431 | if (!htx_is_empty(htx) && !http_eval_after_res_rules(s)) |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 4432 | return 0; |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4433 | |
Christopher Faulet | d6c4836 | 2020-10-19 18:01:38 +0200 | [diff] [blame] | 4434 | if (s->txn->meth == HTTP_METH_HEAD) |
| 4435 | htx_skip_msg_payload(htx); |
| 4436 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4437 | channel_auto_read(req); |
| 4438 | channel_abort(req); |
| 4439 | channel_auto_close(req); |
| 4440 | channel_htx_erase(req, htxbuf(&req->buf)); |
| 4441 | |
| 4442 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 4443 | channel_auto_read(res); |
| 4444 | channel_auto_close(res); |
| 4445 | channel_shutr_now(res); |
Christopher Faulet | 1a9db7c | 2020-06-25 15:36:45 +0200 | [diff] [blame] | 4446 | res->flags |= CF_EOI; /* The response is terminated, add EOI */ |
Christopher Faulet | 42432f3 | 2020-11-20 17:43:16 +0100 | [diff] [blame] | 4447 | htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */ |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4448 | } |
Christopher Faulet | cf6898c | 2020-06-25 15:55:11 +0200 | [diff] [blame] | 4449 | else { |
| 4450 | /* Send ASAP informational messages. Rely on CF_EOI for final |
| 4451 | * response. |
| 4452 | */ |
| 4453 | res->flags |= CF_SEND_DONTWAIT; |
| 4454 | } |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4455 | |
| 4456 | data = htx->data - co_data(res); |
| 4457 | c_adv(res, data); |
| 4458 | htx->first = -1; |
| 4459 | res->total += data; |
| 4460 | return 1; |
| 4461 | } |
| 4462 | |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4463 | void http_server_error(struct stream *s, struct stconn *sc, int err, |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4464 | int finst, struct http_reply *msg) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4465 | { |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4466 | http_reply_and_close(s, s->txn->status, msg); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4467 | if (!(s->flags & SF_ERR_MASK)) |
| 4468 | s->flags |= err; |
| 4469 | if (!(s->flags & SF_FINST_MASK)) |
| 4470 | s->flags |= finst; |
| 4471 | } |
| 4472 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4473 | 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] | 4474 | { |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4475 | if (!msg) { |
| 4476 | channel_htx_truncate(&s->res, htxbuf(&s->res.buf)); |
| 4477 | goto end; |
| 4478 | } |
| 4479 | |
| 4480 | if (http_reply_message(s, msg) == -1) { |
| 4481 | /* 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] | 4482 | * it is already an internal error. If it was already a "const" |
| 4483 | * 500 error, just fail. |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4484 | */ |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4485 | if (s->txn->status == 500) { |
| 4486 | if (s->txn->flags & TX_CONST_REPLY) |
| 4487 | goto end; |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4488 | s->txn->flags |= TX_CONST_REPLY; |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4489 | } |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4490 | s->txn->status = 500; |
| 4491 | s->txn->http_reply = NULL; |
| 4492 | return http_reply_and_close(s, s->txn->status, http_error_message(s)); |
| 4493 | } |
| 4494 | |
| 4495 | end: |
| 4496 | s->res.wex = tick_add_ifset(now_ms, s->res.wto); |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4497 | |
Christopher Faulet | 2d56500 | 2021-09-10 09:17:50 +0200 | [diff] [blame] | 4498 | /* At this staged, HTTP analysis is finished */ |
| 4499 | s->req.analysers &= AN_REQ_FLT_END; |
| 4500 | s->req.analyse_exp = TICK_ETERNITY; |
| 4501 | |
| 4502 | s->res.analysers &= AN_RES_FLT_END; |
| 4503 | s->res.analyse_exp = TICK_ETERNITY; |
| 4504 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4505 | channel_auto_read(&s->req); |
| 4506 | channel_abort(&s->req); |
| 4507 | channel_auto_close(&s->req); |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4508 | channel_htx_erase(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4509 | channel_auto_read(&s->res); |
| 4510 | channel_auto_close(&s->res); |
| 4511 | channel_shutr_now(&s->res); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4512 | } |
| 4513 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4514 | struct http_reply *http_error_message(struct stream *s) |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4515 | { |
| 4516 | const int msgnum = http_get_status_idx(s->txn->status); |
| 4517 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4518 | if (s->txn->http_reply) |
| 4519 | return s->txn->http_reply; |
| 4520 | else if (s->be->replies[msgnum]) |
| 4521 | return s->be->replies[msgnum]; |
| 4522 | else if (strm_fe(s)->replies[msgnum]) |
| 4523 | return strm_fe(s)->replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4524 | else |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4525 | return &http_err_replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4526 | } |
| 4527 | |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4528 | /* Produces an HTX message from an http reply. Depending on the http reply type, |
| 4529 | * a, errorfile, an raw file or a log-format string is used. On success, it |
| 4530 | * returns 0. If an error occurs -1 is returned. If it fails, this function only |
| 4531 | * exits. It is the caller responsibility to do the cleanup. |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4532 | */ |
Christopher Faulet | ae43b6c | 2020-05-27 15:24:22 +0200 | [diff] [blame] | 4533 | 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] | 4534 | { |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4535 | struct buffer *errmsg; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4536 | struct htx_sl *sl; |
| 4537 | struct buffer *body = NULL; |
| 4538 | const char *status, *reason, *clen, *ctype; |
| 4539 | unsigned int slflags; |
| 4540 | int ret = 0; |
| 4541 | |
Christopher Faulet | e29a97e | 2020-05-14 14:49:25 +0200 | [diff] [blame] | 4542 | /* |
| 4543 | * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so |
| 4544 | * |
| 4545 | * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled |
| 4546 | * as no payload if NULL. the TXN status code is set with the status |
| 4547 | * of the original reply. |
| 4548 | */ |
| 4549 | |
| 4550 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4551 | if (reply->body.reply) |
| 4552 | reply = reply->body.reply; |
| 4553 | } |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4554 | if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) { |
| 4555 | /* get default error message */ |
| 4556 | if (reply == s->txn->http_reply) |
| 4557 | s->txn->http_reply = NULL; |
| 4558 | reply = http_error_message(s); |
| 4559 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4560 | if (reply->body.reply) |
| 4561 | reply = reply->body.reply; |
| 4562 | } |
| 4563 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4564 | |
| 4565 | if (reply->type == HTTP_REPLY_ERRMSG) { |
| 4566 | /* implicit or explicit error message*/ |
| 4567 | errmsg = reply->body.errmsg; |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4568 | if (errmsg && !b_is_null(errmsg)) { |
Christopher Faulet | 2056736 | 2020-05-15 14:52:49 +0200 | [diff] [blame] | 4569 | if (!htx_copy_msg(htx, errmsg)) |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4570 | goto fail; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4571 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4572 | } |
| 4573 | else { |
| 4574 | /* no payload, file or log-format string */ |
| 4575 | if (reply->type == HTTP_REPLY_RAW) { |
| 4576 | /* file */ |
| 4577 | body = &reply->body.obj; |
| 4578 | } |
| 4579 | else if (reply->type == HTTP_REPLY_LOGFMT) { |
| 4580 | /* log-format string */ |
| 4581 | body = alloc_trash_chunk(); |
| 4582 | if (!body) |
| 4583 | goto fail_alloc; |
| 4584 | body->data = build_logline(s, body->area, body->size, &reply->body.fmt); |
| 4585 | } |
| 4586 | /* else no payload */ |
| 4587 | |
| 4588 | status = ultoa(reply->status); |
| 4589 | reason = http_get_reason(reply->status); |
| 4590 | slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN); |
| 4591 | if (!body || !b_data(body)) |
| 4592 | slflags |= HTX_SL_F_BODYLESS; |
| 4593 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4594 | if (!sl) |
| 4595 | goto fail; |
| 4596 | sl->info.res.status = reply->status; |
| 4597 | |
| 4598 | clen = (body ? ultoa(b_data(body)) : "0"); |
| 4599 | ctype = reply->ctype; |
| 4600 | |
| 4601 | if (!LIST_ISEMPTY(&reply->hdrs)) { |
| 4602 | struct http_reply_hdr *hdr; |
| 4603 | struct buffer *value = alloc_trash_chunk(); |
| 4604 | |
| 4605 | if (!value) |
| 4606 | goto fail; |
| 4607 | |
| 4608 | list_for_each_entry(hdr, &reply->hdrs, list) { |
| 4609 | chunk_reset(value); |
| 4610 | value->data = build_logline(s, value->area, value->size, &hdr->value); |
| 4611 | if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) { |
| 4612 | free_trash_chunk(value); |
| 4613 | goto fail; |
| 4614 | } |
| 4615 | chunk_reset(value); |
| 4616 | } |
| 4617 | free_trash_chunk(value); |
| 4618 | } |
| 4619 | |
| 4620 | if (!htx_add_header(htx, ist("content-length"), ist(clen)) || |
| 4621 | (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) || |
| 4622 | !htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4623 | (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] | 4624 | goto fail; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4625 | |
| 4626 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4627 | } |
| 4628 | |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4629 | leave: |
| 4630 | if (reply->type == HTTP_REPLY_LOGFMT) |
| 4631 | free_trash_chunk(body); |
| 4632 | return ret; |
| 4633 | |
| 4634 | fail_alloc: |
| 4635 | if (!(s->flags & SF_ERR_MASK)) |
| 4636 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4637 | /* fall through */ |
| 4638 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4639 | ret = -1; |
| 4640 | goto leave; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4641 | } |
| 4642 | |
| 4643 | /* 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] | 4644 | * occurs -1 is returned and the response channel is truncated, removing this |
| 4645 | * way the faulty reply. This function may fail when the reply is formatted |
| 4646 | * (http_reply_to_htx) or when the reply is forwarded |
| 4647 | * (http_forward_proxy_resp). On the last case, it is because a |
| 4648 | * http-after-response rule fails. |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4649 | */ |
| 4650 | int http_reply_message(struct stream *s, struct http_reply *reply) |
| 4651 | { |
| 4652 | struct channel *res = &s->res; |
| 4653 | struct htx *htx = htx_from_buf(&res->buf); |
| 4654 | |
| 4655 | if (s->txn->status == -1) |
| 4656 | s->txn->status = reply->status; |
| 4657 | channel_htx_truncate(res, htx); |
| 4658 | |
| 4659 | if (http_reply_to_htx(s, htx, reply) == -1) |
| 4660 | goto fail; |
| 4661 | |
| 4662 | htx_to_buf(htx, &s->res.buf); |
| 4663 | if (!http_forward_proxy_resp(s, 1)) |
| 4664 | goto fail; |
| 4665 | return 0; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4666 | |
| 4667 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4668 | channel_htx_truncate(res, htx); |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4669 | if (!(s->flags & SF_ERR_MASK)) |
| 4670 | s->flags |= SF_ERR_PRXCOND; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4671 | return -1; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4672 | } |
| 4673 | |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4674 | /* Return the error message corresponding to s->conn_err_type. It is assumed |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4675 | * that the server side is closed. Note that err_type is actually a |
| 4676 | * bitmask, where almost only aborts may be cumulated with other |
| 4677 | * values. We consider that aborted operations are more important |
| 4678 | * than timeouts or errors due to the fact that nobody else in the |
| 4679 | * logs might explain incomplete retries. All others should avoid |
| 4680 | * being cumulated. It should normally not be possible to have multiple |
| 4681 | * aborts at once, but just in case, the first one in sequence is reported. |
| 4682 | * Note that connection errors appearing on the second request of a keep-alive |
| 4683 | * connection are not reported since this allows the client to retry. |
| 4684 | */ |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4685 | void http_return_srv_error(struct stream *s, struct stconn *sc) |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4686 | { |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4687 | int err_type = s->conn_err_type; |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4688 | |
| 4689 | /* set s->txn->status for http_error_message(s) */ |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4690 | if (err_type & STRM_ET_QUEUE_ABRT) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4691 | s->txn->status = -1; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4692 | http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_Q, NULL); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4693 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4694 | else if (err_type & STRM_ET_CONN_ABRT) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4695 | s->txn->status = -1; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4696 | http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_C, NULL); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4697 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4698 | else if (err_type & STRM_ET_QUEUE_TO) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4699 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4700 | http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4701 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4702 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4703 | else if (err_type & STRM_ET_QUEUE_ERR) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4704 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4705 | http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4706 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4707 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4708 | else if (err_type & STRM_ET_CONN_TO) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4709 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4710 | http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4711 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4712 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4713 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4714 | else if (err_type & STRM_ET_CONN_ERR) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4715 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4716 | http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4717 | (s->flags & SF_SRV_REUSED) ? NULL : |
| 4718 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4719 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4720 | else if (err_type & STRM_ET_CONN_RES) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4721 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4722 | http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4723 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4724 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4725 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4726 | else { /* STRM_ET_CONN_OTHER and others */ |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4727 | s->txn->status = 500; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4728 | http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4729 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4730 | } |
| 4731 | } |
| 4732 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4733 | |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4734 | /* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0 |
| 4735 | * on success and -1 on error. |
| 4736 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4737 | 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] | 4738 | { |
| 4739 | /* If we have HTTP/1.1 message with a body and Expect: 100-continue, |
| 4740 | * then we must send an HTTP/1.1 100 Continue intermediate response. |
| 4741 | */ |
| 4742 | if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) && |
| 4743 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
| 4744 | struct ist hdr = { .ptr = "Expect", .len = 6 }; |
| 4745 | struct http_hdr_ctx ctx; |
| 4746 | |
| 4747 | ctx.blk = NULL; |
| 4748 | /* Expect is allowed in 1.1, look for it */ |
| 4749 | if (http_find_header(htx, hdr, &ctx, 0) && |
| 4750 | unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4751 | if (http_reply_100_continue(s) == -1) |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4752 | return -1; |
| 4753 | http_remove_header(htx, &ctx); |
| 4754 | } |
| 4755 | } |
| 4756 | return 0; |
| 4757 | } |
| 4758 | |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4759 | /* Send a 100-Continue response to the client. It returns 0 on success and -1 |
| 4760 | * on error. The response channel is updated accordingly. |
| 4761 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4762 | static int http_reply_100_continue(struct stream *s) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4763 | { |
| 4764 | struct channel *res = &s->res; |
| 4765 | struct htx *htx = htx_from_buf(&res->buf); |
| 4766 | struct htx_sl *sl; |
| 4767 | unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11| |
| 4768 | HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4769 | |
| 4770 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4771 | ist("HTTP/1.1"), ist("100"), ist("Continue")); |
| 4772 | if (!sl) |
| 4773 | goto fail; |
| 4774 | sl->info.res.status = 100; |
| 4775 | |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 4776 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4777 | goto fail; |
| 4778 | |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4779 | if (!http_forward_proxy_resp(s, 0)) |
| 4780 | goto fail; |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4781 | return 0; |
| 4782 | |
| 4783 | fail: |
| 4784 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4785 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4786 | channel_htx_truncate(res, htx); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4787 | return -1; |
| 4788 | } |
| 4789 | |
Christopher Faulet | 12c51e2 | 2018-11-28 15:59:42 +0100 | [diff] [blame] | 4790 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4791 | /* |
| 4792 | * Capture headers from message <htx> according to header list <cap_hdr>, and |
| 4793 | * fill the <cap> pointers appropriately. |
| 4794 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4795 | 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] | 4796 | { |
| 4797 | struct cap_hdr *h; |
| 4798 | int32_t pos; |
| 4799 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4800 | 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] | 4801 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4802 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4803 | struct ist n, v; |
| 4804 | |
| 4805 | if (type == HTX_BLK_EOH) |
| 4806 | break; |
| 4807 | if (type != HTX_BLK_HDR) |
| 4808 | continue; |
| 4809 | |
| 4810 | n = htx_get_blk_name(htx, blk); |
| 4811 | |
| 4812 | for (h = cap_hdr; h; h = h->next) { |
| 4813 | if (h->namelen && (h->namelen == n.len) && |
| 4814 | (strncasecmp(n.ptr, h->name, h->namelen) == 0)) { |
| 4815 | if (cap[h->index] == NULL) |
| 4816 | cap[h->index] = |
| 4817 | pool_alloc(h->pool); |
| 4818 | |
| 4819 | if (cap[h->index] == NULL) { |
| 4820 | ha_alert("HTTP capture : out of memory.\n"); |
| 4821 | break; |
| 4822 | } |
| 4823 | |
| 4824 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 4825 | v = isttrim(v, h->len); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4826 | |
| 4827 | memcpy(cap[h->index], v.ptr, v.len); |
| 4828 | cap[h->index][v.len]=0; |
| 4829 | } |
| 4830 | } |
| 4831 | } |
| 4832 | } |
| 4833 | |
Christopher Faulet | 0b6bdc5 | 2018-10-24 11:05:36 +0200 | [diff] [blame] | 4834 | /* Delete a value in a header between delimiters <from> and <next>. The header |
| 4835 | * itself is delimited by <start> and <end> pointers. The number of characters |
| 4836 | * displaced is returned, and the pointer to the first delimiter is updated if |
| 4837 | * required. The function tries as much as possible to respect the following |
| 4838 | * principles : |
| 4839 | * - replace <from> delimiter by the <next> one unless <from> points to <start>, |
| 4840 | * in which case <next> is simply removed |
| 4841 | * - set exactly one space character after the new first delimiter, unless there |
| 4842 | * are not enough characters in the block being moved to do so. |
| 4843 | * - remove unneeded spaces before the previous delimiter and after the new |
| 4844 | * one. |
| 4845 | * |
| 4846 | * It is the caller's responsibility to ensure that : |
| 4847 | * - <from> points to a valid delimiter or <start> ; |
| 4848 | * - <next> points to a valid delimiter or <end> ; |
| 4849 | * - there are non-space chars before <from>. |
| 4850 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4851 | 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] | 4852 | { |
| 4853 | char *prev = *from; |
| 4854 | |
| 4855 | if (prev == start) { |
| 4856 | /* We're removing the first value. eat the semicolon, if <next> |
| 4857 | * is lower than <end> */ |
| 4858 | if (next < end) |
| 4859 | next++; |
| 4860 | |
| 4861 | while (next < end && HTTP_IS_SPHT(*next)) |
| 4862 | next++; |
| 4863 | } |
| 4864 | else { |
| 4865 | /* Remove useless spaces before the old delimiter. */ |
| 4866 | while (HTTP_IS_SPHT(*(prev-1))) |
| 4867 | prev--; |
| 4868 | *from = prev; |
| 4869 | |
| 4870 | /* copy the delimiter and if possible a space if we're |
| 4871 | * not at the end of the line. |
| 4872 | */ |
| 4873 | if (next < end) { |
| 4874 | *prev++ = *next++; |
| 4875 | if (prev + 1 < next) |
| 4876 | *prev++ = ' '; |
| 4877 | while (next < end && HTTP_IS_SPHT(*next)) |
| 4878 | next++; |
| 4879 | } |
| 4880 | } |
| 4881 | memmove(prev, next, end - next); |
| 4882 | return (prev - next); |
| 4883 | } |
| 4884 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4885 | |
| 4886 | /* 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] | 4887 | * 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] | 4888 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4889 | 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] | 4890 | { |
| 4891 | struct ist dst = ist2(str, 0); |
| 4892 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4893 | if (istcat(&dst, htx_sl_req_meth(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4894 | goto end; |
| 4895 | if (dst.len + 1 > len) |
| 4896 | goto end; |
| 4897 | dst.ptr[dst.len++] = ' '; |
| 4898 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4899 | if (istcat(&dst, htx_sl_req_uri(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4900 | goto end; |
| 4901 | if (dst.len + 1 > len) |
| 4902 | goto end; |
| 4903 | dst.ptr[dst.len++] = ' '; |
| 4904 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4905 | istcat(&dst, htx_sl_req_vsn(sl), len); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4906 | end: |
| 4907 | return dst.len; |
| 4908 | } |
| 4909 | |
| 4910 | /* |
| 4911 | * Print a debug line with a start line. |
| 4912 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4913 | 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] | 4914 | { |
| 4915 | struct session *sess = strm_sess(s); |
| 4916 | int max; |
| 4917 | |
| 4918 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 4919 | dir, |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 4920 | objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1, |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 4921 | sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4922 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4923 | max = HTX_SL_P1_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4924 | UBOUND(max, trash.size - trash.data - 3); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4925 | chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4926 | trash.area[trash.data++] = ' '; |
| 4927 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4928 | max = HTX_SL_P2_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4929 | UBOUND(max, trash.size - trash.data - 2); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4930 | chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4931 | trash.area[trash.data++] = ' '; |
| 4932 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4933 | max = HTX_SL_P3_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4934 | UBOUND(max, trash.size - trash.data - 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4935 | chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4936 | trash.area[trash.data++] = '\n'; |
| 4937 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 4938 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4939 | } |
| 4940 | |
| 4941 | /* |
| 4942 | * Print a debug line with a header. |
| 4943 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4944 | 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] | 4945 | { |
| 4946 | struct session *sess = strm_sess(s); |
| 4947 | int max; |
| 4948 | |
| 4949 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 4950 | dir, |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 4951 | objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1, |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 4952 | sc_conn(s->scb) ? (unsigned short)(__sc_conn(s->scb))->handle.fd : -1); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4953 | |
| 4954 | max = n.len; |
| 4955 | UBOUND(max, trash.size - trash.data - 3); |
| 4956 | chunk_memcat(&trash, n.ptr, max); |
| 4957 | trash.area[trash.data++] = ':'; |
| 4958 | trash.area[trash.data++] = ' '; |
| 4959 | |
| 4960 | max = v.len; |
| 4961 | UBOUND(max, trash.size - trash.data - 1); |
| 4962 | chunk_memcat(&trash, v.ptr, max); |
| 4963 | trash.area[trash.data++] = '\n'; |
| 4964 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 4965 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4966 | } |
| 4967 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 4968 | void http_txn_reset_req(struct http_txn *txn) |
| 4969 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 4970 | txn->req.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 4971 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 4972 | } |
| 4973 | |
| 4974 | void http_txn_reset_res(struct http_txn *txn) |
| 4975 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 4976 | txn->rsp.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 4977 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 4978 | } |
| 4979 | |
| 4980 | /* |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 4981 | * Create and initialize a new HTTP transaction for stream <s>. This should be |
| 4982 | * used before processing any new request. It returns the transaction or NLULL |
| 4983 | * on error. |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 4984 | */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 4985 | struct http_txn *http_create_txn(struct stream *s) |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 4986 | { |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 4987 | struct http_txn *txn; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4988 | struct stconn *sc = s->scf; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 4989 | |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 4990 | txn = pool_alloc(pool_head_http_txn); |
| 4991 | if (!txn) |
| 4992 | return NULL; |
| 4993 | s->txn = txn; |
| 4994 | |
Christopher Faulet | 5eb67f5 | 2022-06-22 17:12:05 +0200 | [diff] [blame] | 4995 | txn->meth = HTTP_METH_OTHER; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4996 | txn->flags = ((sc && sc_ep_test(sc, SE_FL_NOT_FIRST)) ? TX_NOT_FIRST : 0); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 4997 | txn->status = -1; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 4998 | txn->http_reply = NULL; |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 4999 | txn->l7_buffer = BUF_NULL; |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 5000 | write_u32(txn->cache_hash, 0); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5001 | |
| 5002 | txn->cookie_first_date = 0; |
| 5003 | txn->cookie_last_date = 0; |
| 5004 | |
| 5005 | txn->srv_cookie = NULL; |
| 5006 | txn->cli_cookie = NULL; |
| 5007 | txn->uri = NULL; |
| 5008 | |
| 5009 | http_txn_reset_req(txn); |
| 5010 | http_txn_reset_res(txn); |
| 5011 | |
| 5012 | txn->req.chn = &s->req; |
| 5013 | txn->rsp.chn = &s->res; |
| 5014 | |
| 5015 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
| 5016 | |
Aurelien DARRAGON | 5ad2b64 | 2022-11-18 09:17:29 +0100 | [diff] [blame] | 5017 | /* here we don't want to re-initialize s->vars_txn and s->vars_reqres |
| 5018 | * variable lists, because they were already initialized upon stream |
| 5019 | * creation in stream_new(), and thus may already contain some variables |
| 5020 | */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5021 | |
| 5022 | return txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5023 | } |
| 5024 | |
| 5025 | /* to be used at the end of a transaction */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5026 | void http_destroy_txn(struct stream *s) |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5027 | { |
| 5028 | struct http_txn *txn = s->txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5029 | |
| 5030 | /* these ones will have been dynamically allocated */ |
| 5031 | pool_free(pool_head_requri, txn->uri); |
| 5032 | pool_free(pool_head_capture, txn->cli_cookie); |
| 5033 | pool_free(pool_head_capture, txn->srv_cookie); |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5034 | pool_free(pool_head_uniqueid, s->unique_id.ptr); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5035 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5036 | s->unique_id = IST_NULL; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5037 | txn->uri = NULL; |
| 5038 | txn->srv_cookie = NULL; |
| 5039 | txn->cli_cookie = NULL; |
| 5040 | |
Christopher Faulet | 5939925 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 5041 | if (!LIST_ISEMPTY(&s->vars_txn.head)) |
| 5042 | vars_prune(&s->vars_txn, s->sess, s); |
| 5043 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 5044 | vars_prune(&s->vars_reqres, s->sess, s); |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5045 | |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 5046 | b_free(&txn->l7_buffer); |
| 5047 | |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5048 | pool_free(pool_head_http_txn, txn); |
| 5049 | s->txn = NULL; |
Christopher Faulet | 5939925 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 5050 | } |
| 5051 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5052 | |
Christopher Faulet | 71236de | 2023-01-13 09:06:38 +0100 | [diff] [blame] | 5053 | void http_set_term_flags(struct stream *s) |
| 5054 | { |
| 5055 | if (!(s->flags & SF_ERR_MASK)) |
| 5056 | s->flags |= SF_ERR_PRXCOND; |
| 5057 | |
| 5058 | if (!(s->flags & SF_FINST_MASK)) { |
| 5059 | if (s->scb->state == SC_ST_INI) { |
| 5060 | /* Before any connection attempt on the server side, we |
| 5061 | * are still in the request analysis. Just take case to |
| 5062 | * detect tarpit error |
| 5063 | */ |
| 5064 | if (s->req.analysers & AN_REQ_HTTP_TARPIT) |
| 5065 | s->flags |= SF_FINST_T; |
| 5066 | else |
| 5067 | s->flags |= SF_FINST_R; |
| 5068 | } |
| 5069 | else if (s->scb->state == SC_ST_QUE) |
| 5070 | s->flags |= SF_FINST_Q; |
| 5071 | else if (sc_state_in(s->scb->state, SC_SB_REQ|SC_SB_TAR|SC_SB_ASS|SC_SB_CON|SC_SB_CER|SC_SB_RDY)) { |
| 5072 | if (unlikely(objt_applet(s->target))) { |
| 5073 | s->flags |= SF_FINST_R; |
| 5074 | } |
| 5075 | else |
| 5076 | s->flags |= SF_FINST_C; |
| 5077 | } |
| 5078 | else { |
| 5079 | if (s->txn->rsp.msg_state < HTTP_MSG_DATA) { |
| 5080 | /* We are still processing the response headers */ |
| 5081 | s->flags |= SF_FINST_H; |
| 5082 | } |
Christopher Faulet | 5b74f99 | 2023-01-26 19:02:07 +0100 | [diff] [blame] | 5083 | // (res == (done|closing|closed)) & (res->flags & shutw) |
| 5084 | else if (s->txn->rsp.msg_state >= HTTP_MSG_DONE && s->txn->rsp.msg_state < HTTP_MSG_TUNNEL && |
Christopher Faulet | 71236de | 2023-01-13 09:06:38 +0100 | [diff] [blame] | 5085 | (s->flags & (SF_ERR_CLITO|SF_ERR_CLICL))) { |
| 5086 | /* A client error was reported and we are |
| 5087 | * transmitting the last block of data |
| 5088 | */ |
| 5089 | s->flags |= SF_FINST_L; |
| 5090 | } |
| 5091 | else { |
| 5092 | /* Otherwise we are in DATA phase on both sides */ |
| 5093 | s->flags |= SF_FINST_D; |
| 5094 | } |
| 5095 | } |
| 5096 | } |
| 5097 | } |
| 5098 | |
| 5099 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5100 | DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5101 | |
Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 5102 | /* |
| 5103 | * Local variables: |
| 5104 | * c-indent-level: 8 |
| 5105 | * c-basic-offset: 8 |
| 5106 | * End: |
| 5107 | */ |