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