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