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 | |
Aurelien DARRAGON | f628614 | 2023-12-05 17:54:20 +0100 | [diff] [blame] | 69 | static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *px); |
| 70 | static int http_handle_stats(struct stream *s, struct channel *req, struct proxy *px); |
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 | |
Christopher Faulet | 0c370ee | 2023-04-13 16:05:13 +0200 | [diff] [blame] | 114 | /* An abort at this stage means we are performing a "destructive" |
Christopher Faulet | 97b3a61 | 2021-03-15 17:10:12 +0100 | [diff] [blame] | 115 | * HTTP upgrade (TCP>H2). In this case, we can leave. |
| 116 | */ |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 117 | if (s->scf->flags & (SC_FL_ABRT_DONE|SC_FL_EOS)) { |
Christopher Faulet | 97b3a61 | 2021-03-15 17:10:12 +0100 | [diff] [blame] | 118 | s->logs.logwait = 0; |
| 119 | s->logs.level = 0; |
Christopher Faulet | 7eb837d | 2023-04-13 15:22:29 +0200 | [diff] [blame] | 120 | stream_abort(s); |
Christopher Faulet | 97b3a61 | 2021-03-15 17:10:12 +0100 | [diff] [blame] | 121 | req->analysers &= AN_REQ_FLT_END; |
| 122 | req->analyse_exp = TICK_ETERNITY; |
| 123 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s); |
| 124 | return 1; |
| 125 | } |
Christopher Faulet | da46a0d | 2021-01-21 17:32:58 +0100 | [diff] [blame] | 126 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | htx = htxbuf(&req->buf); |
Frédéric Lécaille | 9969adb | 2023-01-18 11:52:21 +0100 | [diff] [blame] | 131 | sl = http_get_stline(htx); |
| 132 | len = HTX_SL_REQ_VLEN(sl); |
| 133 | if (len < 6) { |
| 134 | http_ver = 0; |
| 135 | } |
| 136 | else { |
| 137 | char *ptr; |
| 138 | |
| 139 | ptr = HTX_SL_REQ_VPTR(sl); |
| 140 | http_ver = ptr[5] - '0'; |
| 141 | } |
Christopher Faulet | 8bebd2f | 2020-10-06 17:54:56 +0200 | [diff] [blame] | 142 | |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 143 | /* Parsing errors are caught here */ |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 144 | if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) { |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 145 | stream_inc_http_req_ctr(s); |
Frédéric Lécaille | 9969adb | 2023-01-18 11:52:21 +0100 | [diff] [blame] | 146 | proxy_inc_fe_req_ctr(sess->listener, sess->fe, http_ver); |
Christopher Faulet | bf7175f | 2021-02-10 14:58:01 +0100 | [diff] [blame] | 147 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
| 148 | stream_inc_http_err_ctr(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 149 | goto return_bad_req; |
Christopher Faulet | bf7175f | 2021-02-10 14:58:01 +0100 | [diff] [blame] | 150 | } |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 151 | else |
| 152 | goto return_int_err; |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 155 | /* 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] | 156 | s->srv_error = http_return_srv_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 157 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 158 | msg->msg_state = HTTP_MSG_BODY; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 159 | stream_inc_http_req_ctr(s); |
Frédéric Lécaille | 9969adb | 2023-01-18 11:52:21 +0100 | [diff] [blame] | 160 | 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] | 161 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 162 | /* kill the pending keep-alive timeout */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 163 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 164 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 165 | BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL); |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 166 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 167 | /* 0: we might have to print this header in debug mode */ |
| 168 | if (unlikely((global.mode & MODE_DEBUG) && |
| 169 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 170 | int32_t pos; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 171 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 172 | http_debug_stline("clireq", s, sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 173 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 174 | 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] | 175 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 176 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 177 | |
| 178 | if (type == HTX_BLK_EOH) |
| 179 | break; |
| 180 | if (type != HTX_BLK_HDR) |
| 181 | continue; |
| 182 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 183 | http_debug_hdr("clihdr", s, |
| 184 | htx_get_blk_name(htx, blk), |
| 185 | htx_get_blk_value(htx, blk)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 186 | } |
| 187 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 188 | |
| 189 | /* |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 190 | * 1: identify the method and the version. Also set HTTP flags |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 191 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 192 | txn->meth = sl->info.req.meth; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 193 | if (sl->flags & HTX_SL_F_VER_11) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 194 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 195 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 196 | if (sl->flags & HTX_SL_F_CLEN) |
| 197 | msg->flags |= HTTP_MSGF_CNT_LEN; |
| 198 | else if (sl->flags & HTX_SL_F_CHNK) |
| 199 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Christopher Faulet | b2db4fa | 2018-11-27 16:51:09 +0100 | [diff] [blame] | 200 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 201 | msg->flags |= HTTP_MSGF_BODYLESS; |
Christopher Faulet | 576c358 | 2021-01-08 15:53:01 +0100 | [diff] [blame] | 202 | if (sl->flags & HTX_SL_F_CONN_UPG) |
| 203 | msg->flags |= HTTP_MSGF_CONN_UPG; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 204 | |
| 205 | /* we can make use of server redirect on GET and HEAD */ |
| 206 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 207 | s->flags |= SF_REDIRECTABLE; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 208 | 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] | 209 | /* PRI is reserved for the HTTP/2 preface */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 210 | goto return_bad_req; |
| 211 | } |
| 212 | |
| 213 | /* |
Christopher Faulet | 6072beb | 2020-02-18 15:34:58 +0100 | [diff] [blame] | 214 | * 2: check if the URI matches the monitor_uri. We have to do this for |
| 215 | * every request which gets in, because the monitor-uri is defined by |
| 216 | * the frontend. If the monitor-uri starts with a '/', the matching is |
| 217 | * done against the request's path. Otherwise, the request's uri is |
| 218 | * used. It is a workaround to let HTTP/2 health-checks work as |
| 219 | * expected. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 220 | */ |
Tim Duesterhus | 4b1fcaa | 2022-03-05 00:52:40 +0100 | [diff] [blame] | 221 | if (unlikely(isttest(sess->fe->monitor_uri))) { |
| 222 | const struct ist monitor_uri = sess->fe->monitor_uri; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 223 | 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] | 224 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 225 | if ((istptr(monitor_uri)[0] == '/' && |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 226 | isteq(http_parse_path(&parser), monitor_uri)) || |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 227 | isteq(htx_sl_req_uri(sl), monitor_uri)) { |
| 228 | /* |
| 229 | * We have found the monitor URI |
| 230 | */ |
| 231 | struct acl_cond *cond; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 232 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 233 | s->flags |= SF_MONITOR; |
| 234 | _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 235 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 236 | /* Check if we want to fail this monitor request or not */ |
| 237 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
| 238 | 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] | 239 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 240 | ret = acl_pass(ret); |
| 241 | if (cond->pol == ACL_COND_UNLESS) |
| 242 | ret = !ret; |
| 243 | |
| 244 | if (ret) { |
| 245 | /* we fail this request, let's return 503 service unavail */ |
| 246 | txn->status = 503; |
| 247 | if (!(s->flags & SF_ERR_MASK)) |
| 248 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
| 249 | goto return_prx_cond; |
| 250 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 251 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 252 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 253 | /* nothing to fail, let's reply normally */ |
| 254 | txn->status = 200; |
| 255 | if (!(s->flags & SF_ERR_MASK)) |
| 256 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
| 257 | goto return_prx_cond; |
| 258 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* |
| 262 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 263 | * Note: we cannot log anymore if the request has been |
| 264 | * classified as invalid. |
| 265 | */ |
| 266 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 267 | /* we have a complete HTTP request that we must log */ |
| 268 | if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 269 | size_t len; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 270 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 271 | 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] | 272 | txn->uri[len] = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 273 | |
| 274 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
| 275 | s->do_log(s); |
| 276 | } else { |
| 277 | ha_alert("HTTP logging : out of memory.\n"); |
| 278 | } |
| 279 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 280 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 281 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 282 | * we have what looks like a proxied connection instead of a connection, |
| 283 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 284 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 285 | * happen to do that despite being non-standard :-( |
| 286 | * We consider that a request not beginning with either '/' or '*' is |
| 287 | * a proxied connection, which covers both "scheme://location" and |
| 288 | * CONNECT ip:port. |
| 289 | */ |
| 290 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 291 | *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*') |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 292 | txn->flags |= TX_USE_PX_CONN; |
| 293 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 294 | /* 5: we may need to capture headers */ |
| 295 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 296 | http_capture_headers(htx, s->req_cap, sess->fe->req_cap); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 297 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 298 | /* we may have to wait for the request's body */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 299 | if (s->be->options & PR_O_WREQ_BODY) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 300 | req->analysers |= AN_REQ_HTTP_BODY; |
| 301 | |
| 302 | /* |
| 303 | * RFC7234#4: |
| 304 | * A cache MUST write through requests with methods |
| 305 | * that are unsafe (Section 4.2.1 of [RFC7231]) to |
| 306 | * the origin server; i.e., a cache is not allowed |
| 307 | * to generate a reply to such a request before |
| 308 | * having forwarded the request and having received |
| 309 | * a corresponding response. |
| 310 | * |
| 311 | * RFC7231#4.2.1: |
| 312 | * Of the request methods defined by this |
| 313 | * specification, the GET, HEAD, OPTIONS, and TRACE |
| 314 | * methods are defined to be safe. |
| 315 | */ |
| 316 | if (likely(txn->meth == HTTP_METH_GET || |
| 317 | txn->meth == HTTP_METH_HEAD || |
| 318 | txn->meth == HTTP_METH_OPTIONS || |
| 319 | txn->meth == HTTP_METH_TRACE)) |
| 320 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 321 | |
| 322 | /* end of job, return OK */ |
| 323 | req->analysers &= ~an_bit; |
| 324 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 325 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 326 | 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] | 327 | return 1; |
| 328 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 329 | return_int_err: |
| 330 | txn->status = 500; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 331 | if (!(s->flags & SF_ERR_MASK)) |
| 332 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 333 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 334 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 335 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 336 | goto return_prx_cond; |
| 337 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 338 | return_bad_req: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 339 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 340 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 341 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 342 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 343 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 344 | |
| 345 | return_prx_cond: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 346 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 347 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 348 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 349 | DBG_TRACE_DEVEL("leaving on error", |
| 350 | 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] | 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | /* This stream analyser runs all HTTP request processing which is common to |
| 356 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 357 | * reqadd, stats and redirects. This is performed for the designated proxy. |
| 358 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 359 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 360 | * error, ...). |
| 361 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 362 | 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] | 363 | { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 364 | struct list *def_rules, *rules; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 365 | struct session *sess = s->sess; |
| 366 | struct http_txn *txn = s->txn; |
| 367 | struct http_msg *msg = &txn->req; |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 368 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 369 | struct redirect_rule *rule; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 370 | enum rule_result verdict; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 371 | struct connection *conn = objt_conn(sess->origin); |
| 372 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 373 | 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] | 374 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 375 | htx = htxbuf(&req->buf); |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 376 | |
Christopher Faulet | 1907ccc | 2019-04-29 13:12:02 +0200 | [diff] [blame] | 377 | /* just in case we have some per-backend tracking. Only called the first |
| 378 | * execution of the analyser. */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 379 | if (!s->current_rule && !s->current_rule_list) |
Christopher Faulet | 1907ccc | 2019-04-29 13:12:02 +0200 | [diff] [blame] | 380 | stream_inc_be_http_req_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 381 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 382 | def_rules = ((px->defpx && (an_bit == AN_REQ_HTTP_PROCESS_FE || px != sess->fe)) ? &px->defpx->http_req_rules : NULL); |
| 383 | rules = &px->http_req_rules; |
| 384 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 385 | /* evaluate http-request rules */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 386 | if ((def_rules && !LIST_ISEMPTY(def_rules)) || !LIST_ISEMPTY(rules)) { |
| 387 | verdict = http_req_get_intercept_rule(px, def_rules, rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 388 | |
| 389 | switch (verdict) { |
| 390 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 391 | goto return_prx_yield; |
| 392 | |
| 393 | case HTTP_RULE_RES_CONT: |
| 394 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 395 | break; |
| 396 | |
| 397 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 398 | if (txn->flags & TX_CLTARPIT) |
| 399 | goto tarpit; |
| 400 | goto deny; |
| 401 | |
| 402 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 403 | goto return_prx_cond; |
| 404 | |
| 405 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
| 406 | goto done; |
| 407 | |
| 408 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 409 | goto return_bad_req; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 410 | |
| 411 | case HTTP_RULE_RES_ERROR: /* failed with a bad request */ |
| 412 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 416 | if (px->options2 & (PR_O2_RSTRICT_REQ_HDR_NAMES_BLK|PR_O2_RSTRICT_REQ_HDR_NAMES_DEL)) { |
| 417 | verdict = http_req_restrict_header_names(s, htx, px); |
| 418 | if (verdict == HTTP_RULE_RES_DENY) |
| 419 | goto deny; |
| 420 | } |
| 421 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 422 | if (conn && (conn->flags & CO_FL_EARLY_DATA) && |
Olivier Houchard | 220a26c | 2020-01-23 14:57:36 +0100 | [diff] [blame] | 423 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) { |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 424 | struct http_hdr_ctx ctx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 425 | |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 426 | ctx.blk = NULL; |
| 427 | if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) { |
| 428 | if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1")))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 429 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 430 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /* OK at this stage, we know that the request was accepted according to |
| 434 | * the http-request rules, we can check for the stats. Note that the |
| 435 | * URI is detected *before* the req* rules in order not to be affected |
| 436 | * by a possible reqrep, while they are processed *after* so that a |
| 437 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 438 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 439 | if (!s->target && http_stats_check_uri(s, txn, px)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 440 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | a0b58b5 | 2022-05-27 08:33:53 +0200 | [diff] [blame] | 441 | if (unlikely(!sc_applet_create(s->scb, objt_applet(s->target)))) { |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 442 | s->logs.request_ts = now_ns; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 443 | if (!(s->flags & SF_ERR_MASK)) |
| 444 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 445 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /* parse the whole stats request and extract the relevant information */ |
Aurelien DARRAGON | f628614 | 2023-12-05 17:54:20 +0100 | [diff] [blame] | 449 | http_handle_stats(s, req, px); |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 450 | 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] | 451 | /* not all actions implemented: deny, allow, auth */ |
| 452 | |
| 453 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 454 | goto deny; |
| 455 | |
| 456 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 457 | goto return_prx_cond; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 458 | |
| 459 | if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */ |
| 460 | goto return_bad_req; |
| 461 | |
| 462 | if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */ |
| 463 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 464 | } |
| 465 | |
Christopher Faulet | 2571bc6 | 2019-03-01 11:44:26 +0100 | [diff] [blame] | 466 | /* Proceed with the applets now. */ |
| 467 | if (unlikely(objt_applet(s->target))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 468 | 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] | 469 | _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 470 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 471 | if (http_handle_expect_hdr(s, htx, msg) == -1) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 472 | goto return_int_err; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 473 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 474 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 475 | 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] | 476 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 477 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 478 | if (HAS_FILTERS(s)) |
| 479 | req->analysers |= AN_REQ_FLT_HTTP_HDRS; |
| 480 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 481 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
| 482 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 483 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 484 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 485 | |
Christopher Faulet | 5c281d5 | 2023-03-16 15:53:28 +0100 | [diff] [blame] | 486 | s->scb->flags |= SC_FL_SND_ASAP; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 487 | s->flags |= SF_ASSIGNED; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 488 | goto done; |
| 489 | } |
| 490 | |
| 491 | /* check whether we have some ACLs set to redirect this request */ |
| 492 | list_for_each_entry(rule, &px->redirect_rules, list) { |
| 493 | if (rule->cond) { |
| 494 | int ret; |
| 495 | |
| 496 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 497 | ret = acl_pass(ret); |
| 498 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 499 | ret = !ret; |
| 500 | if (!ret) |
| 501 | continue; |
| 502 | } |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 503 | if (!http_apply_redirect_rule(rule, s, txn)) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 504 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 505 | goto done; |
| 506 | } |
| 507 | |
| 508 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 509 | * If this happens, then the data will not come immediately, so we must |
| 510 | * send all what we have without waiting. Note that due to the small gain |
| 511 | * in waiting for the body of the request, it's easier to simply put the |
Christopher Faulet | 5c281d5 | 2023-03-16 15:53:28 +0100 | [diff] [blame] | 512 | * SC_FL_SND_ASAP flag on the back SC any time. It's a one-shot flag so it |
| 513 | * will remove itself once used. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 514 | */ |
Christopher Faulet | 5c281d5 | 2023-03-16 15:53:28 +0100 | [diff] [blame] | 515 | s->scb->flags |= SC_FL_SND_ASAP; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 516 | |
| 517 | done: /* done with this analyser, continue with next ones that the calling |
| 518 | * points will have set, if any. |
| 519 | */ |
| 520 | req->analyse_exp = TICK_ETERNITY; |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 521 | 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] | 522 | req->analysers &= ~an_bit; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 523 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 524 | 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] | 525 | return 1; |
| 526 | |
| 527 | tarpit: |
| 528 | /* Allow cookie logging |
| 529 | */ |
| 530 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 531 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 532 | |
| 533 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 534 | * which may be the same as the connect timeout if unspecified. |
| 535 | * If unset, then set it to zero because we really want it to |
| 536 | * eventually expire. We build the tarpit as an analyser. |
| 537 | */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 538 | channel_htx_erase(&s->req, htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 539 | |
| 540 | /* wipe the request out so that we can drop the connection early |
| 541 | * if the client closes first. |
| 542 | */ |
| 543 | channel_dont_connect(req); |
| 544 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 545 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
| 546 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 547 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 548 | if (!req->analyse_exp) |
| 549 | req->analyse_exp = tick_add(now_ms, 0); |
| 550 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 551 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 552 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 553 | _HA_ATOMIC_INC(&s->be->be_counters.denied_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 554 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 555 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 556 | goto done_without_exp; |
| 557 | |
| 558 | deny: /* this request was blocked (denied) */ |
| 559 | |
| 560 | /* Allow cookie logging |
| 561 | */ |
| 562 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 563 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 564 | |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 565 | s->logs.request_ts = now_ns; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 566 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 567 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 568 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 569 | _HA_ATOMIC_INC(&s->be->be_counters.denied_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 570 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 571 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 572 | goto return_prx_err; |
| 573 | |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 574 | return_fail_rewrite: |
| 575 | if (!(s->flags & SF_ERR_MASK)) |
| 576 | s->flags |= SF_ERR_PRXCOND; |
| 577 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
| 578 | if (s->flags & SF_BE_ASSIGNED) |
| 579 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
| 580 | if (sess->listener && sess->listener->counters) |
| 581 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
| 582 | if (objt_server(s->target)) |
| 583 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
| 584 | /* fall through */ |
| 585 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 586 | return_int_err: |
| 587 | txn->status = 500; |
| 588 | if (!(s->flags & SF_ERR_MASK)) |
| 589 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 590 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 591 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 592 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 593 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 594 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 595 | goto return_prx_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 596 | |
| 597 | return_bad_req: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 598 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 599 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 600 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 601 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 602 | /* fall through */ |
| 603 | |
| 604 | return_prx_err: |
| 605 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 606 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 607 | |
| 608 | return_prx_cond: |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 609 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 610 | |
| 611 | req->analysers &= AN_REQ_FLT_END; |
| 612 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 613 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 614 | DBG_TRACE_DEVEL("leaving on error", |
| 615 | 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] | 616 | return 0; |
| 617 | |
| 618 | return_prx_yield: |
| 619 | channel_dont_connect(req); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 620 | DBG_TRACE_DEVEL("waiting for more data", |
| 621 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | /* This function performs all the processing enabled for the current request. |
| 626 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 627 | * needs more data, encounters an error, or wants to immediately abort the |
| 628 | * request. It relies on buffers flags, and updates s->req.analysers. |
| 629 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 630 | 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] | 631 | { |
| 632 | struct session *sess = s->sess; |
| 633 | struct http_txn *txn = s->txn; |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 634 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 635 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
| 636 | |
Christopher Faulet | 8bebd2f | 2020-10-06 17:54:56 +0200 | [diff] [blame] | 637 | 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] | 638 | |
| 639 | /* |
| 640 | * Right now, we know that we have processed the entire headers |
| 641 | * and that unwanted requests have been filtered out. We can do |
| 642 | * whatever we want with the remaining request. Also, now we |
| 643 | * may have separate values for ->fe, ->be. |
| 644 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 645 | htx = htxbuf(&req->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 646 | |
| 647 | /* |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 648 | * 7: Now we can work with the cookies. |
| 649 | * Note that doing so might move headers in the request, but |
| 650 | * the fields will stay coherent and the URI will not move. |
| 651 | * This should only be performed in the backend. |
| 652 | */ |
| 653 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 654 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 655 | |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 656 | /* 8: Generate unique ID if a "unique-id-format" is defined. |
| 657 | * |
| 658 | * A unique ID is generated even when it is not sent to ensure that the ID can make use of |
| 659 | * fetches only available in the HTTP request processing stage. |
| 660 | */ |
| 661 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 662 | 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] | 663 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 664 | if (!isttest(unique_id)) { |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 665 | if (!(s->flags & SF_ERR_MASK)) |
| 666 | s->flags |= SF_ERR_RESOURCE; |
| 667 | goto return_int_err; |
| 668 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 669 | |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 670 | /* send unique ID if a "unique-id-header" is defined */ |
Tim Duesterhus | 0643b0e | 2020-03-05 17:56:35 +0100 | [diff] [blame] | 671 | if (isttest(sess->fe->header_unique_id) && |
Tim Duesterhus | 7ad27d4 | 2022-05-18 00:22:15 +0200 | [diff] [blame] | 672 | unlikely(!http_add_header(htx, sess->fe->header_unique_id, unique_id))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 673 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 674 | } |
| 675 | |
Aurelien DARRAGON | b2e2ec5 | 2023-01-09 11:09:03 +0100 | [diff] [blame] | 676 | /* handle http extensions (if configured) */ |
| 677 | if (unlikely(!http_handle_7239_header(s, req))) |
| 678 | goto return_fail_rewrite; |
| 679 | if (unlikely(!http_handle_xff_header(s, req))) |
| 680 | goto return_fail_rewrite; |
| 681 | if (unlikely(!http_handle_xot_header(s, req))) |
| 682 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 683 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 684 | /* Filter the request headers if there are filters attached to the |
| 685 | * stream. |
| 686 | */ |
| 687 | if (HAS_FILTERS(s)) |
| 688 | req->analysers |= AN_REQ_FLT_HTTP_HDRS; |
| 689 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 690 | /* If we have no server assigned yet and we're balancing on url_param |
| 691 | * with a POST request, we may be interested in checking the body for |
| 692 | * that parameter. This will be done in another analyser. |
| 693 | */ |
| 694 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | 089eaa0 | 2019-01-14 15:17:46 +0100 | [diff] [blame] | 695 | s->txn->meth == HTTP_METH_POST && |
| 696 | (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 697 | channel_dont_connect(req); |
| 698 | req->analysers |= AN_REQ_HTTP_BODY; |
| 699 | } |
| 700 | |
| 701 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 702 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 703 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 704 | /* We expect some data from the client. Unless we know for sure |
| 705 | * we already have a full request, we have to re-enable quick-ack |
| 706 | * in case we previously disabled it, otherwise we might cause |
| 707 | * the client to delay further data. |
| 708 | */ |
Willy Tarreau | 9bdcf42 | 2023-01-12 19:40:42 +0100 | [diff] [blame] | 709 | 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] | 710 | conn_set_quickack(cli_conn, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 711 | |
| 712 | /************************************************************* |
| 713 | * OK, that's finished for the headers. We have done what we * |
| 714 | * could. Let's switch to the DATA state. * |
| 715 | ************************************************************/ |
| 716 | req->analyse_exp = TICK_ETERNITY; |
| 717 | req->analysers &= ~an_bit; |
| 718 | |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 719 | s->logs.request_ts = now_ns; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 720 | /* OK let's go on with the BODY now */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 721 | 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] | 722 | return 1; |
| 723 | |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 724 | return_fail_rewrite: |
| 725 | if (!(s->flags & SF_ERR_MASK)) |
| 726 | s->flags |= SF_ERR_PRXCOND; |
| 727 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
| 728 | if (s->flags & SF_BE_ASSIGNED) |
| 729 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
| 730 | if (sess->listener && sess->listener->counters) |
| 731 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
| 732 | if (objt_server(s->target)) |
| 733 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
| 734 | /* fall through */ |
| 735 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 736 | return_int_err: |
| 737 | txn->status = 500; |
| 738 | if (!(s->flags & SF_ERR_MASK)) |
| 739 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 740 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 741 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 742 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 743 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 744 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 745 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 746 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 747 | http_set_term_flags(s); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 748 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 749 | DBG_TRACE_DEVEL("leaving on error", |
| 750 | 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] | 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 755 | * returns zero, at the beginning because it prevents any other processing |
| 756 | * from occurring, and at the end because it terminates the request. |
| 757 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 758 | 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] | 759 | { |
| 760 | struct http_txn *txn = s->txn; |
| 761 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 762 | 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] | 763 | /* This connection is being tarpitted. The CLIENT side has |
| 764 | * already set the connect expiration date to the right |
| 765 | * timeout. We just have to check that the client is still |
| 766 | * there and that the timeout has not expired. |
| 767 | */ |
| 768 | channel_dont_connect(req); |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 769 | if (!(s->scf->flags & (SC_FL_ABRT_DONE|SC_FL_EOS)) && |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 770 | !tick_is_expired(req->analyse_exp, now_ms)) { |
Christopher Faulet | b0c87f1 | 2021-10-29 14:37:07 +0200 | [diff] [blame] | 771 | /* Be sure to drain all data from the request channel */ |
| 772 | channel_htx_erase(req, htxbuf(&req->buf)); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 773 | DBG_TRACE_DEVEL("waiting for tarpit timeout expiry", |
| 774 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 775 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 776 | } |
| 777 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 778 | |
| 779 | /* We will set the queue timer to the time spent, just for |
| 780 | * logging purposes. We fake a 500 server error, so that the |
| 781 | * attacker will not suspect his connection has been tarpitted. |
| 782 | * It will not cause trouble to the logs because we can exclude |
| 783 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 784 | */ |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 785 | s->logs.t_queue = ns_to_ms(now_ns - s->logs.accept_ts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 786 | |
Christopher Faulet | d7bac88 | 2023-04-14 12:05:55 +0200 | [diff] [blame] | 787 | http_reply_and_close(s, txn->status, (!(s->scf->flags & SC_FL_ERROR) ? http_error_message(s) : NULL)); |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 788 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 789 | |
| 790 | 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] | 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 795 | * for either the buffer to be full, or the full advertised contents to have |
| 796 | * reached the buffer. It must only be called after the standard HTTP request |
| 797 | * processing has occurred, because it expects the request to be parsed and will |
| 798 | * look for the Expect header. It may send a 100-Continue interim response. It |
Christopher Faulet | 2954bcc | 2023-04-05 10:42:03 +0200 | [diff] [blame] | 799 | * returns zero if it needs to read more data, or 1 once it has completed its |
| 800 | * analysis. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 801 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 802 | 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] | 803 | { |
| 804 | struct session *sess = s->sess; |
| 805 | struct http_txn *txn = s->txn; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 806 | |
Christopher Faulet | f4569bb | 2023-01-13 10:49:31 +0100 | [diff] [blame] | 807 | 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] | 808 | |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 809 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 810 | switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) { |
| 811 | case HTTP_RULE_RES_CONT: |
| 812 | goto http_end; |
| 813 | case HTTP_RULE_RES_YIELD: |
| 814 | goto missing_data_or_waiting; |
| 815 | case HTTP_RULE_RES_BADREQ: |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 816 | goto return_bad_req; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 817 | case HTTP_RULE_RES_ERROR: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 818 | goto return_int_err; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 819 | case HTTP_RULE_RES_ABRT: |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 820 | goto return_prx_cond; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 821 | default: |
| 822 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | http_end: |
| 826 | /* The situation will not evolve, so let's give up on the analysis. */ |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 827 | s->logs.request_ts = now_ns; /* update the request timer to reflect full request */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 828 | req->analysers &= ~an_bit; |
| 829 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 830 | 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] | 831 | return 1; |
| 832 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 833 | missing_data_or_waiting: |
| 834 | channel_dont_connect(req); |
| 835 | DBG_TRACE_DEVEL("waiting for more data", |
| 836 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 837 | return 0; |
| 838 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 839 | return_int_err: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 840 | txn->status = 500; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 841 | if (!(s->flags & SF_ERR_MASK)) |
| 842 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 843 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 844 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 845 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 846 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 847 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 848 | goto return_prx_err; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 849 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 850 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 851 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 852 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 853 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 854 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 855 | /* fall through */ |
| 856 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 857 | return_prx_err: |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 858 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 859 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 860 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 861 | return_prx_cond: |
Christopher Faulet | f0d80df | 2023-01-13 10:20:20 +0100 | [diff] [blame] | 862 | http_set_term_flags(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 863 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 864 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 865 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 866 | DBG_TRACE_DEVEL("leaving on error", |
| 867 | 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] | 868 | return 0; |
| 869 | } |
| 870 | |
| 871 | /* This function is an analyser which forwards request body (including chunk |
| 872 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 873 | * zero byte. The only situation where it must not be called is when we're in |
| 874 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 875 | * remaining data and to resync after end of body. It expects the msg_state to |
| 876 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 877 | * read more data, or 1 once we can go on with next request or end the stream. |
| 878 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
| 879 | * bytes of pending data + the headers if not already done. |
| 880 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 881 | 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] | 882 | { |
| 883 | struct session *sess = s->sess; |
| 884 | struct http_txn *txn = s->txn; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 885 | struct http_msg *msg = &txn->req; |
| 886 | struct htx *htx; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 887 | short status = 0; |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 888 | int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 889 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 890 | 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] | 891 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 892 | htx = htxbuf(&req->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 893 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 894 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 895 | goto return_bad_req; |
| 896 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 897 | goto return_int_err; |
| 898 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 899 | /* Note that we don't have to send 100-continue back because we don't |
| 900 | * need the data to complete our job, and it's up to the server to |
| 901 | * decide whether to return 100, 417 or anything else in return of |
| 902 | * an "Expect: 100-continue" header. |
| 903 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 904 | if (msg->msg_state == HTTP_MSG_BODY) |
| 905 | msg->msg_state = HTTP_MSG_DATA; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 906 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 907 | /* in most states, we should abort in case of early close */ |
| 908 | channel_auto_close(req); |
| 909 | |
| 910 | if (req->to_forward) { |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 911 | if (req->to_forward == CHN_INFINITE_FORWARD) { |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 912 | if (s->scf->flags & SC_FL_EOI) |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 913 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 914 | } |
| 915 | else { |
| 916 | /* We can't process the buffer's contents yet */ |
| 917 | req->flags |= CF_WAKE_WRITE; |
| 918 | goto missing_data_or_waiting; |
| 919 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 920 | } |
| 921 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 922 | if (msg->msg_state >= HTTP_MSG_ENDING) |
| 923 | goto ending; |
| 924 | |
| 925 | if (txn->meth == HTTP_METH_CONNECT) { |
| 926 | msg->msg_state = HTTP_MSG_ENDING; |
| 927 | goto ending; |
| 928 | } |
| 929 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 930 | /* Forward input data. We get it by removing all outgoing data not |
| 931 | * forwarded yet from HTX data size. If there are some data filters, we |
| 932 | * let them decide the amount of data to forward. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 933 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 934 | if (HAS_REQ_DATA_FILTERS(s)) { |
| 935 | ret = flt_http_payload(s, msg, htx->data); |
| 936 | if (ret < 0) |
| 937 | goto return_bad_req; |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 938 | c_adv(req, ret); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 939 | } |
| 940 | else { |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 941 | c_adv(req, htx->data - co_data(req)); |
Christopher Faulet | 2f7c82b | 2023-02-20 14:06:52 +0100 | [diff] [blame] | 942 | 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] | 943 | channel_htx_forward_forever(req, htx); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 944 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 945 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 946 | if (htx->data != co_data(req)) |
| 947 | goto missing_data_or_waiting; |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 948 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 949 | /* 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] | 950 | * in HTTP_MSG_ENDING state. Then if all data was marked to be |
| 951 | * forwarded, set the state to HTTP_MSG_DONE. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 952 | */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 953 | if (!(htx->flags & HTX_FL_EOM)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 954 | goto missing_data_or_waiting; |
| 955 | |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 956 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 957 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 958 | ending: |
Christopher Faulet | 84d3ef9 | 2023-03-17 15:45:58 +0100 | [diff] [blame] | 959 | s->scb->flags &= ~SC_FL_SND_EXP_MORE; /* no more data are expected to be send */ |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 960 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 961 | /* other states, ENDING...TUNNEL */ |
| 962 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 963 | goto done; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 964 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 965 | if (HAS_REQ_DATA_FILTERS(s)) { |
| 966 | ret = flt_http_end(s, msg); |
| 967 | if (ret <= 0) { |
| 968 | if (!ret) |
| 969 | goto missing_data_or_waiting; |
| 970 | goto return_bad_req; |
| 971 | } |
| 972 | } |
| 973 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 974 | if (txn->meth == HTTP_METH_CONNECT) |
| 975 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 976 | else { |
| 977 | msg->msg_state = HTTP_MSG_DONE; |
| 978 | req->to_forward = 0; |
| 979 | } |
| 980 | |
| 981 | done: |
| 982 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 983 | if (!(txn->flags & TX_CON_WANT_TUN)) |
| 984 | channel_dont_close(req); |
| 985 | |
Christopher Faulet | 64350bb | 2023-04-13 16:37:37 +0200 | [diff] [blame] | 986 | if ((s->scb->flags & SC_FL_SHUT_DONE) && co_data(req)) { |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 987 | /* request errors are most likely due to the server aborting the |
Christopher Faulet | bea90bb | 2023-09-14 11:12:32 +0200 | [diff] [blame] | 988 | * transfer.Bit handle server aborts only if there is no |
| 989 | * response. Otherwise, let a change to foward the response |
| 990 | * first. |
| 991 | */ |
| 992 | if (htx_is_empty(htxbuf(&s->res.buf))) |
| 993 | goto return_srv_abort; |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 994 | } |
| 995 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 996 | http_end_request(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 997 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 998 | 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] | 999 | return 1; |
| 1000 | } |
| 1001 | |
| 1002 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 1003 | * the client's connection and forward any shutdown notification to the |
| 1004 | * server, which will decide whether to close or to go on processing the |
| 1005 | * request. We only do that in tunnel mode, and not in other modes since |
| 1006 | * it can be abused to exhaust source ports. */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 1007 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1008 | channel_auto_read(req); |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 1009 | if ((s->scf->flags & (SC_FL_ABRT_DONE|SC_FL_EOS)) && !(txn->flags & TX_CON_WANT_TUN)) |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1010 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1011 | channel_auto_close(req); |
| 1012 | } |
| 1013 | else if (s->txn->meth == HTTP_METH_POST) { |
| 1014 | /* POST requests may require to read extra CRLF sent by broken |
| 1015 | * browsers and which could cause an RST to be sent upon close |
| 1016 | * on some systems (eg: Linux). */ |
| 1017 | channel_auto_read(req); |
| 1018 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1019 | DBG_TRACE_DEVEL("waiting for the end of the HTTP txn", |
| 1020 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1021 | return 0; |
| 1022 | |
| 1023 | missing_data_or_waiting: |
| 1024 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 1025 | if (msg->msg_state < HTTP_MSG_ENDING && (s->scf->flags & (SC_FL_ABRT_DONE|SC_FL_EOS))) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1026 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1027 | |
| 1028 | waiting: |
| 1029 | /* waiting for the last bits to leave the buffer */ |
Christopher Faulet | bea90bb | 2023-09-14 11:12:32 +0200 | [diff] [blame] | 1030 | if (s->scb->flags & SC_FL_SHUT_DONE) { |
| 1031 | /* Handle server aborts only if there is no response. Otherwise, |
| 1032 | * let a change to foward the response first. |
| 1033 | */ |
| 1034 | if (htx_is_empty(htxbuf(&s->res.buf))) |
| 1035 | goto return_srv_abort; |
| 1036 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1037 | |
| 1038 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 1039 | * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE. |
| 1040 | * And when content-length is used, we never want to let the possible |
| 1041 | * shutdown be forwarded to the other side, as the state machine will |
| 1042 | * take care of it once the client responds. It's also important to |
| 1043 | * prevent TIME_WAITs from accumulating on the backend side, and for |
| 1044 | * HTTP/2 where the last frame comes with a shutdown. |
| 1045 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1046 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1047 | channel_dont_close(req); |
| 1048 | |
| 1049 | /* We know that more data are expected, but we couldn't send more that |
Christopher Faulet | 84d3ef9 | 2023-03-17 15:45:58 +0100 | [diff] [blame] | 1050 | * what we did. So we always set the SC_FL_SND_EXP_MORE flag so that the |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1051 | * system knows it must not set a PUSH on this first part. Interactive |
| 1052 | * modes are already handled by the stream sock layer. We must not do |
| 1053 | * this in content-length mode because it could present the MSG_MORE |
| 1054 | * flag with the last block of forwarded data, which would cause an |
| 1055 | * additional delay to be observed by the receiver. |
| 1056 | */ |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 1057 | if (HAS_REQ_DATA_FILTERS(s)) |
Christopher Faulet | 84d3ef9 | 2023-03-17 15:45:58 +0100 | [diff] [blame] | 1058 | s->scb->flags |= SC_FL_SND_EXP_MORE; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1059 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1060 | DBG_TRACE_DEVEL("waiting for more data to forward", |
| 1061 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1062 | return 0; |
| 1063 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1064 | return_cli_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1065 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 1066 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1067 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1068 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1069 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1070 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1071 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 1072 | 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] | 1073 | status = 400; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1074 | goto return_prx_cond; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1075 | |
| 1076 | return_srv_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1077 | _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts); |
| 1078 | _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1079 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1080 | _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1081 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1082 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1083 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 1084 | 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] | 1085 | status = 502; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1086 | goto return_prx_cond; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1087 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1088 | return_int_err: |
| 1089 | if (!(s->flags & SF_ERR_MASK)) |
| 1090 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1091 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 1092 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1093 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1094 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1095 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1096 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1097 | status = 500; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1098 | goto return_prx_cond; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1099 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1100 | return_bad_req: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1101 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1102 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1103 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1104 | status = 400; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1105 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1106 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1107 | return_prx_cond: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1108 | if (txn->status > 0) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1109 | /* 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] | 1110 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1111 | } else { |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1112 | txn->status = status; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1113 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1114 | } |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 1115 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1116 | DBG_TRACE_DEVEL("leaving on error ", |
| 1117 | 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] | 1118 | return 0; |
| 1119 | } |
| 1120 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1121 | /* 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] | 1122 | /* Returns 0 if we can attempt to retry, -1 otherwise */ |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 1123 | static __inline int do_l7_retry(struct stream *s, struct stconn *sc) |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1124 | { |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1125 | struct channel *req, *res; |
| 1126 | int co_data; |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1127 | |
Christopher Faulet | 731c8e6 | 2022-03-29 16:08:44 +0200 | [diff] [blame] | 1128 | if (s->conn_retries >= s->be->conn_retries) |
Christopher Faulet | 552601d | 2021-05-26 10:31:06 +0200 | [diff] [blame] | 1129 | return -1; |
Christopher Faulet | 41ade74 | 2023-03-01 15:45:39 +0100 | [diff] [blame] | 1130 | s->conn_retries++; |
Christopher Faulet | e763c8c | 2021-05-05 18:23:59 +0200 | [diff] [blame] | 1131 | if (objt_server(s->target)) { |
| 1132 | if (s->flags & SF_CURR_SESS) { |
| 1133 | s->flags &= ~SF_CURR_SESS; |
| 1134 | _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess); |
| 1135 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1136 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries); |
Christopher Faulet | e763c8c | 2021-05-05 18:23:59 +0200 | [diff] [blame] | 1137 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1138 | _HA_ATOMIC_INC(&s->be->be_counters.retries); |
Willy Tarreau | 223995e | 2019-05-04 10:38:31 +0200 | [diff] [blame] | 1139 | |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1140 | req = &s->req; |
| 1141 | res = &s->res; |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 1142 | |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1143 | /* Remove any write error from the request, and read error from the response */ |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 1144 | s->scf->flags &= ~(SC_FL_EOS|SC_FL_ABRT_DONE|SC_FL_ABRT_WANTED); |
Christopher Faulet | 87633c3 | 2023-04-03 18:32:50 +0200 | [diff] [blame] | 1145 | req->flags &= ~CF_WRITE_TIMEOUT; |
| 1146 | res->flags &= ~(CF_READ_TIMEOUT | CF_READ_EVENT); |
Christopher Faulet | 7bf46bb | 2022-01-04 10:56:03 +0100 | [diff] [blame] | 1147 | res->analysers &= AN_RES_FLT_END; |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 1148 | s->conn_err_type = STRM_ET_NONE; |
Christopher Faulet | ae024ce | 2022-03-29 19:02:31 +0200 | [diff] [blame] | 1149 | s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK); |
| 1150 | s->conn_exp = TICK_ETERNITY; |
Olivier Houchard | 4bd5867 | 2019-07-12 16:16:59 +0200 | [diff] [blame] | 1151 | stream_choose_redispatch(s); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1152 | res->to_forward = 0; |
| 1153 | res->analyse_exp = TICK_ETERNITY; |
| 1154 | res->total = 0; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 1155 | |
Christopher Faulet | a1d14a7 | 2023-04-14 10:42:08 +0200 | [diff] [blame] | 1156 | s->scb->flags &= ~(SC_FL_ERROR|SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED); |
Willy Tarreau | 19c65a9 | 2022-05-27 08:49:24 +0200 | [diff] [blame] | 1157 | if (sc_reset_endp(s->scb) < 0) { |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 1158 | if (!(s->flags & SF_ERR_MASK)) |
| 1159 | s->flags |= SF_ERR_INTERNAL; |
| 1160 | return -1; |
| 1161 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1162 | |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1163 | b_free(&req->buf); |
| 1164 | /* Swap the L7 buffer with the channel buffer */ |
| 1165 | /* 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] | 1166 | co_data = b_data(&s->txn->l7_buffer); |
| 1167 | b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer)); |
| 1168 | 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] | 1169 | co_set_data(req, co_data); |
Christopher Faulet | 5b82cc5 | 2020-10-12 15:18:50 +0200 | [diff] [blame] | 1170 | |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 1171 | 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] | 1172 | |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1173 | b_reset(&res->buf); |
| 1174 | co_set_data(res, 0); |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1178 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 1179 | * processing can continue on next analysers, or zero if it either needs more |
| 1180 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 1181 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers |
| 1182 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1183 | * abort. |
| 1184 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1185 | 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] | 1186 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1187 | /* |
| 1188 | * We will analyze a complete HTTP response to check the its syntax. |
| 1189 | * |
| 1190 | * Once the start line and all headers are received, we may perform a |
| 1191 | * capture of the error (if any), and we will set a few fields. We also |
| 1192 | * logging and finally headers capture. |
| 1193 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1194 | struct session *sess = s->sess; |
| 1195 | struct http_txn *txn = s->txn; |
| 1196 | struct http_msg *msg = &txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1197 | struct htx *htx; |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1198 | struct connection *srv_conn; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1199 | struct htx_sl *sl; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1200 | int n; |
| 1201 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1202 | 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] | 1203 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1204 | htx = htxbuf(&rep->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1205 | |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1206 | /* Parsing errors are caught here */ |
| 1207 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 1208 | goto return_bad_res; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1209 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 1210 | goto return_int_err; |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1211 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1212 | /* |
| 1213 | * Now we quickly check if we have found a full valid response. |
| 1214 | * If not so, we check the FD and buffer states before leaving. |
| 1215 | * A full response is indicated by the fact that we have seen |
| 1216 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
| 1217 | * responses are checked first. |
| 1218 | * |
| 1219 | * Depending on whether the client is still there or not, we |
| 1220 | * may send an error response back or not. Note that normally |
| 1221 | * we should only check for HTTP status there, and check I/O |
| 1222 | * errors somewhere else. |
| 1223 | */ |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1224 | next_one: |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 1225 | if (unlikely(htx_is_empty(htx) || htx->first == -1)) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1226 | /* 1: have we encountered a read error ? */ |
Christopher Faulet | d7bac88 | 2023-04-14 12:05:55 +0200 | [diff] [blame] | 1227 | if (s->scb->flags & SC_FL_ERROR) { |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 1228 | struct connection *conn = sc_conn(s->scb); |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1229 | |
Christopher Faulet | 6f78ac5 | 2023-03-01 15:47:18 +0100 | [diff] [blame] | 1230 | |
| 1231 | if ((txn->flags & TX_L7_RETRY) && |
| 1232 | (s->be->retry_type & PR_RE_DISCONNECTED) && |
| 1233 | (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) { |
| 1234 | if (co_data(rep) || do_l7_retry(s, s->scb) == 0) |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
| 1238 | /* Perform a L7 retry on empty response or because server refuses the early data. */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1239 | if ((txn->flags & TX_L7_RETRY) && |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1240 | (s->be->retry_type & PR_RE_EARLY_ERROR) && |
| 1241 | conn && conn->err_code == CO_ER_SSL_EARLY_FAILED && |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1242 | do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1243 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1244 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
Christopher Faulet | ca02388 | 2024-04-09 08:19:01 +0200 | [diff] [blame] | 1248 | if (s->flags & SF_SRV_REUSED) |
Olivier Houchard | 6db1699 | 2019-05-17 15:40:49 +0200 | [diff] [blame] | 1249 | goto abort_keep_alive; |
| 1250 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1251 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1252 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1253 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1254 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1255 | } |
| 1256 | |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1257 | /* if the server refused the early data, just send a 425 */ |
| 1258 | if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1259 | txn->status = 425; |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1260 | else { |
| 1261 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1262 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1263 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1264 | |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1265 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1266 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1267 | |
| 1268 | if (!(s->flags & SF_ERR_MASK)) |
| 1269 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1270 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1271 | DBG_TRACE_DEVEL("leaving on error", |
| 1272 | 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] | 1273 | return 0; |
| 1274 | } |
| 1275 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1276 | /* 2: read timeout : return a 504 to the client. */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1277 | else if (rep->flags & CF_READ_TIMEOUT) { |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1278 | if ((txn->flags & TX_L7_RETRY) && |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1279 | (s->be->retry_type & PR_RE_TIMEOUT)) { |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1280 | if (co_data(rep) || do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1281 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1282 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1283 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1284 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1285 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1286 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1287 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1288 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1289 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1290 | } |
| 1291 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1292 | txn->status = 504; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1293 | stream_inc_http_fail_ctr(s); |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1294 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1295 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1296 | |
| 1297 | if (!(s->flags & SF_ERR_MASK)) |
| 1298 | s->flags |= SF_ERR_SRVTO; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1299 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1300 | DBG_TRACE_DEVEL("leaving on error", |
| 1301 | 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] | 1302 | return 0; |
| 1303 | } |
| 1304 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1305 | /* 3: client abort with an abortonclose */ |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 1306 | else if ((s->scb->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) && (s->scb->flags & SC_FL_SHUT_DONE) && |
| 1307 | (s->scf->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1308 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 1309 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1310 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1311 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1312 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1313 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1314 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1315 | txn->status = 400; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1316 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1317 | |
| 1318 | if (!(s->flags & SF_ERR_MASK)) |
| 1319 | s->flags |= SF_ERR_CLICL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1320 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1321 | |
| 1322 | /* process_stream() will take care of the error */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1323 | DBG_TRACE_DEVEL("leaving on error", |
| 1324 | 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] | 1325 | return 0; |
| 1326 | } |
| 1327 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1328 | /* 4: close from server, capture the response if the server has started to respond */ |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 1329 | else if (s->scb->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) { |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1330 | if ((txn->flags & TX_L7_RETRY) && |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1331 | (s->be->retry_type & PR_RE_DISCONNECTED)) { |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1332 | if (co_data(rep) || do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1333 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1334 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1335 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1336 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1337 | } |
| 1338 | |
Christopher Faulet | ca02388 | 2024-04-09 08:19:01 +0200 | [diff] [blame] | 1339 | if (s->flags & SF_SRV_REUSED) |
Olivier Houchard | 6db1699 | 2019-05-17 15:40:49 +0200 | [diff] [blame] | 1340 | goto abort_keep_alive; |
| 1341 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1342 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1343 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1344 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1345 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1346 | } |
| 1347 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1348 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1349 | stream_inc_http_fail_ctr(s); |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1350 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1351 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1352 | |
| 1353 | if (!(s->flags & SF_ERR_MASK)) |
| 1354 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1355 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1356 | DBG_TRACE_DEVEL("leaving on error", |
| 1357 | 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] | 1358 | return 0; |
| 1359 | } |
| 1360 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1361 | /* 5: write error to client (we don't send any message then) */ |
Christopher Faulet | 2e56a73 | 2023-01-26 16:18:09 +0100 | [diff] [blame] | 1362 | else if (sc_ep_test(s->scf, SE_FL_ERR_PENDING)) { |
Christopher Faulet | ca02388 | 2024-04-09 08:19:01 +0200 | [diff] [blame] | 1363 | if (s->flags & SF_SRV_REUSED) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1364 | goto abort_keep_alive; |
| 1365 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1366 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1367 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1368 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1369 | rep->analysers &= AN_RES_FLT_END; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1370 | |
| 1371 | if (!(s->flags & SF_ERR_MASK)) |
| 1372 | s->flags |= SF_ERR_CLICL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1373 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1374 | |
| 1375 | /* process_stream() will take care of the error */ |
Christopher Faulet | 740420d | 2024-08-01 15:42:09 +0200 | [diff] [blame] | 1376 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1377 | DBG_TRACE_DEVEL("leaving on error", |
| 1378 | 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] | 1379 | return 0; |
| 1380 | } |
| 1381 | |
| 1382 | channel_dont_close(rep); |
Christopher Faulet | 9a790f6 | 2023-03-16 14:40:03 +0100 | [diff] [blame] | 1383 | s->scb->flags |= SC_FL_RCV_ONCE; /* try to get back here ASAP */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1384 | DBG_TRACE_DEVEL("waiting for more data", |
| 1385 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1386 | return 0; |
| 1387 | } |
| 1388 | |
| 1389 | /* More interesting part now : we know that we have a complete |
| 1390 | * response which at least looks like HTTP. We have an indicator |
| 1391 | * of each header's length, so we can parse them quickly. |
| 1392 | */ |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 1393 | BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1394 | sl = http_get_stline(htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1395 | |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1396 | /* Perform a L7 retry because of the status code */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1397 | if ((txn->flags & TX_L7_RETRY) && |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1398 | l7_status_match(s->be, sl->info.res.status) && |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1399 | do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1400 | DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 1401 | return 0; |
| 1402 | } |
| 1403 | |
| 1404 | /* Now, L7 buffer is useless, it can be released */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1405 | b_free(&txn->l7_buffer); |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1406 | |
| 1407 | msg->msg_state = HTTP_MSG_BODY; |
| 1408 | |
| 1409 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1410 | /* 0: we might have to print this header in debug mode */ |
| 1411 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1412 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 1413 | int32_t pos; |
| 1414 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1415 | http_debug_stline("srvrep", s, sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1416 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 1417 | 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] | 1418 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 1419 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 1420 | |
| 1421 | if (type == HTX_BLK_EOH) |
| 1422 | break; |
| 1423 | if (type != HTX_BLK_HDR) |
| 1424 | continue; |
| 1425 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1426 | http_debug_hdr("srvhdr", s, |
| 1427 | htx_get_blk_name(htx, blk), |
| 1428 | htx_get_blk_value(htx, blk)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1429 | } |
| 1430 | } |
| 1431 | |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1432 | /* 1: get the status code and the version. Also set HTTP flags */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1433 | txn->status = sl->info.res.status; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1434 | if (sl->flags & HTX_SL_F_VER_11) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1435 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1436 | if (sl->flags & HTX_SL_F_XFER_LEN) { |
| 1437 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 1438 | if (sl->flags & HTX_SL_F_CLEN) |
| 1439 | msg->flags |= HTTP_MSGF_CNT_LEN; |
| 1440 | else if (sl->flags & HTX_SL_F_CHNK) |
| 1441 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1442 | } |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 1443 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 1444 | msg->flags |= HTTP_MSGF_BODYLESS; |
Christopher Faulet | 576c358 | 2021-01-08 15:53:01 +0100 | [diff] [blame] | 1445 | if (sl->flags & HTX_SL_F_CONN_UPG) |
| 1446 | msg->flags |= HTTP_MSGF_CONN_UPG; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1447 | |
| 1448 | n = txn->status / 100; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1449 | if (n < 1 || n > 5) |
| 1450 | n = 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1451 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1452 | /* when the client triggers a 4xx from the server, it's most often due |
| 1453 | * to a missing object or permission. These events should be tracked |
| 1454 | * because if they happen often, it may indicate a brute force or a |
| 1455 | * vulnerability scan. |
| 1456 | */ |
| 1457 | if (n == 4) |
| 1458 | stream_inc_http_err_ctr(s); |
| 1459 | |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1460 | if (n == 5 && txn->status != 501 && txn->status != 505) |
| 1461 | stream_inc_http_fail_ctr(s); |
| 1462 | |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 1463 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1464 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]); |
| 1465 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req); |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 1466 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1467 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1468 | /* Adjust server's health based on status code. Note: status codes 501 |
| 1469 | * and 505 are triggered on demand by client request, so we must not |
| 1470 | * count them as server failures. |
| 1471 | */ |
| 1472 | if (objt_server(s->target)) { |
| 1473 | 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] | 1474 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1475 | else |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1476 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | /* |
| 1480 | * We may be facing a 100-continue response, or any other informational |
| 1481 | * 1xx response which is non-final, in which case this is not the right |
| 1482 | * response, and we're waiting for the next one. Let's allow this response |
| 1483 | * to go to the client and wait for the next one. There's an exception for |
| 1484 | * 101 which is used later in the code to switch protocols. |
| 1485 | */ |
| 1486 | if (txn->status < 200 && |
| 1487 | (txn->status == 100 || txn->status >= 102)) { |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1488 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1489 | htx->first = channel_htx_fwd_headers(rep, htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1490 | msg->msg_state = HTTP_MSG_RPBEFORE; |
Christopher Faulet | 3499f62 | 2019-09-03 15:23:54 +0200 | [diff] [blame] | 1491 | msg->flags = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1492 | txn->status = 0; |
| 1493 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 5c281d5 | 2023-03-16 15:53:28 +0100 | [diff] [blame] | 1494 | s->scf->flags |= SC_FL_SND_ASAP; /* Send ASAP informational messages */ |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1495 | goto next_one; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1496 | } |
| 1497 | |
Christopher Faulet | 6e6c7b1 | 2021-01-08 16:02:05 +0100 | [diff] [blame] | 1498 | /* A 101-switching-protocols must contains a Connection header with the |
| 1499 | * "upgrade" option and the request too. It means both are agree to |
| 1500 | * upgrade. It is not so strict because there is no test on the Upgrade |
| 1501 | * header content. But it is probably stronger enough for now. |
| 1502 | */ |
| 1503 | if (txn->status == 101 && |
| 1504 | (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG))) |
| 1505 | goto return_bad_res; |
| 1506 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1507 | /* |
| 1508 | * 2: check for cacheability. |
| 1509 | */ |
| 1510 | |
| 1511 | switch (txn->status) { |
| 1512 | case 200: |
| 1513 | case 203: |
| 1514 | case 204: |
| 1515 | case 206: |
| 1516 | case 300: |
| 1517 | case 301: |
| 1518 | case 404: |
| 1519 | case 405: |
| 1520 | case 410: |
| 1521 | case 414: |
| 1522 | case 501: |
| 1523 | break; |
| 1524 | default: |
| 1525 | /* RFC7231#6.1: |
| 1526 | * Responses with status codes that are defined as |
| 1527 | * cacheable by default (e.g., 200, 203, 204, 206, |
| 1528 | * 300, 301, 404, 405, 410, 414, and 501 in this |
| 1529 | * specification) can be reused by a cache with |
| 1530 | * heuristic expiration unless otherwise indicated |
| 1531 | * by the method definition or explicit cache |
| 1532 | * controls [RFC7234]; all other status codes are |
| 1533 | * not cacheable by default. |
| 1534 | */ |
| 1535 | txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK); |
| 1536 | break; |
| 1537 | } |
| 1538 | |
| 1539 | /* |
| 1540 | * 3: we may need to capture headers |
| 1541 | */ |
| 1542 | s->logs.logwait &= ~LW_RESP; |
| 1543 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1544 | http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1545 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1546 | /* Skip parsing if no content length is possible. */ |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 1547 | 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] | 1548 | txn->status == 101)) { |
| 1549 | /* Either we've established an explicit tunnel, or we're |
| 1550 | * switching the protocol. In both cases, we're very unlikely |
| 1551 | * to understand the next protocols. We have to switch to tunnel |
| 1552 | * mode, so that we transfer the request and responses then let |
| 1553 | * this protocol pass unmodified. When we later implement specific |
| 1554 | * parsers for such protocols, we'll want to check the Upgrade |
| 1555 | * header which contains information about that protocol for |
| 1556 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 1557 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 1558 | txn->flags |= TX_CON_WANT_TUN; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1559 | } |
| 1560 | |
Amaury Denoyelle | ab6b074 | 2024-03-20 09:25:03 +0100 | [diff] [blame] | 1561 | /* Check for NTML authentication headers in 401 (WWW-Authenticate) and |
| 1562 | * 407 (Proxy-Authenticate) responses and set the connection to |
| 1563 | * private. |
| 1564 | * |
| 1565 | * Note that this is not performed when using a true multiplexer unless |
| 1566 | * connection is already attached to the session as nothing prevents it |
| 1567 | * from being shared already by several sessions here. |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1568 | */ |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 1569 | srv_conn = sc_conn(s->scb); |
Amaury Denoyelle | ab6b074 | 2024-03-20 09:25:03 +0100 | [diff] [blame] | 1570 | if (srv_conn && |
| 1571 | (LIST_INLIST(&srv_conn->sess_el) || strcmp(srv_conn->mux->name, "H1") == 0)) { |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1572 | struct ist hdr; |
| 1573 | struct http_hdr_ctx ctx; |
| 1574 | |
| 1575 | if (txn->status == 401) |
| 1576 | hdr = ist("WWW-Authenticate"); |
| 1577 | else if (txn->status == 407) |
| 1578 | hdr = ist("Proxy-Authenticate"); |
| 1579 | else |
| 1580 | goto end; |
| 1581 | |
| 1582 | ctx.blk = NULL; |
| 1583 | while (http_find_header(htx, hdr, &ctx, 0)) { |
Willy Tarreau | f1dcced | 2020-05-07 19:27:02 +0200 | [diff] [blame] | 1584 | /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM", |
| 1585 | * possibly followed by blanks and a base64 string, the connection |
| 1586 | * is private. Since it's a mess to deal with, we only check for |
| 1587 | * values starting with "NTLM" or "Nego". Note that often multiple |
| 1588 | * headers are sent by the server there. |
| 1589 | */ |
| 1590 | if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) || |
Willy Tarreau | 49a1d28 | 2020-05-07 19:10:15 +0200 | [diff] [blame] | 1591 | (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) { |
Olivier Houchard | 250031e | 2019-05-29 15:01:50 +0200 | [diff] [blame] | 1592 | sess->flags |= SESS_FL_PREFER_LAST; |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 1593 | conn_set_owner(srv_conn, sess, NULL); |
Christopher Faulet | 21ddc74 | 2020-07-01 15:26:14 +0200 | [diff] [blame] | 1594 | conn_set_private(srv_conn); |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 1595 | /* 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] | 1596 | session_add_conn(srv_conn->owner, srv_conn, srv_conn->target); |
Willy Tarreau | f1dcced | 2020-05-07 19:27:02 +0200 | [diff] [blame] | 1597 | break; |
Olivier Houchard | 250031e | 2019-05-29 15:01:50 +0200 | [diff] [blame] | 1598 | } |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | end: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1603 | /* we want to have the response time before we start processing it */ |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 1604 | s->logs.t_data = ns_to_ms(now_ns - s->logs.accept_ts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1605 | |
| 1606 | /* end of job, return OK */ |
| 1607 | rep->analysers &= ~an_bit; |
| 1608 | rep->analyse_exp = TICK_ETERNITY; |
| 1609 | channel_auto_close(rep); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1610 | 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] | 1611 | return 1; |
| 1612 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1613 | return_int_err: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1614 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 1615 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1616 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1617 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1618 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1619 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1620 | txn->status = 500; |
| 1621 | if (!(s->flags & SF_ERR_MASK)) |
| 1622 | s->flags |= SF_ERR_INTERNAL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1623 | goto return_prx_cond; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1624 | |
| 1625 | return_bad_res: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1626 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1627 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1628 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1629 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1630 | } |
Olivier Houchard | e3249a9 | 2019-05-03 23:01:47 +0200 | [diff] [blame] | 1631 | if ((s->be->retry_type & PR_RE_JUNK_REQUEST) && |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1632 | (txn->flags & TX_L7_RETRY) && |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1633 | do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1634 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1635 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | e3249a9 | 2019-05-03 23:01:47 +0200 | [diff] [blame] | 1636 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1637 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1638 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1639 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1640 | /* fall through */ |
| 1641 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1642 | return_prx_cond: |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1643 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1644 | http_set_term_flags(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1645 | |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1646 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1647 | DBG_TRACE_DEVEL("leaving on error", |
| 1648 | 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] | 1649 | return 0; |
| 1650 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1651 | abort_keep_alive: |
| 1652 | /* A keep-alive request to the server failed on a network error. |
| 1653 | * The client is required to retry. We need to close without returning |
| 1654 | * any other information so that the client retries. |
| 1655 | */ |
| 1656 | txn->status = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1657 | s->logs.logwait = 0; |
| 1658 | s->logs.level = 0; |
Christopher Faulet | 84d3ef9 | 2023-03-17 15:45:58 +0100 | [diff] [blame] | 1659 | s->scf->flags &= ~SC_FL_SND_EXP_MORE; /* speed up sending a previous response */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1660 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1661 | DBG_TRACE_DEVEL("leaving by closing K/A connection", |
| 1662 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1663 | return 0; |
| 1664 | } |
| 1665 | |
| 1666 | /* This function performs all the processing enabled for the current response. |
| 1667 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 1668 | * and updates s->res.analysers. It might make sense to explode it into several |
| 1669 | * other functions. It works like process_request (see indications above). |
| 1670 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1671 | 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] | 1672 | { |
| 1673 | struct session *sess = s->sess; |
| 1674 | struct http_txn *txn = s->txn; |
| 1675 | struct http_msg *msg = &txn->rsp; |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1676 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1677 | struct proxy *cur_proxy; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1678 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 1679 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1680 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 1681 | return 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1682 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1683 | 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] | 1684 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1685 | htx = htxbuf(&rep->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1686 | |
| 1687 | /* The stats applet needs to adjust the Connection header but we don't |
| 1688 | * apply any filter there. |
| 1689 | */ |
| 1690 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 1691 | rep->analysers &= ~an_bit; |
| 1692 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1693 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | /* |
| 1697 | * We will have to evaluate the filters. |
| 1698 | * As opposed to version 1.2, now they will be evaluated in the |
| 1699 | * filters order and not in the header order. This means that |
| 1700 | * each filter has to be validated among all headers. |
| 1701 | * |
| 1702 | * Filters are tried with ->be first, then with ->fe if it is |
| 1703 | * different from ->be. |
| 1704 | * |
| 1705 | * Maybe we are in resume condiion. In this case I choose the |
| 1706 | * "struct proxy" which contains the rule list matching the resume |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1707 | * pointer. If none of these "struct proxy" match, I initialise |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1708 | * the process with the first one. |
| 1709 | * |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1710 | * In fact, I check only correspondence between the current list |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1711 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 1712 | * the loop with the ->be. |
| 1713 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1714 | if (s->current_rule_list == &sess->fe->http_res_rules || |
| 1715 | (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] | 1716 | cur_proxy = sess->fe; |
| 1717 | else |
| 1718 | cur_proxy = s->be; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1719 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1720 | while (1) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1721 | /* evaluate http-response rules */ |
Christopher Faulet | 46f46df | 2021-11-09 16:33:25 +0100 | [diff] [blame] | 1722 | if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1723 | struct list *def_rules, *rules; |
| 1724 | |
| 1725 | def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL); |
| 1726 | rules = &cur_proxy->http_res_rules; |
| 1727 | |
| 1728 | ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1729 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1730 | switch (ret) { |
| 1731 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 1732 | goto return_prx_yield; |
| 1733 | |
| 1734 | case HTTP_RULE_RES_CONT: |
| 1735 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 1736 | break; |
| 1737 | |
| 1738 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 1739 | goto deny; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1740 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1741 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent */ |
| 1742 | goto return_prx_cond; |
| 1743 | |
| 1744 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1745 | goto done; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1746 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1747 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 1748 | goto return_bad_res; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1749 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1750 | case HTTP_RULE_RES_ERROR: /* failed with a bad request */ |
| 1751 | goto return_int_err; |
| 1752 | } |
| 1753 | |
| 1754 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1755 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1756 | /* check whether we're already working on the frontend */ |
| 1757 | if (cur_proxy == sess->fe) |
| 1758 | break; |
| 1759 | cur_proxy = sess->fe; |
| 1760 | } |
| 1761 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1762 | /* OK that's all we can do for 1xx responses */ |
| 1763 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1764 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1765 | |
| 1766 | /* |
| 1767 | * Now check for a server cookie. |
| 1768 | */ |
| 1769 | 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] | 1770 | http_manage_server_side_cookies(s, rep); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1771 | |
| 1772 | /* |
| 1773 | * Check for cache-control or pragma headers if required. |
| 1774 | */ |
| 1775 | 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] | 1776 | http_check_response_for_cacheability(s, rep); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1777 | |
| 1778 | /* |
| 1779 | * Add server cookie in the response if needed |
| 1780 | */ |
| 1781 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 1782 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
| 1783 | (!(s->flags & SF_DIRECT) || |
| 1784 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 1785 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 1786 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 1787 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 1788 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 1789 | !(s->flags & SF_IGNORE_PRST)) { |
| 1790 | /* the server is known, it's not the one the client requested, or the |
| 1791 | * cookie's last seen date needs to be refreshed. We have to |
| 1792 | * insert a set-cookie here, except if we want to insert only on POST |
| 1793 | * requests and this one isn't. Note that servers which don't have cookies |
| 1794 | * (eg: some backup servers) will return a full cookie removal request. |
| 1795 | */ |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1796 | if (!__objt_server(s->target)->cookie) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1797 | chunk_printf(&trash, |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1798 | "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1799 | s->be->cookie_name); |
| 1800 | } |
| 1801 | else { |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1802 | 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] | 1803 | |
| 1804 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 1805 | /* emit last_date, which is mandatory */ |
| 1806 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 1807 | s30tob64((date.tv_sec+3) >> 2, |
| 1808 | trash.area + trash.data); |
| 1809 | trash.data += 5; |
| 1810 | |
| 1811 | if (s->be->cookie_maxlife) { |
| 1812 | /* emit first_date, which is either the original one or |
| 1813 | * the current date. |
| 1814 | */ |
| 1815 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 1816 | s30tob64(txn->cookie_first_date ? |
| 1817 | txn->cookie_first_date >> 2 : |
| 1818 | (date.tv_sec+3) >> 2, |
| 1819 | trash.area + trash.data); |
| 1820 | trash.data += 5; |
| 1821 | } |
| 1822 | } |
| 1823 | chunk_appendf(&trash, "; path=/"); |
| 1824 | } |
| 1825 | |
| 1826 | if (s->be->cookie_domain) |
| 1827 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
| 1828 | |
| 1829 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 1830 | chunk_appendf(&trash, "; HttpOnly"); |
| 1831 | |
| 1832 | if (s->be->ck_opts & PR_CK_SECURE) |
| 1833 | chunk_appendf(&trash, "; Secure"); |
| 1834 | |
Christopher Faulet | 2f53390 | 2020-01-21 11:06:48 +0100 | [diff] [blame] | 1835 | if (s->be->cookie_attrs) |
| 1836 | chunk_appendf(&trash, "; %s", s->be->cookie_attrs); |
| 1837 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1838 | 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] | 1839 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1840 | |
| 1841 | txn->flags &= ~TX_SCK_MASK; |
| 1842 | if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
| 1843 | /* the server did not change, only the date was updated */ |
| 1844 | txn->flags |= TX_SCK_UPDATED; |
| 1845 | else |
| 1846 | txn->flags |= TX_SCK_INSERTED; |
| 1847 | |
| 1848 | /* Here, we will tell an eventual cache on the client side that we don't |
| 1849 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 1850 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 1851 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 1852 | */ |
| 1853 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
| 1854 | |
| 1855 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 1856 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1857 | if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private")))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 1858 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1859 | } |
| 1860 | } |
| 1861 | |
| 1862 | /* |
| 1863 | * Check if result will be cacheable with a cookie. |
| 1864 | * We'll block the response if security checks have caught |
| 1865 | * nasty things such as a cacheable cookie. |
| 1866 | */ |
| 1867 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 1868 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 1869 | (s->be->options & PR_O_CHK_CACHE)) { |
| 1870 | /* we're in presence of a cacheable response containing |
| 1871 | * a set-cookie header. We'll block it as requested by |
| 1872 | * the 'checkcache' option, and send an alert. |
| 1873 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1874 | 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] | 1875 | 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] | 1876 | send_log(s->be, LOG_ALERT, |
| 1877 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1878 | 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] | 1879 | goto deny; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1880 | } |
| 1881 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1882 | end: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 1883 | /* |
| 1884 | * Evaluate after-response rules before forwarding the response. rules |
| 1885 | * from the backend are evaluated first, then one from the frontend if |
| 1886 | * it differs. |
| 1887 | */ |
| 1888 | if (!http_eval_after_res_rules(s)) |
| 1889 | goto return_int_err; |
| 1890 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 1891 | /* Filter the response headers if there are filters attached to the |
| 1892 | * stream. |
| 1893 | */ |
| 1894 | if (HAS_FILTERS(s)) |
| 1895 | rep->analysers |= AN_RES_FLT_HTTP_HDRS; |
| 1896 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1897 | /* Always enter in the body analyzer */ |
| 1898 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 1899 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
| 1900 | |
| 1901 | /* if the user wants to log as soon as possible, without counting |
| 1902 | * bytes from the server, then this is the right moment. We have |
| 1903 | * to temporarily assign bytes_out to log what we currently have. |
| 1904 | */ |
| 1905 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
| 1906 | 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] | 1907 | s->logs.bytes_out = htx->data; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1908 | s->do_log(s); |
| 1909 | s->logs.bytes_out = 0; |
| 1910 | } |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1911 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1912 | done: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 1913 | 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] | 1914 | rep->analysers &= ~an_bit; |
| 1915 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1916 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1917 | return 1; |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1918 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1919 | deny: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1920 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp); |
| 1921 | _HA_ATOMIC_INC(&s->be->be_counters.denied_resp); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1922 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1923 | _HA_ATOMIC_INC(&sess->listener->counters->denied_resp); |
Christopher Faulet | a08546b | 2019-12-16 16:07:34 +0100 | [diff] [blame] | 1924 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1925 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1926 | goto return_prx_err; |
| 1927 | |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 1928 | return_fail_rewrite: |
| 1929 | if (!(s->flags & SF_ERR_MASK)) |
| 1930 | s->flags |= SF_ERR_PRXCOND; |
| 1931 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
| 1932 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
| 1933 | if (sess->listener && sess->listener->counters) |
| 1934 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
| 1935 | if (objt_server(s->target)) |
| 1936 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
| 1937 | /* fall through */ |
| 1938 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1939 | return_int_err: |
| 1940 | txn->status = 500; |
| 1941 | if (!(s->flags & SF_ERR_MASK)) |
| 1942 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1943 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 1944 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
Dragan Dosen | 9a006f9 | 2021-09-21 13:02:09 +0200 | [diff] [blame] | 1945 | if (sess->listener && sess->listener->counters) |
| 1946 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1947 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1948 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1949 | goto return_prx_err; |
| 1950 | |
| 1951 | return_bad_res: |
| 1952 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1953 | stream_inc_http_fail_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1954 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | a20a653 | 2020-02-05 10:16:41 +0100 | [diff] [blame] | 1955 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1956 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | a20a653 | 2020-02-05 10:16:41 +0100 | [diff] [blame] | 1957 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 1958 | } |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1959 | /* fall through */ |
| 1960 | |
| 1961 | return_prx_err: |
| 1962 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 1963 | /* fall through */ |
| 1964 | |
| 1965 | return_prx_cond: |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1966 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1967 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1968 | |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1969 | http_set_term_flags(s); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1970 | |
Christopher Faulet | e58c000 | 2020-03-02 16:21:01 +0100 | [diff] [blame] | 1971 | rep->analysers &= AN_RES_FLT_END; |
| 1972 | s->req.analysers &= AN_REQ_FLT_END; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1973 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1974 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1975 | DBG_TRACE_DEVEL("leaving on error", |
| 1976 | 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] | 1977 | return 0; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1978 | |
| 1979 | return_prx_yield: |
| 1980 | channel_dont_close(rep); |
| 1981 | DBG_TRACE_DEVEL("waiting for more data", |
| 1982 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 1983 | return 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | /* This function is an analyser which forwards response body (including chunk |
| 1987 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 1988 | * zero byte. The only situation where it must not be called is when we're in |
| 1989 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 1990 | * remaining data and to resync after end of body. It expects the msg_state to |
| 1991 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 1992 | * read more data, or 1 once we can go on with next request or end the stream. |
| 1993 | * |
| 1994 | * It is capable of compressing response data both in content-length mode and |
| 1995 | * in chunked mode. The state machines follows different flows depending on |
| 1996 | * whether content-length and chunked modes are used, since there are no |
| 1997 | * trailers in content-length : |
| 1998 | * |
| 1999 | * chk-mode cl-mode |
| 2000 | * ,----- BODY -----. |
| 2001 | * / \ |
| 2002 | * V size > 0 V chk-mode |
| 2003 | * .--> SIZE -------------> DATA -------------> CRLF |
| 2004 | * | | size == 0 | last byte | |
| 2005 | * | v final crlf v inspected | |
| 2006 | * | TRAILERS -----------> DONE | |
| 2007 | * | | |
| 2008 | * `----------------------------------------------' |
| 2009 | * |
| 2010 | * Compression only happens in the DATA state, and must be flushed in final |
| 2011 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 2012 | * is performed at once on final states for all bytes parsed, or when leaving |
| 2013 | * on missing data. |
| 2014 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2015 | 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] | 2016 | { |
| 2017 | struct session *sess = s->sess; |
| 2018 | struct http_txn *txn = s->txn; |
| 2019 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2020 | struct htx *htx; |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2021 | int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2022 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2023 | 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] | 2024 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2025 | htx = htxbuf(&res->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2026 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2027 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 2028 | goto return_bad_res; |
| 2029 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 2030 | goto return_int_err; |
| 2031 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2032 | if (msg->msg_state == HTTP_MSG_BODY) |
| 2033 | msg->msg_state = HTTP_MSG_DATA; |
| 2034 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2035 | /* in most states, we should abort in case of early close */ |
| 2036 | channel_auto_close(res); |
| 2037 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2038 | if (res->to_forward) { |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2039 | if (res->to_forward == CHN_INFINITE_FORWARD) { |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 2040 | if (s->scb->flags & SC_FL_EOI) |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2041 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2042 | } |
| 2043 | else { |
| 2044 | /* We can't process the buffer's contents yet */ |
| 2045 | res->flags |= CF_WAKE_WRITE; |
| 2046 | goto missing_data_or_waiting; |
| 2047 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2048 | } |
| 2049 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2050 | if (msg->msg_state >= HTTP_MSG_ENDING) |
| 2051 | goto ending; |
| 2052 | |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 2053 | 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] | 2054 | (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) { |
| 2055 | msg->msg_state = HTTP_MSG_ENDING; |
| 2056 | goto ending; |
| 2057 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2058 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2059 | /* Forward input data. We get it by removing all outgoing data not |
| 2060 | * forwarded yet from HTX data size. If there are some data filters, we |
| 2061 | * let them decide the amount of data to forward. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2062 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2063 | if (HAS_RSP_DATA_FILTERS(s)) { |
| 2064 | ret = flt_http_payload(s, msg, htx->data); |
| 2065 | if (ret < 0) |
| 2066 | goto return_bad_res; |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 2067 | c_adv(res, ret); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2068 | } |
| 2069 | else { |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 2070 | c_adv(res, htx->data - co_data(res)); |
Christopher Faulet | 2f7c82b | 2023-02-20 14:06:52 +0100 | [diff] [blame] | 2071 | 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] | 2072 | channel_htx_forward_forever(res, htx); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2073 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2074 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2075 | if (htx->data != co_data(res)) |
| 2076 | goto missing_data_or_waiting; |
| 2077 | |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 2078 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && (s->scb->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))) { |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2079 | msg->msg_state = HTTP_MSG_ENDING; |
| 2080 | goto ending; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2081 | } |
| 2082 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2083 | /* 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] | 2084 | * in HTTP_MSG_ENDING state. Then if all data was marked to be |
| 2085 | * forwarded, set the state to HTTP_MSG_DONE. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2086 | */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2087 | if (!(htx->flags & HTX_FL_EOM)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2088 | goto missing_data_or_waiting; |
| 2089 | |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 2090 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2091 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2092 | ending: |
Christopher Faulet | 84d3ef9 | 2023-03-17 15:45:58 +0100 | [diff] [blame] | 2093 | s->scf->flags &= ~SC_FL_SND_EXP_MORE; /* no more data are expected to be sent */ |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 2094 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2095 | /* other states, ENDING...TUNNEL */ |
| 2096 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 2097 | goto done; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2098 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2099 | if (HAS_RSP_DATA_FILTERS(s)) { |
| 2100 | ret = flt_http_end(s, msg); |
| 2101 | if (ret <= 0) { |
| 2102 | if (!ret) |
| 2103 | goto missing_data_or_waiting; |
| 2104 | goto return_bad_res; |
| 2105 | } |
| 2106 | } |
| 2107 | |
Christopher Faulet | 9704797 | 2023-04-17 08:52:10 +0200 | [diff] [blame] | 2108 | if (!(txn->flags & TX_CON_WANT_TUN) && !(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 2109 | /* One-side tunnel */ |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2110 | msg->msg_state = HTTP_MSG_TUNNEL; |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2111 | } |
| 2112 | else { |
| 2113 | msg->msg_state = HTTP_MSG_DONE; |
| 2114 | res->to_forward = 0; |
| 2115 | } |
| 2116 | |
| 2117 | done: |
| 2118 | |
| 2119 | channel_dont_close(res); |
| 2120 | |
Christopher Faulet | 64350bb | 2023-04-13 16:37:37 +0200 | [diff] [blame] | 2121 | if ((s->scf->flags & SC_FL_SHUT_DONE) && co_data(res)) { |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 2122 | /* response errors are most likely due to the client aborting |
| 2123 | * the transfer. */ |
| 2124 | goto return_cli_abort; |
| 2125 | } |
| 2126 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2127 | http_end_response(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2128 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2129 | 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] | 2130 | return 1; |
| 2131 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2132 | DBG_TRACE_DEVEL("waiting for the end of the HTTP txn", |
| 2133 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2134 | return 0; |
| 2135 | |
| 2136 | missing_data_or_waiting: |
Christopher Faulet | 64350bb | 2023-04-13 16:37:37 +0200 | [diff] [blame] | 2137 | if (s->scf->flags & SC_FL_SHUT_DONE) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2138 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2139 | |
| 2140 | /* stop waiting for data if the input is closed before the end. If the |
| 2141 | * client side was already closed, it means that the client has aborted, |
| 2142 | * so we don't want to count this as a server abort. Otherwise it's a |
| 2143 | * server abort. |
| 2144 | */ |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 2145 | if (msg->msg_state < HTTP_MSG_ENDING && (s->scb->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))) { |
| 2146 | if ((s->scf->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) && |
Christopher Faulet | 64350bb | 2023-04-13 16:37:37 +0200 | [diff] [blame] | 2147 | (s->scb->flags & SC_FL_SHUT_DONE)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2148 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2149 | /* If we have some pending data, we continue the processing */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2150 | if (htx_is_empty(htx)) |
| 2151 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2152 | } |
| 2153 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2154 | /* When TE: chunked is used, we need to get there again to parse |
| 2155 | * 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] | 2156 | * set CF_DONTCLOSE. Similarly when there is a content-leng or if there |
| 2157 | * are filters registered on the stream, we don't want to forward a |
| 2158 | * close |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2159 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2160 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2161 | channel_dont_close(res); |
| 2162 | |
| 2163 | /* We know that more data are expected, but we couldn't send more that |
Christopher Faulet | 84d3ef9 | 2023-03-17 15:45:58 +0100 | [diff] [blame] | 2164 | * what we did. So we always set the SC_FL_SND_EXP_MORE flag so that the |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2165 | * system knows it must not set a PUSH on this first part. Interactive |
| 2166 | * modes are already handled by the stream sock layer. We must not do |
| 2167 | * this in content-length mode because it could present the MSG_MORE |
| 2168 | * flag with the last block of forwarded data, which would cause an |
| 2169 | * additional delay to be observed by the receiver. |
| 2170 | */ |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 2171 | if (HAS_RSP_DATA_FILTERS(s)) |
Christopher Faulet | 84d3ef9 | 2023-03-17 15:45:58 +0100 | [diff] [blame] | 2172 | s->scf->flags |= SC_FL_SND_EXP_MORE; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2173 | |
| 2174 | /* the stream handler will take care of timeouts and errors */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2175 | DBG_TRACE_DEVEL("waiting for more data to forward", |
| 2176 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2177 | return 0; |
| 2178 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2179 | return_srv_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2180 | _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts); |
| 2181 | _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2182 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2183 | _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2184 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2185 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts); |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2186 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2187 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 2188 | 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] | 2189 | goto return_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2190 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2191 | return_cli_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2192 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 2193 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2194 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2195 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2196 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2197 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2198 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 2199 | 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] | 2200 | goto return_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2201 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2202 | return_int_err: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2203 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 2204 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2205 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2206 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2207 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2208 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2209 | if (!(s->flags & SF_ERR_MASK)) |
| 2210 | s->flags |= SF_ERR_INTERNAL; |
| 2211 | goto return_error; |
| 2212 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2213 | return_bad_res: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2214 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2215 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2216 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2217 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 2218 | } |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2219 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2220 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2221 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2222 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2223 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2224 | return_error: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2225 | /* don't send any error message as we're in the body */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2226 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 2227 | http_set_term_flags(s); |
| 2228 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2229 | DBG_TRACE_DEVEL("leaving on error", |
| 2230 | 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] | 2231 | return 0; |
| 2232 | } |
| 2233 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2234 | /* Perform an HTTP redirect based on the information in <rule>. The function |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2235 | * returns zero in case of an irrecoverable error such as too large a request |
| 2236 | * to build a valid response, 1 in case of successful redirect (hence the rule |
| 2237 | * is final), or 2 if the rule has to be silently skipped. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2238 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2239 | 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] | 2240 | { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2241 | struct channel *req = &s->req; |
| 2242 | struct channel *res = &s->res; |
| 2243 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2244 | struct htx_sl *sl; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2245 | struct buffer *chunk; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2246 | struct ist status, reason, location; |
| 2247 | unsigned int flags; |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2248 | 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] | 2249 | |
| 2250 | chunk = alloc_trash_chunk(); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2251 | if (!chunk) { |
| 2252 | if (!(s->flags & SF_ERR_MASK)) |
| 2253 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2254 | goto fail; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2255 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2256 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2257 | /* |
| 2258 | * Create the location |
| 2259 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2260 | htx = htxbuf(&req->buf); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2261 | switch(rule->type) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2262 | case REDIRECT_TYPE_SCHEME: { |
| 2263 | struct http_hdr_ctx ctx; |
| 2264 | struct ist path, host; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2265 | struct http_uri_parser parser; |
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 | host = ist(""); |
| 2268 | ctx.blk = NULL; |
| 2269 | if (http_find_header(htx, ist("Host"), &ctx, 0)) |
| 2270 | host = ctx.value; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2271 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2272 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2273 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 2274 | path = http_parse_path(&parser); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2275 | /* build message using path */ |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 2276 | if (isttest(path)) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2277 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2278 | int qs = 0; |
| 2279 | while (qs < path.len) { |
| 2280 | if (*(path.ptr + qs) == '?') { |
| 2281 | path.len = qs; |
| 2282 | break; |
| 2283 | } |
| 2284 | qs++; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2285 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2286 | } |
| 2287 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2288 | else |
| 2289 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2290 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2291 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2292 | /* add scheme */ |
| 2293 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2294 | goto fail; |
| 2295 | } |
| 2296 | else { |
| 2297 | /* add scheme with executing log format */ |
| 2298 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2299 | chunk->size - chunk->data, |
| 2300 | &rule->rdr_fmt); |
| 2301 | } |
| 2302 | /* add "://" + host + path */ |
| 2303 | if (!chunk_memcat(chunk, "://", 3) || |
| 2304 | !chunk_memcat(chunk, host.ptr, host.len) || |
| 2305 | !chunk_memcat(chunk, path.ptr, path.len)) |
| 2306 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2307 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2308 | /* append a slash at the end of the location if needed and missing */ |
| 2309 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2310 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2311 | if (chunk->data + 1 >= chunk->size) |
| 2312 | goto fail; |
| 2313 | chunk->area[chunk->data++] = '/'; |
| 2314 | } |
| 2315 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2316 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2317 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2318 | case REDIRECT_TYPE_PREFIX: { |
| 2319 | struct ist path; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2320 | struct http_uri_parser parser; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2321 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2322 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2323 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 2324 | path = http_parse_path(&parser); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2325 | /* build message using path */ |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 2326 | if (isttest(path)) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2327 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2328 | int qs = 0; |
| 2329 | while (qs < path.len) { |
| 2330 | if (*(path.ptr + qs) == '?') { |
| 2331 | path.len = qs; |
| 2332 | break; |
| 2333 | } |
| 2334 | qs++; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2335 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2336 | } |
| 2337 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2338 | else |
| 2339 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2340 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2341 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2342 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2343 | * add anything, otherwise it makes it hard for the user to |
| 2344 | * configure a self-redirection. |
| 2345 | */ |
| 2346 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 2347 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2348 | goto fail; |
| 2349 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2350 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2351 | else { |
| 2352 | /* add prefix with executing log format */ |
| 2353 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2354 | chunk->size - chunk->data, |
| 2355 | &rule->rdr_fmt); |
| 2356 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2357 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2358 | /* add path */ |
| 2359 | if (!chunk_memcat(chunk, path.ptr, path.len)) |
| 2360 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2361 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2362 | /* append a slash at the end of the location if needed and missing */ |
| 2363 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2364 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2365 | if (chunk->data + 1 >= chunk->size) |
| 2366 | goto fail; |
| 2367 | chunk->area[chunk->data++] = '/'; |
| 2368 | } |
| 2369 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2370 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2371 | case REDIRECT_TYPE_LOCATION: |
| 2372 | default: |
| 2373 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2374 | /* add location */ |
| 2375 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2376 | goto fail; |
| 2377 | } |
| 2378 | else { |
| 2379 | /* add location with executing log format */ |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2380 | int len = build_logline(s, chunk->area + chunk->data, |
| 2381 | chunk->size - chunk->data, |
| 2382 | &rule->rdr_fmt); |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2383 | if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) { |
| 2384 | ret = 2; |
| 2385 | goto out; |
| 2386 | } |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2387 | |
| 2388 | chunk->data += len; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2389 | } |
| 2390 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2391 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2392 | location = ist2(chunk->area, chunk->data); |
| 2393 | |
| 2394 | /* |
| 2395 | * Create the 30x response |
| 2396 | */ |
| 2397 | switch (rule->code) { |
| 2398 | case 308: |
| 2399 | status = ist("308"); |
| 2400 | reason = ist("Permanent Redirect"); |
| 2401 | break; |
| 2402 | case 307: |
| 2403 | status = ist("307"); |
| 2404 | reason = ist("Temporary Redirect"); |
| 2405 | break; |
| 2406 | case 303: |
| 2407 | status = ist("303"); |
| 2408 | reason = ist("See Other"); |
| 2409 | break; |
| 2410 | case 301: |
| 2411 | status = ist("301"); |
| 2412 | reason = ist("Moved Permanently"); |
| 2413 | break; |
| 2414 | case 302: |
| 2415 | default: |
| 2416 | status = ist("302"); |
| 2417 | reason = ist("Found"); |
| 2418 | break; |
| 2419 | } |
| 2420 | |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2421 | if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE) |
| 2422 | close = 1; |
| 2423 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2424 | htx = htx_from_buf(&res->buf); |
Kevin Zhu | 96b3639 | 2020-01-07 09:42:55 +0100 | [diff] [blame] | 2425 | /* Trim any possible response */ |
| 2426 | channel_htx_truncate(&s->res, htx); |
Christopher Faulet | 617ec5c | 2023-10-17 11:43:43 +0200 | [diff] [blame] | 2427 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN|HTX_SL_F_BODYLESS); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2428 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason); |
| 2429 | if (!sl) |
| 2430 | goto fail; |
| 2431 | sl->info.res.status = rule->code; |
| 2432 | s->txn->status = rule->code; |
| 2433 | |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2434 | if (close && !htx_add_header(htx, ist("Connection"), ist("close"))) |
| 2435 | goto fail; |
| 2436 | |
| 2437 | if (!htx_add_header(htx, ist("Content-length"), ist("0")) || |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2438 | !htx_add_header(htx, ist("Location"), location)) |
| 2439 | goto fail; |
| 2440 | |
| 2441 | if (rule->code == 302 || rule->code == 303 || rule->code == 307) { |
| 2442 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache"))) |
| 2443 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | if (rule->cookie_len) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2447 | if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len))) |
| 2448 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2449 | } |
| 2450 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2451 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2452 | goto fail; |
| 2453 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2454 | htx->flags |= HTX_FL_EOM; |
Kevin Zhu | 96b3639 | 2020-01-07 09:42:55 +0100 | [diff] [blame] | 2455 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 2456 | if (!http_forward_proxy_resp(s, 1)) |
| 2457 | goto fail; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2458 | |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2459 | if (rule->flags & REDIRECT_FLAG_FROM_REQ) { |
| 2460 | /* let's log the request time */ |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 2461 | s->logs.request_ts = now_ns; |
Christopher Faulet | d347588 | 2021-10-04 14:16:46 +0200 | [diff] [blame] | 2462 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2463 | |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2464 | 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] | 2465 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2466 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2467 | |
| 2468 | if (!(s->flags & SF_ERR_MASK)) |
| 2469 | s->flags |= SF_ERR_LOCAL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 2470 | http_set_term_flags(s); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2471 | |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2472 | out: |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2473 | free_trash_chunk(chunk); |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2474 | return ret; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2475 | |
| 2476 | fail: |
| 2477 | /* If an error occurred, remove the incomplete HTTP response from the |
| 2478 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 2479 | channel_htx_truncate(res, htxbuf(&res->buf)); |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2480 | ret = 0; |
| 2481 | goto out; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2482 | } |
| 2483 | |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2484 | /* This function filters the request header names to only allow [0-9a-zA-Z-] |
| 2485 | * characters. Depending on the proxy configuration, headers with a name not |
| 2486 | * matching this charset are removed or the request is rejected with a |
| 2487 | * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT |
| 2488 | * to continue the request processing or HTTP_RULE_RES_DENY if the request is |
| 2489 | * rejected. |
| 2490 | */ |
| 2491 | static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px) |
| 2492 | { |
| 2493 | struct htx_blk *blk; |
| 2494 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
| 2495 | |
| 2496 | blk = htx_get_first_blk(htx); |
| 2497 | while (blk) { |
| 2498 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 2499 | |
| 2500 | if (type == HTX_BLK_HDR) { |
| 2501 | struct ist n = htx_get_blk_name(htx, blk); |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2502 | int i, end = istlen(n); |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2503 | |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2504 | for (i = 0; i < end; i++) { |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2505 | if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') { |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2506 | break; |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2507 | } |
| 2508 | } |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2509 | |
| 2510 | if (i < end) { |
| 2511 | /* Disallowed character found - block the request or remove the header */ |
| 2512 | if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK) |
| 2513 | goto block; |
| 2514 | blk = htx_remove_blk(htx, blk); |
| 2515 | continue; |
| 2516 | } |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2517 | } |
| 2518 | if (type == HTX_BLK_EOH) |
| 2519 | break; |
| 2520 | |
| 2521 | blk = htx_get_next_blk(htx, blk); |
| 2522 | } |
| 2523 | out: |
| 2524 | return rule_ret; |
| 2525 | block: |
| 2526 | /* Block the request returning a 403-Forbidden response */ |
| 2527 | s->txn->status = 403; |
| 2528 | rule_ret = HTTP_RULE_RES_DENY; |
| 2529 | goto out; |
| 2530 | } |
| 2531 | |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2532 | /* Replace all headers matching the name <name>. The header value is replaced if |
| 2533 | * it matches the regex <re>. <str> is used for the replacement. If <full> is |
| 2534 | * set to 1, the full-line is matched and replaced. Otherwise, comma-separated |
| 2535 | * values are evaluated one by one. It returns 0 on success and -1 on error. |
| 2536 | */ |
| 2537 | int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name, |
| 2538 | const char *str, struct my_regex *re, int full) |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2539 | { |
| 2540 | struct http_hdr_ctx ctx; |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2541 | |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2542 | ctx.blk = NULL; |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2543 | while (http_find_header(htx, name, &ctx, full)) { |
Christopher Faulet | 80707f7 | 2023-08-04 16:51:11 +0200 | [diff] [blame] | 2544 | struct buffer *output = get_trash_chunk(); |
| 2545 | |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2546 | if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0)) |
| 2547 | continue; |
| 2548 | |
| 2549 | output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch); |
| 2550 | if (output->data == -1) |
| 2551 | return -1; |
| 2552 | if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data))) |
| 2553 | return -1; |
| 2554 | } |
| 2555 | return 0; |
| 2556 | } |
| 2557 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2558 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 2559 | * takes the string from the variable 'replace' with length 'len', then modifies |
| 2560 | * the relevant part of the request line accordingly. Then it updates various |
| 2561 | * pointers to the next elements which were moved, and the total buffer length. |
| 2562 | * It finds the action to be performed in p[2], previously filled by function |
| 2563 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 2564 | * error, though this can be revisited when this code is finally exploited. |
| 2565 | * |
| 2566 | * '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] | 2567 | * 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] | 2568 | * |
| 2569 | * In query string case, the mark question '?' must be set at the start of the |
| 2570 | * string by the caller, event if the replacement query string is empty. |
| 2571 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2572 | int http_req_replace_stline(int action, const char *replace, int len, |
| 2573 | struct proxy *px, struct stream *s) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2574 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2575 | struct htx *htx = htxbuf(&s->req.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2576 | |
| 2577 | switch (action) { |
| 2578 | case 0: // method |
| 2579 | if (!http_replace_req_meth(htx, ist2(replace, len))) |
| 2580 | return -1; |
| 2581 | break; |
| 2582 | |
| 2583 | case 1: // path |
Christopher Faulet | b8ce505 | 2020-08-31 16:11:57 +0200 | [diff] [blame] | 2584 | if (!http_replace_req_path(htx, ist2(replace, len), 0)) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2585 | return -1; |
| 2586 | break; |
| 2587 | |
| 2588 | case 2: // query |
| 2589 | if (!http_replace_req_query(htx, ist2(replace, len))) |
| 2590 | return -1; |
| 2591 | break; |
| 2592 | |
| 2593 | case 3: // uri |
| 2594 | if (!http_replace_req_uri(htx, ist2(replace, len))) |
| 2595 | return -1; |
| 2596 | break; |
| 2597 | |
Christopher Faulet | 312294f | 2020-09-02 17:17:44 +0200 | [diff] [blame] | 2598 | case 4: // path + query |
| 2599 | if (!http_replace_req_path(htx, ist2(replace, len), 1)) |
| 2600 | return -1; |
| 2601 | break; |
| 2602 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2603 | default: |
| 2604 | return -1; |
| 2605 | } |
| 2606 | return 0; |
| 2607 | } |
| 2608 | |
| 2609 | /* This function replace the HTTP status code and the associated message. The |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2610 | * variable <status> contains the new status code. This function never fails. It |
| 2611 | * returns 0 in case of success, -1 in case of internal error. |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2612 | */ |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2613 | 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] | 2614 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2615 | struct htx *htx = htxbuf(&s->res.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2616 | char *res; |
| 2617 | |
| 2618 | chunk_reset(&trash); |
| 2619 | res = ultoa_o(status, trash.area, trash.size); |
| 2620 | trash.data = res - trash.area; |
| 2621 | |
| 2622 | /* Do we have a custom reason format string? */ |
Tim Duesterhus | e296d3e | 2020-03-05 17:56:31 +0100 | [diff] [blame] | 2623 | if (!isttest(reason)) { |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2624 | const char *str = http_get_reason(status); |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 2625 | reason = ist(str); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2626 | } |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2627 | |
Christopher Faulet | bde2c4c | 2020-08-31 16:43:34 +0200 | [diff] [blame] | 2628 | if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason)) |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2629 | return -1; |
Willy Tarreau | 640e253 | 2023-01-10 14:50:44 +0100 | [diff] [blame] | 2630 | s->txn->status = status; |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2631 | return 0; |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2632 | } |
| 2633 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2634 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
| 2635 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 2636 | * further processing of the request (auth, deny, ...), and defaults to |
| 2637 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 2638 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 2639 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 2640 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 2641 | * status. |
| 2642 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2643 | static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, |
| 2644 | struct list *rules, struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2645 | { |
| 2646 | struct session *sess = strm_sess(s); |
| 2647 | struct http_txn *txn = s->txn; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2648 | struct act_rule *rule; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2649 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2650 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2651 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2652 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2653 | * resume condition. If a resume is needed it is always in the action |
| 2654 | * and never in the ACL or converters. In this case, we initialise the |
| 2655 | * current rule, and go to the action execution point. |
| 2656 | */ |
| 2657 | if (s->current_rule) { |
| 2658 | rule = s->current_rule; |
| 2659 | s->current_rule = NULL; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2660 | 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] | 2661 | goto resume_execution; |
| 2662 | } |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2663 | 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] | 2664 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2665 | restart: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2666 | /* start the ruleset evaluation in strict mode */ |
| 2667 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2668 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2669 | list_for_each_entry(rule, s->current_rule_list, list) { |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2670 | /* check optional condition */ |
| 2671 | if (rule->cond) { |
| 2672 | int ret; |
| 2673 | |
| 2674 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 2675 | ret = acl_pass(ret); |
| 2676 | |
| 2677 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2678 | ret = !ret; |
| 2679 | |
| 2680 | if (!ret) /* condition not matched */ |
| 2681 | continue; |
| 2682 | } |
| 2683 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2684 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2685 | resume_execution: |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 2686 | if (rule->kw->flags & KWF_EXPERIMENTAL) |
| 2687 | mark_tainted(TAINTED_ACTION_EXP_EXECUTED); |
| 2688 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2689 | /* Always call the action function if defined */ |
| 2690 | if (rule->action_ptr) { |
Christopher Faulet | d7bac88 | 2023-04-14 12:05:55 +0200 | [diff] [blame] | 2691 | if ((s->scf->flags & SC_FL_ERROR) || |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 2692 | ((s->scf->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) && |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2693 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2694 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2695 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2696 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2697 | case ACT_RET_CONT: |
| 2698 | break; |
| 2699 | case ACT_RET_STOP: |
| 2700 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2701 | s->last_rule_file = rule->conf.file; |
| 2702 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2703 | goto end; |
| 2704 | case ACT_RET_YIELD: |
| 2705 | s->current_rule = rule; |
| 2706 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2707 | goto end; |
| 2708 | case ACT_RET_ERR: |
| 2709 | rule_ret = HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2710 | s->last_rule_file = rule->conf.file; |
| 2711 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2712 | goto end; |
| 2713 | case ACT_RET_DONE: |
| 2714 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2715 | s->last_rule_file = rule->conf.file; |
| 2716 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2717 | goto end; |
| 2718 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 2719 | if (txn->status == -1) |
| 2720 | txn->status = 403; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2721 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2722 | s->last_rule_file = rule->conf.file; |
| 2723 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2724 | goto end; |
| 2725 | case ACT_RET_ABRT: |
| 2726 | rule_ret = HTTP_RULE_RES_ABRT; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2727 | s->last_rule_file = rule->conf.file; |
| 2728 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2729 | goto end; |
| 2730 | case ACT_RET_INV: |
| 2731 | rule_ret = HTTP_RULE_RES_BADREQ; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2732 | s->last_rule_file = rule->conf.file; |
| 2733 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2734 | goto end; |
| 2735 | } |
| 2736 | continue; /* eval the next rule */ |
| 2737 | } |
| 2738 | |
| 2739 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2740 | switch (rule->action) { |
| 2741 | case ACT_ACTION_ALLOW: |
| 2742 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2743 | s->last_rule_file = rule->conf.file; |
| 2744 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2745 | goto end; |
| 2746 | |
| 2747 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2748 | txn->status = rule->arg.http_reply->status; |
| 2749 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2750 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2751 | s->last_rule_file = rule->conf.file; |
| 2752 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2753 | goto end; |
| 2754 | |
| 2755 | case ACT_HTTP_REQ_TARPIT: |
| 2756 | txn->flags |= TX_CLTARPIT; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2757 | txn->status = rule->arg.http_reply->status; |
| 2758 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2759 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2760 | s->last_rule_file = rule->conf.file; |
| 2761 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2762 | goto end; |
| 2763 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2764 | case ACT_HTTP_REDIR: { |
| 2765 | int ret = http_apply_redirect_rule(rule->arg.redir, s, txn); |
| 2766 | |
| 2767 | if (ret == 2) // 2 == skip |
| 2768 | break; |
| 2769 | |
| 2770 | rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2771 | s->last_rule_file = rule->conf.file; |
| 2772 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2773 | goto end; |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2774 | } |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2775 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2776 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2777 | default: |
| 2778 | break; |
| 2779 | } |
| 2780 | } |
| 2781 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2782 | if (def_rules && s->current_rule_list == def_rules) { |
| 2783 | s->current_rule_list = rules; |
| 2784 | goto restart; |
| 2785 | } |
| 2786 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2787 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2788 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2789 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2790 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2791 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2792 | /* we reached the end of the rules, nothing to report */ |
| 2793 | return rule_ret; |
| 2794 | } |
| 2795 | |
| 2796 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 2797 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 2798 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 2799 | * is returned, the process can continue the evaluation of next rule list. If |
| 2800 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 2801 | * 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] | 2802 | * must be returned. If *YIELD is returned, the caller must call again the |
| 2803 | * function with the same context. |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2804 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2805 | static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules, |
| 2806 | struct list *rules, struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2807 | { |
| 2808 | struct session *sess = strm_sess(s); |
| 2809 | struct http_txn *txn = s->txn; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2810 | struct act_rule *rule; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2811 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2812 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2813 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2814 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2815 | * resume condition. If a resume is needed it is always in the action |
| 2816 | * and never in the ACL or converters. In this case, we initialise the |
| 2817 | * current rule, and go to the action execution point. |
| 2818 | */ |
| 2819 | if (s->current_rule) { |
| 2820 | rule = s->current_rule; |
| 2821 | s->current_rule = NULL; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2822 | 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] | 2823 | goto resume_execution; |
| 2824 | } |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2825 | s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules); |
| 2826 | |
| 2827 | restart: |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2828 | |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2829 | /* start the ruleset evaluation in strict mode */ |
| 2830 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2831 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2832 | list_for_each_entry(rule, s->current_rule_list, list) { |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2833 | /* check optional condition */ |
| 2834 | if (rule->cond) { |
| 2835 | int ret; |
| 2836 | |
| 2837 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 2838 | ret = acl_pass(ret); |
| 2839 | |
| 2840 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2841 | ret = !ret; |
| 2842 | |
| 2843 | if (!ret) /* condition not matched */ |
| 2844 | continue; |
| 2845 | } |
| 2846 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2847 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2848 | resume_execution: |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 2849 | if (rule->kw->flags & KWF_EXPERIMENTAL) |
| 2850 | mark_tainted(TAINTED_ACTION_EXP_EXECUTED); |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2851 | |
| 2852 | /* Always call the action function if defined */ |
| 2853 | if (rule->action_ptr) { |
Christopher Faulet | d7bac88 | 2023-04-14 12:05:55 +0200 | [diff] [blame] | 2854 | if ((s->scf->flags & SC_FL_ERROR) || |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 2855 | ((s->scf->flags & (SC_FL_EOS|SC_FL_ABRT_DONE)) && |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2856 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2857 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2858 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2859 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2860 | case ACT_RET_CONT: |
| 2861 | break; |
| 2862 | case ACT_RET_STOP: |
| 2863 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2864 | s->last_rule_file = rule->conf.file; |
| 2865 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2866 | goto end; |
| 2867 | case ACT_RET_YIELD: |
| 2868 | s->current_rule = rule; |
| 2869 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2870 | goto end; |
| 2871 | case ACT_RET_ERR: |
| 2872 | rule_ret = HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2873 | s->last_rule_file = rule->conf.file; |
| 2874 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2875 | goto end; |
| 2876 | case ACT_RET_DONE: |
| 2877 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2878 | s->last_rule_file = rule->conf.file; |
| 2879 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2880 | goto end; |
| 2881 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 2882 | if (txn->status == -1) |
| 2883 | txn->status = 502; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2884 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2885 | s->last_rule_file = rule->conf.file; |
| 2886 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2887 | goto end; |
| 2888 | case ACT_RET_ABRT: |
| 2889 | rule_ret = HTTP_RULE_RES_ABRT; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2890 | s->last_rule_file = rule->conf.file; |
| 2891 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2892 | goto end; |
| 2893 | case ACT_RET_INV: |
| 2894 | rule_ret = HTTP_RULE_RES_BADREQ; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2895 | s->last_rule_file = rule->conf.file; |
| 2896 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2897 | goto end; |
| 2898 | } |
| 2899 | continue; /* eval the next rule */ |
| 2900 | } |
| 2901 | |
| 2902 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2903 | switch (rule->action) { |
| 2904 | case ACT_ACTION_ALLOW: |
| 2905 | rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2906 | s->last_rule_file = rule->conf.file; |
| 2907 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2908 | goto end; |
| 2909 | |
| 2910 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2911 | txn->status = rule->arg.http_reply->status; |
| 2912 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 2913 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2914 | s->last_rule_file = rule->conf.file; |
| 2915 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2916 | goto end; |
| 2917 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2918 | case ACT_HTTP_REDIR: { |
| 2919 | int ret = http_apply_redirect_rule(rule->arg.redir, s, txn); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2920 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2921 | if (ret == 2) // 2 == skip |
| 2922 | break; |
| 2923 | |
| 2924 | rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2925 | s->last_rule_file = rule->conf.file; |
| 2926 | s->last_rule_line = rule->conf.line; |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2927 | goto end; |
| 2928 | } |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2929 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2930 | default: |
| 2931 | break; |
| 2932 | } |
| 2933 | } |
| 2934 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2935 | if (def_rules && s->current_rule_list == def_rules) { |
| 2936 | s->current_rule_list = rules; |
| 2937 | goto restart; |
| 2938 | } |
| 2939 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2940 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2941 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2942 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2943 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2944 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2945 | /* we reached the end of the rules, nothing to report */ |
| 2946 | return rule_ret; |
| 2947 | } |
| 2948 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2949 | /* Executes backend and frontend http-after-response rules for the stream <s>, |
| 2950 | * in that order. it return 1 on success and 0 on error. It is the caller |
| 2951 | * responsibility to catch error or ignore it. If it catches it, this function |
| 2952 | * may be called a second time, for the internal error. |
| 2953 | */ |
| 2954 | int http_eval_after_res_rules(struct stream *s) |
| 2955 | { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2956 | struct list *def_rules, *rules; |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2957 | struct session *sess = s->sess; |
| 2958 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 2959 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 2960 | /* Eval after-response ruleset only if the reply is not const */ |
| 2961 | if (s->txn->flags & TX_CONST_REPLY) |
| 2962 | goto end; |
| 2963 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2964 | /* prune the request variables if not already done and swap to the response variables. */ |
| 2965 | if (s->vars_reqres.scope != SCOPE_RES) { |
| 2966 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 2967 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | b7bfcb3 | 2021-08-31 08:13:25 +0200 | [diff] [blame] | 2968 | vars_init_head(&s->vars_reqres, SCOPE_RES); |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2969 | } |
| 2970 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2971 | def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL); |
| 2972 | rules = &s->be->http_after_res_rules; |
| 2973 | |
| 2974 | ret = http_res_get_intercept_rule(s->be, def_rules, rules, s); |
Christopher Faulet | 4c5a591 | 2021-11-09 17:48:39 +0100 | [diff] [blame] | 2975 | 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] | 2976 | def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL); |
| 2977 | rules = &sess->fe->http_after_res_rules; |
| 2978 | ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s); |
| 2979 | } |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2980 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 2981 | end: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2982 | /* All other codes than CONTINUE, STOP or DONE are forbidden */ |
| 2983 | return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE); |
| 2984 | } |
| 2985 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 2986 | /* |
| 2987 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 2988 | * desirable to call it only when needed. This code is quite complex because |
| 2989 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 2990 | * highly recommended not to touch this part without a good reason ! |
| 2991 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2992 | 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] | 2993 | { |
| 2994 | struct session *sess = s->sess; |
| 2995 | struct http_txn *txn = s->txn; |
| 2996 | struct htx *htx; |
| 2997 | struct http_hdr_ctx ctx; |
| 2998 | char *hdr_beg, *hdr_end, *del_from; |
| 2999 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
| 3000 | int preserve_hdr; |
| 3001 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3002 | htx = htxbuf(&req->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3003 | ctx.blk = NULL; |
| 3004 | while (http_find_header(htx, ist("Cookie"), &ctx, 1)) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3005 | int is_first = 1; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3006 | del_from = NULL; /* nothing to be deleted */ |
| 3007 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 3008 | |
| 3009 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3010 | * attributes whose name begin with a '$', and associate them with |
| 3011 | * the right cookie, if we want to delete this cookie. |
| 3012 | * So there are 3 cases for each cookie read : |
| 3013 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3014 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3015 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3016 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3017 | * "special" cookie. |
| 3018 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3019 | * remove it. If no application cookie persists in the header, we |
| 3020 | * *MUST* delete it. |
| 3021 | * |
| 3022 | * Note: RFC2965 is unclear about the processing of spaces around |
| 3023 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 3024 | * the RFC explicitly allows spaces before it, and not within the |
| 3025 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 3026 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 3027 | * after the equal sign too, resulting in some (rare) buggy |
| 3028 | * implementations trying to do that. So let's do what servers do. |
| 3029 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 3030 | * allowed quoted strings in values, with any possible character |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3031 | * after a backslash, including control chars and delimiters, which |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3032 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 3033 | * within values even without quotes. |
| 3034 | * |
| 3035 | * We have to keep multiple pointers in order to support cookie |
| 3036 | * removal at the beginning, middle or end of header without |
| 3037 | * corrupting the header. All of these headers are valid : |
| 3038 | * |
| 3039 | * hdr_beg hdr_end |
| 3040 | * | | |
| 3041 | * v | |
| 3042 | * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 | |
| 3043 | * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v |
| 3044 | * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3 |
| 3045 | * | | | | | | | |
| 3046 | * | | | | | | | |
| 3047 | * | | | | | | +--> next |
| 3048 | * | | | | | +----> val_end |
| 3049 | * | | | | +-----------> val_beg |
| 3050 | * | | | +--------------> equal |
| 3051 | * | | +----------------> att_end |
| 3052 | * | +---------------------> att_beg |
| 3053 | * +--------------------------> prev |
| 3054 | * |
| 3055 | */ |
| 3056 | hdr_beg = ctx.value.ptr; |
| 3057 | hdr_end = hdr_beg + ctx.value.len; |
| 3058 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3059 | /* Iterate through all cookies on this line */ |
| 3060 | |
| 3061 | /* find att_beg */ |
| 3062 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3063 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3064 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3065 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3066 | |
| 3067 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3068 | att_beg++; |
| 3069 | |
| 3070 | /* find att_end : this is the first character after the last non |
| 3071 | * space before the equal. It may be equal to hdr_end. |
| 3072 | */ |
| 3073 | equal = att_end = att_beg; |
| 3074 | while (equal < hdr_end) { |
| 3075 | if (*equal == '=' || *equal == ',' || *equal == ';') |
| 3076 | break; |
| 3077 | if (HTTP_IS_SPHT(*equal++)) |
| 3078 | continue; |
| 3079 | att_end = equal; |
| 3080 | } |
| 3081 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3082 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3083 | * is between <att_beg> and <equal>, both may be identical. |
| 3084 | */ |
| 3085 | /* look for end of cookie if there is an equal sign */ |
| 3086 | if (equal < hdr_end && *equal == '=') { |
| 3087 | /* look for the beginning of the value */ |
| 3088 | val_beg = equal + 1; |
| 3089 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3090 | val_beg++; |
| 3091 | |
| 3092 | /* find the end of the value, respecting quotes */ |
| 3093 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3094 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3095 | /* 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] | 3096 | val_end = next; |
| 3097 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3098 | val_end--; |
| 3099 | } |
| 3100 | else |
| 3101 | val_beg = val_end = next = equal; |
| 3102 | |
| 3103 | /* We have nothing to do with attributes beginning with |
| 3104 | * '$'. However, they will automatically be removed if a |
| 3105 | * header before them is removed, since they're supposed |
| 3106 | * to be linked together. |
| 3107 | */ |
| 3108 | if (*att_beg == '$') |
| 3109 | continue; |
| 3110 | |
| 3111 | /* Ignore cookies with no equal sign */ |
| 3112 | if (equal == next) { |
| 3113 | /* This is not our cookie, so we must preserve it. But if we already |
| 3114 | * scheduled another cookie for removal, we cannot remove the |
| 3115 | * complete header, but we can remove the previous block itself. |
| 3116 | */ |
| 3117 | preserve_hdr = 1; |
| 3118 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3119 | 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] | 3120 | val_end += delta; |
| 3121 | next += delta; |
| 3122 | hdr_end += delta; |
| 3123 | prev = del_from; |
| 3124 | del_from = NULL; |
| 3125 | } |
| 3126 | continue; |
| 3127 | } |
| 3128 | |
| 3129 | /* if there are spaces around the equal sign, we need to |
| 3130 | * strip them otherwise we'll get trouble for cookie captures, |
| 3131 | * or even for rewrites. Since this happens extremely rarely, |
| 3132 | * it does not hurt performance. |
| 3133 | */ |
| 3134 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3135 | int stripped_before = 0; |
| 3136 | int stripped_after = 0; |
| 3137 | |
| 3138 | if (att_end != equal) { |
| 3139 | memmove(att_end, equal, hdr_end - equal); |
| 3140 | stripped_before = (att_end - equal); |
| 3141 | equal += stripped_before; |
| 3142 | val_beg += stripped_before; |
| 3143 | } |
| 3144 | |
| 3145 | if (val_beg > equal + 1) { |
| 3146 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3147 | stripped_after = (equal + 1) - val_beg; |
| 3148 | val_beg += stripped_after; |
| 3149 | stripped_before += stripped_after; |
| 3150 | } |
| 3151 | |
| 3152 | val_end += stripped_before; |
| 3153 | next += stripped_before; |
| 3154 | hdr_end += stripped_before; |
| 3155 | } |
| 3156 | /* now everything is as on the diagram above */ |
| 3157 | |
| 3158 | /* First, let's see if we want to capture this cookie. We check |
| 3159 | * that we don't already have a client side cookie, because we |
| 3160 | * can only capture one. Also as an optimisation, we ignore |
| 3161 | * cookies shorter than the declared name. |
| 3162 | */ |
| 3163 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 3164 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3165 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3166 | int log_len = val_end - att_beg; |
| 3167 | |
| 3168 | if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3169 | ha_alert("HTTP logging : out of memory.\n"); |
| 3170 | } else { |
| 3171 | if (log_len > sess->fe->capture_len) |
| 3172 | log_len = sess->fe->capture_len; |
| 3173 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 3174 | txn->cli_cookie[log_len] = 0; |
| 3175 | } |
| 3176 | } |
| 3177 | |
| 3178 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 3179 | * following form : |
| 3180 | * |
| 3181 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
| 3182 | * |
| 3183 | * For cookies in prefix mode, the form is : |
| 3184 | * |
| 3185 | * Cookie: NAME=SRV~VALUE |
| 3186 | */ |
| 3187 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3188 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3189 | struct server *srv = s->be->srv; |
| 3190 | char *delim; |
| 3191 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3192 | /* 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] | 3193 | * have the server ID between val_beg and delim, and the original cookie between |
| 3194 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 3195 | * |
| 3196 | * hdr_beg |
| 3197 | * | |
| 3198 | * v |
| 3199 | * NAME=SRV; # in all but prefix modes |
| 3200 | * NAME=SRV~OPAQUE ; # in prefix mode |
| 3201 | * || || | |+-> next |
| 3202 | * || || | +--> val_end |
| 3203 | * || || +---------> delim |
| 3204 | * || |+------------> val_beg |
| 3205 | * || +-------------> att_end = equal |
| 3206 | * |+-----------------> att_beg |
| 3207 | * +------------------> prev |
| 3208 | * |
| 3209 | */ |
| 3210 | if (s->be->ck_opts & PR_CK_PFX) { |
| 3211 | for (delim = val_beg; delim < val_end; delim++) |
| 3212 | if (*delim == COOKIE_DELIM) |
| 3213 | break; |
| 3214 | } |
| 3215 | else { |
| 3216 | char *vbar1; |
| 3217 | delim = val_end; |
| 3218 | /* Now check if the cookie contains a date field, which would |
| 3219 | * appear after a vertical bar ('|') just after the server name |
| 3220 | * and before the delimiter. |
| 3221 | */ |
| 3222 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 3223 | if (vbar1) { |
| 3224 | /* OK, so left of the bar is the server's cookie and |
| 3225 | * right is the last seen date. It is a base64 encoded |
| 3226 | * 30-bit value representing the UNIX date since the |
| 3227 | * epoch in 4-second quantities. |
| 3228 | */ |
| 3229 | int val; |
| 3230 | delim = vbar1++; |
| 3231 | if (val_end - vbar1 >= 5) { |
| 3232 | val = b64tos30(vbar1); |
| 3233 | if (val > 0) |
| 3234 | txn->cookie_last_date = val << 2; |
| 3235 | } |
| 3236 | /* look for a second vertical bar */ |
| 3237 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 3238 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 3239 | val = b64tos30(vbar1 + 1); |
| 3240 | if (val > 0) |
| 3241 | txn->cookie_first_date = val << 2; |
| 3242 | } |
| 3243 | } |
| 3244 | } |
| 3245 | |
| 3246 | /* if the cookie has an expiration date and the proxy wants to check |
| 3247 | * it, then we do that now. We first check if the cookie is too old, |
| 3248 | * then only if it has expired. We detect strict overflow because the |
| 3249 | * time resolution here is not great (4 seconds). Cookies with dates |
| 3250 | * in the future are ignored if their offset is beyond one day. This |
| 3251 | * allows an admin to fix timezone issues without expiring everyone |
| 3252 | * and at the same time avoids keeping unwanted side effects for too |
| 3253 | * long. |
| 3254 | */ |
| 3255 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 3256 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) || |
| 3257 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
| 3258 | txn->flags &= ~TX_CK_MASK; |
| 3259 | txn->flags |= TX_CK_OLD; |
| 3260 | delim = val_beg; // let's pretend we have not found the cookie |
| 3261 | txn->cookie_first_date = 0; |
| 3262 | txn->cookie_last_date = 0; |
| 3263 | } |
| 3264 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 3265 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) || |
| 3266 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
| 3267 | txn->flags &= ~TX_CK_MASK; |
| 3268 | txn->flags |= TX_CK_EXPIRED; |
| 3269 | delim = val_beg; // let's pretend we have not found the cookie |
| 3270 | txn->cookie_first_date = 0; |
| 3271 | txn->cookie_last_date = 0; |
| 3272 | } |
| 3273 | |
| 3274 | /* Here, we'll look for the first running server which supports the cookie. |
| 3275 | * This allows to share a same cookie between several servers, for example |
| 3276 | * to dedicate backup servers to specific servers only. |
| 3277 | * However, to prevent clients from sticking to cookie-less backup server |
| 3278 | * when they have incidentely learned an empty cookie, we simply ignore |
| 3279 | * empty cookies and mark them as invalid. |
| 3280 | * The same behaviour is applied when persistence must be ignored. |
| 3281 | */ |
| 3282 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3283 | srv = NULL; |
| 3284 | |
| 3285 | while (srv) { |
| 3286 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 3287 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 3288 | if ((srv->cur_state != SRV_ST_STOPPED) || |
| 3289 | (s->be->options & PR_O_PERSIST) || |
| 3290 | (s->flags & SF_FORCE_PRST)) { |
| 3291 | /* we found the server and we can use it */ |
| 3292 | txn->flags &= ~TX_CK_MASK; |
| 3293 | txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN; |
| 3294 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
| 3295 | s->target = &srv->obj_type; |
| 3296 | break; |
| 3297 | } else { |
| 3298 | /* we found a server, but it's down, |
| 3299 | * mark it as such and go on in case |
| 3300 | * another one is available. |
| 3301 | */ |
| 3302 | txn->flags &= ~TX_CK_MASK; |
| 3303 | txn->flags |= TX_CK_DOWN; |
| 3304 | } |
| 3305 | } |
| 3306 | srv = srv->next; |
| 3307 | } |
| 3308 | |
| 3309 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
| 3310 | /* no server matched this cookie or we deliberately skipped it */ |
| 3311 | txn->flags &= ~TX_CK_MASK; |
| 3312 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3313 | txn->flags |= TX_CK_UNUSED; |
| 3314 | else |
| 3315 | txn->flags |= TX_CK_INVALID; |
| 3316 | } |
| 3317 | |
| 3318 | /* depending on the cookie mode, we may have to either : |
| 3319 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3320 | * the server never sees it ; |
| 3321 | * - 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] | 3322 | * application cookie so that it does not get accidentally removed later, |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3323 | * if we're in cookie prefix mode |
| 3324 | */ |
| 3325 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
| 3326 | int delta; /* negative */ |
| 3327 | |
| 3328 | memmove(val_beg, delim + 1, hdr_end - (delim + 1)); |
| 3329 | delta = val_beg - (delim + 1); |
| 3330 | val_end += delta; |
| 3331 | next += delta; |
| 3332 | hdr_end += delta; |
| 3333 | del_from = NULL; |
| 3334 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 3335 | } |
| 3336 | else if (del_from == NULL && |
| 3337 | (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) { |
| 3338 | del_from = prev; |
| 3339 | } |
| 3340 | } |
| 3341 | else { |
| 3342 | /* This is not our cookie, so we must preserve it. But if we already |
| 3343 | * scheduled another cookie for removal, we cannot remove the |
| 3344 | * complete header, but we can remove the previous block itself. |
| 3345 | */ |
| 3346 | preserve_hdr = 1; |
| 3347 | |
| 3348 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3349 | 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] | 3350 | if (att_beg >= del_from) |
| 3351 | att_beg += delta; |
| 3352 | if (att_end >= del_from) |
| 3353 | att_end += delta; |
| 3354 | val_beg += delta; |
| 3355 | val_end += delta; |
| 3356 | next += delta; |
| 3357 | hdr_end += delta; |
| 3358 | prev = del_from; |
| 3359 | del_from = NULL; |
| 3360 | } |
| 3361 | } |
| 3362 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3363 | } /* for each cookie */ |
| 3364 | |
| 3365 | |
| 3366 | /* There are no more cookies on this line. |
| 3367 | * We may still have one (or several) marked for deletion at the |
| 3368 | * end of the line. We must do this now in two ways : |
| 3369 | * - if some cookies must be preserved, we only delete from the |
| 3370 | * mark to the end of line ; |
| 3371 | * - if nothing needs to be preserved, simply delete the whole header |
| 3372 | */ |
| 3373 | if (del_from) { |
| 3374 | hdr_end = (preserve_hdr ? del_from : hdr_beg); |
| 3375 | } |
| 3376 | if ((hdr_end - hdr_beg) != ctx.value.len) { |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3377 | if (hdr_beg != hdr_end) |
| 3378 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3379 | else |
| 3380 | http_remove_header(htx, &ctx); |
| 3381 | } |
| 3382 | } /* for each "Cookie header */ |
| 3383 | } |
| 3384 | |
| 3385 | /* |
| 3386 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 3387 | * desirable to call it only when needed. This function is also used when we |
| 3388 | * just need to know if there is a cookie (eg: for check-cache). |
| 3389 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3390 | 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] | 3391 | { |
| 3392 | struct session *sess = s->sess; |
| 3393 | struct http_txn *txn = s->txn; |
| 3394 | struct htx *htx; |
| 3395 | struct http_hdr_ctx ctx; |
| 3396 | struct server *srv; |
| 3397 | char *hdr_beg, *hdr_end; |
| 3398 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3399 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3400 | htx = htxbuf(&res->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3401 | |
| 3402 | ctx.blk = NULL; |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3403 | while (http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3404 | int is_first = 1; |
| 3405 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3406 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 3407 | * <prev> points to the colon. |
| 3408 | */ |
| 3409 | txn->flags |= TX_SCK_PRESENT; |
| 3410 | |
| 3411 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 3412 | * check-cache is enabled) and we are not interested in checking |
| 3413 | * them. Warning, the cookie capture is declared in the frontend. |
| 3414 | */ |
| 3415 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
| 3416 | break; |
| 3417 | |
| 3418 | /* OK so now we know we have to process this response cookie. |
| 3419 | * The format of the Set-Cookie header is slightly different |
| 3420 | * from the format of the Cookie header in that it does not |
| 3421 | * support the comma as a cookie delimiter (thus the header |
| 3422 | * cannot be folded) because the Expires attribute described in |
| 3423 | * the original Netscape's spec may contain an unquoted date |
| 3424 | * with a comma inside. We have to live with this because |
| 3425 | * many browsers don't support Max-Age and some browsers don't |
| 3426 | * support quoted strings. However the Set-Cookie2 header is |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3427 | * clean but basically nobody supports it. |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3428 | * |
| 3429 | * We have to keep multiple pointers in order to support cookie |
| 3430 | * removal at the beginning, middle or end of header without |
| 3431 | * corrupting the header (in case of set-cookie2). A special |
| 3432 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 3433 | * fields after the first semi-colon. The <next> pointer points |
| 3434 | * either to the end of line (set-cookie) or next unquoted comma |
| 3435 | * (set-cookie2). All of these headers are valid : |
| 3436 | * |
| 3437 | * hdr_beg hdr_end |
| 3438 | * | | |
| 3439 | * v | |
| 3440 | * NAME1 = VALUE 1 ; Secure; Path="/" | |
| 3441 | * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v |
| 3442 | * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT |
| 3443 | * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard |
| 3444 | * | | | | | | | | |
| 3445 | * | | | | | | | +-> next |
| 3446 | * | | | | | | +------------> scav |
| 3447 | * | | | | | +--------------> val_end |
| 3448 | * | | | | +--------------------> val_beg |
| 3449 | * | | | +----------------------> equal |
| 3450 | * | | +------------------------> att_end |
| 3451 | * | +----------------------------> att_beg |
| 3452 | * +------------------------------> prev |
| 3453 | * -------------------------------> hdr_beg |
| 3454 | */ |
| 3455 | hdr_beg = ctx.value.ptr; |
| 3456 | hdr_end = hdr_beg + ctx.value.len; |
| 3457 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3458 | |
| 3459 | /* Iterate through all cookies on this line */ |
| 3460 | |
| 3461 | /* find att_beg */ |
| 3462 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3463 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3464 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3465 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3466 | |
| 3467 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3468 | att_beg++; |
| 3469 | |
| 3470 | /* find att_end : this is the first character after the last non |
| 3471 | * space before the equal. It may be equal to hdr_end. |
| 3472 | */ |
| 3473 | equal = att_end = att_beg; |
| 3474 | |
| 3475 | while (equal < hdr_end) { |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3476 | if (*equal == '=' || *equal == ';') |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3477 | break; |
| 3478 | if (HTTP_IS_SPHT(*equal++)) |
| 3479 | continue; |
| 3480 | att_end = equal; |
| 3481 | } |
| 3482 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3483 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3484 | * is between <att_beg> and <equal>, both may be identical. |
| 3485 | */ |
| 3486 | |
| 3487 | /* look for end of cookie if there is an equal sign */ |
| 3488 | if (equal < hdr_end && *equal == '=') { |
| 3489 | /* look for the beginning of the value */ |
| 3490 | val_beg = equal + 1; |
| 3491 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3492 | val_beg++; |
| 3493 | |
| 3494 | /* find the end of the value, respecting quotes */ |
| 3495 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3496 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3497 | /* 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] | 3498 | val_end = next; |
| 3499 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3500 | val_end--; |
| 3501 | } |
| 3502 | else { |
| 3503 | /* <equal> points to next comma, semi-colon or EOL */ |
| 3504 | val_beg = val_end = next = equal; |
| 3505 | } |
| 3506 | |
| 3507 | if (next < hdr_end) { |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3508 | /* For Set-Cookie, since commas are permitted |
| 3509 | * in values, skip to the end. |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3510 | */ |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3511 | next = hdr_end; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3512 | } |
| 3513 | |
| 3514 | /* Now everything is as on the diagram above */ |
| 3515 | |
| 3516 | /* Ignore cookies with no equal sign */ |
| 3517 | if (equal == val_end) |
| 3518 | continue; |
| 3519 | |
| 3520 | /* If there are spaces around the equal sign, we need to |
| 3521 | * strip them otherwise we'll get trouble for cookie captures, |
| 3522 | * or even for rewrites. Since this happens extremely rarely, |
| 3523 | * it does not hurt performance. |
| 3524 | */ |
| 3525 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3526 | int stripped_before = 0; |
| 3527 | int stripped_after = 0; |
| 3528 | |
| 3529 | if (att_end != equal) { |
| 3530 | memmove(att_end, equal, hdr_end - equal); |
| 3531 | stripped_before = (att_end - equal); |
| 3532 | equal += stripped_before; |
| 3533 | val_beg += stripped_before; |
| 3534 | } |
| 3535 | |
| 3536 | if (val_beg > equal + 1) { |
| 3537 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3538 | stripped_after = (equal + 1) - val_beg; |
| 3539 | val_beg += stripped_after; |
| 3540 | stripped_before += stripped_after; |
| 3541 | } |
| 3542 | |
| 3543 | val_end += stripped_before; |
| 3544 | next += stripped_before; |
| 3545 | hdr_end += stripped_before; |
| 3546 | |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3547 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3548 | ctx.value.len = hdr_end - hdr_beg; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | /* First, let's see if we want to capture this cookie. We check |
| 3552 | * that we don't already have a server side cookie, because we |
| 3553 | * can only capture one. Also as an optimisation, we ignore |
| 3554 | * cookies shorter than the declared name. |
| 3555 | */ |
| 3556 | if (sess->fe->capture_name != NULL && |
| 3557 | txn->srv_cookie == NULL && |
| 3558 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3559 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3560 | int log_len = val_end - att_beg; |
| 3561 | if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3562 | ha_alert("HTTP logging : out of memory.\n"); |
| 3563 | } |
| 3564 | else { |
| 3565 | if (log_len > sess->fe->capture_len) |
| 3566 | log_len = sess->fe->capture_len; |
| 3567 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 3568 | txn->srv_cookie[log_len] = 0; |
| 3569 | } |
| 3570 | } |
| 3571 | |
| 3572 | srv = objt_server(s->target); |
| 3573 | /* now check if we need to process it for persistence */ |
| 3574 | if (!(s->flags & SF_IGNORE_PRST) && |
| 3575 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3576 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3577 | /* assume passive cookie by default */ |
| 3578 | txn->flags &= ~TX_SCK_MASK; |
| 3579 | txn->flags |= TX_SCK_FOUND; |
| 3580 | |
| 3581 | /* If the cookie is in insert mode on a known server, we'll delete |
| 3582 | * this occurrence because we'll insert another one later. |
| 3583 | * We'll delete it too if the "indirect" option is set and we're in |
| 3584 | * a direct access. |
| 3585 | */ |
| 3586 | if (s->be->ck_opts & PR_CK_PSV) { |
| 3587 | /* The "preserve" flag was set, we don't want to touch the |
| 3588 | * server's cookie. |
| 3589 | */ |
| 3590 | } |
| 3591 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
| 3592 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
| 3593 | /* this cookie must be deleted */ |
| 3594 | if (prev == hdr_beg && next == hdr_end) { |
| 3595 | /* whole header */ |
| 3596 | http_remove_header(htx, &ctx); |
| 3597 | /* note: while both invalid now, <next> and <hdr_end> |
| 3598 | * are still equal, so the for() will stop as expected. |
| 3599 | */ |
| 3600 | } else { |
| 3601 | /* just remove the value */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3602 | int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3603 | next = prev; |
| 3604 | hdr_end += delta; |
| 3605 | } |
| 3606 | txn->flags &= ~TX_SCK_MASK; |
| 3607 | txn->flags |= TX_SCK_DELETED; |
| 3608 | /* and go on with next cookie */ |
| 3609 | } |
| 3610 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
| 3611 | /* replace bytes val_beg->val_end with the cookie name associated |
| 3612 | * with this server since we know it. |
| 3613 | */ |
| 3614 | int sliding, delta; |
| 3615 | |
| 3616 | ctx.value = ist2(val_beg, val_end - val_beg); |
| 3617 | ctx.lws_before = ctx.lws_after = 0; |
| 3618 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen)); |
| 3619 | delta = srv->cklen - (val_end - val_beg); |
| 3620 | sliding = (ctx.value.ptr - val_beg); |
| 3621 | hdr_beg += sliding; |
| 3622 | val_beg += sliding; |
| 3623 | next += sliding + delta; |
| 3624 | hdr_end += sliding + delta; |
| 3625 | |
| 3626 | txn->flags &= ~TX_SCK_MASK; |
| 3627 | txn->flags |= TX_SCK_REPLACED; |
| 3628 | } |
| 3629 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
| 3630 | /* insert the cookie name associated with this server |
| 3631 | * before existing cookie, and insert a delimiter between them.. |
| 3632 | */ |
| 3633 | int sliding, delta; |
| 3634 | ctx.value = ist2(val_beg, 0); |
| 3635 | ctx.lws_before = ctx.lws_after = 0; |
| 3636 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1)); |
| 3637 | delta = srv->cklen + 1; |
| 3638 | sliding = (ctx.value.ptr - val_beg); |
| 3639 | hdr_beg += sliding; |
| 3640 | val_beg += sliding; |
| 3641 | next += sliding + delta; |
| 3642 | hdr_end += sliding + delta; |
| 3643 | |
| 3644 | val_beg[srv->cklen] = COOKIE_DELIM; |
| 3645 | txn->flags &= ~TX_SCK_MASK; |
| 3646 | txn->flags |= TX_SCK_REPLACED; |
| 3647 | } |
| 3648 | } |
| 3649 | /* 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] | 3650 | * line when next != hdr_end (which should normally not happen |
| 3651 | * with set-cookie2 support removed). |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3652 | */ |
| 3653 | } |
| 3654 | } |
| 3655 | } |
| 3656 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3657 | /* |
| 3658 | * Parses the Cache-Control and Pragma request header fields to determine if |
| 3659 | * the request may be served from the cache and/or if it is cacheable. Updates |
| 3660 | * s->txn->flags. |
| 3661 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3662 | void http_check_request_for_cacheability(struct stream *s, struct channel *req) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3663 | { |
| 3664 | struct http_txn *txn = s->txn; |
| 3665 | struct htx *htx; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3666 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 3667 | int pragma_found, cc_found; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3668 | |
| 3669 | if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE) |
| 3670 | return; /* nothing more to do here */ |
| 3671 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3672 | htx = htxbuf(&req->buf); |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3673 | pragma_found = cc_found = 0; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3674 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3675 | /* Check "pragma" header for HTTP/1.0 compatibility. */ |
| 3676 | if (http_find_header(htx, ist("pragma"), &ctx, 1)) { |
| 3677 | if (isteqi(ctx.value, ist("no-cache"))) { |
| 3678 | pragma_found = 1; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3679 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3680 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3681 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3682 | ctx.blk = NULL; |
| 3683 | /* Don't use the cache and don't try to store if we found the |
| 3684 | * Authorization header */ |
| 3685 | if (http_find_header(htx, ist("authorization"), &ctx, 1)) { |
| 3686 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3687 | txn->flags |= TX_CACHE_IGNORE; |
| 3688 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3689 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3690 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3691 | /* Look for "cache-control" header and iterate over all the values |
| 3692 | * until we find one that specifies that caching is possible or not. */ |
| 3693 | ctx.blk = NULL; |
| 3694 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3695 | cc_found = 1; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3696 | /* We don't check the values after max-age, max-stale nor min-fresh, |
| 3697 | * we simply don't use the cache when they're specified. */ |
| 3698 | if (istmatchi(ctx.value, ist("max-age")) || |
| 3699 | istmatchi(ctx.value, ist("no-cache")) || |
| 3700 | istmatchi(ctx.value, ist("max-stale")) || |
| 3701 | istmatchi(ctx.value, ist("min-fresh"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3702 | txn->flags |= TX_CACHE_IGNORE; |
| 3703 | continue; |
| 3704 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3705 | if (istmatchi(ctx.value, ist("no-store"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3706 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3707 | continue; |
| 3708 | } |
| 3709 | } |
| 3710 | |
| 3711 | /* RFC7234#5.4: |
| 3712 | * When the Cache-Control header field is also present and |
| 3713 | * understood in a request, Pragma is ignored. |
| 3714 | * When the Cache-Control header field is not present in a |
| 3715 | * request, caches MUST consider the no-cache request |
| 3716 | * pragma-directive as having the same effect as if |
| 3717 | * "Cache-Control: no-cache" were present. |
| 3718 | */ |
| 3719 | if (!cc_found && pragma_found) |
| 3720 | txn->flags |= TX_CACHE_IGNORE; |
| 3721 | } |
| 3722 | |
| 3723 | /* |
| 3724 | * Check if response is cacheable or not. Updates s->txn->flags. |
| 3725 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3726 | void http_check_response_for_cacheability(struct stream *s, struct channel *res) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3727 | { |
| 3728 | struct http_txn *txn = s->txn; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3729 | struct http_hdr_ctx ctx = { .blk = NULL }; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3730 | struct htx *htx; |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3731 | int has_freshness_info = 0; |
| 3732 | int has_validator = 0; |
Remi Tricot-Le Breton | 02f4724 | 2023-07-04 17:13:28 +0200 | [diff] [blame] | 3733 | int has_null_maxage = 0; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3734 | |
| 3735 | if (txn->status < 200) { |
| 3736 | /* do not try to cache interim responses! */ |
| 3737 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3738 | return; |
| 3739 | } |
| 3740 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3741 | htx = htxbuf(&res->buf); |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3742 | /* Check "pragma" header for HTTP/1.0 compatibility. */ |
| 3743 | if (http_find_header(htx, ist("pragma"), &ctx, 1)) { |
| 3744 | if (isteqi(ctx.value, ist("no-cache"))) { |
| 3745 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3746 | return; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3747 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3748 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3749 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3750 | /* Look for "cache-control" header and iterate over all the values |
| 3751 | * until we find one that specifies that caching is possible or not. */ |
| 3752 | ctx.blk = NULL; |
| 3753 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
| 3754 | if (isteqi(ctx.value, ist("public"))) { |
| 3755 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3756 | continue; |
| 3757 | } |
Remi Tricot-Le Breton | 02f4724 | 2023-07-04 17:13:28 +0200 | [diff] [blame] | 3758 | /* This max-age might be overridden by a s-maxage directive, do |
| 3759 | * not unset the TX_CACHEABLE yet. */ |
| 3760 | if (isteqi(ctx.value, ist("max-age=0"))) { |
| 3761 | has_null_maxage = 1; |
| 3762 | continue; |
| 3763 | } |
| 3764 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3765 | if (isteqi(ctx.value, ist("private")) || |
| 3766 | isteqi(ctx.value, ist("no-cache")) || |
| 3767 | isteqi(ctx.value, ist("no-store")) || |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3768 | isteqi(ctx.value, ist("s-maxage=0"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3769 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3770 | continue; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3771 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3772 | /* We might have a no-cache="set-cookie" form. */ |
| 3773 | if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) { |
| 3774 | txn->flags &= ~TX_CACHE_COOK; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3775 | continue; |
| 3776 | } |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3777 | |
Remi Tricot-Le Breton | 02f4724 | 2023-07-04 17:13:28 +0200 | [diff] [blame] | 3778 | if (istmatchi(ctx.value, ist("s-maxage"))) { |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3779 | has_freshness_info = 1; |
Remi Tricot-Le Breton | 02f4724 | 2023-07-04 17:13:28 +0200 | [diff] [blame] | 3780 | has_null_maxage = 0; /* The null max-age is overridden, ignore it */ |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3781 | continue; |
| 3782 | } |
Remi Tricot-Le Breton | 02f4724 | 2023-07-04 17:13:28 +0200 | [diff] [blame] | 3783 | if (istmatchi(ctx.value, ist("max-age"))) { |
| 3784 | has_freshness_info = 1; |
| 3785 | continue; |
| 3786 | } |
| 3787 | } |
| 3788 | |
| 3789 | /* We had a 'max-age=0' directive but no extra s-maxage, do not cache |
| 3790 | * the response. */ |
| 3791 | if (has_null_maxage) { |
| 3792 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3793 | } |
| 3794 | |
| 3795 | /* If no freshness information could be found in Cache-Control values, |
| 3796 | * look for an Expires header. */ |
| 3797 | if (!has_freshness_info) { |
| 3798 | ctx.blk = NULL; |
| 3799 | has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0); |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3800 | } |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3801 | |
| 3802 | /* If no freshness information could be found in Cache-Control or Expires |
| 3803 | * values, look for an explicit validator. */ |
| 3804 | if (!has_freshness_info) { |
| 3805 | ctx.blk = NULL; |
| 3806 | has_validator = 1; |
| 3807 | if (!http_find_header(htx, ist("etag"), &ctx, 0)) { |
| 3808 | ctx.blk = NULL; |
| 3809 | if (!http_find_header(htx, ist("last-modified"), &ctx, 0)) |
| 3810 | has_validator = 0; |
| 3811 | } |
| 3812 | } |
| 3813 | |
| 3814 | /* We won't store an entry that has neither a cache validator nor an |
| 3815 | * explicit expiration time, as suggested in RFC 7234#3. */ |
| 3816 | if (!has_freshness_info && !has_validator) |
Remi Tricot-Le Breton | 879debe | 2023-02-21 11:47:17 +0100 | [diff] [blame] | 3817 | txn->flags &= ~TX_CACHEABLE; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3818 | } |
| 3819 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3820 | /* |
| 3821 | * In a GET, HEAD or POST request, check if the requested URI matches the stats uri |
Aurelien DARRAGON | f628614 | 2023-12-05 17:54:20 +0100 | [diff] [blame] | 3822 | * for the current proxy. |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3823 | * |
| 3824 | * It is assumed that the request is either a HEAD, GET, or POST and that the |
| 3825 | * uri_auth field is valid. |
| 3826 | * |
| 3827 | * Returns 1 if stats should be provided, otherwise 0. |
| 3828 | */ |
Aurelien DARRAGON | f628614 | 2023-12-05 17:54:20 +0100 | [diff] [blame] | 3829 | static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *px) |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3830 | { |
Aurelien DARRAGON | f628614 | 2023-12-05 17:54:20 +0100 | [diff] [blame] | 3831 | struct uri_auth *uri_auth = px->uri_auth; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3832 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3833 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3834 | struct ist uri; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3835 | |
| 3836 | if (!uri_auth) |
| 3837 | return 0; |
| 3838 | |
| 3839 | if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST) |
| 3840 | return 0; |
| 3841 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3842 | htx = htxbuf(&s->req.buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 3843 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3844 | uri = htx_sl_req_uri(sl); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 3845 | if (*uri_auth->uri_prefix == '/') { |
| 3846 | struct http_uri_parser parser = http_uri_parser_init(uri); |
| 3847 | uri = http_parse_path(&parser); |
| 3848 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3849 | |
| 3850 | /* check URI size */ |
| 3851 | if (uri_auth->uri_len > uri.len) |
| 3852 | return 0; |
| 3853 | |
| 3854 | if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
| 3855 | return 0; |
| 3856 | |
| 3857 | return 1; |
| 3858 | } |
| 3859 | |
| 3860 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3861 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3862 | * and program a response message if something was unexpected. It cannot fail |
| 3863 | * and always relies on the stats applet to complete the job. It does not touch |
| 3864 | * analysers nor counters, which are left to the caller. It does not touch |
| 3865 | * s->target which is supposed to already point to the stats applet. The caller |
| 3866 | * is expected to have already assigned an appctx to the stream. |
| 3867 | */ |
Aurelien DARRAGON | f628614 | 2023-12-05 17:54:20 +0100 | [diff] [blame] | 3868 | static int http_handle_stats(struct stream *s, struct channel *req, struct proxy *px) |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3869 | { |
| 3870 | struct stats_admin_rule *stats_admin_rule; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3871 | struct session *sess = s->sess; |
| 3872 | struct http_txn *txn = s->txn; |
| 3873 | struct http_msg *msg = &txn->req; |
Aurelien DARRAGON | f628614 | 2023-12-05 17:54:20 +0100 | [diff] [blame] | 3874 | struct uri_auth *uri_auth = px->uri_auth; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3875 | const char *h, *lookup, *end; |
Willy Tarreau | 8e7c6e6 | 2022-05-18 17:58:02 +0200 | [diff] [blame] | 3876 | struct appctx *appctx = __sc_appctx(s->scb); |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3877 | struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3878 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3879 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3880 | |
Willy Tarreau | 41f8852 | 2022-05-03 18:39:27 +0200 | [diff] [blame] | 3881 | appctx->st1 = 0; |
Willy Tarreau | 6ef1648 | 2022-05-06 18:07:53 +0200 | [diff] [blame] | 3882 | ctx->state = STAT_STATE_INIT; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3883 | ctx->st_code = STAT_STATUS_INIT; |
Aurelien DARRAGON | f628614 | 2023-12-05 17:54:20 +0100 | [diff] [blame] | 3884 | ctx->http_px = px; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3885 | ctx->flags |= uri_auth->flags; |
| 3886 | ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3887 | if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD)) |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3888 | ctx->flags |= STAT_CHUNKED; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3889 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3890 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 3891 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3892 | lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len; |
| 3893 | end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3894 | |
| 3895 | for (h = lookup; h <= end - 3; h++) { |
| 3896 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3897 | ctx->flags |= STAT_HIDE_DOWN; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3898 | break; |
| 3899 | } |
Amaury Denoyelle | 91e55ea | 2021-02-25 14:46:08 +0100 | [diff] [blame] | 3900 | } |
| 3901 | |
| 3902 | for (h = lookup; h <= end - 9; h++) { |
| 3903 | if (memcmp(h, ";no-maint", 9) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3904 | ctx->flags |= STAT_HIDE_MAINT; |
Willy Tarreau | 3e32036 | 2020-10-23 17:28:57 +0200 | [diff] [blame] | 3905 | break; |
| 3906 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3907 | } |
| 3908 | |
| 3909 | if (uri_auth->refresh) { |
| 3910 | for (h = lookup; h <= end - 10; h++) { |
| 3911 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3912 | ctx->flags |= STAT_NO_REFRESH; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3913 | break; |
| 3914 | } |
| 3915 | } |
| 3916 | } |
| 3917 | |
| 3918 | for (h = lookup; h <= end - 4; h++) { |
| 3919 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3920 | ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3921 | break; |
| 3922 | } |
| 3923 | } |
| 3924 | |
| 3925 | for (h = lookup; h <= end - 6; h++) { |
| 3926 | if (memcmp(h, ";typed", 6) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3927 | ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
| 3928 | ctx->flags |= STAT_FMT_TYPED; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3929 | break; |
| 3930 | } |
| 3931 | } |
| 3932 | |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 3933 | for (h = lookup; h <= end - 5; h++) { |
| 3934 | if (memcmp(h, ";json", 5) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3935 | ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
| 3936 | ctx->flags |= STAT_FMT_JSON; |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 3937 | break; |
| 3938 | } |
| 3939 | } |
| 3940 | |
| 3941 | for (h = lookup; h <= end - 12; h++) { |
| 3942 | if (memcmp(h, ";json-schema", 12) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3943 | ctx->flags &= ~STAT_FMT_MASK; |
| 3944 | ctx->flags |= STAT_JSON_SCHM; |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 3945 | break; |
| 3946 | } |
| 3947 | } |
| 3948 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3949 | for (h = lookup; h <= end - 8; h++) { |
| 3950 | if (memcmp(h, ";st=", 4) == 0) { |
| 3951 | int i; |
| 3952 | h += 4; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3953 | ctx->st_code = STAT_STATUS_UNKN; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3954 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3955 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3956 | ctx->st_code = i; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3957 | break; |
| 3958 | } |
| 3959 | } |
| 3960 | break; |
| 3961 | } |
| 3962 | } |
| 3963 | |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3964 | ctx->scope_str = 0; |
| 3965 | ctx->scope_len = 0; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3966 | for (h = lookup; h <= end - 8; h++) { |
| 3967 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3968 | int itx = 0; |
| 3969 | const char *h2; |
| 3970 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3971 | const char *err; |
| 3972 | |
| 3973 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3974 | h2 = h; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3975 | ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl); |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 3976 | while (h < end) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3977 | if (*h == ';' || *h == '&' || *h == ' ') |
| 3978 | break; |
| 3979 | itx++; |
| 3980 | h++; |
| 3981 | } |
| 3982 | |
| 3983 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3984 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3985 | ctx->scope_len = itx; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3986 | |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3987 | /* 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] | 3988 | memcpy(scope_txt, h2, itx); |
| 3989 | scope_txt[itx] = '\0'; |
| 3990 | err = invalid_char(scope_txt); |
| 3991 | if (err) { |
| 3992 | /* bad char in search text => clear scope */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3993 | ctx->scope_str = 0; |
| 3994 | ctx->scope_len = 0; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3995 | } |
| 3996 | break; |
| 3997 | } |
| 3998 | } |
| 3999 | |
| 4000 | /* now check whether we have some admin rules for this request */ |
| 4001 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
| 4002 | int ret = 1; |
| 4003 | |
| 4004 | if (stats_admin_rule->cond) { |
| 4005 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 4006 | ret = acl_pass(ret); |
| 4007 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 4008 | ret = !ret; |
| 4009 | } |
| 4010 | |
| 4011 | if (ret) { |
| 4012 | /* no rule, or the rule matches */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4013 | ctx->flags |= STAT_ADMIN; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4014 | break; |
| 4015 | } |
| 4016 | } |
| 4017 | |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4018 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 4019 | appctx->st0 = STAT_HTTP_HEAD; |
| 4020 | else if (txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4021 | if (ctx->flags & STAT_ADMIN) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4022 | appctx->st0 = STAT_HTTP_POST; |
Christopher Faulet | bd9e842 | 2019-08-15 22:26:48 +0200 | [diff] [blame] | 4023 | if (msg->msg_state < HTTP_MSG_DATA) |
| 4024 | req->analysers |= AN_REQ_HTTP_BODY; |
| 4025 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4026 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4027 | /* POST without admin level */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4028 | ctx->flags &= ~STAT_CHUNKED; |
| 4029 | ctx->st_code = STAT_STATUS_DENY; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4030 | appctx->st0 = STAT_HTTP_LAST; |
| 4031 | } |
| 4032 | } |
| 4033 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4034 | /* Unsupported method */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 4035 | ctx->flags &= ~STAT_CHUNKED; |
| 4036 | ctx->st_code = STAT_STATUS_IVAL; |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4037 | appctx->st0 = STAT_HTTP_LAST; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4038 | } |
| 4039 | |
| 4040 | s->task->nice = -32; /* small boost for HTTP statistics */ |
| 4041 | return 1; |
| 4042 | } |
| 4043 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4044 | /* This function waits for the message payload at most <time> milliseconds (may |
| 4045 | * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the |
| 4046 | * payload are received (0 means no limit). It returns HTTP_RULE_* depending on |
| 4047 | * the result: |
| 4048 | * |
| 4049 | * - HTTP_RULE_RES_CONT when conditions are met to stop waiting |
| 4050 | * - HTTP_RULE_RES_YIELD to wait for more data |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 4051 | * - HTTP_RULE_RES_ABRT when a timeout occurred. |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4052 | * - 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] | 4053 | * - HTTP_RULE_RES_ERROR if an internal error occurred |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4054 | * |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 4055 | * 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] | 4056 | * to the client, depending on the channel (408 on request side, 504 on response |
| 4057 | * side). All other errors must be handled by the caller. |
| 4058 | */ |
| 4059 | enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn, |
| 4060 | unsigned int time, unsigned int bytes) |
| 4061 | { |
| 4062 | struct session *sess = s->sess; |
| 4063 | struct http_txn *txn = s->txn; |
| 4064 | struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req); |
| 4065 | struct htx *htx; |
| 4066 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 4067 | |
| 4068 | htx = htxbuf(&chn->buf); |
| 4069 | |
| 4070 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
| 4071 | ret = HTTP_RULE_RES_BADREQ; |
| 4072 | goto end; |
| 4073 | } |
| 4074 | if (htx->flags & HTX_FL_PROCESSING_ERROR) { |
| 4075 | ret = HTTP_RULE_RES_ERROR; |
| 4076 | goto end; |
| 4077 | } |
| 4078 | |
| 4079 | /* Do nothing for bodyless and CONNECT requests */ |
| 4080 | if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS)) |
| 4081 | goto end; |
| 4082 | |
Christopher Faulet | ffcffa8 | 2023-04-05 10:33:31 +0200 | [diff] [blame] | 4083 | if (!(chn->flags & CF_ISRESP)) { |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4084 | if (http_handle_expect_hdr(s, htx, msg) == -1) { |
| 4085 | ret = HTTP_RULE_RES_ERROR; |
| 4086 | goto end; |
| 4087 | } |
| 4088 | } |
| 4089 | |
Christopher Faulet | 2954bcc | 2023-04-05 10:42:03 +0200 | [diff] [blame] | 4090 | /* Now we're are waiting for the payload. We just need to know if all |
| 4091 | * data have been received or if the buffer is full. |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4092 | */ |
Christopher Faulet | 7833596 | 2021-09-23 14:46:32 +0200 | [diff] [blame] | 4093 | if ((htx->flags & HTX_FL_EOM) || |
| 4094 | htx_get_tail_type(htx) > HTX_BLK_DATA || |
| 4095 | channel_htx_full(chn, htx, global.tune.maxrewrite) || |
Willy Tarreau | 99615ed | 2022-05-25 07:29:36 +0200 | [diff] [blame] | 4096 | sc_waiting_room(chn_prod(chn))) |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4097 | goto end; |
| 4098 | |
| 4099 | if (bytes) { |
| 4100 | struct htx_blk *blk; |
| 4101 | unsigned int len = 0; |
| 4102 | |
| 4103 | for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 4104 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 4105 | continue; |
| 4106 | len += htx_get_blksz(blk); |
| 4107 | if (len >= bytes) |
| 4108 | goto end; |
| 4109 | } |
| 4110 | } |
| 4111 | |
| 4112 | if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) { |
| 4113 | if (!(chn->flags & CF_ISRESP)) |
| 4114 | goto abort_req; |
| 4115 | goto abort_res; |
| 4116 | } |
| 4117 | |
| 4118 | /* we get here if we need to wait for more data */ |
Christopher Faulet | ca5309a | 2023-04-17 16:17:32 +0200 | [diff] [blame] | 4119 | if (!(chn_prod(chn)->flags & (SC_FL_EOS|SC_FL_ABRT_DONE))) { |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4120 | if (!tick_isset(chn->analyse_exp)) |
| 4121 | chn->analyse_exp = tick_add_ifset(now_ms, time); |
| 4122 | ret = HTTP_RULE_RES_YIELD; |
| 4123 | } |
| 4124 | |
| 4125 | end: |
| 4126 | return ret; |
| 4127 | |
Christopher Faulet | f0d80df | 2023-01-13 10:20:20 +0100 | [diff] [blame] | 4128 | abort: |
| 4129 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 4130 | ret = HTTP_RULE_RES_ABRT; |
| 4131 | goto end; |
| 4132 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4133 | abort_req: |
| 4134 | txn->status = 408; |
| 4135 | if (!(s->flags & SF_ERR_MASK)) |
| 4136 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4137 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4138 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4139 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | f0d80df | 2023-01-13 10:20:20 +0100 | [diff] [blame] | 4140 | goto abort; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4141 | |
| 4142 | abort_res: |
| 4143 | txn->status = 504; |
| 4144 | if (!(s->flags & SF_ERR_MASK)) |
| 4145 | s->flags |= SF_ERR_SRVTO; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4146 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | f0d80df | 2023-01-13 10:20:20 +0100 | [diff] [blame] | 4147 | goto abort; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4148 | } |
| 4149 | |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4150 | void http_perform_server_redirect(struct stream *s, struct stconn *sc) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4151 | { |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4152 | struct channel *req = &s->req; |
| 4153 | struct channel *res = &s->res; |
| 4154 | struct server *srv; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4155 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4156 | struct htx_sl *sl; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4157 | struct ist path, location; |
| 4158 | unsigned int flags; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4159 | struct http_uri_parser parser; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4160 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4161 | /* |
| 4162 | * Create the location |
| 4163 | */ |
| 4164 | chunk_reset(&trash); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4165 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4166 | /* 1: add the server's prefix */ |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4167 | /* special prefix "/" means don't change URL */ |
| 4168 | srv = __objt_server(s->target); |
| 4169 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 4170 | if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len)) |
| 4171 | return; |
| 4172 | } |
| 4173 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4174 | /* 2: add the request Path */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4175 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 4176 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4177 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4178 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4179 | if (!isttest(path)) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4180 | return; |
| 4181 | |
| 4182 | if (!chunk_memcat(&trash, path.ptr, path.len)) |
| 4183 | return; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4184 | location = ist2(trash.area, trash.data); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4185 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4186 | /* |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 4187 | * Create the 302 response |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4188 | */ |
| 4189 | htx = htx_from_buf(&res->buf); |
Christopher Faulet | 617ec5c | 2023-10-17 11:43:43 +0200 | [diff] [blame] | 4190 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN|HTX_SL_F_BODYLESS); |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4191 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4192 | ist("HTTP/1.1"), ist("302"), ist("Found")); |
| 4193 | if (!sl) |
| 4194 | goto fail; |
| 4195 | sl->info.res.status = 302; |
| 4196 | s->txn->status = 302; |
| 4197 | |
| 4198 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) || |
| 4199 | !htx_add_header(htx, ist("Connection"), ist("close")) || |
| 4200 | !htx_add_header(htx, ist("Content-length"), ist("0")) || |
| 4201 | !htx_add_header(htx, ist("Location"), location)) |
| 4202 | goto fail; |
| 4203 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4204 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4205 | goto fail; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4206 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4207 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | c20afb8 | 2020-01-24 19:16:26 +0100 | [diff] [blame] | 4208 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4209 | if (!http_forward_proxy_resp(s, 1)) |
| 4210 | goto fail; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4211 | |
| 4212 | /* return without error. */ |
Christopher Faulet | cfc11c0 | 2023-04-13 16:10:23 +0200 | [diff] [blame] | 4213 | sc_abort(sc); |
Christopher Faulet | b2b1c3a | 2023-04-13 16:23:48 +0200 | [diff] [blame] | 4214 | sc_shutdown(sc); |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4215 | s->conn_err_type = STRM_ET_NONE; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4216 | sc->state = SC_ST_CLO; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4217 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4218 | if (!(s->flags & SF_ERR_MASK)) |
| 4219 | s->flags |= SF_ERR_LOCAL; |
| 4220 | if (!(s->flags & SF_FINST_MASK)) |
| 4221 | s->flags |= SF_FINST_C; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4222 | |
| 4223 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 4224 | srv_inc_sess_ctr(srv); |
| 4225 | srv_set_sess_last(srv); |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4226 | return; |
| 4227 | |
| 4228 | fail: |
| 4229 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4230 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4231 | channel_htx_truncate(res, htx); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4232 | } |
| 4233 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4234 | /* This function terminates the request because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4235 | * because an error was triggered during the body forwarding. |
| 4236 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4237 | static void http_end_request(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4238 | { |
| 4239 | struct channel *chn = &s->req; |
| 4240 | struct http_txn *txn = s->txn; |
| 4241 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4242 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4243 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4244 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) { |
| 4245 | 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] | 4246 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4247 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4248 | |
| 4249 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4250 | /* No need to read anymore, the request was completely parsed. |
| 4251 | * We can shut the read side unless we want to abort_on_close, |
| 4252 | * or we have a POST request. The issue with POST requests is |
| 4253 | * that some browsers still send a CRLF after the request, and |
| 4254 | * this CRLF must be read so that it does not remain in the kernel |
| 4255 | * buffers, otherwise a close could cause an RST on some systems |
| 4256 | * (eg: Linux). |
| 4257 | */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 4258 | 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] | 4259 | channel_dont_read(chn); |
| 4260 | |
| 4261 | /* if the server closes the connection, we want to immediately react |
| 4262 | * and close the socket to save packets and syscalls. |
| 4263 | */ |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 4264 | s->scb->flags |= SC_FL_NOHALF; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4265 | |
| 4266 | /* In any case we've finished parsing the request so we must |
| 4267 | * disable Nagle when sending data because 1) we're not going |
| 4268 | * to shut this side, and 2) the server is waiting for us to |
| 4269 | * send pending data. |
| 4270 | */ |
Christopher Faulet | 68ef218 | 2023-03-17 15:38:18 +0100 | [diff] [blame] | 4271 | s->scb->flags |= SC_FL_SND_NEVERWAIT; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4272 | |
Christopher Faulet | c2fba3f | 2023-03-01 16:03:17 +0100 | [diff] [blame] | 4273 | if (txn->rsp.msg_state < HTTP_MSG_BODY || |
| 4274 | (txn->rsp.msg_state < HTTP_MSG_DONE && s->scb->state != SC_ST_CLO)) { |
Christopher Faulet | af12436 | 2023-02-14 10:48:02 +0100 | [diff] [blame] | 4275 | /* The server has not finished to respond and the |
| 4276 | * backend SC is not closed, so we don't want to move in |
| 4277 | * order not to upset it. |
Christopher Faulet | d01ce40 | 2019-01-02 17:44:13 +0100 | [diff] [blame] | 4278 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4279 | 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] | 4280 | return; |
| 4281 | } |
| 4282 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4283 | /* When we get here, it means that both the request and the |
| 4284 | * response have finished receiving. Depending on the connection |
| 4285 | * mode, we'll have to wait for the last bytes to leave in either |
| 4286 | * direction, and sometimes for a close to be effective. |
| 4287 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4288 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4289 | /* Tunnel mode will not have any analyser so it needs to |
| 4290 | * poll for reads. |
| 4291 | */ |
Christopher Faulet | 9704797 | 2023-04-17 08:52:10 +0200 | [diff] [blame] | 4292 | channel_auto_read(&s->req); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4293 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Christopher Faulet | 27c17d1 | 2023-04-18 11:01:51 +0200 | [diff] [blame] | 4294 | if (txn->rsp.msg_state != HTTP_MSG_TUNNEL) |
| 4295 | s->res.flags |= CF_WAKE_ONCE; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4296 | } |
| 4297 | else { |
| 4298 | /* we're not expecting any new data to come for this |
| 4299 | * transaction, so we can close it. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4300 | * |
| 4301 | * However, there is an exception if the response |
| 4302 | * length is undefined. In this case, we need to wait |
| 4303 | * the close from the server. The response will be |
| 4304 | * switched in TUNNEL mode until the end. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4305 | */ |
| 4306 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 4307 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4308 | goto check_channel_flags; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4309 | |
Christopher Faulet | 64350bb | 2023-04-13 16:37:37 +0200 | [diff] [blame] | 4310 | if (!(s->scb->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED))) { |
Christopher Faulet | 12762f0 | 2023-04-13 15:40:10 +0200 | [diff] [blame] | 4311 | sc_schedule_abort(s->scf); |
Christopher Faulet | df7cd71 | 2023-04-13 15:56:26 +0200 | [diff] [blame] | 4312 | sc_schedule_shutdown(s->scb); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4313 | } |
| 4314 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4315 | goto check_channel_flags; |
| 4316 | } |
| 4317 | |
| 4318 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4319 | http_msg_closing: |
| 4320 | /* nothing else to forward, just waiting for the output buffer |
Christopher Faulet | df7cd71 | 2023-04-13 15:56:26 +0200 | [diff] [blame] | 4321 | * to be empty and for the shut_wanted to take effect. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4322 | */ |
| 4323 | if (channel_is_empty(chn)) { |
| 4324 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4325 | goto http_msg_closed; |
| 4326 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4327 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4328 | return; |
| 4329 | } |
| 4330 | |
| 4331 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4332 | http_msg_closed: |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4333 | /* if we don't know whether the server will close, we need to hard close */ |
| 4334 | if (txn->rsp.flags & HTTP_MSGF_XFER_LEN) |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 4335 | s->scb->flags |= SC_FL_NOLINGER; /* we want to close ASAP */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4336 | /* 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] | 4337 | if (!(s->be->options & PR_O_ABRT_CLOSE)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4338 | channel_dont_read(chn); |
| 4339 | goto end; |
| 4340 | } |
| 4341 | |
| 4342 | check_channel_flags: |
| 4343 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
Christopher Faulet | 64350bb | 2023-04-13 16:37:37 +0200 | [diff] [blame] | 4344 | if (s->scb->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4345 | /* if we've just closed an output, let's switch */ |
| 4346 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4347 | goto http_msg_closing; |
| 4348 | } |
| 4349 | |
| 4350 | end: |
| 4351 | chn->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4352 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
Christopher Faulet | 68ef218 | 2023-03-17 15:38:18 +0100 | [diff] [blame] | 4353 | s->scb->flags |= SC_FL_SND_NEVERWAIT; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4354 | if (HAS_REQ_DATA_FILTERS(s)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4355 | chn->analysers |= AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | 27c17d1 | 2023-04-18 11:01:51 +0200 | [diff] [blame] | 4356 | else |
| 4357 | c_adv(chn, htxbuf(&chn->buf)->data - co_data(chn)); |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4358 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4359 | channel_auto_close(chn); |
| 4360 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4361 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4362 | } |
| 4363 | |
| 4364 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4365 | /* This function terminates the response because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4366 | * because an error was triggered during the body forwarding. |
| 4367 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4368 | static void http_end_response(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4369 | { |
| 4370 | struct channel *chn = &s->res; |
| 4371 | struct http_txn *txn = s->txn; |
| 4372 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4373 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4374 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4375 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) { |
| 4376 | 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] | 4377 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4378 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4379 | |
| 4380 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4381 | /* In theory, we don't need to read anymore, but we must |
| 4382 | * still monitor the server connection for a possible close |
| 4383 | * while the request is being uploaded, so we don't disable |
| 4384 | * reading. |
| 4385 | */ |
| 4386 | /* channel_dont_read(chn); */ |
| 4387 | |
Christopher Faulet | af12436 | 2023-02-14 10:48:02 +0100 | [diff] [blame] | 4388 | 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] | 4389 | /* The client seems to still be sending data, probably |
| 4390 | * because we got an error response during an upload. |
| 4391 | * We have the choice of either breaking the connection |
| 4392 | * or letting it pass through. Let's do the later. |
| 4393 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4394 | DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4395 | return; |
| 4396 | } |
| 4397 | |
| 4398 | /* When we get here, it means that both the request and the |
| 4399 | * response have finished receiving. Depending on the connection |
| 4400 | * mode, we'll have to wait for the last bytes to leave in either |
| 4401 | * direction, and sometimes for a close to be effective. |
| 4402 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4403 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | 9704797 | 2023-04-17 08:52:10 +0200 | [diff] [blame] | 4404 | channel_auto_read(&s->res); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4405 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Christopher Faulet | 27c17d1 | 2023-04-18 11:01:51 +0200 | [diff] [blame] | 4406 | if (txn->req.msg_state != HTTP_MSG_TUNNEL) |
| 4407 | s->req.flags |= CF_WAKE_ONCE; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4408 | } |
| 4409 | else { |
| 4410 | /* we're not expecting any new data to come for this |
| 4411 | * transaction, so we can close it. |
| 4412 | */ |
Christopher Faulet | 64350bb | 2023-04-13 16:37:37 +0200 | [diff] [blame] | 4413 | if (!(s->scf->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED))) { |
Christopher Faulet | 12762f0 | 2023-04-13 15:40:10 +0200 | [diff] [blame] | 4414 | sc_schedule_abort(s->scb); |
Christopher Faulet | df7cd71 | 2023-04-13 15:56:26 +0200 | [diff] [blame] | 4415 | sc_schedule_shutdown(s->scf); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4416 | } |
| 4417 | } |
| 4418 | goto check_channel_flags; |
| 4419 | } |
| 4420 | |
| 4421 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4422 | http_msg_closing: |
| 4423 | /* nothing else to forward, just waiting for the output buffer |
Christopher Faulet | df7cd71 | 2023-04-13 15:56:26 +0200 | [diff] [blame] | 4424 | * to be empty and for the shut_wanted to take effect. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4425 | */ |
| 4426 | if (channel_is_empty(chn)) { |
| 4427 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4428 | goto http_msg_closed; |
| 4429 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4430 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4431 | return; |
| 4432 | } |
| 4433 | |
| 4434 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4435 | http_msg_closed: |
| 4436 | /* drop any pending data */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4437 | channel_htx_truncate(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4438 | channel_abort(&s->req); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4439 | goto end; |
| 4440 | } |
| 4441 | |
| 4442 | check_channel_flags: |
| 4443 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
Christopher Faulet | 64350bb | 2023-04-13 16:37:37 +0200 | [diff] [blame] | 4444 | if (s->scf->flags & (SC_FL_SHUT_DONE|SC_FL_SHUT_WANTED)) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4445 | /* if we've just closed an output, let's switch */ |
| 4446 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4447 | goto http_msg_closing; |
| 4448 | } |
| 4449 | |
| 4450 | end: |
| 4451 | chn->analysers &= AN_RES_FLT_END; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4452 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
Christopher Faulet | 68ef218 | 2023-03-17 15:38:18 +0100 | [diff] [blame] | 4453 | s->scf->flags |= SC_FL_SND_NEVERWAIT; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4454 | if (HAS_RSP_DATA_FILTERS(s)) |
| 4455 | chn->analysers |= AN_RES_FLT_XFER_DATA; |
Christopher Faulet | 27c17d1 | 2023-04-18 11:01:51 +0200 | [diff] [blame] | 4456 | else |
| 4457 | c_adv(chn, htxbuf(&chn->buf)->data - co_data(chn)); |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4458 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4459 | channel_auto_close(chn); |
| 4460 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4461 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4462 | } |
| 4463 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4464 | /* Forward a response generated by HAProxy (error/redirect/return). This |
| 4465 | * function forwards all pending incoming data. If <final> is set to 0, nothing |
| 4466 | * more is performed. It is used for 1xx informational messages. Otherwise, the |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4467 | * transaction is terminated and the request is emptied. On success 1 is |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4468 | * returned. If an error occurred, 0 is returned. If it fails, this function |
| 4469 | * only exits. It is the caller responsibility to do the cleanup. |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4470 | */ |
| 4471 | int http_forward_proxy_resp(struct stream *s, int final) |
| 4472 | { |
| 4473 | struct channel *req = &s->req; |
| 4474 | struct channel *res = &s->res; |
| 4475 | struct htx *htx = htxbuf(&res->buf); |
| 4476 | size_t data; |
| 4477 | |
| 4478 | if (final) { |
| 4479 | htx->flags |= HTX_FL_PROXY_RESP; |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4480 | |
Christopher Faulet | aab1b67 | 2020-11-18 16:44:02 +0100 | [diff] [blame] | 4481 | if (!htx_is_empty(htx) && !http_eval_after_res_rules(s)) |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 4482 | return 0; |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4483 | |
Christopher Faulet | d6c4836 | 2020-10-19 18:01:38 +0200 | [diff] [blame] | 4484 | if (s->txn->meth == HTTP_METH_HEAD) |
| 4485 | htx_skip_msg_payload(htx); |
| 4486 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4487 | channel_auto_read(req); |
| 4488 | channel_abort(req); |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4489 | channel_htx_erase(req, htxbuf(&req->buf)); |
| 4490 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4491 | channel_auto_read(res); |
| 4492 | channel_auto_close(res); |
Christopher Faulet | 12762f0 | 2023-04-13 15:40:10 +0200 | [diff] [blame] | 4493 | sc_schedule_abort(s->scb); |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 4494 | s->scb->flags |= SC_FL_EOI; /* The response is terminated, add EOI */ |
Christopher Faulet | 42432f3 | 2020-11-20 17:43:16 +0100 | [diff] [blame] | 4495 | htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */ |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4496 | } |
Christopher Faulet | cf6898c | 2020-06-25 15:55:11 +0200 | [diff] [blame] | 4497 | else { |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 4498 | /* Send ASAP informational messages. Rely on SC_FL_EOI for final |
Christopher Faulet | cf6898c | 2020-06-25 15:55:11 +0200 | [diff] [blame] | 4499 | * response. |
| 4500 | */ |
Christopher Faulet | 5c281d5 | 2023-03-16 15:53:28 +0100 | [diff] [blame] | 4501 | s->scf->flags |= SC_FL_SND_ASAP; |
Christopher Faulet | cf6898c | 2020-06-25 15:55:11 +0200 | [diff] [blame] | 4502 | } |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4503 | |
| 4504 | data = htx->data - co_data(res); |
| 4505 | c_adv(res, data); |
| 4506 | htx->first = -1; |
| 4507 | res->total += data; |
| 4508 | return 1; |
| 4509 | } |
| 4510 | |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4511 | void http_server_error(struct stream *s, struct stconn *sc, int err, |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4512 | int finst, struct http_reply *msg) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4513 | { |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4514 | http_reply_and_close(s, s->txn->status, msg); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4515 | if (!(s->flags & SF_ERR_MASK)) |
| 4516 | s->flags |= err; |
| 4517 | if (!(s->flags & SF_FINST_MASK)) |
| 4518 | s->flags |= finst; |
| 4519 | } |
| 4520 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4521 | 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] | 4522 | { |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4523 | if (!msg) { |
| 4524 | channel_htx_truncate(&s->res, htxbuf(&s->res.buf)); |
| 4525 | goto end; |
| 4526 | } |
| 4527 | |
| 4528 | if (http_reply_message(s, msg) == -1) { |
| 4529 | /* 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] | 4530 | * it is already an internal error. If it was already a "const" |
| 4531 | * 500 error, just fail. |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4532 | */ |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4533 | if (s->txn->status == 500) { |
| 4534 | if (s->txn->flags & TX_CONST_REPLY) |
| 4535 | goto end; |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4536 | s->txn->flags |= TX_CONST_REPLY; |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4537 | } |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4538 | s->txn->status = 500; |
| 4539 | s->txn->http_reply = NULL; |
| 4540 | return http_reply_and_close(s, s->txn->status, http_error_message(s)); |
| 4541 | } |
| 4542 | |
| 4543 | end: |
Christopher Faulet | 2d56500 | 2021-09-10 09:17:50 +0200 | [diff] [blame] | 4544 | /* At this staged, HTTP analysis is finished */ |
| 4545 | s->req.analysers &= AN_REQ_FLT_END; |
| 4546 | s->req.analyse_exp = TICK_ETERNITY; |
| 4547 | |
| 4548 | s->res.analysers &= AN_RES_FLT_END; |
| 4549 | s->res.analyse_exp = TICK_ETERNITY; |
| 4550 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4551 | channel_auto_read(&s->req); |
| 4552 | channel_abort(&s->req); |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4553 | channel_htx_erase(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4554 | channel_auto_read(&s->res); |
| 4555 | channel_auto_close(&s->res); |
Christopher Faulet | 12762f0 | 2023-04-13 15:40:10 +0200 | [diff] [blame] | 4556 | sc_schedule_abort(s->scb); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4557 | } |
| 4558 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4559 | struct http_reply *http_error_message(struct stream *s) |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4560 | { |
| 4561 | const int msgnum = http_get_status_idx(s->txn->status); |
| 4562 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4563 | if (s->txn->http_reply) |
| 4564 | return s->txn->http_reply; |
| 4565 | else if (s->be->replies[msgnum]) |
| 4566 | return s->be->replies[msgnum]; |
| 4567 | else if (strm_fe(s)->replies[msgnum]) |
| 4568 | return strm_fe(s)->replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4569 | else |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4570 | return &http_err_replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4571 | } |
| 4572 | |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4573 | /* Produces an HTX message from an http reply. Depending on the http reply type, |
| 4574 | * a, errorfile, an raw file or a log-format string is used. On success, it |
| 4575 | * returns 0. If an error occurs -1 is returned. If it fails, this function only |
| 4576 | * exits. It is the caller responsibility to do the cleanup. |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4577 | */ |
Christopher Faulet | ae43b6c | 2020-05-27 15:24:22 +0200 | [diff] [blame] | 4578 | 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] | 4579 | { |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4580 | struct buffer *errmsg; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4581 | struct htx_sl *sl; |
| 4582 | struct buffer *body = NULL; |
| 4583 | const char *status, *reason, *clen, *ctype; |
| 4584 | unsigned int slflags; |
| 4585 | int ret = 0; |
| 4586 | |
Christopher Faulet | e29a97e | 2020-05-14 14:49:25 +0200 | [diff] [blame] | 4587 | /* |
| 4588 | * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so |
| 4589 | * |
| 4590 | * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled |
| 4591 | * as no payload if NULL. the TXN status code is set with the status |
| 4592 | * of the original reply. |
| 4593 | */ |
| 4594 | |
| 4595 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4596 | if (reply->body.reply) |
| 4597 | reply = reply->body.reply; |
| 4598 | } |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4599 | if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) { |
| 4600 | /* get default error message */ |
| 4601 | if (reply == s->txn->http_reply) |
| 4602 | s->txn->http_reply = NULL; |
| 4603 | reply = http_error_message(s); |
| 4604 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4605 | if (reply->body.reply) |
| 4606 | reply = reply->body.reply; |
| 4607 | } |
| 4608 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4609 | |
| 4610 | if (reply->type == HTTP_REPLY_ERRMSG) { |
| 4611 | /* implicit or explicit error message*/ |
| 4612 | errmsg = reply->body.errmsg; |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4613 | if (errmsg && !b_is_null(errmsg)) { |
Christopher Faulet | 2056736 | 2020-05-15 14:52:49 +0200 | [diff] [blame] | 4614 | if (!htx_copy_msg(htx, errmsg)) |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4615 | goto fail; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4616 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4617 | } |
| 4618 | else { |
| 4619 | /* no payload, file or log-format string */ |
| 4620 | if (reply->type == HTTP_REPLY_RAW) { |
| 4621 | /* file */ |
| 4622 | body = &reply->body.obj; |
| 4623 | } |
| 4624 | else if (reply->type == HTTP_REPLY_LOGFMT) { |
| 4625 | /* log-format string */ |
| 4626 | body = alloc_trash_chunk(); |
| 4627 | if (!body) |
| 4628 | goto fail_alloc; |
| 4629 | body->data = build_logline(s, body->area, body->size, &reply->body.fmt); |
| 4630 | } |
| 4631 | /* else no payload */ |
| 4632 | |
| 4633 | status = ultoa(reply->status); |
| 4634 | reason = http_get_reason(reply->status); |
| 4635 | slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN); |
| 4636 | if (!body || !b_data(body)) |
| 4637 | slflags |= HTX_SL_F_BODYLESS; |
| 4638 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4639 | if (!sl) |
| 4640 | goto fail; |
| 4641 | sl->info.res.status = reply->status; |
| 4642 | |
| 4643 | clen = (body ? ultoa(b_data(body)) : "0"); |
| 4644 | ctype = reply->ctype; |
| 4645 | |
| 4646 | if (!LIST_ISEMPTY(&reply->hdrs)) { |
| 4647 | struct http_reply_hdr *hdr; |
| 4648 | struct buffer *value = alloc_trash_chunk(); |
| 4649 | |
| 4650 | if (!value) |
| 4651 | goto fail; |
| 4652 | |
| 4653 | list_for_each_entry(hdr, &reply->hdrs, list) { |
| 4654 | chunk_reset(value); |
| 4655 | value->data = build_logline(s, value->area, value->size, &hdr->value); |
| 4656 | if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) { |
| 4657 | free_trash_chunk(value); |
| 4658 | goto fail; |
| 4659 | } |
| 4660 | chunk_reset(value); |
| 4661 | } |
| 4662 | free_trash_chunk(value); |
| 4663 | } |
| 4664 | |
| 4665 | if (!htx_add_header(htx, ist("content-length"), ist(clen)) || |
| 4666 | (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) || |
| 4667 | !htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4668 | (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] | 4669 | goto fail; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4670 | |
| 4671 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4672 | } |
| 4673 | |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4674 | leave: |
| 4675 | if (reply->type == HTTP_REPLY_LOGFMT) |
| 4676 | free_trash_chunk(body); |
| 4677 | return ret; |
| 4678 | |
| 4679 | fail_alloc: |
| 4680 | if (!(s->flags & SF_ERR_MASK)) |
| 4681 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4682 | /* fall through */ |
| 4683 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4684 | ret = -1; |
| 4685 | goto leave; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4686 | } |
| 4687 | |
| 4688 | /* 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] | 4689 | * occurs -1 is returned and the response channel is truncated, removing this |
| 4690 | * way the faulty reply. This function may fail when the reply is formatted |
| 4691 | * (http_reply_to_htx) or when the reply is forwarded |
| 4692 | * (http_forward_proxy_resp). On the last case, it is because a |
| 4693 | * http-after-response rule fails. |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4694 | */ |
| 4695 | int http_reply_message(struct stream *s, struct http_reply *reply) |
| 4696 | { |
| 4697 | struct channel *res = &s->res; |
| 4698 | struct htx *htx = htx_from_buf(&res->buf); |
| 4699 | |
| 4700 | if (s->txn->status == -1) |
| 4701 | s->txn->status = reply->status; |
| 4702 | channel_htx_truncate(res, htx); |
| 4703 | |
| 4704 | if (http_reply_to_htx(s, htx, reply) == -1) |
| 4705 | goto fail; |
| 4706 | |
| 4707 | htx_to_buf(htx, &s->res.buf); |
| 4708 | if (!http_forward_proxy_resp(s, 1)) |
| 4709 | goto fail; |
| 4710 | return 0; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4711 | |
| 4712 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4713 | channel_htx_truncate(res, htx); |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4714 | if (!(s->flags & SF_ERR_MASK)) |
| 4715 | s->flags |= SF_ERR_PRXCOND; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4716 | return -1; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4717 | } |
| 4718 | |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4719 | /* 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] | 4720 | * that the server side is closed. Note that err_type is actually a |
| 4721 | * bitmask, where almost only aborts may be cumulated with other |
| 4722 | * values. We consider that aborted operations are more important |
| 4723 | * than timeouts or errors due to the fact that nobody else in the |
| 4724 | * logs might explain incomplete retries. All others should avoid |
| 4725 | * being cumulated. It should normally not be possible to have multiple |
| 4726 | * aborts at once, but just in case, the first one in sequence is reported. |
| 4727 | * Note that connection errors appearing on the second request of a keep-alive |
| 4728 | * connection are not reported since this allows the client to retry. |
| 4729 | */ |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4730 | void http_return_srv_error(struct stream *s, struct stconn *sc) |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4731 | { |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4732 | int err_type = s->conn_err_type; |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4733 | |
| 4734 | /* set s->txn->status for http_error_message(s) */ |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4735 | if (err_type & STRM_ET_QUEUE_ABRT) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4736 | s->txn->status = -1; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4737 | http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_Q, NULL); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4738 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4739 | else if (err_type & STRM_ET_CONN_ABRT) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4740 | s->txn->status = -1; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4741 | http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_C, NULL); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4742 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4743 | else if (err_type & STRM_ET_QUEUE_TO) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4744 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4745 | http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4746 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4747 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4748 | else if (err_type & STRM_ET_QUEUE_ERR) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4749 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4750 | http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4751 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4752 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4753 | else if (err_type & STRM_ET_CONN_TO) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4754 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4755 | http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4756 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4757 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4758 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4759 | else if (err_type & STRM_ET_CONN_ERR) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4760 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4761 | http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4762 | (s->flags & SF_SRV_REUSED) ? NULL : |
| 4763 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4764 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4765 | else if (err_type & STRM_ET_CONN_RES) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4766 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4767 | http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4768 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4769 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4770 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4771 | else { /* STRM_ET_CONN_OTHER and others */ |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4772 | s->txn->status = 500; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4773 | http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4774 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4775 | } |
| 4776 | } |
| 4777 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4778 | |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4779 | /* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0 |
| 4780 | * on success and -1 on error. |
| 4781 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4782 | 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] | 4783 | { |
| 4784 | /* If we have HTTP/1.1 message with a body and Expect: 100-continue, |
| 4785 | * then we must send an HTTP/1.1 100 Continue intermediate response. |
| 4786 | */ |
Christopher Faulet | ffcffa8 | 2023-04-05 10:33:31 +0200 | [diff] [blame] | 4787 | if (!(msg->flags & HTTP_MSGF_EXPECT_CHECKED) && |
| 4788 | (msg->flags & HTTP_MSGF_VER_11) && |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4789 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
| 4790 | struct ist hdr = { .ptr = "Expect", .len = 6 }; |
| 4791 | struct http_hdr_ctx ctx; |
| 4792 | |
| 4793 | ctx.blk = NULL; |
| 4794 | /* Expect is allowed in 1.1, look for it */ |
| 4795 | if (http_find_header(htx, hdr, &ctx, 0) && |
| 4796 | unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4797 | if (http_reply_100_continue(s) == -1) |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4798 | return -1; |
| 4799 | http_remove_header(htx, &ctx); |
| 4800 | } |
| 4801 | } |
Christopher Faulet | ffcffa8 | 2023-04-05 10:33:31 +0200 | [diff] [blame] | 4802 | msg->flags |= HTTP_MSGF_EXPECT_CHECKED; |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4803 | return 0; |
| 4804 | } |
| 4805 | |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4806 | /* Send a 100-Continue response to the client. It returns 0 on success and -1 |
| 4807 | * on error. The response channel is updated accordingly. |
| 4808 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4809 | static int http_reply_100_continue(struct stream *s) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4810 | { |
| 4811 | struct channel *res = &s->res; |
| 4812 | struct htx *htx = htx_from_buf(&res->buf); |
| 4813 | struct htx_sl *sl; |
| 4814 | unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11| |
| 4815 | HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4816 | |
| 4817 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4818 | ist("HTTP/1.1"), ist("100"), ist("Continue")); |
| 4819 | if (!sl) |
| 4820 | goto fail; |
| 4821 | sl->info.res.status = 100; |
| 4822 | |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 4823 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4824 | goto fail; |
| 4825 | |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4826 | if (!http_forward_proxy_resp(s, 0)) |
| 4827 | goto fail; |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4828 | return 0; |
| 4829 | |
| 4830 | fail: |
| 4831 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4832 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4833 | channel_htx_truncate(res, htx); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4834 | return -1; |
| 4835 | } |
| 4836 | |
Christopher Faulet | 12c51e2 | 2018-11-28 15:59:42 +0100 | [diff] [blame] | 4837 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4838 | /* |
| 4839 | * Capture headers from message <htx> according to header list <cap_hdr>, and |
| 4840 | * fill the <cap> pointers appropriately. |
| 4841 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4842 | 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] | 4843 | { |
| 4844 | struct cap_hdr *h; |
| 4845 | int32_t pos; |
| 4846 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4847 | 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] | 4848 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4849 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4850 | struct ist n, v; |
| 4851 | |
| 4852 | if (type == HTX_BLK_EOH) |
| 4853 | break; |
| 4854 | if (type != HTX_BLK_HDR) |
| 4855 | continue; |
| 4856 | |
| 4857 | n = htx_get_blk_name(htx, blk); |
| 4858 | |
| 4859 | for (h = cap_hdr; h; h = h->next) { |
| 4860 | if (h->namelen && (h->namelen == n.len) && |
| 4861 | (strncasecmp(n.ptr, h->name, h->namelen) == 0)) { |
| 4862 | if (cap[h->index] == NULL) |
| 4863 | cap[h->index] = |
| 4864 | pool_alloc(h->pool); |
| 4865 | |
| 4866 | if (cap[h->index] == NULL) { |
| 4867 | ha_alert("HTTP capture : out of memory.\n"); |
| 4868 | break; |
| 4869 | } |
| 4870 | |
| 4871 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 4872 | v = isttrim(v, h->len); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4873 | |
| 4874 | memcpy(cap[h->index], v.ptr, v.len); |
| 4875 | cap[h->index][v.len]=0; |
| 4876 | } |
| 4877 | } |
| 4878 | } |
| 4879 | } |
| 4880 | |
Christopher Faulet | 0b6bdc5 | 2018-10-24 11:05:36 +0200 | [diff] [blame] | 4881 | /* Delete a value in a header between delimiters <from> and <next>. The header |
| 4882 | * itself is delimited by <start> and <end> pointers. The number of characters |
| 4883 | * displaced is returned, and the pointer to the first delimiter is updated if |
| 4884 | * required. The function tries as much as possible to respect the following |
| 4885 | * principles : |
| 4886 | * - replace <from> delimiter by the <next> one unless <from> points to <start>, |
| 4887 | * in which case <next> is simply removed |
| 4888 | * - set exactly one space character after the new first delimiter, unless there |
| 4889 | * are not enough characters in the block being moved to do so. |
| 4890 | * - remove unneeded spaces before the previous delimiter and after the new |
| 4891 | * one. |
| 4892 | * |
| 4893 | * It is the caller's responsibility to ensure that : |
| 4894 | * - <from> points to a valid delimiter or <start> ; |
| 4895 | * - <next> points to a valid delimiter or <end> ; |
| 4896 | * - there are non-space chars before <from>. |
| 4897 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4898 | 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] | 4899 | { |
| 4900 | char *prev = *from; |
| 4901 | |
| 4902 | if (prev == start) { |
| 4903 | /* We're removing the first value. eat the semicolon, if <next> |
| 4904 | * is lower than <end> */ |
| 4905 | if (next < end) |
| 4906 | next++; |
| 4907 | |
| 4908 | while (next < end && HTTP_IS_SPHT(*next)) |
| 4909 | next++; |
| 4910 | } |
| 4911 | else { |
| 4912 | /* Remove useless spaces before the old delimiter. */ |
| 4913 | while (HTTP_IS_SPHT(*(prev-1))) |
| 4914 | prev--; |
| 4915 | *from = prev; |
| 4916 | |
| 4917 | /* copy the delimiter and if possible a space if we're |
| 4918 | * not at the end of the line. |
| 4919 | */ |
| 4920 | if (next < end) { |
| 4921 | *prev++ = *next++; |
| 4922 | if (prev + 1 < next) |
| 4923 | *prev++ = ' '; |
| 4924 | while (next < end && HTTP_IS_SPHT(*next)) |
| 4925 | next++; |
| 4926 | } |
| 4927 | } |
| 4928 | memmove(prev, next, end - next); |
| 4929 | return (prev - next); |
| 4930 | } |
| 4931 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4932 | |
| 4933 | /* 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] | 4934 | * 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] | 4935 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4936 | 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] | 4937 | { |
| 4938 | struct ist dst = ist2(str, 0); |
| 4939 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4940 | if (istcat(&dst, htx_sl_req_meth(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4941 | goto end; |
| 4942 | if (dst.len + 1 > len) |
| 4943 | goto end; |
| 4944 | dst.ptr[dst.len++] = ' '; |
| 4945 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4946 | if (istcat(&dst, htx_sl_req_uri(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4947 | goto end; |
| 4948 | if (dst.len + 1 > len) |
| 4949 | goto end; |
| 4950 | dst.ptr[dst.len++] = ' '; |
| 4951 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4952 | istcat(&dst, htx_sl_req_vsn(sl), len); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4953 | end: |
| 4954 | return dst.len; |
| 4955 | } |
| 4956 | |
| 4957 | /* |
| 4958 | * Print a debug line with a start line. |
| 4959 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4960 | 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] | 4961 | { |
| 4962 | struct session *sess = strm_sess(s); |
| 4963 | int max; |
| 4964 | |
| 4965 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 4966 | dir, |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 4967 | 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] | 4968 | 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] | 4969 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4970 | max = HTX_SL_P1_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4971 | UBOUND(max, trash.size - trash.data - 3); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4972 | chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4973 | trash.area[trash.data++] = ' '; |
| 4974 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4975 | max = HTX_SL_P2_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4976 | UBOUND(max, trash.size - trash.data - 2); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4977 | chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4978 | trash.area[trash.data++] = ' '; |
| 4979 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4980 | max = HTX_SL_P3_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4981 | UBOUND(max, trash.size - trash.data - 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4982 | chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4983 | trash.area[trash.data++] = '\n'; |
| 4984 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 4985 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4986 | } |
| 4987 | |
| 4988 | /* |
| 4989 | * Print a debug line with a header. |
| 4990 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4991 | 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] | 4992 | { |
| 4993 | struct session *sess = strm_sess(s); |
| 4994 | int max; |
| 4995 | |
| 4996 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 4997 | dir, |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 4998 | 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] | 4999 | 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] | 5000 | |
| 5001 | max = n.len; |
| 5002 | UBOUND(max, trash.size - trash.data - 3); |
| 5003 | chunk_memcat(&trash, n.ptr, max); |
| 5004 | trash.area[trash.data++] = ':'; |
| 5005 | trash.area[trash.data++] = ' '; |
| 5006 | |
| 5007 | max = v.len; |
| 5008 | UBOUND(max, trash.size - trash.data - 1); |
| 5009 | chunk_memcat(&trash, v.ptr, max); |
| 5010 | trash.area[trash.data++] = '\n'; |
| 5011 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 5012 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5013 | } |
| 5014 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5015 | void http_txn_reset_req(struct http_txn *txn) |
| 5016 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 5017 | txn->req.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5018 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 5019 | } |
| 5020 | |
| 5021 | void http_txn_reset_res(struct http_txn *txn) |
| 5022 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 5023 | txn->rsp.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5024 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 5025 | } |
| 5026 | |
| 5027 | /* |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5028 | * Create and initialize a new HTTP transaction for stream <s>. This should be |
| 5029 | * used before processing any new request. It returns the transaction or NLULL |
| 5030 | * on error. |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5031 | */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5032 | struct http_txn *http_create_txn(struct stream *s) |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5033 | { |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5034 | struct http_txn *txn; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 5035 | struct stconn *sc = s->scf; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5036 | |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5037 | txn = pool_alloc(pool_head_http_txn); |
| 5038 | if (!txn) |
| 5039 | return NULL; |
| 5040 | s->txn = txn; |
| 5041 | |
Christopher Faulet | 5eb67f5 | 2022-06-22 17:12:05 +0200 | [diff] [blame] | 5042 | txn->meth = HTTP_METH_OTHER; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 5043 | 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] | 5044 | txn->status = -1; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 5045 | txn->http_reply = NULL; |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 5046 | txn->l7_buffer = BUF_NULL; |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 5047 | write_u32(txn->cache_hash, 0); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5048 | |
| 5049 | txn->cookie_first_date = 0; |
| 5050 | txn->cookie_last_date = 0; |
| 5051 | |
| 5052 | txn->srv_cookie = NULL; |
| 5053 | txn->cli_cookie = NULL; |
| 5054 | txn->uri = NULL; |
| 5055 | |
| 5056 | http_txn_reset_req(txn); |
| 5057 | http_txn_reset_res(txn); |
| 5058 | |
| 5059 | txn->req.chn = &s->req; |
| 5060 | txn->rsp.chn = &s->res; |
| 5061 | |
| 5062 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
| 5063 | |
Aurelien DARRAGON | 5ad2b64 | 2022-11-18 09:17:29 +0100 | [diff] [blame] | 5064 | /* here we don't want to re-initialize s->vars_txn and s->vars_reqres |
| 5065 | * variable lists, because they were already initialized upon stream |
| 5066 | * creation in stream_new(), and thus may already contain some variables |
| 5067 | */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5068 | |
| 5069 | return txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5070 | } |
| 5071 | |
| 5072 | /* to be used at the end of a transaction */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5073 | void http_destroy_txn(struct stream *s) |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5074 | { |
| 5075 | struct http_txn *txn = s->txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5076 | |
| 5077 | /* these ones will have been dynamically allocated */ |
| 5078 | pool_free(pool_head_requri, txn->uri); |
| 5079 | pool_free(pool_head_capture, txn->cli_cookie); |
| 5080 | pool_free(pool_head_capture, txn->srv_cookie); |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5081 | pool_free(pool_head_uniqueid, s->unique_id.ptr); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5082 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5083 | s->unique_id = IST_NULL; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5084 | txn->uri = NULL; |
| 5085 | txn->srv_cookie = NULL; |
| 5086 | txn->cli_cookie = NULL; |
| 5087 | |
Christopher Faulet | 5939925 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 5088 | if (!LIST_ISEMPTY(&s->vars_txn.head)) |
| 5089 | vars_prune(&s->vars_txn, s->sess, s); |
| 5090 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 5091 | vars_prune(&s->vars_reqres, s->sess, s); |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5092 | |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 5093 | b_free(&txn->l7_buffer); |
| 5094 | |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5095 | pool_free(pool_head_http_txn, txn); |
| 5096 | s->txn = NULL; |
Christopher Faulet | 5939925 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 5097 | } |
| 5098 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5099 | |
Christopher Faulet | 71236de | 2023-01-13 09:06:38 +0100 | [diff] [blame] | 5100 | void http_set_term_flags(struct stream *s) |
| 5101 | { |
| 5102 | if (!(s->flags & SF_ERR_MASK)) |
| 5103 | s->flags |= SF_ERR_PRXCOND; |
| 5104 | |
| 5105 | if (!(s->flags & SF_FINST_MASK)) { |
| 5106 | if (s->scb->state == SC_ST_INI) { |
| 5107 | /* Before any connection attempt on the server side, we |
| 5108 | * are still in the request analysis. Just take case to |
| 5109 | * detect tarpit error |
| 5110 | */ |
| 5111 | if (s->req.analysers & AN_REQ_HTTP_TARPIT) |
| 5112 | s->flags |= SF_FINST_T; |
| 5113 | else |
| 5114 | s->flags |= SF_FINST_R; |
| 5115 | } |
| 5116 | else if (s->scb->state == SC_ST_QUE) |
| 5117 | s->flags |= SF_FINST_Q; |
| 5118 | 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)) { |
| 5119 | if (unlikely(objt_applet(s->target))) { |
| 5120 | s->flags |= SF_FINST_R; |
| 5121 | } |
| 5122 | else |
| 5123 | s->flags |= SF_FINST_C; |
| 5124 | } |
| 5125 | else { |
| 5126 | if (s->txn->rsp.msg_state < HTTP_MSG_DATA) { |
| 5127 | /* We are still processing the response headers */ |
| 5128 | s->flags |= SF_FINST_H; |
| 5129 | } |
Christopher Faulet | 5b74f99 | 2023-01-26 19:02:07 +0100 | [diff] [blame] | 5130 | // (res == (done|closing|closed)) & (res->flags & shutw) |
| 5131 | 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] | 5132 | (s->flags & (SF_ERR_CLITO|SF_ERR_CLICL))) { |
| 5133 | /* A client error was reported and we are |
| 5134 | * transmitting the last block of data |
| 5135 | */ |
| 5136 | s->flags |= SF_FINST_L; |
| 5137 | } |
| 5138 | else { |
| 5139 | /* Otherwise we are in DATA phase on both sides */ |
| 5140 | s->flags |= SF_FINST_D; |
| 5141 | } |
| 5142 | } |
| 5143 | } |
| 5144 | } |
| 5145 | |
| 5146 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5147 | DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5148 | |
Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 5149 | /* |
| 5150 | * Local variables: |
| 5151 | * c-indent-level: 8 |
| 5152 | * c-basic-offset: 8 |
| 5153 | * End: |
| 5154 | */ |