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