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