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