Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 2 | * HTTP/1 mux-demux for connections |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2018 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 | */ |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 12 | #include <import/ebistree.h> |
Willy Tarreau | 63617db | 2021-10-06 18:23:40 +0200 | [diff] [blame] | 13 | #include <import/ebmbtree.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 14 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 15 | #include <haproxy/api.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 16 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 17 | #include <haproxy/connection.h> |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 18 | #include <haproxy/dynbuf.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 19 | #include <haproxy/h1.h> |
Willy Tarreau | c6fe884 | 2020-06-04 09:00:02 +0200 | [diff] [blame] | 20 | #include <haproxy/h1_htx.h> |
Willy Tarreau | bf07314 | 2020-06-03 12:04:01 +0200 | [diff] [blame] | 21 | #include <haproxy/h2.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 22 | #include <haproxy/http_htx.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 23 | #include <haproxy/htx.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 24 | #include <haproxy/istbuf.h> |
| 25 | #include <haproxy/log.h> |
Christopher Faulet | 18ad15f | 2022-09-15 10:51:26 +0200 | [diff] [blame] | 26 | #include <haproxy/mux_h1-t.h> |
Willy Tarreau | 551271d | 2020-06-04 08:32:23 +0200 | [diff] [blame] | 27 | #include <haproxy/pipe-t.h> |
Willy Tarreau | adc0240 | 2021-05-08 20:28:54 +0200 | [diff] [blame] | 28 | #include <haproxy/proxy.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 29 | #include <haproxy/session-t.h> |
Christopher Faulet | b4c584e | 2021-11-26 17:37:07 +0100 | [diff] [blame] | 30 | #include <haproxy/stats.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 31 | #include <haproxy/stconn.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 32 | #include <haproxy/stream.h> |
Willy Tarreau | c6d61d7 | 2020-06-04 19:02:42 +0200 | [diff] [blame] | 33 | #include <haproxy/trace.h> |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 34 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 35 | /* H1 connection descriptor */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 36 | struct h1c { |
| 37 | struct connection *conn; |
Christopher Faulet | 089cc6e | 2022-10-04 11:24:46 +0200 | [diff] [blame] | 38 | struct h1s *h1s; /* H1 stream descriptor */ |
| 39 | struct task *task; /* timeout management task */ |
| 40 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 41 | uint32_t flags; /* Connection flags: H1C_F_* */ |
Christopher Faulet | ef93be2 | 2022-10-04 17:13:32 +0200 | [diff] [blame] | 42 | enum h1_cs state; /* Connection state */ |
| 43 | |
Christopher Faulet | 089cc6e | 2022-10-04 11:24:46 +0200 | [diff] [blame] | 44 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 45 | struct buffer ibuf; /* Input buffer to store data before parsing */ |
| 46 | struct buffer obuf; /* Output buffer to store data after reformatting */ |
Christopher Faulet | 089cc6e | 2022-10-04 11:24:46 +0200 | [diff] [blame] | 47 | struct proxy *px; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 48 | |
Christopher Faulet | 089cc6e | 2022-10-04 11:24:46 +0200 | [diff] [blame] | 49 | unsigned int errcode; /* Status code when an error occurred at the H1 connection level */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 50 | |
Christopher Faulet | dbe5779 | 2020-10-05 17:50:58 +0200 | [diff] [blame] | 51 | int idle_exp; /* idle expiration date (http-keep-alive or http-request timeout) */ |
| 52 | int timeout; /* client/server timeout duration */ |
| 53 | int shut_timeout; /* client-fin/server-fin timeout duration */ |
Christopher Faulet | 089cc6e | 2022-10-04 11:24:46 +0200 | [diff] [blame] | 54 | |
| 55 | struct h1_counters *px_counters; /* h1 counters attached to proxy */ |
| 56 | struct buffer_wait buf_wait; /* Wait list for buffer allocation */ |
| 57 | struct wait_event wait_event; /* To be used if we're waiting for I/Os */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | /* H1 stream descriptor */ |
| 61 | struct h1s { |
| 62 | struct h1c *h1c; |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 63 | struct sedesc *sd; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 64 | uint32_t flags; /* Connection flags: H1S_F_* */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 65 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 66 | struct wait_event *subs; /* Address of the wait_event the stream connector associated is waiting on */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 67 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 68 | struct session *sess; /* Associated session */ |
Christopher Faulet | d17ad82 | 2020-09-24 15:14:29 +0200 | [diff] [blame] | 69 | struct buffer rxbuf; /* receive buffer, always valid (buf_empty or real buffer) */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 70 | struct h1m req; |
| 71 | struct h1m res; |
| 72 | |
Ilya Shipitsin | 47d1718 | 2020-06-21 21:42:57 +0500 | [diff] [blame] | 73 | enum http_meth_t meth; /* HTTP request method */ |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 74 | uint16_t status; /* HTTP response status */ |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 75 | |
| 76 | char ws_key[25]; /* websocket handshake key */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 77 | }; |
| 78 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 79 | /* Map of headers used to convert outgoing headers */ |
| 80 | struct h1_hdrs_map { |
| 81 | char *name; |
| 82 | struct eb_root map; |
| 83 | }; |
| 84 | |
| 85 | /* An entry in a headers map */ |
| 86 | struct h1_hdr_entry { |
| 87 | struct ist name; |
| 88 | struct ebpt_node node; |
| 89 | }; |
| 90 | |
| 91 | /* Declare the headers map */ |
| 92 | static struct h1_hdrs_map hdrs_map = { .name = NULL, .map = EB_ROOT }; |
Christopher Faulet | 0f9c0f5 | 2022-05-13 09:20:13 +0200 | [diff] [blame] | 93 | static int accept_payload_with_any_method = 0; |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 94 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 95 | /* trace source and events */ |
| 96 | static void h1_trace(enum trace_level level, uint64_t mask, |
| 97 | const struct trace_source *src, |
| 98 | const struct ist where, const struct ist func, |
| 99 | const void *a1, const void *a2, const void *a3, const void *a4); |
| 100 | |
| 101 | /* The event representation is split like this : |
| 102 | * h1c - internal H1 connection |
| 103 | * h1s - internal H1 stream |
| 104 | * strm - application layer |
| 105 | * rx - data receipt |
| 106 | * tx - data transmission |
| 107 | * |
| 108 | */ |
| 109 | static const struct trace_event h1_trace_events[] = { |
| 110 | #define H1_EV_H1C_NEW (1ULL << 0) |
| 111 | { .mask = H1_EV_H1C_NEW, .name = "h1c_new", .desc = "new H1 connection" }, |
| 112 | #define H1_EV_H1C_RECV (1ULL << 1) |
| 113 | { .mask = H1_EV_H1C_RECV, .name = "h1c_recv", .desc = "Rx on H1 connection" }, |
| 114 | #define H1_EV_H1C_SEND (1ULL << 2) |
| 115 | { .mask = H1_EV_H1C_SEND, .name = "h1c_send", .desc = "Tx on H1 connection" }, |
| 116 | #define H1_EV_H1C_BLK (1ULL << 3) |
| 117 | { .mask = H1_EV_H1C_BLK, .name = "h1c_blk", .desc = "H1 connection blocked" }, |
| 118 | #define H1_EV_H1C_WAKE (1ULL << 4) |
| 119 | { .mask = H1_EV_H1C_WAKE, .name = "h1c_wake", .desc = "H1 connection woken up" }, |
| 120 | #define H1_EV_H1C_END (1ULL << 5) |
| 121 | { .mask = H1_EV_H1C_END, .name = "h1c_end", .desc = "H1 connection terminated" }, |
| 122 | #define H1_EV_H1C_ERR (1ULL << 6) |
| 123 | { .mask = H1_EV_H1C_ERR, .name = "h1c_err", .desc = "error on H1 connection" }, |
| 124 | |
| 125 | #define H1_EV_RX_DATA (1ULL << 7) |
| 126 | { .mask = H1_EV_RX_DATA, .name = "rx_data", .desc = "receipt of any H1 data" }, |
| 127 | #define H1_EV_RX_EOI (1ULL << 8) |
| 128 | { .mask = H1_EV_RX_EOI, .name = "rx_eoi", .desc = "receipt of end of H1 input" }, |
| 129 | #define H1_EV_RX_HDRS (1ULL << 9) |
| 130 | { .mask = H1_EV_RX_HDRS, .name = "rx_headers", .desc = "receipt of H1 headers" }, |
| 131 | #define H1_EV_RX_BODY (1ULL << 10) |
| 132 | { .mask = H1_EV_RX_BODY, .name = "rx_body", .desc = "receipt of H1 body" }, |
| 133 | #define H1_EV_RX_TLRS (1ULL << 11) |
| 134 | { .mask = H1_EV_RX_TLRS, .name = "rx_trailerus", .desc = "receipt of H1 trailers" }, |
| 135 | |
| 136 | #define H1_EV_TX_DATA (1ULL << 12) |
| 137 | { .mask = H1_EV_TX_DATA, .name = "tx_data", .desc = "transmission of any H1 data" }, |
| 138 | #define H1_EV_TX_EOI (1ULL << 13) |
| 139 | { .mask = H1_EV_TX_EOI, .name = "tx_eoi", .desc = "transmission of end of H1 input" }, |
| 140 | #define H1_EV_TX_HDRS (1ULL << 14) |
| 141 | { .mask = H1_EV_TX_HDRS, .name = "tx_headers", .desc = "transmission of all headers" }, |
| 142 | #define H1_EV_TX_BODY (1ULL << 15) |
| 143 | { .mask = H1_EV_TX_BODY, .name = "tx_body", .desc = "transmission of H1 body" }, |
| 144 | #define H1_EV_TX_TLRS (1ULL << 16) |
| 145 | { .mask = H1_EV_TX_TLRS, .name = "tx_trailerus", .desc = "transmission of H1 trailers" }, |
| 146 | |
| 147 | #define H1_EV_H1S_NEW (1ULL << 17) |
| 148 | { .mask = H1_EV_H1S_NEW, .name = "h1s_new", .desc = "new H1 stream" }, |
| 149 | #define H1_EV_H1S_BLK (1ULL << 18) |
| 150 | { .mask = H1_EV_H1S_BLK, .name = "h1s_blk", .desc = "H1 stream blocked" }, |
| 151 | #define H1_EV_H1S_END (1ULL << 19) |
| 152 | { .mask = H1_EV_H1S_END, .name = "h1s_end", .desc = "H1 stream terminated" }, |
| 153 | #define H1_EV_H1S_ERR (1ULL << 20) |
| 154 | { .mask = H1_EV_H1S_ERR, .name = "h1s_err", .desc = "error on H1 stream" }, |
| 155 | |
| 156 | #define H1_EV_STRM_NEW (1ULL << 21) |
| 157 | { .mask = H1_EV_STRM_NEW, .name = "strm_new", .desc = "app-layer stream creation" }, |
| 158 | #define H1_EV_STRM_RECV (1ULL << 22) |
| 159 | { .mask = H1_EV_STRM_RECV, .name = "strm_recv", .desc = "receiving data for stream" }, |
| 160 | #define H1_EV_STRM_SEND (1ULL << 23) |
| 161 | { .mask = H1_EV_STRM_SEND, .name = "strm_send", .desc = "sending data for stream" }, |
| 162 | #define H1_EV_STRM_WAKE (1ULL << 24) |
| 163 | { .mask = H1_EV_STRM_WAKE, .name = "strm_wake", .desc = "stream woken up" }, |
| 164 | #define H1_EV_STRM_SHUT (1ULL << 25) |
| 165 | { .mask = H1_EV_STRM_SHUT, .name = "strm_shut", .desc = "stream shutdown" }, |
| 166 | #define H1_EV_STRM_END (1ULL << 26) |
| 167 | { .mask = H1_EV_STRM_END, .name = "strm_end", .desc = "detaching app-layer stream" }, |
| 168 | #define H1_EV_STRM_ERR (1ULL << 27) |
| 169 | { .mask = H1_EV_STRM_ERR, .name = "strm_err", .desc = "stream error" }, |
| 170 | |
| 171 | { } |
| 172 | }; |
| 173 | |
| 174 | static const struct name_desc h1_trace_lockon_args[4] = { |
| 175 | /* arg1 */ { /* already used by the connection */ }, |
| 176 | /* arg2 */ { .name="h1s", .desc="H1 stream" }, |
| 177 | /* arg3 */ { }, |
| 178 | /* arg4 */ { } |
| 179 | }; |
| 180 | |
| 181 | static const struct name_desc h1_trace_decoding[] = { |
| 182 | #define H1_VERB_CLEAN 1 |
| 183 | { .name="clean", .desc="only user-friendly stuff, generally suitable for level \"user\"" }, |
| 184 | #define H1_VERB_MINIMAL 2 |
| 185 | { .name="minimal", .desc="report only h1c/h1s state and flags, no real decoding" }, |
| 186 | #define H1_VERB_SIMPLE 3 |
| 187 | { .name="simple", .desc="add request/response status line or htx info when available" }, |
| 188 | #define H1_VERB_ADVANCED 4 |
| 189 | { .name="advanced", .desc="add header fields or frame decoding when available" }, |
| 190 | #define H1_VERB_COMPLETE 5 |
| 191 | { .name="complete", .desc="add full data dump when available" }, |
| 192 | { /* end */ } |
| 193 | }; |
| 194 | |
Willy Tarreau | 6eb3d37 | 2021-04-10 19:29:26 +0200 | [diff] [blame] | 195 | static struct trace_source trace_h1 __read_mostly = { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 196 | .name = IST("h1"), |
| 197 | .desc = "HTTP/1 multiplexer", |
| 198 | .arg_def = TRC_ARG1_CONN, // TRACE()'s first argument is always a connection |
| 199 | .default_cb = h1_trace, |
| 200 | .known_events = h1_trace_events, |
| 201 | .lockon_args = h1_trace_lockon_args, |
| 202 | .decoding = h1_trace_decoding, |
| 203 | .report_events = ~0, // report everything by default |
| 204 | }; |
| 205 | |
| 206 | #define TRACE_SOURCE &trace_h1 |
| 207 | INITCALL1(STG_REGISTER, trace_register_source, TRACE_SOURCE); |
| 208 | |
Christopher Faulet | b4c584e | 2021-11-26 17:37:07 +0100 | [diff] [blame] | 209 | |
| 210 | /* h1 stats module */ |
| 211 | enum { |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 212 | H1_ST_OPEN_CONN, |
| 213 | H1_ST_OPEN_STREAM, |
Christopher Faulet | 3bca28c | 2021-11-26 18:12:04 +0100 | [diff] [blame] | 214 | H1_ST_TOTAL_CONN, |
| 215 | H1_ST_TOTAL_STREAM, |
| 216 | |
Christopher Faulet | 41951ab | 2021-11-26 18:12:51 +0100 | [diff] [blame] | 217 | H1_ST_BYTES_IN, |
| 218 | H1_ST_BYTES_OUT, |
| 219 | #if defined(USE_LINUX_SPLICE) |
| 220 | H1_ST_SPLICED_BYTES_IN, |
| 221 | H1_ST_SPLICED_BYTES_OUT, |
| 222 | #endif |
Christopher Faulet | b4c584e | 2021-11-26 17:37:07 +0100 | [diff] [blame] | 223 | H1_STATS_COUNT /* must be the last member of the enum */ |
| 224 | }; |
| 225 | |
| 226 | |
| 227 | static struct name_desc h1_stats[] = { |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 228 | [H1_ST_OPEN_CONN] = { .name = "h1_open_connections", |
| 229 | .desc = "Count of currently open connections" }, |
| 230 | [H1_ST_OPEN_STREAM] = { .name = "h1_open_streams", |
| 231 | .desc = "Count of currently open streams" }, |
Christopher Faulet | 3bca28c | 2021-11-26 18:12:04 +0100 | [diff] [blame] | 232 | [H1_ST_TOTAL_CONN] = { .name = "h1_total_connections", |
| 233 | .desc = "Total number of connections" }, |
| 234 | [H1_ST_TOTAL_STREAM] = { .name = "h1_total_streams", |
Christopher Faulet | 41951ab | 2021-11-26 18:12:51 +0100 | [diff] [blame] | 235 | .desc = "Total number of streams" }, |
| 236 | |
| 237 | [H1_ST_BYTES_IN] = { .name = "h1_bytes_in", |
| 238 | .desc = "Total number of bytes received" }, |
| 239 | [H1_ST_BYTES_OUT] = { .name = "h1_bytes_out", |
| 240 | .desc = "Total number of bytes send" }, |
| 241 | #if defined(USE_LINUX_SPLICE) |
| 242 | [H1_ST_SPLICED_BYTES_IN] = { .name = "h1_spliced_bytes_in", |
| 243 | .desc = "Total number of bytes received using kernel splicing" }, |
| 244 | [H1_ST_SPLICED_BYTES_OUT] = { .name = "h1_spliced_bytes_out", |
| 245 | .desc = "Total number of bytes sendusing kernel splicing" }, |
| 246 | #endif |
Christopher Faulet | 3bca28c | 2021-11-26 18:12:04 +0100 | [diff] [blame] | 247 | |
Christopher Faulet | b4c584e | 2021-11-26 17:37:07 +0100 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | static struct h1_counters { |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 251 | long long open_conns; /* count of currently open connections */ |
| 252 | long long open_streams; /* count of currently open streams */ |
Christopher Faulet | 3bca28c | 2021-11-26 18:12:04 +0100 | [diff] [blame] | 253 | long long total_conns; /* total number of connections */ |
| 254 | long long total_streams; /* total number of streams */ |
| 255 | |
Christopher Faulet | 41951ab | 2021-11-26 18:12:51 +0100 | [diff] [blame] | 256 | long long bytes_in; /* number of bytes received */ |
| 257 | long long bytes_out; /* number of bytes sent */ |
| 258 | #if defined(USE_LINUX_SPLICE) |
| 259 | long long spliced_bytes_in; /* number of bytes received using kernel splicing */ |
| 260 | long long spliced_bytes_out; /* number of bytes sent using kernel splicing */ |
| 261 | #endif |
Christopher Faulet | b4c584e | 2021-11-26 17:37:07 +0100 | [diff] [blame] | 262 | } h1_counters; |
| 263 | |
| 264 | static void h1_fill_stats(void *data, struct field *stats) |
| 265 | { |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 266 | struct h1_counters *counters = data; |
| 267 | |
| 268 | stats[H1_ST_OPEN_CONN] = mkf_u64(FN_GAUGE, counters->open_conns); |
| 269 | stats[H1_ST_OPEN_STREAM] = mkf_u64(FN_GAUGE, counters->open_streams); |
Christopher Faulet | 3bca28c | 2021-11-26 18:12:04 +0100 | [diff] [blame] | 270 | stats[H1_ST_TOTAL_CONN] = mkf_u64(FN_COUNTER, counters->total_conns); |
| 271 | stats[H1_ST_TOTAL_STREAM] = mkf_u64(FN_COUNTER, counters->total_streams); |
Christopher Faulet | 41951ab | 2021-11-26 18:12:51 +0100 | [diff] [blame] | 272 | |
| 273 | stats[H1_ST_BYTES_IN] = mkf_u64(FN_COUNTER, counters->bytes_in); |
| 274 | stats[H1_ST_BYTES_OUT] = mkf_u64(FN_COUNTER, counters->bytes_out); |
| 275 | #if defined(USE_LINUX_SPLICE) |
| 276 | stats[H1_ST_SPLICED_BYTES_IN] = mkf_u64(FN_COUNTER, counters->spliced_bytes_in); |
| 277 | stats[H1_ST_SPLICED_BYTES_OUT] = mkf_u64(FN_COUNTER, counters->spliced_bytes_out); |
| 278 | #endif |
Christopher Faulet | b4c584e | 2021-11-26 17:37:07 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static struct stats_module h1_stats_module = { |
| 282 | .name = "h1", |
| 283 | .fill_stats = h1_fill_stats, |
| 284 | .stats = h1_stats, |
| 285 | .stats_count = H1_STATS_COUNT, |
| 286 | .counters = &h1_counters, |
| 287 | .counters_size = sizeof(h1_counters), |
| 288 | .domain_flags = MK_STATS_PROXY_DOMAIN(STATS_PX_CAP_FE|STATS_PX_CAP_BE), |
| 289 | .clearable = 1, |
| 290 | }; |
| 291 | |
| 292 | INITCALL1(STG_REGISTER, stats_register_module, &h1_stats_module); |
| 293 | |
| 294 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 295 | /* the h1c and h1s pools */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 296 | DECLARE_STATIC_POOL(pool_head_h1c, "h1c", sizeof(struct h1c)); |
| 297 | DECLARE_STATIC_POOL(pool_head_h1s, "h1s", sizeof(struct h1s)); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 298 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 299 | static int h1_recv(struct h1c *h1c); |
| 300 | static int h1_send(struct h1c *h1c); |
| 301 | static int h1_process(struct h1c *h1c); |
Willy Tarreau | 691d503 | 2021-01-20 14:55:01 +0100 | [diff] [blame] | 302 | /* h1_io_cb is exported to see it resolved in "show fd" */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 303 | struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state); |
| 304 | struct task *h1_timeout_task(struct task *t, void *context, unsigned int state); |
Christopher Faulet | a85c522 | 2021-10-27 15:36:38 +0200 | [diff] [blame] | 305 | static void h1_shutw_conn(struct connection *conn); |
Christopher Faulet | 660f6f3 | 2019-10-04 10:22:47 +0200 | [diff] [blame] | 306 | static void h1_wake_stream_for_recv(struct h1s *h1s); |
| 307 | static void h1_wake_stream_for_send(struct h1s *h1s); |
Willy Tarreau | a220e59 | 2023-03-21 10:44:44 +0100 | [diff] [blame] | 308 | static void h1s_destroy(struct h1s *h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 309 | |
Willy Tarreau | 97b4d3b | 2022-05-18 07:27:14 +0200 | [diff] [blame] | 310 | /* returns the stconn associated to the H1 stream */ |
| 311 | static forceinline struct stconn *h1s_sc(const struct h1s *h1s) |
| 312 | { |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 313 | return h1s->sd->sc; |
Willy Tarreau | 97b4d3b | 2022-05-18 07:27:14 +0200 | [diff] [blame] | 314 | } |
| 315 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 316 | /* the H1 traces always expect that arg1, if non-null, is of type connection |
| 317 | * (from which we can derive h1c), that arg2, if non-null, is of type h1s, and |
| 318 | * that arg3, if non-null, is a htx for rx/tx headers. |
| 319 | */ |
| 320 | static void h1_trace(enum trace_level level, uint64_t mask, const struct trace_source *src, |
| 321 | const struct ist where, const struct ist func, |
| 322 | const void *a1, const void *a2, const void *a3, const void *a4) |
| 323 | { |
| 324 | const struct connection *conn = a1; |
| 325 | const struct h1c *h1c = conn ? conn->ctx : NULL; |
| 326 | const struct h1s *h1s = a2; |
| 327 | const struct htx *htx = a3; |
| 328 | const size_t *val = a4; |
| 329 | |
| 330 | if (!h1c) |
| 331 | h1c = (h1s ? h1s->h1c : NULL); |
| 332 | |
| 333 | if (!h1c || src->verbosity < H1_VERB_CLEAN) |
| 334 | return; |
| 335 | |
| 336 | /* Display frontend/backend info by default */ |
Christopher Faulet | ef93be2 | 2022-10-04 17:13:32 +0200 | [diff] [blame] | 337 | chunk_appendf(&trace_buf, " : [%c,%s]", ((h1c->flags & H1C_F_IS_BACK) ? 'B' : 'F'), h1c_st_to_str(h1c->state)); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 338 | |
| 339 | /* Display request and response states if h1s is defined */ |
Christopher Faulet | 6580f28 | 2021-11-26 17:31:35 +0100 | [diff] [blame] | 340 | if (h1s) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 341 | chunk_appendf(&trace_buf, " [%s, %s]", |
| 342 | h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state)); |
| 343 | |
Christopher Faulet | 6580f28 | 2021-11-26 17:31:35 +0100 | [diff] [blame] | 344 | if (src->verbosity > H1_VERB_SIMPLE) { |
| 345 | chunk_appendf(&trace_buf, " - req=(.fl=0x%08x .curr_len=%lu .body_len=%lu)", |
| 346 | h1s->req.flags, (unsigned long)h1s->req.curr_len, (unsigned long)h1s->req.body_len); |
| 347 | chunk_appendf(&trace_buf, " res=(.fl=0x%08x .curr_len=%lu .body_len=%lu)", |
| 348 | h1s->res.flags, (unsigned long)h1s->res.curr_len, (unsigned long)h1s->res.body_len); |
| 349 | } |
| 350 | |
| 351 | } |
| 352 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 353 | if (src->verbosity == H1_VERB_CLEAN) |
| 354 | return; |
| 355 | |
| 356 | /* Display the value to the 4th argument (level > STATE) */ |
| 357 | if (src->level > TRACE_LEVEL_STATE && val) |
Willy Tarreau | e18f53e | 2019-11-27 15:41:31 +0100 | [diff] [blame] | 358 | chunk_appendf(&trace_buf, " - VAL=%lu", (long)*val); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 359 | |
| 360 | /* Display status-line if possible (verbosity > MINIMAL) */ |
| 361 | if (src->verbosity > H1_VERB_MINIMAL && htx && htx_nbblks(htx)) { |
| 362 | const struct htx_blk *blk = htx_get_head_blk(htx); |
| 363 | const struct htx_sl *sl = htx_get_blk_ptr(htx, blk); |
| 364 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 365 | |
| 366 | if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL) |
| 367 | chunk_appendf(&trace_buf, " - \"%.*s %.*s %.*s\"", |
| 368 | HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl), |
| 369 | HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl), |
| 370 | HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl)); |
| 371 | } |
| 372 | |
| 373 | /* Display h1c info and, if defined, h1s info (pointer + flags) */ |
| 374 | chunk_appendf(&trace_buf, " - h1c=%p(0x%08x)", h1c, h1c->flags); |
Christopher Faulet | 99293b0 | 2021-11-10 10:30:15 +0100 | [diff] [blame] | 375 | if (h1c->conn) |
| 376 | chunk_appendf(&trace_buf, " conn=%p(0x%08x)", h1c->conn, h1c->conn->flags); |
| 377 | if (h1s) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 378 | chunk_appendf(&trace_buf, " h1s=%p(0x%08x)", h1s, h1s->flags); |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 379 | if (h1s->sd) |
| 380 | chunk_appendf(&trace_buf, " sd=%p(0x%08x)", h1s->sd, se_fl_get(h1s->sd)); |
| 381 | if (h1s->sd && h1s_sc(h1s)) |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 382 | chunk_appendf(&trace_buf, " sc=%p(0x%08x)", h1s_sc(h1s), h1s_sc(h1s)->flags); |
Christopher Faulet | 99293b0 | 2021-11-10 10:30:15 +0100 | [diff] [blame] | 383 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 384 | |
| 385 | if (src->verbosity == H1_VERB_MINIMAL) |
| 386 | return; |
| 387 | |
| 388 | /* Display input and output buffer info (level > USER & verbosity > SIMPLE) */ |
| 389 | if (src->level > TRACE_LEVEL_USER) { |
| 390 | if (src->verbosity == H1_VERB_COMPLETE || |
| 391 | (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_RECV|H1_EV_STRM_RECV)))) |
| 392 | chunk_appendf(&trace_buf, " ibuf=%u@%p+%u/%u", |
| 393 | (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf), |
| 394 | (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf)); |
| 395 | if (src->verbosity == H1_VERB_COMPLETE || |
| 396 | (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_H1C_SEND|H1_EV_STRM_SEND)))) |
| 397 | chunk_appendf(&trace_buf, " obuf=%u@%p+%u/%u", |
| 398 | (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf), |
| 399 | (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf)); |
| 400 | } |
| 401 | |
| 402 | /* Display htx info if defined (level > USER) */ |
| 403 | if (src->level > TRACE_LEVEL_USER && htx) { |
| 404 | int full = 0; |
| 405 | |
| 406 | /* Full htx info (level > STATE && verbosity > SIMPLE) */ |
| 407 | if (src->level > TRACE_LEVEL_STATE) { |
| 408 | if (src->verbosity == H1_VERB_COMPLETE) |
| 409 | full = 1; |
| 410 | else if (src->verbosity == H1_VERB_ADVANCED && (mask & (H1_EV_RX_HDRS|H1_EV_TX_HDRS))) |
| 411 | full = 1; |
| 412 | } |
| 413 | |
| 414 | chunk_memcat(&trace_buf, "\n\t", 2); |
| 415 | htx_dump(&trace_buf, htx, full); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 420 | /*****************************************************/ |
| 421 | /* functions below are for dynamic buffer management */ |
| 422 | /*****************************************************/ |
| 423 | /* |
Christopher Faulet | 2545a0b | 2019-12-05 12:30:55 +0100 | [diff] [blame] | 424 | * Indicates whether or not we may receive data. The rules are the following : |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 425 | * - if an error or a shutdown for reads was detected on the H1 connection we |
Christopher Faulet | 119ac87 | 2020-09-30 17:33:22 +0200 | [diff] [blame] | 426 | * must not attempt to receive |
| 427 | * - if we are waiting for the connection establishment, we must not attempt |
| 428 | * to receive |
Christopher Faulet | 119ac87 | 2020-09-30 17:33:22 +0200 | [diff] [blame] | 429 | * - if reads are explicitly disabled, we must not attempt to receive |
Christopher Faulet | 2545a0b | 2019-12-05 12:30:55 +0100 | [diff] [blame] | 430 | * - if the input buffer failed to be allocated or is full , we must not try |
| 431 | * to receive |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 432 | * - if the mux is blocked on an input condition, we must may not attempt to |
| 433 | * receive |
| 434 | * - otherwise we may attempt to receive |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 435 | */ |
| 436 | static inline int h1_recv_allowed(const struct h1c *h1c) |
| 437 | { |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 438 | if (h1c->flags & (H1C_F_EOS|H1C_F_ERROR)) { |
| 439 | TRACE_DEVEL("recv not allowed because of (eos|error) on h1c", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 440 | return 0; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 441 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 442 | |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 443 | if (h1c->conn->flags & (CO_FL_WAIT_L4_CONN|CO_FL_WAIT_L6_CONN)) { |
| 444 | TRACE_DEVEL("recv not allowed because of (waitl4|waitl6) on connection", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn); |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 445 | return 0; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 446 | } |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 447 | |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 448 | if ((h1c->flags & (H1C_F_IN_ALLOC|H1C_F_IN_FULL|H1C_F_IN_SALLOC))) { |
| 449 | TRACE_DEVEL("recv not allowed because input is blocked", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn); |
Christopher Faulet | 119ac87 | 2020-09-30 17:33:22 +0200 | [diff] [blame] | 450 | return 0; |
| 451 | } |
| 452 | |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 453 | return 1; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* |
| 457 | * Tries to grab a buffer and to re-enables processing on mux <target>. The h1 |
| 458 | * flags are used to figure what buffer was requested. It returns 1 if the |
| 459 | * allocation succeeds, in which case the connection is woken up, or 0 if it's |
| 460 | * impossible to wake up and we prefer to be woken up later. |
| 461 | */ |
| 462 | static int h1_buf_available(void *target) |
| 463 | { |
| 464 | struct h1c *h1c = target; |
| 465 | |
Willy Tarreau | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 466 | if ((h1c->flags & H1C_F_IN_ALLOC) && b_alloc(&h1c->ibuf)) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 467 | TRACE_STATE("unblocking h1c, ibuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 468 | h1c->flags &= ~H1C_F_IN_ALLOC; |
| 469 | if (h1_recv_allowed(h1c)) |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 470 | tasklet_wakeup(h1c->wait_event.tasklet); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 471 | return 1; |
| 472 | } |
| 473 | |
Willy Tarreau | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 474 | if ((h1c->flags & H1C_F_OUT_ALLOC) && b_alloc(&h1c->obuf)) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 475 | TRACE_STATE("unblocking h1s, obuf allocated", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1c->h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 476 | h1c->flags &= ~H1C_F_OUT_ALLOC; |
Christopher Faulet | 660f6f3 | 2019-10-04 10:22:47 +0200 | [diff] [blame] | 477 | if (h1c->h1s) |
| 478 | h1_wake_stream_for_send(h1c->h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 479 | return 1; |
| 480 | } |
| 481 | |
Willy Tarreau | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 482 | if ((h1c->flags & H1C_F_IN_SALLOC) && h1c->h1s && b_alloc(&h1c->h1s->rxbuf)) { |
Christopher Faulet | d17ad82 | 2020-09-24 15:14:29 +0200 | [diff] [blame] | 483 | TRACE_STATE("unblocking h1c, stream rxbuf allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn); |
| 484 | h1c->flags &= ~H1C_F_IN_SALLOC; |
| 485 | tasklet_wakeup(h1c->wait_event.tasklet); |
| 486 | return 1; |
| 487 | } |
| 488 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | /* |
| 493 | * Allocate a buffer. If if fails, it adds the mux in buffer wait queue. |
| 494 | */ |
| 495 | static inline struct buffer *h1_get_buf(struct h1c *h1c, struct buffer *bptr) |
| 496 | { |
| 497 | struct buffer *buf = NULL; |
| 498 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 499 | if (likely(!LIST_INLIST(&h1c->buf_wait.list)) && |
Willy Tarreau | d68d4f1 | 2021-03-22 14:44:31 +0100 | [diff] [blame] | 500 | unlikely((buf = b_alloc(bptr)) == NULL)) { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 501 | h1c->buf_wait.target = h1c; |
| 502 | h1c->buf_wait.wakeup_cb = h1_buf_available; |
Willy Tarreau | b4e3476 | 2021-09-30 19:02:18 +0200 | [diff] [blame] | 503 | LIST_APPEND(&th_ctx->buffer_wq, &h1c->buf_wait.list); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 504 | } |
| 505 | return buf; |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * Release a buffer, if any, and try to wake up entities waiting in the buffer |
| 510 | * wait queue. |
| 511 | */ |
| 512 | static inline void h1_release_buf(struct h1c *h1c, struct buffer *bptr) |
| 513 | { |
| 514 | if (bptr->size) { |
| 515 | b_free(bptr); |
Willy Tarreau | 4d77bbf | 2021-02-20 12:02:46 +0100 | [diff] [blame] | 516 | offer_buffers(h1c->buf_wait.target, 1); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
Christopher Faulet | ef93be2 | 2022-10-04 17:13:32 +0200 | [diff] [blame] | 520 | /* Returns 1 if the H1 connection is alive (IDLE, EMBRYONIC, RUNNING or |
| 521 | * RUNNING). Ortherwise 0 is returned. |
| 522 | */ |
| 523 | static inline int h1_is_alive(const struct h1c *h1c) |
| 524 | { |
| 525 | return (h1c->state <= H1_CS_RUNNING); |
| 526 | } |
| 527 | |
| 528 | /* Switch the H1 connection to CLOSING or CLOSED mode, depending on the output |
| 529 | * buffer state and if there is still a H1 stream or not. If there are sill |
| 530 | * pending outgoing data or if there is still a H1 stream, it is set to CLOSING |
| 531 | * state. Otherwise it is set to CLOSED mode. */ |
| 532 | static inline void h1_close(struct h1c *h1c) |
| 533 | { |
| 534 | h1c->state = ((h1c->h1s || b_data(&h1c->obuf)) ? H1_CS_CLOSING : H1_CS_CLOSED); |
| 535 | } |
| 536 | |
Christopher Faulet | aaa67bc | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 537 | /* returns the number of streams in use on a connection to figure if it's idle |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 538 | * or not. We rely on H1C state to know if the connection is in-use or not. It |
| 539 | * is IDLE only when no H1 stream is attached and when the previous stream, if |
| 540 | * any, was fully terminated without any error and in K/A mode. |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 541 | */ |
| 542 | static int h1_used_streams(struct connection *conn) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 543 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 544 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 545 | |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 546 | return ((h1c->state == H1_CS_IDLE) ? 0 : 1); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 547 | } |
| 548 | |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 549 | /* returns the number of streams still available on a connection */ |
| 550 | static int h1_avail_streams(struct connection *conn) |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 551 | { |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 552 | return 1 - h1_used_streams(conn); |
Olivier Houchard | 8defe4b | 2018-12-02 01:31:17 +0100 | [diff] [blame] | 553 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 554 | |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 555 | /* Refresh the h1c task timeout if necessary */ |
| 556 | static void h1_refresh_timeout(struct h1c *h1c) |
| 557 | { |
Remi Tricot-Le Breton | b5d968d | 2022-04-08 18:04:18 +0200 | [diff] [blame] | 558 | int is_idle_conn = 0; |
| 559 | |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 560 | if (h1c->task) { |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 561 | if (!h1_is_alive(h1c)) { |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 562 | /* half-closed or dead connections : switch to clientfin/serverfin |
| 563 | * timeouts so that we don't hang too long on clients that have |
| 564 | * gone away (especially in tunnel mode). |
Willy Tarreau | 4313d5a | 2020-09-08 15:40:57 +0200 | [diff] [blame] | 565 | */ |
| 566 | h1c->task->expire = tick_add(now_ms, h1c->shut_timeout); |
Christopher Faulet | adcd789 | 2020-10-05 17:13:05 +0200 | [diff] [blame] | 567 | TRACE_DEVEL("refreshing connection's timeout (dead or half-closed)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn); |
Remi Tricot-Le Breton | b5d968d | 2022-04-08 18:04:18 +0200 | [diff] [blame] | 568 | is_idle_conn = 1; |
Christopher Faulet | adcd789 | 2020-10-05 17:13:05 +0200 | [diff] [blame] | 569 | } |
| 570 | else if (b_data(&h1c->obuf)) { |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 571 | /* alive connection with pending outgoing data, need a timeout (server or client). */ |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 572 | h1c->task->expire = tick_add(now_ms, h1c->timeout); |
Christopher Faulet | adcd789 | 2020-10-05 17:13:05 +0200 | [diff] [blame] | 573 | TRACE_DEVEL("refreshing connection's timeout (pending outgoing data)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn); |
| 574 | } |
Christopher Faulet | bc378bb | 2023-11-07 18:36:02 +0100 | [diff] [blame] | 575 | else if (!(h1c->flags & H1C_F_IS_BACK) && (h1c->state == H1_CS_IDLE)) { |
| 576 | /* idle front connections. */ |
| 577 | h1c->task->expire = (tick_isset(h1c->idle_exp) ? h1c->idle_exp : tick_add(now_ms, h1c->timeout)); |
| 578 | TRACE_DEVEL("refreshing connection's timeout (idle front h1c)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn); |
| 579 | is_idle_conn = 1; |
| 580 | } |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 581 | else if (!(h1c->flags & H1C_F_IS_BACK) && (h1c->state != H1_CS_RUNNING)) { |
| 582 | /* alive front connections waiting for a fully usable stream need a timeout. */ |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 583 | h1c->task->expire = tick_add(now_ms, h1c->timeout); |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 584 | TRACE_DEVEL("refreshing connection's timeout (alive front h1c but not ready)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn); |
Remi Tricot-Le Breton | b5d968d | 2022-04-08 18:04:18 +0200 | [diff] [blame] | 585 | /* A frontend connection not yet ready could be treated the same way as an idle |
| 586 | * one in case of soft-close. |
| 587 | */ |
| 588 | is_idle_conn = 1; |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 589 | } |
Christopher Faulet | adcd789 | 2020-10-05 17:13:05 +0200 | [diff] [blame] | 590 | else { |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 591 | /* alive back connections of front connections with a stream connector attached */ |
Christopher Faulet | adcd789 | 2020-10-05 17:13:05 +0200 | [diff] [blame] | 592 | h1c->task->expire = TICK_ETERNITY; |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 593 | TRACE_DEVEL("no connection timeout (alive back h1c or front h1c with an SC)", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn); |
Christopher Faulet | adcd789 | 2020-10-05 17:13:05 +0200 | [diff] [blame] | 594 | } |
| 595 | |
Christopher Faulet | dbe5779 | 2020-10-05 17:50:58 +0200 | [diff] [blame] | 596 | /* Finally set the idle expiration date if shorter */ |
| 597 | h1c->task->expire = tick_first(h1c->task->expire, h1c->idle_exp); |
Remi Tricot-Le Breton | b5d968d | 2022-04-08 18:04:18 +0200 | [diff] [blame] | 598 | |
| 599 | if ((h1c->px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) && |
| 600 | is_idle_conn && tick_isset(global.close_spread_end)) { |
| 601 | /* If a soft-stop is in progress and a close-spread-time |
| 602 | * is set, we want to spread idle connection closing roughly |
| 603 | * evenly across the defined window. This should only |
| 604 | * act on idle frontend connections. |
| 605 | * If the window end is already in the past, we wake the |
| 606 | * timeout task up immediately so that it can be closed. |
| 607 | */ |
| 608 | int remaining_window = tick_remain(now_ms, global.close_spread_end); |
| 609 | if (remaining_window) { |
| 610 | /* We don't need to reset the expire if it would |
| 611 | * already happen before the close window end. |
| 612 | */ |
| 613 | if (tick_is_le(global.close_spread_end, h1c->task->expire)) { |
| 614 | /* Set an expire value shorter than the current value |
| 615 | * because the close spread window end comes earlier. |
| 616 | */ |
| 617 | h1c->task->expire = tick_add(now_ms, statistical_prng_range(remaining_window)); |
| 618 | TRACE_DEVEL("connection timeout set to value before close-spread window end", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn); |
| 619 | } |
| 620 | } |
| 621 | else { |
| 622 | /* We are past the soft close window end, wake the timeout |
| 623 | * task up immediately. |
| 624 | */ |
| 625 | task_wakeup(h1c->task, TASK_WOKEN_TIMER); |
| 626 | } |
| 627 | } |
Christopher Faulet | adcd789 | 2020-10-05 17:13:05 +0200 | [diff] [blame] | 628 | TRACE_DEVEL("new expiration date", H1_EV_H1C_SEND|H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){h1c->task->expire}); |
| 629 | task_queue(h1c->task); |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 630 | } |
| 631 | } |
Christopher Faulet | dbe5779 | 2020-10-05 17:50:58 +0200 | [diff] [blame] | 632 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 633 | static void h1_set_idle_expiration(struct h1c *h1c) |
Christopher Faulet | dbe5779 | 2020-10-05 17:50:58 +0200 | [diff] [blame] | 634 | { |
| 635 | if (h1c->flags & H1C_F_IS_BACK || !h1c->task) { |
| 636 | TRACE_DEVEL("no idle expiration (backend connection || no task)", H1_EV_H1C_RECV, h1c->conn); |
| 637 | h1c->idle_exp = TICK_ETERNITY; |
| 638 | return; |
| 639 | } |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 640 | if (h1c->state == H1_CS_IDLE) { |
Christopher Faulet | dbe5779 | 2020-10-05 17:50:58 +0200 | [diff] [blame] | 641 | if (!tick_isset(h1c->idle_exp)) { |
| 642 | if ((h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* Not the first request */ |
| 643 | !b_data(&h1c->ibuf) && /* No input data */ |
| 644 | tick_isset(h1c->px->timeout.httpka)) { /* K-A timeout set */ |
| 645 | h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpka); |
| 646 | TRACE_DEVEL("set idle expiration (keep-alive timeout)", H1_EV_H1C_RECV, h1c->conn); |
| 647 | } |
| 648 | else { |
| 649 | h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq); |
| 650 | TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn); |
| 651 | } |
| 652 | } |
| 653 | } |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 654 | else if (h1c->state < H1_CS_RUNNING) { |
Christopher Faulet | dbe5779 | 2020-10-05 17:50:58 +0200 | [diff] [blame] | 655 | if (!tick_isset(h1c->idle_exp)) { |
| 656 | h1c->idle_exp = tick_add_ifset(now_ms, h1c->px->timeout.httpreq); |
| 657 | TRACE_DEVEL("set idle expiration (http-request timeout)", H1_EV_H1C_RECV, h1c->conn); |
| 658 | } |
| 659 | } |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 660 | else { |
Christopher Faulet | dbe5779 | 2020-10-05 17:50:58 +0200 | [diff] [blame] | 661 | h1c->idle_exp = TICK_ETERNITY; |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 662 | TRACE_DEVEL("unset idle expiration (running or closing)", H1_EV_H1C_RECV, h1c->conn); |
Christopher Faulet | dbe5779 | 2020-10-05 17:50:58 +0200 | [diff] [blame] | 663 | } |
| 664 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 665 | /*****************************************************************/ |
| 666 | /* functions below are dedicated to the mux setup and management */ |
| 667 | /*****************************************************************/ |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 668 | |
| 669 | /* returns non-zero if there are input data pending for stream h1s. */ |
| 670 | static inline size_t h1s_data_pending(const struct h1s *h1s) |
| 671 | { |
| 672 | const struct h1m *h1m; |
| 673 | |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 674 | h1m = ((h1s->h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req); |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 675 | return ((h1m->state == H1_MSG_DONE) ? 0 : b_data(&h1s->h1c->ibuf)); |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 676 | } |
| 677 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 678 | /* Creates a new stream connector and the associate stream. <input> is used as input |
Christopher Faulet | 16df178 | 2020-12-04 16:47:41 +0100 | [diff] [blame] | 679 | * buffer for the stream. On success, it is transferred to the stream and the |
| 680 | * mux is no longer responsible of it. On error, <input> is unchanged, thus the |
| 681 | * mux must still take care of it. However, there is nothing special to do |
| 682 | * because, on success, <input> is updated to points on BUF_NULL. Thus, calling |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 683 | * b_free() on it is always safe. This function returns the stream connector on |
Christopher Faulet | 16df178 | 2020-12-04 16:47:41 +0100 | [diff] [blame] | 684 | * success or NULL on error. */ |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 685 | static struct stconn *h1s_new_sc(struct h1s *h1s, struct buffer *input) |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 686 | { |
Christopher Faulet | dd2d0d8 | 2021-12-20 09:34:32 +0100 | [diff] [blame] | 687 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 688 | |
Christopher Faulet | dd2d0d8 | 2021-12-20 09:34:32 +0100 | [diff] [blame] | 689 | TRACE_ENTER(H1_EV_STRM_NEW, h1c->conn, h1s); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 690 | |
Christopher Faulet | b669d68 | 2022-03-22 18:37:19 +0100 | [diff] [blame] | 691 | if (h1s->flags & H1S_F_NOT_FIRST) |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 692 | se_fl_set(h1s->sd, SE_FL_NOT_FIRST); |
Christopher Faulet | b669d68 | 2022-03-22 18:37:19 +0100 | [diff] [blame] | 693 | if (h1s->req.flags & H1_MF_UPG_WEBSOCKET) |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 694 | se_fl_set(h1s->sd, SE_FL_WEBSOCKET); |
Christopher Faulet | b669d68 | 2022-03-22 18:37:19 +0100 | [diff] [blame] | 695 | |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 696 | if (!sc_new_from_endp(h1s->sd, h1c->conn->owner, input)) { |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 697 | TRACE_ERROR("SC allocation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1c->conn, h1s); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 698 | goto err; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 699 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 700 | |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 701 | h1c->state = H1_CS_RUNNING; |
Christopher Faulet | dd2d0d8 | 2021-12-20 09:34:32 +0100 | [diff] [blame] | 702 | TRACE_LEAVE(H1_EV_STRM_NEW, h1c->conn, h1s); |
Willy Tarreau | 97b4d3b | 2022-05-18 07:27:14 +0200 | [diff] [blame] | 703 | return h1s_sc(h1s); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 704 | |
Christopher Faulet | 91449b0 | 2022-03-22 18:45:55 +0100 | [diff] [blame] | 705 | err: |
Christopher Faulet | dd2d0d8 | 2021-12-20 09:34:32 +0100 | [diff] [blame] | 706 | TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn, h1s); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 707 | return NULL; |
| 708 | } |
| 709 | |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 710 | static struct stconn *h1s_upgrade_sc(struct h1s *h1s, struct buffer *input) |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 711 | { |
| 712 | TRACE_ENTER(H1_EV_STRM_NEW, h1s->h1c->conn, h1s); |
| 713 | |
Willy Tarreau | df1a2fc | 2022-05-27 11:11:15 +0200 | [diff] [blame] | 714 | if (stream_upgrade_from_sc(h1s_sc(h1s), input) < 0) { |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 715 | TRACE_ERROR("stream upgrade failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, h1s->h1c->conn, h1s); |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 716 | goto err; |
| 717 | } |
| 718 | |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 719 | h1s->h1c->state = H1_CS_RUNNING; |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 720 | TRACE_LEAVE(H1_EV_STRM_NEW, h1s->h1c->conn, h1s); |
Willy Tarreau | 97b4d3b | 2022-05-18 07:27:14 +0200 | [diff] [blame] | 721 | return h1s_sc(h1s); |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 722 | |
| 723 | err: |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 724 | TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1s->h1c->conn, h1s); |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 725 | return NULL; |
| 726 | } |
| 727 | |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 728 | static struct h1s *h1s_new(struct h1c *h1c) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 729 | { |
| 730 | struct h1s *h1s; |
| 731 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 732 | TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn); |
| 733 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 734 | h1s = pool_alloc(pool_head_h1s); |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 735 | if (!h1s) { |
| 736 | TRACE_ERROR("H1S allocation failure", H1_EV_H1S_NEW|H1_EV_H1S_END|H1_EV_H1S_ERR, h1c->conn); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 737 | goto fail; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 738 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 739 | h1s->h1c = h1c; |
| 740 | h1c->h1s = h1s; |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 741 | h1s->sess = NULL; |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 742 | h1s->sd = NULL; |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 743 | h1s->flags = H1S_F_WANT_KAL; |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 744 | h1s->subs = NULL; |
Christopher Faulet | d17ad82 | 2020-09-24 15:14:29 +0200 | [diff] [blame] | 745 | h1s->rxbuf = BUF_NULL; |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 746 | memset(h1s->ws_key, 0, sizeof(h1s->ws_key)); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 747 | |
| 748 | h1m_init_req(&h1s->req); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 749 | h1s->req.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 750 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 751 | h1m_init_res(&h1s->res); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 752 | h1s->res.flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 753 | |
| 754 | h1s->status = 0; |
| 755 | h1s->meth = HTTP_METH_OTHER; |
| 756 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 757 | if (h1c->flags & H1C_F_WAIT_NEXT_REQ) |
| 758 | h1s->flags |= H1S_F_NOT_FIRST; |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 759 | h1s->h1c->state = H1_CS_EMBRYONIC; |
Christopher Faulet | 4427ea7 | 2022-11-23 15:58:59 +0100 | [diff] [blame] | 760 | h1s->h1c->flags &= ~H1C_F_WAIT_NEXT_REQ; |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 761 | TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s); |
| 762 | return h1s; |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 763 | |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 764 | fail: |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 765 | TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn); |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 766 | return NULL; |
| 767 | } |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 768 | |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 769 | static struct h1s *h1c_frt_stream_new(struct h1c *h1c, struct stconn *sc, struct session *sess) |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 770 | { |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 771 | struct h1s *h1s; |
| 772 | |
| 773 | TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn); |
| 774 | |
| 775 | h1s = h1s_new(h1c); |
| 776 | if (!h1s) |
| 777 | goto fail; |
| 778 | |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 779 | if (sc) { |
| 780 | if (sc_attach_mux(sc, h1s, h1c->conn) < 0) |
Christopher Faulet | 070b91b | 2022-03-31 19:27:18 +0200 | [diff] [blame] | 781 | goto fail; |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 782 | h1s->sd = sc->sedesc; |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 783 | } |
| 784 | else { |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 785 | h1s->sd = sedesc_new(); |
| 786 | if (!h1s->sd) |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 787 | goto fail; |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 788 | h1s->sd->se = h1s; |
| 789 | h1s->sd->conn = h1c->conn; |
| 790 | se_fl_set(h1s->sd, SE_FL_T_MUX | SE_FL_ORPHAN); |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 791 | } |
Christopher Faulet | f4b89f1 | 2023-02-23 13:58:13 +0100 | [diff] [blame] | 792 | /* When a request starts, the H1S does not expect data while the request |
| 793 | * is not finished. It does not mean the response must not be received, |
| 794 | * especially if headers were already forwarded. But it is not |
| 795 | * mandatory. |
| 796 | */ |
| 797 | se_expect_no_data(h1s->sd); |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 798 | h1s->sess = sess; |
| 799 | |
| 800 | if (h1c->px->options2 & PR_O2_REQBUG_OK) |
| 801 | h1s->req.err_pos = -1; |
| 802 | |
Christopher Faulet | af5336f | 2022-09-12 07:46:11 +0200 | [diff] [blame] | 803 | HA_ATOMIC_INC(&h1c->px_counters->open_streams); |
| 804 | HA_ATOMIC_INC(&h1c->px_counters->total_streams); |
| 805 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 806 | h1c->idle_exp = TICK_ETERNITY; |
| 807 | h1_set_idle_expiration(h1c); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 808 | TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 809 | return h1s; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 810 | |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 811 | fail: |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 812 | TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn); |
Willy Tarreau | a220e59 | 2023-03-21 10:44:44 +0100 | [diff] [blame] | 813 | h1s_destroy(h1s); |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 814 | return NULL; |
| 815 | } |
| 816 | |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 817 | static struct h1s *h1c_bck_stream_new(struct h1c *h1c, struct stconn *sc, struct session *sess) |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 818 | { |
| 819 | struct h1s *h1s; |
| 820 | |
| 821 | TRACE_ENTER(H1_EV_H1S_NEW, h1c->conn); |
| 822 | |
| 823 | h1s = h1s_new(h1c); |
| 824 | if (!h1s) |
| 825 | goto fail; |
| 826 | |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 827 | if (sc_attach_mux(sc, h1s, h1c->conn) < 0) |
Christopher Faulet | 070b91b | 2022-03-31 19:27:18 +0200 | [diff] [blame] | 828 | goto fail; |
| 829 | |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 830 | h1s->flags |= H1S_F_RX_BLK; |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 831 | h1s->sd = sc->sedesc; |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 832 | h1s->sess = sess; |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 833 | |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 834 | h1c->state = H1_CS_RUNNING; |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 835 | |
| 836 | if (h1c->px->options2 & PR_O2_RSPBUG_OK) |
| 837 | h1s->res.err_pos = -1; |
| 838 | |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 839 | HA_ATOMIC_INC(&h1c->px_counters->open_streams); |
Christopher Faulet | 3bca28c | 2021-11-26 18:12:04 +0100 | [diff] [blame] | 840 | HA_ATOMIC_INC(&h1c->px_counters->total_streams); |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 841 | |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 842 | TRACE_LEAVE(H1_EV_H1S_NEW, h1c->conn, h1s); |
| 843 | return h1s; |
| 844 | |
| 845 | fail: |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 846 | TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_ERR, h1c->conn); |
Willy Tarreau | a220e59 | 2023-03-21 10:44:44 +0100 | [diff] [blame] | 847 | h1s_destroy(h1s); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 848 | return NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | static void h1s_destroy(struct h1s *h1s) |
| 852 | { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 853 | if (h1s) { |
| 854 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 855 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 856 | TRACE_POINT(H1_EV_H1S_END, h1c->conn, h1s); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 857 | h1c->h1s = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 858 | |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 859 | if (h1s->subs) |
| 860 | h1s->subs->events = 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 861 | |
Christopher Faulet | d17ad82 | 2020-09-24 15:14:29 +0200 | [diff] [blame] | 862 | h1_release_buf(h1c, &h1s->rxbuf); |
| 863 | |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 864 | h1c->flags &= ~(H1C_F_WANT_SPLICE| |
Christopher Faulet | 295b8d1 | 2020-09-30 17:14:55 +0200 | [diff] [blame] | 865 | H1C_F_OUT_FULL|H1C_F_OUT_ALLOC|H1C_F_IN_SALLOC| |
| 866 | H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER); |
Christopher Faulet | aaa67bc | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 867 | |
Christopher Faulet | 31da34d | 2022-10-10 16:36:10 +0200 | [diff] [blame] | 868 | if (!(h1c->flags & (H1C_F_EOS|H1C_F_ERR_PENDING|H1C_F_ERROR|H1C_F_ABRT_PENDING|H1C_F_ABRTED)) && /* No error/read0/abort */ |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 869 | h1_is_alive(h1c) && /* still alive */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 870 | (h1s->flags & H1S_F_WANT_KAL) && /* K/A possible */ |
Christopher Faulet | aaa67bc | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 871 | h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { /* req/res in DONE state */ |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 872 | h1c->state = H1_CS_IDLE; |
| 873 | h1c->flags |= H1C_F_WAIT_NEXT_REQ; |
Christopher Faulet | aaa67bc | 2019-12-05 10:23:37 +0100 | [diff] [blame] | 874 | TRACE_STATE("set idle mode on h1c, waiting for the next request", H1_EV_H1C_ERR, h1c->conn, h1s); |
| 875 | } |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 876 | else { |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 877 | h1_close(h1c); |
| 878 | TRACE_STATE("close h1c", H1_EV_H1S_END, h1c->conn, h1s); |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 879 | } |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 880 | |
| 881 | HA_ATOMIC_DEC(&h1c->px_counters->open_streams); |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 882 | BUG_ON(h1s->sd && !se_fl_test(h1s->sd, SE_FL_ORPHAN)); |
| 883 | sedesc_free(h1s->sd); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 884 | pool_free(pool_head_h1s, h1s); |
| 885 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /* |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 889 | * Initialize the mux once it's attached. It is expected that conn->ctx points |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 890 | * to the existing stream connector (for outgoing connections or for incoming |
| 891 | * ones during a mux upgrade) or NULL (for incoming ones during the connection |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 892 | * establishment). <input> is always used as Input buffer and may contain |
| 893 | * data. It is the caller responsibility to not reuse it anymore. Returns < 0 on |
| 894 | * error. |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 895 | */ |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 896 | static int h1_init(struct connection *conn, struct proxy *proxy, struct session *sess, |
| 897 | struct buffer *input) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 898 | { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 899 | struct h1c *h1c; |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 900 | struct task *t = NULL; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 901 | void *conn_ctx = conn->ctx; |
| 902 | |
| 903 | TRACE_ENTER(H1_EV_H1C_NEW); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 904 | |
| 905 | h1c = pool_alloc(pool_head_h1c); |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 906 | if (!h1c) { |
| 907 | TRACE_ERROR("H1C allocation failure", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 908 | goto fail_h1c; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 909 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 910 | h1c->conn = conn; |
| 911 | h1c->px = proxy; |
| 912 | |
Christopher Faulet | ef93be2 | 2022-10-04 17:13:32 +0200 | [diff] [blame] | 913 | h1c->state = H1_CS_IDLE; |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 914 | h1c->flags = H1C_F_NONE; |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 915 | h1c->errcode = 0; |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 916 | h1c->ibuf = *input; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 917 | h1c->obuf = BUF_NULL; |
| 918 | h1c->h1s = NULL; |
Christopher Faulet | 51f73eb | 2019-04-08 11:22:47 +0200 | [diff] [blame] | 919 | h1c->task = NULL; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 920 | |
Willy Tarreau | 90f366b | 2021-02-20 11:49:49 +0100 | [diff] [blame] | 921 | LIST_INIT(&h1c->buf_wait.list); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 922 | h1c->wait_event.tasklet = tasklet_new(); |
| 923 | if (!h1c->wait_event.tasklet) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 924 | goto fail; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 925 | h1c->wait_event.tasklet->process = h1_io_cb; |
| 926 | h1c->wait_event.tasklet->context = h1c; |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 927 | h1c->wait_event.events = 0; |
Christopher Faulet | dbe5779 | 2020-10-05 17:50:58 +0200 | [diff] [blame] | 928 | h1c->idle_exp = TICK_ETERNITY; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 929 | |
Christopher Faulet | e9b7072 | 2019-04-08 10:46:02 +0200 | [diff] [blame] | 930 | if (conn_is_back(conn)) { |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 931 | h1c->flags |= H1C_F_IS_BACK; |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 932 | h1c->shut_timeout = h1c->timeout = proxy->timeout.server; |
| 933 | if (tick_isset(proxy->timeout.serverfin)) |
| 934 | h1c->shut_timeout = proxy->timeout.serverfin; |
Christopher Faulet | 563c345 | 2021-11-26 17:37:51 +0100 | [diff] [blame] | 935 | |
| 936 | h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_be, |
| 937 | &h1_stats_module); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 938 | } else { |
| 939 | h1c->shut_timeout = h1c->timeout = proxy->timeout.client; |
| 940 | if (tick_isset(proxy->timeout.clientfin)) |
| 941 | h1c->shut_timeout = proxy->timeout.clientfin; |
Amaury Denoyelle | d3a88c1 | 2021-05-03 10:47:51 +0200 | [diff] [blame] | 942 | |
Christopher Faulet | 563c345 | 2021-11-26 17:37:51 +0100 | [diff] [blame] | 943 | h1c->px_counters = EXTRA_COUNTERS_GET(proxy->extra_counters_fe, |
| 944 | &h1_stats_module); |
| 945 | |
Amaury Denoyelle | d3a88c1 | 2021-05-03 10:47:51 +0200 | [diff] [blame] | 946 | LIST_APPEND(&mux_stopping_data[tid].list, |
| 947 | &h1c->conn->stopping_list); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 948 | } |
| 949 | if (tick_isset(h1c->timeout)) { |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 950 | t = task_new_here(); |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 951 | if (!t) { |
| 952 | TRACE_ERROR("H1C task allocation failure", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 953 | goto fail; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 954 | } |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 955 | |
| 956 | h1c->task = t; |
| 957 | t->process = h1_timeout_task; |
| 958 | t->context = h1c; |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 959 | |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 960 | t->expire = tick_add(now_ms, h1c->timeout); |
| 961 | } |
| 962 | |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 963 | conn->ctx = h1c; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 964 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 965 | if (h1c->flags & H1C_F_IS_BACK) { |
| 966 | /* Create a new H1S now for backend connection only */ |
Christopher Faulet | 2f0ec66 | 2020-09-24 10:30:15 +0200 | [diff] [blame] | 967 | if (!h1c_bck_stream_new(h1c, conn_ctx, sess)) |
| 968 | goto fail; |
| 969 | } |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 970 | else if (conn_ctx) { |
| 971 | /* Upgraded frontend connection (from TCP) */ |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 972 | if (!h1c_frt_stream_new(h1c, conn_ctx, h1c->conn->owner)) |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 973 | goto fail; |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 974 | |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 975 | /* Attach the SC but Not ready yet */ |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 976 | h1c->state = H1_CS_UPGRADING; |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 977 | TRACE_DEVEL("Inherit the SC from TCP connection to perform an upgrade", |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 978 | H1_EV_H1C_NEW|H1_EV_STRM_NEW, h1c->conn, h1c->h1s); |
| 979 | } |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 980 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 981 | if (t) { |
| 982 | h1_set_idle_expiration(h1c); |
| 983 | t->expire = tick_first(t->expire, h1c->idle_exp); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 984 | task_queue(t); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 985 | } |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 986 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 987 | /* prepare to read something */ |
Christopher Faulet | d9ee788 | 2021-01-21 17:45:45 +0100 | [diff] [blame] | 988 | if (b_data(&h1c->ibuf)) |
| 989 | tasklet_wakeup(h1c->wait_event.tasklet); |
| 990 | else if (h1_recv_allowed(h1c)) |
Christopher Faulet | 089acd5 | 2020-09-21 10:57:52 +0200 | [diff] [blame] | 991 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 992 | |
Frédéric Lécaille | 9969adb | 2023-01-18 11:52:21 +0100 | [diff] [blame] | 993 | if (!conn_is_back(conn)) |
| 994 | proxy_inc_fe_cum_sess_ver_ctr(sess->listener, proxy, 1); |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 995 | HA_ATOMIC_INC(&h1c->px_counters->open_conns); |
Christopher Faulet | 3bca28c | 2021-11-26 18:12:04 +0100 | [diff] [blame] | 996 | HA_ATOMIC_INC(&h1c->px_counters->total_conns); |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 997 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 998 | /* mux->wake will be called soon to complete the operation */ |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 999 | TRACE_LEAVE(H1_EV_H1C_NEW, conn, h1c->h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1000 | return 0; |
| 1001 | |
| 1002 | fail: |
Willy Tarreau | f656279 | 2019-05-07 19:05:35 +0200 | [diff] [blame] | 1003 | task_destroy(t); |
Tim Duesterhus | b1ec21d | 2023-04-22 17:47:32 +0200 | [diff] [blame] | 1004 | tasklet_free(h1c->wait_event.tasklet); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1005 | pool_free(pool_head_h1c, h1c); |
| 1006 | fail_h1c: |
Willy Tarreau | 3b990fe | 2022-01-12 17:24:26 +0100 | [diff] [blame] | 1007 | if (!conn_is_back(conn)) |
| 1008 | LIST_DEL_INIT(&conn->stopping_list); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1009 | conn->ctx = conn_ctx; // restore saved context |
| 1010 | TRACE_DEVEL("leaving in error", H1_EV_H1C_NEW|H1_EV_H1C_END|H1_EV_H1C_ERR); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1011 | return -1; |
| 1012 | } |
| 1013 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 1014 | /* release function. This one should be called to free all resources allocated |
| 1015 | * to the mux. |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1016 | */ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 1017 | static void h1_release(struct h1c *h1c) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1018 | { |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 1019 | struct connection *conn = NULL; |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 1020 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1021 | TRACE_POINT(H1_EV_H1C_END); |
| 1022 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 1023 | /* The connection must be aattached to this mux to be released */ |
| 1024 | if (h1c->conn && h1c->conn->ctx == h1c) |
| 1025 | conn = h1c->conn; |
Christopher Faulet | 61840e7 | 2019-04-15 09:33:32 +0200 | [diff] [blame] | 1026 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 1027 | if (conn && h1c->flags & H1C_F_UPG_H2C) { |
| 1028 | TRACE_DEVEL("upgrading H1 to H2", H1_EV_H1C_END, conn); |
| 1029 | /* Make sure we're no longer subscribed to anything */ |
| 1030 | if (h1c->wait_event.events) |
| 1031 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, |
| 1032 | h1c->wait_event.events, &h1c->wait_event); |
| 1033 | if (conn_upgrade_mux_fe(conn, NULL, &h1c->ibuf, ist("h2"), PROTO_MODE_HTTP) != -1) { |
| 1034 | /* connection successfully upgraded to H2, this |
| 1035 | * mux was already released */ |
| 1036 | return; |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 1037 | } |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 1038 | TRACE_ERROR("h2 upgrade failed", H1_EV_H1C_END|H1_EV_H1C_ERR, conn); |
| 1039 | sess_log(conn->owner); /* Log if the upgrade failed */ |
| 1040 | } |
Olivier Houchard | 45c4437 | 2019-06-11 14:06:23 +0200 | [diff] [blame] | 1041 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1042 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 1043 | if (LIST_INLIST(&h1c->buf_wait.list)) |
| 1044 | LIST_DEL_INIT(&h1c->buf_wait.list); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1045 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 1046 | h1_release_buf(h1c, &h1c->ibuf); |
| 1047 | h1_release_buf(h1c, &h1c->obuf); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 1048 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 1049 | if (h1c->task) { |
| 1050 | h1c->task->context = NULL; |
| 1051 | task_wakeup(h1c->task, TASK_WOKEN_OTHER); |
| 1052 | h1c->task = NULL; |
| 1053 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1054 | |
Christopher Faulet | 551b896 | 2023-03-24 09:26:16 +0100 | [diff] [blame] | 1055 | if (h1c->wait_event.tasklet) { |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 1056 | tasklet_free(h1c->wait_event.tasklet); |
Christopher Faulet | 551b896 | 2023-03-24 09:26:16 +0100 | [diff] [blame] | 1057 | h1c->wait_event.tasklet = NULL; |
| 1058 | } |
Christopher Faulet | 60fa051 | 2021-11-26 18:10:39 +0100 | [diff] [blame] | 1059 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 1060 | h1s_destroy(h1c->h1s); |
| 1061 | if (conn) { |
| 1062 | if (h1c->wait_event.events != 0) |
| 1063 | conn->xprt->unsubscribe(conn, conn->xprt_ctx, h1c->wait_event.events, |
| 1064 | &h1c->wait_event); |
| 1065 | h1_shutw_conn(conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1066 | } |
| 1067 | |
Christopher Faulet | 4de1bff | 2022-04-14 11:36:41 +0200 | [diff] [blame] | 1068 | HA_ATOMIC_DEC(&h1c->px_counters->open_conns); |
| 1069 | pool_free(pool_head_h1c, h1c); |
| 1070 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 1071 | if (conn) { |
Amaury Denoyelle | d3a88c1 | 2021-05-03 10:47:51 +0200 | [diff] [blame] | 1072 | if (!conn_is_back(conn)) |
| 1073 | LIST_DEL_INIT(&conn->stopping_list); |
| 1074 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 1075 | conn->mux = NULL; |
| 1076 | conn->ctx = NULL; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1077 | TRACE_DEVEL("freeing conn", H1_EV_H1C_END, conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1078 | |
Christopher Faulet | 39a96ee | 2019-04-08 10:52:21 +0200 | [diff] [blame] | 1079 | conn_stop_tracking(conn); |
| 1080 | conn_full_close(conn); |
| 1081 | if (conn->destroy_cb) |
| 1082 | conn->destroy_cb(conn); |
| 1083 | conn_free(conn); |
| 1084 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | /******************************************************/ |
| 1088 | /* functions below are for the H1 protocol processing */ |
| 1089 | /******************************************************/ |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1090 | /* Parse the request version and set H1_MF_VER_11 on <h1m> if the version is |
| 1091 | * greater or equal to 1.1 |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1092 | */ |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1093 | static void h1_parse_req_vsn(struct h1m *h1m, const struct htx_sl *sl) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1094 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1095 | const char *p = HTX_SL_REQ_VPTR(sl); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1096 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1097 | if ((HTX_SL_REQ_VLEN(sl) == 8) && |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1098 | (*(p + 5) > '1' || |
| 1099 | (*(p + 5) == '1' && *(p + 7) >= '1'))) |
| 1100 | h1m->flags |= H1_MF_VER_11; |
| 1101 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1102 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1103 | /* Parse the response version and set H1_MF_VER_11 on <h1m> if the version is |
| 1104 | * greater or equal to 1.1 |
| 1105 | */ |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1106 | static void h1_parse_res_vsn(struct h1m *h1m, const struct htx_sl *sl) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1107 | { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1108 | const char *p = HTX_SL_RES_VPTR(sl); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1109 | |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 1110 | if ((HTX_SL_RES_VLEN(sl) == 8) && |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1111 | (*(p + 5) > '1' || |
| 1112 | (*(p + 5) == '1' && *(p + 7) >= '1'))) |
| 1113 | h1m->flags |= H1_MF_VER_11; |
| 1114 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1115 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1116 | /* Deduce the connection mode of the client connection, depending on the |
| 1117 | * configuration and the H1 message flags. This function is called twice, the |
| 1118 | * first time when the request is parsed and the second time when the response |
| 1119 | * is parsed. |
| 1120 | */ |
| 1121 | static void h1_set_cli_conn_mode(struct h1s *h1s, struct h1m *h1m) |
| 1122 | { |
| 1123 | struct proxy *fe = h1s->h1c->px; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1124 | |
| 1125 | if (h1m->flags & H1_MF_RESP) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1126 | /* Output direction: second pass */ |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 1127 | if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1128 | h1s->status == 101) { |
| 1129 | /* Either we've established an explicit tunnel, or we're |
| 1130 | * switching the protocol. In both cases, we're very unlikely to |
| 1131 | * understand the next protocols. We have to switch to tunnel |
| 1132 | * mode, so that we transfer the request and responses then let |
| 1133 | * this protocol pass unmodified. When we later implement |
| 1134 | * specific parsers for such protocols, we'll want to check the |
| 1135 | * Upgrade header which contains information about that protocol |
| 1136 | * for responses with status 101 (eg: see RFC2817 about TLS). |
| 1137 | */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1138 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1139 | TRACE_STATE("set tunnel mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1140 | } |
| 1141 | else if (h1s->flags & H1S_F_WANT_KAL) { |
| 1142 | /* By default the client is in KAL mode. CLOSE mode mean |
| 1143 | * it is imposed by the client itself. So only change |
| 1144 | * KAL mode here. */ |
| 1145 | if (!(h1m->flags & H1_MF_XFER_LEN) || (h1m->flags & H1_MF_CONN_CLO)) { |
| 1146 | /* no length known or explicit close => close */ |
| 1147 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1148 | TRACE_STATE("detect close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1149 | } |
| 1150 | else if (!(h1m->flags & H1_MF_CONN_KAL) && |
| 1151 | (fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO) { |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1152 | /* no explicit keep-alive and option httpclose => close */ |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1153 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1154 | TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1155 | } |
| 1156 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1157 | } |
| 1158 | else { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1159 | /* Input direction: first pass */ |
| 1160 | if (!(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL)) || h1m->flags & H1_MF_CONN_CLO) { |
| 1161 | /* no explicit keep-alive in HTTP/1.0 or explicit close => close*/ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1162 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1163 | TRACE_STATE("detect close mode (req)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1164 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1165 | } |
| 1166 | |
Remi Tricot-Le Breton | b4f5fac | 2022-04-25 17:50:48 +0200 | [diff] [blame] | 1167 | /* If KAL, check if the frontend is stopping. If yes, switch in CLO mode |
Remi Tricot-Le Breton | 4d7fdc6 | 2022-04-26 15:17:18 +0200 | [diff] [blame] | 1168 | * unless a 'close-spread-time' option is set (either to define a |
| 1169 | * soft-close window or to disable active closing (close-spread-time |
| 1170 | * option set to 0). |
Remi Tricot-Le Breton | b4f5fac | 2022-04-25 17:50:48 +0200 | [diff] [blame] | 1171 | */ |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 1172 | if (h1s->flags & H1S_F_WANT_KAL && (fe->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) { |
Remi Tricot-Le Breton | b4f5fac | 2022-04-25 17:50:48 +0200 | [diff] [blame] | 1173 | int want_clo = 1; |
| 1174 | /* If a close-spread-time option is set, we want to avoid |
| 1175 | * closing all the active HTTP connections at once so we add a |
| 1176 | * random factor that will spread the closing. |
| 1177 | */ |
| 1178 | if (tick_isset(global.close_spread_end)) { |
| 1179 | int remaining_window = tick_remain(now_ms, global.close_spread_end); |
| 1180 | if (remaining_window) { |
| 1181 | /* This should increase the closing rate the further along |
| 1182 | * the window we are. |
| 1183 | */ |
| 1184 | want_clo = (remaining_window <= statistical_prng_range(global.close_spread_time)); |
| 1185 | } |
| 1186 | } |
Remi Tricot-Le Breton | 4d7fdc6 | 2022-04-26 15:17:18 +0200 | [diff] [blame] | 1187 | else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE) |
| 1188 | want_clo = 0; |
Remi Tricot-Le Breton | b4f5fac | 2022-04-25 17:50:48 +0200 | [diff] [blame] | 1189 | |
| 1190 | if (want_clo) { |
| 1191 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 1192 | TRACE_STATE("stopping, set close mode", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
| 1193 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1194 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | /* Deduce the connection mode of the client connection, depending on the |
| 1198 | * configuration and the H1 message flags. This function is called twice, the |
| 1199 | * first time when the request is parsed and the second time when the response |
| 1200 | * is parsed. |
| 1201 | */ |
| 1202 | static void h1_set_srv_conn_mode(struct h1s *h1s, struct h1m *h1m) |
| 1203 | { |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1204 | struct session *sess = h1s->sess; |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1205 | struct proxy *be = h1s->h1c->px; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 1206 | int fe_flags = sess ? sess->fe->options : 0; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1207 | |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1208 | if (h1m->flags & H1_MF_RESP) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1209 | /* Input direction: second pass */ |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 1210 | if ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1211 | h1s->status == 101) { |
| 1212 | /* Either we've established an explicit tunnel, or we're |
| 1213 | * switching the protocol. In both cases, we're very unlikely to |
| 1214 | * understand the next protocols. We have to switch to tunnel |
| 1215 | * mode, so that we transfer the request and responses then let |
| 1216 | * this protocol pass unmodified. When we later implement |
| 1217 | * specific parsers for such protocols, we'll want to check the |
| 1218 | * Upgrade header which contains information about that protocol |
| 1219 | * for responses with status 101 (eg: see RFC2817 about TLS). |
| 1220 | */ |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1221 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_TUN; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1222 | TRACE_STATE("set tunnel mode (resp)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1223 | } |
| 1224 | else if (h1s->flags & H1S_F_WANT_KAL) { |
| 1225 | /* By default the server is in KAL mode. CLOSE mode mean |
| 1226 | * it is imposed by haproxy itself. So only change KAL |
| 1227 | * mode here. */ |
| 1228 | if (!(h1m->flags & H1_MF_XFER_LEN) || h1m->flags & H1_MF_CONN_CLO || |
| 1229 | !(h1m->flags & (H1_MF_VER_11|H1_MF_CONN_KAL))){ |
| 1230 | /* no length known or explicit close or no explicit keep-alive in HTTP/1.0 => close */ |
| 1231 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1232 | TRACE_STATE("detect close mode (resp)", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1233 | } |
| 1234 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1235 | } |
Christopher Faulet | 7003378 | 2018-12-05 13:50:11 +0100 | [diff] [blame] | 1236 | else { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1237 | /* Output direction: first pass */ |
| 1238 | if (h1m->flags & H1_MF_CONN_CLO) { |
| 1239 | /* explicit close => close */ |
Christopher Faulet | 7003378 | 2018-12-05 13:50:11 +0100 | [diff] [blame] | 1240 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1241 | TRACE_STATE("detect close mode (req)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1242 | } |
| 1243 | else if (!(h1m->flags & H1_MF_CONN_KAL) && |
| 1244 | ((fe_flags & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
| 1245 | (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL || |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1246 | (be->options & PR_O_HTTP_MODE) == PR_O_HTTP_CLO)) { |
| 1247 | /* no explicit keep-alive option httpclose/server-close => close */ |
| 1248 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1249 | TRACE_STATE("force close mode (req)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1250 | } |
Christopher Faulet | 7003378 | 2018-12-05 13:50:11 +0100 | [diff] [blame] | 1251 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1252 | |
| 1253 | /* If KAL, check if the backend is stopping. If yes, switch in CLO mode */ |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 1254 | if (h1s->flags & H1S_F_WANT_KAL && (be->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1255 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1256 | TRACE_STATE("stopping, set close mode", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
| 1257 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1258 | } |
| 1259 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1260 | static void h1_update_req_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1261 | { |
| 1262 | struct proxy *px = h1s->h1c->px; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1263 | |
| 1264 | /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage" |
| 1265 | * token is found |
| 1266 | */ |
| 1267 | if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1268 | return; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1269 | |
| 1270 | if (h1s->flags & H1S_F_WANT_KAL || px->options2 & PR_O2_FAKE_KA) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1271 | if (!(h1m->flags & H1_MF_VER_11)) { |
| 1272 | TRACE_STATE("add \"Connection: keep-alive\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1273 | *conn_val = ist("keep-alive"); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1274 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1275 | } |
| 1276 | else { /* H1S_F_WANT_CLO && !PR_O2_FAKE_KA */ |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1277 | if (h1m->flags & H1_MF_VER_11) { |
| 1278 | TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1279 | *conn_val = ist("close"); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1280 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1281 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1282 | } |
| 1283 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1284 | static void h1_update_res_conn_value(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val) |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1285 | { |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1286 | /* Don't update "Connection:" header in TUNNEL mode or if "Upgrage" |
| 1287 | * token is found |
| 1288 | */ |
| 1289 | if (h1s->flags & H1S_F_WANT_TUN || h1m->flags & H1_MF_CONN_UPG) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1290 | return; |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1291 | |
| 1292 | if (h1s->flags & H1S_F_WANT_KAL) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1293 | if (!(h1m->flags & H1_MF_VER_11) || |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1294 | !((h1m->flags & h1s->req.flags) & H1_MF_VER_11)) { |
| 1295 | TRACE_STATE("add \"Connection: keep-alive\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1296 | *conn_val = ist("keep-alive"); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1297 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1298 | } |
| 1299 | else { /* H1S_F_WANT_CLO */ |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1300 | if (h1m->flags & H1_MF_VER_11) { |
| 1301 | TRACE_STATE("add \"Connection: close\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1302 | *conn_val = ist("close"); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1303 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1304 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1305 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1306 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1307 | static void h1_process_input_conn_mode(struct h1s *h1s, struct h1m *h1m, struct htx *htx) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1308 | { |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 1309 | if (!(h1s->h1c->flags & H1C_F_IS_BACK)) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1310 | h1_set_cli_conn_mode(h1s, h1m); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1311 | else |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1312 | h1_set_srv_conn_mode(h1s, h1m); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1313 | } |
| 1314 | |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1315 | static void h1_process_output_conn_mode(struct h1s *h1s, struct h1m *h1m, struct ist *conn_val) |
| 1316 | { |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 1317 | if (!(h1s->h1c->flags & H1C_F_IS_BACK)) |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1318 | h1_set_cli_conn_mode(h1s, h1m); |
| 1319 | else |
| 1320 | h1_set_srv_conn_mode(h1s, h1m); |
| 1321 | |
| 1322 | if (!(h1m->flags & H1_MF_RESP)) |
| 1323 | h1_update_req_conn_value(h1s, h1m, conn_val); |
| 1324 | else |
| 1325 | h1_update_res_conn_value(h1s, h1m, conn_val); |
| 1326 | } |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1327 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 1328 | /* Try to adjust the case of the message header name using the global map |
| 1329 | * <hdrs_map>. |
| 1330 | */ |
| 1331 | static void h1_adjust_case_outgoing_hdr(struct h1s *h1s, struct h1m *h1m, struct ist *name) |
| 1332 | { |
| 1333 | struct ebpt_node *node; |
| 1334 | struct h1_hdr_entry *entry; |
| 1335 | |
| 1336 | /* No entry in the map, do nothing */ |
| 1337 | if (eb_is_empty(&hdrs_map.map)) |
| 1338 | return; |
| 1339 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1340 | /* No conversion for the request headers */ |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 1341 | if (!(h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGSRV)) |
| 1342 | return; |
| 1343 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1344 | /* No conversion for the response headers */ |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 1345 | if ((h1m->flags & H1_MF_RESP) && !(h1s->h1c->px->options2 & PR_O2_H1_ADJ_BUGCLI)) |
| 1346 | return; |
| 1347 | |
| 1348 | node = ebis_lookup_len(&hdrs_map.map, name->ptr, name->len); |
| 1349 | if (!node) |
| 1350 | return; |
| 1351 | entry = container_of(node, struct h1_hdr_entry, node); |
| 1352 | name->ptr = entry->name.ptr; |
| 1353 | name->len = entry->name.len; |
| 1354 | } |
| 1355 | |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1356 | /* Append the description of what is present in error snapshot <es> into <out>. |
| 1357 | * The description must be small enough to always fit in a buffer. The output |
| 1358 | * buffer may be the trash so the trash must not be used inside this function. |
| 1359 | */ |
| 1360 | static void h1_show_error_snapshot(struct buffer *out, const struct error_snapshot *es) |
| 1361 | { |
| 1362 | chunk_appendf(out, |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 1363 | " H1 connection flags 0x%08x, H1 stream flags 0x%08x\n" |
| 1364 | " H1 msg state %s(%d), H1 msg flags 0x%08x\n" |
| 1365 | " H1 chunk len %lld bytes, H1 body len %lld bytes :\n", |
| 1366 | es->ctx.h1.c_flags, es->ctx.h1.s_flags, |
| 1367 | h1m_state_str(es->ctx.h1.state), es->ctx.h1.state, |
| 1368 | es->ctx.h1.m_flags, es->ctx.h1.m_clen, es->ctx.h1.m_blen); |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1369 | } |
| 1370 | /* |
| 1371 | * Capture a bad request or response and archive it in the proxy's structure. |
| 1372 | * By default it tries to report the error position as h1m->err_pos. However if |
| 1373 | * this one is not set, it will then report h1m->next, which is the last known |
| 1374 | * parsing point. The function is able to deal with wrapping buffers. It always |
| 1375 | * displays buffers as a contiguous area starting at buf->p. The direction is |
| 1376 | * determined thanks to the h1m's flags. |
| 1377 | */ |
| 1378 | static void h1_capture_bad_message(struct h1c *h1c, struct h1s *h1s, |
| 1379 | struct h1m *h1m, struct buffer *buf) |
| 1380 | { |
Christopher Faulet | db2c17d | 2020-10-15 17:19:46 +0200 | [diff] [blame] | 1381 | struct session *sess = h1s->sess; |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1382 | struct proxy *proxy = h1c->px; |
Olivier Houchard | bdb00c5 | 2020-03-12 15:30:17 +0100 | [diff] [blame] | 1383 | struct proxy *other_end; |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1384 | union error_snapshot_ctx ctx; |
| 1385 | |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 1386 | if (h1c->state == H1_CS_UPGRADING || h1c->state == H1_CS_RUNNING) { |
Olivier Houchard | bdb00c5 | 2020-03-12 15:30:17 +0100 | [diff] [blame] | 1387 | if (sess == NULL) |
Willy Tarreau | ea27f48 | 2022-05-18 16:10:52 +0200 | [diff] [blame] | 1388 | sess = __sc_strm(h1s_sc(h1s))->sess; |
Olivier Houchard | bdb00c5 | 2020-03-12 15:30:17 +0100 | [diff] [blame] | 1389 | if (!(h1m->flags & H1_MF_RESP)) |
Willy Tarreau | ea27f48 | 2022-05-18 16:10:52 +0200 | [diff] [blame] | 1390 | other_end = __sc_strm(h1s_sc(h1s))->be; |
Olivier Houchard | bdb00c5 | 2020-03-12 15:30:17 +0100 | [diff] [blame] | 1391 | else |
| 1392 | other_end = sess->fe; |
| 1393 | } else |
| 1394 | other_end = NULL; |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1395 | |
| 1396 | /* http-specific part now */ |
| 1397 | ctx.h1.state = h1m->state; |
| 1398 | ctx.h1.c_flags = h1c->flags; |
| 1399 | ctx.h1.s_flags = h1s->flags; |
| 1400 | ctx.h1.m_flags = h1m->flags; |
| 1401 | ctx.h1.m_clen = h1m->curr_len; |
| 1402 | ctx.h1.m_blen = h1m->body_len; |
| 1403 | |
| 1404 | proxy_capture_error(proxy, !!(h1m->flags & H1_MF_RESP), other_end, |
| 1405 | h1c->conn->target, sess, buf, 0, 0, |
Christopher Faulet | aa75b3d | 2018-12-05 16:20:40 +0100 | [diff] [blame] | 1406 | (h1m->err_pos >= 0) ? h1m->err_pos : h1m->next, |
| 1407 | &ctx, h1_show_error_snapshot); |
Christopher Faulet | e44769b | 2018-11-29 23:01:45 +0100 | [diff] [blame] | 1408 | } |
| 1409 | |
Christopher Faulet | adb2220 | 2018-12-12 10:32:09 +0100 | [diff] [blame] | 1410 | /* Emit the chunksize followed by a CRLF in front of data of the buffer |
| 1411 | * <buf>. It goes backwards and starts with the byte before the buffer's |
| 1412 | * head. The caller is responsible for ensuring there is enough room left before |
| 1413 | * the buffer's head for the string. |
| 1414 | */ |
| 1415 | static void h1_emit_chunk_size(struct buffer *buf, size_t chksz) |
| 1416 | { |
| 1417 | char *beg, *end; |
| 1418 | |
| 1419 | beg = end = b_head(buf); |
| 1420 | *--beg = '\n'; |
| 1421 | *--beg = '\r'; |
| 1422 | do { |
| 1423 | *--beg = hextab[chksz & 0xF]; |
| 1424 | } while (chksz >>= 4); |
| 1425 | buf->head -= (end - beg); |
| 1426 | b_add(buf, end - beg); |
| 1427 | } |
| 1428 | |
| 1429 | /* Emit a CRLF after the data of the buffer <buf>. The caller is responsible for |
| 1430 | * ensuring there is enough room left in the buffer for the string. */ |
| 1431 | static void h1_emit_chunk_crlf(struct buffer *buf) |
| 1432 | { |
| 1433 | *(b_peek(buf, b_data(buf))) = '\r'; |
| 1434 | *(b_peek(buf, b_data(buf) + 1)) = '\n'; |
| 1435 | b_add(buf, 2); |
| 1436 | } |
| 1437 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1438 | /* |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1439 | * Switch the stream to tunnel mode. This function must only be called on 2xx |
| 1440 | * (successful) replies to CONNECT requests or on 101 (switching protocol). |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1441 | */ |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1442 | static void h1_set_tunnel_mode(struct h1s *h1s) |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1443 | { |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1444 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1445 | |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1446 | h1s->req.state = H1_MSG_TUNNEL; |
| 1447 | h1s->req.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK); |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1448 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1449 | h1s->res.state = H1_MSG_TUNNEL; |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1450 | h1s->res.flags &= ~(H1_MF_XFER_LEN|H1_MF_CLEN|H1_MF_CHNK); |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 1451 | |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1452 | TRACE_STATE("switch H1 stream in tunnel mode", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s); |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 1453 | |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1454 | if (h1s->flags & H1S_F_RX_BLK) { |
| 1455 | h1s->flags &= ~H1S_F_RX_BLK; |
Christopher Faulet | 02c92c3 | 2021-04-09 12:31:48 +0200 | [diff] [blame] | 1456 | h1_wake_stream_for_recv(h1s); |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1457 | TRACE_STATE("Re-enable input processing", H1_EV_RX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1458 | } |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1459 | if (h1s->flags & H1S_F_TX_BLK) { |
| 1460 | h1s->flags &= ~H1S_F_TX_BLK; |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1461 | h1_wake_stream_for_send(h1s); |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1462 | TRACE_STATE("Re-enable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s); |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 1463 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1464 | } |
| 1465 | |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1466 | /* Search for a websocket key header. The message should have been identified |
| 1467 | * as a valid websocket handshake. |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 1468 | * |
| 1469 | * On the request side, if found the key is stored in the session. It might be |
| 1470 | * needed to calculate response key if the server side is using http/2. |
| 1471 | * |
Amaury Denoyelle | aad333a | 2020-12-11 17:53:07 +0100 | [diff] [blame] | 1472 | * On the response side, the key might be verified if haproxy has been |
| 1473 | * responsible for the generation of a key. This happens when a h2 client is |
| 1474 | * interfaced with a h1 server. |
| 1475 | * |
| 1476 | * Returns 0 if no key found or invalid key |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1477 | */ |
| 1478 | static int h1_search_websocket_key(struct h1s *h1s, struct h1m *h1m, struct htx *htx) |
| 1479 | { |
| 1480 | struct htx_blk *blk; |
| 1481 | enum htx_blk_type type; |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 1482 | struct ist n, v; |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1483 | int ws_key_found = 0, idx; |
| 1484 | |
| 1485 | idx = htx_get_head(htx); // returns the SL that we skip |
| 1486 | while ((idx = htx_get_next(htx, idx)) != -1) { |
| 1487 | blk = htx_get_blk(htx, idx); |
| 1488 | type = htx_get_blk_type(blk); |
| 1489 | |
| 1490 | if (type == HTX_BLK_UNUSED) |
| 1491 | continue; |
| 1492 | |
| 1493 | if (type != HTX_BLK_HDR) |
| 1494 | break; |
| 1495 | |
| 1496 | n = htx_get_blk_name(htx, blk); |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 1497 | v = htx_get_blk_value(htx, blk); |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1498 | |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 1499 | /* Websocket key is base64 encoded of 16 bytes */ |
| 1500 | if (isteqi(n, ist("sec-websocket-key")) && v.len == 24 && |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1501 | !(h1m->flags & H1_MF_RESP)) { |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 1502 | /* Copy the key on request side |
| 1503 | * we might need it if the server is using h2 and does |
| 1504 | * not provide the response |
| 1505 | */ |
| 1506 | memcpy(h1s->ws_key, v.ptr, 24); |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1507 | ws_key_found = 1; |
| 1508 | break; |
| 1509 | } |
| 1510 | else if (isteqi(n, ist("sec-websocket-accept")) && |
| 1511 | h1m->flags & H1_MF_RESP) { |
Amaury Denoyelle | aad333a | 2020-12-11 17:53:07 +0100 | [diff] [blame] | 1512 | /* Need to verify the response key if the input was |
| 1513 | * generated by haproxy |
| 1514 | */ |
| 1515 | if (h1s->ws_key[0]) { |
| 1516 | char key[29]; |
| 1517 | h1_calculate_ws_output_key(h1s->ws_key, key); |
| 1518 | if (!isteqi(ist(key), v)) |
| 1519 | break; |
| 1520 | } |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1521 | ws_key_found = 1; |
| 1522 | break; |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | /* missing websocket key, reject the message */ |
| 1527 | if (!ws_key_found) { |
| 1528 | htx->flags |= HTX_FL_PARSING_ERROR; |
| 1529 | return 0; |
| 1530 | } |
| 1531 | |
| 1532 | return 1; |
| 1533 | } |
| 1534 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1535 | /* |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1536 | * Parse HTTP/1 headers. It returns the number of bytes parsed if > 0, or 0 if |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1537 | * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1538 | * flag. If more room is requested, H1S_F_RX_CONGESTED flag is set. If relies on |
| 1539 | * the function http_parse_msg_hdrs() to do the parsing. |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1540 | */ |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 1541 | static size_t h1_handle_headers(struct h1s *h1s, struct h1m *h1m, struct htx *htx, |
| 1542 | struct buffer *buf, size_t *ofs, size_t max) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1543 | { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1544 | union h1_sl h1sl; |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1545 | int ret = 0; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1546 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1547 | TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s, 0, (size_t[]){max}); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1548 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1549 | if (h1s->meth == HTTP_METH_CONNECT) |
| 1550 | h1m->flags |= H1_MF_METH_CONNECT; |
| 1551 | if (h1s->meth == HTTP_METH_HEAD) |
| 1552 | h1m->flags |= H1_MF_METH_HEAD; |
Christopher Faulet | 0ef372a | 2019-04-08 10:57:20 +0200 | [diff] [blame] | 1553 | |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1554 | ret = h1_parse_msg_hdrs(h1m, &h1sl, htx, buf, *ofs, max); |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1555 | if (ret <= 0) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1556 | TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s); |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1557 | if (ret == -1) { |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1558 | h1s->flags |= H1S_F_PARSING_ERROR; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 1559 | TRACE_ERROR("parsing error, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1560 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1561 | } |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1562 | else if (ret == -2) { |
| 1563 | TRACE_STATE("RX path congested, waiting for more space", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_BLK, h1s->h1c->conn, h1s); |
| 1564 | h1s->flags |= H1S_F_RX_CONGESTED; |
| 1565 | } |
| 1566 | ret = 0; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1567 | goto end; |
| 1568 | } |
| 1569 | |
Christopher Faulet | e136bd1 | 2021-09-21 18:44:55 +0200 | [diff] [blame] | 1570 | |
Christopher Faulet | 0f9c0f5 | 2022-05-13 09:20:13 +0200 | [diff] [blame] | 1571 | /* Reject HTTP/1.0 GET/HEAD/DELETE requests with a payload except if |
| 1572 | * accept_payload_with_any_method global option is set. |
| 1573 | *There is a payload if the c-l is not null or the the payload is |
| 1574 | * chunk-encoded. A parsing error is reported but a A |
| 1575 | * 413-Payload-Too-Large is returned instead of a 400-Bad-Request. |
Christopher Faulet | e136bd1 | 2021-09-21 18:44:55 +0200 | [diff] [blame] | 1576 | */ |
Christopher Faulet | 0f9c0f5 | 2022-05-13 09:20:13 +0200 | [diff] [blame] | 1577 | if (!accept_payload_with_any_method && |
| 1578 | !(h1m->flags & (H1_MF_RESP|H1_MF_VER_11)) && |
Christopher Faulet | e136bd1 | 2021-09-21 18:44:55 +0200 | [diff] [blame] | 1579 | (((h1m->flags & H1_MF_CLEN) && h1m->body_len) || (h1m->flags & H1_MF_CHNK)) && |
| 1580 | (h1sl.rq.meth == HTTP_METH_GET || h1sl.rq.meth == HTTP_METH_HEAD || h1sl.rq.meth == HTTP_METH_DELETE)) { |
| 1581 | h1s->flags |= H1S_F_PARSING_ERROR; |
| 1582 | htx->flags |= HTX_FL_PARSING_ERROR; |
| 1583 | h1s->h1c->errcode = 413; |
| 1584 | TRACE_ERROR("HTTP/1.0 GET/HEAD/DELETE request with a payload forbidden", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s); |
| 1585 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1586 | ret = 0; |
| 1587 | goto end; |
| 1588 | } |
| 1589 | |
Christopher Faulet | da3adeb | 2021-09-28 09:50:07 +0200 | [diff] [blame] | 1590 | /* Reject any message with an unknown transfer-encoding. In fact if any |
| 1591 | * encoding other than "chunked". A 422-Unprocessable-Content is |
| 1592 | * returned for an invalid request, a 502-Bad-Gateway for an invalid |
| 1593 | * response. |
| 1594 | */ |
| 1595 | if (h1m->flags & H1_MF_TE_OTHER) { |
| 1596 | h1s->flags |= H1S_F_PARSING_ERROR; |
| 1597 | htx->flags |= HTX_FL_PARSING_ERROR; |
| 1598 | if (!(h1m->flags & H1_MF_RESP)) |
| 1599 | h1s->h1c->errcode = 422; |
| 1600 | TRACE_ERROR("Unknown transfer-encoding", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s); |
| 1601 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1602 | ret = 0; |
| 1603 | goto end; |
| 1604 | } |
| 1605 | |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1606 | /* If websocket handshake, search for the websocket key */ |
| 1607 | if ((h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) == |
| 1608 | (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) { |
| 1609 | int ws_ret = h1_search_websocket_key(h1s, h1m, htx); |
| 1610 | if (!ws_ret) { |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1611 | h1s->flags |= H1S_F_PARSING_ERROR; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 1612 | TRACE_ERROR("missing/invalid websocket key, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_HDRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s); |
Amaury Denoyelle | 18ee5c3 | 2020-12-11 17:53:02 +0100 | [diff] [blame] | 1613 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1614 | |
| 1615 | ret = 0; |
| 1616 | goto end; |
| 1617 | } |
| 1618 | } |
| 1619 | |
Christopher Faulet | 486498c | 2019-10-11 14:22:00 +0200 | [diff] [blame] | 1620 | if (h1m->err_pos >= 0) { |
| 1621 | /* Maybe we found an error during the parsing while we were |
| 1622 | * configured not to block on that, so we have to capture it |
| 1623 | * now. |
| 1624 | */ |
| 1625 | TRACE_STATE("Ignored parsing error", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s); |
| 1626 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1627 | } |
| 1628 | |
Christopher Faulet | 5696f54 | 2020-12-02 16:08:38 +0100 | [diff] [blame] | 1629 | if (!(h1m->flags & H1_MF_RESP)) { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1630 | h1s->meth = h1sl.rq.meth; |
Christopher Faulet | 5696f54 | 2020-12-02 16:08:38 +0100 | [diff] [blame] | 1631 | if (h1s->meth == HTTP_METH_HEAD) |
| 1632 | h1s->flags |= H1S_F_BODYLESS_RESP; |
| 1633 | } |
| 1634 | else { |
Christopher Faulet | f1ba18d | 2018-11-26 21:37:08 +0100 | [diff] [blame] | 1635 | h1s->status = h1sl.st.status; |
Christopher Faulet | 5696f54 | 2020-12-02 16:08:38 +0100 | [diff] [blame] | 1636 | if (h1s->status == 204 || h1s->status == 304) |
| 1637 | h1s->flags |= H1S_F_BODYLESS_RESP; |
| 1638 | } |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 1639 | h1_process_input_conn_mode(h1s, h1m, htx); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1640 | *ofs += ret; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1641 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1642 | end: |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1643 | TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_HDRS, h1s->h1c->conn, h1s, 0, (size_t[]){ret}); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1644 | return ret; |
| 1645 | } |
| 1646 | |
| 1647 | /* |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1648 | * Parse HTTP/1 body. It returns the number of bytes parsed if > 0, or 0 if it |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1649 | * couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR flag. |
| 1650 | * If relies on the function http_parse_msg_data() to do the parsing. |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1651 | */ |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 1652 | static size_t h1_handle_data(struct h1s *h1s, struct h1m *h1m, struct htx **htx, |
| 1653 | struct buffer *buf, size_t *ofs, size_t max, |
| 1654 | struct buffer *htxbuf) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1655 | { |
Christopher Faulet | de471a4 | 2021-02-01 16:37:28 +0100 | [diff] [blame] | 1656 | size_t ret; |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1657 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1658 | TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s, 0, (size_t[]){max}); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1659 | ret = h1_parse_msg_data(h1m, htx, buf, *ofs, max, htxbuf); |
Christopher Faulet | 02a0253 | 2019-11-15 09:36:28 +0100 | [diff] [blame] | 1660 | if (!ret) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1661 | TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s); |
Christopher Faulet | 02a0253 | 2019-11-15 09:36:28 +0100 | [diff] [blame] | 1662 | if ((*htx)->flags & HTX_FL_PARSING_ERROR) { |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1663 | h1s->flags |= H1S_F_PARSING_ERROR; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 1664 | TRACE_ERROR("parsing error, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_ERR, h1s->h1c->conn, h1s); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1665 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1666 | } |
Christopher Faulet | 02a0253 | 2019-11-15 09:36:28 +0100 | [diff] [blame] | 1667 | goto end; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1668 | } |
| 1669 | |
Christopher Faulet | 02a0253 | 2019-11-15 09:36:28 +0100 | [diff] [blame] | 1670 | *ofs += ret; |
| 1671 | |
| 1672 | end: |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1673 | if (b_data(buf) != *ofs && (h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL)) { |
| 1674 | TRACE_STATE("RX path congested, waiting for more space", H1_EV_RX_DATA|H1_EV_RX_BODY|H1_EV_H1S_BLK, h1s->h1c->conn, h1s); |
| 1675 | h1s->flags |= H1S_F_RX_CONGESTED; |
| 1676 | } |
| 1677 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1678 | TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s, 0, (size_t[]){ret}); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1679 | return ret; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | /* |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1683 | * Parse HTTP/1 trailers. It returns the number of bytes parsed if > 0, or 0 if |
| 1684 | * it couldn't proceed. Parsing errors are reported by setting H1S_F_*_ERROR |
| 1685 | * flag and filling h1s->err_pos and h1s->err_state fields. This functions is |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1686 | * responsible to update the parser state <h1m>. If more room is requested, |
| 1687 | * H1S_F_RX_CONGESTED flag is set. |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1688 | */ |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 1689 | static size_t h1_handle_trailers(struct h1s *h1s, struct h1m *h1m, struct htx *htx, |
| 1690 | struct buffer *buf, size_t *ofs, size_t max) |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1691 | { |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1692 | int ret; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1693 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1694 | TRACE_ENTER(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s, 0, (size_t[]){max}); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1695 | ret = h1_parse_msg_tlrs(h1m, htx, buf, *ofs, max); |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1696 | if (ret <= 0) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1697 | TRACE_DEVEL("leaving on missing data or error", H1_EV_RX_DATA|H1_EV_RX_BODY, h1s->h1c->conn, h1s); |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1698 | if (ret == -1) { |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1699 | h1s->flags |= H1S_F_PARSING_ERROR; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 1700 | TRACE_ERROR("parsing error, reject H1 message", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_ERR, h1s->h1c->conn, h1s); |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1701 | h1_capture_bad_message(h1s->h1c, h1s, h1m, buf); |
| 1702 | } |
Christopher Faulet | ae660be | 2022-04-13 17:48:54 +0200 | [diff] [blame] | 1703 | else if (ret == -2) { |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1704 | TRACE_STATE("RX path congested, waiting for more space", H1_EV_RX_DATA|H1_EV_RX_TLRS|H1_EV_H1S_BLK, h1s->h1c->conn, h1s); |
| 1705 | h1s->flags |= H1S_F_RX_CONGESTED; |
| 1706 | } |
| 1707 | ret = 0; |
Christopher Faulet | 02a0253 | 2019-11-15 09:36:28 +0100 | [diff] [blame] | 1708 | goto end; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1709 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1710 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1711 | *ofs += ret; |
Christopher Faulet | 02a0253 | 2019-11-15 09:36:28 +0100 | [diff] [blame] | 1712 | |
| 1713 | end: |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 1714 | TRACE_LEAVE(H1_EV_RX_DATA|H1_EV_RX_TLRS, h1s->h1c->conn, h1s, 0, (size_t[]){ret}); |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1715 | return ret; |
Christopher Faulet | 4f0f88a | 2019-08-10 11:17:44 +0200 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | /* |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1719 | * Process incoming data. It parses data and transfer them from h1c->ibuf into |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1720 | * <buf>. It returns the number of bytes parsed and transferred if > 0, or 0 if |
| 1721 | * it couldn't proceed. |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1722 | * |
| 1723 | * WARNING: H1S_F_RX_CONGESTED flag must be removed before processing input data. |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1724 | */ |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 1725 | static size_t h1_process_demux(struct h1c *h1c, struct buffer *buf, size_t count) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1726 | { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1727 | struct h1s *h1s = h1c->h1s; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1728 | struct h1m *h1m; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1729 | struct htx *htx; |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1730 | size_t data; |
| 1731 | size_t ret = 0; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1732 | size_t total = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1733 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1734 | htx = htx_from_buf(buf); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1735 | TRACE_ENTER(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){count}); |
Willy Tarreau | 3a6190f | 2018-12-16 08:29:56 +0100 | [diff] [blame] | 1736 | |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1737 | h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res); |
Christopher Faulet | 7402776 | 2019-02-26 14:45:05 +0100 | [diff] [blame] | 1738 | data = htx->data; |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1739 | |
Christopher Faulet | bef8900 | 2022-10-05 07:50:19 +0200 | [diff] [blame] | 1740 | if (h1s->flags & (H1S_F_INTERNAL_ERROR|H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) |
Christopher Faulet | 0e54d54 | 2019-07-04 21:22:34 +0200 | [diff] [blame] | 1741 | goto end; |
| 1742 | |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1743 | if (h1s->flags & H1S_F_RX_BLK) |
Christopher Faulet | ec4207c | 2021-04-08 18:34:30 +0200 | [diff] [blame] | 1744 | goto out; |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1745 | |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1746 | /* Always remove congestion flags and try to process more input data */ |
| 1747 | h1s->flags &= ~H1S_F_RX_CONGESTED; |
| 1748 | |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 1749 | do { |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1750 | size_t used = htx_used_space(htx); |
| 1751 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1752 | if (h1m->state <= H1_MSG_LAST_LF) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1753 | TRACE_PROTO("parsing message headers", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s); |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 1754 | ret = h1_handle_headers(h1s, h1m, htx, &h1c->ibuf, &total, count); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1755 | if (!ret) |
| 1756 | break; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1757 | |
| 1758 | TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request headers" : "rcvd H1 response headers"), |
| 1759 | H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s, htx, (size_t[]){ret}); |
| 1760 | |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1761 | if ((h1m->flags & H1_MF_RESP) && |
| 1762 | h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) { |
| 1763 | h1m_init_res(&h1s->res); |
| 1764 | h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1765 | TRACE_STATE("1xx response rcvd", H1_EV_RX_DATA|H1_EV_RX_HDRS, h1c->conn, h1s); |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 1766 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1767 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1768 | else if (h1m->state < H1_MSG_TRAILERS) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1769 | TRACE_PROTO("parsing message payload", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s); |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 1770 | ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf); |
Christopher Faulet | 16a524c | 2021-02-02 21:16:03 +0100 | [diff] [blame] | 1771 | if (h1m->state < H1_MSG_TRAILERS) |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1772 | break; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1773 | |
| 1774 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request payload data" : "rcvd H1 response payload data"), |
| 1775 | H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s, htx, (size_t[]){ret}); |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1776 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1777 | else if (h1m->state == H1_MSG_TRAILERS) { |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1778 | TRACE_PROTO("parsing message trailers", H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s); |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 1779 | ret = h1_handle_trailers(h1s, h1m, htx, &h1c->ibuf, &total, count); |
Christopher Faulet | 16a524c | 2021-02-02 21:16:03 +0100 | [diff] [blame] | 1780 | if (h1m->state != H1_MSG_DONE) |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1781 | break; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1782 | |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1783 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request trailers" : "rcvd H1 response trailers"), |
| 1784 | H1_EV_RX_DATA|H1_EV_RX_TLRS, h1c->conn, h1s, htx, (size_t[]){ret}); |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 1785 | } |
Christopher Faulet | cb55f48 | 2018-12-10 11:56:47 +0100 | [diff] [blame] | 1786 | else if (h1m->state == H1_MSG_DONE) { |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 1787 | TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully rcvd" : "H1 response fully rcvd"), |
| 1788 | H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx); |
Christopher Faulet | 76014fd | 2019-12-10 11:47:22 +0100 | [diff] [blame] | 1789 | |
Christopher Faulet | f4b89f1 | 2023-02-23 13:58:13 +0100 | [diff] [blame] | 1790 | if (!(h1c->flags & H1C_F_IS_BACK)) { |
| 1791 | /* The request was fully received. It means the H1S now |
| 1792 | * expect data from the opposite side |
| 1793 | */ |
| 1794 | se_expect_data(h1s->sd); |
| 1795 | } |
| 1796 | |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1797 | if ((h1m->flags & H1_MF_RESP) && |
| 1798 | ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) |
| 1799 | h1_set_tunnel_mode(h1s); |
Christopher Faulet | b385b50 | 2021-01-13 18:47:57 +0100 | [diff] [blame] | 1800 | else { |
| 1801 | if (h1s->req.state < H1_MSG_DONE || h1s->res.state < H1_MSG_DONE) { |
| 1802 | /* Unfinished transaction: block this input side waiting the end of the output side */ |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1803 | h1s->flags |= H1S_F_RX_BLK; |
| 1804 | TRACE_STATE("Disable input processing", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s); |
Christopher Faulet | b385b50 | 2021-01-13 18:47:57 +0100 | [diff] [blame] | 1805 | } |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1806 | if (h1s->flags & H1S_F_TX_BLK) { |
| 1807 | h1s->flags &= ~H1S_F_TX_BLK; |
Christopher Faulet | 02c92c3 | 2021-04-09 12:31:48 +0200 | [diff] [blame] | 1808 | h1_wake_stream_for_send(h1s); |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1809 | TRACE_STATE("Re-enable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s); |
Christopher Faulet | b385b50 | 2021-01-13 18:47:57 +0100 | [diff] [blame] | 1810 | } |
Christopher Faulet | 23021ad | 2020-07-10 10:01:26 +0200 | [diff] [blame] | 1811 | break; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1812 | } |
Christopher Faulet | cb55f48 | 2018-12-10 11:56:47 +0100 | [diff] [blame] | 1813 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1814 | else if (h1m->state == H1_MSG_TUNNEL) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1815 | TRACE_PROTO("parsing tunneled data", H1_EV_RX_DATA, h1c->conn, h1s); |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 1816 | ret = h1_handle_data(h1s, h1m, &htx, &h1c->ibuf, &total, count, buf); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1817 | if (!ret) |
| 1818 | break; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1819 | |
| 1820 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "rcvd H1 request tunneled data" : "rcvd H1 response tunneled data"), |
| 1821 | H1_EV_RX_DATA|H1_EV_RX_EOI, h1c->conn, h1s, htx, (size_t[]){ret}); |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 1822 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1823 | else { |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1824 | h1s->flags |= H1S_F_PARSING_ERROR; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1825 | break; |
| 1826 | } |
| 1827 | |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1828 | count -= htx_used_space(htx) - used; |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1829 | } while (!(h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR|H1S_F_RX_BLK|H1S_F_RX_CONGESTED))); |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1830 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1831 | |
Christopher Faulet | 2eed800 | 2020-12-07 11:26:13 +0100 | [diff] [blame] | 1832 | if (h1s->flags & (H1S_F_PARSING_ERROR|H1S_F_NOT_IMPL_ERROR)) { |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 1833 | TRACE_ERROR("parsing or not-implemented error", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1834 | goto err; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1835 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1836 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1837 | b_del(&h1c->ibuf, total); |
| 1838 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1839 | TRACE_DEVEL("incoming data parsed", H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret}); |
| 1840 | |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1841 | ret = htx->data - data; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1842 | if ((h1c->flags & H1C_F_IN_FULL) && buf_room_for_htx_data(&h1c->ibuf)) { |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1843 | h1c->flags &= ~H1C_F_IN_FULL; |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1844 | TRACE_STATE("h1c ibuf not full anymore", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s); |
Willy Tarreau | 2c1f37d | 2020-03-04 17:50:02 +0100 | [diff] [blame] | 1845 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1846 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1847 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1848 | if (!b_data(&h1c->ibuf)) |
| 1849 | h1_release_buf(h1c, &h1c->ibuf); |
| 1850 | |
Christopher Faulet | 2177d96 | 2022-10-05 08:39:14 +0200 | [diff] [blame] | 1851 | if (h1m->state <= H1_MSG_LAST_LF) |
| 1852 | goto out; |
| 1853 | |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 1854 | if (h1c->state < H1_CS_RUNNING) { |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 1855 | /* The H1 connection is not ready. Most of time, there is no SC |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 1856 | * attached, except for TCP>H1 upgrade, from a TCP frontend. In both |
| 1857 | * cases, it is only possible on the client side. |
| 1858 | */ |
| 1859 | BUG_ON(h1c->flags & H1C_F_IS_BACK); |
| 1860 | |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 1861 | if (h1c->state == H1_CS_EMBRYONIC) { |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 1862 | TRACE_DEVEL("request headers fully parsed, create and attach the SC", H1_EV_RX_DATA, h1c->conn, h1s); |
Willy Tarreau | 97b4d3b | 2022-05-18 07:27:14 +0200 | [diff] [blame] | 1863 | BUG_ON(h1s_sc(h1s)); |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 1864 | if (!h1s_new_sc(h1s, buf)) { |
Christopher Faulet | bef8900 | 2022-10-05 07:50:19 +0200 | [diff] [blame] | 1865 | h1s->flags |= H1S_F_INTERNAL_ERROR; |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 1866 | goto err; |
| 1867 | } |
| 1868 | } |
| 1869 | else { |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 1870 | TRACE_DEVEL("request headers fully parsed, upgrade the inherited SC", H1_EV_RX_DATA, h1c->conn, h1s); |
Willy Tarreau | 97b4d3b | 2022-05-18 07:27:14 +0200 | [diff] [blame] | 1871 | BUG_ON(h1s_sc(h1s) == NULL); |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 1872 | if (!h1s_upgrade_sc(h1s, buf)) { |
Christopher Faulet | bef8900 | 2022-10-05 07:50:19 +0200 | [diff] [blame] | 1873 | h1s->flags |= H1S_F_INTERNAL_ERROR; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 1874 | TRACE_ERROR("H1S upgrade failure", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s); |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 1875 | goto err; |
| 1876 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1877 | } |
| 1878 | } |
| 1879 | |
Willy Tarreau | 97b4d3b | 2022-05-18 07:27:14 +0200 | [diff] [blame] | 1880 | /* Here h1s_sc(h1s) is always defined */ |
Christopher Faulet | f5ce320 | 2021-11-26 17:26:19 +0100 | [diff] [blame] | 1881 | if (!(h1m->flags & H1_MF_CHNK) && (h1m->state == H1_MSG_DATA || (h1m->state == H1_MSG_TUNNEL))) { |
Christopher Faulet | a583af6 | 2020-09-24 15:35:37 +0200 | [diff] [blame] | 1882 | TRACE_STATE("notify the mux can use splicing", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s); |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 1883 | se_fl_set(h1s->sd, SE_FL_MAY_SPLICE); |
Christopher Faulet | a583af6 | 2020-09-24 15:35:37 +0200 | [diff] [blame] | 1884 | } |
| 1885 | else { |
| 1886 | TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_RX_DATA|H1_EV_RX_BODY, h1c->conn, h1s); |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 1887 | se_fl_clr(h1s->sd, SE_FL_MAY_SPLICE); |
Christopher Faulet | a583af6 | 2020-09-24 15:35:37 +0200 | [diff] [blame] | 1888 | } |
| 1889 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1890 | /* Set EOI on stream connector in DONE state iff: |
Christopher Faulet | a22782b | 2021-02-08 17:18:01 +0100 | [diff] [blame] | 1891 | * - it is a response |
| 1892 | * - it is a request but no a protocol upgrade nor a CONNECT |
| 1893 | * |
| 1894 | * If not set, Wait the response to do so or not depending on the status |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 1895 | * code. |
Christopher Faulet | 3e1748b | 2020-12-07 18:21:27 +0100 | [diff] [blame] | 1896 | */ |
Christopher Faulet | a22782b | 2021-02-08 17:18:01 +0100 | [diff] [blame] | 1897 | if (((h1m->state == H1_MSG_DONE) && (h1m->flags & H1_MF_RESP)) || |
| 1898 | ((h1m->state == H1_MSG_DONE) && (h1s->meth != HTTP_METH_CONNECT) && !(h1m->flags & H1_MF_CONN_UPG))) |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 1899 | se_fl_set(h1s->sd, SE_FL_EOI); |
Christopher Faulet | a583af6 | 2020-09-24 15:35:37 +0200 | [diff] [blame] | 1900 | |
Christopher Faulet | ec4207c | 2021-04-08 18:34:30 +0200 | [diff] [blame] | 1901 | out: |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1902 | /* When Input data are pending for this message, notify upper layer that |
| 1903 | * the mux need more space in the HTX buffer to continue if : |
| 1904 | * |
| 1905 | * - The parser is blocked in MSG_DATA or MSG_TUNNEL state |
| 1906 | * - Headers or trailers are pending to be copied. |
| 1907 | */ |
| 1908 | if (h1s->flags & (H1S_F_RX_CONGESTED)) { |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 1909 | se_fl_set(h1s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM); |
Christopher Faulet | 46e058d | 2021-09-20 07:47:27 +0200 | [diff] [blame] | 1910 | TRACE_STATE("waiting for more room", H1_EV_RX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s); |
| 1911 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1912 | else { |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 1913 | se_fl_clr(h1s->sd, SE_FL_RCV_MORE | SE_FL_WANT_ROOM); |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 1914 | if (h1c->flags & H1C_F_EOS) { |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 1915 | se_fl_set(h1s->sd, SE_FL_EOS); |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 1916 | TRACE_STATE("report EOS to SE", H1_EV_RX_DATA, h1c->conn, h1s); |
Christopher Faulet | 1049856 | 2022-10-10 18:05:25 +0200 | [diff] [blame] | 1917 | if (h1m->state >= H1_MSG_DONE || (h1m->state > H1_MSG_LAST_LF && !(h1m->flags & H1_MF_XFER_LEN))) { |
Christopher Faulet | 1e85778 | 2020-12-08 10:38:22 +0100 | [diff] [blame] | 1918 | /* DONE or TUNNEL or SHUTR without XFER_LEN, set |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 1919 | * EOI on the stream connector */ |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 1920 | se_fl_set(h1s->sd, SE_FL_EOI); |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 1921 | TRACE_STATE("report EOI to SE", H1_EV_RX_DATA, h1c->conn, h1s); |
Christopher Faulet | 3e1748b | 2020-12-07 18:21:27 +0100 | [diff] [blame] | 1922 | } |
Christopher Faulet | 91ff709 | 2023-03-01 16:04:23 +0100 | [diff] [blame] | 1923 | else if (h1m->state < H1_MSG_DONE) { |
Christopher Faulet | e9bacf6 | 2023-03-29 10:23:21 +0200 | [diff] [blame] | 1924 | if (h1m->state <= H1_MSG_LAST_LF && b_data(&h1c->ibuf)) |
Christopher Faulet | 91ff709 | 2023-03-01 16:04:23 +0100 | [diff] [blame] | 1925 | htx->flags |= HTX_FL_PARSING_ERROR; |
Christopher Faulet | e9bacf6 | 2023-03-29 10:23:21 +0200 | [diff] [blame] | 1926 | se_fl_set(h1s->sd, SE_FL_ERROR); |
| 1927 | TRACE_ERROR("message aborted, set error on SC", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s); |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 1928 | } |
Christopher Faulet | b385b50 | 2021-01-13 18:47:57 +0100 | [diff] [blame] | 1929 | |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1930 | if (h1s->flags & H1S_F_TX_BLK) { |
| 1931 | h1s->flags &= ~H1S_F_TX_BLK; |
Christopher Faulet | 02c92c3 | 2021-04-09 12:31:48 +0200 | [diff] [blame] | 1932 | h1_wake_stream_for_send(h1s); |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 1933 | TRACE_STATE("Re-enable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s); |
Christopher Faulet | b385b50 | 2021-01-13 18:47:57 +0100 | [diff] [blame] | 1934 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1935 | } |
Christopher Faulet | a4bd760 | 2023-03-15 19:10:55 +0100 | [diff] [blame] | 1936 | if (h1c->flags & H1C_F_ERROR) { |
| 1937 | /* Report a terminal error to the SE if a previous read error was detected */ |
| 1938 | se_fl_set(h1s->sd, SE_FL_ERROR); |
| 1939 | TRACE_STATE("report ERROR to SE", H1_EV_RX_DATA|H1_EV_H1S_ERR, h1c->conn, h1s); |
| 1940 | } |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 1941 | } |
Christopher Faulet | f6ce9d6 | 2018-12-10 15:30:06 +0100 | [diff] [blame] | 1942 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1943 | end: |
Christopher Faulet | 5966e40 | 2022-07-08 09:02:59 +0200 | [diff] [blame] | 1944 | htx_to_buf(htx, buf); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1945 | TRACE_LEAVE(H1_EV_RX_DATA, h1c->conn, h1s, htx, (size_t[]){ret}); |
Christopher Faulet | 30db3d7 | 2019-05-17 15:35:33 +0200 | [diff] [blame] | 1946 | return ret; |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 1947 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 1948 | err: |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 1949 | htx_to_buf(htx, buf); |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 1950 | se_fl_set(h1s->sd, SE_FL_EOI); |
Christopher Faulet | 75028f8 | 2022-12-16 10:43:11 +0100 | [diff] [blame] | 1951 | if (h1c->state < H1_CS_RUNNING) { |
| 1952 | h1c->flags |= H1C_F_EOS; |
| 1953 | se_fl_set(h1s->sd, SE_FL_EOS); |
| 1954 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1955 | TRACE_DEVEL("leaving on error", H1_EV_RX_DATA|H1_EV_STRM_ERR, h1c->conn, h1s); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1956 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1957 | } |
| 1958 | |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1959 | /* |
| 1960 | * Process outgoing data. It parses data and transfer them from the channel buffer into |
| 1961 | * h1c->obuf. It returns the number of bytes parsed and transferred if > 0, or |
| 1962 | * 0 if it couldn't proceed. |
| 1963 | */ |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 1964 | static size_t h1_process_mux(struct h1c *h1c, struct buffer *buf, size_t count) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1965 | { |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1966 | struct h1s *h1s = h1c->h1s; |
| 1967 | struct h1m *h1m; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1968 | struct htx *chn_htx = NULL; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1969 | struct htx_blk *blk; |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 1970 | struct buffer tmp; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 1971 | size_t total = 0; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 1972 | int last_data = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1973 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 1974 | chn_htx = htxbuf(buf); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1975 | TRACE_ENTER(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){count}); |
| 1976 | |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 1977 | if (htx_is_empty(chn_htx)) |
| 1978 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1979 | |
Christopher Faulet | bef8900 | 2022-10-05 07:50:19 +0200 | [diff] [blame] | 1980 | if (h1s->flags & (H1S_F_INTERNAL_ERROR|H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK)) |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 1981 | goto end; |
| 1982 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1983 | if (!h1_get_buf(h1c, &h1c->obuf)) { |
| 1984 | h1c->flags |= H1C_F_OUT_ALLOC; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 1985 | TRACE_STATE("waiting for h1c obuf allocation", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1986 | goto end; |
| 1987 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 1988 | |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 1989 | h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1990 | |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 1991 | /* the htx is non-empty thus has at least one block */ |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1992 | blk = htx_get_head_blk(chn_htx); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 1993 | |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 1994 | /* Perform some optimizations to reduce the number of buffer copies. |
| 1995 | * First, if the mux's buffer is empty and the htx area contains |
| 1996 | * exactly one data block of the same size as the requested count, |
| 1997 | * then it's possible to simply swap the caller's buffer with the |
| 1998 | * mux's output buffer and adjust offsets and length to match the |
| 1999 | * entire DATA HTX block in the middle. In this case we perform a |
| 2000 | * true zero-copy operation from end-to-end. This is the situation |
| 2001 | * that happens all the time with large files. Second, if this is not |
| 2002 | * possible, but the mux's output buffer is empty, we still have an |
| 2003 | * opportunity to avoid the copy to the intermediary buffer, by making |
| 2004 | * the intermediary buffer's area point to the output buffer's area. |
| 2005 | * In this case we want to skip the HTX header to make sure that copies |
| 2006 | * remain aligned and that this operation remains possible all the |
| 2007 | * time. This goes for headers, data blocks and any data extracted from |
| 2008 | * the HTX blocks. |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 2009 | */ |
| 2010 | if (!b_data(&h1c->obuf)) { |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2011 | if ((h1m->state == H1_MSG_DATA || h1m->state == H1_MSG_TUNNEL) && |
Christopher Faulet | 0d7e634 | 2021-02-08 15:56:36 +0100 | [diff] [blame] | 2012 | (!(h1m->flags & H1_MF_RESP) || !(h1s->flags & H1S_F_BODYLESS_RESP)) && |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2013 | htx_nbblks(chn_htx) == 1 && |
Willy Tarreau | 37dd54d | 2018-12-15 14:48:31 +0100 | [diff] [blame] | 2014 | htx_get_blk_type(blk) == HTX_BLK_DATA && |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 2015 | htx_get_blk_value(chn_htx, blk).len == count) { |
Christopher Faulet | e5596bf | 2020-12-02 16:13:22 +0100 | [diff] [blame] | 2016 | void *old_area; |
| 2017 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2018 | TRACE_PROTO("sending message data (zero-copy)", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx, (size_t[]){count}); |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 2019 | if (h1m->state == H1_MSG_DATA) { |
| 2020 | if (h1m->flags & H1_MF_CLEN) { |
| 2021 | if (count > h1m->curr_len) { |
| 2022 | TRACE_ERROR("too much payload, more than announced", |
| 2023 | H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); |
| 2024 | goto error; |
| 2025 | } |
| 2026 | h1m->curr_len -= count; |
Christopher Faulet | 2eb5243 | 2022-04-07 10:29:38 +0200 | [diff] [blame] | 2027 | if (!h1m->curr_len) |
| 2028 | last_data = 1; |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 2029 | } |
| 2030 | if (chn_htx->flags & HTX_FL_EOM) { |
| 2031 | TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s); |
| 2032 | last_data = 1; |
| 2033 | } |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2034 | } |
| 2035 | |
Christopher Faulet | e5596bf | 2020-12-02 16:13:22 +0100 | [diff] [blame] | 2036 | old_area = h1c->obuf.area; |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 2037 | h1c->obuf.area = buf->area; |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 2038 | h1c->obuf.head = sizeof(struct htx) + blk->addr; |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 2039 | h1c->obuf.data = count; |
| 2040 | |
| 2041 | buf->area = old_area; |
| 2042 | buf->data = buf->head = 0; |
Christopher Faulet | adb2220 | 2018-12-12 10:32:09 +0100 | [diff] [blame] | 2043 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2044 | chn_htx = (struct htx *)buf->area; |
| 2045 | htx_reset(chn_htx); |
| 2046 | |
Christopher Faulet | adb2220 | 2018-12-12 10:32:09 +0100 | [diff] [blame] | 2047 | /* The message is chunked. We need to emit the chunk |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2048 | * size and eventually the last chunk. We have at least |
| 2049 | * the size of the struct htx to write the chunk |
| 2050 | * envelope. It should be enough. |
Christopher Faulet | adb2220 | 2018-12-12 10:32:09 +0100 | [diff] [blame] | 2051 | */ |
| 2052 | if (h1m->flags & H1_MF_CHNK) { |
| 2053 | h1_emit_chunk_size(&h1c->obuf, count); |
| 2054 | h1_emit_chunk_crlf(&h1c->obuf); |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2055 | if (last_data) { |
| 2056 | /* Emit the last chunk too at the buffer's end */ |
| 2057 | b_putblk(&h1c->obuf, "0\r\n\r\n", 5); |
| 2058 | } |
Christopher Faulet | adb2220 | 2018-12-12 10:32:09 +0100 | [diff] [blame] | 2059 | } |
| 2060 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2061 | if (h1m->state == H1_MSG_DATA) |
| 2062 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"), |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2063 | H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count}); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2064 | else |
| 2065 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"), |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2066 | H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){count}); |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2067 | |
Christopher Faulet | e5596bf | 2020-12-02 16:13:22 +0100 | [diff] [blame] | 2068 | total += count; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2069 | if (last_data) { |
| 2070 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 2071 | if (h1s->flags & H1S_F_RX_BLK) { |
| 2072 | h1s->flags &= ~H1S_F_RX_BLK; |
Christopher Faulet | 02c92c3 | 2021-04-09 12:31:48 +0200 | [diff] [blame] | 2073 | h1_wake_stream_for_recv(h1s); |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 2074 | TRACE_STATE("Re-enable input processing", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s); |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"), |
| 2078 | H1_EV_TX_DATA, h1c->conn, h1s); |
| 2079 | } |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 2080 | goto out; |
| 2081 | } |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 2082 | tmp.area = h1c->obuf.area + h1c->obuf.head; |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 2083 | } |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 2084 | else |
| 2085 | tmp.area = trash.area; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2086 | |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 2087 | tmp.data = 0; |
| 2088 | tmp.size = b_room(&h1c->obuf); |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 2089 | while (count && !(h1s->flags & (H1S_F_PROCESSING_ERROR|H1S_F_TX_BLK)) && blk) { |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 2090 | struct htx_sl *sl; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2091 | struct ist n, v; |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 2092 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2093 | uint32_t sz = htx_get_blksz(blk); |
Christopher Faulet | d9233f0 | 2019-10-14 14:01:24 +0200 | [diff] [blame] | 2094 | uint32_t vlen, chklen; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2095 | |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 2096 | vlen = sz; |
Christopher Faulet | d9233f0 | 2019-10-14 14:01:24 +0200 | [diff] [blame] | 2097 | if (type != HTX_BLK_DATA && vlen > count) |
| 2098 | goto full; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2099 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2100 | if (type == HTX_BLK_UNUSED) |
| 2101 | goto nextblk; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2102 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2103 | switch (h1m->state) { |
| 2104 | case H1_MSG_RQBEFORE: |
| 2105 | if (type != HTX_BLK_REQ_SL) |
| 2106 | goto error; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2107 | TRACE_USER("sending request headers", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s, chn_htx); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2108 | sl = htx_get_blk_ptr(chn_htx, blk); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 2109 | h1s->meth = sl->info.req.meth; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2110 | h1_parse_req_vsn(h1m, sl); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 2111 | if (!h1_format_htx_reqline(sl, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2112 | goto full; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2113 | h1m->flags |= H1_MF_XFER_LEN; |
Christopher Faulet | 1f890dd | 2019-02-18 10:33:16 +0100 | [diff] [blame] | 2114 | if (sl->flags & HTX_SL_F_BODYLESS) |
| 2115 | h1m->flags |= H1_MF_CLEN; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2116 | h1m->state = H1_MSG_HDR_FIRST; |
Christopher Faulet | 5696f54 | 2020-12-02 16:08:38 +0100 | [diff] [blame] | 2117 | if (h1s->meth == HTTP_METH_HEAD) |
| 2118 | h1s->flags |= H1S_F_BODYLESS_RESP; |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 2119 | if (h1s->flags & H1S_F_RX_BLK) { |
| 2120 | h1s->flags &= ~H1S_F_RX_BLK; |
Christopher Faulet | 02c92c3 | 2021-04-09 12:31:48 +0200 | [diff] [blame] | 2121 | h1_wake_stream_for_recv(h1s); |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 2122 | TRACE_STATE("Re-enable input processing", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s); |
Christopher Faulet | 089acd5 | 2020-09-21 10:57:52 +0200 | [diff] [blame] | 2123 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 2124 | break; |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 2125 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2126 | case H1_MSG_RPBEFORE: |
| 2127 | if (type != HTX_BLK_RES_SL) |
| 2128 | goto error; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2129 | TRACE_USER("sending response headers", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s, chn_htx); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2130 | sl = htx_get_blk_ptr(chn_htx, blk); |
Christopher Faulet | 570d161 | 2018-11-26 11:13:57 +0100 | [diff] [blame] | 2131 | h1s->status = sl->info.res.status; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2132 | h1_parse_res_vsn(h1m, sl); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 2133 | if (!h1_format_htx_stline(sl, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2134 | goto full; |
Christopher Faulet | 0359911 | 2018-11-27 11:21:21 +0100 | [diff] [blame] | 2135 | if (sl->flags & HTX_SL_F_XFER_LEN) |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2136 | h1m->flags |= H1_MF_XFER_LEN; |
Christopher Faulet | f3e7619 | 2020-12-02 16:46:33 +0100 | [diff] [blame] | 2137 | if (h1s->status < 200) |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 2138 | h1s->flags |= H1S_F_HAVE_O_CONN; |
Christopher Faulet | 5696f54 | 2020-12-02 16:08:38 +0100 | [diff] [blame] | 2139 | else if (h1s->status == 204 || h1s->status == 304) |
| 2140 | h1s->flags |= H1S_F_BODYLESS_RESP; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2141 | h1m->state = H1_MSG_HDR_FIRST; |
| 2142 | break; |
| 2143 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2144 | case H1_MSG_HDR_FIRST: |
| 2145 | case H1_MSG_HDR_NAME: |
| 2146 | case H1_MSG_HDR_L2_LWS: |
| 2147 | if (type == HTX_BLK_EOH) |
| 2148 | goto last_lf; |
| 2149 | if (type != HTX_BLK_HDR) |
| 2150 | goto error; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2151 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2152 | h1m->state = H1_MSG_HDR_NAME; |
| 2153 | n = htx_get_blk_name(chn_htx, blk); |
| 2154 | v = htx_get_blk_value(chn_htx, blk); |
| 2155 | |
Christopher Faulet | 86d144c | 2019-08-14 16:32:25 +0200 | [diff] [blame] | 2156 | /* Skip all pseudo-headers */ |
| 2157 | if (*(n.ptr) == ':') |
| 2158 | goto skip_hdr; |
| 2159 | |
Christopher Faulet | 91fcf21 | 2020-12-02 16:17:15 +0100 | [diff] [blame] | 2160 | if (isteq(n, ist("transfer-encoding"))) { |
| 2161 | if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204)) |
| 2162 | goto skip_hdr; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2163 | h1_parse_xfer_enc_header(h1m, v); |
Christopher Faulet | 91fcf21 | 2020-12-02 16:17:15 +0100 | [diff] [blame] | 2164 | } |
Christopher Faulet | b045bb2 | 2020-02-28 10:42:20 +0100 | [diff] [blame] | 2165 | else if (isteq(n, ist("content-length"))) { |
Christopher Faulet | 91fcf21 | 2020-12-02 16:17:15 +0100 | [diff] [blame] | 2166 | if ((h1m->flags & H1_MF_RESP) && (h1s->status < 200 || h1s->status == 204)) |
| 2167 | goto skip_hdr; |
Christopher Faulet | 5220ef2 | 2019-03-27 15:44:56 +0100 | [diff] [blame] | 2168 | /* Only skip C-L header with invalid value. */ |
| 2169 | if (h1_parse_cont_len_header(h1m, &v) < 0) |
Willy Tarreau | 27cd223 | 2019-01-03 21:52:42 +0100 | [diff] [blame] | 2170 | goto skip_hdr; |
| 2171 | } |
Christopher Faulet | b045bb2 | 2020-02-28 10:42:20 +0100 | [diff] [blame] | 2172 | else if (isteq(n, ist("connection"))) { |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 2173 | h1_parse_connection_header(h1m, &v); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2174 | if (!v.len) |
| 2175 | goto skip_hdr; |
| 2176 | } |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 2177 | else if (isteq(n, ist("upgrade"))) { |
| 2178 | h1_parse_upgrade_header(h1m, v); |
| 2179 | } |
Amaury Denoyelle | aad333a | 2020-12-11 17:53:07 +0100 | [diff] [blame] | 2180 | else if ((isteq(n, ist("sec-websocket-accept")) && |
| 2181 | h1m->flags & H1_MF_RESP) || |
| 2182 | (isteq(n, ist("sec-websocket-key")) && |
| 2183 | !(h1m->flags & H1_MF_RESP))) { |
Christopher Faulet | 62138aa | 2022-11-02 08:42:08 +0100 | [diff] [blame] | 2184 | h1s->flags |= H1S_F_HAVE_WS_KEY; |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 2185 | } |
Christopher Faulet | f56e846 | 2021-09-28 10:56:36 +0200 | [diff] [blame] | 2186 | else if (isteq(n, ist("te"))) { |
| 2187 | /* "te" may only be sent with "trailers" if this value |
| 2188 | * is present, otherwise it must be deleted. |
| 2189 | */ |
| 2190 | v = istist(v, ist("trailers")); |
| 2191 | if (!isttest(v) || (v.len > 8 && v.ptr[8] != ',')) |
| 2192 | goto skip_hdr; |
| 2193 | v = ist("trailers"); |
| 2194 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2195 | |
Christopher Faulet | 67d5809 | 2019-10-02 10:51:38 +0200 | [diff] [blame] | 2196 | /* Skip header if same name is used to add the server name */ |
Tim Duesterhus | b4b0377 | 2022-03-05 00:52:43 +0100 | [diff] [blame] | 2197 | if (!(h1m->flags & H1_MF_RESP) && isttest(h1c->px->server_id_hdr_name) && |
| 2198 | isteqi(n, h1c->px->server_id_hdr_name)) |
Christopher Faulet | 67d5809 | 2019-10-02 10:51:38 +0200 | [diff] [blame] | 2199 | goto skip_hdr; |
| 2200 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2201 | /* Try to adjust the case of the header name */ |
| 2202 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 2203 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 2204 | if (!h1_format_htx_hdr(n, v, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2205 | goto full; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2206 | skip_hdr: |
| 2207 | h1m->state = H1_MSG_HDR_L2_LWS; |
| 2208 | break; |
| 2209 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2210 | case H1_MSG_LAST_LF: |
| 2211 | if (type != HTX_BLK_EOH) |
| 2212 | goto error; |
| 2213 | last_lf: |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 2214 | h1m->state = H1_MSG_LAST_LF; |
| 2215 | if (!(h1s->flags & H1S_F_HAVE_O_CONN)) { |
Christopher Faulet | 04f8919 | 2019-10-16 09:41:07 +0200 | [diff] [blame] | 2216 | if ((chn_htx->flags & HTX_FL_PROXY_RESP) && h1s->req.state != H1_MSG_DONE) { |
Christopher Faulet | 631c7e8 | 2021-09-27 09:47:03 +0200 | [diff] [blame] | 2217 | /* If the reply comes from haproxy while the request is |
| 2218 | * not finished, we force the connection close. */ |
Christopher Faulet | 04f8919 | 2019-10-16 09:41:07 +0200 | [diff] [blame] | 2219 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 2220 | TRACE_STATE("force close mode (resp)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
| 2221 | } |
Christopher Faulet | 631c7e8 | 2021-09-27 09:47:03 +0200 | [diff] [blame] | 2222 | else if ((h1m->flags & (H1_MF_XFER_ENC|H1_MF_CLEN)) == (H1_MF_XFER_ENC|H1_MF_CLEN)) { |
| 2223 | /* T-E + C-L: force close */ |
| 2224 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
Christopher Faulet | 8087fbb | 2023-09-27 15:05:03 +0200 | [diff] [blame] | 2225 | h1m->flags &= ~H1_MF_CLEN; |
Christopher Faulet | 631c7e8 | 2021-09-27 09:47:03 +0200 | [diff] [blame] | 2226 | TRACE_STATE("force close mode (T-E + C-L)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
| 2227 | } |
| 2228 | else if ((h1m->flags & (H1_MF_VER_11|H1_MF_XFER_ENC)) == H1_MF_XFER_ENC) { |
| 2229 | /* T-E + HTTP/1.0: force close */ |
| 2230 | h1s->flags = (h1s->flags & ~H1S_F_WANT_MSK) | H1S_F_WANT_CLO; |
| 2231 | TRACE_STATE("force close mode (T-E + HTTP/1.0)", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1s->h1c->conn, h1s); |
| 2232 | } |
Christopher Faulet | 04f8919 | 2019-10-16 09:41:07 +0200 | [diff] [blame] | 2233 | |
| 2234 | /* the conn_mode must be processed. So do it */ |
Christopher Faulet | a110ecb | 2019-06-17 14:07:46 +0200 | [diff] [blame] | 2235 | n = ist("connection"); |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 2236 | v = ist(""); |
Christopher Faulet | b992af0 | 2019-03-28 15:42:24 +0100 | [diff] [blame] | 2237 | h1_process_output_conn_mode(h1s, h1m, &v); |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 2238 | if (v.len) { |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2239 | /* Try to adjust the case of the header name */ |
| 2240 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 2241 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 2242 | if (!h1_format_htx_hdr(n, v, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2243 | goto full; |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 2244 | } |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 2245 | h1s->flags |= H1S_F_HAVE_O_CONN; |
Christopher Faulet | d1ebb1e | 2018-11-28 16:32:50 +0100 | [diff] [blame] | 2246 | } |
Willy Tarreau | 4710d20 | 2019-01-03 17:39:54 +0100 | [diff] [blame] | 2247 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 2248 | if ((h1s->meth != HTTP_METH_CONNECT && |
| 2249 | (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) == |
Christopher Faulet | 1f890dd | 2019-02-18 10:33:16 +0100 | [diff] [blame] | 2250 | (H1_MF_VER_11|H1_MF_XFER_LEN)) || |
Christopher Faulet | e5596bf | 2020-12-02 16:13:22 +0100 | [diff] [blame] | 2251 | (h1s->status >= 200 && !(h1s->flags & H1S_F_BODYLESS_RESP) && |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 2252 | !(h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) && |
Christopher Faulet | 1f890dd | 2019-02-18 10:33:16 +0100 | [diff] [blame] | 2253 | (h1m->flags & (H1_MF_VER_11|H1_MF_RESP|H1_MF_CLEN|H1_MF_CHNK|H1_MF_XFER_LEN)) == |
| 2254 | (H1_MF_VER_11|H1_MF_RESP|H1_MF_XFER_LEN))) { |
Willy Tarreau | 4710d20 | 2019-01-03 17:39:54 +0100 | [diff] [blame] | 2255 | /* chunking needed but header not seen */ |
Christopher Faulet | 7a991a9 | 2019-10-04 10:23:51 +0200 | [diff] [blame] | 2256 | n = ist("transfer-encoding"); |
| 2257 | v = ist("chunked"); |
| 2258 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 2259 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 2260 | if (!h1_format_htx_hdr(n, v, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2261 | goto full; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2262 | TRACE_STATE("add \"Transfer-Encoding: chunked\"", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s); |
Willy Tarreau | 4710d20 | 2019-01-03 17:39:54 +0100 | [diff] [blame] | 2263 | h1m->flags |= H1_MF_CHNK; |
| 2264 | } |
| 2265 | |
Christopher Faulet | 72ba6cd | 2019-09-24 16:20:05 +0200 | [diff] [blame] | 2266 | /* Now add the server name to a header (if requested) */ |
| 2267 | if (!(h1s->flags & H1S_F_HAVE_SRV_NAME) && |
Tim Duesterhus | b4b0377 | 2022-03-05 00:52:43 +0100 | [diff] [blame] | 2268 | !(h1m->flags & H1_MF_RESP) && isttest(h1c->px->server_id_hdr_name)) { |
Christopher Faulet | 72ba6cd | 2019-09-24 16:20:05 +0200 | [diff] [blame] | 2269 | struct server *srv = objt_server(h1c->conn->target); |
| 2270 | |
| 2271 | if (srv) { |
Tim Duesterhus | b4b0377 | 2022-03-05 00:52:43 +0100 | [diff] [blame] | 2272 | n = h1c->px->server_id_hdr_name; |
Christopher Faulet | 72ba6cd | 2019-09-24 16:20:05 +0200 | [diff] [blame] | 2273 | v = ist(srv->id); |
Christopher Faulet | 5cef2a6 | 2019-10-02 11:06:13 +0200 | [diff] [blame] | 2274 | |
| 2275 | /* Try to adjust the case of the header name */ |
| 2276 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 2277 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 2278 | if (!h1_format_htx_hdr(n, v, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2279 | goto full; |
Christopher Faulet | 72ba6cd | 2019-09-24 16:20:05 +0200 | [diff] [blame] | 2280 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2281 | TRACE_STATE("add server name header", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s); |
Christopher Faulet | 72ba6cd | 2019-09-24 16:20:05 +0200 | [diff] [blame] | 2282 | h1s->flags |= H1S_F_HAVE_SRV_NAME; |
| 2283 | } |
| 2284 | |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 2285 | /* Add websocket handshake key if needed */ |
Christopher Faulet | 62138aa | 2022-11-02 08:42:08 +0100 | [diff] [blame] | 2286 | if (!(h1s->flags & H1S_F_HAVE_WS_KEY) && |
| 2287 | (h1m->flags & (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) == (H1_MF_CONN_UPG|H1_MF_UPG_WEBSOCKET)) { |
Amaury Denoyelle | aad333a | 2020-12-11 17:53:07 +0100 | [diff] [blame] | 2288 | if (!(h1m->flags & H1_MF_RESP)) { |
| 2289 | /* generate a random websocket key |
| 2290 | * stored in the session to |
| 2291 | * verify it on the response side |
| 2292 | */ |
| 2293 | h1_generate_random_ws_input_key(h1s->ws_key); |
| 2294 | |
| 2295 | if (!h1_format_htx_hdr(ist("Sec-Websocket-Key"), |
| 2296 | ist(h1s->ws_key), |
| 2297 | &tmp)) { |
| 2298 | goto full; |
| 2299 | } |
| 2300 | } |
| 2301 | else { |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 2302 | /* add the response header key */ |
| 2303 | char key[29]; |
| 2304 | h1_calculate_ws_output_key(h1s->ws_key, key); |
| 2305 | if (!h1_format_htx_hdr(ist("Sec-Websocket-Accept"), |
| 2306 | ist(key), |
| 2307 | &tmp)) { |
| 2308 | goto full; |
| 2309 | } |
| 2310 | } |
Christopher Faulet | 62138aa | 2022-11-02 08:42:08 +0100 | [diff] [blame] | 2311 | h1s->flags |= H1S_F_HAVE_WS_KEY; |
Amaury Denoyelle | c193823 | 2020-12-11 17:53:03 +0100 | [diff] [blame] | 2312 | } |
| 2313 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2314 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request headers xferred" : "H1 response headers xferred"), |
| 2315 | H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s); |
| 2316 | |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 2317 | if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) { |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2318 | if (!chunk_memcat(&tmp, "\r\n", 2)) |
| 2319 | goto full; |
| 2320 | goto done; |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 2321 | } |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 2322 | else if ((h1m->flags & H1_MF_RESP) && |
Christopher Faulet | c75668e | 2020-12-07 18:10:32 +0100 | [diff] [blame] | 2323 | ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) { |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2324 | if (!chunk_memcat(&tmp, "\r\n", 2)) |
| 2325 | goto full; |
| 2326 | goto done; |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 2327 | } |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 2328 | else if ((h1m->flags & H1_MF_RESP) && |
| 2329 | h1s->status < 200 && (h1s->status == 100 || h1s->status >= 102)) { |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2330 | if (!chunk_memcat(&tmp, "\r\n", 2)) |
| 2331 | goto full; |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 2332 | h1m_init_res(&h1s->res); |
| 2333 | h1m->flags |= (H1_MF_NO_PHDR|H1_MF_CLEAN_CONN_HDR); |
Christopher Faulet | ada34b6 | 2019-05-24 16:36:43 +0200 | [diff] [blame] | 2334 | h1s->flags &= ~H1S_F_HAVE_O_CONN; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2335 | TRACE_STATE("1xx response xferred", H1_EV_TX_DATA|H1_EV_TX_HDRS, h1c->conn, h1s); |
Christopher Faulet | b75b5ea | 2019-05-17 08:37:28 +0200 | [diff] [blame] | 2336 | } |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2337 | else { |
Christopher Faulet | 2eb5243 | 2022-04-07 10:29:38 +0200 | [diff] [blame] | 2338 | /* EOM flag is set or empty payload (C-L to 0) and it is the last block */ |
| 2339 | if (htx_is_unique_blk(chn_htx, blk) && |
| 2340 | ((chn_htx->flags & HTX_FL_EOM) || ((h1m->flags & H1_MF_CLEN) && !h1m->curr_len))) { |
Mickael Torres | 226082d | 2022-11-16 14:29:37 +0100 | [diff] [blame] | 2341 | if ((h1m->flags & H1_MF_CHNK) && !(h1s->flags & H1S_F_BODYLESS_RESP)) { |
Christopher Faulet | 3d6e0e3 | 2021-02-08 09:34:35 +0100 | [diff] [blame] | 2342 | if (!chunk_memcat(&tmp, "\r\n0\r\n\r\n", 7)) |
| 2343 | goto full; |
| 2344 | } |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2345 | else if (!chunk_memcat(&tmp, "\r\n", 2)) |
| 2346 | goto full; |
| 2347 | goto done; |
| 2348 | } |
| 2349 | else if (!chunk_memcat(&tmp, "\r\n", 2)) |
| 2350 | goto full; |
Christopher Faulet | c62c2b9 | 2019-03-28 11:41:39 +0100 | [diff] [blame] | 2351 | h1m->state = H1_MSG_DATA; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2352 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2353 | break; |
| 2354 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2355 | case H1_MSG_DATA: |
Christopher Faulet | f8db73e | 2019-07-04 17:12:12 +0200 | [diff] [blame] | 2356 | case H1_MSG_TUNNEL: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2357 | if (type == HTX_BLK_EOT || type == HTX_BLK_TLR) { |
Christopher Faulet | 0d7e634 | 2021-02-08 15:56:36 +0100 | [diff] [blame] | 2358 | if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) |
| 2359 | goto trailers; |
| 2360 | |
Christopher Faulet | 5433a0b | 2019-06-27 17:40:14 +0200 | [diff] [blame] | 2361 | /* If the message is not chunked, never |
| 2362 | * add the last chunk. */ |
| 2363 | if ((h1m->flags & H1_MF_CHNK) && !chunk_memcat(&tmp, "0\r\n", 3)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2364 | goto full; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2365 | TRACE_PROTO("sending message trailers", H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s, chn_htx); |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2366 | goto trailers; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 2367 | } |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2368 | else if (type != HTX_BLK_DATA) |
| 2369 | goto error; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2370 | |
| 2371 | TRACE_PROTO("sending message data", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx, (size_t[]){sz}); |
Christopher Faulet | d9233f0 | 2019-10-14 14:01:24 +0200 | [diff] [blame] | 2372 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2373 | /* It is the last block of this message. After this one, |
| 2374 | * only tunneled data may be forwarded. */ |
| 2375 | if (h1m->state == H1_MSG_DATA && htx_is_unique_blk(chn_htx, blk) && (chn_htx->flags & HTX_FL_EOM)) { |
| 2376 | TRACE_DEVEL("last message block", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s); |
| 2377 | last_data = 1; |
| 2378 | } |
Christopher Faulet | d9233f0 | 2019-10-14 14:01:24 +0200 | [diff] [blame] | 2379 | |
| 2380 | if (vlen > count) { |
| 2381 | /* Get the maximum amount of data we can xferred */ |
| 2382 | vlen = count; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2383 | last_data = 0; |
Christopher Faulet | d9233f0 | 2019-10-14 14:01:24 +0200 | [diff] [blame] | 2384 | } |
| 2385 | |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 2386 | if (h1m->state == H1_MSG_DATA) { |
| 2387 | if (h1m->flags & H1_MF_CLEN) { |
| 2388 | if (vlen > h1m->curr_len) { |
| 2389 | TRACE_ERROR("too much payload, more than announced", |
| 2390 | H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); |
| 2391 | goto error; |
| 2392 | } |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 2393 | } |
| 2394 | if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) { |
| 2395 | TRACE_PROTO("Skip data for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx); |
| 2396 | goto skip_data; |
| 2397 | } |
Christopher Faulet | 0d7e634 | 2021-02-08 15:56:36 +0100 | [diff] [blame] | 2398 | } |
| 2399 | |
Christopher Faulet | d9233f0 | 2019-10-14 14:01:24 +0200 | [diff] [blame] | 2400 | chklen = 0; |
| 2401 | if (h1m->flags & H1_MF_CHNK) { |
| 2402 | chklen = b_room(&tmp); |
| 2403 | chklen = ((chklen < 16) ? 1 : (chklen < 256) ? 2 : |
| 2404 | (chklen < 4096) ? 3 : (chklen < 65536) ? 4 : |
| 2405 | (chklen < 1048576) ? 5 : 8); |
| 2406 | chklen += 4; /* 2 x CRLF */ |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2407 | |
| 2408 | /* If it is the end of the chunked message (without EOT), reserve the |
| 2409 | * last chunk size */ |
| 2410 | if (last_data) |
| 2411 | chklen += 5; |
Christopher Faulet | d9233f0 | 2019-10-14 14:01:24 +0200 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | if (vlen + chklen > b_room(&tmp)) { |
| 2415 | /* too large for the buffer */ |
| 2416 | if (chklen >= b_room(&tmp)) |
| 2417 | goto full; |
| 2418 | vlen = b_room(&tmp) - chklen; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2419 | last_data = 0; |
Christopher Faulet | d9233f0 | 2019-10-14 14:01:24 +0200 | [diff] [blame] | 2420 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2421 | v = htx_get_blk_value(chn_htx, blk); |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 2422 | v.len = vlen; |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 2423 | if (!h1_format_htx_data(v, &tmp, !!(h1m->flags & H1_MF_CHNK))) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2424 | goto full; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2425 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2426 | /* Space already reserved, so it must succeed */ |
| 2427 | if ((h1m->flags & H1_MF_CHNK) && last_data && !chunk_memcat(&tmp, "0\r\n\r\n", 5)) |
| 2428 | goto error; |
| 2429 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2430 | if (h1m->state == H1_MSG_DATA) |
| 2431 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request payload data xferred" : "H1 response payload data xferred"), |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2432 | H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len}); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2433 | else |
| 2434 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request tunneled data xferred" : "H1 response tunneled data xferred"), |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2435 | H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, 0, (size_t[]){v.len}); |
Christopher Faulet | 0d7e634 | 2021-02-08 15:56:36 +0100 | [diff] [blame] | 2436 | |
| 2437 | skip_data: |
Christopher Faulet | 2eb5243 | 2022-04-07 10:29:38 +0200 | [diff] [blame] | 2438 | if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) { |
Christopher Faulet | b4eca0e | 2022-01-10 17:27:51 +0100 | [diff] [blame] | 2439 | h1m->curr_len -= vlen; |
Christopher Faulet | 2eb5243 | 2022-04-07 10:29:38 +0200 | [diff] [blame] | 2440 | if (!h1m->curr_len) |
| 2441 | last_data = 1; |
| 2442 | } |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2443 | if (last_data) |
| 2444 | goto done; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2445 | break; |
| 2446 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2447 | case H1_MSG_TRAILERS: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2448 | if (type != HTX_BLK_TLR && type != HTX_BLK_EOT) |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2449 | goto error; |
| 2450 | trailers: |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2451 | h1m->state = H1_MSG_TRAILERS; |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2452 | |
Christopher Faulet | d934e8d | 2022-05-05 09:39:42 +0200 | [diff] [blame] | 2453 | if (!(h1m->flags & H1_MF_CHNK)) |
| 2454 | goto done; |
Christopher Faulet | 5433a0b | 2019-06-27 17:40:14 +0200 | [diff] [blame] | 2455 | |
Christopher Faulet | e5596bf | 2020-12-02 16:13:22 +0100 | [diff] [blame] | 2456 | if ((h1m->flags & H1_MF_RESP) && (h1s->flags & H1S_F_BODYLESS_RESP)) { |
| 2457 | TRACE_PROTO("Skip trailers for bodyless response", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s, chn_htx); |
Christopher Faulet | 0d7e634 | 2021-02-08 15:56:36 +0100 | [diff] [blame] | 2458 | if (type == HTX_BLK_EOT) |
| 2459 | goto done; |
Christopher Faulet | e5596bf | 2020-12-02 16:13:22 +0100 | [diff] [blame] | 2460 | break; |
| 2461 | } |
| 2462 | |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 2463 | if (type == HTX_BLK_EOT) { |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 2464 | if (!chunk_memcat(&tmp, "\r\n", 2)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2465 | goto full; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2466 | TRACE_PROTO((!(h1m->flags & H1_MF_RESP) ? "H1 request trailers xferred" : "H1 response trailers xferred"), |
| 2467 | H1_EV_TX_DATA|H1_EV_TX_TLRS, h1c->conn, h1s); |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2468 | goto done; |
Christopher Faulet | 3218821 | 2018-11-20 18:21:43 +0100 | [diff] [blame] | 2469 | } |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 2470 | else { // HTX_BLK_TLR |
| 2471 | n = htx_get_blk_name(chn_htx, blk); |
| 2472 | v = htx_get_blk_value(chn_htx, blk); |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 2473 | |
| 2474 | /* Try to adjust the case of the header name */ |
| 2475 | if (h1c->px->options2 & (PR_O2_H1_ADJ_BUGCLI|PR_O2_H1_ADJ_BUGSRV)) |
| 2476 | h1_adjust_case_outgoing_hdr(h1s, h1m, &n); |
Christopher Faulet | 53a899b | 2019-10-08 16:38:42 +0200 | [diff] [blame] | 2477 | if (!h1_format_htx_hdr(n, v, &tmp)) |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2478 | goto full; |
Christopher Faulet | 2d7c539 | 2019-06-03 10:41:26 +0200 | [diff] [blame] | 2479 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2480 | break; |
| 2481 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2482 | case H1_MSG_DONE: |
Christopher Faulet | d934e8d | 2022-05-05 09:39:42 +0200 | [diff] [blame] | 2483 | /* If the message is not chunked, ignore |
| 2484 | * trailers. It may happen with H2 messages. */ |
| 2485 | if ((type == HTX_BLK_TLR || type == HTX_BLK_EOT) && !(h1m->flags & H1_MF_CHNK)) |
| 2486 | break; |
| 2487 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 2488 | TRACE_STATE("unexpected data xferred in done state", H1_EV_TX_DATA|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); |
| 2489 | goto error; /* For now return an error */ |
| 2490 | |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2491 | done: |
| 2492 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2493 | if (!(h1m->flags & H1_MF_RESP) && h1s->meth == HTTP_METH_CONNECT) { |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 2494 | h1s->flags |= H1S_F_TX_BLK; |
| 2495 | TRACE_STATE("Disable output processing", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s); |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2496 | } |
| 2497 | else if ((h1m->flags & H1_MF_RESP) && |
| 2498 | ((h1s->meth == HTTP_METH_CONNECT && h1s->status >= 200 && h1s->status < 300) || h1s->status == 101)) { |
| 2499 | /* a successful reply to a CONNECT or a protocol switching is sent |
| 2500 | * to the client. Switch the response to tunnel mode. |
| 2501 | */ |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 2502 | h1_set_tunnel_mode(h1s); |
Christopher Faulet | f3158e9 | 2019-11-15 11:14:23 +0100 | [diff] [blame] | 2503 | } |
Christopher Faulet | 5be651d | 2021-01-22 15:28:03 +0100 | [diff] [blame] | 2504 | |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 2505 | if (h1s->flags & H1S_F_RX_BLK) { |
| 2506 | h1s->flags &= ~H1S_F_RX_BLK; |
Christopher Faulet | 02c92c3 | 2021-04-09 12:31:48 +0200 | [diff] [blame] | 2507 | h1_wake_stream_for_recv(h1s); |
Christopher Faulet | 10a8670 | 2021-04-07 14:18:11 +0200 | [diff] [blame] | 2508 | TRACE_STATE("Re-enable input processing", H1_EV_TX_DATA|H1_EV_H1S_BLK|H1_EV_STRM_WAKE, h1c->conn, h1s); |
Christopher Faulet | cd67bff | 2019-06-14 16:54:15 +0200 | [diff] [blame] | 2509 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2510 | |
| 2511 | TRACE_USER((!(h1m->flags & H1_MF_RESP) ? "H1 request fully xferred" : "H1 response fully xferred"), |
| 2512 | H1_EV_TX_DATA, h1c->conn, h1s); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2513 | break; |
| 2514 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2515 | default: |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2516 | error: |
Christopher Faulet | d87d3fa | 2019-06-26 15:16:28 +0200 | [diff] [blame] | 2517 | /* Unexpected error during output processing */ |
Christopher Faulet | 69b4821 | 2019-09-09 10:11:30 +0200 | [diff] [blame] | 2518 | chn_htx->flags |= HTX_FL_PROCESSING_ERROR; |
Christopher Faulet | 60ef12c | 2020-09-24 10:05:44 +0200 | [diff] [blame] | 2519 | h1s->flags |= H1S_F_PROCESSING_ERROR; |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 2520 | se_fl_set(h1s->sd, SE_FL_ERROR); |
| 2521 | TRACE_ERROR("processing output error, set error on h1s", |
| 2522 | H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 2523 | goto end; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2524 | } |
Christopher Faulet | 94b2c76 | 2019-05-24 15:28:57 +0200 | [diff] [blame] | 2525 | |
| 2526 | nextblk: |
Christopher Faulet | b2e8416 | 2018-12-06 11:39:49 +0100 | [diff] [blame] | 2527 | total += vlen; |
| 2528 | count -= vlen; |
| 2529 | if (sz == vlen) |
| 2530 | blk = htx_remove_blk(chn_htx, blk); |
| 2531 | else { |
| 2532 | htx_cut_data_blk(chn_htx, blk, vlen); |
| 2533 | break; |
| 2534 | } |
Christopher Faulet | 129817b | 2018-09-20 16:14:40 +0200 | [diff] [blame] | 2535 | } |
| 2536 | |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2537 | copy: |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 2538 | /* when the output buffer is empty, tmp shares the same area so that we |
| 2539 | * only have to update pointers and lengths. |
| 2540 | */ |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 2541 | if (tmp.area == h1c->obuf.area + h1c->obuf.head) |
| 2542 | h1c->obuf.data = tmp.data; |
Willy Tarreau | c5efa33 | 2018-12-05 11:19:27 +0100 | [diff] [blame] | 2543 | else |
Christopher Faulet | c2518a5 | 2019-06-25 21:41:02 +0200 | [diff] [blame] | 2544 | b_putblk(&h1c->obuf, tmp.area, tmp.data); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2545 | |
Willy Tarreau | 3815b22 | 2018-12-11 19:50:43 +0100 | [diff] [blame] | 2546 | htx_to_buf(chn_htx, buf); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2547 | out: |
| 2548 | if (!buf_room_for_htx_data(&h1c->obuf)) { |
| 2549 | TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2550 | h1c->flags |= H1C_F_OUT_FULL; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2551 | } |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2552 | end: |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2553 | /* Both the request and the response reached the DONE state. So set EOI |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 2554 | * flag on the stream connector. Most of time, the flag will already be set, |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2555 | * except for protocol upgrades. Report an error if data remains blocked |
| 2556 | * in the output buffer. |
| 2557 | */ |
| 2558 | if (h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE) { |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 2559 | se_fl_set(h1s->sd, SE_FL_EOI); |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2560 | if (!htx_is_empty(chn_htx)) { |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 2561 | chn_htx->flags |= HTX_FL_PROCESSING_ERROR; |
| 2562 | h1s->flags |= H1S_F_PROCESSING_ERROR; |
| 2563 | se_fl_set(h1s->sd, SE_FL_ERROR); |
| 2564 | TRACE_ERROR("txn done but data waiting to be sent, set error on h1s", |
| 2565 | H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2566 | } |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 2567 | } |
| 2568 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2569 | TRACE_LEAVE(H1_EV_TX_DATA, h1c->conn, h1s, chn_htx, (size_t[]){total}); |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2570 | return total; |
Christopher Faulet | a61aa54 | 2019-10-14 14:17:00 +0200 | [diff] [blame] | 2571 | |
| 2572 | full: |
| 2573 | TRACE_STATE("h1c obuf full", H1_EV_TX_DATA|H1_EV_H1S_BLK, h1c->conn, h1s); |
| 2574 | h1c->flags |= H1C_F_OUT_FULL; |
| 2575 | goto copy; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2576 | } |
| 2577 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2578 | /*********************************************************/ |
| 2579 | /* functions below are I/O callbacks from the connection */ |
| 2580 | /*********************************************************/ |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 2581 | static void h1_wake_stream_for_recv(struct h1s *h1s) |
| 2582 | { |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2583 | if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_RECV) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2584 | TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2585 | tasklet_wakeup(h1s->subs->tasklet); |
| 2586 | h1s->subs->events &= ~SUB_RETRY_RECV; |
| 2587 | if (!h1s->subs->events) |
| 2588 | h1s->subs = NULL; |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 2589 | } |
| 2590 | } |
| 2591 | static void h1_wake_stream_for_send(struct h1s *h1s) |
| 2592 | { |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2593 | if (h1s && h1s->subs && h1s->subs->events & SUB_RETRY_SEND) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2594 | TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 2595 | tasklet_wakeup(h1s->subs->tasklet); |
| 2596 | h1s->subs->events &= ~SUB_RETRY_SEND; |
| 2597 | if (!h1s->subs->events) |
| 2598 | h1s->subs = NULL; |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 2599 | } |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2600 | } |
| 2601 | |
Christopher Faulet | ad4daf6 | 2021-01-21 17:49:01 +0100 | [diff] [blame] | 2602 | /* alerts the data layer following this sequence : |
| 2603 | * - if the h1s' data layer is subscribed to recv, then it's woken up for recv |
| 2604 | * - if its subscribed to send, then it's woken up for send |
| 2605 | * - if it was subscribed to neither, its ->wake() callback is called |
| 2606 | */ |
| 2607 | static void h1_alert(struct h1s *h1s) |
| 2608 | { |
| 2609 | if (h1s->subs) { |
| 2610 | h1_wake_stream_for_recv(h1s); |
| 2611 | h1_wake_stream_for_send(h1s); |
| 2612 | } |
Willy Tarreau | 2f2318d | 2022-05-18 10:17:16 +0200 | [diff] [blame] | 2613 | else if (h1s_sc(h1s) && h1s_sc(h1s)->app_ops->wake != NULL) { |
Christopher Faulet | ad4daf6 | 2021-01-21 17:49:01 +0100 | [diff] [blame] | 2614 | TRACE_POINT(H1_EV_STRM_WAKE, h1s->h1c->conn, h1s); |
Willy Tarreau | 2f2318d | 2022-05-18 10:17:16 +0200 | [diff] [blame] | 2615 | h1s_sc(h1s)->app_ops->wake(h1s_sc(h1s)); |
Christopher Faulet | ad4daf6 | 2021-01-21 17:49:01 +0100 | [diff] [blame] | 2616 | } |
| 2617 | } |
| 2618 | |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2619 | /* Try to send an HTTP error with h1c->errcode status code. It returns 1 on success |
Christopher Faulet | 56a4994 | 2022-10-04 17:45:24 +0200 | [diff] [blame] | 2620 | * and 0 on error. The flag H1C_F_ABRT_PENDING is set on the H1 connection for |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2621 | * retryable errors (allocation error or buffer full). On success, the error is |
| 2622 | * copied in the output buffer. |
| 2623 | */ |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2624 | static int h1_send_error(struct h1c *h1c) |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2625 | { |
| 2626 | int rc = http_get_status_idx(h1c->errcode); |
| 2627 | int ret = 0; |
| 2628 | |
| 2629 | TRACE_ENTER(H1_EV_H1C_ERR, h1c->conn, 0, 0, (size_t[]){h1c->errcode}); |
| 2630 | |
| 2631 | /* Verify if the error is mapped on /dev/null or any empty file */ |
| 2632 | /// XXX: do a function ! |
| 2633 | if (h1c->px->replies[rc] && |
| 2634 | h1c->px->replies[rc]->type == HTTP_REPLY_ERRMSG && |
| 2635 | h1c->px->replies[rc]->body.errmsg && |
| 2636 | b_is_null(h1c->px->replies[rc]->body.errmsg)) { |
| 2637 | /* Empty error, so claim a success */ |
| 2638 | ret = 1; |
| 2639 | goto out; |
| 2640 | } |
| 2641 | |
| 2642 | if (h1c->flags & (H1C_F_OUT_ALLOC|H1C_F_OUT_FULL)) { |
Christopher Faulet | 56a4994 | 2022-10-04 17:45:24 +0200 | [diff] [blame] | 2643 | h1c->flags |= H1C_F_ABRT_PENDING; |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2644 | goto out; |
| 2645 | } |
| 2646 | |
| 2647 | if (!h1_get_buf(h1c, &h1c->obuf)) { |
Christopher Faulet | 56a4994 | 2022-10-04 17:45:24 +0200 | [diff] [blame] | 2648 | h1c->flags |= (H1C_F_OUT_ALLOC|H1C_F_ABRT_PENDING); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2649 | TRACE_STATE("waiting for h1c obuf allocation", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn); |
| 2650 | goto out; |
| 2651 | } |
Tim Duesterhus | 7750850 | 2022-03-15 13:11:06 +0100 | [diff] [blame] | 2652 | ret = b_istput(&h1c->obuf, ist(http_err_msgs[rc])); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2653 | if (unlikely(ret <= 0)) { |
| 2654 | if (!ret) { |
Christopher Faulet | 56a4994 | 2022-10-04 17:45:24 +0200 | [diff] [blame] | 2655 | h1c->flags |= (H1C_F_OUT_FULL|H1C_F_ABRT_PENDING); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2656 | TRACE_STATE("h1c obuf full", H1_EV_H1C_ERR|H1_EV_H1C_BLK, h1c->conn); |
| 2657 | goto out; |
| 2658 | } |
| 2659 | else { |
| 2660 | /* we cannot report this error, so claim a success */ |
| 2661 | ret = 1; |
| 2662 | } |
| 2663 | } |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 2664 | |
Christopher Faulet | da93802 | 2022-12-15 09:22:35 +0100 | [diff] [blame] | 2665 | if (h1c->state == H1_CS_EMBRYONIC) { |
Christopher Faulet | 8f1f1b0 | 2022-12-15 09:59:50 +0100 | [diff] [blame] | 2666 | BUG_ON(h1c->h1s == NULL || h1s_sc(h1c->h1s) != NULL); |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 2667 | TRACE_DEVEL("Abort embryonic H1S", H1_EV_H1C_ERR, h1c->conn, h1c->h1s); |
| 2668 | h1s_destroy(h1c->h1s); |
| 2669 | } |
| 2670 | |
Christopher Faulet | a1a76ce | 2022-11-23 17:07:48 +0100 | [diff] [blame] | 2671 | h1c->flags = (h1c->flags & ~(H1C_F_WAIT_NEXT_REQ|H1C_F_ABRT_PENDING)) | H1C_F_ABRTED; |
Christopher Faulet | 061a098 | 2022-11-29 17:16:30 +0100 | [diff] [blame] | 2672 | h1_close(h1c); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2673 | out: |
| 2674 | TRACE_LEAVE(H1_EV_H1C_ERR, h1c->conn); |
| 2675 | return ret; |
| 2676 | } |
| 2677 | |
| 2678 | /* Try to send a 500 internal error. It relies on h1_send_error to send the |
| 2679 | * error. This function takes care of incrementing stats and tracked counters. |
| 2680 | */ |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2681 | static int h1_handle_internal_err(struct h1c *h1c) |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2682 | { |
| 2683 | struct session *sess = h1c->conn->owner; |
Christopher Faulet | d1b5730 | 2022-11-23 17:13:12 +0100 | [diff] [blame] | 2684 | int ret = 0; |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2685 | |
| 2686 | session_inc_http_req_ctr(sess); |
Frédéric Lécaille | 9969adb | 2023-01-18 11:52:21 +0100 | [diff] [blame] | 2687 | proxy_inc_fe_req_ctr(sess->listener, sess->fe, 1); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2688 | _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[5]); |
| 2689 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2690 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2691 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2692 | |
| 2693 | h1c->errcode = 500; |
| 2694 | ret = h1_send_error(h1c); |
| 2695 | sess_log(sess); |
| 2696 | return ret; |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 2697 | } |
| 2698 | |
Christopher Faulet | b3230f7 | 2021-09-21 18:38:20 +0200 | [diff] [blame] | 2699 | /* Try to send an error because of a parsing error. By default a 400 bad request |
| 2700 | * error is returned. But the status code may be specified by setting |
| 2701 | * h1c->errcode. It relies on h1_send_error to send the error. This function |
| 2702 | * takes care of incrementing stats and tracked counters. |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2703 | */ |
Christopher Faulet | b3230f7 | 2021-09-21 18:38:20 +0200 | [diff] [blame] | 2704 | static int h1_handle_parsing_error(struct h1c *h1c) |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2705 | { |
| 2706 | struct session *sess = h1c->conn->owner; |
Christopher Faulet | d1b5730 | 2022-11-23 17:13:12 +0100 | [diff] [blame] | 2707 | int ret = 0; |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2708 | |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 2709 | if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB))) { |
Christopher Faulet | a1a76ce | 2022-11-23 17:07:48 +0100 | [diff] [blame] | 2710 | h1c->flags = (h1c->flags & ~H1C_F_WAIT_NEXT_REQ) | H1C_F_ABRTED; |
Christopher Faulet | 061a098 | 2022-11-29 17:16:30 +0100 | [diff] [blame] | 2711 | h1_close(h1c); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2712 | goto end; |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 2713 | } |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2714 | |
| 2715 | session_inc_http_req_ctr(sess); |
| 2716 | session_inc_http_err_ctr(sess); |
Frédéric Lécaille | 9969adb | 2023-01-18 11:52:21 +0100 | [diff] [blame] | 2717 | proxy_inc_fe_req_ctr(sess->listener, sess->fe, 1); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2718 | _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]); |
| 2719 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2720 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2721 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2722 | |
Christopher Faulet | b3230f7 | 2021-09-21 18:38:20 +0200 | [diff] [blame] | 2723 | if (!h1c->errcode) |
| 2724 | h1c->errcode = 400; |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2725 | ret = h1_send_error(h1c); |
Christopher Faulet | 07e10de | 2021-07-26 09:42:49 +0200 | [diff] [blame] | 2726 | if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG)) |
| 2727 | sess_log(sess); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2728 | |
| 2729 | end: |
| 2730 | return ret; |
| 2731 | } |
| 2732 | |
Christopher Faulet | 2eed800 | 2020-12-07 11:26:13 +0100 | [diff] [blame] | 2733 | /* Try to send a 501 not implemented error. It relies on h1_send_error to send |
| 2734 | * the error. This function takes care of incrementing stats and tracked |
| 2735 | * counters. |
| 2736 | */ |
| 2737 | static int h1_handle_not_impl_err(struct h1c *h1c) |
| 2738 | { |
| 2739 | struct session *sess = h1c->conn->owner; |
Christopher Faulet | d1b5730 | 2022-11-23 17:13:12 +0100 | [diff] [blame] | 2740 | int ret = 0; |
Christopher Faulet | 2eed800 | 2020-12-07 11:26:13 +0100 | [diff] [blame] | 2741 | |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 2742 | if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB))) { |
Christopher Faulet | a1a76ce | 2022-11-23 17:07:48 +0100 | [diff] [blame] | 2743 | h1c->flags = (h1c->flags & ~H1C_F_WAIT_NEXT_REQ) | H1C_F_ABRTED; |
Christopher Faulet | 061a098 | 2022-11-29 17:16:30 +0100 | [diff] [blame] | 2744 | h1_close(h1c); |
Christopher Faulet | 2eed800 | 2020-12-07 11:26:13 +0100 | [diff] [blame] | 2745 | goto end; |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 2746 | } |
Christopher Faulet | 2eed800 | 2020-12-07 11:26:13 +0100 | [diff] [blame] | 2747 | |
| 2748 | session_inc_http_req_ctr(sess); |
Frédéric Lécaille | 9969adb | 2023-01-18 11:52:21 +0100 | [diff] [blame] | 2749 | proxy_inc_fe_req_ctr(sess->listener, sess->fe, 1); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2750 | _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]); |
| 2751 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2752 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2753 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | 2eed800 | 2020-12-07 11:26:13 +0100 | [diff] [blame] | 2754 | |
| 2755 | h1c->errcode = 501; |
| 2756 | ret = h1_send_error(h1c); |
Christopher Faulet | 07e10de | 2021-07-26 09:42:49 +0200 | [diff] [blame] | 2757 | if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG)) |
| 2758 | sess_log(sess); |
Christopher Faulet | 2eed800 | 2020-12-07 11:26:13 +0100 | [diff] [blame] | 2759 | |
| 2760 | end: |
| 2761 | return ret; |
| 2762 | } |
| 2763 | |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2764 | /* Try to send a 408 timeout error. It relies on h1_send_error to send the |
| 2765 | * error. This function takes care of incrementing stats and tracked counters. |
| 2766 | */ |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2767 | static int h1_handle_req_tout(struct h1c *h1c) |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2768 | { |
| 2769 | struct session *sess = h1c->conn->owner; |
Christopher Faulet | d1b5730 | 2022-11-23 17:13:12 +0100 | [diff] [blame] | 2770 | int ret = 0; |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2771 | |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 2772 | if (!b_data(&h1c->ibuf) && ((h1c->flags & H1C_F_WAIT_NEXT_REQ) || (sess->fe->options & PR_O_IGNORE_PRB))) { |
Christopher Faulet | a1a76ce | 2022-11-23 17:07:48 +0100 | [diff] [blame] | 2773 | h1c->flags = (h1c->flags & ~H1C_F_WAIT_NEXT_REQ) | H1C_F_ABRTED; |
Christopher Faulet | 061a098 | 2022-11-29 17:16:30 +0100 | [diff] [blame] | 2774 | h1_close(h1c); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2775 | goto end; |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 2776 | } |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2777 | |
| 2778 | session_inc_http_req_ctr(sess); |
Frédéric Lécaille | 9969adb | 2023-01-18 11:52:21 +0100 | [diff] [blame] | 2779 | proxy_inc_fe_req_ctr(sess->listener, sess->fe, 1); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2780 | _HA_ATOMIC_INC(&sess->fe->fe_counters.p.http.rsp[4]); |
| 2781 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 2782 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 2783 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2784 | |
| 2785 | h1c->errcode = 408; |
Christopher Faulet | 2274244 | 2022-11-23 16:58:22 +0100 | [diff] [blame] | 2786 | ret = h1_send_error(h1c); |
Christopher Faulet | 07e10de | 2021-07-26 09:42:49 +0200 | [diff] [blame] | 2787 | if (b_data(&h1c->ibuf) || !(sess->fe->options & PR_O_NULLNOLOG)) |
Christopher Faulet | 2274244 | 2022-11-23 16:58:22 +0100 | [diff] [blame] | 2788 | sess_log(sess); |
| 2789 | |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 2790 | end: |
| 2791 | return ret; |
| 2792 | } |
| 2793 | |
| 2794 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2795 | /* |
| 2796 | * Attempt to read data, and subscribe if none available |
| 2797 | */ |
| 2798 | static int h1_recv(struct h1c *h1c) |
| 2799 | { |
| 2800 | struct connection *conn = h1c->conn; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2801 | size_t ret = 0, max; |
Willy Tarreau | 6e59cb5 | 2020-02-20 11:11:50 +0100 | [diff] [blame] | 2802 | int flags = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2803 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2804 | TRACE_ENTER(H1_EV_H1C_RECV, h1c->conn); |
| 2805 | |
| 2806 | if (h1c->wait_event.events & SUB_RETRY_RECV) { |
| 2807 | TRACE_DEVEL("leaving on sub_recv", H1_EV_H1C_RECV, h1c->conn); |
Christopher Faulet | c386a88 | 2018-12-04 16:06:28 +0100 | [diff] [blame] | 2808 | return (b_data(&h1c->ibuf)); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2809 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2810 | |
Christopher Faulet | ae63576 | 2020-09-21 11:47:16 +0200 | [diff] [blame] | 2811 | if ((h1c->flags & H1C_F_WANT_SPLICE) || !h1_recv_allowed(h1c)) { |
| 2812 | TRACE_DEVEL("leaving on (want_splice|!recv_allowed)", H1_EV_H1C_RECV, h1c->conn); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2813 | return 1; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2814 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2815 | |
Christopher Faulet | f7d5ff3 | 2019-04-16 13:55:08 +0200 | [diff] [blame] | 2816 | if (!h1_get_buf(h1c, &h1c->ibuf)) { |
| 2817 | h1c->flags |= H1C_F_IN_ALLOC; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2818 | TRACE_STATE("waiting for h1c ibuf allocation", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2819 | return 0; |
Christopher Faulet | 9768c26 | 2018-10-22 09:34:31 +0200 | [diff] [blame] | 2820 | } |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 2821 | |
Olivier Houchard | 29a22bc | 2018-12-04 18:16:45 +0100 | [diff] [blame] | 2822 | /* |
| 2823 | * If we only have a small amount of data, realign it, |
| 2824 | * it's probably cheaper than doing 2 recv() calls. |
| 2825 | */ |
| 2826 | if (b_data(&h1c->ibuf) > 0 && b_data(&h1c->ibuf) < 128) |
Christopher Faulet | 00d7cde | 2021-02-04 11:01:51 +0100 | [diff] [blame] | 2827 | b_slow_realign_ofs(&h1c->ibuf, trash.area, sizeof(struct htx)); |
Olivier Houchard | 29a22bc | 2018-12-04 18:16:45 +0100 | [diff] [blame] | 2828 | |
Willy Tarreau | 8871895 | 2023-03-17 12:30:38 +0100 | [diff] [blame] | 2829 | max = buf_room_for_htx_data(&h1c->ibuf); |
| 2830 | |
Willy Tarreau | 6e59cb5 | 2020-02-20 11:11:50 +0100 | [diff] [blame] | 2831 | /* avoid useless reads after first responses */ |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2832 | if (!h1c->h1s || |
| 2833 | (!(h1c->flags & H1C_F_IS_BACK) && h1c->h1s->req.state == H1_MSG_RQBEFORE) || |
Willy Tarreau | 8871895 | 2023-03-17 12:30:38 +0100 | [diff] [blame] | 2834 | ((h1c->flags & H1C_F_IS_BACK) && h1c->h1s->res.state == H1_MSG_RPBEFORE)) { |
Willy Tarreau | 6e59cb5 | 2020-02-20 11:11:50 +0100 | [diff] [blame] | 2835 | flags |= CO_RFL_READ_ONCE; |
| 2836 | |
Willy Tarreau | 8871895 | 2023-03-17 12:30:38 +0100 | [diff] [blame] | 2837 | /* we know that the first read will be constrained to a smaller |
| 2838 | * read by the stream layer in order to respect the reserve. |
| 2839 | * Reading too much will result in global.tune.maxrewrite being |
| 2840 | * left at the end of the buffer, and in a very small read |
| 2841 | * being performed again to complete them (typically 16 bytes |
| 2842 | * freed in the index after headers were consumed) before |
| 2843 | * another larger read. Instead, given that we know we're |
| 2844 | * waiting for a header and we'll be limited, let's perform a |
| 2845 | * shorter first read that the upper layer can retrieve by just |
| 2846 | * a pointer swap and the next read will be doable at once in |
| 2847 | * an empty buffer. |
| 2848 | */ |
| 2849 | if (max > global.tune.bufsize - global.tune.maxrewrite) |
| 2850 | max = global.tune.bufsize - global.tune.maxrewrite; |
| 2851 | } |
| 2852 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2853 | if (max) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2854 | if (h1c->flags & H1C_F_IN_FULL) { |
| 2855 | h1c->flags &= ~H1C_F_IN_FULL; |
| 2856 | TRACE_STATE("h1c ibuf not full anymore", H1_EV_H1C_RECV|H1_EV_H1C_BLK); |
| 2857 | } |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 2858 | |
| 2859 | if (!b_data(&h1c->ibuf)) { |
| 2860 | /* try to pre-align the buffer like the rxbufs will be |
| 2861 | * to optimize memory copies. |
| 2862 | */ |
Willy Tarreau | 78f548f | 2018-12-05 10:02:39 +0100 | [diff] [blame] | 2863 | h1c->ibuf.head = sizeof(struct htx); |
| 2864 | } |
Willy Tarreau | 6e59cb5 | 2020-02-20 11:11:50 +0100 | [diff] [blame] | 2865 | ret = conn->xprt->rcv_buf(conn, conn->xprt_ctx, &h1c->ibuf, max, flags); |
Christopher Faulet | 41951ab | 2021-11-26 18:12:51 +0100 | [diff] [blame] | 2866 | HA_ATOMIC_ADD(&h1c->px_counters->bytes_in, ret); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2867 | } |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 2868 | |
| 2869 | if (conn_xprt_read0_pending(conn)) { |
| 2870 | TRACE_DEVEL("read0 on connection", H1_EV_H1C_RECV, h1c->conn); |
| 2871 | h1c->flags |= H1C_F_EOS; |
| 2872 | } |
| 2873 | if (h1c->conn->flags & CO_FL_ERROR) { |
| 2874 | TRACE_DEVEL("connection error", H1_EV_H1C_RECV, h1c->conn); |
| 2875 | h1c->flags |= H1C_F_ERROR; |
| 2876 | } |
| 2877 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2878 | if (max && !ret && h1_recv_allowed(h1c)) { |
| 2879 | TRACE_STATE("failed to receive data, subscribing", H1_EV_H1C_RECV, h1c->conn); |
| 2880 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
Christopher Faulet | cf56b99 | 2018-12-11 16:12:31 +0100 | [diff] [blame] | 2881 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2882 | else { |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 2883 | TRACE_DATA("data received or pending or connection error", H1_EV_H1C_RECV, h1c->conn, 0, 0, (size_t[]){ret}); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2884 | h1_wake_stream_for_recv(h1c->h1s); |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 2885 | } |
| 2886 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2887 | if (!b_data(&h1c->ibuf)) |
| 2888 | h1_release_buf(h1c, &h1c->ibuf); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2889 | else if (!buf_room_for_htx_data(&h1c->ibuf)) { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2890 | h1c->flags |= H1C_F_IN_FULL; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2891 | TRACE_STATE("h1c ibuf full", H1_EV_H1C_RECV|H1_EV_H1C_BLK); |
| 2892 | } |
| 2893 | |
| 2894 | TRACE_LEAVE(H1_EV_H1C_RECV, h1c->conn); |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 2895 | return !!ret || (h1c->flags & (H1C_F_EOS|H1C_F_ERROR)); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2896 | } |
| 2897 | |
| 2898 | |
| 2899 | /* |
| 2900 | * Try to send data if possible |
| 2901 | */ |
| 2902 | static int h1_send(struct h1c *h1c) |
| 2903 | { |
| 2904 | struct connection *conn = h1c->conn; |
| 2905 | unsigned int flags = 0; |
| 2906 | size_t ret; |
| 2907 | int sent = 0; |
| 2908 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2909 | TRACE_ENTER(H1_EV_H1C_SEND, h1c->conn); |
| 2910 | |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 2911 | if (h1c->flags & (H1C_F_ERROR|H1C_F_ERR_PENDING)) { |
| 2912 | TRACE_DEVEL("leaving on H1C error|err_pending", H1_EV_H1C_SEND, h1c->conn); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2913 | b_reset(&h1c->obuf); |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 2914 | if (h1c->flags & H1C_F_EOS) |
| 2915 | h1c->flags |= H1C_F_ERROR; |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2916 | return 1; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2917 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2918 | |
| 2919 | if (!b_data(&h1c->obuf)) |
| 2920 | goto end; |
| 2921 | |
Christopher Faulet | 4623036 | 2019-10-17 16:04:20 +0200 | [diff] [blame] | 2922 | if (h1c->flags & H1C_F_CO_MSG_MORE) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2923 | flags |= CO_SFL_MSG_MORE; |
Christopher Faulet | 4623036 | 2019-10-17 16:04:20 +0200 | [diff] [blame] | 2924 | if (h1c->flags & H1C_F_CO_STREAMER) |
| 2925 | flags |= CO_SFL_STREAMER; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2926 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2927 | ret = conn->xprt->snd_buf(conn, conn->xprt_ctx, &h1c->obuf, b_data(&h1c->obuf), flags); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2928 | if (ret > 0) { |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 2929 | TRACE_DATA("data sent", H1_EV_H1C_SEND, h1c->conn, 0, 0, (size_t[]){ret}); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2930 | if (h1c->flags & H1C_F_OUT_FULL) { |
| 2931 | h1c->flags &= ~H1C_F_OUT_FULL; |
| 2932 | TRACE_STATE("h1c obuf not full anymore", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn); |
| 2933 | } |
Christopher Faulet | 41951ab | 2021-11-26 18:12:51 +0100 | [diff] [blame] | 2934 | HA_ATOMIC_ADD(&h1c->px_counters->bytes_out, ret); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2935 | b_del(&h1c->obuf, ret); |
| 2936 | sent = 1; |
| 2937 | } |
| 2938 | |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 2939 | if (conn->flags & CO_FL_ERROR) { |
| 2940 | /* connection error, nothing to send, clear the buffer to release it */ |
| 2941 | TRACE_DEVEL("connection error", H1_EV_H1C_SEND, h1c->conn); |
| 2942 | h1c->flags |= H1C_F_ERR_PENDING; |
| 2943 | if (h1c->flags & H1C_F_EOS) |
| 2944 | h1c->flags |= H1C_F_ERROR; |
Christopher Faulet | a462ee0 | 2022-11-22 17:16:22 +0100 | [diff] [blame] | 2945 | else if (!(h1c->wait_event.events & SUB_RETRY_RECV)) { |
| 2946 | /* EOS not seen, so subscribe for reads to be able to |
| 2947 | * catch the error on the reading path. It is especially |
| 2948 | * important if EOI was reached. |
| 2949 | */ |
| 2950 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
| 2951 | } |
Christopher Faulet | 145aa47 | 2018-12-06 10:56:20 +0100 | [diff] [blame] | 2952 | b_reset(&h1c->obuf); |
| 2953 | } |
| 2954 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2955 | end: |
Christopher Faulet | c17c31c | 2022-02-01 18:25:06 +0100 | [diff] [blame] | 2956 | if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC))) |
Christopher Faulet | e17fa2f | 2018-12-11 16:25:36 +0100 | [diff] [blame] | 2957 | h1_wake_stream_for_send(h1c->h1s); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 2958 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2959 | /* We're done, no more to send */ |
| 2960 | if (!b_data(&h1c->obuf)) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2961 | TRACE_DEVEL("leaving with everything sent", H1_EV_H1C_SEND, h1c->conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2962 | h1_release_buf(h1c, &h1c->obuf); |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 2963 | if (h1c->state == H1_CS_CLOSING) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2964 | TRACE_STATE("process pending shutdown for writes", H1_EV_H1C_SEND, h1c->conn); |
Christopher Faulet | a85c522 | 2021-10-27 15:36:38 +0200 | [diff] [blame] | 2965 | h1_shutw_conn(conn); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2966 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2967 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2968 | else if (!(h1c->wait_event.events & SUB_RETRY_SEND)) { |
| 2969 | TRACE_STATE("more data to send, subscribing", H1_EV_H1C_SEND, h1c->conn); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 2970 | conn->xprt->subscribe(conn, conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2971 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2972 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2973 | TRACE_LEAVE(H1_EV_H1C_SEND, h1c->conn); |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 2974 | return sent || (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) || (h1c->state == H1_CS_CLOSED); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2975 | } |
| 2976 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2977 | /* callback called on any event by the connection handler. |
| 2978 | * It applies changes and returns zero, or < 0 if it wants immediate |
| 2979 | * destruction of the connection. |
| 2980 | */ |
| 2981 | static int h1_process(struct h1c * h1c) |
| 2982 | { |
| 2983 | struct connection *conn = h1c->conn; |
| 2984 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 2985 | TRACE_ENTER(H1_EV_H1C_WAKE, conn); |
| 2986 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2987 | /* Try to parse now the first block of a request, creating the H1 stream if necessary */ |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 2988 | if (b_data(&h1c->ibuf) && /* Input data to be processed */ |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 2989 | (h1c->state < H1_CS_RUNNING) && /* IDLE, EMBRYONIC or UPGRADING */ |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 2990 | !(h1c->flags & (H1C_F_IN_SALLOC|H1C_F_ABRT_PENDING))) { /* No allocation failure on the stream rxbuf and no ERROR on the H1C */ |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 2991 | struct h1s *h1s = h1c->h1s; |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2992 | struct buffer *buf; |
| 2993 | size_t count; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 2994 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2995 | /* When it happens for a backend connection, we may release it (it is probably a 408) */ |
| 2996 | if (h1c->flags & H1C_F_IS_BACK) |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 2997 | goto release; |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 2998 | |
| 2999 | /* First of all handle H1 to H2 upgrade (no need to create the H1 stream) */ |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 3000 | if (!(h1c->flags & H1C_F_WAIT_NEXT_REQ) && /* First request */ |
Christopher Faulet | 143e9e5 | 2021-03-08 15:32:03 +0100 | [diff] [blame] | 3001 | !(h1c->px->options2 & PR_O2_NO_H2_UPGRADE) && /* H2 upgrade supported by the proxy */ |
| 3002 | !(conn->mux->flags & MX_FL_NO_UPG)) { /* the current mux supports upgrades */ |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3003 | /* Try to match H2 preface before parsing the request headers. */ |
| 3004 | if (b_isteq(&h1c->ibuf, 0, b_data(&h1c->ibuf), ist(H2_CONN_PREFACE)) > 0) { |
| 3005 | h1c->flags |= H1C_F_UPG_H2C; |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3006 | if (h1c->state == H1_CS_UPGRADING) { |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3007 | BUG_ON(!h1s); |
Christopher Faulet | c393c9e | 2023-04-11 08:32:13 +0200 | [diff] [blame] | 3008 | se_fl_set(h1s->sd, SE_FL_EOI|SE_FL_EOS); /* Set EOS here to release the SC */ |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 3009 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3010 | TRACE_STATE("release h1c to perform H2 upgrade ", H1_EV_RX_DATA|H1_EV_H1C_WAKE); |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 3011 | goto release; |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3012 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3013 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3014 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3015 | /* Create the H1 stream if not already there */ |
| 3016 | if (!h1s) { |
Christopher Faulet | 9ec2f4d | 2022-03-23 15:15:29 +0100 | [diff] [blame] | 3017 | h1s = h1c_frt_stream_new(h1c, NULL, h1c->conn->owner); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3018 | if (!h1s) { |
| 3019 | b_reset(&h1c->ibuf); |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 3020 | h1_handle_internal_err(h1c); |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 3021 | TRACE_ERROR("alloc error", H1_EV_H1C_WAKE|H1_EV_H1C_ERR); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3022 | goto no_parsing; |
| 3023 | } |
| 3024 | } |
| 3025 | |
| 3026 | if (h1s->sess->t_idle == -1) |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 3027 | h1s->sess->t_idle = ns_to_ms(now_ns - h1s->sess->accept_ts) - h1s->sess->t_handshake; |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3028 | |
| 3029 | /* Get the stream rxbuf */ |
| 3030 | buf = h1_get_buf(h1c, &h1s->rxbuf); |
| 3031 | if (!buf) { |
| 3032 | h1c->flags |= H1C_F_IN_SALLOC; |
| 3033 | TRACE_STATE("waiting for stream rxbuf allocation", H1_EV_H1C_WAKE|H1_EV_H1C_BLK, h1c->conn); |
| 3034 | return 0; |
| 3035 | } |
| 3036 | |
| 3037 | count = (buf->size - sizeof(struct htx) - global.tune.maxrewrite); |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 3038 | h1_process_demux(h1c, buf, count); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3039 | h1_release_buf(h1c, &h1s->rxbuf); |
| 3040 | h1_set_idle_expiration(h1c); |
Christopher Faulet | ad4ed00 | 2022-12-16 11:13:00 +0100 | [diff] [blame] | 3041 | if (h1c->state < H1_CS_RUNNING) { |
| 3042 | if (h1s->flags & H1S_F_INTERNAL_ERROR) { |
| 3043 | h1_handle_internal_err(h1c); |
| 3044 | TRACE_ERROR("internal error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR); |
| 3045 | } |
| 3046 | else if (h1s->flags & H1S_F_NOT_IMPL_ERROR) { |
| 3047 | h1_handle_not_impl_err(h1c); |
| 3048 | TRACE_ERROR("not-implemented error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR); |
| 3049 | } |
| 3050 | else if (h1s->flags & H1S_F_PARSING_ERROR || se_fl_test(h1s->sd, SE_FL_ERROR)) { |
| 3051 | h1_handle_parsing_error(h1c); |
| 3052 | TRACE_ERROR("parsing error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR); |
| 3053 | } |
| 3054 | else { |
| 3055 | TRACE_STATE("Incomplete message, subscribing", H1_EV_RX_DATA|H1_EV_H1C_BLK|H1_EV_H1C_WAKE, h1c->conn, h1s); |
| 3056 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
| 3057 | } |
Christopher Faulet | 2177d96 | 2022-10-05 08:39:14 +0200 | [diff] [blame] | 3058 | } |
Christopher Faulet | ae63576 | 2020-09-21 11:47:16 +0200 | [diff] [blame] | 3059 | } |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 3060 | |
Christopher Faulet | e6ef4cd | 2022-11-17 15:54:12 +0100 | [diff] [blame] | 3061 | no_parsing: |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3062 | h1_send(h1c); |
| 3063 | |
Christopher Faulet | 7530830 | 2021-11-15 14:51:37 +0100 | [diff] [blame] | 3064 | /* H1 connection must be released ASAP if: |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 3065 | * - an error occurred on the H1C or |
Christopher Faulet | 7530830 | 2021-11-15 14:51:37 +0100 | [diff] [blame] | 3066 | * - a read0 was received or |
| 3067 | * - a silent shutdown was emitted and all outgoing data sent |
| 3068 | */ |
Christopher Faulet | 31da34d | 2022-10-10 16:36:10 +0200 | [diff] [blame] | 3069 | if ((h1c->flags & (H1C_F_EOS|H1C_F_ERROR|H1C_F_ABRT_PENDING|H1C_F_ABRTED)) || |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3070 | (h1c->state >= H1_CS_CLOSING && (h1c->flags & H1C_F_SILENT_SHUT) && !b_data(&h1c->obuf))) { |
| 3071 | if (h1c->state != H1_CS_RUNNING) { |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 3072 | /* No stream connector or upgrading */ |
Christopher Faulet | 1ffa63c | 2023-10-06 15:34:04 +0200 | [diff] [blame] | 3073 | if (h1c->state < H1_CS_RUNNING && !(h1c->flags & (H1C_F_IS_BACK|H1C_F_ABRT_PENDING))) { |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 3074 | /* shutdown for reads and no error on the frontend connection: Send an error */ |
Christopher Faulet | b3230f7 | 2021-09-21 18:38:20 +0200 | [diff] [blame] | 3075 | if (h1_handle_parsing_error(h1c)) |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3076 | h1_send(h1c); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3077 | } |
Christopher Faulet | 56a4994 | 2022-10-04 17:45:24 +0200 | [diff] [blame] | 3078 | else if (h1c->flags & H1C_F_ABRT_PENDING) { |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3079 | /* Handle pending error, if any (only possible on frontend connection) */ |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3080 | BUG_ON(h1c->flags & H1C_F_IS_BACK); |
| 3081 | if (h1_send_error(h1c)) |
| 3082 | h1_send(h1c); |
| 3083 | } |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3084 | else { |
| 3085 | h1_close(h1c); |
| 3086 | TRACE_STATE("close h1c", H1_EV_H1S_END, h1c->conn); |
| 3087 | } |
Christopher Faulet | ae63576 | 2020-09-21 11:47:16 +0200 | [diff] [blame] | 3088 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3089 | /* If there is some pending outgoing data or error, just wait */ |
Christopher Faulet | 56a4994 | 2022-10-04 17:45:24 +0200 | [diff] [blame] | 3090 | if (h1c->state == H1_CS_CLOSING || (h1c->flags & H1C_F_ABRT_PENDING)) |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3091 | goto end; |
Christopher Faulet | feb1174 | 2018-11-29 15:12:34 +0100 | [diff] [blame] | 3092 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3093 | /* Otherwise we can release the H1 connection */ |
| 3094 | goto release; |
| 3095 | } |
| 3096 | else { |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3097 | struct h1s *h1s = h1c->h1s; |
| 3098 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 3099 | /* Here there is still a H1 stream with a stream connector. |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 3100 | * Report an error at the stream level and wake up the stream |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3101 | */ |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3102 | BUG_ON(!h1s); |
| 3103 | |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 3104 | if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) { |
| 3105 | se_fl_set_error(h1s->sd); |
| 3106 | TRACE_STATE("report (ERR_PENDING|ERROR) to SE", H1_EV_H1C_RECV, conn, h1s); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3107 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3108 | TRACE_POINT(H1_EV_STRM_WAKE, h1c->conn, h1s); |
Christopher Faulet | ad4daf6 | 2021-01-21 17:49:01 +0100 | [diff] [blame] | 3109 | h1_alert(h1s); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3110 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3111 | } |
Willy Tarreau | c493c9c | 2019-06-03 14:18:22 +0200 | [diff] [blame] | 3112 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3113 | if (!b_data(&h1c->ibuf)) |
| 3114 | h1_release_buf(h1c, &h1c->ibuf); |
| 3115 | |
Amaury Denoyelle | efc6e95 | 2021-05-05 11:11:11 +0200 | [diff] [blame] | 3116 | /* Check if a soft-stop is in progress. |
| 3117 | * Release idling front connection if this is the case. |
| 3118 | */ |
| 3119 | if (!(h1c->flags & H1C_F_IS_BACK)) { |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 3120 | if (unlikely(h1c->px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))) { |
William Dauchy | a9dd901 | 2022-01-05 22:53:24 +0100 | [diff] [blame] | 3121 | if (!(h1c->px->options & PR_O_IDLE_CLOSE_RESP) && |
Remi Tricot-Le Breton | b5d968d | 2022-04-08 18:04:18 +0200 | [diff] [blame] | 3122 | h1c->flags & H1C_F_WAIT_NEXT_REQ) { |
| 3123 | |
| 3124 | int send_close = 1; |
| 3125 | /* If a close-spread-time option is set, we want to avoid |
| 3126 | * closing all the active HTTP2 connections at once so we add a |
| 3127 | * random factor that will spread the closing. |
| 3128 | */ |
| 3129 | if (tick_isset(global.close_spread_end)) { |
| 3130 | int remaining_window = tick_remain(now_ms, global.close_spread_end); |
| 3131 | if (remaining_window) { |
| 3132 | /* This should increase the closing rate the |
| 3133 | * further along the window we are. |
| 3134 | */ |
| 3135 | send_close = (remaining_window <= statistical_prng_range(global.close_spread_time)); |
| 3136 | } |
| 3137 | } |
Remi Tricot-Le Breton | 4d7fdc6 | 2022-04-26 15:17:18 +0200 | [diff] [blame] | 3138 | else if (global.tune.options & GTUNE_DISABLE_ACTIVE_CLOSE) |
| 3139 | send_close = 0; /* let the client close his connection himself */ |
Remi Tricot-Le Breton | b5d968d | 2022-04-08 18:04:18 +0200 | [diff] [blame] | 3140 | if (send_close) |
| 3141 | goto release; |
| 3142 | } |
Amaury Denoyelle | efc6e95 | 2021-05-05 11:11:11 +0200 | [diff] [blame] | 3143 | } |
| 3144 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3145 | |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3146 | if (h1c->state == H1_CS_RUNNING && (h1c->flags & H1C_F_WANT_SPLICE) && !h1s_data_pending(h1c->h1s)) { |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3147 | TRACE_DEVEL("xprt rcv_buf blocked (want_splice), notify h1s for recv", H1_EV_H1C_RECV, h1c->conn); |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3148 | h1_wake_stream_for_recv(h1c->h1s); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 3149 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3150 | |
Christopher Faulet | 4736527 | 2018-10-31 17:40:50 +0100 | [diff] [blame] | 3151 | end: |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 3152 | h1_refresh_timeout(h1c); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3153 | TRACE_LEAVE(H1_EV_H1C_WAKE, conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3154 | return 0; |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 3155 | |
| 3156 | release: |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3157 | if (h1c->state == H1_CS_UPGRADING) { |
| 3158 | struct h1s *h1s = h1c->h1s; |
| 3159 | |
| 3160 | /* Don't release the H1 connection right now, we must destroy |
| 3161 | * the attached SC first */ |
| 3162 | BUG_ON(!h1s); |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 3163 | |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 3164 | if (h1c->flags & H1C_F_EOS) { |
Christopher Faulet | c393c9e | 2023-04-11 08:32:13 +0200 | [diff] [blame] | 3165 | se_fl_set(h1s->sd, SE_FL_EOI|SE_FL_EOS); |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 3166 | TRACE_STATE("report EOS to SE", H1_EV_H1C_RECV, conn, h1s); |
| 3167 | } |
| 3168 | if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) { |
| 3169 | se_fl_set_error(h1s->sd); |
| 3170 | TRACE_STATE("report (ERR_PENDING|ERROR) to SE", H1_EV_H1C_RECV, conn, h1s); |
| 3171 | } |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 3172 | h1_alert(h1s); |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 3173 | TRACE_DEVEL("waiting to release the SC before releasing the connection", H1_EV_H1C_WAKE); |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 3174 | } |
| 3175 | else { |
| 3176 | h1_release(h1c); |
| 3177 | TRACE_DEVEL("leaving after releasing the connection", H1_EV_H1C_WAKE); |
| 3178 | } |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 3179 | return -1; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3180 | } |
| 3181 | |
Willy Tarreau | e388f2f | 2021-03-02 16:51:09 +0100 | [diff] [blame] | 3182 | struct task *h1_io_cb(struct task *t, void *ctx, unsigned int state) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3183 | { |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3184 | struct connection *conn; |
| 3185 | struct tasklet *tl = (struct tasklet *)t; |
| 3186 | int conn_in_list; |
Willy Tarreau | e388f2f | 2021-03-02 16:51:09 +0100 | [diff] [blame] | 3187 | struct h1c *h1c = ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3188 | int ret = 0; |
| 3189 | |
Willy Tarreau | e388f2f | 2021-03-02 16:51:09 +0100 | [diff] [blame] | 3190 | if (state & TASK_F_USR1) { |
| 3191 | /* the tasklet was idling on an idle connection, it might have |
| 3192 | * been stolen, let's be careful! |
| 3193 | */ |
| 3194 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 3195 | if (tl->context == NULL) { |
| 3196 | /* The connection has been taken over by another thread, |
| 3197 | * we're no longer responsible for it, so just free the |
| 3198 | * tasklet, and do nothing. |
| 3199 | */ |
| 3200 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 3201 | tasklet_free(tl); |
| 3202 | return NULL; |
| 3203 | } |
| 3204 | conn = h1c->conn; |
| 3205 | TRACE_POINT(H1_EV_H1C_WAKE, conn); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3206 | |
Willy Tarreau | e388f2f | 2021-03-02 16:51:09 +0100 | [diff] [blame] | 3207 | /* Remove the connection from the list, to be sure nobody attempts |
| 3208 | * to use it while we handle the I/O events |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3209 | */ |
Christopher Faulet | 3a7b539 | 2023-03-16 11:43:05 +0100 | [diff] [blame] | 3210 | conn_in_list = conn_get_idle_flag(conn); |
Willy Tarreau | e388f2f | 2021-03-02 16:51:09 +0100 | [diff] [blame] | 3211 | if (conn_in_list) |
| 3212 | conn_delete_from_tree(&conn->hash_node->node); |
| 3213 | |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3214 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Willy Tarreau | e388f2f | 2021-03-02 16:51:09 +0100 | [diff] [blame] | 3215 | } else { |
| 3216 | /* we're certain the connection was not in an idle list */ |
| 3217 | conn = h1c->conn; |
| 3218 | TRACE_ENTER(H1_EV_H1C_WAKE, conn); |
| 3219 | conn_in_list = 0; |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3220 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3221 | |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3222 | if (!(h1c->wait_event.events & SUB_RETRY_SEND)) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3223 | ret = h1_send(h1c); |
Willy Tarreau | 4f6516d | 2018-12-19 13:59:17 +0100 | [diff] [blame] | 3224 | if (!(h1c->wait_event.events & SUB_RETRY_RECV)) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3225 | ret |= h1_recv(h1c); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3226 | if (ret || b_data(&h1c->ibuf)) |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3227 | ret = h1_process(h1c); |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 3228 | |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3229 | /* If we were in an idle list, we want to add it back into it, |
| 3230 | * unless h1_process() returned -1, which mean it has destroyed |
| 3231 | * the connection (testing !ret is enough, if h1_process() wasn't |
| 3232 | * called then ret will be 0 anyway. |
| 3233 | */ |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 3234 | if (ret < 0) |
| 3235 | t = NULL; |
| 3236 | |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3237 | if (!ret && conn_in_list) { |
| 3238 | struct server *srv = objt_server(conn->target); |
| 3239 | |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3240 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3241 | if (conn_in_list == CO_FL_SAFE_LIST) |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 3242 | eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3243 | else |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 3244 | eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node); |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3245 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3246 | } |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 3247 | return t; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3248 | } |
| 3249 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3250 | static int h1_wake(struct connection *conn) |
| 3251 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3252 | struct h1c *h1c = conn->ctx; |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 3253 | int ret; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3254 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3255 | TRACE_POINT(H1_EV_H1C_WAKE, conn); |
| 3256 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 3257 | h1_send(h1c); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 3258 | ret = h1_process(h1c); |
| 3259 | if (ret == 0) { |
| 3260 | struct h1s *h1s = h1c->h1s; |
| 3261 | |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3262 | if (h1c->state == H1_CS_UPGRADING || h1c->state == H1_CS_RUNNING) |
Christopher Faulet | ad4daf6 | 2021-01-21 17:49:01 +0100 | [diff] [blame] | 3263 | h1_alert(h1s); |
Olivier Houchard | 75159a9 | 2018-12-03 18:46:09 +0100 | [diff] [blame] | 3264 | } |
| 3265 | return ret; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3266 | } |
| 3267 | |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 3268 | /* Connection timeout management. The principle is that if there's no receipt |
| 3269 | * nor sending for a certain amount of time, the connection is closed. |
| 3270 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 3271 | struct task *h1_timeout_task(struct task *t, void *context, unsigned int state) |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 3272 | { |
| 3273 | struct h1c *h1c = context; |
| 3274 | int expired = tick_is_expired(t->expire, now_ms); |
| 3275 | |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3276 | TRACE_ENTER(H1_EV_H1C_WAKE, h1c ? h1c->conn : NULL); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3277 | |
Willy Tarreau | 68d4ee9 | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 3278 | if (h1c) { |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3279 | /* Make sure nobody stole the connection from us */ |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3280 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3281 | |
| 3282 | /* Somebody already stole the connection from us, so we should not |
| 3283 | * free it, we just have to free the task. |
| 3284 | */ |
| 3285 | if (!t->context) { |
| 3286 | h1c = NULL; |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3287 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3288 | goto do_leave; |
| 3289 | } |
| 3290 | |
Willy Tarreau | 68d4ee9 | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 3291 | if (!expired) { |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3292 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3293 | TRACE_DEVEL("leaving (not expired)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s); |
Willy Tarreau | 68d4ee9 | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 3294 | return t; |
| 3295 | } |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 3296 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 3297 | /* If a stream connector is still attached and ready to the mux, wait for the |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3298 | * stream's timeout |
Willy Tarreau | 68d4ee9 | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 3299 | */ |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3300 | if (h1c->state == H1_CS_RUNNING) { |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3301 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3302 | t->expire = TICK_ETERNITY; |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 3303 | TRACE_DEVEL("leaving (SC still attached)", H1_EV_H1C_WAKE, h1c->conn, h1c->h1s); |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3304 | return t; |
| 3305 | } |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3306 | |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3307 | /* Try to send an error to the client */ |
Christopher Faulet | 56a4994 | 2022-10-04 17:45:24 +0200 | [diff] [blame] | 3308 | if (h1c->state != H1_CS_CLOSING && !(h1c->flags & (H1C_F_IS_BACK|H1C_F_ERROR|H1C_F_ABRT_PENDING))) { |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 3309 | TRACE_DEVEL("timeout error detected", H1_EV_H1C_WAKE|H1_EV_H1C_ERR, h1c->conn, h1c->h1s); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3310 | if (h1_handle_req_tout(h1c)) |
| 3311 | h1_send(h1c); |
Christopher Faulet | 56a4994 | 2022-10-04 17:45:24 +0200 | [diff] [blame] | 3312 | if (b_data(&h1c->obuf) || (h1c->flags & H1C_F_ABRT_PENDING)) { |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3313 | h1_refresh_timeout(h1c); |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3314 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3315 | return t; |
| 3316 | } |
| 3317 | } |
| 3318 | |
Christopher Faulet | f75cc54 | 2022-11-22 17:06:13 +0100 | [diff] [blame] | 3319 | if (h1c->h1s && !se_fl_test(h1c->h1s->sd, SE_FL_ORPHAN)) { |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 3320 | /* Don't release the H1 connection right now, we must destroy the |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3321 | * attached SC first. */ |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 3322 | se_fl_set(h1c->h1s->sd, SE_FL_EOS | SE_FL_ERROR); |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 3323 | h1_alert(h1c->h1s); |
| 3324 | h1_refresh_timeout(h1c); |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3325 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].idle_conns_lock); |
Willy Tarreau | e68bc61 | 2022-05-27 11:23:05 +0200 | [diff] [blame] | 3326 | TRACE_DEVEL("waiting to release the SC before releasing the connection", H1_EV_H1C_WAKE); |
Christopher Faulet | 0f9395d | 2021-01-21 17:50:19 +0100 | [diff] [blame] | 3327 | return t; |
| 3328 | } |
| 3329 | |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3330 | /* We're about to destroy the connection, so make sure nobody attempts |
| 3331 | * to steal it from us. |
Willy Tarreau | 68d4ee9 | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 3332 | */ |
Christopher Faulet | 3a7b539 | 2023-03-16 11:43:05 +0100 | [diff] [blame] | 3333 | if (h1c->conn->flags & CO_FL_LIST_MASK) |
Amaury Denoyelle | 8990b01 | 2021-02-19 15:29:16 +0100 | [diff] [blame] | 3334 | conn_delete_from_tree(&h1c->conn->hash_node->node); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3335 | |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 3336 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Willy Tarreau | 68d4ee9 | 2020-06-30 11:19:23 +0200 | [diff] [blame] | 3337 | } |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 3338 | |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3339 | do_leave: |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 3340 | task_destroy(t); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 3341 | |
| 3342 | if (!h1c) { |
| 3343 | /* resources were already deleted */ |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3344 | TRACE_DEVEL("leaving (not more h1c)", H1_EV_H1C_WAKE); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 3345 | return NULL; |
| 3346 | } |
| 3347 | |
| 3348 | h1c->task = NULL; |
Christopher Faulet | c1c66a4 | 2020-09-29 15:30:15 +0200 | [diff] [blame] | 3349 | h1_release(h1c); |
| 3350 | TRACE_LEAVE(H1_EV_H1C_WAKE); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 3351 | return NULL; |
| 3352 | } |
| 3353 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3354 | /*******************************************/ |
| 3355 | /* functions below are used by the streams */ |
| 3356 | /*******************************************/ |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3357 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3358 | /* |
| 3359 | * Attach a new stream to a connection |
| 3360 | * (Used for outgoing connections) |
| 3361 | */ |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 3362 | static int h1_attach(struct connection *conn, struct sedesc *sd, struct session *sess) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3363 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3364 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3365 | struct h1s *h1s; |
| 3366 | |
Willy Tarreau | 4d1ff11 | 2022-09-01 15:49:23 +0200 | [diff] [blame] | 3367 | /* this connection is no more idle (if it was at all) */ |
Christopher Faulet | 71abc0c | 2022-10-04 17:06:52 +0200 | [diff] [blame] | 3368 | h1c->flags &= ~H1C_F_SILENT_SHUT; |
Willy Tarreau | 4d1ff11 | 2022-09-01 15:49:23 +0200 | [diff] [blame] | 3369 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3370 | TRACE_ENTER(H1_EV_STRM_NEW, conn); |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 3371 | if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) { |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 3372 | TRACE_ERROR("h1c on error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn); |
| 3373 | goto err; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3374 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3375 | |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 3376 | h1s = h1c_bck_stream_new(h1c, sd->sc, sess); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3377 | if (h1s == NULL) { |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 3378 | TRACE_ERROR("h1s creation failure", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn); |
| 3379 | goto err; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3380 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3381 | |
Willy Tarreau | e388f2f | 2021-03-02 16:51:09 +0100 | [diff] [blame] | 3382 | /* the connection is not idle anymore, let's mark this */ |
| 3383 | HA_ATOMIC_AND(&h1c->wait_event.tasklet->state, ~TASK_F_USR1); |
Willy Tarreau | 4f8cd43 | 2021-03-02 17:27:58 +0100 | [diff] [blame] | 3384 | xprt_set_used(conn, conn->xprt, conn->xprt_ctx); |
Willy Tarreau | e388f2f | 2021-03-02 16:51:09 +0100 | [diff] [blame] | 3385 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3386 | TRACE_LEAVE(H1_EV_STRM_NEW, conn, h1s); |
Christopher Faulet | e00ad35 | 2021-12-16 14:44:31 +0100 | [diff] [blame] | 3387 | return 0; |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 3388 | err: |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 3389 | TRACE_DEVEL("leaving on error", H1_EV_STRM_NEW|H1_EV_STRM_END|H1_EV_STRM_ERR, conn); |
Christopher Faulet | e00ad35 | 2021-12-16 14:44:31 +0100 | [diff] [blame] | 3390 | return -1; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3391 | } |
| 3392 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 3393 | /* Retrieves a valid stream connector from this connection, or returns NULL. |
| 3394 | * For this mux, it's easy as we can only store a single stream connector. |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3395 | */ |
Willy Tarreau | d137353 | 2022-05-27 11:00:59 +0200 | [diff] [blame] | 3396 | static struct stconn *h1_get_first_sc(const struct connection *conn) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3397 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3398 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3399 | struct h1s *h1s = h1c->h1s; |
| 3400 | |
| 3401 | if (h1s) |
Willy Tarreau | 97b4d3b | 2022-05-18 07:27:14 +0200 | [diff] [blame] | 3402 | return h1s_sc(h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3403 | |
| 3404 | return NULL; |
| 3405 | } |
| 3406 | |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3407 | static void h1_destroy(void *ctx) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3408 | { |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3409 | struct h1c *h1c = ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3410 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3411 | TRACE_POINT(H1_EV_H1C_END, h1c->conn); |
Christopher Faulet | 7c452cc | 2022-04-14 11:08:26 +0200 | [diff] [blame] | 3412 | if (!h1c->h1s || h1c->conn->ctx != h1c) |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3413 | h1_release(h1c); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3414 | } |
| 3415 | |
| 3416 | /* |
| 3417 | * Detach the stream from the connection and possibly release the connection. |
| 3418 | */ |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 3419 | static void h1_detach(struct sedesc *sd) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3420 | { |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 3421 | struct h1s *h1s = sd->se; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3422 | struct h1c *h1c; |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3423 | struct session *sess; |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3424 | int is_not_first; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3425 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3426 | TRACE_ENTER(H1_EV_STRM_END, h1s ? h1s->h1c->conn : NULL, h1s); |
| 3427 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3428 | if (!h1s) { |
| 3429 | TRACE_LEAVE(H1_EV_STRM_END); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3430 | return; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3431 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3432 | |
Olivier Houchard | f502aca | 2018-12-14 19:42:40 +0100 | [diff] [blame] | 3433 | sess = h1s->sess; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3434 | h1c = h1s->h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3435 | |
Christopher Faulet | 42849b0 | 2020-10-05 11:35:17 +0200 | [diff] [blame] | 3436 | sess->accept_date = date; |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 3437 | sess->accept_ts = now_ns; |
Christopher Faulet | 42849b0 | 2020-10-05 11:35:17 +0200 | [diff] [blame] | 3438 | sess->t_handshake = 0; |
| 3439 | sess->t_idle = -1; |
| 3440 | |
Olivier Houchard | 8a78690 | 2018-12-15 16:05:40 +0100 | [diff] [blame] | 3441 | is_not_first = h1s->flags & H1S_F_NOT_FIRST; |
| 3442 | h1s_destroy(h1s); |
| 3443 | |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3444 | if (h1c->state == H1_CS_IDLE && (h1c->flags & H1C_F_IS_BACK)) { |
Willy Tarreau | 4d1ff11 | 2022-09-01 15:49:23 +0200 | [diff] [blame] | 3445 | /* this connection may be killed at any moment, we want it to |
| 3446 | * die "cleanly" (i.e. only an RST). |
| 3447 | */ |
Christopher Faulet | 71abc0c | 2022-10-04 17:06:52 +0200 | [diff] [blame] | 3448 | h1c->flags |= H1C_F_SILENT_SHUT; |
Willy Tarreau | 4d1ff11 | 2022-09-01 15:49:23 +0200 | [diff] [blame] | 3449 | |
Christopher Faulet | f1204b8 | 2019-07-19 14:51:06 +0200 | [diff] [blame] | 3450 | /* If there are any excess server data in the input buffer, |
| 3451 | * release it and close the connection ASAP (some data may |
| 3452 | * remain in the output buffer). This happens if a server sends |
| 3453 | * invalid responses. So in such case, we don't want to reuse |
| 3454 | * the connection |
Christopher Faulet | 0362724 | 2019-07-19 11:34:08 +0200 | [diff] [blame] | 3455 | */ |
Christopher Faulet | f1204b8 | 2019-07-19 14:51:06 +0200 | [diff] [blame] | 3456 | if (b_data(&h1c->ibuf)) { |
| 3457 | h1_release_buf(h1c, &h1c->ibuf); |
Christopher Faulet | 061a098 | 2022-11-29 17:16:30 +0100 | [diff] [blame] | 3458 | h1_close(h1c); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3459 | TRACE_DEVEL("remaining data on detach, kill connection", H1_EV_STRM_END|H1_EV_H1C_END); |
Christopher Faulet | f1204b8 | 2019-07-19 14:51:06 +0200 | [diff] [blame] | 3460 | goto release; |
| 3461 | } |
Christopher Faulet | 0362724 | 2019-07-19 11:34:08 +0200 | [diff] [blame] | 3462 | |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 3463 | if (h1c->conn->flags & CO_FL_PRIVATE) { |
| 3464 | /* Add the connection in the session server list, if not already done */ |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3465 | if (!session_add_conn(sess, h1c->conn, h1c->conn->target)) { |
| 3466 | h1c->conn->owner = NULL; |
Olivier Houchard | 2444aa5 | 2020-01-20 13:56:01 +0100 | [diff] [blame] | 3467 | h1c->conn->mux->destroy(h1c); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3468 | goto end; |
Olivier Houchard | 351411f | 2018-12-27 17:20:54 +0100 | [diff] [blame] | 3469 | } |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 3470 | /* Always idle at this step */ |
Olivier Houchard | 2444aa5 | 2020-01-20 13:56:01 +0100 | [diff] [blame] | 3471 | if (session_check_idle_conn(sess, h1c->conn)) { |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3472 | /* The connection got destroyed, let's leave */ |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3473 | TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END); |
| 3474 | goto end; |
| 3475 | } |
Olivier Houchard | a4d4fdf | 2018-12-14 19:27:06 +0100 | [diff] [blame] | 3476 | } |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 3477 | else { |
Olivier Houchard | 2444aa5 | 2020-01-20 13:56:01 +0100 | [diff] [blame] | 3478 | if (h1c->conn->owner == sess) |
| 3479 | h1c->conn->owner = NULL; |
Willy Tarreau | e388f2f | 2021-03-02 16:51:09 +0100 | [diff] [blame] | 3480 | |
| 3481 | /* mark that the tasklet may lose its context to another thread and |
| 3482 | * that the handler needs to check it under the idle conns lock. |
| 3483 | */ |
| 3484 | HA_ATOMIC_OR(&h1c->wait_event.tasklet->state, TASK_F_USR1); |
Olivier Houchard | 3c49c1b | 2020-03-22 19:56:03 +0100 | [diff] [blame] | 3485 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
Willy Tarreau | 4f8cd43 | 2021-03-02 17:27:58 +0100 | [diff] [blame] | 3486 | xprt_set_idle(h1c->conn, h1c->conn->xprt, h1c->conn->xprt_ctx); |
| 3487 | |
Olivier Houchard | dc2f275 | 2020-02-13 19:12:07 +0100 | [diff] [blame] | 3488 | if (!srv_add_to_idle_list(objt_server(h1c->conn->target), h1c->conn, is_not_first)) { |
Olivier Houchard | 2444aa5 | 2020-01-20 13:56:01 +0100 | [diff] [blame] | 3489 | /* The server doesn't want it, let's kill the connection right away */ |
| 3490 | h1c->conn->mux->destroy(h1c); |
| 3491 | TRACE_DEVEL("outgoing connection killed", H1_EV_STRM_END|H1_EV_H1C_END); |
| 3492 | goto end; |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 3493 | } |
Olivier Houchard | 199d4fa | 2020-03-22 23:25:51 +0100 | [diff] [blame] | 3494 | /* At this point, the connection has been added to the |
| 3495 | * server idle list, so another thread may already have |
| 3496 | * hijacked it, so we can't do anything with it. |
| 3497 | */ |
Olivier Houchard | 2444aa5 | 2020-01-20 13:56:01 +0100 | [diff] [blame] | 3498 | return; |
Christopher Faulet | 9400a39 | 2018-11-23 23:10:39 +0100 | [diff] [blame] | 3499 | } |
| 3500 | } |
| 3501 | |
Christopher Faulet | f1204b8 | 2019-07-19 14:51:06 +0200 | [diff] [blame] | 3502 | release: |
Christopher Faulet | 3ac0f43 | 2019-06-28 17:41:42 +0200 | [diff] [blame] | 3503 | /* We don't want to close right now unless the connection is in error or shut down for writes */ |
Christopher Faulet | 71abc0c | 2022-10-04 17:06:52 +0200 | [diff] [blame] | 3504 | if ((h1c->flags & H1C_F_ERROR) || |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3505 | (h1c->state == H1_CS_CLOSED) || |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3506 | (h1c->state == H1_CS_CLOSING && !b_data(&h1c->obuf)) || |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3507 | !h1c->conn->owner) { |
| 3508 | TRACE_DEVEL("killing dead connection", H1_EV_STRM_END, h1c->conn); |
Christopher Faulet | 73c1207 | 2019-04-08 11:23:22 +0200 | [diff] [blame] | 3509 | h1_release(h1c); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3510 | } |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 3511 | else { |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3512 | if (h1c->state == H1_CS_IDLE) { |
Christopher Faulet | 5d3c93c | 2020-10-05 17:14:28 +0200 | [diff] [blame] | 3513 | /* If we have a new request, process it immediately or |
| 3514 | * subscribe for reads waiting for new data |
| 3515 | */ |
Christopher Faulet | 0c366a8 | 2020-12-18 15:13:47 +0100 | [diff] [blame] | 3516 | if (unlikely(b_data(&h1c->ibuf))) { |
| 3517 | if (h1_process(h1c) == -1) |
| 3518 | goto end; |
| 3519 | } |
Christopher Faulet | 5d3c93c | 2020-10-05 17:14:28 +0200 | [diff] [blame] | 3520 | else |
| 3521 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
| 3522 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3523 | h1_set_idle_expiration(h1c); |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 3524 | h1_refresh_timeout(h1c); |
Christopher Faulet | b8093cf | 2019-01-03 16:27:28 +0100 | [diff] [blame] | 3525 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3526 | end: |
| 3527 | TRACE_LEAVE(H1_EV_STRM_END); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3528 | } |
| 3529 | |
| 3530 | |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 3531 | static void h1_shutr(struct stconn *sc, enum co_shr_mode mode) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3532 | { |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 3533 | struct h1s *h1s = __sc_mux_strm(sc); |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 3534 | struct h1c *h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3535 | |
| 3536 | if (!h1s) |
| 3537 | return; |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 3538 | h1c = h1s->h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3539 | |
Christopher Faulet | c3fe6f3 | 2022-10-05 10:24:11 +0200 | [diff] [blame] | 3540 | TRACE_POINT(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode}); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3541 | } |
| 3542 | |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 3543 | static void h1_shutw(struct stconn *sc, enum co_shw_mode mode) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3544 | { |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 3545 | struct h1s *h1s = __sc_mux_strm(sc); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3546 | struct h1c *h1c; |
| 3547 | |
| 3548 | if (!h1s) |
| 3549 | return; |
| 3550 | h1c = h1s->h1c; |
| 3551 | |
Christopher Faulet | 99293b0 | 2021-11-10 10:30:15 +0100 | [diff] [blame] | 3552 | TRACE_ENTER(H1_EV_STRM_SHUT, h1c->conn, h1s, 0, (size_t[]){mode}); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3553 | |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 3554 | if (se_fl_test(h1s->sd, SE_FL_KILL_CONN)) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3555 | TRACE_STATE("stream wants to kill the connection", H1_EV_STRM_SHUT, h1c->conn, h1s); |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 3556 | goto do_shutw; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3557 | } |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 3558 | if (h1c->state == H1_CS_CLOSING || (h1c->flags & (H1C_F_EOS|H1C_F_ERR_PENDING|H1C_F_ERROR))) { |
| 3559 | TRACE_STATE("shutdown on connection (EOS || CLOSING || ERROR)", H1_EV_STRM_SHUT, h1c->conn, h1s); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3560 | goto do_shutw; |
| 3561 | } |
Olivier Houchard | d2e88c7 | 2018-12-19 15:55:23 +0100 | [diff] [blame] | 3562 | |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 3563 | if (h1c->state == H1_CS_UPGRADING) { |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3564 | TRACE_STATE("keep connection alive (UPGRADING)", H1_EV_STRM_SHUT, h1c->conn, h1s); |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 3565 | goto end; |
| 3566 | } |
Christopher Faulet | c4bfa59 | 2020-10-06 17:45:34 +0200 | [diff] [blame] | 3567 | if (((h1s->flags & H1S_F_WANT_KAL) && h1s->req.state == H1_MSG_DONE && h1s->res.state == H1_MSG_DONE)) { |
| 3568 | TRACE_STATE("keep connection alive (want_kal)", H1_EV_STRM_SHUT, h1c->conn, h1s); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3569 | goto end; |
| 3570 | } |
Christopher Faulet | f2824e6 | 2018-10-01 12:12:37 +0200 | [diff] [blame] | 3571 | |
Christopher Faulet | 7f36636 | 2019-04-08 10:51:20 +0200 | [diff] [blame] | 3572 | do_shutw: |
Christopher Faulet | 061a098 | 2022-11-29 17:16:30 +0100 | [diff] [blame] | 3573 | h1_close(h1c); |
Christopher Faulet | 0797656 | 2022-03-31 11:05:05 +0200 | [diff] [blame] | 3574 | if (mode != CO_SHW_NORMAL) |
Christopher Faulet | 71abc0c | 2022-10-04 17:06:52 +0200 | [diff] [blame] | 3575 | h1c->flags |= H1C_F_SILENT_SHUT; |
Christopher Faulet | a85c522 | 2021-10-27 15:36:38 +0200 | [diff] [blame] | 3576 | |
Christopher Faulet | 3c82d8b | 2020-10-05 17:11:16 +0200 | [diff] [blame] | 3577 | if (!b_data(&h1c->obuf)) |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3578 | h1_shutw_conn(h1c->conn); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3579 | end: |
| 3580 | TRACE_LEAVE(H1_EV_STRM_SHUT, h1c->conn, h1s); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3581 | } |
| 3582 | |
Christopher Faulet | a85c522 | 2021-10-27 15:36:38 +0200 | [diff] [blame] | 3583 | static void h1_shutw_conn(struct connection *conn) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3584 | { |
Willy Tarreau | 3d2ee55 | 2018-12-19 14:12:10 +0100 | [diff] [blame] | 3585 | struct h1c *h1c = conn->ctx; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3586 | |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3587 | TRACE_ENTER(H1_EV_H1C_END, conn); |
| 3588 | h1_close(h1c); |
Willy Tarreau | 62592ad | 2021-03-26 09:09:42 +0100 | [diff] [blame] | 3589 | if (conn->flags & CO_FL_SOCK_WR_SH) |
| 3590 | return; |
| 3591 | |
Christopher Faulet | 666a0c4 | 2019-01-08 11:12:04 +0100 | [diff] [blame] | 3592 | conn_xprt_shutw(conn); |
Christopher Faulet | ce7928d | 2022-11-18 08:44:44 +0100 | [diff] [blame] | 3593 | conn_sock_shutw(conn, !(h1c->flags & H1C_F_SILENT_SHUT)); |
Christopher Faulet | 551b896 | 2023-03-24 09:26:16 +0100 | [diff] [blame] | 3594 | |
| 3595 | if (h1c->wait_event.tasklet && !h1c->wait_event.events) |
| 3596 | tasklet_wakeup(h1c->wait_event.tasklet); |
| 3597 | |
Christopher Faulet | a85c522 | 2021-10-27 15:36:38 +0200 | [diff] [blame] | 3598 | TRACE_LEAVE(H1_EV_H1C_END, conn); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3599 | } |
| 3600 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3601 | /* Called from the upper layer, to unsubscribe <es> from events <event_type> |
| 3602 | * The <es> pointer is not allowed to differ from the one passed to the |
| 3603 | * subscribe() call. It always returns zero. |
| 3604 | */ |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 3605 | static int h1_unsubscribe(struct stconn *sc, int event_type, struct wait_event *es) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3606 | { |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 3607 | struct h1s *h1s = __sc_mux_strm(sc); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3608 | |
| 3609 | if (!h1s) |
| 3610 | return 0; |
| 3611 | |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 3612 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3613 | BUG_ON(h1s->subs && h1s->subs != es); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 3614 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3615 | es->events &= ~event_type; |
| 3616 | if (!es->events) |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 3617 | h1s->subs = NULL; |
| 3618 | |
| 3619 | if (event_type & SUB_RETRY_RECV) |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3620 | TRACE_DEVEL("unsubscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 3621 | |
| 3622 | if (event_type & SUB_RETRY_SEND) |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3623 | TRACE_DEVEL("unsubscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 3624 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3625 | return 0; |
| 3626 | } |
| 3627 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3628 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 3629 | * event subscriber <es> is not allowed to change from a previous call as long |
| 3630 | * as at least one event is still subscribed. The <event_type> must only be a |
| 3631 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, unless |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 3632 | * the stream connector <sc> was already detached, in which case it will return |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 3633 | * -1. |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3634 | */ |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 3635 | static int h1_subscribe(struct stconn *sc, int event_type, struct wait_event *es) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3636 | { |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 3637 | struct h1s *h1s = __sc_mux_strm(sc); |
Christopher Faulet | 51bb185 | 2019-09-04 10:22:34 +0200 | [diff] [blame] | 3638 | struct h1c *h1c; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3639 | |
| 3640 | if (!h1s) |
| 3641 | return -1; |
| 3642 | |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 3643 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3644 | BUG_ON(h1s->subs && h1s->subs != es); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 3645 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 3646 | es->events |= event_type; |
| 3647 | h1s->subs = es; |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 3648 | |
| 3649 | if (event_type & SUB_RETRY_RECV) |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3650 | TRACE_DEVEL("subscribe(recv)", H1_EV_STRM_RECV, h1s->h1c->conn, h1s); |
Willy Tarreau | 1b0d4d1 | 2020-01-16 11:03:19 +0100 | [diff] [blame] | 3651 | |
| 3652 | |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3653 | if (event_type & SUB_RETRY_SEND) { |
| 3654 | TRACE_DEVEL("subscribe(send)", H1_EV_STRM_SEND, h1s->h1c->conn, h1s); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3655 | /* |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 3656 | * If the stconn attempts to subscribe, and the |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3657 | * mux isn't subscribed to the connection, then it |
| 3658 | * probably means the connection wasn't established |
| 3659 | * yet, so we have to subscribe. |
| 3660 | */ |
| 3661 | h1c = h1s->h1c; |
| 3662 | if (!(h1c->wait_event.events & SUB_RETRY_SEND)) |
| 3663 | h1c->conn->xprt->subscribe(h1c->conn, |
| 3664 | h1c->conn->xprt_ctx, |
| 3665 | SUB_RETRY_SEND, |
| 3666 | &h1c->wait_event); |
| 3667 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3668 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3669 | } |
| 3670 | |
Christopher Faulet | 564e39c | 2021-09-21 15:50:55 +0200 | [diff] [blame] | 3671 | /* Called from the upper layer, to receive data. |
| 3672 | * |
| 3673 | * The caller is responsible for defragmenting <buf> if necessary. But <flags> |
| 3674 | * must be tested to know the calling context. If CO_RFL_BUF_FLUSH is set, it |
| 3675 | * means the caller wants to flush input data (from the mux buffer and the |
| 3676 | * channel buffer) to be able to use kernel splicing or any kind of mux-to-mux |
| 3677 | * xfer. If CO_RFL_KEEP_RECV is set, the mux must always subscribe for read |
| 3678 | * events before giving back. CO_RFL_BUF_WET is set if <buf> is congested with |
| 3679 | * data scheduled for leaving soon. CO_RFL_BUF_NOT_STUCK is set to instruct the |
| 3680 | * mux it may optimize the data copy to <buf> if necessary. Otherwise, it should |
| 3681 | * copy as much data as possible. |
| 3682 | */ |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 3683 | static size_t h1_rcv_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3684 | { |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 3685 | struct h1s *h1s = __sc_mux_strm(sc); |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 3686 | struct h1c *h1c = h1s->h1c; |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 3687 | struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3688 | size_t ret = 0; |
| 3689 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 3690 | TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count}); |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 3691 | |
Christopher Faulet | 4e72b17 | 2022-10-04 17:35:19 +0200 | [diff] [blame] | 3692 | /* Do nothing for now if not RUNNING (implies UPGRADING) */ |
| 3693 | if (h1c->state < H1_CS_RUNNING) { |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 3694 | TRACE_DEVEL("h1c not ready yet", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn); |
| 3695 | goto end; |
| 3696 | } |
| 3697 | |
Christopher Faulet | 539e029 | 2018-11-19 10:40:09 +0100 | [diff] [blame] | 3698 | if (!(h1c->flags & H1C_F_IN_ALLOC)) |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 3699 | ret = h1_process_demux(h1c, buf, count); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3700 | else |
| 3701 | TRACE_DEVEL("h1c ibuf not allocated", H1_EV_H1C_RECV|H1_EV_H1C_BLK, h1c->conn); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3702 | |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 3703 | if ((flags & CO_RFL_BUF_FLUSH) && se_fl_test(h1s->sd, SE_FL_MAY_SPLICE)) { |
Christopher Faulet | 2b861bf | 2021-04-06 17:24:39 +0200 | [diff] [blame] | 3704 | h1c->flags |= H1C_F_WANT_SPLICE; |
| 3705 | TRACE_STATE("Block xprt rcv_buf to flush stream's buffer (want_splice)", H1_EV_STRM_RECV, h1c->conn, h1s); |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 3706 | } |
Olivier Houchard | 02bac85 | 2019-08-22 18:34:25 +0200 | [diff] [blame] | 3707 | else { |
Christopher Faulet | 1baef15 | 2021-04-08 18:42:59 +0200 | [diff] [blame] | 3708 | if (((flags & CO_RFL_KEEP_RECV) || (h1m->state != H1_MSG_DONE)) && !(h1c->wait_event.events & SUB_RETRY_RECV)) |
Willy Tarreau | 2c1f37d | 2020-03-04 17:50:02 +0100 | [diff] [blame] | 3709 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3710 | } |
Christopher Faulet | 39c7b6b | 2021-01-21 17:44:35 +0100 | [diff] [blame] | 3711 | |
| 3712 | end: |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 3713 | TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret}); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3714 | return ret; |
| 3715 | } |
| 3716 | |
| 3717 | |
| 3718 | /* Called from the upper layer, to send data */ |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 3719 | static size_t h1_snd_buf(struct stconn *sc, struct buffer *buf, size_t count, int flags) |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3720 | { |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 3721 | struct h1s *h1s = __sc_mux_strm(sc); |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3722 | struct h1c *h1c; |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 3723 | size_t total = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3724 | |
| 3725 | if (!h1s) |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 3726 | return 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3727 | h1c = h1s->h1c; |
Olivier Houchard | 305d5ab | 2019-07-24 18:07:06 +0200 | [diff] [blame] | 3728 | |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 3729 | TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){count}); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3730 | |
Olivier Houchard | 305d5ab | 2019-07-24 18:07:06 +0200 | [diff] [blame] | 3731 | /* If we're not connected yet, or we're waiting for a handshake, stop |
| 3732 | * now, as we don't want to remove everything from the channel buffer |
| 3733 | * before we're sure we can send it. |
| 3734 | */ |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 3735 | if (h1c->conn->flags & CO_FL_WAIT_XPRT) { |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3736 | TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s); |
Christopher Faulet | 3b88b8d | 2018-10-26 17:36:03 +0200 | [diff] [blame] | 3737 | return 0; |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3738 | } |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3739 | |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 3740 | if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) { |
| 3741 | se_fl_set_error(h1s->sd); |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 3742 | TRACE_ERROR("H1C on error, leaving in error", H1_EV_STRM_SEND|H1_EV_H1C_ERR|H1_EV_H1S_ERR|H1_EV_STRM_ERR, h1c->conn, h1s); |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 3743 | return 0; |
| 3744 | } |
| 3745 | |
Christopher Faulet | 4623036 | 2019-10-17 16:04:20 +0200 | [diff] [blame] | 3746 | /* Inherit some flags from the upper layer */ |
| 3747 | h1c->flags &= ~(H1C_F_CO_MSG_MORE|H1C_F_CO_STREAMER); |
| 3748 | if (flags & CO_SFL_MSG_MORE) |
| 3749 | h1c->flags |= H1C_F_CO_MSG_MORE; |
| 3750 | if (flags & CO_SFL_STREAMER) |
| 3751 | h1c->flags |= H1C_F_CO_STREAMER; |
| 3752 | |
Christopher Faulet | c31872f | 2019-06-04 22:09:36 +0200 | [diff] [blame] | 3753 | while (count) { |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 3754 | size_t ret = 0; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3755 | |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 3756 | if (!(h1c->flags & (H1C_F_OUT_FULL|H1C_F_OUT_ALLOC))) |
Christopher Faulet | 44c0dcf | 2021-02-16 18:32:08 +0100 | [diff] [blame] | 3757 | ret = h1_process_mux(h1c, buf, count); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3758 | else |
| 3759 | TRACE_DEVEL("h1c obuf not allocated", H1_EV_STRM_SEND|H1_EV_H1S_BLK, h1c->conn, h1s); |
Olivier Houchard | c89a42f | 2020-06-19 16:15:05 +0200 | [diff] [blame] | 3760 | |
Christopher Faulet | b0b8e9b | 2022-09-15 16:21:55 +0200 | [diff] [blame] | 3761 | if (!ret) |
Christopher Faulet | 372b38f | 2022-07-08 15:20:31 +0200 | [diff] [blame] | 3762 | break; |
| 3763 | |
Olivier Houchard | c89a42f | 2020-06-19 16:15:05 +0200 | [diff] [blame] | 3764 | if ((count - ret) > 0) |
| 3765 | h1c->flags |= H1C_F_CO_MSG_MORE; |
| 3766 | |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 3767 | total += ret; |
Christopher Faulet | c31872f | 2019-06-04 22:09:36 +0200 | [diff] [blame] | 3768 | count -= ret; |
Christopher Faulet | b0b8e9b | 2022-09-15 16:21:55 +0200 | [diff] [blame] | 3769 | |
Olivier Houchard | 68787ef | 2020-01-15 19:13:32 +0100 | [diff] [blame] | 3770 | if ((h1c->wait_event.events & SUB_RETRY_SEND) || !h1_send(h1c)) |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 3771 | break; |
Christopher Faulet | b0b8e9b | 2022-09-15 16:21:55 +0200 | [diff] [blame] | 3772 | |
| 3773 | if ((h1c->conn->flags & (CO_FL_ERROR|CO_FL_SOCK_WR_SH))) |
| 3774 | break; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3775 | } |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 3776 | |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 3777 | if (h1c->flags & (H1C_F_ERR_PENDING|H1C_F_ERROR)) { |
| 3778 | // FIXME: following test was removed : |
| 3779 | // ((h1c->conn->flags & CO_FL_ERROR) && (se_fl_test(h1s->sd, SE_FL_EOI | SE_FL_EOS) || !b_data(&h1c->ibuf)))) { |
| 3780 | se_fl_set_error(h1s->sd); |
Christopher Faulet | 26a2643 | 2021-01-27 11:27:50 +0100 | [diff] [blame] | 3781 | TRACE_ERROR("reporting error to the app-layer stream", H1_EV_STRM_SEND|H1_EV_H1S_ERR|H1_EV_STRM_ERR, h1c->conn, h1s); |
Christopher Faulet | dea2474 | 2021-01-22 15:12:30 +0100 | [diff] [blame] | 3782 | } |
| 3783 | |
Christopher Faulet | 7a145d6 | 2020-08-05 11:31:16 +0200 | [diff] [blame] | 3784 | h1_refresh_timeout(h1c); |
Willy Tarreau | 022e5e5 | 2020-09-10 09:33:15 +0200 | [diff] [blame] | 3785 | TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){total}); |
Christopher Faulet | 5d37dac | 2018-11-22 10:58:42 +0100 | [diff] [blame] | 3786 | return total; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 3787 | } |
| 3788 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 3789 | #if defined(USE_LINUX_SPLICE) |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3790 | /* Send and get, using splicing */ |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 3791 | static int h1_rcv_pipe(struct stconn *sc, struct pipe *pipe, unsigned int count) |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3792 | { |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 3793 | struct h1s *h1s = __sc_mux_strm(sc); |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 3794 | struct h1c *h1c = h1s->h1c; |
| 3795 | struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->req : &h1s->res); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3796 | int ret = 0; |
| 3797 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3798 | TRACE_ENTER(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){count}); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3799 | |
Christopher Faulet | 9fa40c4 | 2019-11-05 16:24:27 +0100 | [diff] [blame] | 3800 | if ((h1m->flags & H1_MF_CHNK) || (h1m->state != H1_MSG_DATA && h1m->state != H1_MSG_TUNNEL)) { |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 3801 | h1c->flags &= ~H1C_F_WANT_SPLICE; |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3802 | TRACE_STATE("Allow xprt rcv_buf on !(msg_data|msg_tunnel)", H1_EV_STRM_RECV, h1c->conn, h1s); |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 3803 | goto end; |
| 3804 | } |
| 3805 | |
Christopher Faulet | cf30756 | 2021-07-26 10:49:39 +0200 | [diff] [blame] | 3806 | h1c->flags |= H1C_F_WANT_SPLICE; |
Willy Tarreau | fbdf90a | 2019-06-03 13:42:54 +0200 | [diff] [blame] | 3807 | if (h1s_data_pending(h1s)) { |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3808 | TRACE_STATE("flush input buffer before splicing", H1_EV_STRM_RECV, h1c->conn, h1s); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3809 | goto end; |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 3810 | } |
| 3811 | |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 3812 | if (!h1_recv_allowed(h1c)) { |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3813 | TRACE_DEVEL("leaving on !recv_allowed", H1_EV_STRM_RECV, h1c->conn, h1s); |
Christopher Faulet | 0060be9 | 2020-07-03 15:02:25 +0200 | [diff] [blame] | 3814 | goto end; |
| 3815 | } |
| 3816 | |
Christopher Faulet | f5ce320 | 2021-11-26 17:26:19 +0100 | [diff] [blame] | 3817 | if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN) && count > h1m->curr_len) |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3818 | count = h1m->curr_len; |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3819 | ret = h1c->conn->xprt->rcv_pipe(h1c->conn, h1c->conn->xprt_ctx, pipe, count); |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3820 | if (ret >= 0) { |
Christopher Faulet | f5ce320 | 2021-11-26 17:26:19 +0100 | [diff] [blame] | 3821 | if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) { |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3822 | if (ret > h1m->curr_len) { |
| 3823 | h1s->flags |= H1S_F_PARSING_ERROR; |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 3824 | se_fl_set(h1s->sd, SE_FL_ERROR); |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3825 | TRACE_ERROR("too much payload, more than announced", |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3826 | H1_EV_RX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); |
Christopher Faulet | 9ff1bc8 | 2023-09-22 09:18:40 +0200 | [diff] [blame] | 3827 | goto out; |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3828 | } |
| 3829 | h1m->curr_len -= ret; |
| 3830 | if (!h1m->curr_len) { |
| 3831 | h1m->state = H1_MSG_DONE; |
| 3832 | h1c->flags &= ~H1C_F_WANT_SPLICE; |
Christopher Faulet | f4b89f1 | 2023-02-23 13:58:13 +0100 | [diff] [blame] | 3833 | |
| 3834 | if (!(h1c->flags & H1C_F_IS_BACK)) { |
| 3835 | /* The request was fully received. It means the H1S now |
| 3836 | * expect data from the opposite side |
| 3837 | */ |
| 3838 | se_expect_data(h1s->sd); |
| 3839 | } |
| 3840 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3841 | TRACE_STATE("payload fully received", H1_EV_STRM_RECV, h1c->conn, h1s); |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3842 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3843 | } |
Christopher Faulet | 41951ab | 2021-11-26 18:12:51 +0100 | [diff] [blame] | 3844 | HA_ATOMIC_ADD(&h1c->px_counters->bytes_in, ret); |
| 3845 | HA_ATOMIC_ADD(&h1c->px_counters->spliced_bytes_in, ret); |
Christopher Faulet | e18777b | 2019-04-16 16:46:36 +0200 | [diff] [blame] | 3846 | } |
| 3847 | |
Christopher Faulet | 9ff1bc8 | 2023-09-22 09:18:40 +0200 | [diff] [blame] | 3848 | out: |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3849 | if (conn_xprt_read0_pending(h1c->conn)) { |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 3850 | se_fl_set(h1s->sd, SE_FL_EOS); |
Christopher Faulet | 147e18f | 2023-03-23 17:29:47 +0100 | [diff] [blame] | 3851 | TRACE_STATE("report EOS to SE", H1_EV_STRM_RECV, h1c->conn, h1s); |
| 3852 | if (h1m->state >= H1_MSG_DONE || !(h1m->flags & H1_MF_XFER_LEN)) { |
| 3853 | /* DONE or TUNNEL or SHUTR without XFER_LEN, set |
| 3854 | * EOI on the stream connector */ |
| 3855 | se_fl_set(h1s->sd, SE_FL_EOI); |
| 3856 | TRACE_STATE("report EOI to SE", H1_EV_STRM_RECV, h1c->conn, h1s); |
| 3857 | } |
| 3858 | else { |
| 3859 | se_fl_set(h1s->sd, SE_FL_ERROR); |
| 3860 | h1c->flags = (h1c->flags & ~H1C_F_WANT_SPLICE) | H1C_F_ERROR; |
| 3861 | TRACE_ERROR("message aborted, set error on SC", H1_EV_STRM_RECV|H1_EV_H1S_ERR, h1c->conn, h1s); |
| 3862 | } |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 3863 | h1c->flags = (h1c->flags & ~H1C_F_WANT_SPLICE) | H1C_F_EOS; |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3864 | TRACE_STATE("Allow xprt rcv_buf on read0", H1_EV_STRM_RECV, h1c->conn, h1s); |
Christopher Faulet | 038ad81 | 2019-04-17 11:03:22 +0200 | [diff] [blame] | 3865 | } |
Christopher Faulet | 9ff1bc8 | 2023-09-22 09:18:40 +0200 | [diff] [blame] | 3866 | end: |
Christopher Faulet | 9009c97 | 2022-10-05 12:04:56 +0200 | [diff] [blame] | 3867 | if (h1c->conn->flags & CO_FL_ERROR) { |
| 3868 | se_fl_set(h1s->sd, SE_FL_ERROR); |
Christopher Faulet | 71abc0c | 2022-10-04 17:06:52 +0200 | [diff] [blame] | 3869 | h1c->flags = (h1c->flags & ~H1C_F_WANT_SPLICE) | H1C_F_ERROR; |
Christopher Faulet | 9009c97 | 2022-10-05 12:04:56 +0200 | [diff] [blame] | 3870 | TRACE_DEVEL("connection error", H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); |
| 3871 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3872 | |
Christopher Faulet | 94d3510 | 2021-04-09 11:58:49 +0200 | [diff] [blame] | 3873 | if (!(h1c->flags & H1C_F_WANT_SPLICE)) { |
Christopher Faulet | 0a799aa | 2020-09-24 09:52:52 +0200 | [diff] [blame] | 3874 | TRACE_STATE("notify the mux can't use splicing anymore", H1_EV_STRM_RECV, h1c->conn, h1s); |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 3875 | se_fl_clr(h1s->sd, SE_FL_MAY_SPLICE); |
Christopher Faulet | 94d3510 | 2021-04-09 11:58:49 +0200 | [diff] [blame] | 3876 | if (!(h1c->wait_event.events & SUB_RETRY_RECV)) { |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3877 | TRACE_STATE("restart receiving data, subscribing", H1_EV_STRM_RECV, h1c->conn, h1s); |
| 3878 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
Christopher Faulet | 94d3510 | 2021-04-09 11:58:49 +0200 | [diff] [blame] | 3879 | } |
Christopher Faulet | 2718229 | 2020-07-03 15:08:49 +0200 | [diff] [blame] | 3880 | } |
Willy Tarreau | 17ccd1a | 2020-01-17 16:19:34 +0100 | [diff] [blame] | 3881 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3882 | TRACE_LEAVE(H1_EV_STRM_RECV, h1c->conn, h1s, 0, (size_t[]){ret}); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3883 | return ret; |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3884 | } |
| 3885 | |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 3886 | static int h1_snd_pipe(struct stconn *sc, struct pipe *pipe) |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3887 | { |
Willy Tarreau | 000d63c | 2022-05-27 10:39:17 +0200 | [diff] [blame] | 3888 | struct h1s *h1s = __sc_mux_strm(sc); |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3889 | struct h1c *h1c = h1s->h1c; |
| 3890 | struct h1m *h1m = (!(h1c->flags & H1C_F_IS_BACK) ? &h1s->res : &h1s->req); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3891 | int ret = 0; |
| 3892 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3893 | TRACE_ENTER(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){pipe->data}); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3894 | |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3895 | if (b_data(&h1c->obuf)) { |
| 3896 | if (!(h1c->wait_event.events & SUB_RETRY_SEND)) { |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3897 | TRACE_STATE("more data to send, subscribing", H1_EV_STRM_SEND, h1c->conn, h1s); |
| 3898 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_SEND, &h1c->wait_event); |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3899 | } |
Christopher Faulet | 8454f2d | 2021-04-06 17:27:32 +0200 | [diff] [blame] | 3900 | goto end; |
Christopher Faulet | d44ad5b | 2018-11-19 21:52:12 +0100 | [diff] [blame] | 3901 | } |
Christopher Faulet | 6b81df7 | 2019-10-01 22:08:43 +0200 | [diff] [blame] | 3902 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3903 | ret = h1c->conn->xprt->snd_pipe(h1c->conn, h1c->conn->xprt_ctx, pipe); |
Christopher Faulet | f5ce320 | 2021-11-26 17:26:19 +0100 | [diff] [blame] | 3904 | if (h1m->state == H1_MSG_DATA && (h1m->flags & H1_MF_CLEN)) { |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3905 | if (ret > h1m->curr_len) { |
| 3906 | h1s->flags |= H1S_F_PROCESSING_ERROR; |
Willy Tarreau | 1a0d9ac | 2022-05-27 16:12:05 +0200 | [diff] [blame] | 3907 | se_fl_set(h1s->sd, SE_FL_ERROR); |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3908 | TRACE_ERROR("too much payload, more than announced", |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3909 | H1_EV_TX_DATA|H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3910 | goto end; |
| 3911 | } |
| 3912 | h1m->curr_len -= ret; |
| 3913 | if (!h1m->curr_len) { |
| 3914 | h1m->state = H1_MSG_DONE; |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3915 | TRACE_STATE("payload fully xferred", H1_EV_TX_DATA|H1_EV_TX_BODY, h1c->conn, h1s); |
Christopher Faulet | 140f1a5 | 2021-11-26 16:37:55 +0100 | [diff] [blame] | 3916 | } |
| 3917 | } |
Christopher Faulet | 41951ab | 2021-11-26 18:12:51 +0100 | [diff] [blame] | 3918 | HA_ATOMIC_ADD(&h1c->px_counters->bytes_out, ret); |
| 3919 | HA_ATOMIC_ADD(&h1c->px_counters->spliced_bytes_out, ret); |
Christopher Faulet | 8454f2d | 2021-04-06 17:27:32 +0200 | [diff] [blame] | 3920 | |
| 3921 | end: |
Christopher Faulet | 9009c97 | 2022-10-05 12:04:56 +0200 | [diff] [blame] | 3922 | if (h1c->conn->flags & CO_FL_ERROR) { |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 3923 | h1c->flags = (h1c->flags & ~H1C_F_WANT_SPLICE) | H1C_F_ERR_PENDING; |
| 3924 | if (h1c->flags & H1C_F_EOS) |
| 3925 | h1c->flags |= H1C_F_ERROR; |
Christopher Faulet | a462ee0 | 2022-11-22 17:16:22 +0100 | [diff] [blame] | 3926 | else if (!(h1c->wait_event.events & SUB_RETRY_RECV)) { |
| 3927 | /* EOS not seen, so subscribe for reads to be able to |
| 3928 | * catch the error on the reading path. It is especially |
| 3929 | * important if EOI was reached. |
| 3930 | */ |
| 3931 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
| 3932 | } |
Christopher Faulet | fc473a6 | 2022-10-05 08:22:33 +0200 | [diff] [blame] | 3933 | se_fl_set_error(h1s->sd); |
Christopher Faulet | 9009c97 | 2022-10-05 12:04:56 +0200 | [diff] [blame] | 3934 | TRACE_DEVEL("connection error", H1_EV_STRM_ERR|H1_EV_H1C_ERR|H1_EV_H1S_ERR, h1c->conn, h1s); |
| 3935 | } |
| 3936 | |
Christopher Faulet | 897d612 | 2021-12-17 17:28:35 +0100 | [diff] [blame] | 3937 | TRACE_LEAVE(H1_EV_STRM_SEND, h1c->conn, h1s, 0, (size_t[]){ret}); |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 3938 | return ret; |
| 3939 | } |
| 3940 | #endif |
| 3941 | |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 3942 | static int h1_ctl(struct connection *conn, enum mux_ctl_type mux_ctl, void *output) |
| 3943 | { |
Christopher Faulet | 6d197f9 | 2023-11-14 07:45:43 +0100 | [diff] [blame] | 3944 | struct h1c *h1c = conn->ctx; |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 3945 | int ret = 0; |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 3946 | |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 3947 | switch (mux_ctl) { |
| 3948 | case MUX_STATUS: |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 3949 | if (!(conn->flags & CO_FL_WAIT_XPRT)) |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 3950 | ret |= MUX_STATUS_READY; |
| 3951 | return ret; |
Christopher Faulet | 4c8ad84 | 2020-10-06 14:59:17 +0200 | [diff] [blame] | 3952 | case MUX_EXIT_STATUS: |
Christopher Faulet | 36e46aa | 2021-09-28 11:45:05 +0200 | [diff] [blame] | 3953 | if (output) |
| 3954 | *((int *)output) = h1c->errcode; |
| 3955 | ret = (h1c->errcode == 408 ? MUX_ES_TOUT_ERR : |
| 3956 | (h1c->errcode == 501 ? MUX_ES_NOTIMPL_ERR : |
| 3957 | (h1c->errcode == 500 ? MUX_ES_INTERNAL_ERR : |
| 3958 | ((h1c->errcode >= 400 && h1c->errcode <= 499) ? MUX_ES_INVALID_ERR : |
Christopher Faulet | 2eed800 | 2020-12-07 11:26:13 +0100 | [diff] [blame] | 3959 | MUX_ES_SUCCESS)))); |
Christopher Faulet | c18fc23 | 2020-10-06 17:41:36 +0200 | [diff] [blame] | 3960 | return ret; |
Christopher Faulet | 6d197f9 | 2023-11-14 07:45:43 +0100 | [diff] [blame] | 3961 | case MUX_SUBS_RECV: |
| 3962 | if (!(h1c->wait_event.events & SUB_RETRY_RECV)) |
| 3963 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, SUB_RETRY_RECV, &h1c->wait_event); |
| 3964 | return 0; |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 3965 | default: |
| 3966 | return -1; |
| 3967 | } |
| 3968 | } |
| 3969 | |
Willy Tarreau | 7079c0f | 2022-09-02 16:11:28 +0200 | [diff] [blame] | 3970 | /* appends some info about connection <h1c> to buffer <msg>, or does nothing if |
| 3971 | * <h1c> is NULL. Returns non-zero if the connection is considered suspicious. |
| 3972 | * May emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is |
| 3973 | * not NULL, otherwise a single line is used. |
| 3974 | */ |
| 3975 | static int h1_dump_h1c_info(struct buffer *msg, struct h1c *h1c, const char *pfx) |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 3976 | { |
Willy Tarreau | 0c0c0a2 | 2021-01-21 09:13:35 +0100 | [diff] [blame] | 3977 | int ret = 0; |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 3978 | |
Willy Tarreau | 7079c0f | 2022-09-02 16:11:28 +0200 | [diff] [blame] | 3979 | if (!h1c) |
| 3980 | return ret; |
| 3981 | |
Christopher Faulet | f376a31 | 2019-01-04 15:16:06 +0100 | [diff] [blame] | 3982 | chunk_appendf(msg, " h1c.flg=0x%x .sub=%d .ibuf=%u@%p+%u/%u .obuf=%u@%p+%u/%u", |
| 3983 | h1c->flags, h1c->wait_event.events, |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 3984 | (unsigned int)b_data(&h1c->ibuf), b_orig(&h1c->ibuf), |
| 3985 | (unsigned int)b_head_ofs(&h1c->ibuf), (unsigned int)b_size(&h1c->ibuf), |
Willy Tarreau | 7079c0f | 2022-09-02 16:11:28 +0200 | [diff] [blame] | 3986 | (unsigned int)b_data(&h1c->obuf), b_orig(&h1c->obuf), |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 3987 | (unsigned int)b_head_ofs(&h1c->obuf), (unsigned int)b_size(&h1c->obuf)); |
Christopher Faulet | 38f6135 | 2022-11-30 14:49:56 +0100 | [diff] [blame] | 3988 | |
| 3989 | chunk_appendf(msg, " .task=%p", h1c->task); |
| 3990 | if (h1c->task) { |
| 3991 | chunk_appendf(msg, " .exp=%s", |
| 3992 | h1c->task->expire ? tick_is_expired(h1c->task->expire, now_ms) ? "<PAST>" : |
| 3993 | human_time(TICKS_TO_MS(h1c->task->expire - now_ms), TICKS_TO_MS(1000)) : "<NEVER>"); |
| 3994 | } |
| 3995 | |
Willy Tarreau | 7079c0f | 2022-09-02 16:11:28 +0200 | [diff] [blame] | 3996 | return ret; |
| 3997 | } |
| 3998 | |
| 3999 | /* appends some info about stream <h1s> to buffer <msg>, or does nothing if |
| 4000 | * <h1s> is NULL. Returns non-zero if the stream is considered suspicious. May |
| 4001 | * emit multiple lines, each new one being prefixed with <pfx>, if <pfx> is not |
| 4002 | * NULL, otherwise a single line is used. |
| 4003 | */ |
| 4004 | static int h1_dump_h1s_info(struct buffer *msg, const struct h1s *h1s, const char *pfx) |
| 4005 | { |
| 4006 | const char *method; |
| 4007 | int ret = 0; |
| 4008 | |
| 4009 | if (!h1s) |
| 4010 | return ret; |
| 4011 | |
| 4012 | if (h1s->meth < HTTP_METH_OTHER) |
| 4013 | method = http_known_methods[h1s->meth].ptr; |
| 4014 | else |
| 4015 | method = "UNKNOWN"; |
| 4016 | |
| 4017 | chunk_appendf(msg, " h1s=%p h1s.flg=0x%x .sd.flg=0x%x .req.state=%s .res.state=%s", |
| 4018 | h1s, h1s->flags, se_fl_get(h1s->sd), |
| 4019 | h1m_state_str(h1s->req.state), h1m_state_str(h1s->res.state)); |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 4020 | |
Willy Tarreau | 7079c0f | 2022-09-02 16:11:28 +0200 | [diff] [blame] | 4021 | if (pfx) |
| 4022 | chunk_appendf(msg, "\n%s", pfx); |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 4023 | |
Willy Tarreau | 7079c0f | 2022-09-02 16:11:28 +0200 | [diff] [blame] | 4024 | chunk_appendf(msg, " .meth=%s status=%d", |
| 4025 | method, h1s->status); |
Christopher Faulet | 186367f | 2022-05-30 08:45:15 +0200 | [diff] [blame] | 4026 | |
Willy Tarreau | 7079c0f | 2022-09-02 16:11:28 +0200 | [diff] [blame] | 4027 | chunk_appendf(msg, " .sd.flg=0x%08x", se_fl_get(h1s->sd)); |
| 4028 | if (!se_fl_test(h1s->sd, SE_FL_ORPHAN)) |
| 4029 | chunk_appendf(msg, " .sc.flg=0x%08x .sc.app=%p", |
| 4030 | h1s_sc(h1s)->flags, h1s_sc(h1s)->app); |
Christopher Faulet | 186367f | 2022-05-30 08:45:15 +0200 | [diff] [blame] | 4031 | |
Willy Tarreau | 7079c0f | 2022-09-02 16:11:28 +0200 | [diff] [blame] | 4032 | if (pfx && h1s->subs) |
| 4033 | chunk_appendf(msg, "\n%s", pfx); |
| 4034 | |
| 4035 | chunk_appendf(msg, " .subs=%p", h1s->subs); |
| 4036 | if (h1s->subs) { |
| 4037 | chunk_appendf(msg, "(ev=%d tl=%p", h1s->subs->events, h1s->subs->tasklet); |
| 4038 | chunk_appendf(msg, " tl.calls=%d tl.ctx=%p tl.fct=", |
| 4039 | h1s->subs->tasklet->calls, |
| 4040 | h1s->subs->tasklet->context); |
| 4041 | if (h1s->subs->tasklet->calls >= 1000000) |
| 4042 | ret = 1; |
| 4043 | resolve_sym_name(msg, NULL, h1s->subs->tasklet->process); |
| 4044 | chunk_appendf(msg, ")"); |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 4045 | } |
Willy Tarreau | 0c0c0a2 | 2021-01-21 09:13:35 +0100 | [diff] [blame] | 4046 | return ret; |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4047 | } |
| 4048 | |
Willy Tarreau | 7079c0f | 2022-09-02 16:11:28 +0200 | [diff] [blame] | 4049 | /* for debugging with CLI's "show fd" command */ |
| 4050 | static int h1_show_fd(struct buffer *msg, struct connection *conn) |
| 4051 | { |
| 4052 | struct h1c *h1c = conn->ctx; |
| 4053 | struct h1s *h1s = h1c->h1s; |
| 4054 | int ret = 0; |
| 4055 | |
| 4056 | ret |= h1_dump_h1c_info(msg, h1c, NULL); |
| 4057 | |
| 4058 | if (h1s) |
| 4059 | ret |= h1_dump_h1s_info(msg, h1s, NULL); |
| 4060 | |
| 4061 | return ret; |
| 4062 | } |
| 4063 | |
Willy Tarreau | e6f389d | 2022-09-02 16:32:31 +0200 | [diff] [blame] | 4064 | /* for debugging with CLI's "show sess" command. May emit multiple lines, each |
| 4065 | * new one being prefixed with <pfx>, if <pfx> is not NULL, otherwise a single |
| 4066 | * line is used. Each field starts with a space so it's safe to print it after |
| 4067 | * existing fields. |
| 4068 | */ |
| 4069 | static int h1_show_sd(struct buffer *msg, struct sedesc *sd, const char *pfx) |
| 4070 | { |
| 4071 | struct h1s *h1s = sd->se; |
| 4072 | int ret = 0; |
| 4073 | |
| 4074 | if (!h1s) |
| 4075 | return ret; |
| 4076 | |
| 4077 | ret |= h1_dump_h1s_info(msg, h1s, pfx); |
| 4078 | if (pfx) |
| 4079 | chunk_appendf(msg, "\n%s", pfx); |
| 4080 | chunk_appendf(msg, " h1c=%p", h1s->h1c); |
| 4081 | ret |= h1_dump_h1c_info(msg, h1s->h1c, pfx); |
| 4082 | return ret; |
| 4083 | } |
| 4084 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4085 | |
| 4086 | /* Add an entry in the headers map. Returns -1 on error and 0 on success. */ |
| 4087 | static int add_hdr_case_adjust(const char *from, const char *to, char **err) |
| 4088 | { |
| 4089 | struct h1_hdr_entry *entry; |
| 4090 | |
| 4091 | /* Be sure there is a non-empty <to> */ |
| 4092 | if (!strlen(to)) { |
| 4093 | memprintf(err, "expect <to>"); |
| 4094 | return -1; |
| 4095 | } |
| 4096 | |
| 4097 | /* Be sure only the case differs between <from> and <to> */ |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 4098 | if (strcasecmp(from, to) != 0) { |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4099 | memprintf(err, "<from> and <to> must not differ execpt the case"); |
| 4100 | return -1; |
| 4101 | } |
| 4102 | |
| 4103 | /* Be sure <from> does not already existsin the tree */ |
| 4104 | if (ebis_lookup(&hdrs_map.map, from)) { |
| 4105 | memprintf(err, "duplicate entry '%s'", from); |
| 4106 | return -1; |
| 4107 | } |
| 4108 | |
| 4109 | /* Create the entry and insert it in the tree */ |
| 4110 | entry = malloc(sizeof(*entry)); |
| 4111 | if (!entry) { |
| 4112 | memprintf(err, "out of memory"); |
| 4113 | return -1; |
| 4114 | } |
| 4115 | |
| 4116 | entry->node.key = strdup(from); |
Tim Duesterhus | dcf753a | 2021-03-04 17:31:47 +0100 | [diff] [blame] | 4117 | entry->name = ist(strdup(to)); |
Tim Duesterhus | 7b5777d | 2021-03-02 18:57:28 +0100 | [diff] [blame] | 4118 | if (!entry->node.key || !isttest(entry->name)) { |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4119 | free(entry->node.key); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4120 | istfree(&entry->name); |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4121 | free(entry); |
| 4122 | memprintf(err, "out of memory"); |
| 4123 | return -1; |
| 4124 | } |
| 4125 | ebis_insert(&hdrs_map.map, &entry->node); |
| 4126 | return 0; |
| 4127 | } |
| 4128 | |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 4129 | /* Migrate the the connection to the current thread. |
| 4130 | * Return 0 if successful, non-zero otherwise. |
| 4131 | * Expected to be called with the old thread lock held. |
| 4132 | */ |
Olivier Houchard | 1662cdb | 2020-07-03 14:04:37 +0200 | [diff] [blame] | 4133 | static int h1_takeover(struct connection *conn, int orig_tid) |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 4134 | { |
| 4135 | struct h1c *h1c = conn->ctx; |
Willy Tarreau | 09e0d9e | 2020-07-01 16:39:33 +0200 | [diff] [blame] | 4136 | struct task *task; |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 4137 | |
| 4138 | if (fd_takeover(conn->handle.fd, conn) != 0) |
| 4139 | return -1; |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 4140 | |
| 4141 | if (conn->xprt->takeover && conn->xprt->takeover(conn, conn->xprt_ctx, orig_tid) != 0) { |
| 4142 | /* We failed to takeover the xprt, even if the connection may |
| 4143 | * still be valid, flag it as error'd, as we have already |
| 4144 | * taken over the fd, and wake the tasklet, so that it will |
| 4145 | * destroy it. |
| 4146 | */ |
| 4147 | conn->flags |= CO_FL_ERROR; |
| 4148 | tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid); |
| 4149 | return -1; |
| 4150 | } |
| 4151 | |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 4152 | if (h1c->wait_event.events) |
| 4153 | h1c->conn->xprt->unsubscribe(h1c->conn, h1c->conn->xprt_ctx, |
| 4154 | h1c->wait_event.events, &h1c->wait_event); |
| 4155 | /* To let the tasklet know it should free itself, and do nothing else, |
| 4156 | * set its context to NULL. |
| 4157 | */ |
| 4158 | h1c->wait_event.tasklet->context = NULL; |
Olivier Houchard | 1662cdb | 2020-07-03 14:04:37 +0200 | [diff] [blame] | 4159 | tasklet_wakeup_on(h1c->wait_event.tasklet, orig_tid); |
Willy Tarreau | 09e0d9e | 2020-07-01 16:39:33 +0200 | [diff] [blame] | 4160 | |
| 4161 | task = h1c->task; |
| 4162 | if (task) { |
| 4163 | task->context = NULL; |
| 4164 | h1c->task = NULL; |
| 4165 | __ha_barrier_store(); |
| 4166 | task_kill(task); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 4167 | |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 4168 | h1c->task = task_new_here(); |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 4169 | if (!h1c->task) { |
| 4170 | h1_release(h1c); |
| 4171 | return -1; |
| 4172 | } |
| 4173 | h1c->task->process = h1_timeout_task; |
| 4174 | h1c->task->context = h1c; |
| 4175 | } |
| 4176 | h1c->wait_event.tasklet = tasklet_new(); |
| 4177 | if (!h1c->wait_event.tasklet) { |
| 4178 | h1_release(h1c); |
| 4179 | return -1; |
| 4180 | } |
| 4181 | h1c->wait_event.tasklet->process = h1_io_cb; |
| 4182 | h1c->wait_event.tasklet->context = h1c; |
| 4183 | h1c->conn->xprt->subscribe(h1c->conn, h1c->conn->xprt_ctx, |
| 4184 | SUB_RETRY_RECV, &h1c->wait_event); |
| 4185 | |
| 4186 | return 0; |
| 4187 | } |
| 4188 | |
| 4189 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4190 | static void h1_hdeaders_case_adjust_deinit() |
| 4191 | { |
| 4192 | struct ebpt_node *node, *next; |
| 4193 | struct h1_hdr_entry *entry; |
| 4194 | |
| 4195 | node = ebpt_first(&hdrs_map.map); |
| 4196 | while (node) { |
| 4197 | next = ebpt_next(node); |
| 4198 | ebpt_delete(node); |
| 4199 | entry = container_of(node, struct h1_hdr_entry, node); |
| 4200 | free(entry->node.key); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4201 | istfree(&entry->name); |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4202 | free(entry); |
| 4203 | node = next; |
| 4204 | } |
| 4205 | free(hdrs_map.name); |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 4206 | } |
| 4207 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4208 | static int cfg_h1_headers_case_adjust_postparser() |
| 4209 | { |
| 4210 | FILE *file = NULL; |
| 4211 | char *c, *key_beg, *key_end, *value_beg, *value_end; |
| 4212 | char *err; |
| 4213 | int rc, line = 0, err_code = 0; |
| 4214 | |
| 4215 | if (!hdrs_map.name) |
| 4216 | goto end; |
| 4217 | |
| 4218 | file = fopen(hdrs_map.name, "r"); |
| 4219 | if (!file) { |
Christopher Faulet | b112b1d | 2022-05-13 09:27:13 +0200 | [diff] [blame] | 4220 | ha_alert("h1-headers-case-adjust-file '%s': failed to open file.\n", |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4221 | hdrs_map.name); |
| 4222 | err_code |= ERR_ALERT | ERR_FATAL; |
| 4223 | goto end; |
| 4224 | } |
| 4225 | |
| 4226 | /* now parse all lines. The file may contain only two header name per |
| 4227 | * line, separated by spaces. All heading and trailing spaces will be |
| 4228 | * ignored. Lines starting with a # are ignored. |
| 4229 | */ |
| 4230 | while (fgets(trash.area, trash.size, file) != NULL) { |
| 4231 | line++; |
| 4232 | c = trash.area; |
| 4233 | |
| 4234 | /* strip leading spaces and tabs */ |
| 4235 | while (*c == ' ' || *c == '\t') |
| 4236 | c++; |
| 4237 | |
| 4238 | /* ignore emptu lines, or lines beginning with a dash */ |
| 4239 | if (*c == '#' || *c == '\0' || *c == '\r' || *c == '\n') |
| 4240 | continue; |
| 4241 | |
| 4242 | /* look for the end of the key */ |
| 4243 | key_beg = c; |
| 4244 | while (*c != '\0' && *c != ' ' && *c != '\t' && *c != '\n' && *c != '\r') |
| 4245 | c++; |
| 4246 | key_end = c; |
| 4247 | |
| 4248 | /* strip middle spaces and tabs */ |
| 4249 | while (*c == ' ' || *c == '\t') |
| 4250 | c++; |
| 4251 | |
| 4252 | /* look for the end of the value, it is the end of the line */ |
| 4253 | value_beg = c; |
| 4254 | while (*c && *c != '\n' && *c != '\r') |
| 4255 | c++; |
| 4256 | value_end = c; |
| 4257 | |
| 4258 | /* trim possibly trailing spaces and tabs */ |
| 4259 | while (value_end > value_beg && (value_end[-1] == ' ' || value_end[-1] == '\t')) |
| 4260 | value_end--; |
| 4261 | |
| 4262 | /* set final \0 and check entries */ |
| 4263 | *key_end = '\0'; |
| 4264 | *value_end = '\0'; |
| 4265 | |
| 4266 | err = NULL; |
| 4267 | rc = add_hdr_case_adjust(key_beg, value_beg, &err); |
| 4268 | if (rc < 0) { |
Christopher Faulet | b112b1d | 2022-05-13 09:27:13 +0200 | [diff] [blame] | 4269 | ha_alert("h1-headers-case-adjust-file '%s' : %s at line %d.\n", |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4270 | hdrs_map.name, err, line); |
| 4271 | err_code |= ERR_ALERT | ERR_FATAL; |
Christopher Faulet | cac5c09 | 2019-07-30 16:51:42 +0200 | [diff] [blame] | 4272 | free(err); |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4273 | goto end; |
| 4274 | } |
| 4275 | if (rc > 0) { |
Christopher Faulet | b112b1d | 2022-05-13 09:27:13 +0200 | [diff] [blame] | 4276 | ha_warning("h1-headers-case-adjust-file '%s' : %s at line %d.\n", |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4277 | hdrs_map.name, err, line); |
| 4278 | err_code |= ERR_WARN; |
Christopher Faulet | cac5c09 | 2019-07-30 16:51:42 +0200 | [diff] [blame] | 4279 | free(err); |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4280 | } |
| 4281 | } |
| 4282 | |
| 4283 | end: |
| 4284 | if (file) |
| 4285 | fclose(file); |
| 4286 | hap_register_post_deinit(h1_hdeaders_case_adjust_deinit); |
| 4287 | return err_code; |
| 4288 | } |
| 4289 | |
Christopher Faulet | 0f9c0f5 | 2022-05-13 09:20:13 +0200 | [diff] [blame] | 4290 | /* config parser for global "h1-accept-payload_=-with-any-method" */ |
| 4291 | static int cfg_parse_h1_accept_payload_with_any_method(char **args, int section_type, struct proxy *curpx, |
| 4292 | const struct proxy *defpx, const char *file, int line, |
| 4293 | char **err) |
| 4294 | { |
| 4295 | if (too_many_args(0, args, err, NULL)) |
| 4296 | return -1; |
| 4297 | accept_payload_with_any_method = 1; |
| 4298 | return 0; |
| 4299 | } |
| 4300 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4301 | |
Christopher Faulet | b112b1d | 2022-05-13 09:27:13 +0200 | [diff] [blame] | 4302 | /* config parser for global "h1-header-case-adjust" */ |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4303 | static int cfg_parse_h1_header_case_adjust(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 4304 | const struct proxy *defpx, const char *file, int line, |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4305 | char **err) |
| 4306 | { |
| 4307 | if (too_many_args(2, args, err, NULL)) |
| 4308 | return -1; |
| 4309 | if (!*(args[1]) || !*(args[2])) { |
| 4310 | memprintf(err, "'%s' expects <from> and <to> as argument.", args[0]); |
| 4311 | return -1; |
| 4312 | } |
| 4313 | return add_hdr_case_adjust(args[1], args[2], err); |
| 4314 | } |
| 4315 | |
Christopher Faulet | b112b1d | 2022-05-13 09:27:13 +0200 | [diff] [blame] | 4316 | /* config parser for global "h1-headers-case-adjust-file" */ |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4317 | static int cfg_parse_h1_headers_case_adjust_file(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 4318 | const struct proxy *defpx, const char *file, int line, |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4319 | char **err) |
| 4320 | { |
| 4321 | if (too_many_args(1, args, err, NULL)) |
| 4322 | return -1; |
| 4323 | if (!*(args[1])) { |
| 4324 | memprintf(err, "'%s' expects <file> as argument.", args[0]); |
| 4325 | return -1; |
| 4326 | } |
| 4327 | free(hdrs_map.name); |
| 4328 | hdrs_map.name = strdup(args[1]); |
| 4329 | return 0; |
| 4330 | } |
| 4331 | |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4332 | /* config keyword parsers */ |
| 4333 | static struct cfg_kw_list cfg_kws = {{ }, { |
Christopher Faulet | 0f9c0f5 | 2022-05-13 09:20:13 +0200 | [diff] [blame] | 4334 | { CFG_GLOBAL, "h1-accept-payload-with-any-method", cfg_parse_h1_accept_payload_with_any_method }, |
Christopher Faulet | 98fbe95 | 2019-07-22 16:18:24 +0200 | [diff] [blame] | 4335 | { CFG_GLOBAL, "h1-case-adjust", cfg_parse_h1_header_case_adjust }, |
| 4336 | { CFG_GLOBAL, "h1-case-adjust-file", cfg_parse_h1_headers_case_adjust_file }, |
| 4337 | { 0, NULL, NULL }, |
| 4338 | } |
| 4339 | }; |
| 4340 | |
| 4341 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 4342 | REGISTER_CONFIG_POSTPARSER("h1-headers-map", cfg_h1_headers_case_adjust_postparser); |
| 4343 | |
| 4344 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4345 | /****************************************/ |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 4346 | /* MUX initialization and instantiation */ |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4347 | /****************************************/ |
| 4348 | |
| 4349 | /* The mux operations */ |
Christopher Faulet | 3f612f7 | 2021-02-05 16:44:21 +0100 | [diff] [blame] | 4350 | static const struct mux_ops mux_http_ops = { |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4351 | .init = h1_init, |
| 4352 | .wake = h1_wake, |
| 4353 | .attach = h1_attach, |
Willy Tarreau | d137353 | 2022-05-27 11:00:59 +0200 | [diff] [blame] | 4354 | .get_first_sc = h1_get_first_sc, |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4355 | .detach = h1_detach, |
| 4356 | .destroy = h1_destroy, |
| 4357 | .avail_streams = h1_avail_streams, |
Willy Tarreau | 00f18a3 | 2019-01-26 12:19:01 +0100 | [diff] [blame] | 4358 | .used_streams = h1_used_streams, |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4359 | .rcv_buf = h1_rcv_buf, |
| 4360 | .snd_buf = h1_snd_buf, |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 4361 | #if defined(USE_LINUX_SPLICE) |
Christopher Faulet | 1be55f9 | 2018-10-02 15:59:23 +0200 | [diff] [blame] | 4362 | .rcv_pipe = h1_rcv_pipe, |
| 4363 | .snd_pipe = h1_snd_pipe, |
| 4364 | #endif |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4365 | .subscribe = h1_subscribe, |
| 4366 | .unsubscribe = h1_unsubscribe, |
| 4367 | .shutr = h1_shutr, |
| 4368 | .shutw = h1_shutw, |
Olivier Houchard | a8f6b43 | 2018-12-21 15:20:29 +0100 | [diff] [blame] | 4369 | .show_fd = h1_show_fd, |
Willy Tarreau | e6f389d | 2022-09-02 16:32:31 +0200 | [diff] [blame] | 4370 | .show_sd = h1_show_sd, |
Olivier Houchard | 9b8e11e | 2019-10-25 16:19:26 +0200 | [diff] [blame] | 4371 | .ctl = h1_ctl, |
Olivier Houchard | f12ca9f | 2020-02-20 16:00:18 +0100 | [diff] [blame] | 4372 | .takeover = h1_takeover, |
Christopher Faulet | 9f38f5a | 2019-04-03 09:53:32 +0200 | [diff] [blame] | 4373 | .flags = MX_FL_HTX, |
Willy Tarreau | 0a7a4fb | 2019-05-22 11:36:54 +0200 | [diff] [blame] | 4374 | .name = "H1", |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4375 | }; |
| 4376 | |
Christopher Faulet | 3f612f7 | 2021-02-05 16:44:21 +0100 | [diff] [blame] | 4377 | static const struct mux_ops mux_h1_ops = { |
| 4378 | .init = h1_init, |
| 4379 | .wake = h1_wake, |
| 4380 | .attach = h1_attach, |
Willy Tarreau | d137353 | 2022-05-27 11:00:59 +0200 | [diff] [blame] | 4381 | .get_first_sc = h1_get_first_sc, |
Christopher Faulet | 3f612f7 | 2021-02-05 16:44:21 +0100 | [diff] [blame] | 4382 | .detach = h1_detach, |
| 4383 | .destroy = h1_destroy, |
| 4384 | .avail_streams = h1_avail_streams, |
| 4385 | .used_streams = h1_used_streams, |
| 4386 | .rcv_buf = h1_rcv_buf, |
| 4387 | .snd_buf = h1_snd_buf, |
| 4388 | #if defined(USE_LINUX_SPLICE) |
| 4389 | .rcv_pipe = h1_rcv_pipe, |
| 4390 | .snd_pipe = h1_snd_pipe, |
| 4391 | #endif |
| 4392 | .subscribe = h1_subscribe, |
| 4393 | .unsubscribe = h1_unsubscribe, |
| 4394 | .shutr = h1_shutr, |
| 4395 | .shutw = h1_shutw, |
| 4396 | .show_fd = h1_show_fd, |
Willy Tarreau | e6f389d | 2022-09-02 16:32:31 +0200 | [diff] [blame] | 4397 | .show_sd = h1_show_sd, |
Christopher Faulet | 3f612f7 | 2021-02-05 16:44:21 +0100 | [diff] [blame] | 4398 | .ctl = h1_ctl, |
| 4399 | .takeover = h1_takeover, |
| 4400 | .flags = MX_FL_HTX|MX_FL_NO_UPG, |
| 4401 | .name = "H1", |
| 4402 | }; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4403 | |
Christopher Faulet | 3f612f7 | 2021-02-05 16:44:21 +0100 | [diff] [blame] | 4404 | /* this mux registers default HTX proto but also h1 proto (to be referenced in the conf */ |
| 4405 | static struct mux_proto_list mux_proto_h1 = |
| 4406 | { .token = IST("h1"), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_h1_ops }; |
| 4407 | static struct mux_proto_list mux_proto_http = |
| 4408 | { .token = IST(""), .mode = PROTO_MODE_HTTP, .side = PROTO_SIDE_BOTH, .mux = &mux_http_ops }; |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4409 | |
Christopher Faulet | 3f612f7 | 2021-02-05 16:44:21 +0100 | [diff] [blame] | 4410 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_h1); |
| 4411 | INITCALL1(STG_REGISTER, register_mux_proto, &mux_proto_http); |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4412 | |
Christopher Faulet | 51dbc94 | 2018-09-13 09:05:15 +0200 | [diff] [blame] | 4413 | /* |
| 4414 | * Local variables: |
| 4415 | * c-indent-level: 8 |
| 4416 | * c-basic-offset: 8 |
| 4417 | * End: |
| 4418 | */ |