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