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