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