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