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