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