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