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