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