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