Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
| 4 | * Copyright (C) 2018 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 13 | #include <common/base64.h> |
| 14 | #include <common/config.h> |
| 15 | #include <common/debug.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 16 | #include <common/htx.h> |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 17 | #include <common/net_helper.h> |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 18 | #include <common/uri_auth.h> |
| 19 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 20 | #include <types/capture.h> |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 21 | |
| 22 | #include <proto/acl.h> |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 23 | #include <proto/action.h> |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 24 | #include <proto/channel.h> |
| 25 | #include <proto/checks.h> |
| 26 | #include <proto/connection.h> |
| 27 | #include <proto/filters.h> |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 28 | #include <proto/http_htx.h> |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 29 | #include <proto/log.h> |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 30 | #include <proto/http_ana.h> |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 31 | #include <proto/proxy.h> |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 32 | #include <proto/server.h> |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 33 | #include <proto/stream.h> |
| 34 | #include <proto/stream_interface.h> |
| 35 | #include <proto/stats.h> |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 36 | #include <proto/vars.h> |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 37 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 38 | #define TRACE_SOURCE &trace_strm |
| 39 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 40 | extern const char *stat_status_codes[]; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 41 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 42 | struct pool_head *pool_head_requri = NULL; |
| 43 | struct pool_head *pool_head_capture = NULL; |
| 44 | |
| 45 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 46 | static void http_end_request(struct stream *s); |
| 47 | static void http_end_response(struct stream *s); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 48 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 49 | static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr); |
| 50 | static int http_del_hdr_value(char *start, char *end, char **from, char *next); |
| 51 | static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len); |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 52 | static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl); |
| 53 | static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 54 | |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 55 | static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s); |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 56 | static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct stream *s); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 57 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 58 | static void http_manage_client_side_cookies(struct stream *s, struct channel *req); |
| 59 | static void http_manage_server_side_cookies(struct stream *s, struct channel *res); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 60 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 61 | static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend); |
| 62 | static int http_handle_stats(struct stream *s, struct channel *req); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 63 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 64 | static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg); |
| 65 | static int http_reply_100_continue(struct stream *s); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 66 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 67 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 68 | * processing can continue on next analysers, or zero if it either needs more |
| 69 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 70 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req.analysers |
| 71 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 72 | * abort. |
| 73 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 74 | int http_wait_for_request(struct stream *s, struct channel *req, int an_bit) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 75 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 76 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 77 | /* |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 78 | * We will analyze a complete HTTP request to check the its syntax. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 79 | * |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 80 | * Once the start line and all headers are received, we may perform a |
| 81 | * capture of the error (if any), and we will set a few fields. We also |
| 82 | * check for monitor-uri, logging and finally headers capture. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 83 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 84 | struct session *sess = s->sess; |
| 85 | struct http_txn *txn = s->txn; |
| 86 | struct http_msg *msg = &txn->req; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 87 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 88 | struct htx_sl *sl; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 89 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 90 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 91 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 92 | htx = htxbuf(&req->buf); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 93 | |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 94 | /* Parsing errors are caught here */ |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 95 | if (htx->flags & (HTX_FL_PARSING_ERROR|HTX_FL_PROCESSING_ERROR)) { |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 96 | stream_inc_http_req_ctr(s); |
| 97 | stream_inc_http_err_ctr(s); |
| 98 | proxy_inc_fe_req_ctr(sess->fe); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 99 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 100 | goto return_bad_req; |
| 101 | else |
| 102 | goto return_int_err; |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 105 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 106 | s->srv_error = http_return_srv_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 107 | |
| 108 | /* If there is data available for analysis, log the end of the idle time. */ |
Christopher Faulet | 870aad9 | 2018-11-29 15:23:46 +0100 | [diff] [blame] | 109 | if (c_data(req) && s->logs.t_idle == -1) { |
| 110 | const struct cs_info *csinfo = si_get_cs_info(objt_cs(s->si[0].end)); |
| 111 | |
| 112 | s->logs.t_idle = ((csinfo) |
| 113 | ? csinfo->t_idle |
| 114 | : tv_ms_elapsed(&s->logs.tv_accept, &now) - s->logs.t_handshake); |
| 115 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 116 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 117 | /* |
| 118 | * Now we quickly check if we have found a full valid request. |
| 119 | * If not so, we check the FD and buffer states before leaving. |
| 120 | * A full request is indicated by the fact that we have seen |
| 121 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
| 122 | * requests are checked first. When waiting for a second request |
| 123 | * on a keep-alive stream, if we encounter and error, close, t/o, |
| 124 | * we note the error in the stream flags but don't set any state. |
| 125 | * Since the error will be noted there, it will not be counted by |
| 126 | * process_stream() as a frontend error. |
| 127 | * Last, we may increase some tracked counters' http request errors on |
| 128 | * the cases that are deliberately the client's fault. For instance, |
| 129 | * a timeout or connection reset is not counted as an error. However |
| 130 | * a bad request is. |
| 131 | */ |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 132 | if (unlikely(htx_is_empty(htx) || htx->first == -1)) { |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 133 | if (htx->flags & HTX_FL_UPGRADE) |
| 134 | goto failed_keep_alive; |
| 135 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 136 | /* 1: have we encountered a read error ? */ |
| 137 | if (req->flags & CF_READ_ERROR) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 138 | if (!(s->flags & SF_ERR_MASK)) |
| 139 | s->flags |= SF_ERR_CLICL; |
| 140 | |
| 141 | if (txn->flags & TX_WAIT_NEXT_RQ) |
| 142 | goto failed_keep_alive; |
| 143 | |
| 144 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 145 | goto failed_keep_alive; |
| 146 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 147 | stream_inc_http_err_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 148 | stream_inc_http_req_ctr(s); |
| 149 | proxy_inc_fe_req_ctr(sess->fe); |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 150 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 151 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 152 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 153 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 154 | txn->status = 400; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 155 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 156 | req->analysers &= AN_REQ_FLT_END; |
| 157 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 158 | if (!(s->flags & SF_FINST_MASK)) |
| 159 | s->flags |= SF_FINST_R; |
| 160 | return 0; |
| 161 | } |
| 162 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 163 | /* 2: has the read timeout expired ? */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 164 | else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
| 165 | if (!(s->flags & SF_ERR_MASK)) |
| 166 | s->flags |= SF_ERR_CLITO; |
| 167 | |
| 168 | if (txn->flags & TX_WAIT_NEXT_RQ) |
| 169 | goto failed_keep_alive; |
| 170 | |
| 171 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 172 | goto failed_keep_alive; |
| 173 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 174 | stream_inc_http_err_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 175 | stream_inc_http_req_ctr(s); |
| 176 | proxy_inc_fe_req_ctr(sess->fe); |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 177 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 178 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 179 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 180 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 181 | txn->status = 408; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 182 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 183 | req->analysers &= AN_REQ_FLT_END; |
| 184 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 185 | if (!(s->flags & SF_FINST_MASK)) |
| 186 | s->flags |= SF_FINST_R; |
| 187 | return 0; |
| 188 | } |
| 189 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 190 | /* 3: have we encountered a close ? */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 191 | else if (req->flags & CF_SHUTR) { |
| 192 | if (!(s->flags & SF_ERR_MASK)) |
| 193 | s->flags |= SF_ERR_CLICL; |
| 194 | |
| 195 | if (txn->flags & TX_WAIT_NEXT_RQ) |
| 196 | goto failed_keep_alive; |
| 197 | |
| 198 | if (sess->fe->options & PR_O_IGNORE_PRB) |
| 199 | goto failed_keep_alive; |
| 200 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 201 | stream_inc_http_err_ctr(s); |
| 202 | stream_inc_http_req_ctr(s); |
| 203 | proxy_inc_fe_req_ctr(sess->fe); |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 204 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 205 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 206 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 207 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 208 | txn->status = 400; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 209 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 210 | req->analysers &= AN_REQ_FLT_END; |
| 211 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 212 | if (!(s->flags & SF_FINST_MASK)) |
| 213 | s->flags |= SF_FINST_R; |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | channel_dont_connect(req); |
| 218 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 219 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 220 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 221 | if (sess->listener->options & LI_O_NOQUICKACK && htx_is_not_empty(htx) && |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 222 | objt_conn(sess->origin) && conn_ctrl_ready(__objt_conn(sess->origin))) { |
| 223 | /* We need more data, we have to re-enable quick-ack in case we |
| 224 | * previously disabled it, otherwise we might cause the client |
| 225 | * to delay next data. |
| 226 | */ |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 227 | conn_set_quickack(objt_conn(sess->origin), 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 228 | } |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 229 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 230 | if ((req->flags & CF_READ_PARTIAL) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 231 | /* If the client starts to talk, let's fall back to |
| 232 | * request timeout processing. |
| 233 | */ |
| 234 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 235 | req->analyse_exp = TICK_ETERNITY; |
| 236 | } |
| 237 | |
| 238 | /* just set the request timeout once at the beginning of the request */ |
| 239 | if (!tick_isset(req->analyse_exp)) { |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 240 | if ((txn->flags & TX_WAIT_NEXT_RQ) && tick_isset(s->be->timeout.httpka)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 241 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 242 | else |
| 243 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 244 | } |
| 245 | |
| 246 | /* we're not ready yet */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 247 | DBG_TRACE_DEVEL("waiting for the request", |
| 248 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 249 | return 0; |
| 250 | |
| 251 | failed_keep_alive: |
| 252 | /* Here we process low-level errors for keep-alive requests. In |
| 253 | * short, if the request is not the first one and it experiences |
| 254 | * a timeout, read error or shutdown, we just silently close so |
| 255 | * that the client can try again. |
| 256 | */ |
| 257 | txn->status = 0; |
| 258 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 259 | req->analysers &= AN_REQ_FLT_END; |
| 260 | s->logs.logwait = 0; |
| 261 | s->logs.level = 0; |
| 262 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 263 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 264 | DBG_TRACE_DEVEL("leaving by closing K/A connection", |
| 265 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 269 | msg->msg_state = HTTP_MSG_BODY; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 270 | stream_inc_http_req_ctr(s); |
| 271 | proxy_inc_fe_req_ctr(sess->fe); /* one more valid request for this FE */ |
| 272 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 273 | /* kill the pending keep-alive timeout */ |
| 274 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 275 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 276 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 277 | BUG_ON(htx_get_first_type(htx) != HTX_BLK_REQ_SL); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 278 | sl = http_get_stline(htx); |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 279 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 280 | /* 0: we might have to print this header in debug mode */ |
| 281 | if (unlikely((global.mode & MODE_DEBUG) && |
| 282 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 283 | int32_t pos; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 284 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 285 | http_debug_stline("clireq", s, sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 286 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 287 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 288 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 289 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 290 | |
| 291 | if (type == HTX_BLK_EOH) |
| 292 | break; |
| 293 | if (type != HTX_BLK_HDR) |
| 294 | continue; |
| 295 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 296 | http_debug_hdr("clihdr", s, |
| 297 | htx_get_blk_name(htx, blk), |
| 298 | htx_get_blk_value(htx, blk)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 299 | } |
| 300 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 301 | |
| 302 | /* |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 303 | * 1: identify the method and the version. Also set HTTP flags |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 304 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 305 | txn->meth = sl->info.req.meth; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 306 | if (sl->flags & HTX_SL_F_VER_11) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 307 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 308 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Christopher Faulet | 834eee7 | 2019-02-18 11:35:02 +0100 | [diff] [blame] | 309 | msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK); |
Christopher Faulet | b2db4fa | 2018-11-27 16:51:09 +0100 | [diff] [blame] | 310 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 311 | msg->flags |= HTTP_MSGF_BODYLESS; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 312 | |
| 313 | /* we can make use of server redirect on GET and HEAD */ |
| 314 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 315 | s->flags |= SF_REDIRECTABLE; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 316 | else if (txn->meth == HTTP_METH_OTHER && isteqi(htx_sl_req_meth(sl), ist("PRI"))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 317 | /* PRI is reserved for the HTTP/2 preface */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 318 | goto return_bad_req; |
| 319 | } |
| 320 | |
| 321 | /* |
Christopher Faulet | 6072beb | 2020-02-18 15:34:58 +0100 | [diff] [blame] | 322 | * 2: check if the URI matches the monitor_uri. We have to do this for |
| 323 | * every request which gets in, because the monitor-uri is defined by |
| 324 | * the frontend. If the monitor-uri starts with a '/', the matching is |
| 325 | * done against the request's path. Otherwise, the request's uri is |
| 326 | * used. It is a workaround to let HTTP/2 health-checks work as |
| 327 | * expected. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 328 | */ |
| 329 | if (unlikely((sess->fe->monitor_uri_len != 0) && |
Christopher Faulet | 6072beb | 2020-02-18 15:34:58 +0100 | [diff] [blame] | 330 | ((*sess->fe->monitor_uri == '/' && isteq(http_get_path(htx_sl_req_uri(sl)), |
| 331 | ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))) || |
| 332 | isteq(htx_sl_req_uri(sl), ist2(sess->fe->monitor_uri, sess->fe->monitor_uri_len))))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 333 | /* |
| 334 | * We have found the monitor URI |
| 335 | */ |
| 336 | struct acl_cond *cond; |
| 337 | |
| 338 | s->flags |= SF_MONITOR; |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 339 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 340 | |
| 341 | /* Check if we want to fail this monitor request or not */ |
| 342 | list_for_each_entry(cond, &sess->fe->mon_fail_cond, list) { |
| 343 | int ret = acl_exec_cond(cond, sess->fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 344 | |
| 345 | ret = acl_pass(ret); |
| 346 | if (cond->pol == ACL_COND_UNLESS) |
| 347 | ret = !ret; |
| 348 | |
| 349 | if (ret) { |
| 350 | /* we fail this request, let's return 503 service unavail */ |
| 351 | txn->status = 503; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 352 | if (!(s->flags & SF_ERR_MASK)) |
| 353 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
| 354 | goto return_prx_cond; |
| 355 | } |
| 356 | } |
| 357 | |
Joseph Herlant | c42c0e9 | 2018-11-25 10:43:27 -0800 | [diff] [blame] | 358 | /* nothing to fail, let's reply normally */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 359 | txn->status = 200; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 360 | if (!(s->flags & SF_ERR_MASK)) |
| 361 | s->flags |= SF_ERR_LOCAL; /* we don't want a real error here */ |
| 362 | goto return_prx_cond; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 367 | * Note: we cannot log anymore if the request has been |
| 368 | * classified as invalid. |
| 369 | */ |
| 370 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 371 | /* we have a complete HTTP request that we must log */ |
| 372 | if ((txn->uri = pool_alloc(pool_head_requri)) != NULL) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 373 | size_t len; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 374 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 375 | len = http_fmt_req_line(sl, txn->uri, global.tune.requri_len - 1); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 376 | txn->uri[len] = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 377 | |
| 378 | if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT))) |
| 379 | s->do_log(s); |
| 380 | } else { |
| 381 | ha_alert("HTTP logging : out of memory.\n"); |
| 382 | } |
| 383 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 384 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 385 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 386 | * we have what looks like a proxied connection instead of a connection, |
| 387 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 388 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 389 | * happen to do that despite being non-standard :-( |
| 390 | * We consider that a request not beginning with either '/' or '*' is |
| 391 | * a proxied connection, which covers both "scheme://location" and |
| 392 | * CONNECT ip:port. |
| 393 | */ |
| 394 | if ((sess->fe->options2 & PR_O2_USE_PXHDR) && |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 395 | *HTX_SL_REQ_UPTR(sl) != '/' && *HTX_SL_REQ_UPTR(sl) != '*') |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 396 | txn->flags |= TX_USE_PX_CONN; |
| 397 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 398 | /* 5: we may need to capture headers */ |
| 399 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->req_cap)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 400 | http_capture_headers(htx, s->req_cap, sess->fe->req_cap); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 401 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 402 | /* we may have to wait for the request's body */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 403 | if (s->be->options & PR_O_WREQ_BODY) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 404 | req->analysers |= AN_REQ_HTTP_BODY; |
| 405 | |
| 406 | /* |
| 407 | * RFC7234#4: |
| 408 | * A cache MUST write through requests with methods |
| 409 | * that are unsafe (Section 4.2.1 of [RFC7231]) to |
| 410 | * the origin server; i.e., a cache is not allowed |
| 411 | * to generate a reply to such a request before |
| 412 | * having forwarded the request and having received |
| 413 | * a corresponding response. |
| 414 | * |
| 415 | * RFC7231#4.2.1: |
| 416 | * Of the request methods defined by this |
| 417 | * specification, the GET, HEAD, OPTIONS, and TRACE |
| 418 | * methods are defined to be safe. |
| 419 | */ |
| 420 | if (likely(txn->meth == HTTP_METH_GET || |
| 421 | txn->meth == HTTP_METH_HEAD || |
| 422 | txn->meth == HTTP_METH_OPTIONS || |
| 423 | txn->meth == HTTP_METH_TRACE)) |
| 424 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 425 | |
| 426 | /* end of job, return OK */ |
| 427 | req->analysers &= ~an_bit; |
| 428 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 429 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 430 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 431 | return 1; |
| 432 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 433 | return_int_err: |
| 434 | txn->status = 500; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 435 | if (!(s->flags & SF_ERR_MASK)) |
| 436 | s->flags |= SF_ERR_INTERNAL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 437 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 438 | if (sess->listener->counters) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 439 | _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 440 | goto return_prx_cond; |
| 441 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 442 | return_bad_req: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 443 | txn->status = 400; |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 444 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 445 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 446 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 447 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 448 | |
| 449 | return_prx_cond: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 450 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 451 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 452 | if (!(s->flags & SF_ERR_MASK)) |
| 453 | s->flags |= SF_ERR_PRXCOND; |
| 454 | if (!(s->flags & SF_FINST_MASK)) |
| 455 | s->flags |= SF_FINST_R; |
| 456 | |
| 457 | req->analysers &= AN_REQ_FLT_END; |
| 458 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 459 | DBG_TRACE_DEVEL("leaving on error", |
| 460 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | |
| 465 | /* This stream analyser runs all HTTP request processing which is common to |
| 466 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 467 | * reqadd, stats and redirects. This is performed for the designated proxy. |
| 468 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 469 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 470 | * error, ...). |
| 471 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 472 | int http_process_req_common(struct stream *s, struct channel *req, int an_bit, struct proxy *px) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 473 | { |
| 474 | struct session *sess = s->sess; |
| 475 | struct http_txn *txn = s->txn; |
| 476 | struct http_msg *msg = &txn->req; |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 477 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 478 | struct redirect_rule *rule; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 479 | enum rule_result verdict; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 480 | struct connection *conn = objt_conn(sess->origin); |
| 481 | |
| 482 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
| 483 | /* we need more data */ |
| 484 | goto return_prx_yield; |
| 485 | } |
| 486 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 487 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 488 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 489 | htx = htxbuf(&req->buf); |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 490 | |
Christopher Faulet | 1907ccc | 2019-04-29 13:12:02 +0200 | [diff] [blame] | 491 | /* just in case we have some per-backend tracking. Only called the first |
| 492 | * execution of the analyser. */ |
| 493 | if (!s->current_rule || s->current_rule_list != &px->http_req_rules) |
| 494 | stream_inc_be_http_req_ctr(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 495 | |
| 496 | /* evaluate http-request rules */ |
| 497 | if (!LIST_ISEMPTY(&px->http_req_rules)) { |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 498 | verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 499 | |
| 500 | switch (verdict) { |
| 501 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 502 | goto return_prx_yield; |
| 503 | |
| 504 | case HTTP_RULE_RES_CONT: |
| 505 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 506 | break; |
| 507 | |
| 508 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 509 | if (txn->flags & TX_CLTARPIT) |
| 510 | goto tarpit; |
| 511 | goto deny; |
| 512 | |
| 513 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */ |
| 514 | goto return_prx_cond; |
| 515 | |
| 516 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
| 517 | goto done; |
| 518 | |
| 519 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 520 | goto return_bad_req; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 521 | |
| 522 | case HTTP_RULE_RES_ERROR: /* failed with a bad request */ |
| 523 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | if (conn && (conn->flags & CO_FL_EARLY_DATA) && |
Olivier Houchard | 220a26c | 2020-01-23 14:57:36 +0100 | [diff] [blame] | 528 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) { |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 529 | struct http_hdr_ctx ctx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 530 | |
Christopher Faulet | ff2759f | 2018-10-24 11:13:16 +0200 | [diff] [blame] | 531 | ctx.blk = NULL; |
| 532 | if (!http_find_header(htx, ist("Early-Data"), &ctx, 0)) { |
| 533 | if (unlikely(!http_add_header(htx, ist("Early-Data"), ist("1")))) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 534 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 535 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /* OK at this stage, we know that the request was accepted according to |
| 539 | * the http-request rules, we can check for the stats. Note that the |
| 540 | * URI is detected *before* the req* rules in order not to be affected |
| 541 | * by a possible reqrep, while they are processed *after* so that a |
| 542 | * reqdeny can still block them. This clearly needs to change in 1.6! |
| 543 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 544 | if (!s->target && http_stats_check_uri(s, txn, px)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 545 | s->target = &http_stats_applet.obj_type; |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 546 | if (unlikely(!si_register_handler(&s->si[1], objt_applet(s->target)))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 547 | s->logs.tv_request = now; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 548 | if (!(s->flags & SF_ERR_MASK)) |
| 549 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 550 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | /* parse the whole stats request and extract the relevant information */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 554 | http_handle_stats(s, req); |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 555 | verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 556 | /* not all actions implemented: deny, allow, auth */ |
| 557 | |
| 558 | if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */ |
| 559 | goto deny; |
| 560 | |
| 561 | if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */ |
| 562 | goto return_prx_cond; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 563 | |
| 564 | if (verdict == HTTP_RULE_RES_BADREQ) /* failed with a bad request */ |
| 565 | goto return_bad_req; |
| 566 | |
| 567 | if (verdict == HTTP_RULE_RES_ERROR) /* failed with a bad request */ |
| 568 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 569 | } |
| 570 | |
Christopher Faulet | 2571bc6 | 2019-03-01 11:44:26 +0100 | [diff] [blame] | 571 | /* Proceed with the applets now. */ |
| 572 | if (unlikely(objt_applet(s->target))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 573 | if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 574 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 575 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 576 | if (http_handle_expect_hdr(s, htx, msg) == -1) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 577 | goto return_int_err; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 578 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 579 | if (!(s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 580 | s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 581 | if (!(s->flags & SF_FINST_MASK)) |
| 582 | s->flags |= SF_FINST_R; |
| 583 | |
| 584 | /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */ |
| 585 | req->analysers &= (AN_REQ_HTTP_BODY | AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END); |
| 586 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 587 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Christopher Faulet | bcf242a | 2019-03-01 11:36:26 +0100 | [diff] [blame] | 588 | |
| 589 | req->flags |= CF_SEND_DONTWAIT; |
| 590 | s->flags |= SF_ASSIGNED; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 591 | goto done; |
| 592 | } |
| 593 | |
| 594 | /* check whether we have some ACLs set to redirect this request */ |
| 595 | list_for_each_entry(rule, &px->redirect_rules, list) { |
| 596 | if (rule->cond) { |
| 597 | int ret; |
| 598 | |
| 599 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 600 | ret = acl_pass(ret); |
| 601 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 602 | ret = !ret; |
| 603 | if (!ret) |
| 604 | continue; |
| 605 | } |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 606 | if (!http_apply_redirect_rule(rule, s, txn)) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 607 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 608 | goto done; |
| 609 | } |
| 610 | |
| 611 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 612 | * If this happens, then the data will not come immediately, so we must |
| 613 | * send all what we have without waiting. Note that due to the small gain |
| 614 | * in waiting for the body of the request, it's easier to simply put the |
| 615 | * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 616 | * itself once used. |
| 617 | */ |
| 618 | req->flags |= CF_SEND_DONTWAIT; |
| 619 | |
| 620 | done: /* done with this analyser, continue with next ones that the calling |
| 621 | * points will have set, if any. |
| 622 | */ |
| 623 | req->analyse_exp = TICK_ETERNITY; |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 624 | done_without_exp: /* done with this analyser, but don't reset the analyse_exp. */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 625 | req->analysers &= ~an_bit; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 626 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 627 | return 1; |
| 628 | |
| 629 | tarpit: |
| 630 | /* Allow cookie logging |
| 631 | */ |
| 632 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 633 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 634 | |
| 635 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 636 | * which may be the same as the connect timeout if unspecified. |
| 637 | * If unset, then set it to zero because we really want it to |
| 638 | * eventually expire. We build the tarpit as an analyser. |
| 639 | */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 640 | channel_htx_erase(&s->req, htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 641 | |
| 642 | /* wipe the request out so that we can drop the connection early |
| 643 | * if the client closes first. |
| 644 | */ |
| 645 | channel_dont_connect(req); |
| 646 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 647 | req->analysers &= AN_REQ_FLT_END; /* remove switching rules etc... */ |
| 648 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 649 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 650 | if (!req->analyse_exp) |
| 651 | req->analyse_exp = tick_add(now_ms, 0); |
| 652 | stream_inc_http_err_ctr(s); |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 653 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 654 | if (s->flags & SF_BE_ASSIGNED) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 655 | _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 656 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 657 | _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 658 | goto done_without_exp; |
| 659 | |
| 660 | deny: /* this request was blocked (denied) */ |
| 661 | |
| 662 | /* Allow cookie logging |
| 663 | */ |
| 664 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 665 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 666 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 667 | s->logs.tv_request = now; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 668 | stream_inc_http_err_ctr(s); |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 669 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 670 | if (s->flags & SF_BE_ASSIGNED) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 671 | _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 672 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 673 | _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 674 | goto return_prx_err; |
| 675 | |
| 676 | return_int_err: |
| 677 | txn->status = 500; |
| 678 | if (!(s->flags & SF_ERR_MASK)) |
| 679 | s->flags |= SF_ERR_INTERNAL; |
| 680 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 681 | if (s->flags & SF_BE_ASSIGNED) |
| 682 | _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 683 | if (sess->listener->counters) |
| 684 | _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1); |
| 685 | goto return_prx_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 686 | |
| 687 | return_bad_req: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 688 | txn->status = 400; |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 689 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 690 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 691 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 692 | /* fall through */ |
| 693 | |
| 694 | return_prx_err: |
| 695 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 696 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 697 | |
| 698 | return_prx_cond: |
| 699 | if (!(s->flags & SF_ERR_MASK)) |
| 700 | s->flags |= SF_ERR_PRXCOND; |
| 701 | if (!(s->flags & SF_FINST_MASK)) |
| 702 | s->flags |= SF_FINST_R; |
| 703 | |
| 704 | req->analysers &= AN_REQ_FLT_END; |
| 705 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 706 | DBG_TRACE_DEVEL("leaving on error", |
| 707 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 708 | return 0; |
| 709 | |
| 710 | return_prx_yield: |
| 711 | channel_dont_connect(req); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 712 | DBG_TRACE_DEVEL("waiting for more data", |
| 713 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 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 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 722 | int http_process_request(struct stream *s, struct channel *req, int an_bit) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 723 | { |
| 724 | struct session *sess = s->sess; |
| 725 | struct http_txn *txn = s->txn; |
| 726 | struct http_msg *msg = &txn->req; |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 727 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 728 | struct connection *cli_conn = objt_conn(strm_sess(s)->origin); |
| 729 | |
| 730 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
| 731 | /* we need more data */ |
| 732 | channel_dont_connect(req); |
| 733 | return 0; |
| 734 | } |
| 735 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 736 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 737 | |
| 738 | /* |
| 739 | * Right now, we know that we have processed the entire headers |
| 740 | * and that unwanted requests have been filtered out. We can do |
| 741 | * whatever we want with the remaining request. Also, now we |
| 742 | * may have separate values for ->fe, ->be. |
| 743 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 744 | htx = htxbuf(&req->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 745 | |
| 746 | /* |
| 747 | * If HTTP PROXY is set we simply get remote server address parsing |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 748 | * incoming request. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 749 | */ |
| 750 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SF_ADDR_SET)) { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 751 | struct htx_sl *sl; |
| 752 | struct ist uri, path; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 753 | |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 754 | if (!sockaddr_alloc(&s->target_addr)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 755 | if (!(s->flags & SF_ERR_MASK)) |
| 756 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 757 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 758 | } |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 759 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 760 | uri = htx_sl_req_uri(sl); |
| 761 | path = http_get_path(uri); |
Willy Tarreau | a48f4b3 | 2019-07-17 15:11:59 +0200 | [diff] [blame] | 762 | |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 763 | if (url2sa(uri.ptr, uri.len - path.len, s->target_addr, NULL) == -1) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 764 | goto return_bad_req; |
| 765 | |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 766 | s->target = &s->be->obj_type; |
| 767 | s->flags |= SF_ADDR_SET | SF_ASSIGNED; |
| 768 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 769 | /* if the path was found, we have to remove everything between |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 770 | * uri.ptr and path.ptr (excluded). If it was not found, we need |
| 771 | * to replace from all the uri by a single "/". |
| 772 | * |
| 773 | * Instead of rewritting the whole start line, we just update |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 774 | * the star-line URI. Some space will be lost but it should be |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 775 | * insignificant. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 776 | */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 777 | istcpy(&uri, (path.len ? path : ist("/")), uri.len); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | /* |
| 781 | * 7: Now we can work with the cookies. |
| 782 | * Note that doing so might move headers in the request, but |
| 783 | * the fields will stay coherent and the URI will not move. |
| 784 | * This should only be performed in the backend. |
| 785 | */ |
| 786 | if (s->be->cookie_name || sess->fe->capture_name) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 787 | http_manage_client_side_cookies(s, req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 788 | |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 789 | /* 8: Generate unique ID if a "unique-id-format" is defined. |
| 790 | * |
| 791 | * A unique ID is generated even when it is not sent to ensure that the ID can make use of |
| 792 | * fetches only available in the HTTP request processing stage. |
| 793 | */ |
| 794 | if (!LIST_ISEMPTY(&sess->fe->format_unique_id)) { |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 795 | struct ist unique_id = stream_generate_unique_id(s, &sess->fe->format_unique_id); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 796 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 797 | if (!isttest(unique_id)) { |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 798 | if (!(s->flags & SF_ERR_MASK)) |
| 799 | s->flags |= SF_ERR_RESOURCE; |
| 800 | goto return_int_err; |
| 801 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 802 | |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 803 | /* send unique ID if a "unique-id-header" is defined */ |
Tim Duesterhus | 0643b0e | 2020-03-05 17:56:35 +0100 | [diff] [blame] | 804 | if (isttest(sess->fe->header_unique_id) && |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 805 | unlikely(!http_add_header(htx, sess->fe->header_unique_id, s->unique_id))) |
Tim Duesterhus | 2825b4b | 2020-02-28 15:13:34 +0100 | [diff] [blame] | 806 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | /* |
| 810 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 811 | * asks for it. |
| 812 | */ |
| 813 | if ((sess->fe->options | s->be->options) & PR_O_FWDFOR) { |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 814 | struct http_hdr_ctx ctx = { .blk = NULL }; |
| 815 | struct ist hdr = ist2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : sess->fe->fwdfor_hdr_name, |
| 816 | s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : sess->fe->fwdfor_hdr_len); |
| 817 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 818 | if (!((sess->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 819 | http_find_header(htx, hdr, &ctx, 0)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 820 | /* The header is set to be added only if none is present |
| 821 | * and we found it, so don't do anything. |
| 822 | */ |
| 823 | } |
Willy Tarreau | a48f4b3 | 2019-07-17 15:11:59 +0200 | [diff] [blame] | 824 | else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 825 | /* Add an X-Forwarded-For header unless the source IP is |
| 826 | * in the 'except' network range. |
| 827 | */ |
| 828 | if ((!sess->fe->except_mask.s_addr || |
Willy Tarreau | a48f4b3 | 2019-07-17 15:11:59 +0200 | [diff] [blame] | 829 | (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & sess->fe->except_mask.s_addr) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 830 | != sess->fe->except_net.s_addr) && |
| 831 | (!s->be->except_mask.s_addr || |
Willy Tarreau | a48f4b3 | 2019-07-17 15:11:59 +0200 | [diff] [blame] | 832 | (((struct sockaddr_in *)cli_conn->src)->sin_addr.s_addr & s->be->except_mask.s_addr) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 833 | != s->be->except_net.s_addr)) { |
Willy Tarreau | a48f4b3 | 2019-07-17 15:11:59 +0200 | [diff] [blame] | 834 | unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->src)->sin_addr; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 835 | |
| 836 | /* Note: we rely on the backend to get the header name to be used for |
| 837 | * x-forwarded-for, because the header is really meant for the backends. |
| 838 | * However, if the backend did not specify any option, we have to rely |
| 839 | * on the frontend's header name. |
| 840 | */ |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 841 | chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 842 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 843 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 844 | } |
| 845 | } |
Willy Tarreau | a48f4b3 | 2019-07-17 15:11:59 +0200 | [diff] [blame] | 846 | else if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET6) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 847 | /* FIXME: for the sake of completeness, we should also support |
| 848 | * 'except' here, although it is mostly useless in this case. |
| 849 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 850 | char pn[INET6_ADDRSTRLEN]; |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 851 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 852 | inet_ntop(AF_INET6, |
Willy Tarreau | a48f4b3 | 2019-07-17 15:11:59 +0200 | [diff] [blame] | 853 | (const void *)&((struct sockaddr_in6 *)(cli_conn->src))->sin6_addr, |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 854 | pn, sizeof(pn)); |
| 855 | |
| 856 | /* Note: we rely on the backend to get the header name to be used for |
| 857 | * x-forwarded-for, because the header is really meant for the backends. |
| 858 | * However, if the backend did not specify any option, we have to rely |
| 859 | * on the frontend's header name. |
| 860 | */ |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 861 | chunk_printf(&trash, "%s", pn); |
| 862 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 863 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | |
| 867 | /* |
| 868 | * 10: add X-Original-To if either the frontend or the backend |
| 869 | * asks for it. |
| 870 | */ |
| 871 | if ((sess->fe->options | s->be->options) & PR_O_ORGTO) { |
| 872 | |
| 873 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | a48f4b3 | 2019-07-17 15:11:59 +0200 | [diff] [blame] | 874 | if (cli_conn && conn_get_src(cli_conn) && cli_conn->src->ss_family == AF_INET && conn_get_dst(cli_conn)) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 875 | /* Add an X-Original-To header unless the destination IP is |
| 876 | * in the 'except' network range. |
| 877 | */ |
Willy Tarreau | a48f4b3 | 2019-07-17 15:11:59 +0200 | [diff] [blame] | 878 | if (cli_conn->dst->ss_family == AF_INET && |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 879 | ((!sess->fe->except_mask_to.s_addr || |
Willy Tarreau | a48f4b3 | 2019-07-17 15:11:59 +0200 | [diff] [blame] | 880 | (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & sess->fe->except_mask_to.s_addr) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 881 | != sess->fe->except_to.s_addr) && |
| 882 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | a48f4b3 | 2019-07-17 15:11:59 +0200 | [diff] [blame] | 883 | (((struct sockaddr_in *)cli_conn->dst)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 884 | != s->be->except_to.s_addr))) { |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 885 | struct ist hdr; |
Willy Tarreau | a48f4b3 | 2019-07-17 15:11:59 +0200 | [diff] [blame] | 886 | unsigned char *pn = (unsigned char *)&((struct sockaddr_in *)cli_conn->dst)->sin_addr; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 887 | |
| 888 | /* Note: we rely on the backend to get the header name to be used for |
| 889 | * x-original-to, because the header is really meant for the backends. |
| 890 | * However, if the backend did not specify any option, we have to rely |
| 891 | * on the frontend's header name. |
| 892 | */ |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 893 | if (s->be->orgto_hdr_len) |
| 894 | hdr = ist2(s->be->orgto_hdr_name, s->be->orgto_hdr_len); |
| 895 | else |
| 896 | hdr = ist2(sess->fe->orgto_hdr_name, sess->fe->orgto_hdr_len); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 897 | |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 898 | chunk_printf(&trash, "%d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 899 | if (unlikely(!http_add_header(htx, hdr, ist2(trash.area, trash.data)))) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 900 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 901 | } |
| 902 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 903 | } |
| 904 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 905 | /* If we have no server assigned yet and we're balancing on url_param |
| 906 | * with a POST request, we may be interested in checking the body for |
| 907 | * that parameter. This will be done in another analyser. |
| 908 | */ |
| 909 | if (!(s->flags & (SF_ASSIGNED|SF_DIRECT)) && |
Willy Tarreau | 089eaa0 | 2019-01-14 15:17:46 +0100 | [diff] [blame] | 910 | s->txn->meth == HTTP_METH_POST && |
| 911 | (s->be->lbprm.algo & BE_LB_ALGO) == BE_LB_ALGO_PH) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 912 | channel_dont_connect(req); |
| 913 | req->analysers |= AN_REQ_HTTP_BODY; |
| 914 | } |
| 915 | |
| 916 | req->analysers &= ~AN_REQ_FLT_XFER_DATA; |
| 917 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 918 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 919 | /* We expect some data from the client. Unless we know for sure |
| 920 | * we already have a full request, we have to re-enable quick-ack |
| 921 | * in case we previously disabled it, otherwise we might cause |
| 922 | * the client to delay further data. |
| 923 | */ |
| 924 | if ((sess->listener->options & LI_O_NOQUICKACK) && |
Christopher Faulet | d7bdfb1 | 2018-10-24 11:14:34 +0200 | [diff] [blame] | 925 | (htx_get_tail_type(htx) != HTX_BLK_EOM)) |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 926 | conn_set_quickack(cli_conn, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 927 | |
| 928 | /************************************************************* |
| 929 | * OK, that's finished for the headers. We have done what we * |
| 930 | * could. Let's switch to the DATA state. * |
| 931 | ************************************************************/ |
| 932 | req->analyse_exp = TICK_ETERNITY; |
| 933 | req->analysers &= ~an_bit; |
| 934 | |
| 935 | s->logs.tv_request = now; |
| 936 | /* OK let's go on with the BODY now */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 937 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 938 | return 1; |
| 939 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 940 | return_int_err: |
| 941 | txn->status = 500; |
| 942 | if (!(s->flags & SF_ERR_MASK)) |
| 943 | s->flags |= SF_ERR_INTERNAL; |
| 944 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 945 | if (s->flags & SF_BE_ASSIGNED) |
Christopher Faulet | be20cf3 | 2020-01-24 11:41:38 +0100 | [diff] [blame] | 946 | _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 947 | if (sess->listener->counters) |
| 948 | _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1); |
| 949 | goto return_prx_cond; |
| 950 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 951 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 952 | txn->status = 400; |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 953 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 954 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 955 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 956 | /* fall through */ |
| 957 | |
| 958 | return_prx_cond: |
| 959 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 960 | |
| 961 | if (!(s->flags & SF_ERR_MASK)) |
| 962 | s->flags |= SF_ERR_PRXCOND; |
| 963 | if (!(s->flags & SF_FINST_MASK)) |
| 964 | s->flags |= SF_FINST_R; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 965 | |
| 966 | req->analysers &= AN_REQ_FLT_END; |
| 967 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 968 | DBG_TRACE_DEVEL("leaving on error", |
| 969 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 970 | return 0; |
| 971 | } |
| 972 | |
| 973 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 974 | * returns zero, at the beginning because it prevents any other processing |
| 975 | * from occurring, and at the end because it terminates the request. |
| 976 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 977 | int http_process_tarpit(struct stream *s, struct channel *req, int an_bit) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 978 | { |
| 979 | struct http_txn *txn = s->txn; |
| 980 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 981 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, &txn->req); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 982 | /* This connection is being tarpitted. The CLIENT side has |
| 983 | * already set the connect expiration date to the right |
| 984 | * timeout. We just have to check that the client is still |
| 985 | * there and that the timeout has not expired. |
| 986 | */ |
| 987 | channel_dont_connect(req); |
| 988 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 989 | !tick_is_expired(req->analyse_exp, now_ms)) { |
| 990 | DBG_TRACE_DEVEL("waiting for tarpit timeout expiry", |
| 991 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 992 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 993 | } |
| 994 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 995 | |
| 996 | /* We will set the queue timer to the time spent, just for |
| 997 | * logging purposes. We fake a 500 server error, so that the |
| 998 | * attacker will not suspect his connection has been tarpitted. |
| 999 | * It will not cause trouble to the logs because we can exclude |
| 1000 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 1001 | */ |
| 1002 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 1003 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 1004 | http_reply_and_close(s, txn->status, (!(req->flags & CF_READ_ERROR) ? http_error_message(s) : NULL)); |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 1005 | |
| 1006 | end: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1007 | req->analysers &= AN_REQ_FLT_END; |
| 1008 | req->analyse_exp = TICK_ETERNITY; |
| 1009 | |
| 1010 | if (!(s->flags & SF_ERR_MASK)) |
| 1011 | s->flags |= SF_ERR_PRXCOND; |
| 1012 | if (!(s->flags & SF_FINST_MASK)) |
| 1013 | s->flags |= SF_FINST_T; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1014 | |
| 1015 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1016 | return 0; |
| 1017 | } |
| 1018 | |
| 1019 | /* This function is an analyser which waits for the HTTP request body. It waits |
| 1020 | * for either the buffer to be full, or the full advertised contents to have |
| 1021 | * reached the buffer. It must only be called after the standard HTTP request |
| 1022 | * processing has occurred, because it expects the request to be parsed and will |
| 1023 | * look for the Expect header. It may send a 100-Continue interim response. It |
| 1024 | * takes in input any state starting from HTTP_MSG_BODY and leaves with one of |
| 1025 | * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it |
| 1026 | * needs to read more data, or 1 once it has completed its analysis. |
| 1027 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1028 | int http_wait_for_request_body(struct stream *s, struct channel *req, int an_bit) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1029 | { |
| 1030 | struct session *sess = s->sess; |
| 1031 | struct http_txn *txn = s->txn; |
| 1032 | struct http_msg *msg = &s->txn->req; |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 1033 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1034 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1035 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg); |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 1036 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1037 | htx = htxbuf(&req->buf); |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 1038 | |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1039 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 1040 | goto return_bad_req; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1041 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 1042 | goto return_int_err; |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1043 | |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 1044 | if (msg->msg_state < HTTP_MSG_BODY) |
| 1045 | goto missing_data; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1046 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1047 | /* We have to parse the HTTP request body to find any required data. |
| 1048 | * "balance url_param check_post" should have been the only way to get |
| 1049 | * into this. We were brought here after HTTP header analysis, so all |
| 1050 | * related structures are ready. |
| 1051 | */ |
| 1052 | |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 1053 | if (msg->msg_state < HTTP_MSG_DATA) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1054 | if (http_handle_expect_hdr(s, htx, msg) == -1) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1055 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1056 | } |
| 1057 | |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 1058 | msg->msg_state = HTTP_MSG_DATA; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1059 | |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 1060 | /* Now we're in HTTP_MSG_DATA. We just need to know if all data have |
| 1061 | * been received or if the buffer is full. |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1062 | */ |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 1063 | if (htx_get_tail_type(htx) > HTX_BLK_DATA || |
Christopher Faulet | dcd8c5e | 2019-01-21 11:24:38 +0100 | [diff] [blame] | 1064 | channel_htx_full(req, htx, global.tune.maxrewrite)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1065 | goto http_end; |
| 1066 | |
Christopher Faulet | f76ebe8 | 2018-10-24 11:16:22 +0200 | [diff] [blame] | 1067 | missing_data: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1068 | if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 1069 | txn->status = 408; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1070 | if (!(s->flags & SF_ERR_MASK)) |
| 1071 | s->flags |= SF_ERR_CLITO; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1072 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
| 1073 | if (sess->listener->counters) |
| 1074 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
| 1075 | goto return_prx_cond; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | /* we get here if we need to wait for more data */ |
| 1079 | if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) { |
| 1080 | /* Not enough data. We'll re-use the http-request |
| 1081 | * timeout here. Ideally, we should set the timeout |
| 1082 | * relative to the accept() date. We just set the |
| 1083 | * request timeout once at the beginning of the |
| 1084 | * request. |
| 1085 | */ |
| 1086 | channel_dont_connect(req); |
| 1087 | if (!tick_isset(req->analyse_exp)) |
| 1088 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1089 | DBG_TRACE_DEVEL("waiting for more data", |
| 1090 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1091 | return 0; |
| 1092 | } |
| 1093 | |
| 1094 | http_end: |
| 1095 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 1096 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 1097 | req->analysers &= ~an_bit; |
| 1098 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1099 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1100 | return 1; |
| 1101 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1102 | return_int_err: |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1103 | txn->status = 500; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1104 | if (!(s->flags & SF_ERR_MASK)) |
| 1105 | s->flags |= SF_ERR_INTERNAL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1106 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1107 | if (s->flags & SF_BE_ASSIGNED) |
Christopher Faulet | be20cf3 | 2020-01-24 11:41:38 +0100 | [diff] [blame] | 1108 | _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1109 | if (sess->listener->counters) |
| 1110 | _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1); |
| 1111 | goto return_prx_cond; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1112 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1113 | return_bad_req: /* let's centralize all bad requests */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1114 | txn->status = 400; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1115 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
| 1116 | if (sess->listener->counters) |
| 1117 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
| 1118 | /* fall through */ |
| 1119 | |
| 1120 | return_prx_cond: |
| 1121 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1122 | |
| 1123 | if (!(s->flags & SF_ERR_MASK)) |
| 1124 | s->flags |= SF_ERR_PRXCOND; |
| 1125 | if (!(s->flags & SF_FINST_MASK)) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1126 | s->flags |= (msg->msg_state < HTTP_MSG_DATA ? SF_FINST_R : SF_FINST_D); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1127 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1128 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1129 | req->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1130 | DBG_TRACE_DEVEL("leaving on error", |
| 1131 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1132 | return 0; |
| 1133 | } |
| 1134 | |
| 1135 | /* This function is an analyser which forwards request body (including chunk |
| 1136 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 1137 | * zero byte. The only situation where it must not be called is when we're in |
| 1138 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 1139 | * remaining data and to resync after end of body. It expects the msg_state to |
| 1140 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 1141 | * read more data, or 1 once we can go on with next request or end the stream. |
| 1142 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
| 1143 | * bytes of pending data + the headers if not already done. |
| 1144 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1145 | int http_request_forward_body(struct stream *s, struct channel *req, int an_bit) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1146 | { |
| 1147 | struct session *sess = s->sess; |
| 1148 | struct http_txn *txn = s->txn; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1149 | struct http_msg *msg = &txn->req; |
| 1150 | struct htx *htx; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1151 | short status = 0; |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1152 | int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1153 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1154 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1155 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1156 | htx = htxbuf(&req->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1157 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1158 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 1159 | goto return_bad_req; |
| 1160 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 1161 | goto return_int_err; |
| 1162 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1163 | if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
| 1164 | ((req->flags & CF_SHUTW) && (req->to_forward || co_data(req)))) { |
| 1165 | /* Output closed while we were sending data. We must abort and |
| 1166 | * wake the other side up. |
| 1167 | */ |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1168 | |
Olivier Houchard | 29cac3c | 2019-07-12 15:48:58 +0200 | [diff] [blame] | 1169 | /* Don't abort yet if we had L7 retries activated and it |
| 1170 | * was a write error, we may recover. |
| 1171 | */ |
| 1172 | if (!(req->flags & (CF_READ_ERROR | CF_READ_TIMEOUT)) && |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1173 | (s->si[1].flags & SI_FL_L7_RETRY)) { |
| 1174 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1175 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Olivier Houchard | 29cac3c | 2019-07-12 15:48:58 +0200 | [diff] [blame] | 1176 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1177 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1178 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1179 | http_end_request(s); |
| 1180 | http_end_response(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1181 | DBG_TRACE_DEVEL("leaving on error", |
| 1182 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1183 | return 1; |
| 1184 | } |
| 1185 | |
| 1186 | /* Note that we don't have to send 100-continue back because we don't |
| 1187 | * need the data to complete our job, and it's up to the server to |
| 1188 | * decide whether to return 100, 417 or anything else in return of |
| 1189 | * an "Expect: 100-continue" header. |
| 1190 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1191 | if (msg->msg_state == HTTP_MSG_BODY) |
| 1192 | msg->msg_state = HTTP_MSG_DATA; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1193 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1194 | /* in most states, we should abort in case of early close */ |
| 1195 | channel_auto_close(req); |
| 1196 | |
| 1197 | if (req->to_forward) { |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 1198 | if (req->to_forward == CHN_INFINITE_FORWARD) { |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1199 | if (req->flags & CF_EOI) |
| 1200 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 1201 | } |
| 1202 | else { |
| 1203 | /* We can't process the buffer's contents yet */ |
| 1204 | req->flags |= CF_WAKE_WRITE; |
| 1205 | goto missing_data_or_waiting; |
| 1206 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1207 | } |
| 1208 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1209 | if (msg->msg_state >= HTTP_MSG_ENDING) |
| 1210 | goto ending; |
| 1211 | |
| 1212 | if (txn->meth == HTTP_METH_CONNECT) { |
| 1213 | msg->msg_state = HTTP_MSG_ENDING; |
| 1214 | goto ending; |
| 1215 | } |
| 1216 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1217 | /* Forward input data. We get it by removing all outgoing data not |
| 1218 | * forwarded yet from HTX data size. If there are some data filters, we |
| 1219 | * let them decide the amount of data to forward. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1220 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1221 | if (HAS_REQ_DATA_FILTERS(s)) { |
| 1222 | ret = flt_http_payload(s, msg, htx->data); |
| 1223 | if (ret < 0) |
| 1224 | goto return_bad_req; |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1225 | c_adv(req, ret); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1226 | } |
| 1227 | else { |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1228 | c_adv(req, htx->data - co_data(req)); |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 1229 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
| 1230 | channel_htx_forward_forever(req, htx); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1231 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1232 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1233 | if (htx->data != co_data(req)) |
| 1234 | goto missing_data_or_waiting; |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1235 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1236 | /* Check if the end-of-message is reached and if so, switch the message |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1237 | * in HTTP_MSG_ENDING state. Then if all data was marked to be |
| 1238 | * forwarded, set the state to HTTP_MSG_DONE. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1239 | */ |
| 1240 | if (htx_get_tail_type(htx) != HTX_BLK_EOM) |
| 1241 | goto missing_data_or_waiting; |
| 1242 | |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1243 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1244 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1245 | ending: |
| 1246 | /* other states, ENDING...TUNNEL */ |
| 1247 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 1248 | goto done; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1249 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1250 | if (HAS_REQ_DATA_FILTERS(s)) { |
| 1251 | ret = flt_http_end(s, msg); |
| 1252 | if (ret <= 0) { |
| 1253 | if (!ret) |
| 1254 | goto missing_data_or_waiting; |
| 1255 | goto return_bad_req; |
| 1256 | } |
| 1257 | } |
| 1258 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 1259 | if (txn->meth == HTTP_METH_CONNECT) |
| 1260 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 1261 | else { |
| 1262 | msg->msg_state = HTTP_MSG_DONE; |
| 1263 | req->to_forward = 0; |
| 1264 | } |
| 1265 | |
| 1266 | done: |
| 1267 | /* we don't want to forward closes on DONE except in tunnel mode. */ |
| 1268 | if (!(txn->flags & TX_CON_WANT_TUN)) |
| 1269 | channel_dont_close(req); |
| 1270 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1271 | http_end_request(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1272 | if (!(req->analysers & an_bit)) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1273 | http_end_response(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1274 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 1275 | if (req->flags & CF_SHUTW) { |
| 1276 | /* request errors are most likely due to the |
| 1277 | * server aborting the transfer. */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1278 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1279 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1280 | goto return_bad_req; |
| 1281 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1282 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1283 | return 1; |
| 1284 | } |
| 1285 | |
| 1286 | /* If "option abortonclose" is set on the backend, we want to monitor |
| 1287 | * the client's connection and forward any shutdown notification to the |
| 1288 | * server, which will decide whether to close or to go on processing the |
| 1289 | * request. We only do that in tunnel mode, and not in other modes since |
| 1290 | * it can be abused to exhaust source ports. */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 1291 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1292 | channel_auto_read(req); |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 1293 | if ((req->flags & (CF_SHUTR|CF_READ_NULL)) && !(txn->flags & TX_CON_WANT_TUN)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1294 | s->si[1].flags |= SI_FL_NOLINGER; |
| 1295 | channel_auto_close(req); |
| 1296 | } |
| 1297 | else if (s->txn->meth == HTTP_METH_POST) { |
| 1298 | /* POST requests may require to read extra CRLF sent by broken |
| 1299 | * browsers and which could cause an RST to be sent upon close |
| 1300 | * on some systems (eg: Linux). */ |
| 1301 | channel_auto_read(req); |
| 1302 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1303 | DBG_TRACE_DEVEL("waiting for the end of the HTTP txn", |
| 1304 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1305 | return 0; |
| 1306 | |
| 1307 | missing_data_or_waiting: |
| 1308 | /* stop waiting for data if the input is closed before the end */ |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 1309 | if (msg->msg_state < HTTP_MSG_ENDING && req->flags & CF_SHUTR) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1310 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1311 | |
| 1312 | waiting: |
| 1313 | /* waiting for the last bits to leave the buffer */ |
| 1314 | if (req->flags & CF_SHUTW) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1315 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1316 | |
| 1317 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 1318 | * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE. |
| 1319 | * And when content-length is used, we never want to let the possible |
| 1320 | * shutdown be forwarded to the other side, as the state machine will |
| 1321 | * take care of it once the client responds. It's also important to |
| 1322 | * prevent TIME_WAITs from accumulating on the backend side, and for |
| 1323 | * HTTP/2 where the last frame comes with a shutdown. |
| 1324 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1325 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1326 | channel_dont_close(req); |
| 1327 | |
| 1328 | /* We know that more data are expected, but we couldn't send more that |
| 1329 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
| 1330 | * system knows it must not set a PUSH on this first part. Interactive |
| 1331 | * modes are already handled by the stream sock layer. We must not do |
| 1332 | * this in content-length mode because it could present the MSG_MORE |
| 1333 | * flag with the last block of forwarded data, which would cause an |
| 1334 | * additional delay to be observed by the receiver. |
| 1335 | */ |
| 1336 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
| 1337 | req->flags |= CF_EXPECT_MORE; |
| 1338 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1339 | DBG_TRACE_DEVEL("waiting for more data to forward", |
| 1340 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1341 | return 0; |
| 1342 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1343 | return_cli_abort: |
| 1344 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 1345 | _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1346 | if (sess->listener->counters) |
| 1347 | _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1348 | if (objt_server(s->target)) |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1349 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1350 | if (!(s->flags & SF_ERR_MASK)) |
| 1351 | s->flags |= SF_ERR_CLICL; |
| 1352 | status = 400; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1353 | goto return_prx_cond; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1354 | |
| 1355 | return_srv_abort: |
| 1356 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1); |
| 1357 | _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1358 | if (sess->listener->counters) |
| 1359 | _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1360 | if (objt_server(s->target)) |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1361 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1362 | if (!(s->flags & SF_ERR_MASK)) |
| 1363 | s->flags |= SF_ERR_SRVCL; |
| 1364 | status = 502; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1365 | goto return_prx_cond; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1366 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1367 | return_int_err: |
| 1368 | if (!(s->flags & SF_ERR_MASK)) |
| 1369 | s->flags |= SF_ERR_INTERNAL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1370 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1371 | _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1372 | if (sess->listener->counters) |
| 1373 | _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1374 | if (objt_server(s->target)) |
| 1375 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1376 | status = 500; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1377 | goto return_prx_cond; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1378 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1379 | return_bad_req: |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 1380 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1381 | if (sess->listener->counters) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 1382 | _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1383 | status = 400; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1384 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1385 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1386 | return_prx_cond: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1387 | if (txn->status > 0) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1388 | /* Note: we don't send any error if some data were already sent */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1389 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1390 | } else { |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1391 | txn->status = status; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1392 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1393 | } |
| 1394 | req->analysers &= AN_REQ_FLT_END; |
| 1395 | s->res.analysers &= AN_RES_FLT_END; /* we're in data phase, we want to abort both directions */ |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1396 | if (!(s->flags & SF_ERR_MASK)) |
| 1397 | s->flags |= SF_ERR_PRXCOND; |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 1398 | if (!(s->flags & SF_FINST_MASK)) |
| 1399 | s->flags |= ((txn->rsp.msg_state < HTTP_MSG_ERROR) ? SF_FINST_H : SF_FINST_D); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1400 | DBG_TRACE_DEVEL("leaving on error ", |
| 1401 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1402 | return 0; |
| 1403 | } |
| 1404 | |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1405 | /* Reset the stream and the backend stream_interface to a situation suitable for attemption connection */ |
| 1406 | /* Returns 0 if we can attempt to retry, -1 otherwise */ |
| 1407 | static __inline int do_l7_retry(struct stream *s, struct stream_interface *si) |
| 1408 | { |
| 1409 | struct channel *req, *res; |
| 1410 | int co_data; |
| 1411 | |
| 1412 | si->conn_retries--; |
| 1413 | if (si->conn_retries < 0) |
| 1414 | return -1; |
| 1415 | |
Willy Tarreau | 223995e | 2019-05-04 10:38:31 +0200 | [diff] [blame] | 1416 | if (objt_server(s->target)) |
| 1417 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.retries, 1); |
| 1418 | _HA_ATOMIC_ADD(&s->be->be_counters.retries, 1); |
| 1419 | |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1420 | req = &s->req; |
| 1421 | res = &s->res; |
| 1422 | /* Remove any write error from the request, and read error from the response */ |
| 1423 | req->flags &= ~(CF_WRITE_ERROR | CF_WRITE_TIMEOUT | CF_SHUTW | CF_SHUTW_NOW); |
| 1424 | res->flags &= ~(CF_READ_ERROR | CF_READ_TIMEOUT | CF_SHUTR | CF_EOI | CF_READ_NULL | CF_SHUTR_NOW); |
| 1425 | res->analysers = 0; |
| 1426 | si->flags &= ~(SI_FL_ERR | SI_FL_EXP | SI_FL_RXBLK_SHUT); |
Olivier Houchard | 8cabc97 | 2020-05-12 22:18:14 +0200 | [diff] [blame] | 1427 | s->flags &= ~SF_ADDR_SET; |
Olivier Houchard | 4bd5867 | 2019-07-12 16:16:59 +0200 | [diff] [blame] | 1428 | stream_choose_redispatch(s); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1429 | si->exp = TICK_ETERNITY; |
| 1430 | res->rex = TICK_ETERNITY; |
| 1431 | res->to_forward = 0; |
| 1432 | res->analyse_exp = TICK_ETERNITY; |
| 1433 | res->total = 0; |
Olivier Houchard | 4bd5867 | 2019-07-12 16:16:59 +0200 | [diff] [blame] | 1434 | s->flags &= ~(SF_ERR_SRVTO | SF_ERR_SRVCL); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1435 | si_release_endpoint(&s->si[1]); |
| 1436 | b_free(&req->buf); |
| 1437 | /* Swap the L7 buffer with the channel buffer */ |
| 1438 | /* We know we stored the co_data as b_data, so get it there */ |
| 1439 | co_data = b_data(&si->l7_buffer); |
| 1440 | b_set_data(&si->l7_buffer, b_size(&si->l7_buffer)); |
| 1441 | b_xfer(&req->buf, &si->l7_buffer, b_data(&si->l7_buffer)); |
| 1442 | |
| 1443 | co_set_data(req, co_data); |
| 1444 | b_reset(&res->buf); |
| 1445 | co_set_data(res, 0); |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1449 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 1450 | * processing can continue on next analysers, or zero if it either needs more |
| 1451 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 1452 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->res.analysers |
| 1453 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1454 | * abort. |
| 1455 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1456 | int http_wait_for_response(struct stream *s, struct channel *rep, int an_bit) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1457 | { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1458 | /* |
| 1459 | * We will analyze a complete HTTP response to check the its syntax. |
| 1460 | * |
| 1461 | * Once the start line and all headers are received, we may perform a |
| 1462 | * capture of the error (if any), and we will set a few fields. We also |
| 1463 | * logging and finally headers capture. |
| 1464 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1465 | struct session *sess = s->sess; |
| 1466 | struct http_txn *txn = s->txn; |
| 1467 | struct http_msg *msg = &txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1468 | struct htx *htx; |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1469 | struct stream_interface *si_b = &s->si[1]; |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1470 | struct connection *srv_conn; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1471 | struct htx_sl *sl; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1472 | int n; |
| 1473 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1474 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1475 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1476 | htx = htxbuf(&rep->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1477 | |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1478 | /* Parsing errors are caught here */ |
| 1479 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 1480 | goto return_bad_res; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1481 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 1482 | goto return_int_err; |
Willy Tarreau | 4236f03 | 2019-03-05 10:43:32 +0100 | [diff] [blame] | 1483 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1484 | /* |
| 1485 | * Now we quickly check if we have found a full valid response. |
| 1486 | * If not so, we check the FD and buffer states before leaving. |
| 1487 | * A full response is indicated by the fact that we have seen |
| 1488 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
| 1489 | * responses are checked first. |
| 1490 | * |
| 1491 | * Depending on whether the client is still there or not, we |
| 1492 | * may send an error response back or not. Note that normally |
| 1493 | * we should only check for HTTP status there, and check I/O |
| 1494 | * errors somewhere else. |
| 1495 | */ |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1496 | next_one: |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 1497 | if (unlikely(htx_is_empty(htx) || htx->first == -1)) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1498 | /* 1: have we encountered a read error ? */ |
| 1499 | if (rep->flags & CF_READ_ERROR) { |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1500 | struct connection *conn = NULL; |
| 1501 | |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1502 | if (objt_cs(s->si[1].end)) |
| 1503 | conn = objt_cs(s->si[1].end)->conn; |
| 1504 | |
| 1505 | if (si_b->flags & SI_FL_L7_RETRY && |
| 1506 | (!conn || conn->err_code != CO_ER_SSL_EARLY_FAILED)) { |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1507 | /* If we arrive here, then CF_READ_ERROR was |
| 1508 | * set by si_cs_recv() because we matched a |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1509 | * status, otherwise it would have removed |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1510 | * the SI_FL_L7_RETRY flag, so it's ok not |
| 1511 | * to check s->be->retry_type. |
| 1512 | */ |
| 1513 | if (co_data(rep) || do_l7_retry(s, si_b) == 0) |
| 1514 | return 0; |
| 1515 | } |
| 1516 | |
Olivier Houchard | 6db1699 | 2019-05-17 15:40:49 +0200 | [diff] [blame] | 1517 | if (txn->flags & TX_NOT_FIRST) |
| 1518 | goto abort_keep_alive; |
| 1519 | |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 1520 | _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1521 | if (objt_server(s->target)) { |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 1522 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1523 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1524 | } |
| 1525 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1526 | rep->analysers &= AN_RES_FLT_END; |
| 1527 | txn->status = 502; |
| 1528 | |
| 1529 | /* Check to see if the server refused the early data. |
| 1530 | * If so, just send a 425 |
| 1531 | */ |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1532 | if (conn->err_code == CO_ER_SSL_EARLY_FAILED) { |
| 1533 | if ((s->be->retry_type & PR_RE_EARLY_ERROR) && |
Olivier Houchard | ad26d8d | 2019-05-10 17:48:28 +0200 | [diff] [blame] | 1534 | (si_b->flags & SI_FL_L7_RETRY) && |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1535 | do_l7_retry(s, si_b) == 0) { |
| 1536 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1537 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1538 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1539 | } |
Olivier Houchard | 865d839 | 2019-05-03 22:46:27 +0200 | [diff] [blame] | 1540 | txn->status = 425; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | s->si[1].flags |= SI_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1544 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1545 | |
| 1546 | if (!(s->flags & SF_ERR_MASK)) |
| 1547 | s->flags |= SF_ERR_SRVCL; |
| 1548 | if (!(s->flags & SF_FINST_MASK)) |
| 1549 | s->flags |= SF_FINST_H; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1550 | DBG_TRACE_DEVEL("leaving on error", |
| 1551 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1552 | return 0; |
| 1553 | } |
| 1554 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1555 | /* 2: read timeout : return a 504 to the client. */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1556 | else if (rep->flags & CF_READ_TIMEOUT) { |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1557 | if ((si_b->flags & SI_FL_L7_RETRY) && |
| 1558 | (s->be->retry_type & PR_RE_TIMEOUT)) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1559 | if (co_data(rep) || do_l7_retry(s, si_b) == 0) { |
| 1560 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1561 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1562 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1563 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1564 | } |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 1565 | _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1566 | if (objt_server(s->target)) { |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 1567 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1568 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1569 | } |
| 1570 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1571 | rep->analysers &= AN_RES_FLT_END; |
| 1572 | txn->status = 504; |
| 1573 | s->si[1].flags |= SI_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1574 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1575 | |
| 1576 | if (!(s->flags & SF_ERR_MASK)) |
| 1577 | s->flags |= SF_ERR_SRVTO; |
| 1578 | if (!(s->flags & SF_FINST_MASK)) |
| 1579 | s->flags |= SF_FINST_H; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1580 | DBG_TRACE_DEVEL("leaving on error", |
| 1581 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1582 | return 0; |
| 1583 | } |
| 1584 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1585 | /* 3: client abort with an abortonclose */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1586 | else if ((rep->flags & CF_SHUTR) && ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 1587 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 1588 | _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1589 | if (sess->listener->counters) |
| 1590 | _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1591 | if (objt_server(s->target)) |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 1592 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1593 | |
| 1594 | rep->analysers &= AN_RES_FLT_END; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1595 | txn->status = 400; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1596 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1597 | |
| 1598 | if (!(s->flags & SF_ERR_MASK)) |
| 1599 | s->flags |= SF_ERR_CLICL; |
| 1600 | if (!(s->flags & SF_FINST_MASK)) |
| 1601 | s->flags |= SF_FINST_H; |
| 1602 | |
| 1603 | /* process_stream() will take care of the error */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1604 | DBG_TRACE_DEVEL("leaving on error", |
| 1605 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1606 | return 0; |
| 1607 | } |
| 1608 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1609 | /* 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] | 1610 | else if (rep->flags & CF_SHUTR) { |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1611 | if ((si_b->flags & SI_FL_L7_RETRY) && |
| 1612 | (s->be->retry_type & PR_RE_DISCONNECTED)) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1613 | if (co_data(rep) || do_l7_retry(s, si_b) == 0) { |
| 1614 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1615 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1616 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1617 | } |
Olivier Houchard | a254a37 | 2019-04-05 15:30:12 +0200 | [diff] [blame] | 1618 | } |
| 1619 | |
Olivier Houchard | 6db1699 | 2019-05-17 15:40:49 +0200 | [diff] [blame] | 1620 | if (txn->flags & TX_NOT_FIRST) |
| 1621 | goto abort_keep_alive; |
| 1622 | |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 1623 | _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1624 | if (objt_server(s->target)) { |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 1625 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1626 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1627 | } |
| 1628 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1629 | rep->analysers &= AN_RES_FLT_END; |
| 1630 | txn->status = 502; |
| 1631 | s->si[1].flags |= SI_FL_NOLINGER; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1632 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1633 | |
| 1634 | if (!(s->flags & SF_ERR_MASK)) |
| 1635 | s->flags |= SF_ERR_SRVCL; |
| 1636 | if (!(s->flags & SF_FINST_MASK)) |
| 1637 | s->flags |= SF_FINST_H; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1638 | DBG_TRACE_DEVEL("leaving on error", |
| 1639 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1640 | return 0; |
| 1641 | } |
| 1642 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1643 | /* 5: write error to client (we don't send any message then) */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1644 | else if (rep->flags & CF_WRITE_ERROR) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1645 | if (txn->flags & TX_NOT_FIRST) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1646 | goto abort_keep_alive; |
| 1647 | |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 1648 | _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1649 | if (objt_server(s->target)) |
| 1650 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1651 | rep->analysers &= AN_RES_FLT_END; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1652 | |
| 1653 | if (!(s->flags & SF_ERR_MASK)) |
| 1654 | s->flags |= SF_ERR_CLICL; |
| 1655 | if (!(s->flags & SF_FINST_MASK)) |
| 1656 | s->flags |= SF_FINST_H; |
| 1657 | |
| 1658 | /* process_stream() will take care of the error */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1659 | DBG_TRACE_DEVEL("leaving on error", |
| 1660 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1661 | return 0; |
| 1662 | } |
| 1663 | |
| 1664 | channel_dont_close(rep); |
| 1665 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1666 | DBG_TRACE_DEVEL("waiting for more data", |
| 1667 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1668 | return 0; |
| 1669 | } |
| 1670 | |
| 1671 | /* More interesting part now : we know that we have a complete |
| 1672 | * response which at least looks like HTTP. We have an indicator |
| 1673 | * of each header's length, so we can parse them quickly. |
| 1674 | */ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1675 | msg->msg_state = HTTP_MSG_BODY; |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 1676 | BUG_ON(htx_get_first_type(htx) != HTX_BLK_RES_SL); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 1677 | sl = http_get_stline(htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1678 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1679 | /* 0: we might have to print this header in debug mode */ |
| 1680 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1681 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 1682 | int32_t pos; |
| 1683 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1684 | http_debug_stline("srvrep", s, sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1685 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 1686 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1687 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 1688 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 1689 | |
| 1690 | if (type == HTX_BLK_EOH) |
| 1691 | break; |
| 1692 | if (type != HTX_BLK_HDR) |
| 1693 | continue; |
| 1694 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1695 | http_debug_hdr("srvhdr", s, |
| 1696 | htx_get_blk_name(htx, blk), |
| 1697 | htx_get_blk_value(htx, blk)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1698 | } |
| 1699 | } |
| 1700 | |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1701 | /* 1: get the status code and the version. Also set HTTP flags */ |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1702 | txn->status = sl->info.res.status; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1703 | if (sl->flags & HTX_SL_F_VER_11) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1704 | msg->flags |= HTTP_MSGF_VER_11; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1705 | if (sl->flags & HTX_SL_F_XFER_LEN) { |
| 1706 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Christopher Faulet | 834eee7 | 2019-02-18 11:35:02 +0100 | [diff] [blame] | 1707 | msg->flags |= ((sl->flags & HTX_SL_F_CLEN) ? HTTP_MSGF_CNT_LEN : HTTP_MSGF_TE_CHNK); |
Christopher Faulet | b2db4fa | 2018-11-27 16:51:09 +0100 | [diff] [blame] | 1708 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 1709 | msg->flags |= HTTP_MSGF_BODYLESS; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 1710 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1711 | |
| 1712 | n = txn->status / 100; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1713 | if (n < 1 || n > 5) |
| 1714 | n = 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1715 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1716 | /* when the client triggers a 4xx from the server, it's most often due |
| 1717 | * to a missing object or permission. These events should be tracked |
| 1718 | * because if they happen often, it may indicate a brute force or a |
| 1719 | * vulnerability scan. |
| 1720 | */ |
| 1721 | if (n == 4) |
| 1722 | stream_inc_http_err_ctr(s); |
| 1723 | |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 1724 | if (objt_server(s->target)) { |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 1725 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.rsp[n], 1); |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 1726 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.p.http.cum_req, 1); |
| 1727 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1728 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1729 | /* Adjust server's health based on status code. Note: status codes 501 |
| 1730 | * and 505 are triggered on demand by client request, so we must not |
| 1731 | * count them as server failures. |
| 1732 | */ |
| 1733 | if (objt_server(s->target)) { |
| 1734 | if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505)) |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1735 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_OK); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1736 | else |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1737 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_STS); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | /* |
| 1741 | * We may be facing a 100-continue response, or any other informational |
| 1742 | * 1xx response which is non-final, in which case this is not the right |
| 1743 | * response, and we're waiting for the next one. Let's allow this response |
| 1744 | * to go to the client and wait for the next one. There's an exception for |
| 1745 | * 101 which is used later in the code to switch protocols. |
| 1746 | */ |
| 1747 | if (txn->status < 200 && |
| 1748 | (txn->status == 100 || txn->status >= 102)) { |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 1749 | FLT_STRM_CB(s, flt_http_reset(s, msg)); |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 1750 | htx->first = channel_htx_fwd_headers(rep, htx); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1751 | msg->msg_state = HTTP_MSG_RPBEFORE; |
Christopher Faulet | 3499f62 | 2019-09-03 15:23:54 +0200 | [diff] [blame] | 1752 | msg->flags = 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1753 | txn->status = 0; |
| 1754 | s->logs.t_data = -1; /* was not a response yet */ |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1755 | goto next_one; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | /* |
| 1759 | * 2: check for cacheability. |
| 1760 | */ |
| 1761 | |
| 1762 | switch (txn->status) { |
| 1763 | case 200: |
| 1764 | case 203: |
| 1765 | case 204: |
| 1766 | case 206: |
| 1767 | case 300: |
| 1768 | case 301: |
| 1769 | case 404: |
| 1770 | case 405: |
| 1771 | case 410: |
| 1772 | case 414: |
| 1773 | case 501: |
| 1774 | break; |
| 1775 | default: |
| 1776 | /* RFC7231#6.1: |
| 1777 | * Responses with status codes that are defined as |
| 1778 | * cacheable by default (e.g., 200, 203, 204, 206, |
| 1779 | * 300, 301, 404, 405, 410, 414, and 501 in this |
| 1780 | * specification) can be reused by a cache with |
| 1781 | * heuristic expiration unless otherwise indicated |
| 1782 | * by the method definition or explicit cache |
| 1783 | * controls [RFC7234]; all other status codes are |
| 1784 | * not cacheable by default. |
| 1785 | */ |
| 1786 | txn->flags &= ~(TX_CACHEABLE | TX_CACHE_COOK); |
| 1787 | break; |
| 1788 | } |
| 1789 | |
| 1790 | /* |
| 1791 | * 3: we may need to capture headers |
| 1792 | */ |
| 1793 | s->logs.logwait &= ~LW_RESP; |
| 1794 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->res_cap)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1795 | http_capture_headers(htx, s->res_cap, sess->fe->rsp_cap); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1796 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1797 | /* Skip parsing if no content length is possible. */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1798 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 1799 | txn->status == 101)) { |
| 1800 | /* Either we've established an explicit tunnel, or we're |
| 1801 | * switching the protocol. In both cases, we're very unlikely |
| 1802 | * to understand the next protocols. We have to switch to tunnel |
| 1803 | * mode, so that we transfer the request and responses then let |
| 1804 | * this protocol pass unmodified. When we later implement specific |
| 1805 | * parsers for such protocols, we'll want to check the Upgrade |
| 1806 | * header which contains information about that protocol for |
| 1807 | * responses with status 101 (eg: see RFC2817 about TLS). |
| 1808 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 1809 | txn->flags |= TX_CON_WANT_TUN; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1810 | } |
| 1811 | |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1812 | /* check for NTML authentication headers in 401 (WWW-Authenticate) and |
| 1813 | * 407 (Proxy-Authenticate) responses and set the connection to private |
| 1814 | */ |
| 1815 | srv_conn = cs_conn(objt_cs(s->si[1].end)); |
| 1816 | if (srv_conn) { |
| 1817 | struct ist hdr; |
| 1818 | struct http_hdr_ctx ctx; |
| 1819 | |
| 1820 | if (txn->status == 401) |
| 1821 | hdr = ist("WWW-Authenticate"); |
| 1822 | else if (txn->status == 407) |
| 1823 | hdr = ist("Proxy-Authenticate"); |
| 1824 | else |
| 1825 | goto end; |
| 1826 | |
| 1827 | ctx.blk = NULL; |
| 1828 | while (http_find_header(htx, hdr, &ctx, 0)) { |
Willy Tarreau | f1dcced | 2020-05-07 19:27:02 +0200 | [diff] [blame] | 1829 | /* If www-authenticate contains "Negotiate", "Nego2", or "NTLM", |
| 1830 | * possibly followed by blanks and a base64 string, the connection |
| 1831 | * is private. Since it's a mess to deal with, we only check for |
| 1832 | * values starting with "NTLM" or "Nego". Note that often multiple |
| 1833 | * headers are sent by the server there. |
| 1834 | */ |
| 1835 | if ((ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "Nego", 4) == 0) || |
Willy Tarreau | 49a1d28 | 2020-05-07 19:10:15 +0200 | [diff] [blame] | 1836 | (ctx.value.len >= 4 && strncasecmp(ctx.value.ptr, "NTLM", 4) == 0)) { |
Olivier Houchard | 250031e | 2019-05-29 15:01:50 +0200 | [diff] [blame] | 1837 | sess->flags |= SESS_FL_PREFER_LAST; |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1838 | srv_conn->flags |= CO_FL_PRIVATE; |
Willy Tarreau | f1dcced | 2020-05-07 19:27:02 +0200 | [diff] [blame] | 1839 | break; |
Olivier Houchard | 250031e | 2019-05-29 15:01:50 +0200 | [diff] [blame] | 1840 | } |
Christopher Faulet | 6160832 | 2018-11-23 16:23:45 +0100 | [diff] [blame] | 1841 | } |
| 1842 | } |
| 1843 | |
| 1844 | end: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1845 | /* we want to have the response time before we start processing it */ |
| 1846 | s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 1847 | |
| 1848 | /* end of job, return OK */ |
| 1849 | rep->analysers &= ~an_bit; |
| 1850 | rep->analyse_exp = TICK_ETERNITY; |
| 1851 | channel_auto_close(rep); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1852 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1853 | return 1; |
| 1854 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1855 | return_int_err: |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1856 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1857 | _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 1858 | if (sess->listener->counters) |
| 1859 | _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1860 | if (objt_server(s->target)) |
| 1861 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1862 | txn->status = 500; |
| 1863 | if (!(s->flags & SF_ERR_MASK)) |
| 1864 | s->flags |= SF_ERR_INTERNAL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1865 | goto return_prx_cond; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1866 | |
| 1867 | return_bad_res: |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 1868 | _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1869 | if (objt_server(s->target)) { |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 1870 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
Willy Tarreau | b54c40a | 2018-12-02 19:28:41 +0100 | [diff] [blame] | 1871 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1872 | } |
Olivier Houchard | e3249a9 | 2019-05-03 23:01:47 +0200 | [diff] [blame] | 1873 | if ((s->be->retry_type & PR_RE_JUNK_REQUEST) && |
Olivier Houchard | ad26d8d | 2019-05-10 17:48:28 +0200 | [diff] [blame] | 1874 | (si_b->flags & SI_FL_L7_RETRY) && |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1875 | do_l7_retry(s, si_b) == 0) { |
| 1876 | DBG_TRACE_DEVEL("leaving on L7 retry", |
| 1877 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Olivier Houchard | e3249a9 | 2019-05-03 23:01:47 +0200 | [diff] [blame] | 1878 | return 0; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1879 | } |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1880 | txn->status = 502; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1881 | /* fall through */ |
| 1882 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1883 | return_prx_cond: |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1884 | http_reply_and_close(s, txn->status, http_error_message(s)); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1885 | |
| 1886 | if (!(s->flags & SF_ERR_MASK)) |
| 1887 | s->flags |= SF_ERR_PRXCOND; |
| 1888 | if (!(s->flags & SF_FINST_MASK)) |
| 1889 | s->flags |= SF_FINST_H; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1890 | |
| 1891 | s->si[1].flags |= SI_FL_NOLINGER; |
| 1892 | rep->analysers &= AN_RES_FLT_END; |
Christopher Faulet | e58c000 | 2020-03-02 16:21:01 +0100 | [diff] [blame] | 1893 | s->req.analysers &= AN_REQ_FLT_END; |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 1894 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1895 | DBG_TRACE_DEVEL("leaving on error", |
| 1896 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1897 | return 0; |
| 1898 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1899 | abort_keep_alive: |
| 1900 | /* A keep-alive request to the server failed on a network error. |
| 1901 | * The client is required to retry. We need to close without returning |
| 1902 | * any other information so that the client retries. |
| 1903 | */ |
| 1904 | txn->status = 0; |
| 1905 | rep->analysers &= AN_RES_FLT_END; |
| 1906 | s->req.analysers &= AN_REQ_FLT_END; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1907 | s->logs.logwait = 0; |
| 1908 | s->logs.level = 0; |
| 1909 | s->res.flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1910 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1911 | DBG_TRACE_DEVEL("leaving by closing K/A connection", |
| 1912 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1913 | return 0; |
| 1914 | } |
| 1915 | |
| 1916 | /* This function performs all the processing enabled for the current response. |
| 1917 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 1918 | * and updates s->res.analysers. It might make sense to explode it into several |
| 1919 | * other functions. It works like process_request (see indications above). |
| 1920 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1921 | int http_process_res_common(struct stream *s, struct channel *rep, int an_bit, struct proxy *px) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1922 | { |
| 1923 | struct session *sess = s->sess; |
| 1924 | struct http_txn *txn = s->txn; |
| 1925 | struct http_msg *msg = &txn->rsp; |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1926 | struct htx *htx; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1927 | struct proxy *cur_proxy; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1928 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 1929 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 1930 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
| 1931 | return 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1932 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1933 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1934 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1935 | htx = htxbuf(&rep->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1936 | |
| 1937 | /* The stats applet needs to adjust the Connection header but we don't |
| 1938 | * apply any filter there. |
| 1939 | */ |
| 1940 | if (unlikely(objt_applet(s->target) == &http_stats_applet)) { |
| 1941 | rep->analysers &= ~an_bit; |
| 1942 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1943 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | /* |
| 1947 | * We will have to evaluate the filters. |
| 1948 | * As opposed to version 1.2, now they will be evaluated in the |
| 1949 | * filters order and not in the header order. This means that |
| 1950 | * each filter has to be validated among all headers. |
| 1951 | * |
| 1952 | * Filters are tried with ->be first, then with ->fe if it is |
| 1953 | * different from ->be. |
| 1954 | * |
| 1955 | * Maybe we are in resume condiion. In this case I choose the |
| 1956 | * "struct proxy" which contains the rule list matching the resume |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1957 | * pointer. If none of these "struct proxy" match, I initialise |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1958 | * the process with the first one. |
| 1959 | * |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 1960 | * In fact, I check only correspondence between the current list |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1961 | * pointer and the ->fe rule list. If it doesn't match, I initialize |
| 1962 | * the loop with the ->be. |
| 1963 | */ |
| 1964 | if (s->current_rule_list == &sess->fe->http_res_rules) |
| 1965 | cur_proxy = sess->fe; |
| 1966 | else |
| 1967 | cur_proxy = s->be; |
| 1968 | while (1) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1969 | /* evaluate http-response rules */ |
| 1970 | if (ret == HTTP_RULE_RES_CONT) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1971 | ret = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1972 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1973 | switch (ret) { |
| 1974 | case HTTP_RULE_RES_YIELD: /* some data miss, call the function later. */ |
| 1975 | goto return_prx_yield; |
| 1976 | |
| 1977 | case HTTP_RULE_RES_CONT: |
| 1978 | case HTTP_RULE_RES_STOP: /* nothing to do */ |
| 1979 | break; |
| 1980 | |
| 1981 | case HTTP_RULE_RES_DENY: /* deny or tarpit */ |
| 1982 | goto deny; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1983 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1984 | case HTTP_RULE_RES_ABRT: /* abort request, response already sent */ |
| 1985 | goto return_prx_cond; |
| 1986 | |
| 1987 | case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */ |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 1988 | goto done; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1989 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1990 | case HTTP_RULE_RES_BADREQ: /* failed with a bad request */ |
| 1991 | goto return_bad_res; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1992 | |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 1993 | case HTTP_RULE_RES_ERROR: /* failed with a bad request */ |
| 1994 | goto return_int_err; |
| 1995 | } |
| 1996 | |
| 1997 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1998 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 1999 | /* check whether we're already working on the frontend */ |
| 2000 | if (cur_proxy == sess->fe) |
| 2001 | break; |
| 2002 | cur_proxy = sess->fe; |
| 2003 | } |
| 2004 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2005 | /* OK that's all we can do for 1xx responses */ |
| 2006 | if (unlikely(txn->status < 200 && txn->status != 101)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2007 | goto end; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2008 | |
| 2009 | /* |
| 2010 | * Now check for a server cookie. |
| 2011 | */ |
| 2012 | if (s->be->cookie_name || sess->fe->capture_name || (s->be->options & PR_O_CHK_CACHE)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2013 | http_manage_server_side_cookies(s, rep); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2014 | |
| 2015 | /* |
| 2016 | * Check for cache-control or pragma headers if required. |
| 2017 | */ |
| 2018 | if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2019 | http_check_response_for_cacheability(s, rep); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2020 | |
| 2021 | /* |
| 2022 | * Add server cookie in the response if needed |
| 2023 | */ |
| 2024 | if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) && |
| 2025 | !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) && |
| 2026 | (!(s->flags & SF_DIRECT) || |
| 2027 | ((s->be->cookie_maxidle || txn->cookie_last_date) && |
| 2028 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 2029 | (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 2030 | (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
| 2031 | (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 2032 | !(s->flags & SF_IGNORE_PRST)) { |
| 2033 | /* the server is known, it's not the one the client requested, or the |
| 2034 | * cookie's last seen date needs to be refreshed. We have to |
| 2035 | * insert a set-cookie here, except if we want to insert only on POST |
| 2036 | * requests and this one isn't. Note that servers which don't have cookies |
| 2037 | * (eg: some backup servers) will return a full cookie removal request. |
| 2038 | */ |
| 2039 | if (!objt_server(s->target)->cookie) { |
| 2040 | chunk_printf(&trash, |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2041 | "%s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2042 | s->be->cookie_name); |
| 2043 | } |
| 2044 | else { |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2045 | chunk_printf(&trash, "%s=%s", s->be->cookie_name, objt_server(s->target)->cookie); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2046 | |
| 2047 | if (s->be->cookie_maxidle || s->be->cookie_maxlife) { |
| 2048 | /* emit last_date, which is mandatory */ |
| 2049 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 2050 | s30tob64((date.tv_sec+3) >> 2, |
| 2051 | trash.area + trash.data); |
| 2052 | trash.data += 5; |
| 2053 | |
| 2054 | if (s->be->cookie_maxlife) { |
| 2055 | /* emit first_date, which is either the original one or |
| 2056 | * the current date. |
| 2057 | */ |
| 2058 | trash.area[trash.data++] = COOKIE_DELIM_DATE; |
| 2059 | s30tob64(txn->cookie_first_date ? |
| 2060 | txn->cookie_first_date >> 2 : |
| 2061 | (date.tv_sec+3) >> 2, |
| 2062 | trash.area + trash.data); |
| 2063 | trash.data += 5; |
| 2064 | } |
| 2065 | } |
| 2066 | chunk_appendf(&trash, "; path=/"); |
| 2067 | } |
| 2068 | |
| 2069 | if (s->be->cookie_domain) |
| 2070 | chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain); |
| 2071 | |
| 2072 | if (s->be->ck_opts & PR_CK_HTTPONLY) |
| 2073 | chunk_appendf(&trash, "; HttpOnly"); |
| 2074 | |
| 2075 | if (s->be->ck_opts & PR_CK_SECURE) |
| 2076 | chunk_appendf(&trash, "; Secure"); |
| 2077 | |
Christopher Faulet | 2f53390 | 2020-01-21 11:06:48 +0100 | [diff] [blame] | 2078 | if (s->be->cookie_attrs) |
| 2079 | chunk_appendf(&trash, "; %s", s->be->cookie_attrs); |
| 2080 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2081 | if (unlikely(!http_add_header(htx, ist("Set-Cookie"), ist2(trash.area, trash.data)))) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2082 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2083 | |
| 2084 | txn->flags &= ~TX_SCK_MASK; |
| 2085 | if (__objt_server(s->target)->cookie && (s->flags & SF_DIRECT)) |
| 2086 | /* the server did not change, only the date was updated */ |
| 2087 | txn->flags |= TX_SCK_UPDATED; |
| 2088 | else |
| 2089 | txn->flags |= TX_SCK_INSERTED; |
| 2090 | |
| 2091 | /* Here, we will tell an eventual cache on the client side that we don't |
| 2092 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 2093 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 2094 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 2095 | */ |
| 2096 | if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
| 2097 | |
| 2098 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 2099 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2100 | if (unlikely(!http_add_header(htx, ist("Cache-control"), ist("private")))) |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2101 | goto return_int_err; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2102 | } |
| 2103 | } |
| 2104 | |
| 2105 | /* |
| 2106 | * Check if result will be cacheable with a cookie. |
| 2107 | * We'll block the response if security checks have caught |
| 2108 | * nasty things such as a cacheable cookie. |
| 2109 | */ |
| 2110 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 2111 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
| 2112 | (s->be->options & PR_O_CHK_CACHE)) { |
| 2113 | /* we're in presence of a cacheable response containing |
| 2114 | * a set-cookie header. We'll block it as requested by |
| 2115 | * the 'checkcache' option, and send an alert. |
| 2116 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2117 | ha_alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 2118 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
| 2119 | send_log(s->be, LOG_ALERT, |
| 2120 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 2121 | s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>"); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2122 | goto deny; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2123 | } |
| 2124 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2125 | end: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2126 | /* |
| 2127 | * Evaluate after-response rules before forwarding the response. rules |
| 2128 | * from the backend are evaluated first, then one from the frontend if |
| 2129 | * it differs. |
| 2130 | */ |
| 2131 | if (!http_eval_after_res_rules(s)) |
| 2132 | goto return_int_err; |
| 2133 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2134 | /* Always enter in the body analyzer */ |
| 2135 | rep->analysers &= ~AN_RES_FLT_XFER_DATA; |
| 2136 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
| 2137 | |
| 2138 | /* if the user wants to log as soon as possible, without counting |
| 2139 | * bytes from the server, then this is the right moment. We have |
| 2140 | * to temporarily assign bytes_out to log what we currently have. |
| 2141 | */ |
| 2142 | if (!LIST_ISEMPTY(&sess->fe->logformat) && !(s->logs.logwait & LW_BYTES)) { |
| 2143 | s->logs.t_close = s->logs.t_data; /* to get a valid end date */ |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2144 | s->logs.bytes_out = htx->data; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2145 | s->do_log(s); |
| 2146 | s->logs.bytes_out = 0; |
| 2147 | } |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2148 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2149 | done: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 2150 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2151 | rep->analysers &= ~an_bit; |
| 2152 | rep->analyse_exp = TICK_ETERNITY; |
| 2153 | return 1; |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2154 | |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2155 | deny: |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2156 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1); |
Christopher Faulet | a08546b | 2019-12-16 16:07:34 +0100 | [diff] [blame] | 2157 | _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2158 | if (sess->listener->counters) |
| 2159 | _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1); |
Christopher Faulet | a08546b | 2019-12-16 16:07:34 +0100 | [diff] [blame] | 2160 | if (objt_server(s->target)) |
| 2161 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2162 | goto return_prx_err; |
| 2163 | |
| 2164 | return_int_err: |
| 2165 | txn->status = 500; |
| 2166 | if (!(s->flags & SF_ERR_MASK)) |
| 2167 | s->flags |= SF_ERR_INTERNAL; |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 2168 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2169 | _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1); |
| 2170 | if (objt_server(s->target)) |
| 2171 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 2172 | if (objt_server(s->target)) |
| 2173 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2174 | goto return_prx_err; |
| 2175 | |
| 2176 | return_bad_res: |
| 2177 | txn->status = 502; |
Christopher Faulet | a20a653 | 2020-02-05 10:16:41 +0100 | [diff] [blame] | 2178 | _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
| 2179 | if (objt_server(s->target)) { |
| 2180 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
| 2181 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 2182 | } |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2183 | /* fall through */ |
| 2184 | |
| 2185 | return_prx_err: |
| 2186 | http_reply_and_close(s, txn->status, http_error_message(s)); |
| 2187 | /* fall through */ |
| 2188 | |
| 2189 | return_prx_cond: |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2190 | s->logs.t_data = -1; /* was not a valid response */ |
| 2191 | s->si[1].flags |= SI_FL_NOLINGER; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2192 | |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2193 | if (!(s->flags & SF_ERR_MASK)) |
| 2194 | s->flags |= SF_ERR_PRXCOND; |
| 2195 | if (!(s->flags & SF_FINST_MASK)) |
| 2196 | s->flags |= SF_FINST_H; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2197 | |
Christopher Faulet | e58c000 | 2020-03-02 16:21:01 +0100 | [diff] [blame] | 2198 | rep->analysers &= AN_RES_FLT_END; |
| 2199 | s->req.analysers &= AN_REQ_FLT_END; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2200 | rep->analyse_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2201 | DBG_TRACE_DEVEL("leaving on error", |
| 2202 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | fec7bd1 | 2018-10-24 11:17:50 +0200 | [diff] [blame] | 2203 | return 0; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2204 | |
| 2205 | return_prx_yield: |
| 2206 | channel_dont_close(rep); |
| 2207 | DBG_TRACE_DEVEL("waiting for more data", |
| 2208 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
| 2209 | return 0; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | /* This function is an analyser which forwards response body (including chunk |
| 2213 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 2214 | * zero byte. The only situation where it must not be called is when we're in |
| 2215 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 2216 | * remaining data and to resync after end of body. It expects the msg_state to |
| 2217 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 2218 | * read more data, or 1 once we can go on with next request or end the stream. |
| 2219 | * |
| 2220 | * It is capable of compressing response data both in content-length mode and |
| 2221 | * in chunked mode. The state machines follows different flows depending on |
| 2222 | * whether content-length and chunked modes are used, since there are no |
| 2223 | * trailers in content-length : |
| 2224 | * |
| 2225 | * chk-mode cl-mode |
| 2226 | * ,----- BODY -----. |
| 2227 | * / \ |
| 2228 | * V size > 0 V chk-mode |
| 2229 | * .--> SIZE -------------> DATA -------------> CRLF |
| 2230 | * | | size == 0 | last byte | |
| 2231 | * | v final crlf v inspected | |
| 2232 | * | TRAILERS -----------> DONE | |
| 2233 | * | | |
| 2234 | * `----------------------------------------------' |
| 2235 | * |
| 2236 | * Compression only happens in the DATA state, and must be flushed in final |
| 2237 | * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding |
| 2238 | * is performed at once on final states for all bytes parsed, or when leaving |
| 2239 | * on missing data. |
| 2240 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2241 | int http_response_forward_body(struct stream *s, struct channel *res, int an_bit) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2242 | { |
| 2243 | struct session *sess = s->sess; |
| 2244 | struct http_txn *txn = s->txn; |
| 2245 | struct http_msg *msg = &s->txn->rsp; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2246 | struct htx *htx; |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2247 | int ret; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2248 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2249 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn, msg); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2250 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2251 | htx = htxbuf(&res->buf); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2252 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2253 | if (htx->flags & HTX_FL_PARSING_ERROR) |
| 2254 | goto return_bad_res; |
| 2255 | if (htx->flags & HTX_FL_PROCESSING_ERROR) |
| 2256 | goto return_int_err; |
| 2257 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2258 | 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] | 2259 | ((res->flags & CF_SHUTW) && (res->to_forward || co_data(res)))) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2260 | /* Output closed while we were sending data. We must abort and |
| 2261 | * wake the other side up. |
| 2262 | */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2263 | msg->msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2264 | http_end_response(s); |
| 2265 | http_end_request(s); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2266 | DBG_TRACE_DEVEL("leaving on error", |
| 2267 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2268 | return 1; |
| 2269 | } |
| 2270 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2271 | if (msg->msg_state == HTTP_MSG_BODY) |
| 2272 | msg->msg_state = HTTP_MSG_DATA; |
| 2273 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2274 | /* in most states, we should abort in case of early close */ |
| 2275 | channel_auto_close(res); |
| 2276 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2277 | if (res->to_forward) { |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2278 | if (res->to_forward == CHN_INFINITE_FORWARD) { |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2279 | if (res->flags & CF_EOI) |
| 2280 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2281 | } |
| 2282 | else { |
| 2283 | /* We can't process the buffer's contents yet */ |
| 2284 | res->flags |= CF_WAKE_WRITE; |
| 2285 | goto missing_data_or_waiting; |
| 2286 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2287 | } |
| 2288 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2289 | if (msg->msg_state >= HTTP_MSG_ENDING) |
| 2290 | goto ending; |
| 2291 | |
| 2292 | if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 || |
| 2293 | (!(msg->flags & HTTP_MSGF_XFER_LEN) && !HAS_RSP_DATA_FILTERS(s))) { |
| 2294 | msg->msg_state = HTTP_MSG_ENDING; |
| 2295 | goto ending; |
| 2296 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2297 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2298 | /* Forward input data. We get it by removing all outgoing data not |
| 2299 | * forwarded yet from HTX data size. If there are some data filters, we |
| 2300 | * let them decide the amount of data to forward. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2301 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2302 | if (HAS_RSP_DATA_FILTERS(s)) { |
| 2303 | ret = flt_http_payload(s, msg, htx->data); |
| 2304 | if (ret < 0) |
| 2305 | goto return_bad_res; |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 2306 | c_adv(res, ret); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2307 | } |
| 2308 | else { |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 2309 | c_adv(res, htx->data - co_data(res)); |
Christopher Faulet | 66af0b2 | 2019-03-22 14:54:52 +0100 | [diff] [blame] | 2310 | if (msg->flags & HTTP_MSGF_XFER_LEN) |
| 2311 | channel_htx_forward_forever(res, htx); |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2312 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2313 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2314 | if (htx->data != co_data(res)) |
| 2315 | goto missing_data_or_waiting; |
| 2316 | |
| 2317 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && res->flags & CF_SHUTR) { |
| 2318 | msg->msg_state = HTTP_MSG_ENDING; |
| 2319 | goto ending; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2320 | } |
| 2321 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2322 | /* Check if the end-of-message is reached and if so, switch the message |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 2323 | * in HTTP_MSG_ENDING state. Then if all data was marked to be |
| 2324 | * forwarded, set the state to HTTP_MSG_DONE. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2325 | */ |
| 2326 | if (htx_get_tail_type(htx) != HTX_BLK_EOM) |
| 2327 | goto missing_data_or_waiting; |
| 2328 | |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 2329 | msg->msg_state = HTTP_MSG_ENDING; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2330 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2331 | ending: |
| 2332 | /* other states, ENDING...TUNNEL */ |
| 2333 | if (msg->msg_state >= HTTP_MSG_DONE) |
| 2334 | goto done; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2335 | |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2336 | if (HAS_RSP_DATA_FILTERS(s)) { |
| 2337 | ret = flt_http_end(s, msg); |
| 2338 | if (ret <= 0) { |
| 2339 | if (!ret) |
| 2340 | goto missing_data_or_waiting; |
| 2341 | goto return_bad_res; |
| 2342 | } |
| 2343 | } |
| 2344 | |
Christopher Faulet | 1a3e027 | 2019-11-15 16:31:46 +0100 | [diff] [blame] | 2345 | if ((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || txn->status == 101 || |
| 2346 | !(msg->flags & HTTP_MSGF_XFER_LEN)) { |
| 2347 | msg->msg_state = HTTP_MSG_TUNNEL; |
| 2348 | goto ending; |
| 2349 | } |
| 2350 | else { |
| 2351 | msg->msg_state = HTTP_MSG_DONE; |
| 2352 | res->to_forward = 0; |
| 2353 | } |
| 2354 | |
| 2355 | done: |
| 2356 | |
| 2357 | channel_dont_close(res); |
| 2358 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2359 | http_end_response(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2360 | if (!(res->analysers & an_bit)) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2361 | http_end_request(s); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2362 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2363 | if (res->flags & CF_SHUTW) { |
| 2364 | /* response errors are most likely due to the |
| 2365 | * client aborting the transfer. */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2366 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2367 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2368 | goto return_bad_res; |
| 2369 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2370 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2371 | return 1; |
| 2372 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2373 | DBG_TRACE_DEVEL("waiting for the end of the HTTP txn", |
| 2374 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2375 | return 0; |
| 2376 | |
| 2377 | missing_data_or_waiting: |
| 2378 | if (res->flags & CF_SHUTW) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2379 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2380 | |
| 2381 | /* stop waiting for data if the input is closed before the end. If the |
| 2382 | * client side was already closed, it means that the client has aborted, |
| 2383 | * so we don't want to count this as a server abort. Otherwise it's a |
| 2384 | * server abort. |
| 2385 | */ |
Christopher Faulet | d20fdb0 | 2019-06-13 16:43:22 +0200 | [diff] [blame] | 2386 | if (msg->msg_state < HTTP_MSG_ENDING && res->flags & CF_SHUTR) { |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2387 | if ((s->req.flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2388 | goto return_cli_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2389 | /* If we have some pending data, we continue the processing */ |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2390 | if (htx_is_empty(htx)) |
| 2391 | goto return_srv_abort; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2392 | } |
| 2393 | |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2394 | /* When TE: chunked is used, we need to get there again to parse |
| 2395 | * 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] | 2396 | * set CF_DONTCLOSE. Similarly when there is a content-leng or if there |
| 2397 | * are filters registered on the stream, we don't want to forward a |
| 2398 | * close |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2399 | */ |
Christopher Faulet | aed82cf | 2018-11-30 22:22:32 +0100 | [diff] [blame] | 2400 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || HAS_RSP_DATA_FILTERS(s)) |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2401 | channel_dont_close(res); |
| 2402 | |
| 2403 | /* We know that more data are expected, but we couldn't send more that |
| 2404 | * what we did. So we always set the CF_EXPECT_MORE flag so that the |
| 2405 | * system knows it must not set a PUSH on this first part. Interactive |
| 2406 | * modes are already handled by the stream sock layer. We must not do |
| 2407 | * this in content-length mode because it could present the MSG_MORE |
| 2408 | * flag with the last block of forwarded data, which would cause an |
| 2409 | * additional delay to be observed by the receiver. |
| 2410 | */ |
| 2411 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || (msg->flags & HTTP_MSGF_COMPRESSING)) |
| 2412 | res->flags |= CF_EXPECT_MORE; |
| 2413 | |
| 2414 | /* the stream handler will take care of timeouts and errors */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2415 | DBG_TRACE_DEVEL("waiting for more data to forward", |
| 2416 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2417 | return 0; |
| 2418 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2419 | return_srv_abort: |
| 2420 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1); |
| 2421 | _HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 2422 | if (sess->listener->counters) |
| 2423 | _HA_ATOMIC_ADD(&sess->listener->counters->srv_aborts, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2424 | if (objt_server(s->target)) |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 2425 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.srv_aborts, 1); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2426 | if (!(s->flags & SF_ERR_MASK)) |
| 2427 | s->flags |= SF_ERR_SRVCL; |
| 2428 | goto return_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2429 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2430 | return_cli_abort: |
| 2431 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1); |
| 2432 | _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 2433 | if (sess->listener->counters) |
| 2434 | _HA_ATOMIC_ADD(&sess->listener->counters->cli_aborts, 1); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2435 | if (objt_server(s->target)) |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 2436 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2437 | if (!(s->flags & SF_ERR_MASK)) |
| 2438 | s->flags |= SF_ERR_CLICL; |
| 2439 | goto return_error; |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2440 | |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2441 | return_int_err: |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 2442 | _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2443 | _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 2444 | if (sess->listener->counters) |
| 2445 | _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2446 | if (objt_server(s->target)) |
| 2447 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1); |
Christopher Faulet | b9a92f3 | 2019-09-09 10:15:21 +0200 | [diff] [blame] | 2448 | if (!(s->flags & SF_ERR_MASK)) |
| 2449 | s->flags |= SF_ERR_INTERNAL; |
| 2450 | goto return_error; |
| 2451 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2452 | return_bad_res: |
| 2453 | _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1); |
| 2454 | if (objt_server(s->target)) { |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 2455 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1); |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2456 | health_adjust(__objt_server(s->target), HANA_STATUS_HTTP_RSP); |
| 2457 | } |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2458 | if (!(s->flags & SF_ERR_MASK)) |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2459 | s->flags |= SF_ERR_SRVCL; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2460 | /* fall through */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2461 | |
Christopher Faulet | 93e02d8 | 2019-03-08 14:18:50 +0100 | [diff] [blame] | 2462 | return_error: |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2463 | /* don't send any error message as we're in the body */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2464 | http_reply_and_close(s, txn->status, NULL); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2465 | res->analysers &= AN_RES_FLT_END; |
| 2466 | s->req.analysers &= AN_REQ_FLT_END; /* we're in data phase, we want to abort both directions */ |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2467 | if (!(s->flags & SF_FINST_MASK)) |
| 2468 | s->flags |= SF_FINST_D; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 2469 | DBG_TRACE_DEVEL("leaving on error", |
| 2470 | STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_HTTP_ERR, s, txn); |
Christopher Faulet | e0768eb | 2018-10-03 16:38:02 +0200 | [diff] [blame] | 2471 | return 0; |
| 2472 | } |
| 2473 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2474 | /* Perform an HTTP redirect based on the information in <rule>. The function |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2475 | * returns zero on success, or zero in case of a, irrecoverable error such |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2476 | * as too large a request to build a valid response. |
| 2477 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2478 | int http_apply_redirect_rule(struct redirect_rule *rule, struct stream *s, struct http_txn *txn) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2479 | { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2480 | struct channel *req = &s->req; |
| 2481 | struct channel *res = &s->res; |
| 2482 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 2483 | struct htx_sl *sl; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2484 | struct buffer *chunk; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2485 | struct ist status, reason, location; |
| 2486 | unsigned int flags; |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2487 | int close = 0; /* Try to keep the connection alive byt default */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2488 | |
| 2489 | chunk = alloc_trash_chunk(); |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2490 | if (!chunk) { |
| 2491 | if (!(s->flags & SF_ERR_MASK)) |
| 2492 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2493 | goto fail; |
Christopher Faulet | b8a5371 | 2019-12-16 11:29:38 +0100 | [diff] [blame] | 2494 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2495 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2496 | /* |
| 2497 | * Create the location |
| 2498 | */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2499 | htx = htxbuf(&req->buf); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2500 | switch(rule->type) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2501 | case REDIRECT_TYPE_SCHEME: { |
| 2502 | struct http_hdr_ctx ctx; |
| 2503 | struct ist path, host; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2504 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2505 | host = ist(""); |
| 2506 | ctx.blk = NULL; |
| 2507 | if (http_find_header(htx, ist("Host"), &ctx, 0)) |
| 2508 | host = ctx.value; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2509 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2510 | sl = http_get_stline(htx); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2511 | path = http_get_path(htx_sl_req_uri(sl)); |
| 2512 | /* build message using path */ |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 2513 | if (isttest(path)) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2514 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2515 | int qs = 0; |
| 2516 | while (qs < path.len) { |
| 2517 | if (*(path.ptr + qs) == '?') { |
| 2518 | path.len = qs; |
| 2519 | break; |
| 2520 | } |
| 2521 | qs++; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2522 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2523 | } |
| 2524 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2525 | else |
| 2526 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2527 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2528 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2529 | /* add scheme */ |
| 2530 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2531 | goto fail; |
| 2532 | } |
| 2533 | else { |
| 2534 | /* add scheme with executing log format */ |
| 2535 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2536 | chunk->size - chunk->data, |
| 2537 | &rule->rdr_fmt); |
| 2538 | } |
| 2539 | /* add "://" + host + path */ |
| 2540 | if (!chunk_memcat(chunk, "://", 3) || |
| 2541 | !chunk_memcat(chunk, host.ptr, host.len) || |
| 2542 | !chunk_memcat(chunk, path.ptr, path.len)) |
| 2543 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2544 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2545 | /* append a slash at the end of the location if needed and missing */ |
| 2546 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2547 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2548 | if (chunk->data + 1 >= chunk->size) |
| 2549 | goto fail; |
| 2550 | chunk->area[chunk->data++] = '/'; |
| 2551 | } |
| 2552 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2553 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2554 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2555 | case REDIRECT_TYPE_PREFIX: { |
| 2556 | struct ist path; |
| 2557 | |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 2558 | sl = http_get_stline(htx); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2559 | path = http_get_path(htx_sl_req_uri(sl)); |
| 2560 | /* build message using path */ |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 2561 | if (isttest(path)) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2562 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2563 | int qs = 0; |
| 2564 | while (qs < path.len) { |
| 2565 | if (*(path.ptr + qs) == '?') { |
| 2566 | path.len = qs; |
| 2567 | break; |
| 2568 | } |
| 2569 | qs++; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2570 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2571 | } |
| 2572 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2573 | else |
| 2574 | path = ist("/"); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2575 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2576 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2577 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2578 | * add anything, otherwise it makes it hard for the user to |
| 2579 | * configure a self-redirection. |
| 2580 | */ |
| 2581 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
| 2582 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2583 | goto fail; |
| 2584 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2585 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2586 | else { |
| 2587 | /* add prefix with executing log format */ |
| 2588 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2589 | chunk->size - chunk->data, |
| 2590 | &rule->rdr_fmt); |
| 2591 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2592 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2593 | /* add path */ |
| 2594 | if (!chunk_memcat(chunk, path.ptr, path.len)) |
| 2595 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2596 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2597 | /* append a slash at the end of the location if needed and missing */ |
| 2598 | if (chunk->data && chunk->area[chunk->data - 1] != '/' && |
| 2599 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2600 | if (chunk->data + 1 >= chunk->size) |
| 2601 | goto fail; |
| 2602 | chunk->area[chunk->data++] = '/'; |
| 2603 | } |
| 2604 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2605 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2606 | case REDIRECT_TYPE_LOCATION: |
| 2607 | default: |
| 2608 | if (rule->rdr_str) { /* this is an old "redirect" rule */ |
| 2609 | /* add location */ |
| 2610 | if (!chunk_memcat(chunk, rule->rdr_str, rule->rdr_len)) |
| 2611 | goto fail; |
| 2612 | } |
| 2613 | else { |
| 2614 | /* add location with executing log format */ |
| 2615 | chunk->data += build_logline(s, chunk->area + chunk->data, |
| 2616 | chunk->size - chunk->data, |
| 2617 | &rule->rdr_fmt); |
| 2618 | } |
| 2619 | break; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2620 | } |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2621 | location = ist2(chunk->area, chunk->data); |
| 2622 | |
| 2623 | /* |
| 2624 | * Create the 30x response |
| 2625 | */ |
| 2626 | switch (rule->code) { |
| 2627 | case 308: |
| 2628 | status = ist("308"); |
| 2629 | reason = ist("Permanent Redirect"); |
| 2630 | break; |
| 2631 | case 307: |
| 2632 | status = ist("307"); |
| 2633 | reason = ist("Temporary Redirect"); |
| 2634 | break; |
| 2635 | case 303: |
| 2636 | status = ist("303"); |
| 2637 | reason = ist("See Other"); |
| 2638 | break; |
| 2639 | case 301: |
| 2640 | status = ist("301"); |
| 2641 | reason = ist("Moved Permanently"); |
| 2642 | break; |
| 2643 | case 302: |
| 2644 | default: |
| 2645 | status = ist("302"); |
| 2646 | reason = ist("Found"); |
| 2647 | break; |
| 2648 | } |
| 2649 | |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2650 | if (!(txn->req.flags & HTTP_MSGF_BODYLESS) && txn->req.msg_state != HTTP_MSG_DONE) |
| 2651 | close = 1; |
| 2652 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2653 | htx = htx_from_buf(&res->buf); |
Kevin Zhu | 96b3639 | 2020-01-07 09:42:55 +0100 | [diff] [blame] | 2654 | /* Trim any possible response */ |
| 2655 | channel_htx_truncate(&s->res, htx); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2656 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 2657 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), status, reason); |
| 2658 | if (!sl) |
| 2659 | goto fail; |
| 2660 | sl->info.res.status = rule->code; |
| 2661 | s->txn->status = rule->code; |
| 2662 | |
Christopher Faulet | 08e6646 | 2019-05-23 16:44:59 +0200 | [diff] [blame] | 2663 | if (close && !htx_add_header(htx, ist("Connection"), ist("close"))) |
| 2664 | goto fail; |
| 2665 | |
| 2666 | if (!htx_add_header(htx, ist("Content-length"), ist("0")) || |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2667 | !htx_add_header(htx, ist("Location"), location)) |
| 2668 | goto fail; |
| 2669 | |
| 2670 | if (rule->code == 302 || rule->code == 303 || rule->code == 307) { |
| 2671 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache"))) |
| 2672 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | if (rule->cookie_len) { |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2676 | if (!htx_add_header(htx, ist("Set-Cookie"), ist2(rule->cookie_str, rule->cookie_len))) |
| 2677 | goto fail; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2678 | } |
| 2679 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2680 | if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM)) |
| 2681 | goto fail; |
| 2682 | |
Kevin Zhu | 96b3639 | 2020-01-07 09:42:55 +0100 | [diff] [blame] | 2683 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 2684 | if (!http_forward_proxy_resp(s, 1)) |
| 2685 | goto fail; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2686 | |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2687 | if (rule->flags & REDIRECT_FLAG_FROM_REQ) { |
| 2688 | /* let's log the request time */ |
| 2689 | s->logs.tv_request = now; |
| 2690 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2691 | |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2692 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 2693 | _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1); |
| 2694 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2695 | |
| 2696 | if (!(s->flags & SF_ERR_MASK)) |
| 2697 | s->flags |= SF_ERR_LOCAL; |
| 2698 | if (!(s->flags & SF_FINST_MASK)) |
Christopher Faulet | 60b33a5 | 2020-01-28 09:18:10 +0100 | [diff] [blame] | 2699 | s->flags |= ((rule->flags & REDIRECT_FLAG_FROM_REQ) ? SF_FINST_R : SF_FINST_H); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2700 | |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2701 | free_trash_chunk(chunk); |
| 2702 | return 1; |
| 2703 | |
| 2704 | fail: |
| 2705 | /* If an error occurred, remove the incomplete HTTP response from the |
| 2706 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 2707 | channel_htx_truncate(res, htxbuf(&res->buf)); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2708 | free_trash_chunk(chunk); |
Christopher Faulet | 99daf28 | 2018-11-28 22:58:13 +0100 | [diff] [blame] | 2709 | return 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 2710 | } |
| 2711 | |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2712 | /* Replace all headers matching the name <name>. The header value is replaced if |
| 2713 | * it matches the regex <re>. <str> is used for the replacement. If <full> is |
| 2714 | * set to 1, the full-line is matched and replaced. Otherwise, comma-separated |
| 2715 | * values are evaluated one by one. It returns 0 on success and -1 on error. |
| 2716 | */ |
| 2717 | int http_replace_hdrs(struct stream* s, struct htx *htx, struct ist name, |
| 2718 | const char *str, struct my_regex *re, int full) |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2719 | { |
| 2720 | struct http_hdr_ctx ctx; |
| 2721 | struct buffer *output = get_trash_chunk(); |
| 2722 | |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2723 | ctx.blk = NULL; |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 2724 | while (http_find_header(htx, name, &ctx, full)) { |
Christopher Faulet | 7233352 | 2018-10-24 11:25:02 +0200 | [diff] [blame] | 2725 | if (!regex_exec_match2(re, ctx.value.ptr, ctx.value.len, MAX_MATCH, pmatch, 0)) |
| 2726 | continue; |
| 2727 | |
| 2728 | output->data = exp_replace(output->area, output->size, ctx.value.ptr, str, pmatch); |
| 2729 | if (output->data == -1) |
| 2730 | return -1; |
| 2731 | if (!http_replace_header_value(htx, &ctx, ist2(output->area, output->data))) |
| 2732 | return -1; |
| 2733 | } |
| 2734 | return 0; |
| 2735 | } |
| 2736 | |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2737 | /* This function executes one of the set-{method,path,query,uri} actions. It |
| 2738 | * takes the string from the variable 'replace' with length 'len', then modifies |
| 2739 | * the relevant part of the request line accordingly. Then it updates various |
| 2740 | * pointers to the next elements which were moved, and the total buffer length. |
| 2741 | * It finds the action to be performed in p[2], previously filled by function |
| 2742 | * parse_set_req_line(). It returns 0 in case of success, -1 in case of internal |
| 2743 | * error, though this can be revisited when this code is finally exploited. |
| 2744 | * |
| 2745 | * 'action' can be '0' to replace method, '1' to replace path, '2' to replace |
| 2746 | * query string and 3 to replace uri. |
| 2747 | * |
| 2748 | * In query string case, the mark question '?' must be set at the start of the |
| 2749 | * string by the caller, event if the replacement query string is empty. |
| 2750 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2751 | int http_req_replace_stline(int action, const char *replace, int len, |
| 2752 | struct proxy *px, struct stream *s) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2753 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2754 | struct htx *htx = htxbuf(&s->req.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2755 | |
| 2756 | switch (action) { |
| 2757 | case 0: // method |
| 2758 | if (!http_replace_req_meth(htx, ist2(replace, len))) |
| 2759 | return -1; |
| 2760 | break; |
| 2761 | |
| 2762 | case 1: // path |
| 2763 | if (!http_replace_req_path(htx, ist2(replace, len))) |
| 2764 | return -1; |
| 2765 | break; |
| 2766 | |
| 2767 | case 2: // query |
| 2768 | if (!http_replace_req_query(htx, ist2(replace, len))) |
| 2769 | return -1; |
| 2770 | break; |
| 2771 | |
| 2772 | case 3: // uri |
| 2773 | if (!http_replace_req_uri(htx, ist2(replace, len))) |
| 2774 | return -1; |
| 2775 | break; |
| 2776 | |
| 2777 | default: |
| 2778 | return -1; |
| 2779 | } |
| 2780 | return 0; |
| 2781 | } |
| 2782 | |
| 2783 | /* This function replace the HTTP status code and the associated message. The |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2784 | * variable <status> contains the new status code. This function never fails. It |
| 2785 | * returns 0 in case of success, -1 in case of internal error. |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2786 | */ |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2787 | int http_res_set_status(unsigned int status, struct ist reason, struct stream *s) |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2788 | { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2789 | struct htx *htx = htxbuf(&s->res.buf); |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2790 | char *res; |
| 2791 | |
| 2792 | chunk_reset(&trash); |
| 2793 | res = ultoa_o(status, trash.area, trash.size); |
| 2794 | trash.data = res - trash.area; |
| 2795 | |
| 2796 | /* Do we have a custom reason format string? */ |
Tim Duesterhus | e296d3e | 2020-03-05 17:56:31 +0100 | [diff] [blame] | 2797 | if (!isttest(reason)) { |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2798 | const char *str = http_get_reason(status); |
| 2799 | reason = ist2(str, strlen(str)); |
| 2800 | } |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2801 | |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2802 | if (!http_replace_res_status(htx, ist2(trash.area, trash.data))) |
| 2803 | return -1; |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2804 | if (!http_replace_res_reason(htx, reason)) |
Christopher Faulet | e00d06c | 2019-12-16 17:18:42 +0100 | [diff] [blame] | 2805 | return -1; |
| 2806 | return 0; |
Christopher Faulet | 8d8ac19 | 2018-10-24 11:27:39 +0200 | [diff] [blame] | 2807 | } |
| 2808 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2809 | /* Executes the http-request rules <rules> for stream <s>, proxy <px> and |
| 2810 | * transaction <txn>. Returns the verdict of the first rule that prevents |
| 2811 | * further processing of the request (auth, deny, ...), and defaults to |
| 2812 | * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or |
| 2813 | * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT |
| 2814 | * on txn->flags if it encounters a tarpit rule. If <deny_status> is not NULL |
| 2815 | * and a deny/tarpit rule is matched, it will be filled with this rule's deny |
| 2816 | * status. |
| 2817 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2818 | static enum rule_result http_req_get_intercept_rule(struct proxy *px, struct list *rules, |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 2819 | struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2820 | { |
| 2821 | struct session *sess = strm_sess(s); |
| 2822 | struct http_txn *txn = s->txn; |
| 2823 | struct htx *htx; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2824 | struct act_rule *rule; |
| 2825 | struct http_hdr_ctx ctx; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2826 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2827 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2828 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2829 | htx = htxbuf(&s->req.buf); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2830 | |
| 2831 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2832 | * resume condition. If a resume is needed it is always in the action |
| 2833 | * and never in the ACL or converters. In this case, we initialise the |
| 2834 | * current rule, and go to the action execution point. |
| 2835 | */ |
| 2836 | if (s->current_rule) { |
| 2837 | rule = s->current_rule; |
| 2838 | s->current_rule = NULL; |
| 2839 | if (s->current_rule_list == rules) |
| 2840 | goto resume_execution; |
| 2841 | } |
| 2842 | s->current_rule_list = rules; |
| 2843 | |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2844 | /* start the ruleset evaluation in strict mode */ |
| 2845 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2846 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2847 | list_for_each_entry(rule, rules, list) { |
| 2848 | /* check optional condition */ |
| 2849 | if (rule->cond) { |
| 2850 | int ret; |
| 2851 | |
| 2852 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 2853 | ret = acl_pass(ret); |
| 2854 | |
| 2855 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2856 | ret = !ret; |
| 2857 | |
| 2858 | if (!ret) /* condition not matched */ |
| 2859 | continue; |
| 2860 | } |
| 2861 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2862 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2863 | resume_execution: |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2864 | /* Always call the action function if defined */ |
| 2865 | if (rule->action_ptr) { |
| 2866 | if ((s->req.flags & CF_READ_ERROR) || |
| 2867 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 2868 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2869 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2870 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2871 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2872 | case ACT_RET_CONT: |
| 2873 | break; |
| 2874 | case ACT_RET_STOP: |
| 2875 | rule_ret = HTTP_RULE_RES_STOP; |
| 2876 | goto end; |
| 2877 | case ACT_RET_YIELD: |
| 2878 | s->current_rule = rule; |
| 2879 | rule_ret = HTTP_RULE_RES_YIELD; |
| 2880 | goto end; |
| 2881 | case ACT_RET_ERR: |
| 2882 | rule_ret = HTTP_RULE_RES_ERROR; |
| 2883 | goto end; |
| 2884 | case ACT_RET_DONE: |
| 2885 | rule_ret = HTTP_RULE_RES_DONE; |
| 2886 | goto end; |
| 2887 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 2888 | if (txn->status == -1) |
| 2889 | txn->status = 403; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2890 | rule_ret = HTTP_RULE_RES_DENY; |
| 2891 | goto end; |
| 2892 | case ACT_RET_ABRT: |
| 2893 | rule_ret = HTTP_RULE_RES_ABRT; |
| 2894 | goto end; |
| 2895 | case ACT_RET_INV: |
| 2896 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 2897 | goto end; |
| 2898 | } |
| 2899 | continue; /* eval the next rule */ |
| 2900 | } |
| 2901 | |
| 2902 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2903 | switch (rule->action) { |
| 2904 | case ACT_ACTION_ALLOW: |
| 2905 | rule_ret = HTTP_RULE_RES_STOP; |
| 2906 | goto end; |
| 2907 | |
| 2908 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2909 | txn->status = rule->arg.http_reply->status; |
| 2910 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2911 | rule_ret = HTTP_RULE_RES_DENY; |
| 2912 | goto end; |
| 2913 | |
| 2914 | case ACT_HTTP_REQ_TARPIT: |
| 2915 | txn->flags |= TX_CLTARPIT; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 2916 | txn->status = rule->arg.http_reply->status; |
| 2917 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2918 | rule_ret = HTTP_RULE_RES_DENY; |
| 2919 | goto end; |
| 2920 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2921 | case ACT_HTTP_REDIR: |
Christopher Faulet | 90d22a8 | 2020-03-06 11:18:39 +0100 | [diff] [blame] | 2922 | rule_ret = HTTP_RULE_RES_ABRT; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2923 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 2924 | rule_ret = HTTP_RULE_RES_ERROR; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2925 | goto end; |
| 2926 | |
| 2927 | case ACT_HTTP_SET_NICE: |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2928 | s->task->nice = rule->arg.http.i; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2929 | break; |
| 2930 | |
| 2931 | case ACT_HTTP_SET_TOS: |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2932 | conn_set_tos(objt_conn(sess->origin), rule->arg.http.i); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2933 | break; |
| 2934 | |
| 2935 | case ACT_HTTP_SET_MARK: |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2936 | conn_set_mark(objt_conn(sess->origin), rule->arg.http.i); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2937 | break; |
| 2938 | |
| 2939 | case ACT_HTTP_SET_LOGL: |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2940 | s->logs.level = rule->arg.http.i; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2941 | break; |
| 2942 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2943 | case ACT_HTTP_DEL_HDR: |
| 2944 | /* remove all occurrences of the header */ |
| 2945 | ctx.blk = NULL; |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 2946 | while (http_find_header(htx, rule->arg.http.str, &ctx, 1)) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2947 | http_remove_header(htx, &ctx); |
| 2948 | break; |
| 2949 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 2950 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2951 | default: |
| 2952 | break; |
| 2953 | } |
| 2954 | } |
| 2955 | |
| 2956 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2957 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2958 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 2959 | txn->req.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 2960 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2961 | /* we reached the end of the rules, nothing to report */ |
| 2962 | return rule_ret; |
| 2963 | } |
| 2964 | |
| 2965 | /* Executes the http-response rules <rules> for stream <s> and proxy <px>. It |
| 2966 | * returns one of 5 possible statuses: HTTP_RULE_RES_CONT, HTTP_RULE_RES_STOP, |
| 2967 | * HTTP_RULE_RES_DONE, HTTP_RULE_RES_YIELD, or HTTP_RULE_RES_BADREQ. If *CONT |
| 2968 | * is returned, the process can continue the evaluation of next rule list. If |
| 2969 | * *STOP or *DONE is returned, the process must stop the evaluation. If *BADREQ |
| 2970 | * is returned, it means the operation could not be processed and a server error |
Christopher Faulet | a53abad | 2020-05-13 08:12:22 +0200 | [diff] [blame] | 2971 | * must be returned. If *YIELD is returned, the caller must call again the |
| 2972 | * function with the same context. |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2973 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 2974 | static enum rule_result http_res_get_intercept_rule(struct proxy *px, struct list *rules, |
| 2975 | struct stream *s) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2976 | { |
| 2977 | struct session *sess = strm_sess(s); |
| 2978 | struct http_txn *txn = s->txn; |
| 2979 | struct htx *htx; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2980 | struct act_rule *rule; |
| 2981 | struct http_hdr_ctx ctx; |
| 2982 | enum rule_result rule_ret = HTTP_RULE_RES_CONT; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 2983 | int act_opts = 0; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2984 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 2985 | htx = htxbuf(&s->res.buf); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 2986 | |
| 2987 | /* If "the current_rule_list" match the executed rule list, we are in |
| 2988 | * resume condition. If a resume is needed it is always in the action |
| 2989 | * and never in the ACL or converters. In this case, we initialise the |
| 2990 | * current rule, and go to the action execution point. |
| 2991 | */ |
| 2992 | if (s->current_rule) { |
| 2993 | rule = s->current_rule; |
| 2994 | s->current_rule = NULL; |
| 2995 | if (s->current_rule_list == rules) |
| 2996 | goto resume_execution; |
| 2997 | } |
| 2998 | s->current_rule_list = rules; |
| 2999 | |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 3000 | /* start the ruleset evaluation in strict mode */ |
| 3001 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 3002 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3003 | list_for_each_entry(rule, rules, list) { |
| 3004 | /* check optional condition */ |
| 3005 | if (rule->cond) { |
| 3006 | int ret; |
| 3007 | |
| 3008 | ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL); |
| 3009 | ret = acl_pass(ret); |
| 3010 | |
| 3011 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3012 | ret = !ret; |
| 3013 | |
| 3014 | if (!ret) /* condition not matched */ |
| 3015 | continue; |
| 3016 | } |
| 3017 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 3018 | act_opts |= ACT_OPT_FIRST; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3019 | resume_execution: |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3020 | |
| 3021 | /* Always call the action function if defined */ |
| 3022 | if (rule->action_ptr) { |
| 3023 | if ((s->req.flags & CF_READ_ERROR) || |
| 3024 | ((s->req.flags & (CF_SHUTR|CF_READ_NULL)) && |
| 3025 | (px->options & PR_O_ABRT_CLOSE))) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 3026 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3027 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 3028 | switch (rule->action_ptr(rule, px, sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3029 | case ACT_RET_CONT: |
| 3030 | break; |
| 3031 | case ACT_RET_STOP: |
| 3032 | rule_ret = HTTP_RULE_RES_STOP; |
| 3033 | goto end; |
| 3034 | case ACT_RET_YIELD: |
| 3035 | s->current_rule = rule; |
| 3036 | rule_ret = HTTP_RULE_RES_YIELD; |
| 3037 | goto end; |
| 3038 | case ACT_RET_ERR: |
| 3039 | rule_ret = HTTP_RULE_RES_ERROR; |
| 3040 | goto end; |
| 3041 | case ACT_RET_DONE: |
| 3042 | rule_ret = HTTP_RULE_RES_DONE; |
| 3043 | goto end; |
| 3044 | case ACT_RET_DENY: |
Christopher Faulet | b58f62b | 2020-01-13 16:40:13 +0100 | [diff] [blame] | 3045 | if (txn->status == -1) |
| 3046 | txn->status = 502; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3047 | rule_ret = HTTP_RULE_RES_DENY; |
| 3048 | goto end; |
| 3049 | case ACT_RET_ABRT: |
| 3050 | rule_ret = HTTP_RULE_RES_ABRT; |
| 3051 | goto end; |
| 3052 | case ACT_RET_INV: |
| 3053 | rule_ret = HTTP_RULE_RES_BADREQ; |
| 3054 | goto end; |
| 3055 | } |
| 3056 | continue; /* eval the next rule */ |
| 3057 | } |
| 3058 | |
| 3059 | /* If not action function defined, check for known actions */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3060 | switch (rule->action) { |
| 3061 | case ACT_ACTION_ALLOW: |
| 3062 | rule_ret = HTTP_RULE_RES_STOP; /* "allow" rules are OK */ |
| 3063 | goto end; |
| 3064 | |
| 3065 | case ACT_ACTION_DENY: |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 3066 | txn->status = rule->arg.http_reply->status; |
| 3067 | txn->http_reply = rule->arg.http_reply; |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 3068 | rule_ret = HTTP_RULE_RES_DENY; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3069 | goto end; |
| 3070 | |
| 3071 | case ACT_HTTP_SET_NICE: |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 3072 | s->task->nice = rule->arg.http.i; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3073 | break; |
| 3074 | |
| 3075 | case ACT_HTTP_SET_TOS: |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 3076 | conn_set_tos(objt_conn(sess->origin), rule->arg.http.i); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3077 | break; |
| 3078 | |
| 3079 | case ACT_HTTP_SET_MARK: |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 3080 | conn_set_mark(objt_conn(sess->origin), rule->arg.http.i); |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3081 | break; |
| 3082 | |
| 3083 | case ACT_HTTP_SET_LOGL: |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 3084 | s->logs.level = rule->arg.http.i; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3085 | break; |
| 3086 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3087 | case ACT_HTTP_DEL_HDR: |
| 3088 | /* remove all occurrences of the header */ |
| 3089 | ctx.blk = NULL; |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 3090 | while (http_find_header(htx, rule->arg.http.str, &ctx, 1)) |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3091 | http_remove_header(htx, &ctx); |
| 3092 | break; |
| 3093 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3094 | case ACT_HTTP_REDIR: |
Christopher Faulet | 49c2a70 | 2020-03-06 15:44:37 +0100 | [diff] [blame] | 3095 | rule_ret = HTTP_RULE_RES_ABRT; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3096 | if (!http_apply_redirect_rule(rule->arg.redir, s, txn)) |
Christopher Faulet | 3a26bee | 2019-12-16 12:47:40 +0100 | [diff] [blame] | 3097 | rule_ret = HTTP_RULE_RES_ERROR; |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3098 | goto end; |
| 3099 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 3100 | /* other flags exists, but normally, they never be matched. */ |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3101 | default: |
| 3102 | break; |
| 3103 | } |
| 3104 | } |
| 3105 | |
| 3106 | end: |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 3107 | /* if the ruleset evaluation is finished reset the strict mode */ |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 3108 | if (rule_ret != HTTP_RULE_RES_YIELD) |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 3109 | txn->rsp.flags &= ~HTTP_MSGF_SOFT_RW; |
Christopher Faulet | 46f9554 | 2019-12-20 10:07:22 +0100 | [diff] [blame] | 3110 | |
Christopher Faulet | 3e96419 | 2018-10-24 11:39:23 +0200 | [diff] [blame] | 3111 | /* we reached the end of the rules, nothing to report */ |
| 3112 | return rule_ret; |
| 3113 | } |
| 3114 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3115 | /* Executes backend and frontend http-after-response rules for the stream <s>, |
| 3116 | * in that order. it return 1 on success and 0 on error. It is the caller |
| 3117 | * responsibility to catch error or ignore it. If it catches it, this function |
| 3118 | * may be called a second time, for the internal error. |
| 3119 | */ |
| 3120 | int http_eval_after_res_rules(struct stream *s) |
| 3121 | { |
| 3122 | struct session *sess = s->sess; |
| 3123 | enum rule_result ret = HTTP_RULE_RES_CONT; |
| 3124 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 3125 | /* Eval after-response ruleset only if the reply is not const */ |
| 3126 | if (s->txn->flags & TX_CONST_REPLY) |
| 3127 | goto end; |
| 3128 | |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3129 | /* prune the request variables if not already done and swap to the response variables. */ |
| 3130 | if (s->vars_reqres.scope != SCOPE_RES) { |
| 3131 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 3132 | vars_prune(&s->vars_reqres, s->sess, s); |
| 3133 | vars_init(&s->vars_reqres, SCOPE_RES); |
| 3134 | } |
| 3135 | |
| 3136 | ret = http_res_get_intercept_rule(s->be, &s->be->http_after_res_rules, s); |
| 3137 | if ((ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP) && sess->fe != s->be) |
| 3138 | ret = http_res_get_intercept_rule(sess->fe, &sess->fe->http_after_res_rules, s); |
| 3139 | |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 3140 | end: |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 3141 | /* All other codes than CONTINUE, STOP or DONE are forbidden */ |
| 3142 | return (ret == HTTP_RULE_RES_CONT || ret == HTTP_RULE_RES_STOP || ret == HTTP_RULE_RES_DONE); |
| 3143 | } |
| 3144 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3145 | /* |
| 3146 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 3147 | * desirable to call it only when needed. This code is quite complex because |
| 3148 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 3149 | * highly recommended not to touch this part without a good reason ! |
| 3150 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3151 | static void http_manage_client_side_cookies(struct stream *s, struct channel *req) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3152 | { |
| 3153 | struct session *sess = s->sess; |
| 3154 | struct http_txn *txn = s->txn; |
| 3155 | struct htx *htx; |
| 3156 | struct http_hdr_ctx ctx; |
| 3157 | char *hdr_beg, *hdr_end, *del_from; |
| 3158 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
| 3159 | int preserve_hdr; |
| 3160 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3161 | htx = htxbuf(&req->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3162 | ctx.blk = NULL; |
| 3163 | while (http_find_header(htx, ist("Cookie"), &ctx, 1)) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3164 | int is_first = 1; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3165 | del_from = NULL; /* nothing to be deleted */ |
| 3166 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 3167 | |
| 3168 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3169 | * attributes whose name begin with a '$', and associate them with |
| 3170 | * the right cookie, if we want to delete this cookie. |
| 3171 | * So there are 3 cases for each cookie read : |
| 3172 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3173 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3174 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3175 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3176 | * "special" cookie. |
| 3177 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3178 | * remove it. If no application cookie persists in the header, we |
| 3179 | * *MUST* delete it. |
| 3180 | * |
| 3181 | * Note: RFC2965 is unclear about the processing of spaces around |
| 3182 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 3183 | * the RFC explicitly allows spaces before it, and not within the |
| 3184 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 3185 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 3186 | * after the equal sign too, resulting in some (rare) buggy |
| 3187 | * implementations trying to do that. So let's do what servers do. |
| 3188 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 3189 | * allowed quoted strings in values, with any possible character |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3190 | * after a backslash, including control chars and delimiters, which |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3191 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 3192 | * within values even without quotes. |
| 3193 | * |
| 3194 | * We have to keep multiple pointers in order to support cookie |
| 3195 | * removal at the beginning, middle or end of header without |
| 3196 | * corrupting the header. All of these headers are valid : |
| 3197 | * |
| 3198 | * hdr_beg hdr_end |
| 3199 | * | | |
| 3200 | * v | |
| 3201 | * NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3 | |
| 3202 | * NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3 v |
| 3203 | * NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3 |
| 3204 | * | | | | | | | |
| 3205 | * | | | | | | | |
| 3206 | * | | | | | | +--> next |
| 3207 | * | | | | | +----> val_end |
| 3208 | * | | | | +-----------> val_beg |
| 3209 | * | | | +--------------> equal |
| 3210 | * | | +----------------> att_end |
| 3211 | * | +---------------------> att_beg |
| 3212 | * +--------------------------> prev |
| 3213 | * |
| 3214 | */ |
| 3215 | hdr_beg = ctx.value.ptr; |
| 3216 | hdr_end = hdr_beg + ctx.value.len; |
| 3217 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3218 | /* Iterate through all cookies on this line */ |
| 3219 | |
| 3220 | /* find att_beg */ |
| 3221 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3222 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3223 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3224 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3225 | |
| 3226 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3227 | att_beg++; |
| 3228 | |
| 3229 | /* find att_end : this is the first character after the last non |
| 3230 | * space before the equal. It may be equal to hdr_end. |
| 3231 | */ |
| 3232 | equal = att_end = att_beg; |
| 3233 | while (equal < hdr_end) { |
| 3234 | if (*equal == '=' || *equal == ',' || *equal == ';') |
| 3235 | break; |
| 3236 | if (HTTP_IS_SPHT(*equal++)) |
| 3237 | continue; |
| 3238 | att_end = equal; |
| 3239 | } |
| 3240 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3241 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3242 | * is between <att_beg> and <equal>, both may be identical. |
| 3243 | */ |
| 3244 | /* look for end of cookie if there is an equal sign */ |
| 3245 | if (equal < hdr_end && *equal == '=') { |
| 3246 | /* look for the beginning of the value */ |
| 3247 | val_beg = equal + 1; |
| 3248 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3249 | val_beg++; |
| 3250 | |
| 3251 | /* find the end of the value, respecting quotes */ |
| 3252 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3253 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3254 | /* make val_end point to the first white space or delimiter after the value */ |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3255 | val_end = next; |
| 3256 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3257 | val_end--; |
| 3258 | } |
| 3259 | else |
| 3260 | val_beg = val_end = next = equal; |
| 3261 | |
| 3262 | /* We have nothing to do with attributes beginning with |
| 3263 | * '$'. However, they will automatically be removed if a |
| 3264 | * header before them is removed, since they're supposed |
| 3265 | * to be linked together. |
| 3266 | */ |
| 3267 | if (*att_beg == '$') |
| 3268 | continue; |
| 3269 | |
| 3270 | /* Ignore cookies with no equal sign */ |
| 3271 | if (equal == next) { |
| 3272 | /* This is not our cookie, so we must preserve it. But if we already |
| 3273 | * scheduled another cookie for removal, we cannot remove the |
| 3274 | * complete header, but we can remove the previous block itself. |
| 3275 | */ |
| 3276 | preserve_hdr = 1; |
| 3277 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3278 | int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3279 | val_end += delta; |
| 3280 | next += delta; |
| 3281 | hdr_end += delta; |
| 3282 | prev = del_from; |
| 3283 | del_from = NULL; |
| 3284 | } |
| 3285 | continue; |
| 3286 | } |
| 3287 | |
| 3288 | /* if there are spaces around the equal sign, we need to |
| 3289 | * strip them otherwise we'll get trouble for cookie captures, |
| 3290 | * or even for rewrites. Since this happens extremely rarely, |
| 3291 | * it does not hurt performance. |
| 3292 | */ |
| 3293 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3294 | int stripped_before = 0; |
| 3295 | int stripped_after = 0; |
| 3296 | |
| 3297 | if (att_end != equal) { |
| 3298 | memmove(att_end, equal, hdr_end - equal); |
| 3299 | stripped_before = (att_end - equal); |
| 3300 | equal += stripped_before; |
| 3301 | val_beg += stripped_before; |
| 3302 | } |
| 3303 | |
| 3304 | if (val_beg > equal + 1) { |
| 3305 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3306 | stripped_after = (equal + 1) - val_beg; |
| 3307 | val_beg += stripped_after; |
| 3308 | stripped_before += stripped_after; |
| 3309 | } |
| 3310 | |
| 3311 | val_end += stripped_before; |
| 3312 | next += stripped_before; |
| 3313 | hdr_end += stripped_before; |
| 3314 | } |
| 3315 | /* now everything is as on the diagram above */ |
| 3316 | |
| 3317 | /* First, let's see if we want to capture this cookie. We check |
| 3318 | * that we don't already have a client side cookie, because we |
| 3319 | * can only capture one. Also as an optimisation, we ignore |
| 3320 | * cookies shorter than the declared name. |
| 3321 | */ |
| 3322 | if (sess->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 3323 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3324 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3325 | int log_len = val_end - att_beg; |
| 3326 | |
| 3327 | if ((txn->cli_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3328 | ha_alert("HTTP logging : out of memory.\n"); |
| 3329 | } else { |
| 3330 | if (log_len > sess->fe->capture_len) |
| 3331 | log_len = sess->fe->capture_len; |
| 3332 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 3333 | txn->cli_cookie[log_len] = 0; |
| 3334 | } |
| 3335 | } |
| 3336 | |
| 3337 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 3338 | * following form : |
| 3339 | * |
| 3340 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
| 3341 | * |
| 3342 | * For cookies in prefix mode, the form is : |
| 3343 | * |
| 3344 | * Cookie: NAME=SRV~VALUE |
| 3345 | */ |
| 3346 | if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3347 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3348 | struct server *srv = s->be->srv; |
| 3349 | char *delim; |
| 3350 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3351 | /* if we're in cookie prefix mode, we'll search the delimiter so that we |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3352 | * have the server ID between val_beg and delim, and the original cookie between |
| 3353 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 3354 | * |
| 3355 | * hdr_beg |
| 3356 | * | |
| 3357 | * v |
| 3358 | * NAME=SRV; # in all but prefix modes |
| 3359 | * NAME=SRV~OPAQUE ; # in prefix mode |
| 3360 | * || || | |+-> next |
| 3361 | * || || | +--> val_end |
| 3362 | * || || +---------> delim |
| 3363 | * || |+------------> val_beg |
| 3364 | * || +-------------> att_end = equal |
| 3365 | * |+-----------------> att_beg |
| 3366 | * +------------------> prev |
| 3367 | * |
| 3368 | */ |
| 3369 | if (s->be->ck_opts & PR_CK_PFX) { |
| 3370 | for (delim = val_beg; delim < val_end; delim++) |
| 3371 | if (*delim == COOKIE_DELIM) |
| 3372 | break; |
| 3373 | } |
| 3374 | else { |
| 3375 | char *vbar1; |
| 3376 | delim = val_end; |
| 3377 | /* Now check if the cookie contains a date field, which would |
| 3378 | * appear after a vertical bar ('|') just after the server name |
| 3379 | * and before the delimiter. |
| 3380 | */ |
| 3381 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 3382 | if (vbar1) { |
| 3383 | /* OK, so left of the bar is the server's cookie and |
| 3384 | * right is the last seen date. It is a base64 encoded |
| 3385 | * 30-bit value representing the UNIX date since the |
| 3386 | * epoch in 4-second quantities. |
| 3387 | */ |
| 3388 | int val; |
| 3389 | delim = vbar1++; |
| 3390 | if (val_end - vbar1 >= 5) { |
| 3391 | val = b64tos30(vbar1); |
| 3392 | if (val > 0) |
| 3393 | txn->cookie_last_date = val << 2; |
| 3394 | } |
| 3395 | /* look for a second vertical bar */ |
| 3396 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 3397 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 3398 | val = b64tos30(vbar1 + 1); |
| 3399 | if (val > 0) |
| 3400 | txn->cookie_first_date = val << 2; |
| 3401 | } |
| 3402 | } |
| 3403 | } |
| 3404 | |
| 3405 | /* if the cookie has an expiration date and the proxy wants to check |
| 3406 | * it, then we do that now. We first check if the cookie is too old, |
| 3407 | * then only if it has expired. We detect strict overflow because the |
| 3408 | * time resolution here is not great (4 seconds). Cookies with dates |
| 3409 | * in the future are ignored if their offset is beyond one day. This |
| 3410 | * allows an admin to fix timezone issues without expiring everyone |
| 3411 | * and at the same time avoids keeping unwanted side effects for too |
| 3412 | * long. |
| 3413 | */ |
| 3414 | if (txn->cookie_first_date && s->be->cookie_maxlife && |
| 3415 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) || |
| 3416 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
| 3417 | txn->flags &= ~TX_CK_MASK; |
| 3418 | txn->flags |= TX_CK_OLD; |
| 3419 | delim = val_beg; // let's pretend we have not found the cookie |
| 3420 | txn->cookie_first_date = 0; |
| 3421 | txn->cookie_last_date = 0; |
| 3422 | } |
| 3423 | else if (txn->cookie_last_date && s->be->cookie_maxidle && |
| 3424 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) || |
| 3425 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
| 3426 | txn->flags &= ~TX_CK_MASK; |
| 3427 | txn->flags |= TX_CK_EXPIRED; |
| 3428 | delim = val_beg; // let's pretend we have not found the cookie |
| 3429 | txn->cookie_first_date = 0; |
| 3430 | txn->cookie_last_date = 0; |
| 3431 | } |
| 3432 | |
| 3433 | /* Here, we'll look for the first running server which supports the cookie. |
| 3434 | * This allows to share a same cookie between several servers, for example |
| 3435 | * to dedicate backup servers to specific servers only. |
| 3436 | * However, to prevent clients from sticking to cookie-less backup server |
| 3437 | * when they have incidentely learned an empty cookie, we simply ignore |
| 3438 | * empty cookies and mark them as invalid. |
| 3439 | * The same behaviour is applied when persistence must be ignored. |
| 3440 | */ |
| 3441 | if ((delim == val_beg) || (s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3442 | srv = NULL; |
| 3443 | |
| 3444 | while (srv) { |
| 3445 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 3446 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 3447 | if ((srv->cur_state != SRV_ST_STOPPED) || |
| 3448 | (s->be->options & PR_O_PERSIST) || |
| 3449 | (s->flags & SF_FORCE_PRST)) { |
| 3450 | /* we found the server and we can use it */ |
| 3451 | txn->flags &= ~TX_CK_MASK; |
| 3452 | txn->flags |= (srv->cur_state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN; |
| 3453 | s->flags |= SF_DIRECT | SF_ASSIGNED; |
| 3454 | s->target = &srv->obj_type; |
| 3455 | break; |
| 3456 | } else { |
| 3457 | /* we found a server, but it's down, |
| 3458 | * mark it as such and go on in case |
| 3459 | * another one is available. |
| 3460 | */ |
| 3461 | txn->flags &= ~TX_CK_MASK; |
| 3462 | txn->flags |= TX_CK_DOWN; |
| 3463 | } |
| 3464 | } |
| 3465 | srv = srv->next; |
| 3466 | } |
| 3467 | |
| 3468 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
| 3469 | /* no server matched this cookie or we deliberately skipped it */ |
| 3470 | txn->flags &= ~TX_CK_MASK; |
| 3471 | if ((s->flags & (SF_IGNORE_PRST | SF_ASSIGNED))) |
| 3472 | txn->flags |= TX_CK_UNUSED; |
| 3473 | else |
| 3474 | txn->flags |= TX_CK_INVALID; |
| 3475 | } |
| 3476 | |
| 3477 | /* depending on the cookie mode, we may have to either : |
| 3478 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3479 | * the server never sees it ; |
| 3480 | * - remove the server id from the cookie value, and tag the cookie as an |
Joseph Herlant | e9d5c72 | 2018-11-25 11:00:25 -0800 | [diff] [blame] | 3481 | * application cookie so that it does not get accidentally removed later, |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3482 | * if we're in cookie prefix mode |
| 3483 | */ |
| 3484 | if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
| 3485 | int delta; /* negative */ |
| 3486 | |
| 3487 | memmove(val_beg, delim + 1, hdr_end - (delim + 1)); |
| 3488 | delta = val_beg - (delim + 1); |
| 3489 | val_end += delta; |
| 3490 | next += delta; |
| 3491 | hdr_end += delta; |
| 3492 | del_from = NULL; |
| 3493 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 3494 | } |
| 3495 | else if (del_from == NULL && |
| 3496 | (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) { |
| 3497 | del_from = prev; |
| 3498 | } |
| 3499 | } |
| 3500 | else { |
| 3501 | /* This is not our cookie, so we must preserve it. But if we already |
| 3502 | * scheduled another cookie for removal, we cannot remove the |
| 3503 | * complete header, but we can remove the previous block itself. |
| 3504 | */ |
| 3505 | preserve_hdr = 1; |
| 3506 | |
| 3507 | if (del_from != NULL) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3508 | int delta = http_del_hdr_value(hdr_beg, hdr_end, &del_from, prev); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3509 | if (att_beg >= del_from) |
| 3510 | att_beg += delta; |
| 3511 | if (att_end >= del_from) |
| 3512 | att_end += delta; |
| 3513 | val_beg += delta; |
| 3514 | val_end += delta; |
| 3515 | next += delta; |
| 3516 | hdr_end += delta; |
| 3517 | prev = del_from; |
| 3518 | del_from = NULL; |
| 3519 | } |
| 3520 | } |
| 3521 | |
| 3522 | /* continue with next cookie on this header line */ |
| 3523 | att_beg = next; |
| 3524 | } /* for each cookie */ |
| 3525 | |
| 3526 | |
| 3527 | /* There are no more cookies on this line. |
| 3528 | * We may still have one (or several) marked for deletion at the |
| 3529 | * end of the line. We must do this now in two ways : |
| 3530 | * - if some cookies must be preserved, we only delete from the |
| 3531 | * mark to the end of line ; |
| 3532 | * - if nothing needs to be preserved, simply delete the whole header |
| 3533 | */ |
| 3534 | if (del_from) { |
| 3535 | hdr_end = (preserve_hdr ? del_from : hdr_beg); |
| 3536 | } |
| 3537 | if ((hdr_end - hdr_beg) != ctx.value.len) { |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3538 | if (hdr_beg != hdr_end) |
| 3539 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3540 | else |
| 3541 | http_remove_header(htx, &ctx); |
| 3542 | } |
| 3543 | } /* for each "Cookie header */ |
| 3544 | } |
| 3545 | |
| 3546 | /* |
| 3547 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 3548 | * desirable to call it only when needed. This function is also used when we |
| 3549 | * just need to know if there is a cookie (eg: for check-cache). |
| 3550 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3551 | static void http_manage_server_side_cookies(struct stream *s, struct channel *res) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3552 | { |
| 3553 | struct session *sess = s->sess; |
| 3554 | struct http_txn *txn = s->txn; |
| 3555 | struct htx *htx; |
| 3556 | struct http_hdr_ctx ctx; |
| 3557 | struct server *srv; |
| 3558 | char *hdr_beg, *hdr_end; |
| 3559 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 6f7a02a | 2019-04-15 21:49:49 +0200 | [diff] [blame] | 3560 | int is_cookie2 = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3561 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3562 | htx = htxbuf(&res->buf); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3563 | |
| 3564 | ctx.blk = NULL; |
| 3565 | while (1) { |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3566 | int is_first = 1; |
| 3567 | |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3568 | if (!http_find_header(htx, ist("Set-Cookie"), &ctx, 1)) { |
| 3569 | if (!http_find_header(htx, ist("Set-Cookie2"), &ctx, 1)) |
| 3570 | break; |
| 3571 | is_cookie2 = 1; |
| 3572 | } |
| 3573 | |
| 3574 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 3575 | * <prev> points to the colon. |
| 3576 | */ |
| 3577 | txn->flags |= TX_SCK_PRESENT; |
| 3578 | |
| 3579 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 3580 | * check-cache is enabled) and we are not interested in checking |
| 3581 | * them. Warning, the cookie capture is declared in the frontend. |
| 3582 | */ |
| 3583 | if (s->be->cookie_name == NULL && sess->fe->capture_name == NULL) |
| 3584 | break; |
| 3585 | |
| 3586 | /* OK so now we know we have to process this response cookie. |
| 3587 | * The format of the Set-Cookie header is slightly different |
| 3588 | * from the format of the Cookie header in that it does not |
| 3589 | * support the comma as a cookie delimiter (thus the header |
| 3590 | * cannot be folded) because the Expires attribute described in |
| 3591 | * the original Netscape's spec may contain an unquoted date |
| 3592 | * with a comma inside. We have to live with this because |
| 3593 | * many browsers don't support Max-Age and some browsers don't |
| 3594 | * support quoted strings. However the Set-Cookie2 header is |
| 3595 | * clean. |
| 3596 | * |
| 3597 | * We have to keep multiple pointers in order to support cookie |
| 3598 | * removal at the beginning, middle or end of header without |
| 3599 | * corrupting the header (in case of set-cookie2). A special |
| 3600 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 3601 | * fields after the first semi-colon. The <next> pointer points |
| 3602 | * either to the end of line (set-cookie) or next unquoted comma |
| 3603 | * (set-cookie2). All of these headers are valid : |
| 3604 | * |
| 3605 | * hdr_beg hdr_end |
| 3606 | * | | |
| 3607 | * v | |
| 3608 | * NAME1 = VALUE 1 ; Secure; Path="/" | |
| 3609 | * NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT v |
| 3610 | * NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT |
| 3611 | * NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard |
| 3612 | * | | | | | | | | |
| 3613 | * | | | | | | | +-> next |
| 3614 | * | | | | | | +------------> scav |
| 3615 | * | | | | | +--------------> val_end |
| 3616 | * | | | | +--------------------> val_beg |
| 3617 | * | | | +----------------------> equal |
| 3618 | * | | +------------------------> att_end |
| 3619 | * | +----------------------------> att_beg |
| 3620 | * +------------------------------> prev |
| 3621 | * -------------------------------> hdr_beg |
| 3622 | */ |
| 3623 | hdr_beg = ctx.value.ptr; |
| 3624 | hdr_end = hdr_beg + ctx.value.len; |
| 3625 | for (prev = hdr_beg; prev < hdr_end; prev = next) { |
| 3626 | |
| 3627 | /* Iterate through all cookies on this line */ |
| 3628 | |
| 3629 | /* find att_beg */ |
| 3630 | att_beg = prev; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3631 | if (!is_first) |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3632 | att_beg++; |
Olivier Houchard | f0f4238 | 2019-07-22 17:43:46 +0200 | [diff] [blame] | 3633 | is_first = 0; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3634 | |
| 3635 | while (att_beg < hdr_end && HTTP_IS_SPHT(*att_beg)) |
| 3636 | att_beg++; |
| 3637 | |
| 3638 | /* find att_end : this is the first character after the last non |
| 3639 | * space before the equal. It may be equal to hdr_end. |
| 3640 | */ |
| 3641 | equal = att_end = att_beg; |
| 3642 | |
| 3643 | while (equal < hdr_end) { |
| 3644 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 3645 | break; |
| 3646 | if (HTTP_IS_SPHT(*equal++)) |
| 3647 | continue; |
| 3648 | att_end = equal; |
| 3649 | } |
| 3650 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3651 | /* here, <equal> points to '=', a delimiter or the end. <att_end> |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3652 | * is between <att_beg> and <equal>, both may be identical. |
| 3653 | */ |
| 3654 | |
| 3655 | /* look for end of cookie if there is an equal sign */ |
| 3656 | if (equal < hdr_end && *equal == '=') { |
| 3657 | /* look for the beginning of the value */ |
| 3658 | val_beg = equal + 1; |
| 3659 | while (val_beg < hdr_end && HTTP_IS_SPHT(*val_beg)) |
| 3660 | val_beg++; |
| 3661 | |
| 3662 | /* find the end of the value, respecting quotes */ |
| 3663 | next = http_find_cookie_value_end(val_beg, hdr_end); |
| 3664 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 3665 | /* make val_end point to the first white space or delimiter after the value */ |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3666 | val_end = next; |
| 3667 | while (val_end > val_beg && HTTP_IS_SPHT(*(val_end - 1))) |
| 3668 | val_end--; |
| 3669 | } |
| 3670 | else { |
| 3671 | /* <equal> points to next comma, semi-colon or EOL */ |
| 3672 | val_beg = val_end = next = equal; |
| 3673 | } |
| 3674 | |
| 3675 | if (next < hdr_end) { |
| 3676 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 3677 | * a colon or semi-colon before the end. So skip all attr-value |
| 3678 | * pairs and look for the next comma. For Set-Cookie, since |
| 3679 | * commas are permitted in values, skip to the end. |
| 3680 | */ |
| 3681 | if (is_cookie2) |
| 3682 | next = http_find_hdr_value_end(next, hdr_end); |
| 3683 | else |
| 3684 | next = hdr_end; |
| 3685 | } |
| 3686 | |
| 3687 | /* Now everything is as on the diagram above */ |
| 3688 | |
| 3689 | /* Ignore cookies with no equal sign */ |
| 3690 | if (equal == val_end) |
| 3691 | continue; |
| 3692 | |
| 3693 | /* If there are spaces around the equal sign, we need to |
| 3694 | * strip them otherwise we'll get trouble for cookie captures, |
| 3695 | * or even for rewrites. Since this happens extremely rarely, |
| 3696 | * it does not hurt performance. |
| 3697 | */ |
| 3698 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 3699 | int stripped_before = 0; |
| 3700 | int stripped_after = 0; |
| 3701 | |
| 3702 | if (att_end != equal) { |
| 3703 | memmove(att_end, equal, hdr_end - equal); |
| 3704 | stripped_before = (att_end - equal); |
| 3705 | equal += stripped_before; |
| 3706 | val_beg += stripped_before; |
| 3707 | } |
| 3708 | |
| 3709 | if (val_beg > equal + 1) { |
| 3710 | memmove(equal + 1, val_beg, hdr_end + stripped_before - val_beg); |
| 3711 | stripped_after = (equal + 1) - val_beg; |
| 3712 | val_beg += stripped_after; |
| 3713 | stripped_before += stripped_after; |
| 3714 | } |
| 3715 | |
| 3716 | val_end += stripped_before; |
| 3717 | next += stripped_before; |
| 3718 | hdr_end += stripped_before; |
| 3719 | |
Christopher Faulet | 3e2638e | 2019-06-18 09:49:16 +0200 | [diff] [blame] | 3720 | htx_change_blk_value_len(htx, ctx.blk, hdr_end - hdr_beg); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3721 | ctx.value.len = hdr_end - hdr_beg; |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3722 | } |
| 3723 | |
| 3724 | /* First, let's see if we want to capture this cookie. We check |
| 3725 | * that we don't already have a server side cookie, because we |
| 3726 | * can only capture one. Also as an optimisation, we ignore |
| 3727 | * cookies shorter than the declared name. |
| 3728 | */ |
| 3729 | if (sess->fe->capture_name != NULL && |
| 3730 | txn->srv_cookie == NULL && |
| 3731 | (val_end - att_beg >= sess->fe->capture_namelen) && |
| 3732 | memcmp(att_beg, sess->fe->capture_name, sess->fe->capture_namelen) == 0) { |
| 3733 | int log_len = val_end - att_beg; |
| 3734 | if ((txn->srv_cookie = pool_alloc(pool_head_capture)) == NULL) { |
| 3735 | ha_alert("HTTP logging : out of memory.\n"); |
| 3736 | } |
| 3737 | else { |
| 3738 | if (log_len > sess->fe->capture_len) |
| 3739 | log_len = sess->fe->capture_len; |
| 3740 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 3741 | txn->srv_cookie[log_len] = 0; |
| 3742 | } |
| 3743 | } |
| 3744 | |
| 3745 | srv = objt_server(s->target); |
| 3746 | /* now check if we need to process it for persistence */ |
| 3747 | if (!(s->flags & SF_IGNORE_PRST) && |
| 3748 | (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) && |
| 3749 | (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) { |
| 3750 | /* assume passive cookie by default */ |
| 3751 | txn->flags &= ~TX_SCK_MASK; |
| 3752 | txn->flags |= TX_SCK_FOUND; |
| 3753 | |
| 3754 | /* If the cookie is in insert mode on a known server, we'll delete |
| 3755 | * this occurrence because we'll insert another one later. |
| 3756 | * We'll delete it too if the "indirect" option is set and we're in |
| 3757 | * a direct access. |
| 3758 | */ |
| 3759 | if (s->be->ck_opts & PR_CK_PSV) { |
| 3760 | /* The "preserve" flag was set, we don't want to touch the |
| 3761 | * server's cookie. |
| 3762 | */ |
| 3763 | } |
| 3764 | else if ((srv && (s->be->ck_opts & PR_CK_INS)) || |
| 3765 | ((s->flags & SF_DIRECT) && (s->be->ck_opts & PR_CK_IND))) { |
| 3766 | /* this cookie must be deleted */ |
| 3767 | if (prev == hdr_beg && next == hdr_end) { |
| 3768 | /* whole header */ |
| 3769 | http_remove_header(htx, &ctx); |
| 3770 | /* note: while both invalid now, <next> and <hdr_end> |
| 3771 | * are still equal, so the for() will stop as expected. |
| 3772 | */ |
| 3773 | } else { |
| 3774 | /* just remove the value */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3775 | int delta = http_del_hdr_value(hdr_beg, hdr_end, &prev, next); |
Christopher Faulet | fcda7c6 | 2018-10-24 11:56:22 +0200 | [diff] [blame] | 3776 | next = prev; |
| 3777 | hdr_end += delta; |
| 3778 | } |
| 3779 | txn->flags &= ~TX_SCK_MASK; |
| 3780 | txn->flags |= TX_SCK_DELETED; |
| 3781 | /* and go on with next cookie */ |
| 3782 | } |
| 3783 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) { |
| 3784 | /* replace bytes val_beg->val_end with the cookie name associated |
| 3785 | * with this server since we know it. |
| 3786 | */ |
| 3787 | int sliding, delta; |
| 3788 | |
| 3789 | ctx.value = ist2(val_beg, val_end - val_beg); |
| 3790 | ctx.lws_before = ctx.lws_after = 0; |
| 3791 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen)); |
| 3792 | delta = srv->cklen - (val_end - val_beg); |
| 3793 | sliding = (ctx.value.ptr - val_beg); |
| 3794 | hdr_beg += sliding; |
| 3795 | val_beg += sliding; |
| 3796 | next += sliding + delta; |
| 3797 | hdr_end += sliding + delta; |
| 3798 | |
| 3799 | txn->flags &= ~TX_SCK_MASK; |
| 3800 | txn->flags |= TX_SCK_REPLACED; |
| 3801 | } |
| 3802 | else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) { |
| 3803 | /* insert the cookie name associated with this server |
| 3804 | * before existing cookie, and insert a delimiter between them.. |
| 3805 | */ |
| 3806 | int sliding, delta; |
| 3807 | ctx.value = ist2(val_beg, 0); |
| 3808 | ctx.lws_before = ctx.lws_after = 0; |
| 3809 | http_replace_header_value(htx, &ctx, ist2(srv->cookie, srv->cklen + 1)); |
| 3810 | delta = srv->cklen + 1; |
| 3811 | sliding = (ctx.value.ptr - val_beg); |
| 3812 | hdr_beg += sliding; |
| 3813 | val_beg += sliding; |
| 3814 | next += sliding + delta; |
| 3815 | hdr_end += sliding + delta; |
| 3816 | |
| 3817 | val_beg[srv->cklen] = COOKIE_DELIM; |
| 3818 | txn->flags &= ~TX_SCK_MASK; |
| 3819 | txn->flags |= TX_SCK_REPLACED; |
| 3820 | } |
| 3821 | } |
| 3822 | /* that's done for this cookie, check the next one on the same |
| 3823 | * line when next != hdr_end (only if is_cookie2). |
| 3824 | */ |
| 3825 | } |
| 3826 | } |
| 3827 | } |
| 3828 | |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3829 | /* |
| 3830 | * Parses the Cache-Control and Pragma request header fields to determine if |
| 3831 | * the request may be served from the cache and/or if it is cacheable. Updates |
| 3832 | * s->txn->flags. |
| 3833 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3834 | void http_check_request_for_cacheability(struct stream *s, struct channel *req) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3835 | { |
| 3836 | struct http_txn *txn = s->txn; |
| 3837 | struct htx *htx; |
| 3838 | int32_t pos; |
| 3839 | int pragma_found, cc_found, i; |
| 3840 | |
| 3841 | if ((txn->flags & (TX_CACHEABLE|TX_CACHE_IGNORE)) == TX_CACHE_IGNORE) |
| 3842 | return; /* nothing more to do here */ |
| 3843 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3844 | htx = htxbuf(&req->buf); |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3845 | pragma_found = cc_found = 0; |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 3846 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3847 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 3848 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 3849 | struct ist n, v; |
| 3850 | |
| 3851 | if (type == HTX_BLK_EOH) |
| 3852 | break; |
| 3853 | if (type != HTX_BLK_HDR) |
| 3854 | continue; |
| 3855 | |
| 3856 | n = htx_get_blk_name(htx, blk); |
| 3857 | v = htx_get_blk_value(htx, blk); |
| 3858 | |
Willy Tarreau | 2e754bf | 2018-12-07 11:38:03 +0100 | [diff] [blame] | 3859 | if (isteq(n, ist("pragma"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3860 | if (v.len >= 8 && strncasecmp(v.ptr, "no-cache", 8) == 0) { |
| 3861 | pragma_found = 1; |
| 3862 | continue; |
| 3863 | } |
| 3864 | } |
| 3865 | |
| 3866 | /* Don't use the cache and don't try to store if we found the |
| 3867 | * Authorization header */ |
Willy Tarreau | 2e754bf | 2018-12-07 11:38:03 +0100 | [diff] [blame] | 3868 | if (isteq(n, ist("authorization"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3869 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3870 | txn->flags |= TX_CACHE_IGNORE; |
| 3871 | continue; |
| 3872 | } |
| 3873 | |
Willy Tarreau | 2e754bf | 2018-12-07 11:38:03 +0100 | [diff] [blame] | 3874 | if (!isteq(n, ist("cache-control"))) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3875 | continue; |
| 3876 | |
| 3877 | /* OK, right now we know we have a cache-control header */ |
| 3878 | cc_found = 1; |
| 3879 | if (!v.len) /* no info */ |
| 3880 | continue; |
| 3881 | |
| 3882 | i = 0; |
| 3883 | while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' && |
| 3884 | !isspace((unsigned char)*(v.ptr+i))) |
| 3885 | i++; |
| 3886 | |
| 3887 | /* we have a complete value between v.ptr and (v.ptr+i). We don't check the |
| 3888 | * values after max-age, max-stale nor min-fresh, we simply don't |
| 3889 | * use the cache when they're specified. |
| 3890 | */ |
| 3891 | if (((i == 7) && strncasecmp(v.ptr, "max-age", 7) == 0) || |
| 3892 | ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) || |
| 3893 | ((i == 9) && strncasecmp(v.ptr, "max-stale", 9) == 0) || |
| 3894 | ((i == 9) && strncasecmp(v.ptr, "min-fresh", 9) == 0)) { |
| 3895 | txn->flags |= TX_CACHE_IGNORE; |
| 3896 | continue; |
| 3897 | } |
| 3898 | |
| 3899 | if ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0) { |
| 3900 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3901 | continue; |
| 3902 | } |
| 3903 | } |
| 3904 | |
| 3905 | /* RFC7234#5.4: |
| 3906 | * When the Cache-Control header field is also present and |
| 3907 | * understood in a request, Pragma is ignored. |
| 3908 | * When the Cache-Control header field is not present in a |
| 3909 | * request, caches MUST consider the no-cache request |
| 3910 | * pragma-directive as having the same effect as if |
| 3911 | * "Cache-Control: no-cache" were present. |
| 3912 | */ |
| 3913 | if (!cc_found && pragma_found) |
| 3914 | txn->flags |= TX_CACHE_IGNORE; |
| 3915 | } |
| 3916 | |
| 3917 | /* |
| 3918 | * Check if response is cacheable or not. Updates s->txn->flags. |
| 3919 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 3920 | void http_check_response_for_cacheability(struct stream *s, struct channel *res) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3921 | { |
| 3922 | struct http_txn *txn = s->txn; |
| 3923 | struct htx *htx; |
| 3924 | int32_t pos; |
| 3925 | int i; |
| 3926 | |
| 3927 | if (txn->status < 200) { |
| 3928 | /* do not try to cache interim responses! */ |
| 3929 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3930 | return; |
| 3931 | } |
| 3932 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 3933 | htx = htxbuf(&res->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 3934 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3935 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 3936 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 3937 | struct ist n, v; |
| 3938 | |
| 3939 | if (type == HTX_BLK_EOH) |
| 3940 | break; |
| 3941 | if (type != HTX_BLK_HDR) |
| 3942 | continue; |
| 3943 | |
| 3944 | n = htx_get_blk_name(htx, blk); |
| 3945 | v = htx_get_blk_value(htx, blk); |
| 3946 | |
Willy Tarreau | 2e754bf | 2018-12-07 11:38:03 +0100 | [diff] [blame] | 3947 | if (isteq(n, ist("pragma"))) { |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3948 | if ((v.len >= 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) { |
| 3949 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3950 | return; |
| 3951 | } |
| 3952 | } |
| 3953 | |
Willy Tarreau | 2e754bf | 2018-12-07 11:38:03 +0100 | [diff] [blame] | 3954 | if (!isteq(n, ist("cache-control"))) |
Christopher Faulet | 25a02f6 | 2018-10-24 12:00:25 +0200 | [diff] [blame] | 3955 | continue; |
| 3956 | |
| 3957 | /* OK, right now we know we have a cache-control header */ |
| 3958 | if (!v.len) /* no info */ |
| 3959 | continue; |
| 3960 | |
| 3961 | i = 0; |
| 3962 | while (i < v.len && *(v.ptr+i) != '=' && *(v.ptr+i) != ',' && |
| 3963 | !isspace((unsigned char)*(v.ptr+i))) |
| 3964 | i++; |
| 3965 | |
| 3966 | /* we have a complete value between v.ptr and (v.ptr+i) */ |
| 3967 | if (i < v.len && *(v.ptr + i) == '=') { |
| 3968 | if (((v.len - i) > 1 && (i == 7) && strncasecmp(v.ptr, "max-age=0", 9) == 0) || |
| 3969 | ((v.len - i) > 1 && (i == 8) && strncasecmp(v.ptr, "s-maxage=0", 10) == 0)) { |
| 3970 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3971 | continue; |
| 3972 | } |
| 3973 | |
| 3974 | /* we have something of the form no-cache="set-cookie" */ |
| 3975 | if ((v.len >= 21) && |
| 3976 | strncasecmp(v.ptr, "no-cache=\"set-cookie", 20) == 0 |
| 3977 | && (*(v.ptr + 20) == '"' || *(v.ptr + 20 ) == ',')) |
| 3978 | txn->flags &= ~TX_CACHE_COOK; |
| 3979 | continue; |
| 3980 | } |
| 3981 | |
| 3982 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 3983 | if (((i == 7) && strncasecmp(v.ptr, "private", 7) == 0) || |
| 3984 | ((i == 8) && strncasecmp(v.ptr, "no-cache", 8) == 0) || |
| 3985 | ((i == 8) && strncasecmp(v.ptr, "no-store", 8) == 0)) { |
| 3986 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3987 | return; |
| 3988 | } |
| 3989 | |
| 3990 | if ((i == 6) && strncasecmp(v.ptr, "public", 6) == 0) { |
| 3991 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 3992 | continue; |
| 3993 | } |
| 3994 | } |
| 3995 | } |
| 3996 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 3997 | /* |
| 3998 | * In a GET, HEAD or POST request, check if the requested URI matches the stats uri |
| 3999 | * for the current backend. |
| 4000 | * |
| 4001 | * It is assumed that the request is either a HEAD, GET, or POST and that the |
| 4002 | * uri_auth field is valid. |
| 4003 | * |
| 4004 | * Returns 1 if stats should be provided, otherwise 0. |
| 4005 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4006 | static int http_stats_check_uri(struct stream *s, struct http_txn *txn, struct proxy *backend) |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4007 | { |
| 4008 | struct uri_auth *uri_auth = backend->uri_auth; |
| 4009 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4010 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4011 | struct ist uri; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4012 | |
| 4013 | if (!uri_auth) |
| 4014 | return 0; |
| 4015 | |
| 4016 | if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST) |
| 4017 | return 0; |
| 4018 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4019 | htx = htxbuf(&s->req.buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 4020 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4021 | uri = htx_sl_req_uri(sl); |
Willy Tarreau | 1eb3b48 | 2019-10-31 15:50:28 +0100 | [diff] [blame] | 4022 | if (*uri_auth->uri_prefix == '/') |
| 4023 | uri = http_get_path(uri); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4024 | |
| 4025 | /* check URI size */ |
| 4026 | if (uri_auth->uri_len > uri.len) |
| 4027 | return 0; |
| 4028 | |
| 4029 | if (memcmp(uri.ptr, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
| 4030 | return 0; |
| 4031 | |
| 4032 | return 1; |
| 4033 | } |
| 4034 | |
| 4035 | /* This function prepares an applet to handle the stats. It can deal with the |
| 4036 | * "100-continue" expectation, check that admin rules are met for POST requests, |
| 4037 | * and program a response message if something was unexpected. It cannot fail |
| 4038 | * and always relies on the stats applet to complete the job. It does not touch |
| 4039 | * analysers nor counters, which are left to the caller. It does not touch |
| 4040 | * s->target which is supposed to already point to the stats applet. The caller |
| 4041 | * is expected to have already assigned an appctx to the stream. |
| 4042 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4043 | static int http_handle_stats(struct stream *s, struct channel *req) |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4044 | { |
| 4045 | struct stats_admin_rule *stats_admin_rule; |
| 4046 | struct stream_interface *si = &s->si[1]; |
| 4047 | struct session *sess = s->sess; |
| 4048 | struct http_txn *txn = s->txn; |
| 4049 | struct http_msg *msg = &txn->req; |
| 4050 | struct uri_auth *uri_auth = s->be->uri_auth; |
| 4051 | const char *h, *lookup, *end; |
| 4052 | struct appctx *appctx; |
| 4053 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4054 | struct htx_sl *sl; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4055 | |
| 4056 | appctx = si_appctx(si); |
| 4057 | memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats)); |
| 4058 | appctx->st1 = appctx->st2 = 0; |
| 4059 | appctx->ctx.stats.st_code = STAT_STATUS_INIT; |
Willy Tarreau | 676c29e | 2019-10-09 10:50:01 +0200 | [diff] [blame] | 4060 | appctx->ctx.stats.flags |= uri_auth->flags; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4061 | appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */ |
| 4062 | if ((msg->flags & HTTP_MSGF_VER_11) && (txn->meth != HTTP_METH_HEAD)) |
| 4063 | appctx->ctx.stats.flags |= STAT_CHUNKED; |
| 4064 | |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4065 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 4066 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4067 | lookup = HTX_SL_REQ_UPTR(sl) + uri_auth->uri_len; |
| 4068 | end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4069 | |
| 4070 | for (h = lookup; h <= end - 3; h++) { |
| 4071 | if (memcmp(h, ";up", 3) == 0) { |
| 4072 | appctx->ctx.stats.flags |= STAT_HIDE_DOWN; |
| 4073 | break; |
| 4074 | } |
| 4075 | } |
| 4076 | |
| 4077 | if (uri_auth->refresh) { |
| 4078 | for (h = lookup; h <= end - 10; h++) { |
| 4079 | if (memcmp(h, ";norefresh", 10) == 0) { |
| 4080 | appctx->ctx.stats.flags |= STAT_NO_REFRESH; |
| 4081 | break; |
| 4082 | } |
| 4083 | } |
| 4084 | } |
| 4085 | |
| 4086 | for (h = lookup; h <= end - 4; h++) { |
| 4087 | if (memcmp(h, ";csv", 4) == 0) { |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 4088 | appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4089 | break; |
| 4090 | } |
| 4091 | } |
| 4092 | |
| 4093 | for (h = lookup; h <= end - 6; h++) { |
| 4094 | if (memcmp(h, ";typed", 6) == 0) { |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 4095 | appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4096 | appctx->ctx.stats.flags |= STAT_FMT_TYPED; |
| 4097 | break; |
| 4098 | } |
| 4099 | } |
| 4100 | |
Christopher Faulet | 6338a08 | 2019-09-09 15:50:54 +0200 | [diff] [blame] | 4101 | for (h = lookup; h <= end - 5; h++) { |
| 4102 | if (memcmp(h, ";json", 5) == 0) { |
| 4103 | appctx->ctx.stats.flags &= ~(STAT_FMT_MASK|STAT_JSON_SCHM); |
| 4104 | appctx->ctx.stats.flags |= STAT_FMT_JSON; |
| 4105 | break; |
| 4106 | } |
| 4107 | } |
| 4108 | |
| 4109 | for (h = lookup; h <= end - 12; h++) { |
| 4110 | if (memcmp(h, ";json-schema", 12) == 0) { |
| 4111 | appctx->ctx.stats.flags &= ~STAT_FMT_MASK; |
| 4112 | appctx->ctx.stats.flags |= STAT_JSON_SCHM; |
| 4113 | break; |
| 4114 | } |
| 4115 | } |
| 4116 | |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4117 | for (h = lookup; h <= end - 8; h++) { |
| 4118 | if (memcmp(h, ";st=", 4) == 0) { |
| 4119 | int i; |
| 4120 | h += 4; |
| 4121 | appctx->ctx.stats.st_code = STAT_STATUS_UNKN; |
| 4122 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 4123 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
| 4124 | appctx->ctx.stats.st_code = i; |
| 4125 | break; |
| 4126 | } |
| 4127 | } |
| 4128 | break; |
| 4129 | } |
| 4130 | } |
| 4131 | |
| 4132 | appctx->ctx.stats.scope_str = 0; |
| 4133 | appctx->ctx.stats.scope_len = 0; |
| 4134 | for (h = lookup; h <= end - 8; h++) { |
| 4135 | if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) { |
| 4136 | int itx = 0; |
| 4137 | const char *h2; |
| 4138 | char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1]; |
| 4139 | const char *err; |
| 4140 | |
| 4141 | h += strlen(STAT_SCOPE_INPUT_NAME) + 1; |
| 4142 | h2 = h; |
Christopher Faulet | ed7a066 | 2019-01-14 11:07:34 +0100 | [diff] [blame] | 4143 | appctx->ctx.stats.scope_str = h2 - HTX_SL_REQ_UPTR(sl); |
| 4144 | while (h < end) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4145 | if (*h == ';' || *h == '&' || *h == ' ') |
| 4146 | break; |
| 4147 | itx++; |
| 4148 | h++; |
| 4149 | } |
| 4150 | |
| 4151 | if (itx > STAT_SCOPE_TXT_MAXLEN) |
| 4152 | itx = STAT_SCOPE_TXT_MAXLEN; |
| 4153 | appctx->ctx.stats.scope_len = itx; |
| 4154 | |
| 4155 | /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */ |
| 4156 | memcpy(scope_txt, h2, itx); |
| 4157 | scope_txt[itx] = '\0'; |
| 4158 | err = invalid_char(scope_txt); |
| 4159 | if (err) { |
| 4160 | /* bad char in search text => clear scope */ |
| 4161 | appctx->ctx.stats.scope_str = 0; |
| 4162 | appctx->ctx.stats.scope_len = 0; |
| 4163 | } |
| 4164 | break; |
| 4165 | } |
| 4166 | } |
| 4167 | |
| 4168 | /* now check whether we have some admin rules for this request */ |
| 4169 | list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) { |
| 4170 | int ret = 1; |
| 4171 | |
| 4172 | if (stats_admin_rule->cond) { |
| 4173 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 4174 | ret = acl_pass(ret); |
| 4175 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 4176 | ret = !ret; |
| 4177 | } |
| 4178 | |
| 4179 | if (ret) { |
| 4180 | /* no rule, or the rule matches */ |
| 4181 | appctx->ctx.stats.flags |= STAT_ADMIN; |
| 4182 | break; |
| 4183 | } |
| 4184 | } |
| 4185 | |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4186 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 4187 | appctx->st0 = STAT_HTTP_HEAD; |
| 4188 | else if (txn->meth == HTTP_METH_POST) { |
Christopher Faulet | bd9e842 | 2019-08-15 22:26:48 +0200 | [diff] [blame] | 4189 | if (appctx->ctx.stats.flags & STAT_ADMIN) { |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4190 | appctx->st0 = STAT_HTTP_POST; |
Christopher Faulet | bd9e842 | 2019-08-15 22:26:48 +0200 | [diff] [blame] | 4191 | if (msg->msg_state < HTTP_MSG_DATA) |
| 4192 | req->analysers |= AN_REQ_HTTP_BODY; |
| 4193 | } |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4194 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4195 | /* POST without admin level */ |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4196 | appctx->ctx.stats.flags &= ~STAT_CHUNKED; |
| 4197 | appctx->ctx.stats.st_code = STAT_STATUS_DENY; |
| 4198 | appctx->st0 = STAT_HTTP_LAST; |
| 4199 | } |
| 4200 | } |
| 4201 | else { |
Christopher Faulet | 5d45e38 | 2019-02-27 15:15:23 +0100 | [diff] [blame] | 4202 | /* Unsupported method */ |
| 4203 | appctx->ctx.stats.flags &= ~STAT_CHUNKED; |
| 4204 | appctx->ctx.stats.st_code = STAT_STATUS_IVAL; |
| 4205 | appctx->st0 = STAT_HTTP_LAST; |
Christopher Faulet | 377c5a5 | 2018-10-24 21:21:30 +0200 | [diff] [blame] | 4206 | } |
| 4207 | |
| 4208 | s->task->nice = -32; /* small boost for HTTP statistics */ |
| 4209 | return 1; |
| 4210 | } |
| 4211 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4212 | void http_perform_server_redirect(struct stream *s, struct stream_interface *si) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4213 | { |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4214 | struct channel *req = &s->req; |
| 4215 | struct channel *res = &s->res; |
| 4216 | struct server *srv; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4217 | struct htx *htx; |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4218 | struct htx_sl *sl; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4219 | struct ist path, location; |
| 4220 | unsigned int flags; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4221 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4222 | /* |
| 4223 | * Create the location |
| 4224 | */ |
| 4225 | chunk_reset(&trash); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4226 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4227 | /* 1: add the server's prefix */ |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4228 | /* special prefix "/" means don't change URL */ |
| 4229 | srv = __objt_server(s->target); |
| 4230 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 4231 | if (!chunk_memcat(&trash, srv->rdr_pfx, srv->rdr_len)) |
| 4232 | return; |
| 4233 | } |
| 4234 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4235 | /* 2: add the request Path */ |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 4236 | htx = htxbuf(&req->buf); |
Christopher Faulet | 297fbb4 | 2019-05-13 14:41:27 +0200 | [diff] [blame] | 4237 | sl = http_get_stline(htx); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4238 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4239 | if (!isttest(path)) |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4240 | return; |
| 4241 | |
| 4242 | if (!chunk_memcat(&trash, path.ptr, path.len)) |
| 4243 | return; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4244 | location = ist2(trash.area, trash.data); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4245 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4246 | /* |
| 4247 | * Create the 302 respone |
| 4248 | */ |
| 4249 | htx = htx_from_buf(&res->buf); |
| 4250 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 4251 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4252 | ist("HTTP/1.1"), ist("302"), ist("Found")); |
| 4253 | if (!sl) |
| 4254 | goto fail; |
| 4255 | sl->info.res.status = 302; |
| 4256 | s->txn->status = 302; |
| 4257 | |
| 4258 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) || |
| 4259 | !htx_add_header(htx, ist("Connection"), ist("close")) || |
| 4260 | !htx_add_header(htx, ist("Content-length"), ist("0")) || |
| 4261 | !htx_add_header(htx, ist("Location"), location)) |
| 4262 | goto fail; |
| 4263 | |
| 4264 | if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM)) |
| 4265 | goto fail; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4266 | |
Christopher Faulet | c20afb8 | 2020-01-24 19:16:26 +0100 | [diff] [blame] | 4267 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4268 | if (!http_forward_proxy_resp(s, 1)) |
| 4269 | goto fail; |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4270 | |
| 4271 | /* return without error. */ |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4272 | si_shutr(si); |
| 4273 | si_shutw(si); |
| 4274 | si->err_type = SI_ET_NONE; |
| 4275 | si->state = SI_ST_CLO; |
| 4276 | |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4277 | if (!(s->flags & SF_ERR_MASK)) |
| 4278 | s->flags |= SF_ERR_LOCAL; |
| 4279 | if (!(s->flags & SF_FINST_MASK)) |
| 4280 | s->flags |= SF_FINST_C; |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4281 | |
| 4282 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 4283 | srv_inc_sess_ctr(srv); |
| 4284 | srv_set_sess_last(srv); |
Christopher Faulet | 0eaed6b | 2018-11-28 17:46:40 +0100 | [diff] [blame] | 4285 | return; |
| 4286 | |
| 4287 | fail: |
| 4288 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4289 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4290 | channel_htx_truncate(res, htx); |
Christopher Faulet | fefc73d | 2018-10-24 21:18:04 +0200 | [diff] [blame] | 4291 | } |
| 4292 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4293 | /* This function terminates the request because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4294 | * because an error was triggered during the body forwarding. |
| 4295 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4296 | static void http_end_request(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4297 | { |
| 4298 | struct channel *chn = &s->req; |
| 4299 | struct http_txn *txn = s->txn; |
| 4300 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4301 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4302 | |
Christopher Faulet | b42a8b6 | 2018-11-19 21:59:00 +0100 | [diff] [blame] | 4303 | if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR || |
| 4304 | txn->rsp.msg_state == HTTP_MSG_ERROR)) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4305 | channel_abort(chn); |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4306 | channel_htx_truncate(chn, htxbuf(&chn->buf)); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4307 | goto end; |
| 4308 | } |
| 4309 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4310 | if (unlikely(txn->req.msg_state < HTTP_MSG_DONE)) { |
| 4311 | DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4312 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4313 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4314 | |
| 4315 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4316 | /* No need to read anymore, the request was completely parsed. |
| 4317 | * We can shut the read side unless we want to abort_on_close, |
| 4318 | * or we have a POST request. The issue with POST requests is |
| 4319 | * that some browsers still send a CRLF after the request, and |
| 4320 | * this CRLF must be read so that it does not remain in the kernel |
| 4321 | * buffers, otherwise a close could cause an RST on some systems |
| 4322 | * (eg: Linux). |
| 4323 | */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 4324 | if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4325 | channel_dont_read(chn); |
| 4326 | |
| 4327 | /* if the server closes the connection, we want to immediately react |
| 4328 | * and close the socket to save packets and syscalls. |
| 4329 | */ |
| 4330 | s->si[1].flags |= SI_FL_NOHALF; |
| 4331 | |
| 4332 | /* In any case we've finished parsing the request so we must |
| 4333 | * disable Nagle when sending data because 1) we're not going |
| 4334 | * to shut this side, and 2) the server is waiting for us to |
| 4335 | * send pending data. |
| 4336 | */ |
| 4337 | chn->flags |= CF_NEVER_WAIT; |
| 4338 | |
Christopher Faulet | d01ce40 | 2019-01-02 17:44:13 +0100 | [diff] [blame] | 4339 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4340 | /* The server has not finished to respond, so we |
| 4341 | * don't want to move in order not to upset it. |
| 4342 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4343 | DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | d01ce40 | 2019-01-02 17:44:13 +0100 | [diff] [blame] | 4344 | return; |
| 4345 | } |
| 4346 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4347 | /* When we get here, it means that both the request and the |
| 4348 | * response have finished receiving. Depending on the connection |
| 4349 | * mode, we'll have to wait for the last bytes to leave in either |
| 4350 | * direction, and sometimes for a close to be effective. |
| 4351 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4352 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4353 | /* Tunnel mode will not have any analyser so it needs to |
| 4354 | * poll for reads. |
| 4355 | */ |
| 4356 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4357 | if (b_data(&chn->buf)) { |
| 4358 | DBG_TRACE_DEVEL("waiting to flush the request", STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4359 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4360 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4361 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 4362 | } |
| 4363 | else { |
| 4364 | /* we're not expecting any new data to come for this |
| 4365 | * transaction, so we can close it. |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4366 | * |
| 4367 | * However, there is an exception if the response |
| 4368 | * length is undefined. In this case, we need to wait |
| 4369 | * the close from the server. The response will be |
| 4370 | * switched in TUNNEL mode until the end. |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4371 | */ |
| 4372 | if (!(txn->rsp.flags & HTTP_MSGF_XFER_LEN) && |
| 4373 | txn->rsp.msg_state != HTTP_MSG_CLOSED) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4374 | goto check_channel_flags; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4375 | |
| 4376 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4377 | channel_shutr_now(chn); |
| 4378 | channel_shutw_now(chn); |
| 4379 | } |
| 4380 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4381 | goto check_channel_flags; |
| 4382 | } |
| 4383 | |
| 4384 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4385 | http_msg_closing: |
| 4386 | /* nothing else to forward, just waiting for the output buffer |
| 4387 | * to be empty and for the shutw_now to take effect. |
| 4388 | */ |
| 4389 | if (channel_is_empty(chn)) { |
| 4390 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4391 | goto http_msg_closed; |
| 4392 | } |
| 4393 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4394 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4395 | goto end; |
| 4396 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4397 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4398 | return; |
| 4399 | } |
| 4400 | |
| 4401 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4402 | http_msg_closed: |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4403 | /* if we don't know whether the server will close, we need to hard close */ |
| 4404 | if (txn->rsp.flags & HTTP_MSGF_XFER_LEN) |
| 4405 | s->si[1].flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4406 | /* see above in MSG_DONE why we only do this in these states */ |
Christopher Faulet | 769d0e9 | 2019-03-22 14:23:18 +0100 | [diff] [blame] | 4407 | if (!(s->be->options & PR_O_ABRT_CLOSE)) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4408 | channel_dont_read(chn); |
| 4409 | goto end; |
| 4410 | } |
| 4411 | |
| 4412 | check_channel_flags: |
| 4413 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4414 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4415 | /* if we've just closed an output, let's switch */ |
| 4416 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4417 | goto http_msg_closing; |
| 4418 | } |
| 4419 | |
| 4420 | end: |
| 4421 | chn->analysers &= AN_REQ_FLT_END; |
| 4422 | if (txn->req.msg_state == HTTP_MSG_TUNNEL && HAS_REQ_DATA_FILTERS(s)) |
| 4423 | chn->analysers |= AN_REQ_FLT_XFER_DATA; |
| 4424 | channel_auto_close(chn); |
| 4425 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4426 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4427 | } |
| 4428 | |
| 4429 | |
Ilya Shipitsin | 6fb0f21 | 2020-04-02 15:25:26 +0500 | [diff] [blame] | 4430 | /* This function terminates the response because it was completely analyzed or |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4431 | * because an error was triggered during the body forwarding. |
| 4432 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4433 | static void http_end_response(struct stream *s) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4434 | { |
| 4435 | struct channel *chn = &s->res; |
| 4436 | struct http_txn *txn = s->txn; |
| 4437 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4438 | DBG_TRACE_ENTER(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4439 | |
Christopher Faulet | b42a8b6 | 2018-11-19 21:59:00 +0100 | [diff] [blame] | 4440 | if (unlikely(txn->req.msg_state == HTTP_MSG_ERROR || |
| 4441 | txn->rsp.msg_state == HTTP_MSG_ERROR)) { |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4442 | channel_htx_truncate(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4443 | channel_abort(&s->req); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4444 | goto end; |
| 4445 | } |
| 4446 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4447 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_DONE)) { |
| 4448 | DBG_TRACE_DEVEL("waiting end of the response", STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4449 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4450 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4451 | |
| 4452 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4453 | /* In theory, we don't need to read anymore, but we must |
| 4454 | * still monitor the server connection for a possible close |
| 4455 | * while the request is being uploaded, so we don't disable |
| 4456 | * reading. |
| 4457 | */ |
| 4458 | /* channel_dont_read(chn); */ |
| 4459 | |
| 4460 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4461 | /* The client seems to still be sending data, probably |
| 4462 | * because we got an error response during an upload. |
| 4463 | * We have the choice of either breaking the connection |
| 4464 | * or letting it pass through. Let's do the later. |
| 4465 | */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4466 | DBG_TRACE_DEVEL("waiting end of the request", STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4467 | return; |
| 4468 | } |
| 4469 | |
| 4470 | /* When we get here, it means that both the request and the |
| 4471 | * response have finished receiving. Depending on the connection |
| 4472 | * mode, we'll have to wait for the last bytes to leave in either |
| 4473 | * direction, and sometimes for a close to be effective. |
| 4474 | */ |
Christopher Faulet | c41547b | 2019-07-16 14:32:23 +0200 | [diff] [blame] | 4475 | if (txn->flags & TX_CON_WANT_TUN) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4476 | channel_auto_read(chn); |
| 4477 | chn->flags |= CF_NEVER_WAIT; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4478 | if (b_data(&chn->buf)) { |
| 4479 | DBG_TRACE_DEVEL("waiting to flush the respone", STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4480 | return; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4481 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4482 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4483 | } |
| 4484 | else { |
| 4485 | /* we're not expecting any new data to come for this |
| 4486 | * transaction, so we can close it. |
| 4487 | */ |
| 4488 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4489 | channel_shutr_now(chn); |
| 4490 | channel_shutw_now(chn); |
| 4491 | } |
| 4492 | } |
| 4493 | goto check_channel_flags; |
| 4494 | } |
| 4495 | |
| 4496 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4497 | http_msg_closing: |
| 4498 | /* nothing else to forward, just waiting for the output buffer |
| 4499 | * to be empty and for the shutw_now to take effect. |
| 4500 | */ |
| 4501 | if (channel_is_empty(chn)) { |
| 4502 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4503 | goto http_msg_closed; |
| 4504 | } |
| 4505 | else if (chn->flags & CF_SHUTW) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4506 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 4507 | _HA_ATOMIC_ADD(&strm_sess(s)->fe->fe_counters.cli_aborts, 1); |
Olivier Houchard | a798bf5 | 2019-03-08 18:52:00 +0100 | [diff] [blame] | 4508 | _HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1); |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 4509 | if (strm_sess(s)->listener->counters) |
| 4510 | _HA_ATOMIC_ADD(&strm_sess(s)->listener->counters->cli_aborts, 1); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4511 | if (objt_server(s->target)) |
Christopher Faulet | cff0f73 | 2019-12-16 16:13:44 +0100 | [diff] [blame] | 4512 | _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.cli_aborts, 1); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4513 | goto end; |
| 4514 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4515 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4516 | return; |
| 4517 | } |
| 4518 | |
| 4519 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4520 | http_msg_closed: |
| 4521 | /* drop any pending data */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4522 | channel_htx_truncate(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 4523 | channel_abort(&s->req); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4524 | goto end; |
| 4525 | } |
| 4526 | |
| 4527 | check_channel_flags: |
| 4528 | /* Here, we are in HTTP_MSG_DONE or HTTP_MSG_TUNNEL */ |
| 4529 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
| 4530 | /* if we've just closed an output, let's switch */ |
| 4531 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4532 | goto http_msg_closing; |
| 4533 | } |
| 4534 | |
| 4535 | end: |
| 4536 | chn->analysers &= AN_RES_FLT_END; |
| 4537 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL && HAS_RSP_DATA_FILTERS(s)) |
| 4538 | chn->analysers |= AN_RES_FLT_XFER_DATA; |
| 4539 | channel_auto_close(chn); |
| 4540 | channel_auto_read(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 4541 | DBG_TRACE_LEAVE(STRM_EV_HTTP_ANA, s, txn); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 4542 | } |
| 4543 | |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4544 | /* Forward a response generated by HAProxy (error/redirect/return). This |
| 4545 | * function forwards all pending incoming data. If <final> is set to 0, nothing |
| 4546 | * more is performed. It is used for 1xx informational messages. Otherwise, the |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4547 | * transaction is terminated and the request is emptied. On success 1 is |
| 4548 | * returned. If an error occurred, 0 is returned. |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4549 | */ |
| 4550 | int http_forward_proxy_resp(struct stream *s, int final) |
| 4551 | { |
| 4552 | struct channel *req = &s->req; |
| 4553 | struct channel *res = &s->res; |
| 4554 | struct htx *htx = htxbuf(&res->buf); |
| 4555 | size_t data; |
| 4556 | |
| 4557 | if (final) { |
| 4558 | htx->flags |= HTX_FL_PROXY_RESP; |
Christopher Faulet | 507479b | 2020-05-15 12:29:46 +0200 | [diff] [blame] | 4559 | |
| 4560 | if (!http_eval_after_res_rules(s)) |
Christopher Faulet | 6d0c3df | 2020-01-22 09:26:35 +0100 | [diff] [blame] | 4561 | return 0; |
Christopher Faulet | ef70e25 | 2020-01-28 09:26:19 +0100 | [diff] [blame] | 4562 | |
| 4563 | channel_auto_read(req); |
| 4564 | channel_abort(req); |
| 4565 | channel_auto_close(req); |
| 4566 | channel_htx_erase(req, htxbuf(&req->buf)); |
| 4567 | |
| 4568 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 4569 | channel_auto_read(res); |
| 4570 | channel_auto_close(res); |
| 4571 | channel_shutr_now(res); |
| 4572 | } |
| 4573 | |
| 4574 | data = htx->data - co_data(res); |
| 4575 | c_adv(res, data); |
| 4576 | htx->first = -1; |
| 4577 | res->total += data; |
| 4578 | return 1; |
| 4579 | } |
| 4580 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4581 | void http_server_error(struct stream *s, struct stream_interface *si, int err, |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4582 | int finst, struct http_reply *msg) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4583 | { |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4584 | http_reply_and_close(s, s->txn->status, msg); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4585 | if (!(s->flags & SF_ERR_MASK)) |
| 4586 | s->flags |= err; |
| 4587 | if (!(s->flags & SF_FINST_MASK)) |
| 4588 | s->flags |= finst; |
| 4589 | } |
| 4590 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4591 | void http_reply_and_close(struct stream *s, short status, struct http_reply *msg) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4592 | { |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4593 | if (!msg) { |
| 4594 | channel_htx_truncate(&s->res, htxbuf(&s->res.buf)); |
| 4595 | goto end; |
| 4596 | } |
| 4597 | |
| 4598 | if (http_reply_message(s, msg) == -1) { |
| 4599 | /* On error, return a 500 error message, but don't rewrite it if |
| 4600 | * it is already an internal error. |
| 4601 | */ |
| 4602 | if (s->txn->status == 500) |
| 4603 | s->txn->flags |= TX_CONST_REPLY; |
| 4604 | s->txn->status = 500; |
| 4605 | s->txn->http_reply = NULL; |
| 4606 | return http_reply_and_close(s, s->txn->status, http_error_message(s)); |
| 4607 | } |
| 4608 | |
| 4609 | end: |
| 4610 | s->res.wex = tick_add_ifset(now_ms, s->res.wto); |
| 4611 | s->txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 4612 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4613 | channel_auto_read(&s->req); |
| 4614 | channel_abort(&s->req); |
| 4615 | channel_auto_close(&s->req); |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4616 | channel_htx_erase(&s->req, htxbuf(&s->req.buf)); |
Christopher Faulet | 72c7d8d | 2020-01-27 15:32:25 +0100 | [diff] [blame] | 4617 | channel_auto_read(&s->res); |
| 4618 | channel_auto_close(&s->res); |
| 4619 | channel_shutr_now(&s->res); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4620 | } |
| 4621 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4622 | struct http_reply *http_error_message(struct stream *s) |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4623 | { |
| 4624 | const int msgnum = http_get_status_idx(s->txn->status); |
| 4625 | |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4626 | if (s->txn->http_reply) |
| 4627 | return s->txn->http_reply; |
| 4628 | else if (s->be->replies[msgnum]) |
| 4629 | return s->be->replies[msgnum]; |
| 4630 | else if (strm_fe(s)->replies[msgnum]) |
| 4631 | return strm_fe(s)->replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4632 | else |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4633 | return &http_err_replies[msgnum]; |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4634 | } |
| 4635 | |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4636 | /* Produces an HTX message from an http reply. Depending on the http reply type, a, |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4637 | * errorfile, an raw file or a log-format string is used. On success, it returns |
| 4638 | * 0. If an error occurs -1 is returned. |
| 4639 | */ |
Christopher Faulet | ae43b6c | 2020-05-27 15:24:22 +0200 | [diff] [blame] | 4640 | int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply) |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4641 | { |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4642 | struct buffer *errmsg; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4643 | struct htx_sl *sl; |
| 4644 | struct buffer *body = NULL; |
| 4645 | const char *status, *reason, *clen, *ctype; |
| 4646 | unsigned int slflags; |
| 4647 | int ret = 0; |
| 4648 | |
Christopher Faulet | e29a97e | 2020-05-14 14:49:25 +0200 | [diff] [blame] | 4649 | /* |
| 4650 | * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so |
| 4651 | * |
| 4652 | * - HTTP_REPLY_INDIRECT: switch on another reply if defined or handled |
| 4653 | * as no payload if NULL. the TXN status code is set with the status |
| 4654 | * of the original reply. |
| 4655 | */ |
| 4656 | |
| 4657 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4658 | if (reply->body.reply) |
| 4659 | reply = reply->body.reply; |
| 4660 | } |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4661 | if (reply->type == HTTP_REPLY_ERRMSG && !reply->body.errmsg) { |
| 4662 | /* get default error message */ |
| 4663 | if (reply == s->txn->http_reply) |
| 4664 | s->txn->http_reply = NULL; |
| 4665 | reply = http_error_message(s); |
| 4666 | if (reply->type == HTTP_REPLY_INDIRECT) { |
| 4667 | if (reply->body.reply) |
| 4668 | reply = reply->body.reply; |
| 4669 | } |
| 4670 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4671 | |
| 4672 | if (reply->type == HTTP_REPLY_ERRMSG) { |
| 4673 | /* implicit or explicit error message*/ |
| 4674 | errmsg = reply->body.errmsg; |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4675 | if (errmsg && !b_is_null(errmsg)) { |
Christopher Faulet | 2056736 | 2020-05-15 14:52:49 +0200 | [diff] [blame] | 4676 | if (!htx_copy_msg(htx, errmsg)) |
Christopher Faulet | 8dfeccf | 2020-05-15 14:16:29 +0200 | [diff] [blame] | 4677 | goto fail; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4678 | } |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4679 | } |
| 4680 | else { |
| 4681 | /* no payload, file or log-format string */ |
| 4682 | if (reply->type == HTTP_REPLY_RAW) { |
| 4683 | /* file */ |
| 4684 | body = &reply->body.obj; |
| 4685 | } |
| 4686 | else if (reply->type == HTTP_REPLY_LOGFMT) { |
| 4687 | /* log-format string */ |
| 4688 | body = alloc_trash_chunk(); |
| 4689 | if (!body) |
| 4690 | goto fail_alloc; |
| 4691 | body->data = build_logline(s, body->area, body->size, &reply->body.fmt); |
| 4692 | } |
| 4693 | /* else no payload */ |
| 4694 | |
| 4695 | status = ultoa(reply->status); |
| 4696 | reason = http_get_reason(reply->status); |
| 4697 | slflags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN|HTX_SL_F_CLEN); |
| 4698 | if (!body || !b_data(body)) |
| 4699 | slflags |= HTX_SL_F_BODYLESS; |
| 4700 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, slflags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4701 | if (!sl) |
| 4702 | goto fail; |
| 4703 | sl->info.res.status = reply->status; |
| 4704 | |
| 4705 | clen = (body ? ultoa(b_data(body)) : "0"); |
| 4706 | ctype = reply->ctype; |
| 4707 | |
| 4708 | if (!LIST_ISEMPTY(&reply->hdrs)) { |
| 4709 | struct http_reply_hdr *hdr; |
| 4710 | struct buffer *value = alloc_trash_chunk(); |
| 4711 | |
| 4712 | if (!value) |
| 4713 | goto fail; |
| 4714 | |
| 4715 | list_for_each_entry(hdr, &reply->hdrs, list) { |
| 4716 | chunk_reset(value); |
| 4717 | value->data = build_logline(s, value->area, value->size, &hdr->value); |
| 4718 | if (b_data(value) && !htx_add_header(htx, hdr->name, ist2(b_head(value), b_data(value)))) { |
| 4719 | free_trash_chunk(value); |
| 4720 | goto fail; |
| 4721 | } |
| 4722 | chunk_reset(value); |
| 4723 | } |
| 4724 | free_trash_chunk(value); |
| 4725 | } |
| 4726 | |
| 4727 | if (!htx_add_header(htx, ist("content-length"), ist(clen)) || |
| 4728 | (body && b_data(body) && ctype && !htx_add_header(htx, ist("content-type"), ist(ctype))) || |
| 4729 | !htx_add_endof(htx, HTX_BLK_EOH) || |
| 4730 | (body && b_data(body) && !htx_add_data_atonce(htx, ist2(b_head(body), b_data(body)))) || |
| 4731 | !htx_add_endof(htx, HTX_BLK_EOM)) |
| 4732 | goto fail; |
| 4733 | } |
| 4734 | |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4735 | leave: |
| 4736 | if (reply->type == HTTP_REPLY_LOGFMT) |
| 4737 | free_trash_chunk(body); |
| 4738 | return ret; |
| 4739 | |
| 4740 | fail_alloc: |
| 4741 | if (!(s->flags & SF_ERR_MASK)) |
| 4742 | s->flags |= SF_ERR_RESOURCE; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4743 | /* fall through */ |
| 4744 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4745 | ret = -1; |
| 4746 | goto leave; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4747 | } |
| 4748 | |
| 4749 | /* Send an http reply to the client. On success, it returns 0. If an error |
| 4750 | * occurs -1 is returned. |
| 4751 | */ |
| 4752 | int http_reply_message(struct stream *s, struct http_reply *reply) |
| 4753 | { |
| 4754 | struct channel *res = &s->res; |
| 4755 | struct htx *htx = htx_from_buf(&res->buf); |
| 4756 | |
| 4757 | if (s->txn->status == -1) |
| 4758 | s->txn->status = reply->status; |
| 4759 | channel_htx_truncate(res, htx); |
| 4760 | |
| 4761 | if (http_reply_to_htx(s, htx, reply) == -1) |
| 4762 | goto fail; |
| 4763 | |
| 4764 | htx_to_buf(htx, &s->res.buf); |
| 4765 | if (!http_forward_proxy_resp(s, 1)) |
| 4766 | goto fail; |
| 4767 | return 0; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4768 | |
| 4769 | fail: |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4770 | channel_htx_truncate(res, htx); |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4771 | if (!(s->flags & SF_ERR_MASK)) |
| 4772 | s->flags |= SF_ERR_PRXCOND; |
Christopher Faulet | 97e466c | 2020-05-15 15:12:47 +0200 | [diff] [blame] | 4773 | return -1; |
Christopher Faulet | 0e2ad61 | 2020-05-13 16:38:37 +0200 | [diff] [blame] | 4774 | } |
| 4775 | |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4776 | /* Return the error message corresponding to si->err_type. It is assumed |
| 4777 | * that the server side is closed. Note that err_type is actually a |
| 4778 | * bitmask, where almost only aborts may be cumulated with other |
| 4779 | * values. We consider that aborted operations are more important |
| 4780 | * than timeouts or errors due to the fact that nobody else in the |
| 4781 | * logs might explain incomplete retries. All others should avoid |
| 4782 | * being cumulated. It should normally not be possible to have multiple |
| 4783 | * aborts at once, but just in case, the first one in sequence is reported. |
| 4784 | * Note that connection errors appearing on the second request of a keep-alive |
| 4785 | * connection are not reported since this allows the client to retry. |
| 4786 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4787 | void http_return_srv_error(struct stream *s, struct stream_interface *si) |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4788 | { |
| 4789 | int err_type = si->err_type; |
| 4790 | |
| 4791 | /* set s->txn->status for http_error_message(s) */ |
| 4792 | s->txn->status = 503; |
| 4793 | |
| 4794 | if (err_type & SI_ET_QUEUE_ABRT) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4795 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_Q, |
| 4796 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4797 | else if (err_type & SI_ET_CONN_ABRT) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4798 | http_server_error(s, si, SF_ERR_CLICL, SF_FINST_C, |
| 4799 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4800 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4801 | else if (err_type & SI_ET_QUEUE_TO) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4802 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_Q, |
| 4803 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4804 | else if (err_type & SI_ET_QUEUE_ERR) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4805 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_Q, |
| 4806 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4807 | else if (err_type & SI_ET_CONN_TO) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4808 | http_server_error(s, si, SF_ERR_SRVTO, SF_FINST_C, |
| 4809 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4810 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4811 | else if (err_type & SI_ET_CONN_ERR) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4812 | http_server_error(s, si, SF_ERR_SRVCL, SF_FINST_C, |
| 4813 | (s->flags & SF_SRV_REUSED) ? NULL : |
| 4814 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4815 | else if (err_type & SI_ET_CONN_RES) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4816 | http_server_error(s, si, SF_ERR_RESOURCE, SF_FINST_C, |
| 4817 | (s->txn->flags & TX_NOT_FIRST) ? NULL : |
| 4818 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4819 | else { /* SI_ET_CONN_OTHER and others */ |
| 4820 | s->txn->status = 500; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4821 | http_server_error(s, si, SF_ERR_INTERNAL, SF_FINST_C, |
| 4822 | http_error_message(s)); |
Christopher Faulet | 304cc40 | 2019-07-15 15:46:28 +0200 | [diff] [blame] | 4823 | } |
| 4824 | } |
| 4825 | |
Christopher Faulet | a7b677c | 2018-11-29 16:48:49 +0100 | [diff] [blame] | 4826 | |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4827 | /* Handle Expect: 100-continue for HTTP/1.1 messages if necessary. It returns 0 |
| 4828 | * on success and -1 on error. |
| 4829 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4830 | static int http_handle_expect_hdr(struct stream *s, struct htx *htx, struct http_msg *msg) |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4831 | { |
| 4832 | /* If we have HTTP/1.1 message with a body and Expect: 100-continue, |
| 4833 | * then we must send an HTTP/1.1 100 Continue intermediate response. |
| 4834 | */ |
| 4835 | if (msg->msg_state == HTTP_MSG_BODY && (msg->flags & HTTP_MSGF_VER_11) && |
| 4836 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
| 4837 | struct ist hdr = { .ptr = "Expect", .len = 6 }; |
| 4838 | struct http_hdr_ctx ctx; |
| 4839 | |
| 4840 | ctx.blk = NULL; |
| 4841 | /* Expect is allowed in 1.1, look for it */ |
| 4842 | if (http_find_header(htx, hdr, &ctx, 0) && |
| 4843 | unlikely(isteqi(ctx.value, ist2("100-continue", 12)))) { |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4844 | if (http_reply_100_continue(s) == -1) |
Christopher Faulet | 4a28a53 | 2019-03-01 11:19:40 +0100 | [diff] [blame] | 4845 | return -1; |
| 4846 | http_remove_header(htx, &ctx); |
| 4847 | } |
| 4848 | } |
| 4849 | return 0; |
| 4850 | } |
| 4851 | |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4852 | /* Send a 100-Continue response to the client. It returns 0 on success and -1 |
| 4853 | * on error. The response channel is updated accordingly. |
| 4854 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4855 | static int http_reply_100_continue(struct stream *s) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4856 | { |
| 4857 | struct channel *res = &s->res; |
| 4858 | struct htx *htx = htx_from_buf(&res->buf); |
| 4859 | struct htx_sl *sl; |
| 4860 | unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11| |
| 4861 | HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4862 | |
| 4863 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4864 | ist("HTTP/1.1"), ist("100"), ist("Continue")); |
| 4865 | if (!sl) |
| 4866 | goto fail; |
| 4867 | sl->info.res.status = 100; |
| 4868 | |
Christopher Faulet | 1d5ec09 | 2019-06-26 14:23:54 +0200 | [diff] [blame] | 4869 | if (!htx_add_endof(htx, HTX_BLK_EOH)) |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4870 | goto fail; |
| 4871 | |
Christopher Faulet | a72a7e4 | 2020-01-28 09:28:11 +0100 | [diff] [blame] | 4872 | if (!http_forward_proxy_resp(s, 0)) |
| 4873 | goto fail; |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4874 | return 0; |
| 4875 | |
| 4876 | fail: |
| 4877 | /* If an error occurred, remove the incomplete HTTP response from the |
| 4878 | * buffer */ |
Christopher Faulet | 202c6ce | 2019-01-07 14:57:35 +0100 | [diff] [blame] | 4879 | channel_htx_truncate(res, htx); |
Christopher Faulet | 23a3c79 | 2018-11-28 10:01:23 +0100 | [diff] [blame] | 4880 | return -1; |
| 4881 | } |
| 4882 | |
Christopher Faulet | 12c51e2 | 2018-11-28 15:59:42 +0100 | [diff] [blame] | 4883 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4884 | /* |
| 4885 | * Capture headers from message <htx> according to header list <cap_hdr>, and |
| 4886 | * fill the <cap> pointers appropriately. |
| 4887 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4888 | static void http_capture_headers(struct htx *htx, char **cap, struct cap_hdr *cap_hdr) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4889 | { |
| 4890 | struct cap_hdr *h; |
| 4891 | int32_t pos; |
| 4892 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4893 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4894 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4895 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4896 | struct ist n, v; |
| 4897 | |
| 4898 | if (type == HTX_BLK_EOH) |
| 4899 | break; |
| 4900 | if (type != HTX_BLK_HDR) |
| 4901 | continue; |
| 4902 | |
| 4903 | n = htx_get_blk_name(htx, blk); |
| 4904 | |
| 4905 | for (h = cap_hdr; h; h = h->next) { |
| 4906 | if (h->namelen && (h->namelen == n.len) && |
| 4907 | (strncasecmp(n.ptr, h->name, h->namelen) == 0)) { |
| 4908 | if (cap[h->index] == NULL) |
| 4909 | cap[h->index] = |
| 4910 | pool_alloc(h->pool); |
| 4911 | |
| 4912 | if (cap[h->index] == NULL) { |
| 4913 | ha_alert("HTTP capture : out of memory.\n"); |
| 4914 | break; |
| 4915 | } |
| 4916 | |
| 4917 | v = htx_get_blk_value(htx, blk); |
| 4918 | if (v.len > h->len) |
| 4919 | v.len = h->len; |
| 4920 | |
| 4921 | memcpy(cap[h->index], v.ptr, v.len); |
| 4922 | cap[h->index][v.len]=0; |
| 4923 | } |
| 4924 | } |
| 4925 | } |
| 4926 | } |
| 4927 | |
Christopher Faulet | 0b6bdc5 | 2018-10-24 11:05:36 +0200 | [diff] [blame] | 4928 | /* Delete a value in a header between delimiters <from> and <next>. The header |
| 4929 | * itself is delimited by <start> and <end> pointers. The number of characters |
| 4930 | * displaced is returned, and the pointer to the first delimiter is updated if |
| 4931 | * required. The function tries as much as possible to respect the following |
| 4932 | * principles : |
| 4933 | * - replace <from> delimiter by the <next> one unless <from> points to <start>, |
| 4934 | * in which case <next> is simply removed |
| 4935 | * - set exactly one space character after the new first delimiter, unless there |
| 4936 | * are not enough characters in the block being moved to do so. |
| 4937 | * - remove unneeded spaces before the previous delimiter and after the new |
| 4938 | * one. |
| 4939 | * |
| 4940 | * It is the caller's responsibility to ensure that : |
| 4941 | * - <from> points to a valid delimiter or <start> ; |
| 4942 | * - <next> points to a valid delimiter or <end> ; |
| 4943 | * - there are non-space chars before <from>. |
| 4944 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4945 | static int http_del_hdr_value(char *start, char *end, char **from, char *next) |
Christopher Faulet | 0b6bdc5 | 2018-10-24 11:05:36 +0200 | [diff] [blame] | 4946 | { |
| 4947 | char *prev = *from; |
| 4948 | |
| 4949 | if (prev == start) { |
| 4950 | /* We're removing the first value. eat the semicolon, if <next> |
| 4951 | * is lower than <end> */ |
| 4952 | if (next < end) |
| 4953 | next++; |
| 4954 | |
| 4955 | while (next < end && HTTP_IS_SPHT(*next)) |
| 4956 | next++; |
| 4957 | } |
| 4958 | else { |
| 4959 | /* Remove useless spaces before the old delimiter. */ |
| 4960 | while (HTTP_IS_SPHT(*(prev-1))) |
| 4961 | prev--; |
| 4962 | *from = prev; |
| 4963 | |
| 4964 | /* copy the delimiter and if possible a space if we're |
| 4965 | * not at the end of the line. |
| 4966 | */ |
| 4967 | if (next < end) { |
| 4968 | *prev++ = *next++; |
| 4969 | if (prev + 1 < next) |
| 4970 | *prev++ = ' '; |
| 4971 | while (next < end && HTTP_IS_SPHT(*next)) |
| 4972 | next++; |
| 4973 | } |
| 4974 | } |
| 4975 | memmove(prev, next, end - next); |
| 4976 | return (prev - next); |
| 4977 | } |
| 4978 | |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4979 | |
| 4980 | /* Formats the start line of the request (without CRLF) and puts it in <str> and |
Joseph Herlant | c42c0e9 | 2018-11-25 10:43:27 -0800 | [diff] [blame] | 4981 | * return the written length. The line can be truncated if it exceeds <len>. |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4982 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4983 | static size_t http_fmt_req_line(const struct htx_sl *sl, char *str, size_t len) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4984 | { |
| 4985 | struct ist dst = ist2(str, 0); |
| 4986 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4987 | if (istcat(&dst, htx_sl_req_meth(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4988 | goto end; |
| 4989 | if (dst.len + 1 > len) |
| 4990 | goto end; |
| 4991 | dst.ptr[dst.len++] = ' '; |
| 4992 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4993 | if (istcat(&dst, htx_sl_req_uri(sl), len) == -1) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 4994 | goto end; |
| 4995 | if (dst.len + 1 > len) |
| 4996 | goto end; |
| 4997 | dst.ptr[dst.len++] = ' '; |
| 4998 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 4999 | istcat(&dst, htx_sl_req_vsn(sl), len); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5000 | end: |
| 5001 | return dst.len; |
| 5002 | } |
| 5003 | |
| 5004 | /* |
| 5005 | * Print a debug line with a start line. |
| 5006 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5007 | static void http_debug_stline(const char *dir, struct stream *s, const struct htx_sl *sl) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5008 | { |
| 5009 | struct session *sess = strm_sess(s); |
| 5010 | int max; |
| 5011 | |
| 5012 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 5013 | dir, |
| 5014 | objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1, |
| 5015 | objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1); |
| 5016 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5017 | max = HTX_SL_P1_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5018 | UBOUND(max, trash.size - trash.data - 3); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5019 | chunk_memcat(&trash, HTX_SL_P1_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5020 | trash.area[trash.data++] = ' '; |
| 5021 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5022 | max = HTX_SL_P2_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5023 | UBOUND(max, trash.size - trash.data - 2); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5024 | chunk_memcat(&trash, HTX_SL_P2_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5025 | trash.area[trash.data++] = ' '; |
| 5026 | |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5027 | max = HTX_SL_P3_LEN(sl); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5028 | UBOUND(max, trash.size - trash.data - 1); |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 5029 | chunk_memcat(&trash, HTX_SL_P3_PTR(sl), max); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5030 | trash.area[trash.data++] = '\n'; |
| 5031 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 5032 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5033 | } |
| 5034 | |
| 5035 | /* |
| 5036 | * Print a debug line with a header. |
| 5037 | */ |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5038 | static void http_debug_hdr(const char *dir, struct stream *s, const struct ist n, const struct ist v) |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5039 | { |
| 5040 | struct session *sess = strm_sess(s); |
| 5041 | int max; |
| 5042 | |
| 5043 | chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id, |
| 5044 | dir, |
| 5045 | objt_conn(sess->origin) ? (unsigned short)objt_conn(sess->origin)->handle.fd : -1, |
| 5046 | objt_cs(s->si[1].end) ? (unsigned short)objt_cs(s->si[1].end)->conn->handle.fd : -1); |
| 5047 | |
| 5048 | max = n.len; |
| 5049 | UBOUND(max, trash.size - trash.data - 3); |
| 5050 | chunk_memcat(&trash, n.ptr, max); |
| 5051 | trash.area[trash.data++] = ':'; |
| 5052 | trash.area[trash.data++] = ' '; |
| 5053 | |
| 5054 | max = v.len; |
| 5055 | UBOUND(max, trash.size - trash.data - 1); |
| 5056 | chunk_memcat(&trash, v.ptr, max); |
| 5057 | trash.area[trash.data++] = '\n'; |
| 5058 | |
Willy Tarreau | 2e8ab6b | 2020-03-14 11:03:20 +0100 | [diff] [blame] | 5059 | DISGUISE(write(1, trash.area, trash.data)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5060 | } |
| 5061 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5062 | /* Allocate a new HTTP transaction for stream <s> unless there is one already. |
| 5063 | * In case of allocation failure, everything allocated is freed and NULL is |
| 5064 | * returned. Otherwise the new transaction is assigned to the stream and |
| 5065 | * returned. |
| 5066 | */ |
| 5067 | struct http_txn *http_alloc_txn(struct stream *s) |
| 5068 | { |
| 5069 | struct http_txn *txn = s->txn; |
| 5070 | |
| 5071 | if (txn) |
| 5072 | return txn; |
| 5073 | |
| 5074 | txn = pool_alloc(pool_head_http_txn); |
| 5075 | if (!txn) |
| 5076 | return txn; |
| 5077 | |
| 5078 | s->txn = txn; |
| 5079 | return txn; |
| 5080 | } |
| 5081 | |
| 5082 | void http_txn_reset_req(struct http_txn *txn) |
| 5083 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 5084 | txn->req.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5085 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 5086 | } |
| 5087 | |
| 5088 | void http_txn_reset_res(struct http_txn *txn) |
| 5089 | { |
Christopher Faulet | 1aea50e | 2020-01-17 16:03:53 +0100 | [diff] [blame] | 5090 | txn->rsp.flags = 0; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5091 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 5092 | } |
| 5093 | |
| 5094 | /* |
| 5095 | * Initialize a new HTTP transaction for stream <s>. It is assumed that all |
| 5096 | * the required fields are properly allocated and that we only need to (re)init |
| 5097 | * them. This should be used before processing any new request. |
| 5098 | */ |
| 5099 | void http_init_txn(struct stream *s) |
| 5100 | { |
| 5101 | struct http_txn *txn = s->txn; |
| 5102 | struct conn_stream *cs = objt_cs(s->si[0].end); |
| 5103 | |
| 5104 | txn->flags = ((cs && cs->flags & CS_FL_NOT_FIRST) |
| 5105 | ? (TX_NOT_FIRST|TX_WAIT_NEXT_RQ) |
| 5106 | : 0); |
| 5107 | txn->status = -1; |
Christopher Faulet | 5cb513a | 2020-05-13 17:56:56 +0200 | [diff] [blame] | 5108 | txn->http_reply = NULL; |
Willy Tarreau | 8b50758 | 2020-02-25 09:35:07 +0100 | [diff] [blame] | 5109 | write_u32(txn->cache_hash, 0); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5110 | |
| 5111 | txn->cookie_first_date = 0; |
| 5112 | txn->cookie_last_date = 0; |
| 5113 | |
| 5114 | txn->srv_cookie = NULL; |
| 5115 | txn->cli_cookie = NULL; |
| 5116 | txn->uri = NULL; |
| 5117 | |
| 5118 | http_txn_reset_req(txn); |
| 5119 | http_txn_reset_res(txn); |
| 5120 | |
| 5121 | txn->req.chn = &s->req; |
| 5122 | txn->rsp.chn = &s->res; |
| 5123 | |
| 5124 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
| 5125 | |
| 5126 | vars_init(&s->vars_txn, SCOPE_TXN); |
| 5127 | vars_init(&s->vars_reqres, SCOPE_REQ); |
| 5128 | } |
| 5129 | |
| 5130 | /* to be used at the end of a transaction */ |
| 5131 | void http_end_txn(struct stream *s) |
| 5132 | { |
| 5133 | struct http_txn *txn = s->txn; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5134 | |
| 5135 | /* these ones will have been dynamically allocated */ |
| 5136 | pool_free(pool_head_requri, txn->uri); |
| 5137 | pool_free(pool_head_capture, txn->cli_cookie); |
| 5138 | pool_free(pool_head_capture, txn->srv_cookie); |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5139 | pool_free(pool_head_uniqueid, s->unique_id.ptr); |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5140 | |
Tim Duesterhus | a17e662 | 2020-03-05 20:19:02 +0100 | [diff] [blame] | 5141 | s->unique_id = IST_NULL; |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5142 | txn->uri = NULL; |
| 5143 | txn->srv_cookie = NULL; |
| 5144 | txn->cli_cookie = NULL; |
| 5145 | |
Christopher Faulet | 5939925 | 2019-11-07 14:27:52 +0100 | [diff] [blame] | 5146 | if (!LIST_ISEMPTY(&s->vars_txn.head)) |
| 5147 | vars_prune(&s->vars_txn, s->sess, s); |
| 5148 | if (!LIST_ISEMPTY(&s->vars_reqres.head)) |
| 5149 | vars_prune(&s->vars_reqres, s->sess, s); |
| 5150 | } |
| 5151 | |
Christopher Faulet | a8a46e2 | 2019-07-16 14:53:09 +0200 | [diff] [blame] | 5152 | |
| 5153 | DECLARE_POOL(pool_head_http_txn, "http_txn", sizeof(struct http_txn)); |
Christopher Faulet | 0f22695 | 2018-10-22 09:29:56 +0200 | [diff] [blame] | 5154 | |
Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 5155 | __attribute__((constructor)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5156 | static void __http_protocol_init(void) |
Christopher Faulet | f4eb75d | 2018-10-11 15:55:07 +0200 | [diff] [blame] | 5157 | { |
| 5158 | } |
| 5159 | |
| 5160 | |
| 5161 | /* |
| 5162 | * Local variables: |
| 5163 | * c-indent-level: 8 |
| 5164 | * c-basic-offset: 8 |
| 5165 | * End: |
| 5166 | */ |