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