Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
| 4 | * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Willy Tarreau | dcc048a | 2020-06-04 19:11:43 +0200 | [diff] [blame] | 13 | #include <haproxy/acl.h> |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 14 | #include <haproxy/action-t.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 15 | #include <haproxy/api.h> |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 16 | #include <haproxy/applet.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 17 | #include <haproxy/backend.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 18 | #include <haproxy/base64.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 19 | #include <haproxy/capture-t.h> |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 20 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 21 | #include <haproxy/channel.h> |
Willy Tarreau | 4aa573d | 2020-06-04 18:21:56 +0200 | [diff] [blame] | 22 | #include <haproxy/check.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 23 | #include <haproxy/connection.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 24 | #include <haproxy/errors.h> |
Willy Tarreau | c7babd8 | 2020-06-04 21:29:29 +0200 | [diff] [blame] | 25 | #include <haproxy/filters.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 26 | #include <haproxy/http.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 27 | #include <haproxy/http_ana.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 28 | #include <haproxy/http_htx.h> |
Aurelien DARRAGON | b2bb925 | 2022-12-28 15:37:57 +0100 | [diff] [blame] | 29 | #include <haproxy/http_ext.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 30 | #include <haproxy/htx.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 31 | #include <haproxy/log.h> |
Willy Tarreau | 6131d6a | 2020-06-02 16:48:09 +0200 | [diff] [blame] | 32 | #include <haproxy/net_helper.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 33 | #include <haproxy/proxy.h> |
Willy Tarreau | 7cd8b6e | 2020-06-02 17:32:26 +0200 | [diff] [blame] | 34 | #include <haproxy/regex.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 35 | #include <haproxy/sc_strm.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 36 | #include <haproxy/server-t.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 37 | #include <haproxy/stats.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 38 | #include <haproxy/stconn.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 39 | #include <haproxy/stream.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 40 | #include <haproxy/trace.h> |
Willy Tarreau | 8c42b8a | 2020-06-04 19:27:34 +0200 | [diff] [blame] | 41 | #include <haproxy/uri_auth-t.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 42 | #include <haproxy/vars.h> |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 43 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 44 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 45 | #define TRACE_SOURCE &trace_strm |
| 46 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 47 | extern const char *stat_status_codes[]; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 48 | |
Willy Tarreau | ff88270 | 2021-04-10 17:23:00 +0200 | [diff] [blame] | 49 | struct pool_head *pool_head_requri __read_mostly = NULL; |
| 50 | struct pool_head *pool_head_capture __read_mostly = NULL; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 51 | |
| 52 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 53 | static void http_end_request(struct stream *s); |
| 54 | static void http_end_response(struct stream *s); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 55 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 56 | static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr); |
| 57 | static int http_del_hdr_value(char *start, char *end, char **from, char *next); |
| 58 | static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len); |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 59 | static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl); |
| 60 | static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 61 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 62 | static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s); |
| 63 | static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s); |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 64 | static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 65 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 66 | static void http_manage_client_side_cookies(struct stream *s, struct channel *req); |
| 67 | static void http_manage_server_side_cookies(struct stream *s, struct channel *res); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 68 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 69 | static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend); |
| 70 | static int http_handle_stats(struct stream *s, struct channel *req); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 71 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 72 | static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg); |
| 73 | static int http_reply_100_continue(struct stream *s); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 74 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 75 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 76 | * processing can continue on next analysers, or zero if it either needs more |
| 77 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 78 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers |
| 79 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 80 | * abort. |
| 81 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 82 | int http_wait_for_request(struct stream *s, struct channel *req, int an_bit) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 83 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 84 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 85 | /* |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 86 | * We will analyze a complete HTTP request to check the its syntax. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 87 | * |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 88 | * Once the start line and all headers are received, we may perform a |
| 89 | * capture of the error (if any), and we will set a few fields. We also |
| 90 | * check for monitor-uri, logging and finally headers capture. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 91 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 92 | struct session *sess = s->sess; |
| 93 | struct http_txn *txn = s->txn; |
| 94 | struct http_msg *msg = &txn->req; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 95 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 96 | struct htx_sl *sl; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 97 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 98 | 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] | 99 | |
Christopher Faulet | da46a0d | 2021-01-21 17:32:58 +0100 | [diff] [blame] | 100 | if (unlikely(!IS_HTX_STRM(s))) { |
| 101 | /* It is only possible when a TCP stream is upgrade to HTTP. |
| 102 | * There is a transition period during which there is no |
| 103 | * data. The stream is still in raw mode and SF_IGNORE flag is |
| 104 | * still set. When this happens, the new mux is responsible to |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 105 | * handle all errors. Thus we may leave immediately. |
Christopher Faulet | da46a0d | 2021-01-21 17:32:58 +0100 | [diff] [blame] | 106 | */ |
| 107 | BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 108 | |
Christopher Faulet | 97b3a61 | 2021-03-15 17:10:12 +0100 | [diff] [blame] | 109 | /* Don't connect for now */ |
| 110 | channel_dont_connect(req); |
| 111 | |
| 112 | /* A SHUTR at this stage means we are performing a "destructive" |
| 113 | * HTTP upgrade (TCP>H2). In this case, we can leave. |
| 114 | */ |
| 115 | if (req->flags & CF_SHUTR) { |
| 116 | s->logs.logwait = 0; |
| 117 | s->logs.level = 0; |
| 118 | channel_abort(&s->req); |
| 119 | channel_abort(&s->res); |
| 120 | req->analysers &= AN_REQ_FLT_END; |
| 121 | req->analyse_exp = TICK_ETERNITY; |
| 122 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s); |
| 123 | return 1; |
| 124 | } |
Christopher Faulet | da46a0d | 2021-01-21 17:32:58 +0100 | [diff] [blame] | 125 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | htx = htxbuf(&req->buf); |
Christopher Faulet | 8bebd2f | 2020-10-06 17:54:56 +0200 | [diff] [blame] | 130 | |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 131 | /* Parsing errors are caught here */ |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 132 | if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) { |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 133 | stream_inc_http_req_ctr(s); |
Emeric Brun | 2897644 | 2020-10-07 08:50:09 +0200 | [diff] [blame] | 134 | proxy_inc_fe_req_ctr(sess->listener, sess->fe); |
Christopher Faulet | bf7175f | 2021-02-10 14:58:01 +0100 | [diff] [blame] | 135 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
| 136 | stream_inc_http_err_ctr(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 137 | goto return_bad_req; |
Christopher Faulet | bf7175f | 2021-02-10 14:58:01 +0100 | [diff] [blame] | 138 | } |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 139 | else |
| 140 | goto return_int_err; |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 143 | /* 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] | 144 | s->srv_error = http_return_srv_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 145 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 146 | msg->msg_state = HTTP_MSG_BODY; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 147 | stream_inc_http_req_ctr(s); |
Emeric Brun | 2897644 | 2020-10-07 08:50:09 +0200 | [diff] [blame] | 148 | proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 149 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 150 | /* kill the pending keep-alive timeout */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 151 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 152 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 153 | BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 154 | sl = http_get_stline(htx); |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 155 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 156 | /* 0: we might have to print this header in debug mode */ |
| 157 | if (unlikely((global.mode & MODE_DEBUG) && |
| 158 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 159 | int32_t pos; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 160 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 161 | http_debug_stline("clireq", s, sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 162 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 163 | 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] | 164 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 165 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 166 | |
| 167 | if (type == HTX_BLK_EOH) |
| 168 | break; |
| 169 | if (type != HTX_BLK_HDR) |
| 170 | continue; |
| 171 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 172 | http_debug_hdr("clihdr", s, |
| 173 | htx_get_blk_name(htx, blk), |
| 174 | htx_get_blk_value(htx, blk)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 175 | } |
| 176 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 177 | |
| 178 | /* |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 179 | * 1: identify the method and the version. Also set HTTP flags |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 180 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 181 | txn->meth = sl->info.req.meth; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 182 | if (sl->flags & HTX_SL_F_VER_11) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 183 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 184 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 185 | if (sl->flags & HTX_SL_F_CLEN) |
| 186 | msg->flags |= HTTP_MSGF_CNT_LEN; |
| 187 | else if (sl->flags & HTX_SL_F_CHNK) |
| 188 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Christopher Faulet | b2db4fa | 2018-11-27 16:51:09 +0100 | [diff] [blame] | 189 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 190 | msg->flags |= HTTP_MSGF_BODYLESS; |
Christopher Faulet | 576c358 | 2021-01-08 15:53:01 +0100 | [diff] [blame] | 191 | if (sl->flags & HTX_SL_F_CONN_UPG) |
| 192 | msg->flags |= HTTP_MSGF_CONN_UPG; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 193 | |
| 194 | /* we can make use of server redirect on GET and HEAD */ |
| 195 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 196 | s->flags |= SF_REDIRECTABLE; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 197 | 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] | 198 | /* PRI is reserved for the HTTP/2 preface */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 199 | goto return_bad_req; |
| 200 | } |
| 201 | |
| 202 | /* |
Christopher Faulet | 6072beb | 2020-02-18 15:34:58 +0100 | [diff] [blame] | 203 | * 2: check if the URI matches the monitor_uri. We have to do this for |
| 204 | * every request which gets in, because the monitor-uri is defined by |
| 205 | * the frontend. If the monitor-uri starts with a '/', the matching is |
| 206 | * done against the request's path. Otherwise, the request's uri is |
| 207 | * used. It is a workaround to let HTTP/2 health-checks work as |
| 208 | * expected. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 209 | */ |
Tim Duesterhus | 4b1fcaa | 2022-03-05 00:52:40 +0100 | [diff] [blame] | 210 | if (unlikely(isttest(sess->fe->monitor_uri))) { |
| 211 | const struct ist monitor_uri = sess->fe->monitor_uri; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 212 | 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] | 213 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 214 | if ((istptr(monitor_uri)[0] == '/' && |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 215 | isteq(http_parse_path(&parser), monitor_uri)) || |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 216 | isteq(htx_sl_req_uri(sl), monitor_uri)) { |
| 217 | /* |
| 218 | * We have found the monitor URI |
| 219 | */ |
| 220 | struct acl_cond *cond; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 221 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 222 | s->flags |= SF_MONITOR; |
| 223 | _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req); |
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 | /* Check if we want to fail this monitor request or not */ |
| 226 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
| 227 | 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] | 228 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 229 | ret = acl_pass(ret); |
| 230 | if (cond->pol == ACL_COND_UNLESS) |
| 231 | ret = !ret; |
| 232 | |
| 233 | if (ret) { |
| 234 | /* we fail this request, let's return 503 service unavail */ |
| 235 | txn->status = 503; |
| 236 | if (!(s->flags & SF_ERR_MASK)) |
| 237 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
| 238 | goto return_prx_cond; |
| 239 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 240 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 241 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 242 | /* nothing to fail, let's reply normally */ |
| 243 | txn->status = 200; |
| 244 | if (!(s->flags & SF_ERR_MASK)) |
| 245 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
| 246 | goto return_prx_cond; |
| 247 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /* |
| 251 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 252 | * Note: we cannot log anymore if the request has been |
| 253 | * classified as invalid. |
| 254 | */ |
| 255 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 256 | /* we have a complete HTTP request that we must log */ |
| 257 | if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 258 | size_t len; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 259 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 260 | 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] | 261 | txn->uri[len] = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 262 | |
| 263 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
| 264 | s->do_log(s); |
| 265 | } else { |
| 266 | ha_alert("HTTP logging : out of memory.\n"); |
| 267 | } |
| 268 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 269 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 270 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 271 | * we have what looks like a proxied connection instead of a connection, |
| 272 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 273 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 274 | * happen to do that despite being non-standard :-( |
| 275 | * We consider that a request not beginning with either '/' or '*' is |
| 276 | * a proxied connection, which covers both "scheme://location" and |
| 277 | * CONNECT ip:port. |
| 278 | */ |
| 279 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 280 | *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*') |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 281 | txn->flags |= TX_USE_PX_CONN; |
| 282 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 283 | /* 5: we may need to capture headers */ |
| 284 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 285 | http_capture_headers(htx, s->req_cap, sess->fe->req_cap); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 286 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 287 | /* we may have to wait for the request's body */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 288 | if (s->be->options & PR_O_WREQ_BODY) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 289 | req->analysers |= AN_REQ_HTTP_BODY; |
| 290 | |
| 291 | /* |
| 292 | * RFC7234#4: |
| 293 | * A cache MUST write through requests with methods |
| 294 | * that are unsafe (Section 4.2.1 of [RFC7231]) to |
| 295 | * the origin server; i.e., a cache is not allowed |
| 296 | * to generate a reply to such a request before |
| 297 | * having forwarded the request and having received |
| 298 | * a corresponding response. |
| 299 | * |
| 300 | * RFC7231#4.2.1: |
| 301 | * Of the request methods defined by this |
| 302 | * specification, the GET, HEAD, OPTIONS, and TRACE |
| 303 | * methods are defined to be safe. |
| 304 | */ |
| 305 | if (likely(txn->meth == HTTP_METH_GET || |
| 306 | txn->meth == HTTP_METH_HEAD || |
| 307 | txn->meth == HTTP_METH_OPTIONS || |
| 308 | txn->meth == HTTP_METH_TRACE)) |
| 309 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 310 | |
| 311 | /* end of job, return OK */ |
| 312 | req->analysers &= ~an_bit; |
| 313 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 314 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 315 | 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] | 316 | return 1; |
| 317 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 318 | return_int_err: |
| 319 | txn->status = 500; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 320 | if (!(s->flags & SF_ERR_MASK)) |
| 321 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 322 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 323 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 324 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 325 | goto return_prx_cond; |
| 326 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 327 | return_bad_req: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 328 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 329 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 330 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 331 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 332 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 333 | |
| 334 | return_prx_cond: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 335 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 336 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 337 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 338 | DBG_TRACE_DEVEL("leaving on error", |
| 339 | 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] | 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | /* This stream analyser runs all HTTP request processing which is common to |
| 345 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 346 | * reqadd, stats and redirects. This is performed for the designated proxy. |
| 347 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 348 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 349 | * error, ...). |
| 350 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 351 | 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] | 352 | { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 353 | struct list *def_rules, *rules; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 354 | struct session *sess = s->sess; |
| 355 | struct http_txn *txn = s->txn; |
| 356 | struct http_msg *msg = &txn->req; |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 357 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 358 | struct redirect_rule *rule; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 359 | enum rule_result verdict; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 360 | struct connection *conn = objt_conn(sess->origin); |
| 361 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 362 | 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] | 363 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 364 | htx = htxbuf(&req->buf); |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 365 | |
Christopher Faulet | 1907ccc | 2019-04-29 13:12:02 +0200 | [diff] [blame] | 366 | /* just in case we have some per-backend tracking. Only called the first |
| 367 | * execution of the analyser. */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 368 | if (!s->current_rule && !s->current_rule_list) |
Christopher Faulet | 1907ccc | 2019-04-29 13:12:02 +0200 | [diff] [blame] | 369 | stream_inc_be_http_req_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 370 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 371 | def_rules = ((px->defpx && (an_bit == AN_REQ_HTTP_PROCESS_FE || px != sess->fe)) ? &px->defpx->http_req_rules : NULL); |
| 372 | rules = &px->http_req_rules; |
| 373 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 374 | /* evaluate http-request rules */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 375 | if ((def_rules && !LIST_ISEMPTY(def_rules)) || !LIST_ISEMPTY(rules)) { |
| 376 | verdict = http_req_get_intercept_rule(px, def_rules, rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 377 | |
| 378 | switch (verdict) { |
| 379 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 380 | goto return_prx_yield; |
| 381 | |
| 382 | case HTTP_RULE_RES_CONT: |
| 383 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 384 | break; |
| 385 | |
| 386 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 387 | if (txn->flags & TX_CLTARPIT) |
| 388 | goto tarpit; |
| 389 | goto deny; |
| 390 | |
| 391 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 392 | goto return_prx_cond; |
| 393 | |
| 394 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
| 395 | goto done; |
| 396 | |
| 397 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 398 | goto return_bad_req; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 399 | |
| 400 | case HTTP_RULE_RES_ERROR: /* failed with a bad request */ |
| 401 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 405 | if (px->options2 & (PR_O2_RSTRICT_REQ_HDR_NAMES_BLK|PR_O2_RSTRICT_REQ_HDR_NAMES_DEL)) { |
| 406 | verdict = http_req_restrict_header_names(s, htx, px); |
| 407 | if (verdict == HTTP_RULE_RES_DENY) |
| 408 | goto deny; |
| 409 | } |
| 410 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 411 | if (conn && (conn->flags & CO_FL_EARLY_DATA) && |
Olivier Houchard | 220a26c | 2020-01-23 14:57:36 +0100 | [diff] [blame] | 412 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) { |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 413 | struct http_hdr_ctx ctx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 414 | |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 415 | ctx.blk = NULL; |
| 416 | if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) { |
| 417 | if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1")))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 418 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 419 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /* OK at this stage, we know that the request was accepted according to |
| 423 | * the http-request rules, we can check for the stats. Note that the |
| 424 | * URI is detected *before* the req* rules in order not to be affected |
| 425 | * by a possible reqrep, while they are processed *after* so that a |
| 426 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 427 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 428 | if (!s->target && http_stats_check_uri(s, txn, px)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 429 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | a0b58b5 | 2022-05-27 08:33:53 +0200 | [diff] [blame] | 430 | if (unlikely(!sc_applet_create(s->scb, objt_applet(s->target)))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 431 | s->logs.tv_request = now; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 432 | if (!(s->flags & SF_ERR_MASK)) |
| 433 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 434 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /* parse the whole stats request and extract the relevant information */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 438 | http_handle_stats(s, req); |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 439 | 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] | 440 | /* not all actions implemented: deny, allow, auth */ |
| 441 | |
| 442 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 443 | goto deny; |
| 444 | |
| 445 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 446 | goto return_prx_cond; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 447 | |
| 448 | if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */ |
| 449 | goto return_bad_req; |
| 450 | |
| 451 | if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */ |
| 452 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 453 | } |
| 454 | |
Christopher Faulet | 2571bc6 | 2019-03-01 11:44:26 +0100 | [diff] [blame] | 455 | /* Proceed with the applets now. */ |
| 456 | if (unlikely(objt_applet(s->target))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 457 | 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] | 458 | _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 459 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 460 | if (http_handle_expect_hdr(s, htx, msg) == -1) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 461 | goto return_int_err; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 462 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 463 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 464 | 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] | 465 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 466 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 467 | if (HAS_FILTERS(s)) |
| 468 | req->analysers |= AN_REQ_FLT_HTTP_HDRS; |
| 469 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 470 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
| 471 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 472 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 473 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 474 | |
| 475 | req->flags |= CF_SEND_DONTWAIT; |
| 476 | s->flags |= SF_ASSIGNED; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 477 | goto done; |
| 478 | } |
| 479 | |
| 480 | /* check whether we have some ACLs set to redirect this request */ |
| 481 | list_for_each_entry(rule, &px->redirect_rules, list) { |
| 482 | if (rule->cond) { |
| 483 | int ret; |
| 484 | |
| 485 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 486 | ret = acl_pass(ret); |
| 487 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 488 | ret = !ret; |
| 489 | if (!ret) |
| 490 | continue; |
| 491 | } |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 492 | if (!http_apply_redirect_rule(rule, s, txn)) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 493 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 494 | goto done; |
| 495 | } |
| 496 | |
| 497 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 498 | * If this happens, then the data will not come immediately, so we must |
| 499 | * send all what we have without waiting. Note that due to the small gain |
| 500 | * in waiting for the body of the request, it's easier to simply put the |
| 501 | * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 502 | * itself once used. |
| 503 | */ |
| 504 | req->flags |= CF_SEND_DONTWAIT; |
| 505 | |
| 506 | done: /* done with this analyser, continue with next ones that the calling |
| 507 | * points will have set, if any. |
| 508 | */ |
| 509 | req->analyse_exp = TICK_ETERNITY; |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 510 | 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] | 511 | req->analysers &= ~an_bit; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 512 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 513 | 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] | 514 | return 1; |
| 515 | |
| 516 | tarpit: |
| 517 | /* Allow cookie logging |
| 518 | */ |
| 519 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 520 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 521 | |
| 522 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 523 | * which may be the same as the connect timeout if unspecified. |
| 524 | * If unset, then set it to zero because we really want it to |
| 525 | * eventually expire. We build the tarpit as an analyser. |
| 526 | */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 527 | channel_htx_erase(&s->req, htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 528 | |
| 529 | /* wipe the request out so that we can drop the connection early |
| 530 | * if the client closes first. |
| 531 | */ |
| 532 | channel_dont_connect(req); |
| 533 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 534 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
| 535 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 536 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 537 | if (!req->analyse_exp) |
| 538 | req->analyse_exp = tick_add(now_ms, 0); |
| 539 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 540 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 541 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 542 | _HA_ATOMIC_INC(&s->be->be_counters.denied_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 543 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 544 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 545 | goto done_without_exp; |
| 546 | |
| 547 | deny: /* this request was blocked (denied) */ |
| 548 | |
| 549 | /* Allow cookie logging |
| 550 | */ |
| 551 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 552 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 553 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 554 | s->logs.tv_request = now; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 555 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 556 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 557 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 558 | _HA_ATOMIC_INC(&s->be->be_counters.denied_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 559 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 560 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 561 | goto return_prx_err; |
| 562 | |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 563 | return_fail_rewrite: |
| 564 | if (!(s->flags & SF_ERR_MASK)) |
| 565 | s->flags |= SF_ERR_PRXCOND; |
| 566 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
| 567 | if (s->flags & SF_BE_ASSIGNED) |
| 568 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
| 569 | if (sess->listener && sess->listener->counters) |
| 570 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
| 571 | if (objt_server(s->target)) |
| 572 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
| 573 | /* fall through */ |
| 574 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 575 | return_int_err: |
| 576 | txn->status = 500; |
| 577 | if (!(s->flags & SF_ERR_MASK)) |
| 578 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 579 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 580 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 581 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 582 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 583 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 584 | goto return_prx_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 585 | |
| 586 | return_bad_req: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 587 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 588 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 589 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 590 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 591 | /* fall through */ |
| 592 | |
| 593 | return_prx_err: |
| 594 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 595 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 596 | |
| 597 | return_prx_cond: |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 598 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 599 | |
| 600 | req->analysers &= AN_REQ_FLT_END; |
| 601 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 602 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 603 | DBG_TRACE_DEVEL("leaving on error", |
| 604 | 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] | 605 | return 0; |
| 606 | |
| 607 | return_prx_yield: |
| 608 | channel_dont_connect(req); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 609 | DBG_TRACE_DEVEL("waiting for more data", |
| 610 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | /* This function performs all the processing enabled for the current request. |
| 615 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 616 | * needs more data, encounters an error, or wants to immediately abort the |
| 617 | * request. It relies on buffers flags, and updates s->req.analysers. |
| 618 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 619 | 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] | 620 | { |
| 621 | struct session *sess = s->sess; |
| 622 | struct http_txn *txn = s->txn; |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 623 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 624 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
| 625 | |
Christopher Faulet | 8bebd2f | 2020-10-06 17:54:56 +0200 | [diff] [blame] | 626 | 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] | 627 | |
| 628 | /* |
| 629 | * Right now, we know that we have processed the entire headers |
| 630 | * and that unwanted requests have been filtered out. We can do |
| 631 | * whatever we want with the remaining request. Also, now we |
| 632 | * may have separate values for ->fe, ->be. |
| 633 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 634 | htx = htxbuf(&req->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 635 | |
| 636 | /* |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 637 | * 7: Now we can work with the cookies. |
| 638 | * Note that doing so might move headers in the request, but |
| 639 | * the fields will stay coherent and the URI will not move. |
| 640 | * This should only be performed in the backend. |
| 641 | */ |
| 642 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 643 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 644 | |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 645 | /* 8: Generate unique ID if a "unique-id-format" is defined. |
| 646 | * |
| 647 | * A unique ID is generated even when it is not sent to ensure that the ID can make use of |
| 648 | * fetches only available in the HTTP request processing stage. |
| 649 | */ |
| 650 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 651 | 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] | 652 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 653 | if (!isttest(unique_id)) { |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 654 | if (!(s->flags & SF_ERR_MASK)) |
| 655 | s->flags |= SF_ERR_RESOURCE; |
| 656 | goto return_int_err; |
| 657 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 658 | |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 659 | /* send unique ID if a "unique-id-header" is defined */ |
Tim Duesterhus | 0643b0e | 2020-03-05 17:56:35 +0100 | [diff] [blame] | 660 | if (isttest(sess->fe->header_unique_id) && |
Tim Duesterhus | 7ad27d4 | 2022-05-18 00:22:15 +0200 | [diff] [blame] | 661 | unlikely(!http_add_header(htx, sess->fe->header_unique_id, unique_id))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 662 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 663 | } |
| 664 | |
Aurelien DARRAGON | b2bb925 | 2022-12-28 15:37:57 +0100 | [diff] [blame] | 665 | /* add forwarded header (RFC 7239) (ignored for frontends) */ |
| 666 | if (s->be->options & PR_O_HTTP_7239) { |
| 667 | if (unlikely(!http_handle_7239_header(s, req))) |
| 668 | goto return_fail_rewrite; |
| 669 | } |
| 670 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 671 | /* |
Aurelien DARRAGON | 730b983 | 2022-12-28 18:53:05 +0100 | [diff] [blame] | 672 | * add X-Forwarded-For if either the frontend or the backend |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 673 | * asks for it. |
| 674 | */ |
Aurelien DARRAGON | 730b983 | 2022-12-28 18:53:05 +0100 | [diff] [blame] | 675 | if ((sess->fe->options | s->be->options) & PR_O_HTTP_XFF) { |
| 676 | if (unlikely(!http_handle_xff_header(s, req))) |
| 677 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /* |
Aurelien DARRAGON | f958341 | 2022-12-29 15:45:41 +0100 | [diff] [blame] | 681 | * add X-Original-To if either the frontend or the backend |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 682 | * asks for it. |
| 683 | */ |
Aurelien DARRAGON | f958341 | 2022-12-29 15:45:41 +0100 | [diff] [blame] | 684 | if ((sess->fe->options | s->be->options) & PR_O_HTTP_XOT) { |
| 685 | if (unlikely(!http_handle_xot_header(s, req))) |
| 686 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 687 | } |
| 688 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 689 | /* Filter the request headers if there are filters attached to the |
| 690 | * stream. |
| 691 | */ |
| 692 | if (HAS_FILTERS(s)) |
| 693 | req->analysers |= AN_REQ_FLT_HTTP_HDRS; |
| 694 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 695 | /* If we have no server assigned yet and we're balancing on url_param |
| 696 | * with a POST request, we may be interested in checking the body for |
| 697 | * that parameter. This will be done in another analyser. |
| 698 | */ |
| 699 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | 089eaa0 | 2019-01-14 15:17:46 +0100 | [diff] [blame] | 700 | s->txn->meth == HTTP_METH_POST && |
| 701 | (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 702 | channel_dont_connect(req); |
| 703 | req->analysers |= AN_REQ_HTTP_BODY; |
| 704 | } |
| 705 | |
| 706 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 707 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 708 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 709 | /* We expect some data from the client. Unless we know for sure |
| 710 | * we already have a full request, we have to re-enable quick-ack |
| 711 | * in case we previously disabled it, otherwise we might cause |
| 712 | * the client to delay further data. |
| 713 | */ |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 714 | if ((sess->listener && (sess->listener->options & LI_O_NOQUICKACK)) && !(htx->flags & HTX_FL_EOM)) |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 715 | conn_set_quickack(cli_conn, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 716 | |
| 717 | /************************************************************* |
| 718 | * OK, that's finished for the headers. We have done what we * |
| 719 | * could. Let's switch to the DATA state. * |
| 720 | ************************************************************/ |
| 721 | req->analyse_exp = TICK_ETERNITY; |
| 722 | req->analysers &= ~an_bit; |
| 723 | |
| 724 | s->logs.tv_request = now; |
| 725 | /* OK let's go on with the BODY now */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 726 | 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] | 727 | return 1; |
| 728 | |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 729 | return_fail_rewrite: |
| 730 | if (!(s->flags & SF_ERR_MASK)) |
| 731 | s->flags |= SF_ERR_PRXCOND; |
| 732 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
| 733 | if (s->flags & SF_BE_ASSIGNED) |
| 734 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
| 735 | if (sess->listener && sess->listener->counters) |
| 736 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
| 737 | if (objt_server(s->target)) |
| 738 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
| 739 | /* fall through */ |
| 740 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 741 | return_int_err: |
| 742 | txn->status = 500; |
| 743 | if (!(s->flags & SF_ERR_MASK)) |
| 744 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 745 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 746 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 747 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 748 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 749 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 750 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 751 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 752 | http_set_term_flags(s); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 753 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 754 | DBG_TRACE_DEVEL("leaving on error", |
| 755 | 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] | 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 760 | * returns zero, at the beginning because it prevents any other processing |
| 761 | * from occurring, and at the end because it terminates the request. |
| 762 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 763 | 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] | 764 | { |
| 765 | struct http_txn *txn = s->txn; |
| 766 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 767 | 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] | 768 | /* This connection is being tarpitted. The CLIENT side has |
| 769 | * already set the connect expiration date to the right |
| 770 | * timeout. We just have to check that the client is still |
| 771 | * there and that the timeout has not expired. |
| 772 | */ |
| 773 | channel_dont_connect(req); |
Christopher Faulet | 4b490b7 | 2023-01-04 11:55:15 +0100 | [diff] [blame] | 774 | if (!(req->flags & CF_SHUTR) && |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 775 | !tick_is_expired(req->analyse_exp, now_ms)) { |
Christopher Faulet | b0c87f1 | 2021-10-29 14:37:07 +0200 | [diff] [blame] | 776 | /* Be sure to drain all data from the request channel */ |
| 777 | channel_htx_erase(req, htxbuf(&req->buf)); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 778 | DBG_TRACE_DEVEL("waiting for tarpit timeout expiry", |
| 779 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 780 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 781 | } |
| 782 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 783 | |
| 784 | /* We will set the queue timer to the time spent, just for |
| 785 | * logging purposes. We fake a 500 server error, so that the |
| 786 | * attacker will not suspect his connection has been tarpitted. |
| 787 | * It will not cause trouble to the logs because we can exclude |
| 788 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 789 | */ |
| 790 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 791 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 792 | http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL)); |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 793 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 794 | |
| 795 | 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] | 796 | return 0; |
| 797 | } |
| 798 | |
| 799 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 800 | * for either the buffer to be full, or the full advertised contents to have |
| 801 | * reached the buffer. It must only be called after the standard HTTP request |
| 802 | * processing has occurred, because it expects the request to be parsed and will |
| 803 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 804 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 805 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 806 | * needs to read more data, or 1 once it has completed its analysis. |
| 807 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 808 | 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] | 809 | { |
| 810 | struct session *sess = s->sess; |
| 811 | struct http_txn *txn = s->txn; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 812 | |
Christopher Faulet | f4569bb | 2023-01-13 10:49:31 +0100 | [diff] [blame] | 813 | 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] | 814 | |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 815 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 816 | switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) { |
| 817 | case HTTP_RULE_RES_CONT: |
| 818 | goto http_end; |
| 819 | case HTTP_RULE_RES_YIELD: |
| 820 | goto missing_data_or_waiting; |
| 821 | case HTTP_RULE_RES_BADREQ: |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 822 | goto return_bad_req; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 823 | case HTTP_RULE_RES_ERROR: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 824 | goto return_int_err; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 825 | case HTTP_RULE_RES_ABRT: |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 826 | goto return_prx_cond; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 827 | default: |
| 828 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | http_end: |
| 832 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 833 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 834 | req->analysers &= ~an_bit; |
| 835 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 836 | 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] | 837 | return 1; |
| 838 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 839 | missing_data_or_waiting: |
| 840 | channel_dont_connect(req); |
| 841 | DBG_TRACE_DEVEL("waiting for more data", |
| 842 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 843 | return 0; |
| 844 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 845 | return_int_err: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 846 | txn->status = 500; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 847 | if (!(s->flags & SF_ERR_MASK)) |
| 848 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 849 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 850 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 851 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 852 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 853 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 854 | goto return_prx_err; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 855 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 856 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 857 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 858 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 859 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 860 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 861 | /* fall through */ |
| 862 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 863 | return_prx_err: |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 864 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 865 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 866 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 867 | return_prx_cond: |
Christopher Faulet | f0d80df | 2023-01-13 10:20:20 +0100 | [diff] [blame] | 868 | http_set_term_flags(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 869 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 870 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 871 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 872 | DBG_TRACE_DEVEL("leaving on error", |
| 873 | 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] | 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | /* This function is an analyser which forwards request body (including chunk |
| 878 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 879 | * zero byte. The only situation where it must not be called is when we're in |
| 880 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 881 | * remaining data and to resync after end of body. It expects the msg_state to |
| 882 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 883 | * read more data, or 1 once we can go on with next request or end the stream. |
| 884 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
| 885 | * bytes of pending data + the headers if not already done. |
| 886 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 887 | 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] | 888 | { |
| 889 | struct session *sess = s->sess; |
| 890 | struct http_txn *txn = s->txn; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 891 | struct http_msg *msg = &txn->req; |
| 892 | struct htx *htx; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 893 | short status = 0; |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 894 | int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 895 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 896 | 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] | 897 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 898 | htx = htxbuf(&req->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 899 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 900 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 901 | goto return_bad_req; |
| 902 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 903 | goto return_int_err; |
| 904 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 905 | /* Note that we don't have to send 100-continue back because we don't |
| 906 | * need the data to complete our job, and it's up to the server to |
| 907 | * decide whether to return 100, 417 or anything else in return of |
| 908 | * an "Expect: 100-continue" header. |
| 909 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 910 | if (msg->msg_state == HTTP_MSG_BODY) |
| 911 | msg->msg_state = HTTP_MSG_DATA; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 912 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 913 | /* in most states, we should abort in case of early close */ |
| 914 | channel_auto_close(req); |
| 915 | |
| 916 | if (req->to_forward) { |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 917 | if (req->to_forward == CHN_INFINITE_FORWARD) { |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 918 | if (req->flags & CF_EOI) |
| 919 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 920 | } |
| 921 | else { |
| 922 | /* We can't process the buffer's contents yet */ |
| 923 | req->flags |= CF_WAKE_WRITE; |
| 924 | goto missing_data_or_waiting; |
| 925 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 926 | } |
| 927 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 928 | if (msg->msg_state >= HTTP_MSG_ENDING) |
| 929 | goto ending; |
| 930 | |
| 931 | if (txn->meth == HTTP_METH_CONNECT) { |
| 932 | msg->msg_state = HTTP_MSG_ENDING; |
| 933 | goto ending; |
| 934 | } |
| 935 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 936 | /* Forward input data. We get it by removing all outgoing data not |
| 937 | * forwarded yet from HTX data size. If there are some data filters, we |
| 938 | * let them decide the amount of data to forward. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 939 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 940 | if (HAS_REQ_DATA_FILTERS(s)) { |
| 941 | ret = flt_http_payload(s, msg, htx->data); |
| 942 | if (ret < 0) |
| 943 | goto return_bad_req; |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 944 | c_adv(req, ret); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 945 | } |
| 946 | else { |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 947 | c_adv(req, htx->data - co_data(req)); |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 948 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
| 949 | channel_htx_forward_forever(req, htx); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 950 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 951 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 952 | if (htx->data != co_data(req)) |
| 953 | goto missing_data_or_waiting; |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 954 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 955 | /* 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] | 956 | * in HTTP_MSG_ENDING state. Then if all data was marked to be |
| 957 | * forwarded, set the state to HTTP_MSG_DONE. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 958 | */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 959 | if (!(htx->flags & HTX_FL_EOM)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 960 | goto missing_data_or_waiting; |
| 961 | |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 962 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 963 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 964 | ending: |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 965 | req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */ |
| 966 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 967 | /* other states, ENDING...TUNNEL */ |
| 968 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 969 | goto done; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 970 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 971 | if (HAS_REQ_DATA_FILTERS(s)) { |
| 972 | ret = flt_http_end(s, msg); |
| 973 | if (ret <= 0) { |
| 974 | if (!ret) |
| 975 | goto missing_data_or_waiting; |
| 976 | goto return_bad_req; |
| 977 | } |
| 978 | } |
| 979 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 980 | if (txn->meth == HTTP_METH_CONNECT) |
| 981 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 982 | else { |
| 983 | msg->msg_state = HTTP_MSG_DONE; |
| 984 | req->to_forward = 0; |
| 985 | } |
| 986 | |
| 987 | done: |
| 988 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 989 | if (!(txn->flags & TX_CON_WANT_TUN)) |
| 990 | channel_dont_close(req); |
| 991 | |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 992 | if ((req->flags & CF_SHUTW) && co_data(req)) { |
| 993 | /* request errors are most likely due to the server aborting the |
| 994 | * transfer. */ |
| 995 | goto return_srv_abort; |
| 996 | } |
| 997 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 998 | http_end_request(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 999 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1000 | 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] | 1001 | return 1; |
| 1002 | } |
| 1003 | |
| 1004 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 1005 | * the client's connection and forward any shutdown notification to the |
| 1006 | * server, which will decide whether to close or to go on processing the |
| 1007 | * request. We only do that in tunnel mode, and not in other modes since |
| 1008 | * it can be abused to exhaust source ports. */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 1009 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1010 | channel_auto_read(req); |
Christopher Faulet | 446d803 | 2022-12-12 07:53:18 +0100 | [diff] [blame] | 1011 | if ((req->flags & CF_SHUTR) && !(txn->flags & TX_CON_WANT_TUN)) |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1012 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1013 | channel_auto_close(req); |
| 1014 | } |
| 1015 | else if (s->txn->meth == HTTP_METH_POST) { |
| 1016 | /* POST requests may require to read extra CRLF sent by broken |
| 1017 | * browsers and which could cause an RST to be sent upon close |
| 1018 | * on some systems (eg: Linux). */ |
| 1019 | channel_auto_read(req); |
| 1020 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1021 | DBG_TRACE_DEVEL("waiting for the end of the HTTP txn", |
| 1022 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1023 | return 0; |
| 1024 | |
| 1025 | missing_data_or_waiting: |
| 1026 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1027 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1028 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1029 | |
| 1030 | waiting: |
| 1031 | /* waiting for the last bits to leave the buffer */ |
| 1032 | if (req->flags & CF_SHUTW) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1033 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1034 | |
| 1035 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 1036 | * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE. |
| 1037 | * And when content-length is used, we never want to let the possible |
| 1038 | * shutdown be forwarded to the other side, as the state machine will |
| 1039 | * take care of it once the client responds. It's also important to |
| 1040 | * prevent TIME_WAITs from accumulating on the backend side, and for |
| 1041 | * HTTP/2 where the last frame comes with a shutdown. |
| 1042 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1043 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1044 | channel_dont_close(req); |
| 1045 | |
| 1046 | /* We know that more data are expected, but we couldn't send more that |
| 1047 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
| 1048 | * system knows it must not set a PUSH on this first part. Interactive |
| 1049 | * modes are already handled by the stream sock layer. We must not do |
| 1050 | * this in content-length mode because it could present the MSG_MORE |
| 1051 | * flag with the last block of forwarded data, which would cause an |
| 1052 | * additional delay to be observed by the receiver. |
| 1053 | */ |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 1054 | if (HAS_REQ_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1055 | req->flags |= CF_EXPECT_MORE; |
| 1056 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1057 | DBG_TRACE_DEVEL("waiting for more data to forward", |
| 1058 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1059 | return 0; |
| 1060 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1061 | return_cli_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1062 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 1063 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1064 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1065 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1066 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1067 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1068 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 1069 | 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] | 1070 | status = 400; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1071 | goto return_prx_cond; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1072 | |
| 1073 | return_srv_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1074 | _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts); |
| 1075 | _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1076 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1077 | _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1078 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1079 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1080 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 1081 | 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] | 1082 | status = 502; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1083 | goto return_prx_cond; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1084 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1085 | return_int_err: |
| 1086 | if (!(s->flags & SF_ERR_MASK)) |
| 1087 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1088 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 1089 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1090 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1091 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1092 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1093 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1094 | status = 500; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1095 | goto return_prx_cond; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1096 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1097 | return_bad_req: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1098 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1099 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1100 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1101 | status = 400; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1102 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1103 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1104 | return_prx_cond: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1105 | if (txn->status > 0) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1106 | /* 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] | 1107 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1108 | } else { |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1109 | txn->status = status; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1110 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1111 | } |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 1112 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1113 | DBG_TRACE_DEVEL("leaving on error ", |
| 1114 | 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] | 1115 | return 0; |
| 1116 | } |
| 1117 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1118 | /* 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] | 1119 | /* Returns 0 if we can attempt to retry, -1 otherwise */ |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 1120 | static __inline int do_l7_retry(struct stream *s, struct stconn *sc) |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1121 | { |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1122 | struct channel *req, *res; |
| 1123 | int co_data; |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1124 | |
Christopher Faulet | 731c8e6 | 2022-03-29 16:08:44 +0200 | [diff] [blame] | 1125 | s->conn_retries++; |
| 1126 | if (s->conn_retries >= s->be->conn_retries) |
Christopher Faulet | 552601d | 2021-05-26 10:31:06 +0200 | [diff] [blame] | 1127 | return -1; |
Christopher Faulet | 5b82cc5 | 2020-10-12 15:18:50 +0200 | [diff] [blame] | 1128 | |
Christopher Faulet | e763c8c | 2021-05-05 18:23:59 +0200 | [diff] [blame] | 1129 | if (objt_server(s->target)) { |
| 1130 | if (s->flags & SF_CURR_SESS) { |
| 1131 | s->flags &= ~SF_CURR_SESS; |
| 1132 | _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess); |
| 1133 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1134 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries); |
Christopher Faulet | e763c8c | 2021-05-05 18:23:59 +0200 | [diff] [blame] | 1135 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1136 | _HA_ATOMIC_INC(&s->be->be_counters.retries); |
Willy Tarreau | 223995e | 2019-05-04 10:38:31 +0200 | [diff] [blame] | 1137 | |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1138 | req = &s->req; |
| 1139 | res = &s->res; |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1140 | /* Remove any write error from the request, and read error from the response */ |
| 1141 | req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW); |
Christopher Faulet | 6e1bbc4 | 2022-12-12 08:08:15 +0100 | [diff] [blame] | 1142 | res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_EVENT | CF_SHUTR_NOW); |
Christopher Faulet | 7bf46bb | 2022-01-04 10:56:03 +0100 | [diff] [blame] | 1143 | res->analysers &= AN_RES_FLT_END; |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 1144 | s->conn_err_type = STRM_ET_NONE; |
Christopher Faulet | ae024ce | 2022-03-29 19:02:31 +0200 | [diff] [blame] | 1145 | s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK); |
| 1146 | s->conn_exp = TICK_ETERNITY; |
Olivier Houchard | 4bd5867 | 2019-07-12 16:16:59 +0200 | [diff] [blame] | 1147 | stream_choose_redispatch(s); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1148 | res->rex = TICK_ETERNITY; |
| 1149 | res->to_forward = 0; |
| 1150 | res->analyse_exp = TICK_ETERNITY; |
| 1151 | res->total = 0; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 1152 | |
Willy Tarreau | 19c65a9 | 2022-05-27 08:49:24 +0200 | [diff] [blame] | 1153 | if (sc_reset_endp(s->scb) < 0) { |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 1154 | if (!(s->flags & SF_ERR_MASK)) |
| 1155 | s->flags |= SF_ERR_INTERNAL; |
| 1156 | return -1; |
| 1157 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1158 | |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1159 | b_free(&req->buf); |
| 1160 | /* Swap the L7 buffer with the channel buffer */ |
| 1161 | /* 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] | 1162 | co_data = b_data(&s->txn->l7_buffer); |
| 1163 | b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer)); |
| 1164 | 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] | 1165 | co_set_data(req, co_data); |
Christopher Faulet | 5b82cc5 | 2020-10-12 15:18:50 +0200 | [diff] [blame] | 1166 | |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 1167 | 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] | 1168 | |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1169 | b_reset(&res->buf); |
| 1170 | co_set_data(res, 0); |
| 1171 | return 0; |
| 1172 | } |
| 1173 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1174 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 1175 | * processing can continue on next analysers, or zero if it either needs more |
| 1176 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 1177 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers |
| 1178 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1179 | * abort. |
| 1180 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1181 | 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] | 1182 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1183 | /* |
| 1184 | * We will analyze a complete HTTP response to check the its syntax. |
| 1185 | * |
| 1186 | * Once the start line and all headers are received, we may perform a |
| 1187 | * capture of the error (if any), and we will set a few fields. We also |
| 1188 | * logging and finally headers capture. |
| 1189 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1190 | struct session *sess = s->sess; |
| 1191 | struct http_txn *txn = s->txn; |
| 1192 | struct http_msg *msg = &txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1193 | struct htx *htx; |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1194 | struct connection *srv_conn; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1195 | struct htx_sl *sl; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1196 | int n; |
| 1197 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1198 | 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] | 1199 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1200 | htx = htxbuf(&rep->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1201 | |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1202 | /* Parsing errors are caught here */ |
| 1203 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 1204 | goto return_bad_res; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1205 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 1206 | goto return_int_err; |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1207 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1208 | /* |
| 1209 | * Now we quickly check if we have found a full valid response. |
| 1210 | * If not so, we check the FD and buffer states before leaving. |
| 1211 | * A full response is indicated by the fact that we have seen |
| 1212 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
| 1213 | * responses are checked first. |
| 1214 | * |
| 1215 | * Depending on whether the client is still there or not, we |
| 1216 | * may send an error response back or not. Note that normally |
| 1217 | * we should only check for HTTP status there, and check I/O |
| 1218 | * errors somewhere else. |
| 1219 | */ |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1220 | next_one: |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 1221 | if (unlikely(htx_is_empty(htx) || htx->first == -1)) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1222 | /* 1: have we encountered a read error ? */ |
| 1223 | if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 1224 | struct connection *conn = sc_conn(s->scb); |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1225 | |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1226 | /* Perform a L7 retry because server refuses the early data. */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1227 | if ((txn->flags & TX_L7_RETRY) && |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1228 | (s->be->retry_type & PR_RE_EARLY_ERROR) && |
| 1229 | conn && conn->err_code == CO_ER_SSL_EARLY_FAILED && |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1230 | do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1231 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1232 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 1233 | return 0; |
| 1234 | } |
| 1235 | |
Olivier Houchard | 6db1699 | 2019-05-17 15:40:49 +0200 | [diff] [blame] | 1236 | if (txn->flags & TX_NOT_FIRST) |
| 1237 | goto abort_keep_alive; |
| 1238 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1239 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1240 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1241 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1242 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1243 | } |
| 1244 | |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1245 | /* if the server refused the early data, just send a 425 */ |
| 1246 | if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1247 | txn->status = 425; |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1248 | else { |
| 1249 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1250 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1251 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1252 | |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1253 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1254 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1255 | |
| 1256 | if (!(s->flags & SF_ERR_MASK)) |
| 1257 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1258 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1259 | DBG_TRACE_DEVEL("leaving on error", |
| 1260 | 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] | 1261 | return 0; |
| 1262 | } |
| 1263 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1264 | /* 2: read timeout : return a 504 to the client. */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1265 | else if (rep->flags & CF_READ_TIMEOUT) { |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1266 | if ((txn->flags & TX_L7_RETRY) && |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1267 | (s->be->retry_type & PR_RE_TIMEOUT)) { |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1268 | if (co_data(rep) || do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1269 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1270 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1271 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1272 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1273 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1274 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1275 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1276 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1277 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1278 | } |
| 1279 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1280 | txn->status = 504; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1281 | stream_inc_http_fail_ctr(s); |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1282 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1283 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1284 | |
| 1285 | if (!(s->flags & SF_ERR_MASK)) |
| 1286 | s->flags |= SF_ERR_SRVTO; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1287 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1288 | DBG_TRACE_DEVEL("leaving on error", |
| 1289 | 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] | 1290 | return 0; |
| 1291 | } |
| 1292 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1293 | /* 3: client abort with an abortonclose */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1294 | else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1295 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 1296 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1297 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1298 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1299 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1300 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1301 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1302 | txn->status = 400; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1303 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1304 | |
| 1305 | if (!(s->flags & SF_ERR_MASK)) |
| 1306 | s->flags |= SF_ERR_CLICL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1307 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1308 | |
| 1309 | /* process_stream() will take care of the error */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1310 | DBG_TRACE_DEVEL("leaving on error", |
| 1311 | 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] | 1312 | return 0; |
| 1313 | } |
| 1314 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1315 | /* 4: close from server, capture the response if the server has started to respond */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1316 | else if (rep->flags & CF_SHUTR) { |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1317 | if ((txn->flags & TX_L7_RETRY) && |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1318 | (s->be->retry_type & PR_RE_DISCONNECTED)) { |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1319 | if (co_data(rep) || do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1320 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1321 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1322 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1323 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1324 | } |
| 1325 | |
Olivier Houchard | 6db1699 | 2019-05-17 15:40:49 +0200 | [diff] [blame] | 1326 | if (txn->flags & TX_NOT_FIRST) |
| 1327 | goto abort_keep_alive; |
| 1328 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1329 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1330 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1331 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1332 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1333 | } |
| 1334 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1335 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1336 | stream_inc_http_fail_ctr(s); |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1337 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1338 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1339 | |
| 1340 | if (!(s->flags & SF_ERR_MASK)) |
| 1341 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1342 | http_set_term_flags(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1343 | DBG_TRACE_DEVEL("leaving on error", |
| 1344 | 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] | 1345 | return 0; |
| 1346 | } |
| 1347 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1348 | /* 5: write error to client (we don't send any message then) */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1349 | else if (rep->flags & CF_WRITE_ERROR) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1350 | if (txn->flags & TX_NOT_FIRST) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1351 | goto abort_keep_alive; |
| 1352 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1353 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1354 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1355 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1356 | rep->analysers &= AN_RES_FLT_END; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1357 | |
| 1358 | if (!(s->flags & SF_ERR_MASK)) |
| 1359 | s->flags |= SF_ERR_CLICL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1360 | http_set_term_flags(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1361 | |
| 1362 | /* process_stream() will take care of the error */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1363 | DBG_TRACE_DEVEL("leaving on error", |
| 1364 | 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] | 1365 | return 0; |
| 1366 | } |
| 1367 | |
| 1368 | channel_dont_close(rep); |
| 1369 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1370 | DBG_TRACE_DEVEL("waiting for more data", |
| 1371 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1372 | return 0; |
| 1373 | } |
| 1374 | |
| 1375 | /* More interesting part now : we know that we have a complete |
| 1376 | * response which at least looks like HTTP. We have an indicator |
| 1377 | * of each header's length, so we can parse them quickly. |
| 1378 | */ |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 1379 | BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1380 | sl = http_get_stline(htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1381 | |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1382 | /* Perform a L7 retry because of the status code */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1383 | if ((txn->flags & TX_L7_RETRY) && |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1384 | l7_status_match(s->be, sl->info.res.status) && |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1385 | do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1386 | DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 1387 | return 0; |
| 1388 | } |
| 1389 | |
| 1390 | /* Now, L7 buffer is useless, it can be released */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1391 | b_free(&txn->l7_buffer); |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1392 | |
| 1393 | msg->msg_state = HTTP_MSG_BODY; |
| 1394 | |
| 1395 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1396 | /* 0: we might have to print this header in debug mode */ |
| 1397 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1398 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 1399 | int32_t pos; |
| 1400 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1401 | http_debug_stline("srvrep", s, sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1402 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 1403 | 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] | 1404 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 1405 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 1406 | |
| 1407 | if (type == HTX_BLK_EOH) |
| 1408 | break; |
| 1409 | if (type != HTX_BLK_HDR) |
| 1410 | continue; |
| 1411 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1412 | http_debug_hdr("srvhdr", s, |
| 1413 | htx_get_blk_name(htx, blk), |
| 1414 | htx_get_blk_value(htx, blk)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1415 | } |
| 1416 | } |
| 1417 | |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1418 | /* 1: get the status code and the version. Also set HTTP flags */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1419 | txn->status = sl->info.res.status; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1420 | if (sl->flags & HTX_SL_F_VER_11) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1421 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1422 | if (sl->flags & HTX_SL_F_XFER_LEN) { |
| 1423 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 1424 | if (sl->flags & HTX_SL_F_CLEN) |
| 1425 | msg->flags |= HTTP_MSGF_CNT_LEN; |
| 1426 | else if (sl->flags & HTX_SL_F_CHNK) |
| 1427 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1428 | } |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 1429 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 1430 | msg->flags |= HTTP_MSGF_BODYLESS; |
Christopher Faulet | 576c358 | 2021-01-08 15:53:01 +0100 | [diff] [blame] | 1431 | if (sl->flags & HTX_SL_F_CONN_UPG) |
| 1432 | msg->flags |= HTTP_MSGF_CONN_UPG; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1433 | |
| 1434 | n = txn->status / 100; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1435 | if (n < 1 || n > 5) |
| 1436 | n = 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1437 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1438 | /* when the client triggers a 4xx from the server, it's most often due |
| 1439 | * to a missing object or permission. These events should be tracked |
| 1440 | * because if they happen often, it may indicate a brute force or a |
| 1441 | * vulnerability scan. |
| 1442 | */ |
| 1443 | if (n == 4) |
| 1444 | stream_inc_http_err_ctr(s); |
| 1445 | |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1446 | if (n == 5 && txn->status != 501 && txn->status != 505) |
| 1447 | stream_inc_http_fail_ctr(s); |
| 1448 | |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 1449 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1450 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]); |
| 1451 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req); |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 1452 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1453 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1454 | /* Adjust server's health based on status code. Note: status codes 501 |
| 1455 | * and 505 are triggered on demand by client request, so we must not |
| 1456 | * count them as server failures. |
| 1457 | */ |
| 1458 | if (objt_server(s->target)) { |
| 1459 | 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] | 1460 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1461 | else |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1462 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | /* |
| 1466 | * We may be facing a 100-continue response, or any other informational |
| 1467 | * 1xx response which is non-final, in which case this is not the right |
| 1468 | * response, and we're waiting for the next one. Let's allow this response |
| 1469 | * to go to the client and wait for the next one. There's an exception for |
| 1470 | * 101 which is used later in the code to switch protocols. |
| 1471 | */ |
| 1472 | if (txn->status < 200 && |
| 1473 | (txn->status == 100 || txn->status >= 102)) { |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1474 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1475 | htx->first = channel_htx_fwd_headers(rep, htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1476 | msg->msg_state = HTTP_MSG_RPBEFORE; |
Christopher Faulet | 3499f62 | 2019-09-03 15:23:54 +0200 | [diff] [blame] | 1477 | msg->flags = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1478 | txn->status = 0; |
| 1479 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 7d51845 | 2020-08-31 11:07:07 +0200 | [diff] [blame] | 1480 | rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */ |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1481 | goto next_one; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1482 | } |
| 1483 | |
Christopher Faulet | 6e6c7b1 | 2021-01-08 16:02:05 +0100 | [diff] [blame] | 1484 | /* A 101-switching-protocols must contains a Connection header with the |
| 1485 | * "upgrade" option and the request too. It means both are agree to |
| 1486 | * upgrade. It is not so strict because there is no test on the Upgrade |
| 1487 | * header content. But it is probably stronger enough for now. |
| 1488 | */ |
| 1489 | if (txn->status == 101 && |
| 1490 | (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG))) |
| 1491 | goto return_bad_res; |
| 1492 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1493 | /* |
| 1494 | * 2: check for cacheability. |
| 1495 | */ |
| 1496 | |
| 1497 | switch (txn->status) { |
| 1498 | case 200: |
| 1499 | case 203: |
| 1500 | case 204: |
| 1501 | case 206: |
| 1502 | case 300: |
| 1503 | case 301: |
| 1504 | case 404: |
| 1505 | case 405: |
| 1506 | case 410: |
| 1507 | case 414: |
| 1508 | case 501: |
| 1509 | break; |
| 1510 | default: |
| 1511 | /* RFC7231#6.1: |
| 1512 | * Responses with status codes that are defined as |
| 1513 | * cacheable by default (e.g., 200, 203, 204, 206, |
| 1514 | * 300, 301, 404, 405, 410, 414, and 501 in this |
| 1515 | * specification) can be reused by a cache with |
| 1516 | * heuristic expiration unless otherwise indicated |
| 1517 | * by the method definition or explicit cache |
| 1518 | * controls [RFC7234]; all other status codes are |
| 1519 | * not cacheable by default. |
| 1520 | */ |
| 1521 | txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK); |
| 1522 | break; |
| 1523 | } |
| 1524 | |
| 1525 | /* |
| 1526 | * 3: we may need to capture headers |
| 1527 | */ |
| 1528 | s->logs.logwait &= ~LW_RESP; |
| 1529 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1530 | http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1531 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1532 | /* Skip parsing if no content length is possible. */ |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 1533 | 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] | 1534 | txn->status == 101)) { |
| 1535 | /* Either we've established an explicit tunnel, or we're |
| 1536 | * switching the protocol. In both cases, we're very unlikely |
| 1537 | * to understand the next protocols. We have to switch to tunnel |
| 1538 | * mode, so that we transfer the request and responses then let |
| 1539 | * this protocol pass unmodified. When we later implement specific |
| 1540 | * parsers for such protocols, we'll want to check the Upgrade |
| 1541 | * header which contains information about that protocol for |
| 1542 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 1543 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 1544 | txn->flags |= TX_CON_WANT_TUN; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1545 | } |
| 1546 | |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1547 | /* check for NTML authentication headers in 401 (WWW-Authenticate) and |
| 1548 | * 407 (Proxy-Authenticate) responses and set the connection to private |
| 1549 | */ |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 1550 | srv_conn = sc_conn(s->scb); |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1551 | if (srv_conn) { |
| 1552 | struct ist hdr; |
| 1553 | struct http_hdr_ctx ctx; |
| 1554 | |
| 1555 | if (txn->status == 401) |
| 1556 | hdr = ist("WWW-Authenticate"); |
| 1557 | else if (txn->status == 407) |
| 1558 | hdr = ist("Proxy-Authenticate"); |
| 1559 | else |
| 1560 | goto end; |
| 1561 | |
| 1562 | ctx.blk = NULL; |
| 1563 | while (http_find_header(htx, hdr, &ctx, 0)) { |
Willy Tarreau | f1dcced | 2020-05-07 19:27:02 +0200 | [diff] [blame] | 1564 | /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM", |
| 1565 | * possibly followed by blanks and a base64 string, the connection |
| 1566 | * is private. Since it's a mess to deal with, we only check for |
| 1567 | * values starting with "NTLM" or "Nego". Note that often multiple |
| 1568 | * headers are sent by the server there. |
| 1569 | */ |
| 1570 | if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) || |
Willy Tarreau | 49a1d28 | 2020-05-07 19:10:15 +0200 | [diff] [blame] | 1571 | (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) { |
Olivier Houchard | 250031e | 2019-05-29 15:01:50 +0200 | [diff] [blame] | 1572 | sess->flags |= SESS_FL_PREFER_LAST; |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 1573 | conn_set_owner(srv_conn, sess, NULL); |
Christopher Faulet | 21ddc74 | 2020-07-01 15:26:14 +0200 | [diff] [blame] | 1574 | conn_set_private(srv_conn); |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 1575 | /* 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] | 1576 | session_add_conn(srv_conn->owner, srv_conn, srv_conn->target); |
Willy Tarreau | f1dcced | 2020-05-07 19:27:02 +0200 | [diff] [blame] | 1577 | break; |
Olivier Houchard | 250031e | 2019-05-29 15:01:50 +0200 | [diff] [blame] | 1578 | } |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | end: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1583 | /* we want to have the response time before we start processing it */ |
| 1584 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 1585 | |
| 1586 | /* end of job, return OK */ |
| 1587 | rep->analysers &= ~an_bit; |
| 1588 | rep->analyse_exp = TICK_ETERNITY; |
| 1589 | channel_auto_close(rep); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1590 | 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] | 1591 | return 1; |
| 1592 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1593 | return_int_err: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1594 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 1595 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1596 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1597 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1598 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1599 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1600 | txn->status = 500; |
| 1601 | if (!(s->flags & SF_ERR_MASK)) |
| 1602 | s->flags |= SF_ERR_INTERNAL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1603 | goto return_prx_cond; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1604 | |
| 1605 | return_bad_res: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1606 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1607 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1608 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1609 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1610 | } |
Olivier Houchard | e3249a9 | 2019-05-03 23:01:47 +0200 | [diff] [blame] | 1611 | if ((s->be->retry_type & PR_RE_JUNK_REQUEST) && |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1612 | (txn->flags & TX_L7_RETRY) && |
Willy Tarreau | 7cb9e6c | 2022-05-17 19:40:40 +0200 | [diff] [blame] | 1613 | do_l7_retry(s, s->scb) == 0) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1614 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1615 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | e3249a9 | 2019-05-03 23:01:47 +0200 | [diff] [blame] | 1616 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1617 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1618 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1619 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1620 | /* fall through */ |
| 1621 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1622 | return_prx_cond: |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1623 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1624 | http_set_term_flags(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1625 | |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1626 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1627 | DBG_TRACE_DEVEL("leaving on error", |
| 1628 | 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] | 1629 | return 0; |
| 1630 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1631 | abort_keep_alive: |
| 1632 | /* A keep-alive request to the server failed on a network error. |
| 1633 | * The client is required to retry. We need to close without returning |
| 1634 | * any other information so that the client retries. |
| 1635 | */ |
| 1636 | txn->status = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1637 | s->logs.logwait = 0; |
| 1638 | s->logs.level = 0; |
| 1639 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1640 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1641 | DBG_TRACE_DEVEL("leaving by closing K/A connection", |
| 1642 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1643 | return 0; |
| 1644 | } |
| 1645 | |
| 1646 | /* This function performs all the processing enabled for the current response. |
| 1647 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 1648 | * and updates s->res.analysers. It might make sense to explode it into several |
| 1649 | * other functions. It works like process_request (see indications above). |
| 1650 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1651 | 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] | 1652 | { |
| 1653 | struct session *sess = s->sess; |
| 1654 | struct http_txn *txn = s->txn; |
| 1655 | struct http_msg *msg = &txn->rsp; |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1656 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1657 | struct proxy *cur_proxy; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1658 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 1659 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1660 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 1661 | return 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1662 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1663 | 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] | 1664 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1665 | htx = htxbuf(&rep->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1666 | |
| 1667 | /* The stats applet needs to adjust the Connection header but we don't |
| 1668 | * apply any filter there. |
| 1669 | */ |
| 1670 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 1671 | rep->analysers &= ~an_bit; |
| 1672 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1673 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | /* |
| 1677 | * We will have to evaluate the filters. |
| 1678 | * As opposed to version 1.2, now they will be evaluated in the |
| 1679 | * filters order and not in the header order. This means that |
| 1680 | * each filter has to be validated among all headers. |
| 1681 | * |
| 1682 | * Filters are tried with ->be first, then with ->fe if it is |
| 1683 | * different from ->be. |
| 1684 | * |
| 1685 | * Maybe we are in resume condiion. In this case I choose the |
| 1686 | * "struct proxy" which contains the rule list matching the resume |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1687 | * pointer. If none of these "struct proxy" match, I initialise |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1688 | * the process with the first one. |
| 1689 | * |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1690 | * In fact, I check only correspondence between the current list |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1691 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 1692 | * the loop with the ->be. |
| 1693 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1694 | if (s->current_rule_list == &sess->fe->http_res_rules || |
| 1695 | (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] | 1696 | cur_proxy = sess->fe; |
| 1697 | else |
| 1698 | cur_proxy = s->be; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1699 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1700 | while (1) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1701 | /* evaluate http-response rules */ |
Christopher Faulet | 46f46df | 2021-11-09 16:33:25 +0100 | [diff] [blame] | 1702 | if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1703 | struct list *def_rules, *rules; |
| 1704 | |
| 1705 | def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL); |
| 1706 | rules = &cur_proxy->http_res_rules; |
| 1707 | |
| 1708 | ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1709 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1710 | switch (ret) { |
| 1711 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 1712 | goto return_prx_yield; |
| 1713 | |
| 1714 | case HTTP_RULE_RES_CONT: |
| 1715 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 1716 | break; |
| 1717 | |
| 1718 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 1719 | goto deny; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1720 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1721 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent */ |
| 1722 | goto return_prx_cond; |
| 1723 | |
| 1724 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1725 | goto done; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1726 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1727 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 1728 | goto return_bad_res; |
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 | case HTTP_RULE_RES_ERROR: /* failed with a bad request */ |
| 1731 | goto return_int_err; |
| 1732 | } |
| 1733 | |
| 1734 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1735 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1736 | /* check whether we're already working on the frontend */ |
| 1737 | if (cur_proxy == sess->fe) |
| 1738 | break; |
| 1739 | cur_proxy = sess->fe; |
| 1740 | } |
| 1741 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1742 | /* OK that's all we can do for 1xx responses */ |
| 1743 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1744 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1745 | |
| 1746 | /* |
| 1747 | * Now check for a server cookie. |
| 1748 | */ |
| 1749 | 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] | 1750 | http_manage_server_side_cookies(s, rep); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1751 | |
| 1752 | /* |
| 1753 | * Check for cache-control or pragma headers if required. |
| 1754 | */ |
| 1755 | 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] | 1756 | http_check_response_for_cacheability(s, rep); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1757 | |
| 1758 | /* |
| 1759 | * Add server cookie in the response if needed |
| 1760 | */ |
| 1761 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 1762 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
| 1763 | (!(s->flags & SF_DIRECT) || |
| 1764 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 1765 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 1766 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 1767 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 1768 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 1769 | !(s->flags & SF_IGNORE_PRST)) { |
| 1770 | /* the server is known, it's not the one the client requested, or the |
| 1771 | * cookie's last seen date needs to be refreshed. We have to |
| 1772 | * insert a set-cookie here, except if we want to insert only on POST |
| 1773 | * requests and this one isn't. Note that servers which don't have cookies |
| 1774 | * (eg: some backup servers) will return a full cookie removal request. |
| 1775 | */ |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1776 | if (!__objt_server(s->target)->cookie) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1777 | chunk_printf(&trash, |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1778 | "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1779 | s->be->cookie_name); |
| 1780 | } |
| 1781 | else { |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1782 | 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] | 1783 | |
| 1784 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 1785 | /* emit last_date, which is mandatory */ |
| 1786 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 1787 | s30tob64((date.tv_sec+3) >> 2, |
| 1788 | trash.area + trash.data); |
| 1789 | trash.data += 5; |
| 1790 | |
| 1791 | if (s->be->cookie_maxlife) { |
| 1792 | /* emit first_date, which is either the original one or |
| 1793 | * the current date. |
| 1794 | */ |
| 1795 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 1796 | s30tob64(txn->cookie_first_date ? |
| 1797 | txn->cookie_first_date >> 2 : |
| 1798 | (date.tv_sec+3) >> 2, |
| 1799 | trash.area + trash.data); |
| 1800 | trash.data += 5; |
| 1801 | } |
| 1802 | } |
| 1803 | chunk_appendf(&trash, "; path=/"); |
| 1804 | } |
| 1805 | |
| 1806 | if (s->be->cookie_domain) |
| 1807 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
| 1808 | |
| 1809 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 1810 | chunk_appendf(&trash, "; HttpOnly"); |
| 1811 | |
| 1812 | if (s->be->ck_opts & PR_CK_SECURE) |
| 1813 | chunk_appendf(&trash, "; Secure"); |
| 1814 | |
Christopher Faulet | 2f53390 | 2020-01-21 11:06:48 +0100 | [diff] [blame] | 1815 | if (s->be->cookie_attrs) |
| 1816 | chunk_appendf(&trash, "; %s", s->be->cookie_attrs); |
| 1817 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1818 | 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] | 1819 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1820 | |
| 1821 | txn->flags &= ~TX_SCK_MASK; |
| 1822 | if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
| 1823 | /* the server did not change, only the date was updated */ |
| 1824 | txn->flags |= TX_SCK_UPDATED; |
| 1825 | else |
| 1826 | txn->flags |= TX_SCK_INSERTED; |
| 1827 | |
| 1828 | /* Here, we will tell an eventual cache on the client side that we don't |
| 1829 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 1830 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 1831 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 1832 | */ |
| 1833 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
| 1834 | |
| 1835 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 1836 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1837 | if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private")))) |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 1838 | goto return_fail_rewrite; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | /* |
| 1843 | * Check if result will be cacheable with a cookie. |
| 1844 | * We'll block the response if security checks have caught |
| 1845 | * nasty things such as a cacheable cookie. |
| 1846 | */ |
| 1847 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 1848 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 1849 | (s->be->options & PR_O_CHK_CACHE)) { |
| 1850 | /* we're in presence of a cacheable response containing |
| 1851 | * a set-cookie header. We'll block it as requested by |
| 1852 | * the 'checkcache' option, and send an alert. |
| 1853 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1854 | 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] | 1855 | 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] | 1856 | send_log(s->be, LOG_ALERT, |
| 1857 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1858 | 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] | 1859 | goto deny; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1860 | } |
| 1861 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1862 | end: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 1863 | /* |
| 1864 | * Evaluate after-response rules before forwarding the response. rules |
| 1865 | * from the backend are evaluated first, then one from the frontend if |
| 1866 | * it differs. |
| 1867 | */ |
| 1868 | if (!http_eval_after_res_rules(s)) |
| 1869 | goto return_int_err; |
| 1870 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 1871 | /* Filter the response headers if there are filters attached to the |
| 1872 | * stream. |
| 1873 | */ |
| 1874 | if (HAS_FILTERS(s)) |
| 1875 | rep->analysers |= AN_RES_FLT_HTTP_HDRS; |
| 1876 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1877 | /* Always enter in the body analyzer */ |
| 1878 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 1879 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
| 1880 | |
| 1881 | /* if the user wants to log as soon as possible, without counting |
| 1882 | * bytes from the server, then this is the right moment. We have |
| 1883 | * to temporarily assign bytes_out to log what we currently have. |
| 1884 | */ |
| 1885 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
| 1886 | 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] | 1887 | s->logs.bytes_out = htx->data; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1888 | s->do_log(s); |
| 1889 | s->logs.bytes_out = 0; |
| 1890 | } |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1891 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1892 | done: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 1893 | 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] | 1894 | rep->analysers &= ~an_bit; |
| 1895 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1896 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1897 | return 1; |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1898 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1899 | deny: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1900 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp); |
| 1901 | _HA_ATOMIC_INC(&s->be->be_counters.denied_resp); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1902 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1903 | _HA_ATOMIC_INC(&sess->listener->counters->denied_resp); |
Christopher Faulet | a08546b | 2019-12-16 16:07:34 +0100 | [diff] [blame] | 1904 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1905 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1906 | goto return_prx_err; |
| 1907 | |
Christopher Faulet | d649b57 | 2022-06-01 17:42:35 +0200 | [diff] [blame] | 1908 | return_fail_rewrite: |
| 1909 | if (!(s->flags & SF_ERR_MASK)) |
| 1910 | s->flags |= SF_ERR_PRXCOND; |
| 1911 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_rewrites); |
| 1912 | _HA_ATOMIC_INC(&s->be->be_counters.failed_rewrites); |
| 1913 | if (sess->listener && sess->listener->counters) |
| 1914 | _HA_ATOMIC_INC(&sess->listener->counters->failed_rewrites); |
| 1915 | if (objt_server(s->target)) |
| 1916 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_rewrites); |
| 1917 | /* fall through */ |
| 1918 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1919 | return_int_err: |
| 1920 | txn->status = 500; |
| 1921 | if (!(s->flags & SF_ERR_MASK)) |
| 1922 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1923 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 1924 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
Dragan Dosen | 9a006f9 | 2021-09-21 13:02:09 +0200 | [diff] [blame] | 1925 | if (sess->listener && sess->listener->counters) |
| 1926 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1927 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1928 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1929 | goto return_prx_err; |
| 1930 | |
| 1931 | return_bad_res: |
| 1932 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1933 | stream_inc_http_fail_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1934 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | a20a653 | 2020-02-05 10:16:41 +0100 | [diff] [blame] | 1935 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1936 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | a20a653 | 2020-02-05 10:16:41 +0100 | [diff] [blame] | 1937 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 1938 | } |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1939 | /* fall through */ |
| 1940 | |
| 1941 | return_prx_err: |
| 1942 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 1943 | /* fall through */ |
| 1944 | |
| 1945 | return_prx_cond: |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1946 | s->logs.t_data = -1; /* was not a valid response */ |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 1947 | s->scb->flags |= SC_FL_NOLINGER; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1948 | |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 1949 | http_set_term_flags(s); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1950 | |
Christopher Faulet | e58c000 | 2020-03-02 16:21:01 +0100 | [diff] [blame] | 1951 | rep->analysers &= AN_RES_FLT_END; |
| 1952 | s->req.analysers &= AN_REQ_FLT_END; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1953 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1954 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1955 | DBG_TRACE_DEVEL("leaving on error", |
| 1956 | 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] | 1957 | return 0; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1958 | |
| 1959 | return_prx_yield: |
| 1960 | channel_dont_close(rep); |
| 1961 | DBG_TRACE_DEVEL("waiting for more data", |
| 1962 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 1963 | return 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | /* This function is an analyser which forwards response body (including chunk |
| 1967 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 1968 | * zero byte. The only situation where it must not be called is when we're in |
| 1969 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 1970 | * remaining data and to resync after end of body. It expects the msg_state to |
| 1971 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 1972 | * read more data, or 1 once we can go on with next request or end the stream. |
| 1973 | * |
| 1974 | * It is capable of compressing response data both in content-length mode and |
| 1975 | * in chunked mode. The state machines follows different flows depending on |
| 1976 | * whether content-length and chunked modes are used, since there are no |
| 1977 | * trailers in content-length : |
| 1978 | * |
| 1979 | * chk-mode cl-mode |
| 1980 | * ,----- BODY -----. |
| 1981 | * / \ |
| 1982 | * V size > 0 V chk-mode |
| 1983 | * .--> SIZE -------------> DATA -------------> CRLF |
| 1984 | * | | size == 0 | last byte | |
| 1985 | * | v final crlf v inspected | |
| 1986 | * | TRAILERS -----------> DONE | |
| 1987 | * | | |
| 1988 | * `----------------------------------------------' |
| 1989 | * |
| 1990 | * Compression only happens in the DATA state, and must be flushed in final |
| 1991 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 1992 | * is performed at once on final states for all bytes parsed, or when leaving |
| 1993 | * on missing data. |
| 1994 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1995 | 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] | 1996 | { |
| 1997 | struct session *sess = s->sess; |
| 1998 | struct http_txn *txn = s->txn; |
| 1999 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2000 | struct htx *htx; |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2001 | int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2002 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2003 | 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] | 2004 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2005 | htx = htxbuf(&res->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2006 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2007 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 2008 | goto return_bad_res; |
| 2009 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 2010 | goto return_int_err; |
| 2011 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2012 | if (msg->msg_state == HTTP_MSG_BODY) |
| 2013 | msg->msg_state = HTTP_MSG_DATA; |
| 2014 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2015 | /* in most states, we should abort in case of early close */ |
| 2016 | channel_auto_close(res); |
| 2017 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2018 | if (res->to_forward) { |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2019 | if (res->to_forward == CHN_INFINITE_FORWARD) { |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2020 | if (res->flags & CF_EOI) |
| 2021 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2022 | } |
| 2023 | else { |
| 2024 | /* We can't process the buffer's contents yet */ |
| 2025 | res->flags |= CF_WAKE_WRITE; |
| 2026 | goto missing_data_or_waiting; |
| 2027 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2028 | } |
| 2029 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2030 | if (msg->msg_state >= HTTP_MSG_ENDING) |
| 2031 | goto ending; |
| 2032 | |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 2033 | 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] | 2034 | (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) { |
| 2035 | msg->msg_state = HTTP_MSG_ENDING; |
| 2036 | goto ending; |
| 2037 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2038 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2039 | /* Forward input data. We get it by removing all outgoing data not |
| 2040 | * forwarded yet from HTX data size. If there are some data filters, we |
| 2041 | * let them decide the amount of data to forward. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2042 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2043 | if (HAS_RSP_DATA_FILTERS(s)) { |
| 2044 | ret = flt_http_payload(s, msg, htx->data); |
| 2045 | if (ret < 0) |
| 2046 | goto return_bad_res; |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 2047 | c_adv(res, ret); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2048 | } |
| 2049 | else { |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 2050 | c_adv(res, htx->data - co_data(res)); |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2051 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
| 2052 | channel_htx_forward_forever(res, htx); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2053 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2054 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2055 | if (htx->data != co_data(res)) |
| 2056 | goto missing_data_or_waiting; |
| 2057 | |
| 2058 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) { |
| 2059 | msg->msg_state = HTTP_MSG_ENDING; |
| 2060 | goto ending; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2061 | } |
| 2062 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2063 | /* 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] | 2064 | * in HTTP_MSG_ENDING state. Then if all data was marked to be |
| 2065 | * forwarded, set the state to HTTP_MSG_DONE. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2066 | */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2067 | if (!(htx->flags & HTX_FL_EOM)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2068 | goto missing_data_or_waiting; |
| 2069 | |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 2070 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2071 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2072 | ending: |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 2073 | res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */ |
| 2074 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2075 | /* other states, ENDING...TUNNEL */ |
| 2076 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 2077 | goto done; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2078 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2079 | if (HAS_RSP_DATA_FILTERS(s)) { |
| 2080 | ret = flt_http_end(s, msg); |
| 2081 | if (ret <= 0) { |
| 2082 | if (!ret) |
| 2083 | goto missing_data_or_waiting; |
| 2084 | goto return_bad_res; |
| 2085 | } |
| 2086 | } |
| 2087 | |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 2088 | 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] | 2089 | !(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 2090 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 2091 | goto ending; |
| 2092 | } |
| 2093 | else { |
| 2094 | msg->msg_state = HTTP_MSG_DONE; |
| 2095 | res->to_forward = 0; |
| 2096 | } |
| 2097 | |
| 2098 | done: |
| 2099 | |
| 2100 | channel_dont_close(res); |
| 2101 | |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 2102 | if ((res->flags & CF_SHUTW) && co_data(res)) { |
| 2103 | /* response errors are most likely due to the client aborting |
| 2104 | * the transfer. */ |
| 2105 | goto return_cli_abort; |
| 2106 | } |
| 2107 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2108 | http_end_response(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2109 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2110 | 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] | 2111 | return 1; |
| 2112 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2113 | DBG_TRACE_DEVEL("waiting for the end of the HTTP txn", |
| 2114 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2115 | return 0; |
| 2116 | |
| 2117 | missing_data_or_waiting: |
| 2118 | if (res->flags & CF_SHUTW) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2119 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2120 | |
| 2121 | /* stop waiting for data if the input is closed before the end. If the |
| 2122 | * client side was already closed, it means that the client has aborted, |
| 2123 | * so we don't want to count this as a server abort. Otherwise it's a |
| 2124 | * server abort. |
| 2125 | */ |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 2126 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2127 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2128 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2129 | /* If we have some pending data, we continue the processing */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2130 | if (htx_is_empty(htx)) |
| 2131 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2132 | } |
| 2133 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2134 | /* When TE: chunked is used, we need to get there again to parse |
| 2135 | * 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] | 2136 | * set CF_DONTCLOSE. Similarly when there is a content-leng or if there |
| 2137 | * are filters registered on the stream, we don't want to forward a |
| 2138 | * close |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2139 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2140 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2141 | channel_dont_close(res); |
| 2142 | |
| 2143 | /* We know that more data are expected, but we couldn't send more that |
| 2144 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
| 2145 | * system knows it must not set a PUSH on this first part. Interactive |
| 2146 | * modes are already handled by the stream sock layer. We must not do |
| 2147 | * this in content-length mode because it could present the MSG_MORE |
| 2148 | * flag with the last block of forwarded data, which would cause an |
| 2149 | * additional delay to be observed by the receiver. |
| 2150 | */ |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 2151 | if (HAS_RSP_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2152 | res->flags |= CF_EXPECT_MORE; |
| 2153 | |
| 2154 | /* the stream handler will take care of timeouts and errors */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2155 | DBG_TRACE_DEVEL("waiting for more data to forward", |
| 2156 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2157 | return 0; |
| 2158 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2159 | return_srv_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2160 | _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts); |
| 2161 | _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2162 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2163 | _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2164 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2165 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts); |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2166 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2167 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 2168 | 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] | 2169 | goto return_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2170 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2171 | return_cli_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2172 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 2173 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2174 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2175 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2176 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2177 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2178 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 2179 | 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] | 2180 | goto return_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2181 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2182 | return_int_err: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2183 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 2184 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2185 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2186 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2187 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2188 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2189 | if (!(s->flags & SF_ERR_MASK)) |
| 2190 | s->flags |= SF_ERR_INTERNAL; |
| 2191 | goto return_error; |
| 2192 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2193 | return_bad_res: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2194 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2195 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2196 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2197 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 2198 | } |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2199 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2200 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2201 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2202 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2203 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2204 | return_error: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2205 | /* don't send any error message as we're in the body */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2206 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | f2b02cf | 2023-01-13 11:02:28 +0100 | [diff] [blame] | 2207 | http_set_term_flags(s); |
| 2208 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2209 | DBG_TRACE_DEVEL("leaving on error", |
| 2210 | 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] | 2211 | return 0; |
| 2212 | } |
| 2213 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2214 | /* Perform an HTTP redirect based on the information in <rule>. The function |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2215 | * returns zero in case of an irrecoverable error such as too large a request |
| 2216 | * to build a valid response, 1 in case of successful redirect (hence the rule |
| 2217 | * is final), or 2 if the rule has to be silently skipped. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2218 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2219 | 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] | 2220 | { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2221 | struct channel *req = &s->req; |
| 2222 | struct channel *res = &s->res; |
| 2223 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2224 | struct htx_sl *sl; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2225 | struct buffer *chunk; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2226 | struct ist status, reason, location; |
| 2227 | unsigned int flags; |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2228 | 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] | 2229 | |
| 2230 | chunk = alloc_trash_chunk(); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2231 | if (!chunk) { |
| 2232 | if (!(s->flags & SF_ERR_MASK)) |
| 2233 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2234 | goto fail; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2235 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2236 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2237 | /* |
| 2238 | * Create the location |
| 2239 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2240 | htx = htxbuf(&req->buf); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2241 | switch(rule->type) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2242 | case REDIRECT_TYPE_SCHEME: { |
| 2243 | struct http_hdr_ctx ctx; |
| 2244 | struct ist path, host; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2245 | struct http_uri_parser parser; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2246 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2247 | host = ist(""); |
| 2248 | ctx.blk = NULL; |
| 2249 | if (http_find_header(htx, ist("Host"), &ctx, 0)) |
| 2250 | host = ctx.value; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2251 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2252 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2253 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 2254 | path = http_parse_path(&parser); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2255 | /* build message using path */ |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 2256 | if (isttest(path)) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2257 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2258 | int qs = 0; |
| 2259 | while (qs < path.len) { |
| 2260 | if (*(path.ptr + qs) == '?') { |
| 2261 | path.len = qs; |
| 2262 | break; |
| 2263 | } |
| 2264 | qs++; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2265 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2266 | } |
| 2267 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2268 | else |
| 2269 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2270 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2271 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2272 | /* add scheme */ |
| 2273 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2274 | goto fail; |
| 2275 | } |
| 2276 | else { |
| 2277 | /* add scheme with executing log format */ |
| 2278 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2279 | chunk->size - chunk->data, |
| 2280 | &rule->rdr_fmt); |
| 2281 | } |
| 2282 | /* add "://" + host + path */ |
| 2283 | if (!chunk_memcat(chunk, "://", 3) || |
| 2284 | !chunk_memcat(chunk, host.ptr, host.len) || |
| 2285 | !chunk_memcat(chunk, path.ptr, path.len)) |
| 2286 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2287 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2288 | /* append a slash at the end of the location if needed and missing */ |
| 2289 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2290 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2291 | if (chunk->data + 1 >= chunk->size) |
| 2292 | goto fail; |
| 2293 | chunk->area[chunk->data++] = '/'; |
| 2294 | } |
| 2295 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2296 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2297 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2298 | case REDIRECT_TYPE_PREFIX: { |
| 2299 | struct ist path; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2300 | struct http_uri_parser parser; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2301 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2302 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2303 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 2304 | path = http_parse_path(&parser); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2305 | /* build message using path */ |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 2306 | if (isttest(path)) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2307 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2308 | int qs = 0; |
| 2309 | while (qs < path.len) { |
| 2310 | if (*(path.ptr + qs) == '?') { |
| 2311 | path.len = qs; |
| 2312 | break; |
| 2313 | } |
| 2314 | qs++; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2315 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2316 | } |
| 2317 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2318 | else |
| 2319 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2320 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2321 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2322 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2323 | * add anything, otherwise it makes it hard for the user to |
| 2324 | * configure a self-redirection. |
| 2325 | */ |
| 2326 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 2327 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2328 | goto fail; |
| 2329 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2330 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2331 | else { |
| 2332 | /* add prefix with executing log format */ |
| 2333 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2334 | chunk->size - chunk->data, |
| 2335 | &rule->rdr_fmt); |
| 2336 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2337 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2338 | /* add path */ |
| 2339 | if (!chunk_memcat(chunk, path.ptr, path.len)) |
| 2340 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2341 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2342 | /* append a slash at the end of the location if needed and missing */ |
| 2343 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2344 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2345 | if (chunk->data + 1 >= chunk->size) |
| 2346 | goto fail; |
| 2347 | chunk->area[chunk->data++] = '/'; |
| 2348 | } |
| 2349 | break; |
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 | case REDIRECT_TYPE_LOCATION: |
| 2352 | default: |
| 2353 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2354 | /* add location */ |
| 2355 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2356 | goto fail; |
| 2357 | } |
| 2358 | else { |
| 2359 | /* add location with executing log format */ |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2360 | int len = build_logline(s, chunk->area + chunk->data, |
| 2361 | chunk->size - chunk->data, |
| 2362 | &rule->rdr_fmt); |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2363 | if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) { |
| 2364 | ret = 2; |
| 2365 | goto out; |
| 2366 | } |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2367 | |
| 2368 | chunk->data += len; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2369 | } |
| 2370 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2371 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2372 | location = ist2(chunk->area, chunk->data); |
| 2373 | |
| 2374 | /* |
| 2375 | * Create the 30x response |
| 2376 | */ |
| 2377 | switch (rule->code) { |
| 2378 | case 308: |
| 2379 | status = ist("308"); |
| 2380 | reason = ist("Permanent Redirect"); |
| 2381 | break; |
| 2382 | case 307: |
| 2383 | status = ist("307"); |
| 2384 | reason = ist("Temporary Redirect"); |
| 2385 | break; |
| 2386 | case 303: |
| 2387 | status = ist("303"); |
| 2388 | reason = ist("See Other"); |
| 2389 | break; |
| 2390 | case 301: |
| 2391 | status = ist("301"); |
| 2392 | reason = ist("Moved Permanently"); |
| 2393 | break; |
| 2394 | case 302: |
| 2395 | default: |
| 2396 | status = ist("302"); |
| 2397 | reason = ist("Found"); |
| 2398 | break; |
| 2399 | } |
| 2400 | |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2401 | if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE) |
| 2402 | close = 1; |
| 2403 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2404 | htx = htx_from_buf(&res->buf); |
Kevin Zhu | 96b3639 | 2020-01-07 09:42:55 +0100 | [diff] [blame] | 2405 | /* Trim any possible response */ |
| 2406 | channel_htx_truncate(&s->res, htx); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2407 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 2408 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason); |
| 2409 | if (!sl) |
| 2410 | goto fail; |
| 2411 | sl->info.res.status = rule->code; |
| 2412 | s->txn->status = rule->code; |
| 2413 | |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2414 | if (close && !htx_add_header(htx, ist("Connection"), ist("close"))) |
| 2415 | goto fail; |
| 2416 | |
| 2417 | if (!htx_add_header(htx, ist("Content-length"), ist("0")) || |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2418 | !htx_add_header(htx, ist("Location"), location)) |
| 2419 | goto fail; |
| 2420 | |
| 2421 | if (rule->code == 302 || rule->code == 303 || rule->code == 307) { |
| 2422 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache"))) |
| 2423 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2424 | } |
| 2425 | |
| 2426 | if (rule->cookie_len) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2427 | if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len))) |
| 2428 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2429 | } |
| 2430 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2431 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2432 | goto fail; |
| 2433 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2434 | htx->flags |= HTX_FL_EOM; |
Kevin Zhu | 96b3639 | 2020-01-07 09:42:55 +0100 | [diff] [blame] | 2435 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 2436 | if (!http_forward_proxy_resp(s, 1)) |
| 2437 | goto fail; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2438 | |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2439 | if (rule->flags & REDIRECT_FLAG_FROM_REQ) { |
| 2440 | /* let's log the request time */ |
| 2441 | s->logs.tv_request = now; |
Christopher Faulet | d347588 | 2021-10-04 14:16:46 +0200 | [diff] [blame] | 2442 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2443 | |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2444 | 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] | 2445 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2446 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2447 | |
| 2448 | if (!(s->flags & SF_ERR_MASK)) |
| 2449 | s->flags |= SF_ERR_LOCAL; |
Christopher Faulet | 4a66c94 | 2023-01-13 09:43:21 +0100 | [diff] [blame] | 2450 | http_set_term_flags(s); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2451 | |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2452 | out: |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2453 | free_trash_chunk(chunk); |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2454 | return ret; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2455 | |
| 2456 | fail: |
| 2457 | /* If an error occurred, remove the incomplete HTTP response from the |
| 2458 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 2459 | channel_htx_truncate(res, htxbuf(&res->buf)); |
Christopher Faulet | eab1757 | 2022-04-26 20:34:38 +0200 | [diff] [blame] | 2460 | ret = 0; |
| 2461 | goto out; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2462 | } |
| 2463 | |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2464 | /* This function filters the request header names to only allow [0-9a-zA-Z-] |
| 2465 | * characters. Depending on the proxy configuration, headers with a name not |
| 2466 | * matching this charset are removed or the request is rejected with a |
| 2467 | * 403-Forbidden response if such name are found. It returns HTTP_RULE_RES_CONT |
| 2468 | * to continue the request processing or HTTP_RULE_RES_DENY if the request is |
| 2469 | * rejected. |
| 2470 | */ |
| 2471 | static enum rule_result http_req_restrict_header_names(struct stream *s, struct htx *htx, struct proxy *px) |
| 2472 | { |
| 2473 | struct htx_blk *blk; |
| 2474 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
| 2475 | |
| 2476 | blk = htx_get_first_blk(htx); |
| 2477 | while (blk) { |
| 2478 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 2479 | |
| 2480 | if (type == HTX_BLK_HDR) { |
| 2481 | struct ist n = htx_get_blk_name(htx, blk); |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2482 | int i, end = istlen(n); |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2483 | |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2484 | for (i = 0; i < end; i++) { |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2485 | if (!isalnum((unsigned char)n.ptr[i]) && n.ptr[i] != '-') { |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2486 | break; |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2487 | } |
| 2488 | } |
Mateusz Malek | 4b85a96 | 2022-08-17 14:22:09 +0200 | [diff] [blame] | 2489 | |
| 2490 | if (i < end) { |
| 2491 | /* Disallowed character found - block the request or remove the header */ |
| 2492 | if (px->options2 & PR_O2_RSTRICT_REQ_HDR_NAMES_BLK) |
| 2493 | goto block; |
| 2494 | blk = htx_remove_blk(htx, blk); |
| 2495 | continue; |
| 2496 | } |
Christopher Faulet | 18c13d3 | 2022-05-16 11:43:10 +0200 | [diff] [blame] | 2497 | } |
| 2498 | if (type == HTX_BLK_EOH) |
| 2499 | break; |
| 2500 | |
| 2501 | blk = htx_get_next_blk(htx, blk); |
| 2502 | } |
| 2503 | out: |
| 2504 | return rule_ret; |
| 2505 | block: |
| 2506 | /* Block the request returning a 403-Forbidden response */ |
| 2507 | s->txn->status = 403; |
| 2508 | rule_ret = HTTP_RULE_RES_DENY; |
| 2509 | goto out; |
| 2510 | } |
| 2511 | |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2512 | /* Replace all headers matching the name <name>. The header value is replaced if |
| 2513 | * it matches the regex <re>. <str> is used for the replacement. If <full> is |
| 2514 | * set to 1, the full-line is matched and replaced. Otherwise, comma-separated |
| 2515 | * values are evaluated one by one. It returns 0 on success and -1 on error. |
| 2516 | */ |
| 2517 | int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name, |
| 2518 | const char *str, struct my_regex *re, int full) |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2519 | { |
| 2520 | struct http_hdr_ctx ctx; |
| 2521 | struct buffer *output = get_trash_chunk(); |
| 2522 | |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2523 | ctx.blk = NULL; |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2524 | while (http_find_header(htx, name, &ctx, full)) { |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2525 | if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0)) |
| 2526 | continue; |
| 2527 | |
| 2528 | output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch); |
| 2529 | if (output->data == -1) |
| 2530 | return -1; |
| 2531 | if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data))) |
| 2532 | return -1; |
| 2533 | } |
| 2534 | return 0; |
| 2535 | } |
| 2536 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2537 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 2538 | * takes the string from the variable 'replace' with length 'len', then modifies |
| 2539 | * the relevant part of the request line accordingly. Then it updates various |
| 2540 | * pointers to the next elements which were moved, and the total buffer length. |
| 2541 | * It finds the action to be performed in p[2], previously filled by function |
| 2542 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 2543 | * error, though this can be revisited when this code is finally exploited. |
| 2544 | * |
| 2545 | * '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] | 2546 | * 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] | 2547 | * |
| 2548 | * In query string case, the mark question '?' must be set at the start of the |
| 2549 | * string by the caller, event if the replacement query string is empty. |
| 2550 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2551 | int http_req_replace_stline(int action, const char *replace, int len, |
| 2552 | struct proxy *px, struct stream *s) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2553 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2554 | struct htx *htx = htxbuf(&s->req.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2555 | |
| 2556 | switch (action) { |
| 2557 | case 0: // method |
| 2558 | if (!http_replace_req_meth(htx, ist2(replace, len))) |
| 2559 | return -1; |
| 2560 | break; |
| 2561 | |
| 2562 | case 1: // path |
Christopher Faulet | b8ce505 | 2020-08-31 16:11:57 +0200 | [diff] [blame] | 2563 | if (!http_replace_req_path(htx, ist2(replace, len), 0)) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2564 | return -1; |
| 2565 | break; |
| 2566 | |
| 2567 | case 2: // query |
| 2568 | if (!http_replace_req_query(htx, ist2(replace, len))) |
| 2569 | return -1; |
| 2570 | break; |
| 2571 | |
| 2572 | case 3: // uri |
| 2573 | if (!http_replace_req_uri(htx, ist2(replace, len))) |
| 2574 | return -1; |
| 2575 | break; |
| 2576 | |
Christopher Faulet | 312294f | 2020-09-02 17:17:44 +0200 | [diff] [blame] | 2577 | case 4: // path + query |
| 2578 | if (!http_replace_req_path(htx, ist2(replace, len), 1)) |
| 2579 | return -1; |
| 2580 | break; |
| 2581 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2582 | default: |
| 2583 | return -1; |
| 2584 | } |
| 2585 | return 0; |
| 2586 | } |
| 2587 | |
| 2588 | /* This function replace the HTTP status code and the associated message. The |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2589 | * variable <status> contains the new status code. This function never fails. It |
| 2590 | * returns 0 in case of success, -1 in case of internal error. |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2591 | */ |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2592 | 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] | 2593 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2594 | struct htx *htx = htxbuf(&s->res.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2595 | char *res; |
| 2596 | |
| 2597 | chunk_reset(&trash); |
| 2598 | res = ultoa_o(status, trash.area, trash.size); |
| 2599 | trash.data = res - trash.area; |
| 2600 | |
| 2601 | /* Do we have a custom reason format string? */ |
Tim Duesterhus | e296d3e | 2020-03-05 17:56:31 +0100 | [diff] [blame] | 2602 | if (!isttest(reason)) { |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2603 | const char *str = http_get_reason(status); |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 2604 | reason = ist(str); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2605 | } |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2606 | |
Christopher Faulet | bde2c4c | 2020-08-31 16:43:34 +0200 | [diff] [blame] | 2607 | if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason)) |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2608 | return -1; |
Willy Tarreau | 640e253 | 2023-01-10 14:50:44 +0100 | [diff] [blame] | 2609 | s->txn->status = status; |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2610 | return 0; |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2611 | } |
| 2612 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2613 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
| 2614 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 2615 | * further processing of the request (auth, deny, ...), and defaults to |
| 2616 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 2617 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 2618 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 2619 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 2620 | * status. |
| 2621 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2622 | static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, |
| 2623 | struct list *rules, struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2624 | { |
| 2625 | struct session *sess = strm_sess(s); |
| 2626 | struct http_txn *txn = s->txn; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2627 | struct act_rule *rule; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2628 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2629 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2630 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2631 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2632 | * resume condition. If a resume is needed it is always in the action |
| 2633 | * and never in the ACL or converters. In this case, we initialise the |
| 2634 | * current rule, and go to the action execution point. |
| 2635 | */ |
| 2636 | if (s->current_rule) { |
| 2637 | rule = s->current_rule; |
| 2638 | s->current_rule = NULL; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2639 | 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] | 2640 | goto resume_execution; |
| 2641 | } |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2642 | 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] | 2643 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2644 | restart: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2645 | /* start the ruleset evaluation in strict mode */ |
| 2646 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2647 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2648 | list_for_each_entry(rule, s->current_rule_list, list) { |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2649 | /* check optional condition */ |
| 2650 | if (rule->cond) { |
| 2651 | int ret; |
| 2652 | |
| 2653 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 2654 | ret = acl_pass(ret); |
| 2655 | |
| 2656 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2657 | ret = !ret; |
| 2658 | |
| 2659 | if (!ret) /* condition not matched */ |
| 2660 | continue; |
| 2661 | } |
| 2662 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2663 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2664 | resume_execution: |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 2665 | if (rule->kw->flags & KWF_EXPERIMENTAL) |
| 2666 | mark_tainted(TAINTED_ACTION_EXP_EXECUTED); |
| 2667 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2668 | /* Always call the action function if defined */ |
| 2669 | if (rule->action_ptr) { |
| 2670 | if ((s->req.flags & CF_READ_ERROR) || |
Christopher Faulet | 446d803 | 2022-12-12 07:53:18 +0100 | [diff] [blame] | 2671 | ((s->req.flags & CF_SHUTR) && |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2672 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2673 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2674 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2675 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2676 | case ACT_RET_CONT: |
| 2677 | break; |
| 2678 | case ACT_RET_STOP: |
| 2679 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2680 | s->last_rule_file = rule->conf.file; |
| 2681 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2682 | goto end; |
| 2683 | case ACT_RET_YIELD: |
| 2684 | s->current_rule = rule; |
| 2685 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2686 | goto end; |
| 2687 | case ACT_RET_ERR: |
| 2688 | rule_ret = HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2689 | s->last_rule_file = rule->conf.file; |
| 2690 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2691 | goto end; |
| 2692 | case ACT_RET_DONE: |
| 2693 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2694 | s->last_rule_file = rule->conf.file; |
| 2695 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2696 | goto end; |
| 2697 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 2698 | if (txn->status == -1) |
| 2699 | txn->status = 403; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2700 | rule_ret = HTTP_RULE_RES_DENY; |
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_ABRT: |
| 2705 | rule_ret = HTTP_RULE_RES_ABRT; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2706 | s->last_rule_file = rule->conf.file; |
| 2707 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2708 | goto end; |
| 2709 | case ACT_RET_INV: |
| 2710 | rule_ret = HTTP_RULE_RES_BADREQ; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2711 | s->last_rule_file = rule->conf.file; |
| 2712 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2713 | goto end; |
| 2714 | } |
| 2715 | continue; /* eval the next rule */ |
| 2716 | } |
| 2717 | |
| 2718 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2719 | switch (rule->action) { |
| 2720 | case ACT_ACTION_ALLOW: |
| 2721 | rule_ret = HTTP_RULE_RES_STOP; |
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 | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2724 | goto end; |
| 2725 | |
| 2726 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2727 | txn->status = rule->arg.http_reply->status; |
| 2728 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2729 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2730 | s->last_rule_file = rule->conf.file; |
| 2731 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2732 | goto end; |
| 2733 | |
| 2734 | case ACT_HTTP_REQ_TARPIT: |
| 2735 | txn->flags |= TX_CLTARPIT; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2736 | txn->status = rule->arg.http_reply->status; |
| 2737 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2738 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2739 | s->last_rule_file = rule->conf.file; |
| 2740 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2741 | goto end; |
| 2742 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2743 | case ACT_HTTP_REDIR: { |
| 2744 | int ret = http_apply_redirect_rule(rule->arg.redir, s, txn); |
| 2745 | |
| 2746 | if (ret == 2) // 2 == skip |
| 2747 | break; |
| 2748 | |
| 2749 | rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2750 | s->last_rule_file = rule->conf.file; |
| 2751 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2752 | goto end; |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2753 | } |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2754 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2755 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2756 | default: |
| 2757 | break; |
| 2758 | } |
| 2759 | } |
| 2760 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2761 | if (def_rules && s->current_rule_list == def_rules) { |
| 2762 | s->current_rule_list = rules; |
| 2763 | goto restart; |
| 2764 | } |
| 2765 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2766 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2767 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2768 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2769 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2770 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2771 | /* we reached the end of the rules, nothing to report */ |
| 2772 | return rule_ret; |
| 2773 | } |
| 2774 | |
| 2775 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 2776 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 2777 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 2778 | * is returned, the process can continue the evaluation of next rule list. If |
| 2779 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 2780 | * 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] | 2781 | * must be returned. If *YIELD is returned, the caller must call again the |
| 2782 | * function with the same context. |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2783 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2784 | static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules, |
| 2785 | struct list *rules, struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2786 | { |
| 2787 | struct session *sess = strm_sess(s); |
| 2788 | struct http_txn *txn = s->txn; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2789 | struct act_rule *rule; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2790 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2791 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2792 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2793 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2794 | * resume condition. If a resume is needed it is always in the action |
| 2795 | * and never in the ACL or converters. In this case, we initialise the |
| 2796 | * current rule, and go to the action execution point. |
| 2797 | */ |
| 2798 | if (s->current_rule) { |
| 2799 | rule = s->current_rule; |
| 2800 | s->current_rule = NULL; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2801 | 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] | 2802 | goto resume_execution; |
| 2803 | } |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2804 | s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules); |
| 2805 | |
| 2806 | restart: |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2807 | |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2808 | /* start the ruleset evaluation in strict mode */ |
| 2809 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2810 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2811 | list_for_each_entry(rule, s->current_rule_list, list) { |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2812 | /* check optional condition */ |
| 2813 | if (rule->cond) { |
| 2814 | int ret; |
| 2815 | |
| 2816 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 2817 | ret = acl_pass(ret); |
| 2818 | |
| 2819 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2820 | ret = !ret; |
| 2821 | |
| 2822 | if (!ret) /* condition not matched */ |
| 2823 | continue; |
| 2824 | } |
| 2825 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2826 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2827 | resume_execution: |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 2828 | if (rule->kw->flags & KWF_EXPERIMENTAL) |
| 2829 | mark_tainted(TAINTED_ACTION_EXP_EXECUTED); |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2830 | |
| 2831 | /* Always call the action function if defined */ |
| 2832 | if (rule->action_ptr) { |
| 2833 | if ((s->req.flags & CF_READ_ERROR) || |
Christopher Faulet | 446d803 | 2022-12-12 07:53:18 +0100 | [diff] [blame] | 2834 | ((s->req.flags & CF_SHUTR) && |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2835 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2836 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2837 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2838 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2839 | case ACT_RET_CONT: |
| 2840 | break; |
| 2841 | case ACT_RET_STOP: |
| 2842 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2843 | s->last_rule_file = rule->conf.file; |
| 2844 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2845 | goto end; |
| 2846 | case ACT_RET_YIELD: |
| 2847 | s->current_rule = rule; |
| 2848 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2849 | goto end; |
| 2850 | case ACT_RET_ERR: |
| 2851 | rule_ret = HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2852 | s->last_rule_file = rule->conf.file; |
| 2853 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2854 | goto end; |
| 2855 | case ACT_RET_DONE: |
| 2856 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2857 | s->last_rule_file = rule->conf.file; |
| 2858 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2859 | goto end; |
| 2860 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 2861 | if (txn->status == -1) |
| 2862 | txn->status = 502; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2863 | rule_ret = HTTP_RULE_RES_DENY; |
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_ABRT: |
| 2868 | rule_ret = HTTP_RULE_RES_ABRT; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2869 | s->last_rule_file = rule->conf.file; |
| 2870 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2871 | goto end; |
| 2872 | case ACT_RET_INV: |
| 2873 | rule_ret = HTTP_RULE_RES_BADREQ; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2874 | s->last_rule_file = rule->conf.file; |
| 2875 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2876 | goto end; |
| 2877 | } |
| 2878 | continue; /* eval the next rule */ |
| 2879 | } |
| 2880 | |
| 2881 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2882 | switch (rule->action) { |
| 2883 | case ACT_ACTION_ALLOW: |
| 2884 | rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
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 | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2887 | goto end; |
| 2888 | |
| 2889 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2890 | txn->status = rule->arg.http_reply->status; |
| 2891 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 2892 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2893 | s->last_rule_file = rule->conf.file; |
| 2894 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2895 | goto end; |
| 2896 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2897 | case ACT_HTTP_REDIR: { |
| 2898 | int ret = http_apply_redirect_rule(rule->arg.redir, s, txn); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2899 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2900 | if (ret == 2) // 2 == skip |
| 2901 | break; |
| 2902 | |
| 2903 | rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2904 | s->last_rule_file = rule->conf.file; |
| 2905 | s->last_rule_line = rule->conf.line; |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2906 | goto end; |
| 2907 | } |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2908 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2909 | default: |
| 2910 | break; |
| 2911 | } |
| 2912 | } |
| 2913 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2914 | if (def_rules && s->current_rule_list == def_rules) { |
| 2915 | s->current_rule_list = rules; |
| 2916 | goto restart; |
| 2917 | } |
| 2918 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2919 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2920 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2921 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2922 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2923 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2924 | /* we reached the end of the rules, nothing to report */ |
| 2925 | return rule_ret; |
| 2926 | } |
| 2927 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2928 | /* Executes backend and frontend http-after-response rules for the stream <s>, |
| 2929 | * in that order. it return 1 on success and 0 on error. It is the caller |
| 2930 | * responsibility to catch error or ignore it. If it catches it, this function |
| 2931 | * may be called a second time, for the internal error. |
| 2932 | */ |
| 2933 | int http_eval_after_res_rules(struct stream *s) |
| 2934 | { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2935 | struct list *def_rules, *rules; |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2936 | struct session *sess = s->sess; |
| 2937 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 2938 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 2939 | /* Eval after-response ruleset only if the reply is not const */ |
| 2940 | if (s->txn->flags & TX_CONST_REPLY) |
| 2941 | goto end; |
| 2942 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2943 | /* prune the request variables if not already done and swap to the response variables. */ |
| 2944 | if (s->vars_reqres.scope != SCOPE_RES) { |
| 2945 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 2946 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | b7bfcb3 | 2021-08-31 08:13:25 +0200 | [diff] [blame] | 2947 | vars_init_head(&s->vars_reqres, SCOPE_RES); |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2948 | } |
| 2949 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2950 | def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL); |
| 2951 | rules = &s->be->http_after_res_rules; |
| 2952 | |
| 2953 | ret = http_res_get_intercept_rule(s->be, def_rules, rules, s); |
Christopher Faulet | 4c5a591 | 2021-11-09 17:48:39 +0100 | [diff] [blame] | 2954 | 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] | 2955 | def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL); |
| 2956 | rules = &sess->fe->http_after_res_rules; |
| 2957 | ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s); |
| 2958 | } |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2959 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 2960 | end: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2961 | /* All other codes than CONTINUE, STOP or DONE are forbidden */ |
| 2962 | return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE); |
| 2963 | } |
| 2964 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 2965 | /* |
| 2966 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 2967 | * desirable to call it only when needed. This code is quite complex because |
| 2968 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 2969 | * highly recommended not to touch this part without a good reason ! |
| 2970 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2971 | 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] | 2972 | { |
| 2973 | struct session *sess = s->sess; |
| 2974 | struct http_txn *txn = s->txn; |
| 2975 | struct htx *htx; |
| 2976 | struct http_hdr_ctx ctx; |
| 2977 | char *hdr_beg, *hdr_end, *del_from; |
| 2978 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
| 2979 | int preserve_hdr; |
| 2980 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2981 | htx = htxbuf(&req->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 2982 | ctx.blk = NULL; |
| 2983 | while (http_find_header(htx, ist("Cookie"), &ctx, 1)) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 2984 | int is_first = 1; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 2985 | del_from = NULL; /* nothing to be deleted */ |
| 2986 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 2987 | |
| 2988 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 2989 | * attributes whose name begin with a '$', and associate them with |
| 2990 | * the right cookie, if we want to delete this cookie. |
| 2991 | * So there are 3 cases for each cookie read : |
| 2992 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 2993 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 2994 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 2995 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 2996 | * "special" cookie. |
| 2997 | * At the end of loop, if a "special" cookie remains, we may have to |
| 2998 | * remove it. If no application cookie persists in the header, we |
| 2999 | * *MUST* delete it. |
| 3000 | * |
| 3001 | * Note: RFC2965 is unclear about the processing of spaces around |
| 3002 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 3003 | * the RFC explicitly allows spaces before it, and not within the |
| 3004 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 3005 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 3006 | * after the equal sign too, resulting in some (rare) buggy |
| 3007 | * implementations trying to do that. So let's do what servers do. |
| 3008 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 3009 | * allowed quoted strings in values, with any possible character |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3010 | * after a backslash, including control chars and delimiters, which |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3011 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 3012 | * within values even without quotes. |
| 3013 | * |
| 3014 | * We have to keep multiple pointers in order to support cookie |
| 3015 | * removal at the beginning, middle or end of header without |
| 3016 | * corrupting the header. All of these headers are valid : |
| 3017 | * |
| 3018 | * hdr_beg hdr_end |
| 3019 | * | | |
| 3020 | * v | |
| 3021 | * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 | |
| 3022 | * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v |
| 3023 | * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3 |
| 3024 | * | | | | | | | |
| 3025 | * | | | | | | | |
| 3026 | * | | | | | | +--> next |
| 3027 | * | | | | | +----> val_end |
| 3028 | * | | | | +-----------> val_beg |
| 3029 | * | | | +--------------> equal |
| 3030 | * | | +----------------> att_end |
| 3031 | * | +---------------------> att_beg |
| 3032 | * +--------------------------> prev |
| 3033 | * |
| 3034 | */ |
| 3035 | hdr_beg = ctx.value.ptr; |
| 3036 | hdr_end = hdr_beg + ctx.value.len; |
| 3037 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3038 | /* Iterate through all cookies on this line */ |
| 3039 | |
| 3040 | /* find att_beg */ |
| 3041 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3042 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3043 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3044 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3045 | |
| 3046 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3047 | att_beg++; |
| 3048 | |
| 3049 | /* find att_end : this is the first character after the last non |
| 3050 | * space before the equal. It may be equal to hdr_end. |
| 3051 | */ |
| 3052 | equal = att_end = att_beg; |
| 3053 | while (equal < hdr_end) { |
| 3054 | if (*equal == '=' || *equal == ',' || *equal == ';') |
| 3055 | break; |
| 3056 | if (HTTP_IS_SPHT(*equal++)) |
| 3057 | continue; |
| 3058 | att_end = equal; |
| 3059 | } |
| 3060 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3061 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3062 | * is between <att_beg> and <equal>, both may be identical. |
| 3063 | */ |
| 3064 | /* look for end of cookie if there is an equal sign */ |
| 3065 | if (equal < hdr_end && *equal == '=') { |
| 3066 | /* look for the beginning of the value */ |
| 3067 | val_beg = equal + 1; |
| 3068 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3069 | val_beg++; |
| 3070 | |
| 3071 | /* find the end of the value, respecting quotes */ |
| 3072 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3073 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3074 | /* 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] | 3075 | val_end = next; |
| 3076 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3077 | val_end--; |
| 3078 | } |
| 3079 | else |
| 3080 | val_beg = val_end = next = equal; |
| 3081 | |
| 3082 | /* We have nothing to do with attributes beginning with |
| 3083 | * '$'. However, they will automatically be removed if a |
| 3084 | * header before them is removed, since they're supposed |
| 3085 | * to be linked together. |
| 3086 | */ |
| 3087 | if (*att_beg == '$') |
| 3088 | continue; |
| 3089 | |
| 3090 | /* Ignore cookies with no equal sign */ |
| 3091 | if (equal == next) { |
| 3092 | /* This is not our cookie, so we must preserve it. But if we already |
| 3093 | * scheduled another cookie for removal, we cannot remove the |
| 3094 | * complete header, but we can remove the previous block itself. |
| 3095 | */ |
| 3096 | preserve_hdr = 1; |
| 3097 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3098 | 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] | 3099 | val_end += delta; |
| 3100 | next += delta; |
| 3101 | hdr_end += delta; |
| 3102 | prev = del_from; |
| 3103 | del_from = NULL; |
| 3104 | } |
| 3105 | continue; |
| 3106 | } |
| 3107 | |
| 3108 | /* if there are spaces around the equal sign, we need to |
| 3109 | * strip them otherwise we'll get trouble for cookie captures, |
| 3110 | * or even for rewrites. Since this happens extremely rarely, |
| 3111 | * it does not hurt performance. |
| 3112 | */ |
| 3113 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3114 | int stripped_before = 0; |
| 3115 | int stripped_after = 0; |
| 3116 | |
| 3117 | if (att_end != equal) { |
| 3118 | memmove(att_end, equal, hdr_end - equal); |
| 3119 | stripped_before = (att_end - equal); |
| 3120 | equal += stripped_before; |
| 3121 | val_beg += stripped_before; |
| 3122 | } |
| 3123 | |
| 3124 | if (val_beg > equal + 1) { |
| 3125 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3126 | stripped_after = (equal + 1) - val_beg; |
| 3127 | val_beg += stripped_after; |
| 3128 | stripped_before += stripped_after; |
| 3129 | } |
| 3130 | |
| 3131 | val_end += stripped_before; |
| 3132 | next += stripped_before; |
| 3133 | hdr_end += stripped_before; |
| 3134 | } |
| 3135 | /* now everything is as on the diagram above */ |
| 3136 | |
| 3137 | /* First, let's see if we want to capture this cookie. We check |
| 3138 | * that we don't already have a client side cookie, because we |
| 3139 | * can only capture one. Also as an optimisation, we ignore |
| 3140 | * cookies shorter than the declared name. |
| 3141 | */ |
| 3142 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 3143 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3144 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3145 | int log_len = val_end - att_beg; |
| 3146 | |
| 3147 | if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3148 | ha_alert("HTTP logging : out of memory.\n"); |
| 3149 | } else { |
| 3150 | if (log_len > sess->fe->capture_len) |
| 3151 | log_len = sess->fe->capture_len; |
| 3152 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 3153 | txn->cli_cookie[log_len] = 0; |
| 3154 | } |
| 3155 | } |
| 3156 | |
| 3157 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 3158 | * following form : |
| 3159 | * |
| 3160 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
| 3161 | * |
| 3162 | * For cookies in prefix mode, the form is : |
| 3163 | * |
| 3164 | * Cookie: NAME=SRV~VALUE |
| 3165 | */ |
| 3166 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3167 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3168 | struct server *srv = s->be->srv; |
| 3169 | char *delim; |
| 3170 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3171 | /* 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] | 3172 | * have the server ID between val_beg and delim, and the original cookie between |
| 3173 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 3174 | * |
| 3175 | * hdr_beg |
| 3176 | * | |
| 3177 | * v |
| 3178 | * NAME=SRV; # in all but prefix modes |
| 3179 | * NAME=SRV~OPAQUE ; # in prefix mode |
| 3180 | * || || | |+-> next |
| 3181 | * || || | +--> val_end |
| 3182 | * || || +---------> delim |
| 3183 | * || |+------------> val_beg |
| 3184 | * || +-------------> att_end = equal |
| 3185 | * |+-----------------> att_beg |
| 3186 | * +------------------> prev |
| 3187 | * |
| 3188 | */ |
| 3189 | if (s->be->ck_opts & PR_CK_PFX) { |
| 3190 | for (delim = val_beg; delim < val_end; delim++) |
| 3191 | if (*delim == COOKIE_DELIM) |
| 3192 | break; |
| 3193 | } |
| 3194 | else { |
| 3195 | char *vbar1; |
| 3196 | delim = val_end; |
| 3197 | /* Now check if the cookie contains a date field, which would |
| 3198 | * appear after a vertical bar ('|') just after the server name |
| 3199 | * and before the delimiter. |
| 3200 | */ |
| 3201 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 3202 | if (vbar1) { |
| 3203 | /* OK, so left of the bar is the server's cookie and |
| 3204 | * right is the last seen date. It is a base64 encoded |
| 3205 | * 30-bit value representing the UNIX date since the |
| 3206 | * epoch in 4-second quantities. |
| 3207 | */ |
| 3208 | int val; |
| 3209 | delim = vbar1++; |
| 3210 | if (val_end - vbar1 >= 5) { |
| 3211 | val = b64tos30(vbar1); |
| 3212 | if (val > 0) |
| 3213 | txn->cookie_last_date = val << 2; |
| 3214 | } |
| 3215 | /* look for a second vertical bar */ |
| 3216 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 3217 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 3218 | val = b64tos30(vbar1 + 1); |
| 3219 | if (val > 0) |
| 3220 | txn->cookie_first_date = val << 2; |
| 3221 | } |
| 3222 | } |
| 3223 | } |
| 3224 | |
| 3225 | /* if the cookie has an expiration date and the proxy wants to check |
| 3226 | * it, then we do that now. We first check if the cookie is too old, |
| 3227 | * then only if it has expired. We detect strict overflow because the |
| 3228 | * time resolution here is not great (4 seconds). Cookies with dates |
| 3229 | * in the future are ignored if their offset is beyond one day. This |
| 3230 | * allows an admin to fix timezone issues without expiring everyone |
| 3231 | * and at the same time avoids keeping unwanted side effects for too |
| 3232 | * long. |
| 3233 | */ |
| 3234 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 3235 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) || |
| 3236 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
| 3237 | txn->flags &= ~TX_CK_MASK; |
| 3238 | txn->flags |= TX_CK_OLD; |
| 3239 | delim = val_beg; // let's pretend we have not found the cookie |
| 3240 | txn->cookie_first_date = 0; |
| 3241 | txn->cookie_last_date = 0; |
| 3242 | } |
| 3243 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 3244 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) || |
| 3245 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
| 3246 | txn->flags &= ~TX_CK_MASK; |
| 3247 | txn->flags |= TX_CK_EXPIRED; |
| 3248 | delim = val_beg; // let's pretend we have not found the cookie |
| 3249 | txn->cookie_first_date = 0; |
| 3250 | txn->cookie_last_date = 0; |
| 3251 | } |
| 3252 | |
| 3253 | /* Here, we'll look for the first running server which supports the cookie. |
| 3254 | * This allows to share a same cookie between several servers, for example |
| 3255 | * to dedicate backup servers to specific servers only. |
| 3256 | * However, to prevent clients from sticking to cookie-less backup server |
| 3257 | * when they have incidentely learned an empty cookie, we simply ignore |
| 3258 | * empty cookies and mark them as invalid. |
| 3259 | * The same behaviour is applied when persistence must be ignored. |
| 3260 | */ |
| 3261 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3262 | srv = NULL; |
| 3263 | |
| 3264 | while (srv) { |
| 3265 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 3266 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 3267 | if ((srv->cur_state != SRV_ST_STOPPED) || |
| 3268 | (s->be->options & PR_O_PERSIST) || |
| 3269 | (s->flags & SF_FORCE_PRST)) { |
| 3270 | /* we found the server and we can use it */ |
| 3271 | txn->flags &= ~TX_CK_MASK; |
| 3272 | txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN; |
| 3273 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
| 3274 | s->target = &srv->obj_type; |
| 3275 | break; |
| 3276 | } else { |
| 3277 | /* we found a server, but it's down, |
| 3278 | * mark it as such and go on in case |
| 3279 | * another one is available. |
| 3280 | */ |
| 3281 | txn->flags &= ~TX_CK_MASK; |
| 3282 | txn->flags |= TX_CK_DOWN; |
| 3283 | } |
| 3284 | } |
| 3285 | srv = srv->next; |
| 3286 | } |
| 3287 | |
| 3288 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
| 3289 | /* no server matched this cookie or we deliberately skipped it */ |
| 3290 | txn->flags &= ~TX_CK_MASK; |
| 3291 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3292 | txn->flags |= TX_CK_UNUSED; |
| 3293 | else |
| 3294 | txn->flags |= TX_CK_INVALID; |
| 3295 | } |
| 3296 | |
| 3297 | /* depending on the cookie mode, we may have to either : |
| 3298 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3299 | * the server never sees it ; |
| 3300 | * - 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] | 3301 | * application cookie so that it does not get accidentally removed later, |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3302 | * if we're in cookie prefix mode |
| 3303 | */ |
| 3304 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
| 3305 | int delta; /* negative */ |
| 3306 | |
| 3307 | memmove(val_beg, delim + 1, hdr_end - (delim + 1)); |
| 3308 | delta = val_beg - (delim + 1); |
| 3309 | val_end += delta; |
| 3310 | next += delta; |
| 3311 | hdr_end += delta; |
| 3312 | del_from = NULL; |
| 3313 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 3314 | } |
| 3315 | else if (del_from == NULL && |
| 3316 | (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) { |
| 3317 | del_from = prev; |
| 3318 | } |
| 3319 | } |
| 3320 | else { |
| 3321 | /* This is not our cookie, so we must preserve it. But if we already |
| 3322 | * scheduled another cookie for removal, we cannot remove the |
| 3323 | * complete header, but we can remove the previous block itself. |
| 3324 | */ |
| 3325 | preserve_hdr = 1; |
| 3326 | |
| 3327 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3328 | 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] | 3329 | if (att_beg >= del_from) |
| 3330 | att_beg += delta; |
| 3331 | if (att_end >= del_from) |
| 3332 | att_end += delta; |
| 3333 | val_beg += delta; |
| 3334 | val_end += delta; |
| 3335 | next += delta; |
| 3336 | hdr_end += delta; |
| 3337 | prev = del_from; |
| 3338 | del_from = NULL; |
| 3339 | } |
| 3340 | } |
| 3341 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3342 | } /* for each cookie */ |
| 3343 | |
| 3344 | |
| 3345 | /* There are no more cookies on this line. |
| 3346 | * We may still have one (or several) marked for deletion at the |
| 3347 | * end of the line. We must do this now in two ways : |
| 3348 | * - if some cookies must be preserved, we only delete from the |
| 3349 | * mark to the end of line ; |
| 3350 | * - if nothing needs to be preserved, simply delete the whole header |
| 3351 | */ |
| 3352 | if (del_from) { |
| 3353 | hdr_end = (preserve_hdr ? del_from : hdr_beg); |
| 3354 | } |
| 3355 | if ((hdr_end - hdr_beg) != ctx.value.len) { |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3356 | if (hdr_beg != hdr_end) |
| 3357 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3358 | else |
| 3359 | http_remove_header(htx, &ctx); |
| 3360 | } |
| 3361 | } /* for each "Cookie header */ |
| 3362 | } |
| 3363 | |
| 3364 | /* |
| 3365 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 3366 | * desirable to call it only when needed. This function is also used when we |
| 3367 | * just need to know if there is a cookie (eg: for check-cache). |
| 3368 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3369 | 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] | 3370 | { |
| 3371 | struct session *sess = s->sess; |
| 3372 | struct http_txn *txn = s->txn; |
| 3373 | struct htx *htx; |
| 3374 | struct http_hdr_ctx ctx; |
| 3375 | struct server *srv; |
| 3376 | char *hdr_beg, *hdr_end; |
| 3377 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3378 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3379 | htx = htxbuf(&res->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3380 | |
| 3381 | ctx.blk = NULL; |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3382 | while (http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3383 | int is_first = 1; |
| 3384 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3385 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 3386 | * <prev> points to the colon. |
| 3387 | */ |
| 3388 | txn->flags |= TX_SCK_PRESENT; |
| 3389 | |
| 3390 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 3391 | * check-cache is enabled) and we are not interested in checking |
| 3392 | * them. Warning, the cookie capture is declared in the frontend. |
| 3393 | */ |
| 3394 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
| 3395 | break; |
| 3396 | |
| 3397 | /* OK so now we know we have to process this response cookie. |
| 3398 | * The format of the Set-Cookie header is slightly different |
| 3399 | * from the format of the Cookie header in that it does not |
| 3400 | * support the comma as a cookie delimiter (thus the header |
| 3401 | * cannot be folded) because the Expires attribute described in |
| 3402 | * the original Netscape's spec may contain an unquoted date |
| 3403 | * with a comma inside. We have to live with this because |
| 3404 | * many browsers don't support Max-Age and some browsers don't |
| 3405 | * support quoted strings. However the Set-Cookie2 header is |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3406 | * clean but basically nobody supports it. |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3407 | * |
| 3408 | * We have to keep multiple pointers in order to support cookie |
| 3409 | * removal at the beginning, middle or end of header without |
| 3410 | * corrupting the header (in case of set-cookie2). A special |
| 3411 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 3412 | * fields after the first semi-colon. The <next> pointer points |
| 3413 | * either to the end of line (set-cookie) or next unquoted comma |
| 3414 | * (set-cookie2). All of these headers are valid : |
| 3415 | * |
| 3416 | * hdr_beg hdr_end |
| 3417 | * | | |
| 3418 | * v | |
| 3419 | * NAME1 = VALUE 1 ; Secure; Path="/" | |
| 3420 | * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v |
| 3421 | * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT |
| 3422 | * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard |
| 3423 | * | | | | | | | | |
| 3424 | * | | | | | | | +-> next |
| 3425 | * | | | | | | +------------> scav |
| 3426 | * | | | | | +--------------> val_end |
| 3427 | * | | | | +--------------------> val_beg |
| 3428 | * | | | +----------------------> equal |
| 3429 | * | | +------------------------> att_end |
| 3430 | * | +----------------------------> att_beg |
| 3431 | * +------------------------------> prev |
| 3432 | * -------------------------------> hdr_beg |
| 3433 | */ |
| 3434 | hdr_beg = ctx.value.ptr; |
| 3435 | hdr_end = hdr_beg + ctx.value.len; |
| 3436 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3437 | |
| 3438 | /* Iterate through all cookies on this line */ |
| 3439 | |
| 3440 | /* find att_beg */ |
| 3441 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3442 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3443 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3444 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3445 | |
| 3446 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3447 | att_beg++; |
| 3448 | |
| 3449 | /* find att_end : this is the first character after the last non |
| 3450 | * space before the equal. It may be equal to hdr_end. |
| 3451 | */ |
| 3452 | equal = att_end = att_beg; |
| 3453 | |
| 3454 | while (equal < hdr_end) { |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3455 | if (*equal == '=' || *equal == ';') |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3456 | break; |
| 3457 | if (HTTP_IS_SPHT(*equal++)) |
| 3458 | continue; |
| 3459 | att_end = equal; |
| 3460 | } |
| 3461 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3462 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3463 | * is between <att_beg> and <equal>, both may be identical. |
| 3464 | */ |
| 3465 | |
| 3466 | /* look for end of cookie if there is an equal sign */ |
| 3467 | if (equal < hdr_end && *equal == '=') { |
| 3468 | /* look for the beginning of the value */ |
| 3469 | val_beg = equal + 1; |
| 3470 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3471 | val_beg++; |
| 3472 | |
| 3473 | /* find the end of the value, respecting quotes */ |
| 3474 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3475 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3476 | /* 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] | 3477 | val_end = next; |
| 3478 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3479 | val_end--; |
| 3480 | } |
| 3481 | else { |
| 3482 | /* <equal> points to next comma, semi-colon or EOL */ |
| 3483 | val_beg = val_end = next = equal; |
| 3484 | } |
| 3485 | |
| 3486 | if (next < hdr_end) { |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3487 | /* For Set-Cookie, since commas are permitted |
| 3488 | * in values, skip to the end. |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3489 | */ |
Willy Tarreau | aa1909e | 2022-11-14 18:58:35 +0100 | [diff] [blame] | 3490 | next = hdr_end; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | /* Now everything is as on the diagram above */ |
| 3494 | |
| 3495 | /* Ignore cookies with no equal sign */ |
| 3496 | if (equal == val_end) |
| 3497 | continue; |
| 3498 | |
| 3499 | /* If there are spaces around the equal sign, we need to |
| 3500 | * strip them otherwise we'll get trouble for cookie captures, |
| 3501 | * or even for rewrites. Since this happens extremely rarely, |
| 3502 | * it does not hurt performance. |
| 3503 | */ |
| 3504 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3505 | int stripped_before = 0; |
| 3506 | int stripped_after = 0; |
| 3507 | |
| 3508 | if (att_end != equal) { |
| 3509 | memmove(att_end, equal, hdr_end - equal); |
| 3510 | stripped_before = (att_end - equal); |
| 3511 | equal += stripped_before; |
| 3512 | val_beg += stripped_before; |
| 3513 | } |
| 3514 | |
| 3515 | if (val_beg > equal + 1) { |
| 3516 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3517 | stripped_after = (equal + 1) - val_beg; |
| 3518 | val_beg += stripped_after; |
| 3519 | stripped_before += stripped_after; |
| 3520 | } |
| 3521 | |
| 3522 | val_end += stripped_before; |
| 3523 | next += stripped_before; |
| 3524 | hdr_end += stripped_before; |
| 3525 | |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3526 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3527 | ctx.value.len = hdr_end - hdr_beg; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3528 | } |
| 3529 | |
| 3530 | /* First, let's see if we want to capture this cookie. We check |
| 3531 | * that we don't already have a server side cookie, because we |
| 3532 | * can only capture one. Also as an optimisation, we ignore |
| 3533 | * cookies shorter than the declared name. |
| 3534 | */ |
| 3535 | if (sess->fe->capture_name != NULL && |
| 3536 | txn->srv_cookie == NULL && |
| 3537 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3538 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3539 | int log_len = val_end - att_beg; |
| 3540 | if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3541 | ha_alert("HTTP logging : out of memory.\n"); |
| 3542 | } |
| 3543 | else { |
| 3544 | if (log_len > sess->fe->capture_len) |
| 3545 | log_len = sess->fe->capture_len; |
| 3546 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 3547 | txn->srv_cookie[log_len] = 0; |
| 3548 | } |
| 3549 | } |
| 3550 | |
| 3551 | srv = objt_server(s->target); |
| 3552 | /* now check if we need to process it for persistence */ |
| 3553 | if (!(s->flags & SF_IGNORE_PRST) && |
| 3554 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3555 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3556 | /* assume passive cookie by default */ |
| 3557 | txn->flags &= ~TX_SCK_MASK; |
| 3558 | txn->flags |= TX_SCK_FOUND; |
| 3559 | |
| 3560 | /* If the cookie is in insert mode on a known server, we'll delete |
| 3561 | * this occurrence because we'll insert another one later. |
| 3562 | * We'll delete it too if the "indirect" option is set and we're in |
| 3563 | * a direct access. |
| 3564 | */ |
| 3565 | if (s->be->ck_opts & PR_CK_PSV) { |
| 3566 | /* The "preserve" flag was set, we don't want to touch the |
| 3567 | * server's cookie. |
| 3568 | */ |
| 3569 | } |
| 3570 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
| 3571 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
| 3572 | /* this cookie must be deleted */ |
| 3573 | if (prev == hdr_beg && next == hdr_end) { |
| 3574 | /* whole header */ |
| 3575 | http_remove_header(htx, &ctx); |
| 3576 | /* note: while both invalid now, <next> and <hdr_end> |
| 3577 | * are still equal, so the for() will stop as expected. |
| 3578 | */ |
| 3579 | } else { |
| 3580 | /* just remove the value */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3581 | int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3582 | next = prev; |
| 3583 | hdr_end += delta; |
| 3584 | } |
| 3585 | txn->flags &= ~TX_SCK_MASK; |
| 3586 | txn->flags |= TX_SCK_DELETED; |
| 3587 | /* and go on with next cookie */ |
| 3588 | } |
| 3589 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
| 3590 | /* replace bytes val_beg->val_end with the cookie name associated |
| 3591 | * with this server since we know it. |
| 3592 | */ |
| 3593 | int sliding, delta; |
| 3594 | |
| 3595 | ctx.value = ist2(val_beg, val_end - val_beg); |
| 3596 | ctx.lws_before = ctx.lws_after = 0; |
| 3597 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen)); |
| 3598 | delta = srv->cklen - (val_end - val_beg); |
| 3599 | sliding = (ctx.value.ptr - val_beg); |
| 3600 | hdr_beg += sliding; |
| 3601 | val_beg += sliding; |
| 3602 | next += sliding + delta; |
| 3603 | hdr_end += sliding + delta; |
| 3604 | |
| 3605 | txn->flags &= ~TX_SCK_MASK; |
| 3606 | txn->flags |= TX_SCK_REPLACED; |
| 3607 | } |
| 3608 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
| 3609 | /* insert the cookie name associated with this server |
| 3610 | * before existing cookie, and insert a delimiter between them.. |
| 3611 | */ |
| 3612 | int sliding, delta; |
| 3613 | ctx.value = ist2(val_beg, 0); |
| 3614 | ctx.lws_before = ctx.lws_after = 0; |
| 3615 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1)); |
| 3616 | delta = srv->cklen + 1; |
| 3617 | sliding = (ctx.value.ptr - val_beg); |
| 3618 | hdr_beg += sliding; |
| 3619 | val_beg += sliding; |
| 3620 | next += sliding + delta; |
| 3621 | hdr_end += sliding + delta; |
| 3622 | |
| 3623 | val_beg[srv->cklen] = COOKIE_DELIM; |
| 3624 | txn->flags &= ~TX_SCK_MASK; |
| 3625 | txn->flags |= TX_SCK_REPLACED; |
| 3626 | } |
| 3627 | } |
| 3628 | /* 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] | 3629 | * line when next != hdr_end (which should normally not happen |
| 3630 | * with set-cookie2 support removed). |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3631 | */ |
| 3632 | } |
| 3633 | } |
| 3634 | } |
| 3635 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3636 | /* |
| 3637 | * Parses the Cache-Control and Pragma request header fields to determine if |
| 3638 | * the request may be served from the cache and/or if it is cacheable. Updates |
| 3639 | * s->txn->flags. |
| 3640 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3641 | void http_check_request_for_cacheability(struct stream *s, struct channel *req) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3642 | { |
| 3643 | struct http_txn *txn = s->txn; |
| 3644 | struct htx *htx; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3645 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 3646 | int pragma_found, cc_found; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3647 | |
| 3648 | if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE) |
| 3649 | return; /* nothing more to do here */ |
| 3650 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3651 | htx = htxbuf(&req->buf); |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3652 | pragma_found = cc_found = 0; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3653 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3654 | /* Check "pragma" header for HTTP/1.0 compatibility. */ |
| 3655 | if (http_find_header(htx, ist("pragma"), &ctx, 1)) { |
| 3656 | if (isteqi(ctx.value, ist("no-cache"))) { |
| 3657 | pragma_found = 1; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3658 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3659 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3660 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3661 | ctx.blk = NULL; |
| 3662 | /* Don't use the cache and don't try to store if we found the |
| 3663 | * Authorization header */ |
| 3664 | if (http_find_header(htx, ist("authorization"), &ctx, 1)) { |
| 3665 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3666 | txn->flags |= TX_CACHE_IGNORE; |
| 3667 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3668 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3669 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3670 | /* Look for "cache-control" header and iterate over all the values |
| 3671 | * until we find one that specifies that caching is possible or not. */ |
| 3672 | ctx.blk = NULL; |
| 3673 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3674 | cc_found = 1; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3675 | /* We don't check the values after max-age, max-stale nor min-fresh, |
| 3676 | * we simply don't use the cache when they're specified. */ |
| 3677 | if (istmatchi(ctx.value, ist("max-age")) || |
| 3678 | istmatchi(ctx.value, ist("no-cache")) || |
| 3679 | istmatchi(ctx.value, ist("max-stale")) || |
| 3680 | istmatchi(ctx.value, ist("min-fresh"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3681 | txn->flags |= TX_CACHE_IGNORE; |
| 3682 | continue; |
| 3683 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3684 | if (istmatchi(ctx.value, ist("no-store"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3685 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3686 | continue; |
| 3687 | } |
| 3688 | } |
| 3689 | |
| 3690 | /* RFC7234#5.4: |
| 3691 | * When the Cache-Control header field is also present and |
| 3692 | * understood in a request, Pragma is ignored. |
| 3693 | * When the Cache-Control header field is not present in a |
| 3694 | * request, caches MUST consider the no-cache request |
| 3695 | * pragma-directive as having the same effect as if |
| 3696 | * "Cache-Control: no-cache" were present. |
| 3697 | */ |
| 3698 | if (!cc_found && pragma_found) |
| 3699 | txn->flags |= TX_CACHE_IGNORE; |
| 3700 | } |
| 3701 | |
| 3702 | /* |
| 3703 | * Check if response is cacheable or not. Updates s->txn->flags. |
| 3704 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3705 | void http_check_response_for_cacheability(struct stream *s, struct channel *res) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3706 | { |
| 3707 | struct http_txn *txn = s->txn; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3708 | struct http_hdr_ctx ctx = { .blk = NULL }; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3709 | struct htx *htx; |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3710 | int has_freshness_info = 0; |
| 3711 | int has_validator = 0; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3712 | |
| 3713 | if (txn->status < 200) { |
| 3714 | /* do not try to cache interim responses! */ |
| 3715 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3716 | return; |
| 3717 | } |
| 3718 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3719 | htx = htxbuf(&res->buf); |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3720 | /* Check "pragma" header for HTTP/1.0 compatibility. */ |
| 3721 | if (http_find_header(htx, ist("pragma"), &ctx, 1)) { |
| 3722 | if (isteqi(ctx.value, ist("no-cache"))) { |
| 3723 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3724 | return; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3725 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3726 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3727 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3728 | /* Look for "cache-control" header and iterate over all the values |
| 3729 | * until we find one that specifies that caching is possible or not. */ |
| 3730 | ctx.blk = NULL; |
| 3731 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
| 3732 | if (isteqi(ctx.value, ist("public"))) { |
| 3733 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3734 | continue; |
| 3735 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3736 | if (isteqi(ctx.value, ist("private")) || |
| 3737 | isteqi(ctx.value, ist("no-cache")) || |
| 3738 | isteqi(ctx.value, ist("no-store")) || |
| 3739 | isteqi(ctx.value, ist("max-age=0")) || |
| 3740 | isteqi(ctx.value, ist("s-maxage=0"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3741 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3742 | continue; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3743 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3744 | /* We might have a no-cache="set-cookie" form. */ |
| 3745 | if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) { |
| 3746 | txn->flags &= ~TX_CACHE_COOK; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3747 | continue; |
| 3748 | } |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3749 | |
| 3750 | if (istmatchi(ctx.value, ist("s-maxage")) || |
| 3751 | istmatchi(ctx.value, ist("max-age"))) { |
| 3752 | has_freshness_info = 1; |
| 3753 | continue; |
| 3754 | } |
| 3755 | } |
| 3756 | |
| 3757 | /* If no freshness information could be found in Cache-Control values, |
| 3758 | * look for an Expires header. */ |
| 3759 | if (!has_freshness_info) { |
| 3760 | ctx.blk = NULL; |
| 3761 | has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0); |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3762 | } |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3763 | |
| 3764 | /* If no freshness information could be found in Cache-Control or Expires |
| 3765 | * values, look for an explicit validator. */ |
| 3766 | if (!has_freshness_info) { |
| 3767 | ctx.blk = NULL; |
| 3768 | has_validator = 1; |
| 3769 | if (!http_find_header(htx, ist("etag"), &ctx, 0)) { |
| 3770 | ctx.blk = NULL; |
| 3771 | if (!http_find_header(htx, ist("last-modified"), &ctx, 0)) |
| 3772 | has_validator = 0; |
| 3773 | } |
| 3774 | } |
| 3775 | |
| 3776 | /* We won't store an entry that has neither a cache validator nor an |
| 3777 | * explicit expiration time, as suggested in RFC 7234#3. */ |
| 3778 | if (!has_freshness_info && !has_validator) |
| 3779 | txn->flags |= TX_CACHE_IGNORE; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3780 | } |
| 3781 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3782 | /* |
| 3783 | * In a GET, HEAD or POST request, check if the requested URI matches the stats uri |
| 3784 | * for the current backend. |
| 3785 | * |
| 3786 | * It is assumed that the request is either a HEAD, GET, or POST and that the |
| 3787 | * uri_auth field is valid. |
| 3788 | * |
| 3789 | * Returns 1 if stats should be provided, otherwise 0. |
| 3790 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3791 | static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend) |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3792 | { |
| 3793 | struct uri_auth *uri_auth = backend->uri_auth; |
| 3794 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3795 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3796 | struct ist uri; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3797 | |
| 3798 | if (!uri_auth) |
| 3799 | return 0; |
| 3800 | |
| 3801 | if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST) |
| 3802 | return 0; |
| 3803 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3804 | htx = htxbuf(&s->req.buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 3805 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3806 | uri = htx_sl_req_uri(sl); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 3807 | if (*uri_auth->uri_prefix == '/') { |
| 3808 | struct http_uri_parser parser = http_uri_parser_init(uri); |
| 3809 | uri = http_parse_path(&parser); |
| 3810 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3811 | |
| 3812 | /* check URI size */ |
| 3813 | if (uri_auth->uri_len > uri.len) |
| 3814 | return 0; |
| 3815 | |
| 3816 | if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
| 3817 | return 0; |
| 3818 | |
| 3819 | return 1; |
| 3820 | } |
| 3821 | |
| 3822 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3823 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3824 | * and program a response message if something was unexpected. It cannot fail |
| 3825 | * and always relies on the stats applet to complete the job. It does not touch |
| 3826 | * analysers nor counters, which are left to the caller. It does not touch |
| 3827 | * s->target which is supposed to already point to the stats applet. The caller |
| 3828 | * is expected to have already assigned an appctx to the stream. |
| 3829 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3830 | static int http_handle_stats(struct stream *s, struct channel *req) |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3831 | { |
| 3832 | struct stats_admin_rule *stats_admin_rule; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3833 | struct session *sess = s->sess; |
| 3834 | struct http_txn *txn = s->txn; |
| 3835 | struct http_msg *msg = &txn->req; |
| 3836 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3837 | const char *h, *lookup, *end; |
Willy Tarreau | 8e7c6e6 | 2022-05-18 17:58:02 +0200 | [diff] [blame] | 3838 | struct appctx *appctx = __sc_appctx(s->scb); |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3839 | struct show_stat_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3840 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3841 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3842 | |
Willy Tarreau | 41f8852 | 2022-05-03 18:39:27 +0200 | [diff] [blame] | 3843 | appctx->st1 = 0; |
Willy Tarreau | 6ef1648 | 2022-05-06 18:07:53 +0200 | [diff] [blame] | 3844 | ctx->state = STAT_STATE_INIT; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3845 | ctx->st_code = STAT_STATUS_INIT; |
| 3846 | ctx->flags |= uri_auth->flags; |
| 3847 | ctx->flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3848 | if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD)) |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3849 | ctx->flags |= STAT_CHUNKED; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3850 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3851 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 3852 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3853 | lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len; |
| 3854 | end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3855 | |
| 3856 | for (h = lookup; h <= end - 3; h++) { |
| 3857 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3858 | ctx->flags |= STAT_HIDE_DOWN; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3859 | break; |
| 3860 | } |
Amaury Denoyelle | 91e55ea | 2021-02-25 14:46:08 +0100 | [diff] [blame] | 3861 | } |
| 3862 | |
| 3863 | for (h = lookup; h <= end - 9; h++) { |
| 3864 | if (memcmp(h, ";no-maint", 9) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3865 | ctx->flags |= STAT_HIDE_MAINT; |
Willy Tarreau | 3e32036 | 2020-10-23 17:28:57 +0200 | [diff] [blame] | 3866 | break; |
| 3867 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3868 | } |
| 3869 | |
| 3870 | if (uri_auth->refresh) { |
| 3871 | for (h = lookup; h <= end - 10; h++) { |
| 3872 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3873 | ctx->flags |= STAT_NO_REFRESH; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3874 | break; |
| 3875 | } |
| 3876 | } |
| 3877 | } |
| 3878 | |
| 3879 | for (h = lookup; h <= end - 4; h++) { |
| 3880 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3881 | ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3882 | break; |
| 3883 | } |
| 3884 | } |
| 3885 | |
| 3886 | for (h = lookup; h <= end - 6; h++) { |
| 3887 | if (memcmp(h, ";typed", 6) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3888 | ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
| 3889 | ctx->flags |= STAT_FMT_TYPED; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3890 | break; |
| 3891 | } |
| 3892 | } |
| 3893 | |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 3894 | for (h = lookup; h <= end - 5; h++) { |
| 3895 | if (memcmp(h, ";json", 5) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3896 | ctx->flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
| 3897 | ctx->flags |= STAT_FMT_JSON; |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 3898 | break; |
| 3899 | } |
| 3900 | } |
| 3901 | |
| 3902 | for (h = lookup; h <= end - 12; h++) { |
| 3903 | if (memcmp(h, ";json-schema", 12) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3904 | ctx->flags &= ~STAT_FMT_MASK; |
| 3905 | ctx->flags |= STAT_JSON_SCHM; |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 3906 | break; |
| 3907 | } |
| 3908 | } |
| 3909 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3910 | for (h = lookup; h <= end - 8; h++) { |
| 3911 | if (memcmp(h, ";st=", 4) == 0) { |
| 3912 | int i; |
| 3913 | h += 4; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3914 | ctx->st_code = STAT_STATUS_UNKN; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3915 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3916 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3917 | ctx->st_code = i; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3918 | break; |
| 3919 | } |
| 3920 | } |
| 3921 | break; |
| 3922 | } |
| 3923 | } |
| 3924 | |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3925 | ctx->scope_str = 0; |
| 3926 | ctx->scope_len = 0; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3927 | for (h = lookup; h <= end - 8; h++) { |
| 3928 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 3929 | int itx = 0; |
| 3930 | const char *h2; |
| 3931 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 3932 | const char *err; |
| 3933 | |
| 3934 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 3935 | h2 = h; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3936 | ctx->scope_str = h2 - HTX_SL_REQ_UPTR(sl); |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 3937 | while (h < end) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3938 | if (*h == ';' || *h == '&' || *h == ' ') |
| 3939 | break; |
| 3940 | itx++; |
| 3941 | h++; |
| 3942 | } |
| 3943 | |
| 3944 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 3945 | itx = STAT_SCOPE_TXT_MAXLEN; |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3946 | ctx->scope_len = itx; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3947 | |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3948 | /* 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] | 3949 | memcpy(scope_txt, h2, itx); |
| 3950 | scope_txt[itx] = '\0'; |
| 3951 | err = invalid_char(scope_txt); |
| 3952 | if (err) { |
| 3953 | /* bad char in search text => clear scope */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3954 | ctx->scope_str = 0; |
| 3955 | ctx->scope_len = 0; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3956 | } |
| 3957 | break; |
| 3958 | } |
| 3959 | } |
| 3960 | |
| 3961 | /* now check whether we have some admin rules for this request */ |
| 3962 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
| 3963 | int ret = 1; |
| 3964 | |
| 3965 | if (stats_admin_rule->cond) { |
| 3966 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 3967 | ret = acl_pass(ret); |
| 3968 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3969 | ret = !ret; |
| 3970 | } |
| 3971 | |
| 3972 | if (ret) { |
| 3973 | /* no rule, or the rule matches */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3974 | ctx->flags |= STAT_ADMIN; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3975 | break; |
| 3976 | } |
| 3977 | } |
| 3978 | |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 3979 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 3980 | appctx->st0 = STAT_HTTP_HEAD; |
| 3981 | else if (txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3982 | if (ctx->flags & STAT_ADMIN) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3983 | appctx->st0 = STAT_HTTP_POST; |
Christopher Faulet | bd9e842 | 2019-08-15 22:26:48 +0200 | [diff] [blame] | 3984 | if (msg->msg_state < HTTP_MSG_DATA) |
| 3985 | req->analysers |= AN_REQ_HTTP_BODY; |
| 3986 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3987 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 3988 | /* POST without admin level */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3989 | ctx->flags &= ~STAT_CHUNKED; |
| 3990 | ctx->st_code = STAT_STATUS_DENY; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3991 | appctx->st0 = STAT_HTTP_LAST; |
| 3992 | } |
| 3993 | } |
| 3994 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 3995 | /* Unsupported method */ |
Willy Tarreau | 91cefca | 2022-05-03 17:08:29 +0200 | [diff] [blame] | 3996 | ctx->flags &= ~STAT_CHUNKED; |
| 3997 | ctx->st_code = STAT_STATUS_IVAL; |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 3998 | appctx->st0 = STAT_HTTP_LAST; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3999 | } |
| 4000 | |
| 4001 | s->task->nice = -32; /* small boost for HTTP statistics */ |
| 4002 | return 1; |
| 4003 | } |
| 4004 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4005 | /* This function waits for the message payload at most <time> milliseconds (may |
| 4006 | * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the |
| 4007 | * payload are received (0 means no limit). It returns HTTP_RULE_* depending on |
| 4008 | * the result: |
| 4009 | * |
| 4010 | * - HTTP_RULE_RES_CONT when conditions are met to stop waiting |
| 4011 | * - HTTP_RULE_RES_YIELD to wait for more data |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 4012 | * - HTTP_RULE_RES_ABRT when a timeout occurred. |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4013 | * - 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] | 4014 | * - HTTP_RULE_RES_ERROR if an internal error occurred |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4015 | * |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 4016 | * 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] | 4017 | * to the client, depending on the channel (408 on request side, 504 on response |
| 4018 | * side). All other errors must be handled by the caller. |
| 4019 | */ |
| 4020 | enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn, |
| 4021 | unsigned int time, unsigned int bytes) |
| 4022 | { |
| 4023 | struct session *sess = s->sess; |
| 4024 | struct http_txn *txn = s->txn; |
| 4025 | struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req); |
| 4026 | struct htx *htx; |
| 4027 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 4028 | |
| 4029 | htx = htxbuf(&chn->buf); |
| 4030 | |
| 4031 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
| 4032 | ret = HTTP_RULE_RES_BADREQ; |
| 4033 | goto end; |
| 4034 | } |
| 4035 | if (htx->flags & HTX_FL_PROCESSING_ERROR) { |
| 4036 | ret = HTTP_RULE_RES_ERROR; |
| 4037 | goto end; |
| 4038 | } |
| 4039 | |
| 4040 | /* Do nothing for bodyless and CONNECT requests */ |
| 4041 | if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS)) |
| 4042 | goto end; |
| 4043 | |
| 4044 | if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) { |
| 4045 | if (http_handle_expect_hdr(s, htx, msg) == -1) { |
| 4046 | ret = HTTP_RULE_RES_ERROR; |
| 4047 | goto end; |
| 4048 | } |
| 4049 | } |
| 4050 | |
| 4051 | msg->msg_state = HTTP_MSG_DATA; |
| 4052 | |
| 4053 | /* Now we're in HTTP_MSG_DATA. We just need to know if all data have |
| 4054 | * been received or if the buffer is full. |
| 4055 | */ |
Christopher Faulet | 7833596 | 2021-09-23 14:46:32 +0200 | [diff] [blame] | 4056 | if ((htx->flags & HTX_FL_EOM) || |
| 4057 | htx_get_tail_type(htx) > HTX_BLK_DATA || |
| 4058 | channel_htx_full(chn, htx, global.tune.maxrewrite) || |
Willy Tarreau | 99615ed | 2022-05-25 07:29:36 +0200 | [diff] [blame] | 4059 | sc_waiting_room(chn_prod(chn))) |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4060 | goto end; |
| 4061 | |
| 4062 | if (bytes) { |
| 4063 | struct htx_blk *blk; |
| 4064 | unsigned int len = 0; |
| 4065 | |
| 4066 | for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 4067 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 4068 | continue; |
| 4069 | len += htx_get_blksz(blk); |
| 4070 | if (len >= bytes) |
| 4071 | goto end; |
| 4072 | } |
| 4073 | } |
| 4074 | |
| 4075 | if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) { |
| 4076 | if (!(chn->flags & CF_ISRESP)) |
| 4077 | goto abort_req; |
| 4078 | goto abort_res; |
| 4079 | } |
| 4080 | |
| 4081 | /* we get here if we need to wait for more data */ |
Christopher Faulet | 4b490b7 | 2023-01-04 11:55:15 +0100 | [diff] [blame] | 4082 | if (!(chn->flags & CF_SHUTR)) { |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4083 | if (!tick_isset(chn->analyse_exp)) |
| 4084 | chn->analyse_exp = tick_add_ifset(now_ms, time); |
| 4085 | ret = HTTP_RULE_RES_YIELD; |
| 4086 | } |
| 4087 | |
| 4088 | end: |
| 4089 | return ret; |
| 4090 | |
Christopher Faulet | f0d80df | 2023-01-13 10:20:20 +0100 | [diff] [blame] | 4091 | abort: |
| 4092 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 4093 | ret = HTTP_RULE_RES_ABRT; |
| 4094 | goto end; |
| 4095 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4096 | abort_req: |
| 4097 | txn->status = 408; |
| 4098 | if (!(s->flags & SF_ERR_MASK)) |
| 4099 | s->flags |= SF_ERR_CLITO; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4100 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4101 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4102 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | f0d80df | 2023-01-13 10:20:20 +0100 | [diff] [blame] | 4103 | goto abort; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4104 | |
| 4105 | abort_res: |
| 4106 | txn->status = 504; |
| 4107 | if (!(s->flags & SF_ERR_MASK)) |
| 4108 | s->flags |= SF_ERR_SRVTO; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4109 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | f0d80df | 2023-01-13 10:20:20 +0100 | [diff] [blame] | 4110 | goto abort; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4111 | } |
| 4112 | |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4113 | void http_perform_server_redirect(struct stream *s, struct stconn *sc) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4114 | { |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4115 | struct channel *req = &s->req; |
| 4116 | struct channel *res = &s->res; |
| 4117 | struct server *srv; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4118 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4119 | struct htx_sl *sl; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4120 | struct ist path, location; |
| 4121 | unsigned int flags; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4122 | struct http_uri_parser parser; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4123 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4124 | /* |
| 4125 | * Create the location |
| 4126 | */ |
| 4127 | chunk_reset(&trash); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4128 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4129 | /* 1: add the server's prefix */ |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4130 | /* special prefix "/" means don't change URL */ |
| 4131 | srv = __objt_server(s->target); |
| 4132 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 4133 | if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len)) |
| 4134 | return; |
| 4135 | } |
| 4136 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4137 | /* 2: add the request Path */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4138 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 4139 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4140 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4141 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4142 | if (!isttest(path)) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4143 | return; |
| 4144 | |
| 4145 | if (!chunk_memcat(&trash, path.ptr, path.len)) |
| 4146 | return; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4147 | location = ist2(trash.area, trash.data); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4148 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4149 | /* |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 4150 | * Create the 302 response |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4151 | */ |
| 4152 | htx = htx_from_buf(&res->buf); |
| 4153 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 4154 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4155 | ist("HTTP/1.1"), ist("302"), ist("Found")); |
| 4156 | if (!sl) |
| 4157 | goto fail; |
| 4158 | sl->info.res.status = 302; |
| 4159 | s->txn->status = 302; |
| 4160 | |
| 4161 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) || |
| 4162 | !htx_add_header(htx, ist("Connection"), ist("close")) || |
| 4163 | !htx_add_header(htx, ist("Content-length"), ist("0")) || |
| 4164 | !htx_add_header(htx, ist("Location"), location)) |
| 4165 | goto fail; |
| 4166 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4167 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4168 | goto fail; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4169 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4170 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | c20afb8 | 2020-01-24 19:16:26 +0100 | [diff] [blame] | 4171 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4172 | if (!http_forward_proxy_resp(s, 1)) |
| 4173 | goto fail; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4174 | |
| 4175 | /* return without error. */ |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4176 | sc_shutr(sc); |
| 4177 | sc_shutw(sc); |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4178 | s->conn_err_type = STRM_ET_NONE; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4179 | sc->state = SC_ST_CLO; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4180 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4181 | if (!(s->flags & SF_ERR_MASK)) |
| 4182 | s->flags |= SF_ERR_LOCAL; |
| 4183 | if (!(s->flags & SF_FINST_MASK)) |
| 4184 | s->flags |= SF_FINST_C; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4185 | |
| 4186 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 4187 | srv_inc_sess_ctr(srv); |
| 4188 | srv_set_sess_last(srv); |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4189 | return; |
| 4190 | |
| 4191 | fail: |
| 4192 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4193 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4194 | channel_htx_truncate(res, htx); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4195 | } |
| 4196 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4197 | /* This function terminates the request because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4198 | * because an error was triggered during the body forwarding. |
| 4199 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4200 | static void http_end_request(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4201 | { |
| 4202 | struct channel *chn = &s->req; |
| 4203 | struct http_txn *txn = s->txn; |
| 4204 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4205 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4206 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4207 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) { |
| 4208 | 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] | 4209 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4210 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4211 | |
| 4212 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4213 | /* No need to read anymore, the request was completely parsed. |
| 4214 | * We can shut the read side unless we want to abort_on_close, |
| 4215 | * or we have a POST request. The issue with POST requests is |
| 4216 | * that some browsers still send a CRLF after the request, and |
| 4217 | * this CRLF must be read so that it does not remain in the kernel |
| 4218 | * buffers, otherwise a close could cause an RST on some systems |
| 4219 | * (eg: Linux). |
| 4220 | */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 4221 | 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] | 4222 | channel_dont_read(chn); |
| 4223 | |
| 4224 | /* if the server closes the connection, we want to immediately react |
| 4225 | * and close the socket to save packets and syscalls. |
| 4226 | */ |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 4227 | s->scb->flags |= SC_FL_NOHALF; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4228 | |
| 4229 | /* In any case we've finished parsing the request so we must |
| 4230 | * disable Nagle when sending data because 1) we're not going |
| 4231 | * to shut this side, and 2) the server is waiting for us to |
| 4232 | * send pending data. |
| 4233 | */ |
| 4234 | chn->flags |= CF_NEVER_WAIT; |
| 4235 | |
Christopher Faulet | d01ce40 | 2019-01-02 17:44:13 +0100 | [diff] [blame] | 4236 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4237 | /* The server has not finished to respond, so we |
| 4238 | * don't want to move in order not to upset it. |
| 4239 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4240 | 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] | 4241 | return; |
| 4242 | } |
| 4243 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4244 | /* When we get here, it means that both the request and the |
| 4245 | * response have finished receiving. Depending on the connection |
| 4246 | * mode, we'll have to wait for the last bytes to leave in either |
| 4247 | * direction, and sometimes for a close to be effective. |
| 4248 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4249 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4250 | /* Tunnel mode will not have any analyser so it needs to |
| 4251 | * poll for reads. |
| 4252 | */ |
| 4253 | channel_auto_read(chn); |
| 4254 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 4255 | } |
| 4256 | else { |
| 4257 | /* we're not expecting any new data to come for this |
| 4258 | * transaction, so we can close it. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4259 | * |
| 4260 | * However, there is an exception if the response |
| 4261 | * length is undefined. In this case, we need to wait |
| 4262 | * the close from the server. The response will be |
| 4263 | * switched in TUNNEL mode until the end. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4264 | */ |
| 4265 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 4266 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4267 | goto check_channel_flags; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4268 | |
| 4269 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4270 | channel_shutr_now(chn); |
| 4271 | channel_shutw_now(chn); |
| 4272 | } |
| 4273 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4274 | goto check_channel_flags; |
| 4275 | } |
| 4276 | |
| 4277 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4278 | http_msg_closing: |
| 4279 | /* nothing else to forward, just waiting for the output buffer |
| 4280 | * to be empty and for the shutw_now to take effect. |
| 4281 | */ |
| 4282 | if (channel_is_empty(chn)) { |
| 4283 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4284 | goto http_msg_closed; |
| 4285 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4286 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4287 | return; |
| 4288 | } |
| 4289 | |
| 4290 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4291 | http_msg_closed: |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4292 | /* if we don't know whether the server will close, we need to hard close */ |
| 4293 | if (txn->rsp.flags & HTTP_MSGF_XFER_LEN) |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 4294 | s->scb->flags |= SC_FL_NOLINGER; /* we want to close ASAP */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4295 | /* 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] | 4296 | if (!(s->be->options & PR_O_ABRT_CLOSE)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4297 | channel_dont_read(chn); |
| 4298 | goto end; |
| 4299 | } |
| 4300 | |
| 4301 | check_channel_flags: |
| 4302 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4303 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4304 | /* if we've just closed an output, let's switch */ |
| 4305 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4306 | goto http_msg_closing; |
| 4307 | } |
| 4308 | |
| 4309 | end: |
| 4310 | chn->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4311 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4312 | chn->flags |= CF_NEVER_WAIT; |
| 4313 | if (HAS_REQ_DATA_FILTERS(s)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4314 | chn->analysers |= AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4315 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4316 | channel_auto_close(chn); |
| 4317 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4318 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4319 | } |
| 4320 | |
| 4321 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4322 | /* This function terminates the response because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4323 | * because an error was triggered during the body forwarding. |
| 4324 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4325 | static void http_end_response(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4326 | { |
| 4327 | struct channel *chn = &s->res; |
| 4328 | struct http_txn *txn = s->txn; |
| 4329 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4330 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4331 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4332 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) { |
| 4333 | 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] | 4334 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4335 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4336 | |
| 4337 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4338 | /* In theory, we don't need to read anymore, but we must |
| 4339 | * still monitor the server connection for a possible close |
| 4340 | * while the request is being uploaded, so we don't disable |
| 4341 | * reading. |
| 4342 | */ |
| 4343 | /* channel_dont_read(chn); */ |
| 4344 | |
| 4345 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4346 | /* The client seems to still be sending data, probably |
| 4347 | * because we got an error response during an upload. |
| 4348 | * We have the choice of either breaking the connection |
| 4349 | * or letting it pass through. Let's do the later. |
| 4350 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4351 | 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] | 4352 | return; |
| 4353 | } |
| 4354 | |
| 4355 | /* When we get here, it means that both the request and the |
| 4356 | * response have finished receiving. Depending on the connection |
| 4357 | * mode, we'll have to wait for the last bytes to leave in either |
| 4358 | * direction, and sometimes for a close to be effective. |
| 4359 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4360 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4361 | channel_auto_read(chn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4362 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4363 | } |
| 4364 | else { |
| 4365 | /* we're not expecting any new data to come for this |
| 4366 | * transaction, so we can close it. |
| 4367 | */ |
| 4368 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4369 | channel_shutr_now(chn); |
| 4370 | channel_shutw_now(chn); |
| 4371 | } |
| 4372 | } |
| 4373 | goto check_channel_flags; |
| 4374 | } |
| 4375 | |
| 4376 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4377 | http_msg_closing: |
| 4378 | /* nothing else to forward, just waiting for the output buffer |
| 4379 | * to be empty and for the shutw_now to take effect. |
| 4380 | */ |
| 4381 | if (channel_is_empty(chn)) { |
| 4382 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4383 | goto http_msg_closed; |
| 4384 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4385 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4386 | return; |
| 4387 | } |
| 4388 | |
| 4389 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4390 | http_msg_closed: |
| 4391 | /* drop any pending data */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4392 | channel_htx_truncate(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4393 | channel_abort(&s->req); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4394 | goto end; |
| 4395 | } |
| 4396 | |
| 4397 | check_channel_flags: |
| 4398 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4399 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4400 | /* if we've just closed an output, let's switch */ |
| 4401 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4402 | goto http_msg_closing; |
| 4403 | } |
| 4404 | |
| 4405 | end: |
| 4406 | chn->analysers &= AN_RES_FLT_END; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4407 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4408 | chn->flags |= CF_NEVER_WAIT; |
| 4409 | if (HAS_RSP_DATA_FILTERS(s)) |
| 4410 | chn->analysers |= AN_RES_FLT_XFER_DATA; |
| 4411 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4412 | channel_auto_close(chn); |
| 4413 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4414 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4415 | } |
| 4416 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4417 | /* Forward a response generated by HAProxy (error/redirect/return). This |
| 4418 | * function forwards all pending incoming data. If <final> is set to 0, nothing |
| 4419 | * more is performed. It is used for 1xx informational messages. Otherwise, the |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4420 | * transaction is terminated and the request is emptied. On success 1 is |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4421 | * returned. If an error occurred, 0 is returned. If it fails, this function |
| 4422 | * only exits. It is the caller responsibility to do the cleanup. |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4423 | */ |
| 4424 | int http_forward_proxy_resp(struct stream *s, int final) |
| 4425 | { |
| 4426 | struct channel *req = &s->req; |
| 4427 | struct channel *res = &s->res; |
| 4428 | struct htx *htx = htxbuf(&res->buf); |
| 4429 | size_t data; |
| 4430 | |
| 4431 | if (final) { |
| 4432 | htx->flags |= HTX_FL_PROXY_RESP; |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4433 | |
Christopher Faulet | aab1b67 | 2020-11-18 16:44:02 +0100 | [diff] [blame] | 4434 | if (!htx_is_empty(htx) && !http_eval_after_res_rules(s)) |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 4435 | return 0; |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4436 | |
Christopher Faulet | d6c4836 | 2020-10-19 18:01:38 +0200 | [diff] [blame] | 4437 | if (s->txn->meth == HTTP_METH_HEAD) |
| 4438 | htx_skip_msg_payload(htx); |
| 4439 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4440 | channel_auto_read(req); |
| 4441 | channel_abort(req); |
| 4442 | channel_auto_close(req); |
| 4443 | channel_htx_erase(req, htxbuf(&req->buf)); |
| 4444 | |
| 4445 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 4446 | channel_auto_read(res); |
| 4447 | channel_auto_close(res); |
| 4448 | channel_shutr_now(res); |
Christopher Faulet | 1a9db7c | 2020-06-25 15:36:45 +0200 | [diff] [blame] | 4449 | res->flags |= CF_EOI; /* The response is terminated, add EOI */ |
Christopher Faulet | 42432f3 | 2020-11-20 17:43:16 +0100 | [diff] [blame] | 4450 | htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */ |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4451 | } |
Christopher Faulet | cf6898c | 2020-06-25 15:55:11 +0200 | [diff] [blame] | 4452 | else { |
| 4453 | /* Send ASAP informational messages. Rely on CF_EOI for final |
| 4454 | * response. |
| 4455 | */ |
| 4456 | res->flags |= CF_SEND_DONTWAIT; |
| 4457 | } |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4458 | |
| 4459 | data = htx->data - co_data(res); |
| 4460 | c_adv(res, data); |
| 4461 | htx->first = -1; |
| 4462 | res->total += data; |
| 4463 | return 1; |
| 4464 | } |
| 4465 | |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4466 | void http_server_error(struct stream *s, struct stconn *sc, int err, |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4467 | int finst, struct http_reply *msg) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4468 | { |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4469 | http_reply_and_close(s, s->txn->status, msg); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4470 | if (!(s->flags & SF_ERR_MASK)) |
| 4471 | s->flags |= err; |
| 4472 | if (!(s->flags & SF_FINST_MASK)) |
| 4473 | s->flags |= finst; |
| 4474 | } |
| 4475 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4476 | 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] | 4477 | { |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4478 | if (!msg) { |
| 4479 | channel_htx_truncate(&s->res, htxbuf(&s->res.buf)); |
| 4480 | goto end; |
| 4481 | } |
| 4482 | |
| 4483 | if (http_reply_message(s, msg) == -1) { |
| 4484 | /* 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] | 4485 | * it is already an internal error. If it was already a "const" |
| 4486 | * 500 error, just fail. |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4487 | */ |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4488 | if (s->txn->status == 500) { |
| 4489 | if (s->txn->flags & TX_CONST_REPLY) |
| 4490 | goto end; |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4491 | s->txn->flags |= TX_CONST_REPLY; |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4492 | } |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4493 | s->txn->status = 500; |
| 4494 | s->txn->http_reply = NULL; |
| 4495 | return http_reply_and_close(s, s->txn->status, http_error_message(s)); |
| 4496 | } |
| 4497 | |
| 4498 | end: |
| 4499 | s->res.wex = tick_add_ifset(now_ms, s->res.wto); |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4500 | |
Christopher Faulet | 2d56500 | 2021-09-10 09:17:50 +0200 | [diff] [blame] | 4501 | /* At this staged, HTTP analysis is finished */ |
| 4502 | s->req.analysers &= AN_REQ_FLT_END; |
| 4503 | s->req.analyse_exp = TICK_ETERNITY; |
| 4504 | |
| 4505 | s->res.analysers &= AN_RES_FLT_END; |
| 4506 | s->res.analyse_exp = TICK_ETERNITY; |
| 4507 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4508 | channel_auto_read(&s->req); |
| 4509 | channel_abort(&s->req); |
| 4510 | channel_auto_close(&s->req); |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4511 | channel_htx_erase(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4512 | channel_auto_read(&s->res); |
| 4513 | channel_auto_close(&s->res); |
| 4514 | channel_shutr_now(&s->res); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4515 | } |
| 4516 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4517 | struct http_reply *http_error_message(struct stream *s) |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4518 | { |
| 4519 | const int msgnum = http_get_status_idx(s->txn->status); |
| 4520 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4521 | if (s->txn->http_reply) |
| 4522 | return s->txn->http_reply; |
| 4523 | else if (s->be->replies[msgnum]) |
| 4524 | return s->be->replies[msgnum]; |
| 4525 | else if (strm_fe(s)->replies[msgnum]) |
| 4526 | return strm_fe(s)->replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4527 | else |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4528 | return &http_err_replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4529 | } |
| 4530 | |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4531 | /* Produces an HTX message from an http reply. Depending on the http reply type, |
| 4532 | * a, errorfile, an raw file or a log-format string is used. On success, it |
| 4533 | * returns 0. If an error occurs -1 is returned. If it fails, this function only |
| 4534 | * exits. It is the caller responsibility to do the cleanup. |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4535 | */ |
Christopher Faulet | ae43b6c | 2020-05-27 15:24:22 +0200 | [diff] [blame] | 4536 | 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] | 4537 | { |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4538 | struct buffer *errmsg; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4539 | struct htx_sl *sl; |
| 4540 | struct buffer *body = NULL; |
| 4541 | const char *status, *reason, *clen, *ctype; |
| 4542 | unsigned int slflags; |
| 4543 | int ret = 0; |
| 4544 | |
Christopher Faulet | e29a97e | 2020-05-14 14:49:25 +0200 | [diff] [blame] | 4545 | /* |
| 4546 | * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so |
| 4547 | * |
| 4548 | * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled |
| 4549 | * as no payload if NULL. the TXN status code is set with the status |
| 4550 | * of the original reply. |
| 4551 | */ |
| 4552 | |
| 4553 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4554 | if (reply->body.reply) |
| 4555 | reply = reply->body.reply; |
| 4556 | } |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4557 | if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) { |
| 4558 | /* get default error message */ |
| 4559 | if (reply == s->txn->http_reply) |
| 4560 | s->txn->http_reply = NULL; |
| 4561 | reply = http_error_message(s); |
| 4562 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4563 | if (reply->body.reply) |
| 4564 | reply = reply->body.reply; |
| 4565 | } |
| 4566 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4567 | |
| 4568 | if (reply->type == HTTP_REPLY_ERRMSG) { |
| 4569 | /* implicit or explicit error message*/ |
| 4570 | errmsg = reply->body.errmsg; |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4571 | if (errmsg && !b_is_null(errmsg)) { |
Christopher Faulet | 2056736 | 2020-05-15 14:52:49 +0200 | [diff] [blame] | 4572 | if (!htx_copy_msg(htx, errmsg)) |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4573 | goto fail; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4574 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4575 | } |
| 4576 | else { |
| 4577 | /* no payload, file or log-format string */ |
| 4578 | if (reply->type == HTTP_REPLY_RAW) { |
| 4579 | /* file */ |
| 4580 | body = &reply->body.obj; |
| 4581 | } |
| 4582 | else if (reply->type == HTTP_REPLY_LOGFMT) { |
| 4583 | /* log-format string */ |
| 4584 | body = alloc_trash_chunk(); |
| 4585 | if (!body) |
| 4586 | goto fail_alloc; |
| 4587 | body->data = build_logline(s, body->area, body->size, &reply->body.fmt); |
| 4588 | } |
| 4589 | /* else no payload */ |
| 4590 | |
| 4591 | status = ultoa(reply->status); |
| 4592 | reason = http_get_reason(reply->status); |
| 4593 | slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN); |
| 4594 | if (!body || !b_data(body)) |
| 4595 | slflags |= HTX_SL_F_BODYLESS; |
| 4596 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4597 | if (!sl) |
| 4598 | goto fail; |
| 4599 | sl->info.res.status = reply->status; |
| 4600 | |
| 4601 | clen = (body ? ultoa(b_data(body)) : "0"); |
| 4602 | ctype = reply->ctype; |
| 4603 | |
| 4604 | if (!LIST_ISEMPTY(&reply->hdrs)) { |
| 4605 | struct http_reply_hdr *hdr; |
| 4606 | struct buffer *value = alloc_trash_chunk(); |
| 4607 | |
| 4608 | if (!value) |
| 4609 | goto fail; |
| 4610 | |
| 4611 | list_for_each_entry(hdr, &reply->hdrs, list) { |
| 4612 | chunk_reset(value); |
| 4613 | value->data = build_logline(s, value->area, value->size, &hdr->value); |
| 4614 | if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) { |
| 4615 | free_trash_chunk(value); |
| 4616 | goto fail; |
| 4617 | } |
| 4618 | chunk_reset(value); |
| 4619 | } |
| 4620 | free_trash_chunk(value); |
| 4621 | } |
| 4622 | |
| 4623 | if (!htx_add_header(htx, ist("content-length"), ist(clen)) || |
| 4624 | (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) || |
| 4625 | !htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4626 | (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] | 4627 | goto fail; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4628 | |
| 4629 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4630 | } |
| 4631 | |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4632 | leave: |
| 4633 | if (reply->type == HTTP_REPLY_LOGFMT) |
| 4634 | free_trash_chunk(body); |
| 4635 | return ret; |
| 4636 | |
| 4637 | fail_alloc: |
| 4638 | if (!(s->flags & SF_ERR_MASK)) |
| 4639 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4640 | /* fall through */ |
| 4641 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4642 | ret = -1; |
| 4643 | goto leave; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4644 | } |
| 4645 | |
| 4646 | /* 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] | 4647 | * occurs -1 is returned and the response channel is truncated, removing this |
| 4648 | * way the faulty reply. This function may fail when the reply is formatted |
| 4649 | * (http_reply_to_htx) or when the reply is forwarded |
| 4650 | * (http_forward_proxy_resp). On the last case, it is because a |
| 4651 | * http-after-response rule fails. |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4652 | */ |
| 4653 | int http_reply_message(struct stream *s, struct http_reply *reply) |
| 4654 | { |
| 4655 | struct channel *res = &s->res; |
| 4656 | struct htx *htx = htx_from_buf(&res->buf); |
| 4657 | |
| 4658 | if (s->txn->status == -1) |
| 4659 | s->txn->status = reply->status; |
| 4660 | channel_htx_truncate(res, htx); |
| 4661 | |
| 4662 | if (http_reply_to_htx(s, htx, reply) == -1) |
| 4663 | goto fail; |
| 4664 | |
| 4665 | htx_to_buf(htx, &s->res.buf); |
| 4666 | if (!http_forward_proxy_resp(s, 1)) |
| 4667 | goto fail; |
| 4668 | return 0; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4669 | |
| 4670 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4671 | channel_htx_truncate(res, htx); |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4672 | if (!(s->flags & SF_ERR_MASK)) |
| 4673 | s->flags |= SF_ERR_PRXCOND; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4674 | return -1; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4675 | } |
| 4676 | |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4677 | /* 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] | 4678 | * that the server side is closed. Note that err_type is actually a |
| 4679 | * bitmask, where almost only aborts may be cumulated with other |
| 4680 | * values. We consider that aborted operations are more important |
| 4681 | * than timeouts or errors due to the fact that nobody else in the |
| 4682 | * logs might explain incomplete retries. All others should avoid |
| 4683 | * being cumulated. It should normally not be possible to have multiple |
| 4684 | * aborts at once, but just in case, the first one in sequence is reported. |
| 4685 | * Note that connection errors appearing on the second request of a keep-alive |
| 4686 | * connection are not reported since this allows the client to retry. |
| 4687 | */ |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4688 | void http_return_srv_error(struct stream *s, struct stconn *sc) |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4689 | { |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4690 | int err_type = s->conn_err_type; |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4691 | |
| 4692 | /* set s->txn->status for http_error_message(s) */ |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4693 | if (err_type & STRM_ET_QUEUE_ABRT) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4694 | s->txn->status = -1; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4695 | http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_Q, NULL); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4696 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4697 | else if (err_type & STRM_ET_CONN_ABRT) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4698 | s->txn->status = -1; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4699 | http_server_error(s, sc, SF_ERR_CLICL, SF_FINST_C, NULL); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4700 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4701 | else if (err_type & STRM_ET_QUEUE_TO) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4702 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4703 | http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_Q, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4704 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4705 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4706 | else if (err_type & STRM_ET_QUEUE_ERR) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4707 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4708 | http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_Q, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4709 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4710 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4711 | else if (err_type & STRM_ET_CONN_TO) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4712 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4713 | http_server_error(s, sc, SF_ERR_SRVTO, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4714 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4715 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4716 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4717 | else if (err_type & STRM_ET_CONN_ERR) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4718 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4719 | http_server_error(s, sc, SF_ERR_SRVCL, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4720 | (s->flags & SF_SRV_REUSED) ? NULL : |
| 4721 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4722 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4723 | else if (err_type & STRM_ET_CONN_RES) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4724 | s->txn->status = 503; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4725 | http_server_error(s, sc, SF_ERR_RESOURCE, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4726 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4727 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4728 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4729 | else { /* STRM_ET_CONN_OTHER and others */ |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4730 | s->txn->status = 500; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4731 | http_server_error(s, sc, SF_ERR_INTERNAL, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4732 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4733 | } |
| 4734 | } |
| 4735 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4736 | |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4737 | /* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0 |
| 4738 | * on success and -1 on error. |
| 4739 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4740 | 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] | 4741 | { |
| 4742 | /* If we have HTTP/1.1 message with a body and Expect: 100-continue, |
| 4743 | * then we must send an HTTP/1.1 100 Continue intermediate response. |
| 4744 | */ |
| 4745 | if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) && |
| 4746 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
| 4747 | struct ist hdr = { .ptr = "Expect", .len = 6 }; |
| 4748 | struct http_hdr_ctx ctx; |
| 4749 | |
| 4750 | ctx.blk = NULL; |
| 4751 | /* Expect is allowed in 1.1, look for it */ |
| 4752 | if (http_find_header(htx, hdr, &ctx, 0) && |
| 4753 | unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4754 | if (http_reply_100_continue(s) == -1) |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4755 | return -1; |
| 4756 | http_remove_header(htx, &ctx); |
| 4757 | } |
| 4758 | } |
| 4759 | return 0; |
| 4760 | } |
| 4761 | |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4762 | /* Send a 100-Continue response to the client. It returns 0 on success and -1 |
| 4763 | * on error. The response channel is updated accordingly. |
| 4764 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4765 | static int http_reply_100_continue(struct stream *s) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4766 | { |
| 4767 | struct channel *res = &s->res; |
| 4768 | struct htx *htx = htx_from_buf(&res->buf); |
| 4769 | struct htx_sl *sl; |
| 4770 | unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11| |
| 4771 | HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4772 | |
| 4773 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4774 | ist("HTTP/1.1"), ist("100"), ist("Continue")); |
| 4775 | if (!sl) |
| 4776 | goto fail; |
| 4777 | sl->info.res.status = 100; |
| 4778 | |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 4779 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4780 | goto fail; |
| 4781 | |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4782 | if (!http_forward_proxy_resp(s, 0)) |
| 4783 | goto fail; |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4784 | return 0; |
| 4785 | |
| 4786 | fail: |
| 4787 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4788 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4789 | channel_htx_truncate(res, htx); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4790 | return -1; |
| 4791 | } |
| 4792 | |
Christopher Faulet | 12c51e2 | 2018-11-28 15:59:42 +0100 | [diff] [blame] | 4793 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4794 | /* |
| 4795 | * Capture headers from message <htx> according to header list <cap_hdr>, and |
| 4796 | * fill the <cap> pointers appropriately. |
| 4797 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4798 | 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] | 4799 | { |
| 4800 | struct cap_hdr *h; |
| 4801 | int32_t pos; |
| 4802 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4803 | 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] | 4804 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4805 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4806 | struct ist n, v; |
| 4807 | |
| 4808 | if (type == HTX_BLK_EOH) |
| 4809 | break; |
| 4810 | if (type != HTX_BLK_HDR) |
| 4811 | continue; |
| 4812 | |
| 4813 | n = htx_get_blk_name(htx, blk); |
| 4814 | |
| 4815 | for (h = cap_hdr; h; h = h->next) { |
| 4816 | if (h->namelen && (h->namelen == n.len) && |
| 4817 | (strncasecmp(n.ptr, h->name, h->namelen) == 0)) { |
| 4818 | if (cap[h->index] == NULL) |
| 4819 | cap[h->index] = |
| 4820 | pool_alloc(h->pool); |
| 4821 | |
| 4822 | if (cap[h->index] == NULL) { |
| 4823 | ha_alert("HTTP capture : out of memory.\n"); |
| 4824 | break; |
| 4825 | } |
| 4826 | |
| 4827 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 4828 | v = isttrim(v, h->len); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4829 | |
| 4830 | memcpy(cap[h->index], v.ptr, v.len); |
| 4831 | cap[h->index][v.len]=0; |
| 4832 | } |
| 4833 | } |
| 4834 | } |
| 4835 | } |
| 4836 | |
Christopher Faulet | 0b6bdc5 | 2018-10-24 11:05:36 +0200 | [diff] [blame] | 4837 | /* Delete a value in a header between delimiters <from> and <next>. The header |
| 4838 | * itself is delimited by <start> and <end> pointers. The number of characters |
| 4839 | * displaced is returned, and the pointer to the first delimiter is updated if |
| 4840 | * required. The function tries as much as possible to respect the following |
| 4841 | * principles : |
| 4842 | * - replace <from> delimiter by the <next> one unless <from> points to <start>, |
| 4843 | * in which case <next> is simply removed |
| 4844 | * - set exactly one space character after the new first delimiter, unless there |
| 4845 | * are not enough characters in the block being moved to do so. |
| 4846 | * - remove unneeded spaces before the previous delimiter and after the new |
| 4847 | * one. |
| 4848 | * |
| 4849 | * It is the caller's responsibility to ensure that : |
| 4850 | * - <from> points to a valid delimiter or <start> ; |
| 4851 | * - <next> points to a valid delimiter or <end> ; |
| 4852 | * - there are non-space chars before <from>. |
| 4853 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4854 | 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] | 4855 | { |
| 4856 | char *prev = *from; |
| 4857 | |
| 4858 | if (prev == start) { |
| 4859 | /* We're removing the first value. eat the semicolon, if <next> |
| 4860 | * is lower than <end> */ |
| 4861 | if (next < end) |
| 4862 | next++; |
| 4863 | |
| 4864 | while (next < end && HTTP_IS_SPHT(*next)) |
| 4865 | next++; |
| 4866 | } |
| 4867 | else { |
| 4868 | /* Remove useless spaces before the old delimiter. */ |
| 4869 | while (HTTP_IS_SPHT(*(prev-1))) |
| 4870 | prev--; |
| 4871 | *from = prev; |
| 4872 | |
| 4873 | /* copy the delimiter and if possible a space if we're |
| 4874 | * not at the end of the line. |
| 4875 | */ |
| 4876 | if (next < end) { |
| 4877 | *prev++ = *next++; |
| 4878 | if (prev + 1 < next) |
| 4879 | *prev++ = ' '; |
| 4880 | while (next < end && HTTP_IS_SPHT(*next)) |
| 4881 | next++; |
| 4882 | } |
| 4883 | } |
| 4884 | memmove(prev, next, end - next); |
| 4885 | return (prev - next); |
| 4886 | } |
| 4887 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4888 | |
| 4889 | /* 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] | 4890 | * 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] | 4891 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4892 | 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] | 4893 | { |
| 4894 | struct ist dst = ist2(str, 0); |
| 4895 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4896 | if (istcat(&dst, htx_sl_req_meth(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4897 | goto end; |
| 4898 | if (dst.len + 1 > len) |
| 4899 | goto end; |
| 4900 | dst.ptr[dst.len++] = ' '; |
| 4901 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4902 | if (istcat(&dst, htx_sl_req_uri(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4903 | goto end; |
| 4904 | if (dst.len + 1 > len) |
| 4905 | goto end; |
| 4906 | dst.ptr[dst.len++] = ' '; |
| 4907 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4908 | istcat(&dst, htx_sl_req_vsn(sl), len); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4909 | end: |
| 4910 | return dst.len; |
| 4911 | } |
| 4912 | |
| 4913 | /* |
| 4914 | * Print a debug line with a start line. |
| 4915 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4916 | 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] | 4917 | { |
| 4918 | struct session *sess = strm_sess(s); |
| 4919 | int max; |
| 4920 | |
| 4921 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 4922 | dir, |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 4923 | 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] | 4924 | 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] | 4925 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4926 | max = HTX_SL_P1_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4927 | UBOUND(max, trash.size - trash.data - 3); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4928 | chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4929 | trash.area[trash.data++] = ' '; |
| 4930 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4931 | max = HTX_SL_P2_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4932 | UBOUND(max, trash.size - trash.data - 2); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4933 | chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4934 | trash.area[trash.data++] = ' '; |
| 4935 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4936 | max = HTX_SL_P3_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4937 | UBOUND(max, trash.size - trash.data - 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4938 | chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4939 | trash.area[trash.data++] = '\n'; |
| 4940 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 4941 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4942 | } |
| 4943 | |
| 4944 | /* |
| 4945 | * Print a debug line with a header. |
| 4946 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4947 | 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] | 4948 | { |
| 4949 | struct session *sess = strm_sess(s); |
| 4950 | int max; |
| 4951 | |
| 4952 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 4953 | dir, |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 4954 | 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] | 4955 | 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] | 4956 | |
| 4957 | max = n.len; |
| 4958 | UBOUND(max, trash.size - trash.data - 3); |
| 4959 | chunk_memcat(&trash, n.ptr, max); |
| 4960 | trash.area[trash.data++] = ':'; |
| 4961 | trash.area[trash.data++] = ' '; |
| 4962 | |
| 4963 | max = v.len; |
| 4964 | UBOUND(max, trash.size - trash.data - 1); |
| 4965 | chunk_memcat(&trash, v.ptr, max); |
| 4966 | trash.area[trash.data++] = '\n'; |
| 4967 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 4968 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4969 | } |
| 4970 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 4971 | void http_txn_reset_req(struct http_txn *txn) |
| 4972 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 4973 | txn->req.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 4974 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 4975 | } |
| 4976 | |
| 4977 | void http_txn_reset_res(struct http_txn *txn) |
| 4978 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 4979 | txn->rsp.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 4980 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 4981 | } |
| 4982 | |
| 4983 | /* |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 4984 | * Create and initialize a new HTTP transaction for stream <s>. This should be |
| 4985 | * used before processing any new request. It returns the transaction or NLULL |
| 4986 | * on error. |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 4987 | */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 4988 | struct http_txn *http_create_txn(struct stream *s) |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 4989 | { |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 4990 | struct http_txn *txn; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4991 | struct stconn *sc = s->scf; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 4992 | |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 4993 | txn = pool_alloc(pool_head_http_txn); |
| 4994 | if (!txn) |
| 4995 | return NULL; |
| 4996 | s->txn = txn; |
| 4997 | |
Christopher Faulet | 5eb67f5 | 2022-06-22 17:12:05 +0200 | [diff] [blame] | 4998 | txn->meth = HTTP_METH_OTHER; |
Willy Tarreau | b49672d | 2022-05-27 10:13:37 +0200 | [diff] [blame] | 4999 | 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] | 5000 | txn->status = -1; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 5001 | txn->http_reply = NULL; |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 5002 | txn->l7_buffer = BUF_NULL; |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 5003 | write_u32(txn->cache_hash, 0); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5004 | |
| 5005 | txn->cookie_first_date = 0; |
| 5006 | txn->cookie_last_date = 0; |
| 5007 | |
| 5008 | txn->srv_cookie = NULL; |
| 5009 | txn->cli_cookie = NULL; |
| 5010 | txn->uri = NULL; |
| 5011 | |
| 5012 | http_txn_reset_req(txn); |
| 5013 | http_txn_reset_res(txn); |
| 5014 | |
| 5015 | txn->req.chn = &s->req; |
| 5016 | txn->rsp.chn = &s->res; |
| 5017 | |
| 5018 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
| 5019 | |
Aurelien DARRAGON | 5ad2b64 | 2022-11-18 09:17:29 +0100 | [diff] [blame] | 5020 | /* here we don't want to re-initialize s->vars_txn and s->vars_reqres |
| 5021 | * variable lists, because they were already initialized upon stream |
| 5022 | * creation in stream_new(), and thus may already contain some variables |
| 5023 | */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5024 | |
| 5025 | return txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5026 | } |
| 5027 | |
| 5028 | /* to be used at the end of a transaction */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5029 | void http_destroy_txn(struct stream *s) |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5030 | { |
| 5031 | struct http_txn *txn = s->txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5032 | |
| 5033 | /* these ones will have been dynamically allocated */ |
| 5034 | pool_free(pool_head_requri, txn->uri); |
| 5035 | pool_free(pool_head_capture, txn->cli_cookie); |
| 5036 | pool_free(pool_head_capture, txn->srv_cookie); |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5037 | pool_free(pool_head_uniqueid, s->unique_id.ptr); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5038 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5039 | s->unique_id = IST_NULL; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5040 | txn->uri = NULL; |
| 5041 | txn->srv_cookie = NULL; |
| 5042 | txn->cli_cookie = NULL; |
| 5043 | |
Christopher Faulet | 5939925 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 5044 | if (!LIST_ISEMPTY(&s->vars_txn.head)) |
| 5045 | vars_prune(&s->vars_txn, s->sess, s); |
| 5046 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 5047 | vars_prune(&s->vars_reqres, s->sess, s); |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5048 | |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 5049 | b_free(&txn->l7_buffer); |
| 5050 | |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5051 | pool_free(pool_head_http_txn, txn); |
| 5052 | s->txn = NULL; |
Christopher Faulet | 5939925 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 5053 | } |
| 5054 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5055 | |
Christopher Faulet | 71236de | 2023-01-13 09:06:38 +0100 | [diff] [blame] | 5056 | void http_set_term_flags(struct stream *s) |
| 5057 | { |
| 5058 | if (!(s->flags & SF_ERR_MASK)) |
| 5059 | s->flags |= SF_ERR_PRXCOND; |
| 5060 | |
| 5061 | if (!(s->flags & SF_FINST_MASK)) { |
| 5062 | if (s->scb->state == SC_ST_INI) { |
| 5063 | /* Before any connection attempt on the server side, we |
| 5064 | * are still in the request analysis. Just take case to |
| 5065 | * detect tarpit error |
| 5066 | */ |
| 5067 | if (s->req.analysers & AN_REQ_HTTP_TARPIT) |
| 5068 | s->flags |= SF_FINST_T; |
| 5069 | else |
| 5070 | s->flags |= SF_FINST_R; |
| 5071 | } |
| 5072 | else if (s->scb->state == SC_ST_QUE) |
| 5073 | s->flags |= SF_FINST_Q; |
| 5074 | 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)) { |
| 5075 | if (unlikely(objt_applet(s->target))) { |
| 5076 | s->flags |= SF_FINST_R; |
| 5077 | } |
| 5078 | else |
| 5079 | s->flags |= SF_FINST_C; |
| 5080 | } |
| 5081 | else { |
| 5082 | if (s->txn->rsp.msg_state < HTTP_MSG_DATA) { |
| 5083 | /* We are still processing the response headers */ |
| 5084 | s->flags |= SF_FINST_H; |
| 5085 | } |
| 5086 | // (res >= done) & (res->flags & shutw) |
| 5087 | else if (s->txn->rsp.msg_state >= HTTP_MSG_DONE && |
| 5088 | (s->flags & (SF_ERR_CLITO|SF_ERR_CLICL))) { |
| 5089 | /* A client error was reported and we are |
| 5090 | * transmitting the last block of data |
| 5091 | */ |
| 5092 | s->flags |= SF_FINST_L; |
| 5093 | } |
| 5094 | else { |
| 5095 | /* Otherwise we are in DATA phase on both sides */ |
| 5096 | s->flags |= SF_FINST_D; |
| 5097 | } |
| 5098 | } |
| 5099 | } |
| 5100 | } |
| 5101 | |
| 5102 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5103 | DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5104 | |
Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 5105 | /* |
| 5106 | * Local variables: |
| 5107 | * c-indent-level: 8 |
| 5108 | * c-basic-offset: 8 |
| 5109 | * End: |
| 5110 | */ |