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