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