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> |
| 22 | #include <proto/channel.h> |
| 23 | #include <proto/checks.h> |
| 24 | #include <proto/connection.h> |
| 25 | #include <proto/filters.h> |
| 26 | #include <proto/hdr_idx.h> |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 27 | #include <proto/http_htx.h> |
| 28 | #include <proto/htx.h> |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 29 | #include <proto/log.h> |
| 30 | #include <proto/proto_http.h> |
| 31 | #include <proto/proxy.h> |
| 32 | #include <proto/stream.h> |
| 33 | #include <proto/stream_interface.h> |
| 34 | #include <proto/stats.h> |
| 35 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 36 | |
| 37 | static void htx_end_request(struct stream *s); |
| 38 | static void htx_end_response(struct stream *s); |
| 39 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 40 | 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] | 41 | 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] | 42 | 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^] | 43 | 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] | 44 | static void htx_debug_stline(const char *dir, struct stream *s, const union h1_sl sl); |
| 45 | static void htx_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v); |
| 46 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 47 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 48 | * processing can continue on next analysers, or zero if it either needs more |
| 49 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 50 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers |
| 51 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 52 | * abort. |
| 53 | */ |
| 54 | int htx_wait_for_request(struct stream *s, struct channel *req, int an_bit) |
| 55 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 56 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 57 | /* |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 58 | * We will analyze a complete HTTP request to check the its syntax. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 59 | * |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 60 | * Once the start line and all headers are received, we may perform a |
| 61 | * capture of the error (if any), and we will set a few fields. We also |
| 62 | * check for monitor-uri, logging and finally headers capture. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 63 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 64 | struct session *sess = s->sess; |
| 65 | struct http_txn *txn = s->txn; |
| 66 | struct http_msg *msg = &txn->req; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 67 | struct htx *htx; |
| 68 | union h1_sl sl; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 69 | |
| 70 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
| 71 | now_ms, __FUNCTION__, |
| 72 | s, |
| 73 | req, |
| 74 | req->rex, req->wex, |
| 75 | req->flags, |
| 76 | ci_data(req), |
| 77 | req->analysers); |
| 78 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 79 | htx = htx_from_buf(&req->buf); |
| 80 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 81 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 82 | s->srv_error = http_return_srv_error; |
| 83 | |
| 84 | /* If there is data available for analysis, log the end of the idle time. */ |
| 85 | if (c_data(req) && s->logs.t_idle == -1) |
| 86 | s->logs.t_idle = tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake; |
| 87 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 88 | /* |
| 89 | * Now we quickly check if we have found a full valid request. |
| 90 | * If not so, we check the FD and buffer states before leaving. |
| 91 | * A full request is indicated by the fact that we have seen |
| 92 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
| 93 | * requests are checked first. When waiting for a second request |
| 94 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 95 | * we note the error in the stream flags but don't set any state. |
| 96 | * Since the error will be noted there, it will not be counted by |
| 97 | * process_stream() as a frontend error. |
| 98 | * Last, we may increase some tracked counters' http request errors on |
| 99 | * the cases that are deliberately the client's fault. For instance, |
| 100 | * a timeout or connection reset is not counted as an error. However |
| 101 | * a bad request is. |
| 102 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 103 | if (unlikely(htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH)) { |
| 104 | /* 1: have we encountered a read error ? */ |
| 105 | if (req->flags & CF_READ_ERROR) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 106 | if (!(s->flags & SF_ERR_MASK)) |
| 107 | s->flags |= SF_ERR_CLICL; |
| 108 | |
| 109 | if (txn->flags & TX_WAIT_NEXT_RQ) |
| 110 | goto failed_keep_alive; |
| 111 | |
| 112 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 113 | goto failed_keep_alive; |
| 114 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 115 | stream_inc_http_err_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 116 | stream_inc_http_req_ctr(s); |
| 117 | proxy_inc_fe_req_ctr(sess->fe); |
| 118 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
| 119 | if (sess->listener->counters) |
| 120 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
| 121 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 122 | txn->status = 400; |
| 123 | msg->err_state = msg->msg_state; |
| 124 | msg->msg_state = HTTP_MSG_ERROR; |
| 125 | htx_reply_and_close(s, txn->status, NULL); |
| 126 | req->analysers &= AN_REQ_FLT_END; |
| 127 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 128 | if (!(s->flags & SF_FINST_MASK)) |
| 129 | s->flags |= SF_FINST_R; |
| 130 | return 0; |
| 131 | } |
| 132 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 133 | /* 2: has the read timeout expired ? */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 134 | else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
| 135 | if (!(s->flags & SF_ERR_MASK)) |
| 136 | s->flags |= SF_ERR_CLITO; |
| 137 | |
| 138 | if (txn->flags & TX_WAIT_NEXT_RQ) |
| 139 | goto failed_keep_alive; |
| 140 | |
| 141 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 142 | goto failed_keep_alive; |
| 143 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 144 | stream_inc_http_err_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 145 | stream_inc_http_req_ctr(s); |
| 146 | proxy_inc_fe_req_ctr(sess->fe); |
| 147 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
| 148 | if (sess->listener->counters) |
| 149 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
| 150 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 151 | txn->status = 408; |
| 152 | msg->err_state = msg->msg_state; |
| 153 | msg->msg_state = HTTP_MSG_ERROR; |
| 154 | htx_reply_and_close(s, txn->status, http_error_message(s)); |
| 155 | req->analysers &= AN_REQ_FLT_END; |
| 156 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 157 | if (!(s->flags & SF_FINST_MASK)) |
| 158 | s->flags |= SF_FINST_R; |
| 159 | return 0; |
| 160 | } |
| 161 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 162 | /* 3: have we encountered a close ? */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 163 | else if (req->flags & CF_SHUTR) { |
| 164 | if (!(s->flags & SF_ERR_MASK)) |
| 165 | s->flags |= SF_ERR_CLICL; |
| 166 | |
| 167 | if (txn->flags & TX_WAIT_NEXT_RQ) |
| 168 | goto failed_keep_alive; |
| 169 | |
| 170 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 171 | goto failed_keep_alive; |
| 172 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 173 | stream_inc_http_err_ctr(s); |
| 174 | stream_inc_http_req_ctr(s); |
| 175 | proxy_inc_fe_req_ctr(sess->fe); |
| 176 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
| 177 | if (sess->listener->counters) |
| 178 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
| 179 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 180 | txn->status = 400; |
| 181 | msg->err_state = msg->msg_state; |
| 182 | msg->msg_state = HTTP_MSG_ERROR; |
| 183 | htx_reply_and_close(s, txn->status, http_error_message(s)); |
| 184 | req->analysers &= AN_REQ_FLT_END; |
| 185 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 186 | if (!(s->flags & SF_FINST_MASK)) |
| 187 | s->flags |= SF_FINST_R; |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | channel_dont_connect(req); |
| 192 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 193 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
| 194 | #ifdef TCP_QUICKACK |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 195 | if (sess->listener->options & LI_O_NOQUICKACK && htx_is_not_empty(htx) && |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 196 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
| 197 | /* We need more data, we have to re-enable quick-ack in case we |
| 198 | * previously disabled it, otherwise we might cause the client |
| 199 | * to delay next data. |
| 200 | */ |
| 201 | setsockopt(__objt_conn(sess->origin)->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
| 202 | } |
| 203 | #endif |
| 204 | |
| 205 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 206 | /* If the client starts to talk, let's fall back to |
| 207 | * request timeout processing. |
| 208 | */ |
| 209 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 210 | req->analyse_exp = TICK_ETERNITY; |
| 211 | } |
| 212 | |
| 213 | /* just set the request timeout once at the beginning of the request */ |
| 214 | if (!tick_isset(req->analyse_exp)) { |
| 215 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 216 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 217 | tick_isset(s->be->timeout.httpka)) |
| 218 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 219 | else |
| 220 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 221 | } |
| 222 | |
| 223 | /* we're not ready yet */ |
| 224 | return 0; |
| 225 | |
| 226 | failed_keep_alive: |
| 227 | /* Here we process low-level errors for keep-alive requests. In |
| 228 | * short, if the request is not the first one and it experiences |
| 229 | * a timeout, read error or shutdown, we just silently close so |
| 230 | * that the client can try again. |
| 231 | */ |
| 232 | txn->status = 0; |
| 233 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 234 | req->analysers &= AN_REQ_FLT_END; |
| 235 | s->logs.logwait = 0; |
| 236 | s->logs.level = 0; |
| 237 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 238 | htx_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 239 | return 0; |
| 240 | } |
| 241 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 242 | msg->msg_state = HTTP_MSG_BODY; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 243 | stream_inc_http_req_ctr(s); |
| 244 | proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */ |
| 245 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 246 | /* kill the pending keep-alive timeout */ |
| 247 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 248 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 249 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 250 | /* 0: we might have to print this header in debug mode */ |
| 251 | if (unlikely((global.mode & MODE_DEBUG) && |
| 252 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 253 | int32_t pos; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 254 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 255 | htx_debug_stline("clireq", s, http_find_stline(htx)); |
| 256 | |
| 257 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 258 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 259 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 260 | |
| 261 | if (type == HTX_BLK_EOH) |
| 262 | break; |
| 263 | if (type != HTX_BLK_HDR) |
| 264 | continue; |
| 265 | |
| 266 | htx_debug_hdr("clihdr", s, |
| 267 | htx_get_blk_name(htx, blk), |
| 268 | htx_get_blk_value(htx, blk)); |
| 269 | } |
| 270 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 271 | |
| 272 | /* |
| 273 | * 1: identify the method |
| 274 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 275 | sl = http_find_stline(htx); |
| 276 | txn->meth = sl.rq.meth; |
| 277 | msg->flags |= HTTP_MSGF_XFER_LEN; |
| 278 | |
| 279 | /* ... and check if the request is HTTP/1.1 or above */ |
| 280 | if ((sl.rq.v.len == 8) && |
| 281 | ((*(sl.rq.v.ptr + 5) > '1') || |
| 282 | ((*(sl.rq.v.ptr + 5) == '1') && (*(sl.rq.v.ptr + 7) >= '1')))) |
| 283 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 284 | |
| 285 | /* we can make use of server redirect on GET and HEAD */ |
| 286 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 287 | s->flags |= SF_REDIRECTABLE; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 288 | 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] | 289 | /* PRI is reserved for the HTTP/2 preface */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 290 | goto return_bad_req; |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * 2: check if the URI matches the monitor_uri. |
| 295 | * We have to do this for every request which gets in, because |
| 296 | * the monitor-uri is defined by the frontend. |
| 297 | */ |
| 298 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 299 | 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] | 300 | /* |
| 301 | * We have found the monitor URI |
| 302 | */ |
| 303 | struct acl_cond *cond; |
| 304 | |
| 305 | s->flags |= SF_MONITOR; |
| 306 | HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
| 307 | |
| 308 | /* Check if we want to fail this monitor request or not */ |
| 309 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
| 310 | int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 311 | |
| 312 | ret = acl_pass(ret); |
| 313 | if (cond->pol == ACL_COND_UNLESS) |
| 314 | ret = !ret; |
| 315 | |
| 316 | if (ret) { |
| 317 | /* we fail this request, let's return 503 service unavail */ |
| 318 | txn->status = 503; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 319 | htx_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 320 | if (!(s->flags & SF_ERR_MASK)) |
| 321 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
| 322 | goto return_prx_cond; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /* nothing to fail, let's reply normaly */ |
| 327 | txn->status = 200; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 328 | htx_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 329 | if (!(s->flags & SF_ERR_MASK)) |
| 330 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
| 331 | goto return_prx_cond; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 336 | * Note: we cannot log anymore if the request has been |
| 337 | * classified as invalid. |
| 338 | */ |
| 339 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 340 | /* we have a complete HTTP request that we must log */ |
| 341 | if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 342 | size_t len; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 343 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 344 | len = htx_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1); |
| 345 | txn->uri[len] = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 346 | |
| 347 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
| 348 | s->do_log(s); |
| 349 | } else { |
| 350 | ha_alert("HTTP logging : out of memory.\n"); |
| 351 | } |
| 352 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 353 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 354 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 355 | * we have what looks like a proxied connection instead of a connection, |
| 356 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 357 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 358 | * happen to do that despite being non-standard :-( |
| 359 | * We consider that a request not beginning with either '/' or '*' is |
| 360 | * a proxied connection, which covers both "scheme://location" and |
| 361 | * CONNECT ip:port. |
| 362 | */ |
| 363 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 364 | *(sl.rq.u.ptr) != '/' && *(sl.rq.u.ptr) != '*') |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 365 | txn->flags |= TX_USE_PX_CONN; |
| 366 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 367 | /* 5: we may need to capture headers */ |
| 368 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 369 | htx_capture_headers(htx, s->req_cap, sess->fe->req_cap); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 370 | |
| 371 | /* Until set to anything else, the connection mode is set as Keep-Alive. It will |
| 372 | * only change if both the request and the config reference something else. |
| 373 | * Option httpclose by itself sets tunnel mode where headers are mangled. |
| 374 | * However, if another mode is set, it will affect it (eg: server-close/ |
| 375 | * keep-alive + httpclose = close). Note that we avoid to redo the same work |
| 376 | * if FE and BE have the same settings (common). The method consists in |
| 377 | * checking if options changed between the two calls (implying that either |
| 378 | * one is non-null, or one of them is non-null and we are there for the first |
| 379 | * time. |
| 380 | */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 381 | 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] | 382 | htx_adjust_conn_mode(s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 383 | |
| 384 | /* we may have to wait for the request's body */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 385 | if (s->be->options & PR_O_WREQ_BODY) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 386 | req->analysers |= AN_REQ_HTTP_BODY; |
| 387 | |
| 388 | /* |
| 389 | * RFC7234#4: |
| 390 | * A cache MUST write through requests with methods |
| 391 | * that are unsafe (Section 4.2.1 of [RFC7231]) to |
| 392 | * the origin server; i.e., a cache is not allowed |
| 393 | * to generate a reply to such a request before |
| 394 | * having forwarded the request and having received |
| 395 | * a corresponding response. |
| 396 | * |
| 397 | * RFC7231#4.2.1: |
| 398 | * Of the request methods defined by this |
| 399 | * specification, the GET, HEAD, OPTIONS, and TRACE |
| 400 | * methods are defined to be safe. |
| 401 | */ |
| 402 | if (likely(txn->meth == HTTP_METH_GET || |
| 403 | txn->meth == HTTP_METH_HEAD || |
| 404 | txn->meth == HTTP_METH_OPTIONS || |
| 405 | txn->meth == HTTP_METH_TRACE)) |
| 406 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 407 | |
| 408 | /* end of job, return OK */ |
| 409 | req->analysers &= ~an_bit; |
| 410 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 411 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 412 | return 1; |
| 413 | |
| 414 | return_bad_req: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 415 | txn->status = 400; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 416 | txn->req.err_state = txn->req.msg_state; |
| 417 | txn->req.msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 418 | htx_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 419 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
| 420 | if (sess->listener->counters) |
| 421 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
| 422 | |
| 423 | return_prx_cond: |
| 424 | if (!(s->flags & SF_ERR_MASK)) |
| 425 | s->flags |= SF_ERR_PRXCOND; |
| 426 | if (!(s->flags & SF_FINST_MASK)) |
| 427 | s->flags |= SF_FINST_R; |
| 428 | |
| 429 | req->analysers &= AN_REQ_FLT_END; |
| 430 | req->analyse_exp = TICK_ETERNITY; |
| 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | /* This stream analyser runs all HTTP request processing which is common to |
| 436 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 437 | * reqadd, stats and redirects. This is performed for the designated proxy. |
| 438 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 439 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 440 | * error, ...). |
| 441 | */ |
| 442 | int htx_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px) |
| 443 | { |
| 444 | struct session *sess = s->sess; |
| 445 | struct http_txn *txn = s->txn; |
| 446 | struct http_msg *msg = &txn->req; |
| 447 | struct redirect_rule *rule; |
| 448 | struct cond_wordlist *wl; |
| 449 | enum rule_result verdict; |
| 450 | int deny_status = HTTP_ERR_403; |
| 451 | struct connection *conn = objt_conn(sess->origin); |
| 452 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 453 | // TODO: Disabled for now |
| 454 | req->analyse_exp = TICK_ETERNITY; |
| 455 | req->analysers &= ~an_bit; |
| 456 | return 1; |
| 457 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 458 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
| 459 | /* we need more data */ |
| 460 | goto return_prx_yield; |
| 461 | } |
| 462 | |
| 463 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
| 464 | now_ms, __FUNCTION__, |
| 465 | s, |
| 466 | req, |
| 467 | req->rex, req->wex, |
| 468 | req->flags, |
| 469 | ci_data(req), |
| 470 | req->analysers); |
| 471 | |
| 472 | /* just in case we have some per-backend tracking */ |
| 473 | stream_inc_be_http_req_ctr(s); |
| 474 | |
| 475 | /* evaluate http-request rules */ |
| 476 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
| 477 | verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, &deny_status); |
| 478 | |
| 479 | switch (verdict) { |
| 480 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 481 | goto return_prx_yield; |
| 482 | |
| 483 | case HTTP_RULE_RES_CONT: |
| 484 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 485 | break; |
| 486 | |
| 487 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 488 | if (txn->flags & TX_CLTARPIT) |
| 489 | goto tarpit; |
| 490 | goto deny; |
| 491 | |
| 492 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 493 | goto return_prx_cond; |
| 494 | |
| 495 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
| 496 | goto done; |
| 497 | |
| 498 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 499 | goto return_bad_req; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | if (conn && (conn->flags & CO_FL_EARLY_DATA) && |
| 504 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) { |
| 505 | struct hdr_ctx ctx; |
| 506 | |
| 507 | ctx.idx = 0; |
| 508 | if (!http_find_header2("Early-Data", strlen("Early-Data"), |
| 509 | ci_head(&s->req), &txn->hdr_idx, &ctx)) { |
| 510 | if (unlikely(http_header_add_tail2(&txn->req, |
| 511 | &txn->hdr_idx, "Early-Data: 1", |
| 512 | strlen("Early-Data: 1")) < 0)) { |
| 513 | goto return_bad_req; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | } |
| 518 | |
| 519 | /* OK at this stage, we know that the request was accepted according to |
| 520 | * the http-request rules, we can check for the stats. Note that the |
| 521 | * URI is detected *before* the req* rules in order not to be affected |
| 522 | * by a possible reqrep, while they are processed *after* so that a |
| 523 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 524 | */ |
| 525 | if (stats_check_uri(&s->si[1], txn, px)) { |
| 526 | s->target = &http_stats_applet.obj_type; |
| 527 | if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target)))) { |
| 528 | txn->status = 500; |
| 529 | s->logs.tv_request = now; |
| 530 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 531 | |
| 532 | if (!(s->flags & SF_ERR_MASK)) |
| 533 | s->flags |= SF_ERR_RESOURCE; |
| 534 | goto return_prx_cond; |
| 535 | } |
| 536 | |
| 537 | /* parse the whole stats request and extract the relevant information */ |
| 538 | http_handle_stats(s, req); |
| 539 | verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, &deny_status); |
| 540 | /* not all actions implemented: deny, allow, auth */ |
| 541 | |
| 542 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 543 | goto deny; |
| 544 | |
| 545 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 546 | goto return_prx_cond; |
| 547 | } |
| 548 | |
| 549 | /* evaluate the req* rules except reqadd */ |
| 550 | if (px->req_exp != NULL) { |
| 551 | if (apply_filters_to_request(s, req, px) < 0) |
| 552 | goto return_bad_req; |
| 553 | |
| 554 | if (txn->flags & TX_CLDENY) |
| 555 | goto deny; |
| 556 | |
| 557 | if (txn->flags & TX_CLTARPIT) { |
| 558 | deny_status = HTTP_ERR_500; |
| 559 | goto tarpit; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | /* add request headers from the rule sets in the same order */ |
| 564 | list_for_each_entry(wl, &px->req_add, list) { |
| 565 | if (wl->cond) { |
| 566 | int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 567 | ret = acl_pass(ret); |
| 568 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 569 | ret = !ret; |
| 570 | if (!ret) |
| 571 | continue; |
| 572 | } |
| 573 | |
| 574 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0)) |
| 575 | goto return_bad_req; |
| 576 | } |
| 577 | |
| 578 | |
| 579 | /* Proceed with the stats now. */ |
| 580 | if (unlikely(objt_applet(s->target) == &http_stats_applet) || |
| 581 | unlikely(objt_applet(s->target) == &http_cache_applet)) { |
| 582 | /* process the stats request now */ |
| 583 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 584 | HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
| 585 | |
| 586 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 587 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 588 | if (!(s->flags & SF_FINST_MASK)) |
| 589 | s->flags |= SF_FINST_R; |
| 590 | |
| 591 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
| 592 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 593 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 594 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
| 595 | goto done; |
| 596 | } |
| 597 | |
| 598 | /* check whether we have some ACLs set to redirect this request */ |
| 599 | list_for_each_entry(rule, &px->redirect_rules, list) { |
| 600 | if (rule->cond) { |
| 601 | int ret; |
| 602 | |
| 603 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 604 | ret = acl_pass(ret); |
| 605 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 606 | ret = !ret; |
| 607 | if (!ret) |
| 608 | continue; |
| 609 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 610 | if (!htx_apply_redirect_rule(rule, s, txn)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 611 | goto return_bad_req; |
| 612 | goto done; |
| 613 | } |
| 614 | |
| 615 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 616 | * If this happens, then the data will not come immediately, so we must |
| 617 | * send all what we have without waiting. Note that due to the small gain |
| 618 | * in waiting for the body of the request, it's easier to simply put the |
| 619 | * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 620 | * itself once used. |
| 621 | */ |
| 622 | req->flags |= CF_SEND_DONTWAIT; |
| 623 | |
| 624 | done: /* done with this analyser, continue with next ones that the calling |
| 625 | * points will have set, if any. |
| 626 | */ |
| 627 | req->analyse_exp = TICK_ETERNITY; |
| 628 | done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */ |
| 629 | req->analysers &= ~an_bit; |
| 630 | return 1; |
| 631 | |
| 632 | tarpit: |
| 633 | /* Allow cookie logging |
| 634 | */ |
| 635 | if (s->be->cookie_name || sess->fe->capture_name) |
| 636 | manage_client_side_cookies(s, req); |
| 637 | |
| 638 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 639 | * which may be the same as the connect timeout if unspecified. |
| 640 | * If unset, then set it to zero because we really want it to |
| 641 | * eventually expire. We build the tarpit as an analyser. |
| 642 | */ |
| 643 | channel_erase(&s->req); |
| 644 | |
| 645 | /* wipe the request out so that we can drop the connection early |
| 646 | * if the client closes first. |
| 647 | */ |
| 648 | channel_dont_connect(req); |
| 649 | |
| 650 | txn->status = http_err_codes[deny_status]; |
| 651 | |
| 652 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
| 653 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 654 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 655 | if (!req->analyse_exp) |
| 656 | req->analyse_exp = tick_add(now_ms, 0); |
| 657 | stream_inc_http_err_ctr(s); |
| 658 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
| 659 | if (sess->fe != s->be) |
| 660 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
| 661 | if (sess->listener->counters) |
| 662 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
| 663 | goto done_without_exp; |
| 664 | |
| 665 | deny: /* this request was blocked (denied) */ |
| 666 | |
| 667 | /* Allow cookie logging |
| 668 | */ |
| 669 | if (s->be->cookie_name || sess->fe->capture_name) |
| 670 | manage_client_side_cookies(s, req); |
| 671 | |
| 672 | txn->flags |= TX_CLDENY; |
| 673 | txn->status = http_err_codes[deny_status]; |
| 674 | s->logs.tv_request = now; |
| 675 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 676 | stream_inc_http_err_ctr(s); |
| 677 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
| 678 | if (sess->fe != s->be) |
| 679 | HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
| 680 | if (sess->listener->counters) |
| 681 | HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
| 682 | goto return_prx_cond; |
| 683 | |
| 684 | return_bad_req: |
| 685 | /* We centralize bad requests processing here */ |
| 686 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 687 | /* we detected a parsing error. We want to archive this request |
| 688 | * in the dedicated proxy area for later troubleshooting. |
| 689 | */ |
| 690 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
| 691 | } |
| 692 | |
| 693 | txn->req.err_state = txn->req.msg_state; |
| 694 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 695 | txn->status = 400; |
| 696 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 697 | |
| 698 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
| 699 | if (sess->listener->counters) |
| 700 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
| 701 | |
| 702 | return_prx_cond: |
| 703 | if (!(s->flags & SF_ERR_MASK)) |
| 704 | s->flags |= SF_ERR_PRXCOND; |
| 705 | if (!(s->flags & SF_FINST_MASK)) |
| 706 | s->flags |= SF_FINST_R; |
| 707 | |
| 708 | req->analysers &= AN_REQ_FLT_END; |
| 709 | req->analyse_exp = TICK_ETERNITY; |
| 710 | return 0; |
| 711 | |
| 712 | return_prx_yield: |
| 713 | channel_dont_connect(req); |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | /* This function performs all the processing enabled for the current request. |
| 718 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 719 | * needs more data, encounters an error, or wants to immediately abort the |
| 720 | * request. It relies on buffers flags, and updates s->req.analysers. |
| 721 | */ |
| 722 | int htx_process_request(struct stream *s, struct channel *req, int an_bit) |
| 723 | { |
| 724 | struct session *sess = s->sess; |
| 725 | struct http_txn *txn = s->txn; |
| 726 | struct http_msg *msg = &txn->req; |
| 727 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
| 728 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 729 | // TODO: Disabled for now |
| 730 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 731 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
| 732 | req->analyse_exp = TICK_ETERNITY; |
| 733 | req->analysers &= ~an_bit; |
| 734 | return 1; |
| 735 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 736 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
| 737 | /* we need more data */ |
| 738 | channel_dont_connect(req); |
| 739 | return 0; |
| 740 | } |
| 741 | |
| 742 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
| 743 | now_ms, __FUNCTION__, |
| 744 | s, |
| 745 | req, |
| 746 | req->rex, req->wex, |
| 747 | req->flags, |
| 748 | ci_data(req), |
| 749 | req->analysers); |
| 750 | |
| 751 | /* |
| 752 | * Right now, we know that we have processed the entire headers |
| 753 | * and that unwanted requests have been filtered out. We can do |
| 754 | * whatever we want with the remaining request. Also, now we |
| 755 | * may have separate values for ->fe, ->be. |
| 756 | */ |
| 757 | |
| 758 | /* |
| 759 | * If HTTP PROXY is set we simply get remote server address parsing |
| 760 | * incoming request. Note that this requires that a connection is |
| 761 | * allocated on the server side. |
| 762 | */ |
| 763 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) { |
| 764 | struct connection *conn; |
| 765 | char *path; |
| 766 | |
| 767 | /* Note that for now we don't reuse existing proxy connections */ |
| 768 | if (unlikely((conn = cs_conn(si_alloc_cs(&s->si[1], NULL))) == NULL)) { |
| 769 | txn->req.err_state = txn->req.msg_state; |
| 770 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 771 | txn->status = 500; |
| 772 | req->analysers &= AN_REQ_FLT_END; |
| 773 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 774 | |
| 775 | if (!(s->flags & SF_ERR_MASK)) |
| 776 | s->flags |= SF_ERR_RESOURCE; |
| 777 | if (!(s->flags & SF_FINST_MASK)) |
| 778 | s->flags |= SF_FINST_R; |
| 779 | |
| 780 | return 0; |
| 781 | } |
| 782 | |
| 783 | path = http_txn_get_path(txn); |
| 784 | if (url2sa(ci_head(req) + msg->sl.rq.u, |
| 785 | path ? path - (ci_head(req) + msg->sl.rq.u) : msg->sl.rq.u_l, |
| 786 | &conn->addr.to, NULL) == -1) |
| 787 | goto return_bad_req; |
| 788 | |
| 789 | /* if the path was found, we have to remove everything between |
| 790 | * ci_head(req) + msg->sl.rq.u and path (excluded). If it was not |
| 791 | * found, we need to replace from ci_head(req) + msg->sl.rq.u for |
| 792 | * u_l characters by a single "/". |
| 793 | */ |
| 794 | if (path) { |
| 795 | char *cur_ptr = ci_head(req); |
| 796 | char *cur_end = cur_ptr + txn->req.sl.rq.l; |
| 797 | int delta; |
| 798 | |
| 799 | delta = b_rep_blk(&req->buf, cur_ptr + msg->sl.rq.u, path, NULL, 0); |
| 800 | http_msg_move_end(&txn->req, delta); |
| 801 | cur_end += delta; |
| 802 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 803 | goto return_bad_req; |
| 804 | } |
| 805 | else { |
| 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, |
| 811 | cur_ptr + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1); |
| 812 | http_msg_move_end(&txn->req, delta); |
| 813 | cur_end += delta; |
| 814 | if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL) |
| 815 | goto return_bad_req; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | /* |
| 820 | * 7: Now we can work with the cookies. |
| 821 | * Note that doing so might move headers in the request, but |
| 822 | * the fields will stay coherent and the URI will not move. |
| 823 | * This should only be performed in the backend. |
| 824 | */ |
| 825 | if (s->be->cookie_name || sess->fe->capture_name) |
| 826 | manage_client_side_cookies(s, req); |
| 827 | |
| 828 | /* add unique-id if "header-unique-id" is specified */ |
| 829 | |
| 830 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id) && !s->unique_id) { |
| 831 | if ((s->unique_id = pool_alloc(pool_head_uniqueid)) == NULL) |
| 832 | goto return_bad_req; |
| 833 | s->unique_id[0] = '\0'; |
| 834 | build_logline(s, s->unique_id, UNIQUEID_LEN, &sess->fe->format_unique_id); |
| 835 | } |
| 836 | |
| 837 | if (sess->fe->header_unique_id && s->unique_id) { |
| 838 | if (chunk_printf(&trash, "%s: %s", sess->fe->header_unique_id, s->unique_id) < 0) |
| 839 | goto return_bad_req; |
| 840 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, trash.data) < 0)) |
| 841 | goto return_bad_req; |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 846 | * asks for it. |
| 847 | */ |
| 848 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
| 849 | struct hdr_ctx ctx = { .idx = 0 }; |
| 850 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
| 851 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 852 | s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len, |
| 853 | ci_head(req), &txn->hdr_idx, &ctx)) { |
| 854 | /* The header is set to be added only if none is present |
| 855 | * and we found it, so don't do anything. |
| 856 | */ |
| 857 | } |
| 858 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
| 859 | /* Add an X-Forwarded-For header unless the source IP is |
| 860 | * in the 'except' network range. |
| 861 | */ |
| 862 | if ((!sess->fe->except_mask.s_addr || |
| 863 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
| 864 | != sess->fe->except_net.s_addr) && |
| 865 | (!s->be->except_mask.s_addr || |
| 866 | (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr) |
| 867 | != s->be->except_net.s_addr)) { |
| 868 | int len; |
| 869 | unsigned char *pn; |
| 870 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr; |
| 871 | |
| 872 | /* Note: we rely on the backend to get the header name to be used for |
| 873 | * x-forwarded-for, because the header is really meant for the backends. |
| 874 | * However, if the backend did not specify any option, we have to rely |
| 875 | * on the frontend's header name. |
| 876 | */ |
| 877 | if (s->be->fwdfor_hdr_len) { |
| 878 | len = s->be->fwdfor_hdr_len; |
| 879 | memcpy(trash.area, |
| 880 | s->be->fwdfor_hdr_name, len); |
| 881 | } else { |
| 882 | len = sess->fe->fwdfor_hdr_len; |
| 883 | memcpy(trash.area, |
| 884 | sess->fe->fwdfor_hdr_name, len); |
| 885 | } |
| 886 | len += snprintf(trash.area + len, |
| 887 | trash.size - len, |
| 888 | ": %d.%d.%d.%d", pn[0], pn[1], |
| 889 | pn[2], pn[3]); |
| 890 | |
| 891 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0)) |
| 892 | goto return_bad_req; |
| 893 | } |
| 894 | } |
| 895 | else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) { |
| 896 | /* FIXME: for the sake of completeness, we should also support |
| 897 | * 'except' here, although it is mostly useless in this case. |
| 898 | */ |
| 899 | int len; |
| 900 | char pn[INET6_ADDRSTRLEN]; |
| 901 | inet_ntop(AF_INET6, |
| 902 | (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr, |
| 903 | pn, sizeof(pn)); |
| 904 | |
| 905 | /* Note: we rely on the backend to get the header name to be used for |
| 906 | * x-forwarded-for, because the header is really meant for the backends. |
| 907 | * However, if the backend did not specify any option, we have to rely |
| 908 | * on the frontend's header name. |
| 909 | */ |
| 910 | if (s->be->fwdfor_hdr_len) { |
| 911 | len = s->be->fwdfor_hdr_len; |
| 912 | memcpy(trash.area, s->be->fwdfor_hdr_name, |
| 913 | len); |
| 914 | } else { |
| 915 | len = sess->fe->fwdfor_hdr_len; |
| 916 | memcpy(trash.area, sess->fe->fwdfor_hdr_name, |
| 917 | len); |
| 918 | } |
| 919 | len += snprintf(trash.area + len, trash.size - len, |
| 920 | ": %s", pn); |
| 921 | |
| 922 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0)) |
| 923 | goto return_bad_req; |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | /* |
| 928 | * 10: add X-Original-To if either the frontend or the backend |
| 929 | * asks for it. |
| 930 | */ |
| 931 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
| 932 | |
| 933 | /* FIXME: don't know if IPv6 can handle that case too. */ |
| 934 | if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) { |
| 935 | /* Add an X-Original-To header unless the destination IP is |
| 936 | * in the 'except' network range. |
| 937 | */ |
| 938 | conn_get_to_addr(cli_conn); |
| 939 | |
| 940 | if (cli_conn->addr.to.ss_family == AF_INET && |
| 941 | ((!sess->fe->except_mask_to.s_addr || |
| 942 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
| 943 | != sess->fe->except_to.s_addr) && |
| 944 | (!s->be->except_mask_to.s_addr || |
| 945 | (((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
| 946 | != s->be->except_to.s_addr))) { |
| 947 | int len; |
| 948 | unsigned char *pn; |
| 949 | pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr; |
| 950 | |
| 951 | /* Note: we rely on the backend to get the header name to be used for |
| 952 | * x-original-to, because the header is really meant for the backends. |
| 953 | * However, if the backend did not specify any option, we have to rely |
| 954 | * on the frontend's header name. |
| 955 | */ |
| 956 | if (s->be->orgto_hdr_len) { |
| 957 | len = s->be->orgto_hdr_len; |
| 958 | memcpy(trash.area, |
| 959 | s->be->orgto_hdr_name, len); |
| 960 | } else { |
| 961 | len = sess->fe->orgto_hdr_len; |
| 962 | memcpy(trash.area, |
| 963 | sess->fe->orgto_hdr_name, len); |
| 964 | } |
| 965 | len += snprintf(trash.area + len, |
| 966 | trash.size - len, |
| 967 | ": %d.%d.%d.%d", pn[0], pn[1], |
| 968 | pn[2], pn[3]); |
| 969 | |
| 970 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.area, len) < 0)) |
| 971 | goto return_bad_req; |
| 972 | } |
| 973 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 974 | } |
| 975 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 976 | /* If we have no server assigned yet and we're balancing on url_param |
| 977 | * with a POST request, we may be interested in checking the body for |
| 978 | * that parameter. This will be done in another analyser. |
| 979 | */ |
| 980 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
| 981 | s->txn->meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
| 982 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
| 983 | channel_dont_connect(req); |
| 984 | req->analysers |= AN_REQ_HTTP_BODY; |
| 985 | } |
| 986 | |
| 987 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 988 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
| 989 | #ifdef TCP_QUICKACK |
| 990 | /* We expect some data from the client. Unless we know for sure |
| 991 | * we already have a full request, we have to re-enable quick-ack |
| 992 | * in case we previously disabled it, otherwise we might cause |
| 993 | * the client to delay further data. |
| 994 | */ |
| 995 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
| 996 | cli_conn && conn_ctrl_ready(cli_conn) && |
| 997 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
| 998 | (msg->body_len > ci_data(req) - txn->req.eoh - 2))) |
| 999 | setsockopt(cli_conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
| 1000 | #endif |
| 1001 | |
| 1002 | /************************************************************* |
| 1003 | * OK, that's finished for the headers. We have done what we * |
| 1004 | * could. Let's switch to the DATA state. * |
| 1005 | ************************************************************/ |
| 1006 | req->analyse_exp = TICK_ETERNITY; |
| 1007 | req->analysers &= ~an_bit; |
| 1008 | |
| 1009 | s->logs.tv_request = now; |
| 1010 | /* OK let's go on with the BODY now */ |
| 1011 | return 1; |
| 1012 | |
| 1013 | return_bad_req: /* let's centralize all bad requests */ |
| 1014 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 1015 | /* we detected a parsing error. We want to archive this request |
| 1016 | * in the dedicated proxy area for later troubleshooting. |
| 1017 | */ |
| 1018 | http_capture_bad_message(sess->fe, s, msg, msg->err_state, sess->fe); |
| 1019 | } |
| 1020 | |
| 1021 | txn->req.err_state = txn->req.msg_state; |
| 1022 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 1023 | txn->status = 400; |
| 1024 | req->analysers &= AN_REQ_FLT_END; |
| 1025 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 1026 | |
| 1027 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
| 1028 | if (sess->listener->counters) |
| 1029 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
| 1030 | |
| 1031 | if (!(s->flags & SF_ERR_MASK)) |
| 1032 | s->flags |= SF_ERR_PRXCOND; |
| 1033 | if (!(s->flags & SF_FINST_MASK)) |
| 1034 | s->flags |= SF_FINST_R; |
| 1035 | return 0; |
| 1036 | } |
| 1037 | |
| 1038 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 1039 | * returns zero, at the beginning because it prevents any other processing |
| 1040 | * from occurring, and at the end because it terminates the request. |
| 1041 | */ |
| 1042 | int htx_process_tarpit(struct stream *s, struct channel *req, int an_bit) |
| 1043 | { |
| 1044 | struct http_txn *txn = s->txn; |
| 1045 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1046 | // TODO: Disabled for now |
| 1047 | req->analyse_exp = TICK_ETERNITY; |
| 1048 | req->analysers &= ~an_bit; |
| 1049 | return 1; |
| 1050 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1051 | /* This connection is being tarpitted. The CLIENT side has |
| 1052 | * already set the connect expiration date to the right |
| 1053 | * timeout. We just have to check that the client is still |
| 1054 | * there and that the timeout has not expired. |
| 1055 | */ |
| 1056 | channel_dont_connect(req); |
| 1057 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
| 1058 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 1059 | return 0; |
| 1060 | |
| 1061 | /* We will set the queue timer to the time spent, just for |
| 1062 | * logging purposes. We fake a 500 server error, so that the |
| 1063 | * attacker will not suspect his connection has been tarpitted. |
| 1064 | * It will not cause trouble to the logs because we can exclude |
| 1065 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 1066 | */ |
| 1067 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 1068 | |
| 1069 | if (!(req->flags & CF_READ_ERROR)) |
| 1070 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 1071 | |
| 1072 | req->analysers &= AN_REQ_FLT_END; |
| 1073 | req->analyse_exp = TICK_ETERNITY; |
| 1074 | |
| 1075 | if (!(s->flags & SF_ERR_MASK)) |
| 1076 | s->flags |= SF_ERR_PRXCOND; |
| 1077 | if (!(s->flags & SF_FINST_MASK)) |
| 1078 | s->flags |= SF_FINST_T; |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
| 1082 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 1083 | * for either the buffer to be full, or the full advertised contents to have |
| 1084 | * reached the buffer. It must only be called after the standard HTTP request |
| 1085 | * processing has occurred, because it expects the request to be parsed and will |
| 1086 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 1087 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 1088 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 1089 | * needs to read more data, or 1 once it has completed its analysis. |
| 1090 | */ |
| 1091 | int htx_wait_for_request_body(struct stream *s, struct channel *req, int an_bit) |
| 1092 | { |
| 1093 | struct session *sess = s->sess; |
| 1094 | struct http_txn *txn = s->txn; |
| 1095 | struct http_msg *msg = &s->txn->req; |
| 1096 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1097 | // TODO: Disabled for now |
| 1098 | req->analyse_exp = TICK_ETERNITY; |
| 1099 | req->analysers &= ~an_bit; |
| 1100 | return 1; |
| 1101 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1102 | /* We have to parse the HTTP request body to find any required data. |
| 1103 | * "balance url_param check_post" should have been the only way to get |
| 1104 | * into this. We were brought here after HTTP header analysis, so all |
| 1105 | * related structures are ready. |
| 1106 | */ |
| 1107 | |
| 1108 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 1109 | /* This is the first call */ |
| 1110 | if (msg->msg_state < HTTP_MSG_BODY) |
| 1111 | goto missing_data; |
| 1112 | |
| 1113 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 1114 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 1115 | * send an HTTP/1.1 100 Continue intermediate response. |
| 1116 | */ |
| 1117 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 1118 | struct hdr_ctx ctx; |
| 1119 | ctx.idx = 0; |
| 1120 | /* Expect is allowed in 1.1, look for it */ |
| 1121 | if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &ctx) && |
| 1122 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 1123 | co_inject(&s->res, HTTP_100.ptr, HTTP_100.len); |
| 1124 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
| 1125 | } |
| 1126 | } |
| 1127 | msg->msg_state = HTTP_MSG_100_SENT; |
| 1128 | } |
| 1129 | |
| 1130 | /* we have msg->sov which points to the first byte of message body. |
| 1131 | * ci_head(req) still points to the beginning of the message. We |
| 1132 | * must save the body in msg->next because it survives buffer |
| 1133 | * re-alignments. |
| 1134 | */ |
| 1135 | msg->next = msg->sov; |
| 1136 | |
| 1137 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 1138 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 1139 | else |
| 1140 | msg->msg_state = HTTP_MSG_DATA; |
| 1141 | } |
| 1142 | |
| 1143 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) { |
| 1144 | /* We're in content-length mode, we just have to wait for enough data. */ |
| 1145 | if (http_body_bytes(msg) < msg->body_len) |
| 1146 | goto missing_data; |
| 1147 | |
| 1148 | /* OK we have everything we need now */ |
| 1149 | goto http_end; |
| 1150 | } |
| 1151 | |
| 1152 | /* OK here we're parsing a chunked-encoded message */ |
| 1153 | |
| 1154 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
| 1155 | /* read the chunk size and assign it to ->chunk_len, then |
| 1156 | * set ->sov and ->next to point to the body and switch to DATA or |
| 1157 | * TRAILERS state. |
| 1158 | */ |
| 1159 | unsigned int chunk; |
| 1160 | int ret = h1_parse_chunk_size(&req->buf, co_data(req) + msg->next, c_data(req), &chunk); |
| 1161 | |
| 1162 | if (!ret) |
| 1163 | goto missing_data; |
| 1164 | else if (ret < 0) { |
| 1165 | msg->err_pos = ci_data(req) + ret; |
| 1166 | if (msg->err_pos < 0) |
| 1167 | msg->err_pos += req->buf.size; |
| 1168 | stream_inc_http_err_ctr(s); |
| 1169 | goto return_bad_req; |
| 1170 | } |
| 1171 | |
| 1172 | msg->chunk_len = chunk; |
| 1173 | msg->body_len += chunk; |
| 1174 | |
| 1175 | msg->sol = ret; |
| 1176 | msg->next += ret; |
| 1177 | msg->msg_state = msg->chunk_len ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
| 1178 | } |
| 1179 | |
| 1180 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
| 1181 | * We have the first data byte is in msg->sov + msg->sol. We're waiting |
| 1182 | * for at least a whole chunk or the whole content length bytes after |
| 1183 | * msg->sov + msg->sol. |
| 1184 | */ |
| 1185 | if (msg->msg_state == HTTP_MSG_TRAILERS) |
| 1186 | goto http_end; |
| 1187 | |
| 1188 | if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */ |
| 1189 | goto http_end; |
| 1190 | |
| 1191 | missing_data: |
| 1192 | /* we get here if we need to wait for more data. If the buffer is full, |
| 1193 | * we have the maximum we can expect. |
| 1194 | */ |
| 1195 | if (channel_full(req, global.tune.maxrewrite)) |
| 1196 | goto http_end; |
| 1197 | |
| 1198 | if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 1199 | txn->status = 408; |
| 1200 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 1201 | |
| 1202 | if (!(s->flags & SF_ERR_MASK)) |
| 1203 | s->flags |= SF_ERR_CLITO; |
| 1204 | if (!(s->flags & SF_FINST_MASK)) |
| 1205 | s->flags |= SF_FINST_D; |
| 1206 | goto return_err_msg; |
| 1207 | } |
| 1208 | |
| 1209 | /* we get here if we need to wait for more data */ |
| 1210 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
| 1211 | /* Not enough data. We'll re-use the http-request |
| 1212 | * timeout here. Ideally, we should set the timeout |
| 1213 | * relative to the accept() date. We just set the |
| 1214 | * request timeout once at the beginning of the |
| 1215 | * request. |
| 1216 | */ |
| 1217 | channel_dont_connect(req); |
| 1218 | if (!tick_isset(req->analyse_exp)) |
| 1219 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 1220 | return 0; |
| 1221 | } |
| 1222 | |
| 1223 | http_end: |
| 1224 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 1225 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 1226 | req->analysers &= ~an_bit; |
| 1227 | req->analyse_exp = TICK_ETERNITY; |
| 1228 | return 1; |
| 1229 | |
| 1230 | return_bad_req: /* let's centralize all bad requests */ |
| 1231 | txn->req.err_state = txn->req.msg_state; |
| 1232 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 1233 | txn->status = 400; |
| 1234 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 1235 | |
| 1236 | if (!(s->flags & SF_ERR_MASK)) |
| 1237 | s->flags |= SF_ERR_PRXCOND; |
| 1238 | if (!(s->flags & SF_FINST_MASK)) |
| 1239 | s->flags |= SF_FINST_R; |
| 1240 | |
| 1241 | return_err_msg: |
| 1242 | req->analysers &= AN_REQ_FLT_END; |
| 1243 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
| 1244 | if (sess->listener->counters) |
| 1245 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
| 1249 | /* This function is an analyser which forwards request body (including chunk |
| 1250 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 1251 | * zero byte. The only situation where it must not be called is when we're in |
| 1252 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 1253 | * remaining data and to resync after end of body. It expects the msg_state to |
| 1254 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 1255 | * read more data, or 1 once we can go on with next request or end the stream. |
| 1256 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
| 1257 | * bytes of pending data + the headers if not already done. |
| 1258 | */ |
| 1259 | int htx_request_forward_body(struct stream *s, struct channel *req, int an_bit) |
| 1260 | { |
| 1261 | struct session *sess = s->sess; |
| 1262 | struct http_txn *txn = s->txn; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1263 | struct http_msg *msg = &txn->req; |
| 1264 | struct htx *htx; |
| 1265 | //int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1266 | |
| 1267 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
| 1268 | now_ms, __FUNCTION__, |
| 1269 | s, |
| 1270 | req, |
| 1271 | req->rex, req->wex, |
| 1272 | req->flags, |
| 1273 | ci_data(req), |
| 1274 | req->analysers); |
| 1275 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1276 | htx = htx_from_buf(&req->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1277 | |
| 1278 | if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
| 1279 | ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) { |
| 1280 | /* Output closed while we were sending data. We must abort and |
| 1281 | * wake the other side up. |
| 1282 | */ |
| 1283 | msg->err_state = msg->msg_state; |
| 1284 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1285 | htx_end_request(s); |
| 1286 | htx_end_response(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1287 | return 1; |
| 1288 | } |
| 1289 | |
| 1290 | /* Note that we don't have to send 100-continue back because we don't |
| 1291 | * need the data to complete our job, and it's up to the server to |
| 1292 | * decide whether to return 100, 417 or anything else in return of |
| 1293 | * an "Expect: 100-continue" header. |
| 1294 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1295 | if (msg->msg_state == HTTP_MSG_BODY) |
| 1296 | msg->msg_state = HTTP_MSG_DATA; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1297 | |
| 1298 | /* Some post-connect processing might want us to refrain from starting to |
| 1299 | * forward data. Currently, the only reason for this is "balance url_param" |
| 1300 | * whichs need to parse/process the request after we've enabled forwarding. |
| 1301 | */ |
| 1302 | if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) { |
| 1303 | if (!(s->res.flags & CF_READ_ATTACHED)) { |
| 1304 | channel_auto_connect(req); |
| 1305 | req->flags |= CF_WAKE_CONNECT; |
| 1306 | channel_dont_close(req); /* don't fail on early shutr */ |
| 1307 | goto waiting; |
| 1308 | } |
| 1309 | msg->flags &= ~HTTP_MSGF_WAIT_CONN; |
| 1310 | } |
| 1311 | |
| 1312 | /* in most states, we should abort in case of early close */ |
| 1313 | channel_auto_close(req); |
| 1314 | |
| 1315 | if (req->to_forward) { |
| 1316 | /* We can't process the buffer's contents yet */ |
| 1317 | req->flags |= CF_WAKE_WRITE; |
| 1318 | goto missing_data_or_waiting; |
| 1319 | } |
| 1320 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1321 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 1322 | goto done; |
| 1323 | |
| 1324 | /* Forward all input data. We get it by removing all outgoing data not |
| 1325 | * forwarded yet from HTX data size. |
| 1326 | */ |
| 1327 | c_adv(req, htx->data - co_data(req)); |
| 1328 | |
| 1329 | /* To let the function channel_forward work as expected we must update |
| 1330 | * the channel's buffer to pretend there is no more input data. The |
| 1331 | * right length is then restored. We must do that, because when an HTX |
| 1332 | * message is stored into a buffer, it appears as full. |
| 1333 | */ |
| 1334 | b_set_data(&req->buf, co_data(req)); |
| 1335 | if (htx->extra != ULLONG_MAX) |
| 1336 | htx->extra -= channel_forward(req, htx->extra); |
| 1337 | b_set_data(&req->buf, b_size(&req->buf)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1338 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1339 | /* Check if the end-of-message is reached and if so, switch the message |
| 1340 | * in HTTP_MSG_DONE state. |
| 1341 | */ |
| 1342 | if (htx_get_tail_type(htx) != HTX_BLK_EOM) |
| 1343 | goto missing_data_or_waiting; |
| 1344 | |
| 1345 | msg->msg_state = HTTP_MSG_DONE; |
| 1346 | |
| 1347 | done: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1348 | /* other states, DONE...TUNNEL */ |
| 1349 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 1350 | if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 1351 | channel_dont_close(req); |
| 1352 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1353 | htx_end_request(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1354 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1355 | htx_end_response(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1356 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 1357 | if (req->flags & CF_SHUTW) { |
| 1358 | /* request errors are most likely due to the |
| 1359 | * server aborting the transfer. */ |
| 1360 | goto aborted_xfer; |
| 1361 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1362 | goto return_bad_req; |
| 1363 | } |
| 1364 | return 1; |
| 1365 | } |
| 1366 | |
| 1367 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 1368 | * the client's connection and forward any shutdown notification to the |
| 1369 | * server, which will decide whether to close or to go on processing the |
| 1370 | * request. We only do that in tunnel mode, and not in other modes since |
| 1371 | * it can be abused to exhaust source ports. */ |
| 1372 | if ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)) { |
| 1373 | channel_auto_read(req); |
| 1374 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && |
| 1375 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)) |
| 1376 | s->si[1].flags |= SI_FL_NOLINGER; |
| 1377 | channel_auto_close(req); |
| 1378 | } |
| 1379 | else if (s->txn->meth == HTTP_METH_POST) { |
| 1380 | /* POST requests may require to read extra CRLF sent by broken |
| 1381 | * browsers and which could cause an RST to be sent upon close |
| 1382 | * on some systems (eg: Linux). */ |
| 1383 | channel_auto_read(req); |
| 1384 | } |
| 1385 | return 0; |
| 1386 | |
| 1387 | missing_data_or_waiting: |
| 1388 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1389 | if (msg->msg_state < HTTP_MSG_DONE && req->flags & CF_SHUTR) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1390 | if (!(s->flags & SF_ERR_MASK)) |
| 1391 | s->flags |= SF_ERR_CLICL; |
| 1392 | if (!(s->flags & SF_FINST_MASK)) { |
| 1393 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 1394 | s->flags |= SF_FINST_H; |
| 1395 | else |
| 1396 | s->flags |= SF_FINST_D; |
| 1397 | } |
| 1398 | |
| 1399 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 1400 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
| 1401 | if (objt_server(s->target)) |
| 1402 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
| 1403 | |
| 1404 | goto return_bad_req_stats_ok; |
| 1405 | } |
| 1406 | |
| 1407 | waiting: |
| 1408 | /* waiting for the last bits to leave the buffer */ |
| 1409 | if (req->flags & CF_SHUTW) |
| 1410 | goto aborted_xfer; |
| 1411 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1412 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1413 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 1414 | * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE. |
| 1415 | * And when content-length is used, we never want to let the possible |
| 1416 | * shutdown be forwarded to the other side, as the state machine will |
| 1417 | * take care of it once the client responds. It's also important to |
| 1418 | * prevent TIME_WAITs from accumulating on the backend side, and for |
| 1419 | * HTTP/2 where the last frame comes with a shutdown. |
| 1420 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1421 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1422 | channel_dont_close(req); |
| 1423 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1424 | #if 0 // FIXME [Cf]: Probably not required now, but I need more time to think |
| 1425 | // about if |
| 1426 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1427 | /* We know that more data are expected, but we couldn't send more that |
| 1428 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
| 1429 | * system knows it must not set a PUSH on this first part. Interactive |
| 1430 | * modes are already handled by the stream sock layer. We must not do |
| 1431 | * this in content-length mode because it could present the MSG_MORE |
| 1432 | * flag with the last block of forwarded data, which would cause an |
| 1433 | * additional delay to be observed by the receiver. |
| 1434 | */ |
| 1435 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 1436 | req->flags |= CF_EXPECT_MORE; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1437 | #endif |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1438 | |
| 1439 | return 0; |
| 1440 | |
| 1441 | return_bad_req: /* let's centralize all bad requests */ |
| 1442 | HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
| 1443 | if (sess->listener->counters) |
| 1444 | HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
| 1445 | |
| 1446 | return_bad_req_stats_ok: |
| 1447 | txn->req.err_state = txn->req.msg_state; |
| 1448 | txn->req.msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1449 | if (txn->status > 0) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1450 | /* 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] | 1451 | htx_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1452 | } else { |
| 1453 | txn->status = 400; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1454 | htx_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1455 | } |
| 1456 | req->analysers &= AN_REQ_FLT_END; |
| 1457 | s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */ |
| 1458 | |
| 1459 | if (!(s->flags & SF_ERR_MASK)) |
| 1460 | s->flags |= SF_ERR_PRXCOND; |
| 1461 | if (!(s->flags & SF_FINST_MASK)) { |
| 1462 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 1463 | s->flags |= SF_FINST_H; |
| 1464 | else |
| 1465 | s->flags |= SF_FINST_D; |
| 1466 | } |
| 1467 | return 0; |
| 1468 | |
| 1469 | aborted_xfer: |
| 1470 | txn->req.err_state = txn->req.msg_state; |
| 1471 | txn->req.msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1472 | if (txn->status > 0) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1473 | /* 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] | 1474 | htx_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1475 | } else { |
| 1476 | txn->status = 502; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1477 | htx_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1478 | } |
| 1479 | req->analysers &= AN_REQ_FLT_END; |
| 1480 | s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */ |
| 1481 | |
| 1482 | HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1); |
| 1483 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
| 1484 | if (objt_server(s->target)) |
| 1485 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
| 1486 | |
| 1487 | if (!(s->flags & SF_ERR_MASK)) |
| 1488 | s->flags |= SF_ERR_SRVCL; |
| 1489 | if (!(s->flags & SF_FINST_MASK)) { |
| 1490 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 1491 | s->flags |= SF_FINST_H; |
| 1492 | else |
| 1493 | s->flags |= SF_FINST_D; |
| 1494 | } |
| 1495 | return 0; |
| 1496 | } |
| 1497 | |
| 1498 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 1499 | * processing can continue on next analysers, or zero if it either needs more |
| 1500 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 1501 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers |
| 1502 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1503 | * abort. |
| 1504 | */ |
| 1505 | int htx_wait_for_response(struct stream *s, struct channel *rep, int an_bit) |
| 1506 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1507 | /* |
| 1508 | * We will analyze a complete HTTP response to check the its syntax. |
| 1509 | * |
| 1510 | * Once the start line and all headers are received, we may perform a |
| 1511 | * capture of the error (if any), and we will set a few fields. We also |
| 1512 | * logging and finally headers capture. |
| 1513 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1514 | struct session *sess = s->sess; |
| 1515 | struct http_txn *txn = s->txn; |
| 1516 | struct http_msg *msg = &txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1517 | struct htx *htx; |
| 1518 | union h1_sl sl; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1519 | int n; |
| 1520 | |
| 1521 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
| 1522 | now_ms, __FUNCTION__, |
| 1523 | s, |
| 1524 | rep, |
| 1525 | rep->rex, rep->wex, |
| 1526 | rep->flags, |
| 1527 | ci_data(rep), |
| 1528 | rep->analysers); |
| 1529 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1530 | htx = htx_from_buf(&rep->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1531 | |
| 1532 | /* |
| 1533 | * Now we quickly check if we have found a full valid response. |
| 1534 | * If not so, we check the FD and buffer states before leaving. |
| 1535 | * A full response is indicated by the fact that we have seen |
| 1536 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
| 1537 | * responses are checked first. |
| 1538 | * |
| 1539 | * Depending on whether the client is still there or not, we |
| 1540 | * may send an error response back or not. Note that normally |
| 1541 | * we should only check for HTTP status there, and check I/O |
| 1542 | * errors somewhere else. |
| 1543 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1544 | if (unlikely(htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH)) { |
| 1545 | /* 1: have we encountered a read error ? */ |
| 1546 | if (rep->flags & CF_READ_ERROR) { |
| 1547 | if (txn->flags & TX_NOT_FIRST) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1548 | goto abort_keep_alive; |
| 1549 | |
| 1550 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
| 1551 | if (objt_server(s->target)) { |
| 1552 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
| 1553 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
| 1554 | } |
| 1555 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1556 | rep->analysers &= AN_RES_FLT_END; |
| 1557 | txn->status = 502; |
| 1558 | |
| 1559 | /* Check to see if the server refused the early data. |
| 1560 | * If so, just send a 425 |
| 1561 | */ |
| 1562 | if (objt_cs(s->si[1].end)) { |
| 1563 | struct connection *conn = objt_cs(s->si[1].end)->conn; |
| 1564 | |
| 1565 | if (conn->err_code == CO_ER_SSL_EARLY_FAILED) |
| 1566 | txn->status = 425; |
| 1567 | } |
| 1568 | |
| 1569 | s->si[1].flags |= SI_FL_NOLINGER; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1570 | htx_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1571 | |
| 1572 | if (!(s->flags & SF_ERR_MASK)) |
| 1573 | s->flags |= SF_ERR_SRVCL; |
| 1574 | if (!(s->flags & SF_FINST_MASK)) |
| 1575 | s->flags |= SF_FINST_H; |
| 1576 | return 0; |
| 1577 | } |
| 1578 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1579 | /* 2: read timeout : return a 504 to the client. */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1580 | else if (rep->flags & CF_READ_TIMEOUT) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1581 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
| 1582 | if (objt_server(s->target)) { |
| 1583 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
| 1584 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
| 1585 | } |
| 1586 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1587 | rep->analysers &= AN_RES_FLT_END; |
| 1588 | txn->status = 504; |
| 1589 | s->si[1].flags |= SI_FL_NOLINGER; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1590 | htx_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1591 | |
| 1592 | if (!(s->flags & SF_ERR_MASK)) |
| 1593 | s->flags |= SF_ERR_SRVTO; |
| 1594 | if (!(s->flags & SF_FINST_MASK)) |
| 1595 | s->flags |= SF_FINST_H; |
| 1596 | return 0; |
| 1597 | } |
| 1598 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1599 | /* 3: client abort with an abortonclose */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1600 | else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
| 1601 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 1602 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
| 1603 | if (objt_server(s->target)) |
| 1604 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
| 1605 | |
| 1606 | rep->analysers &= AN_RES_FLT_END; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1607 | txn->status = 400; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1608 | htx_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1609 | |
| 1610 | if (!(s->flags & SF_ERR_MASK)) |
| 1611 | s->flags |= SF_ERR_CLICL; |
| 1612 | if (!(s->flags & SF_FINST_MASK)) |
| 1613 | s->flags |= SF_FINST_H; |
| 1614 | |
| 1615 | /* process_stream() will take care of the error */ |
| 1616 | return 0; |
| 1617 | } |
| 1618 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1619 | /* 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] | 1620 | else if (rep->flags & CF_SHUTR) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1621 | if (txn->flags & TX_NOT_FIRST) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1622 | goto abort_keep_alive; |
| 1623 | |
| 1624 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
| 1625 | if (objt_server(s->target)) { |
| 1626 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
| 1627 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
| 1628 | } |
| 1629 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1630 | rep->analysers &= AN_RES_FLT_END; |
| 1631 | txn->status = 502; |
| 1632 | s->si[1].flags |= SI_FL_NOLINGER; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1633 | htx_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1634 | |
| 1635 | if (!(s->flags & SF_ERR_MASK)) |
| 1636 | s->flags |= SF_ERR_SRVCL; |
| 1637 | if (!(s->flags & SF_FINST_MASK)) |
| 1638 | s->flags |= SF_FINST_H; |
| 1639 | return 0; |
| 1640 | } |
| 1641 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1642 | /* 5: write error to client (we don't send any message then) */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1643 | else if (rep->flags & CF_WRITE_ERROR) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1644 | if (txn->flags & TX_NOT_FIRST) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1645 | goto abort_keep_alive; |
| 1646 | |
| 1647 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
| 1648 | rep->analysers &= AN_RES_FLT_END; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1649 | |
| 1650 | if (!(s->flags & SF_ERR_MASK)) |
| 1651 | s->flags |= SF_ERR_CLICL; |
| 1652 | if (!(s->flags & SF_FINST_MASK)) |
| 1653 | s->flags |= SF_FINST_H; |
| 1654 | |
| 1655 | /* process_stream() will take care of the error */ |
| 1656 | return 0; |
| 1657 | } |
| 1658 | |
| 1659 | channel_dont_close(rep); |
| 1660 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 1661 | return 0; |
| 1662 | } |
| 1663 | |
| 1664 | /* More interesting part now : we know that we have a complete |
| 1665 | * response which at least looks like HTTP. We have an indicator |
| 1666 | * of each header's length, so we can parse them quickly. |
| 1667 | */ |
| 1668 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1669 | msg->msg_state = HTTP_MSG_BODY; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1670 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1671 | /* 0: we might have to print this header in debug mode */ |
| 1672 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1673 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 1674 | int32_t pos; |
| 1675 | |
| 1676 | htx_debug_stline("srvrep", s, http_find_stline(htx)); |
| 1677 | |
| 1678 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 1679 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 1680 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 1681 | |
| 1682 | if (type == HTX_BLK_EOH) |
| 1683 | break; |
| 1684 | if (type != HTX_BLK_HDR) |
| 1685 | continue; |
| 1686 | |
| 1687 | htx_debug_hdr("srvhdr", s, |
| 1688 | htx_get_blk_name(htx, blk), |
| 1689 | htx_get_blk_value(htx, blk)); |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | /* 1: get the status code */ |
| 1694 | sl = http_find_stline(htx); |
| 1695 | txn->status = sl.st.status; |
| 1696 | if (htx->extra != ULLONG_MAX) |
| 1697 | msg->flags |= HTTP_MSGF_XFER_LEN; |
| 1698 | |
| 1699 | /* ... and check if the request is HTTP/1.1 or above */ |
| 1700 | if ((sl.st.v.len == 8) && |
| 1701 | ((*(sl.st.v.ptr + 5) > '1') || |
| 1702 | ((*(sl.st.v.ptr + 5) == '1') && (*(sl.st.v.ptr + 7) >= '1')))) |
| 1703 | msg->flags |= HTTP_MSGF_VER_11; |
| 1704 | |
| 1705 | n = txn->status / 100; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1706 | if (n < 1 || n > 5) |
| 1707 | n = 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1708 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1709 | /* when the client triggers a 4xx from the server, it's most often due |
| 1710 | * to a missing object or permission. These events should be tracked |
| 1711 | * because if they happen often, it may indicate a brute force or a |
| 1712 | * vulnerability scan. |
| 1713 | */ |
| 1714 | if (n == 4) |
| 1715 | stream_inc_http_err_ctr(s); |
| 1716 | |
| 1717 | if (objt_server(s->target)) |
| 1718 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.p.http.rsp[n], 1); |
| 1719 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1720 | /* Adjust server's health based on status code. Note: status codes 501 |
| 1721 | * and 505 are triggered on demand by client request, so we must not |
| 1722 | * count them as server failures. |
| 1723 | */ |
| 1724 | if (objt_server(s->target)) { |
| 1725 | if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505)) |
| 1726 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
| 1727 | else |
| 1728 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
| 1729 | } |
| 1730 | |
| 1731 | /* |
| 1732 | * We may be facing a 100-continue response, or any other informational |
| 1733 | * 1xx response which is non-final, in which case this is not the right |
| 1734 | * response, and we're waiting for the next one. Let's allow this response |
| 1735 | * to go to the client and wait for the next one. There's an exception for |
| 1736 | * 101 which is used later in the code to switch protocols. |
| 1737 | */ |
| 1738 | if (txn->status < 200 && |
| 1739 | (txn->status == 100 || txn->status >= 102)) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1740 | //FLT_STRM_CB(s, flt_htx_reset(s, http, htx)); |
| 1741 | c_adv(rep, htx->data); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1742 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 1743 | txn->status = 0; |
| 1744 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1745 | return 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | /* |
| 1749 | * 2: check for cacheability. |
| 1750 | */ |
| 1751 | |
| 1752 | switch (txn->status) { |
| 1753 | case 200: |
| 1754 | case 203: |
| 1755 | case 204: |
| 1756 | case 206: |
| 1757 | case 300: |
| 1758 | case 301: |
| 1759 | case 404: |
| 1760 | case 405: |
| 1761 | case 410: |
| 1762 | case 414: |
| 1763 | case 501: |
| 1764 | break; |
| 1765 | default: |
| 1766 | /* RFC7231#6.1: |
| 1767 | * Responses with status codes that are defined as |
| 1768 | * cacheable by default (e.g., 200, 203, 204, 206, |
| 1769 | * 300, 301, 404, 405, 410, 414, and 501 in this |
| 1770 | * specification) can be reused by a cache with |
| 1771 | * heuristic expiration unless otherwise indicated |
| 1772 | * by the method definition or explicit cache |
| 1773 | * controls [RFC7234]; all other status codes are |
| 1774 | * not cacheable by default. |
| 1775 | */ |
| 1776 | txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK); |
| 1777 | break; |
| 1778 | } |
| 1779 | |
| 1780 | /* |
| 1781 | * 3: we may need to capture headers |
| 1782 | */ |
| 1783 | s->logs.logwait &= ~LW_RESP; |
| 1784 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1785 | htx_capture_headers(htx, s->res_cap, sess->fe->rsp_cap); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1786 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1787 | /* Skip parsing if no content length is possible. */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1788 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 1789 | txn->status == 101)) { |
| 1790 | /* Either we've established an explicit tunnel, or we're |
| 1791 | * switching the protocol. In both cases, we're very unlikely |
| 1792 | * to understand the next protocols. We have to switch to tunnel |
| 1793 | * mode, so that we transfer the request and responses then let |
| 1794 | * this protocol pass unmodified. When we later implement specific |
| 1795 | * parsers for such protocols, we'll want to check the Upgrade |
| 1796 | * header which contains information about that protocol for |
| 1797 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 1798 | */ |
| 1799 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1800 | } |
| 1801 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1802 | /* we want to have the response time before we start processing it */ |
| 1803 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 1804 | |
| 1805 | /* end of job, return OK */ |
| 1806 | rep->analysers &= ~an_bit; |
| 1807 | rep->analyse_exp = TICK_ETERNITY; |
| 1808 | channel_auto_close(rep); |
| 1809 | return 1; |
| 1810 | |
| 1811 | abort_keep_alive: |
| 1812 | /* A keep-alive request to the server failed on a network error. |
| 1813 | * The client is required to retry. We need to close without returning |
| 1814 | * any other information so that the client retries. |
| 1815 | */ |
| 1816 | txn->status = 0; |
| 1817 | rep->analysers &= AN_RES_FLT_END; |
| 1818 | s->req.analysers &= AN_REQ_FLT_END; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1819 | s->logs.logwait = 0; |
| 1820 | s->logs.level = 0; |
| 1821 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1822 | htx_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1823 | return 0; |
| 1824 | } |
| 1825 | |
| 1826 | /* This function performs all the processing enabled for the current response. |
| 1827 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 1828 | * and updates s->res.analysers. It might make sense to explode it into several |
| 1829 | * other functions. It works like process_request (see indications above). |
| 1830 | */ |
| 1831 | int htx_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px) |
| 1832 | { |
| 1833 | struct session *sess = s->sess; |
| 1834 | struct http_txn *txn = s->txn; |
| 1835 | struct http_msg *msg = &txn->rsp; |
| 1836 | struct proxy *cur_proxy; |
| 1837 | struct cond_wordlist *wl; |
| 1838 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 1839 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1840 | // TODO: Disabled for now |
| 1841 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 1842 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
| 1843 | rep->analyse_exp = TICK_ETERNITY; |
| 1844 | rep->analysers &= ~an_bit; |
| 1845 | return 1; |
| 1846 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1847 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
| 1848 | now_ms, __FUNCTION__, |
| 1849 | s, |
| 1850 | rep, |
| 1851 | rep->rex, rep->wex, |
| 1852 | rep->flags, |
| 1853 | ci_data(rep), |
| 1854 | rep->analysers); |
| 1855 | |
| 1856 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 1857 | return 0; |
| 1858 | |
| 1859 | /* The stats applet needs to adjust the Connection header but we don't |
| 1860 | * apply any filter there. |
| 1861 | */ |
| 1862 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 1863 | rep->analysers &= ~an_bit; |
| 1864 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1865 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | /* |
| 1869 | * We will have to evaluate the filters. |
| 1870 | * As opposed to version 1.2, now they will be evaluated in the |
| 1871 | * filters order and not in the header order. This means that |
| 1872 | * each filter has to be validated among all headers. |
| 1873 | * |
| 1874 | * Filters are tried with ->be first, then with ->fe if it is |
| 1875 | * different from ->be. |
| 1876 | * |
| 1877 | * Maybe we are in resume condiion. In this case I choose the |
| 1878 | * "struct proxy" which contains the rule list matching the resume |
| 1879 | * pointer. If none of theses "struct proxy" match, I initialise |
| 1880 | * the process with the first one. |
| 1881 | * |
| 1882 | * In fact, I check only correspondance betwwen the current list |
| 1883 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 1884 | * the loop with the ->be. |
| 1885 | */ |
| 1886 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 1887 | cur_proxy = sess->fe; |
| 1888 | else |
| 1889 | cur_proxy = s->be; |
| 1890 | while (1) { |
| 1891 | struct proxy *rule_set = cur_proxy; |
| 1892 | |
| 1893 | /* evaluate http-response rules */ |
| 1894 | if (ret == HTTP_RULE_RES_CONT) { |
| 1895 | ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s); |
| 1896 | |
| 1897 | if (ret == HTTP_RULE_RES_BADREQ) |
| 1898 | goto return_srv_prx_502; |
| 1899 | |
| 1900 | if (ret == HTTP_RULE_RES_DONE) { |
| 1901 | rep->analysers &= ~an_bit; |
| 1902 | rep->analyse_exp = TICK_ETERNITY; |
| 1903 | return 1; |
| 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | /* we need to be called again. */ |
| 1908 | if (ret == HTTP_RULE_RES_YIELD) { |
| 1909 | channel_dont_close(rep); |
| 1910 | return 0; |
| 1911 | } |
| 1912 | |
| 1913 | /* try headers filters */ |
| 1914 | if (rule_set->rsp_exp != NULL) { |
| 1915 | if (apply_filters_to_response(s, rep, rule_set) < 0) { |
| 1916 | return_bad_resp: |
| 1917 | if (objt_server(s->target)) { |
| 1918 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
| 1919 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 1920 | } |
| 1921 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
| 1922 | return_srv_prx_502: |
| 1923 | rep->analysers &= AN_RES_FLT_END; |
| 1924 | txn->status = 502; |
| 1925 | s->logs.t_data = -1; /* was not a valid response */ |
| 1926 | s->si[1].flags |= SI_FL_NOLINGER; |
| 1927 | channel_truncate(rep); |
| 1928 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 1929 | if (!(s->flags & SF_ERR_MASK)) |
| 1930 | s->flags |= SF_ERR_PRXCOND; |
| 1931 | if (!(s->flags & SF_FINST_MASK)) |
| 1932 | s->flags |= SF_FINST_H; |
| 1933 | return 0; |
| 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | /* has the response been denied ? */ |
| 1938 | if (txn->flags & TX_SVDENY) { |
| 1939 | if (objt_server(s->target)) |
| 1940 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
| 1941 | |
| 1942 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 1943 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
| 1944 | if (sess->listener->counters) |
| 1945 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
| 1946 | |
| 1947 | goto return_srv_prx_502; |
| 1948 | } |
| 1949 | |
| 1950 | /* add response headers from the rule sets in the same order */ |
| 1951 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
| 1952 | if (txn->status < 200 && txn->status != 101) |
| 1953 | break; |
| 1954 | if (wl->cond) { |
| 1955 | int ret = acl_exec_cond(wl->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 1956 | ret = acl_pass(ret); |
| 1957 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 1958 | ret = !ret; |
| 1959 | if (!ret) |
| 1960 | continue; |
| 1961 | } |
| 1962 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, wl->s, strlen(wl->s)) < 0)) |
| 1963 | goto return_bad_resp; |
| 1964 | } |
| 1965 | |
| 1966 | /* check whether we're already working on the frontend */ |
| 1967 | if (cur_proxy == sess->fe) |
| 1968 | break; |
| 1969 | cur_proxy = sess->fe; |
| 1970 | } |
| 1971 | |
| 1972 | /* After this point, this anayzer can't return yield, so we can |
| 1973 | * remove the bit corresponding to this analyzer from the list. |
| 1974 | * |
| 1975 | * Note that the intermediate returns and goto found previously |
| 1976 | * reset the analyzers. |
| 1977 | */ |
| 1978 | rep->analysers &= ~an_bit; |
| 1979 | rep->analyse_exp = TICK_ETERNITY; |
| 1980 | |
| 1981 | /* OK that's all we can do for 1xx responses */ |
| 1982 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1983 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1984 | |
| 1985 | /* |
| 1986 | * Now check for a server cookie. |
| 1987 | */ |
| 1988 | if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE)) |
| 1989 | manage_server_side_cookies(s, rep); |
| 1990 | |
| 1991 | /* |
| 1992 | * Check for cache-control or pragma headers if required. |
| 1993 | */ |
| 1994 | if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)) |
| 1995 | check_response_for_cacheability(s, rep); |
| 1996 | |
| 1997 | /* |
| 1998 | * Add server cookie in the response if needed |
| 1999 | */ |
| 2000 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 2001 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
| 2002 | (!(s->flags & SF_DIRECT) || |
| 2003 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 2004 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 2005 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 2006 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 2007 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 2008 | !(s->flags & SF_IGNORE_PRST)) { |
| 2009 | /* the server is known, it's not the one the client requested, or the |
| 2010 | * cookie's last seen date needs to be refreshed. We have to |
| 2011 | * insert a set-cookie here, except if we want to insert only on POST |
| 2012 | * requests and this one isn't. Note that servers which don't have cookies |
| 2013 | * (eg: some backup servers) will return a full cookie removal request. |
| 2014 | */ |
| 2015 | if (!objt_server(s->target)->cookie) { |
| 2016 | chunk_printf(&trash, |
| 2017 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 2018 | s->be->cookie_name); |
| 2019 | } |
| 2020 | else { |
| 2021 | chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie); |
| 2022 | |
| 2023 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 2024 | /* emit last_date, which is mandatory */ |
| 2025 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 2026 | s30tob64((date.tv_sec+3) >> 2, |
| 2027 | trash.area + trash.data); |
| 2028 | trash.data += 5; |
| 2029 | |
| 2030 | if (s->be->cookie_maxlife) { |
| 2031 | /* emit first_date, which is either the original one or |
| 2032 | * the current date. |
| 2033 | */ |
| 2034 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 2035 | s30tob64(txn->cookie_first_date ? |
| 2036 | txn->cookie_first_date >> 2 : |
| 2037 | (date.tv_sec+3) >> 2, |
| 2038 | trash.area + trash.data); |
| 2039 | trash.data += 5; |
| 2040 | } |
| 2041 | } |
| 2042 | chunk_appendf(&trash, "; path=/"); |
| 2043 | } |
| 2044 | |
| 2045 | if (s->be->cookie_domain) |
| 2046 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
| 2047 | |
| 2048 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 2049 | chunk_appendf(&trash, "; HttpOnly"); |
| 2050 | |
| 2051 | if (s->be->ck_opts & PR_CK_SECURE) |
| 2052 | chunk_appendf(&trash, "; Secure"); |
| 2053 | |
| 2054 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.area, trash.data) < 0)) |
| 2055 | goto return_bad_resp; |
| 2056 | |
| 2057 | txn->flags &= ~TX_SCK_MASK; |
| 2058 | if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
| 2059 | /* the server did not change, only the date was updated */ |
| 2060 | txn->flags |= TX_SCK_UPDATED; |
| 2061 | else |
| 2062 | txn->flags |= TX_SCK_INSERTED; |
| 2063 | |
| 2064 | /* Here, we will tell an eventual cache on the client side that we don't |
| 2065 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 2066 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 2067 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 2068 | */ |
| 2069 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
| 2070 | |
| 2071 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 2072 | |
| 2073 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
| 2074 | "Cache-control: private", 22) < 0)) |
| 2075 | goto return_bad_resp; |
| 2076 | } |
| 2077 | } |
| 2078 | |
| 2079 | /* |
| 2080 | * Check if result will be cacheable with a cookie. |
| 2081 | * We'll block the response if security checks have caught |
| 2082 | * nasty things such as a cacheable cookie. |
| 2083 | */ |
| 2084 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 2085 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 2086 | (s->be->options & PR_O_CHK_CACHE)) { |
| 2087 | /* we're in presence of a cacheable response containing |
| 2088 | * a set-cookie header. We'll block it as requested by |
| 2089 | * the 'checkcache' option, and send an alert. |
| 2090 | */ |
| 2091 | if (objt_server(s->target)) |
| 2092 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_secu, 1); |
| 2093 | |
| 2094 | HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
| 2095 | HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
| 2096 | if (sess->listener->counters) |
| 2097 | HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
| 2098 | |
| 2099 | ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 2100 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 2101 | send_log(s->be, LOG_ALERT, |
| 2102 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 2103 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 2104 | goto return_srv_prx_502; |
| 2105 | } |
| 2106 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2107 | end: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2108 | /* Always enter in the body analyzer */ |
| 2109 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 2110 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
| 2111 | |
| 2112 | /* if the user wants to log as soon as possible, without counting |
| 2113 | * bytes from the server, then this is the right moment. We have |
| 2114 | * to temporarily assign bytes_out to log what we currently have. |
| 2115 | */ |
| 2116 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
| 2117 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
| 2118 | s->logs.bytes_out = txn->rsp.eoh; |
| 2119 | s->do_log(s); |
| 2120 | s->logs.bytes_out = 0; |
| 2121 | } |
| 2122 | return 1; |
| 2123 | } |
| 2124 | |
| 2125 | /* This function is an analyser which forwards response body (including chunk |
| 2126 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 2127 | * zero byte. The only situation where it must not be called is when we're in |
| 2128 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 2129 | * remaining data and to resync after end of body. It expects the msg_state to |
| 2130 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 2131 | * read more data, or 1 once we can go on with next request or end the stream. |
| 2132 | * |
| 2133 | * It is capable of compressing response data both in content-length mode and |
| 2134 | * in chunked mode. The state machines follows different flows depending on |
| 2135 | * whether content-length and chunked modes are used, since there are no |
| 2136 | * trailers in content-length : |
| 2137 | * |
| 2138 | * chk-mode cl-mode |
| 2139 | * ,----- BODY -----. |
| 2140 | * / \ |
| 2141 | * V size > 0 V chk-mode |
| 2142 | * .--> SIZE -------------> DATA -------------> CRLF |
| 2143 | * | | size == 0 | last byte | |
| 2144 | * | v final crlf v inspected | |
| 2145 | * | TRAILERS -----------> DONE | |
| 2146 | * | | |
| 2147 | * `----------------------------------------------' |
| 2148 | * |
| 2149 | * Compression only happens in the DATA state, and must be flushed in final |
| 2150 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 2151 | * is performed at once on final states for all bytes parsed, or when leaving |
| 2152 | * on missing data. |
| 2153 | */ |
| 2154 | int htx_response_forward_body(struct stream *s, struct channel *res, int an_bit) |
| 2155 | { |
| 2156 | struct session *sess = s->sess; |
| 2157 | struct http_txn *txn = s->txn; |
| 2158 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2159 | struct htx *htx; |
| 2160 | //int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2161 | |
| 2162 | DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n", |
| 2163 | now_ms, __FUNCTION__, |
| 2164 | s, |
| 2165 | res, |
| 2166 | res->rex, res->wex, |
| 2167 | res->flags, |
| 2168 | ci_data(res), |
| 2169 | res->analysers); |
| 2170 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2171 | htx = htx_from_buf(&res->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2172 | |
| 2173 | 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] | 2174 | ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2175 | /* Output closed while we were sending data. We must abort and |
| 2176 | * wake the other side up. |
| 2177 | */ |
| 2178 | msg->err_state = msg->msg_state; |
| 2179 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2180 | htx_end_response(s); |
| 2181 | htx_end_request(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2182 | return 1; |
| 2183 | } |
| 2184 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2185 | if (msg->msg_state == HTTP_MSG_BODY) |
| 2186 | msg->msg_state = HTTP_MSG_DATA; |
| 2187 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2188 | /* in most states, we should abort in case of early close */ |
| 2189 | channel_auto_close(res); |
| 2190 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2191 | if (res->to_forward) { |
| 2192 | /* We can't process the buffer's contents yet */ |
| 2193 | res->flags |= CF_WAKE_WRITE; |
| 2194 | goto missing_data_or_waiting; |
| 2195 | } |
| 2196 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2197 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 2198 | goto done; |
| 2199 | |
| 2200 | /* Forward all input data. We get it by removing all outgoing data not |
| 2201 | * forwarded yet from HTX data size. |
| 2202 | */ |
| 2203 | c_adv(res, htx->data - co_data(res)); |
| 2204 | |
| 2205 | /* To let the function channel_forward work as expected we must update |
| 2206 | * the channel's buffer to pretend there is no more input data. The |
| 2207 | * right length is then restored. We must do that, because when an HTX |
| 2208 | * message is stored into a buffer, it appears as full. |
| 2209 | */ |
| 2210 | b_set_data(&res->buf, co_data(res)); |
| 2211 | if (htx->extra != ULLONG_MAX) |
| 2212 | htx->extra -= channel_forward(res, htx->extra); |
| 2213 | b_set_data(&res->buf, b_size(&res->buf)); |
| 2214 | |
| 2215 | if (!(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 2216 | /* The server still sending data that should be filtered */ |
| 2217 | if (res->flags & CF_SHUTR || !HAS_DATA_FILTERS(s, res)) { |
| 2218 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 2219 | goto done; |
| 2220 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2221 | } |
| 2222 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2223 | /* Check if the end-of-message is reached and if so, switch the message |
| 2224 | * in HTTP_MSG_DONE state. |
| 2225 | */ |
| 2226 | if (htx_get_tail_type(htx) != HTX_BLK_EOM) |
| 2227 | goto missing_data_or_waiting; |
| 2228 | |
| 2229 | msg->msg_state = HTTP_MSG_DONE; |
| 2230 | |
| 2231 | done: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2232 | /* other states, DONE...TUNNEL */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2233 | channel_dont_close(res); |
| 2234 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2235 | htx_end_response(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2236 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2237 | htx_end_request(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2238 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2239 | if (res->flags & CF_SHUTW) { |
| 2240 | /* response errors are most likely due to the |
| 2241 | * client aborting the transfer. */ |
| 2242 | goto aborted_xfer; |
| 2243 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2244 | goto return_bad_res; |
| 2245 | } |
| 2246 | return 1; |
| 2247 | } |
| 2248 | return 0; |
| 2249 | |
| 2250 | missing_data_or_waiting: |
| 2251 | if (res->flags & CF_SHUTW) |
| 2252 | goto aborted_xfer; |
| 2253 | |
| 2254 | /* stop waiting for data if the input is closed before the end. If the |
| 2255 | * client side was already closed, it means that the client has aborted, |
| 2256 | * so we don't want to count this as a server abort. Otherwise it's a |
| 2257 | * server abort. |
| 2258 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2259 | if (msg->msg_state < HTTP_MSG_DONE && res->flags & CF_SHUTR) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2260 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
| 2261 | goto aborted_xfer; |
| 2262 | /* If we have some pending data, we continue the processing */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2263 | if (htx_is_empty(htx)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2264 | if (!(s->flags & SF_ERR_MASK)) |
| 2265 | s->flags |= SF_ERR_SRVCL; |
| 2266 | HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
| 2267 | if (objt_server(s->target)) |
| 2268 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.srv_aborts, 1); |
| 2269 | goto return_bad_res_stats_ok; |
| 2270 | } |
| 2271 | } |
| 2272 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2273 | /* When TE: chunked is used, we need to get there again to parse |
| 2274 | * 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] | 2275 | * set CF_DONTCLOSE. Similarly when there is a content-leng or if there |
| 2276 | * are filters registered on the stream, we don't want to forward a |
| 2277 | * close |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2278 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2279 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_DATA_FILTERS(s, res)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2280 | channel_dont_close(res); |
| 2281 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2282 | #if 0 // FIXME [Cf]: Probably not required now, but I need more time to think |
| 2283 | // about if |
| 2284 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2285 | /* We know that more data are expected, but we couldn't send more that |
| 2286 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
| 2287 | * system knows it must not set a PUSH on this first part. Interactive |
| 2288 | * modes are already handled by the stream sock layer. We must not do |
| 2289 | * this in content-length mode because it could present the MSG_MORE |
| 2290 | * flag with the last block of forwarded data, which would cause an |
| 2291 | * additional delay to be observed by the receiver. |
| 2292 | */ |
| 2293 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING)) |
| 2294 | res->flags |= CF_EXPECT_MORE; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2295 | #endif |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2296 | |
| 2297 | /* the stream handler will take care of timeouts and errors */ |
| 2298 | return 0; |
| 2299 | |
| 2300 | return_bad_res: /* let's centralize all bad responses */ |
| 2301 | HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
| 2302 | if (objt_server(s->target)) |
| 2303 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_resp, 1); |
| 2304 | |
| 2305 | return_bad_res_stats_ok: |
| 2306 | txn->rsp.err_state = txn->rsp.msg_state; |
| 2307 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 2308 | /* don't send any error message as we're in the body */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2309 | htx_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2310 | res->analysers &= AN_RES_FLT_END; |
| 2311 | s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */ |
| 2312 | if (objt_server(s->target)) |
| 2313 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
| 2314 | |
| 2315 | if (!(s->flags & SF_ERR_MASK)) |
| 2316 | s->flags |= SF_ERR_PRXCOND; |
| 2317 | if (!(s->flags & SF_FINST_MASK)) |
| 2318 | s->flags |= SF_FINST_D; |
| 2319 | return 0; |
| 2320 | |
| 2321 | aborted_xfer: |
| 2322 | txn->rsp.err_state = txn->rsp.msg_state; |
| 2323 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 2324 | /* don't send any error message as we're in the body */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2325 | htx_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2326 | res->analysers &= AN_RES_FLT_END; |
| 2327 | s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */ |
| 2328 | |
| 2329 | HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 2330 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
| 2331 | if (objt_server(s->target)) |
| 2332 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
| 2333 | |
| 2334 | if (!(s->flags & SF_ERR_MASK)) |
| 2335 | s->flags |= SF_ERR_CLICL; |
| 2336 | if (!(s->flags & SF_FINST_MASK)) |
| 2337 | s->flags |= SF_FINST_D; |
| 2338 | return 0; |
| 2339 | } |
| 2340 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 2341 | void htx_adjust_conn_mode(struct stream *s, struct http_txn *txn) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2342 | { |
| 2343 | struct proxy *fe = strm_fe(s); |
| 2344 | int tmp = TX_CON_WANT_CLO; |
| 2345 | |
| 2346 | if ((fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN) |
| 2347 | tmp = TX_CON_WANT_TUN; |
| 2348 | |
| 2349 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 2350 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2351 | } |
| 2352 | |
| 2353 | /* Perform an HTTP redirect based on the information in <rule>. The function |
| 2354 | * returns non-zero on success, or zero in case of a, irrecoverable error such |
| 2355 | * as too large a request to build a valid response. |
| 2356 | */ |
| 2357 | int htx_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn) |
| 2358 | { |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2359 | struct htx *htx = htx_from_buf(&s->req.buf); |
| 2360 | union h1_sl sl; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2361 | const char *msg_fmt; |
| 2362 | struct buffer *chunk; |
| 2363 | int ret = 0; |
| 2364 | |
| 2365 | chunk = alloc_trash_chunk(); |
| 2366 | if (!chunk) |
| 2367 | goto leave; |
| 2368 | |
| 2369 | /* build redirect message */ |
| 2370 | switch(rule->code) { |
| 2371 | case 308: |
| 2372 | msg_fmt = HTTP_308; |
| 2373 | break; |
| 2374 | case 307: |
| 2375 | msg_fmt = HTTP_307; |
| 2376 | break; |
| 2377 | case 303: |
| 2378 | msg_fmt = HTTP_303; |
| 2379 | break; |
| 2380 | case 301: |
| 2381 | msg_fmt = HTTP_301; |
| 2382 | break; |
| 2383 | case 302: |
| 2384 | default: |
| 2385 | msg_fmt = HTTP_302; |
| 2386 | break; |
| 2387 | } |
| 2388 | |
| 2389 | if (unlikely(!chunk_strcpy(chunk, msg_fmt))) |
| 2390 | goto leave; |
| 2391 | |
| 2392 | switch(rule->type) { |
| 2393 | case REDIRECT_TYPE_SCHEME: { |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2394 | struct http_hdr_ctx ctx; |
| 2395 | struct ist path, host; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2396 | |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2397 | host = ist(""); |
| 2398 | ctx.blk = NULL; |
| 2399 | if (http_find_header(htx, ist("Host"), &ctx, 0)) |
| 2400 | host = ctx.value; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2401 | |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2402 | sl = http_find_stline(htx); |
| 2403 | path = http_get_path(sl.rq.u); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2404 | /* build message using path */ |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2405 | if (path.ptr) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2406 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2407 | int qs = 0; |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2408 | while (qs < path.len) { |
| 2409 | if (*(path.ptr + qs) == '?') { |
| 2410 | path.len = qs; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2411 | break; |
| 2412 | } |
| 2413 | qs++; |
| 2414 | } |
| 2415 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2416 | } |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2417 | else |
| 2418 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2419 | |
| 2420 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2421 | /* add scheme */ |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2422 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2423 | goto leave; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2424 | } |
| 2425 | else { |
| 2426 | /* add scheme with executing log format */ |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2427 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2428 | chunk->size - chunk->data, |
| 2429 | &rule->rdr_fmt); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2430 | } |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2431 | /* add "://" + host + path */ |
| 2432 | if (!chunk_memcat(chunk, "://", 3) || |
| 2433 | !chunk_memcat(chunk, host.ptr, host.len) || |
| 2434 | !chunk_memcat(chunk, path.ptr, path.len)) |
| 2435 | goto leave; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2436 | |
| 2437 | /* append a slash at the end of the location if needed and missing */ |
| 2438 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2439 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2440 | if (chunk->data + 1 >= chunk->size) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2441 | goto leave; |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2442 | chunk->area[chunk->data++] = '/'; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2443 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2444 | break; |
| 2445 | } |
| 2446 | case REDIRECT_TYPE_PREFIX: { |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2447 | struct ist path; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2448 | |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2449 | sl = http_find_stline(htx); |
| 2450 | path = http_get_path(sl.rq.u); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2451 | /* build message using path */ |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2452 | if (path.ptr) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2453 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2454 | int qs = 0; |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2455 | while (qs < path.len) { |
| 2456 | if (*(path.ptr + qs) == '?') { |
| 2457 | path.len = qs; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2458 | break; |
| 2459 | } |
| 2460 | qs++; |
| 2461 | } |
| 2462 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2463 | } |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2464 | else |
| 2465 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2466 | |
| 2467 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2468 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2469 | * add anything, otherwise it makes it hard for the user to |
| 2470 | * configure a self-redirection. |
| 2471 | */ |
| 2472 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2473 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2474 | goto leave; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2475 | } |
| 2476 | } |
| 2477 | else { |
| 2478 | /* add prefix with executing log format */ |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2479 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2480 | chunk->size - chunk->data, |
| 2481 | &rule->rdr_fmt); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | /* add path */ |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2485 | if (!chunk_memcat(chunk, path.ptr, path.len)) |
| 2486 | goto leave; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2487 | |
| 2488 | /* append a slash at the end of the location if needed and missing */ |
| 2489 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2490 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2491 | if (chunk->data + 1 >= chunk->size) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2492 | goto leave; |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2493 | chunk->area[chunk->data++] = '/'; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2494 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2495 | break; |
| 2496 | } |
| 2497 | case REDIRECT_TYPE_LOCATION: |
| 2498 | default: |
| 2499 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2500 | /* add location */ |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2501 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2502 | goto leave; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2503 | } |
| 2504 | else { |
| 2505 | /* add location with executing log format */ |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2506 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2507 | chunk->size - chunk->data, |
| 2508 | &rule->rdr_fmt); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2509 | } |
| 2510 | break; |
| 2511 | } |
| 2512 | |
| 2513 | if (rule->cookie_len) { |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2514 | if (!chunk_memcat(chunk, "\r\nSet-Cookie: ", 14) || |
| 2515 | !chunk_memcat(chunk, rule->cookie_str, rule->cookie_len)) |
| 2516 | goto leave; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2517 | } |
| 2518 | |
| 2519 | /* add end of headers and the keep-alive/close status. */ |
| 2520 | txn->status = rule->code; |
| 2521 | /* let's log the request time */ |
| 2522 | s->logs.tv_request = now; |
| 2523 | |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2524 | /* FIXME: close for now, but it could be cool to handle the keep-alive here */ |
| 2525 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2526 | if (!chunk_memcat(chunk, "\r\nProxy-Connection: close\r\n\r\n", 29)) |
| 2527 | goto leave; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2528 | } else { |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2529 | if (!chunk_memcat(chunk, "\r\nConnection: close\r\n\r\n", 23)) |
| 2530 | goto leave; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2531 | } |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2532 | htx_reply_and_close(s, txn->status, chunk); |
| 2533 | s->req.analysers &= AN_REQ_FLT_END; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2534 | |
| 2535 | if (!(s->flags & SF_ERR_MASK)) |
| 2536 | s->flags |= SF_ERR_LOCAL; |
| 2537 | if (!(s->flags & SF_FINST_MASK)) |
| 2538 | s->flags |= SF_FINST_R; |
| 2539 | |
| 2540 | ret = 1; |
Christopher Faulet | 80f14bf | 2018-10-24 11:02:25 +0200 | [diff] [blame] | 2541 | leave: |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2542 | free_trash_chunk(chunk); |
| 2543 | return ret; |
| 2544 | } |
| 2545 | |
| 2546 | /* This function terminates the request because it was completly analyzed or |
| 2547 | * because an error was triggered during the body forwarding. |
| 2548 | */ |
| 2549 | static void htx_end_request(struct stream *s) |
| 2550 | { |
| 2551 | struct channel *chn = &s->req; |
| 2552 | struct http_txn *txn = s->txn; |
| 2553 | |
| 2554 | DPRINTF(stderr,"[%u] %s: stream=%p states=%s,%s req->analysers=0x%08x res->analysers=0x%08x\n", |
| 2555 | now_ms, __FUNCTION__, s, |
| 2556 | h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state), |
| 2557 | s->req.analysers, s->res.analysers); |
| 2558 | |
| 2559 | if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR)) { |
| 2560 | channel_abort(chn); |
| 2561 | channel_truncate(chn); |
| 2562 | goto end; |
| 2563 | } |
| 2564 | |
| 2565 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) |
| 2566 | return; |
| 2567 | |
| 2568 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
| 2569 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 2570 | /* The server has not finished to respond, so we |
| 2571 | * don't want to move in order not to upset it. |
| 2572 | */ |
| 2573 | return; |
| 2574 | } |
| 2575 | |
| 2576 | /* No need to read anymore, the request was completely parsed. |
| 2577 | * We can shut the read side unless we want to abort_on_close, |
| 2578 | * or we have a POST request. The issue with POST requests is |
| 2579 | * that some browsers still send a CRLF after the request, and |
| 2580 | * this CRLF must be read so that it does not remain in the kernel |
| 2581 | * buffers, otherwise a close could cause an RST on some systems |
| 2582 | * (eg: Linux). |
| 2583 | */ |
| 2584 | if ((!(s->be->options & PR_O_ABRT_CLOSE) || (s->si[0].flags & SI_FL_CLEAN_ABRT)) && |
| 2585 | txn->meth != HTTP_METH_POST) |
| 2586 | channel_dont_read(chn); |
| 2587 | |
| 2588 | /* if the server closes the connection, we want to immediately react |
| 2589 | * and close the socket to save packets and syscalls. |
| 2590 | */ |
| 2591 | s->si[1].flags |= SI_FL_NOHALF; |
| 2592 | |
| 2593 | /* In any case we've finished parsing the request so we must |
| 2594 | * disable Nagle when sending data because 1) we're not going |
| 2595 | * to shut this side, and 2) the server is waiting for us to |
| 2596 | * send pending data. |
| 2597 | */ |
| 2598 | chn->flags |= CF_NEVER_WAIT; |
| 2599 | |
| 2600 | /* When we get here, it means that both the request and the |
| 2601 | * response have finished receiving. Depending on the connection |
| 2602 | * mode, we'll have to wait for the last bytes to leave in either |
| 2603 | * direction, and sometimes for a close to be effective. |
| 2604 | */ |
| 2605 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 2606 | /* Tunnel mode will not have any analyser so it needs to |
| 2607 | * poll for reads. |
| 2608 | */ |
| 2609 | channel_auto_read(chn); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2610 | if (b_data(&chn->buf)) |
| 2611 | return; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2612 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 2613 | } |
| 2614 | else { |
| 2615 | /* we're not expecting any new data to come for this |
| 2616 | * transaction, so we can close it. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2617 | * |
| 2618 | * However, there is an exception if the response |
| 2619 | * length is undefined. In this case, we need to wait |
| 2620 | * the close from the server. The response will be |
| 2621 | * switched in TUNNEL mode until the end. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2622 | */ |
| 2623 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 2624 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2625 | goto check_channel_flags; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2626 | |
| 2627 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 2628 | channel_shutr_now(chn); |
| 2629 | channel_shutw_now(chn); |
| 2630 | } |
| 2631 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2632 | goto check_channel_flags; |
| 2633 | } |
| 2634 | |
| 2635 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 2636 | http_msg_closing: |
| 2637 | /* nothing else to forward, just waiting for the output buffer |
| 2638 | * to be empty and for the shutw_now to take effect. |
| 2639 | */ |
| 2640 | if (channel_is_empty(chn)) { |
| 2641 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 2642 | goto http_msg_closed; |
| 2643 | } |
| 2644 | else if (chn->flags & CF_SHUTW) { |
| 2645 | txn->req.err_state = txn->req.msg_state; |
| 2646 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2647 | goto end; |
| 2648 | } |
| 2649 | return; |
| 2650 | } |
| 2651 | |
| 2652 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 2653 | http_msg_closed: |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2654 | /* if we don't know whether the server will close, we need to hard close */ |
| 2655 | if (txn->rsp.flags & HTTP_MSGF_XFER_LEN) |
| 2656 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2657 | /* see above in MSG_DONE why we only do this in these states */ |
| 2658 | if ((!(s->be->options & PR_O_ABRT_CLOSE) || (s->si[0].flags & SI_FL_CLEAN_ABRT))) |
| 2659 | channel_dont_read(chn); |
| 2660 | goto end; |
| 2661 | } |
| 2662 | |
| 2663 | check_channel_flags: |
| 2664 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 2665 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 2666 | /* if we've just closed an output, let's switch */ |
| 2667 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 2668 | goto http_msg_closing; |
| 2669 | } |
| 2670 | |
| 2671 | end: |
| 2672 | chn->analysers &= AN_REQ_FLT_END; |
| 2673 | if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s)) |
| 2674 | chn->analysers |= AN_REQ_FLT_XFER_DATA; |
| 2675 | channel_auto_close(chn); |
| 2676 | channel_auto_read(chn); |
| 2677 | } |
| 2678 | |
| 2679 | |
| 2680 | /* This function terminates the response because it was completly analyzed or |
| 2681 | * because an error was triggered during the body forwarding. |
| 2682 | */ |
| 2683 | static void htx_end_response(struct stream *s) |
| 2684 | { |
| 2685 | struct channel *chn = &s->res; |
| 2686 | struct http_txn *txn = s->txn; |
| 2687 | |
| 2688 | DPRINTF(stderr,"[%u] %s: stream=%p states=%s,%s req->analysers=0x%08x res->analysers=0x%08x\n", |
| 2689 | now_ms, __FUNCTION__, s, |
| 2690 | h1_msg_state_str(txn->req.msg_state), h1_msg_state_str(txn->rsp.msg_state), |
| 2691 | s->req.analysers, s->res.analysers); |
| 2692 | |
| 2693 | if (unlikely(txn->rsp.msg_state == HTTP_MSG_ERROR)) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2694 | channel_truncate(chn); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2695 | channel_abort(&s->req); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2696 | goto end; |
| 2697 | } |
| 2698 | |
| 2699 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) |
| 2700 | return; |
| 2701 | |
| 2702 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 2703 | /* In theory, we don't need to read anymore, but we must |
| 2704 | * still monitor the server connection for a possible close |
| 2705 | * while the request is being uploaded, so we don't disable |
| 2706 | * reading. |
| 2707 | */ |
| 2708 | /* channel_dont_read(chn); */ |
| 2709 | |
| 2710 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 2711 | /* The client seems to still be sending data, probably |
| 2712 | * because we got an error response during an upload. |
| 2713 | * We have the choice of either breaking the connection |
| 2714 | * or letting it pass through. Let's do the later. |
| 2715 | */ |
| 2716 | return; |
| 2717 | } |
| 2718 | |
| 2719 | /* When we get here, it means that both the request and the |
| 2720 | * response have finished receiving. Depending on the connection |
| 2721 | * mode, we'll have to wait for the last bytes to leave in either |
| 2722 | * direction, and sometimes for a close to be effective. |
| 2723 | */ |
| 2724 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) { |
| 2725 | channel_auto_read(chn); |
| 2726 | chn->flags |= CF_NEVER_WAIT; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2727 | if (b_data(&chn->buf)) |
| 2728 | return; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2729 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 2730 | } |
| 2731 | else { |
| 2732 | /* we're not expecting any new data to come for this |
| 2733 | * transaction, so we can close it. |
| 2734 | */ |
| 2735 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 2736 | channel_shutr_now(chn); |
| 2737 | channel_shutw_now(chn); |
| 2738 | } |
| 2739 | } |
| 2740 | goto check_channel_flags; |
| 2741 | } |
| 2742 | |
| 2743 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 2744 | http_msg_closing: |
| 2745 | /* nothing else to forward, just waiting for the output buffer |
| 2746 | * to be empty and for the shutw_now to take effect. |
| 2747 | */ |
| 2748 | if (channel_is_empty(chn)) { |
| 2749 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 2750 | goto http_msg_closed; |
| 2751 | } |
| 2752 | else if (chn->flags & CF_SHUTW) { |
| 2753 | txn->rsp.err_state = txn->rsp.msg_state; |
| 2754 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 2755 | HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
| 2756 | if (objt_server(s->target)) |
| 2757 | HA_ATOMIC_ADD(&objt_server(s->target)->counters.cli_aborts, 1); |
| 2758 | goto end; |
| 2759 | } |
| 2760 | return; |
| 2761 | } |
| 2762 | |
| 2763 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 2764 | http_msg_closed: |
| 2765 | /* drop any pending data */ |
| 2766 | channel_truncate(chn); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2767 | channel_abort(&s->req); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2768 | goto end; |
| 2769 | } |
| 2770 | |
| 2771 | check_channel_flags: |
| 2772 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 2773 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 2774 | /* if we've just closed an output, let's switch */ |
| 2775 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 2776 | goto http_msg_closing; |
| 2777 | } |
| 2778 | |
| 2779 | end: |
| 2780 | chn->analysers &= AN_RES_FLT_END; |
| 2781 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s)) |
| 2782 | chn->analysers |= AN_RES_FLT_XFER_DATA; |
| 2783 | channel_auto_close(chn); |
| 2784 | channel_auto_read(chn); |
| 2785 | } |
| 2786 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 2787 | void htx_server_error(struct stream *s, struct stream_interface *si, int err, |
| 2788 | int finst, const struct buffer *msg) |
| 2789 | { |
| 2790 | channel_auto_read(si_oc(si)); |
| 2791 | channel_abort(si_oc(si)); |
| 2792 | channel_auto_close(si_oc(si)); |
| 2793 | channel_erase(si_oc(si)); |
| 2794 | channel_auto_close(si_ic(si)); |
| 2795 | channel_auto_read(si_ic(si)); |
| 2796 | if (msg) { |
| 2797 | struct channel *chn = si_ic(si); |
| 2798 | struct htx *htx; |
| 2799 | |
| 2800 | htx = htx_from_buf(&chn->buf); |
| 2801 | htx_add_oob(htx, ist2(msg->area, msg->data)); |
| 2802 | //FLT_STRM_CB(s, flt_htx_reply(s, s->txn->status, htx)); |
| 2803 | b_set_data(&chn->buf, b_size(&chn->buf)); |
| 2804 | c_adv(chn, htx->data); |
| 2805 | chn->total += htx->data; |
| 2806 | } |
| 2807 | if (!(s->flags & SF_ERR_MASK)) |
| 2808 | s->flags |= err; |
| 2809 | if (!(s->flags & SF_FINST_MASK)) |
| 2810 | s->flags |= finst; |
| 2811 | } |
| 2812 | |
| 2813 | void htx_reply_and_close(struct stream *s, short status, struct buffer *msg) |
| 2814 | { |
| 2815 | channel_auto_read(&s->req); |
| 2816 | channel_abort(&s->req); |
| 2817 | channel_auto_close(&s->req); |
| 2818 | channel_erase(&s->req); |
| 2819 | channel_truncate(&s->res); |
| 2820 | |
| 2821 | s->txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2822 | if (msg) { |
| 2823 | struct channel *chn = &s->res; |
| 2824 | struct htx *htx; |
| 2825 | |
| 2826 | htx = htx_from_buf(&chn->buf); |
| 2827 | htx_add_oob(htx, ist2(msg->area, msg->data)); |
| 2828 | //FLT_STRM_CB(s, flt_htx_reply(s, s->txn->status, htx)); |
| 2829 | b_set_data(&chn->buf, b_size(&chn->buf)); |
| 2830 | c_adv(chn, htx->data); |
| 2831 | chn->total += htx->data; |
| 2832 | } |
| 2833 | |
| 2834 | s->res.wex = tick_add_ifset(now_ms, s->res.wto); |
| 2835 | channel_auto_read(&s->res); |
| 2836 | channel_auto_close(&s->res); |
| 2837 | channel_shutr_now(&s->res); |
| 2838 | } |
| 2839 | |
| 2840 | /* |
| 2841 | * Capture headers from message <htx> according to header list <cap_hdr>, and |
| 2842 | * fill the <cap> pointers appropriately. |
| 2843 | */ |
| 2844 | static void htx_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr) |
| 2845 | { |
| 2846 | struct cap_hdr *h; |
| 2847 | int32_t pos; |
| 2848 | |
| 2849 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 2850 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 2851 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 2852 | struct ist n, v; |
| 2853 | |
| 2854 | if (type == HTX_BLK_EOH) |
| 2855 | break; |
| 2856 | if (type != HTX_BLK_HDR) |
| 2857 | continue; |
| 2858 | |
| 2859 | n = htx_get_blk_name(htx, blk); |
| 2860 | |
| 2861 | for (h = cap_hdr; h; h = h->next) { |
| 2862 | if (h->namelen && (h->namelen == n.len) && |
| 2863 | (strncasecmp(n.ptr, h->name, h->namelen) == 0)) { |
| 2864 | if (cap[h->index] == NULL) |
| 2865 | cap[h->index] = |
| 2866 | pool_alloc(h->pool); |
| 2867 | |
| 2868 | if (cap[h->index] == NULL) { |
| 2869 | ha_alert("HTTP capture : out of memory.\n"); |
| 2870 | break; |
| 2871 | } |
| 2872 | |
| 2873 | v = htx_get_blk_value(htx, blk); |
| 2874 | if (v.len > h->len) |
| 2875 | v.len = h->len; |
| 2876 | |
| 2877 | memcpy(cap[h->index], v.ptr, v.len); |
| 2878 | cap[h->index][v.len]=0; |
| 2879 | } |
| 2880 | } |
| 2881 | } |
| 2882 | } |
| 2883 | |
Christopher Faulet | 0b6bdc5 | 2018-10-24 11:05:36 +0200 | [diff] [blame] | 2884 | /* Delete a value in a header between delimiters <from> and <next>. The header |
| 2885 | * itself is delimited by <start> and <end> pointers. The number of characters |
| 2886 | * displaced is returned, and the pointer to the first delimiter is updated if |
| 2887 | * required. The function tries as much as possible to respect the following |
| 2888 | * principles : |
| 2889 | * - replace <from> delimiter by the <next> one unless <from> points to <start>, |
| 2890 | * in which case <next> is simply removed |
| 2891 | * - set exactly one space character after the new first delimiter, unless there |
| 2892 | * are not enough characters in the block being moved to do so. |
| 2893 | * - remove unneeded spaces before the previous delimiter and after the new |
| 2894 | * one. |
| 2895 | * |
| 2896 | * It is the caller's responsibility to ensure that : |
| 2897 | * - <from> points to a valid delimiter or <start> ; |
| 2898 | * - <next> points to a valid delimiter or <end> ; |
| 2899 | * - there are non-space chars before <from>. |
| 2900 | */ |
| 2901 | static int htx_del_hdr_value(char *start, char *end, char **from, char *next) |
| 2902 | { |
| 2903 | char *prev = *from; |
| 2904 | |
| 2905 | if (prev == start) { |
| 2906 | /* We're removing the first value. eat the semicolon, if <next> |
| 2907 | * is lower than <end> */ |
| 2908 | if (next < end) |
| 2909 | next++; |
| 2910 | |
| 2911 | while (next < end && HTTP_IS_SPHT(*next)) |
| 2912 | next++; |
| 2913 | } |
| 2914 | else { |
| 2915 | /* Remove useless spaces before the old delimiter. */ |
| 2916 | while (HTTP_IS_SPHT(*(prev-1))) |
| 2917 | prev--; |
| 2918 | *from = prev; |
| 2919 | |
| 2920 | /* copy the delimiter and if possible a space if we're |
| 2921 | * not at the end of the line. |
| 2922 | */ |
| 2923 | if (next < end) { |
| 2924 | *prev++ = *next++; |
| 2925 | if (prev + 1 < next) |
| 2926 | *prev++ = ' '; |
| 2927 | while (next < end && HTTP_IS_SPHT(*next)) |
| 2928 | next++; |
| 2929 | } |
| 2930 | } |
| 2931 | memmove(prev, next, end - next); |
| 2932 | return (prev - next); |
| 2933 | } |
| 2934 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 2935 | |
| 2936 | /* Formats the start line of the request (without CRLF) and puts it in <str> and |
| 2937 | * return the written lenght. The line can be truncated if it exceeds <len>. |
| 2938 | */ |
| 2939 | static size_t htx_fmt_req_line(const union h1_sl sl, char *str, size_t len) |
| 2940 | { |
| 2941 | struct ist dst = ist2(str, 0); |
| 2942 | |
| 2943 | if (istcat(&dst, sl.rq.m, len) == -1) |
| 2944 | goto end; |
| 2945 | if (dst.len + 1 > len) |
| 2946 | goto end; |
| 2947 | dst.ptr[dst.len++] = ' '; |
| 2948 | |
| 2949 | if (istcat(&dst, sl.rq.u, len) == -1) |
| 2950 | goto end; |
| 2951 | if (dst.len + 1 > len) |
| 2952 | goto end; |
| 2953 | dst.ptr[dst.len++] = ' '; |
| 2954 | |
| 2955 | istcat(&dst, sl.rq.v, len); |
| 2956 | end: |
| 2957 | return dst.len; |
| 2958 | } |
| 2959 | |
Christopher Faulet | f052354 | 2018-10-24 11:06:58 +0200 | [diff] [blame^] | 2960 | /* Formats the start line of the response (without CRLF) and puts it in <str> and |
| 2961 | * return the written lenght. The line can be truncated if it exceeds <len>. |
| 2962 | */ |
| 2963 | static size_t htx_fmt_res_line(const union h1_sl sl, char *str, size_t len) |
| 2964 | { |
| 2965 | struct ist dst = ist2(str, 0); |
| 2966 | |
| 2967 | if (istcat(&dst, sl.st.v, len) == -1) |
| 2968 | goto end; |
| 2969 | if (dst.len + 1 > len) |
| 2970 | goto end; |
| 2971 | dst.ptr[dst.len++] = ' '; |
| 2972 | |
| 2973 | if (istcat(&dst, sl.st.c, len) == -1) |
| 2974 | goto end; |
| 2975 | if (dst.len + 1 > len) |
| 2976 | goto end; |
| 2977 | dst.ptr[dst.len++] = ' '; |
| 2978 | |
| 2979 | istcat(&dst, sl.st.r, len); |
| 2980 | end: |
| 2981 | return dst.len; |
| 2982 | } |
| 2983 | |
| 2984 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 2985 | /* |
| 2986 | * Print a debug line with a start line. |
| 2987 | */ |
| 2988 | static void htx_debug_stline(const char *dir, struct stream *s, const union h1_sl sl) |
| 2989 | { |
| 2990 | struct session *sess = strm_sess(s); |
| 2991 | int max; |
| 2992 | |
| 2993 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 2994 | dir, |
| 2995 | objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1, |
| 2996 | objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1); |
| 2997 | |
| 2998 | max = sl.rq.m.len; |
| 2999 | UBOUND(max, trash.size - trash.data - 3); |
| 3000 | chunk_memcat(&trash, sl.rq.m.ptr, max); |
| 3001 | trash.area[trash.data++] = ' '; |
| 3002 | |
| 3003 | max = sl.rq.u.len; |
| 3004 | UBOUND(max, trash.size - trash.data - 2); |
| 3005 | chunk_memcat(&trash, sl.rq.u.ptr, max); |
| 3006 | trash.area[trash.data++] = ' '; |
| 3007 | |
| 3008 | max = sl.rq.v.len; |
| 3009 | UBOUND(max, trash.size - trash.data - 1); |
| 3010 | chunk_memcat(&trash, sl.rq.v.ptr, max); |
| 3011 | trash.area[trash.data++] = '\n'; |
| 3012 | |
| 3013 | shut_your_big_mouth_gcc(write(1, trash.area, trash.data)); |
| 3014 | } |
| 3015 | |
| 3016 | /* |
| 3017 | * Print a debug line with a header. |
| 3018 | */ |
| 3019 | static void htx_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v) |
| 3020 | { |
| 3021 | struct session *sess = strm_sess(s); |
| 3022 | int max; |
| 3023 | |
| 3024 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 3025 | dir, |
| 3026 | objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1, |
| 3027 | objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1); |
| 3028 | |
| 3029 | max = n.len; |
| 3030 | UBOUND(max, trash.size - trash.data - 3); |
| 3031 | chunk_memcat(&trash, n.ptr, max); |
| 3032 | trash.area[trash.data++] = ':'; |
| 3033 | trash.area[trash.data++] = ' '; |
| 3034 | |
| 3035 | max = v.len; |
| 3036 | UBOUND(max, trash.size - trash.data - 1); |
| 3037 | chunk_memcat(&trash, v.ptr, max); |
| 3038 | trash.area[trash.data++] = '\n'; |
| 3039 | |
| 3040 | shut_your_big_mouth_gcc(write(1, trash.area, trash.data)); |
| 3041 | } |
| 3042 | |
| 3043 | |
Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 3044 | __attribute__((constructor)) |
| 3045 | static void __htx_protocol_init(void) |
| 3046 | { |
| 3047 | } |
| 3048 | |
| 3049 | |
| 3050 | /* |
| 3051 | * Local variables: |
| 3052 | * c-indent-level: 8 |
| 3053 | * c-basic-offset: 8 |
| 3054 | * End: |
| 3055 | */ |