Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
| 4 | * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Willy Tarreau | dcc048a | 2020-06-04 19:11:43 +0200 | [diff] [blame] | 13 | #include <haproxy/acl.h> |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 14 | #include <haproxy/action-t.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 15 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 16 | #include <haproxy/backend.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 17 | #include <haproxy/base64.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 18 | #include <haproxy/capture-t.h> |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 19 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 20 | #include <haproxy/channel.h> |
Willy Tarreau | 4aa573d | 2020-06-04 18:21:56 +0200 | [diff] [blame] | 21 | #include <haproxy/check.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 22 | #include <haproxy/connection.h> |
Christopher Faulet | 8da67aa | 2022-03-29 17:53:09 +0200 | [diff] [blame] | 23 | #include <haproxy/conn_stream.h> |
| 24 | #include <haproxy/cs_utils.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 25 | #include <haproxy/errors.h> |
Willy Tarreau | c7babd8 | 2020-06-04 21:29:29 +0200 | [diff] [blame] | 26 | #include <haproxy/filters.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 27 | #include <haproxy/http.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 28 | #include <haproxy/http_ana.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 29 | #include <haproxy/http_htx.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 | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 35 | #include <haproxy/server-t.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 36 | #include <haproxy/stats.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 37 | #include <haproxy/stream.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 38 | #include <haproxy/trace.h> |
Willy Tarreau | 8c42b8a | 2020-06-04 19:27:34 +0200 | [diff] [blame] | 39 | #include <haproxy/uri_auth-t.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 40 | #include <haproxy/vars.h> |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 41 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 42 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 43 | #define TRACE_SOURCE &trace_strm |
| 44 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 45 | extern const char *stat_status_codes[]; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 46 | |
Willy Tarreau | ff88270 | 2021-04-10 17:23:00 +0200 | [diff] [blame] | 47 | struct pool_head *pool_head_requri __read_mostly = NULL; |
| 48 | struct pool_head *pool_head_capture __read_mostly = NULL; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 49 | |
| 50 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 51 | static void http_end_request(struct stream *s); |
| 52 | static void http_end_response(struct stream *s); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 53 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 54 | static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr); |
| 55 | static int http_del_hdr_value(char *start, char *end, char **from, char *next); |
| 56 | 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] | 57 | static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl); |
| 58 | 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] | 59 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 60 | static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s); |
| 61 | static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules, struct list *rules, struct stream *s); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 62 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 63 | static void http_manage_client_side_cookies(struct stream *s, struct channel *req); |
| 64 | static void http_manage_server_side_cookies(struct stream *s, struct channel *res); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 65 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 66 | static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend); |
| 67 | static int http_handle_stats(struct stream *s, struct channel *req); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 68 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 69 | static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg); |
| 70 | static int http_reply_100_continue(struct stream *s); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 71 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 72 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 73 | * processing can continue on next analysers, or zero if it either needs more |
| 74 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 75 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers |
| 76 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 77 | * abort. |
| 78 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 79 | int http_wait_for_request(struct stream *s, struct channel *req, int an_bit) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 80 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 81 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 82 | /* |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 83 | * We will analyze a complete HTTP request to check the its syntax. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 84 | * |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 85 | * Once the start line and all headers are received, we may perform a |
| 86 | * capture of the error (if any), and we will set a few fields. We also |
| 87 | * check for monitor-uri, logging and finally headers capture. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 88 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 89 | struct session *sess = s->sess; |
| 90 | struct http_txn *txn = s->txn; |
| 91 | struct http_msg *msg = &txn->req; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 92 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 93 | struct htx_sl *sl; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 94 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 95 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 96 | |
Christopher Faulet | da46a0d | 2021-01-21 17:32:58 +0100 | [diff] [blame] | 97 | if (unlikely(!IS_HTX_STRM(s))) { |
| 98 | /* It is only possible when a TCP stream is upgrade to HTTP. |
| 99 | * There is a transition period during which there is no |
| 100 | * data. The stream is still in raw mode and SF_IGNORE flag is |
| 101 | * still set. When this happens, the new mux is responsible to |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 102 | * handle all errors. Thus we may leave immediately. |
Christopher Faulet | da46a0d | 2021-01-21 17:32:58 +0100 | [diff] [blame] | 103 | */ |
| 104 | BUG_ON(!(s->flags & SF_IGNORE) || !c_empty(&s->req)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 105 | |
Christopher Faulet | 97b3a61 | 2021-03-15 17:10:12 +0100 | [diff] [blame] | 106 | /* Don't connect for now */ |
| 107 | channel_dont_connect(req); |
| 108 | |
| 109 | /* A SHUTR at this stage means we are performing a "destructive" |
| 110 | * HTTP upgrade (TCP>H2). In this case, we can leave. |
| 111 | */ |
| 112 | if (req->flags & CF_SHUTR) { |
| 113 | s->logs.logwait = 0; |
| 114 | s->logs.level = 0; |
| 115 | channel_abort(&s->req); |
| 116 | channel_abort(&s->res); |
| 117 | req->analysers &= AN_REQ_FLT_END; |
| 118 | req->analyse_exp = TICK_ETERNITY; |
| 119 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s); |
| 120 | return 1; |
| 121 | } |
Christopher Faulet | da46a0d | 2021-01-21 17:32:58 +0100 | [diff] [blame] | 122 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA, s); |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | htx = htxbuf(&req->buf); |
Christopher Faulet | 8bebd2f | 2020-10-06 17:54:56 +0200 | [diff] [blame] | 127 | |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 128 | /* Parsing errors are caught here */ |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 129 | if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) { |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 130 | stream_inc_http_req_ctr(s); |
Emeric Brun | 2897644 | 2020-10-07 08:50:09 +0200 | [diff] [blame] | 131 | proxy_inc_fe_req_ctr(sess->listener, sess->fe); |
Christopher Faulet | bf7175f | 2021-02-10 14:58:01 +0100 | [diff] [blame] | 132 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
| 133 | stream_inc_http_err_ctr(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 134 | goto return_bad_req; |
Christopher Faulet | bf7175f | 2021-02-10 14:58:01 +0100 | [diff] [blame] | 135 | } |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 136 | else |
| 137 | goto return_int_err; |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 140 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 141 | s->srv_error = http_return_srv_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 142 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 143 | msg->msg_state = HTTP_MSG_BODY; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 144 | stream_inc_http_req_ctr(s); |
Emeric Brun | 2897644 | 2020-10-07 08:50:09 +0200 | [diff] [blame] | 145 | proxy_inc_fe_req_ctr(sess->listener, sess->fe); /* one more valid request for this FE */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 146 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 147 | /* kill the pending keep-alive timeout */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 148 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 149 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 150 | BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 151 | sl = http_get_stline(htx); |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 152 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 153 | /* 0: we might have to print this header in debug mode */ |
| 154 | if (unlikely((global.mode & MODE_DEBUG) && |
| 155 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 156 | int32_t pos; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 157 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 158 | http_debug_stline("clireq", s, sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 159 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 160 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 161 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 162 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 163 | |
| 164 | if (type == HTX_BLK_EOH) |
| 165 | break; |
| 166 | if (type != HTX_BLK_HDR) |
| 167 | continue; |
| 168 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 169 | http_debug_hdr("clihdr", s, |
| 170 | htx_get_blk_name(htx, blk), |
| 171 | htx_get_blk_value(htx, blk)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 172 | } |
| 173 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 174 | |
| 175 | /* |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 176 | * 1: identify the method and the version. Also set HTTP flags |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 177 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 178 | txn->meth = sl->info.req.meth; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 179 | if (sl->flags & HTX_SL_F_VER_11) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 180 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 181 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 182 | if (sl->flags & HTX_SL_F_CLEN) |
| 183 | msg->flags |= HTTP_MSGF_CNT_LEN; |
| 184 | else if (sl->flags & HTX_SL_F_CHNK) |
| 185 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Christopher Faulet | b2db4fa | 2018-11-27 16:51:09 +0100 | [diff] [blame] | 186 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 187 | msg->flags |= HTTP_MSGF_BODYLESS; |
Christopher Faulet | 576c358 | 2021-01-08 15:53:01 +0100 | [diff] [blame] | 188 | if (sl->flags & HTX_SL_F_CONN_UPG) |
| 189 | msg->flags |= HTTP_MSGF_CONN_UPG; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 190 | |
| 191 | /* we can make use of server redirect on GET and HEAD */ |
| 192 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 193 | s->flags |= SF_REDIRECTABLE; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 194 | else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 195 | /* PRI is reserved for the HTTP/2 preface */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 196 | goto return_bad_req; |
| 197 | } |
| 198 | |
| 199 | /* |
Christopher Faulet | 6072beb | 2020-02-18 15:34:58 +0100 | [diff] [blame] | 200 | * 2: check if the URI matches the monitor_uri. We have to do this for |
| 201 | * every request which gets in, because the monitor-uri is defined by |
| 202 | * the frontend. If the monitor-uri starts with a '/', the matching is |
| 203 | * done against the request's path. Otherwise, the request's uri is |
| 204 | * used. It is a workaround to let HTTP/2 health-checks work as |
| 205 | * expected. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 206 | */ |
Tim Duesterhus | 4b1fcaa | 2022-03-05 00:52:40 +0100 | [diff] [blame] | 207 | if (unlikely(isttest(sess->fe->monitor_uri))) { |
| 208 | const struct ist monitor_uri = sess->fe->monitor_uri; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 209 | 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] | 210 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 211 | if ((istptr(monitor_uri)[0] == '/' && |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 212 | isteq(http_parse_path(&parser), monitor_uri)) || |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 213 | isteq(htx_sl_req_uri(sl), monitor_uri)) { |
| 214 | /* |
| 215 | * We have found the monitor URI |
| 216 | */ |
| 217 | struct acl_cond *cond; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 218 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 219 | s->flags |= SF_MONITOR; |
| 220 | _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req); |
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 | /* Check if we want to fail this monitor request or not */ |
| 223 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
| 224 | 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] | 225 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 226 | ret = acl_pass(ret); |
| 227 | if (cond->pol == ACL_COND_UNLESS) |
| 228 | ret = !ret; |
| 229 | |
| 230 | if (ret) { |
| 231 | /* we fail this request, let's return 503 service unavail */ |
| 232 | txn->status = 503; |
| 233 | if (!(s->flags & SF_ERR_MASK)) |
| 234 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
| 235 | goto return_prx_cond; |
| 236 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 237 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 238 | |
Amaury Denoyelle | 5a9bd37 | 2021-07-06 11:23:10 +0200 | [diff] [blame] | 239 | /* nothing to fail, let's reply normally */ |
| 240 | txn->status = 200; |
| 241 | if (!(s->flags & SF_ERR_MASK)) |
| 242 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
| 243 | goto return_prx_cond; |
| 244 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* |
| 248 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 249 | * Note: we cannot log anymore if the request has been |
| 250 | * classified as invalid. |
| 251 | */ |
| 252 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 253 | /* we have a complete HTTP request that we must log */ |
| 254 | if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 255 | size_t len; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 256 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 257 | 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] | 258 | txn->uri[len] = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 259 | |
| 260 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
| 261 | s->do_log(s); |
| 262 | } else { |
| 263 | ha_alert("HTTP logging : out of memory.\n"); |
| 264 | } |
| 265 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 266 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 267 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 268 | * we have what looks like a proxied connection instead of a connection, |
| 269 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 270 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 271 | * happen to do that despite being non-standard :-( |
| 272 | * We consider that a request not beginning with either '/' or '*' is |
| 273 | * a proxied connection, which covers both "scheme://location" and |
| 274 | * CONNECT ip:port. |
| 275 | */ |
| 276 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 277 | *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*') |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 278 | txn->flags |= TX_USE_PX_CONN; |
| 279 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 280 | /* 5: we may need to capture headers */ |
| 281 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 282 | http_capture_headers(htx, s->req_cap, sess->fe->req_cap); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 283 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 284 | /* we may have to wait for the request's body */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 285 | if (s->be->options & PR_O_WREQ_BODY) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 286 | req->analysers |= AN_REQ_HTTP_BODY; |
| 287 | |
| 288 | /* |
| 289 | * RFC7234#4: |
| 290 | * A cache MUST write through requests with methods |
| 291 | * that are unsafe (Section 4.2.1 of [RFC7231]) to |
| 292 | * the origin server; i.e., a cache is not allowed |
| 293 | * to generate a reply to such a request before |
| 294 | * having forwarded the request and having received |
| 295 | * a corresponding response. |
| 296 | * |
| 297 | * RFC7231#4.2.1: |
| 298 | * Of the request methods defined by this |
| 299 | * specification, the GET, HEAD, OPTIONS, and TRACE |
| 300 | * methods are defined to be safe. |
| 301 | */ |
| 302 | if (likely(txn->meth == HTTP_METH_GET || |
| 303 | txn->meth == HTTP_METH_HEAD || |
| 304 | txn->meth == HTTP_METH_OPTIONS || |
| 305 | txn->meth == HTTP_METH_TRACE)) |
| 306 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 307 | |
| 308 | /* end of job, return OK */ |
| 309 | req->analysers &= ~an_bit; |
| 310 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 311 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 312 | 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] | 313 | return 1; |
| 314 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 315 | return_int_err: |
| 316 | txn->status = 500; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 317 | if (!(s->flags & SF_ERR_MASK)) |
| 318 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 319 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 320 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 321 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 322 | goto return_prx_cond; |
| 323 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 324 | return_bad_req: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 325 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 326 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 327 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 328 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 329 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 330 | |
| 331 | return_prx_cond: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 332 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 333 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 334 | if (!(s->flags & SF_ERR_MASK)) |
| 335 | s->flags |= SF_ERR_PRXCOND; |
| 336 | if (!(s->flags & SF_FINST_MASK)) |
| 337 | s->flags |= SF_FINST_R; |
| 338 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 339 | DBG_TRACE_DEVEL("leaving on error", |
| 340 | 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] | 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | /* This stream analyser runs all HTTP request processing which is common to |
| 346 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 347 | * reqadd, stats and redirects. This is performed for the designated proxy. |
| 348 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 349 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 350 | * error, ...). |
| 351 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 352 | 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] | 353 | { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 354 | struct list *def_rules, *rules; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 355 | struct session *sess = s->sess; |
| 356 | struct http_txn *txn = s->txn; |
| 357 | struct http_msg *msg = &txn->req; |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 358 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 359 | struct redirect_rule *rule; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 360 | enum rule_result verdict; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 361 | struct connection *conn = objt_conn(sess->origin); |
| 362 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 363 | 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] | 364 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 365 | htx = htxbuf(&req->buf); |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 366 | |
Christopher Faulet | 1907ccc | 2019-04-29 13:12:02 +0200 | [diff] [blame] | 367 | /* just in case we have some per-backend tracking. Only called the first |
| 368 | * execution of the analyser. */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 369 | if (!s->current_rule && !s->current_rule_list) |
Christopher Faulet | 1907ccc | 2019-04-29 13:12:02 +0200 | [diff] [blame] | 370 | stream_inc_be_http_req_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 371 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 372 | def_rules = ((px->defpx && (an_bit == AN_REQ_HTTP_PROCESS_FE || px != sess->fe)) ? &px->defpx->http_req_rules : NULL); |
| 373 | rules = &px->http_req_rules; |
| 374 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 375 | /* evaluate http-request rules */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 376 | if ((def_rules && !LIST_ISEMPTY(def_rules)) || !LIST_ISEMPTY(rules)) { |
| 377 | verdict = http_req_get_intercept_rule(px, def_rules, rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 378 | |
| 379 | switch (verdict) { |
| 380 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 381 | goto return_prx_yield; |
| 382 | |
| 383 | case HTTP_RULE_RES_CONT: |
| 384 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 385 | break; |
| 386 | |
| 387 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 388 | if (txn->flags & TX_CLTARPIT) |
| 389 | goto tarpit; |
| 390 | goto deny; |
| 391 | |
| 392 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 393 | goto return_prx_cond; |
| 394 | |
| 395 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
| 396 | goto done; |
| 397 | |
| 398 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 399 | goto return_bad_req; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 400 | |
| 401 | case HTTP_RULE_RES_ERROR: /* failed with a bad request */ |
| 402 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
| 406 | if (conn && (conn->flags & CO_FL_EARLY_DATA) && |
Olivier Houchard | 220a26c | 2020-01-23 14:57:36 +0100 | [diff] [blame] | 407 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) { |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 408 | struct http_hdr_ctx ctx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 409 | |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 410 | ctx.blk = NULL; |
| 411 | if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) { |
| 412 | if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1")))) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 413 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 414 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /* OK at this stage, we know that the request was accepted according to |
| 418 | * the http-request rules, we can check for the stats. Note that the |
| 419 | * URI is detected *before* the req* rules in order not to be affected |
| 420 | * by a possible reqrep, while they are processed *after* so that a |
| 421 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 422 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 423 | if (!s->target && http_stats_check_uri(s, txn, px)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 424 | s->target = &http_stats_applet.obj_type; |
Christopher Faulet | 1336ccf | 2022-04-12 18:15:16 +0200 | [diff] [blame^] | 425 | if (unlikely(!cs_applet_create(s->csb, objt_applet(s->target)))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 426 | s->logs.tv_request = now; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 427 | if (!(s->flags & SF_ERR_MASK)) |
| 428 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 429 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /* parse the whole stats request and extract the relevant information */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 433 | http_handle_stats(s, req); |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 434 | 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] | 435 | /* not all actions implemented: deny, allow, auth */ |
| 436 | |
| 437 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 438 | goto deny; |
| 439 | |
| 440 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 441 | goto return_prx_cond; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 442 | |
| 443 | if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */ |
| 444 | goto return_bad_req; |
| 445 | |
| 446 | if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */ |
| 447 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 448 | } |
| 449 | |
Christopher Faulet | 2571bc6 | 2019-03-01 11:44:26 +0100 | [diff] [blame] | 450 | /* Proceed with the applets now. */ |
| 451 | if (unlikely(objt_applet(s->target))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 452 | 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] | 453 | _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 454 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 455 | if (http_handle_expect_hdr(s, htx, msg) == -1) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 456 | goto return_int_err; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 457 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 458 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 459 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 460 | if (!(s->flags & SF_FINST_MASK)) |
| 461 | s->flags |= SF_FINST_R; |
| 462 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 463 | if (HAS_FILTERS(s)) |
| 464 | req->analysers |= AN_REQ_FLT_HTTP_HDRS; |
| 465 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 466 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
| 467 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 468 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 469 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 470 | |
| 471 | req->flags |= CF_SEND_DONTWAIT; |
| 472 | s->flags |= SF_ASSIGNED; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 473 | goto done; |
| 474 | } |
| 475 | |
| 476 | /* check whether we have some ACLs set to redirect this request */ |
| 477 | list_for_each_entry(rule, &px->redirect_rules, list) { |
| 478 | if (rule->cond) { |
| 479 | int ret; |
| 480 | |
| 481 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 482 | ret = acl_pass(ret); |
| 483 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 484 | ret = !ret; |
| 485 | if (!ret) |
| 486 | continue; |
| 487 | } |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 488 | if (!http_apply_redirect_rule(rule, s, txn)) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 489 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 490 | goto done; |
| 491 | } |
| 492 | |
| 493 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 494 | * If this happens, then the data will not come immediately, so we must |
| 495 | * send all what we have without waiting. Note that due to the small gain |
| 496 | * in waiting for the body of the request, it's easier to simply put the |
| 497 | * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 498 | * itself once used. |
| 499 | */ |
| 500 | req->flags |= CF_SEND_DONTWAIT; |
| 501 | |
| 502 | done: /* done with this analyser, continue with next ones that the calling |
| 503 | * points will have set, if any. |
| 504 | */ |
| 505 | req->analyse_exp = TICK_ETERNITY; |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 506 | 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] | 507 | req->analysers &= ~an_bit; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 508 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 509 | 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] | 510 | return 1; |
| 511 | |
| 512 | tarpit: |
| 513 | /* Allow cookie logging |
| 514 | */ |
| 515 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 516 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 517 | |
| 518 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 519 | * which may be the same as the connect timeout if unspecified. |
| 520 | * If unset, then set it to zero because we really want it to |
| 521 | * eventually expire. We build the tarpit as an analyser. |
| 522 | */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 523 | channel_htx_erase(&s->req, htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 524 | |
| 525 | /* wipe the request out so that we can drop the connection early |
| 526 | * if the client closes first. |
| 527 | */ |
| 528 | channel_dont_connect(req); |
| 529 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 530 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
| 531 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 532 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 533 | if (!req->analyse_exp) |
| 534 | req->analyse_exp = tick_add(now_ms, 0); |
| 535 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 536 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 537 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 538 | _HA_ATOMIC_INC(&s->be->be_counters.denied_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 539 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 540 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 541 | goto done_without_exp; |
| 542 | |
| 543 | deny: /* this request was blocked (denied) */ |
| 544 | |
| 545 | /* Allow cookie logging |
| 546 | */ |
| 547 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 548 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 549 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 550 | s->logs.tv_request = now; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 551 | stream_inc_http_err_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 552 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 553 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 554 | _HA_ATOMIC_INC(&s->be->be_counters.denied_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 555 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 556 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 557 | goto return_prx_err; |
| 558 | |
| 559 | return_int_err: |
| 560 | txn->status = 500; |
| 561 | if (!(s->flags & SF_ERR_MASK)) |
| 562 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 563 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 564 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 565 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 566 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 567 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 568 | goto return_prx_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 569 | |
| 570 | return_bad_req: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 571 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 572 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 573 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 574 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 575 | /* fall through */ |
| 576 | |
| 577 | return_prx_err: |
| 578 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 579 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 580 | |
| 581 | return_prx_cond: |
| 582 | if (!(s->flags & SF_ERR_MASK)) |
| 583 | s->flags |= SF_ERR_PRXCOND; |
| 584 | if (!(s->flags & SF_FINST_MASK)) |
| 585 | s->flags |= SF_FINST_R; |
| 586 | |
| 587 | req->analysers &= AN_REQ_FLT_END; |
| 588 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 589 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 590 | DBG_TRACE_DEVEL("leaving on error", |
| 591 | 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] | 592 | return 0; |
| 593 | |
| 594 | return_prx_yield: |
| 595 | channel_dont_connect(req); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 596 | DBG_TRACE_DEVEL("waiting for more data", |
| 597 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | /* This function performs all the processing enabled for the current request. |
| 602 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 603 | * needs more data, encounters an error, or wants to immediately abort the |
| 604 | * request. It relies on buffers flags, and updates s->req.analysers. |
| 605 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 606 | 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] | 607 | { |
| 608 | struct session *sess = s->sess; |
| 609 | struct http_txn *txn = s->txn; |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 610 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 611 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
| 612 | |
Christopher Faulet | 8bebd2f | 2020-10-06 17:54:56 +0200 | [diff] [blame] | 613 | 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] | 614 | |
| 615 | /* |
| 616 | * Right now, we know that we have processed the entire headers |
| 617 | * and that unwanted requests have been filtered out. We can do |
| 618 | * whatever we want with the remaining request. Also, now we |
| 619 | * may have separate values for ->fe, ->be. |
| 620 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 621 | htx = htxbuf(&req->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 622 | |
| 623 | /* |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 624 | * 7: Now we can work with the cookies. |
| 625 | * Note that doing so might move headers in the request, but |
| 626 | * the fields will stay coherent and the URI will not move. |
| 627 | * This should only be performed in the backend. |
| 628 | */ |
| 629 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 630 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 631 | |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 632 | /* 8: Generate unique ID if a "unique-id-format" is defined. |
| 633 | * |
| 634 | * A unique ID is generated even when it is not sent to ensure that the ID can make use of |
| 635 | * fetches only available in the HTTP request processing stage. |
| 636 | */ |
| 637 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 638 | 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] | 639 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 640 | if (!isttest(unique_id)) { |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 641 | if (!(s->flags & SF_ERR_MASK)) |
| 642 | s->flags |= SF_ERR_RESOURCE; |
| 643 | goto return_int_err; |
| 644 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 645 | |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 646 | /* send unique ID if a "unique-id-header" is defined */ |
Tim Duesterhus | 0643b0e | 2020-03-05 17:56:35 +0100 | [diff] [blame] | 647 | if (isttest(sess->fe->header_unique_id) && |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 648 | unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id))) |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 649 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /* |
| 653 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 654 | * asks for it. |
| 655 | */ |
| 656 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Christopher Faulet | 8da67aa | 2022-03-29 17:53:09 +0200 | [diff] [blame] | 657 | const struct sockaddr_storage *src = cs_src(s->csf); |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 658 | struct http_hdr_ctx ctx = { .blk = NULL }; |
Tim Duesterhus | b50ab84 | 2022-03-05 00:52:41 +0100 | [diff] [blame] | 659 | struct ist hdr = isttest(s->be->fwdfor_hdr_name) ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name; |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 660 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 661 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 662 | http_find_header(htx, hdr, &ctx, 0)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 663 | /* The header is set to be added only if none is present |
| 664 | * and we found it, so don't do anything. |
| 665 | */ |
| 666 | } |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 667 | else if (src && src->ss_family == AF_INET) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 668 | /* Add an X-Forwarded-For header unless the source IP is |
| 669 | * in the 'except' network range. |
| 670 | */ |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 671 | if (ipcmp2net(src, &sess->fe->except_xff_net) && |
| 672 | ipcmp2net(src, &s->be->except_xff_net)) { |
| 673 | unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)src)->sin_addr; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 674 | |
| 675 | /* Note: we rely on the backend to get the header name to be used for |
| 676 | * x-forwarded-for, because the header is really meant for the backends. |
| 677 | * However, if the backend did not specify any option, we have to rely |
| 678 | * on the frontend's header name. |
| 679 | */ |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 680 | chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 681 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 682 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 683 | } |
| 684 | } |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 685 | else if (src && src->ss_family == AF_INET6) { |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 686 | /* Add an X-Forwarded-For header unless the source IP is |
| 687 | * in the 'except' network range. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 688 | */ |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 689 | if (ipcmp2net(src, &sess->fe->except_xff_net) && |
| 690 | ipcmp2net(src, &s->be->except_xff_net)) { |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 691 | char pn[INET6_ADDRSTRLEN]; |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 692 | |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 693 | inet_ntop(AF_INET6, |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 694 | (const void *)&((struct sockaddr_in6 *)(src))->sin6_addr, |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 695 | pn, sizeof(pn)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 696 | |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 697 | /* Note: we rely on the backend to get the header name to be used for |
| 698 | * x-forwarded-for, because the header is really meant for the backends. |
| 699 | * However, if the backend did not specify any option, we have to rely |
| 700 | * on the frontend's header name. |
| 701 | */ |
| 702 | chunk_printf(&trash, "%s", pn); |
| 703 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
| 704 | goto return_int_err; |
| 705 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | |
| 709 | /* |
| 710 | * 10: add X-Original-To if either the frontend or the backend |
| 711 | * asks for it. |
| 712 | */ |
| 713 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
Christopher Faulet | 8da67aa | 2022-03-29 17:53:09 +0200 | [diff] [blame] | 714 | const struct sockaddr_storage *dst = cs_dst(s->csf); |
Tim Duesterhus | e502c3e | 2022-03-05 00:52:42 +0100 | [diff] [blame] | 715 | struct ist hdr = isttest(s->be->orgto_hdr_name) ? s->be->orgto_hdr_name : sess->fe->orgto_hdr_name; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 716 | |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 717 | if (dst && dst->ss_family == AF_INET) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 718 | /* Add an X-Original-To header unless the destination IP is |
| 719 | * in the 'except' network range. |
| 720 | */ |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 721 | if (ipcmp2net(dst, &sess->fe->except_xot_net) && |
| 722 | ipcmp2net(dst, &s->be->except_xot_net)) { |
| 723 | unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)dst)->sin_addr; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 724 | |
| 725 | /* Note: we rely on the backend to get the header name to be used for |
| 726 | * x-original-to, because the header is really meant for the backends. |
| 727 | * However, if the backend did not specify any option, we have to rely |
| 728 | * on the frontend's header name. |
| 729 | */ |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 730 | chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 731 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 732 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 733 | } |
| 734 | } |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 735 | else if (dst && dst->ss_family == AF_INET6) { |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 736 | /* Add an X-Original-To header unless the source IP is |
| 737 | * in the 'except' network range. |
| 738 | */ |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 739 | if (ipcmp2net(dst, &sess->fe->except_xot_net) && |
| 740 | ipcmp2net(dst, &s->be->except_xot_net)) { |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 741 | char pn[INET6_ADDRSTRLEN]; |
| 742 | |
| 743 | inet_ntop(AF_INET6, |
Christopher Faulet | 8a104ba | 2021-10-25 07:41:30 +0200 | [diff] [blame] | 744 | (const void *)&((struct sockaddr_in6 *)dst)->sin6_addr, |
Christopher Faulet | 5d1def6 | 2021-02-26 09:19:15 +0100 | [diff] [blame] | 745 | pn, sizeof(pn)); |
| 746 | |
| 747 | /* Note: we rely on the backend to get the header name to be used for |
| 748 | * x-forwarded-for, because the header is really meant for the backends. |
| 749 | * However, if the backend did not specify any option, we have to rely |
| 750 | * on the frontend's header name. |
| 751 | */ |
| 752 | chunk_printf(&trash, "%s", pn); |
| 753 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
| 754 | goto return_int_err; |
| 755 | } |
| 756 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 757 | } |
| 758 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 759 | /* Filter the request headers if there are filters attached to the |
| 760 | * stream. |
| 761 | */ |
| 762 | if (HAS_FILTERS(s)) |
| 763 | req->analysers |= AN_REQ_FLT_HTTP_HDRS; |
| 764 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 765 | /* If we have no server assigned yet and we're balancing on url_param |
| 766 | * with a POST request, we may be interested in checking the body for |
| 767 | * that parameter. This will be done in another analyser. |
| 768 | */ |
| 769 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | 089eaa0 | 2019-01-14 15:17:46 +0100 | [diff] [blame] | 770 | s->txn->meth == HTTP_METH_POST && |
| 771 | (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 772 | channel_dont_connect(req); |
| 773 | req->analysers |= AN_REQ_HTTP_BODY; |
| 774 | } |
| 775 | |
| 776 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 777 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 778 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 779 | /* We expect some data from the client. Unless we know for sure |
| 780 | * we already have a full request, we have to re-enable quick-ack |
| 781 | * in case we previously disabled it, otherwise we might cause |
| 782 | * the client to delay further data. |
| 783 | */ |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 784 | 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] | 785 | conn_set_quickack(cli_conn, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 786 | |
| 787 | /************************************************************* |
| 788 | * OK, that's finished for the headers. We have done what we * |
| 789 | * could. Let's switch to the DATA state. * |
| 790 | ************************************************************/ |
| 791 | req->analyse_exp = TICK_ETERNITY; |
| 792 | req->analysers &= ~an_bit; |
| 793 | |
| 794 | s->logs.tv_request = now; |
| 795 | /* OK let's go on with the BODY now */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 796 | 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] | 797 | return 1; |
| 798 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 799 | return_int_err: |
| 800 | txn->status = 500; |
| 801 | if (!(s->flags & SF_ERR_MASK)) |
| 802 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 803 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 804 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 805 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 806 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 807 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 808 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 809 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 810 | |
| 811 | if (!(s->flags & SF_ERR_MASK)) |
| 812 | s->flags |= SF_ERR_PRXCOND; |
| 813 | if (!(s->flags & SF_FINST_MASK)) |
| 814 | s->flags |= SF_FINST_R; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 815 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 816 | DBG_TRACE_DEVEL("leaving on error", |
| 817 | 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] | 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 822 | * returns zero, at the beginning because it prevents any other processing |
| 823 | * from occurring, and at the end because it terminates the request. |
| 824 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 825 | 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] | 826 | { |
| 827 | struct http_txn *txn = s->txn; |
| 828 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 829 | 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] | 830 | /* This connection is being tarpitted. The CLIENT side has |
| 831 | * already set the connect expiration date to the right |
| 832 | * timeout. We just have to check that the client is still |
| 833 | * there and that the timeout has not expired. |
| 834 | */ |
| 835 | channel_dont_connect(req); |
| 836 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 837 | !tick_is_expired(req->analyse_exp, now_ms)) { |
Christopher Faulet | b0c87f1 | 2021-10-29 14:37:07 +0200 | [diff] [blame] | 838 | /* Be sure to drain all data from the request channel */ |
| 839 | channel_htx_erase(req, htxbuf(&req->buf)); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 840 | DBG_TRACE_DEVEL("waiting for tarpit timeout expiry", |
| 841 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 842 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 843 | } |
| 844 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 845 | |
| 846 | /* We will set the queue timer to the time spent, just for |
| 847 | * logging purposes. We fake a 500 server error, so that the |
| 848 | * attacker will not suspect his connection has been tarpitted. |
| 849 | * It will not cause trouble to the logs because we can exclude |
| 850 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 851 | */ |
| 852 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 853 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 854 | http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL)); |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 855 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 856 | if (!(s->flags & SF_ERR_MASK)) |
| 857 | s->flags |= SF_ERR_PRXCOND; |
| 858 | if (!(s->flags & SF_FINST_MASK)) |
| 859 | s->flags |= SF_FINST_T; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 860 | |
| 861 | 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] | 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 866 | * for either the buffer to be full, or the full advertised contents to have |
| 867 | * reached the buffer. It must only be called after the standard HTTP request |
| 868 | * processing has occurred, because it expects the request to be parsed and will |
| 869 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 870 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 871 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 872 | * needs to read more data, or 1 once it has completed its analysis. |
| 873 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 874 | 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] | 875 | { |
| 876 | struct session *sess = s->sess; |
| 877 | struct http_txn *txn = s->txn; |
| 878 | struct http_msg *msg = &s->txn->req; |
| 879 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 880 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg); |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 881 | |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 882 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 883 | switch (http_wait_for_msg_body(s, req, s->be->timeout.httpreq, 0)) { |
| 884 | case HTTP_RULE_RES_CONT: |
| 885 | goto http_end; |
| 886 | case HTTP_RULE_RES_YIELD: |
| 887 | goto missing_data_or_waiting; |
| 888 | case HTTP_RULE_RES_BADREQ: |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 889 | goto return_bad_req; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 890 | case HTTP_RULE_RES_ERROR: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 891 | goto return_int_err; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 892 | case HTTP_RULE_RES_ABRT: |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 893 | goto return_prx_cond; |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 894 | default: |
| 895 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | http_end: |
| 899 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 900 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 901 | req->analysers &= ~an_bit; |
| 902 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 903 | 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] | 904 | return 1; |
| 905 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 906 | missing_data_or_waiting: |
| 907 | channel_dont_connect(req); |
| 908 | DBG_TRACE_DEVEL("waiting for more data", |
| 909 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 910 | return 0; |
| 911 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 912 | return_int_err: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 913 | txn->status = 500; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 914 | if (!(s->flags & SF_ERR_MASK)) |
| 915 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 916 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 917 | if (s->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 918 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 919 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 920 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 921 | goto return_prx_err; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 922 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 923 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 924 | txn->status = 400; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 925 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 926 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 927 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 928 | /* fall through */ |
| 929 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 930 | return_prx_err: |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 931 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 932 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 933 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 934 | return_prx_cond: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 935 | if (!(s->flags & SF_ERR_MASK)) |
| 936 | s->flags |= SF_ERR_PRXCOND; |
| 937 | if (!(s->flags & SF_FINST_MASK)) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 938 | s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 939 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 940 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 941 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 942 | DBG_TRACE_DEVEL("leaving on error", |
| 943 | 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] | 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | /* This function is an analyser which forwards request body (including chunk |
| 948 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 949 | * zero byte. The only situation where it must not be called is when we're in |
| 950 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 951 | * remaining data and to resync after end of body. It expects the msg_state to |
| 952 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 953 | * read more data, or 1 once we can go on with next request or end the stream. |
| 954 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
| 955 | * bytes of pending data + the headers if not already done. |
| 956 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 957 | 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] | 958 | { |
| 959 | struct session *sess = s->sess; |
| 960 | struct http_txn *txn = s->txn; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 961 | struct http_msg *msg = &txn->req; |
| 962 | struct htx *htx; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 963 | short status = 0; |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 964 | int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 965 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 966 | 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] | 967 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 968 | htx = htxbuf(&req->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 969 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 970 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 971 | goto return_bad_req; |
| 972 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 973 | goto return_int_err; |
| 974 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 975 | if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
| 976 | ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) { |
| 977 | /* Output closed while we were sending data. We must abort and |
| 978 | * wake the other side up. |
Christopher Faulet | f506d96 | 2021-04-27 10:56:28 +0200 | [diff] [blame] | 979 | * |
| 980 | * If we have finished to send the request and the response is |
| 981 | * still in progress, don't catch write error on the request |
| 982 | * side if it is in fact a read error on the server side. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 983 | */ |
Christopher Faulet | f506d96 | 2021-04-27 10:56:28 +0200 | [diff] [blame] | 984 | if (msg->msg_state == HTTP_MSG_DONE && (s->res.flags & CF_READ_ERROR) && s->res.analysers) |
| 985 | return 0; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 986 | |
Olivier Houchard | 29cac3c | 2019-07-12 15:48:58 +0200 | [diff] [blame] | 987 | /* Don't abort yet if we had L7 retries activated and it |
| 988 | * was a write error, we may recover. |
| 989 | */ |
| 990 | if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) && |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 991 | (txn->flags & TX_L7_RETRY)) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 992 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 993 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Olivier Houchard | 29cac3c | 2019-07-12 15:48:58 +0200 | [diff] [blame] | 994 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 995 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 996 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 997 | http_end_request(s); |
| 998 | http_end_response(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 999 | DBG_TRACE_DEVEL("leaving on error", |
| 1000 | 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] | 1001 | return 1; |
| 1002 | } |
| 1003 | |
| 1004 | /* Note that we don't have to send 100-continue back because we don't |
| 1005 | * need the data to complete our job, and it's up to the server to |
| 1006 | * decide whether to return 100, 417 or anything else in return of |
| 1007 | * an "Expect: 100-continue" header. |
| 1008 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1009 | if (msg->msg_state == HTTP_MSG_BODY) |
| 1010 | msg->msg_state = HTTP_MSG_DATA; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1011 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1012 | /* in most states, we should abort in case of early close */ |
| 1013 | channel_auto_close(req); |
| 1014 | |
| 1015 | if (req->to_forward) { |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 1016 | if (req->to_forward == CHN_INFINITE_FORWARD) { |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1017 | if (req->flags & CF_EOI) |
| 1018 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 1019 | } |
| 1020 | else { |
| 1021 | /* We can't process the buffer's contents yet */ |
| 1022 | req->flags |= CF_WAKE_WRITE; |
| 1023 | goto missing_data_or_waiting; |
| 1024 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1025 | } |
| 1026 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1027 | if (msg->msg_state >= HTTP_MSG_ENDING) |
| 1028 | goto ending; |
| 1029 | |
| 1030 | if (txn->meth == HTTP_METH_CONNECT) { |
| 1031 | msg->msg_state = HTTP_MSG_ENDING; |
| 1032 | goto ending; |
| 1033 | } |
| 1034 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1035 | /* Forward input data. We get it by removing all outgoing data not |
| 1036 | * forwarded yet from HTX data size. If there are some data filters, we |
| 1037 | * let them decide the amount of data to forward. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1038 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1039 | if (HAS_REQ_DATA_FILTERS(s)) { |
| 1040 | ret = flt_http_payload(s, msg, htx->data); |
| 1041 | if (ret < 0) |
| 1042 | goto return_bad_req; |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1043 | c_adv(req, ret); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1044 | } |
| 1045 | else { |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1046 | c_adv(req, htx->data - co_data(req)); |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 1047 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
| 1048 | channel_htx_forward_forever(req, htx); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1049 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1050 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1051 | if (htx->data != co_data(req)) |
| 1052 | goto missing_data_or_waiting; |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1053 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1054 | /* 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] | 1055 | * in HTTP_MSG_ENDING state. Then if all data was marked to be |
| 1056 | * forwarded, set the state to HTTP_MSG_DONE. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1057 | */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 1058 | if (!(htx->flags & HTX_FL_EOM)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1059 | goto missing_data_or_waiting; |
| 1060 | |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1061 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1062 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1063 | ending: |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 1064 | req->flags &= ~CF_EXPECT_MORE; /* no more data are expected */ |
| 1065 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1066 | /* other states, ENDING...TUNNEL */ |
| 1067 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 1068 | goto done; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1069 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1070 | if (HAS_REQ_DATA_FILTERS(s)) { |
| 1071 | ret = flt_http_end(s, msg); |
| 1072 | if (ret <= 0) { |
| 1073 | if (!ret) |
| 1074 | goto missing_data_or_waiting; |
| 1075 | goto return_bad_req; |
| 1076 | } |
| 1077 | } |
| 1078 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1079 | if (txn->meth == HTTP_METH_CONNECT) |
| 1080 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 1081 | else { |
| 1082 | msg->msg_state = HTTP_MSG_DONE; |
| 1083 | req->to_forward = 0; |
| 1084 | } |
| 1085 | |
| 1086 | done: |
| 1087 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 1088 | if (!(txn->flags & TX_CON_WANT_TUN)) |
| 1089 | channel_dont_close(req); |
| 1090 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1091 | http_end_request(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1092 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1093 | http_end_response(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1094 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 1095 | if (req->flags & CF_SHUTW) { |
| 1096 | /* request errors are most likely due to the |
| 1097 | * server aborting the transfer. */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1098 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1099 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1100 | goto return_bad_req; |
| 1101 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1102 | 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] | 1103 | return 1; |
| 1104 | } |
| 1105 | |
| 1106 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 1107 | * the client's connection and forward any shutdown notification to the |
| 1108 | * server, which will decide whether to close or to go on processing the |
| 1109 | * request. We only do that in tunnel mode, and not in other modes since |
| 1110 | * it can be abused to exhaust source ports. */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 1111 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1112 | channel_auto_read(req); |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 1113 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN)) |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1114 | s->csb->flags |= CS_FL_NOLINGER; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1115 | channel_auto_close(req); |
| 1116 | } |
| 1117 | else if (s->txn->meth == HTTP_METH_POST) { |
| 1118 | /* POST requests may require to read extra CRLF sent by broken |
| 1119 | * browsers and which could cause an RST to be sent upon close |
| 1120 | * on some systems (eg: Linux). */ |
| 1121 | channel_auto_read(req); |
| 1122 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1123 | DBG_TRACE_DEVEL("waiting for the end of the HTTP txn", |
| 1124 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1125 | return 0; |
| 1126 | |
| 1127 | missing_data_or_waiting: |
| 1128 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1129 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1130 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1131 | |
| 1132 | waiting: |
| 1133 | /* waiting for the last bits to leave the buffer */ |
| 1134 | if (req->flags & CF_SHUTW) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1135 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1136 | |
| 1137 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 1138 | * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE. |
| 1139 | * And when content-length is used, we never want to let the possible |
| 1140 | * shutdown be forwarded to the other side, as the state machine will |
| 1141 | * take care of it once the client responds. It's also important to |
| 1142 | * prevent TIME_WAITs from accumulating on the backend side, and for |
| 1143 | * HTTP/2 where the last frame comes with a shutdown. |
| 1144 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1145 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1146 | channel_dont_close(req); |
| 1147 | |
| 1148 | /* We know that more data are expected, but we couldn't send more that |
| 1149 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
| 1150 | * system knows it must not set a PUSH on this first part. Interactive |
| 1151 | * modes are already handled by the stream sock layer. We must not do |
| 1152 | * this in content-length mode because it could present the MSG_MORE |
| 1153 | * flag with the last block of forwarded data, which would cause an |
| 1154 | * additional delay to be observed by the receiver. |
| 1155 | */ |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 1156 | if (HAS_REQ_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1157 | req->flags |= CF_EXPECT_MORE; |
| 1158 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1159 | DBG_TRACE_DEVEL("waiting for more data to forward", |
| 1160 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1161 | return 0; |
| 1162 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1163 | return_cli_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1164 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 1165 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1166 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1167 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1168 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1169 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1170 | if (!(s->flags & SF_ERR_MASK)) |
| 1171 | s->flags |= SF_ERR_CLICL; |
| 1172 | status = 400; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1173 | goto return_prx_cond; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1174 | |
| 1175 | return_srv_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1176 | _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts); |
| 1177 | _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1178 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1179 | _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1180 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1181 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1182 | if (!(s->flags & SF_ERR_MASK)) |
| 1183 | s->flags |= SF_ERR_SRVCL; |
| 1184 | status = 502; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1185 | goto return_prx_cond; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1186 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1187 | return_int_err: |
| 1188 | if (!(s->flags & SF_ERR_MASK)) |
| 1189 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1190 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 1191 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1192 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1193 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1194 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1195 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1196 | status = 500; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1197 | goto return_prx_cond; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1198 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1199 | return_bad_req: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1200 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1201 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1202 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1203 | status = 400; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1204 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1205 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1206 | return_prx_cond: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1207 | if (txn->status > 0) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1208 | /* 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] | 1209 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1210 | } else { |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1211 | txn->status = status; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1212 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1213 | } |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1214 | if (!(s->flags & SF_ERR_MASK)) |
| 1215 | s->flags |= SF_ERR_PRXCOND; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1216 | if (!(s->flags & SF_FINST_MASK)) |
| 1217 | s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1218 | DBG_TRACE_DEVEL("leaving on error ", |
| 1219 | 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] | 1220 | return 0; |
| 1221 | } |
| 1222 | |
Christopher Faulet | 7739799 | 2022-04-04 11:07:08 +0200 | [diff] [blame] | 1223 | /* Reset the stream and the backend conn_stream to a situation suitable for attemption connection */ |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1224 | /* Returns 0 if we can attempt to retry, -1 otherwise */ |
Christopher Faulet | 7739799 | 2022-04-04 11:07:08 +0200 | [diff] [blame] | 1225 | static __inline int do_l7_retry(struct stream *s, struct conn_stream *cs) |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1226 | { |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1227 | struct channel *req, *res; |
| 1228 | int co_data; |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1229 | |
Christopher Faulet | 731c8e6 | 2022-03-29 16:08:44 +0200 | [diff] [blame] | 1230 | s->conn_retries++; |
| 1231 | if (s->conn_retries >= s->be->conn_retries) |
Christopher Faulet | 552601d | 2021-05-26 10:31:06 +0200 | [diff] [blame] | 1232 | return -1; |
Christopher Faulet | 5b82cc5 | 2020-10-12 15:18:50 +0200 | [diff] [blame] | 1233 | |
Christopher Faulet | e763c8c | 2021-05-05 18:23:59 +0200 | [diff] [blame] | 1234 | if (objt_server(s->target)) { |
| 1235 | if (s->flags & SF_CURR_SESS) { |
| 1236 | s->flags &= ~SF_CURR_SESS; |
| 1237 | _HA_ATOMIC_DEC(&__objt_server(s->target)->cur_sess); |
| 1238 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1239 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.retries); |
Christopher Faulet | e763c8c | 2021-05-05 18:23:59 +0200 | [diff] [blame] | 1240 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1241 | _HA_ATOMIC_INC(&s->be->be_counters.retries); |
Willy Tarreau | 223995e | 2019-05-04 10:38:31 +0200 | [diff] [blame] | 1242 | |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1243 | req = &s->req; |
| 1244 | res = &s->res; |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1245 | /* Remove any write error from the request, and read error from the response */ |
| 1246 | req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW); |
| 1247 | res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW); |
Christopher Faulet | 7bf46bb | 2022-01-04 10:56:03 +0100 | [diff] [blame] | 1248 | res->analysers &= AN_RES_FLT_END; |
Christopher Faulet | 7739799 | 2022-04-04 11:07:08 +0200 | [diff] [blame] | 1249 | cs->endp->flags &= ~CS_EP_RXBLK_SHUT; |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 1250 | s->conn_err_type = STRM_ET_NONE; |
Christopher Faulet | ae024ce | 2022-03-29 19:02:31 +0200 | [diff] [blame] | 1251 | s->flags &= ~(SF_CONN_EXP | SF_ERR_MASK | SF_FINST_MASK); |
| 1252 | s->conn_exp = TICK_ETERNITY; |
Olivier Houchard | 4bd5867 | 2019-07-12 16:16:59 +0200 | [diff] [blame] | 1253 | stream_choose_redispatch(s); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1254 | res->rex = TICK_ETERNITY; |
| 1255 | res->to_forward = 0; |
| 1256 | res->analyse_exp = TICK_ETERNITY; |
| 1257 | res->total = 0; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 1258 | |
| 1259 | if (cs_reset_endp(s->csb) < 0) { |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 1260 | if (!(s->flags & SF_ERR_MASK)) |
| 1261 | s->flags |= SF_ERR_INTERNAL; |
| 1262 | return -1; |
| 1263 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1264 | |
Christopher Faulet | 9f5382e | 2021-05-21 13:46:14 +0200 | [diff] [blame] | 1265 | b_free(&req->buf); |
| 1266 | /* Swap the L7 buffer with the channel buffer */ |
| 1267 | /* 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] | 1268 | co_data = b_data(&s->txn->l7_buffer); |
| 1269 | b_set_data(&s->txn->l7_buffer, b_size(&s->txn->l7_buffer)); |
| 1270 | 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] | 1271 | co_set_data(req, co_data); |
Christopher Faulet | 5b82cc5 | 2020-10-12 15:18:50 +0200 | [diff] [blame] | 1272 | |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 1273 | 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] | 1274 | |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1275 | b_reset(&res->buf); |
| 1276 | co_set_data(res, 0); |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1280 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 1281 | * processing can continue on next analysers, or zero if it either needs more |
| 1282 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 1283 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers |
| 1284 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1285 | * abort. |
| 1286 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1287 | 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] | 1288 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1289 | /* |
| 1290 | * We will analyze a complete HTTP response to check the its syntax. |
| 1291 | * |
| 1292 | * Once the start line and all headers are received, we may perform a |
| 1293 | * capture of the error (if any), and we will set a few fields. We also |
| 1294 | * logging and finally headers capture. |
| 1295 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1296 | struct session *sess = s->sess; |
| 1297 | struct http_txn *txn = s->txn; |
| 1298 | struct http_msg *msg = &txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1299 | struct htx *htx; |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1300 | struct connection *srv_conn; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1301 | struct htx_sl *sl; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1302 | int n; |
| 1303 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1304 | 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] | 1305 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1306 | htx = htxbuf(&rep->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1307 | |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1308 | /* Parsing errors are caught here */ |
| 1309 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 1310 | goto return_bad_res; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1311 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 1312 | goto return_int_err; |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1313 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1314 | /* |
| 1315 | * Now we quickly check if we have found a full valid response. |
| 1316 | * If not so, we check the FD and buffer states before leaving. |
| 1317 | * A full response is indicated by the fact that we have seen |
| 1318 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
| 1319 | * responses are checked first. |
| 1320 | * |
| 1321 | * Depending on whether the client is still there or not, we |
| 1322 | * may send an error response back or not. Note that normally |
| 1323 | * we should only check for HTTP status there, and check I/O |
| 1324 | * errors somewhere else. |
| 1325 | */ |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1326 | next_one: |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 1327 | if (unlikely(htx_is_empty(htx) || htx->first == -1)) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1328 | /* 1: have we encountered a read error ? */ |
| 1329 | if (rep->flags & CF_READ_ERROR) { |
Christopher Faulet | 95a61e8 | 2021-12-22 14:22:03 +0100 | [diff] [blame] | 1330 | struct connection *conn = cs_conn(s->csb); |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1331 | |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1332 | /* Perform a L7 retry because server refuses the early data. */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1333 | if ((txn->flags & TX_L7_RETRY) && |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1334 | (s->be->retry_type & PR_RE_EARLY_ERROR) && |
| 1335 | conn && conn->err_code == CO_ER_SSL_EARLY_FAILED && |
Christopher Faulet | 7739799 | 2022-04-04 11:07:08 +0200 | [diff] [blame] | 1336 | do_l7_retry(s, s->csb) == 0) { |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1337 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1338 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
Olivier Houchard | 6db1699 | 2019-05-17 15:40:49 +0200 | [diff] [blame] | 1342 | if (txn->flags & TX_NOT_FIRST) |
| 1343 | goto abort_keep_alive; |
| 1344 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1345 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1346 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1347 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1348 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1349 | } |
| 1350 | |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1351 | /* if the server refused the early data, just send a 425 */ |
| 1352 | if (conn && conn->err_code == CO_ER_SSL_EARLY_FAILED) |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1353 | txn->status = 425; |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1354 | else { |
| 1355 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1356 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | d976923 | 2021-05-26 12:15:37 +0200 | [diff] [blame] | 1357 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1358 | |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1359 | s->csb->flags |= CS_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1360 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1361 | |
| 1362 | if (!(s->flags & SF_ERR_MASK)) |
| 1363 | s->flags |= SF_ERR_SRVCL; |
| 1364 | if (!(s->flags & SF_FINST_MASK)) |
| 1365 | s->flags |= SF_FINST_H; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1366 | DBG_TRACE_DEVEL("leaving on error", |
| 1367 | 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] | 1368 | return 0; |
| 1369 | } |
| 1370 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1371 | /* 2: read timeout : return a 504 to the client. */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1372 | else if (rep->flags & CF_READ_TIMEOUT) { |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1373 | if ((txn->flags & TX_L7_RETRY) && |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1374 | (s->be->retry_type & PR_RE_TIMEOUT)) { |
Christopher Faulet | 7739799 | 2022-04-04 11:07:08 +0200 | [diff] [blame] | 1375 | if (co_data(rep) || do_l7_retry(s, s->csb) == 0) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1376 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1377 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1378 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1379 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1380 | } |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1381 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1382 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1383 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1384 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1385 | } |
| 1386 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1387 | txn->status = 504; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1388 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1389 | s->csb->flags |= CS_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1390 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1391 | |
| 1392 | if (!(s->flags & SF_ERR_MASK)) |
| 1393 | s->flags |= SF_ERR_SRVTO; |
| 1394 | if (!(s->flags & SF_FINST_MASK)) |
| 1395 | s->flags |= SF_FINST_H; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1396 | DBG_TRACE_DEVEL("leaving on error", |
| 1397 | 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] | 1398 | return 0; |
| 1399 | } |
| 1400 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1401 | /* 3: client abort with an abortonclose */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1402 | 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] | 1403 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 1404 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1405 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1406 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1407 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1408 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1409 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1410 | txn->status = 400; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1411 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1412 | |
| 1413 | if (!(s->flags & SF_ERR_MASK)) |
| 1414 | s->flags |= SF_ERR_CLICL; |
| 1415 | if (!(s->flags & SF_FINST_MASK)) |
| 1416 | s->flags |= SF_FINST_H; |
| 1417 | |
| 1418 | /* process_stream() will take care of the error */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1419 | DBG_TRACE_DEVEL("leaving on error", |
| 1420 | 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] | 1421 | return 0; |
| 1422 | } |
| 1423 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1424 | /* 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] | 1425 | else if (rep->flags & CF_SHUTR) { |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1426 | if ((txn->flags & TX_L7_RETRY) && |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1427 | (s->be->retry_type & PR_RE_DISCONNECTED)) { |
Christopher Faulet | 7739799 | 2022-04-04 11:07:08 +0200 | [diff] [blame] | 1428 | if (co_data(rep) || do_l7_retry(s, s->csb) == 0) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1429 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1430 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1431 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1432 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1433 | } |
| 1434 | |
Olivier Houchard | 6db1699 | 2019-05-17 15:40:49 +0200 | [diff] [blame] | 1435 | if (txn->flags & TX_NOT_FIRST) |
| 1436 | goto abort_keep_alive; |
| 1437 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1438 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1439 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1440 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1441 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1442 | } |
| 1443 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1444 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1445 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1446 | s->csb->flags |= CS_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1447 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1448 | |
| 1449 | if (!(s->flags & SF_ERR_MASK)) |
| 1450 | s->flags |= SF_ERR_SRVCL; |
| 1451 | if (!(s->flags & SF_FINST_MASK)) |
| 1452 | s->flags |= SF_FINST_H; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1453 | DBG_TRACE_DEVEL("leaving on error", |
| 1454 | 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] | 1455 | return 0; |
| 1456 | } |
| 1457 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1458 | /* 5: write error to client (we don't send any message then) */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1459 | else if (rep->flags & CF_WRITE_ERROR) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1460 | if (txn->flags & TX_NOT_FIRST) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1461 | goto abort_keep_alive; |
| 1462 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1463 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1464 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1465 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1466 | rep->analysers &= AN_RES_FLT_END; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1467 | |
| 1468 | if (!(s->flags & SF_ERR_MASK)) |
| 1469 | s->flags |= SF_ERR_CLICL; |
| 1470 | if (!(s->flags & SF_FINST_MASK)) |
| 1471 | s->flags |= SF_FINST_H; |
| 1472 | |
| 1473 | /* process_stream() will take care of the error */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1474 | DBG_TRACE_DEVEL("leaving on error", |
| 1475 | 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] | 1476 | return 0; |
| 1477 | } |
| 1478 | |
| 1479 | channel_dont_close(rep); |
| 1480 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1481 | DBG_TRACE_DEVEL("waiting for more data", |
| 1482 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | /* More interesting part now : we know that we have a complete |
| 1487 | * response which at least looks like HTTP. We have an indicator |
| 1488 | * of each header's length, so we can parse them quickly. |
| 1489 | */ |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 1490 | BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1491 | sl = http_get_stline(htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1492 | |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1493 | /* Perform a L7 retry because of the status code */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1494 | if ((txn->flags & TX_L7_RETRY) && |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1495 | l7_status_match(s->be, sl->info.res.status) && |
Christopher Faulet | 7739799 | 2022-04-04 11:07:08 +0200 | [diff] [blame] | 1496 | do_l7_retry(s, s->csb) == 0) { |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1497 | DBG_TRACE_DEVEL("leaving on L7 retry", STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 1498 | return 0; |
| 1499 | } |
| 1500 | |
| 1501 | /* Now, L7 buffer is useless, it can be released */ |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1502 | b_free(&txn->l7_buffer); |
Christopher Faulet | 1f08bff | 2021-05-26 13:14:39 +0200 | [diff] [blame] | 1503 | |
| 1504 | msg->msg_state = HTTP_MSG_BODY; |
| 1505 | |
| 1506 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1507 | /* 0: we might have to print this header in debug mode */ |
| 1508 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1509 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 1510 | int32_t pos; |
| 1511 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1512 | http_debug_stline("srvrep", s, sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1513 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 1514 | 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] | 1515 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 1516 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 1517 | |
| 1518 | if (type == HTX_BLK_EOH) |
| 1519 | break; |
| 1520 | if (type != HTX_BLK_HDR) |
| 1521 | continue; |
| 1522 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1523 | http_debug_hdr("srvhdr", s, |
| 1524 | htx_get_blk_name(htx, blk), |
| 1525 | htx_get_blk_value(htx, blk)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1526 | } |
| 1527 | } |
| 1528 | |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1529 | /* 1: get the status code and the version. Also set HTTP flags */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1530 | txn->status = sl->info.res.status; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1531 | if (sl->flags & HTX_SL_F_VER_11) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1532 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1533 | if (sl->flags & HTX_SL_F_XFER_LEN) { |
| 1534 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 1535 | if (sl->flags & HTX_SL_F_CLEN) |
| 1536 | msg->flags |= HTTP_MSGF_CNT_LEN; |
| 1537 | else if (sl->flags & HTX_SL_F_CHNK) |
| 1538 | msg->flags |= HTTP_MSGF_TE_CHNK; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1539 | } |
Christopher Faulet | 2a40854 | 2020-11-20 14:22:37 +0100 | [diff] [blame] | 1540 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 1541 | msg->flags |= HTTP_MSGF_BODYLESS; |
Christopher Faulet | 576c358 | 2021-01-08 15:53:01 +0100 | [diff] [blame] | 1542 | if (sl->flags & HTX_SL_F_CONN_UPG) |
| 1543 | msg->flags |= HTTP_MSGF_CONN_UPG; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1544 | |
| 1545 | n = txn->status / 100; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1546 | if (n < 1 || n > 5) |
| 1547 | n = 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1548 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1549 | /* when the client triggers a 4xx from the server, it's most often due |
| 1550 | * to a missing object or permission. These events should be tracked |
| 1551 | * because if they happen often, it may indicate a brute force or a |
| 1552 | * vulnerability scan. |
| 1553 | */ |
| 1554 | if (n == 4) |
| 1555 | stream_inc_http_err_ctr(s); |
| 1556 | |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1557 | if (n == 5 && txn->status != 501 && txn->status != 505) |
| 1558 | stream_inc_http_fail_ctr(s); |
| 1559 | |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 1560 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1561 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.rsp[n]); |
| 1562 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.p.http.cum_req); |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 1563 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1564 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1565 | /* Adjust server's health based on status code. Note: status codes 501 |
| 1566 | * and 505 are triggered on demand by client request, so we must not |
| 1567 | * count them as server failures. |
| 1568 | */ |
| 1569 | if (objt_server(s->target)) { |
| 1570 | 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] | 1571 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1572 | else |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1573 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | /* |
| 1577 | * We may be facing a 100-continue response, or any other informational |
| 1578 | * 1xx response which is non-final, in which case this is not the right |
| 1579 | * response, and we're waiting for the next one. Let's allow this response |
| 1580 | * to go to the client and wait for the next one. There's an exception for |
| 1581 | * 101 which is used later in the code to switch protocols. |
| 1582 | */ |
| 1583 | if (txn->status < 200 && |
| 1584 | (txn->status == 100 || txn->status >= 102)) { |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1585 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1586 | htx->first = channel_htx_fwd_headers(rep, htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1587 | msg->msg_state = HTTP_MSG_RPBEFORE; |
Christopher Faulet | 3499f62 | 2019-09-03 15:23:54 +0200 | [diff] [blame] | 1588 | msg->flags = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1589 | txn->status = 0; |
| 1590 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 7d51845 | 2020-08-31 11:07:07 +0200 | [diff] [blame] | 1591 | rep->flags |= CF_SEND_DONTWAIT; /* Send ASAP informational messages */ |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1592 | goto next_one; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1593 | } |
| 1594 | |
Christopher Faulet | 6e6c7b1 | 2021-01-08 16:02:05 +0100 | [diff] [blame] | 1595 | /* A 101-switching-protocols must contains a Connection header with the |
| 1596 | * "upgrade" option and the request too. It means both are agree to |
| 1597 | * upgrade. It is not so strict because there is no test on the Upgrade |
| 1598 | * header content. But it is probably stronger enough for now. |
| 1599 | */ |
| 1600 | if (txn->status == 101 && |
| 1601 | (!(txn->req.flags & HTTP_MSGF_CONN_UPG) || !(txn->rsp.flags & HTTP_MSGF_CONN_UPG))) |
| 1602 | goto return_bad_res; |
| 1603 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1604 | /* |
| 1605 | * 2: check for cacheability. |
| 1606 | */ |
| 1607 | |
| 1608 | switch (txn->status) { |
| 1609 | case 200: |
| 1610 | case 203: |
| 1611 | case 204: |
| 1612 | case 206: |
| 1613 | case 300: |
| 1614 | case 301: |
| 1615 | case 404: |
| 1616 | case 405: |
| 1617 | case 410: |
| 1618 | case 414: |
| 1619 | case 501: |
| 1620 | break; |
| 1621 | default: |
| 1622 | /* RFC7231#6.1: |
| 1623 | * Responses with status codes that are defined as |
| 1624 | * cacheable by default (e.g., 200, 203, 204, 206, |
| 1625 | * 300, 301, 404, 405, 410, 414, and 501 in this |
| 1626 | * specification) can be reused by a cache with |
| 1627 | * heuristic expiration unless otherwise indicated |
| 1628 | * by the method definition or explicit cache |
| 1629 | * controls [RFC7234]; all other status codes are |
| 1630 | * not cacheable by default. |
| 1631 | */ |
| 1632 | txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK); |
| 1633 | break; |
| 1634 | } |
| 1635 | |
| 1636 | /* |
| 1637 | * 3: we may need to capture headers |
| 1638 | */ |
| 1639 | s->logs.logwait &= ~LW_RESP; |
| 1640 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1641 | http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1642 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1643 | /* Skip parsing if no content length is possible. */ |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 1644 | 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] | 1645 | txn->status == 101)) { |
| 1646 | /* Either we've established an explicit tunnel, or we're |
| 1647 | * switching the protocol. In both cases, we're very unlikely |
| 1648 | * to understand the next protocols. We have to switch to tunnel |
| 1649 | * mode, so that we transfer the request and responses then let |
| 1650 | * this protocol pass unmodified. When we later implement specific |
| 1651 | * parsers for such protocols, we'll want to check the Upgrade |
| 1652 | * header which contains information about that protocol for |
| 1653 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 1654 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 1655 | txn->flags |= TX_CON_WANT_TUN; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1656 | } |
| 1657 | |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1658 | /* check for NTML authentication headers in 401 (WWW-Authenticate) and |
| 1659 | * 407 (Proxy-Authenticate) responses and set the connection to private |
| 1660 | */ |
Christopher Faulet | 95a61e8 | 2021-12-22 14:22:03 +0100 | [diff] [blame] | 1661 | srv_conn = cs_conn(s->csb); |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1662 | if (srv_conn) { |
| 1663 | struct ist hdr; |
| 1664 | struct http_hdr_ctx ctx; |
| 1665 | |
| 1666 | if (txn->status == 401) |
| 1667 | hdr = ist("WWW-Authenticate"); |
| 1668 | else if (txn->status == 407) |
| 1669 | hdr = ist("Proxy-Authenticate"); |
| 1670 | else |
| 1671 | goto end; |
| 1672 | |
| 1673 | ctx.blk = NULL; |
| 1674 | while (http_find_header(htx, hdr, &ctx, 0)) { |
Willy Tarreau | f1dcced | 2020-05-07 19:27:02 +0200 | [diff] [blame] | 1675 | /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM", |
| 1676 | * possibly followed by blanks and a base64 string, the connection |
| 1677 | * is private. Since it's a mess to deal with, we only check for |
| 1678 | * values starting with "NTLM" or "Nego". Note that often multiple |
| 1679 | * headers are sent by the server there. |
| 1680 | */ |
| 1681 | if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) || |
Willy Tarreau | 49a1d28 | 2020-05-07 19:10:15 +0200 | [diff] [blame] | 1682 | (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) { |
Olivier Houchard | 250031e | 2019-05-29 15:01:50 +0200 | [diff] [blame] | 1683 | sess->flags |= SESS_FL_PREFER_LAST; |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 1684 | conn_set_owner(srv_conn, sess, NULL); |
Christopher Faulet | 21ddc74 | 2020-07-01 15:26:14 +0200 | [diff] [blame] | 1685 | conn_set_private(srv_conn); |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 1686 | /* 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] | 1687 | session_add_conn(srv_conn->owner, srv_conn, srv_conn->target); |
Willy Tarreau | f1dcced | 2020-05-07 19:27:02 +0200 | [diff] [blame] | 1688 | break; |
Olivier Houchard | 250031e | 2019-05-29 15:01:50 +0200 | [diff] [blame] | 1689 | } |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | end: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1694 | /* we want to have the response time before we start processing it */ |
| 1695 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 1696 | |
| 1697 | /* end of job, return OK */ |
| 1698 | rep->analysers &= ~an_bit; |
| 1699 | rep->analyse_exp = TICK_ETERNITY; |
| 1700 | channel_auto_close(rep); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1701 | 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] | 1702 | return 1; |
| 1703 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1704 | return_int_err: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1705 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 1706 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 1707 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1708 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1709 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1710 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1711 | txn->status = 500; |
| 1712 | if (!(s->flags & SF_ERR_MASK)) |
| 1713 | s->flags |= SF_ERR_INTERNAL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1714 | goto return_prx_cond; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1715 | |
| 1716 | return_bad_res: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1717 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1718 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 1719 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1720 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1721 | } |
Olivier Houchard | e3249a9 | 2019-05-03 23:01:47 +0200 | [diff] [blame] | 1722 | if ((s->be->retry_type & PR_RE_JUNK_REQUEST) && |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 1723 | (txn->flags & TX_L7_RETRY) && |
Christopher Faulet | 7739799 | 2022-04-04 11:07:08 +0200 | [diff] [blame] | 1724 | do_l7_retry(s, s->csb) == 0) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1725 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1726 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | e3249a9 | 2019-05-03 23:01:47 +0200 | [diff] [blame] | 1727 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1728 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1729 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 1730 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1731 | /* fall through */ |
| 1732 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1733 | return_prx_cond: |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1734 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1735 | |
| 1736 | if (!(s->flags & SF_ERR_MASK)) |
| 1737 | s->flags |= SF_ERR_PRXCOND; |
| 1738 | if (!(s->flags & SF_FINST_MASK)) |
| 1739 | s->flags |= SF_FINST_H; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1740 | |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 1741 | s->csb->flags |= CS_FL_NOLINGER; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1742 | DBG_TRACE_DEVEL("leaving on error", |
| 1743 | 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] | 1744 | return 0; |
| 1745 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1746 | abort_keep_alive: |
| 1747 | /* A keep-alive request to the server failed on a network error. |
| 1748 | * The client is required to retry. We need to close without returning |
| 1749 | * any other information so that the client retries. |
| 1750 | */ |
| 1751 | txn->status = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1752 | s->logs.logwait = 0; |
| 1753 | s->logs.level = 0; |
| 1754 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1755 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1756 | DBG_TRACE_DEVEL("leaving by closing K/A connection", |
| 1757 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1758 | return 0; |
| 1759 | } |
| 1760 | |
| 1761 | /* This function performs all the processing enabled for the current response. |
| 1762 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 1763 | * and updates s->res.analysers. It might make sense to explode it into several |
| 1764 | * other functions. It works like process_request (see indications above). |
| 1765 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1766 | 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] | 1767 | { |
| 1768 | struct session *sess = s->sess; |
| 1769 | struct http_txn *txn = s->txn; |
| 1770 | struct http_msg *msg = &txn->rsp; |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1771 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1772 | struct proxy *cur_proxy; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1773 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 1774 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1775 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 1776 | return 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1777 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1778 | 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] | 1779 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1780 | htx = htxbuf(&rep->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1781 | |
| 1782 | /* The stats applet needs to adjust the Connection header but we don't |
| 1783 | * apply any filter there. |
| 1784 | */ |
| 1785 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 1786 | rep->analysers &= ~an_bit; |
| 1787 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1788 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | /* |
| 1792 | * We will have to evaluate the filters. |
| 1793 | * As opposed to version 1.2, now they will be evaluated in the |
| 1794 | * filters order and not in the header order. This means that |
| 1795 | * each filter has to be validated among all headers. |
| 1796 | * |
| 1797 | * Filters are tried with ->be first, then with ->fe if it is |
| 1798 | * different from ->be. |
| 1799 | * |
| 1800 | * Maybe we are in resume condiion. In this case I choose the |
| 1801 | * "struct proxy" which contains the rule list matching the resume |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1802 | * pointer. If none of these "struct proxy" match, I initialise |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1803 | * the process with the first one. |
| 1804 | * |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1805 | * In fact, I check only correspondence between the current list |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1806 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 1807 | * the loop with the ->be. |
| 1808 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1809 | if (s->current_rule_list == &sess->fe->http_res_rules || |
| 1810 | (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] | 1811 | cur_proxy = sess->fe; |
| 1812 | else |
| 1813 | cur_proxy = s->be; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1814 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1815 | while (1) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1816 | /* evaluate http-response rules */ |
Christopher Faulet | 46f46df | 2021-11-09 16:33:25 +0100 | [diff] [blame] | 1817 | if (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 1818 | struct list *def_rules, *rules; |
| 1819 | |
| 1820 | def_rules = ((cur_proxy->defpx && (cur_proxy == s->be || cur_proxy->defpx != s->be->defpx)) ? &cur_proxy->defpx->http_res_rules : NULL); |
| 1821 | rules = &cur_proxy->http_res_rules; |
| 1822 | |
| 1823 | ret = http_res_get_intercept_rule(cur_proxy, def_rules, rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1824 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1825 | switch (ret) { |
| 1826 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 1827 | goto return_prx_yield; |
| 1828 | |
| 1829 | case HTTP_RULE_RES_CONT: |
| 1830 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 1831 | break; |
| 1832 | |
| 1833 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 1834 | goto deny; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1835 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1836 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent */ |
| 1837 | goto return_prx_cond; |
| 1838 | |
| 1839 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1840 | goto done; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1841 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1842 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 1843 | goto return_bad_res; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1844 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1845 | case HTTP_RULE_RES_ERROR: /* failed with a bad request */ |
| 1846 | goto return_int_err; |
| 1847 | } |
| 1848 | |
| 1849 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1850 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1851 | /* check whether we're already working on the frontend */ |
| 1852 | if (cur_proxy == sess->fe) |
| 1853 | break; |
| 1854 | cur_proxy = sess->fe; |
| 1855 | } |
| 1856 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1857 | /* OK that's all we can do for 1xx responses */ |
| 1858 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1859 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1860 | |
| 1861 | /* |
| 1862 | * Now check for a server cookie. |
| 1863 | */ |
| 1864 | 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] | 1865 | http_manage_server_side_cookies(s, rep); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1866 | |
| 1867 | /* |
| 1868 | * Check for cache-control or pragma headers if required. |
| 1869 | */ |
| 1870 | 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] | 1871 | http_check_response_for_cacheability(s, rep); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1872 | |
| 1873 | /* |
| 1874 | * Add server cookie in the response if needed |
| 1875 | */ |
| 1876 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 1877 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
| 1878 | (!(s->flags & SF_DIRECT) || |
| 1879 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 1880 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 1881 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 1882 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 1883 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 1884 | !(s->flags & SF_IGNORE_PRST)) { |
| 1885 | /* the server is known, it's not the one the client requested, or the |
| 1886 | * cookie's last seen date needs to be refreshed. We have to |
| 1887 | * insert a set-cookie here, except if we want to insert only on POST |
| 1888 | * requests and this one isn't. Note that servers which don't have cookies |
| 1889 | * (eg: some backup servers) will return a full cookie removal request. |
| 1890 | */ |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1891 | if (!__objt_server(s->target)->cookie) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1892 | chunk_printf(&trash, |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1893 | "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1894 | s->be->cookie_name); |
| 1895 | } |
| 1896 | else { |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1897 | 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] | 1898 | |
| 1899 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 1900 | /* emit last_date, which is mandatory */ |
| 1901 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 1902 | s30tob64((date.tv_sec+3) >> 2, |
| 1903 | trash.area + trash.data); |
| 1904 | trash.data += 5; |
| 1905 | |
| 1906 | if (s->be->cookie_maxlife) { |
| 1907 | /* emit first_date, which is either the original one or |
| 1908 | * the current date. |
| 1909 | */ |
| 1910 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 1911 | s30tob64(txn->cookie_first_date ? |
| 1912 | txn->cookie_first_date >> 2 : |
| 1913 | (date.tv_sec+3) >> 2, |
| 1914 | trash.area + trash.data); |
| 1915 | trash.data += 5; |
| 1916 | } |
| 1917 | } |
| 1918 | chunk_appendf(&trash, "; path=/"); |
| 1919 | } |
| 1920 | |
| 1921 | if (s->be->cookie_domain) |
| 1922 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
| 1923 | |
| 1924 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 1925 | chunk_appendf(&trash, "; HttpOnly"); |
| 1926 | |
| 1927 | if (s->be->ck_opts & PR_CK_SECURE) |
| 1928 | chunk_appendf(&trash, "; Secure"); |
| 1929 | |
Christopher Faulet | 2f53390 | 2020-01-21 11:06:48 +0100 | [diff] [blame] | 1930 | if (s->be->cookie_attrs) |
| 1931 | chunk_appendf(&trash, "; %s", s->be->cookie_attrs); |
| 1932 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1933 | if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data)))) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1934 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1935 | |
| 1936 | txn->flags &= ~TX_SCK_MASK; |
| 1937 | if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
| 1938 | /* the server did not change, only the date was updated */ |
| 1939 | txn->flags |= TX_SCK_UPDATED; |
| 1940 | else |
| 1941 | txn->flags |= TX_SCK_INSERTED; |
| 1942 | |
| 1943 | /* Here, we will tell an eventual cache on the client side that we don't |
| 1944 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 1945 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 1946 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 1947 | */ |
| 1948 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
| 1949 | |
| 1950 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 1951 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1952 | if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private")))) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1953 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | /* |
| 1958 | * Check if result will be cacheable with a cookie. |
| 1959 | * We'll block the response if security checks have caught |
| 1960 | * nasty things such as a cacheable cookie. |
| 1961 | */ |
| 1962 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 1963 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 1964 | (s->be->options & PR_O_CHK_CACHE)) { |
| 1965 | /* we're in presence of a cacheable response containing |
| 1966 | * a set-cookie header. We'll block it as requested by |
| 1967 | * the 'checkcache' option, and send an alert. |
| 1968 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1969 | 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] | 1970 | 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] | 1971 | send_log(s->be, LOG_ALERT, |
| 1972 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 1973 | 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] | 1974 | goto deny; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1975 | } |
| 1976 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1977 | end: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 1978 | /* |
| 1979 | * Evaluate after-response rules before forwarding the response. rules |
| 1980 | * from the backend are evaluated first, then one from the frontend if |
| 1981 | * it differs. |
| 1982 | */ |
| 1983 | if (!http_eval_after_res_rules(s)) |
| 1984 | goto return_int_err; |
| 1985 | |
Christopher Faulet | c2ac5e4 | 2021-03-08 18:20:09 +0100 | [diff] [blame] | 1986 | /* Filter the response headers if there are filters attached to the |
| 1987 | * stream. |
| 1988 | */ |
| 1989 | if (HAS_FILTERS(s)) |
| 1990 | rep->analysers |= AN_RES_FLT_HTTP_HDRS; |
| 1991 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1992 | /* Always enter in the body analyzer */ |
| 1993 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 1994 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
| 1995 | |
| 1996 | /* if the user wants to log as soon as possible, without counting |
| 1997 | * bytes from the server, then this is the right moment. We have |
| 1998 | * to temporarily assign bytes_out to log what we currently have. |
| 1999 | */ |
| 2000 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
| 2001 | 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] | 2002 | s->logs.bytes_out = htx->data; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2003 | s->do_log(s); |
| 2004 | s->logs.bytes_out = 0; |
| 2005 | } |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2006 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2007 | done: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2008 | 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] | 2009 | rep->analysers &= ~an_bit; |
| 2010 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2011 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2012 | return 1; |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2013 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2014 | deny: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2015 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_resp); |
| 2016 | _HA_ATOMIC_INC(&s->be->be_counters.denied_resp); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2017 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2018 | _HA_ATOMIC_INC(&sess->listener->counters->denied_resp); |
Christopher Faulet | a08546b | 2019-12-16 16:07:34 +0100 | [diff] [blame] | 2019 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2020 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2021 | goto return_prx_err; |
| 2022 | |
| 2023 | return_int_err: |
| 2024 | txn->status = 500; |
| 2025 | if (!(s->flags & SF_ERR_MASK)) |
| 2026 | s->flags |= SF_ERR_INTERNAL; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2027 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 2028 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
Dragan Dosen | 9a006f9 | 2021-09-21 13:02:09 +0200 | [diff] [blame] | 2029 | if (sess->listener && sess->listener->counters) |
| 2030 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 2031 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2032 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2033 | goto return_prx_err; |
| 2034 | |
| 2035 | return_bad_res: |
| 2036 | txn->status = 502; |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2037 | stream_inc_http_fail_ctr(s); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2038 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | a20a653 | 2020-02-05 10:16:41 +0100 | [diff] [blame] | 2039 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2040 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | a20a653 | 2020-02-05 10:16:41 +0100 | [diff] [blame] | 2041 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 2042 | } |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2043 | /* fall through */ |
| 2044 | |
| 2045 | return_prx_err: |
| 2046 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 2047 | /* fall through */ |
| 2048 | |
| 2049 | return_prx_cond: |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2050 | s->logs.t_data = -1; /* was not a valid response */ |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 2051 | s->csb->flags |= CS_FL_NOLINGER; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2052 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2053 | if (!(s->flags & SF_ERR_MASK)) |
| 2054 | s->flags |= SF_ERR_PRXCOND; |
| 2055 | if (!(s->flags & SF_FINST_MASK)) |
| 2056 | s->flags |= SF_FINST_H; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2057 | |
Christopher Faulet | e58c000 | 2020-03-02 16:21:01 +0100 | [diff] [blame] | 2058 | rep->analysers &= AN_RES_FLT_END; |
| 2059 | s->req.analysers &= AN_REQ_FLT_END; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2060 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2061 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2062 | DBG_TRACE_DEVEL("leaving on error", |
| 2063 | 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] | 2064 | return 0; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2065 | |
| 2066 | return_prx_yield: |
| 2067 | channel_dont_close(rep); |
| 2068 | DBG_TRACE_DEVEL("waiting for more data", |
| 2069 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 2070 | return 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | /* This function is an analyser which forwards response body (including chunk |
| 2074 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 2075 | * zero byte. The only situation where it must not be called is when we're in |
| 2076 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 2077 | * remaining data and to resync after end of body. It expects the msg_state to |
| 2078 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 2079 | * read more data, or 1 once we can go on with next request or end the stream. |
| 2080 | * |
| 2081 | * It is capable of compressing response data both in content-length mode and |
| 2082 | * in chunked mode. The state machines follows different flows depending on |
| 2083 | * whether content-length and chunked modes are used, since there are no |
| 2084 | * trailers in content-length : |
| 2085 | * |
| 2086 | * chk-mode cl-mode |
| 2087 | * ,----- BODY -----. |
| 2088 | * / \ |
| 2089 | * V size > 0 V chk-mode |
| 2090 | * .--> SIZE -------------> DATA -------------> CRLF |
| 2091 | * | | size == 0 | last byte | |
| 2092 | * | v final crlf v inspected | |
| 2093 | * | TRAILERS -----------> DONE | |
| 2094 | * | | |
| 2095 | * `----------------------------------------------' |
| 2096 | * |
| 2097 | * Compression only happens in the DATA state, and must be flushed in final |
| 2098 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 2099 | * is performed at once on final states for all bytes parsed, or when leaving |
| 2100 | * on missing data. |
| 2101 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2102 | 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] | 2103 | { |
| 2104 | struct session *sess = s->sess; |
| 2105 | struct http_txn *txn = s->txn; |
| 2106 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2107 | struct htx *htx; |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2108 | int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2109 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2110 | 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] | 2111 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2112 | htx = htxbuf(&res->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2113 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2114 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 2115 | goto return_bad_res; |
| 2116 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 2117 | goto return_int_err; |
| 2118 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2119 | if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2120 | ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2121 | /* Output closed while we were sending data. We must abort and |
| 2122 | * wake the other side up. |
| 2123 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2124 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2125 | http_end_response(s); |
| 2126 | http_end_request(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2127 | DBG_TRACE_DEVEL("leaving on error", |
| 2128 | 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] | 2129 | return 1; |
| 2130 | } |
| 2131 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2132 | if (msg->msg_state == HTTP_MSG_BODY) |
| 2133 | msg->msg_state = HTTP_MSG_DATA; |
| 2134 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2135 | /* in most states, we should abort in case of early close */ |
| 2136 | channel_auto_close(res); |
| 2137 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2138 | if (res->to_forward) { |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2139 | if (res->to_forward == CHN_INFINITE_FORWARD) { |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2140 | if (res->flags & CF_EOI) |
| 2141 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2142 | } |
| 2143 | else { |
| 2144 | /* We can't process the buffer's contents yet */ |
| 2145 | res->flags |= CF_WAKE_WRITE; |
| 2146 | goto missing_data_or_waiting; |
| 2147 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2148 | } |
| 2149 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2150 | if (msg->msg_state >= HTTP_MSG_ENDING) |
| 2151 | goto ending; |
| 2152 | |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 2153 | 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] | 2154 | (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) { |
| 2155 | msg->msg_state = HTTP_MSG_ENDING; |
| 2156 | goto ending; |
| 2157 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2158 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2159 | /* Forward input data. We get it by removing all outgoing data not |
| 2160 | * forwarded yet from HTX data size. If there are some data filters, we |
| 2161 | * let them decide the amount of data to forward. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2162 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2163 | if (HAS_RSP_DATA_FILTERS(s)) { |
| 2164 | ret = flt_http_payload(s, msg, htx->data); |
| 2165 | if (ret < 0) |
| 2166 | goto return_bad_res; |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 2167 | c_adv(res, ret); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2168 | } |
| 2169 | else { |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 2170 | c_adv(res, htx->data - co_data(res)); |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2171 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
| 2172 | channel_htx_forward_forever(res, htx); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2173 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2174 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2175 | if (htx->data != co_data(res)) |
| 2176 | goto missing_data_or_waiting; |
| 2177 | |
| 2178 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) { |
| 2179 | msg->msg_state = HTTP_MSG_ENDING; |
| 2180 | goto ending; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2181 | } |
| 2182 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2183 | /* 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] | 2184 | * in HTTP_MSG_ENDING state. Then if all data was marked to be |
| 2185 | * forwarded, set the state to HTTP_MSG_DONE. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2186 | */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2187 | if (!(htx->flags & HTX_FL_EOM)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2188 | goto missing_data_or_waiting; |
| 2189 | |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 2190 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2191 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2192 | ending: |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 2193 | res->flags &= ~CF_EXPECT_MORE; /* no more data are expected */ |
| 2194 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2195 | /* other states, ENDING...TUNNEL */ |
| 2196 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 2197 | goto done; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2198 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2199 | if (HAS_RSP_DATA_FILTERS(s)) { |
| 2200 | ret = flt_http_end(s, msg); |
| 2201 | if (ret <= 0) { |
| 2202 | if (!ret) |
| 2203 | goto missing_data_or_waiting; |
| 2204 | goto return_bad_res; |
| 2205 | } |
| 2206 | } |
| 2207 | |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 2208 | 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] | 2209 | !(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 2210 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 2211 | goto ending; |
| 2212 | } |
| 2213 | else { |
| 2214 | msg->msg_state = HTTP_MSG_DONE; |
| 2215 | res->to_forward = 0; |
| 2216 | } |
| 2217 | |
| 2218 | done: |
| 2219 | |
| 2220 | channel_dont_close(res); |
| 2221 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2222 | http_end_response(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2223 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2224 | http_end_request(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2225 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2226 | if (res->flags & CF_SHUTW) { |
| 2227 | /* response errors are most likely due to the |
| 2228 | * client aborting the transfer. */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2229 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2230 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2231 | goto return_bad_res; |
| 2232 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2233 | 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] | 2234 | return 1; |
| 2235 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2236 | DBG_TRACE_DEVEL("waiting for the end of the HTTP txn", |
| 2237 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2238 | return 0; |
| 2239 | |
| 2240 | missing_data_or_waiting: |
| 2241 | if (res->flags & CF_SHUTW) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2242 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2243 | |
| 2244 | /* stop waiting for data if the input is closed before the end. If the |
| 2245 | * client side was already closed, it means that the client has aborted, |
| 2246 | * so we don't want to count this as a server abort. Otherwise it's a |
| 2247 | * server abort. |
| 2248 | */ |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 2249 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2250 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2251 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2252 | /* If we have some pending data, we continue the processing */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2253 | if (htx_is_empty(htx)) |
| 2254 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2255 | } |
| 2256 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2257 | /* When TE: chunked is used, we need to get there again to parse |
| 2258 | * 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] | 2259 | * set CF_DONTCLOSE. Similarly when there is a content-leng or if there |
| 2260 | * are filters registered on the stream, we don't want to forward a |
| 2261 | * close |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2262 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2263 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2264 | channel_dont_close(res); |
| 2265 | |
| 2266 | /* We know that more data are expected, but we couldn't send more that |
| 2267 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
| 2268 | * system knows it must not set a PUSH on this first part. Interactive |
| 2269 | * modes are already handled by the stream sock layer. We must not do |
| 2270 | * this in content-length mode because it could present the MSG_MORE |
| 2271 | * flag with the last block of forwarded data, which would cause an |
| 2272 | * additional delay to be observed by the receiver. |
| 2273 | */ |
Christopher Faulet | 2151cdd | 2020-07-22 16:34:59 +0200 | [diff] [blame] | 2274 | if (HAS_RSP_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2275 | res->flags |= CF_EXPECT_MORE; |
| 2276 | |
| 2277 | /* the stream handler will take care of timeouts and errors */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2278 | DBG_TRACE_DEVEL("waiting for more data to forward", |
| 2279 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2280 | return 0; |
| 2281 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2282 | return_srv_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2283 | _HA_ATOMIC_INC(&sess->fe->fe_counters.srv_aborts); |
| 2284 | _HA_ATOMIC_INC(&s->be->be_counters.srv_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2285 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2286 | _HA_ATOMIC_INC(&sess->listener->counters->srv_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2287 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2288 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.srv_aborts); |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2289 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2290 | if (!(s->flags & SF_ERR_MASK)) |
| 2291 | s->flags |= SF_ERR_SRVCL; |
| 2292 | goto return_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2293 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2294 | return_cli_abort: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2295 | _HA_ATOMIC_INC(&sess->fe->fe_counters.cli_aborts); |
| 2296 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2297 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2298 | _HA_ATOMIC_INC(&sess->listener->counters->cli_aborts); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2299 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2300 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2301 | if (!(s->flags & SF_ERR_MASK)) |
| 2302 | s->flags |= SF_ERR_CLICL; |
| 2303 | goto return_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2304 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2305 | return_int_err: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2306 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
| 2307 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2308 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2309 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2310 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2311 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2312 | if (!(s->flags & SF_ERR_MASK)) |
| 2313 | s->flags |= SF_ERR_INTERNAL; |
| 2314 | goto return_error; |
| 2315 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2316 | return_bad_res: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2317 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2318 | if (objt_server(s->target)) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2319 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2320 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 2321 | } |
Willy Tarreau | 826f3ab | 2021-02-10 12:07:15 +0100 | [diff] [blame] | 2322 | stream_inc_http_fail_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2323 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2324 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2325 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2326 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2327 | return_error: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2328 | /* don't send any error message as we're in the body */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2329 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2330 | if (!(s->flags & SF_FINST_MASK)) |
| 2331 | s->flags |= SF_FINST_D; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2332 | DBG_TRACE_DEVEL("leaving on error", |
| 2333 | 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] | 2334 | return 0; |
| 2335 | } |
| 2336 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2337 | /* Perform an HTTP redirect based on the information in <rule>. The function |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2338 | * returns zero in case of an irrecoverable error such as too large a request |
| 2339 | * to build a valid response, 1 in case of successful redirect (hence the rule |
| 2340 | * is final), or 2 if the rule has to be silently skipped. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2341 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2342 | 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] | 2343 | { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2344 | struct channel *req = &s->req; |
| 2345 | struct channel *res = &s->res; |
| 2346 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2347 | struct htx_sl *sl; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2348 | struct buffer *chunk; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2349 | struct ist status, reason, location; |
| 2350 | unsigned int flags; |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2351 | int close = 0; /* Try to keep the connection alive byt default */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2352 | |
| 2353 | chunk = alloc_trash_chunk(); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2354 | if (!chunk) { |
| 2355 | if (!(s->flags & SF_ERR_MASK)) |
| 2356 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2357 | goto fail; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2358 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2359 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2360 | /* |
| 2361 | * Create the location |
| 2362 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2363 | htx = htxbuf(&req->buf); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2364 | switch(rule->type) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2365 | case REDIRECT_TYPE_SCHEME: { |
| 2366 | struct http_hdr_ctx ctx; |
| 2367 | struct ist path, host; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2368 | struct http_uri_parser parser; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2369 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2370 | host = ist(""); |
| 2371 | ctx.blk = NULL; |
| 2372 | if (http_find_header(htx, ist("Host"), &ctx, 0)) |
| 2373 | host = ctx.value; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2374 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2375 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2376 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 2377 | path = http_parse_path(&parser); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2378 | /* build message using path */ |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 2379 | if (isttest(path)) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2380 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2381 | int qs = 0; |
| 2382 | while (qs < path.len) { |
| 2383 | if (*(path.ptr + qs) == '?') { |
| 2384 | path.len = qs; |
| 2385 | break; |
| 2386 | } |
| 2387 | qs++; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2388 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2389 | } |
| 2390 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2391 | else |
| 2392 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2393 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2394 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2395 | /* add scheme */ |
| 2396 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2397 | goto fail; |
| 2398 | } |
| 2399 | else { |
| 2400 | /* add scheme with executing log format */ |
| 2401 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2402 | chunk->size - chunk->data, |
| 2403 | &rule->rdr_fmt); |
| 2404 | } |
| 2405 | /* add "://" + host + path */ |
| 2406 | if (!chunk_memcat(chunk, "://", 3) || |
| 2407 | !chunk_memcat(chunk, host.ptr, host.len) || |
| 2408 | !chunk_memcat(chunk, path.ptr, path.len)) |
| 2409 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2410 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2411 | /* append a slash at the end of the location if needed and missing */ |
| 2412 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2413 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2414 | if (chunk->data + 1 >= chunk->size) |
| 2415 | goto fail; |
| 2416 | chunk->area[chunk->data++] = '/'; |
| 2417 | } |
| 2418 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2419 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2420 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2421 | case REDIRECT_TYPE_PREFIX: { |
| 2422 | struct ist path; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2423 | struct http_uri_parser parser; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2424 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2425 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 2426 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 2427 | path = http_parse_path(&parser); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2428 | /* build message using path */ |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 2429 | if (isttest(path)) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2430 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2431 | int qs = 0; |
| 2432 | while (qs < path.len) { |
| 2433 | if (*(path.ptr + qs) == '?') { |
| 2434 | path.len = qs; |
| 2435 | break; |
| 2436 | } |
| 2437 | qs++; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2438 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2439 | } |
| 2440 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2441 | else |
| 2442 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2443 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2444 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2445 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2446 | * add anything, otherwise it makes it hard for the user to |
| 2447 | * configure a self-redirection. |
| 2448 | */ |
| 2449 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 2450 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2451 | goto fail; |
| 2452 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2453 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2454 | else { |
| 2455 | /* add prefix with executing log format */ |
| 2456 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2457 | chunk->size - chunk->data, |
| 2458 | &rule->rdr_fmt); |
| 2459 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2460 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2461 | /* add path */ |
| 2462 | if (!chunk_memcat(chunk, path.ptr, path.len)) |
| 2463 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2464 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2465 | /* append a slash at the end of the location if needed and missing */ |
| 2466 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2467 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2468 | if (chunk->data + 1 >= chunk->size) |
| 2469 | goto fail; |
| 2470 | chunk->area[chunk->data++] = '/'; |
| 2471 | } |
| 2472 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2473 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2474 | case REDIRECT_TYPE_LOCATION: |
| 2475 | default: |
| 2476 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2477 | /* add location */ |
| 2478 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2479 | goto fail; |
| 2480 | } |
| 2481 | else { |
| 2482 | /* add location with executing log format */ |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2483 | int len = build_logline(s, chunk->area + chunk->data, |
| 2484 | chunk->size - chunk->data, |
| 2485 | &rule->rdr_fmt); |
| 2486 | if (!len && rule->flags & REDIRECT_FLAG_IGNORE_EMPTY) |
| 2487 | return 2; |
| 2488 | |
| 2489 | chunk->data += len; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2490 | } |
| 2491 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2492 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2493 | location = ist2(chunk->area, chunk->data); |
| 2494 | |
| 2495 | /* |
| 2496 | * Create the 30x response |
| 2497 | */ |
| 2498 | switch (rule->code) { |
| 2499 | case 308: |
| 2500 | status = ist("308"); |
| 2501 | reason = ist("Permanent Redirect"); |
| 2502 | break; |
| 2503 | case 307: |
| 2504 | status = ist("307"); |
| 2505 | reason = ist("Temporary Redirect"); |
| 2506 | break; |
| 2507 | case 303: |
| 2508 | status = ist("303"); |
| 2509 | reason = ist("See Other"); |
| 2510 | break; |
| 2511 | case 301: |
| 2512 | status = ist("301"); |
| 2513 | reason = ist("Moved Permanently"); |
| 2514 | break; |
| 2515 | case 302: |
| 2516 | default: |
| 2517 | status = ist("302"); |
| 2518 | reason = ist("Found"); |
| 2519 | break; |
| 2520 | } |
| 2521 | |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2522 | if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE) |
| 2523 | close = 1; |
| 2524 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2525 | htx = htx_from_buf(&res->buf); |
Kevin Zhu | 96b3639 | 2020-01-07 09:42:55 +0100 | [diff] [blame] | 2526 | /* Trim any possible response */ |
| 2527 | channel_htx_truncate(&s->res, htx); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2528 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 2529 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason); |
| 2530 | if (!sl) |
| 2531 | goto fail; |
| 2532 | sl->info.res.status = rule->code; |
| 2533 | s->txn->status = rule->code; |
| 2534 | |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2535 | if (close && !htx_add_header(htx, ist("Connection"), ist("close"))) |
| 2536 | goto fail; |
| 2537 | |
| 2538 | if (!htx_add_header(htx, ist("Content-length"), ist("0")) || |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2539 | !htx_add_header(htx, ist("Location"), location)) |
| 2540 | goto fail; |
| 2541 | |
| 2542 | if (rule->code == 302 || rule->code == 303 || rule->code == 307) { |
| 2543 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache"))) |
| 2544 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2545 | } |
| 2546 | |
| 2547 | if (rule->cookie_len) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2548 | if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len))) |
| 2549 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2550 | } |
| 2551 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2552 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2553 | goto fail; |
| 2554 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2555 | htx->flags |= HTX_FL_EOM; |
Kevin Zhu | 96b3639 | 2020-01-07 09:42:55 +0100 | [diff] [blame] | 2556 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 2557 | if (!http_forward_proxy_resp(s, 1)) |
| 2558 | goto fail; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2559 | |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2560 | if (rule->flags & REDIRECT_FLAG_FROM_REQ) { |
| 2561 | /* let's log the request time */ |
| 2562 | s->logs.tv_request = now; |
Christopher Faulet | d347588 | 2021-10-04 14:16:46 +0200 | [diff] [blame] | 2563 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2564 | |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2565 | 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] | 2566 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2567 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2568 | |
| 2569 | if (!(s->flags & SF_ERR_MASK)) |
| 2570 | s->flags |= SF_ERR_LOCAL; |
| 2571 | if (!(s->flags & SF_FINST_MASK)) |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2572 | s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2573 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2574 | free_trash_chunk(chunk); |
| 2575 | return 1; |
| 2576 | |
| 2577 | fail: |
| 2578 | /* If an error occurred, remove the incomplete HTTP response from the |
| 2579 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 2580 | channel_htx_truncate(res, htxbuf(&res->buf)); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2581 | free_trash_chunk(chunk); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2582 | return 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2583 | } |
| 2584 | |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2585 | /* Replace all headers matching the name <name>. The header value is replaced if |
| 2586 | * it matches the regex <re>. <str> is used for the replacement. If <full> is |
| 2587 | * set to 1, the full-line is matched and replaced. Otherwise, comma-separated |
| 2588 | * values are evaluated one by one. It returns 0 on success and -1 on error. |
| 2589 | */ |
| 2590 | int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name, |
| 2591 | const char *str, struct my_regex *re, int full) |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2592 | { |
| 2593 | struct http_hdr_ctx ctx; |
| 2594 | struct buffer *output = get_trash_chunk(); |
| 2595 | |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2596 | ctx.blk = NULL; |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2597 | while (http_find_header(htx, name, &ctx, full)) { |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2598 | if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0)) |
| 2599 | continue; |
| 2600 | |
| 2601 | output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch); |
| 2602 | if (output->data == -1) |
| 2603 | return -1; |
| 2604 | if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data))) |
| 2605 | return -1; |
| 2606 | } |
| 2607 | return 0; |
| 2608 | } |
| 2609 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2610 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 2611 | * takes the string from the variable 'replace' with length 'len', then modifies |
| 2612 | * the relevant part of the request line accordingly. Then it updates various |
| 2613 | * pointers to the next elements which were moved, and the total buffer length. |
| 2614 | * It finds the action to be performed in p[2], previously filled by function |
| 2615 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 2616 | * error, though this can be revisited when this code is finally exploited. |
| 2617 | * |
| 2618 | * '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] | 2619 | * 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] | 2620 | * |
| 2621 | * In query string case, the mark question '?' must be set at the start of the |
| 2622 | * string by the caller, event if the replacement query string is empty. |
| 2623 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2624 | int http_req_replace_stline(int action, const char *replace, int len, |
| 2625 | struct proxy *px, struct stream *s) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2626 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2627 | struct htx *htx = htxbuf(&s->req.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2628 | |
| 2629 | switch (action) { |
| 2630 | case 0: // method |
| 2631 | if (!http_replace_req_meth(htx, ist2(replace, len))) |
| 2632 | return -1; |
| 2633 | break; |
| 2634 | |
| 2635 | case 1: // path |
Christopher Faulet | b8ce505 | 2020-08-31 16:11:57 +0200 | [diff] [blame] | 2636 | if (!http_replace_req_path(htx, ist2(replace, len), 0)) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2637 | return -1; |
| 2638 | break; |
| 2639 | |
| 2640 | case 2: // query |
| 2641 | if (!http_replace_req_query(htx, ist2(replace, len))) |
| 2642 | return -1; |
| 2643 | break; |
| 2644 | |
| 2645 | case 3: // uri |
| 2646 | if (!http_replace_req_uri(htx, ist2(replace, len))) |
| 2647 | return -1; |
| 2648 | break; |
| 2649 | |
Christopher Faulet | 312294f | 2020-09-02 17:17:44 +0200 | [diff] [blame] | 2650 | case 4: // path + query |
| 2651 | if (!http_replace_req_path(htx, ist2(replace, len), 1)) |
| 2652 | return -1; |
| 2653 | break; |
| 2654 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2655 | default: |
| 2656 | return -1; |
| 2657 | } |
| 2658 | return 0; |
| 2659 | } |
| 2660 | |
| 2661 | /* This function replace the HTTP status code and the associated message. The |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2662 | * variable <status> contains the new status code. This function never fails. It |
| 2663 | * returns 0 in case of success, -1 in case of internal error. |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2664 | */ |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2665 | 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] | 2666 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2667 | struct htx *htx = htxbuf(&s->res.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2668 | char *res; |
| 2669 | |
| 2670 | chunk_reset(&trash); |
| 2671 | res = ultoa_o(status, trash.area, trash.size); |
| 2672 | trash.data = res - trash.area; |
| 2673 | |
| 2674 | /* Do we have a custom reason format string? */ |
Tim Duesterhus | e296d3e | 2020-03-05 17:56:31 +0100 | [diff] [blame] | 2675 | if (!isttest(reason)) { |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2676 | const char *str = http_get_reason(status); |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 2677 | reason = ist(str); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2678 | } |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2679 | |
Christopher Faulet | bde2c4c | 2020-08-31 16:43:34 +0200 | [diff] [blame] | 2680 | if (!http_replace_res_status(htx, ist2(trash.area, trash.data), reason)) |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2681 | return -1; |
| 2682 | return 0; |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2683 | } |
| 2684 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2685 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
| 2686 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 2687 | * further processing of the request (auth, deny, ...), and defaults to |
| 2688 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 2689 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 2690 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 2691 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 2692 | * status. |
| 2693 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2694 | static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *def_rules, |
| 2695 | struct list *rules, struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2696 | { |
| 2697 | struct session *sess = strm_sess(s); |
| 2698 | struct http_txn *txn = s->txn; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2699 | struct act_rule *rule; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2700 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2701 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2702 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2703 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2704 | * resume condition. If a resume is needed it is always in the action |
| 2705 | * and never in the ACL or converters. In this case, we initialise the |
| 2706 | * current rule, and go to the action execution point. |
| 2707 | */ |
| 2708 | if (s->current_rule) { |
| 2709 | rule = s->current_rule; |
| 2710 | s->current_rule = NULL; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2711 | 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] | 2712 | goto resume_execution; |
| 2713 | } |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2714 | 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] | 2715 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2716 | restart: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2717 | /* start the ruleset evaluation in strict mode */ |
| 2718 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2719 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2720 | list_for_each_entry(rule, s->current_rule_list, list) { |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2721 | /* check optional condition */ |
| 2722 | if (rule->cond) { |
| 2723 | int ret; |
| 2724 | |
| 2725 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 2726 | ret = acl_pass(ret); |
| 2727 | |
| 2728 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2729 | ret = !ret; |
| 2730 | |
| 2731 | if (!ret) /* condition not matched */ |
| 2732 | continue; |
| 2733 | } |
| 2734 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2735 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2736 | resume_execution: |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 2737 | if (rule->kw->flags & KWF_EXPERIMENTAL) |
| 2738 | mark_tainted(TAINTED_ACTION_EXP_EXECUTED); |
| 2739 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2740 | /* Always call the action function if defined */ |
| 2741 | if (rule->action_ptr) { |
| 2742 | if ((s->req.flags & CF_READ_ERROR) || |
| 2743 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 2744 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2745 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2746 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2747 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2748 | case ACT_RET_CONT: |
| 2749 | break; |
| 2750 | case ACT_RET_STOP: |
| 2751 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2752 | s->last_rule_file = rule->conf.file; |
| 2753 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2754 | goto end; |
| 2755 | case ACT_RET_YIELD: |
| 2756 | s->current_rule = rule; |
| 2757 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2758 | goto end; |
| 2759 | case ACT_RET_ERR: |
| 2760 | rule_ret = HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2761 | s->last_rule_file = rule->conf.file; |
| 2762 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2763 | goto end; |
| 2764 | case ACT_RET_DONE: |
| 2765 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2766 | s->last_rule_file = rule->conf.file; |
| 2767 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2768 | goto end; |
| 2769 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 2770 | if (txn->status == -1) |
| 2771 | txn->status = 403; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2772 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2773 | s->last_rule_file = rule->conf.file; |
| 2774 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2775 | goto end; |
| 2776 | case ACT_RET_ABRT: |
| 2777 | rule_ret = HTTP_RULE_RES_ABRT; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2778 | s->last_rule_file = rule->conf.file; |
| 2779 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2780 | goto end; |
| 2781 | case ACT_RET_INV: |
| 2782 | rule_ret = HTTP_RULE_RES_BADREQ; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2783 | s->last_rule_file = rule->conf.file; |
| 2784 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2785 | goto end; |
| 2786 | } |
| 2787 | continue; /* eval the next rule */ |
| 2788 | } |
| 2789 | |
| 2790 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2791 | switch (rule->action) { |
| 2792 | case ACT_ACTION_ALLOW: |
| 2793 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2794 | s->last_rule_file = rule->conf.file; |
| 2795 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2796 | goto end; |
| 2797 | |
| 2798 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2799 | txn->status = rule->arg.http_reply->status; |
| 2800 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2801 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2802 | s->last_rule_file = rule->conf.file; |
| 2803 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2804 | goto end; |
| 2805 | |
| 2806 | case ACT_HTTP_REQ_TARPIT: |
| 2807 | txn->flags |= TX_CLTARPIT; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2808 | txn->status = rule->arg.http_reply->status; |
| 2809 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2810 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2811 | s->last_rule_file = rule->conf.file; |
| 2812 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2813 | goto end; |
| 2814 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2815 | case ACT_HTTP_REDIR: { |
| 2816 | int ret = http_apply_redirect_rule(rule->arg.redir, s, txn); |
| 2817 | |
| 2818 | if (ret == 2) // 2 == skip |
| 2819 | break; |
| 2820 | |
| 2821 | rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2822 | s->last_rule_file = rule->conf.file; |
| 2823 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2824 | goto end; |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2825 | } |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2826 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2827 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2828 | default: |
| 2829 | break; |
| 2830 | } |
| 2831 | } |
| 2832 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2833 | if (def_rules && s->current_rule_list == def_rules) { |
| 2834 | s->current_rule_list = rules; |
| 2835 | goto restart; |
| 2836 | } |
| 2837 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2838 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2839 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2840 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2841 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2842 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2843 | /* we reached the end of the rules, nothing to report */ |
| 2844 | return rule_ret; |
| 2845 | } |
| 2846 | |
| 2847 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 2848 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 2849 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 2850 | * is returned, the process can continue the evaluation of next rule list. If |
| 2851 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 2852 | * 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] | 2853 | * must be returned. If *YIELD is returned, the caller must call again the |
| 2854 | * function with the same context. |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2855 | */ |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2856 | static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *def_rules, |
| 2857 | struct list *rules, struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2858 | { |
| 2859 | struct session *sess = strm_sess(s); |
| 2860 | struct http_txn *txn = s->txn; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2861 | struct act_rule *rule; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2862 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2863 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2864 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2865 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2866 | * resume condition. If a resume is needed it is always in the action |
| 2867 | * and never in the ACL or converters. In this case, we initialise the |
| 2868 | * current rule, and go to the action execution point. |
| 2869 | */ |
| 2870 | if (s->current_rule) { |
| 2871 | rule = s->current_rule; |
| 2872 | s->current_rule = NULL; |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2873 | 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] | 2874 | goto resume_execution; |
| 2875 | } |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2876 | s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules); |
| 2877 | |
| 2878 | restart: |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2879 | |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2880 | /* start the ruleset evaluation in strict mode */ |
| 2881 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2882 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2883 | list_for_each_entry(rule, s->current_rule_list, list) { |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2884 | /* check optional condition */ |
| 2885 | if (rule->cond) { |
| 2886 | int ret; |
| 2887 | |
| 2888 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 2889 | ret = acl_pass(ret); |
| 2890 | |
| 2891 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2892 | ret = !ret; |
| 2893 | |
| 2894 | if (!ret) /* condition not matched */ |
| 2895 | continue; |
| 2896 | } |
| 2897 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2898 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2899 | resume_execution: |
Amaury Denoyelle | 0351773 | 2021-05-07 14:25:01 +0200 | [diff] [blame] | 2900 | if (rule->kw->flags & KWF_EXPERIMENTAL) |
| 2901 | mark_tainted(TAINTED_ACTION_EXP_EXECUTED); |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2902 | |
| 2903 | /* Always call the action function if defined */ |
| 2904 | if (rule->action_ptr) { |
| 2905 | if ((s->req.flags & CF_READ_ERROR) || |
| 2906 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 2907 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2908 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2909 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2910 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2911 | case ACT_RET_CONT: |
| 2912 | break; |
| 2913 | case ACT_RET_STOP: |
| 2914 | rule_ret = HTTP_RULE_RES_STOP; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2915 | s->last_rule_file = rule->conf.file; |
| 2916 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2917 | goto end; |
| 2918 | case ACT_RET_YIELD: |
| 2919 | s->current_rule = rule; |
| 2920 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2921 | goto end; |
| 2922 | case ACT_RET_ERR: |
| 2923 | rule_ret = HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2924 | s->last_rule_file = rule->conf.file; |
| 2925 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2926 | goto end; |
| 2927 | case ACT_RET_DONE: |
| 2928 | rule_ret = HTTP_RULE_RES_DONE; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2929 | s->last_rule_file = rule->conf.file; |
| 2930 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2931 | goto end; |
| 2932 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 2933 | if (txn->status == -1) |
| 2934 | txn->status = 502; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2935 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2936 | s->last_rule_file = rule->conf.file; |
| 2937 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2938 | goto end; |
| 2939 | case ACT_RET_ABRT: |
| 2940 | rule_ret = HTTP_RULE_RES_ABRT; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2941 | s->last_rule_file = rule->conf.file; |
| 2942 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2943 | goto end; |
| 2944 | case ACT_RET_INV: |
| 2945 | rule_ret = HTTP_RULE_RES_BADREQ; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2946 | s->last_rule_file = rule->conf.file; |
| 2947 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2948 | goto end; |
| 2949 | } |
| 2950 | continue; /* eval the next rule */ |
| 2951 | } |
| 2952 | |
| 2953 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2954 | switch (rule->action) { |
| 2955 | case ACT_ACTION_ALLOW: |
| 2956 | rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2957 | s->last_rule_file = rule->conf.file; |
| 2958 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2959 | goto end; |
| 2960 | |
| 2961 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2962 | txn->status = rule->arg.http_reply->status; |
| 2963 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 2964 | rule_ret = HTTP_RULE_RES_DENY; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2965 | s->last_rule_file = rule->conf.file; |
| 2966 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2967 | goto end; |
| 2968 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2969 | case ACT_HTTP_REDIR: { |
| 2970 | int ret = http_apply_redirect_rule(rule->arg.redir, s, txn); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2971 | |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2972 | if (ret == 2) // 2 == skip |
| 2973 | break; |
| 2974 | |
| 2975 | rule_ret = ret ? HTTP_RULE_RES_ABRT : HTTP_RULE_RES_ERROR; |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 2976 | s->last_rule_file = rule->conf.file; |
| 2977 | s->last_rule_line = rule->conf.line; |
Willy Tarreau | bc1223b | 2021-09-02 16:54:33 +0200 | [diff] [blame] | 2978 | goto end; |
| 2979 | } |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2980 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2981 | default: |
| 2982 | break; |
| 2983 | } |
| 2984 | } |
| 2985 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 2986 | if (def_rules && s->current_rule_list == def_rules) { |
| 2987 | s->current_rule_list = rules; |
| 2988 | goto restart; |
| 2989 | } |
| 2990 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2991 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2992 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2993 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2994 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2995 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2996 | /* we reached the end of the rules, nothing to report */ |
| 2997 | return rule_ret; |
| 2998 | } |
| 2999 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3000 | /* Executes backend and frontend http-after-response rules for the stream <s>, |
| 3001 | * in that order. it return 1 on success and 0 on error. It is the caller |
| 3002 | * responsibility to catch error or ignore it. If it catches it, this function |
| 3003 | * may be called a second time, for the internal error. |
| 3004 | */ |
| 3005 | int http_eval_after_res_rules(struct stream *s) |
| 3006 | { |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 3007 | struct list *def_rules, *rules; |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3008 | struct session *sess = s->sess; |
| 3009 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 3010 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 3011 | /* Eval after-response ruleset only if the reply is not const */ |
| 3012 | if (s->txn->flags & TX_CONST_REPLY) |
| 3013 | goto end; |
| 3014 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3015 | /* prune the request variables if not already done and swap to the response variables. */ |
| 3016 | if (s->vars_reqres.scope != SCOPE_RES) { |
| 3017 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 3018 | vars_prune(&s->vars_reqres, s->sess, s); |
Willy Tarreau | b7bfcb3 | 2021-08-31 08:13:25 +0200 | [diff] [blame] | 3019 | vars_init_head(&s->vars_reqres, SCOPE_RES); |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3020 | } |
| 3021 | |
Christopher Faulet | d4150ad | 2021-10-13 15:35:55 +0200 | [diff] [blame] | 3022 | def_rules = (s->be->defpx ? &s->be->defpx->http_after_res_rules : NULL); |
| 3023 | rules = &s->be->http_after_res_rules; |
| 3024 | |
| 3025 | ret = http_res_get_intercept_rule(s->be, def_rules, rules, s); |
Christopher Faulet | 4c5a591 | 2021-11-09 17:48:39 +0100 | [diff] [blame] | 3026 | 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] | 3027 | def_rules = ((sess->fe->defpx && sess->fe->defpx != s->be->defpx) ? &sess->fe->defpx->http_after_res_rules : NULL); |
| 3028 | rules = &sess->fe->http_after_res_rules; |
| 3029 | ret = http_res_get_intercept_rule(sess->fe, def_rules, rules, s); |
| 3030 | } |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3031 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 3032 | end: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3033 | /* All other codes than CONTINUE, STOP or DONE are forbidden */ |
| 3034 | return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE); |
| 3035 | } |
| 3036 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3037 | /* |
| 3038 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 3039 | * desirable to call it only when needed. This code is quite complex because |
| 3040 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 3041 | * highly recommended not to touch this part without a good reason ! |
| 3042 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3043 | 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] | 3044 | { |
| 3045 | struct session *sess = s->sess; |
| 3046 | struct http_txn *txn = s->txn; |
| 3047 | struct htx *htx; |
| 3048 | struct http_hdr_ctx ctx; |
| 3049 | char *hdr_beg, *hdr_end, *del_from; |
| 3050 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
| 3051 | int preserve_hdr; |
| 3052 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3053 | htx = htxbuf(&req->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3054 | ctx.blk = NULL; |
| 3055 | while (http_find_header(htx, ist("Cookie"), &ctx, 1)) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3056 | int is_first = 1; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3057 | del_from = NULL; /* nothing to be deleted */ |
| 3058 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 3059 | |
| 3060 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3061 | * attributes whose name begin with a '$', and associate them with |
| 3062 | * the right cookie, if we want to delete this cookie. |
| 3063 | * So there are 3 cases for each cookie read : |
| 3064 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3065 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3066 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3067 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3068 | * "special" cookie. |
| 3069 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3070 | * remove it. If no application cookie persists in the header, we |
| 3071 | * *MUST* delete it. |
| 3072 | * |
| 3073 | * Note: RFC2965 is unclear about the processing of spaces around |
| 3074 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 3075 | * the RFC explicitly allows spaces before it, and not within the |
| 3076 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 3077 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 3078 | * after the equal sign too, resulting in some (rare) buggy |
| 3079 | * implementations trying to do that. So let's do what servers do. |
| 3080 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 3081 | * allowed quoted strings in values, with any possible character |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3082 | * after a backslash, including control chars and delimiters, which |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3083 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 3084 | * within values even without quotes. |
| 3085 | * |
| 3086 | * We have to keep multiple pointers in order to support cookie |
| 3087 | * removal at the beginning, middle or end of header without |
| 3088 | * corrupting the header. All of these headers are valid : |
| 3089 | * |
| 3090 | * hdr_beg hdr_end |
| 3091 | * | | |
| 3092 | * v | |
| 3093 | * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 | |
| 3094 | * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v |
| 3095 | * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3 |
| 3096 | * | | | | | | | |
| 3097 | * | | | | | | | |
| 3098 | * | | | | | | +--> next |
| 3099 | * | | | | | +----> val_end |
| 3100 | * | | | | +-----------> val_beg |
| 3101 | * | | | +--------------> equal |
| 3102 | * | | +----------------> att_end |
| 3103 | * | +---------------------> att_beg |
| 3104 | * +--------------------------> prev |
| 3105 | * |
| 3106 | */ |
| 3107 | hdr_beg = ctx.value.ptr; |
| 3108 | hdr_end = hdr_beg + ctx.value.len; |
| 3109 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3110 | /* Iterate through all cookies on this line */ |
| 3111 | |
| 3112 | /* find att_beg */ |
| 3113 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3114 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3115 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3116 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3117 | |
| 3118 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3119 | att_beg++; |
| 3120 | |
| 3121 | /* find att_end : this is the first character after the last non |
| 3122 | * space before the equal. It may be equal to hdr_end. |
| 3123 | */ |
| 3124 | equal = att_end = att_beg; |
| 3125 | while (equal < hdr_end) { |
| 3126 | if (*equal == '=' || *equal == ',' || *equal == ';') |
| 3127 | break; |
| 3128 | if (HTTP_IS_SPHT(*equal++)) |
| 3129 | continue; |
| 3130 | att_end = equal; |
| 3131 | } |
| 3132 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3133 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3134 | * is between <att_beg> and <equal>, both may be identical. |
| 3135 | */ |
| 3136 | /* look for end of cookie if there is an equal sign */ |
| 3137 | if (equal < hdr_end && *equal == '=') { |
| 3138 | /* look for the beginning of the value */ |
| 3139 | val_beg = equal + 1; |
| 3140 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3141 | val_beg++; |
| 3142 | |
| 3143 | /* find the end of the value, respecting quotes */ |
| 3144 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3145 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3146 | /* 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] | 3147 | val_end = next; |
| 3148 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3149 | val_end--; |
| 3150 | } |
| 3151 | else |
| 3152 | val_beg = val_end = next = equal; |
| 3153 | |
| 3154 | /* We have nothing to do with attributes beginning with |
| 3155 | * '$'. However, they will automatically be removed if a |
| 3156 | * header before them is removed, since they're supposed |
| 3157 | * to be linked together. |
| 3158 | */ |
| 3159 | if (*att_beg == '$') |
| 3160 | continue; |
| 3161 | |
| 3162 | /* Ignore cookies with no equal sign */ |
| 3163 | if (equal == next) { |
| 3164 | /* This is not our cookie, so we must preserve it. But if we already |
| 3165 | * scheduled another cookie for removal, we cannot remove the |
| 3166 | * complete header, but we can remove the previous block itself. |
| 3167 | */ |
| 3168 | preserve_hdr = 1; |
| 3169 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3170 | 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] | 3171 | val_end += delta; |
| 3172 | next += delta; |
| 3173 | hdr_end += delta; |
| 3174 | prev = del_from; |
| 3175 | del_from = NULL; |
| 3176 | } |
| 3177 | continue; |
| 3178 | } |
| 3179 | |
| 3180 | /* if there are spaces around the equal sign, we need to |
| 3181 | * strip them otherwise we'll get trouble for cookie captures, |
| 3182 | * or even for rewrites. Since this happens extremely rarely, |
| 3183 | * it does not hurt performance. |
| 3184 | */ |
| 3185 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3186 | int stripped_before = 0; |
| 3187 | int stripped_after = 0; |
| 3188 | |
| 3189 | if (att_end != equal) { |
| 3190 | memmove(att_end, equal, hdr_end - equal); |
| 3191 | stripped_before = (att_end - equal); |
| 3192 | equal += stripped_before; |
| 3193 | val_beg += stripped_before; |
| 3194 | } |
| 3195 | |
| 3196 | if (val_beg > equal + 1) { |
| 3197 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3198 | stripped_after = (equal + 1) - val_beg; |
| 3199 | val_beg += stripped_after; |
| 3200 | stripped_before += stripped_after; |
| 3201 | } |
| 3202 | |
| 3203 | val_end += stripped_before; |
| 3204 | next += stripped_before; |
| 3205 | hdr_end += stripped_before; |
| 3206 | } |
| 3207 | /* now everything is as on the diagram above */ |
| 3208 | |
| 3209 | /* First, let's see if we want to capture this cookie. We check |
| 3210 | * that we don't already have a client side cookie, because we |
| 3211 | * can only capture one. Also as an optimisation, we ignore |
| 3212 | * cookies shorter than the declared name. |
| 3213 | */ |
| 3214 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 3215 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3216 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3217 | int log_len = val_end - att_beg; |
| 3218 | |
| 3219 | if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3220 | ha_alert("HTTP logging : out of memory.\n"); |
| 3221 | } else { |
| 3222 | if (log_len > sess->fe->capture_len) |
| 3223 | log_len = sess->fe->capture_len; |
| 3224 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 3225 | txn->cli_cookie[log_len] = 0; |
| 3226 | } |
| 3227 | } |
| 3228 | |
| 3229 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 3230 | * following form : |
| 3231 | * |
| 3232 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
| 3233 | * |
| 3234 | * For cookies in prefix mode, the form is : |
| 3235 | * |
| 3236 | * Cookie: NAME=SRV~VALUE |
| 3237 | */ |
| 3238 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3239 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3240 | struct server *srv = s->be->srv; |
| 3241 | char *delim; |
| 3242 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3243 | /* 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] | 3244 | * have the server ID between val_beg and delim, and the original cookie between |
| 3245 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 3246 | * |
| 3247 | * hdr_beg |
| 3248 | * | |
| 3249 | * v |
| 3250 | * NAME=SRV; # in all but prefix modes |
| 3251 | * NAME=SRV~OPAQUE ; # in prefix mode |
| 3252 | * || || | |+-> next |
| 3253 | * || || | +--> val_end |
| 3254 | * || || +---------> delim |
| 3255 | * || |+------------> val_beg |
| 3256 | * || +-------------> att_end = equal |
| 3257 | * |+-----------------> att_beg |
| 3258 | * +------------------> prev |
| 3259 | * |
| 3260 | */ |
| 3261 | if (s->be->ck_opts & PR_CK_PFX) { |
| 3262 | for (delim = val_beg; delim < val_end; delim++) |
| 3263 | if (*delim == COOKIE_DELIM) |
| 3264 | break; |
| 3265 | } |
| 3266 | else { |
| 3267 | char *vbar1; |
| 3268 | delim = val_end; |
| 3269 | /* Now check if the cookie contains a date field, which would |
| 3270 | * appear after a vertical bar ('|') just after the server name |
| 3271 | * and before the delimiter. |
| 3272 | */ |
| 3273 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 3274 | if (vbar1) { |
| 3275 | /* OK, so left of the bar is the server's cookie and |
| 3276 | * right is the last seen date. It is a base64 encoded |
| 3277 | * 30-bit value representing the UNIX date since the |
| 3278 | * epoch in 4-second quantities. |
| 3279 | */ |
| 3280 | int val; |
| 3281 | delim = vbar1++; |
| 3282 | if (val_end - vbar1 >= 5) { |
| 3283 | val = b64tos30(vbar1); |
| 3284 | if (val > 0) |
| 3285 | txn->cookie_last_date = val << 2; |
| 3286 | } |
| 3287 | /* look for a second vertical bar */ |
| 3288 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 3289 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 3290 | val = b64tos30(vbar1 + 1); |
| 3291 | if (val > 0) |
| 3292 | txn->cookie_first_date = val << 2; |
| 3293 | } |
| 3294 | } |
| 3295 | } |
| 3296 | |
| 3297 | /* if the cookie has an expiration date and the proxy wants to check |
| 3298 | * it, then we do that now. We first check if the cookie is too old, |
| 3299 | * then only if it has expired. We detect strict overflow because the |
| 3300 | * time resolution here is not great (4 seconds). Cookies with dates |
| 3301 | * in the future are ignored if their offset is beyond one day. This |
| 3302 | * allows an admin to fix timezone issues without expiring everyone |
| 3303 | * and at the same time avoids keeping unwanted side effects for too |
| 3304 | * long. |
| 3305 | */ |
| 3306 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 3307 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) || |
| 3308 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
| 3309 | txn->flags &= ~TX_CK_MASK; |
| 3310 | txn->flags |= TX_CK_OLD; |
| 3311 | delim = val_beg; // let's pretend we have not found the cookie |
| 3312 | txn->cookie_first_date = 0; |
| 3313 | txn->cookie_last_date = 0; |
| 3314 | } |
| 3315 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 3316 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) || |
| 3317 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
| 3318 | txn->flags &= ~TX_CK_MASK; |
| 3319 | txn->flags |= TX_CK_EXPIRED; |
| 3320 | delim = val_beg; // let's pretend we have not found the cookie |
| 3321 | txn->cookie_first_date = 0; |
| 3322 | txn->cookie_last_date = 0; |
| 3323 | } |
| 3324 | |
| 3325 | /* Here, we'll look for the first running server which supports the cookie. |
| 3326 | * This allows to share a same cookie between several servers, for example |
| 3327 | * to dedicate backup servers to specific servers only. |
| 3328 | * However, to prevent clients from sticking to cookie-less backup server |
| 3329 | * when they have incidentely learned an empty cookie, we simply ignore |
| 3330 | * empty cookies and mark them as invalid. |
| 3331 | * The same behaviour is applied when persistence must be ignored. |
| 3332 | */ |
| 3333 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3334 | srv = NULL; |
| 3335 | |
| 3336 | while (srv) { |
| 3337 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 3338 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 3339 | if ((srv->cur_state != SRV_ST_STOPPED) || |
| 3340 | (s->be->options & PR_O_PERSIST) || |
| 3341 | (s->flags & SF_FORCE_PRST)) { |
| 3342 | /* we found the server and we can use it */ |
| 3343 | txn->flags &= ~TX_CK_MASK; |
| 3344 | txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN; |
| 3345 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
| 3346 | s->target = &srv->obj_type; |
| 3347 | break; |
| 3348 | } else { |
| 3349 | /* we found a server, but it's down, |
| 3350 | * mark it as such and go on in case |
| 3351 | * another one is available. |
| 3352 | */ |
| 3353 | txn->flags &= ~TX_CK_MASK; |
| 3354 | txn->flags |= TX_CK_DOWN; |
| 3355 | } |
| 3356 | } |
| 3357 | srv = srv->next; |
| 3358 | } |
| 3359 | |
| 3360 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
| 3361 | /* no server matched this cookie or we deliberately skipped it */ |
| 3362 | txn->flags &= ~TX_CK_MASK; |
| 3363 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3364 | txn->flags |= TX_CK_UNUSED; |
| 3365 | else |
| 3366 | txn->flags |= TX_CK_INVALID; |
| 3367 | } |
| 3368 | |
| 3369 | /* depending on the cookie mode, we may have to either : |
| 3370 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3371 | * the server never sees it ; |
| 3372 | * - 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] | 3373 | * application cookie so that it does not get accidentally removed later, |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3374 | * if we're in cookie prefix mode |
| 3375 | */ |
| 3376 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
| 3377 | int delta; /* negative */ |
| 3378 | |
| 3379 | memmove(val_beg, delim + 1, hdr_end - (delim + 1)); |
| 3380 | delta = val_beg - (delim + 1); |
| 3381 | val_end += delta; |
| 3382 | next += delta; |
| 3383 | hdr_end += delta; |
| 3384 | del_from = NULL; |
| 3385 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 3386 | } |
| 3387 | else if (del_from == NULL && |
| 3388 | (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) { |
| 3389 | del_from = prev; |
| 3390 | } |
| 3391 | } |
| 3392 | else { |
| 3393 | /* This is not our cookie, so we must preserve it. But if we already |
| 3394 | * scheduled another cookie for removal, we cannot remove the |
| 3395 | * complete header, but we can remove the previous block itself. |
| 3396 | */ |
| 3397 | preserve_hdr = 1; |
| 3398 | |
| 3399 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3400 | 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] | 3401 | if (att_beg >= del_from) |
| 3402 | att_beg += delta; |
| 3403 | if (att_end >= del_from) |
| 3404 | att_end += delta; |
| 3405 | val_beg += delta; |
| 3406 | val_end += delta; |
| 3407 | next += delta; |
| 3408 | hdr_end += delta; |
| 3409 | prev = del_from; |
| 3410 | del_from = NULL; |
| 3411 | } |
| 3412 | } |
| 3413 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3414 | } /* for each cookie */ |
| 3415 | |
| 3416 | |
| 3417 | /* There are no more cookies on this line. |
| 3418 | * We may still have one (or several) marked for deletion at the |
| 3419 | * end of the line. We must do this now in two ways : |
| 3420 | * - if some cookies must be preserved, we only delete from the |
| 3421 | * mark to the end of line ; |
| 3422 | * - if nothing needs to be preserved, simply delete the whole header |
| 3423 | */ |
| 3424 | if (del_from) { |
| 3425 | hdr_end = (preserve_hdr ? del_from : hdr_beg); |
| 3426 | } |
| 3427 | if ((hdr_end - hdr_beg) != ctx.value.len) { |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3428 | if (hdr_beg != hdr_end) |
| 3429 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3430 | else |
| 3431 | http_remove_header(htx, &ctx); |
| 3432 | } |
| 3433 | } /* for each "Cookie header */ |
| 3434 | } |
| 3435 | |
| 3436 | /* |
| 3437 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 3438 | * desirable to call it only when needed. This function is also used when we |
| 3439 | * just need to know if there is a cookie (eg: for check-cache). |
| 3440 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3441 | 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] | 3442 | { |
| 3443 | struct session *sess = s->sess; |
| 3444 | struct http_txn *txn = s->txn; |
| 3445 | struct htx *htx; |
| 3446 | struct http_hdr_ctx ctx; |
| 3447 | struct server *srv; |
| 3448 | char *hdr_beg, *hdr_end; |
| 3449 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 6f7a02a | 2019-04-15 21:49:49 +0200 | [diff] [blame] | 3450 | int is_cookie2 = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3451 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3452 | htx = htxbuf(&res->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3453 | |
| 3454 | ctx.blk = NULL; |
| 3455 | while (1) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3456 | int is_first = 1; |
| 3457 | |
Andrew McDermott | bfb15ab | 2022-02-11 18:26:49 +0000 | [diff] [blame] | 3458 | if (is_cookie2 || !http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) { |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3459 | if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1)) |
| 3460 | break; |
| 3461 | is_cookie2 = 1; |
| 3462 | } |
| 3463 | |
| 3464 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 3465 | * <prev> points to the colon. |
| 3466 | */ |
| 3467 | txn->flags |= TX_SCK_PRESENT; |
| 3468 | |
| 3469 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 3470 | * check-cache is enabled) and we are not interested in checking |
| 3471 | * them. Warning, the cookie capture is declared in the frontend. |
| 3472 | */ |
| 3473 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
| 3474 | break; |
| 3475 | |
| 3476 | /* OK so now we know we have to process this response cookie. |
| 3477 | * The format of the Set-Cookie header is slightly different |
| 3478 | * from the format of the Cookie header in that it does not |
| 3479 | * support the comma as a cookie delimiter (thus the header |
| 3480 | * cannot be folded) because the Expires attribute described in |
| 3481 | * the original Netscape's spec may contain an unquoted date |
| 3482 | * with a comma inside. We have to live with this because |
| 3483 | * many browsers don't support Max-Age and some browsers don't |
| 3484 | * support quoted strings. However the Set-Cookie2 header is |
| 3485 | * clean. |
| 3486 | * |
| 3487 | * We have to keep multiple pointers in order to support cookie |
| 3488 | * removal at the beginning, middle or end of header without |
| 3489 | * corrupting the header (in case of set-cookie2). A special |
| 3490 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 3491 | * fields after the first semi-colon. The <next> pointer points |
| 3492 | * either to the end of line (set-cookie) or next unquoted comma |
| 3493 | * (set-cookie2). All of these headers are valid : |
| 3494 | * |
| 3495 | * hdr_beg hdr_end |
| 3496 | * | | |
| 3497 | * v | |
| 3498 | * NAME1 = VALUE 1 ; Secure; Path="/" | |
| 3499 | * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v |
| 3500 | * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT |
| 3501 | * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard |
| 3502 | * | | | | | | | | |
| 3503 | * | | | | | | | +-> next |
| 3504 | * | | | | | | +------------> scav |
| 3505 | * | | | | | +--------------> val_end |
| 3506 | * | | | | +--------------------> val_beg |
| 3507 | * | | | +----------------------> equal |
| 3508 | * | | +------------------------> att_end |
| 3509 | * | +----------------------------> att_beg |
| 3510 | * +------------------------------> prev |
| 3511 | * -------------------------------> hdr_beg |
| 3512 | */ |
| 3513 | hdr_beg = ctx.value.ptr; |
| 3514 | hdr_end = hdr_beg + ctx.value.len; |
| 3515 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3516 | |
| 3517 | /* Iterate through all cookies on this line */ |
| 3518 | |
| 3519 | /* find att_beg */ |
| 3520 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3521 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3522 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3523 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3524 | |
| 3525 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3526 | att_beg++; |
| 3527 | |
| 3528 | /* find att_end : this is the first character after the last non |
| 3529 | * space before the equal. It may be equal to hdr_end. |
| 3530 | */ |
| 3531 | equal = att_end = att_beg; |
| 3532 | |
| 3533 | while (equal < hdr_end) { |
| 3534 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 3535 | break; |
| 3536 | if (HTTP_IS_SPHT(*equal++)) |
| 3537 | continue; |
| 3538 | att_end = equal; |
| 3539 | } |
| 3540 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3541 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3542 | * is between <att_beg> and <equal>, both may be identical. |
| 3543 | */ |
| 3544 | |
| 3545 | /* look for end of cookie if there is an equal sign */ |
| 3546 | if (equal < hdr_end && *equal == '=') { |
| 3547 | /* look for the beginning of the value */ |
| 3548 | val_beg = equal + 1; |
| 3549 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3550 | val_beg++; |
| 3551 | |
| 3552 | /* find the end of the value, respecting quotes */ |
| 3553 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3554 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3555 | /* 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] | 3556 | val_end = next; |
| 3557 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3558 | val_end--; |
| 3559 | } |
| 3560 | else { |
| 3561 | /* <equal> points to next comma, semi-colon or EOL */ |
| 3562 | val_beg = val_end = next = equal; |
| 3563 | } |
| 3564 | |
| 3565 | if (next < hdr_end) { |
| 3566 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 3567 | * a colon or semi-colon before the end. So skip all attr-value |
| 3568 | * pairs and look for the next comma. For Set-Cookie, since |
| 3569 | * commas are permitted in values, skip to the end. |
| 3570 | */ |
| 3571 | if (is_cookie2) |
| 3572 | next = http_find_hdr_value_end(next, hdr_end); |
| 3573 | else |
| 3574 | next = hdr_end; |
| 3575 | } |
| 3576 | |
| 3577 | /* Now everything is as on the diagram above */ |
| 3578 | |
| 3579 | /* Ignore cookies with no equal sign */ |
| 3580 | if (equal == val_end) |
| 3581 | continue; |
| 3582 | |
| 3583 | /* If there are spaces around the equal sign, we need to |
| 3584 | * strip them otherwise we'll get trouble for cookie captures, |
| 3585 | * or even for rewrites. Since this happens extremely rarely, |
| 3586 | * it does not hurt performance. |
| 3587 | */ |
| 3588 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3589 | int stripped_before = 0; |
| 3590 | int stripped_after = 0; |
| 3591 | |
| 3592 | if (att_end != equal) { |
| 3593 | memmove(att_end, equal, hdr_end - equal); |
| 3594 | stripped_before = (att_end - equal); |
| 3595 | equal += stripped_before; |
| 3596 | val_beg += stripped_before; |
| 3597 | } |
| 3598 | |
| 3599 | if (val_beg > equal + 1) { |
| 3600 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3601 | stripped_after = (equal + 1) - val_beg; |
| 3602 | val_beg += stripped_after; |
| 3603 | stripped_before += stripped_after; |
| 3604 | } |
| 3605 | |
| 3606 | val_end += stripped_before; |
| 3607 | next += stripped_before; |
| 3608 | hdr_end += stripped_before; |
| 3609 | |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3610 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3611 | ctx.value.len = hdr_end - hdr_beg; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3612 | } |
| 3613 | |
| 3614 | /* First, let's see if we want to capture this cookie. We check |
| 3615 | * that we don't already have a server side cookie, because we |
| 3616 | * can only capture one. Also as an optimisation, we ignore |
| 3617 | * cookies shorter than the declared name. |
| 3618 | */ |
| 3619 | if (sess->fe->capture_name != NULL && |
| 3620 | txn->srv_cookie == NULL && |
| 3621 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3622 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3623 | int log_len = val_end - att_beg; |
| 3624 | if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3625 | ha_alert("HTTP logging : out of memory.\n"); |
| 3626 | } |
| 3627 | else { |
| 3628 | if (log_len > sess->fe->capture_len) |
| 3629 | log_len = sess->fe->capture_len; |
| 3630 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 3631 | txn->srv_cookie[log_len] = 0; |
| 3632 | } |
| 3633 | } |
| 3634 | |
| 3635 | srv = objt_server(s->target); |
| 3636 | /* now check if we need to process it for persistence */ |
| 3637 | if (!(s->flags & SF_IGNORE_PRST) && |
| 3638 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3639 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3640 | /* assume passive cookie by default */ |
| 3641 | txn->flags &= ~TX_SCK_MASK; |
| 3642 | txn->flags |= TX_SCK_FOUND; |
| 3643 | |
| 3644 | /* If the cookie is in insert mode on a known server, we'll delete |
| 3645 | * this occurrence because we'll insert another one later. |
| 3646 | * We'll delete it too if the "indirect" option is set and we're in |
| 3647 | * a direct access. |
| 3648 | */ |
| 3649 | if (s->be->ck_opts & PR_CK_PSV) { |
| 3650 | /* The "preserve" flag was set, we don't want to touch the |
| 3651 | * server's cookie. |
| 3652 | */ |
| 3653 | } |
| 3654 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
| 3655 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
| 3656 | /* this cookie must be deleted */ |
| 3657 | if (prev == hdr_beg && next == hdr_end) { |
| 3658 | /* whole header */ |
| 3659 | http_remove_header(htx, &ctx); |
| 3660 | /* note: while both invalid now, <next> and <hdr_end> |
| 3661 | * are still equal, so the for() will stop as expected. |
| 3662 | */ |
| 3663 | } else { |
| 3664 | /* just remove the value */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3665 | int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3666 | next = prev; |
| 3667 | hdr_end += delta; |
| 3668 | } |
| 3669 | txn->flags &= ~TX_SCK_MASK; |
| 3670 | txn->flags |= TX_SCK_DELETED; |
| 3671 | /* and go on with next cookie */ |
| 3672 | } |
| 3673 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
| 3674 | /* replace bytes val_beg->val_end with the cookie name associated |
| 3675 | * with this server since we know it. |
| 3676 | */ |
| 3677 | int sliding, delta; |
| 3678 | |
| 3679 | ctx.value = ist2(val_beg, val_end - val_beg); |
| 3680 | ctx.lws_before = ctx.lws_after = 0; |
| 3681 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen)); |
| 3682 | delta = srv->cklen - (val_end - val_beg); |
| 3683 | sliding = (ctx.value.ptr - val_beg); |
| 3684 | hdr_beg += sliding; |
| 3685 | val_beg += sliding; |
| 3686 | next += sliding + delta; |
| 3687 | hdr_end += sliding + delta; |
| 3688 | |
| 3689 | txn->flags &= ~TX_SCK_MASK; |
| 3690 | txn->flags |= TX_SCK_REPLACED; |
| 3691 | } |
| 3692 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
| 3693 | /* insert the cookie name associated with this server |
| 3694 | * before existing cookie, and insert a delimiter between them.. |
| 3695 | */ |
| 3696 | int sliding, delta; |
| 3697 | ctx.value = ist2(val_beg, 0); |
| 3698 | ctx.lws_before = ctx.lws_after = 0; |
| 3699 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1)); |
| 3700 | delta = srv->cklen + 1; |
| 3701 | sliding = (ctx.value.ptr - val_beg); |
| 3702 | hdr_beg += sliding; |
| 3703 | val_beg += sliding; |
| 3704 | next += sliding + delta; |
| 3705 | hdr_end += sliding + delta; |
| 3706 | |
| 3707 | val_beg[srv->cklen] = COOKIE_DELIM; |
| 3708 | txn->flags &= ~TX_SCK_MASK; |
| 3709 | txn->flags |= TX_SCK_REPLACED; |
| 3710 | } |
| 3711 | } |
| 3712 | /* that's done for this cookie, check the next one on the same |
| 3713 | * line when next != hdr_end (only if is_cookie2). |
| 3714 | */ |
| 3715 | } |
| 3716 | } |
| 3717 | } |
| 3718 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3719 | /* |
| 3720 | * Parses the Cache-Control and Pragma request header fields to determine if |
| 3721 | * the request may be served from the cache and/or if it is cacheable. Updates |
| 3722 | * s->txn->flags. |
| 3723 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3724 | void http_check_request_for_cacheability(struct stream *s, struct channel *req) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3725 | { |
| 3726 | struct http_txn *txn = s->txn; |
| 3727 | struct htx *htx; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3728 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 3729 | int pragma_found, cc_found; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3730 | |
| 3731 | if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE) |
| 3732 | return; /* nothing more to do here */ |
| 3733 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3734 | htx = htxbuf(&req->buf); |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3735 | pragma_found = cc_found = 0; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3736 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3737 | /* Check "pragma" header for HTTP/1.0 compatibility. */ |
| 3738 | if (http_find_header(htx, ist("pragma"), &ctx, 1)) { |
| 3739 | if (isteqi(ctx.value, ist("no-cache"))) { |
| 3740 | pragma_found = 1; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3741 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3742 | } |
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 | ctx.blk = NULL; |
| 3745 | /* Don't use the cache and don't try to store if we found the |
| 3746 | * Authorization header */ |
| 3747 | if (http_find_header(htx, ist("authorization"), &ctx, 1)) { |
| 3748 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3749 | txn->flags |= TX_CACHE_IGNORE; |
| 3750 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3751 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3752 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3753 | /* Look for "cache-control" header and iterate over all the values |
| 3754 | * until we find one that specifies that caching is possible or not. */ |
| 3755 | ctx.blk = NULL; |
| 3756 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3757 | cc_found = 1; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3758 | /* We don't check the values after max-age, max-stale nor min-fresh, |
| 3759 | * we simply don't use the cache when they're specified. */ |
| 3760 | if (istmatchi(ctx.value, ist("max-age")) || |
| 3761 | istmatchi(ctx.value, ist("no-cache")) || |
| 3762 | istmatchi(ctx.value, ist("max-stale")) || |
| 3763 | istmatchi(ctx.value, ist("min-fresh"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3764 | txn->flags |= TX_CACHE_IGNORE; |
| 3765 | continue; |
| 3766 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3767 | if (istmatchi(ctx.value, ist("no-store"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3768 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3769 | continue; |
| 3770 | } |
| 3771 | } |
| 3772 | |
| 3773 | /* RFC7234#5.4: |
| 3774 | * When the Cache-Control header field is also present and |
| 3775 | * understood in a request, Pragma is ignored. |
| 3776 | * When the Cache-Control header field is not present in a |
| 3777 | * request, caches MUST consider the no-cache request |
| 3778 | * pragma-directive as having the same effect as if |
| 3779 | * "Cache-Control: no-cache" were present. |
| 3780 | */ |
| 3781 | if (!cc_found && pragma_found) |
| 3782 | txn->flags |= TX_CACHE_IGNORE; |
| 3783 | } |
| 3784 | |
| 3785 | /* |
| 3786 | * Check if response is cacheable or not. Updates s->txn->flags. |
| 3787 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3788 | void http_check_response_for_cacheability(struct stream *s, struct channel *res) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3789 | { |
| 3790 | struct http_txn *txn = s->txn; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3791 | struct http_hdr_ctx ctx = { .blk = NULL }; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3792 | struct htx *htx; |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3793 | int has_freshness_info = 0; |
| 3794 | int has_validator = 0; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3795 | |
| 3796 | if (txn->status < 200) { |
| 3797 | /* do not try to cache interim responses! */ |
| 3798 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3799 | return; |
| 3800 | } |
| 3801 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3802 | htx = htxbuf(&res->buf); |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3803 | /* Check "pragma" header for HTTP/1.0 compatibility. */ |
| 3804 | if (http_find_header(htx, ist("pragma"), &ctx, 1)) { |
| 3805 | if (isteqi(ctx.value, ist("no-cache"))) { |
| 3806 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3807 | return; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3808 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3809 | } |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3810 | |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3811 | /* Look for "cache-control" header and iterate over all the values |
| 3812 | * until we find one that specifies that caching is possible or not. */ |
| 3813 | ctx.blk = NULL; |
| 3814 | while (http_find_header(htx, ist("cache-control"), &ctx, 0)) { |
| 3815 | if (isteqi(ctx.value, ist("public"))) { |
| 3816 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3817 | continue; |
| 3818 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3819 | if (isteqi(ctx.value, ist("private")) || |
| 3820 | isteqi(ctx.value, ist("no-cache")) || |
| 3821 | isteqi(ctx.value, ist("no-store")) || |
| 3822 | isteqi(ctx.value, ist("max-age=0")) || |
| 3823 | isteqi(ctx.value, ist("s-maxage=0"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3824 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3825 | continue; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3826 | } |
Remi Tricot-Le Breton | 40ed97b | 2020-10-28 11:35:15 +0100 | [diff] [blame] | 3827 | /* We might have a no-cache="set-cookie" form. */ |
| 3828 | if (istmatchi(ctx.value, ist("no-cache=\"set-cookie"))) { |
| 3829 | txn->flags &= ~TX_CACHE_COOK; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3830 | continue; |
| 3831 | } |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3832 | |
| 3833 | if (istmatchi(ctx.value, ist("s-maxage")) || |
| 3834 | istmatchi(ctx.value, ist("max-age"))) { |
| 3835 | has_freshness_info = 1; |
| 3836 | continue; |
| 3837 | } |
| 3838 | } |
| 3839 | |
| 3840 | /* If no freshness information could be found in Cache-Control values, |
| 3841 | * look for an Expires header. */ |
| 3842 | if (!has_freshness_info) { |
| 3843 | ctx.blk = NULL; |
| 3844 | has_freshness_info = http_find_header(htx, ist("expires"), &ctx, 0); |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3845 | } |
Remi Tricot-Le Breton | cc9bf2e | 2020-11-12 11:14:41 +0100 | [diff] [blame] | 3846 | |
| 3847 | /* If no freshness information could be found in Cache-Control or Expires |
| 3848 | * values, look for an explicit validator. */ |
| 3849 | if (!has_freshness_info) { |
| 3850 | ctx.blk = NULL; |
| 3851 | has_validator = 1; |
| 3852 | if (!http_find_header(htx, ist("etag"), &ctx, 0)) { |
| 3853 | ctx.blk = NULL; |
| 3854 | if (!http_find_header(htx, ist("last-modified"), &ctx, 0)) |
| 3855 | has_validator = 0; |
| 3856 | } |
| 3857 | } |
| 3858 | |
| 3859 | /* We won't store an entry that has neither a cache validator nor an |
| 3860 | * explicit expiration time, as suggested in RFC 7234#3. */ |
| 3861 | if (!has_freshness_info && !has_validator) |
| 3862 | txn->flags |= TX_CACHE_IGNORE; |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3863 | } |
| 3864 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3865 | /* |
| 3866 | * In a GET, HEAD or POST request, check if the requested URI matches the stats uri |
| 3867 | * for the current backend. |
| 3868 | * |
| 3869 | * It is assumed that the request is either a HEAD, GET, or POST and that the |
| 3870 | * uri_auth field is valid. |
| 3871 | * |
| 3872 | * Returns 1 if stats should be provided, otherwise 0. |
| 3873 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3874 | 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] | 3875 | { |
| 3876 | struct uri_auth *uri_auth = backend->uri_auth; |
| 3877 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3878 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3879 | struct ist uri; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3880 | |
| 3881 | if (!uri_auth) |
| 3882 | return 0; |
| 3883 | |
| 3884 | if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST) |
| 3885 | return 0; |
| 3886 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3887 | htx = htxbuf(&s->req.buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 3888 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3889 | uri = htx_sl_req_uri(sl); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 3890 | if (*uri_auth->uri_prefix == '/') { |
| 3891 | struct http_uri_parser parser = http_uri_parser_init(uri); |
| 3892 | uri = http_parse_path(&parser); |
| 3893 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3894 | |
| 3895 | /* check URI size */ |
| 3896 | if (uri_auth->uri_len > uri.len) |
| 3897 | return 0; |
| 3898 | |
| 3899 | if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
| 3900 | return 0; |
| 3901 | |
| 3902 | return 1; |
| 3903 | } |
| 3904 | |
| 3905 | /* This function prepares an applet to handle the stats. It can deal with the |
| 3906 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 3907 | * and program a response message if something was unexpected. It cannot fail |
| 3908 | * and always relies on the stats applet to complete the job. It does not touch |
| 3909 | * analysers nor counters, which are left to the caller. It does not touch |
| 3910 | * s->target which is supposed to already point to the stats applet. The caller |
| 3911 | * is expected to have already assigned an appctx to the stream. |
| 3912 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3913 | static int http_handle_stats(struct stream *s, struct channel *req) |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3914 | { |
| 3915 | struct stats_admin_rule *stats_admin_rule; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3916 | struct session *sess = s->sess; |
| 3917 | struct http_txn *txn = s->txn; |
| 3918 | struct http_msg *msg = &txn->req; |
| 3919 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 3920 | const char *h, *lookup, *end; |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 3921 | struct appctx *appctx = __cs_appctx(s->csb); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3922 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3923 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3924 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3925 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 3926 | appctx->st1 = appctx->st2 = 0; |
| 3927 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
Willy Tarreau | 676c29e | 2019-10-09 10:50:01 +0200 | [diff] [blame] | 3928 | appctx->ctx.stats.flags |= uri_auth->flags; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3929 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
| 3930 | if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD)) |
| 3931 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
| 3932 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3933 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 3934 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 3935 | lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len; |
| 3936 | end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3937 | |
| 3938 | for (h = lookup; h <= end - 3; h++) { |
| 3939 | if (memcmp(h, ";up", 3) == 0) { |
| 3940 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
| 3941 | break; |
| 3942 | } |
Amaury Denoyelle | 91e55ea | 2021-02-25 14:46:08 +0100 | [diff] [blame] | 3943 | } |
| 3944 | |
| 3945 | for (h = lookup; h <= end - 9; h++) { |
| 3946 | if (memcmp(h, ";no-maint", 9) == 0) { |
Willy Tarreau | 3e32036 | 2020-10-23 17:28:57 +0200 | [diff] [blame] | 3947 | appctx->ctx.stats.flags |= STAT_HIDE_MAINT; |
| 3948 | break; |
| 3949 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3950 | } |
| 3951 | |
| 3952 | if (uri_auth->refresh) { |
| 3953 | for (h = lookup; h <= end - 10; h++) { |
| 3954 | if (memcmp(h, ";norefresh", 10) == 0) { |
| 3955 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
| 3956 | break; |
| 3957 | } |
| 3958 | } |
| 3959 | } |
| 3960 | |
| 3961 | for (h = lookup; h <= end - 4; h++) { |
| 3962 | if (memcmp(h, ";csv", 4) == 0) { |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 3963 | appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3964 | break; |
| 3965 | } |
| 3966 | } |
| 3967 | |
| 3968 | for (h = lookup; h <= end - 6; h++) { |
| 3969 | if (memcmp(h, ";typed", 6) == 0) { |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 3970 | appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3971 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
| 3972 | break; |
| 3973 | } |
| 3974 | } |
| 3975 | |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 3976 | for (h = lookup; h <= end - 5; h++) { |
| 3977 | if (memcmp(h, ";json", 5) == 0) { |
| 3978 | appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
| 3979 | appctx->ctx.stats.flags |= STAT_FMT_JSON; |
| 3980 | break; |
| 3981 | } |
| 3982 | } |
| 3983 | |
| 3984 | for (h = lookup; h <= end - 12; h++) { |
| 3985 | if (memcmp(h, ";json-schema", 12) == 0) { |
| 3986 | appctx->ctx.stats.flags &= ~STAT_FMT_MASK; |
| 3987 | appctx->ctx.stats.flags |= STAT_JSON_SCHM; |
| 3988 | break; |
| 3989 | } |
| 3990 | } |
| 3991 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3992 | for (h = lookup; h <= end - 8; h++) { |
| 3993 | if (memcmp(h, ";st=", 4) == 0) { |
| 3994 | int i; |
| 3995 | h += 4; |
| 3996 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
| 3997 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 3998 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
| 3999 | appctx->ctx.stats.st_code = i; |
| 4000 | break; |
| 4001 | } |
| 4002 | } |
| 4003 | break; |
| 4004 | } |
| 4005 | } |
| 4006 | |
| 4007 | appctx->ctx.stats.scope_str = 0; |
| 4008 | appctx->ctx.stats.scope_len = 0; |
| 4009 | for (h = lookup; h <= end - 8; h++) { |
| 4010 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 4011 | int itx = 0; |
| 4012 | const char *h2; |
| 4013 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 4014 | const char *err; |
| 4015 | |
| 4016 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 4017 | h2 = h; |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 4018 | appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl); |
| 4019 | while (h < end) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4020 | if (*h == ';' || *h == '&' || *h == ' ') |
| 4021 | break; |
| 4022 | itx++; |
| 4023 | h++; |
| 4024 | } |
| 4025 | |
| 4026 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 4027 | itx = STAT_SCOPE_TXT_MAXLEN; |
| 4028 | appctx->ctx.stats.scope_len = itx; |
| 4029 | |
| 4030 | /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */ |
| 4031 | memcpy(scope_txt, h2, itx); |
| 4032 | scope_txt[itx] = '\0'; |
| 4033 | err = invalid_char(scope_txt); |
| 4034 | if (err) { |
| 4035 | /* bad char in search text => clear scope */ |
| 4036 | appctx->ctx.stats.scope_str = 0; |
| 4037 | appctx->ctx.stats.scope_len = 0; |
| 4038 | } |
| 4039 | break; |
| 4040 | } |
| 4041 | } |
| 4042 | |
| 4043 | /* now check whether we have some admin rules for this request */ |
| 4044 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
| 4045 | int ret = 1; |
| 4046 | |
| 4047 | if (stats_admin_rule->cond) { |
| 4048 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 4049 | ret = acl_pass(ret); |
| 4050 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 4051 | ret = !ret; |
| 4052 | } |
| 4053 | |
| 4054 | if (ret) { |
| 4055 | /* no rule, or the rule matches */ |
| 4056 | appctx->ctx.stats.flags |= STAT_ADMIN; |
| 4057 | break; |
| 4058 | } |
| 4059 | } |
| 4060 | |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4061 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 4062 | appctx->st0 = STAT_HTTP_HEAD; |
| 4063 | else if (txn->meth == HTTP_METH_POST) { |
Christopher Faulet | bd9e842 | 2019-08-15 22:26:48 +0200 | [diff] [blame] | 4064 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4065 | appctx->st0 = STAT_HTTP_POST; |
Christopher Faulet | bd9e842 | 2019-08-15 22:26:48 +0200 | [diff] [blame] | 4066 | if (msg->msg_state < HTTP_MSG_DATA) |
| 4067 | req->analysers |= AN_REQ_HTTP_BODY; |
| 4068 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4069 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4070 | /* POST without admin level */ |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4071 | appctx->ctx.stats.flags &= ~STAT_CHUNKED; |
| 4072 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 4073 | appctx->st0 = STAT_HTTP_LAST; |
| 4074 | } |
| 4075 | } |
| 4076 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4077 | /* Unsupported method */ |
| 4078 | appctx->ctx.stats.flags &= ~STAT_CHUNKED; |
| 4079 | appctx->ctx.stats.st_code = STAT_STATUS_IVAL; |
| 4080 | appctx->st0 = STAT_HTTP_LAST; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | s->task->nice = -32; /* small boost for HTTP statistics */ |
| 4084 | return 1; |
| 4085 | } |
| 4086 | |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4087 | /* This function waits for the message payload at most <time> milliseconds (may |
| 4088 | * be set to TICK_ETERNITY). It stops to wait if at least <bytes> bytes of the |
| 4089 | * payload are received (0 means no limit). It returns HTTP_RULE_* depending on |
| 4090 | * the result: |
| 4091 | * |
| 4092 | * - HTTP_RULE_RES_CONT when conditions are met to stop waiting |
| 4093 | * - HTTP_RULE_RES_YIELD to wait for more data |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 4094 | * - HTTP_RULE_RES_ABRT when a timeout occurred. |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4095 | * - 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] | 4096 | * - HTTP_RULE_RES_ERROR if an internal error occurred |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4097 | * |
Ilya Shipitsin | b2be9a1 | 2021-04-24 13:25:42 +0500 | [diff] [blame] | 4098 | * 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] | 4099 | * to the client, depending on the channel (408 on request side, 504 on response |
| 4100 | * side). All other errors must be handled by the caller. |
| 4101 | */ |
| 4102 | enum rule_result http_wait_for_msg_body(struct stream *s, struct channel *chn, |
| 4103 | unsigned int time, unsigned int bytes) |
| 4104 | { |
| 4105 | struct session *sess = s->sess; |
| 4106 | struct http_txn *txn = s->txn; |
| 4107 | struct http_msg *msg = ((chn->flags & CF_ISRESP) ? &txn->rsp : &txn->req); |
| 4108 | struct htx *htx; |
| 4109 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 4110 | |
| 4111 | htx = htxbuf(&chn->buf); |
| 4112 | |
| 4113 | if (htx->flags & HTX_FL_PARSING_ERROR) { |
| 4114 | ret = HTTP_RULE_RES_BADREQ; |
| 4115 | goto end; |
| 4116 | } |
| 4117 | if (htx->flags & HTX_FL_PROCESSING_ERROR) { |
| 4118 | ret = HTTP_RULE_RES_ERROR; |
| 4119 | goto end; |
| 4120 | } |
| 4121 | |
| 4122 | /* Do nothing for bodyless and CONNECT requests */ |
| 4123 | if (txn->meth == HTTP_METH_CONNECT || (msg->flags & HTTP_MSGF_BODYLESS)) |
| 4124 | goto end; |
| 4125 | |
| 4126 | if (!(chn->flags & CF_ISRESP) && msg->msg_state < HTTP_MSG_DATA) { |
| 4127 | if (http_handle_expect_hdr(s, htx, msg) == -1) { |
| 4128 | ret = HTTP_RULE_RES_ERROR; |
| 4129 | goto end; |
| 4130 | } |
| 4131 | } |
| 4132 | |
| 4133 | msg->msg_state = HTTP_MSG_DATA; |
| 4134 | |
| 4135 | /* Now we're in HTTP_MSG_DATA. We just need to know if all data have |
| 4136 | * been received or if the buffer is full. |
| 4137 | */ |
Christopher Faulet | 7833596 | 2021-09-23 14:46:32 +0200 | [diff] [blame] | 4138 | if ((htx->flags & HTX_FL_EOM) || |
| 4139 | htx_get_tail_type(htx) > HTX_BLK_DATA || |
| 4140 | channel_htx_full(chn, htx, global.tune.maxrewrite) || |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame] | 4141 | cs_rx_blocked_room(chn_prod(chn))) |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4142 | goto end; |
| 4143 | |
| 4144 | if (bytes) { |
| 4145 | struct htx_blk *blk; |
| 4146 | unsigned int len = 0; |
| 4147 | |
| 4148 | for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) { |
| 4149 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 4150 | continue; |
| 4151 | len += htx_get_blksz(blk); |
| 4152 | if (len >= bytes) |
| 4153 | goto end; |
| 4154 | } |
| 4155 | } |
| 4156 | |
| 4157 | if ((chn->flags & CF_READ_TIMEOUT) || tick_is_expired(chn->analyse_exp, now_ms)) { |
| 4158 | if (!(chn->flags & CF_ISRESP)) |
| 4159 | goto abort_req; |
| 4160 | goto abort_res; |
| 4161 | } |
| 4162 | |
| 4163 | /* we get here if we need to wait for more data */ |
| 4164 | if (!(chn->flags & (CF_SHUTR | CF_READ_ERROR))) { |
| 4165 | if (!tick_isset(chn->analyse_exp)) |
| 4166 | chn->analyse_exp = tick_add_ifset(now_ms, time); |
| 4167 | ret = HTTP_RULE_RES_YIELD; |
| 4168 | } |
| 4169 | |
| 4170 | end: |
| 4171 | return ret; |
| 4172 | |
| 4173 | abort_req: |
| 4174 | txn->status = 408; |
| 4175 | if (!(s->flags & SF_ERR_MASK)) |
| 4176 | s->flags |= SF_ERR_CLITO; |
| 4177 | if (!(s->flags & SF_FINST_MASK)) |
| 4178 | s->flags |= SF_FINST_D; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4179 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4180 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4181 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | 021a8e4 | 2021-03-29 10:46:38 +0200 | [diff] [blame] | 4182 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 4183 | ret = HTTP_RULE_RES_ABRT; |
| 4184 | goto end; |
| 4185 | |
| 4186 | abort_res: |
| 4187 | txn->status = 504; |
| 4188 | if (!(s->flags & SF_ERR_MASK)) |
| 4189 | s->flags |= SF_ERR_SRVTO; |
| 4190 | if (!(s->flags & SF_FINST_MASK)) |
| 4191 | s->flags |= SF_FINST_D; |
| 4192 | stream_inc_http_fail_ctr(s); |
| 4193 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 4194 | ret = HTTP_RULE_RES_ABRT; |
| 4195 | goto end; |
| 4196 | } |
| 4197 | |
Christopher Faulet | 0eb32c0 | 2022-04-04 11:06:31 +0200 | [diff] [blame] | 4198 | void http_perform_server_redirect(struct stream *s, struct conn_stream *cs) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4199 | { |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4200 | struct channel *req = &s->req; |
| 4201 | struct channel *res = &s->res; |
| 4202 | struct server *srv; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4203 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4204 | struct htx_sl *sl; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4205 | struct ist path, location; |
| 4206 | unsigned int flags; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4207 | struct http_uri_parser parser; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4208 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4209 | /* |
| 4210 | * Create the location |
| 4211 | */ |
| 4212 | chunk_reset(&trash); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4213 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4214 | /* 1: add the server's prefix */ |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4215 | /* special prefix "/" means don't change URL */ |
| 4216 | srv = __objt_server(s->target); |
| 4217 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 4218 | if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len)) |
| 4219 | return; |
| 4220 | } |
| 4221 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4222 | /* 2: add the request Path */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4223 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 4224 | sl = http_get_stline(htx); |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4225 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4226 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4227 | if (!isttest(path)) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4228 | return; |
| 4229 | |
| 4230 | if (!chunk_memcat(&trash, path.ptr, path.len)) |
| 4231 | return; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4232 | location = ist2(trash.area, trash.data); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4233 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4234 | /* |
| 4235 | * Create the 302 respone |
| 4236 | */ |
| 4237 | htx = htx_from_buf(&res->buf); |
| 4238 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 4239 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4240 | ist("HTTP/1.1"), ist("302"), ist("Found")); |
| 4241 | if (!sl) |
| 4242 | goto fail; |
| 4243 | sl->info.res.status = 302; |
| 4244 | s->txn->status = 302; |
| 4245 | |
| 4246 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) || |
| 4247 | !htx_add_header(htx, ist("Connection"), ist("close")) || |
| 4248 | !htx_add_header(htx, ist("Content-length"), ist("0")) || |
| 4249 | !htx_add_header(htx, ist("Location"), location)) |
| 4250 | goto fail; |
| 4251 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4252 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4253 | goto fail; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4254 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4255 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | c20afb8 | 2020-01-24 19:16:26 +0100 | [diff] [blame] | 4256 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4257 | if (!http_forward_proxy_resp(s, 1)) |
| 4258 | goto fail; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4259 | |
| 4260 | /* return without error. */ |
Christopher Faulet | 0eb32c0 | 2022-04-04 11:06:31 +0200 | [diff] [blame] | 4261 | cs_shutr(cs); |
| 4262 | cs_shutw(cs); |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4263 | s->conn_err_type = STRM_ET_NONE; |
Christopher Faulet | 0eb32c0 | 2022-04-04 11:06:31 +0200 | [diff] [blame] | 4264 | cs->state = CS_ST_CLO; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4265 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4266 | if (!(s->flags & SF_ERR_MASK)) |
| 4267 | s->flags |= SF_ERR_LOCAL; |
| 4268 | if (!(s->flags & SF_FINST_MASK)) |
| 4269 | s->flags |= SF_FINST_C; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4270 | |
| 4271 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 4272 | srv_inc_sess_ctr(srv); |
| 4273 | srv_set_sess_last(srv); |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4274 | return; |
| 4275 | |
| 4276 | fail: |
| 4277 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4278 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4279 | channel_htx_truncate(res, htx); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4280 | } |
| 4281 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4282 | /* This function terminates the request because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4283 | * because an error was triggered during the body forwarding. |
| 4284 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4285 | static void http_end_request(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4286 | { |
| 4287 | struct channel *chn = &s->req; |
| 4288 | struct http_txn *txn = s->txn; |
| 4289 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4290 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4291 | |
Christopher Faulet | b42a8b6 | 2018-11-19 21:59:00 +0100 | [diff] [blame] | 4292 | if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR || |
| 4293 | txn->rsp.msg_state == HTTP_MSG_ERROR)) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4294 | channel_abort(chn); |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4295 | channel_htx_truncate(chn, htxbuf(&chn->buf)); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4296 | goto end; |
| 4297 | } |
| 4298 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4299 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) { |
| 4300 | 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] | 4301 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4302 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4303 | |
| 4304 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4305 | /* No need to read anymore, the request was completely parsed. |
| 4306 | * We can shut the read side unless we want to abort_on_close, |
| 4307 | * or we have a POST request. The issue with POST requests is |
| 4308 | * that some browsers still send a CRLF after the request, and |
| 4309 | * this CRLF must be read so that it does not remain in the kernel |
| 4310 | * buffers, otherwise a close could cause an RST on some systems |
| 4311 | * (eg: Linux). |
| 4312 | */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 4313 | 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] | 4314 | channel_dont_read(chn); |
| 4315 | |
| 4316 | /* if the server closes the connection, we want to immediately react |
| 4317 | * and close the socket to save packets and syscalls. |
| 4318 | */ |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 4319 | s->csb->flags |= CS_FL_NOHALF; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4320 | |
| 4321 | /* In any case we've finished parsing the request so we must |
| 4322 | * disable Nagle when sending data because 1) we're not going |
| 4323 | * to shut this side, and 2) the server is waiting for us to |
| 4324 | * send pending data. |
| 4325 | */ |
| 4326 | chn->flags |= CF_NEVER_WAIT; |
| 4327 | |
Christopher Faulet | d01ce40 | 2019-01-02 17:44:13 +0100 | [diff] [blame] | 4328 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4329 | /* The server has not finished to respond, so we |
| 4330 | * don't want to move in order not to upset it. |
| 4331 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4332 | 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] | 4333 | return; |
| 4334 | } |
| 4335 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4336 | /* When we get here, it means that both the request and the |
| 4337 | * response have finished receiving. Depending on the connection |
| 4338 | * mode, we'll have to wait for the last bytes to leave in either |
| 4339 | * direction, and sometimes for a close to be effective. |
| 4340 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4341 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4342 | /* Tunnel mode will not have any analyser so it needs to |
| 4343 | * poll for reads. |
| 4344 | */ |
| 4345 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4346 | if (b_data(&chn->buf)) { |
| 4347 | DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4348 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4349 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4350 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 4351 | } |
| 4352 | else { |
| 4353 | /* we're not expecting any new data to come for this |
| 4354 | * transaction, so we can close it. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4355 | * |
| 4356 | * However, there is an exception if the response |
| 4357 | * length is undefined. In this case, we need to wait |
| 4358 | * the close from the server. The response will be |
| 4359 | * switched in TUNNEL mode until the end. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4360 | */ |
| 4361 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 4362 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4363 | goto check_channel_flags; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4364 | |
| 4365 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4366 | channel_shutr_now(chn); |
| 4367 | channel_shutw_now(chn); |
| 4368 | } |
| 4369 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4370 | goto check_channel_flags; |
| 4371 | } |
| 4372 | |
| 4373 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4374 | http_msg_closing: |
| 4375 | /* nothing else to forward, just waiting for the output buffer |
| 4376 | * to be empty and for the shutw_now to take effect. |
| 4377 | */ |
| 4378 | if (channel_is_empty(chn)) { |
| 4379 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4380 | goto http_msg_closed; |
| 4381 | } |
| 4382 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4383 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4384 | goto end; |
| 4385 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4386 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4387 | return; |
| 4388 | } |
| 4389 | |
| 4390 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4391 | http_msg_closed: |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4392 | /* if we don't know whether the server will close, we need to hard close */ |
| 4393 | if (txn->rsp.flags & HTTP_MSGF_XFER_LEN) |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 4394 | s->csb->flags |= CS_FL_NOLINGER; /* we want to close ASAP */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4395 | /* 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] | 4396 | if (!(s->be->options & PR_O_ABRT_CLOSE)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4397 | channel_dont_read(chn); |
| 4398 | goto end; |
| 4399 | } |
| 4400 | |
| 4401 | check_channel_flags: |
| 4402 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4403 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4404 | /* if we've just closed an output, let's switch */ |
| 4405 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4406 | goto http_msg_closing; |
| 4407 | } |
| 4408 | |
| 4409 | end: |
| 4410 | chn->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4411 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4412 | chn->flags |= CF_NEVER_WAIT; |
| 4413 | if (HAS_REQ_DATA_FILTERS(s)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4414 | chn->analysers |= AN_REQ_FLT_XFER_DATA; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4415 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4416 | channel_auto_close(chn); |
| 4417 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4418 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4419 | } |
| 4420 | |
| 4421 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4422 | /* This function terminates the response because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4423 | * because an error was triggered during the body forwarding. |
| 4424 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4425 | static void http_end_response(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4426 | { |
| 4427 | struct channel *chn = &s->res; |
| 4428 | struct http_txn *txn = s->txn; |
| 4429 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4430 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4431 | |
Christopher Faulet | b42a8b6 | 2018-11-19 21:59:00 +0100 | [diff] [blame] | 4432 | if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR || |
| 4433 | txn->rsp.msg_state == HTTP_MSG_ERROR)) { |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4434 | channel_htx_truncate(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4435 | channel_abort(&s->req); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4436 | goto end; |
| 4437 | } |
| 4438 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4439 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) { |
| 4440 | 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] | 4441 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4442 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4443 | |
| 4444 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4445 | /* In theory, we don't need to read anymore, but we must |
| 4446 | * still monitor the server connection for a possible close |
| 4447 | * while the request is being uploaded, so we don't disable |
| 4448 | * reading. |
| 4449 | */ |
| 4450 | /* channel_dont_read(chn); */ |
| 4451 | |
| 4452 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4453 | /* The client seems to still be sending data, probably |
| 4454 | * because we got an error response during an upload. |
| 4455 | * We have the choice of either breaking the connection |
| 4456 | * or letting it pass through. Let's do the later. |
| 4457 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4458 | 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] | 4459 | return; |
| 4460 | } |
| 4461 | |
| 4462 | /* When we get here, it means that both the request and the |
| 4463 | * response have finished receiving. Depending on the connection |
| 4464 | * mode, we'll have to wait for the last bytes to leave in either |
| 4465 | * direction, and sometimes for a close to be effective. |
| 4466 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4467 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4468 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4469 | if (b_data(&chn->buf)) { |
| 4470 | DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4471 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4472 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4473 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4474 | } |
| 4475 | else { |
| 4476 | /* we're not expecting any new data to come for this |
| 4477 | * transaction, so we can close it. |
| 4478 | */ |
| 4479 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4480 | channel_shutr_now(chn); |
| 4481 | channel_shutw_now(chn); |
| 4482 | } |
| 4483 | } |
| 4484 | goto check_channel_flags; |
| 4485 | } |
| 4486 | |
| 4487 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4488 | http_msg_closing: |
| 4489 | /* nothing else to forward, just waiting for the output buffer |
| 4490 | * to be empty and for the shutw_now to take effect. |
| 4491 | */ |
| 4492 | if (channel_is_empty(chn)) { |
| 4493 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4494 | goto http_msg_closed; |
| 4495 | } |
| 4496 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4497 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4498 | _HA_ATOMIC_INC(&strm_sess(s)->fe->fe_counters.cli_aborts); |
| 4499 | _HA_ATOMIC_INC(&s->be->be_counters.cli_aborts); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 4500 | if (strm_sess(s)->listener && strm_sess(s)->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4501 | _HA_ATOMIC_INC(&strm_sess(s)->listener->counters->cli_aborts); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4502 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 4503 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.cli_aborts); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4504 | goto end; |
| 4505 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4506 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4507 | return; |
| 4508 | } |
| 4509 | |
| 4510 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4511 | http_msg_closed: |
| 4512 | /* drop any pending data */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4513 | channel_htx_truncate(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4514 | channel_abort(&s->req); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4515 | goto end; |
| 4516 | } |
| 4517 | |
| 4518 | check_channel_flags: |
| 4519 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4520 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4521 | /* if we've just closed an output, let's switch */ |
| 4522 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4523 | goto http_msg_closing; |
| 4524 | } |
| 4525 | |
| 4526 | end: |
| 4527 | chn->analysers &= AN_RES_FLT_END; |
Christopher Faulet | 198ef8b | 2020-12-15 13:32:55 +0100 | [diff] [blame] | 4528 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4529 | chn->flags |= CF_NEVER_WAIT; |
| 4530 | if (HAS_RSP_DATA_FILTERS(s)) |
| 4531 | chn->analysers |= AN_RES_FLT_XFER_DATA; |
| 4532 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4533 | channel_auto_close(chn); |
| 4534 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4535 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4536 | } |
| 4537 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4538 | /* Forward a response generated by HAProxy (error/redirect/return). This |
| 4539 | * function forwards all pending incoming data. If <final> is set to 0, nothing |
| 4540 | * more is performed. It is used for 1xx informational messages. Otherwise, the |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4541 | * transaction is terminated and the request is emptied. On success 1 is |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4542 | * returned. If an error occurred, 0 is returned. If it fails, this function |
| 4543 | * only exits. It is the caller responsibility to do the cleanup. |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4544 | */ |
| 4545 | int http_forward_proxy_resp(struct stream *s, int final) |
| 4546 | { |
| 4547 | struct channel *req = &s->req; |
| 4548 | struct channel *res = &s->res; |
| 4549 | struct htx *htx = htxbuf(&res->buf); |
| 4550 | size_t data; |
| 4551 | |
| 4552 | if (final) { |
| 4553 | htx->flags |= HTX_FL_PROXY_RESP; |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4554 | |
Christopher Faulet | aab1b67 | 2020-11-18 16:44:02 +0100 | [diff] [blame] | 4555 | if (!htx_is_empty(htx) && !http_eval_after_res_rules(s)) |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 4556 | return 0; |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4557 | |
Christopher Faulet | d6c4836 | 2020-10-19 18:01:38 +0200 | [diff] [blame] | 4558 | if (s->txn->meth == HTTP_METH_HEAD) |
| 4559 | htx_skip_msg_payload(htx); |
| 4560 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4561 | channel_auto_read(req); |
| 4562 | channel_abort(req); |
| 4563 | channel_auto_close(req); |
| 4564 | channel_htx_erase(req, htxbuf(&req->buf)); |
| 4565 | |
| 4566 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 4567 | channel_auto_read(res); |
| 4568 | channel_auto_close(res); |
| 4569 | channel_shutr_now(res); |
Christopher Faulet | 1a9db7c | 2020-06-25 15:36:45 +0200 | [diff] [blame] | 4570 | res->flags |= CF_EOI; /* The response is terminated, add EOI */ |
Christopher Faulet | 42432f3 | 2020-11-20 17:43:16 +0100 | [diff] [blame] | 4571 | htxbuf(&res->buf)->flags |= HTX_FL_EOM; /* no more data are expected */ |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4572 | } |
Christopher Faulet | cf6898c | 2020-06-25 15:55:11 +0200 | [diff] [blame] | 4573 | else { |
| 4574 | /* Send ASAP informational messages. Rely on CF_EOI for final |
| 4575 | * response. |
| 4576 | */ |
| 4577 | res->flags |= CF_SEND_DONTWAIT; |
| 4578 | } |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4579 | |
| 4580 | data = htx->data - co_data(res); |
| 4581 | c_adv(res, data); |
| 4582 | htx->first = -1; |
| 4583 | res->total += data; |
| 4584 | return 1; |
| 4585 | } |
| 4586 | |
Christopher Faulet | 0eb32c0 | 2022-04-04 11:06:31 +0200 | [diff] [blame] | 4587 | void http_server_error(struct stream *s, struct conn_stream *cs, int err, |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4588 | int finst, struct http_reply *msg) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4589 | { |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4590 | http_reply_and_close(s, s->txn->status, msg); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4591 | if (!(s->flags & SF_ERR_MASK)) |
| 4592 | s->flags |= err; |
| 4593 | if (!(s->flags & SF_FINST_MASK)) |
| 4594 | s->flags |= finst; |
| 4595 | } |
| 4596 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4597 | 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] | 4598 | { |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4599 | if (!msg) { |
| 4600 | channel_htx_truncate(&s->res, htxbuf(&s->res.buf)); |
| 4601 | goto end; |
| 4602 | } |
| 4603 | |
| 4604 | if (http_reply_message(s, msg) == -1) { |
| 4605 | /* 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] | 4606 | * it is already an internal error. If it was already a "const" |
| 4607 | * 500 error, just fail. |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4608 | */ |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4609 | if (s->txn->status == 500) { |
| 4610 | if (s->txn->flags & TX_CONST_REPLY) |
| 4611 | goto end; |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4612 | s->txn->flags |= TX_CONST_REPLY; |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4613 | } |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4614 | s->txn->status = 500; |
| 4615 | s->txn->http_reply = NULL; |
| 4616 | return http_reply_and_close(s, s->txn->status, http_error_message(s)); |
| 4617 | } |
| 4618 | |
| 4619 | end: |
| 4620 | s->res.wex = tick_add_ifset(now_ms, s->res.wto); |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4621 | |
Christopher Faulet | 2d56500 | 2021-09-10 09:17:50 +0200 | [diff] [blame] | 4622 | /* At this staged, HTTP analysis is finished */ |
| 4623 | s->req.analysers &= AN_REQ_FLT_END; |
| 4624 | s->req.analyse_exp = TICK_ETERNITY; |
| 4625 | |
| 4626 | s->res.analysers &= AN_RES_FLT_END; |
| 4627 | s->res.analyse_exp = TICK_ETERNITY; |
| 4628 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4629 | channel_auto_read(&s->req); |
| 4630 | channel_abort(&s->req); |
| 4631 | channel_auto_close(&s->req); |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4632 | channel_htx_erase(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4633 | channel_auto_read(&s->res); |
| 4634 | channel_auto_close(&s->res); |
| 4635 | channel_shutr_now(&s->res); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4636 | } |
| 4637 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4638 | struct http_reply *http_error_message(struct stream *s) |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4639 | { |
| 4640 | const int msgnum = http_get_status_idx(s->txn->status); |
| 4641 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4642 | if (s->txn->http_reply) |
| 4643 | return s->txn->http_reply; |
| 4644 | else if (s->be->replies[msgnum]) |
| 4645 | return s->be->replies[msgnum]; |
| 4646 | else if (strm_fe(s)->replies[msgnum]) |
| 4647 | return strm_fe(s)->replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4648 | else |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4649 | return &http_err_replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4650 | } |
| 4651 | |
Christopher Faulet | 40e6b55 | 2020-06-25 16:04:50 +0200 | [diff] [blame] | 4652 | /* Produces an HTX message from an http reply. Depending on the http reply type, |
| 4653 | * a, errorfile, an raw file or a log-format string is used. On success, it |
| 4654 | * returns 0. If an error occurs -1 is returned. If it fails, this function only |
| 4655 | * exits. It is the caller responsibility to do the cleanup. |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4656 | */ |
Christopher Faulet | ae43b6c | 2020-05-27 15:24:22 +0200 | [diff] [blame] | 4657 | 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] | 4658 | { |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4659 | struct buffer *errmsg; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4660 | struct htx_sl *sl; |
| 4661 | struct buffer *body = NULL; |
| 4662 | const char *status, *reason, *clen, *ctype; |
| 4663 | unsigned int slflags; |
| 4664 | int ret = 0; |
| 4665 | |
Christopher Faulet | e29a97e | 2020-05-14 14:49:25 +0200 | [diff] [blame] | 4666 | /* |
| 4667 | * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so |
| 4668 | * |
| 4669 | * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled |
| 4670 | * as no payload if NULL. the TXN status code is set with the status |
| 4671 | * of the original reply. |
| 4672 | */ |
| 4673 | |
| 4674 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4675 | if (reply->body.reply) |
| 4676 | reply = reply->body.reply; |
| 4677 | } |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4678 | if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) { |
| 4679 | /* get default error message */ |
| 4680 | if (reply == s->txn->http_reply) |
| 4681 | s->txn->http_reply = NULL; |
| 4682 | reply = http_error_message(s); |
| 4683 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4684 | if (reply->body.reply) |
| 4685 | reply = reply->body.reply; |
| 4686 | } |
| 4687 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4688 | |
| 4689 | if (reply->type == HTTP_REPLY_ERRMSG) { |
| 4690 | /* implicit or explicit error message*/ |
| 4691 | errmsg = reply->body.errmsg; |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4692 | if (errmsg && !b_is_null(errmsg)) { |
Christopher Faulet | 2056736 | 2020-05-15 14:52:49 +0200 | [diff] [blame] | 4693 | if (!htx_copy_msg(htx, errmsg)) |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4694 | goto fail; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4695 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4696 | } |
| 4697 | else { |
| 4698 | /* no payload, file or log-format string */ |
| 4699 | if (reply->type == HTTP_REPLY_RAW) { |
| 4700 | /* file */ |
| 4701 | body = &reply->body.obj; |
| 4702 | } |
| 4703 | else if (reply->type == HTTP_REPLY_LOGFMT) { |
| 4704 | /* log-format string */ |
| 4705 | body = alloc_trash_chunk(); |
| 4706 | if (!body) |
| 4707 | goto fail_alloc; |
| 4708 | body->data = build_logline(s, body->area, body->size, &reply->body.fmt); |
| 4709 | } |
| 4710 | /* else no payload */ |
| 4711 | |
| 4712 | status = ultoa(reply->status); |
| 4713 | reason = http_get_reason(reply->status); |
| 4714 | slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN); |
| 4715 | if (!body || !b_data(body)) |
| 4716 | slflags |= HTX_SL_F_BODYLESS; |
| 4717 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4718 | if (!sl) |
| 4719 | goto fail; |
| 4720 | sl->info.res.status = reply->status; |
| 4721 | |
| 4722 | clen = (body ? ultoa(b_data(body)) : "0"); |
| 4723 | ctype = reply->ctype; |
| 4724 | |
| 4725 | if (!LIST_ISEMPTY(&reply->hdrs)) { |
| 4726 | struct http_reply_hdr *hdr; |
| 4727 | struct buffer *value = alloc_trash_chunk(); |
| 4728 | |
| 4729 | if (!value) |
| 4730 | goto fail; |
| 4731 | |
| 4732 | list_for_each_entry(hdr, &reply->hdrs, list) { |
| 4733 | chunk_reset(value); |
| 4734 | value->data = build_logline(s, value->area, value->size, &hdr->value); |
| 4735 | if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) { |
| 4736 | free_trash_chunk(value); |
| 4737 | goto fail; |
| 4738 | } |
| 4739 | chunk_reset(value); |
| 4740 | } |
| 4741 | free_trash_chunk(value); |
| 4742 | } |
| 4743 | |
| 4744 | if (!htx_add_header(htx, ist("content-length"), ist(clen)) || |
| 4745 | (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) || |
| 4746 | !htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4747 | (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] | 4748 | goto fail; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4749 | |
| 4750 | htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4751 | } |
| 4752 | |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4753 | leave: |
| 4754 | if (reply->type == HTTP_REPLY_LOGFMT) |
| 4755 | free_trash_chunk(body); |
| 4756 | return ret; |
| 4757 | |
| 4758 | fail_alloc: |
| 4759 | if (!(s->flags & SF_ERR_MASK)) |
| 4760 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4761 | /* fall through */ |
| 4762 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4763 | ret = -1; |
| 4764 | goto leave; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4765 | } |
| 4766 | |
| 4767 | /* 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] | 4768 | * occurs -1 is returned and the response channel is truncated, removing this |
| 4769 | * way the faulty reply. This function may fail when the reply is formatted |
| 4770 | * (http_reply_to_htx) or when the reply is forwarded |
| 4771 | * (http_forward_proxy_resp). On the last case, it is because a |
| 4772 | * http-after-response rule fails. |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4773 | */ |
| 4774 | int http_reply_message(struct stream *s, struct http_reply *reply) |
| 4775 | { |
| 4776 | struct channel *res = &s->res; |
| 4777 | struct htx *htx = htx_from_buf(&res->buf); |
| 4778 | |
| 4779 | if (s->txn->status == -1) |
| 4780 | s->txn->status = reply->status; |
| 4781 | channel_htx_truncate(res, htx); |
| 4782 | |
| 4783 | if (http_reply_to_htx(s, htx, reply) == -1) |
| 4784 | goto fail; |
| 4785 | |
| 4786 | htx_to_buf(htx, &s->res.buf); |
| 4787 | if (!http_forward_proxy_resp(s, 1)) |
| 4788 | goto fail; |
| 4789 | return 0; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4790 | |
| 4791 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4792 | channel_htx_truncate(res, htx); |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4793 | if (!(s->flags & SF_ERR_MASK)) |
| 4794 | s->flags |= SF_ERR_PRXCOND; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4795 | return -1; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4796 | } |
| 4797 | |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4798 | /* 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] | 4799 | * that the server side is closed. Note that err_type is actually a |
| 4800 | * bitmask, where almost only aborts may be cumulated with other |
| 4801 | * values. We consider that aborted operations are more important |
| 4802 | * than timeouts or errors due to the fact that nobody else in the |
| 4803 | * logs might explain incomplete retries. All others should avoid |
| 4804 | * being cumulated. It should normally not be possible to have multiple |
| 4805 | * aborts at once, but just in case, the first one in sequence is reported. |
| 4806 | * Note that connection errors appearing on the second request of a keep-alive |
| 4807 | * connection are not reported since this allows the client to retry. |
| 4808 | */ |
Christopher Faulet | 0eb32c0 | 2022-04-04 11:06:31 +0200 | [diff] [blame] | 4809 | void http_return_srv_error(struct stream *s, struct conn_stream *cs) |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4810 | { |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4811 | int err_type = s->conn_err_type; |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4812 | |
| 4813 | /* set s->txn->status for http_error_message(s) */ |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4814 | if (err_type & STRM_ET_QUEUE_ABRT) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4815 | s->txn->status = -1; |
Christopher Faulet | 0eb32c0 | 2022-04-04 11:06:31 +0200 | [diff] [blame] | 4816 | http_server_error(s, cs, SF_ERR_CLICL, SF_FINST_Q, NULL); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4817 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4818 | else if (err_type & STRM_ET_CONN_ABRT) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4819 | s->txn->status = -1; |
Christopher Faulet | 0eb32c0 | 2022-04-04 11:06:31 +0200 | [diff] [blame] | 4820 | http_server_error(s, cs, SF_ERR_CLICL, SF_FINST_C, NULL); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4821 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4822 | else if (err_type & STRM_ET_QUEUE_TO) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4823 | s->txn->status = 503; |
Christopher Faulet | 0eb32c0 | 2022-04-04 11:06:31 +0200 | [diff] [blame] | 4824 | http_server_error(s, cs, SF_ERR_SRVTO, SF_FINST_Q, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4825 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4826 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4827 | else if (err_type & STRM_ET_QUEUE_ERR) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4828 | s->txn->status = 503; |
Christopher Faulet | 0eb32c0 | 2022-04-04 11:06:31 +0200 | [diff] [blame] | 4829 | http_server_error(s, cs, SF_ERR_SRVCL, SF_FINST_Q, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4830 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4831 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4832 | else if (err_type & STRM_ET_CONN_TO) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4833 | s->txn->status = 503; |
Christopher Faulet | 0eb32c0 | 2022-04-04 11:06:31 +0200 | [diff] [blame] | 4834 | http_server_error(s, cs, SF_ERR_SRVTO, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4835 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4836 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4837 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4838 | else if (err_type & STRM_ET_CONN_ERR) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4839 | s->txn->status = 503; |
Christopher Faulet | 0eb32c0 | 2022-04-04 11:06:31 +0200 | [diff] [blame] | 4840 | http_server_error(s, cs, SF_ERR_SRVCL, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4841 | (s->flags & SF_SRV_REUSED) ? NULL : |
| 4842 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4843 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4844 | else if (err_type & STRM_ET_CONN_RES) { |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4845 | s->txn->status = 503; |
Christopher Faulet | 0eb32c0 | 2022-04-04 11:06:31 +0200 | [diff] [blame] | 4846 | http_server_error(s, cs, SF_ERR_RESOURCE, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4847 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4848 | http_error_message(s)); |
Christopher Faulet | 5e702fc | 2021-06-02 14:07:24 +0200 | [diff] [blame] | 4849 | } |
Christopher Faulet | 50264b4 | 2022-03-30 19:39:30 +0200 | [diff] [blame] | 4850 | else { /* STRM_ET_CONN_OTHER and others */ |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4851 | s->txn->status = 500; |
Christopher Faulet | 0eb32c0 | 2022-04-04 11:06:31 +0200 | [diff] [blame] | 4852 | http_server_error(s, cs, SF_ERR_INTERNAL, SF_FINST_C, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4853 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4854 | } |
| 4855 | } |
| 4856 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4857 | |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4858 | /* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0 |
| 4859 | * on success and -1 on error. |
| 4860 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4861 | 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] | 4862 | { |
| 4863 | /* If we have HTTP/1.1 message with a body and Expect: 100-continue, |
| 4864 | * then we must send an HTTP/1.1 100 Continue intermediate response. |
| 4865 | */ |
| 4866 | if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) && |
| 4867 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
| 4868 | struct ist hdr = { .ptr = "Expect", .len = 6 }; |
| 4869 | struct http_hdr_ctx ctx; |
| 4870 | |
| 4871 | ctx.blk = NULL; |
| 4872 | /* Expect is allowed in 1.1, look for it */ |
| 4873 | if (http_find_header(htx, hdr, &ctx, 0) && |
| 4874 | unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4875 | if (http_reply_100_continue(s) == -1) |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4876 | return -1; |
| 4877 | http_remove_header(htx, &ctx); |
| 4878 | } |
| 4879 | } |
| 4880 | return 0; |
| 4881 | } |
| 4882 | |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4883 | /* Send a 100-Continue response to the client. It returns 0 on success and -1 |
| 4884 | * on error. The response channel is updated accordingly. |
| 4885 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4886 | static int http_reply_100_continue(struct stream *s) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4887 | { |
| 4888 | struct channel *res = &s->res; |
| 4889 | struct htx *htx = htx_from_buf(&res->buf); |
| 4890 | struct htx_sl *sl; |
| 4891 | unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11| |
| 4892 | HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4893 | |
| 4894 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4895 | ist("HTTP/1.1"), ist("100"), ist("Continue")); |
| 4896 | if (!sl) |
| 4897 | goto fail; |
| 4898 | sl->info.res.status = 100; |
| 4899 | |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 4900 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4901 | goto fail; |
| 4902 | |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4903 | if (!http_forward_proxy_resp(s, 0)) |
| 4904 | goto fail; |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4905 | return 0; |
| 4906 | |
| 4907 | fail: |
| 4908 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4909 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4910 | channel_htx_truncate(res, htx); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4911 | return -1; |
| 4912 | } |
| 4913 | |
Christopher Faulet | 12c51e2 | 2018-11-28 15:59:42 +0100 | [diff] [blame] | 4914 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4915 | /* |
| 4916 | * Capture headers from message <htx> according to header list <cap_hdr>, and |
| 4917 | * fill the <cap> pointers appropriately. |
| 4918 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4919 | 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] | 4920 | { |
| 4921 | struct cap_hdr *h; |
| 4922 | int32_t pos; |
| 4923 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4924 | 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] | 4925 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4926 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4927 | struct ist n, v; |
| 4928 | |
| 4929 | if (type == HTX_BLK_EOH) |
| 4930 | break; |
| 4931 | if (type != HTX_BLK_HDR) |
| 4932 | continue; |
| 4933 | |
| 4934 | n = htx_get_blk_name(htx, blk); |
| 4935 | |
| 4936 | for (h = cap_hdr; h; h = h->next) { |
| 4937 | if (h->namelen && (h->namelen == n.len) && |
| 4938 | (strncasecmp(n.ptr, h->name, h->namelen) == 0)) { |
| 4939 | if (cap[h->index] == NULL) |
| 4940 | cap[h->index] = |
| 4941 | pool_alloc(h->pool); |
| 4942 | |
| 4943 | if (cap[h->index] == NULL) { |
| 4944 | ha_alert("HTTP capture : out of memory.\n"); |
| 4945 | break; |
| 4946 | } |
| 4947 | |
| 4948 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 4949 | v = isttrim(v, h->len); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4950 | |
| 4951 | memcpy(cap[h->index], v.ptr, v.len); |
| 4952 | cap[h->index][v.len]=0; |
| 4953 | } |
| 4954 | } |
| 4955 | } |
| 4956 | } |
| 4957 | |
Christopher Faulet | 0b6bdc5 | 2018-10-24 11:05:36 +0200 | [diff] [blame] | 4958 | /* Delete a value in a header between delimiters <from> and <next>. The header |
| 4959 | * itself is delimited by <start> and <end> pointers. The number of characters |
| 4960 | * displaced is returned, and the pointer to the first delimiter is updated if |
| 4961 | * required. The function tries as much as possible to respect the following |
| 4962 | * principles : |
| 4963 | * - replace <from> delimiter by the <next> one unless <from> points to <start>, |
| 4964 | * in which case <next> is simply removed |
| 4965 | * - set exactly one space character after the new first delimiter, unless there |
| 4966 | * are not enough characters in the block being moved to do so. |
| 4967 | * - remove unneeded spaces before the previous delimiter and after the new |
| 4968 | * one. |
| 4969 | * |
| 4970 | * It is the caller's responsibility to ensure that : |
| 4971 | * - <from> points to a valid delimiter or <start> ; |
| 4972 | * - <next> points to a valid delimiter or <end> ; |
| 4973 | * - there are non-space chars before <from>. |
| 4974 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4975 | 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] | 4976 | { |
| 4977 | char *prev = *from; |
| 4978 | |
| 4979 | if (prev == start) { |
| 4980 | /* We're removing the first value. eat the semicolon, if <next> |
| 4981 | * is lower than <end> */ |
| 4982 | if (next < end) |
| 4983 | next++; |
| 4984 | |
| 4985 | while (next < end && HTTP_IS_SPHT(*next)) |
| 4986 | next++; |
| 4987 | } |
| 4988 | else { |
| 4989 | /* Remove useless spaces before the old delimiter. */ |
| 4990 | while (HTTP_IS_SPHT(*(prev-1))) |
| 4991 | prev--; |
| 4992 | *from = prev; |
| 4993 | |
| 4994 | /* copy the delimiter and if possible a space if we're |
| 4995 | * not at the end of the line. |
| 4996 | */ |
| 4997 | if (next < end) { |
| 4998 | *prev++ = *next++; |
| 4999 | if (prev + 1 < next) |
| 5000 | *prev++ = ' '; |
| 5001 | while (next < end && HTTP_IS_SPHT(*next)) |
| 5002 | next++; |
| 5003 | } |
| 5004 | } |
| 5005 | memmove(prev, next, end - next); |
| 5006 | return (prev - next); |
| 5007 | } |
| 5008 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5009 | |
| 5010 | /* 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] | 5011 | * 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] | 5012 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5013 | 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] | 5014 | { |
| 5015 | struct ist dst = ist2(str, 0); |
| 5016 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5017 | if (istcat(&dst, htx_sl_req_meth(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5018 | goto end; |
| 5019 | if (dst.len + 1 > len) |
| 5020 | goto end; |
| 5021 | dst.ptr[dst.len++] = ' '; |
| 5022 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5023 | if (istcat(&dst, htx_sl_req_uri(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5024 | goto end; |
| 5025 | if (dst.len + 1 > len) |
| 5026 | goto end; |
| 5027 | dst.ptr[dst.len++] = ' '; |
| 5028 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5029 | istcat(&dst, htx_sl_req_vsn(sl), len); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5030 | end: |
| 5031 | return dst.len; |
| 5032 | } |
| 5033 | |
| 5034 | /* |
| 5035 | * Print a debug line with a start line. |
| 5036 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5037 | 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] | 5038 | { |
| 5039 | struct session *sess = strm_sess(s); |
| 5040 | int max; |
| 5041 | |
| 5042 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 5043 | dir, |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 5044 | objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1, |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 5045 | cs_conn(s->csb) ? (unsigned short)(__cs_conn(s->csb))->handle.fd : -1); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5046 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5047 | max = HTX_SL_P1_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5048 | UBOUND(max, trash.size - trash.data - 3); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5049 | chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5050 | trash.area[trash.data++] = ' '; |
| 5051 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5052 | max = HTX_SL_P2_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5053 | UBOUND(max, trash.size - trash.data - 2); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5054 | chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5055 | trash.area[trash.data++] = ' '; |
| 5056 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5057 | max = HTX_SL_P3_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5058 | UBOUND(max, trash.size - trash.data - 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5059 | chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5060 | trash.area[trash.data++] = '\n'; |
| 5061 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 5062 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5063 | } |
| 5064 | |
| 5065 | /* |
| 5066 | * Print a debug line with a header. |
| 5067 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5068 | 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] | 5069 | { |
| 5070 | struct session *sess = strm_sess(s); |
| 5071 | int max; |
| 5072 | |
| 5073 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 5074 | dir, |
Willy Tarreau | 88bc800 | 2021-12-06 07:01:02 +0000 | [diff] [blame] | 5075 | objt_conn(sess->origin) ? (unsigned short)__objt_conn(sess->origin)->handle.fd : -1, |
Christopher Faulet | 693b23b | 2022-02-28 09:09:05 +0100 | [diff] [blame] | 5076 | cs_conn(s->csb) ? (unsigned short)(__cs_conn(s->csb))->handle.fd : -1); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5077 | |
| 5078 | max = n.len; |
| 5079 | UBOUND(max, trash.size - trash.data - 3); |
| 5080 | chunk_memcat(&trash, n.ptr, max); |
| 5081 | trash.area[trash.data++] = ':'; |
| 5082 | trash.area[trash.data++] = ' '; |
| 5083 | |
| 5084 | max = v.len; |
| 5085 | UBOUND(max, trash.size - trash.data - 1); |
| 5086 | chunk_memcat(&trash, v.ptr, max); |
| 5087 | trash.area[trash.data++] = '\n'; |
| 5088 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 5089 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5090 | } |
| 5091 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5092 | void http_txn_reset_req(struct http_txn *txn) |
| 5093 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 5094 | txn->req.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5095 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 5096 | } |
| 5097 | |
| 5098 | void http_txn_reset_res(struct http_txn *txn) |
| 5099 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 5100 | txn->rsp.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5101 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 5102 | } |
| 5103 | |
| 5104 | /* |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5105 | * Create and initialize a new HTTP transaction for stream <s>. This should be |
| 5106 | * used before processing any new request. It returns the transaction or NLULL |
| 5107 | * on error. |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5108 | */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5109 | struct http_txn *http_create_txn(struct stream *s) |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5110 | { |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5111 | struct http_txn *txn; |
Christopher Faulet | 95a61e8 | 2021-12-22 14:22:03 +0100 | [diff] [blame] | 5112 | struct conn_stream *cs = s->csf; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5113 | |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5114 | txn = pool_alloc(pool_head_http_txn); |
| 5115 | if (!txn) |
| 5116 | return NULL; |
| 5117 | s->txn = txn; |
| 5118 | |
Christopher Faulet | e9e4820 | 2022-03-22 18:13:29 +0100 | [diff] [blame] | 5119 | txn->flags = ((cs && cs->endp->flags & CS_EP_NOT_FIRST) ? TX_NOT_FIRST : 0); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5120 | txn->status = -1; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 5121 | txn->http_reply = NULL; |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 5122 | txn->l7_buffer = BUF_NULL; |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 5123 | write_u32(txn->cache_hash, 0); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5124 | |
| 5125 | txn->cookie_first_date = 0; |
| 5126 | txn->cookie_last_date = 0; |
| 5127 | |
| 5128 | txn->srv_cookie = NULL; |
| 5129 | txn->cli_cookie = NULL; |
| 5130 | txn->uri = NULL; |
| 5131 | |
| 5132 | http_txn_reset_req(txn); |
| 5133 | http_txn_reset_res(txn); |
| 5134 | |
| 5135 | txn->req.chn = &s->req; |
| 5136 | txn->rsp.chn = &s->res; |
| 5137 | |
| 5138 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
| 5139 | |
Willy Tarreau | b7bfcb3 | 2021-08-31 08:13:25 +0200 | [diff] [blame] | 5140 | vars_init_head(&s->vars_txn, SCOPE_TXN); |
| 5141 | vars_init_head(&s->vars_reqres, SCOPE_REQ); |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5142 | |
| 5143 | return txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5144 | } |
| 5145 | |
| 5146 | /* to be used at the end of a transaction */ |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5147 | void http_destroy_txn(struct stream *s) |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5148 | { |
| 5149 | struct http_txn *txn = s->txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5150 | |
| 5151 | /* these ones will have been dynamically allocated */ |
| 5152 | pool_free(pool_head_requri, txn->uri); |
| 5153 | pool_free(pool_head_capture, txn->cli_cookie); |
| 5154 | pool_free(pool_head_capture, txn->srv_cookie); |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5155 | pool_free(pool_head_uniqueid, s->unique_id.ptr); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5156 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5157 | s->unique_id = IST_NULL; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5158 | txn->uri = NULL; |
| 5159 | txn->srv_cookie = NULL; |
| 5160 | txn->cli_cookie = NULL; |
| 5161 | |
Christopher Faulet | 5939925 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 5162 | if (!LIST_ISEMPTY(&s->vars_txn.head)) |
| 5163 | vars_prune(&s->vars_txn, s->sess, s); |
| 5164 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 5165 | vars_prune(&s->vars_reqres, s->sess, s); |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5166 | |
Christopher Faulet | e05bf9e | 2022-03-29 15:23:40 +0200 | [diff] [blame] | 5167 | b_free(&txn->l7_buffer); |
| 5168 | |
Christopher Faulet | 75f619a | 2021-03-08 19:12:58 +0100 | [diff] [blame] | 5169 | pool_free(pool_head_http_txn, txn); |
| 5170 | s->txn = NULL; |
Christopher Faulet | 5939925 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 5171 | } |
| 5172 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5173 | |
| 5174 | DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5175 | |
Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 5176 | __attribute__((constructor)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5177 | static void __http_protocol_init(void) |
Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 5178 | { |
| 5179 | } |
| 5180 | |
| 5181 | |
| 5182 | /* |
| 5183 | * Local variables: |
| 5184 | * c-indent-level: 8 |
| 5185 | * c-basic-offset: 8 |
| 5186 | * End: |
| 5187 | */ |