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